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
61 static void g_mencoder_class_init (GMencoderClass *klass);
62 static void g_mencoder_init (GMencoder *object);
63 static void g_mencoder_dispose (GObject *object);
64 static void g_mencoder_finalize (GObject *object);
66 _create_audio_bin (const gchar* encode,
70 _create_video_bin (const gchar* encode,
78 _pipeline_bus_cb (GstBus *bus,
81 static void _decodebin_new_pad_cb (GstElement* object,
85 static void _decodebin_unknown_type_cb (GstElement* object,
89 static void _close_output (GMencoder *self);
90 static void _open_output (GMencoder *self,
92 static GstElement* _create_source (const gchar* uri);
93 static GstElement*_create_pipeline (GMencoder *self,
94 const gchar* video_encode,
95 const gchar* mux_name,
96 gchar** video_encode_prop,
101 const gchar* audio_encode,
102 gchar** audio_encode_prop,
104 static gboolean _tick_cb (gpointer data);
111 static guint g_mencoder_signals[LAST_SIGNAL] = { 0 };
113 G_DEFINE_TYPE(GMencoder, g_mencoder, G_TYPE_OBJECT)
116 g_mencoder_class_init (GMencoderClass *klass)
118 GObjectClass *object_class;
120 object_class = (GObjectClass *) klass;
122 g_type_class_add_private (klass, sizeof (GMencoderPrivate));
124 object_class->dispose = g_mencoder_dispose;
125 object_class->finalize = g_mencoder_finalize;
127 g_mencoder_signals[PAUSED] =
128 g_signal_new ("paused",
129 G_OBJECT_CLASS_TYPE (object_class),
132 g_cclosure_marshal_VOID__VOID,
135 g_mencoder_signals[PLAYING] =
136 g_signal_new ("playing",
137 G_OBJECT_CLASS_TYPE (object_class),
140 g_cclosure_marshal_VOID__VOID,
143 g_mencoder_signals[STOPED] =
144 g_signal_new ("stoped",
145 G_OBJECT_CLASS_TYPE (object_class),
148 g_cclosure_marshal_VOID__VOID,
151 g_mencoder_signals[EOS] =
153 G_OBJECT_CLASS_TYPE (object_class),
156 g_cclosure_marshal_VOID__VOID,
160 g_mencoder_signals[ERROR] =
161 g_signal_new ("error",
162 G_OBJECT_CLASS_TYPE (object_class),
165 g_cclosure_marshal_VOID__STRING,
166 G_TYPE_NONE, 1, G_TYPE_STRING);
170 g_mencoder_init (GMencoder *self)
172 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
173 priv->info = g_new0 (SetupInfo, 1);
177 g_mencoder_dispose (GObject *object)
182 g_mencoder_finalize (GObject *object)
185 g_mencoder_close_stream (G_MENCODER (object));
189 g_mencoder_new (void)
191 return g_object_new (G_TYPE_MENCODER, NULL);
196 _obj_set_prop (GObject *obj,
197 const gchar *prop_name,
198 const gchar *prop_val)
202 GParamSpec *s = NULL;
203 GObjectClass *k = G_OBJECT_GET_CLASS (obj);
206 g_value_init (&v, G_TYPE_STRING);
207 g_value_set_string (&v, prop_val);
209 s = g_object_class_find_property (k, prop_name);
211 g_print ("Invalid property name: %s\n", prop_name);
215 g_value_init (&p, s->value_type);
216 switch (s->value_type)
219 g_value_set_int (&p, atoi (prop_val));
222 g_value_set_string (&p, prop_val);
228 g_object_set_property (obj, prop_name, &p);
234 _create_element_with_prop (const gchar* factory_name,
235 const gchar* element_name,
241 g_debug ("Creating element: %s", factory_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 gst_bin_add (GST_BIN (priv->pipe), src);
526 #ifdef SUPPORT_MULT_INPUT
527 ap = gst_bin_get_by_name (GST_BIN (priv->pipe), "ap");
528 vp = gst_bin_get_by_name (GST_BIN (priv->pipe), "vp");
530 ap = gst_bin_get_by_name (GST_BIN (priv->pipe), "abin");
531 vp = gst_bin_get_by_name (GST_BIN (priv->pipe), "vbin");
534 if ((vp == NULL) || (ap == NULL)) {
535 g_warning ("Fail to get output bin");
539 pad_src = gst_element_get_pad (src, "src_audio");
540 pad_sink = gst_element_get_compatible_pad (ap,
542 gst_pad_get_caps (pad_src));
544 if ((pad_sink == NULL) || (pad_src == NULL))
547 GstPadLinkReturn lret = gst_pad_link (pad_src, pad_sink);
548 if (lret != GST_PAD_LINK_OK)
551 gst_object_unref (pad_src);
552 gst_object_unref (pad_sink);
554 pad_src = gst_element_get_pad (src, "src_video");
555 pad_sink = gst_element_get_compatible_pad (vp,
557 gst_pad_get_caps (pad_src));
559 if ((pad_src == NULL) || (pad_sink == NULL))
562 if (gst_pad_link (pad_src, pad_sink) != GST_PAD_LINK_OK) {
563 g_warning ("invalid source. video");
569 g_debug ("Uri: [%s] OK ", uri);
572 if ((src != NULL) && (ret == FALSE)) {
573 gst_bin_remove (GST_BIN (priv->pipe), src);
574 gst_object_unref (src);
578 gst_object_unref (ap);
581 gst_object_unref (vp);
584 gst_object_unref (pad_src);
586 if (pad_sink != NULL)
587 gst_object_unref (pad_sink);
595 g_mencoder_remove_uri (GMencoder *self,
598 // GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
603 g_mencoder_play_stream (GMencoder *self)
605 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
607 g_return_if_fail (priv->ready == FALSE);
609 gst_element_set_state (priv->pipe, GST_STATE_PLAYING);
611 priv->tick_id = g_timeout_add (500, _tick_cb, self);
615 g_mencoder_pause_stream (GMencoder *self)
617 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
619 g_return_if_fail (priv->ready == TRUE);
620 gst_element_set_state (priv->pipe, GST_STATE_PAUSED);
624 g_mencoder_close_stream (GMencoder *self)
627 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
629 g_debug ("g_mencoder_close_stream");
630 if (priv->tick_id != 0) {
631 g_source_remove (priv->tick_id);
635 if (priv->pipe != NULL) {
636 gst_element_set_state (priv->pipe, GST_STATE_NULL);
637 gst_object_unref (priv->pipe);
649 _create_pipeline (GMencoder *self,
650 const gchar* video_encode,
651 const gchar* mux_name,
652 gchar** video_encode_prop,
657 const gchar* audio_encode,
658 gchar** audio_encode_prop,
662 GstElement *pipe = NULL;
663 GstElement *sink = NULL;
664 GstElement *mux = NULL;
665 GstElement *abin = NULL;
666 GstElement *vbin = NULL;
667 GstPad *aux_pad = NULL;
668 GstPad *mux_pad = NULL;
669 #ifdef SUPPORT_MULT_INPUT
670 GstElement *ap = NULL;
671 GstElement *vp = NULL;
673 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
675 pipe = gst_pipeline_new ("pipe");
677 #ifdef SUPPORT_MULT_INPUT
678 ap = gst_element_factory_make ("concatmux", "ap");
679 vp = gst_element_factory_make ("concatmux", "vp");
680 gst_bin_add_many (GST_BIN (pipe), ap, vp, NULL);
683 mux = gst_element_factory_make ((mux_name ? mux_name : "ffmux_mpeg"), "mux");
688 sink = gst_element_factory_make ("fdsink", "sink");
692 g_object_set (G_OBJECT(sink),
696 abin = _create_audio_bin (audio_encode, audio_encode_prop, audio_rate);
700 vbin = _create_video_bin (video_encode, video_encode_prop, video_fps, video_rate, video_width, video_height);
705 gst_bin_add_many (GST_BIN (pipe), abin, vbin, mux, sink, NULL);
708 #ifdef SUPPORT_MULT_INPUT
709 if (gst_element_link (ap, abin) == FALSE) {
710 g_warning ("Fail to link concat and abin");
714 if (gst_element_link (vp, vbin) == FALSE) {
715 g_warning ("Fail to link concat and vbin");
720 aux_pad = gst_element_get_pad (abin, "src");
721 mux_pad = gst_element_get_compatible_pad (mux, aux_pad, GST_PAD_CAPS (aux_pad));
722 if (mux_pad == NULL) {
723 g_warning ("Mux element no have audio PAD");
726 GstPadLinkReturn ret = gst_pad_link (aux_pad, mux_pad);
727 if (ret != GST_PAD_LINK_OK) {
728 g_warning ("Fail link audio and mux: %d", ret);
732 gst_object_unref (aux_pad);
733 gst_object_unref (mux_pad);
735 aux_pad = gst_element_get_pad (vbin, "src");
736 mux_pad = gst_element_get_compatible_pad (mux, aux_pad, GST_PAD_CAPS (aux_pad));
737 if (mux_pad == NULL) {
738 g_warning ("Mux element no have video PAD");
741 ret = gst_pad_link (aux_pad, mux_pad);
742 if (ret != GST_PAD_LINK_OK) {
743 g_warning ("Fail link video and mux: %d", ret);
746 gst_object_unref (aux_pad);
747 gst_object_unref (mux_pad);
752 gst_element_link (mux, sink);
754 bus = gst_pipeline_get_bus (GST_PIPELINE (pipe));
755 gst_bus_add_watch (bus, _pipeline_bus_cb, self);
756 gst_object_unref (bus);
760 g_warning ("Invalid uri");
763 gst_object_unref (pipe);
768 gst_object_unref (mux);
771 if (mux_pad != NULL) {
772 gst_object_unref (mux_pad);
775 if (aux_pad != NULL) {
776 gst_object_unref (mux_pad);
780 gst_object_unref (sink);
784 gst_object_unref (abin);
788 gst_object_unref (vbin);
796 _close_output (GMencoder *self)
802 _create_source (const gchar* uri)
813 bsrc = gst_bin_new (NULL);
815 //src = gst_element_factory_make ("gnomevfssrc", "src");
816 //g_object_set (G_OBJECT (src), "location", uri, NULL);
817 src = gst_element_make_from_uri (GST_URI_SRC, uri, "src");
821 decode = gst_element_factory_make ("decodebin2", "decode");
825 aqueue = gst_element_factory_make ("queue", "aqueue");
829 vqueue = gst_element_factory_make ("queue", "vqueue");
833 gst_bin_add_many (GST_BIN (bsrc), src, decode, aqueue, vqueue, NULL);
834 gst_element_link (src, decode);
836 g_signal_connect (G_OBJECT (decode),
838 G_CALLBACK (_decodebin_new_pad_cb),
841 g_signal_connect (G_OBJECT (decode),
843 G_CALLBACK (_decodebin_unknown_type_cb),
846 src_pad = gst_element_get_pad (aqueue, "src");
847 gst_element_add_pad (bsrc, gst_ghost_pad_new("src_audio", src_pad));
848 gst_object_unref (src_pad);
850 src_pad = gst_element_get_pad (vqueue, "src");
851 gst_element_add_pad (bsrc, gst_ghost_pad_new("src_video", src_pad));
852 gst_object_unref (src_pad);
858 gst_object_unref (src);
861 if (decode != NULL) {
862 gst_object_unref (decode);
865 if (aqueue != NULL) {
866 gst_object_unref (aqueue);
869 if (vqueue != NULL) {
870 gst_object_unref (vqueue);
877 _open_output (GMencoder *self,
881 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
883 i = g_strsplit (uri, "://", 0);
884 if (strcmp (i[0], "fd") == 0) {
885 priv->fd = atoi (i[1]);
886 } else if (strcmp (i[0], "file") == 0) {
887 priv->fd = open (i[1], O_WRONLY | O_CREAT | O_TRUNC);
889 g_warning ("Output uri not supported");
896 _pipeline_bus_cb (GstBus *bus,
900 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (user_data);
902 switch (GST_MESSAGE_TYPE (msg))
904 case GST_MESSAGE_STATE_CHANGED:
908 GstState pendingstate;
911 gst_message_parse_state_changed (msg, &oldstate,
912 &newstate, &pendingstate);
914 if (pendingstate != GST_STATE_VOID_PENDING)
917 if ((oldstate == GST_STATE_READY) &&
918 (newstate == GST_STATE_PAUSED)) {
920 g_signal_emit (user_data, g_mencoder_signals[PAUSED], 0);
921 } else if ((oldstate == GST_STATE_PAUSED) &&
922 (newstate == GST_STATE_PLAYING)) {
923 g_signal_emit (user_data, g_mencoder_signals[PLAYING], 0);
924 } else if ((oldstate == GST_STATE_READY) &&
925 (newstate == GST_STATE_NULL)) {
926 g_signal_emit (user_data, g_mencoder_signals[STOPED], 0);
930 case GST_MESSAGE_ERROR:
936 gst_message_parse_error (msg, &error, &debug);
937 err_str = g_strdup_printf ("Error [%d] %s (%s)", error->code,
940 g_signal_emit (user_data, g_mencoder_signals[ERROR], 0, err_str);
943 g_clear_error (&error);
948 case GST_MESSAGE_EOS:
950 g_signal_emit (user_data, g_mencoder_signals[EOS], 0);
964 _decodebin_new_pad_cb (GstElement* object,
970 gchar *str_caps = NULL;
971 GstElement *sink_element;
974 caps = gst_pad_get_caps (pad);
975 str_caps = gst_caps_to_string (caps);
976 g_debug ("New pad : %s", str_caps);
977 if (strstr (str_caps, "audio") != NULL) {
978 sink_element = gst_bin_get_by_name (GST_BIN (user_data), "aqueue");
979 } else if (strstr (str_caps, "video") != NULL) {
980 sink_element = gst_bin_get_by_name (GST_BIN (user_data), "vqueue");
982 g_warning ("invalid caps %s", str_caps);
985 sink_pad = gst_element_get_pad (sink_element, "sink");
986 gst_pad_link (pad, sink_pad);
988 gst_object_unref (sink_element);
989 gst_object_unref (sink_pad);
991 gst_caps_unref (caps);
995 _decodebin_unknown_type_cb (GstElement* object,
1000 g_warning ("Unknown Type");
1001 //priv->ready = FALSE;
1005 _tick_cb (gpointer user_data)
1007 GstFormat format = GST_FORMAT_TIME;
1009 gint64 duration = 0;
1011 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (user_data);
1013 if (gst_element_query_duration (priv->pipe, &format, &duration)) {
1014 gst_element_query_position (priv->pipe, &format, &cur);
1015 g_print ("PROGRESS:%lli%\n", (100 * cur) / duration);