[svn r851] Bug #1790620 fixed. COPYING file updated to the correct license (LGPL)
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_ulong (&p, atol(prop_val));
235 g_value_set_string(&p, prop_val);
238 g_value_set_boolean(&p, (gboolean) atoi (prop_val));
241 g_value_set_double(&p, atof (prop_val));
244 g_value_set_float(&p, (float) atof (prop_val));
247 g_value_set_enum(&p, atoi(prop_val));
248 g_warning ("Property %s of type %s. Not supported using default enum",
249 prop_name, g_type_name (s->value_type));
253 g_object_set_property(obj, prop_name, &p);
259 _create_element_with_prop(const gchar * factory_name,
260 const gchar * element_name, gchar ** prop)
265 ret = gst_element_factory_make(factory_name, element_name);
270 for (i = 0; i < g_strv_length(prop); i++) {
271 if (prop[i] != NULL) {
272 char **v = g_strsplit(prop[i], "=", 2);
273 if (g_strv_length(v) == 2) {
274 _obj_set_prop(G_OBJECT(ret), v[0], v[1]);
286 _create_audio_bin(const gchar * encode, gchar ** encode_prop, gint rate)
288 GstElement *abin = NULL;
289 GstElement *aqueue = NULL;
290 GstElement *aconvert = NULL;
291 GstElement *aencode = NULL;
292 GstElement *aqueue_src = NULL;
295 // audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay !
296 // udpsink name=upd_audio host=224.0.0.1 port=5002
297 abin = gst_bin_new("abin");
298 aqueue = gst_element_factory_make("queue", "aqueue");
299 aconvert = gst_element_factory_make("audioconvert", "aconvert");
301 _create_element_with_prop((encode ? encode : "lame"), "aencode",
303 aqueue_src = gst_element_factory_make("queue", "aqueue_src");
305 if ((abin == NULL) || (aqueue == NULL) || (aconvert == NULL)
306 || (aencode == NULL) || (aqueue_src == NULL)) {
307 g_warning("Audio elements not found");
311 g_object_set(G_OBJECT(aencode), "bitrate", 32, NULL);
313 * if (rate > 0) { g_object_set (G_OBJECT (aencode), "bitrate", 32,
317 gst_bin_add_many(GST_BIN(abin), aqueue, aconvert, aencode, aqueue_src,
319 if (gst_element_link_many(aqueue, aconvert, aencode, aqueue_src, NULL)
321 g_warning("Not Link audio elements");
323 // TODO: apply audio rate
325 // ghost pad the audio bin
326 apad = gst_element_get_pad(aqueue, "sink");
327 gst_element_add_pad(abin, gst_ghost_pad_new("sink", apad));
328 gst_object_unref(apad);
330 apad = gst_element_get_pad(aqueue_src, "src");
331 gst_element_add_pad(abin, gst_ghost_pad_new("src", apad));
332 gst_object_unref(apad);
337 gst_object_unref(abin);
340 gst_object_unref(aqueue);
342 if (aconvert != NULL)
343 gst_object_unref(aconvert);
346 gst_object_unref(aencode);
348 if (aqueue_src != NULL)
349 gst_object_unref(aqueue_src);
352 gst_object_unref(apad);
360 // queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! colorspace
361 // ! rate ! encode ! queue
363 _create_video_bin(const gchar * encode,
364 gchar ** encode_prop,
365 gdouble fps, gint rate, guint width, guint height,
366 gboolean use_deinterlace)
368 GstElement *vbin = NULL;
369 GstElement *vqueue = NULL;
370 GstElement *vqueue_src = NULL;
371 GstElement *vcolorspace = NULL;
372 GstElement *vencode = NULL;
373 GstElement *vrate = NULL;
374 GstElement *deinterlace = NULL;
375 GstElement *walk = NULL;
378 vbin = gst_bin_new("vbin");
379 vqueue = gst_element_factory_make("queue", "vqueue");
381 gst_element_factory_make("ffmpegcolorspace", "colorspace");
383 if (use_deinterlace) {
384 deinterlace = gst_element_factory_make ("ffdeinterlace", "deinterlace");
385 if (deinterlace == NULL) {
386 g_warning ("Fail to create deinterlace element: Continue without deinterlace.");
391 vencode = _create_element_with_prop((encode !=
393 "ffenc_mpeg1video"), "vencode",
395 vqueue_src = gst_element_factory_make("queue", "queue_src");
397 if ((vbin == NULL) || (vqueue == NULL) || (vcolorspace == NULL)
398 || (vencode == NULL) || (vqueue_src == NULL)) {
399 g_warning("Video elements not found");
403 gst_bin_add_many(GST_BIN(vbin), vqueue, vcolorspace, vencode,
406 if (deinterlace != NULL) {
407 gst_bin_add(GST_BIN(vbin), deinterlace);
408 gst_element_link (vqueue, deinterlace);
414 if ((width > 0) && (height > 0)) {
418 gst_element_factory_make("videoscale", "vscale");
420 g_object_set (G_OBJECT (vscale), "method", 1, NULL);
422 gst_bin_add(GST_BIN(vbin), vscale);
424 vcaps = gst_caps_new_simple("video/x-raw-yuv",
425 "width", G_TYPE_INT, width,
426 "height", G_TYPE_INT, height, NULL);
428 gst_element_link(walk, vscale);
430 if (gst_element_link_filtered(vscale, vcolorspace, vcaps) == FALSE) {
431 g_warning("Fail to resize video");
432 gst_object_unref(vcaps);
433 gst_object_unref(vscale);
436 gst_caps_unref(vcaps);
438 gst_element_link(walk, vcolorspace);
442 // Changing the video fps
444 vrate = gst_element_factory_make("videorate", "vrate");
446 gst_bin_add(GST_BIN(vbin), vrate);
448 if (gst_element_link(vcolorspace, vrate) == FALSE) {
449 g_warning("Fail to link video elements");
453 vcaps = gst_caps_new_simple("video/x-raw-yuv",
454 "framerate", GST_TYPE_FRACTION,
455 (int) (fps * 1000), 1000, NULL);
457 if (gst_element_link_filtered(vrate, vencode, vcaps) == FALSE) {
458 g_warning("Fail to link vrate with vencode.");
461 gst_caps_unref(vcaps);
463 if (gst_element_link(vcolorspace, vencode) == FALSE) {
464 g_warning("Fail to link colorspace and video encode element.");
469 gst_element_link(vencode, vqueue_src);
471 // ghost pad the video bin
472 vpad = gst_element_get_pad(vqueue, "sink");
473 gst_element_add_pad(vbin, gst_ghost_pad_new("sink", vpad));
474 gst_object_unref(vpad);
476 vpad = gst_element_get_pad(vqueue_src, "src");
477 gst_element_add_pad(vbin, gst_ghost_pad_new("src", vpad));
478 gst_object_unref(vpad);
484 gst_object_unref(vpad);
487 gst_object_unref(vbin);
490 gst_object_unref(vqueue);
493 gst_object_unref(vencode);
495 if (vqueue_src != NULL)
496 gst_object_unref(vqueue_src);
498 if (vcolorspace != NULL)
499 gst_object_unref(vcolorspace);
507 g_mencoder_setup_stream(GMencoder * self,
509 gboolean deinterlace,
510 const gchar * mux_name,
511 const gchar * video_encode,
512 gchar ** video_encode_prop,
517 const gchar * audio_encode,
518 gchar ** audio_encode_prop,
519 guint audio_rate, const gchar * out_uri)
521 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
522 if (priv->ready == TRUE) {
524 ("Stream already configured. You need close stream first.");
529 if (_open_output(self, out_uri) == FALSE) {
534 priv->send_chunked = chunked;
535 priv->pipe = _create_pipeline(self,
543 audio_encode, audio_encode_prop,
547 return (priv->pipe != NULL);
552 g_mencoder_append_uri(GMencoder * self, const gchar * uri)
557 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
558 gboolean ret = FALSE;
559 GstElement *ap = NULL;
560 GstElement *vp = NULL;
563 g_return_val_if_fail(priv->pipe != NULL, FALSE);
564 g_return_val_if_fail(priv->ready == FALSE, FALSE);
566 src = _create_source(self, uri);
570 priv->src = gst_bin_get_by_name(GST_BIN(src), "src");
572 gst_bin_add(GST_BIN(priv->pipe), src);
574 ap = gst_bin_get_by_name(GST_BIN(priv->pipe), "abin");
575 vp = gst_bin_get_by_name(GST_BIN(priv->pipe), "vbin");
577 if ((vp == NULL) || (ap == NULL)) {
578 g_warning("Fail to get output bin");
582 pad_src = gst_element_get_pad(src, "src_audio");
583 pad_sink = gst_element_get_compatible_pad(ap,
585 gst_pad_get_caps(pad_src));
587 if ((pad_sink == NULL) || (pad_src == NULL))
590 GstPadLinkReturn lret = gst_pad_link(pad_src, pad_sink);
591 if (lret != GST_PAD_LINK_OK)
594 gst_object_unref(pad_src);
595 gst_object_unref(pad_sink);
597 pad_src = gst_element_get_pad(src, "src_video");
598 pad_sink = gst_element_get_compatible_pad(vp,
600 gst_pad_get_caps(pad_src));
602 if ((pad_src == NULL) || (pad_sink == NULL))
605 if (gst_pad_link(pad_src, pad_sink) != GST_PAD_LINK_OK) {
606 g_warning("invalid source. video");
614 if ((src != NULL) && (ret == FALSE)) {
615 gst_bin_remove(GST_BIN(priv->pipe), src);
616 gst_object_unref(src);
620 gst_object_unref(ap);
623 gst_object_unref(vp);
626 gst_object_unref(pad_src);
628 if (pad_sink != NULL)
629 gst_object_unref(pad_sink);
637 g_mencoder_remove_uri(GMencoder * self, const gchar * uri)
639 // GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
644 g_mencoder_play_stream(GMencoder * self)
646 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
647 g_return_if_fail(priv->ready == FALSE);
649 gst_element_set_state(priv->pipe, GST_STATE_PLAYING);
650 if (priv->tick_id != 0) {
651 g_source_remove (priv->tick_id);
653 priv->tick_id = g_timeout_add(500, _tick_cb, self);
655 if (priv->timeout_id != 0) {
656 g_source_remove (priv->timeout_id);
658 //priv->timeout_id = g_timeout_add(GMENCODER_TIMEOUT, _process_timeout_cb, self);
662 g_mencoder_pause_stream(GMencoder * self)
664 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
665 g_return_if_fail(priv->ready == TRUE);
666 gst_element_set_state(priv->pipe, GST_STATE_PAUSED);
670 g_mencoder_close_stream(GMencoder * self)
673 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
674 if (priv->tick_id != 0) {
675 g_source_remove(priv->tick_id);
679 if (priv->timeout_id != 0) {
680 g_source_remove (priv->timeout_id);
681 priv->timeout_id = 0;
684 if (priv->pipe != NULL) {
685 // TODO: fixe pipeline dispose
686 //gst_element_set_state (priv->pipe, GST_STATE_NULL);
687 // g_debug ("SETING STATE TO NULL: OK");
688 // gst_element_set_state (priv->pipe, GST_STATE_NULL);
689 //gst_object_unref (priv->pipe);
690 //gst_object_unref(priv->src);
701 _create_pipeline(GMencoder * self,
702 const gchar * video_encode,
703 const gchar * mux_name,
704 gchar ** video_encode_prop,
709 const gchar * audio_encode,
710 gchar ** audio_encode_prop, guint audio_rate,
711 gboolean deinterlace)
714 GstElement *pipe = NULL;
715 GstElement *sink = NULL;
716 GstElement *mux = NULL;
717 GstElement *abin = NULL;
718 GstElement *vbin = NULL;
719 GstElement *queue = NULL;
720 GstPad *aux_pad = NULL;
721 GstPad *mux_pad = NULL;
723 pipe = gst_pipeline_new("pipe");
726 gst_element_factory_make((mux_name ? mux_name : "ffmux_mpeg"),
731 queue = gst_element_factory_make("queue", "queueu_sink");
734 sink = gst_element_factory_make("fakesink", "sink");
735 g_object_set (G_OBJECT (sink), "signal-handoffs", TRUE, NULL);
736 g_signal_connect (G_OBJECT (sink),
738 G_CALLBACK (_buffer_arrive_cb),
741 abin = _create_audio_bin(audio_encode, audio_encode_prop, audio_rate);
746 _create_video_bin(video_encode, video_encode_prop, video_fps,
747 video_rate, video_width, video_height, deinterlace);
752 gst_bin_add_many(GST_BIN(pipe), abin, vbin, mux, queue, sink, NULL);
755 // Link bins with mux
756 aux_pad = gst_element_get_pad(abin, "src");
758 gst_element_get_compatible_pad(mux, aux_pad,
759 GST_PAD_CAPS(aux_pad));
760 if (mux_pad == NULL) {
761 g_warning("Mux element no have audio PAD");
764 GstPadLinkReturn ret = gst_pad_link(aux_pad, mux_pad);
765 if (ret != GST_PAD_LINK_OK) {
766 g_warning("Fail link audio and mux: %d", ret);
770 gst_object_unref(aux_pad);
771 gst_object_unref(mux_pad);
773 aux_pad = gst_element_get_pad(vbin, "src");
775 gst_element_get_compatible_pad(mux, aux_pad,
776 GST_PAD_CAPS(aux_pad));
777 if (mux_pad == NULL) {
778 g_warning("Mux element no have video PAD");
781 ret = gst_pad_link(aux_pad, mux_pad);
782 if (ret != GST_PAD_LINK_OK) {
783 g_warning("Fail link video and mux: %d", ret);
786 gst_object_unref(aux_pad);
787 gst_object_unref(mux_pad);
791 // Link mux with sink
792 gst_element_link_many(mux, queue, sink, NULL);
794 bus = gst_pipeline_get_bus(GST_PIPELINE(pipe));
795 gst_bus_add_watch(bus, _pipeline_bus_cb, self);
796 gst_object_unref(bus);
800 g_warning("Invalid uri");
803 gst_object_unref(pipe);
808 gst_object_unref(mux);
811 if (mux_pad != NULL) {
812 gst_object_unref(mux_pad);
815 if (aux_pad != NULL) {
816 gst_object_unref(mux_pad);
820 gst_object_unref(sink);
824 gst_object_unref(abin);
828 gst_object_unref(vbin);
836 _close_output(GMencoder * self)
841 _create_v4l_source (GMencoder *self, const gchar * uri)
844 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
847 info = g_strsplit (uri+6, ":", 3);
848 if (g_strv_length (info) != 3) {
852 priv->v4lsrc = gst_element_factory_make ("v4l2src", "src");
853 g_debug ("channel %s, norm %s, frequ %s", info[0], info[1], info[2]);
854 g_object_set (G_OBJECT (priv->v4lsrc),
857 "frequency", atoi (info[2]),
864 _create_source(GMencoder *self, const gchar * uri)
867 GstElement *bsrc = NULL;
868 GstElement *src = NULL;
869 GstElement *aqueue = NULL;
870 GstElement *vqueue = NULL;
871 GstElement *decode = NULL;
872 GstPad *src_pad = NULL;
875 bsrc = gst_bin_new(NULL);
877 // src = gst_element_factory_make ("gnomevfssrc", "src");
878 // g_object_set (G_OBJECT (src), "location", uri, NULL);
879 if (strncmp (uri, "v4l://", 6) == 0) {
881 src = _create_v4l_source (self, uri);
884 src = gst_element_make_from_uri(GST_URI_SRC, uri, "src");
890 decode = gst_element_factory_make("decodebin2", "decode");
894 aqueue = gst_element_factory_make("queue", "aqueue");
898 vqueue = gst_element_factory_make("queue", "vqueue");
902 gst_bin_add_many(GST_BIN(bsrc), src, decode, aqueue, vqueue,
904 gst_element_link (src, decode);
906 g_signal_connect(G_OBJECT(decode),
908 G_CALLBACK(_decodebin_new_pad_cb), bsrc);
910 g_signal_connect(G_OBJECT(decode),
912 G_CALLBACK(_decodebin_unknown_type_cb), pipe);
914 src_pad = gst_element_get_pad(aqueue, "src");
915 gst_element_add_pad(bsrc, gst_ghost_pad_new("src_audio", src_pad));
916 gst_object_unref(src_pad);
918 src_pad = gst_element_get_pad(vqueue, "src");
919 gst_element_add_pad(bsrc, gst_ghost_pad_new("src_video", src_pad));
920 gst_object_unref(src_pad);
925 g_debug ("Fail to create source element");
927 gst_object_unref(src);
930 if (decode != NULL) {
931 gst_object_unref(decode);
934 if (aqueue != NULL) {
935 gst_object_unref(aqueue);
938 if (vqueue != NULL) {
939 gst_object_unref(vqueue);
946 _open_output(GMencoder * self, const gchar * uri)
949 GnomeVFSResult result;
950 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
952 i = g_strsplit(uri, "://", 0);
953 if (strcmp(i[0], "fd") == 0) {
954 result = gnome_vfs_open_fd (&priv->handle, atoi(i[1]));
956 if (g_file_test (i[1], G_FILE_TEST_EXISTS) == FALSE) {
957 result = gnome_vfs_create (&priv->handle, uri, GNOME_VFS_OPEN_WRITE, FALSE,
958 GNOME_VFS_PERM_USER_WRITE | GNOME_VFS_PERM_USER_READ | GNOME_VFS_PERM_GROUP_READ);
960 result = gnome_vfs_open (&priv->handle, uri,
961 GNOME_VFS_OPEN_WRITE | GNOME_VFS_OPEN_TRUNCATE);
966 return (result == GNOME_VFS_OK);
970 _pipeline_bus_cb(GstBus * bus, GstMessage * msg, gpointer user_data)
972 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
974 switch (GST_MESSAGE_TYPE(msg)) {
976 case GST_MESSAGE_STATE_CHANGED:
980 GstState pendingstate;
983 gst_message_parse_state_changed(msg, &oldstate,
984 &newstate, &pendingstate);
986 if (pendingstate != GST_STATE_VOID_PENDING)
989 if ((oldstate == GST_STATE_READY)
990 && (newstate == GST_STATE_PAUSED)) {
992 g_signal_emit(user_data, g_mencoder_signals[PAUSED],
994 } else if ((oldstate == GST_STATE_PAUSED)
995 && (newstate == GST_STATE_PLAYING)) {
996 g_signal_emit(user_data, g_mencoder_signals[PLAYING], 0);
997 } else if ((oldstate == GST_STATE_READY) &&
998 (newstate == GST_STATE_NULL)) {
999 g_signal_emit(user_data, g_mencoder_signals[STOPED], 0);
1004 case GST_MESSAGE_ERROR:
1010 if (priv->tick_id != 0) {
1011 g_source_remove(priv->tick_id);
1015 gst_message_parse_error(msg, &error, &debug);
1016 err_str = g_strdup_printf("Error [%d] %s (%s)", error->code,
1017 error->message, debug);
1018 priv->ready = FALSE;
1019 g_signal_emit(user_data, g_mencoder_signals[ERROR], 0,
1022 g_clear_error(&error);
1027 case GST_MESSAGE_EOS:
1028 priv->ready = FALSE;
1029 #ifdef USE_MANUAL_SINK
1030 _flush_queue (G_MENCODER (user_data));
1032 g_signal_emit(user_data, g_mencoder_signals[EOS], 0);
1035 case GST_MESSAGE_DURATION:
1039 gst_message_parse_duration(msg, &format, &duration);
1040 if (format == GST_FORMAT_BYTES)
1041 priv->duration = duration;
1055 _decodebin_new_pad_cb(GstElement * object,
1056 GstPad * pad, gboolean flag, gpointer user_data)
1059 gchar *str_caps = NULL;
1060 GstElement *sink_element;
1063 caps = gst_pad_get_caps(pad);
1064 str_caps = gst_caps_to_string(caps);
1065 if (strstr(str_caps, "audio") != NULL) {
1066 sink_element = gst_bin_get_by_name(GST_BIN(user_data), "aqueue");
1067 } else if (strstr(str_caps, "video") != NULL) {
1068 sink_element = gst_bin_get_by_name(GST_BIN(user_data), "vqueue");
1070 g_warning("invalid caps %s", str_caps);
1073 sink_pad = gst_element_get_pad(sink_element, "sink");
1074 gst_pad_link(pad, sink_pad);
1076 gst_object_unref(sink_element);
1077 gst_object_unref(sink_pad);
1079 gst_caps_unref(caps);
1083 _decodebin_unknown_type_cb(GstElement * object,
1084 GstPad * pad, GstCaps * caps,
1087 g_warning("Unknown Type");
1088 // priv->ready = FALSE;
1092 _tick_cb(gpointer user_data)
1094 GstFormat format = GST_FORMAT_BYTES;
1097 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
1099 if (priv->duration == 0) {
1101 if (gst_element_query_duration(priv->src, &format, &d))
1105 if (priv->duration != 0) {
1106 gst_element_query_position(priv->src, &format, &cur);
1107 g_print("PROGRESS:%lli\n", (99 * cur) / priv->duration);
1114 _process_timeout_cb (gpointer user_data)
1116 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
1118 g_signal_emit(user_data, g_mencoder_signals[ERROR], 0, "timeout");
1119 priv->timeout_id = 0;
1124 #ifdef USE_MANUAL_SINK
1126 _send_buffer (GnomeVFSHandle *handle, gpointer buff, gint size)
1130 GnomeVFSResult result;
1131 GnomeVFSFileSize bytes_written;
1133 b_send = g_byte_array_new ();
1134 msg = g_strdup_printf ("%x\r\n", size);
1135 b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
1138 b_send = g_byte_array_append (b_send, buff, size);
1140 msg = g_strdup ("\r\n");
1141 b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
1144 result = gnome_vfs_write (handle, b_send->data, b_send->len, &bytes_written);
1145 g_byte_array_free (b_send, TRUE);
1147 return (result == GNOME_VFS_OK);
1151 _flush_queue (GMencoder *self)
1153 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
1155 if (priv->send_chunked) {
1156 GnomeVFSFileSize bytes_written;
1158 end_msg = g_strdup ("0\r\n\r\n");
1159 gnome_vfs_write (priv->handle,
1160 (const guint8*) end_msg,
1161 strlen(end_msg) * sizeof(gchar),
1168 _buffer_arrive_cb (GstElement* object,
1173 GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
1175 if (priv->timeout_id != 0) {
1176 g_source_remove (priv->timeout_id);
1177 priv->timeout_id = 0;
1180 if (priv->send_chunked) {
1181 if (_send_buffer (priv->handle, GST_BUFFER_DATA (buff), GST_BUFFER_SIZE (buff)) == FALSE)
1184 GnomeVFSResult result;
1185 GnomeVFSFileSize bytes_written;
1187 result = gnome_vfs_write (priv->handle,
1188 GST_BUFFER_DATA (buff),
1189 GST_BUFFER_SIZE (buff),
1192 if (result != GNOME_VFS_OK)
1199 if (priv->tick_id != 0) {
1200 g_source_remove(priv->tick_id);
1203 g_signal_emit(user_data, g_mencoder_signals[ERROR], 0, "Fail to write on socket");
1204 gst_element_set_state (priv->pipe, GST_STATE_PAUSED);