gst-gmyth/concatmux/gstconcatmux.c
author melunko
Thu May 24 21:41:18 2007 +0100 (2007-05-24)
branchtrunk
changeset 710 320440ee97df
parent 611 aa1f309ec686
child 751 3cf3c6019e3b
permissions -rw-r--r--
[svn r716] debian/control changed to set packages to session user/library
     1 /* concat muxer plugin for GStreamer
     2  * Copyright (C) 2004 Renato Filhps <renato.filho@indt.org.br>
     3  *
     4  * This library is free software; you can redistribute it and/or
     5  * modify it under the terms of the GNU Library General Public
     6  * License as published by the Free Software Foundation; either
     7  * version 2 of the License, or (at your option) any later version.
     8  *
     9  * This library is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    12  * Library General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU Library General Public
    15  * License along with this library; if not, write to the
    16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    17  * Boston, MA 02111-1307, USA.
    18  */
    19 
    20 /**
    21  * SECTION:element-concatmux
    22  * @short_description: Concat that takes one or several digital streams
    23  * and muxes them to a single stream.
    24  *
    25  * <refsect2>
    26  * <title>Sample pipelines</title>
    27  * <para>
    28  * Here is a simple pipeline to concat 2 files into a another file:
    29  * <programlisting>
    30  * gst-launch concatmux name=m ! filesink location=output.txt  filesrc location=file_a.txt !  m. filesrc location=file_b.txt ! m.
    31  * </programlisting>
    32  * </para>
    33  * </refsect2>
    34  */
    35 
    36 #ifdef HAVE_CONFIG_H
    37 #include "config.h"
    38 #endif
    39 
    40 #include <gst/gst.h>
    41 #include <gst/base/gstcollectpads.h>
    42 
    43 #include <string.h>
    44 
    45 GST_DEBUG_CATEGORY_STATIC (gst_concat_mux_debug);
    46 #define GST_CAT_DEFAULT gst_concat_mux_debug
    47 
    48 #define GST_TYPE_CONCAT_MUX (gst_concat_mux_get_type())
    49 #define GST_CONCAT_MUX(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_CONCAT_MUX, GstConcatMux))
    50 #define GST_CONCAT_MUX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_CONCAT_MUX, GstConcatMux))
    51 #define GST_IS_CONCAT_MUX(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_CONCAT_MUX))
    52 #define GST_IS_CONCAT_MUX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_CONCAT_MUX))
    53 
    54 typedef struct _GstConcatMux GstConcatMux;
    55 typedef struct _GstConcatMuxClass GstConcatMuxClass;
    56 
    57 /**
    58  * GstConcatMux:
    59  *
    60  * The opaque #GstConcatMux structure.
    61  */
    62 struct _GstConcatMux
    63 {
    64   GstElement element;
    65 
    66   /* Caps */
    67   GstCaps *sink_caps;
    68 
    69   /* pad */
    70   GstPad *srcpad;
    71   GstPad *sinkpad;
    72 
    73   /* sinkpads */
    74   GSList *sinks;
    75   gint numpads;
    76 
    77   /* offset in stream */
    78   guint64 offset;
    79   guint64 timeoffset;
    80   guint64 start_time;
    81 
    82   gboolean negotiated;
    83   gboolean resync;
    84   gboolean done;
    85 };
    86 
    87 struct _GstConcatMuxClass
    88 {
    89   GstElementClass parent_class;
    90 };
    91 
    92 /* elementfactory information */
    93 static const GstElementDetails gst_concat_mux_details =
    94 GST_ELEMENT_DETAILS ("Concat muxer",
    95     "Codec/Muxer",
    96     "mux concat streams",
    97     "Renato Filho <renato.filho@indt.org>");
    98 
    99 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
   100     GST_PAD_SRC,
   101     GST_PAD_ALWAYS,
   102     GST_STATIC_CAPS_ANY
   103     );
   104 
   105 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
   106     GST_PAD_SINK,
   107     GST_PAD_REQUEST,
   108     GST_STATIC_CAPS_ANY         
   109     );
   110 
   111 static void gst_concat_mux_base_init (gpointer g_class);
   112 static void gst_concat_mux_class_init (GstConcatMuxClass * klass);
   113 static void gst_concat_mux_init (GstConcatMux * concat_mux);
   114 
   115 static void gst_concat_mux_finalize (GObject * object);
   116 
   117 static gboolean gst_concat_mux_handle_src_event (GstPad * pad,
   118     GstEvent * event);
   119 static gboolean gst_concat_mux_handle_sink_event (GstPad * pad,
   120     GstEvent * event);
   121 
   122 static GstPad *gst_concat_mux_request_new_pad (GstElement * element,
   123     GstPadTemplate * templ, const gchar * name);
   124 static GstStateChangeReturn gst_concat_mux_change_state (GstElement *
   125     element, GstStateChange transition);
   126 
   127 static GstFlowReturn gst_concat_mux_chain (GstPad * pad,  GstBuffer * buf);
   128 static void gst_concat_mux_clear (GstConcatMux *mux);
   129 
   130 
   131 static GstElementClass *parent_class = NULL;
   132 
   133 GType
   134 gst_concat_mux_get_type (void)
   135 {
   136   static GType concat_mux_type = 0;
   137 
   138   if (!concat_mux_type) {
   139     static const GTypeInfo concat_mux_info = {
   140       sizeof (GstConcatMuxClass),
   141       gst_concat_mux_base_init,
   142       NULL,
   143       (GClassInitFunc) gst_concat_mux_class_init,
   144       NULL,
   145       NULL,
   146       sizeof (GstConcatMux),
   147       0,
   148       (GInstanceInitFunc) gst_concat_mux_init,
   149     };
   150 
   151     concat_mux_type =
   152         g_type_register_static (GST_TYPE_ELEMENT, "GstConcatMux",
   153         &concat_mux_info, 0);
   154   }
   155   return concat_mux_type;
   156 }
   157 
   158 static void
   159 gst_concat_mux_base_init (gpointer g_class)
   160 {
   161   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
   162 
   163   gst_element_class_add_pad_template (element_class,
   164       gst_static_pad_template_get (&src_factory));
   165   gst_element_class_add_pad_template (element_class,
   166       gst_static_pad_template_get (&sink_factory));
   167 
   168   gst_element_class_set_details (element_class, &gst_concat_mux_details);
   169 }
   170 
   171 static void
   172 gst_concat_mux_class_init (GstConcatMuxClass * klass)
   173 {
   174   GObjectClass *gobject_class;
   175   GstElementClass *gstelement_class;
   176 
   177   gobject_class = (GObjectClass *) klass;
   178   gstelement_class = (GstElementClass *) klass;
   179 
   180   parent_class = g_type_class_peek_parent (klass);
   181 
   182   gobject_class->finalize = gst_concat_mux_finalize;
   183 
   184   gstelement_class->request_new_pad = gst_concat_mux_request_new_pad;
   185   gstelement_class->change_state = gst_concat_mux_change_state;
   186 }
   187 
   188 static void
   189 gst_concat_mux_init (GstConcatMux * concat_mux)
   190 {
   191   GstElementClass *klass = GST_ELEMENT_GET_CLASS (concat_mux);
   192 
   193   concat_mux->srcpad =
   194       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
   195           "src"), "src");
   196   gst_pad_set_event_function (concat_mux->srcpad,
   197       gst_concat_mux_handle_src_event);
   198   gst_element_add_pad (GST_ELEMENT (concat_mux), concat_mux->srcpad);
   199 }
   200 
   201 static void
   202 gst_concat_mux_finalize (GObject * object)
   203 {
   204   GstConcatMux *concat_mux;
   205 
   206   concat_mux = GST_CONCAT_MUX (object);
   207   gst_concat_mux_clear (GST_CONCAT_MUX (object));
   208 
   209   G_OBJECT_CLASS (parent_class)->finalize (object);
   210 }
   211 
   212 static void
   213 gst_concat_mux_free_pad (gpointer data,
   214                             gpointer user_data)
   215 {
   216    GMutex *mux;
   217 
   218    mux = gst_pad_get_element_private (GST_PAD (data));
   219    g_mutex_unlock (mux);
   220    g_mutex_free (mux);
   221    gst_object_unref (GST_OBJECT (data)); 
   222 }
   223 
   224 static void
   225 gst_concat_mux_clear (GstConcatMux *mux)
   226 {
   227   mux->resync = TRUE;
   228   mux->timeoffset = 0;
   229   mux->offset = 0;
   230   mux->negotiated = FALSE;
   231   mux->done = TRUE;
   232   if (mux->sinks != NULL) {
   233     g_slist_foreach (mux->sinks, gst_concat_mux_free_pad, mux);
   234     g_slist_free (mux->sinks);
   235     mux->sinks = NULL;
   236   }
   237 }
   238 
   239 
   240 static GstPadLinkReturn
   241 gst_concat_mux_sinkconnect (GstPad * pad, GstPad * peer)
   242 {
   243   gchar *pad_name = NULL;
   244   GstConcatMux *concat_mux;
   245 
   246   concat_mux = GST_CONCAT_MUX (gst_pad_get_parent (pad));
   247 
   248   if (concat_mux->sink_caps != NULL) {
   249       GstCaps *peer_caps = gst_pad_get_caps (peer);
   250       GstCaps *intersect;
   251 
   252       intersect =  gst_caps_intersect (concat_mux->sink_caps, peer_caps);
   253       if (intersect == NULL) {
   254           gst_caps_unref (peer_caps);
   255           return GST_PAD_LINK_NOFORMAT;
   256       }
   257       gst_caps_unref (peer_caps);
   258       gst_caps_unref (intersect);
   259   } else {
   260       concat_mux->sink_caps = gst_pad_get_caps (pad);
   261   }
   262 
   263   pad_name = gst_pad_get_name (pad);
   264 
   265   GST_DEBUG_OBJECT (concat_mux, "sinkconnect triggered on %s", pad_name);
   266 
   267   g_free (pad_name);
   268 
   269   gst_object_unref (concat_mux);
   270 
   271   return GST_PAD_LINK_OK;
   272 }
   273 
   274 static GstPad *
   275 gst_concat_mux_request_new_pad (GstElement * element,
   276     GstPadTemplate * templ, const gchar * req_name)
   277 {
   278   GstConcatMux *concat_mux;
   279   GstPad *newpad;
   280   GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
   281   GMutex *mutex;
   282 
   283   g_return_val_if_fail (templ != NULL, NULL);
   284 
   285   if (templ->direction != GST_PAD_SINK) {
   286     g_warning ("concat_mux: request pad that is not a SINK pad\n");
   287     return NULL;
   288   }
   289 
   290   g_return_val_if_fail (GST_IS_CONCAT_MUX (element), NULL);
   291 
   292   concat_mux = GST_CONCAT_MUX (element);
   293 
   294   if (templ == gst_element_class_get_pad_template (klass, "sink_%d")) {
   295     gchar *name;
   296 
   297     /* create new pad with the name */
   298     name = g_strdup_printf ("sink_%02d", concat_mux->numpads);
   299     g_debug ("NEw pad %s", name);
   300     newpad = gst_pad_new_from_template (templ, name);
   301     g_free (name);
   302     concat_mux->sinks = g_slist_append (concat_mux->sinks, newpad);
   303     g_debug ("New sink %p / %d", newpad, g_slist_length (concat_mux->sinks));
   304     concat_mux->numpads++;
   305   } else {
   306     g_warning ("concat_mux: this is not our template!\n");
   307     return NULL;
   308   }
   309 
   310   mutex = g_mutex_new ();
   311 
   312   if (concat_mux->sinkpad == NULL) {
   313       concat_mux->sinkpad = newpad;
   314   }
   315   else {
   316       g_mutex_lock (mutex);
   317   }
   318 
   319   gst_pad_set_element_private (newpad, mutex);
   320   /* setup some pad functions */
   321   gst_pad_set_link_function (newpad, gst_concat_mux_sinkconnect);
   322   gst_pad_set_event_function (newpad, gst_concat_mux_handle_sink_event);
   323   gst_pad_set_chain_function (newpad, gst_concat_mux_chain);
   324 
   325   /* add the pad to the element */
   326   gst_element_add_pad (element, newpad);
   327 
   328   return newpad;
   329 }
   330 
   331 /* handle events */
   332 static gboolean
   333 gst_concat_mux_handle_src_event (GstPad * pad, GstEvent * event)
   334 {
   335   GstConcatMux *concat_mux;
   336   GstEventType type;
   337 
   338   concat_mux = GST_CONCAT_MUX (gst_pad_get_parent (pad));
   339 
   340   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
   341 
   342   switch (type) {
   343     case GST_EVENT_SEEK:
   344       /* disable seeking for now */
   345       return FALSE;
   346     default:
   347       break;
   348   }
   349 
   350   gst_object_unref (concat_mux);
   351 
   352   return gst_pad_event_default (pad, event);
   353 }
   354 
   355 /* handle events */
   356 static gboolean
   357 gst_concat_mux_handle_sink_event (GstPad * pad, GstEvent * event)
   358 {
   359   GstConcatMux *mux;
   360   GstEventType type;
   361 
   362   mux = GST_CONCAT_MUX (gst_pad_get_parent (pad));
   363 
   364   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
   365 
   366   switch (type) {
   367     case GST_EVENT_EOS:
   368     {
   369         mux->resync = TRUE;
   370         g_debug ("sink EOS %p / %d", pad, g_slist_length (mux->sinks));
   371         /* mark pad eos */
   372         mux->sinks  = g_slist_remove (mux->sinks, pad);
   373         g_debug ("sink len %d", g_slist_length (mux->sinks));
   374         if (g_slist_length (mux->sinks) != 0) {
   375            GMutex *mutex;
   376            mux->sinkpad = mux->sinks->data;
   377            mutex = (GMutex *) gst_pad_get_element_private (mux->sinkpad);
   378            g_mutex_unlock (mutex);
   379            g_debug ("sink pad %p", mux->sinkpad);
   380            return TRUE;
   381         }
   382 
   383         g_debug ("sink list is empty");
   384     }
   385     default:
   386       break;
   387   }
   388 
   389   gst_object_unref (mux);
   390 
   391   return gst_pad_event_default (pad, event);
   392 }
   393 
   394 static GstFlowReturn
   395 gst_concat_mux_chain (GstPad * pad,  GstBuffer * buf)
   396 {
   397   GstConcatMux *mux = (GstConcatMux *) GST_PAD_PARENT (pad);
   398   GstBuffer *databuf = NULL;
   399   GstFlowReturn ret = GST_FLOW_OK;
   400   GMutex *mutex;
   401 
   402 
   403   mutex = (GMutex*) gst_pad_get_element_private (pad);
   404 
   405   g_mutex_lock (mutex);
   406   if (mux->done) {
   407       g_debug ("DONE pad %p", pad);
   408       g_mutex_unlock (mutex);
   409       return GST_FLOW_OK;
   410   }
   411 
   412   databuf = gst_buffer_make_metadata_writable (buf);
   413 
   414   if (!mux->negotiated) {
   415       /*
   416     GstCaps *newcaps;
   417     newcaps = gst_pad_get_caps (mux->sinkpad);
   418 
   419     g_debug ("CAPS: %s",gst_caps_to_string (newcaps));
   420 
   421     if (!gst_pad_set_caps (mux->srcpad, newcaps))
   422       goto nego_error;
   423     */
   424     mux->negotiated = TRUE;            
   425   } 
   426 
   427   /*
   428   g_debug ("Running [%s]\n"
   429         "\tTOFFSET    [%"G_GUINT64_FORMAT"]\n"
   430         "\tB_TSTAMP   [%"G_GUINT64_FORMAT"]\n"
   431         "\tB_DURATION [%"G_GUINT64_FORMAT"]\n"
   432         "\tOFFSET     [%"G_GUINT64_FORMAT"]\n"
   433         "\tB_OFFSET   [%"G_GUINT64_FORMAT"]",
   434         gst_element_get_name (mux),
   435         mux->timeoffset, 
   436         GST_BUFFER_TIMESTAMP (databuf),
   437         GST_BUFFER_DURATION (databuf),
   438         mux->offset,
   439         GST_BUFFER_OFFSET (databuf));
   440   */        
   441 
   442 
   443   if (mux->resync) {
   444     g_debug ("RESYNC [%s]", gst_element_get_name (mux));
   445     mux->timeoffset += GST_BUFFER_TIMESTAMP (databuf);
   446     GST_BUFFER_TIMESTAMP (databuf) = mux->timeoffset;
   447     mux->timeoffset += GST_BUFFER_DURATION (databuf);
   448 
   449     mux->offset += GST_BUFFER_OFFSET (databuf);
   450     GST_BUFFER_OFFSET (databuf) = mux->offset;
   451     mux->offset += GST_BUFFER_SIZE (databuf);
   452     mux->resync = FALSE;
   453   } else {
   454 
   455     GST_BUFFER_TIMESTAMP (databuf) = mux->timeoffset;
   456     mux->timeoffset += GST_BUFFER_DURATION (databuf);
   457 
   458     GST_BUFFER_OFFSET (databuf) = mux->offset;
   459     mux->offset += GST_BUFFER_SIZE (databuf);
   460   }
   461 
   462   gst_buffer_set_caps (databuf, GST_PAD_CAPS (pad));
   463   ret = gst_pad_push (mux->srcpad, databuf);
   464 
   465   //gst_buffer_unref (buf);
   466 
   467   g_mutex_unlock (mutex);
   468   return ret;
   469 /*
   470 nego_error:
   471   {
   472     GST_WARNING_OBJECT (mux, "failed to set caps");
   473     GST_ELEMENT_ERROR (mux, CORE, NEGOTIATION, (NULL), (NULL));
   474     return GST_FLOW_NOT_NEGOTIATED;
   475   }
   476   */
   477   /*
   478 no_caps:
   479   {
   480     GST_WARNING_OBJECT (mux, "no caps on the incoming buffer %p", best->buffer);
   481     GST_ELEMENT_ERROR (mux, CORE, NEGOTIATION, (NULL), (NULL));
   482     ret = GST_FLOW_NOT_NEGOTIATED;
   483     goto beach;
   484   }
   485   */
   486 }
   487 
   488 static GstStateChangeReturn
   489 gst_concat_mux_change_state (GstElement * element, GstStateChange transition)
   490 {
   491   GstConcatMux *concat_mux;
   492   GstStateChangeReturn ret;
   493 
   494   concat_mux = GST_CONCAT_MUX (element);
   495 
   496   switch (transition) {
   497     case GST_STATE_CHANGE_READY_TO_PAUSED:
   498       concat_mux->done = FALSE;
   499       concat_mux->resync = TRUE;
   500       GST_DEBUG_OBJECT (concat_mux, "starting collect pads");
   501       break;
   502     case GST_STATE_CHANGE_PAUSED_TO_READY:
   503       GST_DEBUG_OBJECT (concat_mux, "stopping collect pads");
   504       gst_concat_mux_clear (concat_mux);
   505       break;
   506     default:
   507       break;
   508   }
   509 
   510   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
   511   if (ret == GST_STATE_CHANGE_FAILURE)
   512     return ret;
   513 
   514   switch (transition) {
   515     default:
   516       break;
   517   }
   518 
   519   return ret;
   520 }
   521 
   522 gboolean
   523 gst_concat_mux_plugin_init (GstPlugin * plugin)
   524 {
   525 #ifdef ENABLE_NLS
   526   setlocale (LC_ALL, "");
   527   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
   528 #endif /* ENABLE_NLS */
   529 
   530   GST_DEBUG_CATEGORY_INIT (gst_concat_mux_debug, "concatmux", 0,
   531       "concat muxer");
   532       
   533   return gst_element_register (plugin, "concatmux", GST_RANK_NONE,
   534       GST_TYPE_CONCAT_MUX);
   535 }
   536 
   537 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
   538     GST_VERSION_MINOR,
   539     "concatmux",
   540     "Concat streamers",
   541      gst_concat_mux_plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
   542