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 if (prop[i] != NULL) {
250 char** v = g_strsplit(prop[i], "=", 2);
251 if (g_strv_length (v) == 2) {
252 _obj_set_prop (G_OBJECT (ret), v[0], v[1]);
264 _create_audio_bin (const gchar* encode,
268 GstElement *abin = NULL;
269 GstElement *aqueue = NULL;
270 GstElement *aconvert = NULL;
271 GstElement *aencode = NULL;
272 GstElement *aqueue_src = NULL;
275 //audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay ! udpsink name=upd_audio host=224.0.0.1 port=5002
276 abin = gst_bin_new ("abin");
277 aqueue = gst_element_factory_make ("queue", "aqueue");
278 aconvert= gst_element_factory_make ("audioconvert", "aconvert");
279 aencode = _create_element_with_prop ((encode ? encode : "lame"), "aencode", encode_prop);
280 aqueue_src= gst_element_factory_make ("queue", "aqueue_src");
282 if ((abin == NULL) || (aqueue == NULL) || (aconvert == NULL)
283 || (aencode == NULL) || (aqueue_src == NULL)) {
284 g_warning ("Audio elements not found");
288 g_object_set (G_OBJECT (aencode), "bitrate", 32, NULL);
291 g_object_set (G_OBJECT (aencode), "bitrate", 32, NULL);
295 gst_bin_add_many (GST_BIN (abin), aqueue, aconvert, aencode, aqueue_src, NULL);
296 if (gst_element_link_many (aqueue, aconvert, aencode, aqueue_src, NULL) == FALSE) {
297 g_warning ("Not Link audio elements");
300 //TODO: apply audio rate
302 // ghost pad the audio bin
303 apad = gst_element_get_pad (aqueue, "sink");
304 gst_element_add_pad (abin, gst_ghost_pad_new("sink", apad));
305 gst_object_unref (apad);
307 apad = gst_element_get_pad (aqueue_src, "src");
308 gst_element_add_pad (abin, gst_ghost_pad_new("src", apad));
309 gst_object_unref (apad);
314 gst_object_unref (abin);
317 gst_object_unref (aqueue);
319 if (aconvert != NULL)
320 gst_object_unref (aconvert);
323 gst_object_unref (aencode);
325 if (aqueue_src != NULL)
326 gst_object_unref (aqueue_src);
329 gst_object_unref (apad);
337 //queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! colorspace ! rate ! encode ! queue
339 _create_video_bin (const gchar* encode,
346 GstElement *vbin = NULL;
347 GstElement *vqueue = NULL;
348 GstElement* vqueue_src = NULL;
349 GstElement *vcolorspace = NULL;
350 GstElement *vencode = NULL;
351 GstElement *vrate = NULL;
354 vbin = gst_bin_new ("vbin");
355 vqueue = gst_element_factory_make ("queue", "vqueue");
356 vcolorspace = gst_element_factory_make ("ffmpegcolorspace", "colorspace");
358 vencode = _create_element_with_prop (
359 (encode != NULL ? encode : "ffenc_mpeg1video"),
360 "vencode", encode_prop);
361 vqueue_src = gst_element_factory_make ("queue", "queue_src");
363 if ((vbin == NULL) || (vqueue == NULL) || (vcolorspace == NULL)
364 || (vencode == NULL) || (vqueue_src == NULL)) {
365 g_warning ("Video elements not found");
369 gst_bin_add_many (GST_BIN (vbin), vqueue, vcolorspace, vencode, vqueue_src, NULL);
372 if ((width > 0) && (height > 0)) {
375 GstElement *vscale = gst_element_factory_make ("videoscale", "vscale");
377 gst_bin_add (GST_BIN (vbin), vscale);
379 vcaps = gst_caps_new_simple ("video/x-raw-yuv",
380 "width", G_TYPE_INT, width,
381 "height", G_TYPE_INT, height,
384 gst_element_link (vqueue, vscale);
386 if (gst_element_link_filtered (vscale, vcolorspace, vcaps) == FALSE) {
387 g_warning ("Fail to resize video");
388 gst_object_unref (vcaps);
389 gst_object_unref (vscale);
392 gst_caps_unref (vcaps);
394 gst_element_link (vqueue, vcolorspace);
399 //Changing the video fps
401 vrate = gst_element_factory_make ("videorate", "vrate");
403 g_debug ("Setting FPS: %.2f", fps);
404 gst_bin_add (GST_BIN (vbin), vrate);
406 if (gst_element_link (vcolorspace, vrate) == FALSE) {
407 g_warning ("Fail to link video elements");
411 vcaps = gst_caps_new_simple ("video/x-raw-yuv",
412 "framerate", GST_TYPE_FRACTION, (int) (fps * 1000), 1000, NULL);
414 if (gst_element_link_filtered (vrate, vencode, vcaps) == FALSE) {
415 g_warning ("Fail to link vrate with vencode.");
418 gst_caps_unref (vcaps);
420 if (gst_element_link (vcolorspace, vencode) == FALSE) {
421 g_warning ("Fail to link colorspace and video encode element.");
426 gst_element_link (vencode, vqueue_src);
428 // ghost pad the video bin
429 vpad = gst_element_get_pad (vqueue, "sink");
430 gst_element_add_pad (vbin, gst_ghost_pad_new ("sink", vpad));
431 gst_object_unref (vpad);
433 vpad = gst_element_get_pad (vqueue_src, "src");
434 gst_element_add_pad (vbin, gst_ghost_pad_new ("src", vpad));
435 gst_object_unref (vpad);
441 gst_object_unref (vpad);
444 gst_object_unref (vbin);
447 gst_object_unref (vqueue);
450 gst_object_unref (vencode);
452 if (vqueue_src != NULL)
453 gst_object_unref (vqueue_src);
455 if (vcolorspace != NULL)
456 gst_object_unref (vcolorspace);
464 g_mencoder_setup_stream (GMencoder *self,
465 const gchar* mux_name,
466 const gchar* video_encode,
467 gchar** video_encode_prop,
472 const gchar* audio_encode,
473 gchar** audio_encode_prop,
475 const gchar* out_uri)
477 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
478 if (priv->ready == TRUE) {
479 g_warning ("Stream already configured. You need close stream first.");
483 _close_output (self);
484 _open_output (self, out_uri);
487 priv->pipe = _create_pipeline (self,
503 g_mencoder_append_uri (GMencoder *self,
509 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
510 gboolean ret = FALSE;
511 GstElement *ap = NULL;
512 GstElement *vp = NULL;
515 g_return_val_if_fail (priv->pipe != NULL, FALSE);
516 g_return_val_if_fail (priv->ready == FALSE, FALSE);
518 #ifndef SUPPORT_MULT_INPUT
519 g_return_val_if_fail (priv->sources < 1, FALSE);
522 src = _create_source (uri);
526 priv->src = gst_bin_get_by_name (GST_BIN (src), "src");
528 gst_bin_add (GST_BIN (priv->pipe), src);
530 #ifdef SUPPORT_MULT_INPUT
531 ap = gst_bin_get_by_name (GST_BIN (priv->pipe), "ap");
532 vp = gst_bin_get_by_name (GST_BIN (priv->pipe), "vp");
534 ap = gst_bin_get_by_name (GST_BIN (priv->pipe), "abin");
535 vp = gst_bin_get_by_name (GST_BIN (priv->pipe), "vbin");
538 if ((vp == NULL) || (ap == NULL)) {
539 g_warning ("Fail to get output bin");
543 pad_src = gst_element_get_pad (src, "src_audio");
544 pad_sink = gst_element_get_compatible_pad (ap,
546 gst_pad_get_caps (pad_src));
548 if ((pad_sink == NULL) || (pad_src == NULL))
551 GstPadLinkReturn lret = gst_pad_link (pad_src, pad_sink);
552 if (lret != GST_PAD_LINK_OK)
555 gst_object_unref (pad_src);
556 gst_object_unref (pad_sink);
558 pad_src = gst_element_get_pad (src, "src_video");
559 pad_sink = gst_element_get_compatible_pad (vp,
561 gst_pad_get_caps (pad_src));
563 if ((pad_src == NULL) || (pad_sink == NULL))
566 if (gst_pad_link (pad_src, pad_sink) != GST_PAD_LINK_OK) {
567 g_warning ("invalid source. video");
573 g_debug ("Uri: [%s] OK ", uri);
576 if ((src != NULL) && (ret == FALSE)) {
577 gst_bin_remove (GST_BIN (priv->pipe), src);
578 gst_object_unref (src);
582 gst_object_unref (ap);
585 gst_object_unref (vp);
588 gst_object_unref (pad_src);
590 if (pad_sink != NULL)
591 gst_object_unref (pad_sink);
599 g_mencoder_remove_uri (GMencoder *self,
602 // GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
607 g_mencoder_play_stream (GMencoder *self)
609 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
611 g_return_if_fail (priv->ready == FALSE);
613 gst_element_set_state (priv->pipe, GST_STATE_PLAYING);
615 priv->tick_id = g_timeout_add (500, _tick_cb, self);
619 g_mencoder_pause_stream (GMencoder *self)
621 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
623 g_return_if_fail (priv->ready == TRUE);
624 gst_element_set_state (priv->pipe, GST_STATE_PAUSED);
628 g_mencoder_close_stream (GMencoder *self)
631 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
633 g_debug ("g_mencoder_close_stream");
634 if (priv->tick_id != 0) {
635 g_source_remove (priv->tick_id);
639 if (priv->pipe != NULL) {
642 //gst_element_set_state (priv->pipe, GST_STATE_NULL);
643 //gst_object_unref (priv->pipe);
644 gst_object_unref (priv->src);
655 _create_pipeline (GMencoder *self,
656 const gchar* video_encode,
657 const gchar* mux_name,
658 gchar** video_encode_prop,
663 const gchar* audio_encode,
664 gchar** audio_encode_prop,
668 GstElement *pipe = NULL;
669 GstElement *sink = NULL;
670 GstElement *mux = NULL;
671 GstElement *abin = NULL;
672 GstElement *vbin = NULL;
673 GstPad *aux_pad = NULL;
674 GstPad *mux_pad = NULL;
675 #ifdef SUPPORT_MULT_INPUT
676 GstElement *ap = NULL;
677 GstElement *vp = NULL;
679 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
681 pipe = gst_pipeline_new ("pipe");
683 #ifdef SUPPORT_MULT_INPUT
684 ap = gst_element_factory_make ("concatmux", "ap");
685 vp = gst_element_factory_make ("concatmux", "vp");
686 gst_bin_add_many (GST_BIN (pipe), ap, vp, NULL);
689 mux = gst_element_factory_make ((mux_name ? mux_name : "ffmux_mpeg"), "mux");
694 sink = gst_element_factory_make ("fdsink", "sink");
698 g_object_set (G_OBJECT(sink),
702 abin = _create_audio_bin (audio_encode, audio_encode_prop, audio_rate);
706 vbin = _create_video_bin (video_encode, video_encode_prop, video_fps, video_rate, video_width, video_height);
711 gst_bin_add_many (GST_BIN (pipe), abin, vbin, mux, sink, NULL);
714 #ifdef SUPPORT_MULT_INPUT
715 if (gst_element_link (ap, abin) == FALSE) {
716 g_warning ("Fail to link concat and abin");
720 if (gst_element_link (vp, vbin) == FALSE) {
721 g_warning ("Fail to link concat and vbin");
726 aux_pad = gst_element_get_pad (abin, "src");
727 mux_pad = gst_element_get_compatible_pad (mux, aux_pad, GST_PAD_CAPS (aux_pad));
728 if (mux_pad == NULL) {
729 g_warning ("Mux element no have audio PAD");
732 GstPadLinkReturn ret = gst_pad_link (aux_pad, mux_pad);
733 if (ret != GST_PAD_LINK_OK) {
734 g_warning ("Fail link audio and mux: %d", ret);
738 gst_object_unref (aux_pad);
739 gst_object_unref (mux_pad);
741 aux_pad = gst_element_get_pad (vbin, "src");
742 mux_pad = gst_element_get_compatible_pad (mux, aux_pad, GST_PAD_CAPS (aux_pad));
743 if (mux_pad == NULL) {
744 g_warning ("Mux element no have video PAD");
747 ret = gst_pad_link (aux_pad, mux_pad);
748 if (ret != GST_PAD_LINK_OK) {
749 g_warning ("Fail link video and mux: %d", ret);
752 gst_object_unref (aux_pad);
753 gst_object_unref (mux_pad);
758 gst_element_link (mux, sink);
760 bus = gst_pipeline_get_bus (GST_PIPELINE (pipe));
761 gst_bus_add_watch (bus, _pipeline_bus_cb, self);
762 gst_object_unref (bus);
766 g_warning ("Invalid uri");
769 gst_object_unref (pipe);
774 gst_object_unref (mux);
777 if (mux_pad != NULL) {
778 gst_object_unref (mux_pad);
781 if (aux_pad != NULL) {
782 gst_object_unref (mux_pad);
786 gst_object_unref (sink);
790 gst_object_unref (abin);
794 gst_object_unref (vbin);
802 _close_output (GMencoder *self)
808 _create_source (const gchar* uri)
811 GstElement *bsrc = NULL;
812 GstElement *src = NULL;
813 GstElement *aqueue = NULL;
814 GstElement *vqueue = NULL;
815 GstElement *decode = NULL;
816 GstPad *src_pad = NULL;
819 bsrc = gst_bin_new (NULL);
821 //src = gst_element_factory_make ("gnomevfssrc", "src");
822 //g_object_set (G_OBJECT (src), "location", uri, NULL);
823 src = gst_element_make_from_uri (GST_URI_SRC, uri, "src");
827 decode = gst_element_factory_make ("decodebin2", "decode");
831 aqueue = gst_element_factory_make ("queue", "aqueue");
835 vqueue = gst_element_factory_make ("queue", "vqueue");
839 gst_bin_add_many (GST_BIN (bsrc), src, decode, aqueue, vqueue, NULL);
840 gst_element_link (src, decode);
842 g_signal_connect (G_OBJECT (decode),
844 G_CALLBACK (_decodebin_new_pad_cb),
847 g_signal_connect (G_OBJECT (decode),
849 G_CALLBACK (_decodebin_unknown_type_cb),
852 src_pad = gst_element_get_pad (aqueue, "src");
853 gst_element_add_pad (bsrc, gst_ghost_pad_new("src_audio", src_pad));
854 gst_object_unref (src_pad);
856 src_pad = gst_element_get_pad (vqueue, "src");
857 gst_element_add_pad (bsrc, gst_ghost_pad_new("src_video", src_pad));
858 gst_object_unref (src_pad);
864 gst_object_unref (src);
867 if (decode != NULL) {
868 gst_object_unref (decode);
871 if (aqueue != NULL) {
872 gst_object_unref (aqueue);
875 if (vqueue != NULL) {
876 gst_object_unref (vqueue);
883 _open_output (GMencoder *self,
887 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
889 i = g_strsplit (uri, "://", 0);
890 if (strcmp (i[0], "fd") == 0) {
891 priv->fd = atoi (i[1]);
892 } else if (strcmp (i[0], "file") == 0) {
893 priv->fd = open (i[1], O_WRONLY | O_CREAT | O_TRUNC);
895 g_warning ("Output uri not supported");
902 _pipeline_bus_cb (GstBus *bus,
906 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (user_data);
908 switch (GST_MESSAGE_TYPE (msg))
910 case GST_MESSAGE_STATE_CHANGED:
914 GstState pendingstate;
917 gst_message_parse_state_changed (msg, &oldstate,
918 &newstate, &pendingstate);
920 if (pendingstate != GST_STATE_VOID_PENDING)
923 if ((oldstate == GST_STATE_READY) &&
924 (newstate == GST_STATE_PAUSED)) {
926 g_signal_emit (user_data, g_mencoder_signals[PAUSED], 0);
927 } else if ((oldstate == GST_STATE_PAUSED) &&
928 (newstate == GST_STATE_PLAYING)) {
929 g_signal_emit (user_data, g_mencoder_signals[PLAYING], 0);
930 } else if ((oldstate == GST_STATE_READY) &&
931 (newstate == GST_STATE_NULL)) {
932 g_signal_emit (user_data, g_mencoder_signals[STOPED], 0);
936 case GST_MESSAGE_ERROR:
942 if (priv->tick_id != 0) {
943 g_source_remove (priv->tick_id);
947 gst_message_parse_error (msg, &error, &debug);
948 err_str = g_strdup_printf ("Error [%d] %s (%s)", error->code,
951 g_debug ("%s", err_str);
953 g_signal_emit (user_data, g_mencoder_signals[ERROR], 0, err_str);
955 g_clear_error (&error);
957 g_debug ("ERROR DONE");
961 case GST_MESSAGE_EOS:
963 g_signal_emit (user_data, g_mencoder_signals[EOS], 0);
965 case GST_MESSAGE_DURATION:
969 gst_message_parse_duration (msg, &format, &duration);
970 if (format == GST_FORMAT_BYTES)
971 priv->duration = duration;
985 _decodebin_new_pad_cb (GstElement* object,
991 gchar *str_caps = NULL;
992 GstElement *sink_element;
995 caps = gst_pad_get_caps (pad);
996 str_caps = gst_caps_to_string (caps);
997 g_debug ("New pad : %s", str_caps);
998 if (strstr (str_caps, "audio") != NULL) {
999 sink_element = gst_bin_get_by_name (GST_BIN (user_data), "aqueue");
1000 } else if (strstr (str_caps, "video") != NULL) {
1001 sink_element = gst_bin_get_by_name (GST_BIN (user_data), "vqueue");
1003 g_warning ("invalid caps %s", str_caps);
1006 sink_pad = gst_element_get_pad (sink_element, "sink");
1007 gst_pad_link (pad, sink_pad);
1009 gst_object_unref (sink_element);
1010 gst_object_unref (sink_pad);
1012 gst_caps_unref (caps);
1016 _decodebin_unknown_type_cb (GstElement* object,
1021 g_warning ("Unknown Type");
1022 //priv->ready = FALSE;
1026 _tick_cb (gpointer user_data)
1028 GstFormat format = GST_FORMAT_BYTES;
1031 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (user_data);
1033 if (priv->duration == 0) {
1035 if (gst_element_query_duration (priv->src, &format, &d))
1039 if (priv->duration != 0) {
1040 gst_element_query_position (priv->src, &format, &cur);
1041 g_print ("PROGRESS:%lli\n", (99 * cur) / priv->duration);