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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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-1.0 videotestsrc ! video/x-raw, 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
36 /* FIXME: drop/merge tag events, or at least send them delayed after stream-start */
41 #include "multipartmux.h"
43 GST_DEBUG_CATEGORY_STATIC (gst_multipart_mux_debug);
44 #define GST_CAT_DEFAULT gst_multipart_mux_debug
46 #define DEFAULT_BOUNDARY "ThisRandomString"
55 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
58 GST_STATIC_CAPS ("multipart/x-mixed-replace")
61 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
64 GST_STATIC_CAPS_ANY /* we can take anything, really */
73 /* convert from gst structure names to mime types. Add more when needed. */
74 static const MimeTypeMap mimetypes[] = {
75 {"audio/x-mulaw", "audio/basic"},
79 static void gst_multipart_mux_finalize (GObject * object);
81 static gboolean gst_multipart_mux_handle_src_event (GstPad * pad,
82 GstObject * parent, GstEvent * event);
83 static GstPad *gst_multipart_mux_request_new_pad (GstElement * element,
84 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
85 static GstStateChangeReturn gst_multipart_mux_change_state (GstElement *
86 element, GstStateChange transition);
88 static gboolean gst_multipart_mux_sink_event (GstCollectPads * pads,
89 GstCollectData * pad, GstEvent * event, GstMultipartMux * mux);
90 static GstFlowReturn gst_multipart_mux_collected (GstCollectPads * pads,
91 GstMultipartMux * mux);
93 static void gst_multipart_mux_set_property (GObject * object, guint prop_id,
94 const GValue * value, GParamSpec * pspec);
95 static void gst_multipart_mux_get_property (GObject * object, guint prop_id,
96 GValue * value, GParamSpec * pspec);
98 #define gst_multipart_mux_parent_class parent_class
99 G_DEFINE_TYPE (GstMultipartMux, gst_multipart_mux, GST_TYPE_ELEMENT);
102 gst_multipart_mux_class_init (GstMultipartMuxClass * klass)
104 GObjectClass *gobject_class;
105 GstElementClass *gstelement_class;
108 gobject_class = (GObjectClass *) klass;
109 gstelement_class = (GstElementClass *) klass;
111 parent_class = g_type_class_peek_parent (klass);
113 gobject_class->finalize = gst_multipart_mux_finalize;
114 gobject_class->get_property = gst_multipart_mux_get_property;
115 gobject_class->set_property = gst_multipart_mux_set_property;
117 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BOUNDARY,
118 g_param_spec_string ("boundary", "Boundary", "Boundary string",
119 DEFAULT_BOUNDARY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
121 gstelement_class->request_new_pad = gst_multipart_mux_request_new_pad;
122 gstelement_class->change_state = gst_multipart_mux_change_state;
124 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
125 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
127 gst_element_class_set_static_metadata (gstelement_class, "Multipart muxer",
128 "Codec/Muxer", "mux multipart streams", "Wim Taymans <wim@fluendo.com>");
130 /* populate mime types */
131 klass->mimetypes = g_hash_table_new (g_str_hash, g_str_equal);
132 for (i = 0; mimetypes[i].key; i++) {
133 g_hash_table_insert (klass->mimetypes, (gpointer) mimetypes[i].key,
134 (gpointer) mimetypes[i].val);
139 gst_multipart_mux_init (GstMultipartMux * multipart_mux)
141 GstElementClass *klass = GST_ELEMENT_GET_CLASS (multipart_mux);
143 multipart_mux->srcpad =
144 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
146 gst_pad_set_event_function (multipart_mux->srcpad,
147 gst_multipart_mux_handle_src_event);
148 gst_element_add_pad (GST_ELEMENT (multipart_mux), multipart_mux->srcpad);
150 multipart_mux->boundary = g_strdup (DEFAULT_BOUNDARY);
152 multipart_mux->collect = gst_collect_pads_new ();
153 gst_collect_pads_set_event_function (multipart_mux->collect,
154 (GstCollectPadsEventFunction)
155 GST_DEBUG_FUNCPTR (gst_multipart_mux_sink_event), multipart_mux);
156 gst_collect_pads_set_function (multipart_mux->collect,
157 (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_multipart_mux_collected),
162 gst_multipart_mux_finalize (GObject * object)
164 GstMultipartMux *multipart_mux;
166 multipart_mux = GST_MULTIPART_MUX (object);
168 g_free (multipart_mux->boundary);
170 if (multipart_mux->collect)
171 gst_object_unref (multipart_mux->collect);
173 G_OBJECT_CLASS (parent_class)->finalize (object);
177 gst_multipart_mux_request_new_pad (GstElement * element,
178 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
180 GstMultipartMux *multipart_mux;
182 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
185 if (templ != gst_element_class_get_pad_template (klass, "sink_%u"))
188 multipart_mux = GST_MULTIPART_MUX (element);
190 /* create new pad with the name */
191 name = g_strdup_printf ("sink_%u", multipart_mux->numpads);
192 newpad = gst_pad_new_from_template (templ, name);
195 /* construct our own wrapper data structure for the pad to
196 * keep track of its status */
198 GstMultipartPadData *multipartpad;
200 multipartpad = (GstMultipartPadData *)
201 gst_collect_pads_add_pad (multipart_mux->collect, newpad,
202 sizeof (GstMultipartPadData), NULL, TRUE);
204 /* save a pointer to our data in the pad */
205 multipartpad->pad = newpad;
206 gst_pad_set_element_private (newpad, multipartpad);
207 multipart_mux->numpads++;
210 /* add the pad to the element */
211 gst_element_add_pad (element, newpad);
218 g_warning ("multipart_mux: this is not our template!");
225 gst_multipart_mux_handle_src_event (GstPad * pad, GstObject * parent,
230 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
234 /* disable seeking for now */
240 return gst_pad_event_default (pad, parent, event);
244 gst_multipart_mux_get_mime (GstMultipartMux * mux, GstStructure * s)
246 GstMultipartMuxClass *klass;
253 klass = GST_MULTIPART_MUX_GET_CLASS (mux);
255 name = gst_structure_get_name (s);
257 /* use hashtable to convert to mime type */
258 mime = g_hash_table_lookup (klass->mimetypes, name);
260 if (!strcmp (name, "audio/x-adpcm"))
261 gst_structure_get_int (s, "bitrate", &bitrate);
265 mime = "audio/G726-16";
268 mime = "audio/G726-24";
271 mime = "audio/G726-32";
274 mime = "audio/G726-40";
277 /* no mime type mapping, use name */
282 /* RFC2046 requires audio/basic to be mulaw 8000Hz mono */
283 if (g_ascii_strcasecmp (mime, "audio/basic") == 0) {
284 if (gst_structure_get_int (s, "rate", &rate) &&
285 gst_structure_get_int (s, "channels", &channels)) {
286 if (rate != 8000 || channels != 1) {
297 * Given two pads, compare the buffers queued on it and return 0 if they have
298 * an equal priority, 1 if the new pad is better, -1 if the old pad is better
301 gst_multipart_mux_compare_pads (GstMultipartMux * multipart_mux,
302 GstMultipartPadData * old, GstMultipartPadData * new)
304 guint64 oldtime, newtime;
306 /* if the old pad doesn't contain anything or is even NULL, return
307 * the new pad as best candidate and vice versa */
308 if (old == NULL || old->buffer == NULL)
310 if (new == NULL || new->buffer == NULL)
313 if (GST_CLOCK_TIME_IS_VALID (old->dts_timestamp) &&
314 GST_CLOCK_TIME_IS_VALID (new->dts_timestamp)) {
315 oldtime = old->dts_timestamp;
316 newtime = new->dts_timestamp;
318 oldtime = old->pts_timestamp;
319 newtime = new->pts_timestamp;
322 /* no timestamp on old buffer, it must go first */
323 if (oldtime == GST_CLOCK_TIME_NONE)
326 /* no timestamp on new buffer, it must go first */
327 if (newtime == GST_CLOCK_TIME_NONE)
330 /* old buffer has higher timestamp, new one should go first */
331 if (newtime < oldtime)
333 /* new buffer has higher timestamp, old one should go first */
334 else if (newtime > oldtime)
337 /* same priority if all of the above failed */
341 /* make sure a buffer is queued on all pads, returns a pointer to an multipartpad
342 * that holds the best buffer or NULL when no pad was usable */
343 static GstMultipartPadData *
344 gst_multipart_mux_queue_pads (GstMultipartMux * mux)
347 GstMultipartPadData *bestpad = NULL;
349 g_return_val_if_fail (GST_IS_MULTIPART_MUX (mux), NULL);
351 /* try to make sure we have a buffer from each usable pad first */
352 walk = mux->collect->data;
354 GstCollectData *data = (GstCollectData *) walk->data;
355 GstMultipartPadData *pad = (GstMultipartPadData *) data;
357 walk = g_slist_next (walk);
359 /* try to get a new buffer for this pad if needed and possible */
360 if (pad->buffer == NULL) {
361 GstBuffer *buf = NULL;
363 buf = gst_collect_pads_pop (mux->collect, data);
365 /* Store timestamps with segment_start and preroll */
366 if (buf && GST_BUFFER_PTS_IS_VALID (buf)) {
368 gst_segment_to_running_time (&data->segment, GST_FORMAT_TIME,
369 GST_BUFFER_PTS (buf));
371 pad->pts_timestamp = GST_CLOCK_TIME_NONE;
373 if (buf && GST_BUFFER_DTS_IS_VALID (buf)) {
375 gst_segment_to_running_time (&data->segment, GST_FORMAT_TIME,
376 GST_BUFFER_DTS (buf));
378 pad->dts_timestamp = GST_CLOCK_TIME_NONE;
385 /* we should have a buffer now, see if it is the best stream to
387 if (pad->buffer != NULL) {
388 if (gst_multipart_mux_compare_pads (mux, bestpad, pad) > 0) {
398 gst_multipart_mux_sink_event (GstCollectPads * pads, GstCollectData * data,
399 GstEvent * event, GstMultipartMux * mux)
403 switch (GST_EVENT_TYPE (event)) {
404 case GST_EVENT_FLUSH_STOP:
406 mux->need_segment = TRUE;
413 ret = gst_collect_pads_event_default (pads, data, event, FALSE);
420 * 1) find a pad to pull on, this is done by pulling on all pads and
421 * looking at the buffers to decide which one should be muxed first.
422 * 2) create a new buffer for the header
423 * 3) push both buffers on best pad, go to 1
426 gst_multipart_mux_collected (GstCollectPads * pads, GstMultipartMux * mux)
428 GstMultipartPadData *best;
429 GstFlowReturn ret = GST_FLOW_OK;
430 gchar *header = NULL;
432 GstBuffer *headerbuf = NULL;
433 GstBuffer *footerbuf = NULL;
434 GstBuffer *databuf = NULL;
435 GstStructure *structure = NULL;
439 GST_DEBUG_OBJECT (mux, "all pads are collected");
441 if (mux->need_stream_start) {
444 /* stream-start (FIXME: create id based on input ids) */
445 g_snprintf (s_id, sizeof (s_id), "multipartmux-%08x", g_random_int ());
446 gst_pad_push_event (mux->srcpad, gst_event_new_stream_start (s_id));
448 mux->need_stream_start = FALSE;
451 /* queue buffers on all pads; find a buffer with the lowest timestamp */
452 best = gst_multipart_mux_queue_pads (mux);
456 else if (!best->buffer)
459 /* If not negotiated yet set caps on src pad */
460 if (!mux->negotiated) {
463 newcaps = gst_caps_new_simple ("multipart/x-mixed-replace",
464 "boundary", G_TYPE_STRING, mux->boundary, NULL);
466 if (!gst_pad_set_caps (mux->srcpad, newcaps)) {
467 gst_caps_unref (newcaps);
471 gst_caps_unref (newcaps);
472 mux->negotiated = TRUE;
475 /* see if we need to push a segment */
476 if (mux->need_segment) {
480 if (best->dts_timestamp != GST_CLOCK_TIME_NONE) {
481 time = best->dts_timestamp;
482 } else if (best->pts_timestamp != GST_CLOCK_TIME_NONE) {
483 time = best->pts_timestamp;
488 /* for the segment, we take the first timestamp we see, we don't know the
489 * length and the position is 0 */
490 gst_segment_init (&segment, GST_FORMAT_TIME);
491 segment.start = time;
493 gst_pad_push_event (mux->srcpad, gst_event_new_segment (&segment));
495 mux->need_segment = FALSE;
498 caps = gst_pad_get_current_caps (best->pad);
502 structure = gst_caps_get_structure (caps, 0);
504 gst_caps_unref (caps);
508 /* get the mime type for the structure */
509 mime = gst_multipart_mux_get_mime (mux, structure);
510 gst_caps_unref (caps);
512 header = g_strdup_printf ("--%s\r\nContent-Type: %s\r\n"
513 "Content-Length: %" G_GSIZE_FORMAT "\r\n\r\n",
514 mux->boundary, mime, gst_buffer_get_size (best->buffer));
515 headerlen = strlen (header);
517 headerbuf = gst_buffer_new_allocate (NULL, headerlen, NULL);
518 gst_buffer_fill (headerbuf, 0, header, headerlen);
521 /* the header has the same timestamps as the data buffer (which we will push
522 * below) and has a duration of 0 */
523 GST_BUFFER_PTS (headerbuf) = best->pts_timestamp;
524 GST_BUFFER_DTS (headerbuf) = best->dts_timestamp;
525 GST_BUFFER_DURATION (headerbuf) = 0;
526 GST_BUFFER_OFFSET (headerbuf) = mux->offset;
527 mux->offset += headerlen;
528 GST_BUFFER_OFFSET_END (headerbuf) = mux->offset;
530 GST_DEBUG_OBJECT (mux, "pushing %" G_GSIZE_FORMAT " bytes header buffer",
532 ret = gst_pad_push (mux->srcpad, headerbuf);
533 if (ret != GST_FLOW_OK)
534 /* push always takes ownership of the buffer, even after an error, so we
535 * don't need to unref headerbuf here. */
538 /* take best->buffer, we don't need to unref it later as we will push it
540 databuf = gst_buffer_make_writable (best->buffer);
543 /* we need to updated the timestamps to match the running_time */
544 GST_BUFFER_PTS (databuf) = best->pts_timestamp;
545 GST_BUFFER_DTS (databuf) = best->dts_timestamp;
546 GST_BUFFER_OFFSET (databuf) = mux->offset;
547 mux->offset += gst_buffer_get_size (databuf);
548 GST_BUFFER_OFFSET_END (databuf) = mux->offset;
549 GST_BUFFER_FLAG_SET (databuf, GST_BUFFER_FLAG_DELTA_UNIT);
551 GST_DEBUG_OBJECT (mux, "pushing %" G_GSIZE_FORMAT " bytes data buffer",
552 gst_buffer_get_size (databuf));
553 ret = gst_pad_push (mux->srcpad, databuf);
554 if (ret != GST_FLOW_OK)
555 /* push always takes ownership of the buffer, even after an error, so we
556 * don't need to unref headerbuf here. */
559 footerbuf = gst_buffer_new_allocate (NULL, 2, NULL);
560 gst_buffer_fill (footerbuf, 0, "\r\n", 2);
562 /* the footer has the same timestamps as the data buffer and has a
564 GST_BUFFER_PTS (footerbuf) = best->pts_timestamp;
565 GST_BUFFER_DTS (footerbuf) = best->dts_timestamp;
566 GST_BUFFER_DURATION (footerbuf) = 0;
567 GST_BUFFER_OFFSET (footerbuf) = mux->offset;
569 GST_BUFFER_OFFSET_END (footerbuf) = mux->offset;
570 GST_BUFFER_FLAG_SET (footerbuf, GST_BUFFER_FLAG_DELTA_UNIT);
572 GST_DEBUG_OBJECT (mux, "pushing 2 bytes footer buffer");
573 ret = gst_pad_push (mux->srcpad, footerbuf);
576 if (best && best->buffer) {
577 gst_buffer_unref (best->buffer);
585 /* There is a best but no buffer, this is not quite right.. */
586 GST_ELEMENT_ERROR (mux, STREAM, FAILED, (NULL), ("internal muxing error"));
587 ret = GST_FLOW_ERROR;
592 GST_DEBUG_OBJECT (mux, "Pushing EOS");
593 gst_pad_push_event (mux->srcpad, gst_event_new_eos ());
599 GST_WARNING_OBJECT (mux, "failed to set caps");
600 GST_ELEMENT_ERROR (mux, CORE, NEGOTIATION, (NULL), (NULL));
601 ret = GST_FLOW_NOT_NEGOTIATED;
606 GST_WARNING_OBJECT (mux, "no caps on the incoming buffer %p", best->buffer);
607 GST_ELEMENT_ERROR (mux, CORE, NEGOTIATION, (NULL), (NULL));
608 ret = GST_FLOW_NOT_NEGOTIATED;
614 gst_multipart_mux_get_property (GObject * object,
615 guint prop_id, GValue * value, GParamSpec * pspec)
617 GstMultipartMux *mux;
619 mux = GST_MULTIPART_MUX (object);
623 g_value_set_string (value, mux->boundary);
626 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
632 gst_multipart_mux_set_property (GObject * object,
633 guint prop_id, const GValue * value, GParamSpec * pspec)
635 GstMultipartMux *mux;
637 mux = GST_MULTIPART_MUX (object);
641 g_free (mux->boundary);
642 mux->boundary = g_strdup (g_value_get_string (value));
645 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
650 static GstStateChangeReturn
651 gst_multipart_mux_change_state (GstElement * element, GstStateChange transition)
653 GstMultipartMux *multipart_mux;
654 GstStateChangeReturn ret;
656 multipart_mux = GST_MULTIPART_MUX (element);
658 switch (transition) {
659 case GST_STATE_CHANGE_READY_TO_PAUSED:
660 multipart_mux->offset = 0;
661 multipart_mux->negotiated = FALSE;
662 multipart_mux->need_segment = TRUE;
663 multipart_mux->need_stream_start = TRUE;
664 GST_DEBUG_OBJECT (multipart_mux, "starting collect pads");
665 gst_collect_pads_start (multipart_mux->collect);
667 case GST_STATE_CHANGE_PAUSED_TO_READY:
668 GST_DEBUG_OBJECT (multipart_mux, "stopping collect pads");
669 gst_collect_pads_stop (multipart_mux->collect);
675 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
676 if (ret == GST_STATE_CHANGE_FAILURE)
679 switch (transition) {
688 gst_multipart_mux_plugin_init (GstPlugin * plugin)
690 GST_DEBUG_CATEGORY_INIT (gst_multipart_mux_debug, "multipartmux", 0,
693 return gst_element_register (plugin, "multipartmux", GST_RANK_NONE,
694 GST_TYPE_MULTIPART_MUX);