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.
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/audio/audio.h>
30 #include "gstrtpvorbispay.h"
31 #include "gstrtputils.h"
33 GST_DEBUG_CATEGORY_STATIC (rtpvorbispay_debug);
34 #define GST_CAT_DEFAULT (rtpvorbispay_debug)
37 * http://www.rfc-editor.org/rfc/rfc5215.txt
40 static GstStaticPadTemplate gst_rtp_vorbis_pay_src_template =
41 GST_STATIC_PAD_TEMPLATE ("src",
44 GST_STATIC_CAPS ("application/x-rtp, "
45 "media = (string) \"audio\", "
46 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
47 "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"VORBIS\""
48 /* All required parameters
50 * "encoding-params = (string) <num channels>"
51 * "configuration = (string) ANY"
56 static GstStaticPadTemplate gst_rtp_vorbis_pay_sink_template =
57 GST_STATIC_PAD_TEMPLATE ("sink",
60 GST_STATIC_CAPS ("audio/x-vorbis")
63 #define DEFAULT_CONFIG_INTERVAL 0
71 #define gst_rtp_vorbis_pay_parent_class parent_class
72 G_DEFINE_TYPE (GstRtpVorbisPay, gst_rtp_vorbis_pay, GST_TYPE_RTP_BASE_PAYLOAD);
74 static gboolean gst_rtp_vorbis_pay_setcaps (GstRTPBasePayload * basepayload,
76 static GstStateChangeReturn gst_rtp_vorbis_pay_change_state (GstElement *
77 element, GstStateChange transition);
78 static GstFlowReturn gst_rtp_vorbis_pay_handle_buffer (GstRTPBasePayload * pad,
80 static gboolean gst_rtp_vorbis_pay_sink_event (GstRTPBasePayload * payload,
83 static gboolean gst_rtp_vorbis_pay_parse_id (GstRTPBasePayload * basepayload,
84 guint8 * data, guint size);
85 static gboolean gst_rtp_vorbis_pay_finish_headers (GstRTPBasePayload *
88 static void gst_rtp_vorbis_pay_set_property (GObject * object, guint prop_id,
89 const GValue * value, GParamSpec * pspec);
90 static void gst_rtp_vorbis_pay_get_property (GObject * object, guint prop_id,
91 GValue * value, GParamSpec * pspec);
94 gst_rtp_vorbis_pay_class_init (GstRtpVorbisPayClass * klass)
96 GObjectClass *gobject_class;
97 GstElementClass *gstelement_class;
98 GstRTPBasePayloadClass *gstrtpbasepayload_class;
100 gobject_class = (GObjectClass *) klass;
101 gstelement_class = (GstElementClass *) klass;
102 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
104 gstelement_class->change_state = gst_rtp_vorbis_pay_change_state;
106 gstrtpbasepayload_class->set_caps = gst_rtp_vorbis_pay_setcaps;
107 gstrtpbasepayload_class->handle_buffer = gst_rtp_vorbis_pay_handle_buffer;
108 gstrtpbasepayload_class->sink_event = gst_rtp_vorbis_pay_sink_event;
110 gobject_class->set_property = gst_rtp_vorbis_pay_set_property;
111 gobject_class->get_property = gst_rtp_vorbis_pay_get_property;
113 gst_element_class_add_static_pad_template (gstelement_class,
114 &gst_rtp_vorbis_pay_src_template);
115 gst_element_class_add_static_pad_template (gstelement_class,
116 &gst_rtp_vorbis_pay_sink_template);
118 gst_element_class_set_static_metadata (gstelement_class,
119 "RTP Vorbis payloader",
120 "Codec/Payloader/Network/RTP",
121 "Payload-encode Vorbis audio into RTP packets (RFC 5215)",
122 "Wim Taymans <wim.taymans@gmail.com>");
124 GST_DEBUG_CATEGORY_INIT (rtpvorbispay_debug, "rtpvorbispay", 0,
125 "Vorbis RTP Payloader");
127 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONFIG_INTERVAL,
128 g_param_spec_uint ("config-interval", "Config Send Interval",
129 "Send Config Insertion Interval in seconds (configuration headers "
130 "will be multiplexed in the data stream when detected.) (0 = disabled)",
131 0, 3600, DEFAULT_CONFIG_INTERVAL,
132 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
137 gst_rtp_vorbis_pay_init (GstRtpVorbisPay * rtpvorbispay)
139 rtpvorbispay->last_config = GST_CLOCK_TIME_NONE;
143 gst_rtp_vorbis_pay_clear_packet (GstRtpVorbisPay * rtpvorbispay)
145 if (rtpvorbispay->packet)
146 gst_buffer_unref (rtpvorbispay->packet);
147 rtpvorbispay->packet = NULL;
148 g_list_free_full (rtpvorbispay->packet_buffers,
149 (GDestroyNotify) gst_buffer_unref);
150 rtpvorbispay->packet_buffers = NULL;
154 gst_rtp_vorbis_pay_cleanup (GstRtpVorbisPay * rtpvorbispay)
156 gst_rtp_vorbis_pay_clear_packet (rtpvorbispay);
157 g_list_free_full (rtpvorbispay->headers, (GDestroyNotify) gst_buffer_unref);
158 rtpvorbispay->headers = NULL;
159 g_free (rtpvorbispay->config_data);
160 rtpvorbispay->config_data = NULL;
161 rtpvorbispay->last_config = GST_CLOCK_TIME_NONE;
165 gst_rtp_vorbis_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
167 GstRtpVorbisPay *rtpvorbispay;
174 rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
176 s = gst_caps_get_structure (caps, 0);
178 rtpvorbispay->need_headers = TRUE;
180 if ((array = gst_structure_get_value (s, "streamheader")) == NULL)
183 if (G_VALUE_TYPE (array) != GST_TYPE_ARRAY)
186 if ((asize = gst_value_array_get_size (array)) < 3)
189 for (i = 0; i < asize; i++) {
192 value = gst_value_array_get_value (array, i);
193 if ((buf = gst_value_get_buffer (value)) == NULL)
196 gst_buffer_map (buf, &map, GST_MAP_READ);
198 goto invalid_streamheader;
200 /* no data packets allowed */
201 if ((map.data[0] & 1) == 0)
202 goto invalid_streamheader;
204 /* we need packets with id 1, 3, 5 */
205 if (map.data[0] != (i * 2) + 1)
206 goto invalid_streamheader;
209 /* identification, we need to parse this in order to get the clock rate. */
210 if (G_UNLIKELY (!gst_rtp_vorbis_pay_parse_id (basepayload, map.data,
212 goto parse_id_failed;
214 GST_DEBUG_OBJECT (rtpvorbispay, "collecting header %d", i);
215 rtpvorbispay->headers =
216 g_list_append (rtpvorbispay->headers, gst_buffer_ref (buf));
217 gst_buffer_unmap (buf, &map);
219 if (!gst_rtp_vorbis_pay_finish_headers (basepayload))
228 GST_WARNING_OBJECT (rtpvorbispay, "streamheader with null buffer received");
231 invalid_streamheader:
233 GST_WARNING_OBJECT (rtpvorbispay, "unable to parse initial header");
234 gst_buffer_unmap (buf, &map);
239 GST_WARNING_OBJECT (rtpvorbispay, "unable to parse initial header");
240 gst_buffer_unmap (buf, &map);
245 GST_WARNING_OBJECT (rtpvorbispay, "unable to finish headers");
251 gst_rtp_vorbis_pay_reset_packet (GstRtpVorbisPay * rtpvorbispay, guint8 VDT)
254 GstRTPBuffer rtp = { NULL };
256 GST_LOG_OBJECT (rtpvorbispay, "reset packet");
258 rtpvorbispay->payload_pos = 4;
259 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_READ, &rtp);
260 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
261 gst_rtp_buffer_unmap (&rtp);
262 rtpvorbispay->payload_left = payload_len - 4;
263 rtpvorbispay->payload_duration = 0;
264 rtpvorbispay->payload_F = 0;
265 rtpvorbispay->payload_VDT = VDT;
266 rtpvorbispay->payload_pkts = 0;
270 gst_rtp_vorbis_pay_init_packet (GstRtpVorbisPay * rtpvorbispay, guint8 VDT,
271 GstClockTime timestamp)
273 GST_LOG_OBJECT (rtpvorbispay, "starting new packet, VDT: %d", VDT);
275 gst_rtp_vorbis_pay_clear_packet (rtpvorbispay);
277 /* new packet allocate max packet size */
278 rtpvorbispay->packet =
279 gst_rtp_buffer_new_allocate_len (GST_RTP_BASE_PAYLOAD_MTU
280 (rtpvorbispay), 0, 0);
281 gst_rtp_vorbis_pay_reset_packet (rtpvorbispay, VDT);
283 GST_BUFFER_PTS (rtpvorbispay->packet) = timestamp;
287 gst_rtp_vorbis_pay_flush_packet (GstRtpVorbisPay * rtpvorbispay)
292 GstRTPBuffer rtp = { NULL };
295 /* check for empty packet */
296 if (!rtpvorbispay->packet || rtpvorbispay->payload_pos <= 4)
299 GST_LOG_OBJECT (rtpvorbispay, "flushing packet");
301 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
304 payload = gst_rtp_buffer_get_payload (&rtp);
307 * 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
308 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
309 * | Ident | F |VDT|# pkts.|
310 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
312 * F: Fragment type (0=none, 1=start, 2=cont, 3=end)
313 * VDT: Vorbis data type (0=vorbis, 1=config, 2=comment, 3=reserved)
314 * pkts: number of packets.
316 payload[0] = (rtpvorbispay->payload_ident >> 16) & 0xff;
317 payload[1] = (rtpvorbispay->payload_ident >> 8) & 0xff;
318 payload[2] = (rtpvorbispay->payload_ident) & 0xff;
319 payload[3] = (rtpvorbispay->payload_F & 0x3) << 6 |
320 (rtpvorbispay->payload_VDT & 0x3) << 4 |
321 (rtpvorbispay->payload_pkts & 0xf);
323 gst_rtp_buffer_unmap (&rtp);
325 /* shrink the buffer size to the last written byte */
326 hlen = gst_rtp_buffer_calc_header_len (0);
327 gst_buffer_resize (rtpvorbispay->packet, 0, hlen + rtpvorbispay->payload_pos);
329 GST_BUFFER_DURATION (rtpvorbispay->packet) = rtpvorbispay->payload_duration;
331 for (l = g_list_last (rtpvorbispay->packet_buffers); l; l = l->prev) {
332 GstBuffer *buf = GST_BUFFER_CAST (l->data);
333 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpvorbispay), rtpvorbispay->packet,
334 buf, g_quark_from_static_string (GST_META_TAG_AUDIO_STR));
335 gst_buffer_unref (buf);
337 g_list_free (rtpvorbispay->packet_buffers);
338 rtpvorbispay->packet_buffers = NULL;
340 /* push, this gives away our ref to the packet, so clear it. */
342 gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpvorbispay),
343 rtpvorbispay->packet);
344 rtpvorbispay->packet = NULL;
350 gst_rtp_vorbis_pay_finish_headers (GstRTPBasePayload * basepayload)
352 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
354 guint length, size, n_headers, configlen, extralen;
355 gchar *cstr, *configuration;
356 guint8 *data, *config;
360 GST_DEBUG_OBJECT (rtpvorbispay, "finish headers");
362 if (!rtpvorbispay->headers)
365 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
366 * | Number of packed headers |
367 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
368 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
370 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
371 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
373 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
374 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
376 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
378 * We only construct a config containing 1 packed header like this:
381 * 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
382 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
383 * | Ident | length ..
384 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
385 * .. | n. of headers | length1 | length2 ..
386 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
387 * .. | Identification Header ..
388 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
389 * .................................................................
390 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
391 * .. | Comment Header ..
392 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
393 * .................................................................
394 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
395 * .. Comment Header |
396 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
398 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
399 * .................................................................
400 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
402 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
405 /* we need 4 bytes for the number of headers (which is always 1), 3 bytes for
406 * the ident, 2 bytes for length, 1 byte for n. of headers. */
407 size = 4 + 3 + 2 + 1;
409 /* count the size of the headers first and update the hash */
412 ident = fnv1_hash_32_new ();
414 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
415 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
419 bsize = gst_buffer_get_size (buf);
423 /* count number of bytes needed for length fields, we don't need this for
424 * the last header. */
425 if (g_list_next (walk)) {
433 gst_buffer_map (buf, &map, GST_MAP_READ);
434 ident = fnv1_hash_32_update (ident, map.data, map.size);
435 gst_buffer_unmap (buf, &map);
438 /* packet length is header size + packet length */
439 configlen = size + length;
440 config = data = g_malloc (configlen);
442 /* number of packed headers, we only pack 1 header */
448 ident = fnv1_hash_32_to_24 (ident);
449 rtpvorbispay->payload_ident = ident;
450 GST_DEBUG_OBJECT (rtpvorbispay, "ident 0x%08x", ident);
452 /* take lower 3 bytes */
453 data[4] = (ident >> 16) & 0xff;
454 data[5] = (ident >> 8) & 0xff;
455 data[6] = ident & 0xff;
457 /* store length of all vorbis headers */
458 data[7] = ((length) >> 8) & 0xff;
459 data[8] = (length) & 0xff;
461 /* store number of headers minus one. */
462 data[9] = n_headers - 1;
465 /* store length for each header */
466 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
467 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
468 guint bsize, size, temp;
471 /* only need to store the length when it's not the last header */
472 if (!g_list_next (walk))
475 bsize = gst_buffer_get_size (buf);
485 bsize = gst_buffer_get_size (buf);
486 /* write the size backwards */
490 data[size] = (bsize & 0x7f) | flag;
492 flag = 0x80; /* Flag bit on all bytes of the length except the last */
497 /* copy header data */
498 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
499 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
501 gst_buffer_extract (buf, 0, data, gst_buffer_get_size (buf));
502 data += gst_buffer_get_size (buf);
504 rtpvorbispay->need_headers = FALSE;
506 /* serialize to base64 */
507 configuration = g_base64_encode (config, configlen);
509 /* store for later re-sending */
510 g_free (rtpvorbispay->config_data);
511 rtpvorbispay->config_size = configlen - 4 - 3 - 2;
512 rtpvorbispay->config_data = g_malloc (rtpvorbispay->config_size);
513 rtpvorbispay->config_extra_len = extralen;
514 memcpy (rtpvorbispay->config_data, config + 4 + 3 + 2,
515 rtpvorbispay->config_size);
519 /* configure payloader settings */
520 cstr = g_strdup_printf ("%d", rtpvorbispay->channels);
521 gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "VORBIS",
524 gst_rtp_base_payload_set_outcaps (basepayload, "encoding-params",
525 G_TYPE_STRING, cstr, "configuration", G_TYPE_STRING, configuration, NULL);
527 g_free (configuration);
534 GST_DEBUG_OBJECT (rtpvorbispay, "finish headers");
540 gst_rtp_vorbis_pay_parse_id (GstRTPBasePayload * basepayload, guint8 * data,
543 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
545 gint32 rate, version;
547 if (G_UNLIKELY (size < 16))
550 if (G_UNLIKELY (memcmp (data, "\001vorbis", 7)))
554 if (G_UNLIKELY ((version = GST_READ_UINT32_LE (data)) != 0))
555 goto invalid_version;
558 if (G_UNLIKELY ((channels = *data++) < 1))
559 goto invalid_channels;
561 if (G_UNLIKELY ((rate = GST_READ_UINT32_LE (data)) < 1))
564 /* all fine, store the values */
565 rtpvorbispay->channels = channels;
566 rtpvorbispay->rate = rate;
573 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
574 ("Identification packet is too short, need at least 16, got %d", size),
580 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
581 ("Invalid header start in identification packet"), (NULL));
586 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
587 ("Invalid version, expected 0, got %d", version), (NULL));
592 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
593 ("Invalid rate %d", rate), (NULL));
598 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
599 ("Invalid channels %d", channels), (NULL));
605 gst_rtp_vorbis_pay_payload_buffer (GstRtpVorbisPay * rtpvorbispay, guint8 VDT,
606 GstBuffer * buffer, guint8 * data, guint size, GstClockTime timestamp,
607 GstClockTime duration, guint not_in_length)
609 GstFlowReturn ret = GST_FLOW_OK;
612 GstClockTime newduration;
615 guint8 *ppos, *payload;
617 GstRTPBuffer rtp = { NULL };
619 /* size increases with packet length and 2 bytes size eader. */
620 newduration = rtpvorbispay->payload_duration;
621 if (duration != GST_CLOCK_TIME_NONE)
622 newduration += duration;
624 newsize = rtpvorbispay->payload_pos + 2 + size;
625 packet_len = gst_rtp_buffer_calc_packet_len (newsize, 0, 0);
627 /* check buffer filled against length and max latency */
628 flush = gst_rtp_base_payload_is_filled (GST_RTP_BASE_PAYLOAD (rtpvorbispay),
629 packet_len, newduration);
630 /* we can store up to 15 vorbis packets in one RTP packet. */
631 flush |= (rtpvorbispay->payload_pkts == 15);
632 /* flush if we have a new VDT */
633 if (rtpvorbispay->packet)
634 flush |= (rtpvorbispay->payload_VDT != VDT);
636 ret = gst_rtp_vorbis_pay_flush_packet (rtpvorbispay);
638 if (ret != GST_FLOW_OK)
641 /* create new packet if we must */
642 if (!rtpvorbispay->packet) {
643 gst_rtp_vorbis_pay_init_packet (rtpvorbispay, VDT, timestamp);
646 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
647 payload = gst_rtp_buffer_get_payload (&rtp);
648 ppos = payload + rtpvorbispay->payload_pos;
651 /* put buffer in packet, it either fits completely or needs to be fragmented
652 * over multiple RTP packets. */
654 plen = MIN (rtpvorbispay->payload_left - 2, size);
656 GST_LOG_OBJECT (rtpvorbispay, "append %u bytes", plen);
658 /* data is copied in the payload with a 2 byte length header */
659 ppos[0] = ((plen - not_in_length) >> 8) & 0xff;
660 ppos[1] = ((plen - not_in_length) & 0xff);
662 memcpy (&ppos[2], data, plen);
665 if (!rtpvorbispay->packet_buffers
666 || rtpvorbispay->packet_buffers->data != (gpointer) buffer)
667 rtpvorbispay->packet_buffers =
668 g_list_prepend (rtpvorbispay->packet_buffers,
669 gst_buffer_ref (buffer));
673 for (l = rtpvorbispay->headers; l; l = l->next)
674 rtpvorbispay->packet_buffers =
675 g_list_prepend (rtpvorbispay->packet_buffers,
676 gst_buffer_ref (l->data));
679 /* only first (only) configuration cuts length field */
680 /* NOTE: spec (if any) is not clear on this ... */
686 rtpvorbispay->payload_pos += plen + 2;
687 rtpvorbispay->payload_left -= plen + 2;
691 /* last fragment, set F to 0x3. */
692 rtpvorbispay->payload_F = 0x3;
694 /* fragment continues, set F to 0x2. */
695 rtpvorbispay->payload_F = 0x2;
698 /* fragmented packet starts, set F to 0x1, mark ourselves as
700 rtpvorbispay->payload_F = 0x1;
705 gst_rtp_buffer_unmap (&rtp);
706 /* fragmented packets are always flushed and have ptks of 0 */
707 rtpvorbispay->payload_pkts = 0;
708 ret = gst_rtp_vorbis_pay_flush_packet (rtpvorbispay);
711 /* start new packet and get pointers. VDT stays the same. */
712 gst_rtp_vorbis_pay_init_packet (rtpvorbispay,
713 rtpvorbispay->payload_VDT, timestamp);
714 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
715 payload = gst_rtp_buffer_get_payload (&rtp);
716 ppos = payload + rtpvorbispay->payload_pos;
719 /* unfragmented packet, update stats for next packet, size == 0 and we
720 * exit the while loop */
721 rtpvorbispay->payload_pkts++;
722 if (duration != GST_CLOCK_TIME_NONE)
723 rtpvorbispay->payload_duration += duration;
725 } while (size && ret == GST_FLOW_OK);
728 gst_rtp_buffer_unmap (&rtp);
736 gst_rtp_vorbis_pay_handle_buffer (GstRTPBasePayload * basepayload,
739 GstRtpVorbisPay *rtpvorbispay;
744 GstClockTime duration, timestamp;
747 rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
749 gst_buffer_map (buffer, &map, GST_MAP_READ);
752 duration = GST_BUFFER_DURATION (buffer);
753 timestamp = GST_BUFFER_PTS (buffer);
755 GST_LOG_OBJECT (rtpvorbispay, "size %" G_GSIZE_FORMAT
756 ", duration %" GST_TIME_FORMAT, size, GST_TIME_ARGS (duration));
758 if (G_UNLIKELY (size < 1))
761 /* find packet type */
765 /* identification, we need to parse this in order to get the clock rate. */
766 if (G_UNLIKELY (!gst_rtp_vorbis_pay_parse_id (basepayload, data, size)))
767 goto parse_id_failed;
769 } else if (data[0] == 3) {
772 } else if (data[0] == 5) {
781 /* we need to collect the headers and construct a config string from them */
783 GST_DEBUG_OBJECT (rtpvorbispay, "collecting header");
784 /* append header to the list of headers */
785 gst_buffer_unmap (buffer, &map);
786 rtpvorbispay->headers = g_list_append (rtpvorbispay->headers, buffer);
789 } else if (rtpvorbispay->headers && rtpvorbispay->need_headers) {
790 if (!gst_rtp_vorbis_pay_finish_headers (basepayload))
794 /* there is a config request, see if we need to insert it */
795 if (rtpvorbispay->config_interval > 0 && rtpvorbispay->config_data) {
796 gboolean send_config = FALSE;
798 if (rtpvorbispay->last_config != -1) {
801 GST_LOG_OBJECT (rtpvorbispay,
802 "now %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
803 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpvorbispay->last_config));
805 /* calculate diff between last config in milliseconds */
806 if (timestamp > rtpvorbispay->last_config) {
807 diff = timestamp - rtpvorbispay->last_config;
812 GST_DEBUG_OBJECT (rtpvorbispay,
813 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
815 /* bigger than interval, queue config */
816 /* FIXME should convert timestamps to running time */
817 if (GST_TIME_AS_SECONDS (diff) >= rtpvorbispay->config_interval) {
818 GST_DEBUG_OBJECT (rtpvorbispay, "time to send config");
822 /* no known previous config time, send now */
823 GST_DEBUG_OBJECT (rtpvorbispay, "no previous config time, send now");
828 /* we need to send config now first */
829 /* different TDT type forces flush */
830 gst_rtp_vorbis_pay_payload_buffer (rtpvorbispay, 1,
831 NULL, rtpvorbispay->config_data, rtpvorbispay->config_size,
832 timestamp, GST_CLOCK_TIME_NONE, rtpvorbispay->config_extra_len);
834 if (timestamp != -1) {
835 rtpvorbispay->last_config = timestamp;
841 gst_rtp_vorbis_pay_payload_buffer (rtpvorbispay, VDT, buffer, data, size,
842 timestamp, duration, 0);
844 gst_buffer_unmap (buffer, &map);
845 gst_buffer_unref (buffer);
853 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
854 ("Invalid packet size (1 < %" G_GSIZE_FORMAT ")", size), (NULL));
855 gst_buffer_unmap (buffer, &map);
856 gst_buffer_unref (buffer);
861 gst_buffer_unmap (buffer, &map);
862 gst_buffer_unref (buffer);
863 return GST_FLOW_ERROR;
867 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
868 (NULL), ("Ignoring unknown header received"));
869 gst_buffer_unmap (buffer, &map);
870 gst_buffer_unref (buffer);
875 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
876 (NULL), ("Error initializing header config"));
877 gst_buffer_unmap (buffer, &map);
878 gst_buffer_unref (buffer);
884 gst_rtp_vorbis_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
886 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (payload);
888 switch (GST_EVENT_TYPE (event)) {
889 case GST_EVENT_FLUSH_STOP:
890 gst_rtp_vorbis_pay_clear_packet (rtpvorbispay);
895 /* false to let parent handle event as well */
896 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
899 static GstStateChangeReturn
900 gst_rtp_vorbis_pay_change_state (GstElement * element,
901 GstStateChange transition)
903 GstRtpVorbisPay *rtpvorbispay;
904 GstStateChangeReturn ret;
906 rtpvorbispay = GST_RTP_VORBIS_PAY (element);
908 switch (transition) {
913 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
915 switch (transition) {
916 case GST_STATE_CHANGE_PAUSED_TO_READY:
917 gst_rtp_vorbis_pay_cleanup (rtpvorbispay);
926 gst_rtp_vorbis_pay_set_property (GObject * object, guint prop_id,
927 const GValue * value, GParamSpec * pspec)
929 GstRtpVorbisPay *rtpvorbispay;
931 rtpvorbispay = GST_RTP_VORBIS_PAY (object);
934 case PROP_CONFIG_INTERVAL:
935 rtpvorbispay->config_interval = g_value_get_uint (value);
943 gst_rtp_vorbis_pay_get_property (GObject * object, guint prop_id,
944 GValue * value, GParamSpec * pspec)
946 GstRtpVorbisPay *rtpvorbispay;
948 rtpvorbispay = GST_RTP_VORBIS_PAY (object);
951 case PROP_CONFIG_INTERVAL:
952 g_value_set_uint (value, rtpvorbispay->config_interval);
960 gst_rtp_vorbis_pay_plugin_init (GstPlugin * plugin)
962 return gst_element_register (plugin, "rtpvorbispay",
963 GST_RANK_SECONDARY, GST_TYPE_RTP_VORBIS_PAY);