11 #include <sys/types.h>
12 #include <sys/socket.h>
14 #include "gmencoder.h"
16 #define G_MENCODER_GET_PRIVATE(obj) \
17 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), G_TYPE_MENCODER, GMencoderPrivate))
19 // #define SUPPORT_MULT_INPUT 0
20 #define USE_MANUAL_SINK
21 #define BUFFER_SIZE (1024 * 64)
23 typedef struct _GMencoderPrivate GMencoderPrivate;
24 typedef struct _SetupInfo SetupInfo;
29 gchar **video_encode_prop;
35 gchar **audio_encode_prop;
40 struct _GMencoderPrivate {
54 #ifdef USE_MANUAL_SINK
68 static void g_mencoder_class_init(GMencoderClass * klass);
69 static void g_mencoder_init(GMencoder * object);
70 static void g_mencoder_dispose(GObject * object);
71 static void g_mencoder_finalize(GObject * object);
72 static GstElement *_create_audio_bin(const gchar * encode,
73 gchar ** encode_prop, gint rate);
74 static GstElement *_create_video_bin(const gchar * encode,
77 gint rate, guint width, guint height);
80 _pipeline_bus_cb(GstBus * bus, GstMessage * msg, gpointer user_data);
82 static void _decodebin_new_pad_cb(GstElement * object,
84 gboolean flag, gpointer user_data);
86 static void _decodebin_unknown_type_cb(GstElement * object,
91 static void _close_output(GMencoder * self);
92 static gboolean _open_output(GMencoder * self, const gchar * uri);
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 #ifdef USE_MANUAL_SINK
107 static void _flush_queue (GMencoder *self);
108 static void _buffer_arrive_cb (GstElement* object,
115 static gboolean _tick_cb(gpointer data);
117 static guint g_mencoder_signals[LAST_SIGNAL] = { 0 };
119 G_DEFINE_TYPE(GMencoder, g_mencoder, G_TYPE_OBJECT)
121 static void g_mencoder_class_init(GMencoderClass * klass)
123 GObjectClass *object_class;
124 object_class = (GObjectClass *) klass;
125 g_type_class_add_private(klass, sizeof(GMencoderPrivate));
127 object_class->dispose = g_mencoder_dispose;
128 object_class->finalize = g_mencoder_finalize;
130 g_mencoder_signals[PAUSED] =
131 g_signal_new("paused",
132 G_OBJECT_CLASS_TYPE(object_class),
135 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
137 g_mencoder_signals[PLAYING] =
138 g_signal_new("playing",
139 G_OBJECT_CLASS_TYPE(object_class),
142 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
144 g_mencoder_signals[STOPED] =
145 g_signal_new("stoped",
146 G_OBJECT_CLASS_TYPE(object_class),
149 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
151 g_mencoder_signals[EOS] =
153 G_OBJECT_CLASS_TYPE(object_class),
156 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
158 g_mencoder_signals[ERROR] =
159 g_signal_new("error",
160 G_OBJECT_CLASS_TYPE(object_class),
163 g_cclosure_marshal_VOID__STRING,
164 G_TYPE_NONE, 1, G_TYPE_STRING);
168 g_mencoder_init(GMencoder * self)
170 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
171 priv->info = g_new0(SetupInfo, 1);
172 #ifdef USE_MANUAL_SINK
173 priv->queue = g_byte_array_new ();
178 g_mencoder_dispose(GObject * object)
183 g_mencoder_finalize(GObject * object)
185 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(object);
188 g_mencoder_close_stream(G_MENCODER(object));
190 #ifdef USE_MANUAL_SINK
191 g_byte_array_free (priv->queue, TRUE);
198 return g_object_new(G_TYPE_MENCODER, NULL);
203 _obj_set_prop(GObject * obj, const gchar * prop_name,
204 const gchar * prop_val)
208 GParamSpec *s = NULL;
209 GObjectClass *k = G_OBJECT_GET_CLASS(obj);
212 g_value_init(&v, G_TYPE_STRING);
213 g_value_set_string(&v, prop_val);
215 s = g_object_class_find_property(k, prop_name);
217 g_print("Invalid property name: %s\n", prop_name);
221 g_value_init(&p, s->value_type);
222 switch (s->value_type) {
224 g_value_set_int(&p, atoi(prop_val));
227 g_value_set_string(&p, prop_val);
233 g_object_set_property(obj, prop_name, &p);
239 _create_element_with_prop(const gchar * factory_name,
240 const gchar * element_name, gchar ** prop)
245 ret = gst_element_factory_make(factory_name, element_name);
250 for (i = 0; i < g_strv_length(prop); i++) {
251 if (prop[i] != NULL) {
252 char **v = g_strsplit(prop[i], "=", 2);
253 if (g_strv_length(v) == 2) {
254 _obj_set_prop(G_OBJECT(ret), v[0], v[1]);
266 _create_audio_bin(const gchar * encode, gchar ** encode_prop, gint rate)
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 !
276 // udpsink name=upd_audio host=224.0.0.1 port=5002
277 abin = gst_bin_new("abin");
278 aqueue = gst_element_factory_make("queue", "aqueue");
279 aconvert = gst_element_factory_make("audioconvert", "aconvert");
281 _create_element_with_prop((encode ? encode : "lame"), "aencode",
283 aqueue_src = gst_element_factory_make("queue", "aqueue_src");
285 if ((abin == NULL) || (aqueue == NULL) || (aconvert == NULL)
286 || (aencode == NULL) || (aqueue_src == NULL)) {
287 g_warning("Audio elements not found");
291 g_object_set(G_OBJECT(aencode), "bitrate", 32, NULL);
293 * if (rate > 0) { g_object_set (G_OBJECT (aencode), "bitrate", 32,
297 gst_bin_add_many(GST_BIN(abin), aqueue, aconvert, aencode, aqueue_src,
299 if (gst_element_link_many(aqueue, aconvert, aencode, aqueue_src, NULL)
301 g_warning("Not Link audio elements");
303 // TODO: apply audio rate
305 // ghost pad the audio bin
306 apad = gst_element_get_pad(aqueue, "sink");
307 gst_element_add_pad(abin, gst_ghost_pad_new("sink", apad));
308 gst_object_unref(apad);
310 apad = gst_element_get_pad(aqueue_src, "src");
311 gst_element_add_pad(abin, gst_ghost_pad_new("src", apad));
312 gst_object_unref(apad);
317 gst_object_unref(abin);
320 gst_object_unref(aqueue);
322 if (aconvert != NULL)
323 gst_object_unref(aconvert);
326 gst_object_unref(aencode);
328 if (aqueue_src != NULL)
329 gst_object_unref(aqueue_src);
332 gst_object_unref(apad);
340 // queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! colorspace
341 // ! rate ! encode ! queue
343 _create_video_bin(const gchar * encode,
344 gchar ** encode_prop,
345 gdouble fps, gint rate, guint width, guint height)
347 GstElement *vbin = NULL;
348 GstElement *vqueue = NULL;
349 GstElement *vqueue_src = NULL;
350 GstElement *vcolorspace = NULL;
351 GstElement *vencode = NULL;
352 GstElement *vrate = NULL;
355 vbin = gst_bin_new("vbin");
356 vqueue = gst_element_factory_make("queue", "vqueue");
358 gst_element_factory_make("ffmpegcolorspace", "colorspace");
360 vencode = _create_element_with_prop((encode !=
362 "ffenc_mpeg1video"), "vencode",
364 vqueue_src = gst_element_factory_make("queue", "queue_src");
366 if ((vbin == NULL) || (vqueue == NULL) || (vcolorspace == NULL)
367 || (vencode == NULL) || (vqueue_src == NULL)) {
368 g_warning("Video elements not found");
372 gst_bin_add_many(GST_BIN(vbin), vqueue, vcolorspace, vencode,
375 if ((width > 0) && (height > 0)) {
379 gst_element_factory_make("videoscale", "vscale");
381 gst_bin_add(GST_BIN(vbin), vscale);
383 vcaps = gst_caps_new_simple("video/x-raw-yuv",
384 "width", G_TYPE_INT, width,
385 "height", G_TYPE_INT, height, NULL);
387 gst_element_link(vqueue, vscale);
389 if (gst_element_link_filtered(vscale, vcolorspace, vcaps) == FALSE) {
390 g_warning("Fail to resize video");
391 gst_object_unref(vcaps);
392 gst_object_unref(vscale);
395 gst_caps_unref(vcaps);
397 gst_element_link(vqueue, vcolorspace);
401 // Changing the video fps
403 vrate = gst_element_factory_make("videorate", "vrate");
405 gst_bin_add(GST_BIN(vbin), vrate);
407 if (gst_element_link(vcolorspace, vrate) == FALSE) {
408 g_warning("Fail to link video elements");
412 vcaps = gst_caps_new_simple("video/x-raw-yuv",
413 "framerate", GST_TYPE_FRACTION,
414 (int) (fps * 1000), 1000, NULL);
416 if (gst_element_link_filtered(vrate, vencode, vcaps) == FALSE) {
417 g_warning("Fail to link vrate with vencode.");
420 gst_caps_unref(vcaps);
422 if (gst_element_link(vcolorspace, vencode) == FALSE) {
423 g_warning("Fail to link colorspace and video encode element.");
428 gst_element_link(vencode, vqueue_src);
430 // ghost pad the video bin
431 vpad = gst_element_get_pad(vqueue, "sink");
432 gst_element_add_pad(vbin, gst_ghost_pad_new("sink", vpad));
433 gst_object_unref(vpad);
435 vpad = gst_element_get_pad(vqueue_src, "src");
436 gst_element_add_pad(vbin, gst_ghost_pad_new("src", vpad));
437 gst_object_unref(vpad);
443 gst_object_unref(vpad);
446 gst_object_unref(vbin);
449 gst_object_unref(vqueue);
452 gst_object_unref(vencode);
454 if (vqueue_src != NULL)
455 gst_object_unref(vqueue_src);
457 if (vcolorspace != NULL)
458 gst_object_unref(vcolorspace);
466 g_mencoder_setup_stream(GMencoder * self,
467 const gchar * mux_name,
468 const gchar * video_encode,
469 gchar ** video_encode_prop,
474 const gchar * audio_encode,
475 gchar ** audio_encode_prop,
476 guint audio_rate, const gchar * out_uri)
478 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
479 if (priv->ready == TRUE) {
481 ("Stream already configured. You need close stream first.");
486 if (_open_output(self, out_uri) == FALSE)
490 priv->pipe = _create_pipeline(self,
498 audio_encode, audio_encode_prop,
501 return (priv->pipe != NULL);
506 g_mencoder_append_uri(GMencoder * self, const gchar * uri)
511 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
512 gboolean ret = FALSE;
513 GstElement *ap = NULL;
514 GstElement *vp = NULL;
517 g_return_val_if_fail(priv->pipe != NULL, FALSE);
518 g_return_val_if_fail(priv->ready == FALSE, FALSE);
520 #ifndef SUPPORT_MULT_INPUT
521 g_return_val_if_fail(priv->sources < 1, FALSE);
524 src = _create_source(uri);
528 priv->src = gst_bin_get_by_name(GST_BIN(src), "src");
530 gst_bin_add(GST_BIN(priv->pipe), src);
532 #ifdef SUPPORT_MULT_INPUT
533 ap = gst_bin_get_by_name(GST_BIN(priv->pipe), "ap");
534 vp = gst_bin_get_by_name(GST_BIN(priv->pipe), "vp");
536 ap = gst_bin_get_by_name(GST_BIN(priv->pipe), "abin");
537 vp = gst_bin_get_by_name(GST_BIN(priv->pipe), "vbin");
540 if ((vp == NULL) || (ap == NULL)) {
541 g_warning("Fail to get output bin");
545 pad_src = gst_element_get_pad(src, "src_audio");
546 pad_sink = gst_element_get_compatible_pad(ap,
548 gst_pad_get_caps(pad_src));
550 if ((pad_sink == NULL) || (pad_src == NULL))
553 GstPadLinkReturn lret = gst_pad_link(pad_src, pad_sink);
554 if (lret != GST_PAD_LINK_OK)
557 gst_object_unref(pad_src);
558 gst_object_unref(pad_sink);
560 pad_src = gst_element_get_pad(src, "src_video");
561 pad_sink = gst_element_get_compatible_pad(vp,
563 gst_pad_get_caps(pad_src));
565 if ((pad_src == NULL) || (pad_sink == NULL))
568 if (gst_pad_link(pad_src, pad_sink) != GST_PAD_LINK_OK) {
569 g_warning("invalid source. video");
577 if ((src != NULL) && (ret == FALSE)) {
578 gst_bin_remove(GST_BIN(priv->pipe), src);
579 gst_object_unref(src);
583 gst_object_unref(ap);
586 gst_object_unref(vp);
589 gst_object_unref(pad_src);
591 if (pad_sink != NULL)
592 gst_object_unref(pad_sink);
600 g_mencoder_remove_uri(GMencoder * self, const gchar * uri)
602 // GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
607 g_mencoder_play_stream(GMencoder * self)
609 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
610 g_return_if_fail(priv->ready == FALSE);
612 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);
620 g_return_if_fail(priv->ready == TRUE);
621 gst_element_set_state(priv->pipe, GST_STATE_PAUSED);
625 g_mencoder_close_stream(GMencoder * self)
628 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
629 if (priv->tick_id != 0) {
630 g_source_remove(priv->tick_id);
634 if (priv->pipe != NULL) {
635 // TODO: fixe pipeline dispose
636 // gst_element_set_state (priv->pipe, GST_STATE_NULL);
637 // g_debug ("SETING STATE TO NULL: OK");
638 // gst_element_set_state (priv->pipe, GST_STATE_NULL);
639 // gst_object_unref (priv->pipe);
640 gst_object_unref(priv->src);
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, guint audio_rate)
663 GstElement *pipe = NULL;
664 GstElement *sink = NULL;
665 GstElement *mux = NULL;
666 GstElement *abin = NULL;
667 GstElement *vbin = NULL;
668 GstElement *queue = NULL;
669 GstPad *aux_pad = NULL;
670 GstPad *mux_pad = NULL;
671 #ifdef SUPPORT_MULT_INPUT
672 GstElement *ap = NULL;
673 GstElement *vp = NULL;
676 pipe = gst_pipeline_new("pipe");
678 #ifdef SUPPORT_MULT_INPUT
679 ap = gst_element_factory_make("concatmux", "ap");
680 vp = gst_element_factory_make("concatmux", "vp");
681 gst_bin_add_many(GST_BIN(pipe), ap, vp, NULL);
683 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
686 gst_element_factory_make((mux_name ? mux_name : "ffmux_mpeg"),
691 queue = gst_element_factory_make("queue", "queueu_sink");
694 #ifdef USE_MANUAL_SINK
695 sink = gst_element_factory_make("fakesink", "sink");
696 g_object_set (G_OBJECT (sink), "signal-handoffs", TRUE, NULL);
697 g_signal_connect (G_OBJECT (sink),
699 G_CALLBACK (_buffer_arrive_cb),
702 sink = gst_element_factory_make("fdsink", "sink");
706 g_object_set(G_OBJECT(sink), "fd", priv->fd, "sync", FALSE, NULL);
709 abin = _create_audio_bin(audio_encode, audio_encode_prop, audio_rate);
714 _create_video_bin(video_encode, video_encode_prop, video_fps,
715 video_rate, video_width, video_height);
720 gst_bin_add_many(GST_BIN(pipe), abin, vbin, mux, queue, sink, NULL);
723 #ifdef SUPPORT_MULT_INPUT
724 if (gst_element_link(ap, abin) == FALSE) {
725 g_warning("Fail to link concat and abin");
729 if (gst_element_link(vp, vbin) == FALSE) {
730 g_warning("Fail to link concat and vbin");
734 // Link bins with mux
735 aux_pad = gst_element_get_pad(abin, "src");
737 gst_element_get_compatible_pad(mux, aux_pad,
738 GST_PAD_CAPS(aux_pad));
739 if (mux_pad == NULL) {
740 g_warning("Mux element no have audio PAD");
743 GstPadLinkReturn ret = gst_pad_link(aux_pad, mux_pad);
744 if (ret != GST_PAD_LINK_OK) {
745 g_warning("Fail link audio and mux: %d", ret);
749 gst_object_unref(aux_pad);
750 gst_object_unref(mux_pad);
752 aux_pad = gst_element_get_pad(vbin, "src");
754 gst_element_get_compatible_pad(mux, aux_pad,
755 GST_PAD_CAPS(aux_pad));
756 if (mux_pad == NULL) {
757 g_warning("Mux element no have video PAD");
760 ret = gst_pad_link(aux_pad, mux_pad);
761 if (ret != GST_PAD_LINK_OK) {
762 g_warning("Fail link video and mux: %d", ret);
765 gst_object_unref(aux_pad);
766 gst_object_unref(mux_pad);
770 // Link mux with sink
771 gst_element_link_many(mux, queue, sink, NULL);
773 bus = gst_pipeline_get_bus(GST_PIPELINE(pipe));
774 gst_bus_add_watch(bus, _pipeline_bus_cb, self);
775 gst_object_unref(bus);
779 g_warning("Invalid uri");
782 gst_object_unref(pipe);
787 gst_object_unref(mux);
790 if (mux_pad != NULL) {
791 gst_object_unref(mux_pad);
794 if (aux_pad != NULL) {
795 gst_object_unref(mux_pad);
799 gst_object_unref(sink);
803 gst_object_unref(abin);
807 gst_object_unref(vbin);
815 _close_output(GMencoder * self)
820 _create_source(const gchar * uri)
823 GstElement *bsrc = NULL;
824 GstElement *src = NULL;
825 GstElement *queue = NULL;
826 GstElement *aqueue = NULL;
827 GstElement *vqueue = NULL;
828 GstElement *decode = NULL;
829 GstPad *src_pad = NULL;
832 bsrc = gst_bin_new(NULL);
834 // src = gst_element_factory_make ("gnomevfssrc", "src");
835 // g_object_set (G_OBJECT (src), "location", uri, NULL);
836 src = gst_element_make_from_uri(GST_URI_SRC, uri, "src");
840 decode = gst_element_factory_make("decodebin", "decode");
844 queue = gst_element_factory_make("queue", "queue_src");
845 aqueue = gst_element_factory_make("queue", "aqueue");
849 vqueue = gst_element_factory_make("queue", "vqueue");
853 gst_bin_add_many(GST_BIN(bsrc), src, queue, decode, aqueue, vqueue,
855 gst_element_link_many(src, queue, decode, NULL);
857 g_signal_connect(G_OBJECT(decode),
859 G_CALLBACK(_decodebin_new_pad_cb), bsrc);
861 g_signal_connect(G_OBJECT(decode),
863 G_CALLBACK(_decodebin_unknown_type_cb), pipe);
865 src_pad = gst_element_get_pad(aqueue, "src");
866 gst_element_add_pad(bsrc, gst_ghost_pad_new("src_audio", src_pad));
867 gst_object_unref(src_pad);
869 src_pad = gst_element_get_pad(vqueue, "src");
870 gst_element_add_pad(bsrc, gst_ghost_pad_new("src_video", src_pad));
871 gst_object_unref(src_pad);
877 gst_object_unref(src);
880 if (decode != NULL) {
881 gst_object_unref(decode);
884 if (aqueue != NULL) {
885 gst_object_unref(aqueue);
888 if (vqueue != NULL) {
889 gst_object_unref(vqueue);
896 _open_output(GMencoder * self, const gchar * uri)
900 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
902 i = g_strsplit(uri, "://", 0);
903 if (strcmp(i[0], "fd") == 0) {
904 priv->fd = atoi(i[1]);
905 fcntl (priv->fd, F_SETFL, O_ASYNC);
906 } else if (strcmp(i[0], "file") == 0) {
907 if (g_file_test (i[1], G_FILE_TEST_EXISTS)) {
908 if (unlink (i[1]) != 0) {
909 g_warning ("Fail to write in : %s", uri);
914 priv->fd = open(i[1], O_WRONLY | O_CREAT | O_TRUNC,
915 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
917 if (priv->fd == -1) {
918 g_warning ("Fail to open : %s", uri);
922 g_warning("Output uri not supported");
932 _pipeline_bus_cb(GstBus * bus, GstMessage * msg, gpointer user_data)
934 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
936 switch (GST_MESSAGE_TYPE(msg)) {
938 case GST_MESSAGE_STATE_CHANGED:
942 GstState pendingstate;
945 gst_message_parse_state_changed(msg, &oldstate,
946 &newstate, &pendingstate);
948 if (pendingstate != GST_STATE_VOID_PENDING)
951 if ((oldstate == GST_STATE_READY)
952 && (newstate == GST_STATE_PAUSED)) {
954 g_signal_emit(user_data, g_mencoder_signals[PAUSED],
956 } else if ((oldstate == GST_STATE_PAUSED)
957 && (newstate == GST_STATE_PLAYING)) {
958 g_signal_emit(user_data, g_mencoder_signals[PLAYING], 0);
959 } else if ((oldstate == GST_STATE_READY) &&
960 (newstate == GST_STATE_NULL)) {
961 g_signal_emit(user_data, g_mencoder_signals[STOPED], 0);
966 case GST_MESSAGE_ERROR:
972 if (priv->tick_id != 0) {
973 g_source_remove(priv->tick_id);
977 gst_message_parse_error(msg, &error, &debug);
978 err_str = g_strdup_printf("Error [%d] %s (%s)", error->code,
979 error->message, debug);
981 g_signal_emit(user_data, g_mencoder_signals[ERROR], 0,
984 g_clear_error(&error);
989 case GST_MESSAGE_EOS:
991 #ifdef USE_MANUAL_SINK
992 _flush_queue (G_MENCODER (user_data));
994 g_signal_emit(user_data, g_mencoder_signals[EOS], 0);
997 case GST_MESSAGE_DURATION:
1001 gst_message_parse_duration(msg, &format, &duration);
1002 if (format == GST_FORMAT_BYTES)
1003 priv->duration = duration;
1017 _decodebin_new_pad_cb(GstElement * object,
1018 GstPad * pad, gboolean flag, gpointer user_data)
1021 gchar *str_caps = NULL;
1022 GstElement *sink_element;
1025 caps = gst_pad_get_caps(pad);
1026 str_caps = gst_caps_to_string(caps);
1027 if (strstr(str_caps, "audio") != NULL) {
1028 sink_element = gst_bin_get_by_name(GST_BIN(user_data), "aqueue");
1029 } else if (strstr(str_caps, "video") != NULL) {
1030 sink_element = gst_bin_get_by_name(GST_BIN(user_data), "vqueue");
1032 g_warning("invalid caps %s", str_caps);
1035 sink_pad = gst_element_get_pad(sink_element, "sink");
1036 gst_pad_link(pad, sink_pad);
1038 gst_object_unref(sink_element);
1039 gst_object_unref(sink_pad);
1041 gst_caps_unref(caps);
1045 _decodebin_unknown_type_cb(GstElement * object,
1046 GstPad * pad, GstCaps * caps,
1049 g_warning("Unknown Type");
1050 // priv->ready = FALSE;
1054 _tick_cb(gpointer user_data)
1056 GstFormat format = GST_FORMAT_BYTES;
1059 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
1061 if (priv->duration == 0) {
1063 if (gst_element_query_duration(priv->src, &format, &d))
1067 if (priv->duration != 0) {
1068 gst_element_query_position(priv->src, &format, &cur);
1069 g_print("PROGRESS:%lli\n", (99 * cur) / priv->duration);
1076 #ifdef USE_MANUAL_SINK
1078 _send_buffer (gint fd, gpointer buff, gint size)
1080 gboolean ret = TRUE;
1084 b_send = g_byte_array_new ();
1085 msg = g_strdup_printf ("%x\r\n", size);
1086 b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
1089 b_send = g_byte_array_append (b_send, buff, size);
1091 msg = g_strdup ("\r\n");
1092 b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
1095 if (send (fd, b_send->data, b_send->len, MSG_MORE) <= 0)
1097 g_byte_array_free (b_send, TRUE);
1103 _flush_queue (GMencoder *self)
1106 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
1108 if (BUFFER_SIZE == 0)
1111 if (priv->queue->len > 0) {
1112 _send_buffer (priv->fd, priv->queue->data, priv->queue->len);
1113 priv->queue = g_byte_array_remove_range (priv->queue, 0, priv->queue->len);
1116 end_msg = g_strdup ("0\r\n\r\n");
1117 write (priv->fd, (const guint8*) end_msg, strlen(end_msg) * sizeof(gchar));
1122 _buffer_arrive_cb (GstElement* object,
1127 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
1129 if (BUFFER_SIZE == 0) {
1130 if (_send_buffer (priv->fd, GST_BUFFER_DATA (buff), GST_BUFFER_SIZE (buff)) == FALSE)
1135 priv->queue = g_byte_array_append (priv->queue, GST_BUFFER_DATA (buff), GST_BUFFER_SIZE (buff));
1136 while (priv->queue->len >= BUFFER_SIZE) {
1137 //g_usleep (0.2 * G_USEC_PER_SEC);
1138 if (_send_buffer (priv->fd, priv->queue->data, BUFFER_SIZE) == FALSE)
1140 priv->queue = g_byte_array_remove_range (priv->queue, 0, BUFFER_SIZE);
1145 if (priv->tick_id != 0) {
1146 g_source_remove(priv->tick_id);
1149 priv->queue = g_byte_array_remove_range (priv->queue, 0, priv->queue->len);
1150 g_signal_emit(user_data, g_mencoder_signals[ERROR], 0, "Error on socket");