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