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_%d",
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_base_init (gpointer g_class);
79 static void gst_multipart_mux_class_init (GstMultipartMuxClass * klass);
80 static void gst_multipart_mux_init (GstMultipartMux * multipart_mux);
82 static void gst_multipart_mux_finalize (GObject * object);
84 static gboolean gst_multipart_mux_handle_src_event (GstPad * pad,
86 static GstPad *gst_multipart_mux_request_new_pad (GstElement * element,
87 GstPadTemplate * templ, const gchar * name);
88 static GstStateChangeReturn gst_multipart_mux_change_state (GstElement *
89 element, GstStateChange transition);
91 static GstFlowReturn gst_multipart_mux_collected (GstCollectPads * pads,
92 GstMultipartMux * mux);
94 static void gst_multipart_mux_set_property (GObject * object, guint prop_id,
95 const GValue * value, GParamSpec * pspec);
96 static void gst_multipart_mux_get_property (GObject * object, guint prop_id,
97 GValue * value, GParamSpec * pspec);
99 static GstElementClass *parent_class = NULL;
102 gst_multipart_mux_get_type (void)
104 static GType multipart_mux_type = 0;
106 if (!multipart_mux_type) {
107 static const GTypeInfo multipart_mux_info = {
108 sizeof (GstMultipartMuxClass),
109 gst_multipart_mux_base_init,
111 (GClassInitFunc) gst_multipart_mux_class_init,
114 sizeof (GstMultipartMux),
116 (GInstanceInitFunc) gst_multipart_mux_init,
120 g_type_register_static (GST_TYPE_ELEMENT, "GstMultipartMux",
121 &multipart_mux_info, 0);
123 return multipart_mux_type;
127 gst_multipart_mux_base_init (gpointer g_class)
129 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
131 gst_element_class_add_pad_template (element_class,
132 gst_static_pad_template_get (&src_factory));
133 gst_element_class_add_pad_template (element_class,
134 gst_static_pad_template_get (&sink_factory));
136 gst_element_class_set_details_simple (element_class, "Multipart muxer",
137 "Codec/Muxer", "mux multipart streams", "Wim Taymans <wim@fluendo.com>");
141 gst_multipart_mux_class_init (GstMultipartMuxClass * klass)
143 GObjectClass *gobject_class;
144 GstElementClass *gstelement_class;
147 gobject_class = (GObjectClass *) klass;
148 gstelement_class = (GstElementClass *) klass;
150 parent_class = g_type_class_peek_parent (klass);
152 gobject_class->finalize = gst_multipart_mux_finalize;
153 gobject_class->get_property = gst_multipart_mux_get_property;
154 gobject_class->set_property = gst_multipart_mux_set_property;
156 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BOUNDARY,
157 g_param_spec_string ("boundary", "Boundary", "Boundary string",
158 DEFAULT_BOUNDARY, G_PARAM_READWRITE));
160 gstelement_class->request_new_pad = gst_multipart_mux_request_new_pad;
161 gstelement_class->change_state = gst_multipart_mux_change_state;
163 /* populate mime types */
164 klass->mimetypes = g_hash_table_new (g_str_hash, g_str_equal);
165 for (i = 0; mimetypes[i].key; i++) {
166 g_hash_table_insert (klass->mimetypes, (gpointer) mimetypes[i].key,
167 (gpointer) mimetypes[i].val);
172 gst_multipart_mux_init (GstMultipartMux * multipart_mux)
174 GstElementClass *klass = GST_ELEMENT_GET_CLASS (multipart_mux);
176 multipart_mux->srcpad =
177 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
179 gst_pad_set_event_function (multipart_mux->srcpad,
180 gst_multipart_mux_handle_src_event);
181 gst_element_add_pad (GST_ELEMENT (multipart_mux), multipart_mux->srcpad);
183 multipart_mux->boundary = g_strdup (DEFAULT_BOUNDARY);
185 multipart_mux->collect = gst_collect_pads_new ();
186 gst_collect_pads_set_function (multipart_mux->collect,
187 (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_multipart_mux_collected),
192 gst_multipart_mux_finalize (GObject * object)
194 GstMultipartMux *multipart_mux;
196 multipart_mux = GST_MULTIPART_MUX (object);
198 g_free (multipart_mux->boundary);
200 if (multipart_mux->collect)
201 gst_object_unref (multipart_mux->collect);
203 G_OBJECT_CLASS (parent_class)->finalize (object);
207 gst_multipart_mux_request_new_pad (GstElement * element,
208 GstPadTemplate * templ, const gchar * req_name)
210 GstMultipartMux *multipart_mux;
212 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
215 if (templ != gst_element_class_get_pad_template (klass, "sink_%d"))
218 multipart_mux = GST_MULTIPART_MUX (element);
220 /* create new pad with the name */
221 name = g_strdup_printf ("sink_%02d", multipart_mux->numpads);
222 newpad = gst_pad_new_from_template (templ, name);
225 /* construct our own wrapper data structure for the pad to
226 * keep track of its status */
228 GstMultipartPadData *multipartpad;
230 multipartpad = (GstMultipartPadData *)
231 gst_collect_pads_add_pad (multipart_mux->collect, newpad,
232 sizeof (GstMultipartPadData));
234 /* save a pointer to our data in the pad */
235 gst_pad_set_element_private (newpad, multipartpad);
236 multipart_mux->numpads++;
239 /* add the pad to the element */
240 gst_element_add_pad (element, newpad);
247 g_warning ("multipart_mux: this is not our template!");
254 gst_multipart_mux_handle_src_event (GstPad * pad, GstEvent * event)
256 GstMultipartMux *multipart_mux;
259 multipart_mux = GST_MULTIPART_MUX (gst_pad_get_parent (pad));
261 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
265 /* disable seeking for now */
271 gst_object_unref (multipart_mux);
273 return gst_pad_event_default (pad, event);
277 gst_multipart_mux_get_mime (GstMultipartMux * mux, GstStructure * s)
279 GstMultipartMuxClass *klass;
286 klass = GST_MULTIPART_MUX_GET_CLASS (mux);
288 name = gst_structure_get_name (s);
290 /* use hashtable to convert to mime type */
291 mime = g_hash_table_lookup (klass->mimetypes, name);
293 if (!strcmp (name, "audio/x-adpcm"))
294 gst_structure_get_int (s, "bitrate", &bitrate);
298 mime = "audio/G726-16";
301 mime = "audio/G726-24";
304 mime = "audio/G726-32";
307 mime = "audio/G726-40";
310 /* no mime type mapping, use name */
315 /* RFC2046 requires audio/basic to be mulaw 8000Hz mono */
316 if (g_ascii_strcasecmp (mime, "audio/basic") == 0) {
317 if (gst_structure_get_int (s, "rate", &rate) &&
318 gst_structure_get_int (s, "channels", &channels)) {
319 if (rate != 8000 || channels != 1) {
330 * Given two pads, compare the buffers queued on it and return 0 if they have
331 * an equal priority, 1 if the new pad is better, -1 if the old pad is better
334 gst_multipart_mux_compare_pads (GstMultipartMux * multipart_mux,
335 GstMultipartPadData * old, GstMultipartPadData * new)
337 guint64 oldtime, newtime;
339 /* if the old pad doesn't contain anything or is even NULL, return
340 * the new pad as best candidate and vice versa */
341 if (old == NULL || old->buffer == NULL)
343 if (new == NULL || new->buffer == NULL)
346 /* no timestamp on old buffer, it must go first */
347 oldtime = old->timestamp;
348 if (oldtime == GST_CLOCK_TIME_NONE)
351 /* no timestamp on new buffer, it must go first */
352 newtime = new->timestamp;
353 if (newtime == GST_CLOCK_TIME_NONE)
356 /* old buffer has higher timestamp, new one should go first */
357 if (newtime < oldtime)
359 /* new buffer has higher timestamp, old one should go first */
360 else if (newtime > oldtime)
363 /* same priority if all of the above failed */
367 /* make sure a buffer is queued on all pads, returns a pointer to an multipartpad
368 * that holds the best buffer or NULL when no pad was usable */
369 static GstMultipartPadData *
370 gst_multipart_mux_queue_pads (GstMultipartMux * mux)
373 GstMultipartPadData *bestpad = NULL;
375 g_return_val_if_fail (GST_IS_MULTIPART_MUX (mux), NULL);
377 /* try to make sure we have a buffer from each usable pad first */
378 walk = mux->collect->data;
380 GstCollectData *data = (GstCollectData *) walk->data;
381 GstMultipartPadData *pad = (GstMultipartPadData *) data;
383 walk = g_slist_next (walk);
385 /* try to get a new buffer for this pad if needed and possible */
386 if (pad->buffer == NULL) {
387 GstBuffer *buf = NULL;
389 buf = gst_collect_pads_pop (mux->collect, data);
391 /* Store timestamp with segment_start and preroll */
392 if (buf && GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
394 gst_segment_to_running_time (&data->segment, GST_FORMAT_TIME,
395 GST_BUFFER_TIMESTAMP (buf));
397 pad->timestamp = GST_CLOCK_TIME_NONE;
403 /* we should have a buffer now, see if it is the best stream to
405 if (pad->buffer != NULL) {
406 if (gst_multipart_mux_compare_pads (mux, bestpad, pad) > 0) {
417 * 1) find a pad to pull on, this is done by pulling on all pads and
418 * looking at the buffers to decide which one should be muxed first.
419 * 2) create a new buffer for the header
420 * 3) push both buffers on best pad, go to 1
423 gst_multipart_mux_collected (GstCollectPads * pads, GstMultipartMux * mux)
425 GstMultipartPadData *best;
426 GstFlowReturn ret = GST_FLOW_OK;
427 gchar *header = NULL;
429 GstBuffer *headerbuf = NULL;
430 GstBuffer *databuf = NULL;
431 GstStructure *structure = NULL;
434 GST_DEBUG_OBJECT (mux, "all pads are collected");
436 /* queue buffers on all pads; find a buffer with the lowest timestamp */
437 best = gst_multipart_mux_queue_pads (mux);
441 else if (!best->buffer)
444 /* If not negotiated yet set caps on src pad */
445 if (!mux->negotiated) {
448 newcaps = gst_caps_new_simple ("multipart/x-mixed-replace",
449 "boundary", G_TYPE_STRING, mux->boundary, NULL);
451 if (!gst_pad_set_caps (mux->srcpad, newcaps)) {
452 gst_caps_unref (newcaps);
456 gst_caps_unref (newcaps);
457 mux->negotiated = TRUE;
460 /* see if we need to push a segment */
461 if (mux->need_segment) {
465 if (best->timestamp != -1)
466 time = best->timestamp;
470 /* for the segment, we take the first timestamp we see, we don't know the
471 * length and the position is 0 */
472 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME,
475 gst_pad_push_event (mux->srcpad, event);
477 mux->need_segment = FALSE;
480 structure = gst_caps_get_structure (GST_BUFFER_CAPS (best->buffer), 0);
484 /* get the mime type for the structure */
485 mime = gst_multipart_mux_get_mime (mux, structure);
487 header = g_strdup_printf ("\r\n--%s\r\nContent-Type: %s\r\n"
488 "Content-Length: %u\r\n\r\n",
489 mux->boundary, mime, GST_BUFFER_SIZE (best->buffer));
490 headerlen = strlen (header);
492 ret = gst_pad_alloc_buffer_and_set_caps (mux->srcpad, GST_BUFFER_OFFSET_NONE,
493 headerlen, GST_PAD_CAPS (mux->srcpad), &headerbuf);
494 if (ret != GST_FLOW_OK)
497 memcpy (GST_BUFFER_DATA (headerbuf), header, headerlen);
500 /* the header has the same timestamp as the data buffer (which we will push
501 * below) and has a duration of 0 */
502 GST_BUFFER_TIMESTAMP (headerbuf) = best->timestamp;
503 GST_BUFFER_DURATION (headerbuf) = 0;
504 GST_BUFFER_OFFSET (headerbuf) = mux->offset;
505 mux->offset += headerlen;
506 GST_BUFFER_OFFSET_END (headerbuf) = mux->offset;
508 GST_DEBUG_OBJECT (mux, "pushing %" G_GSIZE_FORMAT " bytes header buffer",
510 ret = gst_pad_push (mux->srcpad, headerbuf);
511 if (ret != GST_FLOW_OK)
512 /* push always takes ownership of the buffer, even after an error, so we
513 * don't need to unref headerbuf here. */
516 /* take best->buffer, we don't need to unref it later as we will push it
518 databuf = gst_buffer_make_metadata_writable (best->buffer);
521 gst_buffer_set_caps (databuf, GST_PAD_CAPS (mux->srcpad));
522 /* we need to updated the timestamp to match the running_time */
523 GST_BUFFER_TIMESTAMP (databuf) = best->timestamp;
524 GST_BUFFER_OFFSET (databuf) = mux->offset;
525 mux->offset += GST_BUFFER_SIZE (databuf);
526 GST_BUFFER_OFFSET_END (databuf) = mux->offset;
527 GST_BUFFER_FLAG_SET (databuf, GST_BUFFER_FLAG_DELTA_UNIT);
529 GST_DEBUG_OBJECT (mux, "pushing %u bytes data buffer",
530 GST_BUFFER_SIZE (databuf));
531 ret = gst_pad_push (mux->srcpad, databuf);
534 if (best && best->buffer) {
535 gst_buffer_unref (best->buffer);
543 /* There is a best but no buffer, this is not quite right.. */
544 GST_ELEMENT_ERROR (mux, STREAM, FAILED, (NULL), ("internal muxing error"));
545 ret = GST_FLOW_ERROR;
550 GST_DEBUG_OBJECT (mux, "Pushing EOS");
551 gst_pad_push_event (mux->srcpad, gst_event_new_eos ());
552 ret = GST_FLOW_UNEXPECTED;
557 GST_WARNING_OBJECT (mux, "failed to set caps");
558 GST_ELEMENT_ERROR (mux, CORE, NEGOTIATION, (NULL), (NULL));
559 ret = GST_FLOW_NOT_NEGOTIATED;
564 GST_WARNING_OBJECT (mux, "no caps on the incoming buffer %p", best->buffer);
565 GST_ELEMENT_ERROR (mux, CORE, NEGOTIATION, (NULL), (NULL));
566 ret = GST_FLOW_NOT_NEGOTIATED;
571 GST_WARNING_OBJECT (mux,
572 "failed allocating a %" G_GSIZE_FORMAT " bytes buffer", headerlen);
579 gst_multipart_mux_get_property (GObject * object,
580 guint prop_id, GValue * value, GParamSpec * pspec)
582 GstMultipartMux *mux;
584 mux = GST_MULTIPART_MUX (object);
588 g_value_set_string (value, mux->boundary);
591 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
597 gst_multipart_mux_set_property (GObject * object,
598 guint prop_id, const GValue * value, GParamSpec * pspec)
600 GstMultipartMux *mux;
602 mux = GST_MULTIPART_MUX (object);
606 g_free (mux->boundary);
607 mux->boundary = g_strdup (g_value_get_string (value));
610 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
615 static GstStateChangeReturn
616 gst_multipart_mux_change_state (GstElement * element, GstStateChange transition)
618 GstMultipartMux *multipart_mux;
619 GstStateChangeReturn ret;
621 multipart_mux = GST_MULTIPART_MUX (element);
623 switch (transition) {
624 case GST_STATE_CHANGE_READY_TO_PAUSED:
625 multipart_mux->offset = 0;
626 multipart_mux->negotiated = FALSE;
627 multipart_mux->need_segment = TRUE;
628 GST_DEBUG_OBJECT (multipart_mux, "starting collect pads");
629 gst_collect_pads_start (multipart_mux->collect);
631 case GST_STATE_CHANGE_PAUSED_TO_READY:
632 GST_DEBUG_OBJECT (multipart_mux, "stopping collect pads");
633 gst_collect_pads_stop (multipart_mux->collect);
639 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
640 if (ret == GST_STATE_CHANGE_FAILURE)
643 switch (transition) {
652 gst_multipart_mux_plugin_init (GstPlugin * plugin)
654 GST_DEBUG_CATEGORY_INIT (gst_multipart_mux_debug, "multipartmux", 0,
657 return gst_element_register (plugin, "multipartmux", GST_RANK_NONE,
658 GST_TYPE_MULTIPART_MUX);