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,
84 static void _decodebin_new_pad_cb (GstElement* object,
89 static void _decodebin_unknown_type_cb (GstElement* object,
94 static void _close_output (GMencoder *self);
95 static void _open_output (GMencoder *self,
98 static GstElement* _create_source (const gchar* uri);
99 static GstElement*_create_pipeline (GMencoder *self,
100 const gchar* video_encode,
101 const gchar* mux_name,
102 gchar** video_encode_prop,
107 const gchar* audio_encode,
108 gchar** audio_encode_prop,
111 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;
121 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,
159 g_mencoder_signals[ERROR] =
160 g_signal_new ("error",
161 G_OBJECT_CLASS_TYPE (object_class),
164 g_cclosure_marshal_VOID__STRING,
165 G_TYPE_NONE, 1, G_TYPE_STRING);
169 g_mencoder_init (GMencoder *self)
171 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
172 priv->info = g_new0 (SetupInfo, 1);
176 g_mencoder_dispose (GObject *object)
181 g_mencoder_finalize (GObject *object)
184 g_mencoder_close_stream (G_MENCODER (object));
188 g_mencoder_new (void)
190 return g_object_new (G_TYPE_MENCODER, NULL);
195 _obj_set_prop (GObject *obj,
196 const gchar *prop_name,
197 const gchar *prop_val)
201 GParamSpec *s = NULL;
202 GObjectClass *k = G_OBJECT_GET_CLASS (obj);
205 g_value_init (&v, G_TYPE_STRING);
206 g_value_set_string (&v, prop_val);
208 s = g_object_class_find_property (k, prop_name);
210 g_print ("Invalid property name: %s\n", prop_name);
214 g_value_init (&p, s->value_type);
215 switch (s->value_type)
218 g_value_set_int (&p, atoi (prop_val));
221 g_value_set_string (&p, prop_val);
227 g_object_set_property (obj, prop_name, &p);
233 _create_element_with_prop (const gchar* factory_name,
234 const gchar* element_name,
240 ret = gst_element_factory_make (factory_name, element_name);
245 for (i=0; i < g_strv_length (prop); i++) {
246 if (prop[i] != NULL) {
247 char** v = g_strsplit(prop[i], "=", 2);
248 if (g_strv_length (v) == 2) {
249 _obj_set_prop (G_OBJECT (ret), v[0], v[1]);
261 _create_audio_bin (const gchar* encode,
265 GstElement *abin = NULL;
266 GstElement *aqueue = NULL;
267 GstElement *aconvert = NULL;
268 GstElement *aencode = NULL;
269 GstElement *aqueue_src = NULL;
272 //audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay ! udpsink name=upd_audio host=224.0.0.1 port=5002
273 abin = gst_bin_new ("abin");
274 aqueue = gst_element_factory_make ("queue", "aqueue");
275 aconvert= gst_element_factory_make ("audioconvert", "aconvert");
276 aencode = _create_element_with_prop ((encode ? encode : "lame"), "aencode", encode_prop);
277 aqueue_src= gst_element_factory_make ("queue", "aqueue_src");
279 if ((abin == NULL) || (aqueue == NULL) || (aconvert == NULL)
280 || (aencode == NULL) || (aqueue_src == NULL)) {
281 g_warning ("Audio elements not found");
285 g_object_set (G_OBJECT (aencode), "bitrate", 32, NULL);
288 g_object_set (G_OBJECT (aencode), "bitrate", 32, NULL);
292 gst_bin_add_many (GST_BIN (abin), aqueue, aconvert, aencode, aqueue_src, NULL);
293 if (gst_element_link_many (aqueue, aconvert, aencode, aqueue_src, NULL) == FALSE) {
294 g_warning ("Not Link audio elements");
297 //TODO: apply audio rate
299 // ghost pad the audio bin
300 apad = gst_element_get_pad (aqueue, "sink");
301 gst_element_add_pad (abin, gst_ghost_pad_new("sink", apad));
302 gst_object_unref (apad);
304 apad = gst_element_get_pad (aqueue_src, "src");
305 gst_element_add_pad (abin, gst_ghost_pad_new("src", apad));
306 gst_object_unref (apad);
311 gst_object_unref (abin);
314 gst_object_unref (aqueue);
316 if (aconvert != NULL)
317 gst_object_unref (aconvert);
320 gst_object_unref (aencode);
322 if (aqueue_src != NULL)
323 gst_object_unref (aqueue_src);
326 gst_object_unref (apad);
334 //queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! colorspace ! rate ! encode ! queue
336 _create_video_bin (const gchar* encode,
343 GstElement *vbin = NULL;
344 GstElement *vqueue = NULL;
345 GstElement* vqueue_src = NULL;
346 GstElement *vcolorspace = NULL;
347 GstElement *vencode = NULL;
348 GstElement *vrate = NULL;
351 vbin = gst_bin_new ("vbin");
352 vqueue = gst_element_factory_make ("queue", "vqueue");
353 vcolorspace = gst_element_factory_make ("ffmpegcolorspace", "colorspace");
355 vencode = _create_element_with_prop (
356 (encode != NULL ? encode : "ffenc_mpeg1video"),
357 "vencode", encode_prop);
358 vqueue_src = gst_element_factory_make ("queue", "queue_src");
360 if ((vbin == NULL) || (vqueue == NULL) || (vcolorspace == NULL)
361 || (vencode == NULL) || (vqueue_src == NULL)) {
362 g_warning ("Video elements not found");
366 gst_bin_add_many (GST_BIN (vbin), vqueue, vcolorspace, vencode, vqueue_src, NULL);
368 if ((width > 0) && (height > 0)) {
371 GstElement *vscale = gst_element_factory_make ("videoscale", "vscale");
373 gst_bin_add (GST_BIN (vbin), vscale);
375 vcaps = gst_caps_new_simple ("video/x-raw-yuv",
376 "width", G_TYPE_INT, width,
377 "height", G_TYPE_INT, height,
380 gst_element_link (vqueue, vscale);
382 if (gst_element_link_filtered (vscale, vcolorspace, vcaps) == FALSE) {
383 g_warning ("Fail to resize video");
384 gst_object_unref (vcaps);
385 gst_object_unref (vscale);
388 gst_caps_unref (vcaps);
390 gst_element_link (vqueue, vcolorspace);
394 //Changing the video fps
396 vrate = gst_element_factory_make ("videorate", "vrate");
398 gst_bin_add (GST_BIN (vbin), vrate);
400 if (gst_element_link (vcolorspace, vrate) == FALSE) {
401 g_warning ("Fail to link video elements");
405 vcaps = gst_caps_new_simple ("video/x-raw-yuv",
406 "framerate", GST_TYPE_FRACTION, (int) (fps * 1000), 1000, NULL);
408 if (gst_element_link_filtered (vrate, vencode, vcaps) == FALSE) {
409 g_warning ("Fail to link vrate with vencode.");
412 gst_caps_unref (vcaps);
414 if (gst_element_link (vcolorspace, vencode) == FALSE) {
415 g_warning ("Fail to link colorspace and video encode element.");
420 gst_element_link (vencode, vqueue_src);
422 // ghost pad the video bin
423 vpad = gst_element_get_pad (vqueue, "sink");
424 gst_element_add_pad (vbin, gst_ghost_pad_new ("sink", vpad));
425 gst_object_unref (vpad);
427 vpad = gst_element_get_pad (vqueue_src, "src");
428 gst_element_add_pad (vbin, gst_ghost_pad_new ("src", vpad));
429 gst_object_unref (vpad);
435 gst_object_unref (vpad);
438 gst_object_unref (vbin);
441 gst_object_unref (vqueue);
444 gst_object_unref (vencode);
446 if (vqueue_src != NULL)
447 gst_object_unref (vqueue_src);
449 if (vcolorspace != NULL)
450 gst_object_unref (vcolorspace);
458 g_mencoder_setup_stream (GMencoder *self,
459 const gchar* mux_name,
460 const gchar* video_encode,
461 gchar** video_encode_prop,
466 const gchar* audio_encode,
467 gchar** audio_encode_prop,
469 const gchar* out_uri)
471 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
472 if (priv->ready == TRUE) {
473 g_warning ("Stream already configured. You need close stream first.");
477 _close_output (self);
478 _open_output (self, out_uri);
481 priv->pipe = _create_pipeline (self,
497 g_mencoder_append_uri (GMencoder *self,
503 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
504 gboolean ret = FALSE;
505 GstElement *ap = NULL;
506 GstElement *vp = NULL;
509 g_return_val_if_fail (priv->pipe != NULL, FALSE);
510 g_return_val_if_fail (priv->ready == FALSE, FALSE);
512 #ifndef SUPPORT_MULT_INPUT
513 g_return_val_if_fail (priv->sources < 1, FALSE);
516 src = _create_source (uri);
520 priv->src = gst_bin_get_by_name (GST_BIN (src), "src");
522 gst_bin_add (GST_BIN (priv->pipe), src);
524 #ifdef SUPPORT_MULT_INPUT
525 ap = gst_bin_get_by_name (GST_BIN (priv->pipe), "ap");
526 vp = gst_bin_get_by_name (GST_BIN (priv->pipe), "vp");
528 ap = gst_bin_get_by_name (GST_BIN (priv->pipe), "abin");
529 vp = gst_bin_get_by_name (GST_BIN (priv->pipe), "vbin");
532 if ((vp == NULL) || (ap == NULL)) {
533 g_warning ("Fail to get output bin");
537 pad_src = gst_element_get_pad (src, "src_audio");
538 pad_sink = gst_element_get_compatible_pad (ap,
540 gst_pad_get_caps (pad_src));
542 if ((pad_sink == NULL) || (pad_src == NULL))
545 GstPadLinkReturn lret = gst_pad_link (pad_src, pad_sink);
546 if (lret != GST_PAD_LINK_OK)
549 gst_object_unref (pad_src);
550 gst_object_unref (pad_sink);
552 pad_src = gst_element_get_pad (src, "src_video");
553 pad_sink = gst_element_get_compatible_pad (vp,
555 gst_pad_get_caps (pad_src));
557 if ((pad_src == NULL) || (pad_sink == NULL))
560 if (gst_pad_link (pad_src, pad_sink) != GST_PAD_LINK_OK) {
561 g_warning ("invalid source. video");
569 if ((src != NULL) && (ret == FALSE)) {
570 gst_bin_remove (GST_BIN (priv->pipe), src);
571 gst_object_unref (src);
575 gst_object_unref (ap);
578 gst_object_unref (vp);
581 gst_object_unref (pad_src);
583 if (pad_sink != NULL)
584 gst_object_unref (pad_sink);
592 g_mencoder_remove_uri (GMencoder *self,
595 // GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
600 g_mencoder_play_stream (GMencoder *self)
602 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
603 g_return_if_fail (priv->ready == FALSE);
605 gst_element_set_state (priv->pipe, GST_STATE_PLAYING);
606 priv->tick_id = g_timeout_add (500, _tick_cb, self);
610 g_mencoder_pause_stream (GMencoder *self)
612 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
613 g_return_if_fail (priv->ready == TRUE);
614 gst_element_set_state (priv->pipe, GST_STATE_PAUSED);
618 g_mencoder_close_stream (GMencoder *self)
621 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
622 if (priv->tick_id != 0) {
623 g_source_remove (priv->tick_id);
627 if (priv->pipe != NULL) {
628 //TODO: fixe pipeline dispose
629 //gst_element_set_state (priv->pipe, GST_STATE_NULL);
630 //g_debug ("SETING STATE TO NULL: OK");
631 //gst_element_set_state (priv->pipe, GST_STATE_NULL);
632 //gst_object_unref (priv->pipe);
633 gst_object_unref (priv->src);
644 _create_pipeline (GMencoder *self,
645 const gchar* video_encode,
646 const gchar* mux_name,
647 gchar** video_encode_prop,
652 const gchar* audio_encode,
653 gchar** audio_encode_prop,
657 GstElement *pipe = NULL;
658 GstElement *sink = NULL;
659 GstElement *mux = NULL;
660 GstElement *abin = NULL;
661 GstElement *vbin = NULL;
662 GstElement *queue= NULL;
663 GstPad *aux_pad = NULL;
664 GstPad *mux_pad = NULL;
665 #ifdef SUPPORT_MULT_INPUT
666 GstElement *ap = NULL;
667 GstElement *vp = NULL;
669 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
671 pipe = gst_pipeline_new ("pipe");
673 #ifdef SUPPORT_MULT_INPUT
674 ap = gst_element_factory_make ("concatmux", "ap");
675 vp = gst_element_factory_make ("concatmux", "vp");
676 gst_bin_add_many (GST_BIN (pipe), ap, vp, NULL);
679 mux = gst_element_factory_make ((mux_name ? mux_name : "ffmux_mpeg"), "mux");
683 queue = gst_element_factory_make ("queue", "queueu_sink");
686 sink = gst_element_factory_make ("fdsink", "sink");
690 g_object_set (G_OBJECT(sink),
695 abin = _create_audio_bin (audio_encode, audio_encode_prop, audio_rate);
699 vbin = _create_video_bin (video_encode, video_encode_prop, video_fps, video_rate, video_width, video_height);
704 gst_bin_add_many (GST_BIN (pipe), abin, vbin, mux, queue, sink, NULL);
707 #ifdef SUPPORT_MULT_INPUT
708 if (gst_element_link (ap, abin) == FALSE) {
709 g_warning ("Fail to link concat and abin");
713 if (gst_element_link (vp, vbin) == FALSE) {
714 g_warning ("Fail to link concat and vbin");
719 aux_pad = gst_element_get_pad (abin, "src");
720 mux_pad = gst_element_get_compatible_pad (mux, aux_pad, GST_PAD_CAPS (aux_pad));
721 if (mux_pad == NULL) {
722 g_warning ("Mux element no have audio PAD");
725 GstPadLinkReturn ret = gst_pad_link (aux_pad, mux_pad);
726 if (ret != GST_PAD_LINK_OK) {
727 g_warning ("Fail link audio and mux: %d", ret);
731 gst_object_unref (aux_pad);
732 gst_object_unref (mux_pad);
734 aux_pad = gst_element_get_pad (vbin, "src");
735 mux_pad = gst_element_get_compatible_pad (mux, aux_pad, GST_PAD_CAPS (aux_pad));
736 if (mux_pad == NULL) {
737 g_warning ("Mux element no have video PAD");
740 ret = gst_pad_link (aux_pad, mux_pad);
741 if (ret != GST_PAD_LINK_OK) {
742 g_warning ("Fail link video and mux: %d", ret);
745 gst_object_unref (aux_pad);
746 gst_object_unref (mux_pad);
751 gst_element_link_many (mux, queue, sink, NULL);
753 bus = gst_pipeline_get_bus (GST_PIPELINE (pipe));
754 gst_bus_add_watch (bus, _pipeline_bus_cb, self);
755 gst_object_unref (bus);
759 g_warning ("Invalid uri");
762 gst_object_unref (pipe);
767 gst_object_unref (mux);
770 if (mux_pad != NULL) {
771 gst_object_unref (mux_pad);
774 if (aux_pad != NULL) {
775 gst_object_unref (mux_pad);
779 gst_object_unref (sink);
783 gst_object_unref (abin);
787 gst_object_unref (vbin);
795 _close_output (GMencoder *self)
800 _create_source (const gchar* uri)
803 GstElement *bsrc = NULL;
804 GstElement *src = NULL;
805 GstElement *queue = NULL;
806 GstElement *aqueue = NULL;
807 GstElement *vqueue = NULL;
808 GstElement *decode = NULL;
809 GstPad *src_pad = NULL;
812 bsrc = gst_bin_new (NULL);
814 //src = gst_element_factory_make ("gnomevfssrc", "src");
815 //g_object_set (G_OBJECT (src), "location", uri, NULL);
816 src = gst_element_make_from_uri (GST_URI_SRC, uri, "src");
820 decode = gst_element_factory_make ("decodebin", "decode");
824 queue = gst_element_factory_make ("queue", "queue_src");
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, queue, decode, aqueue, vqueue, NULL);
834 gst_element_link_many (src, queue, decode, NULL);
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,
888 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
890 g_warning ("Output uri not supported");
897 _pipeline_bus_cb (GstBus *bus,
901 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (user_data);
903 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);
933 case GST_MESSAGE_ERROR:
939 if (priv->tick_id != 0) {
940 g_source_remove (priv->tick_id);
944 gst_message_parse_error (msg, &error, &debug);
945 err_str = g_strdup_printf ("Error [%d] %s (%s)", error->code,
949 g_signal_emit (user_data, g_mencoder_signals[ERROR], 0, err_str);
951 g_clear_error (&error);
956 case GST_MESSAGE_EOS:
958 g_signal_emit (user_data, g_mencoder_signals[EOS], 0);
961 case GST_MESSAGE_DURATION:
965 gst_message_parse_duration (msg, &format, &duration);
966 if (format == GST_FORMAT_BYTES)
967 priv->duration = duration;
981 _decodebin_new_pad_cb (GstElement* object,
987 gchar *str_caps = NULL;
988 GstElement *sink_element;
991 caps = gst_pad_get_caps (pad);
992 str_caps = gst_caps_to_string (caps);
993 if (strstr (str_caps, "audio") != NULL) {
994 sink_element = gst_bin_get_by_name (GST_BIN (user_data), "aqueue");
995 } else if (strstr (str_caps, "video") != NULL) {
996 sink_element = gst_bin_get_by_name (GST_BIN (user_data), "vqueue");
998 g_warning ("invalid caps %s", str_caps);
1001 sink_pad = gst_element_get_pad (sink_element, "sink");
1002 gst_pad_link (pad, sink_pad);
1004 gst_object_unref (sink_element);
1005 gst_object_unref (sink_pad);
1007 gst_caps_unref (caps);
1011 _decodebin_unknown_type_cb (GstElement* object,
1016 g_warning ("Unknown Type");
1017 //priv->ready = FALSE;
1021 _tick_cb (gpointer user_data)
1023 GstFormat format = GST_FORMAT_BYTES;
1026 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (user_data);
1028 if (priv->duration == 0) {
1030 if (gst_element_query_duration (priv->src, &format, &d))
1034 if (priv->duration != 0) {
1035 gst_element_query_position (priv->src, &format, &cur);
1036 g_print ("PROGRESS:%lli\n", (99 * cur) / priv->duration);