12 #include "gmencoder.h"
14 #define G_MENCODER_GET_PRIVATE(obj) \
15 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), G_TYPE_MENCODER, GMencoderPrivate))
17 //#define SUPPORT_MULT_INPUT 0
19 typedef struct _GMencoderPrivate GMencoderPrivate;
20 typedef struct _SetupInfo SetupInfo;
26 gchar** video_encode_prop;
32 gchar** audio_encode_prop;
37 struct _GMencoderPrivate
63 static void g_mencoder_class_init (GMencoderClass *klass);
64 static void g_mencoder_init (GMencoder *object);
65 static void g_mencoder_dispose (GObject *object);
66 static void g_mencoder_finalize (GObject *object);
68 _create_audio_bin (const gchar* encode,
72 _create_video_bin (const gchar* encode,
80 _pipeline_bus_cb (GstBus *bus,
83 static void _decodebin_new_pad_cb (GstElement* object,
87 static void _decodebin_unknown_type_cb (GstElement* object,
91 static void _close_output (GMencoder *self);
92 static void _open_output (GMencoder *self,
94 static GstElement* _create_source (const gchar* uri);
95 static GstElement*_create_pipeline (GMencoder *self,
96 const gchar* video_encode,
97 const gchar* mux_name,
98 gchar** video_encode_prop,
103 const gchar* audio_encode,
104 gchar** audio_encode_prop,
106 static gboolean _tick_cb (gpointer data);
113 static guint g_mencoder_signals[LAST_SIGNAL] = { 0 };
115 G_DEFINE_TYPE(GMencoder, g_mencoder, G_TYPE_OBJECT)
118 g_mencoder_class_init (GMencoderClass *klass)
120 GObjectClass *object_class;
122 object_class = (GObjectClass *) klass;
124 g_type_class_add_private (klass, sizeof (GMencoderPrivate));
126 object_class->dispose = g_mencoder_dispose;
127 object_class->finalize = g_mencoder_finalize;
129 g_mencoder_signals[PAUSED] =
130 g_signal_new ("paused",
131 G_OBJECT_CLASS_TYPE (object_class),
134 g_cclosure_marshal_VOID__VOID,
137 g_mencoder_signals[PLAYING] =
138 g_signal_new ("playing",
139 G_OBJECT_CLASS_TYPE (object_class),
142 g_cclosure_marshal_VOID__VOID,
145 g_mencoder_signals[STOPED] =
146 g_signal_new ("stoped",
147 G_OBJECT_CLASS_TYPE (object_class),
150 g_cclosure_marshal_VOID__VOID,
153 g_mencoder_signals[EOS] =
155 G_OBJECT_CLASS_TYPE (object_class),
158 g_cclosure_marshal_VOID__VOID,
162 g_mencoder_signals[ERROR] =
163 g_signal_new ("error",
164 G_OBJECT_CLASS_TYPE (object_class),
167 g_cclosure_marshal_VOID__STRING,
168 G_TYPE_NONE, 1, G_TYPE_STRING);
172 g_mencoder_init (GMencoder *self)
174 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
175 priv->info = g_new0 (SetupInfo, 1);
179 g_mencoder_dispose (GObject *object)
184 g_mencoder_finalize (GObject *object)
187 g_mencoder_close_stream (G_MENCODER (object));
191 g_mencoder_new (void)
193 return g_object_new (G_TYPE_MENCODER, NULL);
198 _obj_set_prop (GObject *obj,
199 const gchar *prop_name,
200 const gchar *prop_val)
204 GParamSpec *s = NULL;
205 GObjectClass *k = G_OBJECT_GET_CLASS (obj);
208 g_value_init (&v, G_TYPE_STRING);
209 g_value_set_string (&v, prop_val);
211 s = g_object_class_find_property (k, prop_name);
213 g_print ("Invalid property name: %s\n", prop_name);
217 g_value_init (&p, s->value_type);
218 switch (s->value_type)
221 g_value_set_int (&p, atoi (prop_val));
224 g_value_set_string (&p, prop_val);
230 g_object_set_property (obj, prop_name, &p);
236 _create_element_with_prop (const gchar* factory_name,
237 const gchar* element_name,
243 ret = gst_element_factory_make (factory_name, element_name);
248 for (i=0; i < g_strv_length (prop); i++) {
249 char** v = g_strsplit(prop[i], "=", 2);
250 if (g_strv_length (v) == 2) {
251 _obj_set_prop (G_OBJECT (ret), v[0], v[1]);
262 _create_audio_bin (const gchar* encode,
266 GstElement *abin = NULL;
267 GstElement *aqueue = NULL;
268 GstElement *aconvert = NULL;
269 GstElement *aencode = NULL;
270 GstElement *aqueue_src = NULL;
273 //audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay ! udpsink name=upd_audio host=224.0.0.1 port=5002
274 abin = gst_bin_new ("abin");
275 aqueue = gst_element_factory_make ("queue", "aqueue");
276 aconvert= gst_element_factory_make ("audioconvert", "aconvert");
277 aencode = _create_element_with_prop ((encode ? encode : "lame"), "aencode", encode_prop);
278 aqueue_src= gst_element_factory_make ("queue", "aqueue_src");
280 if ((abin == NULL) || (aqueue == NULL) || (aconvert == NULL)
281 || (aencode == NULL) || (aqueue_src == NULL)) {
282 g_warning ("Audio elements not found");
286 g_object_set (G_OBJECT (aencode), "bitrate", 32, NULL);
289 g_object_set (G_OBJECT (aencode), "bitrate", 32, NULL);
293 gst_bin_add_many (GST_BIN (abin), aqueue, aconvert, aencode, aqueue_src, NULL);
294 if (gst_element_link_many (aqueue, aconvert, aencode, aqueue_src, NULL) == FALSE) {
295 g_warning ("Not Link audio elements");
298 //TODO: apply audio rate
300 // ghost pad the audio bin
301 apad = gst_element_get_pad (aqueue, "sink");
302 gst_element_add_pad (abin, gst_ghost_pad_new("sink", apad));
303 gst_object_unref (apad);
305 apad = gst_element_get_pad (aqueue_src, "src");
306 gst_element_add_pad (abin, gst_ghost_pad_new("src", apad));
307 gst_object_unref (apad);
312 gst_object_unref (abin);
315 gst_object_unref (aqueue);
317 if (aconvert != NULL)
318 gst_object_unref (aconvert);
321 gst_object_unref (aencode);
323 if (aqueue_src != NULL)
324 gst_object_unref (aqueue_src);
327 gst_object_unref (apad);
335 //queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! colorspace ! rate ! encode ! queue
337 _create_video_bin (const gchar* encode,
344 GstElement *vbin = NULL;
345 GstElement *vqueue = NULL;
346 GstElement* vqueue_src = NULL;
347 GstElement *vcolorspace = NULL;
348 GstElement *vencode = NULL;
349 GstElement *vrate = NULL;
352 vbin = gst_bin_new ("vbin");
353 vqueue = gst_element_factory_make ("queue", "vqueue");
354 vcolorspace = gst_element_factory_make ("ffmpegcolorspace", "colorspace");
356 vencode = _create_element_with_prop (
357 (encode != NULL ? encode : "ffenc_mpeg1video"),
358 "vencode", encode_prop);
359 vqueue_src = gst_element_factory_make ("queue", "queue_src");
361 if ((vbin == NULL) || (vqueue == NULL) || (vcolorspace == NULL)
362 || (vencode == NULL) || (vqueue_src == NULL)) {
363 g_warning ("Video elements not found");
367 gst_bin_add_many (GST_BIN (vbin), vqueue, vcolorspace, vencode, vqueue_src, NULL);
370 if ((width > 0) && (height > 0)) {
373 GstElement *vscale = gst_element_factory_make ("videoscale", "vscale");
375 gst_bin_add (GST_BIN (vbin), vscale);
377 vcaps = gst_caps_new_simple ("video/x-raw-yuv",
378 "width", G_TYPE_INT, width,
379 "height", G_TYPE_INT, height,
382 gst_element_link (vqueue, vscale);
384 if (gst_element_link_filtered (vscale, vcolorspace, vcaps) == FALSE) {
385 g_warning ("Fail to resize video");
386 gst_object_unref (vcaps);
387 gst_object_unref (vscale);
390 gst_caps_unref (vcaps);
392 gst_element_link (vqueue, vcolorspace);
397 //Changing the video fps
399 vrate = gst_element_factory_make ("videorate", "vrate");
401 g_debug ("Setting FPS: %.2f", fps);
402 gst_bin_add (GST_BIN (vbin), vrate);
404 if (gst_element_link (vcolorspace, vrate) == FALSE) {
405 g_warning ("Fail to link video elements");
409 vcaps = gst_caps_new_simple ("video/x-raw-yuv",
410 "framerate", GST_TYPE_FRACTION, (int) (fps * 1000), 1000, NULL);
412 if (gst_element_link_filtered (vrate, vencode, vcaps) == FALSE) {
413 g_warning ("Fail to link vrate with vencode.");
416 gst_caps_unref (vcaps);
418 if (gst_element_link (vcolorspace, vencode) == FALSE) {
419 g_warning ("Fail to link colorspace and video encode element.");
424 gst_element_link (vencode, vqueue_src);
426 // ghost pad the video bin
427 vpad = gst_element_get_pad (vqueue, "sink");
428 gst_element_add_pad (vbin, gst_ghost_pad_new ("sink", vpad));
429 gst_object_unref (vpad);
431 vpad = gst_element_get_pad (vqueue_src, "src");
432 gst_element_add_pad (vbin, gst_ghost_pad_new ("src", vpad));
433 gst_object_unref (vpad);
439 gst_object_unref (vpad);
442 gst_object_unref (vbin);
445 gst_object_unref (vqueue);
448 gst_object_unref (vencode);
450 if (vqueue_src != NULL)
451 gst_object_unref (vqueue_src);
453 if (vcolorspace != NULL)
454 gst_object_unref (vcolorspace);
462 g_mencoder_setup_stream (GMencoder *self,
463 const gchar* mux_name,
464 const gchar* video_encode,
465 gchar** video_encode_prop,
470 const gchar* audio_encode,
471 gchar** audio_encode_prop,
473 const gchar* out_uri)
475 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
476 if (priv->ready == TRUE) {
477 g_warning ("Stream already configured. You need close stream first.");
481 _close_output (self);
482 _open_output (self, out_uri);
485 priv->pipe = _create_pipeline (self,
501 g_mencoder_append_uri (GMencoder *self,
507 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
508 gboolean ret = FALSE;
509 GstElement *ap = NULL;
510 GstElement *vp = NULL;
513 g_return_val_if_fail (priv->pipe != NULL, FALSE);
514 g_return_val_if_fail (priv->ready == FALSE, FALSE);
516 #ifndef SUPPORT_MULT_INPUT
517 g_return_val_if_fail (priv->sources < 1, FALSE);
520 src = _create_source (uri);
524 priv->src = gst_bin_get_by_name (GST_BIN (src), "src");
526 gst_bin_add (GST_BIN (priv->pipe), src);
528 #ifdef SUPPORT_MULT_INPUT
529 ap = gst_bin_get_by_name (GST_BIN (priv->pipe), "ap");
530 vp = gst_bin_get_by_name (GST_BIN (priv->pipe), "vp");
532 ap = gst_bin_get_by_name (GST_BIN (priv->pipe), "abin");
533 vp = gst_bin_get_by_name (GST_BIN (priv->pipe), "vbin");
536 if ((vp == NULL) || (ap == NULL)) {
537 g_warning ("Fail to get output bin");
541 pad_src = gst_element_get_pad (src, "src_audio");
542 pad_sink = gst_element_get_compatible_pad (ap,
544 gst_pad_get_caps (pad_src));
546 if ((pad_sink == NULL) || (pad_src == NULL))
549 GstPadLinkReturn lret = gst_pad_link (pad_src, pad_sink);
550 if (lret != GST_PAD_LINK_OK)
553 gst_object_unref (pad_src);
554 gst_object_unref (pad_sink);
556 pad_src = gst_element_get_pad (src, "src_video");
557 pad_sink = gst_element_get_compatible_pad (vp,
559 gst_pad_get_caps (pad_src));
561 if ((pad_src == NULL) || (pad_sink == NULL))
564 if (gst_pad_link (pad_src, pad_sink) != GST_PAD_LINK_OK) {
565 g_warning ("invalid source. video");
571 g_debug ("Uri: [%s] OK ", uri);
574 if ((src != NULL) && (ret == FALSE)) {
575 gst_bin_remove (GST_BIN (priv->pipe), src);
576 gst_object_unref (src);
580 gst_object_unref (ap);
583 gst_object_unref (vp);
586 gst_object_unref (pad_src);
588 if (pad_sink != NULL)
589 gst_object_unref (pad_sink);
597 g_mencoder_remove_uri (GMencoder *self,
600 // GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
605 g_mencoder_play_stream (GMencoder *self)
607 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
609 g_return_if_fail (priv->ready == FALSE);
611 gst_element_set_state (priv->pipe, GST_STATE_PLAYING);
613 priv->tick_id = g_timeout_add (500, _tick_cb, self);
617 g_mencoder_pause_stream (GMencoder *self)
619 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
621 g_return_if_fail (priv->ready == TRUE);
622 gst_element_set_state (priv->pipe, GST_STATE_PAUSED);
626 g_mencoder_close_stream (GMencoder *self)
629 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
631 g_debug ("g_mencoder_close_stream");
632 if (priv->tick_id != 0) {
633 g_source_remove (priv->tick_id);
637 if (priv->pipe != NULL) {
638 gst_element_set_state (priv->pipe, GST_STATE_NULL);
639 gst_object_unref (priv->pipe);
651 _create_pipeline (GMencoder *self,
652 const gchar* video_encode,
653 const gchar* mux_name,
654 gchar** video_encode_prop,
659 const gchar* audio_encode,
660 gchar** audio_encode_prop,
664 GstElement *pipe = NULL;
665 GstElement *sink = NULL;
666 GstElement *mux = NULL;
667 GstElement *abin = NULL;
668 GstElement *vbin = NULL;
669 GstPad *aux_pad = NULL;
670 GstPad *mux_pad = NULL;
671 #ifdef SUPPORT_MULT_INPUT
672 GstElement *ap = NULL;
673 GstElement *vp = NULL;
675 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
677 pipe = gst_pipeline_new ("pipe");
679 #ifdef SUPPORT_MULT_INPUT
680 ap = gst_element_factory_make ("concatmux", "ap");
681 vp = gst_element_factory_make ("concatmux", "vp");
682 gst_bin_add_many (GST_BIN (pipe), ap, vp, NULL);
685 mux = gst_element_factory_make ((mux_name ? mux_name : "ffmux_mpeg"), "mux");
690 sink = gst_element_factory_make ("fdsink", "sink");
694 g_object_set (G_OBJECT(sink),
698 abin = _create_audio_bin (audio_encode, audio_encode_prop, audio_rate);
702 vbin = _create_video_bin (video_encode, video_encode_prop, video_fps, video_rate, video_width, video_height);
707 gst_bin_add_many (GST_BIN (pipe), abin, vbin, mux, sink, NULL);
710 #ifdef SUPPORT_MULT_INPUT
711 if (gst_element_link (ap, abin) == FALSE) {
712 g_warning ("Fail to link concat and abin");
716 if (gst_element_link (vp, vbin) == FALSE) {
717 g_warning ("Fail to link concat and vbin");
722 aux_pad = gst_element_get_pad (abin, "src");
723 mux_pad = gst_element_get_compatible_pad (mux, aux_pad, GST_PAD_CAPS (aux_pad));
724 if (mux_pad == NULL) {
725 g_warning ("Mux element no have audio PAD");
728 GstPadLinkReturn ret = gst_pad_link (aux_pad, mux_pad);
729 if (ret != GST_PAD_LINK_OK) {
730 g_warning ("Fail link audio and mux: %d", ret);
734 gst_object_unref (aux_pad);
735 gst_object_unref (mux_pad);
737 aux_pad = gst_element_get_pad (vbin, "src");
738 mux_pad = gst_element_get_compatible_pad (mux, aux_pad, GST_PAD_CAPS (aux_pad));
739 if (mux_pad == NULL) {
740 g_warning ("Mux element no have video PAD");
743 ret = gst_pad_link (aux_pad, mux_pad);
744 if (ret != GST_PAD_LINK_OK) {
745 g_warning ("Fail link video and mux: %d", ret);
748 gst_object_unref (aux_pad);
749 gst_object_unref (mux_pad);
754 gst_element_link (mux, sink);
756 bus = gst_pipeline_get_bus (GST_PIPELINE (pipe));
757 gst_bus_add_watch (bus, _pipeline_bus_cb, self);
758 gst_object_unref (bus);
762 g_warning ("Invalid uri");
765 gst_object_unref (pipe);
770 gst_object_unref (mux);
773 if (mux_pad != NULL) {
774 gst_object_unref (mux_pad);
777 if (aux_pad != NULL) {
778 gst_object_unref (mux_pad);
782 gst_object_unref (sink);
786 gst_object_unref (abin);
790 gst_object_unref (vbin);
798 _close_output (GMencoder *self)
804 _create_source (const gchar* uri)
807 GstElement *bsrc = NULL;
808 GstElement *src = NULL;
809 GstElement *aqueue = NULL;
810 GstElement *vqueue = NULL;
811 GstElement *decode = NULL;
812 GstPad *src_pad = NULL;
815 bsrc = gst_bin_new (NULL);
817 //src = gst_element_factory_make ("gnomevfssrc", "src");
818 //g_object_set (G_OBJECT (src), "location", uri, NULL);
819 src = gst_element_make_from_uri (GST_URI_SRC, uri, "src");
823 decode = gst_element_factory_make ("decodebin2", "decode");
827 aqueue = gst_element_factory_make ("queue", "aqueue");
831 vqueue = gst_element_factory_make ("queue", "vqueue");
835 gst_bin_add_many (GST_BIN (bsrc), src, decode, aqueue, vqueue, NULL);
836 gst_element_link (src, decode);
838 g_signal_connect (G_OBJECT (decode),
840 G_CALLBACK (_decodebin_new_pad_cb),
843 g_signal_connect (G_OBJECT (decode),
845 G_CALLBACK (_decodebin_unknown_type_cb),
848 src_pad = gst_element_get_pad (aqueue, "src");
849 gst_element_add_pad (bsrc, gst_ghost_pad_new("src_audio", src_pad));
850 gst_object_unref (src_pad);
852 src_pad = gst_element_get_pad (vqueue, "src");
853 gst_element_add_pad (bsrc, gst_ghost_pad_new("src_video", src_pad));
854 gst_object_unref (src_pad);
860 gst_object_unref (src);
863 if (decode != NULL) {
864 gst_object_unref (decode);
867 if (aqueue != NULL) {
868 gst_object_unref (aqueue);
871 if (vqueue != NULL) {
872 gst_object_unref (vqueue);
879 _open_output (GMencoder *self,
883 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
885 i = g_strsplit (uri, "://", 0);
886 if (strcmp (i[0], "fd") == 0) {
887 priv->fd = atoi (i[1]);
888 } else if (strcmp (i[0], "file") == 0) {
889 priv->fd = open (i[1], O_WRONLY | O_CREAT | O_TRUNC);
891 g_warning ("Output uri not supported");
898 _pipeline_bus_cb (GstBus *bus,
902 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (user_data);
904 switch (GST_MESSAGE_TYPE (msg))
906 case GST_MESSAGE_STATE_CHANGED:
910 GstState pendingstate;
913 gst_message_parse_state_changed (msg, &oldstate,
914 &newstate, &pendingstate);
916 if (pendingstate != GST_STATE_VOID_PENDING)
919 if ((oldstate == GST_STATE_READY) &&
920 (newstate == GST_STATE_PAUSED)) {
922 g_signal_emit (user_data, g_mencoder_signals[PAUSED], 0);
923 } else if ((oldstate == GST_STATE_PAUSED) &&
924 (newstate == GST_STATE_PLAYING)) {
925 g_signal_emit (user_data, g_mencoder_signals[PLAYING], 0);
926 } else if ((oldstate == GST_STATE_READY) &&
927 (newstate == GST_STATE_NULL)) {
928 g_signal_emit (user_data, g_mencoder_signals[STOPED], 0);
932 case GST_MESSAGE_ERROR:
938 gst_message_parse_error (msg, &error, &debug);
939 err_str = g_strdup_printf ("Error [%d] %s (%s)", error->code,
942 g_signal_emit (user_data, g_mencoder_signals[ERROR], 0, err_str);
945 g_clear_error (&error);
950 case GST_MESSAGE_EOS:
952 g_signal_emit (user_data, g_mencoder_signals[EOS], 0);
954 case GST_MESSAGE_DURATION:
958 gst_message_parse_duration (msg, &format, &duration);
959 if (format == GST_FORMAT_BYTES)
960 priv->duration = duration;
974 _decodebin_new_pad_cb (GstElement* object,
980 gchar *str_caps = NULL;
981 GstElement *sink_element;
984 caps = gst_pad_get_caps (pad);
985 str_caps = gst_caps_to_string (caps);
986 g_debug ("New pad : %s", str_caps);
987 if (strstr (str_caps, "audio") != NULL) {
988 sink_element = gst_bin_get_by_name (GST_BIN (user_data), "aqueue");
989 } else if (strstr (str_caps, "video") != NULL) {
990 sink_element = gst_bin_get_by_name (GST_BIN (user_data), "vqueue");
992 g_warning ("invalid caps %s", str_caps);
995 sink_pad = gst_element_get_pad (sink_element, "sink");
996 gst_pad_link (pad, sink_pad);
998 gst_object_unref (sink_element);
999 gst_object_unref (sink_pad);
1001 gst_caps_unref (caps);
1005 _decodebin_unknown_type_cb (GstElement* object,
1010 g_warning ("Unknown Type");
1011 //priv->ready = FALSE;
1015 _tick_cb (gpointer user_data)
1017 GstFormat format = GST_FORMAT_BYTES;
1020 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (user_data);
1022 if (priv->duration == 0) {
1024 if (gst_element_query_duration (priv->src, &format, &d))
1028 if (priv->duration != 0) {
1029 gst_element_query_position (priv->src, &format, &cur);
1030 g_print ("PROGRESS:%lli\n", (99 * cur) / priv->duration);