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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <gst/tag/tag.h>
25 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpvorbisdepay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpvorbisdepay_debug);
31 #define GST_CAT_DEFAULT (rtpvorbisdepay_debug)
34 * http://www.rfc-editor.org/rfc/rfc5215.txt
37 static GstStaticPadTemplate gst_rtp_vorbis_depay_sink_template =
38 GST_STATIC_PAD_TEMPLATE ("sink",
41 GST_STATIC_CAPS ("application/x-rtp, "
42 "media = (string) \"audio\", "
43 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
44 "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"VORBIS\""
45 /* All required parameters
47 * "encoding-params = (string) <num channels>"
48 * "configuration = (string) ANY"
53 static GstStaticPadTemplate gst_rtp_vorbis_depay_src_template =
54 GST_STATIC_PAD_TEMPLATE ("src",
57 GST_STATIC_CAPS ("audio/x-vorbis")
60 GST_BOILERPLATE (GstRtpVorbisDepay, gst_rtp_vorbis_depay, GstBaseRTPDepayload,
61 GST_TYPE_BASE_RTP_DEPAYLOAD);
63 static gboolean gst_rtp_vorbis_depay_setcaps (GstBaseRTPDepayload * depayload,
65 static GstBuffer *gst_rtp_vorbis_depay_process (GstBaseRTPDepayload * depayload,
68 static void gst_rtp_vorbis_depay_finalize (GObject * object);
70 static GstStateChangeReturn gst_rtp_vorbis_depay_change_state (GstElement *
71 element, GstStateChange transition);
75 gst_rtp_vorbis_depay_base_init (gpointer klass)
77 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
79 gst_element_class_add_static_pad_template (element_class,
80 &gst_rtp_vorbis_depay_sink_template);
81 gst_element_class_add_static_pad_template (element_class,
82 &gst_rtp_vorbis_depay_src_template);
84 gst_element_class_set_details_simple (element_class, "RTP Vorbis depayloader",
85 "Codec/Depayloader/Network/RTP",
86 "Extracts Vorbis Audio from RTP packets (RFC 5215)",
87 "Wim Taymans <wim.taymans@gmail.com>");
91 gst_rtp_vorbis_depay_class_init (GstRtpVorbisDepayClass * klass)
93 GObjectClass *gobject_class;
94 GstElementClass *gstelement_class;
95 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
97 gobject_class = (GObjectClass *) klass;
98 gstelement_class = (GstElementClass *) klass;
99 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
101 gobject_class->finalize = gst_rtp_vorbis_depay_finalize;
103 gstelement_class->change_state = gst_rtp_vorbis_depay_change_state;
105 gstbasertpdepayload_class->process = gst_rtp_vorbis_depay_process;
106 gstbasertpdepayload_class->set_caps = gst_rtp_vorbis_depay_setcaps;
108 GST_DEBUG_CATEGORY_INIT (rtpvorbisdepay_debug, "rtpvorbisdepay", 0,
109 "Vorbis RTP Depayloader");
113 gst_rtp_vorbis_depay_init (GstRtpVorbisDepay * rtpvorbisdepay,
114 GstRtpVorbisDepayClass * klass)
116 rtpvorbisdepay->adapter = gst_adapter_new ();
120 free_config (GstRtpVorbisConfig * conf)
124 for (headers = conf->headers; headers; headers = g_list_next (headers)) {
125 GstBuffer *header = GST_BUFFER_CAST (headers->data);
127 gst_buffer_unref (header);
129 g_list_free (conf->headers);
134 free_indents (GstRtpVorbisDepay * rtpvorbisdepay)
138 for (walk = rtpvorbisdepay->configs; walk; walk = g_list_next (walk)) {
139 free_config ((GstRtpVorbisConfig *) walk->data);
141 g_list_free (rtpvorbisdepay->configs);
142 rtpvorbisdepay->configs = NULL;
146 gst_rtp_vorbis_depay_finalize (GObject * object)
148 GstRtpVorbisDepay *rtpvorbisdepay = GST_RTP_VORBIS_DEPAY (object);
150 g_object_unref (rtpvorbisdepay->adapter);
152 G_OBJECT_CLASS (parent_class)->finalize (object);
155 /* takes ownership of confbuf */
157 gst_rtp_vorbis_depay_parse_configuration (GstRtpVorbisDepay * rtpvorbisdepay,
167 data = GST_BUFFER_DATA (confbuf);
168 size = GST_BUFFER_SIZE (confbuf);
170 GST_DEBUG_OBJECT (rtpvorbisdepay, "config size %u", size);
172 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
173 * | Number of packed headers |
174 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
175 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
177 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
178 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
180 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
181 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
183 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
188 num_headers = GST_READ_UINT32_BE (data);
193 GST_DEBUG_OBJECT (rtpvorbisdepay, "have %u headers", num_headers);
196 * 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
197 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
198 * | Ident | length ..
199 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
200 * .. | n. of headers | length1 | length2 ..
201 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
202 * .. | Identification Header ..
203 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
204 * .................................................................
205 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
206 * .. | Comment Header ..
207 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
208 * .................................................................
209 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
210 * .. Comment Header |
211 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
213 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
214 * .................................................................
215 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219 for (i = 0; i < num_headers; i++) {
223 GstRtpVorbisConfig *conf;
230 ident = (data[0] << 16) | (data[1] << 8) | data[2];
231 length = (data[3] << 8) | data[4];
237 GST_DEBUG_OBJECT (rtpvorbisdepay,
238 "header %d, ident 0x%08x, length %u, left %u", i, ident, length, size);
240 /* FIXME check if we already got this ident */
242 /* length might also include count of following size fields */
243 if (size < length && size + 1 != length)
246 /* read header sizes we read 2 sizes, the third size (for which we allocate
247 * space) must be derived from the total packed header length. */
248 h_sizes = g_newa (guint, n_headers + 1);
249 for (j = 0; j < n_headers; j++) {
260 h_size = (h_size << 7) | (b & 0x7f);
262 GST_DEBUG_OBJECT (rtpvorbisdepay, "headers %d: size: %u", j, h_size);
270 /* last header length is the remaining space */
271 GST_DEBUG_OBJECT (rtpvorbisdepay, "last header size: %u", length);
274 GST_DEBUG_OBJECT (rtpvorbisdepay, "preparing headers");
275 conf = g_new0 (GstRtpVorbisConfig, 1);
278 for (j = 0; j <= n_headers; j++) {
283 if (j != n_headers || size + extra != h_size) {
287 /* otherwise means that overall length field contained total length,
288 * including extra fields */
293 GST_DEBUG_OBJECT (rtpvorbisdepay, "reading header %d, size %u", j,
296 buf = gst_buffer_create_sub (confbuf, offset, h_size);
297 conf->headers = g_list_append (conf->headers, buf);
301 rtpvorbisdepay->configs = g_list_append (rtpvorbisdepay->configs, conf);
303 gst_buffer_unref (confbuf);
310 GST_DEBUG_OBJECT (rtpvorbisdepay, "configuration too small");
311 gst_buffer_unref (confbuf);
317 gst_rtp_vorbis_depay_parse_inband_configuration (GstRtpVorbisDepay *
318 rtpvorbisdepay, guint ident, guint8 * configuration, guint size,
324 if (G_UNLIKELY (size < 4))
327 /* transform inline to out-of-band and parse that one */
328 confbuf = gst_buffer_new_and_alloc (size + 9);
329 conf = GST_BUFFER_DATA (confbuf);
331 GST_WRITE_UINT32_BE (conf, 1);
333 GST_WRITE_UINT24_BE (conf + 4, ident);
334 /* write sort-of-length */
335 GST_WRITE_UINT16_BE (conf + 7, length);
337 memcpy (conf + 9, configuration, size);
339 return gst_rtp_vorbis_depay_parse_configuration (rtpvorbisdepay, confbuf);
343 gst_rtp_vorbis_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
345 GstStructure *structure;
346 GstRtpVorbisDepay *rtpvorbisdepay;
348 const gchar *configuration;
352 rtpvorbisdepay = GST_RTP_VORBIS_DEPAY (depayload);
354 structure = gst_caps_get_structure (caps, 0);
357 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
360 /* read and parse configuration string */
361 configuration = gst_structure_get_string (structure, "configuration");
367 /* deserialize base64 to buffer */
368 data = g_base64_decode (configuration, &size);
370 confbuf = gst_buffer_new ();
371 GST_BUFFER_DATA (confbuf) = data;
372 GST_BUFFER_MALLOCDATA (confbuf) = data;
373 GST_BUFFER_SIZE (confbuf) = size;
374 if (!gst_rtp_vorbis_depay_parse_configuration (rtpvorbisdepay, confbuf))
375 goto invalid_configuration;
377 GST_WARNING_OBJECT (rtpvorbisdepay, "no configuration specified");
380 /* caps seem good, configure element */
381 depayload->clock_rate = clock_rate;
383 /* set caps on pad and on header */
384 srccaps = gst_caps_new_simple ("audio/x-vorbis", NULL);
385 res = gst_pad_set_caps (depayload->srcpad, srccaps);
386 gst_caps_unref (srccaps);
391 invalid_configuration:
393 GST_ERROR_OBJECT (rtpvorbisdepay, "invalid configuration specified");
398 GST_ERROR_OBJECT (rtpvorbisdepay, "no clock-rate specified");
404 gst_rtp_vorbis_depay_switch_codebook (GstRtpVorbisDepay * rtpvorbisdepay,
408 gboolean res = FALSE;
410 GST_DEBUG_OBJECT (rtpvorbisdepay, "Looking up code book ident 0x%08x", ident);
411 for (walk = rtpvorbisdepay->configs; walk; walk = g_list_next (walk)) {
412 GstRtpVorbisConfig *conf = (GstRtpVorbisConfig *) walk->data;
414 if (conf->ident == ident) {
417 /* FIXME, remove pads, create new pad.. */
419 /* push out all the headers */
420 for (headers = conf->headers; headers; headers = g_list_next (headers)) {
421 GstBuffer *header = GST_BUFFER_CAST (headers->data);
423 gst_buffer_ref (header);
424 gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpvorbisdepay),
427 /* remember the current config */
428 rtpvorbisdepay->config = conf;
433 /* we don't know about the headers, figure out an alternative method for
434 * getting the codebooks. FIXME, fail for now. */
440 gst_rtp_vorbis_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
442 GstRtpVorbisDepay *rtpvorbisdepay;
446 guint8 *payload, *to_free = NULL;
448 guint32 header, ident;
449 guint8 F, VDT, packets;
451 rtpvorbisdepay = GST_RTP_VORBIS_DEPAY (depayload);
453 payload_len = gst_rtp_buffer_get_payload_len (buf);
455 GST_DEBUG_OBJECT (depayload, "got RTP packet of size %d", payload_len);
457 /* we need at least 4 bytes for the packet header */
458 if (G_UNLIKELY (payload_len < 4))
461 payload = gst_rtp_buffer_get_payload (buf);
463 header = GST_READ_UINT32_BE (payload);
466 * 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
467 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
468 * | Ident | F |VDT|# pkts.|
469 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
471 * F: Fragment type (0=none, 1=start, 2=cont, 3=end)
472 * VDT: Vorbis data type (0=vorbis, 1=config, 2=comment, 3=reserved)
473 * pkts: number of packets.
475 VDT = (header & 0x30) >> 4;
476 if (G_UNLIKELY (VDT == 3))
477 goto ignore_reserved;
479 GST_DEBUG_OBJECT (depayload, "header: 0x%08x", header);
480 ident = (header >> 8) & 0xffffff;
481 F = (header & 0xc0) >> 6;
482 packets = (header & 0xf);
485 gboolean do_switch = FALSE;
487 /* we have a raw payload, find the codebook for the ident */
488 if (!rtpvorbisdepay->config) {
489 /* we don't have an active codebook, find the codebook and
491 GST_DEBUG_OBJECT (rtpvorbisdepay, "No active codebook, switching");
493 } else if (rtpvorbisdepay->config->ident != ident) {
494 /* codebook changed */
495 GST_DEBUG_OBJECT (rtpvorbisdepay, "codebook changed, switching");
499 if (!gst_rtp_vorbis_depay_switch_codebook (rtpvorbisdepay, ident))
508 GST_DEBUG_OBJECT (depayload, "ident: %u, F: %d, VDT: %d, packets: %d", ident,
511 /* fragmented packets, assemble */
517 /* if we start a packet, clear adapter and start assembling. */
518 gst_adapter_clear (rtpvorbisdepay->adapter);
519 GST_DEBUG_OBJECT (depayload, "start assemble");
520 rtpvorbisdepay->assembling = TRUE;
523 if (!rtpvorbisdepay->assembling)
526 /* first assembled packet, reuse 2 bytes to store the length */
527 headerskip = (F == 1 ? 4 : 6);
528 /* skip header and length. */
529 vdata = gst_rtp_buffer_get_payload_subbuffer (buf, headerskip, -1);
531 GST_DEBUG_OBJECT (depayload, "assemble vorbis packet");
532 gst_adapter_push (rtpvorbisdepay->adapter, vdata);
534 /* packet is not complete, we are done */
538 /* construct assembled buffer */
539 payload_len = gst_adapter_available (rtpvorbisdepay->adapter);
540 payload = gst_adapter_take (rtpvorbisdepay->adapter, payload_len);
542 payload[0] = ((payload_len - 2) >> 8) & 0xff;
543 payload[1] = (payload_len - 2) & 0xff;
547 GST_DEBUG_OBJECT (depayload, "assemble done");
549 /* we not assembling anymore now */
550 rtpvorbisdepay->assembling = FALSE;
551 gst_adapter_clear (rtpvorbisdepay->adapter);
553 /* payload now points to a length with that many vorbis data bytes.
554 * Iterate over the packets and send them out.
557 * 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
558 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
559 * | length | vorbis data ..
560 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
562 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
563 * | length | next vorbis packet data ..
564 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
566 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+*
568 timestamp = gst_rtp_buffer_get_timestamp (buf);
570 while (payload_len > 2) {
573 length = GST_READ_UINT16_BE (payload);
577 GST_DEBUG_OBJECT (depayload, "read length %u, avail: %d", length,
580 /* skip packet if something odd happens */
581 if (G_UNLIKELY (length > payload_len))
584 /* handle in-band configuration */
585 if (G_UNLIKELY (VDT == 1)) {
586 GST_DEBUG_OBJECT (rtpvorbisdepay, "in-band configuration");
587 if (!gst_rtp_vorbis_depay_parse_inband_configuration (rtpvorbisdepay,
588 ident, payload, payload_len, length))
589 goto invalid_configuration;
593 /* create buffer for packet */
594 if (G_UNLIKELY (to_free)) {
595 outbuf = gst_buffer_new ();
596 GST_BUFFER_DATA (outbuf) = payload;
597 GST_BUFFER_MALLOCDATA (outbuf) = to_free;
598 GST_BUFFER_SIZE (outbuf) = length;
601 outbuf = gst_buffer_new_and_alloc (length);
602 memcpy (GST_BUFFER_DATA (outbuf), payload, length);
606 payload_len -= length;
609 /* push with timestamp of the last packet, which is the same timestamp that
610 * should apply to the first assembled packet. */
611 ret = gst_base_rtp_depayload_push_ts (depayload, timestamp, outbuf);
613 ret = gst_base_rtp_depayload_push (depayload, outbuf);
615 if (ret != GST_FLOW_OK)
618 /* make sure we don't set a timestamp on next buffers */
633 GST_ELEMENT_WARNING (rtpvorbisdepay, STREAM, DECODE,
634 (NULL), ("Could not switch codebooks"));
639 GST_ELEMENT_WARNING (rtpvorbisdepay, STREAM, DECODE,
640 (NULL), ("Packet was too short (%d < 4)", payload_len));
645 GST_WARNING_OBJECT (rtpvorbisdepay, "reserved VDT ignored");
650 GST_ELEMENT_WARNING (rtpvorbisdepay, STREAM, DECODE,
651 (NULL), ("Packet contains invalid data"));
654 invalid_configuration:
656 /* fatal, as we otherwise risk carrying on without output */
657 GST_ELEMENT_ERROR (rtpvorbisdepay, STREAM, DECODE,
658 (NULL), ("Packet contains invalid configuration"));
663 static GstStateChangeReturn
664 gst_rtp_vorbis_depay_change_state (GstElement * element,
665 GstStateChange transition)
667 GstRtpVorbisDepay *rtpvorbisdepay;
668 GstStateChangeReturn ret;
670 rtpvorbisdepay = GST_RTP_VORBIS_DEPAY (element);
672 switch (transition) {
673 case GST_STATE_CHANGE_NULL_TO_READY:
675 case GST_STATE_CHANGE_READY_TO_PAUSED:
681 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
683 switch (transition) {
684 case GST_STATE_CHANGE_PAUSED_TO_READY:
685 free_indents (rtpvorbisdepay);
687 case GST_STATE_CHANGE_READY_TO_NULL:
696 gst_rtp_vorbis_depay_plugin_init (GstPlugin * plugin)
698 return gst_element_register (plugin, "rtpvorbisdepay",
699 GST_RANK_SECONDARY, GST_TYPE_RTP_VORBIS_DEPAY);