11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <libgnomevfs/gnome-vfs.h>
14 #include <gst/interfaces/tuner.h>
16 #include "gmencoder.h"
18 #define G_MENCODER_GET_PRIVATE(obj) \
19 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), G_TYPE_MENCODER, GMencoderPrivate))
21 #define USE_MANUAL_SINK
23 typedef struct _GMencoderPrivate GMencoderPrivate;
24 typedef struct _SetupInfo SetupInfo;
29 gchar **video_encode_prop;
35 gchar **audio_encode_prop;
40 struct _GMencoderPrivate {
47 GnomeVFSHandle *handle;
56 gboolean send_chunked;
75 static void g_mencoder_class_init(GMencoderClass * klass);
76 static void g_mencoder_init(GMencoder * object);
77 static void g_mencoder_dispose(GObject * object);
78 static void g_mencoder_finalize(GObject * object);
79 static GstElement *_create_audio_bin(const gchar * encode,
80 gchar ** encode_prop, gint rate);
81 static GstElement *_create_video_bin(const gchar * encode,
84 gint rate, guint width, guint height,
85 gboolean use_deinterlace);
88 _pipeline_bus_cb(GstBus * bus, GstMessage * msg, gpointer user_data);
90 static void _decodebin_new_pad_cb(GstElement * object,
92 gboolean flag, gpointer user_data);
94 static void _decodebin_unknown_type_cb(GstElement * object,
99 static void _close_output(GMencoder * self);
100 static gboolean _open_output(GMencoder * self, const gchar * uri);
102 static GstElement *_create_source(GMencoder *self, const gchar * uri);
103 static GstElement *_create_pipeline(GMencoder * self,
104 const gchar * video_encode,
105 const gchar * mux_name,
106 gchar ** video_encode_prop,
111 const gchar * audio_encode,
112 gchar ** audio_encode_prop,
114 gboolean deinterlace);
115 #ifdef USE_MANUAL_SINK
116 static void _flush_queue (GMencoder *self);
117 static void _buffer_arrive_cb (GstElement* object,
124 static gboolean _tick_cb(gpointer data);
126 static guint g_mencoder_signals[LAST_SIGNAL] = { 0 };
128 G_DEFINE_TYPE(GMencoder, g_mencoder, G_TYPE_OBJECT)
130 static void g_mencoder_class_init(GMencoderClass * klass)
132 GObjectClass *object_class;
133 object_class = (GObjectClass *) klass;
134 g_type_class_add_private(klass, sizeof(GMencoderPrivate));
136 object_class->dispose = g_mencoder_dispose;
137 object_class->finalize = g_mencoder_finalize;
139 g_mencoder_signals[PAUSED] =
140 g_signal_new("paused",
141 G_OBJECT_CLASS_TYPE(object_class),
144 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
146 g_mencoder_signals[PLAYING] =
147 g_signal_new("playing",
148 G_OBJECT_CLASS_TYPE(object_class),
151 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
153 g_mencoder_signals[STOPED] =
154 g_signal_new("stoped",
155 G_OBJECT_CLASS_TYPE(object_class),
158 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
160 g_mencoder_signals[EOS] =
162 G_OBJECT_CLASS_TYPE(object_class),
165 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
167 g_mencoder_signals[ERROR] =
168 g_signal_new("error",
169 G_OBJECT_CLASS_TYPE(object_class),
172 g_cclosure_marshal_VOID__STRING,
173 G_TYPE_NONE, 1, G_TYPE_STRING);
177 g_mencoder_init(GMencoder * self)
179 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
180 priv->info = g_new0(SetupInfo, 1);
184 g_mencoder_dispose(GObject * object)
189 g_mencoder_finalize(GObject * object)
191 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(object);
194 g_mencoder_close_stream(G_MENCODER(object));
201 return g_object_new(G_TYPE_MENCODER, NULL);
206 _obj_set_prop(GObject * obj, const gchar * prop_name,
207 const gchar * prop_val)
211 GParamSpec *s = NULL;
212 GObjectClass *k = G_OBJECT_GET_CLASS(obj);
215 g_value_init(&v, G_TYPE_STRING);
216 g_value_set_string(&v, prop_val);
218 s = g_object_class_find_property(k, prop_name);
220 g_print("Invalid property name: %s\n", prop_name);
224 g_value_init(&p, s->value_type);
225 switch (s->value_type) {
227 g_value_set_int(&p, atoi(prop_val));
230 g_value_set_ulong (&p, atol(prop_val));
233 g_value_set_string(&p, prop_val);
236 g_value_set_boolean(&p, (gboolean) atoi (prop_val));
239 g_value_set_double(&p, atof (prop_val));
242 g_value_set_float(&p, (float) atof (prop_val));
245 g_value_set_enum(&p, atoi(prop_val));
246 g_warning ("Property %s of type %s. Not supported using default enum",
247 prop_name, g_type_name (s->value_type));
251 g_object_set_property(obj, prop_name, &p);
257 _create_element_with_prop(const gchar * factory_name,
258 const gchar * element_name, gchar ** prop)
263 ret = gst_element_factory_make(factory_name, element_name);
268 for (i = 0; i < g_strv_length(prop); i++) {
269 if (prop[i] != NULL) {
270 char **v = g_strsplit(prop[i], "=", 2);
271 if (g_strv_length(v) == 2) {
272 _obj_set_prop(G_OBJECT(ret), v[0], v[1]);
284 _create_audio_bin(const gchar * encode, gchar ** encode_prop, gint rate)
286 GstElement *abin = NULL;
287 GstElement *aqueue = NULL;
288 GstElement *aconvert = NULL;
289 GstElement *aencode = NULL;
290 GstElement *aqueue_src = NULL;
293 // audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay !
294 // udpsink name=upd_audio host=224.0.0.1 port=5002
295 abin = gst_bin_new("abin");
296 aqueue = gst_element_factory_make("queue", "aqueue");
297 aconvert = gst_element_factory_make("audioconvert", "aconvert");
299 _create_element_with_prop((encode ? encode : "lame"), "aencode",
301 aqueue_src = gst_element_factory_make("queue", "aqueue_src");
303 if ((abin == NULL) || (aqueue == NULL) || (aconvert == NULL)
304 || (aencode == NULL) || (aqueue_src == NULL)) {
305 g_warning("Audio elements not found");
309 g_object_set(G_OBJECT(aencode), "bitrate", 32, NULL);
311 * if (rate > 0) { g_object_set (G_OBJECT (aencode), "bitrate", 32,
315 gst_bin_add_many(GST_BIN(abin), aqueue, aconvert, aencode, aqueue_src,
317 if (gst_element_link_many(aqueue, aconvert, aencode, aqueue_src, NULL)
319 g_warning("Not Link audio elements");
321 // TODO: apply audio rate
323 // ghost pad the audio bin
324 apad = gst_element_get_pad(aqueue, "sink");
325 gst_element_add_pad(abin, gst_ghost_pad_new("sink", apad));
326 gst_object_unref(apad);
328 apad = gst_element_get_pad(aqueue_src, "src");
329 gst_element_add_pad(abin, gst_ghost_pad_new("src", apad));
330 gst_object_unref(apad);
335 gst_object_unref(abin);
338 gst_object_unref(aqueue);
340 if (aconvert != NULL)
341 gst_object_unref(aconvert);
344 gst_object_unref(aencode);
346 if (aqueue_src != NULL)
347 gst_object_unref(aqueue_src);
350 gst_object_unref(apad);
358 // queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! colorspace
359 // ! rate ! encode ! queue
361 _create_video_bin(const gchar * encode,
362 gchar ** encode_prop,
363 gdouble fps, gint rate, guint width, guint height,
364 gboolean use_deinterlace)
366 GstElement *vbin = NULL;
367 GstElement *vqueue = NULL;
368 GstElement *vqueue_src = NULL;
369 GstElement *vcolorspace = NULL;
370 GstElement *vencode = NULL;
371 GstElement *vrate = NULL;
372 GstElement *deinterlace = NULL;
373 GstElement *walk = NULL;
376 vbin = gst_bin_new("vbin");
377 vqueue = gst_element_factory_make("queue", "vqueue");
379 gst_element_factory_make("ffmpegcolorspace", "colorspace");
381 if (use_deinterlace) {
382 deinterlace = gst_element_factory_make ("ffdeinterlace", "deinterlace");
383 if (deinterlace == NULL) {
384 g_warning ("Fail to create deinterlace element: Continue without deinterlace.");
389 vencode = _create_element_with_prop((encode !=
391 "ffenc_mpeg1video"), "vencode",
393 vqueue_src = gst_element_factory_make("queue", "queue_src");
395 if ((vbin == NULL) || (vqueue == NULL) || (vcolorspace == NULL)
396 || (vencode == NULL) || (vqueue_src == NULL)) {
397 g_warning("Video elements not found");
401 gst_bin_add_many(GST_BIN(vbin), vqueue, vcolorspace, vencode,
404 if (deinterlace != NULL) {
405 gst_bin_add(GST_BIN(vbin), deinterlace);
406 gst_element_link (vqueue, deinterlace);
412 if ((width > 0) && (height > 0)) {
416 gst_element_factory_make("videoscale", "vscale");
418 g_object_set (G_OBJECT (vscale), "method", 1, NULL);
420 gst_bin_add(GST_BIN(vbin), vscale);
422 vcaps = gst_caps_new_simple("video/x-raw-yuv",
423 "width", G_TYPE_INT, width,
424 "height", G_TYPE_INT, height, NULL);
426 gst_element_link(walk, vscale);
428 if (gst_element_link_filtered(vscale, vcolorspace, vcaps) == FALSE) {
429 g_warning("Fail to resize video");
430 gst_object_unref(vcaps);
431 gst_object_unref(vscale);
434 gst_caps_unref(vcaps);
436 gst_element_link(walk, vcolorspace);
440 // Changing the video fps
442 vrate = gst_element_factory_make("videorate", "vrate");
444 gst_bin_add(GST_BIN(vbin), vrate);
446 if (gst_element_link(vcolorspace, vrate) == FALSE) {
447 g_warning("Fail to link video elements");
451 vcaps = gst_caps_new_simple("video/x-raw-yuv",
452 "framerate", GST_TYPE_FRACTION,
453 (int) (fps * 1000), 1000, NULL);
455 if (gst_element_link_filtered(vrate, vencode, vcaps) == FALSE) {
456 g_warning("Fail to link vrate with vencode.");
459 gst_caps_unref(vcaps);
461 if (gst_element_link(vcolorspace, vencode) == FALSE) {
462 g_warning("Fail to link colorspace and video encode element.");
467 gst_element_link(vencode, vqueue_src);
469 // ghost pad the video bin
470 vpad = gst_element_get_pad(vqueue, "sink");
471 gst_element_add_pad(vbin, gst_ghost_pad_new("sink", vpad));
472 gst_object_unref(vpad);
474 vpad = gst_element_get_pad(vqueue_src, "src");
475 gst_element_add_pad(vbin, gst_ghost_pad_new("src", vpad));
476 gst_object_unref(vpad);
482 gst_object_unref(vpad);
485 gst_object_unref(vbin);
488 gst_object_unref(vqueue);
491 gst_object_unref(vencode);
493 if (vqueue_src != NULL)
494 gst_object_unref(vqueue_src);
496 if (vcolorspace != NULL)
497 gst_object_unref(vcolorspace);
505 g_mencoder_setup_stream(GMencoder * self,
507 gboolean deinterlace,
508 const gchar * mux_name,
509 const gchar * video_encode,
510 gchar ** video_encode_prop,
515 const gchar * audio_encode,
516 gchar ** audio_encode_prop,
517 guint audio_rate, const gchar * out_uri)
519 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
520 if (priv->ready == TRUE) {
522 ("Stream already configured. You need close stream first.");
527 if (_open_output(self, out_uri) == FALSE) {
532 priv->send_chunked = chunked;
533 priv->pipe = _create_pipeline(self,
541 audio_encode, audio_encode_prop,
545 return (priv->pipe != NULL);
550 g_mencoder_append_uri(GMencoder * self, const gchar * uri)
555 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
556 gboolean ret = FALSE;
557 GstElement *ap = NULL;
558 GstElement *vp = NULL;
561 g_return_val_if_fail(priv->pipe != NULL, FALSE);
562 g_return_val_if_fail(priv->ready == FALSE, FALSE);
564 src = _create_source(self, uri);
568 priv->src = gst_bin_get_by_name(GST_BIN(src), "src");
570 gst_bin_add(GST_BIN(priv->pipe), src);
572 ap = gst_bin_get_by_name(GST_BIN(priv->pipe), "abin");
573 vp = gst_bin_get_by_name(GST_BIN(priv->pipe), "vbin");
575 if ((vp == NULL) || (ap == NULL)) {
576 g_warning("Fail to get output bin");
580 pad_src = gst_element_get_pad(src, "src_audio");
581 pad_sink = gst_element_get_compatible_pad(ap,
583 gst_pad_get_caps(pad_src));
585 if ((pad_sink == NULL) || (pad_src == NULL))
588 GstPadLinkReturn lret = gst_pad_link(pad_src, pad_sink);
589 if (lret != GST_PAD_LINK_OK)
592 gst_object_unref(pad_src);
593 gst_object_unref(pad_sink);
595 pad_src = gst_element_get_pad(src, "src_video");
596 pad_sink = gst_element_get_compatible_pad(vp,
598 gst_pad_get_caps(pad_src));
600 if ((pad_src == NULL) || (pad_sink == NULL))
603 if (gst_pad_link(pad_src, pad_sink) != GST_PAD_LINK_OK) {
604 g_warning("invalid source. video");
612 if ((src != NULL) && (ret == FALSE)) {
613 gst_bin_remove(GST_BIN(priv->pipe), src);
614 gst_object_unref(src);
618 gst_object_unref(ap);
621 gst_object_unref(vp);
624 gst_object_unref(pad_src);
626 if (pad_sink != NULL)
627 gst_object_unref(pad_sink);
635 g_mencoder_remove_uri(GMencoder * self, const gchar * uri)
637 // GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
642 g_mencoder_play_stream(GMencoder * self)
644 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
645 g_return_if_fail(priv->ready == FALSE);
647 gst_element_set_state(priv->pipe, GST_STATE_PLAYING);
648 priv->tick_id = g_timeout_add(500, _tick_cb, self);
652 g_mencoder_pause_stream(GMencoder * self)
654 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
655 g_return_if_fail(priv->ready == TRUE);
656 gst_element_set_state(priv->pipe, GST_STATE_PAUSED);
660 g_mencoder_close_stream(GMencoder * self)
663 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
664 if (priv->tick_id != 0) {
665 g_source_remove(priv->tick_id);
669 if (priv->pipe != NULL) {
670 // TODO: fixe pipeline dispose
671 //gst_element_set_state (priv->pipe, GST_STATE_NULL);
672 // g_debug ("SETING STATE TO NULL: OK");
673 // gst_element_set_state (priv->pipe, GST_STATE_NULL);
674 //gst_object_unref (priv->pipe);
675 //gst_object_unref(priv->src);
686 _create_pipeline(GMencoder * self,
687 const gchar * video_encode,
688 const gchar * mux_name,
689 gchar ** video_encode_prop,
694 const gchar * audio_encode,
695 gchar ** audio_encode_prop, guint audio_rate,
696 gboolean deinterlace)
699 GstElement *pipe = NULL;
700 GstElement *sink = NULL;
701 GstElement *mux = NULL;
702 GstElement *abin = NULL;
703 GstElement *vbin = NULL;
704 GstElement *queue = NULL;
705 GstPad *aux_pad = NULL;
706 GstPad *mux_pad = NULL;
708 pipe = gst_pipeline_new("pipe");
711 gst_element_factory_make((mux_name ? mux_name : "ffmux_mpeg"),
716 queue = gst_element_factory_make("queue", "queueu_sink");
719 sink = gst_element_factory_make("fakesink", "sink");
720 g_object_set (G_OBJECT (sink), "signal-handoffs", TRUE, NULL);
721 g_signal_connect (G_OBJECT (sink),
723 G_CALLBACK (_buffer_arrive_cb),
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, deinterlace);
737 gst_bin_add_many(GST_BIN(pipe), abin, vbin, mux, queue, sink, NULL);
740 // Link bins with mux
741 aux_pad = gst_element_get_pad(abin, "src");
743 gst_element_get_compatible_pad(mux, aux_pad,
744 GST_PAD_CAPS(aux_pad));
745 if (mux_pad == NULL) {
746 g_warning("Mux element no have audio PAD");
749 GstPadLinkReturn ret = gst_pad_link(aux_pad, mux_pad);
750 if (ret != GST_PAD_LINK_OK) {
751 g_warning("Fail link audio and mux: %d", ret);
755 gst_object_unref(aux_pad);
756 gst_object_unref(mux_pad);
758 aux_pad = gst_element_get_pad(vbin, "src");
760 gst_element_get_compatible_pad(mux, aux_pad,
761 GST_PAD_CAPS(aux_pad));
762 if (mux_pad == NULL) {
763 g_warning("Mux element no have video PAD");
766 ret = gst_pad_link(aux_pad, mux_pad);
767 if (ret != GST_PAD_LINK_OK) {
768 g_warning("Fail link video and mux: %d", ret);
771 gst_object_unref(aux_pad);
772 gst_object_unref(mux_pad);
776 // Link mux with sink
777 gst_element_link_many(mux, queue, sink, NULL);
779 bus = gst_pipeline_get_bus(GST_PIPELINE(pipe));
780 gst_bus_add_watch(bus, _pipeline_bus_cb, self);
781 gst_object_unref(bus);
785 g_warning("Invalid uri");
788 gst_object_unref(pipe);
793 gst_object_unref(mux);
796 if (mux_pad != NULL) {
797 gst_object_unref(mux_pad);
800 if (aux_pad != NULL) {
801 gst_object_unref(mux_pad);
805 gst_object_unref(sink);
809 gst_object_unref(abin);
813 gst_object_unref(vbin);
821 _close_output(GMencoder * self)
826 _create_v4l_source (GMencoder *self, const gchar * uri)
829 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
832 info = g_strsplit (uri+6, ":", 3);
833 if (g_strv_length (info) != 3) {
837 priv->v4lsrc = gst_element_factory_make ("v4l2src", "src");
838 g_debug ("channel %s, norm %s, frequ %s", info[0], info[1], info[2]);
839 g_object_set (G_OBJECT (priv->v4lsrc),
842 "frequency", atoi (info[2]),
849 _create_source(GMencoder *self, const gchar * uri)
852 GstElement *bsrc = NULL;
853 GstElement *src = NULL;
854 GstElement *queue = NULL;
855 GstElement *aqueue = NULL;
856 GstElement *vqueue = NULL;
857 GstElement *decode = NULL;
858 GstPad *src_pad = NULL;
861 bsrc = gst_bin_new(NULL);
863 // src = gst_element_factory_make ("gnomevfssrc", "src");
864 // g_object_set (G_OBJECT (src), "location", uri, NULL);
865 if (strncmp (uri, "v4l://", 6) == 0) {
867 src = _create_v4l_source (self, uri);
871 src = gst_element_make_from_uri(GST_URI_SRC, uri, "src");
877 decode = gst_element_factory_make("decodebin", "decode");
881 queue = gst_element_factory_make("queue", "queue_src");
882 aqueue = gst_element_factory_make("queue", "aqueue");
886 vqueue = gst_element_factory_make("queue", "vqueue");
890 gst_bin_add_many(GST_BIN(bsrc), src, queue, decode, aqueue, vqueue,
892 gst_element_link_many(src, queue, decode, NULL);
894 g_signal_connect(G_OBJECT(decode),
896 G_CALLBACK(_decodebin_new_pad_cb), bsrc);
898 g_signal_connect(G_OBJECT(decode),
900 G_CALLBACK(_decodebin_unknown_type_cb), pipe);
902 src_pad = gst_element_get_pad(aqueue, "src");
903 gst_element_add_pad(bsrc, gst_ghost_pad_new("src_audio", src_pad));
904 gst_object_unref(src_pad);
906 src_pad = gst_element_get_pad(vqueue, "src");
907 gst_element_add_pad(bsrc, gst_ghost_pad_new("src_video", src_pad));
908 gst_object_unref(src_pad);
913 g_debug ("Fail to create source element");
915 gst_object_unref(src);
918 if (decode != NULL) {
919 gst_object_unref(decode);
922 if (aqueue != NULL) {
923 gst_object_unref(aqueue);
926 if (vqueue != NULL) {
927 gst_object_unref(vqueue);
934 _open_output(GMencoder * self, const gchar * uri)
937 GnomeVFSResult result;
938 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
940 i = g_strsplit(uri, "://", 0);
941 if (strcmp(i[0], "fd") == 0) {
942 result = gnome_vfs_open_fd (&priv->handle, atoi(i[1]));
944 g_debug ("Teste file :%s", i[1]);
945 if (g_file_test (i[1], G_FILE_TEST_EXISTS) == FALSE) {
946 result = gnome_vfs_create (&priv->handle, uri, GNOME_VFS_OPEN_WRITE, TRUE,
947 GNOME_VFS_PERM_USER_WRITE | GNOME_VFS_PERM_USER_READ | GNOME_VFS_PERM_GROUP_READ);
949 result = gnome_vfs_open (&priv->handle, uri,
950 GNOME_VFS_OPEN_WRITE | GNOME_VFS_OPEN_TRUNCATE);
955 return (result == GNOME_VFS_OK);
959 _pipeline_bus_cb(GstBus * bus, GstMessage * msg, gpointer user_data)
961 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
963 switch (GST_MESSAGE_TYPE(msg)) {
965 case GST_MESSAGE_STATE_CHANGED:
969 GstState pendingstate;
972 gst_message_parse_state_changed(msg, &oldstate,
973 &newstate, &pendingstate);
975 if (pendingstate != GST_STATE_VOID_PENDING)
978 if ((oldstate == GST_STATE_READY)
979 && (newstate == GST_STATE_PAUSED)) {
981 g_signal_emit(user_data, g_mencoder_signals[PAUSED],
983 } else if ((oldstate == GST_STATE_PAUSED)
984 && (newstate == GST_STATE_PLAYING)) {
985 g_signal_emit(user_data, g_mencoder_signals[PLAYING], 0);
986 } else if ((oldstate == GST_STATE_READY) &&
987 (newstate == GST_STATE_NULL)) {
988 g_signal_emit(user_data, g_mencoder_signals[STOPED], 0);
993 case GST_MESSAGE_ERROR:
999 if (priv->tick_id != 0) {
1000 g_source_remove(priv->tick_id);
1004 gst_message_parse_error(msg, &error, &debug);
1005 err_str = g_strdup_printf("Error [%d] %s (%s)", error->code,
1006 error->message, debug);
1007 priv->ready = FALSE;
1008 g_signal_emit(user_data, g_mencoder_signals[ERROR], 0,
1011 g_clear_error(&error);
1016 case GST_MESSAGE_EOS:
1017 priv->ready = FALSE;
1018 #ifdef USE_MANUAL_SINK
1019 _flush_queue (G_MENCODER (user_data));
1021 g_signal_emit(user_data, g_mencoder_signals[EOS], 0);
1024 case GST_MESSAGE_DURATION:
1028 gst_message_parse_duration(msg, &format, &duration);
1029 if (format == GST_FORMAT_BYTES)
1030 priv->duration = duration;
1044 _decodebin_new_pad_cb(GstElement * object,
1045 GstPad * pad, gboolean flag, gpointer user_data)
1048 gchar *str_caps = NULL;
1049 GstElement *sink_element;
1052 caps = gst_pad_get_caps(pad);
1053 str_caps = gst_caps_to_string(caps);
1054 if (strstr(str_caps, "audio") != NULL) {
1055 sink_element = gst_bin_get_by_name(GST_BIN(user_data), "aqueue");
1056 } else if (strstr(str_caps, "video") != NULL) {
1057 sink_element = gst_bin_get_by_name(GST_BIN(user_data), "vqueue");
1059 g_warning("invalid caps %s", str_caps);
1062 sink_pad = gst_element_get_pad(sink_element, "sink");
1063 gst_pad_link(pad, sink_pad);
1065 gst_object_unref(sink_element);
1066 gst_object_unref(sink_pad);
1068 gst_caps_unref(caps);
1072 _decodebin_unknown_type_cb(GstElement * object,
1073 GstPad * pad, GstCaps * caps,
1076 g_warning("Unknown Type");
1077 // priv->ready = FALSE;
1081 _tick_cb(gpointer user_data)
1083 GstFormat format = GST_FORMAT_BYTES;
1086 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
1088 if (priv->duration == 0) {
1090 if (gst_element_query_duration(priv->src, &format, &d))
1094 if (priv->duration != 0) {
1095 gst_element_query_position(priv->src, &format, &cur);
1096 g_print("PROGRESS:%lli\n", (99 * cur) / priv->duration);
1103 #ifdef USE_MANUAL_SINK
1105 _send_buffer (GnomeVFSHandle *handle, gpointer buff, gint size)
1109 GnomeVFSResult result;
1110 GnomeVFSFileSize bytes_written;
1112 b_send = g_byte_array_new ();
1113 msg = g_strdup_printf ("%x\r\n", size);
1114 b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
1117 b_send = g_byte_array_append (b_send, buff, size);
1119 msg = g_strdup ("\r\n");
1120 b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
1123 result = gnome_vfs_write (handle, b_send->data, b_send->len, &bytes_written);
1124 g_byte_array_free (b_send, TRUE);
1126 return (result == GNOME_VFS_OK);
1130 _flush_queue (GMencoder *self)
1132 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
1134 if (priv->send_chunked) {
1135 GnomeVFSFileSize bytes_written;
1137 end_msg = g_strdup ("0\r\n\r\n");
1138 gnome_vfs_write (priv->handle,
1139 (const guint8*) end_msg,
1140 strlen(end_msg) * sizeof(gchar),
1147 _buffer_arrive_cb (GstElement* object,
1152 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
1155 if (priv->send_chunked) {
1156 if (_send_buffer (priv->handle, GST_BUFFER_DATA (buff), GST_BUFFER_SIZE (buff)) == FALSE)
1159 GnomeVFSResult result;
1160 GnomeVFSFileSize bytes_written;
1162 result = gnome_vfs_write (priv->handle,
1163 GST_BUFFER_DATA (buff),
1164 GST_BUFFER_SIZE (buff),
1167 if (result != GNOME_VFS_OK)
1174 if (priv->tick_id != 0) {
1175 g_source_remove(priv->tick_id);
1178 g_signal_emit(user_data, g_mencoder_signals[ERROR], 0, "Fail to write on socket");
1179 gst_element_set_state (priv->pipe, GST_STATE_PAUSED);