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>
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 DEFAULT_CONFIG_INTERVAL 0
69 #define gst_rtp_vorbis_pay_parent_class parent_class
70 G_DEFINE_TYPE (GstRtpVorbisPay, gst_rtp_vorbis_pay, GST_TYPE_RTP_BASE_PAYLOAD);
72 static gboolean gst_rtp_vorbis_pay_setcaps (GstRTPBasePayload * basepayload,
74 static GstStateChangeReturn gst_rtp_vorbis_pay_change_state (GstElement *
75 element, GstStateChange transition);
76 static GstFlowReturn gst_rtp_vorbis_pay_handle_buffer (GstRTPBasePayload * pad,
78 static gboolean gst_rtp_vorbis_pay_sink_event (GstRTPBasePayload * payload,
81 static gboolean gst_rtp_vorbis_pay_parse_id (GstRTPBasePayload * basepayload,
82 guint8 * data, guint size);
83 static gboolean gst_rtp_vorbis_pay_finish_headers (GstRTPBasePayload *
86 static void gst_rtp_vorbis_pay_set_property (GObject * object, guint prop_id,
87 const GValue * value, GParamSpec * pspec);
88 static void gst_rtp_vorbis_pay_get_property (GObject * object, guint prop_id,
89 GValue * value, GParamSpec * pspec);
92 gst_rtp_vorbis_pay_class_init (GstRtpVorbisPayClass * klass)
94 GObjectClass *gobject_class;
95 GstElementClass *gstelement_class;
96 GstRTPBasePayloadClass *gstrtpbasepayload_class;
98 gobject_class = (GObjectClass *) klass;
99 gstelement_class = (GstElementClass *) klass;
100 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
102 gstelement_class->change_state = gst_rtp_vorbis_pay_change_state;
104 gstrtpbasepayload_class->set_caps = gst_rtp_vorbis_pay_setcaps;
105 gstrtpbasepayload_class->handle_buffer = gst_rtp_vorbis_pay_handle_buffer;
106 gstrtpbasepayload_class->sink_event = gst_rtp_vorbis_pay_sink_event;
108 gobject_class->set_property = gst_rtp_vorbis_pay_set_property;
109 gobject_class->get_property = gst_rtp_vorbis_pay_get_property;
111 gst_element_class_add_pad_template (gstelement_class,
112 gst_static_pad_template_get (&gst_rtp_vorbis_pay_src_template));
113 gst_element_class_add_pad_template (gstelement_class,
114 gst_static_pad_template_get (&gst_rtp_vorbis_pay_sink_template));
116 gst_element_class_set_static_metadata (gstelement_class,
117 "RTP Vorbis depayloader",
118 "Codec/Payloader/Network/RTP",
119 "Payload-encode Vorbis audio into RTP packets (RFC 5215)",
120 "Wim Taymans <wimi.taymans@gmail.com>");
122 GST_DEBUG_CATEGORY_INIT (rtpvorbispay_debug, "rtpvorbispay", 0,
123 "Vorbis RTP Payloader");
125 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONFIG_INTERVAL,
126 g_param_spec_uint ("config-interval", "Config Send Interval",
127 "Send Config Insertion Interval in seconds (configuration headers "
128 "will be multiplexed in the data stream when detected.) (0 = disabled)",
129 0, 3600, DEFAULT_CONFIG_INTERVAL,
130 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
135 gst_rtp_vorbis_pay_init (GstRtpVorbisPay * rtpvorbispay)
137 rtpvorbispay->last_config = GST_CLOCK_TIME_NONE;
141 gst_rtp_vorbis_pay_clear_packet (GstRtpVorbisPay * rtpvorbispay)
143 if (rtpvorbispay->packet)
144 gst_buffer_unref (rtpvorbispay->packet);
145 rtpvorbispay->packet = NULL;
149 gst_rtp_vorbis_pay_cleanup (GstRtpVorbisPay * rtpvorbispay)
151 g_list_foreach (rtpvorbispay->headers, (GFunc) gst_mini_object_unref, NULL);
152 g_list_free (rtpvorbispay->headers);
153 rtpvorbispay->headers = NULL;
155 gst_rtp_vorbis_pay_clear_packet (rtpvorbispay);
157 if (rtpvorbispay->config_data)
158 g_free (rtpvorbispay->config_data);
159 rtpvorbispay->config_data = NULL;
160 rtpvorbispay->last_config = GST_CLOCK_TIME_NONE;
164 gst_rtp_vorbis_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
166 GstRtpVorbisPay *rtpvorbispay;
173 rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
175 s = gst_caps_get_structure (caps, 0);
177 rtpvorbispay->need_headers = TRUE;
179 if ((array = gst_structure_get_value (s, "streamheader")) == NULL)
182 if (G_VALUE_TYPE (array) != GST_TYPE_ARRAY)
185 if ((asize = gst_value_array_get_size (array)) < 3)
188 for (i = 0; i < asize; i++) {
191 value = gst_value_array_get_value (array, i);
192 if ((buf = gst_value_get_buffer (value)) == NULL)
195 gst_buffer_map (buf, &map, GST_MAP_READ);
197 goto invalid_streamheader;
199 /* no data packets allowed */
200 if ((map.data[0] & 1) == 0)
201 goto invalid_streamheader;
203 /* we need packets with id 1, 3, 5 */
204 if (map.data[0] != (i * 2) + 1)
205 goto invalid_streamheader;
208 /* identification, we need to parse this in order to get the clock rate. */
209 if (G_UNLIKELY (!gst_rtp_vorbis_pay_parse_id (basepayload, map.data,
211 goto parse_id_failed;
213 GST_DEBUG_OBJECT (rtpvorbispay, "collecting header %d", i);
214 rtpvorbispay->headers =
215 g_list_append (rtpvorbispay->headers, gst_buffer_ref (buf));
216 gst_buffer_unmap (buf, &map);
218 if (!gst_rtp_vorbis_pay_finish_headers (basepayload))
227 GST_WARNING_OBJECT (rtpvorbispay, "streamheader with null buffer received");
230 invalid_streamheader:
232 GST_WARNING_OBJECT (rtpvorbispay, "unable to parse initial header");
233 gst_buffer_unmap (buf, &map);
238 GST_WARNING_OBJECT (rtpvorbispay, "unable to parse initial header");
239 gst_buffer_unmap (buf, &map);
244 GST_WARNING_OBJECT (rtpvorbispay, "unable to finish headers");
250 gst_rtp_vorbis_pay_reset_packet (GstRtpVorbisPay * rtpvorbispay, guint8 VDT)
253 GstRTPBuffer rtp = { NULL };
255 GST_LOG_OBJECT (rtpvorbispay, "reset packet");
257 rtpvorbispay->payload_pos = 4;
258 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_READ, &rtp);
259 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
260 gst_rtp_buffer_unmap (&rtp);
261 rtpvorbispay->payload_left = payload_len - 4;
262 rtpvorbispay->payload_duration = 0;
263 rtpvorbispay->payload_F = 0;
264 rtpvorbispay->payload_VDT = VDT;
265 rtpvorbispay->payload_pkts = 0;
269 gst_rtp_vorbis_pay_init_packet (GstRtpVorbisPay * rtpvorbispay, guint8 VDT,
270 GstClockTime timestamp)
272 GST_LOG_OBJECT (rtpvorbispay, "starting new packet, VDT: %d", VDT);
274 if (rtpvorbispay->packet)
275 gst_buffer_unref (rtpvorbispay->packet);
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_TIMESTAMP (rtpvorbispay->packet) = timestamp;
287 gst_rtp_vorbis_pay_flush_packet (GstRtpVorbisPay * rtpvorbispay)
292 GstRTPBuffer rtp = { NULL };
294 /* check for empty packet */
295 if (!rtpvorbispay->packet || rtpvorbispay->payload_pos <= 4)
298 GST_LOG_OBJECT (rtpvorbispay, "flushing packet");
300 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
303 payload = gst_rtp_buffer_get_payload (&rtp);
306 * 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
307 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
308 * | Ident | F |VDT|# pkts.|
309 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
311 * F: Fragment type (0=none, 1=start, 2=cont, 3=end)
312 * VDT: Vorbis data type (0=vorbis, 1=config, 2=comment, 3=reserved)
313 * pkts: number of packets.
315 payload[0] = (rtpvorbispay->payload_ident >> 16) & 0xff;
316 payload[1] = (rtpvorbispay->payload_ident >> 8) & 0xff;
317 payload[2] = (rtpvorbispay->payload_ident) & 0xff;
318 payload[3] = (rtpvorbispay->payload_F & 0x3) << 6 |
319 (rtpvorbispay->payload_VDT & 0x3) << 4 |
320 (rtpvorbispay->payload_pkts & 0xf);
322 gst_rtp_buffer_unmap (&rtp);
324 /* shrink the buffer size to the last written byte */
325 hlen = gst_rtp_buffer_calc_header_len (0);
326 gst_buffer_resize (rtpvorbispay->packet, 0, hlen + rtpvorbispay->payload_pos);
328 GST_BUFFER_DURATION (rtpvorbispay->packet) = rtpvorbispay->payload_duration;
330 /* push, this gives away our ref to the packet, so clear it. */
332 gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpvorbispay),
333 rtpvorbispay->packet);
334 rtpvorbispay->packet = NULL;
340 gst_rtp_vorbis_pay_finish_headers (GstRTPBasePayload * basepayload)
342 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
344 guint length, size, n_headers, configlen, extralen;
345 gchar *cstr, *configuration;
346 guint8 *data, *config;
350 GST_DEBUG_OBJECT (rtpvorbispay, "finish headers");
352 if (!rtpvorbispay->headers)
355 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
356 * | Number of packed headers |
357 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
358 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
360 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
361 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
363 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
364 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
366 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
368 * We only construct a config containing 1 packed header like this:
371 * 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
372 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
373 * | Ident | length ..
374 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
375 * .. | n. of headers | length1 | length2 ..
376 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
377 * .. | Identification Header ..
378 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
379 * .................................................................
380 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
381 * .. | Comment Header ..
382 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
383 * .................................................................
384 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
385 * .. Comment Header |
386 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
388 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
389 * .................................................................
390 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
392 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
395 /* we need 4 bytes for the number of headers (which is always 1), 3 bytes for
396 * the ident, 2 bytes for length, 1 byte for n. of headers. */
397 size = 4 + 3 + 2 + 1;
399 /* count the size of the headers first and update the hash */
402 ident = fnv1_hash_32_new ();
404 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
405 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
409 bsize = gst_buffer_get_size (buf);
413 /* count number of bytes needed for length fields, we don't need this for
414 * the last header. */
415 if (g_list_next (walk)) {
423 gst_buffer_map (buf, &map, GST_MAP_READ);
424 ident = fnv1_hash_32_update (ident, map.data, map.size);
425 gst_buffer_unmap (buf, &map);
428 /* packet length is header size + packet length */
429 configlen = size + length;
430 config = data = g_malloc (configlen);
432 /* number of packed headers, we only pack 1 header */
438 ident = fnv1_hash_32_to_24 (ident);
439 rtpvorbispay->payload_ident = ident;
440 GST_DEBUG_OBJECT (rtpvorbispay, "ident 0x%08x", ident);
442 /* take lower 3 bytes */
443 data[4] = (ident >> 16) & 0xff;
444 data[5] = (ident >> 8) & 0xff;
445 data[6] = ident & 0xff;
447 /* store length of all vorbis headers */
448 data[7] = ((length) >> 8) & 0xff;
449 data[8] = (length) & 0xff;
451 /* store number of headers minus one. */
452 data[9] = n_headers - 1;
455 /* store length for each header */
456 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
457 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
458 guint bsize, size, temp;
461 /* only need to store the length when it's not the last header */
462 if (!g_list_next (walk))
465 bsize = gst_buffer_get_size (buf);
475 bsize = gst_buffer_get_size (buf);
476 /* write the size backwards */
480 data[size] = (bsize & 0x7f) | flag;
482 flag = 0x80; /* Flag bit on all bytes of the length except the last */
487 /* copy header data */
488 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
489 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
491 gst_buffer_extract (buf, 0, data, gst_buffer_get_size (buf));
492 data += gst_buffer_get_size (buf);
493 gst_buffer_unref (buf);
495 g_list_free (rtpvorbispay->headers);
496 rtpvorbispay->headers = NULL;
497 rtpvorbispay->need_headers = FALSE;
499 /* serialize to base64 */
500 configuration = g_base64_encode (config, configlen);
502 /* store for later re-sending */
503 if (rtpvorbispay->config_data)
504 g_free (rtpvorbispay->config_data);
505 rtpvorbispay->config_size = configlen - 4 - 3 - 2;
506 rtpvorbispay->config_data = g_malloc (rtpvorbispay->config_size);
507 rtpvorbispay->config_extra_len = extralen;
508 memcpy (rtpvorbispay->config_data, config + 4 + 3 + 2,
509 rtpvorbispay->config_size);
513 /* configure payloader settings */
514 cstr = g_strdup_printf ("%d", rtpvorbispay->channels);
515 gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "VORBIS",
518 gst_rtp_base_payload_set_outcaps (basepayload, "encoding-params",
519 G_TYPE_STRING, cstr, "configuration", G_TYPE_STRING, configuration, NULL);
521 g_free (configuration);
528 GST_DEBUG_OBJECT (rtpvorbispay, "finish headers");
534 gst_rtp_vorbis_pay_parse_id (GstRTPBasePayload * basepayload, guint8 * data,
537 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
539 gint32 rate, version;
541 if (G_UNLIKELY (size < 16))
544 if (G_UNLIKELY (memcmp (data, "\001vorbis", 7)))
548 if (G_UNLIKELY ((version = GST_READ_UINT32_LE (data)) != 0))
549 goto invalid_version;
552 if (G_UNLIKELY ((channels = *data++) < 1))
553 goto invalid_channels;
555 if (G_UNLIKELY ((rate = GST_READ_UINT32_LE (data)) < 1))
558 /* all fine, store the values */
559 rtpvorbispay->channels = channels;
560 rtpvorbispay->rate = rate;
567 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
568 ("Identification packet is too short, need at least 16, got %d", size),
574 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
575 ("Invalid header start in identification packet"), (NULL));
580 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
581 ("Invalid version, expected 0, got %d", version), (NULL));
586 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
587 ("Invalid rate %d", rate), (NULL));
592 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
593 ("Invalid channels %d", channels), (NULL));
599 gst_rtp_vorbis_pay_payload_buffer (GstRtpVorbisPay * rtpvorbispay, guint8 VDT,
600 guint8 * data, guint size, GstClockTime timestamp, GstClockTime duration,
603 GstFlowReturn ret = GST_FLOW_OK;
606 GstClockTime newduration;
609 guint8 *ppos, *payload;
611 GstRTPBuffer rtp = { NULL };
613 /* size increases with packet length and 2 bytes size eader. */
614 newduration = rtpvorbispay->payload_duration;
615 if (duration != GST_CLOCK_TIME_NONE)
616 newduration += duration;
618 newsize = rtpvorbispay->payload_pos + 2 + size;
619 packet_len = gst_rtp_buffer_calc_packet_len (newsize, 0, 0);
621 /* check buffer filled against length and max latency */
622 flush = gst_rtp_base_payload_is_filled (GST_RTP_BASE_PAYLOAD (rtpvorbispay),
623 packet_len, newduration);
624 /* we can store up to 15 vorbis packets in one RTP packet. */
625 flush |= (rtpvorbispay->payload_pkts == 15);
626 /* flush if we have a new VDT */
627 if (rtpvorbispay->packet)
628 flush |= (rtpvorbispay->payload_VDT != VDT);
630 ret = gst_rtp_vorbis_pay_flush_packet (rtpvorbispay);
632 /* create new packet if we must */
633 if (!rtpvorbispay->packet) {
634 gst_rtp_vorbis_pay_init_packet (rtpvorbispay, VDT, timestamp);
637 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
638 payload = gst_rtp_buffer_get_payload (&rtp);
639 ppos = payload + rtpvorbispay->payload_pos;
642 /* put buffer in packet, it either fits completely or needs to be fragmented
643 * over multiple RTP packets. */
645 plen = MIN (rtpvorbispay->payload_left - 2, size);
647 GST_LOG_OBJECT (rtpvorbispay, "append %u bytes", plen);
649 /* data is copied in the payload with a 2 byte length header */
650 ppos[0] = ((plen - not_in_length) >> 8) & 0xff;
651 ppos[1] = ((plen - not_in_length) & 0xff);
653 memcpy (&ppos[2], data, plen);
655 /* only first (only) configuration cuts length field */
656 /* NOTE: spec (if any) is not clear on this ... */
662 rtpvorbispay->payload_pos += plen + 2;
663 rtpvorbispay->payload_left -= plen + 2;
667 /* last fragment, set F to 0x3. */
668 rtpvorbispay->payload_F = 0x3;
670 /* fragment continues, set F to 0x2. */
671 rtpvorbispay->payload_F = 0x2;
674 /* fragmented packet starts, set F to 0x1, mark ourselves as
676 rtpvorbispay->payload_F = 0x1;
681 gst_rtp_buffer_unmap (&rtp);
682 /* fragmented packets are always flushed and have ptks of 0 */
683 rtpvorbispay->payload_pkts = 0;
684 ret = gst_rtp_vorbis_pay_flush_packet (rtpvorbispay);
687 /* start new packet and get pointers. VDT stays the same. */
688 gst_rtp_vorbis_pay_init_packet (rtpvorbispay,
689 rtpvorbispay->payload_VDT, timestamp);
690 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
691 payload = gst_rtp_buffer_get_payload (&rtp);
692 ppos = payload + rtpvorbispay->payload_pos;
695 /* unfragmented packet, update stats for next packet, size == 0 and we
696 * exit the while loop */
697 rtpvorbispay->payload_pkts++;
698 if (duration != GST_CLOCK_TIME_NONE)
699 rtpvorbispay->payload_duration += duration;
704 gst_rtp_buffer_unmap (&rtp);
710 gst_rtp_vorbis_pay_handle_buffer (GstRTPBasePayload * basepayload,
713 GstRtpVorbisPay *rtpvorbispay;
718 GstClockTime duration, timestamp;
721 rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
723 gst_buffer_map (buffer, &map, GST_MAP_READ);
726 duration = GST_BUFFER_DURATION (buffer);
727 timestamp = GST_BUFFER_TIMESTAMP (buffer);
729 GST_LOG_OBJECT (rtpvorbispay, "size %" G_GSIZE_FORMAT
730 ", duration %" GST_TIME_FORMAT, size, GST_TIME_ARGS (duration));
732 if (G_UNLIKELY (size < 1))
735 /* find packet type */
739 /* identification, we need to parse this in order to get the clock rate. */
740 if (G_UNLIKELY (!gst_rtp_vorbis_pay_parse_id (basepayload, data, size)))
741 goto parse_id_failed;
743 } else if (data[0] == 3) {
746 } else if (data[0] == 5) {
755 /* we need to collect the headers and construct a config string from them */
757 GST_DEBUG_OBJECT (rtpvorbispay, "collecting header");
758 /* append header to the list of headers */
759 gst_buffer_unmap (buffer, &map);
760 rtpvorbispay->headers = g_list_append (rtpvorbispay->headers, buffer);
763 } else if (rtpvorbispay->headers) {
764 if (rtpvorbispay->need_headers) {
765 if (!gst_rtp_vorbis_pay_finish_headers (basepayload))
768 g_list_free_full (rtpvorbispay->headers,
769 (GDestroyNotify) gst_buffer_unref);
770 rtpvorbispay->headers = NULL;
774 /* there is a config request, see if we need to insert it */
775 if (rtpvorbispay->config_interval > 0 && rtpvorbispay->config_data) {
776 gboolean send_config = FALSE;
778 if (rtpvorbispay->last_config != -1) {
781 GST_LOG_OBJECT (rtpvorbispay,
782 "now %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
783 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpvorbispay->last_config));
785 /* calculate diff between last config in milliseconds */
786 if (timestamp > rtpvorbispay->last_config) {
787 diff = timestamp - rtpvorbispay->last_config;
792 GST_DEBUG_OBJECT (rtpvorbispay,
793 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
795 /* bigger than interval, queue config */
796 /* FIXME should convert timestamps to running time */
797 if (GST_TIME_AS_SECONDS (diff) >= rtpvorbispay->config_interval) {
798 GST_DEBUG_OBJECT (rtpvorbispay, "time to send config");
802 /* no known previous config time, send now */
803 GST_DEBUG_OBJECT (rtpvorbispay, "no previous config time, send now");
808 /* we need to send config now first */
809 /* different TDT type forces flush */
810 gst_rtp_vorbis_pay_payload_buffer (rtpvorbispay, 1,
811 rtpvorbispay->config_data, rtpvorbispay->config_size,
812 timestamp, GST_CLOCK_TIME_NONE, rtpvorbispay->config_extra_len);
814 if (timestamp != -1) {
815 rtpvorbispay->last_config = timestamp;
820 ret = gst_rtp_vorbis_pay_payload_buffer (rtpvorbispay, VDT, data, size,
821 timestamp, duration, 0);
823 gst_buffer_unmap (buffer, &map);
824 gst_buffer_unref (buffer);
832 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
833 ("Invalid packet size (1 < %" G_GSIZE_FORMAT ")", size), (NULL));
834 gst_buffer_unmap (buffer, &map);
835 gst_buffer_unref (buffer);
840 gst_buffer_unmap (buffer, &map);
841 gst_buffer_unref (buffer);
842 return GST_FLOW_ERROR;
846 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
847 (NULL), ("Ignoring unknown header received"));
848 gst_buffer_unmap (buffer, &map);
849 gst_buffer_unref (buffer);
854 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
855 (NULL), ("Error initializing header config"));
856 gst_buffer_unmap (buffer, &map);
857 gst_buffer_unref (buffer);
863 gst_rtp_vorbis_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
865 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (payload);
867 switch (GST_EVENT_TYPE (event)) {
868 case GST_EVENT_FLUSH_STOP:
869 gst_rtp_vorbis_pay_clear_packet (rtpvorbispay);
874 /* false to let parent handle event as well */
875 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
878 static GstStateChangeReturn
879 gst_rtp_vorbis_pay_change_state (GstElement * element,
880 GstStateChange transition)
882 GstRtpVorbisPay *rtpvorbispay;
883 GstStateChangeReturn ret;
885 rtpvorbispay = GST_RTP_VORBIS_PAY (element);
887 switch (transition) {
892 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
894 switch (transition) {
895 case GST_STATE_CHANGE_PAUSED_TO_READY:
896 gst_rtp_vorbis_pay_cleanup (rtpvorbispay);
905 gst_rtp_vorbis_pay_set_property (GObject * object, guint prop_id,
906 const GValue * value, GParamSpec * pspec)
908 GstRtpVorbisPay *rtpvorbispay;
910 rtpvorbispay = GST_RTP_VORBIS_PAY (object);
913 case PROP_CONFIG_INTERVAL:
914 rtpvorbispay->config_interval = g_value_get_uint (value);
922 gst_rtp_vorbis_pay_get_property (GObject * object, guint prop_id,
923 GValue * value, GParamSpec * pspec)
925 GstRtpVorbisPay *rtpvorbispay;
927 rtpvorbispay = GST_RTP_VORBIS_PAY (object);
930 case PROP_CONFIG_INTERVAL:
931 g_value_set_uint (value, rtpvorbispay->config_interval);
939 gst_rtp_vorbis_pay_plugin_init (GstPlugin * plugin)
941 return gst_element_register (plugin, "rtpvorbispay",
942 GST_RANK_SECONDARY, GST_TYPE_RTP_VORBIS_PAY);