gmyth-stream/gmencoder/src/gmencoder.c
author renatofilho
Wed Sep 26 19:04:24 2007 +0100 (2007-09-26)
branchtrunk
changeset 857 dda04b6cf0b2
parent 847 3c18c36245e7
child 888 f1af7f8cd7d9
permissions -rw-r--r--
[svn r863] create a flag to work with decodebin1
     1 #ifdef HAVE_CONFIG_H
     2 #include "config.h"
     3 #endif
     4 
     5 #include <sys/stat.h>
     6 #include <fcntl.h>
     7 #include <unistd.h>
     8 #include <glib.h>
     9 #include <gst/gst.h>
    10 #include <string.h>
    11 #include <sys/types.h>
    12 #include <sys/socket.h>
    13 #include <libgnomevfs/gnome-vfs.h>
    14 #include <gst/interfaces/tuner.h>
    15 
    16 #include "gmencoder.h"
    17 
    18 #define G_MENCODER_GET_PRIVATE(obj) \
    19     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), G_TYPE_MENCODER, GMencoderPrivate))
    20 
    21 #define USE_MANUAL_SINK
    22 #define GMENCODER_TIMEOUT	5000
    23 
    24 typedef struct _GMencoderPrivate GMencoderPrivate;
    25 typedef struct _SetupInfo SetupInfo;
    26 
    27 struct _SetupInfo {
    28     gchar          *video_encode;
    29     gchar          *mux_name;
    30     gchar         **video_encode_prop;
    31     gdouble         video_fps;
    32     gdouble         video_rate;
    33     guint           video_width;
    34     guint           video_height;
    35     gchar          *audio_encode;
    36     gchar         **audio_encode_prop;
    37     guint           audio_rate;
    38 };
    39 
    40 
    41 struct _GMencoderPrivate {
    42     GstElement     *pipe;
    43     GstElement     *abin;
    44     GstElement     *vbin;
    45     GstElement     *sink;
    46     GstElement     *src;
    47 
    48     GnomeVFSHandle  *handle;
    49 
    50     gboolean        ready;
    51     SetupInfo      *info;
    52     GstClockTime    videot;
    53     GstClockTime    audiot;
    54     gint            sources;
    55     gint            tick_id;
    56     gint64          duration;
    57     gboolean        send_chunked;
    58     gint 	    timeout_id;
    59 
    60     //V4l info
    61     GstElement *v4lsrc;
    62     gchar *channel;
    63     gchar *norm;
    64     glong frequency;
    65 };
    66 
    67 enum {
    68     PAUSED,
    69     PLAYING,
    70     STOPED,
    71     EOS,
    72     ERROR,
    73     LAST_SIGNAL
    74 };
    75 
    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,
    83                                      gchar ** encode_prop,
    84                                      gdouble fps,
    85                                      gint rate, guint width, guint height,
    86                                      gboolean use_deinterlace);
    87 
    88 static          gboolean
    89 _pipeline_bus_cb(GstBus * bus, GstMessage * msg, gpointer user_data);
    90 
    91 static void     _decodebin_new_pad_cb(GstElement * object,
    92                                       GstPad * pad,
    93                                       gboolean flag, gpointer user_data);
    94 
    95 static void     _decodebin_unknown_type_cb(GstElement * object,
    96                                            GstPad * pad,
    97                                            GstCaps * caps,
    98                                            gpointer user_data);
    99 
   100 static void     _close_output(GMencoder * self);
   101 static gboolean _open_output(GMencoder * self, const gchar * uri);
   102 
   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,
   108                                     gdouble video_fps,
   109                                     gdouble video_rate,
   110                                     guint video_width,
   111                                     guint video_height,
   112                                     const gchar * audio_encode,
   113                                     gchar ** audio_encode_prop,
   114                                     guint audio_rate,
   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,
   120                                      GstBuffer* buff,
   121                                      GstPad* pad,
   122                                      gpointer user_data);
   123 #endif
   124 
   125 
   126 static gboolean _tick_cb(gpointer data);
   127 
   128 static guint    g_mencoder_signals[LAST_SIGNAL] = { 0 };
   129 
   130 G_DEFINE_TYPE(GMencoder, g_mencoder, G_TYPE_OBJECT)
   131 
   132 static void     g_mencoder_class_init(GMencoderClass * klass)
   133 {
   134     GObjectClass   *object_class;
   135     object_class = (GObjectClass *) klass;
   136     g_type_class_add_private(klass, sizeof(GMencoderPrivate));
   137 
   138     object_class->dispose = g_mencoder_dispose;
   139     object_class->finalize = g_mencoder_finalize;
   140 
   141     g_mencoder_signals[PAUSED] =
   142         g_signal_new("paused",
   143                      G_OBJECT_CLASS_TYPE(object_class),
   144                      G_SIGNAL_RUN_FIRST,
   145                      0, NULL, NULL,
   146                      g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
   147 
   148     g_mencoder_signals[PLAYING] =
   149         g_signal_new("playing",
   150                      G_OBJECT_CLASS_TYPE(object_class),
   151                      G_SIGNAL_RUN_FIRST,
   152                      0, NULL, NULL,
   153                      g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
   154 
   155     g_mencoder_signals[STOPED] =
   156         g_signal_new("stoped",
   157                      G_OBJECT_CLASS_TYPE(object_class),
   158                      G_SIGNAL_RUN_FIRST,
   159                      0, NULL, NULL,
   160                      g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
   161 
   162     g_mencoder_signals[EOS] =
   163         g_signal_new("eos",
   164                      G_OBJECT_CLASS_TYPE(object_class),
   165                      G_SIGNAL_RUN_FIRST,
   166                      0, NULL, NULL,
   167                      g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
   168 
   169     g_mencoder_signals[ERROR] =
   170         g_signal_new("error",
   171                      G_OBJECT_CLASS_TYPE(object_class),
   172                      G_SIGNAL_RUN_LAST,
   173                      0, NULL, NULL,
   174                      g_cclosure_marshal_VOID__STRING,
   175                      G_TYPE_NONE, 1, G_TYPE_STRING);
   176 }
   177 
   178 static void
   179 g_mencoder_init(GMencoder * self)
   180 {
   181     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   182     priv->info = g_new0(SetupInfo, 1);
   183 }
   184 
   185 static void
   186 g_mencoder_dispose(GObject * object)
   187 {
   188 }
   189 
   190 static void
   191 g_mencoder_finalize(GObject * object)
   192 {
   193     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(object);
   194 
   195     // TODO: clear vars
   196     g_mencoder_close_stream(G_MENCODER(object));
   197     g_free (priv->info);
   198 }
   199 
   200 GMencoder      *
   201 g_mencoder_new(void)
   202 {
   203     return g_object_new(G_TYPE_MENCODER, NULL);
   204 }
   205 
   206 
   207 static void
   208 _obj_set_prop(GObject * obj, const gchar * prop_name,
   209               const gchar * prop_val)
   210 {
   211     GValue          p = { 0 };
   212     GValue          v = { 0 };
   213     GParamSpec     *s = NULL;
   214     GObjectClass   *k = G_OBJECT_GET_CLASS(obj);
   215 
   216 
   217     g_value_init(&v, G_TYPE_STRING);
   218     g_value_set_string(&v, prop_val);
   219 
   220     s = g_object_class_find_property(k, prop_name);
   221     if (s == NULL) {
   222         g_print("Invalid property name: %s\n", prop_name);
   223         return;
   224     }
   225 
   226     g_value_init(&p, s->value_type);
   227     switch (s->value_type) {
   228     case G_TYPE_INT:
   229         g_value_set_int(&p, atoi(prop_val));
   230         break;
   231     case G_TYPE_ULONG:
   232         g_value_set_ulong (&p, atol(prop_val));
   233         break;
   234     case G_TYPE_STRING:
   235         g_value_set_string(&p, prop_val);
   236         break;
   237     case G_TYPE_BOOLEAN:
   238         g_value_set_boolean(&p, (gboolean) atoi (prop_val));
   239         break;
   240     case G_TYPE_DOUBLE:
   241         g_value_set_double(&p, atof (prop_val));
   242         break;
   243     case G_TYPE_FLOAT:
   244         g_value_set_float(&p, (float) atof (prop_val));
   245         break;
   246     default:
   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));
   250         return;
   251     }
   252 
   253     g_object_set_property(obj, prop_name, &p);
   254     g_value_unset(&v);
   255     g_value_unset(&p);
   256 }
   257 
   258 static GstElement *
   259 _create_element_with_prop(const gchar * factory_name,
   260                           const gchar * element_name, gchar ** prop)
   261 {
   262     GstElement     *ret;
   263     int             i;
   264 
   265     ret = gst_element_factory_make(factory_name, element_name);
   266     if (ret == NULL)
   267         return NULL;
   268 
   269     if (prop != NULL) {
   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]);
   275                 }
   276                 g_strfreev(v);
   277             }
   278         }
   279     }
   280 
   281     return ret;
   282 
   283 }
   284 
   285 static GstElement *
   286 _create_audio_bin(const gchar * encode, gchar ** encode_prop, gint rate)
   287 {
   288     GstElement     *abin = NULL;
   289     GstElement     *aqueue = NULL;
   290     GstElement     *aconvert = NULL;
   291     GstElement     *aencode = NULL;
   292     GstElement     *aqueue_src = NULL;
   293     GstPad         *apad = NULL;
   294 
   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");
   300     aencode =
   301         _create_element_with_prop((encode ? encode : "lame"), "aencode",
   302                                   encode_prop);
   303     aqueue_src = gst_element_factory_make("queue", "aqueue_src");
   304 
   305     if ((abin == NULL) || (aqueue == NULL) || (aconvert == NULL)
   306         || (aencode == NULL) || (aqueue_src == NULL)) {
   307         g_warning("Audio elements not found");
   308         goto error;
   309     }
   310 
   311     g_object_set(G_OBJECT(aencode), "bitrate", 32, NULL);
   312     /*
   313      * if (rate > 0) { g_object_set (G_OBJECT (aencode), "bitrate", 32,
   314      * NULL); } 
   315      */
   316 
   317     gst_bin_add_many(GST_BIN(abin), aqueue, aconvert, aencode, aqueue_src,
   318                      NULL);
   319     if (gst_element_link_many(aqueue, aconvert, aencode, aqueue_src, NULL)
   320         == FALSE) {
   321         g_warning("Not Link audio elements");
   322     }
   323     // TODO: apply audio rate
   324 
   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);
   329 
   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);
   333 
   334     return abin;
   335   error:
   336     if (abin != NULL)
   337         gst_object_unref(abin);
   338 
   339     if (aqueue != NULL)
   340         gst_object_unref(aqueue);
   341 
   342     if (aconvert != NULL)
   343         gst_object_unref(aconvert);
   344 
   345     if (aencode != NULL)
   346         gst_object_unref(aencode);
   347 
   348     if (aqueue_src != NULL)
   349         gst_object_unref(aqueue_src);
   350 
   351     if (apad != NULL)
   352         gst_object_unref(apad);
   353 
   354     return NULL;
   355 }
   356 
   357 
   358 
   359 
   360 // queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! colorspace
   361 // ! rate ! encode ! queue
   362 static GstElement *
   363 _create_video_bin(const gchar * encode,
   364                   gchar ** encode_prop,
   365                   gdouble fps, gint rate, guint width, guint height,
   366                   gboolean use_deinterlace)
   367 {
   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;
   376     GstPad         *vpad = NULL;
   377 
   378     vbin = gst_bin_new("vbin");
   379     vqueue = gst_element_factory_make("queue", "vqueue");
   380     vcolorspace =
   381         gst_element_factory_make("ffmpegcolorspace", "colorspace");
   382 
   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.");
   387         }
   388     }
   389 
   390 
   391     vencode = _create_element_with_prop((encode !=
   392                                          NULL ? encode :
   393                                          "ffenc_mpeg1video"), "vencode",
   394                                         encode_prop);
   395     vqueue_src = gst_element_factory_make("queue", "queue_src");
   396 
   397     if ((vbin == NULL) || (vqueue == NULL) || (vcolorspace == NULL)
   398         || (vencode == NULL) || (vqueue_src == NULL)) {
   399         g_warning("Video elements not found");
   400         goto error;
   401     }
   402 
   403     gst_bin_add_many(GST_BIN(vbin), vqueue, vcolorspace, vencode,
   404                      vqueue_src, NULL);
   405 
   406     if (deinterlace != NULL) {
   407         gst_bin_add(GST_BIN(vbin), deinterlace);
   408         gst_element_link (vqueue, deinterlace);
   409         walk = deinterlace;
   410     } else {
   411         walk = vqueue;
   412     }
   413 
   414     if ((width > 0) && (height > 0)) {
   415         // Scalling video
   416         GstCaps        *vcaps;
   417         GstElement     *vscale =
   418             gst_element_factory_make("videoscale", "vscale");
   419 
   420 	g_object_set (G_OBJECT (vscale), "method", 1, NULL);
   421 
   422         gst_bin_add(GST_BIN(vbin), vscale);
   423 
   424         vcaps = gst_caps_new_simple("video/x-raw-yuv",
   425                                     "width", G_TYPE_INT, width,
   426                                     "height", G_TYPE_INT, height, NULL);
   427 
   428         gst_element_link(walk, vscale);
   429 
   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);
   434             goto error;
   435         }
   436         gst_caps_unref(vcaps);
   437     } else {
   438         gst_element_link(walk, vcolorspace);
   439     }
   440 
   441     if (fps > 0) {
   442         // Changing the video fps
   443         GstCaps        *vcaps;
   444         vrate = gst_element_factory_make("videorate", "vrate");
   445 
   446         gst_bin_add(GST_BIN(vbin), vrate);
   447 
   448         if (gst_element_link(vcolorspace, vrate) == FALSE) {
   449             g_warning("Fail to link video elements");
   450             goto error;
   451         }
   452 
   453         vcaps = gst_caps_new_simple("video/x-raw-yuv",
   454                                     "framerate", GST_TYPE_FRACTION,
   455                                     (int) (fps * 1000), 1000, NULL);
   456 
   457         if (gst_element_link_filtered(vrate, vencode, vcaps) == FALSE) {
   458             g_warning("Fail to link vrate with vencode.");
   459             goto error;
   460         }
   461         gst_caps_unref(vcaps);
   462     } else {
   463         if (gst_element_link(vcolorspace, vencode) == FALSE) {
   464             g_warning("Fail to link colorspace and video encode element.");
   465             goto error;
   466         }
   467     }
   468 
   469     gst_element_link(vencode, vqueue_src);
   470 
   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);
   475 
   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);
   479 
   480     return vbin;
   481 
   482   error:
   483     if (vpad != NULL)
   484         gst_object_unref(vpad);
   485 
   486     if (vbin != NULL)
   487         gst_object_unref(vbin);
   488 
   489     if (vqueue != NULL)
   490         gst_object_unref(vqueue);
   491 
   492     if (vencode != NULL)
   493         gst_object_unref(vencode);
   494 
   495     if (vqueue_src != NULL)
   496         gst_object_unref(vqueue_src);
   497 
   498     if (vcolorspace != NULL)
   499         gst_object_unref(vcolorspace);
   500 
   501     return NULL;
   502 }
   503 
   504 
   505 
   506 gboolean 
   507 g_mencoder_setup_stream(GMencoder * self,
   508 			gboolean chunked,
   509 			gboolean deinterlace,
   510                         const gchar * mux_name,
   511                         const gchar * video_encode,
   512                         gchar ** video_encode_prop,
   513                         gdouble video_fps,
   514                         gdouble video_rate,
   515                         guint video_width,
   516                         guint video_height,
   517                         const gchar * audio_encode,
   518                         gchar ** audio_encode_prop,
   519                         guint audio_rate, const gchar * out_uri)
   520 {
   521     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   522     if (priv->ready == TRUE) {
   523         g_warning
   524             ("Stream already configured. You need close stream first.");
   525         return FALSE;
   526     }
   527 
   528     _close_output(self);
   529     if (_open_output(self, out_uri) == FALSE) {	
   530         return FALSE;
   531     }
   532 
   533     priv->sources = 0;
   534     priv->send_chunked = chunked;
   535     priv->pipe = _create_pipeline(self,
   536                                   video_encode,
   537                                   mux_name,
   538                                   video_encode_prop,
   539                                   video_fps,
   540                                   video_rate,
   541                                   video_width,
   542                                   video_height,
   543                                   audio_encode, audio_encode_prop,
   544                                   audio_rate,
   545                                   deinterlace);
   546 
   547     return (priv->pipe != NULL);
   548 }
   549 
   550 
   551 gboolean
   552 g_mencoder_append_uri(GMencoder * self, const gchar * uri)
   553 {
   554     GstPad         *pad_src;
   555     GstPad         *pad_sink;
   556     GstElement     *src;
   557     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   558     gboolean        ret = FALSE;
   559     GstElement     *ap = NULL;
   560     GstElement     *vp = NULL;
   561 
   562 
   563     g_return_val_if_fail(priv->pipe != NULL, FALSE);
   564     g_return_val_if_fail(priv->ready == FALSE, FALSE);
   565 
   566     src = _create_source(self, uri);
   567     if (src == NULL)
   568         return FALSE;
   569 
   570     priv->src = gst_bin_get_by_name(GST_BIN(src), "src");
   571 
   572     gst_bin_add(GST_BIN(priv->pipe), src);
   573 
   574     ap = gst_bin_get_by_name(GST_BIN(priv->pipe), "abin");
   575     vp = gst_bin_get_by_name(GST_BIN(priv->pipe), "vbin");
   576 
   577     if ((vp == NULL) || (ap == NULL)) {
   578         g_warning("Fail to get output bin");
   579         goto error;
   580     }
   581 
   582     pad_src = gst_element_get_pad(src, "src_audio");
   583     pad_sink = gst_element_get_compatible_pad(ap,
   584                                               pad_src,
   585                                               gst_pad_get_caps(pad_src));
   586 
   587     if ((pad_sink == NULL) || (pad_src == NULL))
   588         goto error;
   589 
   590     GstPadLinkReturn lret = gst_pad_link(pad_src, pad_sink);
   591     if (lret != GST_PAD_LINK_OK)
   592         goto error;
   593 
   594     gst_object_unref(pad_src);
   595     gst_object_unref(pad_sink);
   596 
   597     pad_src = gst_element_get_pad(src, "src_video");
   598     pad_sink = gst_element_get_compatible_pad(vp,
   599                                               pad_src,
   600                                               gst_pad_get_caps(pad_src));
   601 
   602     if ((pad_src == NULL) || (pad_sink == NULL))
   603         goto error;
   604 
   605     if (gst_pad_link(pad_src, pad_sink) != GST_PAD_LINK_OK) {
   606         g_warning("invalid source. video");
   607         goto error;
   608     }
   609 
   610     priv->sources++;
   611     ret = TRUE;
   612   error:
   613 
   614     if ((src != NULL) && (ret == FALSE)) {
   615         gst_bin_remove(GST_BIN(priv->pipe), src);
   616         gst_object_unref(src);
   617     }
   618 
   619     if (ap != NULL)
   620         gst_object_unref(ap);
   621 
   622     if (vp != NULL)
   623         gst_object_unref(vp);
   624 
   625     if (pad_src != NULL)
   626         gst_object_unref(pad_src);
   627 
   628     if (pad_sink != NULL)
   629         gst_object_unref(pad_sink);
   630 
   631     return ret;
   632 }
   633 
   634 
   635 
   636 void
   637 g_mencoder_remove_uri(GMencoder * self, const gchar * uri)
   638 {
   639     // GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
   640     // TODO: remove src
   641 }
   642 
   643 void
   644 g_mencoder_play_stream(GMencoder * self)
   645 {
   646     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   647     g_return_if_fail(priv->ready == FALSE);
   648     priv->ready = TRUE;
   649     gst_element_set_state(priv->pipe, GST_STATE_PLAYING);
   650     if (priv->tick_id != 0) {
   651         g_source_remove (priv->tick_id);
   652     }
   653     priv->tick_id = g_timeout_add(500, _tick_cb, self);
   654 
   655     if (priv->timeout_id != 0) {
   656         g_source_remove (priv->timeout_id);
   657     }
   658     //priv->timeout_id = g_timeout_add(GMENCODER_TIMEOUT, _process_timeout_cb, self);
   659 }
   660 
   661 void
   662 g_mencoder_pause_stream(GMencoder * self)
   663 {
   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);
   667 }
   668 
   669 void
   670 g_mencoder_close_stream(GMencoder * self)
   671 {
   672 
   673     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   674     if (priv->tick_id != 0) {
   675         g_source_remove(priv->tick_id);
   676         priv->tick_id = 0;
   677     }
   678 
   679     if (priv->timeout_id != 0) {
   680         g_source_remove (priv->timeout_id);
   681 	priv->timeout_id = 0;
   682     }
   683 
   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);
   691         priv->src = NULL;
   692         priv->pipe = NULL;
   693         priv->abin = NULL;
   694         priv->vbin = NULL;
   695         priv->sink = NULL;
   696     }
   697     priv->ready = FALSE;
   698 }
   699 
   700 static GstElement *
   701 _create_pipeline(GMencoder * self,
   702                  const gchar * video_encode,
   703                  const gchar * mux_name,
   704                  gchar ** video_encode_prop,
   705                  gdouble video_fps,
   706                  gdouble video_rate,
   707                  guint video_width,
   708                  guint video_height,
   709                  const gchar * audio_encode,
   710                  gchar ** audio_encode_prop, guint audio_rate,
   711 		         gboolean deinterlace)
   712 {
   713     GstBus         *bus = NULL;
   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;
   722 
   723     pipe = gst_pipeline_new("pipe");
   724 
   725     mux =
   726         gst_element_factory_make((mux_name ? mux_name : "ffmux_mpeg"),
   727                                  "mux");
   728     if (mux == NULL)
   729         goto error;
   730 
   731     queue = gst_element_factory_make("queue", "queueu_sink");
   732 
   733 
   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),
   737                       "handoff",
   738                       G_CALLBACK (_buffer_arrive_cb),
   739                       self);
   740 
   741     abin = _create_audio_bin(audio_encode, audio_encode_prop, audio_rate);
   742     if (abin == NULL)
   743         goto error;
   744 
   745     vbin =
   746         _create_video_bin(video_encode, video_encode_prop, video_fps,
   747                           video_rate, video_width, video_height, deinterlace);
   748     if (vbin == NULL)
   749         goto error;
   750 
   751     // Finish Pipe
   752     gst_bin_add_many(GST_BIN(pipe), abin, vbin, mux, queue, sink, NULL);
   753 
   754 
   755     // Link bins with mux
   756     aux_pad = gst_element_get_pad(abin, "src");
   757     mux_pad =
   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");
   762         goto error;
   763     }
   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);
   767         goto error;
   768 
   769     }
   770     gst_object_unref(aux_pad);
   771     gst_object_unref(mux_pad);
   772 
   773     aux_pad = gst_element_get_pad(vbin, "src");
   774     mux_pad =
   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");
   779         goto error;
   780     }
   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);
   784         goto error;
   785     }
   786     gst_object_unref(aux_pad);
   787     gst_object_unref(mux_pad);
   788     aux_pad = NULL;
   789     mux_pad = NULL;
   790 
   791     // Link mux with sink
   792     gst_element_link_many(mux, queue, sink, NULL);
   793 
   794     bus = gst_pipeline_get_bus(GST_PIPELINE(pipe));
   795     gst_bus_add_watch(bus, _pipeline_bus_cb, self);
   796     gst_object_unref(bus);
   797     return pipe;
   798 
   799   error:
   800     g_warning("Invalid uri");
   801 
   802     if (pipe != NULL) {
   803         gst_object_unref(pipe);
   804     }
   805 
   806 
   807     if (mux != NULL) {
   808         gst_object_unref(mux);
   809     }
   810 
   811     if (mux_pad != NULL) {
   812         gst_object_unref(mux_pad);
   813     }
   814 
   815     if (aux_pad != NULL) {
   816         gst_object_unref(mux_pad);
   817     }
   818 
   819     if (sink != NULL) {
   820         gst_object_unref(sink);
   821     }
   822 
   823     if (abin != NULL) {
   824         gst_object_unref(abin);
   825     }
   826 
   827     if (vbin != NULL) {
   828         gst_object_unref(vbin);
   829     }
   830 
   831     return FALSE;
   832 }
   833 
   834 
   835 static void
   836 _close_output(GMencoder * self)
   837 {
   838 }
   839 
   840 static GstElement *
   841 _create_v4l_source (GMencoder *self, const gchar * uri)
   842 {
   843     gchar **info;
   844     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   845 
   846 
   847     info = g_strsplit (uri+6, ":", 3);
   848     if (g_strv_length (info) != 3) {
   849         return NULL;
   850     }
   851 
   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),
   855                   "channel", info[0],
   856                   "norm", info[1],
   857                   "frequency", atoi (info[2]),
   858                   NULL);
   859 
   860     return priv->v4lsrc;
   861 }
   862 
   863 static GstElement *
   864 _create_source(GMencoder *self, const gchar * uri)
   865 {
   866 
   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;
   873 
   874 
   875     bsrc = gst_bin_new(NULL);
   876 
   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) {
   880         g_debug ("V4L");
   881         src = _create_v4l_source (self, uri);
   882     }
   883     else {
   884         src = gst_element_make_from_uri(GST_URI_SRC, uri, "src");
   885     }
   886 
   887     if (src == NULL)
   888         goto error;
   889 
   890 
   891     if (g_getenv ("USE_DECODEBIN1"))
   892       decode = gst_element_factory_make("decodebin", "decode");
   893     else
   894       decode = gst_element_factory_make("decodebin2", "decode");
   895     if (decode == NULL)
   896         goto error;
   897 
   898     aqueue = gst_element_factory_make("queue", "aqueue");
   899     if (aqueue == NULL)
   900         goto error;
   901 
   902     vqueue = gst_element_factory_make("queue", "vqueue");
   903     if (vqueue == NULL)
   904         goto error;
   905 
   906     gst_bin_add_many(GST_BIN(bsrc), src, decode, aqueue, vqueue,
   907                      NULL);
   908     gst_element_link (src, decode);
   909 
   910     g_signal_connect(G_OBJECT(decode),
   911                      "new-decoded-pad",
   912                      G_CALLBACK(_decodebin_new_pad_cb), bsrc);
   913 
   914     g_signal_connect(G_OBJECT(decode),
   915                      "unknown-type",
   916                      G_CALLBACK(_decodebin_unknown_type_cb), pipe);
   917 
   918     src_pad = gst_element_get_pad(aqueue, "src");
   919     gst_element_add_pad(bsrc, gst_ghost_pad_new("src_audio", src_pad));
   920     gst_object_unref(src_pad);
   921 
   922     src_pad = gst_element_get_pad(vqueue, "src");
   923     gst_element_add_pad(bsrc, gst_ghost_pad_new("src_video", src_pad));
   924     gst_object_unref(src_pad);
   925 
   926     return bsrc;
   927 
   928   error:
   929     g_debug ("Fail to create source element");
   930     if (src != NULL) {
   931         gst_object_unref(src);
   932     }
   933 
   934     if (decode != NULL) {
   935         gst_object_unref(decode);
   936     }
   937 
   938     if (aqueue != NULL) {
   939         gst_object_unref(aqueue);
   940     }
   941 
   942     if (vqueue != NULL) {
   943         gst_object_unref(vqueue);
   944     }
   945 
   946     return NULL;
   947 }
   948 
   949 static gboolean
   950 _open_output(GMencoder * self, const gchar * uri)
   951 {
   952     gchar         **i;
   953     GnomeVFSResult result;
   954     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   955 
   956     i = g_strsplit(uri, "://", 0);
   957     if (strcmp(i[0], "fd") == 0) {
   958         result = gnome_vfs_open_fd (&priv->handle, atoi(i[1]));
   959     } else {
   960         if (g_file_test (i[1], G_FILE_TEST_EXISTS) == FALSE) {
   961             result = gnome_vfs_create (&priv->handle, uri, GNOME_VFS_OPEN_WRITE, FALSE,
   962                               GNOME_VFS_PERM_USER_WRITE | GNOME_VFS_PERM_USER_READ | GNOME_VFS_PERM_GROUP_READ);
   963         } else {
   964             result = gnome_vfs_open (&priv->handle, uri,
   965                                      GNOME_VFS_OPEN_WRITE | GNOME_VFS_OPEN_TRUNCATE);
   966         }
   967     }
   968 
   969     g_strfreev(i);
   970     return (result == GNOME_VFS_OK);
   971 }
   972 
   973 static          gboolean
   974 _pipeline_bus_cb(GstBus * bus, GstMessage * msg, gpointer user_data)
   975 {
   976     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
   977 
   978     switch (GST_MESSAGE_TYPE(msg)) {
   979 
   980     case GST_MESSAGE_STATE_CHANGED:
   981         {
   982             GstState        oldstate;
   983             GstState        newstate;
   984             GstState        pendingstate;
   985 
   986 
   987             gst_message_parse_state_changed(msg, &oldstate,
   988                                             &newstate, &pendingstate);
   989 
   990             if (pendingstate != GST_STATE_VOID_PENDING)
   991                 break;
   992 
   993             if ((oldstate == GST_STATE_READY)
   994                 && (newstate == GST_STATE_PAUSED)) {
   995                 if (priv->ready)
   996                     g_signal_emit(user_data, g_mencoder_signals[PAUSED],
   997                                   0);
   998             } else if ((oldstate == GST_STATE_PAUSED)
   999                        && (newstate == GST_STATE_PLAYING)) {
  1000                 g_signal_emit(user_data, g_mencoder_signals[PLAYING], 0);
  1001             } else if ((oldstate == GST_STATE_READY) &&
  1002                        (newstate == GST_STATE_NULL)) {
  1003                 g_signal_emit(user_data, g_mencoder_signals[STOPED], 0);
  1004             }
  1005             break;
  1006         }
  1007 
  1008     case GST_MESSAGE_ERROR:
  1009         {
  1010             GError         *error;
  1011             gchar          *debug;
  1012             gchar          *err_str;
  1013 
  1014             if (priv->tick_id != 0) {
  1015                 g_source_remove(priv->tick_id);
  1016                 priv->tick_id = 0;
  1017             }
  1018 
  1019             gst_message_parse_error(msg, &error, &debug);
  1020             err_str = g_strdup_printf("Error [%d] %s (%s)", error->code,
  1021                                       error->message, debug);
  1022             priv->ready = FALSE;
  1023             g_signal_emit(user_data, g_mencoder_signals[ERROR], 0,
  1024                           err_str);
  1025             g_free(err_str);
  1026             g_clear_error(&error);
  1027             g_free(debug);
  1028             break;
  1029         }
  1030 
  1031     case GST_MESSAGE_EOS:
  1032         priv->ready = FALSE;
  1033 #ifdef USE_MANUAL_SINK
  1034         _flush_queue (G_MENCODER (user_data));
  1035 #endif
  1036         g_signal_emit(user_data, g_mencoder_signals[EOS], 0);
  1037         break;
  1038 
  1039     case GST_MESSAGE_DURATION:
  1040         {
  1041             GstFormat       format;
  1042             gint64          duration;
  1043             gst_message_parse_duration(msg, &format, &duration);
  1044             if (format == GST_FORMAT_BYTES)
  1045                 priv->duration = duration;
  1046             break;
  1047         }
  1048     default:
  1049         {
  1050             break;
  1051         }
  1052     }
  1053     return TRUE;
  1054 }
  1055 
  1056 
  1057 
  1058 static void
  1059 _decodebin_new_pad_cb(GstElement * object,
  1060                       GstPad * pad, gboolean flag, gpointer user_data)
  1061 {
  1062     GstCaps        *caps;
  1063     gchar          *str_caps = NULL;
  1064     GstElement     *sink_element;
  1065     GstPad         *sink_pad;
  1066 
  1067     caps = gst_pad_get_caps(pad);
  1068     str_caps = gst_caps_to_string(caps);
  1069     if (strstr(str_caps, "audio") != NULL) {
  1070         sink_element = gst_bin_get_by_name(GST_BIN(user_data), "aqueue");
  1071     } else if (strstr(str_caps, "video") != NULL) {
  1072         sink_element = gst_bin_get_by_name(GST_BIN(user_data), "vqueue");
  1073     } else {
  1074         g_warning("invalid caps %s", str_caps);
  1075     }
  1076 
  1077     sink_pad = gst_element_get_pad(sink_element, "sink");
  1078     gst_pad_link(pad, sink_pad);
  1079 
  1080     gst_object_unref(sink_element);
  1081     gst_object_unref(sink_pad);
  1082     g_free(str_caps);
  1083     gst_caps_unref(caps);
  1084 }
  1085 
  1086 static void
  1087 _decodebin_unknown_type_cb(GstElement * object,
  1088                            GstPad * pad, GstCaps * caps,
  1089                            gpointer user_data)
  1090 {
  1091     g_warning("Unknown Type");
  1092     // priv->ready = FALSE;
  1093 }
  1094 
  1095 static          gboolean
  1096 _tick_cb(gpointer user_data)
  1097 {
  1098     GstFormat       format = GST_FORMAT_BYTES;
  1099     gint64          cur = 0;
  1100 
  1101     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
  1102 
  1103     if (priv->duration == 0) {
  1104         gint64          d = 0;
  1105         if (gst_element_query_duration(priv->src, &format, &d))
  1106             priv->duration = d;
  1107     }
  1108 
  1109     if (priv->duration != 0) {
  1110         gst_element_query_position(priv->src, &format, &cur);
  1111         g_print("PROGRESS:%lli\n", (99 * cur) / priv->duration);
  1112     }
  1113 
  1114     return TRUE;
  1115 }
  1116 
  1117 static gboolean 
  1118 _process_timeout_cb (gpointer user_data)
  1119 {
  1120     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
  1121 
  1122     g_signal_emit(user_data, g_mencoder_signals[ERROR], 0, "timeout");
  1123     priv->timeout_id = 0;
  1124     return FALSE;
  1125 }
  1126 
  1127 
  1128 #ifdef USE_MANUAL_SINK
  1129 static gboolean
  1130 _send_buffer (GnomeVFSHandle *handle, gpointer buff, gint size)
  1131 {
  1132     gchar *msg;
  1133     GByteArray *b_send;
  1134     GnomeVFSResult result;
  1135     GnomeVFSFileSize bytes_written;
  1136 
  1137     b_send = g_byte_array_new ();
  1138     msg = g_strdup_printf ("%x\r\n", size);
  1139     b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
  1140     g_free (msg);
  1141 
  1142     b_send = g_byte_array_append (b_send, buff, size);
  1143 
  1144     msg = g_strdup ("\r\n");
  1145     b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
  1146     g_free (msg);
  1147 
  1148     result = gnome_vfs_write (handle, b_send->data, b_send->len, &bytes_written);
  1149     g_byte_array_free (b_send, TRUE);
  1150 
  1151     return (result == GNOME_VFS_OK);
  1152 }
  1153 
  1154 static void
  1155 _flush_queue (GMencoder *self)
  1156 {
  1157     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
  1158 
  1159     if (priv->send_chunked) {
  1160         GnomeVFSFileSize bytes_written;
  1161         gchar *end_msg;
  1162         end_msg = g_strdup ("0\r\n\r\n");
  1163         gnome_vfs_write (priv->handle,
  1164                          (const guint8*) end_msg,
  1165                          strlen(end_msg) * sizeof(gchar),
  1166                          &bytes_written);
  1167         g_free (end_msg);
  1168     }
  1169 }
  1170 
  1171 static void
  1172 _buffer_arrive_cb (GstElement* object,
  1173                    GstBuffer* buff,
  1174                    GstPad* pad,
  1175                    gpointer user_data)
  1176 {
  1177     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
  1178 
  1179     if (priv->timeout_id != 0) {
  1180 	g_source_remove (priv->timeout_id);
  1181 	priv->timeout_id = 0;
  1182     }
  1183 
  1184     if (priv->send_chunked) {
  1185         if (_send_buffer (priv->handle, GST_BUFFER_DATA (buff), GST_BUFFER_SIZE (buff)) == FALSE)
  1186             goto error;
  1187     } else {
  1188         GnomeVFSResult result;
  1189         GnomeVFSFileSize bytes_written;
  1190 
  1191         result = gnome_vfs_write (priv->handle,
  1192                                   GST_BUFFER_DATA (buff),
  1193                                   GST_BUFFER_SIZE (buff),
  1194                                   &bytes_written);
  1195 
  1196         if (result != GNOME_VFS_OK)
  1197             goto error;
  1198     }
  1199 
  1200     return;
  1201 
  1202 error:
  1203     if (priv->tick_id != 0) {
  1204         g_source_remove(priv->tick_id);
  1205         priv->tick_id = 0;
  1206     }
  1207     g_signal_emit(user_data, g_mencoder_signals[ERROR], 0, "Fail to write on socket");
  1208     gst_element_set_state (priv->pipe, GST_STATE_PAUSED);
  1209 }
  1210 
  1211 #endif