2 * Copyright (C) <2006> Wim Taymans <wim.taymans@gmail.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.
24 #include <gst/tag/tag.h>
25 #include <gst/rtp/gstrtpbuffer.h>
26 #include <gst/audio/audio.h>
29 #include "gstrtpvorbisdepay.h"
30 #include "gstrtputils.h"
32 GST_DEBUG_CATEGORY_STATIC (rtpvorbisdepay_debug);
33 #define GST_CAT_DEFAULT (rtpvorbisdepay_debug)
36 * http://www.rfc-editor.org/rfc/rfc5215.txt
39 static GstStaticPadTemplate gst_rtp_vorbis_depay_sink_template =
40 GST_STATIC_PAD_TEMPLATE ("sink",
43 GST_STATIC_CAPS ("application/x-rtp, "
44 "media = (string) \"audio\", "
45 "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"VORBIS\""
46 /* All required parameters
48 * "encoding-params = (string) <num channels>"
49 * "configuration = (string) ANY"
54 static GstStaticPadTemplate gst_rtp_vorbis_depay_src_template =
55 GST_STATIC_PAD_TEMPLATE ("src",
58 GST_STATIC_CAPS ("audio/x-vorbis")
61 #define gst_rtp_vorbis_depay_parent_class parent_class
62 G_DEFINE_TYPE (GstRtpVorbisDepay, gst_rtp_vorbis_depay,
63 GST_TYPE_RTP_BASE_DEPAYLOAD);
65 static gboolean gst_rtp_vorbis_depay_setcaps (GstRTPBaseDepayload * depayload,
67 static GstBuffer *gst_rtp_vorbis_depay_process (GstRTPBaseDepayload * depayload,
70 static void gst_rtp_vorbis_depay_finalize (GObject * object);
72 static GstStateChangeReturn gst_rtp_vorbis_depay_change_state (GstElement *
73 element, GstStateChange transition);
76 gst_rtp_vorbis_depay_class_init (GstRtpVorbisDepayClass * klass)
78 GObjectClass *gobject_class;
79 GstElementClass *gstelement_class;
80 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
82 gobject_class = (GObjectClass *) klass;
83 gstelement_class = (GstElementClass *) klass;
84 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
86 gobject_class->finalize = gst_rtp_vorbis_depay_finalize;
88 gstelement_class->change_state = gst_rtp_vorbis_depay_change_state;
90 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_vorbis_depay_process;
91 gstrtpbasedepayload_class->set_caps = gst_rtp_vorbis_depay_setcaps;
93 gst_element_class_add_static_pad_template (gstelement_class,
94 &gst_rtp_vorbis_depay_sink_template);
95 gst_element_class_add_static_pad_template (gstelement_class,
96 &gst_rtp_vorbis_depay_src_template);
98 gst_element_class_set_static_metadata (gstelement_class,
99 "RTP Vorbis depayloader", "Codec/Depayloader/Network/RTP",
100 "Extracts Vorbis Audio from RTP packets (RFC 5215)",
101 "Wim Taymans <wim.taymans@gmail.com>");
103 GST_DEBUG_CATEGORY_INIT (rtpvorbisdepay_debug, "rtpvorbisdepay", 0,
104 "Vorbis RTP Depayloader");
108 gst_rtp_vorbis_depay_init (GstRtpVorbisDepay * rtpvorbisdepay)
110 rtpvorbisdepay->adapter = gst_adapter_new ();
114 free_config (GstRtpVorbisConfig * conf)
116 g_list_free_full (conf->headers, (GDestroyNotify) gst_buffer_unref);
121 free_indents (GstRtpVorbisDepay * rtpvorbisdepay)
123 g_list_free_full (rtpvorbisdepay->configs, (GDestroyNotify) free_config);
124 rtpvorbisdepay->configs = NULL;
128 gst_rtp_vorbis_depay_finalize (GObject * object)
130 GstRtpVorbisDepay *rtpvorbisdepay = GST_RTP_VORBIS_DEPAY (object);
132 g_object_unref (rtpvorbisdepay->adapter);
134 G_OBJECT_CLASS (parent_class)->finalize (object);
137 /* takes ownership of confbuf */
139 gst_rtp_vorbis_depay_parse_configuration (GstRtpVorbisDepay * rtpvorbisdepay,
150 gst_buffer_map (confbuf, &map, GST_MAP_READ);
154 GST_DEBUG_OBJECT (rtpvorbisdepay, "config size %" G_GSIZE_FORMAT, size);
156 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
157 * | Number of packed headers |
158 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
159 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
162 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
164 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
165 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
167 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
172 num_headers = GST_READ_UINT32_BE (data);
177 GST_DEBUG_OBJECT (rtpvorbisdepay, "have %u headers", num_headers);
180 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
181 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
182 * | Ident | length ..
183 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
184 * .. | n. of headers | length1 | length2 ..
185 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
186 * .. | Identification Header ..
187 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
188 * .................................................................
189 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
190 * .. | Comment Header ..
191 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
192 * .................................................................
193 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
194 * .. Comment Header |
195 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
197 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
198 * .................................................................
199 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
201 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203 for (i = 0; i < num_headers; i++) {
207 GstRtpVorbisConfig *conf;
214 ident = (data[0] << 16) | (data[1] << 8) | data[2];
215 length = (data[3] << 8) | data[4];
221 GST_DEBUG_OBJECT (rtpvorbisdepay,
222 "header %d, ident 0x%08x, length %u, left %" G_GSIZE_FORMAT, i, ident,
225 /* FIXME check if we already got this ident */
227 /* length might also include count of following size fields */
228 if (size < length && size + 1 != length)
231 /* read header sizes we read 2 sizes, the third size (for which we allocate
232 * space) must be derived from the total packed header length. */
233 h_sizes = g_newa (guint, n_headers + 1);
234 for (j = 0; j < n_headers; j++) {
245 h_size = (h_size << 7) | (b & 0x7f);
247 GST_DEBUG_OBJECT (rtpvorbisdepay, "headers %d: size: %u", j, h_size);
255 /* last header length is the remaining space */
256 GST_DEBUG_OBJECT (rtpvorbisdepay, "last header size: %u", length);
259 GST_DEBUG_OBJECT (rtpvorbisdepay, "preparing headers");
260 conf = g_new0 (GstRtpVorbisConfig, 1);
263 for (j = 0; j <= n_headers; j++) {
268 if (j != n_headers || size + extra != h_size) {
272 /* otherwise means that overall length field contained total length,
273 * including extra fields */
278 GST_DEBUG_OBJECT (rtpvorbisdepay, "reading header %d, size %u", j,
281 buf = gst_buffer_copy_region (confbuf, GST_BUFFER_COPY_ALL, offset,
283 conf->headers = g_list_append (conf->headers, buf);
287 rtpvorbisdepay->configs = g_list_append (rtpvorbisdepay->configs, conf);
290 gst_buffer_unmap (confbuf, &map);
291 gst_buffer_unref (confbuf);
298 GST_DEBUG_OBJECT (rtpvorbisdepay, "configuration too small");
299 gst_buffer_unmap (confbuf, &map);
300 gst_buffer_unref (confbuf);
306 gst_rtp_vorbis_depay_parse_inband_configuration (GstRtpVorbisDepay *
307 rtpvorbisdepay, guint ident, guint8 * configuration, guint size,
313 if (G_UNLIKELY (size < 4))
316 /* transform inline to out-of-band and parse that one */
317 confbuf = gst_buffer_new_and_alloc (size + 9);
318 gst_buffer_map (confbuf, &map, GST_MAP_WRITE);
320 GST_WRITE_UINT32_BE (map.data, 1);
322 GST_WRITE_UINT24_BE (map.data + 4, ident);
323 /* write sort-of-length */
324 GST_WRITE_UINT16_BE (map.data + 7, length);
326 memcpy (map.data + 9, configuration, size);
327 gst_buffer_unmap (confbuf, &map);
329 return gst_rtp_vorbis_depay_parse_configuration (rtpvorbisdepay, confbuf);
333 gst_rtp_vorbis_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
335 GstStructure *structure;
336 GstRtpVorbisDepay *rtpvorbisdepay;
338 const gchar *configuration;
342 rtpvorbisdepay = GST_RTP_VORBIS_DEPAY (depayload);
344 structure = gst_caps_get_structure (caps, 0);
347 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
350 /* read and parse configuration string */
351 configuration = gst_structure_get_string (structure, "configuration");
357 /* deserialize base64 to buffer */
358 data = g_base64_decode (configuration, &size);
360 confbuf = gst_buffer_new ();
361 gst_buffer_append_memory (confbuf,
362 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
363 if (!gst_rtp_vorbis_depay_parse_configuration (rtpvorbisdepay, confbuf))
364 goto invalid_configuration;
366 GST_WARNING_OBJECT (rtpvorbisdepay, "no configuration specified");
369 /* caps seem good, configure element */
370 depayload->clock_rate = clock_rate;
372 /* set caps on pad and on header */
373 srccaps = gst_caps_new_empty_simple ("audio/x-vorbis");
374 res = gst_pad_set_caps (depayload->srcpad, srccaps);
375 gst_caps_unref (srccaps);
380 invalid_configuration:
382 GST_ERROR_OBJECT (rtpvorbisdepay, "invalid configuration specified");
387 GST_ERROR_OBJECT (rtpvorbisdepay, "no clock-rate specified");
393 gst_rtp_vorbis_depay_switch_codebook (GstRtpVorbisDepay * rtpvorbisdepay,
397 gboolean res = FALSE;
399 GST_DEBUG_OBJECT (rtpvorbisdepay, "Looking up code book ident 0x%08x", ident);
400 for (walk = rtpvorbisdepay->configs; walk; walk = g_list_next (walk)) {
401 GstRtpVorbisConfig *conf = (GstRtpVorbisConfig *) walk->data;
403 if (conf->ident == ident) {
406 /* FIXME, remove pads, create new pad.. */
408 /* push out all the headers */
409 for (headers = conf->headers; headers; headers = g_list_next (headers)) {
410 GstBuffer *header = GST_BUFFER_CAST (headers->data);
412 gst_buffer_ref (header);
413 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpvorbisdepay),
416 /* remember the current config */
417 rtpvorbisdepay->config = conf;
422 /* we don't know about the headers, figure out an alternative method for
423 * getting the codebooks. FIXME, fail for now. */
429 gst_rtp_vorbis_depay_process (GstRTPBaseDepayload * depayload,
432 GstRtpVorbisDepay *rtpvorbisdepay;
436 GstBuffer *payload_buffer = NULL;
439 guint32 header, ident;
440 guint8 F, VDT, packets;
443 rtpvorbisdepay = GST_RTP_VORBIS_DEPAY (depayload);
445 payload_len = gst_rtp_buffer_get_payload_len (rtp);
447 GST_DEBUG_OBJECT (depayload, "got RTP packet of size %d", payload_len);
449 /* we need at least 4 bytes for the packet header */
450 if (G_UNLIKELY (payload_len < 4))
453 payload = gst_rtp_buffer_get_payload (rtp);
454 header = GST_READ_UINT32_BE (payload);
457 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
458 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
459 * | Ident | F |VDT|# pkts.|
460 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
462 * F: Fragment type (0=none, 1=start, 2=cont, 3=end)
463 * VDT: Vorbis data type (0=vorbis, 1=config, 2=comment, 3=reserved)
464 * pkts: number of packets.
466 VDT = (header & 0x30) >> 4;
467 if (G_UNLIKELY (VDT == 3))
468 goto ignore_reserved;
470 GST_DEBUG_OBJECT (depayload, "header: 0x%08x", header);
471 ident = (header >> 8) & 0xffffff;
472 F = (header & 0xc0) >> 6;
473 packets = (header & 0xf);
476 gboolean do_switch = FALSE;
478 /* we have a raw payload, find the codebook for the ident */
479 if (!rtpvorbisdepay->config) {
480 /* we don't have an active codebook, find the codebook and
482 GST_DEBUG_OBJECT (rtpvorbisdepay, "No active codebook, switching");
484 } else if (rtpvorbisdepay->config->ident != ident) {
485 /* codebook changed */
486 GST_DEBUG_OBJECT (rtpvorbisdepay, "codebook changed, switching");
490 if (!gst_rtp_vorbis_depay_switch_codebook (rtpvorbisdepay, ident))
495 GST_DEBUG_OBJECT (depayload, "ident: %u, F: %d, VDT: %d, packets: %d", ident,
498 /* fragmented packets, assemble */
503 /* if we start a packet, clear adapter and start assembling. */
504 gst_adapter_clear (rtpvorbisdepay->adapter);
505 GST_DEBUG_OBJECT (depayload, "start assemble");
506 rtpvorbisdepay->assembling = TRUE;
509 if (!rtpvorbisdepay->assembling)
512 /* skip header and length. */
513 vdata = gst_rtp_buffer_get_payload_subbuffer (rtp, 6, -1);
515 GST_DEBUG_OBJECT (depayload, "assemble vorbis packet");
516 gst_adapter_push (rtpvorbisdepay->adapter, vdata);
518 /* packet is not complete, we are done */
522 /* construct assembled buffer */
523 length = gst_adapter_available (rtpvorbisdepay->adapter);
524 payload_buffer = gst_adapter_take_buffer (rtpvorbisdepay->adapter, length);
526 payload_buffer = gst_rtp_buffer_get_payload_subbuffer (rtp, 4, -1);
530 GST_DEBUG_OBJECT (depayload, "assemble done");
532 gst_buffer_map (payload_buffer, &map, GST_MAP_READ);
534 payload_len = map.size;
536 /* we not assembling anymore now */
537 rtpvorbisdepay->assembling = FALSE;
538 gst_adapter_clear (rtpvorbisdepay->adapter);
540 /* payload now points to a length with that many vorbis data bytes.
541 * Iterate over the packets and send them out.
544 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
545 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
546 * | length | vorbis data ..
547 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
549 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
550 * | length | next vorbis packet data ..
551 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
553 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+*
555 while (payload_len > 2) {
556 /* If length is not 0, we have a reassembled packet for which we
557 * calculated the length already and don't have to skip over the
558 * length field anymore
561 length = GST_READ_UINT16_BE (payload);
566 GST_DEBUG_OBJECT (depayload, "read length %u, avail: %d", length,
569 /* skip packet if something odd happens */
570 if (G_UNLIKELY (length > payload_len))
573 /* handle in-band configuration */
574 if (G_UNLIKELY (VDT == 1)) {
575 GST_DEBUG_OBJECT (rtpvorbisdepay, "in-band configuration");
576 if (!gst_rtp_vorbis_depay_parse_inband_configuration (rtpvorbisdepay,
577 ident, payload, payload_len, length))
578 goto invalid_configuration;
582 /* create buffer for packet */
584 gst_buffer_copy_region (payload_buffer, GST_BUFFER_COPY_ALL,
585 payload - map.data, length);
588 payload_len -= length;
589 /* make sure to read next length */
592 ret = gst_rtp_base_depayload_push (depayload, outbuf);
593 if (ret != GST_FLOW_OK)
597 gst_buffer_unmap (payload_buffer, &map);
598 gst_buffer_unref (payload_buffer);
604 if (payload_buffer) {
605 gst_buffer_unmap (payload_buffer, &map);
606 gst_buffer_unref (payload_buffer);
613 GST_ELEMENT_WARNING (rtpvorbisdepay, STREAM, DECODE,
614 (NULL), ("Could not switch codebooks"));
619 GST_ELEMENT_WARNING (rtpvorbisdepay, STREAM, DECODE,
620 (NULL), ("Packet was too short (%d < 4)", payload_len));
625 GST_WARNING_OBJECT (rtpvorbisdepay, "reserved VDT ignored");
630 GST_ELEMENT_WARNING (rtpvorbisdepay, STREAM, DECODE,
631 (NULL), ("Packet contains invalid data"));
632 if (payload_buffer) {
633 gst_buffer_unmap (payload_buffer, &map);
634 gst_buffer_unref (payload_buffer);
638 invalid_configuration:
640 /* fatal, as we otherwise risk carrying on without output */
641 GST_ELEMENT_ERROR (rtpvorbisdepay, STREAM, DECODE,
642 (NULL), ("Packet contains invalid configuration"));
643 if (payload_buffer) {
644 gst_buffer_unmap (payload_buffer, &map);
645 gst_buffer_unref (payload_buffer);
651 static GstStateChangeReturn
652 gst_rtp_vorbis_depay_change_state (GstElement * element,
653 GstStateChange transition)
655 GstRtpVorbisDepay *rtpvorbisdepay;
656 GstStateChangeReturn ret;
658 rtpvorbisdepay = GST_RTP_VORBIS_DEPAY (element);
660 switch (transition) {
661 case GST_STATE_CHANGE_NULL_TO_READY:
663 case GST_STATE_CHANGE_READY_TO_PAUSED:
669 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
671 switch (transition) {
672 case GST_STATE_CHANGE_PAUSED_TO_READY:
673 free_indents (rtpvorbisdepay);
675 case GST_STATE_CHANGE_READY_TO_NULL:
684 gst_rtp_vorbis_depay_plugin_init (GstPlugin * plugin)
686 return gst_element_register (plugin, "rtpvorbisdepay",
687 GST_RANK_SECONDARY, GST_TYPE_RTP_VORBIS_DEPAY);