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
22 #define GMENCODER_TIMEOUT 5000
24 typedef struct _GMencoderPrivate GMencoderPrivate;
25 typedef struct _SetupInfo SetupInfo;
30 gchar **video_encode_prop;
36 gchar **audio_encode_prop;
41 struct _GMencoderPrivate {
48 GnomeVFSHandle *handle;
57 gboolean send_chunked;
76 static void g_mencoder_class_init(GMencoderClass * klass);
77 static void g_mencoder_init(GMencoder * object);
78 static void g_mencoder_dispose(GObject * object);
79 static void g_mencoder_finalize(GObject * object);
80 static GstElement *_create_audio_bin(const gchar * encode,
81 gchar ** encode_prop, gint rate);
82 static GstElement *_create_video_bin(const gchar * encode,
85 gint rate, guint width, guint height,
86 gboolean use_deinterlace);
89 _pipeline_bus_cb(GstBus * bus, GstMessage * msg, gpointer user_data);
91 static void _decodebin_new_pad_cb(GstElement * object,
93 gboolean flag, gpointer user_data);
95 static void _decodebin_unknown_type_cb(GstElement * object,
100 static void _close_output(GMencoder * self);
101 static gboolean _open_output(GMencoder * self, const gchar * uri);
103 static GstElement *_create_source(GMencoder *self, const gchar * uri);
104 static GstElement *_create_pipeline(GMencoder * self,
105 const gchar * video_encode,
106 const gchar * mux_name,
107 gchar ** video_encode_prop,
112 const gchar * audio_encode,
113 gchar ** audio_encode_prop,
115 gboolean deinterlace);
116 static gboolean _process_timeout_cb (gpointer user_data);
117 #ifdef USE_MANUAL_SINK
118 static void _flush_queue (GMencoder *self);
119 static void _buffer_arrive_cb (GstElement* object,
126 static gboolean _tick_cb(gpointer data);
128 static guint g_mencoder_signals[LAST_SIGNAL] = { 0 };
130 G_DEFINE_TYPE(GMencoder, g_mencoder, G_TYPE_OBJECT)
132 static void g_mencoder_class_init(GMencoderClass * klass)
134 GObjectClass *object_class;
135 object_class = (GObjectClass *) klass;
136 g_type_class_add_private(klass, sizeof(GMencoderPrivate));
138 object_class->dispose = g_mencoder_dispose;
139 object_class->finalize = g_mencoder_finalize;
141 g_mencoder_signals[PAUSED] =
142 g_signal_new("paused",
143 G_OBJECT_CLASS_TYPE(object_class),
146 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
148 g_mencoder_signals[PLAYING] =
149 g_signal_new("playing",
150 G_OBJECT_CLASS_TYPE(object_class),
153 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
155 g_mencoder_signals[STOPED] =
156 g_signal_new("stoped",
157 G_OBJECT_CLASS_TYPE(object_class),
160 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
162 g_mencoder_signals[EOS] =
164 G_OBJECT_CLASS_TYPE(object_class),
167 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
169 g_mencoder_signals[ERROR] =
170 g_signal_new("error",
171 G_OBJECT_CLASS_TYPE(object_class),
174 g_cclosure_marshal_VOID__STRING,
175 G_TYPE_NONE, 1, G_TYPE_STRING);
179 g_mencoder_init(GMencoder * self)
181 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
182 priv->info = g_new0(SetupInfo, 1);
186 g_mencoder_dispose(GObject * object)
191 g_mencoder_finalize(GObject * object)
193 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(object);
196 g_mencoder_close_stream(G_MENCODER(object));
203 return g_object_new(G_TYPE_MENCODER, NULL);
208 _obj_set_prop(GObject * obj, const gchar * prop_name,
209 const gchar * prop_val)
213 GParamSpec *s = NULL;
214 GObjectClass *k = G_OBJECT_GET_CLASS(obj);
217 g_value_init(&v, G_TYPE_STRING);
218 g_value_set_string(&v, prop_val);
220 s = g_object_class_find_property(k, prop_name);
222 g_print("Invalid property name: %s\n", prop_name);
226 g_value_init(&p, s->value_type);
227 switch (s->value_type) {
229 g_value_set_int(&p, atoi(prop_val));
232 g_value_set_uint (&p, (guint) atoi(prop_val));
235 g_value_set_ulong (&p, atol(prop_val));
238 g_value_set_string(&p, prop_val);
241 g_value_set_boolean(&p, (gboolean) atoi (prop_val));
244 g_value_set_double(&p, atof (prop_val));
247 g_value_set_float(&p, (float) atof (prop_val));
250 g_value_set_enum(&p, atoi(prop_val));
251 g_warning ("Property %s of type %s. Not supported using default enum",
252 prop_name, g_type_name (s->value_type));
256 g_object_set_property(obj, prop_name, &p);
262 _create_element_with_prop(const gchar * factory_name,
263 const gchar * element_name, gchar ** prop)
268 ret = gst_element_factory_make(factory_name, element_name);
273 for (i = 0; i < g_strv_length(prop); i++) {
274 if (prop[i] != NULL) {
275 char **v = g_strsplit(prop[i], "=", 2);
276 if (g_strv_length(v) == 2) {
277 _obj_set_prop(G_OBJECT(ret), v[0], v[1]);
289 _create_audio_bin(const gchar * encode, gchar ** encode_prop, gint rate)
291 GstElement *abin = NULL;
292 GstElement *aqueue = NULL;
293 GstElement *aconvert = NULL;
294 GstElement *aencode = NULL;
295 GstElement *aqueue_src = NULL;
298 // audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay !
299 // udpsink name=upd_audio host=224.0.0.1 port=5002
300 abin = gst_bin_new("abin");
301 aqueue = gst_element_factory_make("queue", "aqueue");
302 aconvert = gst_element_factory_make("audioconvert", "aconvert");
304 _create_element_with_prop((encode ? encode : "lame"), "aencode",
306 aqueue_src = gst_element_factory_make("queue", "aqueue_src");
308 if ((abin == NULL) || (aqueue == NULL) || (aconvert == NULL)
309 || (aencode == NULL) || (aqueue_src == NULL)) {
310 g_warning("Audio elements not found");
315 * if (rate > 0) { g_object_set (G_OBJECT (aencode), "bitrate", 32,
319 gst_bin_add_many(GST_BIN(abin), aqueue, aconvert, aencode, aqueue_src,
321 if (gst_element_link_many(aqueue, aconvert, aencode, aqueue_src, NULL)
323 g_warning("Not Link audio elements");
325 // TODO: apply audio rate
327 // ghost pad the audio bin
328 apad = gst_element_get_pad(aqueue, "sink");
329 gst_element_add_pad(abin, gst_ghost_pad_new("sink", apad));
330 gst_object_unref(apad);
332 apad = gst_element_get_pad(aqueue_src, "src");
333 gst_element_add_pad(abin, gst_ghost_pad_new("src", apad));
334 gst_object_unref(apad);
339 gst_object_unref(abin);
342 gst_object_unref(aqueue);
344 if (aconvert != NULL)
345 gst_object_unref(aconvert);
348 gst_object_unref(aencode);
350 if (aqueue_src != NULL)
351 gst_object_unref(aqueue_src);
354 gst_object_unref(apad);
362 // queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! colorspace
363 // ! rate ! encode ! queue
365 _create_video_bin(const gchar * encode,
366 gchar ** encode_prop,
367 gdouble fps, gint rate, guint width, guint height,
368 gboolean use_deinterlace)
370 GstElement *vbin = NULL;
371 GstElement *vqueue = NULL;
372 GstElement *vqueue_src = NULL;
373 GstElement *vcolorspace = NULL;
374 GstElement *vencode = NULL;
375 GstElement *vrate = NULL;
376 GstElement *deinterlace = NULL;
377 GstElement *walk = NULL;
380 vbin = gst_bin_new("vbin");
381 vqueue = gst_element_factory_make("queue", "vqueue");
383 gst_element_factory_make("ffmpegcolorspace", "colorspace");
385 if (use_deinterlace) {
386 deinterlace = gst_element_factory_make ("ffdeinterlace", "deinterlace");
387 if (deinterlace == NULL) {
388 g_warning ("Fail to create deinterlace element: Continue without deinterlace.");
393 vencode = _create_element_with_prop((encode !=
395 "ffenc_mpeg1video"), "vencode",
397 vqueue_src = gst_element_factory_make("queue", "queue_src");
399 if ((vbin == NULL) || (vqueue == NULL) || (vcolorspace == NULL)
400 || (vencode == NULL) || (vqueue_src == NULL)) {
401 g_warning("Video elements not found");
405 gst_bin_add_many(GST_BIN(vbin), vqueue, vcolorspace, vencode,
408 if (deinterlace != NULL) {
409 gst_bin_add(GST_BIN(vbin), deinterlace);
410 gst_element_link (vqueue, deinterlace);
416 if ((width > 0) && (height > 0)) {
421 vscale = gst_element_factory_make("videoscale", "vscale");
422 //g_object_set (G_OBJECT (vscale), "method", 1, NULL);
423 gst_bin_add(GST_BIN(vbin), vscale);
425 vcaps = gst_caps_new_simple("video/x-raw-yuv",
426 "width", G_TYPE_INT, width,
427 "height", G_TYPE_INT, height, NULL);
429 gst_element_link(walk, vscale);
431 if (gst_element_link_filtered(vscale, vcolorspace, vcaps) == FALSE) {
432 g_warning("Fail to resize video");
433 gst_object_unref(vcaps);
434 gst_object_unref(vscale);
437 gst_caps_unref(vcaps);
439 gst_element_link(walk, vcolorspace);
443 // Changing the video fps
445 vrate = gst_element_factory_make("videorate", "vrate");
447 gst_bin_add(GST_BIN(vbin), vrate);
449 if (gst_element_link(vcolorspace, vrate) == FALSE) {
450 g_warning("Fail to link video elements");
454 vcaps = gst_caps_new_simple("video/x-raw-yuv",
455 "framerate", GST_TYPE_FRACTION,
456 (int) (fps * 1000), 1000, NULL);
458 if (gst_element_link_filtered(vrate, vencode, vcaps) == FALSE) {
459 g_warning("Fail to link vrate with vencode.");
462 gst_caps_unref(vcaps);
464 if (gst_element_link(vcolorspace, vencode) == FALSE) {
465 g_warning("Fail to link colorspace and video encode element.");
470 gst_element_link(vencode, vqueue_src);
472 // ghost pad the video bin
473 vpad = gst_element_get_pad(vqueue, "sink");
474 gst_element_add_pad(vbin, gst_ghost_pad_new("sink", vpad));
475 gst_object_unref(vpad);
477 vpad = gst_element_get_pad(vqueue_src, "src");
478 gst_element_add_pad(vbin, gst_ghost_pad_new("src", vpad));
479 gst_object_unref(vpad);
485 gst_object_unref(vpad);
488 gst_object_unref(vbin);
491 gst_object_unref(vqueue);
494 gst_object_unref(vencode);
496 if (vqueue_src != NULL)
497 gst_object_unref(vqueue_src);
499 if (vcolorspace != NULL)
500 gst_object_unref(vcolorspace);
508 g_mencoder_setup_stream(GMencoder * self,
510 gboolean deinterlace,
511 const gchar * mux_name,
512 const gchar * video_encode,
513 gchar ** video_encode_prop,
518 const gchar * audio_encode,
519 gchar ** audio_encode_prop,
520 guint audio_rate, const gchar * out_uri)
522 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
523 if (priv->ready == TRUE) {
525 ("Stream already configured. You need close stream first.");
530 if (_open_output(self, out_uri) == FALSE) {
535 priv->send_chunked = chunked;
536 priv->pipe = _create_pipeline(self,
544 audio_encode, audio_encode_prop,
548 return (priv->pipe != NULL);
553 g_mencoder_append_uri(GMencoder * self, const gchar * uri)
558 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
559 gboolean ret = FALSE;
560 GstElement *ap = NULL;
561 GstElement *vp = NULL;
564 g_return_val_if_fail(priv->pipe != NULL, FALSE);
565 g_return_val_if_fail(priv->ready == FALSE, FALSE);
567 src = _create_source(self, uri);
571 priv->src = gst_bin_get_by_name(GST_BIN(src), "src");
573 gst_bin_add(GST_BIN(priv->pipe), src);
575 ap = gst_bin_get_by_name(GST_BIN(priv->pipe), "abin");
576 vp = gst_bin_get_by_name(GST_BIN(priv->pipe), "vbin");
578 if ((vp == NULL) || (ap == NULL)) {
579 g_warning("Fail to get output bin");
583 pad_src = gst_element_get_pad(src, "src_audio");
584 pad_sink = gst_element_get_compatible_pad(ap,
586 gst_pad_get_caps(pad_src));
588 if ((pad_sink == NULL) || (pad_src == NULL))
591 GstPadLinkReturn lret = gst_pad_link(pad_src, pad_sink);
592 if (lret != GST_PAD_LINK_OK)
595 gst_object_unref(pad_src);
596 gst_object_unref(pad_sink);
598 pad_src = gst_element_get_pad(src, "src_video");
599 pad_sink = gst_element_get_compatible_pad(vp,
601 gst_pad_get_caps(pad_src));
603 if ((pad_src == NULL) || (pad_sink == NULL))
606 if (gst_pad_link(pad_src, pad_sink) != GST_PAD_LINK_OK) {
607 g_warning("invalid source. video");
615 if ((src != NULL) && (ret == FALSE)) {
616 gst_bin_remove(GST_BIN(priv->pipe), src);
617 gst_object_unref(src);
621 gst_object_unref(ap);
624 gst_object_unref(vp);
627 gst_object_unref(pad_src);
629 if (pad_sink != NULL)
630 gst_object_unref(pad_sink);
638 g_mencoder_remove_uri(GMencoder * self, const gchar * uri)
640 // GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
645 g_mencoder_play_stream(GMencoder * self)
647 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
648 g_return_if_fail(priv->ready == FALSE);
650 gst_element_set_state(priv->pipe, GST_STATE_PLAYING);
651 if (priv->tick_id != 0) {
652 g_source_remove (priv->tick_id);
654 priv->tick_id = g_timeout_add(500, _tick_cb, self);
656 if (priv->timeout_id != 0) {
657 g_source_remove (priv->timeout_id);
659 //priv->timeout_id = g_timeout_add(GMENCODER_TIMEOUT, _process_timeout_cb, self);
663 g_mencoder_pause_stream(GMencoder * self)
665 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
666 g_return_if_fail(priv->ready == TRUE);
667 gst_element_set_state(priv->pipe, GST_STATE_PAUSED);
671 g_mencoder_close_stream(GMencoder * self)
674 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
675 if (priv->tick_id != 0) {
676 g_source_remove(priv->tick_id);
680 if (priv->timeout_id != 0) {
681 g_source_remove (priv->timeout_id);
682 priv->timeout_id = 0;
685 if (priv->pipe != NULL) {
686 // TODO: fixe pipeline dispose
687 //gst_element_set_state (priv->pipe, GST_STATE_NULL);
688 // g_debug ("SETING STATE TO NULL: OK");
689 // gst_element_set_state (priv->pipe, GST_STATE_NULL);
690 //gst_object_unref (priv->pipe);
691 //gst_object_unref(priv->src);
702 _create_pipeline(GMencoder * self,
703 const gchar * video_encode,
704 const gchar * mux_name,
705 gchar ** video_encode_prop,
710 const gchar * audio_encode,
711 gchar ** audio_encode_prop, guint audio_rate,
712 gboolean deinterlace)
715 GstElement *pipe = NULL;
716 GstElement *sink = NULL;
717 GstElement *mux = NULL;
718 GstElement *abin = NULL;
719 GstElement *vbin = NULL;
720 GstElement *queue = NULL;
721 GstPad *aux_pad = NULL;
722 GstPad *mux_pad = NULL;
724 pipe = gst_pipeline_new("pipe");
727 gst_element_factory_make((mux_name ? mux_name : "ffmux_mpeg"),
732 queue = gst_element_factory_make("queue", "queueu_sink");
735 sink = gst_element_factory_make("fakesink", "sink");
736 g_object_set (G_OBJECT (sink), "signal-handoffs", TRUE, NULL);
737 g_signal_connect (G_OBJECT (sink),
739 G_CALLBACK (_buffer_arrive_cb),
742 abin = _create_audio_bin(audio_encode, audio_encode_prop, audio_rate);
747 _create_video_bin(video_encode, video_encode_prop, video_fps,
748 video_rate, video_width, video_height, deinterlace);
753 gst_bin_add_many(GST_BIN(pipe), abin, vbin, mux, queue, sink, NULL);
756 // Link bins with mux
757 aux_pad = gst_element_get_pad(abin, "src");
759 gst_element_get_compatible_pad(mux, aux_pad,
760 GST_PAD_CAPS(aux_pad));
761 if (mux_pad == NULL) {
762 g_warning("Mux element no have audio PAD");
765 GstPadLinkReturn ret = gst_pad_link(aux_pad, mux_pad);
766 if (ret != GST_PAD_LINK_OK) {
767 g_warning("Fail link audio and mux: %d", ret);
771 gst_object_unref(aux_pad);
772 gst_object_unref(mux_pad);
774 aux_pad = gst_element_get_pad(vbin, "src");
776 gst_element_get_compatible_pad(mux, aux_pad,
777 GST_PAD_CAPS(aux_pad));
778 if (mux_pad == NULL) {
779 g_warning("Mux element no have video PAD");
782 ret = gst_pad_link(aux_pad, mux_pad);
783 if (ret != GST_PAD_LINK_OK) {
784 g_warning("Fail link video and mux: %d", ret);
787 gst_object_unref(aux_pad);
788 gst_object_unref(mux_pad);
792 // Link mux with sink
793 gst_element_link_many(mux, queue, sink, NULL);
795 bus = gst_pipeline_get_bus(GST_PIPELINE(pipe));
796 gst_bus_add_watch(bus, _pipeline_bus_cb, self);
797 gst_object_unref(bus);
801 g_warning("Invalid uri");
804 gst_object_unref(pipe);
809 gst_object_unref(mux);
812 if (mux_pad != NULL) {
813 gst_object_unref(mux_pad);
816 if (aux_pad != NULL) {
817 gst_object_unref(mux_pad);
821 gst_object_unref(sink);
825 gst_object_unref(abin);
829 gst_object_unref(vbin);
837 _close_output(GMencoder * self)
842 _create_v4l_source (GMencoder *self, const gchar * uri)
845 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
848 info = g_strsplit (uri+6, ":", 3);
849 if (g_strv_length (info) != 3) {
853 priv->v4lsrc = gst_element_factory_make ("v4l2src", "src");
854 g_debug ("channel %s, norm %s, frequ %s", info[0], info[1], info[2]);
855 g_object_set (G_OBJECT (priv->v4lsrc),
858 "frequency", atoi (info[2]),
865 _create_source(GMencoder *self, const gchar * uri)
868 GstElement *bsrc = NULL;
869 GstElement *src = NULL;
870 GstElement *aqueue = NULL;
871 GstElement *vqueue = NULL;
872 GstElement *decode = NULL;
873 GstPad *src_pad = NULL;
876 bsrc = gst_bin_new(NULL);
878 // src = gst_element_factory_make ("gnomevfssrc", "src");
879 // g_object_set (G_OBJECT (src), "location", uri, NULL);
880 if (strncmp (uri, "v4l://", 6) == 0) {
882 src = _create_v4l_source (self, uri);
885 src = gst_element_make_from_uri(GST_URI_SRC, uri, "src");
892 if (g_getenv ("USE_DECODEBIN1"))
893 decode = gst_element_factory_make("decodebin", "decode");
895 decode = gst_element_factory_make("decodebin2", "decode");
899 aqueue = gst_element_factory_make("queue", "aqueue");
903 vqueue = gst_element_factory_make("queue", "vqueue");
907 gst_bin_add_many(GST_BIN(bsrc), src, decode, aqueue, vqueue,
909 gst_element_link (src, decode);
911 g_signal_connect(G_OBJECT(decode),
913 G_CALLBACK(_decodebin_new_pad_cb), bsrc);
915 g_signal_connect(G_OBJECT(decode),
917 G_CALLBACK(_decodebin_unknown_type_cb), pipe);
919 src_pad = gst_element_get_pad(aqueue, "src");
920 gst_element_add_pad(bsrc, gst_ghost_pad_new("src_audio", src_pad));
921 gst_object_unref(src_pad);
923 src_pad = gst_element_get_pad(vqueue, "src");
924 gst_element_add_pad(bsrc, gst_ghost_pad_new("src_video", src_pad));
925 gst_object_unref(src_pad);
930 g_debug ("Fail to create source element");
932 gst_object_unref(src);
935 if (decode != NULL) {
936 gst_object_unref(decode);
939 if (aqueue != NULL) {
940 gst_object_unref(aqueue);
943 if (vqueue != NULL) {
944 gst_object_unref(vqueue);
951 _open_output(GMencoder * self, const gchar * uri)
954 GnomeVFSResult result;
955 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
957 i = g_strsplit(uri, "://", 0);
958 if (strcmp(i[0], "fd") == 0) {
959 result = gnome_vfs_open_fd (&priv->handle, atoi(i[1]));
961 if (g_file_test (i[1], G_FILE_TEST_EXISTS) == FALSE) {
962 result = gnome_vfs_create (&priv->handle, uri, GNOME_VFS_OPEN_WRITE, FALSE,
963 GNOME_VFS_PERM_USER_WRITE | GNOME_VFS_PERM_USER_READ | GNOME_VFS_PERM_GROUP_READ);
965 result = gnome_vfs_open (&priv->handle, uri,
966 GNOME_VFS_OPEN_WRITE | GNOME_VFS_OPEN_TRUNCATE);
971 return (result == GNOME_VFS_OK);
975 _pipeline_bus_cb(GstBus * bus, GstMessage * msg, gpointer user_data)
977 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
979 switch (GST_MESSAGE_TYPE(msg)) {
981 case GST_MESSAGE_STATE_CHANGED:
985 GstState pendingstate;
988 gst_message_parse_state_changed(msg, &oldstate,
989 &newstate, &pendingstate);
991 if (pendingstate != GST_STATE_VOID_PENDING)
994 if ((oldstate == GST_STATE_READY)
995 && (newstate == GST_STATE_PAUSED)) {
997 g_signal_emit(user_data, g_mencoder_signals[PAUSED],
999 } else if ((oldstate == GST_STATE_PAUSED)
1000 && (newstate == GST_STATE_PLAYING)) {
1001 g_signal_emit(user_data, g_mencoder_signals[PLAYING], 0);
1002 } else if ((oldstate == GST_STATE_READY) &&
1003 (newstate == GST_STATE_NULL)) {
1004 g_signal_emit(user_data, g_mencoder_signals[STOPED], 0);
1009 case GST_MESSAGE_ERROR:
1015 if (priv->tick_id != 0) {
1016 g_source_remove(priv->tick_id);
1020 gst_message_parse_error(msg, &error, &debug);
1021 err_str = g_strdup_printf("Error [%d] %s (%s)", error->code,
1022 error->message, debug);
1023 priv->ready = FALSE;
1024 g_signal_emit(user_data, g_mencoder_signals[ERROR], 0,
1027 g_clear_error(&error);
1032 case GST_MESSAGE_EOS:
1033 priv->ready = FALSE;
1034 #ifdef USE_MANUAL_SINK
1035 _flush_queue (G_MENCODER (user_data));
1037 g_signal_emit(user_data, g_mencoder_signals[EOS], 0);
1040 case GST_MESSAGE_DURATION:
1044 gst_message_parse_duration(msg, &format, &duration);
1045 if (format == GST_FORMAT_BYTES)
1046 priv->duration = duration;
1060 _decodebin_new_pad_cb(GstElement * object,
1061 GstPad * pad, gboolean flag, gpointer user_data)
1064 gchar *str_caps = NULL;
1065 GstElement *sink_element;
1068 caps = gst_pad_get_caps(pad);
1069 str_caps = gst_caps_to_string(caps);
1070 if (strstr(str_caps, "audio") != NULL) {
1071 sink_element = gst_bin_get_by_name(GST_BIN(user_data), "aqueue");
1072 } else if (strstr(str_caps, "video") != NULL) {
1073 sink_element = gst_bin_get_by_name(GST_BIN(user_data), "vqueue");
1075 g_warning("invalid caps %s", str_caps);
1078 sink_pad = gst_element_get_pad(sink_element, "sink");
1079 gst_pad_link(pad, sink_pad);
1081 gst_object_unref(sink_element);
1082 gst_object_unref(sink_pad);
1084 gst_caps_unref(caps);
1088 _decodebin_unknown_type_cb(GstElement * object,
1089 GstPad * pad, GstCaps * caps,
1092 g_warning("Unknown Type");
1093 // priv->ready = FALSE;
1097 _tick_cb(gpointer user_data)
1099 GstFormat format = GST_FORMAT_BYTES;
1102 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
1104 if (priv->duration == 0) {
1106 if (gst_element_query_duration(priv->src, &format, &d))
1110 if (priv->duration != 0) {
1111 gst_element_query_position(priv->src, &format, &cur);
1112 g_print("PROGRESS:%lli\n", (99 * cur) / priv->duration);
1119 _process_timeout_cb (gpointer user_data)
1121 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
1123 g_signal_emit(user_data, g_mencoder_signals[ERROR], 0, "timeout");
1124 priv->timeout_id = 0;
1129 #ifdef USE_MANUAL_SINK
1131 _send_buffer (GnomeVFSHandle *handle, gpointer buff, gint size)
1135 GnomeVFSResult result;
1136 GnomeVFSFileSize bytes_written;
1138 b_send = g_byte_array_new ();
1139 msg = g_strdup_printf ("%x\r\n", size);
1140 b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
1143 b_send = g_byte_array_append (b_send, buff, size);
1145 msg = g_strdup ("\r\n");
1146 b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
1149 result = gnome_vfs_write (handle, b_send->data, b_send->len, &bytes_written);
1150 g_byte_array_free (b_send, TRUE);
1152 return (result == GNOME_VFS_OK);
1156 _flush_queue (GMencoder *self)
1158 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
1160 if (priv->send_chunked) {
1161 GnomeVFSFileSize bytes_written;
1163 end_msg = g_strdup ("0\r\n\r\n");
1164 gnome_vfs_write (priv->handle,
1165 (const guint8*) end_msg,
1166 strlen(end_msg) * sizeof(gchar),
1173 _buffer_arrive_cb (GstElement* object,
1178 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
1180 if (priv->timeout_id != 0) {
1181 g_source_remove (priv->timeout_id);
1182 priv->timeout_id = 0;
1185 if (priv->send_chunked) {
1186 if (_send_buffer (priv->handle, GST_BUFFER_DATA (buff), GST_BUFFER_SIZE (buff)) == FALSE)
1189 GnomeVFSResult result;
1190 GnomeVFSFileSize bytes_written;
1192 result = gnome_vfs_write (priv->handle,
1193 GST_BUFFER_DATA (buff),
1194 GST_BUFFER_SIZE (buff),
1197 if (result != GNOME_VFS_OK)
1204 if (priv->tick_id != 0) {
1205 g_source_remove(priv->tick_id);
1208 g_signal_emit(user_data, g_mencoder_signals[ERROR], 0, "Fail to write on socket");
1209 gst_element_set_state (priv->pipe, GST_STATE_PAUSED);