2 * Copyright (C) 2006 Sjoerd Simons <sjoerd@luon.net>
3 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
5 * gstmultipartdemux.c: multipart stream demuxer
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 * SECTION:element-multipartdemux
25 * @see_also: #GstMultipartMux
27 * MultipartDemux uses the Content-type field of incoming buffers to demux and
28 * push data to dynamic source pads. Most of the time multipart streams are
29 * sequential JPEG frames generated from a live source such as a network source
32 * The output buffers of the multipartdemux typically have no timestamps and are
33 * usually played as fast as possible (at the rate that the source provides the
36 * the content in multipart files is separated with a boundary string that can
37 * be configured specifically with the #GstMultipartDemux:boundary property
38 * otherwise it will be autodetected.
41 * <title>Sample pipelines</title>
43 * gst-launch-1.0 filesrc location=/tmp/test.multipart ! multipartdemux ! image/jpeg,framerate=\(fraction\)5/1 ! jpegparse ! jpegdec ! videoconvert ! autovideosink
44 * ]| a simple pipeline to demux a multipart file muxed with #GstMultipartMux
45 * containing JPEG frames.
53 #include "multipartdemux.h"
55 GST_DEBUG_CATEGORY_STATIC (gst_multipart_demux_debug);
56 #define GST_CAT_DEFAULT gst_multipart_demux_debug
58 #define DEFAULT_BOUNDARY NULL
59 #define DEFAULT_SINGLE_STREAM FALSE
68 static GstStaticPadTemplate multipart_demux_src_template_factory =
69 GST_STATIC_PAD_TEMPLATE ("src_%u",
74 static GstStaticPadTemplate multipart_demux_sink_template_factory =
75 GST_STATIC_PAD_TEMPLATE ("sink",
78 GST_STATIC_CAPS ("multipart/x-mixed-replace")
87 /* convert from mime types to gst structure names. Add more when needed. The
88 * mime-type is stored as lowercase */
89 static const GstNamesMap gstnames[] = {
90 /* RFC 2046 says audio/basic is mulaw, mono, 8000Hz */
91 {"audio/basic", "audio/x-mulaw, channels=1, rate=8000"},
93 "audio/x-adpcm, bitrate=16000, layout=g726, channels=1, rate=8000"},
95 "audio/x-adpcm, bitrate=24000, layout=g726, channels=1, rate=8000"},
97 "audio/x-adpcm, bitrate=32000, layout=g726, channels=1, rate=8000"},
99 "audio/x-adpcm, bitrate=40000, layout=g726, channels=1, rate=8000"},
100 /* Panasonic Network Cameras non-standard types */
102 "audio/x-adpcm, bitrate=32000, layout=g726, channels=1, rate=8000"},
107 static GstFlowReturn gst_multipart_demux_chain (GstPad * pad,
108 GstObject * parent, GstBuffer * buf);
109 static gboolean gst_multipart_demux_event (GstPad * pad,
110 GstObject * parent, GstEvent * event);
112 static GstStateChangeReturn gst_multipart_demux_change_state (GstElement *
113 element, GstStateChange transition);
115 static void gst_multipart_set_property (GObject * object, guint prop_id,
116 const GValue * value, GParamSpec * pspec);
118 static void gst_multipart_get_property (GObject * object, guint prop_id,
119 GValue * value, GParamSpec * pspec);
121 static void gst_multipart_demux_dispose (GObject * object);
123 #define gst_multipart_demux_parent_class parent_class
124 G_DEFINE_TYPE (GstMultipartDemux, gst_multipart_demux, GST_TYPE_ELEMENT);
127 gst_multipart_demux_class_init (GstMultipartDemuxClass * klass)
131 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
132 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
134 gobject_class->dispose = gst_multipart_demux_dispose;
135 gobject_class->set_property = gst_multipart_set_property;
136 gobject_class->get_property = gst_multipart_get_property;
138 g_object_class_install_property (gobject_class, PROP_BOUNDARY,
139 g_param_spec_string ("boundary", "Boundary",
140 "The boundary string separating data, automatic if NULL",
142 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
145 * GstMultipartDemux:single-stream:
147 * Assume that there is only one stream whose content-type will
148 * not change and emit no-more-pads as soon as the first boundary
149 * content is parsed, decoded, and pads are linked.
151 g_object_class_install_property (gobject_class, PROP_SINGLE_STREAM,
152 g_param_spec_boolean ("single-stream", "Single Stream",
153 "Assume that there is only one stream whose content-type will not change and emit no-more-pads as soon as the first boundary content is parsed, decoded, and pads are linked",
154 DEFAULT_SINGLE_STREAM, G_PARAM_READWRITE));
156 /* populate gst names and mime types pairs */
157 klass->gstnames = g_hash_table_new (g_str_hash, g_str_equal);
158 for (i = 0; gstnames[i].key; i++) {
159 g_hash_table_insert (klass->gstnames, (gpointer) gstnames[i].key,
160 (gpointer) gstnames[i].val);
163 gstelement_class->change_state = gst_multipart_demux_change_state;
165 gst_element_class_add_static_pad_template (gstelement_class,
166 &multipart_demux_sink_template_factory);
167 gst_element_class_add_static_pad_template (gstelement_class,
168 &multipart_demux_src_template_factory);
169 gst_element_class_set_static_metadata (gstelement_class, "Multipart demuxer",
170 "Codec/Demuxer", "demux multipart streams",
171 "Wim Taymans <wim.taymans@gmail.com>, Sjoerd Simons <sjoerd@luon.net>");
175 gst_multipart_demux_init (GstMultipartDemux * multipart)
177 /* create the sink pad */
179 gst_pad_new_from_static_template (&multipart_demux_sink_template_factory,
181 gst_element_add_pad (GST_ELEMENT_CAST (multipart), multipart->sinkpad);
182 gst_pad_set_chain_function (multipart->sinkpad,
183 GST_DEBUG_FUNCPTR (gst_multipart_demux_chain));
184 gst_pad_set_event_function (multipart->sinkpad,
185 GST_DEBUG_FUNCPTR (gst_multipart_demux_event));
187 multipart->adapter = gst_adapter_new ();
188 multipart->boundary = DEFAULT_BOUNDARY;
189 multipart->mime_type = NULL;
190 multipart->content_length = -1;
191 multipart->header_completed = FALSE;
192 multipart->scanpos = 0;
193 multipart->singleStream = DEFAULT_SINGLE_STREAM;
194 multipart->have_group_id = FALSE;
195 multipart->group_id = G_MAXUINT;
199 gst_multipart_demux_remove_src_pads (GstMultipartDemux * demux)
201 while (demux->srcpads != NULL) {
202 GstMultipartPad *mppad = demux->srcpads->data;
204 gst_element_remove_pad (GST_ELEMENT (demux), mppad->pad);
205 g_free (mppad->mime);
207 demux->srcpads = g_slist_delete_link (demux->srcpads, demux->srcpads);
209 demux->srcpads = NULL;
214 gst_multipart_demux_dispose (GObject * object)
216 GstMultipartDemux *demux = GST_MULTIPART_DEMUX (object);
218 if (demux->adapter != NULL)
219 g_object_unref (demux->adapter);
220 demux->adapter = NULL;
221 g_free (demux->boundary);
222 demux->boundary = NULL;
223 g_free (demux->mime_type);
224 demux->mime_type = NULL;
225 gst_multipart_demux_remove_src_pads (demux);
227 G_OBJECT_CLASS (parent_class)->dispose (object);
231 gst_multipart_demux_get_gstname (GstMultipartDemux * demux, gchar * mimetype)
233 GstMultipartDemuxClass *klass;
234 const gchar *gstname;
236 klass = GST_MULTIPART_DEMUX_GET_CLASS (demux);
238 /* use hashtable to convert to gst name */
239 gstname = g_hash_table_lookup (klass->gstnames, mimetype);
240 if (gstname == NULL) {
241 /* no gst name mapping, use mime type */
244 GST_DEBUG_OBJECT (demux, "gst name for %s is %s", mimetype, gstname);
249 gst_multipart_combine_flows (GstMultipartDemux * demux, GstMultipartPad * pad,
254 /* store the value */
257 /* any other error that is not-linked can be returned right
259 if (ret != GST_FLOW_NOT_LINKED)
262 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
263 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
264 GstMultipartPad *opad = (GstMultipartPad *) walk->data;
266 ret = opad->last_ret;
267 /* some other return value (must be SUCCESS but we can return
268 * other values as well) */
269 if (ret != GST_FLOW_NOT_LINKED)
272 /* if we get here, all other pads were unlinked and we return
278 static GstMultipartPad *
279 gst_multipart_find_pad_by_mime (GstMultipartDemux * demux, gchar * mime,
284 walk = demux->srcpads;
286 GstMultipartPad *pad = (GstMultipartPad *) walk->data;
288 if (!strcmp (pad->mime, mime)) {
297 /* pad not found, create it */
300 GstMultipartPad *mppad;
302 const gchar *capsname;
307 mppad = g_new0 (GstMultipartPad, 1);
309 GST_DEBUG_OBJECT (demux, "creating pad with mime: %s", mime);
311 name = g_strdup_printf ("src_%u", demux->numpads);
313 gst_pad_new_from_static_template (&multipart_demux_src_template_factory,
318 mppad->mime = g_strdup (mime);
319 mppad->last_ret = GST_FLOW_OK;
320 mppad->last_ts = GST_CLOCK_TIME_NONE;
321 mppad->discont = TRUE;
323 demux->srcpads = g_slist_prepend (demux->srcpads, mppad);
326 gst_pad_use_fixed_caps (pad);
327 gst_pad_set_active (pad, TRUE);
329 /* prepare and send stream-start */
330 if (!demux->have_group_id) {
331 event = gst_pad_get_sticky_event (demux->sinkpad,
332 GST_EVENT_STREAM_START, 0);
335 demux->have_group_id =
336 gst_event_parse_group_id (event, &demux->group_id);
337 gst_event_unref (event);
338 } else if (!demux->have_group_id) {
339 demux->have_group_id = TRUE;
340 demux->group_id = gst_util_group_id_next ();
344 stream_id = gst_pad_create_stream_id (pad,
345 GST_ELEMENT_CAST (demux), demux->mime_type);
347 event = gst_event_new_stream_start (stream_id);
348 if (demux->have_group_id)
349 gst_event_set_group_id (event, demux->group_id);
351 gst_pad_store_sticky_event (pad, event);
353 gst_event_unref (event);
355 /* take the mime type, convert it to the caps name */
356 capsname = gst_multipart_demux_get_gstname (demux, mime);
357 caps = gst_caps_from_string (capsname);
358 GST_DEBUG_OBJECT (demux, "caps for pad: %s", capsname);
359 gst_pad_set_caps (pad, caps);
360 gst_element_add_pad (GST_ELEMENT_CAST (demux), pad);
361 gst_caps_unref (caps);
367 if (demux->singleStream) {
368 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
376 get_line_end (const guint8 * data, const guint8 * dataend, guint8 ** end,
380 gboolean foundr = FALSE;
382 for (x = (guint8 *) data; x < dataend; x++) {
385 } else if (*x == '\n') {
386 *end = x - (foundr ? 1 : 0);
395 get_mime_len (const guint8 * data, guint maxlen)
400 while (*x != '\0' && *x != '\r' && *x != '\n' && *x != ';') {
407 multipart_parse_header (GstMultipartDemux * multipart)
410 const guint8 *dataend;
417 datalen = gst_adapter_available (multipart->adapter);
418 data = gst_adapter_map (multipart->adapter, datalen);
419 dataend = data + datalen;
421 /* Skip leading whitespace, pos endposition should at least leave space for
422 * the boundary and a \n */
423 for (pos = (guint8 *) data; pos < dataend - 4 && g_ascii_isspace (*pos);
426 if (pos >= dataend - 4)
429 if (G_UNLIKELY (pos[0] != '-' || pos[1] != '-')) {
430 GST_DEBUG_OBJECT (multipart, "No boundary available");
434 /* First the boundary */
435 if (!get_line_end (pos, dataend, &end, &next))
438 /* Ignore the leading -- */
439 boundary_len = end - pos - 2;
440 boundary = (gchar *) pos + 2;
441 if (boundary_len < 1) {
442 GST_DEBUG_OBJECT (multipart, "No boundary available");
446 if (G_UNLIKELY (multipart->boundary == NULL)) {
447 /* First time we see the boundary, copy it */
448 multipart->boundary = g_strndup (boundary, boundary_len);
449 multipart->boundary_len = boundary_len;
450 } else if (G_UNLIKELY (boundary_len != multipart->boundary_len)) {
451 /* Something odd is going on, either the boundary indicated EOS or it's
453 if (G_UNLIKELY (boundary_len == multipart->boundary_len + 2 &&
454 !strncmp (boundary, multipart->boundary, multipart->boundary_len) &&
455 !strncmp (boundary + multipart->boundary_len, "--", 2)))
458 GST_DEBUG_OBJECT (multipart,
459 "Boundary length doesn't match detected boundary (%d <> %d",
460 boundary_len, multipart->boundary_len);
462 } else if (G_UNLIKELY (strncmp (boundary, multipart->boundary, boundary_len))) {
463 GST_DEBUG_OBJECT (multipart, "Boundary doesn't match previous boundary");
468 while (get_line_end (pos, dataend, &end, &next)) {
469 guint len = end - pos;
472 /* empty line, data starts behind us */
473 GST_DEBUG_OBJECT (multipart,
474 "Parsed the header - boundary: %s, mime-type: %s, content-length: %d",
475 multipart->boundary, multipart->mime_type, multipart->content_length);
476 gst_adapter_unmap (multipart->adapter);
480 if (len >= 14 && !g_ascii_strncasecmp ("content-type:", (gchar *) pos, 13)) {
483 /* only take the mime type up to the first ; if any. After ; there can be
484 * properties that we don't handle yet. */
485 mime_len = get_mime_len (pos + 14, len - 14);
487 g_free (multipart->mime_type);
488 multipart->mime_type = g_ascii_strdown ((gchar *) pos + 14, mime_len);
489 } else if (len >= 15 &&
490 !g_ascii_strncasecmp ("content-length:", (gchar *) pos, 15)) {
491 multipart->content_length =
492 g_ascii_strtoull ((gchar *) pos + 15, NULL, 10);
498 GST_DEBUG_OBJECT (multipart, "Need more data for the header");
499 gst_adapter_unmap (multipart->adapter);
501 return MULTIPART_NEED_MORE_DATA;
505 GST_ELEMENT_ERROR (multipart, STREAM, DEMUX, (NULL),
506 ("Boundary not found in the multipart header"));
507 gst_adapter_unmap (multipart->adapter);
508 return MULTIPART_DATA_ERROR;
512 GST_DEBUG_OBJECT (multipart, "we are EOS");
513 gst_adapter_unmap (multipart->adapter);
514 return MULTIPART_DATA_EOS;
519 multipart_find_boundary (GstMultipartDemux * multipart, gint * datalen)
521 /* Adaptor is positioned at the start of the data */
522 const guint8 *data, *pos;
523 const guint8 *dataend;
526 if (multipart->content_length >= 0) {
527 /* fast path, known content length :) */
528 len = multipart->content_length;
529 if (gst_adapter_available (multipart->adapter) >= len + 2) {
531 data = gst_adapter_map (multipart->adapter, len + 1);
533 /* If data[len] contains \r then assume a newline is \r\n */
534 if (data[len] == '\r')
536 else if (data[len] == '\n')
539 gst_adapter_unmap (multipart->adapter);
540 /* Don't check if boundary is actually there, but let the header parsing
541 * bail out if it isn't */
545 return MULTIPART_NEED_MORE_DATA;
549 len = gst_adapter_available (multipart->adapter);
551 return MULTIPART_NEED_MORE_DATA;
552 data = gst_adapter_map (multipart->adapter, len);
553 dataend = data + len;
555 for (pos = data + multipart->scanpos;
556 pos <= dataend - multipart->boundary_len - 2; pos++) {
557 if (*pos == '-' && pos[1] == '-' &&
558 !strncmp ((gchar *) pos + 2,
559 multipart->boundary, multipart->boundary_len)) {
560 /* Found the boundary! Check if there was a newline before the boundary */
562 if (pos - 2 > data && pos[-2] == '\r')
564 else if (pos - 1 > data && pos[-1] == '\n')
568 gst_adapter_unmap (multipart->adapter);
569 multipart->scanpos = 0;
573 gst_adapter_unmap (multipart->adapter);
574 multipart->scanpos = pos - data;
575 return MULTIPART_NEED_MORE_DATA;
579 gst_multipart_demux_event (GstPad * pad, GstObject * parent, GstEvent * event)
581 GstMultipartDemux *multipart;
583 multipart = GST_MULTIPART_DEMUX (parent);
585 switch (GST_EVENT_TYPE (event)) {
587 if (!multipart->srcpads) {
588 GST_ELEMENT_ERROR (multipart, STREAM, WRONG_TYPE,
589 ("This stream contains no valid streams."),
590 ("Got EOS before adding any pads"));
591 gst_event_unref (event);
594 return gst_pad_event_default (pad, parent, event);
598 return gst_pad_event_default (pad, parent, event);
603 gst_multipart_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
605 GstMultipartDemux *multipart;
610 multipart = GST_MULTIPART_DEMUX (parent);
611 adapter = multipart->adapter;
615 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT)) {
618 for (l = multipart->srcpads; l != NULL; l = l->next) {
619 GstMultipartPad *srcpad = l->data;
621 srcpad->discont = TRUE;
623 gst_adapter_clear (adapter);
625 gst_adapter_push (adapter, buf);
627 while (gst_adapter_available (adapter) > 0) {
628 GstMultipartPad *srcpad;
633 if (G_UNLIKELY (!multipart->header_completed)) {
634 if ((size = multipart_parse_header (multipart)) < 0) {
637 gst_adapter_flush (adapter, size);
638 multipart->header_completed = TRUE;
641 if ((size = multipart_find_boundary (multipart, &datalen)) < 0) {
645 /* Invalidate header info */
646 multipart->header_completed = FALSE;
647 multipart->content_length = -1;
649 if (G_UNLIKELY (datalen <= 0)) {
650 GST_DEBUG_OBJECT (multipart, "skipping empty content.");
651 gst_adapter_flush (adapter, size - datalen);
652 } else if (G_UNLIKELY (!multipart->mime_type)) {
653 GST_DEBUG_OBJECT (multipart, "content has no MIME type.");
654 gst_adapter_flush (adapter, size - datalen);
659 gst_multipart_find_pad_by_mime (multipart,
660 multipart->mime_type, &created);
662 ts = gst_adapter_prev_pts (adapter, NULL);
663 outbuf = gst_adapter_take_buffer (adapter, datalen);
664 gst_adapter_flush (adapter, size - datalen);
670 gst_segment_init (&segment, GST_FORMAT_TIME);
672 /* Push new segment, first buffer has 0 timestamp */
673 gst_pad_push_event (srcpad->pad, gst_event_new_segment (&segment));
675 tags = gst_tag_list_new (GST_TAG_CONTAINER_FORMAT, "Multipart", NULL);
676 gst_tag_list_set_scope (tags, GST_TAG_SCOPE_GLOBAL);
677 gst_pad_push_event (srcpad->pad, gst_event_new_tag (tags));
680 outbuf = gst_buffer_make_writable (outbuf);
681 if (srcpad->last_ts == GST_CLOCK_TIME_NONE || srcpad->last_ts != ts) {
682 GST_BUFFER_TIMESTAMP (outbuf) = ts;
683 srcpad->last_ts = ts;
685 GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
688 if (srcpad->discont) {
689 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
690 srcpad->discont = FALSE;
692 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DISCONT);
695 GST_DEBUG_OBJECT (multipart,
696 "pushing buffer with timestamp %" GST_TIME_FORMAT,
697 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
698 res = gst_pad_push (srcpad->pad, outbuf);
699 res = gst_multipart_combine_flows (multipart, srcpad, res);
700 if (res != GST_FLOW_OK)
706 if (G_UNLIKELY (size == MULTIPART_DATA_ERROR))
707 return GST_FLOW_ERROR;
708 if (G_UNLIKELY (size == MULTIPART_DATA_EOS))
714 static GstStateChangeReturn
715 gst_multipart_demux_change_state (GstElement * element,
716 GstStateChange transition)
718 GstMultipartDemux *multipart;
719 GstStateChangeReturn ret;
721 multipart = GST_MULTIPART_DEMUX (element);
723 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
724 if (ret == GST_STATE_CHANGE_FAILURE)
727 switch (transition) {
728 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
730 case GST_STATE_CHANGE_PAUSED_TO_READY:
731 multipart->header_completed = FALSE;
732 g_free (multipart->boundary);
733 multipart->boundary = NULL;
734 g_free (multipart->mime_type);
735 multipart->mime_type = NULL;
736 gst_adapter_clear (multipart->adapter);
737 multipart->content_length = -1;
738 multipart->scanpos = 0;
739 gst_multipart_demux_remove_src_pads (multipart);
740 multipart->have_group_id = FALSE;
741 multipart->group_id = G_MAXUINT;
743 case GST_STATE_CHANGE_READY_TO_NULL:
754 gst_multipart_set_property (GObject * object, guint prop_id,
755 const GValue * value, GParamSpec * pspec)
757 GstMultipartDemux *filter;
759 filter = GST_MULTIPART_DEMUX (object);
763 /* Not really that useful anymore as we can reliably autoscan */
764 g_free (filter->boundary);
765 filter->boundary = g_value_dup_string (value);
766 if (filter->boundary != NULL) {
767 filter->boundary_len = strlen (filter->boundary);
770 case PROP_SINGLE_STREAM:
771 filter->singleStream = g_value_get_boolean (value);
774 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
780 gst_multipart_get_property (GObject * object, guint prop_id,
781 GValue * value, GParamSpec * pspec)
783 GstMultipartDemux *filter;
785 filter = GST_MULTIPART_DEMUX (object);
789 g_value_set_string (value, filter->boundary);
791 case PROP_SINGLE_STREAM:
792 g_value_set_boolean (value, filter->singleStream);
795 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
803 gst_multipart_demux_plugin_init (GstPlugin * plugin)
805 GST_DEBUG_CATEGORY_INIT (gst_multipart_demux_debug,
806 "multipartdemux", 0, "multipart demuxer");
808 return gst_element_register (plugin, "multipartdemux", GST_RANK_PRIMARY,
809 GST_TYPE_MULTIPART_DEMUX);