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_static_pad_template (element_class, &src_factory);
132 gst_element_class_add_static_pad_template (element_class, &sink_factory);
134 gst_element_class_set_details_simple (element_class, "Multipart muxer",
135 "Codec/Muxer", "mux multipart streams", "Wim Taymans <wim@fluendo.com>");
139 gst_multipart_mux_class_init (GstMultipartMuxClass * klass)
141 GObjectClass *gobject_class;
142 GstElementClass *gstelement_class;
145 gobject_class = (GObjectClass *) klass;
146 gstelement_class = (GstElementClass *) klass;
148 parent_class = g_type_class_peek_parent (klass);
150 gobject_class->finalize = gst_multipart_mux_finalize;
151 gobject_class->get_property = gst_multipart_mux_get_property;
152 gobject_class->set_property = gst_multipart_mux_set_property;
154 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BOUNDARY,
155 g_param_spec_string ("boundary", "Boundary", "Boundary string",
156 DEFAULT_BOUNDARY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158 gstelement_class->request_new_pad = gst_multipart_mux_request_new_pad;
159 gstelement_class->change_state = gst_multipart_mux_change_state;
161 /* populate mime types */
162 klass->mimetypes = g_hash_table_new (g_str_hash, g_str_equal);
163 for (i = 0; mimetypes[i].key; i++) {
164 g_hash_table_insert (klass->mimetypes, (gpointer) mimetypes[i].key,
165 (gpointer) mimetypes[i].val);
170 gst_multipart_mux_init (GstMultipartMux * multipart_mux)
172 GstElementClass *klass = GST_ELEMENT_GET_CLASS (multipart_mux);
174 multipart_mux->srcpad =
175 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
177 gst_pad_set_event_function (multipart_mux->srcpad,
178 gst_multipart_mux_handle_src_event);
179 gst_element_add_pad (GST_ELEMENT (multipart_mux), multipart_mux->srcpad);
181 multipart_mux->boundary = g_strdup (DEFAULT_BOUNDARY);
183 multipart_mux->collect = gst_collect_pads_new ();
184 gst_collect_pads_set_function (multipart_mux->collect,
185 (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_multipart_mux_collected),
190 gst_multipart_mux_finalize (GObject * object)
192 GstMultipartMux *multipart_mux;
194 multipart_mux = GST_MULTIPART_MUX (object);
196 g_free (multipart_mux->boundary);
198 if (multipart_mux->collect)
199 gst_object_unref (multipart_mux->collect);
201 G_OBJECT_CLASS (parent_class)->finalize (object);
205 gst_multipart_mux_request_new_pad (GstElement * element,
206 GstPadTemplate * templ, const gchar * req_name)
208 GstMultipartMux *multipart_mux;
210 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
213 if (templ != gst_element_class_get_pad_template (klass, "sink_%d"))
216 multipart_mux = GST_MULTIPART_MUX (element);
218 /* create new pad with the name */
219 name = g_strdup_printf ("sink_%02d", multipart_mux->numpads);
220 newpad = gst_pad_new_from_template (templ, name);
223 /* construct our own wrapper data structure for the pad to
224 * keep track of its status */
226 GstMultipartPadData *multipartpad;
228 multipartpad = (GstMultipartPadData *)
229 gst_collect_pads_add_pad (multipart_mux->collect, newpad,
230 sizeof (GstMultipartPadData));
232 /* save a pointer to our data in the pad */
233 gst_pad_set_element_private (newpad, multipartpad);
234 multipart_mux->numpads++;
237 /* add the pad to the element */
238 gst_element_add_pad (element, newpad);
245 g_warning ("multipart_mux: this is not our template!");
252 gst_multipart_mux_handle_src_event (GstPad * pad, GstEvent * event)
254 GstMultipartMux *multipart_mux;
257 multipart_mux = GST_MULTIPART_MUX (gst_pad_get_parent (pad));
259 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
263 /* disable seeking for now */
269 gst_object_unref (multipart_mux);
271 return gst_pad_event_default (pad, event);
275 gst_multipart_mux_get_mime (GstMultipartMux * mux, GstStructure * s)
277 GstMultipartMuxClass *klass;
284 klass = GST_MULTIPART_MUX_GET_CLASS (mux);
286 name = gst_structure_get_name (s);
288 /* use hashtable to convert to mime type */
289 mime = g_hash_table_lookup (klass->mimetypes, name);
291 if (!strcmp (name, "audio/x-adpcm"))
292 gst_structure_get_int (s, "bitrate", &bitrate);
296 mime = "audio/G726-16";
299 mime = "audio/G726-24";
302 mime = "audio/G726-32";
305 mime = "audio/G726-40";
308 /* no mime type mapping, use name */
313 /* RFC2046 requires audio/basic to be mulaw 8000Hz mono */
314 if (g_ascii_strcasecmp (mime, "audio/basic") == 0) {
315 if (gst_structure_get_int (s, "rate", &rate) &&
316 gst_structure_get_int (s, "channels", &channels)) {
317 if (rate != 8000 || channels != 1) {
328 * Given two pads, compare the buffers queued on it and return 0 if they have
329 * an equal priority, 1 if the new pad is better, -1 if the old pad is better
332 gst_multipart_mux_compare_pads (GstMultipartMux * multipart_mux,
333 GstMultipartPadData * old, GstMultipartPadData * new)
335 guint64 oldtime, newtime;
337 /* if the old pad doesn't contain anything or is even NULL, return
338 * the new pad as best candidate and vice versa */
339 if (old == NULL || old->buffer == NULL)
341 if (new == NULL || new->buffer == NULL)
344 /* no timestamp on old buffer, it must go first */
345 oldtime = old->timestamp;
346 if (oldtime == GST_CLOCK_TIME_NONE)
349 /* no timestamp on new buffer, it must go first */
350 newtime = new->timestamp;
351 if (newtime == GST_CLOCK_TIME_NONE)
354 /* old buffer has higher timestamp, new one should go first */
355 if (newtime < oldtime)
357 /* new buffer has higher timestamp, old one should go first */
358 else if (newtime > oldtime)
361 /* same priority if all of the above failed */
365 /* make sure a buffer is queued on all pads, returns a pointer to an multipartpad
366 * that holds the best buffer or NULL when no pad was usable */
367 static GstMultipartPadData *
368 gst_multipart_mux_queue_pads (GstMultipartMux * mux)
371 GstMultipartPadData *bestpad = NULL;
373 g_return_val_if_fail (GST_IS_MULTIPART_MUX (mux), NULL);
375 /* try to make sure we have a buffer from each usable pad first */
376 walk = mux->collect->data;
378 GstCollectData *data = (GstCollectData *) walk->data;
379 GstMultipartPadData *pad = (GstMultipartPadData *) data;
381 walk = g_slist_next (walk);
383 /* try to get a new buffer for this pad if needed and possible */
384 if (pad->buffer == NULL) {
385 GstBuffer *buf = NULL;
387 buf = gst_collect_pads_pop (mux->collect, data);
389 /* Store timestamp with segment_start and preroll */
390 if (buf && GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
392 gst_segment_to_running_time (&data->segment, GST_FORMAT_TIME,
393 GST_BUFFER_TIMESTAMP (buf));
395 pad->timestamp = GST_CLOCK_TIME_NONE;
401 /* we should have a buffer now, see if it is the best stream to
403 if (pad->buffer != NULL) {
404 if (gst_multipart_mux_compare_pads (mux, bestpad, pad) > 0) {
415 * 1) find a pad to pull on, this is done by pulling on all pads and
416 * looking at the buffers to decide which one should be muxed first.
417 * 2) create a new buffer for the header
418 * 3) push both buffers on best pad, go to 1
421 gst_multipart_mux_collected (GstCollectPads * pads, GstMultipartMux * mux)
423 GstMultipartPadData *best;
424 GstFlowReturn ret = GST_FLOW_OK;
425 gchar *header = NULL;
427 GstBuffer *headerbuf = NULL;
428 GstBuffer *footerbuf = NULL;
429 GstBuffer *databuf = NULL;
430 GstStructure *structure = NULL;
433 GST_DEBUG_OBJECT (mux, "all pads are collected");
435 /* queue buffers on all pads; find a buffer with the lowest timestamp */
436 best = gst_multipart_mux_queue_pads (mux);
440 else if (!best->buffer)
443 /* If not negotiated yet set caps on src pad */
444 if (!mux->negotiated) {
447 newcaps = gst_caps_new_simple ("multipart/x-mixed-replace",
448 "boundary", G_TYPE_STRING, mux->boundary, NULL);
450 if (!gst_pad_set_caps (mux->srcpad, newcaps)) {
451 gst_caps_unref (newcaps);
455 gst_caps_unref (newcaps);
456 mux->negotiated = TRUE;
459 /* see if we need to push a segment */
460 if (mux->need_segment) {
464 if (best->timestamp != -1)
465 time = best->timestamp;
469 /* for the segment, we take the first timestamp we see, we don't know the
470 * length and the position is 0 */
471 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME,
474 gst_pad_push_event (mux->srcpad, event);
476 mux->need_segment = FALSE;
479 structure = gst_caps_get_structure (GST_BUFFER_CAPS (best->buffer), 0);
483 /* get the mime type for the structure */
484 mime = gst_multipart_mux_get_mime (mux, structure);
486 header = g_strdup_printf ("--%s\r\nContent-Type: %s\r\n"
487 "Content-Length: %u\r\n\r\n",
488 mux->boundary, mime, GST_BUFFER_SIZE (best->buffer));
489 headerlen = strlen (header);
491 ret = gst_pad_alloc_buffer_and_set_caps (mux->srcpad, GST_BUFFER_OFFSET_NONE,
492 headerlen, GST_PAD_CAPS (mux->srcpad), &headerbuf);
493 if (ret != GST_FLOW_OK)
496 memcpy (GST_BUFFER_DATA (headerbuf), header, headerlen);
499 /* the header has the same timestamp as the data buffer (which we will push
500 * below) and has a duration of 0 */
501 GST_BUFFER_TIMESTAMP (headerbuf) = best->timestamp;
502 GST_BUFFER_DURATION (headerbuf) = 0;
503 GST_BUFFER_OFFSET (headerbuf) = mux->offset;
504 mux->offset += headerlen;
505 GST_BUFFER_OFFSET_END (headerbuf) = mux->offset;
507 GST_DEBUG_OBJECT (mux, "pushing %" G_GSIZE_FORMAT " bytes header buffer",
509 ret = gst_pad_push (mux->srcpad, headerbuf);
510 if (ret != GST_FLOW_OK)
511 /* push always takes ownership of the buffer, even after an error, so we
512 * don't need to unref headerbuf here. */
515 /* take best->buffer, we don't need to unref it later as we will push it
517 databuf = gst_buffer_make_metadata_writable (best->buffer);
520 gst_buffer_set_caps (databuf, GST_PAD_CAPS (mux->srcpad));
521 /* we need to updated the timestamp to match the running_time */
522 GST_BUFFER_TIMESTAMP (databuf) = best->timestamp;
523 GST_BUFFER_OFFSET (databuf) = mux->offset;
524 mux->offset += GST_BUFFER_SIZE (databuf);
525 GST_BUFFER_OFFSET_END (databuf) = mux->offset;
526 GST_BUFFER_FLAG_SET (databuf, GST_BUFFER_FLAG_DELTA_UNIT);
528 GST_DEBUG_OBJECT (mux, "pushing %u bytes data buffer",
529 GST_BUFFER_SIZE (databuf));
530 ret = gst_pad_push (mux->srcpad, databuf);
531 if (ret != GST_FLOW_OK)
532 /* push always takes ownership of the buffer, even after an error, so we
533 * don't need to unref headerbuf here. */
536 ret = gst_pad_alloc_buffer_and_set_caps (mux->srcpad, GST_BUFFER_OFFSET_NONE,
537 2, GST_PAD_CAPS (mux->srcpad), &footerbuf);
538 if (ret != GST_FLOW_OK)
541 memcpy (GST_BUFFER_DATA (footerbuf), "\r\n", 2);
543 /* the footer has the same timestamp as the data buffer and has a
545 GST_BUFFER_TIMESTAMP (footerbuf) = best->timestamp;
546 GST_BUFFER_DURATION (footerbuf) = 0;
547 GST_BUFFER_OFFSET (footerbuf) = mux->offset;
549 GST_BUFFER_OFFSET_END (footerbuf) = mux->offset;
550 GST_BUFFER_FLAG_SET (footerbuf, GST_BUFFER_FLAG_DELTA_UNIT);
552 GST_DEBUG_OBJECT (mux, "pushing 2 bytes footer buffer");
553 ret = gst_pad_push (mux->srcpad, footerbuf);
556 if (best && best->buffer) {
557 gst_buffer_unref (best->buffer);
565 /* There is a best but no buffer, this is not quite right.. */
566 GST_ELEMENT_ERROR (mux, STREAM, FAILED, (NULL), ("internal muxing error"));
567 ret = GST_FLOW_ERROR;
572 GST_DEBUG_OBJECT (mux, "Pushing EOS");
573 gst_pad_push_event (mux->srcpad, gst_event_new_eos ());
574 ret = GST_FLOW_UNEXPECTED;
579 GST_WARNING_OBJECT (mux, "failed to set caps");
580 GST_ELEMENT_ERROR (mux, CORE, NEGOTIATION, (NULL), (NULL));
581 ret = GST_FLOW_NOT_NEGOTIATED;
586 GST_WARNING_OBJECT (mux, "no caps on the incoming buffer %p", best->buffer);
587 GST_ELEMENT_ERROR (mux, CORE, NEGOTIATION, (NULL), (NULL));
588 ret = GST_FLOW_NOT_NEGOTIATED;
593 GST_WARNING_OBJECT (mux,
594 "failed allocating a %" G_GSIZE_FORMAT " bytes buffer", headerlen);
601 gst_multipart_mux_get_property (GObject * object,
602 guint prop_id, GValue * value, GParamSpec * pspec)
604 GstMultipartMux *mux;
606 mux = GST_MULTIPART_MUX (object);
610 g_value_set_string (value, mux->boundary);
613 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
619 gst_multipart_mux_set_property (GObject * object,
620 guint prop_id, const GValue * value, GParamSpec * pspec)
622 GstMultipartMux *mux;
624 mux = GST_MULTIPART_MUX (object);
628 g_free (mux->boundary);
629 mux->boundary = g_strdup (g_value_get_string (value));
632 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
637 static GstStateChangeReturn
638 gst_multipart_mux_change_state (GstElement * element, GstStateChange transition)
640 GstMultipartMux *multipart_mux;
641 GstStateChangeReturn ret;
643 multipart_mux = GST_MULTIPART_MUX (element);
645 switch (transition) {
646 case GST_STATE_CHANGE_READY_TO_PAUSED:
647 multipart_mux->offset = 0;
648 multipart_mux->negotiated = FALSE;
649 multipart_mux->need_segment = TRUE;
650 GST_DEBUG_OBJECT (multipart_mux, "starting collect pads");
651 gst_collect_pads_start (multipart_mux->collect);
653 case GST_STATE_CHANGE_PAUSED_TO_READY:
654 GST_DEBUG_OBJECT (multipart_mux, "stopping collect pads");
655 gst_collect_pads_stop (multipart_mux->collect);
661 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
662 if (ret == GST_STATE_CHANGE_FAILURE)
665 switch (transition) {
674 gst_multipart_mux_plugin_init (GstPlugin * plugin)
676 GST_DEBUG_CATEGORY_INIT (gst_multipart_mux_debug, "multipartmux", 0,
679 return gst_element_register (plugin, "multipartmux", GST_RANK_NONE,
680 GST_TYPE_MULTIPART_MUX);