[svn r702] Changed the architecture to support GObject signals in the clinkc's UPnP model.
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 gst_bin_add (GST_BIN (vbin), vrate);
405 if (gst_element_link (vcolorspace, vrate) == FALSE) {
406 g_warning ("Fail to link video elements");
410 vcaps = gst_caps_new_simple ("video/x-raw-yuv",
411 "framerate", GST_TYPE_FRACTION, (int) (fps * 1000), 1000, NULL);
413 if (gst_element_link_filtered (vrate, vencode, vcaps) == FALSE) {
414 g_warning ("Fail to link vrate with vencode.");
417 gst_caps_unref (vcaps);
419 if (gst_element_link (vcolorspace, vencode) == FALSE) {
420 g_warning ("Fail to link colorspace and video encode element.");
425 gst_element_link (vencode, vqueue_src);
427 // ghost pad the video bin
428 vpad = gst_element_get_pad (vqueue, "sink");
429 gst_element_add_pad (vbin, gst_ghost_pad_new ("sink", vpad));
430 gst_object_unref (vpad);
432 vpad = gst_element_get_pad (vqueue_src, "src");
433 gst_element_add_pad (vbin, gst_ghost_pad_new ("src", vpad));
434 gst_object_unref (vpad);
440 gst_object_unref (vpad);
443 gst_object_unref (vbin);
446 gst_object_unref (vqueue);
449 gst_object_unref (vencode);
451 if (vqueue_src != NULL)
452 gst_object_unref (vqueue_src);
454 if (vcolorspace != NULL)
455 gst_object_unref (vcolorspace);
463 g_mencoder_setup_stream (GMencoder *self,
464 const gchar* mux_name,
465 const gchar* video_encode,
466 gchar** video_encode_prop,
471 const gchar* audio_encode,
472 gchar** audio_encode_prop,
474 const gchar* out_uri)
476 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
477 if (priv->ready == TRUE) {
478 g_warning ("Stream already configured. You need close stream first.");
482 _close_output (self);
483 _open_output (self, out_uri);
486 priv->pipe = _create_pipeline (self,
502 g_mencoder_append_uri (GMencoder *self,
508 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
509 gboolean ret = FALSE;
510 GstElement *ap = NULL;
511 GstElement *vp = NULL;
514 g_return_val_if_fail (priv->pipe != NULL, FALSE);
515 g_return_val_if_fail (priv->ready == FALSE, FALSE);
517 #ifndef SUPPORT_MULT_INPUT
518 g_return_val_if_fail (priv->sources < 1, FALSE);
521 src = _create_source (uri);
525 priv->src = gst_bin_get_by_name (GST_BIN (src), "src");
527 gst_bin_add (GST_BIN (priv->pipe), src);
529 #ifdef SUPPORT_MULT_INPUT
530 ap = gst_bin_get_by_name (GST_BIN (priv->pipe), "ap");
531 vp = gst_bin_get_by_name (GST_BIN (priv->pipe), "vp");
533 ap = gst_bin_get_by_name (GST_BIN (priv->pipe), "abin");
534 vp = gst_bin_get_by_name (GST_BIN (priv->pipe), "vbin");
537 if ((vp == NULL) || (ap == NULL)) {
538 g_warning ("Fail to get output bin");
542 pad_src = gst_element_get_pad (src, "src_audio");
543 pad_sink = gst_element_get_compatible_pad (ap,
545 gst_pad_get_caps (pad_src));
547 if ((pad_sink == NULL) || (pad_src == NULL))
550 GstPadLinkReturn lret = gst_pad_link (pad_src, pad_sink);
551 if (lret != GST_PAD_LINK_OK)
554 gst_object_unref (pad_src);
555 gst_object_unref (pad_sink);
557 pad_src = gst_element_get_pad (src, "src_video");
558 pad_sink = gst_element_get_compatible_pad (vp,
560 gst_pad_get_caps (pad_src));
562 if ((pad_src == NULL) || (pad_sink == NULL))
565 if (gst_pad_link (pad_src, pad_sink) != GST_PAD_LINK_OK) {
566 g_warning ("invalid source. video");
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 if (priv->tick_id != 0) {
632 g_source_remove (priv->tick_id);
636 if (priv->pipe != NULL) {
637 //TODO: fixe pipeline dispose
638 g_debug ("SETING STATE TO NULL");
639 gst_element_set_state (priv->pipe, GST_STATE_NULL);
640 g_debug ("SETING STATE TO NULL: OK");
641 //gst_object_unref (priv->pipe);
642 gst_object_unref (priv->src);
653 _create_pipeline (GMencoder *self,
654 const gchar* video_encode,
655 const gchar* mux_name,
656 gchar** video_encode_prop,
661 const gchar* audio_encode,
662 gchar** audio_encode_prop,
666 GstElement *pipe = NULL;
667 GstElement *sink = NULL;
668 GstElement *mux = NULL;
669 GstElement *abin = NULL;
670 GstElement *vbin = NULL;
671 GstPad *aux_pad = NULL;
672 GstPad *mux_pad = NULL;
673 #ifdef SUPPORT_MULT_INPUT
674 GstElement *ap = NULL;
675 GstElement *vp = NULL;
677 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
679 pipe = gst_pipeline_new ("pipe");
681 #ifdef SUPPORT_MULT_INPUT
682 ap = gst_element_factory_make ("concatmux", "ap");
683 vp = gst_element_factory_make ("concatmux", "vp");
684 gst_bin_add_many (GST_BIN (pipe), ap, vp, NULL);
687 mux = gst_element_factory_make ((mux_name ? mux_name : "ffmux_mpeg"), "mux");
692 sink = gst_element_factory_make ("fdsink", "sink");
696 g_object_set (G_OBJECT(sink),
700 abin = _create_audio_bin (audio_encode, audio_encode_prop, audio_rate);
704 vbin = _create_video_bin (video_encode, video_encode_prop, video_fps, video_rate, video_width, video_height);
709 gst_bin_add_many (GST_BIN (pipe), abin, vbin, mux, sink, NULL);
712 #ifdef SUPPORT_MULT_INPUT
713 if (gst_element_link (ap, abin) == FALSE) {
714 g_warning ("Fail to link concat and abin");
718 if (gst_element_link (vp, vbin) == FALSE) {
719 g_warning ("Fail to link concat and vbin");
724 aux_pad = gst_element_get_pad (abin, "src");
725 mux_pad = gst_element_get_compatible_pad (mux, aux_pad, GST_PAD_CAPS (aux_pad));
726 if (mux_pad == NULL) {
727 g_warning ("Mux element no have audio PAD");
730 GstPadLinkReturn ret = gst_pad_link (aux_pad, mux_pad);
731 if (ret != GST_PAD_LINK_OK) {
732 g_warning ("Fail link audio and mux: %d", ret);
736 gst_object_unref (aux_pad);
737 gst_object_unref (mux_pad);
739 aux_pad = gst_element_get_pad (vbin, "src");
740 mux_pad = gst_element_get_compatible_pad (mux, aux_pad, GST_PAD_CAPS (aux_pad));
741 if (mux_pad == NULL) {
742 g_warning ("Mux element no have video PAD");
745 ret = gst_pad_link (aux_pad, mux_pad);
746 if (ret != GST_PAD_LINK_OK) {
747 g_warning ("Fail link video and mux: %d", ret);
750 gst_object_unref (aux_pad);
751 gst_object_unref (mux_pad);
756 gst_element_link (mux, sink);
758 bus = gst_pipeline_get_bus (GST_PIPELINE (pipe));
759 gst_bus_add_watch (bus, _pipeline_bus_cb, self);
760 gst_object_unref (bus);
764 g_warning ("Invalid uri");
767 gst_object_unref (pipe);
772 gst_object_unref (mux);
775 if (mux_pad != NULL) {
776 gst_object_unref (mux_pad);
779 if (aux_pad != NULL) {
780 gst_object_unref (mux_pad);
784 gst_object_unref (sink);
788 gst_object_unref (abin);
792 gst_object_unref (vbin);
800 _close_output (GMencoder *self)
806 _create_source (const gchar* uri)
809 GstElement *bsrc = NULL;
810 GstElement *src = NULL;
811 GstElement *aqueue = NULL;
812 GstElement *vqueue = NULL;
813 GstElement *decode = NULL;
814 GstPad *src_pad = NULL;
817 bsrc = gst_bin_new (NULL);
819 //src = gst_element_factory_make ("gnomevfssrc", "src");
820 //g_object_set (G_OBJECT (src), "location", uri, NULL);
821 src = gst_element_make_from_uri (GST_URI_SRC, uri, "src");
825 decode = gst_element_factory_make ("decodebin2", "decode");
829 aqueue = gst_element_factory_make ("queue", "aqueue");
833 vqueue = gst_element_factory_make ("queue", "vqueue");
837 gst_bin_add_many (GST_BIN (bsrc), src, decode, aqueue, vqueue, NULL);
838 gst_element_link (src, decode);
840 g_signal_connect (G_OBJECT (decode),
842 G_CALLBACK (_decodebin_new_pad_cb),
845 g_signal_connect (G_OBJECT (decode),
847 G_CALLBACK (_decodebin_unknown_type_cb),
850 src_pad = gst_element_get_pad (aqueue, "src");
851 gst_element_add_pad (bsrc, gst_ghost_pad_new("src_audio", src_pad));
852 gst_object_unref (src_pad);
854 src_pad = gst_element_get_pad (vqueue, "src");
855 gst_element_add_pad (bsrc, gst_ghost_pad_new("src_video", src_pad));
856 gst_object_unref (src_pad);
862 gst_object_unref (src);
865 if (decode != NULL) {
866 gst_object_unref (decode);
869 if (aqueue != NULL) {
870 gst_object_unref (aqueue);
873 if (vqueue != NULL) {
874 gst_object_unref (vqueue);
881 _open_output (GMencoder *self,
885 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
887 i = g_strsplit (uri, "://", 0);
888 if (strcmp (i[0], "fd") == 0) {
889 priv->fd = atoi (i[1]);
890 } else if (strcmp (i[0], "file") == 0) {
891 priv->fd = open (i[1], O_WRONLY | O_CREAT | O_TRUNC);
893 g_warning ("Output uri not supported");
900 _pipeline_bus_cb (GstBus *bus,
904 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (user_data);
906 switch (GST_MESSAGE_TYPE (msg))
908 case GST_MESSAGE_STATE_CHANGED:
912 GstState pendingstate;
915 gst_message_parse_state_changed (msg, &oldstate,
916 &newstate, &pendingstate);
918 if (pendingstate != GST_STATE_VOID_PENDING)
921 if ((oldstate == GST_STATE_READY) &&
922 (newstate == GST_STATE_PAUSED)) {
924 g_signal_emit (user_data, g_mencoder_signals[PAUSED], 0);
925 } else if ((oldstate == GST_STATE_PAUSED) &&
926 (newstate == GST_STATE_PLAYING)) {
927 g_signal_emit (user_data, g_mencoder_signals[PLAYING], 0);
928 } else if ((oldstate == GST_STATE_READY) &&
929 (newstate == GST_STATE_NULL)) {
930 g_signal_emit (user_data, g_mencoder_signals[STOPED], 0);
934 case GST_MESSAGE_ERROR:
940 if (priv->tick_id != 0) {
941 g_source_remove (priv->tick_id);
945 gst_message_parse_error (msg, &error, &debug);
946 err_str = g_strdup_printf ("Error [%d] %s (%s)", error->code,
950 g_signal_emit (user_data, g_mencoder_signals[ERROR], 0, err_str);
952 g_clear_error (&error);
957 case GST_MESSAGE_EOS:
959 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);