1 /* concat muxer plugin for GStreamer
2 * Copyright (C) 2004 Renato Filhps <renato.filho@indt.org.br>
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.
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.
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.
21 * SECTION:element-concatmux
22 * @short_description: Concat that takes one or several digital streams
23 * and muxes them to a single stream.
26 * <title>Sample pipelines</title>
28 * Here is a simple pipeline to concat 2 files into a another file:
30 * gst-launch concatmux name=m ! filesink location=output.txt filesrc location=file_a.txt ! m. filesrc location=file_b.txt ! m.
41 #include <gst/base/gstcollectpads.h>
45 GST_DEBUG_CATEGORY_STATIC(gst_concat_mux_debug);
46 #define GST_CAT_DEFAULT gst_concat_mux_debug
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))
54 typedef struct _GstConcatMux GstConcatMux;
55 typedef struct _GstConcatMuxClass GstConcatMuxClass;
60 * The opaque #GstConcatMux structure.
77 /* offset in stream */
87 struct _GstConcatMuxClass
89 GstElementClass parent_class;
92 /* elementfactory information */
93 static const GstElementDetails gst_concat_mux_details =
94 GST_ELEMENT_DETAILS("Concat muxer",
97 "Renato Filho <renato.filho@indt.org>");
99 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE("src",
102 GST_STATIC_CAPS_ANY);
104 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE("sink_%d",
107 GST_STATIC_CAPS_ANY);
109 static void gst_concat_mux_base_init(gpointer g_class);
110 static void gst_concat_mux_class_init(GstConcatMuxClass * klass);
111 static void gst_concat_mux_init(GstConcatMux * concat_mux);
113 static void gst_concat_mux_finalize(GObject * object);
115 static gboolean gst_concat_mux_handle_src_event(GstPad * pad,
117 static gboolean gst_concat_mux_handle_sink_event(GstPad * pad,
120 static GstPad *gst_concat_mux_request_new_pad(GstElement * element,
121 GstPadTemplate * templ,
123 static GstStateChangeReturn gst_concat_mux_change_state(GstElement * element,
127 static GstFlowReturn gst_concat_mux_chain(GstPad * pad, GstBuffer * buf);
128 static void gst_concat_mux_clear(GstConcatMux * mux);
131 static GstElementClass *parent_class = NULL;
134 gst_concat_mux_get_type(void)
136 static GType concat_mux_type = 0;
138 if (!concat_mux_type)
140 static const GTypeInfo concat_mux_info = {
141 sizeof(GstConcatMuxClass),
142 gst_concat_mux_base_init,
144 (GClassInitFunc) gst_concat_mux_class_init,
147 sizeof(GstConcatMux),
149 (GInstanceInitFunc) gst_concat_mux_init,
153 g_type_register_static(GST_TYPE_ELEMENT, "GstConcatMux",
154 &concat_mux_info, 0);
156 return concat_mux_type;
160 gst_concat_mux_base_init(gpointer g_class)
162 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
164 gst_element_class_add_pad_template(element_class,
165 gst_static_pad_template_get
167 gst_element_class_add_pad_template(element_class,
168 gst_static_pad_template_get
171 gst_element_class_set_details(element_class, &gst_concat_mux_details);
175 gst_concat_mux_class_init(GstConcatMuxClass * klass)
177 GObjectClass *gobject_class;
178 GstElementClass *gstelement_class;
180 gobject_class = (GObjectClass *) klass;
181 gstelement_class = (GstElementClass *) klass;
183 parent_class = g_type_class_peek_parent(klass);
185 gobject_class->finalize = gst_concat_mux_finalize;
187 gstelement_class->request_new_pad = gst_concat_mux_request_new_pad;
188 gstelement_class->change_state = gst_concat_mux_change_state;
192 gst_concat_mux_init(GstConcatMux * concat_mux)
194 GstElementClass *klass = GST_ELEMENT_GET_CLASS(concat_mux);
197 gst_pad_new_from_template(gst_element_class_get_pad_template(klass,
200 gst_pad_set_event_function(concat_mux->srcpad,
201 gst_concat_mux_handle_src_event);
202 gst_element_add_pad(GST_ELEMENT(concat_mux), concat_mux->srcpad);
206 gst_concat_mux_finalize(GObject * object)
208 GstConcatMux *concat_mux;
210 concat_mux = GST_CONCAT_MUX(object);
211 gst_concat_mux_clear(GST_CONCAT_MUX(object));
213 G_OBJECT_CLASS(parent_class)->finalize(object);
217 gst_concat_mux_free_pad(gpointer data, gpointer user_data)
221 mux = gst_pad_get_element_private(GST_PAD(data));
224 gst_object_unref(GST_OBJECT(data));
228 gst_concat_mux_clear(GstConcatMux * mux)
233 mux->negotiated = FALSE;
235 if (mux->sinks != NULL)
237 g_slist_foreach(mux->sinks, gst_concat_mux_free_pad, mux);
238 g_slist_free(mux->sinks);
244 static GstPadLinkReturn
245 gst_concat_mux_sinkconnect(GstPad * pad, GstPad * peer)
247 gchar *pad_name = NULL;
248 GstConcatMux *concat_mux;
250 concat_mux = GST_CONCAT_MUX(gst_pad_get_parent(pad));
252 if (concat_mux->sink_caps != NULL)
254 GstCaps *peer_caps = gst_pad_get_caps(peer);
257 intersect = gst_caps_intersect(concat_mux->sink_caps, peer_caps);
258 if (intersect == NULL)
260 gst_caps_unref(peer_caps);
261 return GST_PAD_LINK_NOFORMAT;
263 gst_caps_unref(peer_caps);
264 gst_caps_unref(intersect);
268 concat_mux->sink_caps = gst_pad_get_caps(pad);
271 pad_name = gst_pad_get_name(pad);
273 GST_DEBUG_OBJECT(concat_mux, "sinkconnect triggered on %s", pad_name);
277 gst_object_unref(concat_mux);
279 return GST_PAD_LINK_OK;
283 gst_concat_mux_request_new_pad(GstElement * element,
284 GstPadTemplate * templ, const gchar * req_name)
286 GstConcatMux *concat_mux;
288 GstElementClass *klass = GST_ELEMENT_GET_CLASS(element);
291 g_return_val_if_fail(templ != NULL, NULL);
293 if (templ->direction != GST_PAD_SINK)
295 g_warning("concat_mux: request pad that is not a SINK pad\n");
299 g_return_val_if_fail(GST_IS_CONCAT_MUX(element), NULL);
301 concat_mux = GST_CONCAT_MUX(element);
303 if (templ == gst_element_class_get_pad_template(klass, "sink_%d"))
307 /* create new pad with the name */
308 name = g_strdup_printf("sink_%02d", concat_mux->numpads);
309 g_debug("NEw pad %s", name);
310 newpad = gst_pad_new_from_template(templ, name);
312 concat_mux->sinks = g_slist_append(concat_mux->sinks, newpad);
313 g_debug("New sink %p / %d", newpad, g_slist_length(concat_mux->sinks));
314 concat_mux->numpads++;
318 g_warning("concat_mux: this is not our template!\n");
322 mutex = g_mutex_new();
324 if (concat_mux->sinkpad == NULL)
326 concat_mux->sinkpad = newpad;
333 gst_pad_set_element_private(newpad, mutex);
334 /* setup some pad functions */
335 gst_pad_set_link_function(newpad, gst_concat_mux_sinkconnect);
336 gst_pad_set_event_function(newpad, gst_concat_mux_handle_sink_event);
337 gst_pad_set_chain_function(newpad, gst_concat_mux_chain);
339 /* add the pad to the element */
340 gst_element_add_pad(element, newpad);
347 gst_concat_mux_handle_src_event(GstPad * pad, GstEvent * event)
349 GstConcatMux *concat_mux;
352 concat_mux = GST_CONCAT_MUX(gst_pad_get_parent(pad));
354 type = event ? GST_EVENT_TYPE(event) : GST_EVENT_UNKNOWN;
359 /* disable seeking for now */
365 gst_object_unref(concat_mux);
367 return gst_pad_event_default(pad, event);
372 gst_concat_mux_handle_sink_event(GstPad * pad, GstEvent * event)
377 mux = GST_CONCAT_MUX(gst_pad_get_parent(pad));
379 type = event ? GST_EVENT_TYPE(event) : GST_EVENT_UNKNOWN;
386 g_debug("sink EOS %p / %d", pad, g_slist_length(mux->sinks));
388 mux->sinks = g_slist_remove(mux->sinks, pad);
389 g_debug("sink len %d", g_slist_length(mux->sinks));
390 if (g_slist_length(mux->sinks) != 0)
393 mux->sinkpad = mux->sinks->data;
394 mutex = (GMutex *) gst_pad_get_element_private(mux->sinkpad);
395 g_mutex_unlock(mutex);
396 g_debug("sink pad %p", mux->sinkpad);
400 g_debug("sink list is empty");
406 gst_object_unref(mux);
408 return gst_pad_event_default(pad, event);
412 gst_concat_mux_chain(GstPad * pad, GstBuffer * buf)
414 GstConcatMux *mux = (GstConcatMux *) GST_PAD_PARENT(pad);
415 GstBuffer *databuf = NULL;
416 GstFlowReturn ret = GST_FLOW_OK;
420 mutex = (GMutex *) gst_pad_get_element_private(pad);
425 g_debug("DONE pad %p", pad);
426 g_mutex_unlock(mutex);
430 databuf = gst_buffer_make_metadata_writable(buf);
432 if (!mux->negotiated)
436 newcaps = gst_pad_get_caps (mux->sinkpad);
438 g_debug ("CAPS: %s",gst_caps_to_string (newcaps));
440 if (!gst_pad_set_caps (mux->srcpad, newcaps))
443 mux->negotiated = TRUE;
447 g_debug ("Running [%s]\n"
448 "\tTOFFSET [%"G_GUINT64_FORMAT"]\n"
449 "\tB_TSTAMP [%"G_GUINT64_FORMAT"]\n"
450 "\tB_DURATION [%"G_GUINT64_FORMAT"]\n"
451 "\tOFFSET [%"G_GUINT64_FORMAT"]\n"
452 "\tB_OFFSET [%"G_GUINT64_FORMAT"]",
453 gst_element_get_name (mux),
455 GST_BUFFER_TIMESTAMP (databuf),
456 GST_BUFFER_DURATION (databuf),
458 GST_BUFFER_OFFSET (databuf));
464 g_debug("RESYNC [%s]", gst_element_get_name(mux));
465 mux->timeoffset += GST_BUFFER_TIMESTAMP(databuf);
466 GST_BUFFER_TIMESTAMP(databuf) = mux->timeoffset;
467 mux->timeoffset += GST_BUFFER_DURATION(databuf);
469 mux->offset += GST_BUFFER_OFFSET(databuf);
470 GST_BUFFER_OFFSET(databuf) = mux->offset;
471 mux->offset += GST_BUFFER_SIZE(databuf);
477 GST_BUFFER_TIMESTAMP(databuf) = mux->timeoffset;
478 mux->timeoffset += GST_BUFFER_DURATION(databuf);
480 GST_BUFFER_OFFSET(databuf) = mux->offset;
481 mux->offset += GST_BUFFER_SIZE(databuf);
484 gst_buffer_set_caps(databuf, GST_PAD_CAPS(pad));
485 ret = gst_pad_push(mux->srcpad, databuf);
487 //gst_buffer_unref (buf);
489 g_mutex_unlock(mutex);
494 GST_WARNING_OBJECT (mux, "failed to set caps");
495 GST_ELEMENT_ERROR (mux, CORE, NEGOTIATION, (NULL), (NULL));
496 return GST_FLOW_NOT_NEGOTIATED;
502 GST_WARNING_OBJECT (mux, "no caps on the incoming buffer %p", best->buffer);
503 GST_ELEMENT_ERROR (mux, CORE, NEGOTIATION, (NULL), (NULL));
504 ret = GST_FLOW_NOT_NEGOTIATED;
510 static GstStateChangeReturn
511 gst_concat_mux_change_state(GstElement * element, GstStateChange transition)
513 GstConcatMux *concat_mux;
514 GstStateChangeReturn ret;
516 concat_mux = GST_CONCAT_MUX(element);
520 case GST_STATE_CHANGE_READY_TO_PAUSED:
521 concat_mux->done = FALSE;
522 concat_mux->resync = TRUE;
523 GST_DEBUG_OBJECT(concat_mux, "starting collect pads");
525 case GST_STATE_CHANGE_PAUSED_TO_READY:
526 GST_DEBUG_OBJECT(concat_mux, "stopping collect pads");
527 gst_concat_mux_clear(concat_mux);
533 ret = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
534 if (ret == GST_STATE_CHANGE_FAILURE)
547 gst_concat_mux_plugin_init(GstPlugin * plugin)
550 setlocale(LC_ALL, "");
551 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
552 #endif /* ENABLE_NLS */
554 GST_DEBUG_CATEGORY_INIT(gst_concat_mux_debug, "concatmux", 0,
557 return gst_element_register(plugin, "concatmux", GST_RANK_NONE,
558 GST_TYPE_CONCAT_MUX);
561 GST_PLUGIN_DEFINE(GST_VERSION_MAJOR,
565 gst_concat_mux_plugin_init, VERSION, GST_LICENSE,
566 GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)