gmyth-stream/gmencoder/src/gmencoder.c
author renatofilho
Wed Jan 23 20:04:59 2008 +0000 (2008-01-23)
branchtrunk
changeset 891 0cc42675b88d
parent 888 f1af7f8cd7d9
child 897 715f854a93cd
permissions -rw-r--r--
[svn r897] fixed channel_id, program_id types
     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_UINT:
   232         g_value_set_uint (&p, (guint) atoi(prop_val));
   233         break;
   234     case G_TYPE_ULONG:
   235         g_value_set_ulong (&p, atol(prop_val));
   236         break;
   237     case G_TYPE_STRING:
   238         g_value_set_string(&p, prop_val);
   239         break;
   240     case G_TYPE_BOOLEAN:
   241         g_value_set_boolean(&p, (gboolean) atoi (prop_val));
   242         break;
   243     case G_TYPE_DOUBLE:
   244         g_value_set_double(&p, atof (prop_val));
   245         break;
   246     case G_TYPE_FLOAT:
   247         g_value_set_float(&p, (float) atof (prop_val));
   248         break;
   249     default:
   250         g_value_set_enum(&p, atoi(prop_val));
   251         g_warning ("Property %s of type %s. Not supported using default enum", 
   252                    prop_name, g_type_name (s->value_type));
   253         return;
   254     }
   255 
   256     g_object_set_property(obj, prop_name, &p);
   257     g_value_unset(&v);
   258     g_value_unset(&p);
   259 }
   260 
   261 static GstElement *
   262 _create_element_with_prop(const gchar * factory_name,
   263                           const gchar * element_name, gchar ** prop)
   264 {
   265     GstElement     *ret;
   266     int             i;
   267 
   268     ret = gst_element_factory_make(factory_name, element_name);
   269     if (ret == NULL)
   270         return NULL;
   271 
   272     if (prop != NULL) {
   273         for (i = 0; i < g_strv_length(prop); i++) {
   274             if (prop[i] != NULL) {
   275                 char **v = g_strsplit(prop[i], "=", 2);
   276                 if (g_strv_length(v) == 2) {
   277                     _obj_set_prop(G_OBJECT(ret), v[0], v[1]);
   278                 }
   279                 g_strfreev(v);
   280             }
   281         }
   282     }
   283 
   284     return ret;
   285 
   286 }
   287 
   288 static GstElement *
   289 _create_audio_bin(const gchar * encode, gchar ** encode_prop, gint rate)
   290 {
   291     GstElement     *abin = NULL;
   292     GstElement     *aqueue = NULL;
   293     GstElement     *aconvert = NULL;
   294     GstElement     *aencode = NULL;
   295     GstElement     *aqueue_src = NULL;
   296     GstPad         *apad = NULL;
   297 
   298     // audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay !
   299     // udpsink name=upd_audio host=224.0.0.1 port=5002
   300     abin = gst_bin_new("abin");
   301     aqueue = gst_element_factory_make("queue", "aqueue");
   302     aconvert = gst_element_factory_make("audioconvert", "aconvert");
   303     aencode =
   304         _create_element_with_prop((encode ? encode : "lame"), "aencode",
   305                                   encode_prop);
   306     aqueue_src = gst_element_factory_make("queue", "aqueue_src");
   307 
   308     if ((abin == NULL) || (aqueue == NULL) || (aconvert == NULL)
   309         || (aencode == NULL) || (aqueue_src == NULL)) {
   310         g_warning("Audio elements not found");
   311         goto error;
   312     }
   313 
   314     /*
   315      * if (rate > 0) { g_object_set (G_OBJECT (aencode), "bitrate", 32,
   316      * NULL); } 
   317      */
   318 
   319     gst_bin_add_many(GST_BIN(abin), aqueue, aconvert, aencode, aqueue_src,
   320                      NULL);
   321     if (gst_element_link_many(aqueue, aconvert, aencode, aqueue_src, NULL)
   322         == FALSE) {
   323         g_warning("Not Link audio elements");
   324     }
   325     // TODO: apply audio rate
   326 
   327     // ghost pad the audio bin
   328     apad = gst_element_get_pad(aqueue, "sink");
   329     gst_element_add_pad(abin, gst_ghost_pad_new("sink", apad));
   330     gst_object_unref(apad);
   331 
   332     apad = gst_element_get_pad(aqueue_src, "src");
   333     gst_element_add_pad(abin, gst_ghost_pad_new("src", apad));
   334     gst_object_unref(apad);
   335 
   336     return abin;
   337   error:
   338     if (abin != NULL)
   339         gst_object_unref(abin);
   340 
   341     if (aqueue != NULL)
   342         gst_object_unref(aqueue);
   343 
   344     if (aconvert != NULL)
   345         gst_object_unref(aconvert);
   346 
   347     if (aencode != NULL)
   348         gst_object_unref(aencode);
   349 
   350     if (aqueue_src != NULL)
   351         gst_object_unref(aqueue_src);
   352 
   353     if (apad != NULL)
   354         gst_object_unref(apad);
   355 
   356     return NULL;
   357 }
   358 
   359 
   360 
   361 
   362 // queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! colorspace
   363 // ! rate ! encode ! queue
   364 static GstElement *
   365 _create_video_bin(const gchar * encode,
   366                   gchar ** encode_prop,
   367                   gdouble fps, gint rate, guint width, guint height,
   368                   gboolean use_deinterlace)
   369 {
   370     GstElement     *vbin = NULL;
   371     GstElement     *vqueue = NULL;
   372     GstElement     *vqueue_src = NULL;
   373     GstElement     *vcolorspace = NULL;
   374     GstElement     *vencode = NULL;
   375     GstElement     *vrate = NULL;
   376     GstElement     *deinterlace = NULL;
   377     GstElement     *walk = NULL;
   378     GstPad         *vpad = NULL;
   379 
   380     vbin = gst_bin_new("vbin");
   381     vqueue = gst_element_factory_make("queue", "vqueue");
   382     vcolorspace =
   383         gst_element_factory_make("ffmpegcolorspace", "colorspace");
   384 
   385     if (use_deinterlace) {
   386         deinterlace = gst_element_factory_make ("ffdeinterlace", "deinterlace");
   387         if (deinterlace == NULL) {
   388            g_warning ("Fail to create deinterlace element: Continue without deinterlace.");
   389         }
   390     }
   391 
   392 
   393     vencode = _create_element_with_prop((encode !=
   394                                          NULL ? encode :
   395                                          "ffenc_mpeg1video"), "vencode",
   396                                         encode_prop);
   397     vqueue_src = gst_element_factory_make("queue", "queue_src");
   398 
   399     if ((vbin == NULL) || (vqueue == NULL) || (vcolorspace == NULL)
   400         || (vencode == NULL) || (vqueue_src == NULL)) {
   401         g_warning("Video elements not found");
   402         goto error;
   403     }
   404 
   405     gst_bin_add_many(GST_BIN(vbin), vqueue, vcolorspace, vencode,
   406                      vqueue_src, NULL);
   407 
   408     if (deinterlace != NULL) {
   409         gst_bin_add(GST_BIN(vbin), deinterlace);
   410         gst_element_link (vqueue, deinterlace);
   411         walk = deinterlace;
   412     } else {
   413         walk = vqueue;
   414     }
   415 
   416     if ((width > 0) && (height > 0)) {
   417         // Scalling video
   418         GstCaps        *vcaps;
   419         GstElement     *vscale;
   420 
   421         vscale = gst_element_factory_make("videoscale", "vscale");
   422         //g_object_set (G_OBJECT (vscale), "method", 1, NULL);
   423         gst_bin_add(GST_BIN(vbin), vscale);
   424 
   425         vcaps = gst_caps_new_simple("video/x-raw-yuv",
   426                                     "width", G_TYPE_INT, width,
   427                                     "height", G_TYPE_INT, height, NULL);
   428 
   429         gst_element_link(walk, vscale);
   430 
   431         if (gst_element_link_filtered(vscale, vcolorspace, vcaps) == FALSE) {
   432             g_warning("Fail to resize video");
   433             gst_object_unref(vcaps);
   434             gst_object_unref(vscale);
   435             goto error;
   436         }
   437         gst_caps_unref(vcaps);
   438     } else {
   439         gst_element_link(walk, vcolorspace);
   440     }
   441 
   442     if (fps > 0) {
   443         // Changing the video fps
   444         GstCaps        *vcaps;
   445         vrate = gst_element_factory_make("videorate", "vrate");
   446 
   447         gst_bin_add(GST_BIN(vbin), vrate);
   448 
   449         if (gst_element_link(vcolorspace, vrate) == FALSE) {
   450             g_warning("Fail to link video elements");
   451             goto error;
   452         }
   453 
   454         vcaps = gst_caps_new_simple("video/x-raw-yuv",
   455                                     "framerate", GST_TYPE_FRACTION,
   456                                     (int) (fps * 1000), 1000, NULL);
   457 
   458         if (gst_element_link_filtered(vrate, vencode, vcaps) == FALSE) {
   459             g_warning("Fail to link vrate with vencode.");
   460             goto error;
   461         }
   462         gst_caps_unref(vcaps);
   463     } else {
   464         if (gst_element_link(vcolorspace, vencode) == FALSE) {
   465             g_warning("Fail to link colorspace and video encode element.");
   466             goto error;
   467         }
   468     }
   469 
   470     gst_element_link(vencode, vqueue_src);
   471 
   472     // ghost pad the video bin
   473     vpad = gst_element_get_pad(vqueue, "sink");
   474     gst_element_add_pad(vbin, gst_ghost_pad_new("sink", vpad));
   475     gst_object_unref(vpad);
   476 
   477     vpad = gst_element_get_pad(vqueue_src, "src");
   478     gst_element_add_pad(vbin, gst_ghost_pad_new("src", vpad));
   479     gst_object_unref(vpad);
   480 
   481     return vbin;
   482 
   483   error:
   484     if (vpad != NULL)
   485         gst_object_unref(vpad);
   486 
   487     if (vbin != NULL)
   488         gst_object_unref(vbin);
   489 
   490     if (vqueue != NULL)
   491         gst_object_unref(vqueue);
   492 
   493     if (vencode != NULL)
   494         gst_object_unref(vencode);
   495 
   496     if (vqueue_src != NULL)
   497         gst_object_unref(vqueue_src);
   498 
   499     if (vcolorspace != NULL)
   500         gst_object_unref(vcolorspace);
   501 
   502     return NULL;
   503 }
   504 
   505 
   506 
   507 gboolean 
   508 g_mencoder_setup_stream(GMencoder * self,
   509 			gboolean chunked,
   510 			gboolean deinterlace,
   511                         const gchar * mux_name,
   512                         const gchar * video_encode,
   513                         gchar ** video_encode_prop,
   514                         gdouble video_fps,
   515                         gdouble video_rate,
   516                         guint video_width,
   517                         guint video_height,
   518                         const gchar * audio_encode,
   519                         gchar ** audio_encode_prop,
   520                         guint audio_rate, const gchar * out_uri)
   521 {
   522     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   523     if (priv->ready == TRUE) {
   524         g_warning
   525             ("Stream already configured. You need close stream first.");
   526         return FALSE;
   527     }
   528 
   529     _close_output(self);
   530     if (_open_output(self, out_uri) == FALSE) {	
   531         return FALSE;
   532     }
   533 
   534     priv->sources = 0;
   535     priv->send_chunked = chunked;
   536     priv->pipe = _create_pipeline(self,
   537                                   video_encode,
   538                                   mux_name,
   539                                   video_encode_prop,
   540                                   video_fps,
   541                                   video_rate,
   542                                   video_width,
   543                                   video_height,
   544                                   audio_encode, audio_encode_prop,
   545                                   audio_rate,
   546                                   deinterlace);
   547 
   548     return (priv->pipe != NULL);
   549 }
   550 
   551 
   552 gboolean
   553 g_mencoder_append_uri(GMencoder * self, const gchar * uri)
   554 {
   555     GstPad         *pad_src;
   556     GstPad         *pad_sink;
   557     GstElement     *src;
   558     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   559     gboolean        ret = FALSE;
   560     GstElement     *ap = NULL;
   561     GstElement     *vp = NULL;
   562 
   563 
   564     g_return_val_if_fail(priv->pipe != NULL, FALSE);
   565     g_return_val_if_fail(priv->ready == FALSE, FALSE);
   566 
   567     src = _create_source(self, uri);
   568     if (src == NULL)
   569         return FALSE;
   570 
   571     priv->src = gst_bin_get_by_name(GST_BIN(src), "src");
   572 
   573     gst_bin_add(GST_BIN(priv->pipe), src);
   574 
   575     ap = gst_bin_get_by_name(GST_BIN(priv->pipe), "abin");
   576     vp = gst_bin_get_by_name(GST_BIN(priv->pipe), "vbin");
   577 
   578     if ((vp == NULL) || (ap == NULL)) {
   579         g_warning("Fail to get output bin");
   580         goto error;
   581     }
   582 
   583     pad_src = gst_element_get_pad(src, "src_audio");
   584     pad_sink = gst_element_get_compatible_pad(ap,
   585                                               pad_src,
   586                                               gst_pad_get_caps(pad_src));
   587 
   588     if ((pad_sink == NULL) || (pad_src == NULL))
   589         goto error;
   590 
   591     GstPadLinkReturn lret = gst_pad_link(pad_src, pad_sink);
   592     if (lret != GST_PAD_LINK_OK)
   593         goto error;
   594 
   595     gst_object_unref(pad_src);
   596     gst_object_unref(pad_sink);
   597 
   598     pad_src = gst_element_get_pad(src, "src_video");
   599     pad_sink = gst_element_get_compatible_pad(vp,
   600                                               pad_src,
   601                                               gst_pad_get_caps(pad_src));
   602 
   603     if ((pad_src == NULL) || (pad_sink == NULL))
   604         goto error;
   605 
   606     if (gst_pad_link(pad_src, pad_sink) != GST_PAD_LINK_OK) {
   607         g_warning("invalid source. video");
   608         goto error;
   609     }
   610 
   611     priv->sources++;
   612     ret = TRUE;
   613   error:
   614 
   615     if ((src != NULL) && (ret == FALSE)) {
   616         gst_bin_remove(GST_BIN(priv->pipe), src);
   617         gst_object_unref(src);
   618     }
   619 
   620     if (ap != NULL)
   621         gst_object_unref(ap);
   622 
   623     if (vp != NULL)
   624         gst_object_unref(vp);
   625 
   626     if (pad_src != NULL)
   627         gst_object_unref(pad_src);
   628 
   629     if (pad_sink != NULL)
   630         gst_object_unref(pad_sink);
   631 
   632     return ret;
   633 }
   634 
   635 
   636 
   637 void
   638 g_mencoder_remove_uri(GMencoder * self, const gchar * uri)
   639 {
   640     // GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE (self);
   641     // TODO: remove src
   642 }
   643 
   644 void
   645 g_mencoder_play_stream(GMencoder * self)
   646 {
   647     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   648     g_return_if_fail(priv->ready == FALSE);
   649     priv->ready = TRUE;
   650     gst_element_set_state(priv->pipe, GST_STATE_PLAYING);
   651     if (priv->tick_id != 0) {
   652         g_source_remove (priv->tick_id);
   653     }
   654     priv->tick_id = g_timeout_add(500, _tick_cb, self);
   655 
   656     if (priv->timeout_id != 0) {
   657         g_source_remove (priv->timeout_id);
   658     }
   659     //priv->timeout_id = g_timeout_add(GMENCODER_TIMEOUT, _process_timeout_cb, self);
   660 }
   661 
   662 void
   663 g_mencoder_pause_stream(GMencoder * self)
   664 {
   665     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   666     g_return_if_fail(priv->ready == TRUE);
   667     gst_element_set_state(priv->pipe, GST_STATE_PAUSED);
   668 }
   669 
   670 void
   671 g_mencoder_close_stream(GMencoder * self)
   672 {
   673 
   674     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   675     if (priv->tick_id != 0) {
   676         g_source_remove(priv->tick_id);
   677         priv->tick_id = 0;
   678     }
   679 
   680     if (priv->timeout_id != 0) {
   681         g_source_remove (priv->timeout_id);
   682 	priv->timeout_id = 0;
   683     }
   684 
   685     if (priv->pipe != NULL) {
   686         // TODO: fixe pipeline dispose
   687         //gst_element_set_state (priv->pipe, GST_STATE_NULL);
   688         // g_debug ("SETING STATE TO NULL: OK");
   689         // gst_element_set_state (priv->pipe, GST_STATE_NULL);
   690         //gst_object_unref (priv->pipe);
   691         //gst_object_unref(priv->src);
   692         priv->src = NULL;
   693         priv->pipe = NULL;
   694         priv->abin = NULL;
   695         priv->vbin = NULL;
   696         priv->sink = NULL;
   697     }
   698     priv->ready = FALSE;
   699 }
   700 
   701 static GstElement *
   702 _create_pipeline(GMencoder * self,
   703                  const gchar * video_encode,
   704                  const gchar * mux_name,
   705                  gchar ** video_encode_prop,
   706                  gdouble video_fps,
   707                  gdouble video_rate,
   708                  guint video_width,
   709                  guint video_height,
   710                  const gchar * audio_encode,
   711                  gchar ** audio_encode_prop, guint audio_rate,
   712 		         gboolean deinterlace)
   713 {
   714     GstBus         *bus = NULL;
   715     GstElement     *pipe = NULL;
   716     GstElement     *sink = NULL;
   717     GstElement     *mux = NULL;
   718     GstElement     *abin = NULL;
   719     GstElement     *vbin = NULL;
   720     GstElement     *queue = NULL;
   721     GstPad         *aux_pad = NULL;
   722     GstPad         *mux_pad = NULL;
   723 
   724     pipe = gst_pipeline_new("pipe");
   725 
   726     mux =
   727         gst_element_factory_make((mux_name ? mux_name : "ffmux_mpeg"),
   728                                  "mux");
   729     if (mux == NULL)
   730         goto error;
   731 
   732     queue = gst_element_factory_make("queue", "queueu_sink");
   733 
   734 
   735     sink = gst_element_factory_make("fakesink", "sink");
   736     g_object_set (G_OBJECT (sink), "signal-handoffs", TRUE, NULL);
   737     g_signal_connect (G_OBJECT (sink),
   738                       "handoff",
   739                       G_CALLBACK (_buffer_arrive_cb),
   740                       self);
   741 
   742     abin = _create_audio_bin(audio_encode, audio_encode_prop, audio_rate);
   743     if (abin == NULL)
   744         goto error;
   745 
   746     vbin =
   747         _create_video_bin(video_encode, video_encode_prop, video_fps,
   748                           video_rate, video_width, video_height, deinterlace);
   749     if (vbin == NULL)
   750         goto error;
   751 
   752     // Finish Pipe
   753     gst_bin_add_many(GST_BIN(pipe), abin, vbin, mux, queue, sink, NULL);
   754 
   755 
   756     // Link bins with mux
   757     aux_pad = gst_element_get_pad(abin, "src");
   758     mux_pad =
   759         gst_element_get_compatible_pad(mux, aux_pad,
   760                                        GST_PAD_CAPS(aux_pad));
   761     if (mux_pad == NULL) {
   762         g_warning("Mux element no have audio PAD");
   763         goto error;
   764     }
   765     GstPadLinkReturn ret = gst_pad_link(aux_pad, mux_pad);
   766     if (ret != GST_PAD_LINK_OK) {
   767         g_warning("Fail link audio and mux: %d", ret);
   768         goto error;
   769 
   770     }
   771     gst_object_unref(aux_pad);
   772     gst_object_unref(mux_pad);
   773 
   774     aux_pad = gst_element_get_pad(vbin, "src");
   775     mux_pad =
   776         gst_element_get_compatible_pad(mux, aux_pad,
   777                                        GST_PAD_CAPS(aux_pad));
   778     if (mux_pad == NULL) {
   779         g_warning("Mux element no have video PAD");
   780         goto error;
   781     }
   782     ret = gst_pad_link(aux_pad, mux_pad);
   783     if (ret != GST_PAD_LINK_OK) {
   784         g_warning("Fail link video and mux: %d", ret);
   785         goto error;
   786     }
   787     gst_object_unref(aux_pad);
   788     gst_object_unref(mux_pad);
   789     aux_pad = NULL;
   790     mux_pad = NULL;
   791 
   792     // Link mux with sink
   793     gst_element_link_many(mux, queue, sink, NULL);
   794 
   795     bus = gst_pipeline_get_bus(GST_PIPELINE(pipe));
   796     gst_bus_add_watch(bus, _pipeline_bus_cb, self);
   797     gst_object_unref(bus);
   798     return pipe;
   799 
   800   error:
   801     g_warning("Invalid uri");
   802 
   803     if (pipe != NULL) {
   804         gst_object_unref(pipe);
   805     }
   806 
   807 
   808     if (mux != NULL) {
   809         gst_object_unref(mux);
   810     }
   811 
   812     if (mux_pad != NULL) {
   813         gst_object_unref(mux_pad);
   814     }
   815 
   816     if (aux_pad != NULL) {
   817         gst_object_unref(mux_pad);
   818     }
   819 
   820     if (sink != NULL) {
   821         gst_object_unref(sink);
   822     }
   823 
   824     if (abin != NULL) {
   825         gst_object_unref(abin);
   826     }
   827 
   828     if (vbin != NULL) {
   829         gst_object_unref(vbin);
   830     }
   831 
   832     return FALSE;
   833 }
   834 
   835 
   836 static void
   837 _close_output(GMencoder * self)
   838 {
   839 }
   840 
   841 static GstElement *
   842 _create_v4l_source (GMencoder *self, const gchar * uri)
   843 {
   844     gchar **info;
   845     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   846 
   847 
   848     info = g_strsplit (uri+6, ":", 3);
   849     if (g_strv_length (info) != 3) {
   850         return NULL;
   851     }
   852 
   853     priv->v4lsrc = gst_element_factory_make ("v4l2src", "src");
   854     g_debug ("channel %s, norm %s, frequ %s", info[0], info[1], info[2]);
   855     g_object_set (G_OBJECT (priv->v4lsrc),
   856                   "channel", info[0],
   857                   "norm", info[1],
   858                   "frequency", atoi (info[2]),
   859                   NULL);
   860 
   861     return priv->v4lsrc;
   862 }
   863 
   864 static GstElement *
   865 _create_source(GMencoder *self, const gchar * uri)
   866 {
   867 
   868     GstElement     *bsrc = NULL;
   869     GstElement     *src = NULL;
   870     GstElement     *aqueue = NULL;
   871     GstElement     *vqueue = NULL;
   872     GstElement     *decode = NULL;
   873     GstPad         *src_pad = NULL;
   874 
   875 
   876     bsrc = gst_bin_new(NULL);
   877 
   878     // src = gst_element_factory_make ("gnomevfssrc", "src");
   879     // g_object_set (G_OBJECT (src), "location", uri, NULL);
   880     if (strncmp (uri, "v4l://", 6) == 0) {
   881         g_debug ("V4L");
   882         src = _create_v4l_source (self, uri);
   883     }
   884     else {
   885         src = gst_element_make_from_uri(GST_URI_SRC, uri, "src");
   886     }
   887 
   888     if (src == NULL)
   889         goto error;
   890 
   891 
   892     if (g_getenv ("USE_DECODEBIN1"))
   893       decode = gst_element_factory_make("decodebin", "decode");
   894     else
   895       decode = gst_element_factory_make("decodebin2", "decode");
   896     if (decode == NULL)
   897         goto error;
   898 
   899     aqueue = gst_element_factory_make("queue", "aqueue");
   900     if (aqueue == NULL)
   901         goto error;
   902 
   903     vqueue = gst_element_factory_make("queue", "vqueue");
   904     if (vqueue == NULL)
   905         goto error;
   906 
   907     gst_bin_add_many(GST_BIN(bsrc), src, decode, aqueue, vqueue,
   908                      NULL);
   909     gst_element_link (src, decode);
   910 
   911     g_signal_connect(G_OBJECT(decode),
   912                      "new-decoded-pad",
   913                      G_CALLBACK(_decodebin_new_pad_cb), bsrc);
   914 
   915     g_signal_connect(G_OBJECT(decode),
   916                      "unknown-type",
   917                      G_CALLBACK(_decodebin_unknown_type_cb), pipe);
   918 
   919     src_pad = gst_element_get_pad(aqueue, "src");
   920     gst_element_add_pad(bsrc, gst_ghost_pad_new("src_audio", src_pad));
   921     gst_object_unref(src_pad);
   922 
   923     src_pad = gst_element_get_pad(vqueue, "src");
   924     gst_element_add_pad(bsrc, gst_ghost_pad_new("src_video", src_pad));
   925     gst_object_unref(src_pad);
   926 
   927     return bsrc;
   928 
   929   error:
   930     g_debug ("Fail to create source element");
   931     if (src != NULL) {
   932         gst_object_unref(src);
   933     }
   934 
   935     if (decode != NULL) {
   936         gst_object_unref(decode);
   937     }
   938 
   939     if (aqueue != NULL) {
   940         gst_object_unref(aqueue);
   941     }
   942 
   943     if (vqueue != NULL) {
   944         gst_object_unref(vqueue);
   945     }
   946 
   947     return NULL;
   948 }
   949 
   950 static gboolean
   951 _open_output(GMencoder * self, const gchar * uri)
   952 {
   953     gchar         **i;
   954     GnomeVFSResult result;
   955     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
   956 
   957     i = g_strsplit(uri, "://", 0);
   958     if (strcmp(i[0], "fd") == 0) {
   959         result = gnome_vfs_open_fd (&priv->handle, atoi(i[1]));
   960     } else {
   961         if (g_file_test (i[1], G_FILE_TEST_EXISTS) == FALSE) {
   962             result = gnome_vfs_create (&priv->handle, uri, GNOME_VFS_OPEN_WRITE, FALSE,
   963                               GNOME_VFS_PERM_USER_WRITE | GNOME_VFS_PERM_USER_READ | GNOME_VFS_PERM_GROUP_READ);
   964         } else {
   965             result = gnome_vfs_open (&priv->handle, uri,
   966                                      GNOME_VFS_OPEN_WRITE | GNOME_VFS_OPEN_TRUNCATE);
   967         }
   968     }
   969 
   970     g_strfreev(i);
   971     return (result == GNOME_VFS_OK);
   972 }
   973 
   974 static          gboolean
   975 _pipeline_bus_cb(GstBus * bus, GstMessage * msg, gpointer user_data)
   976 {
   977     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
   978 
   979     switch (GST_MESSAGE_TYPE(msg)) {
   980 
   981     case GST_MESSAGE_STATE_CHANGED:
   982         {
   983             GstState        oldstate;
   984             GstState        newstate;
   985             GstState        pendingstate;
   986 
   987 
   988             gst_message_parse_state_changed(msg, &oldstate,
   989                                             &newstate, &pendingstate);
   990 
   991             if (pendingstate != GST_STATE_VOID_PENDING)
   992                 break;
   993 
   994             if ((oldstate == GST_STATE_READY)
   995                 && (newstate == GST_STATE_PAUSED)) {
   996                 if (priv->ready)
   997                     g_signal_emit(user_data, g_mencoder_signals[PAUSED],
   998                                   0);
   999             } else if ((oldstate == GST_STATE_PAUSED)
  1000                        && (newstate == GST_STATE_PLAYING)) {
  1001                 g_signal_emit(user_data, g_mencoder_signals[PLAYING], 0);
  1002             } else if ((oldstate == GST_STATE_READY) &&
  1003                        (newstate == GST_STATE_NULL)) {
  1004                 g_signal_emit(user_data, g_mencoder_signals[STOPED], 0);
  1005             }
  1006             break;
  1007         }
  1008 
  1009     case GST_MESSAGE_ERROR:
  1010         {
  1011             GError         *error;
  1012             gchar          *debug;
  1013             gchar          *err_str;
  1014 
  1015             if (priv->tick_id != 0) {
  1016                 g_source_remove(priv->tick_id);
  1017                 priv->tick_id = 0;
  1018             }
  1019 
  1020             gst_message_parse_error(msg, &error, &debug);
  1021             err_str = g_strdup_printf("Error [%d] %s (%s)", error->code,
  1022                                       error->message, debug);
  1023             priv->ready = FALSE;
  1024             g_signal_emit(user_data, g_mencoder_signals[ERROR], 0,
  1025                           err_str);
  1026             g_free(err_str);
  1027             g_clear_error(&error);
  1028             g_free(debug);
  1029             break;
  1030         }
  1031 
  1032     case GST_MESSAGE_EOS:
  1033         priv->ready = FALSE;
  1034 #ifdef USE_MANUAL_SINK
  1035         _flush_queue (G_MENCODER (user_data));
  1036 #endif
  1037         g_signal_emit(user_data, g_mencoder_signals[EOS], 0);
  1038         break;
  1039 
  1040     case GST_MESSAGE_DURATION:
  1041         {
  1042             GstFormat       format;
  1043             gint64          duration;
  1044             gst_message_parse_duration(msg, &format, &duration);
  1045             if (format == GST_FORMAT_BYTES)
  1046                 priv->duration = duration;
  1047             break;
  1048         }
  1049     default:
  1050         {
  1051             break;
  1052         }
  1053     }
  1054     return TRUE;
  1055 }
  1056 
  1057 
  1058 
  1059 static void
  1060 _decodebin_new_pad_cb(GstElement * object,
  1061                       GstPad * pad, gboolean flag, gpointer user_data)
  1062 {
  1063     GstCaps        *caps;
  1064     gchar          *str_caps = NULL;
  1065     GstElement     *sink_element;
  1066     GstPad         *sink_pad;
  1067 
  1068     caps = gst_pad_get_caps(pad);
  1069     str_caps = gst_caps_to_string(caps);
  1070     if (strstr(str_caps, "audio") != NULL) {
  1071         sink_element = gst_bin_get_by_name(GST_BIN(user_data), "aqueue");
  1072     } else if (strstr(str_caps, "video") != NULL) {
  1073         sink_element = gst_bin_get_by_name(GST_BIN(user_data), "vqueue");
  1074     } else {
  1075         g_warning("invalid caps %s", str_caps);
  1076     }
  1077 
  1078     sink_pad = gst_element_get_pad(sink_element, "sink");
  1079     gst_pad_link(pad, sink_pad);
  1080 
  1081     gst_object_unref(sink_element);
  1082     gst_object_unref(sink_pad);
  1083     g_free(str_caps);
  1084     gst_caps_unref(caps);
  1085 }
  1086 
  1087 static void
  1088 _decodebin_unknown_type_cb(GstElement * object,
  1089                            GstPad * pad, GstCaps * caps,
  1090                            gpointer user_data)
  1091 {
  1092     g_warning("Unknown Type");
  1093     // priv->ready = FALSE;
  1094 }
  1095 
  1096 static          gboolean
  1097 _tick_cb(gpointer user_data)
  1098 {
  1099     GstFormat       format = GST_FORMAT_BYTES;
  1100     gint64          cur = 0;
  1101 
  1102     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
  1103 
  1104     if (priv->duration == 0) {
  1105         gint64          d = 0;
  1106         if (gst_element_query_duration(priv->src, &format, &d))
  1107             priv->duration = d;
  1108     }
  1109 
  1110     if (priv->duration != 0) {
  1111         gst_element_query_position(priv->src, &format, &cur);
  1112         g_print("PROGRESS:%lli\n", (99 * cur) / priv->duration);
  1113     }
  1114 
  1115     return TRUE;
  1116 }
  1117 
  1118 static gboolean 
  1119 _process_timeout_cb (gpointer user_data)
  1120 {
  1121     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
  1122 
  1123     g_signal_emit(user_data, g_mencoder_signals[ERROR], 0, "timeout");
  1124     priv->timeout_id = 0;
  1125     return FALSE;
  1126 }
  1127 
  1128 
  1129 #ifdef USE_MANUAL_SINK
  1130 static gboolean
  1131 _send_buffer (GnomeVFSHandle *handle, gpointer buff, gint size)
  1132 {
  1133     gchar *msg;
  1134     GByteArray *b_send;
  1135     GnomeVFSResult result;
  1136     GnomeVFSFileSize bytes_written;
  1137 
  1138     b_send = g_byte_array_new ();
  1139     msg = g_strdup_printf ("%x\r\n", size);
  1140     b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
  1141     g_free (msg);
  1142 
  1143     b_send = g_byte_array_append (b_send, buff, size);
  1144 
  1145     msg = g_strdup ("\r\n");
  1146     b_send = g_byte_array_append (b_send, (const guint8*) msg, strlen (msg) * sizeof (gchar));
  1147     g_free (msg);
  1148 
  1149     result = gnome_vfs_write (handle, b_send->data, b_send->len, &bytes_written);
  1150     g_byte_array_free (b_send, TRUE);
  1151 
  1152     return (result == GNOME_VFS_OK);
  1153 }
  1154 
  1155 static void
  1156 _flush_queue (GMencoder *self)
  1157 {
  1158     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(self);
  1159 
  1160     if (priv->send_chunked) {
  1161         GnomeVFSFileSize bytes_written;
  1162         gchar *end_msg;
  1163         end_msg = g_strdup ("0\r\n\r\n");
  1164         gnome_vfs_write (priv->handle,
  1165                          (const guint8*) end_msg,
  1166                          strlen(end_msg) * sizeof(gchar),
  1167                          &bytes_written);
  1168         g_free (end_msg);
  1169     }
  1170 }
  1171 
  1172 static void
  1173 _buffer_arrive_cb (GstElement* object,
  1174                    GstBuffer* buff,
  1175                    GstPad* pad,
  1176                    gpointer user_data)
  1177 {
  1178     GMencoderPrivate *priv = G_MENCODER_GET_PRIVATE(user_data);
  1179 
  1180     if (priv->timeout_id != 0) {
  1181 	g_source_remove (priv->timeout_id);
  1182 	priv->timeout_id = 0;
  1183     }
  1184 
  1185     if (priv->send_chunked) {
  1186         if (_send_buffer (priv->handle, GST_BUFFER_DATA (buff), GST_BUFFER_SIZE (buff)) == FALSE)
  1187             goto error;
  1188     } else {
  1189         GnomeVFSResult result;
  1190         GnomeVFSFileSize bytes_written;
  1191 
  1192         result = gnome_vfs_write (priv->handle,
  1193                                   GST_BUFFER_DATA (buff),
  1194                                   GST_BUFFER_SIZE (buff),
  1195                                   &bytes_written);
  1196 
  1197         if (result != GNOME_VFS_OK)
  1198             goto error;
  1199     }
  1200 
  1201     return;
  1202 
  1203 error:
  1204     if (priv->tick_id != 0) {
  1205         g_source_remove(priv->tick_id);
  1206         priv->tick_id = 0;
  1207     }
  1208     g_signal_emit(user_data, g_mencoder_signals[ERROR], 0, "Fail to write on socket");
  1209     gst_element_set_state (priv->pipe, GST_STATE_PAUSED);
  1210 }
  1211 
  1212 #endif