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 gboolean send_chunked;
55 #ifdef USE_MANUAL_SINK
69 static void g_mencoder_class_init(GMencoderClass * klass);
70 static void g_mencoder_init(GMencoder * object);
71 static void g_mencoder_dispose(GObject * object);
72 static void g_mencoder_finalize(GObject * object);
73 static GstElement *_create_audio_bin(const gchar * encode,
74 gchar ** encode_prop, gint rate);
75 static GstElement *_create_video_bin(const gchar * encode,
78 gint rate, guint width, guint height);
81 _pipeline_bus_cb(GstBus * bus, GstMessage * msg, gpointer user_data);
83 static void _decodebin_new_pad_cb(GstElement * object,
85 gboolean flag, gpointer user_data);
87 static void _decodebin_unknown_type_cb(GstElement * object,
92 static void _close_output(GMencoder * self);
93 static gboolean _open_output(GMencoder * self, const gchar * uri);
95 static GstElement *_create_source(const gchar * uri);
96 static GstElement *_create_pipeline(GMencoder * self,
97 const gchar * video_encode,
98 const gchar * mux_name,
99 gchar ** video_encode_prop,
104 const gchar * audio_encode,
105 gchar ** audio_encode_prop,
107 #ifdef USE_MANUAL_SINK
108 static void _flush_queue (GMencoder *self);
109 static void _buffer_arrive_cb (GstElement* object,
116 static gboolean _tick_cb(gpointer data);
118 static guint g_mencoder_signals[LAST_SIGNAL] = { 0 };
120 G_DEFINE_TYPE(GMencoder, g_mencoder, G_TYPE_OBJECT)
122 static void g_mencoder_class_init(GMencoderClass * klass)
124 GObjectClass *object_class;
125 object_class = (GObjectClass *) klass;
126 g_type_class_add_private(klass, sizeof(GMencoderPrivate));
128 object_class->dispose = g_mencoder_dispose;
129 object_class->finalize = g_mencoder_finalize;
131 g_mencoder_signals[PAUSED] =
132 g_signal_new("paused",
133 G_OBJECT_CLASS_TYPE(object_class),
136 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
138 g_mencoder_signals[PLAYING] =
139 g_signal_new("playing",
140 G_OBJECT_CLASS_TYPE(object_class),
143 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
145 g_mencoder_signals[STOPED] =
146 g_signal_new("stoped",
147 G_OBJECT_CLASS_TYPE(object_class),
150 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
152 g_mencoder_signals[EOS] =
154 G_OBJECT_CLASS_TYPE(object_class),
157 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
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);
173 #ifdef USE_MANUAL_SINK
174 priv->queue = g_byte_array_new ();
179 g_mencoder_dispose(GObject * object)
184 g_mencoder_finalize(GObject * object)
186 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(object);
189 g_mencoder_close_stream(G_MENCODER(object));
191 #ifdef USE_MANUAL_SINK
192 g_byte_array_free (priv->queue, TRUE);
199 return g_object_new(G_TYPE_MENCODER, NULL);
204 _obj_set_prop(GObject * obj, const gchar * prop_name,
205 const gchar * prop_val)
209 GParamSpec *s = NULL;
210 GObjectClass *k = G_OBJECT_GET_CLASS(obj);
213 g_value_init(&v, G_TYPE_STRING);
214 g_value_set_string(&v, prop_val);
216 s = g_object_class_find_property(k, prop_name);
218 g_print("Invalid property name: %s\n", prop_name);
222 g_value_init(&p, s->value_type);
223 switch (s->value_type) {
225 g_value_set_int(&p, atoi(prop_val));
228 g_value_set_ulong (&p, atol(prop_val));
231 g_value_set_string(&p, prop_val);
234 g_value_set_boolean(&p, (gboolean) atoi (prop_val));
237 g_value_set_double(&p, atof (prop_val));
240 g_value_set_float(&p, (float) atof (prop_val));
243 g_warning ("Property %s of type %s. Not supported",
244 prop_name, g_type_name (s->value_type));
248 g_object_set_property(obj, prop_name, &p);
254 _create_element_with_prop(const gchar * factory_name,
255 const gchar * element_name, gchar ** prop)
260 ret = gst_element_factory_make(factory_name, element_name);
265 for (i = 0; i < g_strv_length(prop); i++) {
266 if (prop[i] != NULL) {
267 char **v = g_strsplit(prop[i], "=", 2);
268 if (g_strv_length(v) == 2) {
269 _obj_set_prop(G_OBJECT(ret), v[0], v[1]);
281 _create_audio_bin(const gchar * encode, gchar ** encode_prop, gint rate)
283 GstElement *abin = NULL;
284 GstElement *aqueue = NULL;
285 GstElement *aconvert = NULL;
286 GstElement *aencode = NULL;
287 GstElement *aqueue_src = NULL;
290 // audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay !
291 // udpsink name=upd_audio host=224.0.0.1 port=5002
292 abin = gst_bin_new("abin");
293 aqueue = gst_element_factory_make("queue", "aqueue");
294 aconvert = gst_element_factory_make("audioconvert", "aconvert");
296 _create_element_with_prop((encode ? encode : "lame"), "aencode",
298 aqueue_src = gst_element_factory_make("queue", "aqueue_src");
300 if ((abin == NULL) || (aqueue == NULL) || (aconvert == NULL)
301 || (aencode == NULL) || (aqueue_src == NULL)) {
302 g_warning("Audio elements not found");
306 g_object_set(G_OBJECT(aencode), "bitrate", 32, NULL);
308 * if (rate > 0) { g_object_set (G_OBJECT (aencode), "bitrate", 32,
312 gst_bin_add_many(GST_BIN(abin), aqueue, aconvert, aencode, aqueue_src,
314 if (gst_element_link_many(aqueue, aconvert, aencode, aqueue_src, NULL)
316 g_warning("Not Link audio elements");
318 // TODO: apply audio rate
320 // ghost pad the audio bin
321 apad = gst_element_get_pad(aqueue, "sink");
322 gst_element_add_pad(abin, gst_ghost_pad_new("sink", apad));
323 gst_object_unref(apad);
325 apad = gst_element_get_pad(aqueue_src, "src");
326 gst_element_add_pad(abin, gst_ghost_pad_new("src", apad));
327 gst_object_unref(apad);
332 gst_object_unref(abin);
335 gst_object_unref(aqueue);
337 if (aconvert != NULL)
338 gst_object_unref(aconvert);
341 gst_object_unref(aencode);
343 if (aqueue_src != NULL)
344 gst_object_unref(aqueue_src);
347 gst_object_unref(apad);
355 // queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! colorspace
356 // ! rate ! encode ! queue
358 _create_video_bin(const gchar * encode,
359 gchar ** encode_prop,
360 gdouble fps, gint rate, guint width, guint height)
362 GstElement *vbin = NULL;
363 GstElement *vqueue = NULL;
364 GstElement *vqueue_src = NULL;
365 GstElement *vcolorspace = NULL;
366 GstElement *vencode = NULL;
367 GstElement *vrate = NULL;
370 vbin = gst_bin_new("vbin");
371 vqueue = gst_element_factory_make("queue", "vqueue");
373 gst_element_factory_make("ffmpegcolorspace", "colorspace");
375 vencode = _create_element_with_prop((encode !=
377 "ffenc_mpeg1video"), "vencode",
379 vqueue_src = gst_element_factory_make("queue", "queue_src");
381 if ((vbin == NULL) || (vqueue == NULL) || (vcolorspace == NULL)
382 || (vencode == NULL) || (vqueue_src == NULL)) {
383 g_warning("Video elements not found");
387 gst_bin_add_many(GST_BIN(vbin), vqueue, vcolorspace, vencode,
390 if ((width > 0) && (height > 0)) {
394 gst_element_factory_make("videoscale", "vscale");
396 gst_bin_add(GST_BIN(vbin), vscale);
398 vcaps = gst_caps_new_simple("video/x-raw-yuv",
399 "width", G_TYPE_INT, width,
400 "height", G_TYPE_INT, height, NULL);
402 gst_element_link(vqueue, vscale);
404 if (gst_element_link_filtered(vscale, vcolorspace, vcaps) == FALSE) {
405 g_warning("Fail to resize video");
406 gst_object_unref(vcaps);
407 gst_object_unref(vscale);
410 gst_caps_unref(vcaps);
412 gst_element_link(vqueue, vcolorspace);
416 // Changing the video fps
418 vrate = gst_element_factory_make("videorate", "vrate");
420 gst_bin_add(GST_BIN(vbin), vrate);
422 if (gst_element_link(vcolorspace, vrate) == FALSE) {
423 g_warning("Fail to link video elements");
427 vcaps = gst_caps_new_simple("video/x-raw-yuv",
428 "framerate", GST_TYPE_FRACTION,
429 (int) (fps * 1000), 1000, NULL);
431 if (gst_element_link_filtered(vrate, vencode, vcaps) == FALSE) {
432 g_warning("Fail to link vrate with vencode.");
435 gst_caps_unref(vcaps);
437 if (gst_element_link(vcolorspace, vencode) == FALSE) {
438 g_warning("Fail to link colorspace and video encode element.");
443 gst_element_link(vencode, vqueue_src);
445 // ghost pad the video bin
446 vpad = gst_element_get_pad(vqueue, "sink");
447 gst_element_add_pad(vbin, gst_ghost_pad_new("sink", vpad));
448 gst_object_unref(vpad);
450 vpad = gst_element_get_pad(vqueue_src, "src");
451 gst_element_add_pad(vbin, gst_ghost_pad_new("src", vpad));
452 gst_object_unref(vpad);
458 gst_object_unref(vpad);
461 gst_object_unref(vbin);
464 gst_object_unref(vqueue);
467 gst_object_unref(vencode);
469 if (vqueue_src != NULL)
470 gst_object_unref(vqueue_src);
472 if (vcolorspace != NULL)
473 gst_object_unref(vcolorspace);
481 g_mencoder_setup_stream(GMencoder * self,
483 const gchar * mux_name,
484 const gchar * video_encode,
485 gchar ** video_encode_prop,
490 const gchar * audio_encode,
491 gchar ** audio_encode_prop,
492 guint audio_rate, const gchar * out_uri)
494 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
495 if (priv->ready == TRUE) {
497 ("Stream already configured. You need close stream first.");
502 if (_open_output(self, out_uri) == FALSE)
506 priv->send_chunked = chunked;
507 priv->pipe = _create_pipeline(self,
515 audio_encode, audio_encode_prop,
518 return (priv->pipe != NULL);
523 g_mencoder_append_uri(GMencoder * self, const gchar * uri)
528 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
529 gboolean ret = FALSE;
530 GstElement *ap = NULL;
531 GstElement *vp = NULL;
534 g_return_val_if_fail(priv->pipe != NULL, FALSE);
535 g_return_val_if_fail(priv->ready == FALSE, FALSE);
537 #ifndef SUPPORT_MULT_INPUT
538 g_return_val_if_fail(priv->sources < 1, FALSE);
541 src = _create_source(uri);
545 priv->src = gst_bin_get_by_name(GST_BIN(src), "src");
547 gst_bin_add(GST_BIN(priv->pipe), src);
549 #ifdef SUPPORT_MULT_INPUT
550 ap = gst_bin_get_by_name(GST_BIN(priv->pipe), "ap");
551 vp = gst_bin_get_by_name(GST_BIN(priv->pipe), "vp");
553 ap = gst_bin_get_by_name(GST_BIN(priv->pipe), "abin");
554 vp = gst_bin_get_by_name(GST_BIN(priv->pipe), "vbin");
557 if ((vp == NULL) || (ap == NULL)) {
558 g_warning("Fail to get output bin");
562 pad_src = gst_element_get_pad(src, "src_audio");
563 pad_sink = gst_element_get_compatible_pad(ap,
565 gst_pad_get_caps(pad_src));
567 if ((pad_sink == NULL) || (pad_src == NULL))
570 GstPadLinkReturn lret = gst_pad_link(pad_src, pad_sink);
571 if (lret != GST_PAD_LINK_OK)
574 gst_object_unref(pad_src);
575 gst_object_unref(pad_sink);
577 pad_src = gst_element_get_pad(src, "src_video");
578 pad_sink = gst_element_get_compatible_pad(vp,
580 gst_pad_get_caps(pad_src));
582 if ((pad_src == NULL) || (pad_sink == NULL))
585 if (gst_pad_link(pad_src, pad_sink) != GST_PAD_LINK_OK) {
586 g_warning("invalid source. video");
594 if ((src != NULL) && (ret == FALSE)) {
595 gst_bin_remove(GST_BIN(priv->pipe), src);
596 gst_object_unref(src);
600 gst_object_unref(ap);
603 gst_object_unref(vp);
606 gst_object_unref(pad_src);
608 if (pad_sink != NULL)
609 gst_object_unref(pad_sink);
617 g_mencoder_remove_uri(GMencoder * self, const gchar * uri)
619 // GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
624 g_mencoder_play_stream(GMencoder * self)
626 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
627 g_return_if_fail(priv->ready == FALSE);
629 gst_element_set_state(priv->pipe, GST_STATE_PLAYING);
630 priv->tick_id = g_timeout_add(500, _tick_cb, self);
634 g_mencoder_pause_stream(GMencoder * self)
636 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
637 g_return_if_fail(priv->ready == TRUE);
638 gst_element_set_state(priv->pipe, GST_STATE_PAUSED);
642 g_mencoder_close_stream(GMencoder * self)
645 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
646 if (priv->tick_id != 0) {
647 g_source_remove(priv->tick_id);
651 if (priv->pipe != NULL) {
652 // TODO: fixe pipeline dispose
653 // gst_element_set_state (priv->pipe, GST_STATE_NULL);
654 // g_debug ("SETING STATE TO NULL: OK");
655 // gst_element_set_state (priv->pipe, GST_STATE_NULL);
656 // gst_object_unref (priv->pipe);
657 gst_object_unref(priv->src);
668 _create_pipeline(GMencoder * self,
669 const gchar * video_encode,
670 const gchar * mux_name,
671 gchar ** video_encode_prop,
676 const gchar * audio_encode,
677 gchar ** audio_encode_prop, guint audio_rate)
680 GstElement *pipe = NULL;
681 GstElement *sink = NULL;
682 GstElement *mux = NULL;
683 GstElement *abin = NULL;
684 GstElement *vbin = NULL;
685 GstElement *queue = NULL;
686 GstPad *aux_pad = NULL;
687 GstPad *mux_pad = NULL;
688 #ifdef SUPPORT_MULT_INPUT
689 GstElement *ap = NULL;
690 GstElement *vp = NULL;
693 pipe = gst_pipeline_new("pipe");
695 #ifdef SUPPORT_MULT_INPUT
696 ap = gst_element_factory_make("concatmux", "ap");
697 vp = gst_element_factory_make("concatmux", "vp");
698 gst_bin_add_many(GST_BIN(pipe), ap, vp, NULL);
700 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
703 gst_element_factory_make((mux_name ? mux_name : "ffmux_mpeg"),
708 queue = gst_element_factory_make("queue", "queueu_sink");
711 #ifdef USE_MANUAL_SINK
712 sink = gst_element_factory_make("fakesink", "sink");
713 g_object_set (G_OBJECT (sink), "signal-handoffs", TRUE, NULL);
714 g_signal_connect (G_OBJECT (sink),
716 G_CALLBACK (_buffer_arrive_cb),
719 sink = gst_element_factory_make("fdsink", "sink");
723 g_object_set(G_OBJECT(sink), "fd", priv->fd, "sync", FALSE, NULL);
726 abin = _create_audio_bin(audio_encode, audio_encode_prop, audio_rate);
731 _create_video_bin(video_encode, video_encode_prop, video_fps,
732 video_rate, video_width, video_height);
737 gst_bin_add_many(GST_BIN(pipe), abin, vbin, mux, queue, sink, NULL);
740 #ifdef SUPPORT_MULT_INPUT
741 if (gst_element_link(ap, abin) == FALSE) {
742 g_warning("Fail to link concat and abin");
746 if (gst_element_link(vp, vbin) == FALSE) {
747 g_warning("Fail to link concat and vbin");
751 // Link bins with mux
752 aux_pad = gst_element_get_pad(abin, "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 audio PAD");
760 GstPadLinkReturn ret = gst_pad_link(aux_pad, mux_pad);
761 if (ret != GST_PAD_LINK_OK) {
762 g_warning("Fail link audio and mux: %d", ret);
766 gst_object_unref(aux_pad);
767 gst_object_unref(mux_pad);
769 aux_pad = gst_element_get_pad(vbin, "src");
771 gst_element_get_compatible_pad(mux, aux_pad,
772 GST_PAD_CAPS(aux_pad));
773 if (mux_pad == NULL) {
774 g_warning("Mux element no have video PAD");
777 ret = gst_pad_link(aux_pad, mux_pad);
778 if (ret != GST_PAD_LINK_OK) {
779 g_warning("Fail link video and mux: %d", ret);
782 gst_object_unref(aux_pad);
783 gst_object_unref(mux_pad);
787 // Link mux with sink
788 gst_element_link_many(mux, queue, sink, NULL);
790 bus = gst_pipeline_get_bus(GST_PIPELINE(pipe));
791 gst_bus_add_watch(bus, _pipeline_bus_cb, self);
792 gst_object_unref(bus);
796 g_warning("Invalid uri");
799 gst_object_unref(pipe);
804 gst_object_unref(mux);
807 if (mux_pad != NULL) {
808 gst_object_unref(mux_pad);
811 if (aux_pad != NULL) {
812 gst_object_unref(mux_pad);
816 gst_object_unref(sink);
820 gst_object_unref(abin);
824 gst_object_unref(vbin);
832 _close_output(GMencoder * self)
837 _create_source(const gchar * uri)
840 GstElement *bsrc = NULL;
841 GstElement *src = NULL;
842 GstElement *queue = NULL;
843 GstElement *aqueue = NULL;
844 GstElement *vqueue = NULL;
845 GstElement *decode = NULL;
846 GstPad *src_pad = NULL;
849 bsrc = gst_bin_new(NULL);
851 // src = gst_element_factory_make ("gnomevfssrc", "src");
852 // g_object_set (G_OBJECT (src), "location", uri, NULL);
853 src = gst_element_make_from_uri(GST_URI_SRC, uri, "src");
857 decode = gst_element_factory_make("decodebin", "decode");
861 queue = gst_element_factory_make("queue", "queue_src");
862 aqueue = gst_element_factory_make("queue", "aqueue");
866 vqueue = gst_element_factory_make("queue", "vqueue");
870 gst_bin_add_many(GST_BIN(bsrc), src, queue, decode, aqueue, vqueue,
872 gst_element_link_many(src, queue, decode, NULL);
874 g_signal_connect(G_OBJECT(decode),
876 G_CALLBACK(_decodebin_new_pad_cb), bsrc);
878 g_signal_connect(G_OBJECT(decode),
880 G_CALLBACK(_decodebin_unknown_type_cb), pipe);
882 src_pad = gst_element_get_pad(aqueue, "src");
883 gst_element_add_pad(bsrc, gst_ghost_pad_new("src_audio", src_pad));
884 gst_object_unref(src_pad);
886 src_pad = gst_element_get_pad(vqueue, "src");
887 gst_element_add_pad(bsrc, gst_ghost_pad_new("src_video", src_pad));
888 gst_object_unref(src_pad);
894 gst_object_unref(src);
897 if (decode != NULL) {
898 gst_object_unref(decode);
901 if (aqueue != NULL) {
902 gst_object_unref(aqueue);
905 if (vqueue != NULL) {
906 gst_object_unref(vqueue);
913 _open_output(GMencoder * self, const gchar * uri)
917 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
919 i = g_strsplit(uri, "://", 0);
920 if (strcmp(i[0], "fd") == 0) {
921 priv->fd = atoi(i[1]);
922 if (priv->send_chunked)
923 fcntl (priv->fd, F_SETFL, O_ASYNC);
924 } else if (strcmp(i[0], "file") == 0) {
925 if (g_file_test (i[1], G_FILE_TEST_EXISTS)) {
926 if (unlink (i[1]) != 0) {
927 g_warning ("Fail to write in : %s", uri);
932 priv->fd = open(i[1], O_WRONLY | O_CREAT | O_TRUNC,
933 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
935 if (priv->fd == -1) {
936 g_warning ("Fail to open : %s", uri);
940 g_warning("Output uri not supported");
950 _pipeline_bus_cb(GstBus * bus, GstMessage * msg, gpointer user_data)
952 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
954 switch (GST_MESSAGE_TYPE(msg)) {
956 case GST_MESSAGE_STATE_CHANGED:
960 GstState pendingstate;
963 gst_message_parse_state_changed(msg, &oldstate,
964 &newstate, &pendingstate);
966 if (pendingstate != GST_STATE_VOID_PENDING)
969 if ((oldstate == GST_STATE_READY)
970 && (newstate == GST_STATE_PAUSED)) {
972 g_signal_emit(user_data, g_mencoder_signals[PAUSED],
974 } else if ((oldstate == GST_STATE_PAUSED)
975 && (newstate == GST_STATE_PLAYING)) {
976 g_signal_emit(user_data, g_mencoder_signals[PLAYING], 0);
977 } else if ((oldstate == GST_STATE_READY) &&
978 (newstate == GST_STATE_NULL)) {
979 g_signal_emit(user_data, g_mencoder_signals[STOPED], 0);
984 case GST_MESSAGE_ERROR:
990 if (priv->tick_id != 0) {
991 g_source_remove(priv->tick_id);
995 gst_message_parse_error(msg, &error, &debug);
996 err_str = g_strdup_printf("Error [%d] %s (%s)", error->code,
997 error->message, debug);
999 g_signal_emit(user_data, g_mencoder_signals[ERROR], 0,
1002 g_clear_error(&error);
1007 case GST_MESSAGE_EOS:
1008 priv->ready = FALSE;
1009 #ifdef USE_MANUAL_SINK
1010 _flush_queue (G_MENCODER (user_data));
1012 g_signal_emit(user_data, g_mencoder_signals[EOS], 0);
1015 case GST_MESSAGE_DURATION:
1019 gst_message_parse_duration(msg, &format, &duration);
1020 if (format == GST_FORMAT_BYTES)
1021 priv->duration = duration;
1035 _decodebin_new_pad_cb(GstElement * object,
1036 GstPad * pad, gboolean flag, gpointer user_data)
1039 gchar *str_caps = NULL;
1040 GstElement *sink_element;
1043 caps = gst_pad_get_caps(pad);
1044 str_caps = gst_caps_to_string(caps);
1045 if (strstr(str_caps, "audio") != NULL) {
1046 sink_element = gst_bin_get_by_name(GST_BIN(user_data), "aqueue");
1047 } else if (strstr(str_caps, "video") != NULL) {
1048 sink_element = gst_bin_get_by_name(GST_BIN(user_data), "vqueue");
1050 g_warning("invalid caps %s", str_caps);
1053 sink_pad = gst_element_get_pad(sink_element, "sink");
1054 gst_pad_link(pad, sink_pad);
1056 gst_object_unref(sink_element);
1057 gst_object_unref(sink_pad);
1059 gst_caps_unref(caps);
1063 _decodebin_unknown_type_cb(GstElement * object,
1064 GstPad * pad, GstCaps * caps,
1067 g_warning("Unknown Type");
1068 // priv->ready = FALSE;
1072 _tick_cb(gpointer user_data)
1074 GstFormat format = GST_FORMAT_BYTES;
1077 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
1079 if (priv->duration == 0) {
1081 if (gst_element_query_duration(priv->src, &format, &d))
1085 if (priv->duration != 0) {
1086 gst_element_query_position(priv->src, &format, &cur);
1087 g_print("PROGRESS:%lli\n", (99 * cur) / priv->duration);
1094 #ifdef USE_MANUAL_SINK
1096 _send_buffer (gint fd, gpointer buff, gint size)
1098 gboolean ret = TRUE;
1102 b_send = g_byte_array_new ();
1103 msg = g_strdup_printf ("%x\r\n", size);
1104 b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
1107 b_send = g_byte_array_append (b_send, buff, size);
1109 msg = g_strdup ("\r\n");
1110 b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
1113 if (send (fd, b_send->data, b_send->len, MSG_MORE) <= 0)
1115 g_byte_array_free (b_send, TRUE);
1121 _flush_queue (GMencoder *self)
1124 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
1126 if (BUFFER_SIZE == 0)
1129 if (priv->queue->len > 0) {
1130 _send_buffer (priv->fd, priv->queue->data, priv->queue->len);
1131 priv->queue = g_byte_array_remove_range (priv->queue, 0, priv->queue->len);
1134 end_msg = g_strdup ("0\r\n\r\n");
1135 write (priv->fd, (const guint8*) end_msg, strlen(end_msg) * sizeof(gchar));
1140 _buffer_arrive_cb (GstElement* object,
1145 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
1147 if (BUFFER_SIZE == 0) {
1148 if (_send_buffer (priv->fd, GST_BUFFER_DATA (buff), GST_BUFFER_SIZE (buff)) == FALSE)
1153 priv->queue = g_byte_array_append (priv->queue, GST_BUFFER_DATA (buff), GST_BUFFER_SIZE (buff));
1154 while (priv->queue->len >= BUFFER_SIZE) {
1155 //g_usleep (0.2 * G_USEC_PER_SEC);
1156 if (priv->send_chunked) {
1157 if (_send_buffer (priv->fd, priv->queue->data, BUFFER_SIZE) == FALSE)
1160 if (write (priv->fd, priv->queue->data, BUFFER_SIZE) == -1)
1163 priv->queue = g_byte_array_remove_range (priv->queue, 0, BUFFER_SIZE);
1168 if (priv->tick_id != 0) {
1169 g_source_remove(priv->tick_id);
1172 priv->queue = g_byte_array_remove_range (priv->queue, 0, priv->queue->len);
1173 g_signal_emit(user_data, g_mencoder_signals[ERROR], 0, "Error on socket");