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.
26 #include <gst/rtp/gstrtpbuffer.h>
29 #include "gstrtpvorbispay.h"
31 GST_DEBUG_CATEGORY_STATIC (rtpvorbispay_debug);
32 #define GST_CAT_DEFAULT (rtpvorbispay_debug)
35 * http://www.rfc-editor.org/rfc/rfc5215.txt
38 static GstStaticPadTemplate gst_rtp_vorbis_pay_src_template =
39 GST_STATIC_PAD_TEMPLATE ("src",
42 GST_STATIC_CAPS ("application/x-rtp, "
43 "media = (string) \"audio\", "
44 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
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_pay_sink_template =
55 GST_STATIC_PAD_TEMPLATE ("sink",
58 GST_STATIC_CAPS ("audio/x-vorbis")
61 #define gst_rtp_vorbis_pay_parent_class parent_class
62 G_DEFINE_TYPE (GstRtpVorbisPay, gst_rtp_vorbis_pay, GST_TYPE_BASE_RTP_PAYLOAD);
64 static gboolean gst_rtp_vorbis_pay_setcaps (GstBaseRTPPayload * basepayload,
66 static GstStateChangeReturn gst_rtp_vorbis_pay_change_state (GstElement *
67 element, GstStateChange transition);
68 static GstFlowReturn gst_rtp_vorbis_pay_handle_buffer (GstBaseRTPPayload * pad,
70 static gboolean gst_rtp_vorbis_pay_handle_event (GstBaseRTPPayload * payload,
74 gst_rtp_vorbis_pay_class_init (GstRtpVorbisPayClass * klass)
76 GstElementClass *gstelement_class;
77 GstBaseRTPPayloadClass *gstbasertppayload_class;
79 gstelement_class = (GstElementClass *) klass;
80 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
82 gstelement_class->change_state = gst_rtp_vorbis_pay_change_state;
84 gstbasertppayload_class->set_caps = gst_rtp_vorbis_pay_setcaps;
85 gstbasertppayload_class->handle_buffer = gst_rtp_vorbis_pay_handle_buffer;
86 gstbasertppayload_class->handle_event = gst_rtp_vorbis_pay_handle_event;
88 gst_element_class_add_pad_template (gstelement_class,
89 gst_static_pad_template_get (&gst_rtp_vorbis_pay_src_template));
90 gst_element_class_add_pad_template (gstelement_class,
91 gst_static_pad_template_get (&gst_rtp_vorbis_pay_sink_template));
93 gst_element_class_set_details_simple (gstelement_class,
94 "RTP Vorbis depayloader",
95 "Codec/Payloader/Network/RTP",
96 "Payload-encode Vorbis audio into RTP packets (RFC 5215)",
97 "Wim Taymans <wimi.taymans@gmail.com>");
99 GST_DEBUG_CATEGORY_INIT (rtpvorbispay_debug, "rtpvorbispay", 0,
100 "Vorbis RTP Payloader");
104 gst_rtp_vorbis_pay_init (GstRtpVorbisPay * rtpvorbispay)
106 /* needed because of GST_BOILERPLATE */
110 gst_rtp_vorbis_pay_clear_packet (GstRtpVorbisPay * rtpvorbispay)
112 if (rtpvorbispay->packet)
113 gst_buffer_unref (rtpvorbispay->packet);
114 rtpvorbispay->packet = NULL;
118 gst_rtp_vorbis_pay_cleanup (GstRtpVorbisPay * rtpvorbispay)
120 g_list_foreach (rtpvorbispay->headers, (GFunc) gst_mini_object_unref, NULL);
121 g_list_free (rtpvorbispay->headers);
122 rtpvorbispay->headers = NULL;
124 gst_rtp_vorbis_pay_clear_packet (rtpvorbispay);
128 gst_rtp_vorbis_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
130 GstRtpVorbisPay *rtpvorbispay;
132 rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
134 rtpvorbispay->need_headers = TRUE;
140 gst_rtp_vorbis_pay_reset_packet (GstRtpVorbisPay * rtpvorbispay, guint8 VDT)
145 GST_LOG_OBJECT (rtpvorbispay, "reset packet");
147 rtpvorbispay->payload_pos = 4;
148 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_READ, &rtp);
149 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
150 gst_rtp_buffer_unmap (&rtp);
151 rtpvorbispay->payload_left = payload_len - 4;
152 rtpvorbispay->payload_duration = 0;
153 rtpvorbispay->payload_F = 0;
154 rtpvorbispay->payload_VDT = VDT;
155 rtpvorbispay->payload_pkts = 0;
159 gst_rtp_vorbis_pay_init_packet (GstRtpVorbisPay * rtpvorbispay, guint8 VDT,
160 GstClockTime timestamp)
162 GST_LOG_OBJECT (rtpvorbispay, "starting new packet, VDT: %d", VDT);
164 if (rtpvorbispay->packet)
165 gst_buffer_unref (rtpvorbispay->packet);
167 /* new packet allocate max packet size */
168 rtpvorbispay->packet =
169 gst_rtp_buffer_new_allocate_len (GST_BASE_RTP_PAYLOAD_MTU
170 (rtpvorbispay), 0, 0);
171 gst_rtp_vorbis_pay_reset_packet (rtpvorbispay, VDT);
172 GST_BUFFER_TIMESTAMP (rtpvorbispay->packet) = timestamp;
176 gst_rtp_vorbis_pay_flush_packet (GstRtpVorbisPay * rtpvorbispay)
183 /* check for empty packet */
184 if (!rtpvorbispay->packet || rtpvorbispay->payload_pos <= 4)
187 GST_LOG_OBJECT (rtpvorbispay, "flushing packet");
189 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
192 payload = gst_rtp_buffer_get_payload (&rtp);
195 * 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
196 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
197 * | Ident | F |VDT|# pkts.|
198 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
200 * F: Fragment type (0=none, 1=start, 2=cont, 3=end)
201 * VDT: Vorbis data type (0=vorbis, 1=config, 2=comment, 3=reserved)
202 * pkts: number of packets.
204 payload[0] = (rtpvorbispay->payload_ident >> 16) & 0xff;
205 payload[1] = (rtpvorbispay->payload_ident >> 8) & 0xff;
206 payload[2] = (rtpvorbispay->payload_ident) & 0xff;
207 payload[3] = (rtpvorbispay->payload_F & 0x3) << 6 |
208 (rtpvorbispay->payload_VDT & 0x3) << 4 |
209 (rtpvorbispay->payload_pkts & 0xf);
211 gst_rtp_buffer_unmap (&rtp);
213 /* shrink the buffer size to the last written byte */
214 hlen = gst_rtp_buffer_calc_header_len (0);
215 gst_buffer_resize (rtpvorbispay->packet, 0, hlen + rtpvorbispay->payload_pos);
217 GST_BUFFER_DURATION (rtpvorbispay->packet) = rtpvorbispay->payload_duration;
219 /* push, this gives away our ref to the packet, so clear it. */
221 gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpvorbispay),
222 rtpvorbispay->packet);
223 rtpvorbispay->packet = NULL;
229 gst_rtp_vorbis_pay_finish_headers (GstBaseRTPPayload * basepayload)
231 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
233 guint length, size, n_headers, configlen;
234 gchar *cstr, *configuration;
235 guint8 *data, *config;
239 GST_DEBUG_OBJECT (rtpvorbispay, "finish headers");
241 if (!rtpvorbispay->headers)
244 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
245 * | Number of packed headers |
246 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
247 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
249 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
250 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
252 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
253 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
255 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
257 * We only construct a config containing 1 packed header like this:
260 * 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
261 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
262 * | Ident | length ..
263 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
264 * .. | n. of headers | length1 | length2 ..
265 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
266 * .. | Identification Header ..
267 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
268 * .................................................................
269 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
270 * .. | Comment Header ..
271 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
272 * .................................................................
273 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
274 * .. Comment Header |
275 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
277 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
278 * .................................................................
279 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
281 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
284 /* we need 4 bytes for the number of headers (which is always 1), 3 bytes for
285 * the ident, 2 bytes for length, 1 byte for n. of headers. */
286 size = 4 + 3 + 2 + 1;
288 /* count the size of the headers first and update the hash */
291 ident = fnv1_hash_32_new ();
292 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
293 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
297 bsize = osize = gst_buffer_get_size (buf);
301 /* count number of bytes needed for length fields, we don't need this for
302 * the last header. */
303 if (g_list_next (walk)) {
310 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_READ);
311 ident = fnv1_hash_32_update (ident, data, osize);
312 gst_buffer_unmap (buf, data, -1);
315 /* packet length is header size + packet length */
316 configlen = size + length;
317 config = data = g_malloc (configlen);
319 /* number of packed headers, we only pack 1 header */
325 ident = fnv1_hash_32_to_24 (ident);
326 rtpvorbispay->payload_ident = ident;
327 GST_DEBUG_OBJECT (rtpvorbispay, "ident 0x%08x", ident);
329 /* take lower 3 bytes */
330 data[4] = (ident >> 16) & 0xff;
331 data[5] = (ident >> 8) & 0xff;
332 data[6] = ident & 0xff;
334 /* store length of all vorbis headers */
335 data[7] = ((length) >> 8) & 0xff;
336 data[8] = (length) & 0xff;
338 /* store number of headers minus one. */
339 data[9] = n_headers - 1;
342 /* store length for each header */
343 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
344 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
345 guint bsize, size, temp;
348 /* only need to store the length when it's not the last header */
349 if (!g_list_next (walk))
352 bsize = gst_buffer_get_size (buf);
362 bsize = gst_buffer_get_size (buf);
363 /* write the size backwards */
367 data[size] = (bsize & 0x7f) | flag;
369 flag = 0x80; /* Flag bit on all bytes of the length except the last */
374 /* copy header data */
375 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
376 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
378 gst_buffer_extract (buf, 0, data, gst_buffer_get_size (buf));
379 data += gst_buffer_get_size (buf);
382 /* serialize to base64 */
383 configuration = g_base64_encode (config, configlen);
386 /* configure payloader settings */
387 cstr = g_strdup_printf ("%d", rtpvorbispay->channels);
388 gst_basertppayload_set_options (basepayload, "audio", TRUE, "VORBIS",
391 gst_basertppayload_set_outcaps (basepayload, "encoding-params",
392 G_TYPE_STRING, cstr, "configuration", G_TYPE_STRING, configuration, NULL);
394 g_free (configuration);
401 GST_DEBUG_OBJECT (rtpvorbispay, "finish headers");
407 gst_rtp_vorbis_pay_parse_id (GstBaseRTPPayload * basepayload, guint8 * data,
410 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
412 gint32 rate, version;
414 if (G_UNLIKELY (size < 16))
417 if (G_UNLIKELY (memcmp (data, "\001vorbis", 7)))
421 if (G_UNLIKELY ((version = GST_READ_UINT32_LE (data)) != 0))
422 goto invalid_version;
425 if (G_UNLIKELY ((channels = *data++) < 1))
426 goto invalid_channels;
428 if (G_UNLIKELY ((rate = GST_READ_UINT32_LE (data)) < 1))
431 /* all fine, store the values */
432 rtpvorbispay->channels = channels;
433 rtpvorbispay->rate = rate;
440 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
441 ("Identification packet is too short, need at least 16, got %d", size),
447 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
448 ("Invalid header start in identification packet"), (NULL));
453 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
454 ("Invalid version, expected 0, got %d", version), (NULL));
459 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
460 ("Invalid rate %d", rate), (NULL));
465 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
466 ("Invalid channels %d", channels), (NULL));
472 gst_rtp_vorbis_pay_handle_buffer (GstBaseRTPPayload * basepayload,
475 GstRtpVorbisPay *rtpvorbispay;
481 GstClockTime duration, newduration, timestamp;
485 guint8 *ppos, *payload;
489 rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
491 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
492 duration = GST_BUFFER_DURATION (buffer);
493 timestamp = GST_BUFFER_TIMESTAMP (buffer);
495 GST_LOG_OBJECT (rtpvorbispay, "size %" G_GSIZE_FORMAT
496 ", duration %" GST_TIME_FORMAT, size, GST_TIME_ARGS (duration));
498 if (G_UNLIKELY (size < 1 || size > 0xffff))
501 /* find packet type */
505 /* identification, we need to parse this in order to get the clock rate. */
506 if (G_UNLIKELY (!gst_rtp_vorbis_pay_parse_id (basepayload, data, size)))
507 goto parse_id_failed;
509 } else if (data[0] == 3) {
512 } else if (data[0] == 5) {
521 if (rtpvorbispay->need_headers) {
522 /* we need to collect the headers and construct a config string from them */
524 GST_DEBUG_OBJECT (rtpvorbispay, "collecting header");
525 /* append header to the list of headers */
526 gst_buffer_unmap (buffer, data, -1);
527 rtpvorbispay->headers = g_list_append (rtpvorbispay->headers, buffer);
531 if (!gst_rtp_vorbis_pay_finish_headers (basepayload))
533 rtpvorbispay->need_headers = FALSE;
537 /* size increases with packet length and 2 bytes size eader. */
538 newduration = rtpvorbispay->payload_duration;
539 if (duration != GST_CLOCK_TIME_NONE)
540 newduration += duration;
542 newsize = rtpvorbispay->payload_pos + 2 + size;
543 packet_len = gst_rtp_buffer_calc_packet_len (newsize, 0, 0);
545 /* check buffer filled against length and max latency */
546 flush = gst_basertppayload_is_filled (basepayload, packet_len, newduration);
547 /* we can store up to 15 vorbis packets in one RTP packet. */
548 flush |= (rtpvorbispay->payload_pkts == 15);
549 /* flush if we have a new VDT */
550 if (rtpvorbispay->packet)
551 flush |= (rtpvorbispay->payload_VDT != VDT);
553 gst_rtp_vorbis_pay_flush_packet (rtpvorbispay);
555 /* create new packet if we must */
556 if (!rtpvorbispay->packet) {
557 gst_rtp_vorbis_pay_init_packet (rtpvorbispay, VDT, timestamp);
560 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
561 payload = gst_rtp_buffer_get_payload (&rtp);
562 ppos = payload + rtpvorbispay->payload_pos;
567 /* put buffer in packet, it either fits completely or needs to be fragmented
568 * over multiple RTP packets. */
570 plen = MIN (rtpvorbispay->payload_left - 2, size);
572 GST_LOG_OBJECT (rtpvorbispay, "append %u bytes", plen);
574 /* data is copied in the payload with a 2 byte length header */
575 ppos[0] = (plen >> 8) & 0xff;
576 ppos[1] = (plen & 0xff);
577 memcpy (&ppos[2], data, plen);
582 rtpvorbispay->payload_pos += plen + 2;
583 rtpvorbispay->payload_left -= plen + 2;
587 /* last fragment, set F to 0x3. */
588 rtpvorbispay->payload_F = 0x3;
590 /* fragment continues, set F to 0x2. */
591 rtpvorbispay->payload_F = 0x2;
594 /* fragmented packet starts, set F to 0x1, mark ourselves as
596 rtpvorbispay->payload_F = 0x1;
601 gst_rtp_buffer_unmap (&rtp);
602 /* fragmented packets are always flushed and have ptks of 0 */
603 rtpvorbispay->payload_pkts = 0;
604 ret = gst_rtp_vorbis_pay_flush_packet (rtpvorbispay);
607 /* start new packet and get pointers. VDT stays the same. */
608 gst_rtp_vorbis_pay_init_packet (rtpvorbispay,
609 rtpvorbispay->payload_VDT, timestamp);
610 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
611 payload = gst_rtp_buffer_get_payload (&rtp);
612 ppos = payload + rtpvorbispay->payload_pos;
615 /* unfragmented packet, update stats for next packet, size == 0 and we
616 * exit the while loop */
617 rtpvorbispay->payload_pkts++;
618 if (duration != GST_CLOCK_TIME_NONE)
619 rtpvorbispay->payload_duration += duration;
624 gst_rtp_buffer_unmap (&rtp);
626 gst_buffer_unmap (buffer, data, -1);
627 gst_buffer_unref (buffer);
635 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
636 ("Invalid packet size (1 < %d <= 0xffff)", size), (NULL));
637 gst_buffer_unmap (buffer, data, -1);
638 gst_buffer_unref (buffer);
643 gst_buffer_unmap (buffer, data, -1);
644 gst_buffer_unref (buffer);
645 return GST_FLOW_ERROR;
649 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
650 (NULL), ("Ignoring unknown header received"));
651 gst_buffer_unmap (buffer, data, -1);
652 gst_buffer_unref (buffer);
657 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
658 (NULL), ("Error initializing header config"));
659 gst_buffer_unmap (buffer, data, -1);
660 gst_buffer_unref (buffer);
666 gst_rtp_vorbis_pay_handle_event (GstBaseRTPPayload * payload, GstEvent * event)
668 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (payload);
670 switch (GST_EVENT_TYPE (event)) {
671 case GST_EVENT_FLUSH_STOP:
672 gst_rtp_vorbis_pay_clear_packet (rtpvorbispay);
677 /* false to let parent handle event as well */
681 static GstStateChangeReturn
682 gst_rtp_vorbis_pay_change_state (GstElement * element,
683 GstStateChange transition)
685 GstRtpVorbisPay *rtpvorbispay;
686 GstStateChangeReturn ret;
688 rtpvorbispay = GST_RTP_VORBIS_PAY (element);
690 switch (transition) {
695 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
697 switch (transition) {
698 case GST_STATE_CHANGE_PAUSED_TO_READY:
699 gst_rtp_vorbis_pay_cleanup (rtpvorbispay);
708 gst_rtp_vorbis_pay_plugin_init (GstPlugin * plugin)
710 return gst_element_register (plugin, "rtpvorbispay",
711 GST_RANK_SECONDARY, GST_TYPE_RTP_VORBIS_PAY);