1 /* multipart muxer plugin for GStreamer
2 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
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-multipartmux
23 * MultipartMux uses the #GstCaps of the sink pad as the Content-type field for
24 * incoming buffers when muxing them to a multipart stream. Most of the time
25 * multipart streams are sequential JPEG frames.
28 * <title>Sample pipelines</title>
30 * gst-launch videotestsrc ! video/x-raw-yuv, framerate='(fraction)'5/1 ! jpegenc ! multipartmux ! filesink location=/tmp/test.multipart
31 * ]| a pipeline to mux 5 JPEG frames per second into a multipart stream
40 #include "multipartmux.h"
42 GST_DEBUG_CATEGORY_STATIC (gst_multipart_mux_debug);
43 #define GST_CAT_DEFAULT gst_multipart_mux_debug
45 #define DEFAULT_BOUNDARY "ThisRandomString"
54 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
57 GST_STATIC_CAPS ("multipart/x-mixed-replace")
60 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
63 GST_STATIC_CAPS_ANY /* we can take anything, really */
72 /* convert from gst structure names to mime types. Add more when needed. */
73 static const MimeTypeMap mimetypes[] = {
74 {"audio/x-mulaw", "audio/basic"},
78 static void gst_multipart_mux_finalize (GObject * object);
80 static gboolean gst_multipart_mux_handle_src_event (GstPad * pad,
81 GstObject * parent, GstEvent * event);
82 static GstPad *gst_multipart_mux_request_new_pad (GstElement * element,
83 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
84 static GstStateChangeReturn gst_multipart_mux_change_state (GstElement *
85 element, GstStateChange transition);
87 static GstFlowReturn gst_multipart_mux_collected (GstCollectPads2 * pads,
88 GstMultipartMux * mux);
90 static void gst_multipart_mux_set_property (GObject * object, guint prop_id,
91 const GValue * value, GParamSpec * pspec);
92 static void gst_multipart_mux_get_property (GObject * object, guint prop_id,
93 GValue * value, GParamSpec * pspec);
95 #define gst_multipart_mux_parent_class parent_class
96 G_DEFINE_TYPE (GstMultipartMux, gst_multipart_mux, GST_TYPE_ELEMENT);
99 gst_multipart_mux_class_init (GstMultipartMuxClass * klass)
101 GObjectClass *gobject_class;
102 GstElementClass *gstelement_class;
105 gobject_class = (GObjectClass *) klass;
106 gstelement_class = (GstElementClass *) klass;
108 parent_class = g_type_class_peek_parent (klass);
110 gobject_class->finalize = gst_multipart_mux_finalize;
111 gobject_class->get_property = gst_multipart_mux_get_property;
112 gobject_class->set_property = gst_multipart_mux_set_property;
114 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BOUNDARY,
115 g_param_spec_string ("boundary", "Boundary", "Boundary string",
116 DEFAULT_BOUNDARY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
118 gstelement_class->request_new_pad = gst_multipart_mux_request_new_pad;
119 gstelement_class->change_state = gst_multipart_mux_change_state;
121 gst_element_class_add_pad_template (gstelement_class,
122 gst_static_pad_template_get (&src_factory));
123 gst_element_class_add_pad_template (gstelement_class,
124 gst_static_pad_template_get (&sink_factory));
126 gst_element_class_set_details_simple (gstelement_class, "Multipart muxer",
127 "Codec/Muxer", "mux multipart streams", "Wim Taymans <wim@fluendo.com>");
129 /* populate mime types */
130 klass->mimetypes = g_hash_table_new (g_str_hash, g_str_equal);
131 for (i = 0; mimetypes[i].key; i++) {
132 g_hash_table_insert (klass->mimetypes, (gpointer) mimetypes[i].key,
133 (gpointer) mimetypes[i].val);
138 gst_multipart_mux_init (GstMultipartMux * multipart_mux)
140 GstElementClass *klass = GST_ELEMENT_GET_CLASS (multipart_mux);
142 multipart_mux->srcpad =
143 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
145 gst_pad_set_event_function (multipart_mux->srcpad,
146 gst_multipart_mux_handle_src_event);
147 gst_element_add_pad (GST_ELEMENT (multipart_mux), multipart_mux->srcpad);
149 multipart_mux->boundary = g_strdup (DEFAULT_BOUNDARY);
151 multipart_mux->collect = gst_collect_pads2_new ();
152 gst_collect_pads2_set_function (multipart_mux->collect,
153 (GstCollectPads2Function) GST_DEBUG_FUNCPTR (gst_multipart_mux_collected),
158 gst_multipart_mux_finalize (GObject * object)
160 GstMultipartMux *multipart_mux;
162 multipart_mux = GST_MULTIPART_MUX (object);
164 g_free (multipart_mux->boundary);
166 if (multipart_mux->collect)
167 gst_object_unref (multipart_mux->collect);
169 G_OBJECT_CLASS (parent_class)->finalize (object);
173 gst_multipart_mux_request_new_pad (GstElement * element,
174 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
176 GstMultipartMux *multipart_mux;
178 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
181 if (templ != gst_element_class_get_pad_template (klass, "sink_%u"))
184 multipart_mux = GST_MULTIPART_MUX (element);
186 /* create new pad with the name */
187 name = g_strdup_printf ("sink_%u", multipart_mux->numpads);
188 newpad = gst_pad_new_from_template (templ, name);
191 /* construct our own wrapper data structure for the pad to
192 * keep track of its status */
194 GstMultipartPadData *multipartpad;
196 multipartpad = (GstMultipartPadData *)
197 gst_collect_pads2_add_pad (multipart_mux->collect, newpad,
198 sizeof (GstMultipartPadData));
200 /* save a pointer to our data in the pad */
201 multipartpad->pad = newpad;
202 gst_pad_set_element_private (newpad, multipartpad);
203 multipart_mux->numpads++;
206 /* add the pad to the element */
207 gst_element_add_pad (element, newpad);
214 g_warning ("multipart_mux: this is not our template!");
221 gst_multipart_mux_handle_src_event (GstPad * pad, GstObject * parent,
226 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
230 /* disable seeking for now */
236 return gst_pad_event_default (pad, parent, event);
240 gst_multipart_mux_get_mime (GstMultipartMux * mux, GstStructure * s)
242 GstMultipartMuxClass *klass;
249 klass = GST_MULTIPART_MUX_GET_CLASS (mux);
251 name = gst_structure_get_name (s);
253 /* use hashtable to convert to mime type */
254 mime = g_hash_table_lookup (klass->mimetypes, name);
256 if (!strcmp (name, "audio/x-adpcm"))
257 gst_structure_get_int (s, "bitrate", &bitrate);
261 mime = "audio/G726-16";
264 mime = "audio/G726-24";
267 mime = "audio/G726-32";
270 mime = "audio/G726-40";
273 /* no mime type mapping, use name */
278 /* RFC2046 requires audio/basic to be mulaw 8000Hz mono */
279 if (g_ascii_strcasecmp (mime, "audio/basic") == 0) {
280 if (gst_structure_get_int (s, "rate", &rate) &&
281 gst_structure_get_int (s, "channels", &channels)) {
282 if (rate != 8000 || channels != 1) {
293 * Given two pads, compare the buffers queued on it and return 0 if they have
294 * an equal priority, 1 if the new pad is better, -1 if the old pad is better
297 gst_multipart_mux_compare_pads (GstMultipartMux * multipart_mux,
298 GstMultipartPadData * old, GstMultipartPadData * new)
300 guint64 oldtime, newtime;
302 /* if the old pad doesn't contain anything or is even NULL, return
303 * the new pad as best candidate and vice versa */
304 if (old == NULL || old->buffer == NULL)
306 if (new == NULL || new->buffer == NULL)
309 /* no timestamp on old buffer, it must go first */
310 oldtime = old->timestamp;
311 if (oldtime == GST_CLOCK_TIME_NONE)
314 /* no timestamp on new buffer, it must go first */
315 newtime = new->timestamp;
316 if (newtime == GST_CLOCK_TIME_NONE)
319 /* old buffer has higher timestamp, new one should go first */
320 if (newtime < oldtime)
322 /* new buffer has higher timestamp, old one should go first */
323 else if (newtime > oldtime)
326 /* same priority if all of the above failed */
330 /* make sure a buffer is queued on all pads, returns a pointer to an multipartpad
331 * that holds the best buffer or NULL when no pad was usable */
332 static GstMultipartPadData *
333 gst_multipart_mux_queue_pads (GstMultipartMux * mux)
336 GstMultipartPadData *bestpad = NULL;
338 g_return_val_if_fail (GST_IS_MULTIPART_MUX (mux), NULL);
340 /* try to make sure we have a buffer from each usable pad first */
341 walk = mux->collect->data;
343 GstCollectData2 *data = (GstCollectData2 *) walk->data;
344 GstMultipartPadData *pad = (GstMultipartPadData *) data;
346 walk = g_slist_next (walk);
348 /* try to get a new buffer for this pad if needed and possible */
349 if (pad->buffer == NULL) {
350 GstBuffer *buf = NULL;
352 buf = gst_collect_pads2_pop (mux->collect, data);
354 /* Store timestamp with segment_start and preroll */
355 if (buf && GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
357 gst_segment_to_running_time (&data->segment, GST_FORMAT_TIME,
358 GST_BUFFER_TIMESTAMP (buf));
360 pad->timestamp = GST_CLOCK_TIME_NONE;
366 /* we should have a buffer now, see if it is the best stream to
368 if (pad->buffer != NULL) {
369 if (gst_multipart_mux_compare_pads (mux, bestpad, pad) > 0) {
380 * 1) find a pad to pull on, this is done by pulling on all pads and
381 * looking at the buffers to decide which one should be muxed first.
382 * 2) create a new buffer for the header
383 * 3) push both buffers on best pad, go to 1
386 gst_multipart_mux_collected (GstCollectPads2 * pads, GstMultipartMux * mux)
388 GstMultipartPadData *best;
389 GstFlowReturn ret = GST_FLOW_OK;
390 gchar *header = NULL;
392 GstBuffer *headerbuf = NULL;
393 GstBuffer *footerbuf = NULL;
394 GstBuffer *databuf = NULL;
395 GstStructure *structure = NULL;
398 static GstAllocationParams params = { 0, 0, 0, 1, };
400 GST_DEBUG_OBJECT (mux, "all pads are collected");
402 /* queue buffers on all pads; find a buffer with the lowest timestamp */
403 best = gst_multipart_mux_queue_pads (mux);
407 else if (!best->buffer)
410 /* If not negotiated yet set caps on src pad */
411 if (!mux->negotiated) {
414 newcaps = gst_caps_new_simple ("multipart/x-mixed-replace",
415 "boundary", G_TYPE_STRING, mux->boundary, NULL);
417 if (!gst_pad_set_caps (mux->srcpad, newcaps)) {
418 gst_caps_unref (newcaps);
422 gst_caps_unref (newcaps);
423 mux->negotiated = TRUE;
426 /* see if we need to push a segment */
427 if (mux->need_segment) {
431 if (best->timestamp != -1)
432 time = best->timestamp;
436 /* for the segment, we take the first timestamp we see, we don't know the
437 * length and the position is 0 */
438 gst_segment_init (&segment, GST_FORMAT_TIME);
439 segment.start = time;
441 gst_pad_push_event (mux->srcpad, gst_event_new_segment (&segment));
443 mux->need_segment = FALSE;
446 caps = gst_pad_get_current_caps (best->pad);
450 structure = gst_caps_get_structure (caps, 0);
452 gst_caps_unref (caps);
456 /* get the mime type for the structure */
457 mime = gst_multipart_mux_get_mime (mux, structure);
458 gst_caps_unref (caps);
460 header = g_strdup_printf ("--%s\r\nContent-Type: %s\r\n"
461 "Content-Length: %" G_GSIZE_FORMAT "\r\n\r\n",
462 mux->boundary, mime, gst_buffer_get_size (best->buffer));
463 headerlen = strlen (header);
465 headerbuf = gst_buffer_new_allocate (NULL, headerlen, ¶ms);
466 gst_buffer_fill (headerbuf, 0, header, headerlen);
469 /* the header has the same timestamp as the data buffer (which we will push
470 * below) and has a duration of 0 */
471 GST_BUFFER_TIMESTAMP (headerbuf) = best->timestamp;
472 GST_BUFFER_DURATION (headerbuf) = 0;
473 GST_BUFFER_OFFSET (headerbuf) = mux->offset;
474 mux->offset += headerlen;
475 GST_BUFFER_OFFSET_END (headerbuf) = mux->offset;
477 GST_DEBUG_OBJECT (mux, "pushing %" G_GSIZE_FORMAT " bytes header buffer",
479 ret = gst_pad_push (mux->srcpad, headerbuf);
480 if (ret != GST_FLOW_OK)
481 /* push always takes ownership of the buffer, even after an error, so we
482 * don't need to unref headerbuf here. */
485 /* take best->buffer, we don't need to unref it later as we will push it
487 databuf = gst_buffer_make_writable (best->buffer);
490 /* we need to updated the timestamp to match the running_time */
491 GST_BUFFER_TIMESTAMP (databuf) = best->timestamp;
492 GST_BUFFER_OFFSET (databuf) = mux->offset;
493 mux->offset += gst_buffer_get_size (databuf);
494 GST_BUFFER_OFFSET_END (databuf) = mux->offset;
495 GST_BUFFER_FLAG_SET (databuf, GST_BUFFER_FLAG_DELTA_UNIT);
497 GST_DEBUG_OBJECT (mux, "pushing %" G_GSIZE_FORMAT " bytes data buffer",
498 gst_buffer_get_size (databuf));
499 ret = gst_pad_push (mux->srcpad, databuf);
500 if (ret != GST_FLOW_OK)
501 /* push always takes ownership of the buffer, even after an error, so we
502 * don't need to unref headerbuf here. */
505 footerbuf = gst_buffer_new_allocate (NULL, 2, ¶ms);
506 gst_buffer_fill (footerbuf, 0, "\r\n", 2);
508 /* the footer has the same timestamp as the data buffer and has a
510 GST_BUFFER_TIMESTAMP (footerbuf) = best->timestamp;
511 GST_BUFFER_DURATION (footerbuf) = 0;
512 GST_BUFFER_OFFSET (footerbuf) = mux->offset;
514 GST_BUFFER_OFFSET_END (footerbuf) = mux->offset;
515 GST_BUFFER_FLAG_SET (footerbuf, GST_BUFFER_FLAG_DELTA_UNIT);
517 GST_DEBUG_OBJECT (mux, "pushing 2 bytes footer buffer");
518 ret = gst_pad_push (mux->srcpad, footerbuf);
521 if (best && best->buffer) {
522 gst_buffer_unref (best->buffer);
530 /* There is a best but no buffer, this is not quite right.. */
531 GST_ELEMENT_ERROR (mux, STREAM, FAILED, (NULL), ("internal muxing error"));
532 ret = GST_FLOW_ERROR;
537 GST_DEBUG_OBJECT (mux, "Pushing EOS");
538 gst_pad_push_event (mux->srcpad, gst_event_new_eos ());
544 GST_WARNING_OBJECT (mux, "failed to set caps");
545 GST_ELEMENT_ERROR (mux, CORE, NEGOTIATION, (NULL), (NULL));
546 ret = GST_FLOW_NOT_NEGOTIATED;
551 GST_WARNING_OBJECT (mux, "no caps on the incoming buffer %p", best->buffer);
552 GST_ELEMENT_ERROR (mux, CORE, NEGOTIATION, (NULL), (NULL));
553 ret = GST_FLOW_NOT_NEGOTIATED;
559 gst_multipart_mux_get_property (GObject * object,
560 guint prop_id, GValue * value, GParamSpec * pspec)
562 GstMultipartMux *mux;
564 mux = GST_MULTIPART_MUX (object);
568 g_value_set_string (value, mux->boundary);
571 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
577 gst_multipart_mux_set_property (GObject * object,
578 guint prop_id, const GValue * value, GParamSpec * pspec)
580 GstMultipartMux *mux;
582 mux = GST_MULTIPART_MUX (object);
586 g_free (mux->boundary);
587 mux->boundary = g_strdup (g_value_get_string (value));
590 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
595 static GstStateChangeReturn
596 gst_multipart_mux_change_state (GstElement * element, GstStateChange transition)
598 GstMultipartMux *multipart_mux;
599 GstStateChangeReturn ret;
601 multipart_mux = GST_MULTIPART_MUX (element);
603 switch (transition) {
604 case GST_STATE_CHANGE_READY_TO_PAUSED:
605 multipart_mux->offset = 0;
606 multipart_mux->negotiated = FALSE;
607 multipart_mux->need_segment = TRUE;
608 GST_DEBUG_OBJECT (multipart_mux, "starting collect pads");
609 gst_collect_pads2_start (multipart_mux->collect);
611 case GST_STATE_CHANGE_PAUSED_TO_READY:
612 GST_DEBUG_OBJECT (multipart_mux, "stopping collect pads");
613 gst_collect_pads2_stop (multipart_mux->collect);
619 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
620 if (ret == GST_STATE_CHANGE_FAILURE)
623 switch (transition) {
632 gst_multipart_mux_plugin_init (GstPlugin * plugin)
634 GST_DEBUG_CATEGORY_INIT (gst_multipart_mux_debug, "multipartmux", 0,
637 return gst_element_register (plugin, "multipartmux", GST_RANK_NONE,
638 GST_TYPE_MULTIPART_MUX);