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_pad_template (gstelement_class,
114 gst_static_pad_template_get (&gst_rtp_vorbis_pay_src_template));
115 gst_element_class_add_pad_template (gstelement_class,
116 gst_static_pad_template_get (&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 if (rtpvorbispay->config_data)
160 g_free (rtpvorbispay->config_data);
161 rtpvorbispay->config_data = NULL;
162 rtpvorbispay->last_config = GST_CLOCK_TIME_NONE;
166 gst_rtp_vorbis_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
168 GstRtpVorbisPay *rtpvorbispay;
175 rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
177 s = gst_caps_get_structure (caps, 0);
179 rtpvorbispay->need_headers = TRUE;
181 if ((array = gst_structure_get_value (s, "streamheader")) == NULL)
184 if (G_VALUE_TYPE (array) != GST_TYPE_ARRAY)
187 if ((asize = gst_value_array_get_size (array)) < 3)
190 for (i = 0; i < asize; i++) {
193 value = gst_value_array_get_value (array, i);
194 if ((buf = gst_value_get_buffer (value)) == NULL)
197 gst_buffer_map (buf, &map, GST_MAP_READ);
199 goto invalid_streamheader;
201 /* no data packets allowed */
202 if ((map.data[0] & 1) == 0)
203 goto invalid_streamheader;
205 /* we need packets with id 1, 3, 5 */
206 if (map.data[0] != (i * 2) + 1)
207 goto invalid_streamheader;
210 /* identification, we need to parse this in order to get the clock rate. */
211 if (G_UNLIKELY (!gst_rtp_vorbis_pay_parse_id (basepayload, map.data,
213 goto parse_id_failed;
215 GST_DEBUG_OBJECT (rtpvorbispay, "collecting header %d", i);
216 rtpvorbispay->headers =
217 g_list_append (rtpvorbispay->headers, gst_buffer_ref (buf));
218 gst_buffer_unmap (buf, &map);
220 if (!gst_rtp_vorbis_pay_finish_headers (basepayload))
229 GST_WARNING_OBJECT (rtpvorbispay, "streamheader with null buffer received");
232 invalid_streamheader:
234 GST_WARNING_OBJECT (rtpvorbispay, "unable to parse initial header");
235 gst_buffer_unmap (buf, &map);
240 GST_WARNING_OBJECT (rtpvorbispay, "unable to parse initial header");
241 gst_buffer_unmap (buf, &map);
246 GST_WARNING_OBJECT (rtpvorbispay, "unable to finish headers");
252 gst_rtp_vorbis_pay_reset_packet (GstRtpVorbisPay * rtpvorbispay, guint8 VDT)
255 GstRTPBuffer rtp = { NULL };
257 GST_LOG_OBJECT (rtpvorbispay, "reset packet");
259 rtpvorbispay->payload_pos = 4;
260 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_READ, &rtp);
261 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
262 gst_rtp_buffer_unmap (&rtp);
263 rtpvorbispay->payload_left = payload_len - 4;
264 rtpvorbispay->payload_duration = 0;
265 rtpvorbispay->payload_F = 0;
266 rtpvorbispay->payload_VDT = VDT;
267 rtpvorbispay->payload_pkts = 0;
271 gst_rtp_vorbis_pay_init_packet (GstRtpVorbisPay * rtpvorbispay, guint8 VDT,
272 GstClockTime timestamp)
274 GST_LOG_OBJECT (rtpvorbispay, "starting new packet, VDT: %d", VDT);
276 gst_rtp_vorbis_pay_clear_packet (rtpvorbispay);
278 /* new packet allocate max packet size */
279 rtpvorbispay->packet =
280 gst_rtp_buffer_new_allocate_len (GST_RTP_BASE_PAYLOAD_MTU
281 (rtpvorbispay), 0, 0);
282 gst_rtp_vorbis_pay_reset_packet (rtpvorbispay, VDT);
284 GST_BUFFER_PTS (rtpvorbispay->packet) = timestamp;
288 gst_rtp_vorbis_pay_flush_packet (GstRtpVorbisPay * rtpvorbispay)
293 GstRTPBuffer rtp = { NULL };
296 /* check for empty packet */
297 if (!rtpvorbispay->packet || rtpvorbispay->payload_pos <= 4)
300 GST_LOG_OBJECT (rtpvorbispay, "flushing packet");
302 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
305 payload = gst_rtp_buffer_get_payload (&rtp);
308 * 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
309 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
310 * | Ident | F |VDT|# pkts.|
311 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
313 * F: Fragment type (0=none, 1=start, 2=cont, 3=end)
314 * VDT: Vorbis data type (0=vorbis, 1=config, 2=comment, 3=reserved)
315 * pkts: number of packets.
317 payload[0] = (rtpvorbispay->payload_ident >> 16) & 0xff;
318 payload[1] = (rtpvorbispay->payload_ident >> 8) & 0xff;
319 payload[2] = (rtpvorbispay->payload_ident) & 0xff;
320 payload[3] = (rtpvorbispay->payload_F & 0x3) << 6 |
321 (rtpvorbispay->payload_VDT & 0x3) << 4 |
322 (rtpvorbispay->payload_pkts & 0xf);
324 gst_rtp_buffer_unmap (&rtp);
326 /* shrink the buffer size to the last written byte */
327 hlen = gst_rtp_buffer_calc_header_len (0);
328 gst_buffer_resize (rtpvorbispay->packet, 0, hlen + rtpvorbispay->payload_pos);
330 GST_BUFFER_DURATION (rtpvorbispay->packet) = rtpvorbispay->payload_duration;
332 for (l = g_list_last (rtpvorbispay->packet_buffers); l; l = l->prev) {
333 GstBuffer *buf = GST_BUFFER_CAST (l->data);
334 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpvorbispay), rtpvorbispay->packet,
335 buf, g_quark_from_static_string (GST_META_TAG_AUDIO_STR));
336 gst_buffer_unref (buf);
338 g_list_free (rtpvorbispay->packet_buffers);
339 rtpvorbispay->packet_buffers = NULL;
341 /* push, this gives away our ref to the packet, so clear it. */
343 gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpvorbispay),
344 rtpvorbispay->packet);
345 rtpvorbispay->packet = NULL;
351 gst_rtp_vorbis_pay_finish_headers (GstRTPBasePayload * basepayload)
353 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
355 guint length, size, n_headers, configlen, extralen;
356 gchar *cstr, *configuration;
357 guint8 *data, *config;
361 GST_DEBUG_OBJECT (rtpvorbispay, "finish headers");
363 if (!rtpvorbispay->headers)
366 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
367 * | Number of packed headers |
368 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
369 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
371 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
372 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
374 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
375 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
377 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
379 * We only construct a config containing 1 packed header like this:
382 * 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
383 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
384 * | Ident | length ..
385 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
386 * .. | n. of headers | length1 | length2 ..
387 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
388 * .. | Identification Header ..
389 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
390 * .................................................................
391 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
392 * .. | Comment Header ..
393 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
394 * .................................................................
395 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
396 * .. Comment Header |
397 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
399 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
400 * .................................................................
401 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
403 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
406 /* we need 4 bytes for the number of headers (which is always 1), 3 bytes for
407 * the ident, 2 bytes for length, 1 byte for n. of headers. */
408 size = 4 + 3 + 2 + 1;
410 /* count the size of the headers first and update the hash */
413 ident = fnv1_hash_32_new ();
415 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
416 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
420 bsize = gst_buffer_get_size (buf);
424 /* count number of bytes needed for length fields, we don't need this for
425 * the last header. */
426 if (g_list_next (walk)) {
434 gst_buffer_map (buf, &map, GST_MAP_READ);
435 ident = fnv1_hash_32_update (ident, map.data, map.size);
436 gst_buffer_unmap (buf, &map);
439 /* packet length is header size + packet length */
440 configlen = size + length;
441 config = data = g_malloc (configlen);
443 /* number of packed headers, we only pack 1 header */
449 ident = fnv1_hash_32_to_24 (ident);
450 rtpvorbispay->payload_ident = ident;
451 GST_DEBUG_OBJECT (rtpvorbispay, "ident 0x%08x", ident);
453 /* take lower 3 bytes */
454 data[4] = (ident >> 16) & 0xff;
455 data[5] = (ident >> 8) & 0xff;
456 data[6] = ident & 0xff;
458 /* store length of all vorbis headers */
459 data[7] = ((length) >> 8) & 0xff;
460 data[8] = (length) & 0xff;
462 /* store number of headers minus one. */
463 data[9] = n_headers - 1;
466 /* store length for each header */
467 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
468 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
469 guint bsize, size, temp;
472 /* only need to store the length when it's not the last header */
473 if (!g_list_next (walk))
476 bsize = gst_buffer_get_size (buf);
486 bsize = gst_buffer_get_size (buf);
487 /* write the size backwards */
491 data[size] = (bsize & 0x7f) | flag;
493 flag = 0x80; /* Flag bit on all bytes of the length except the last */
498 /* copy header data */
499 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
500 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
502 gst_buffer_extract (buf, 0, data, gst_buffer_get_size (buf));
503 data += gst_buffer_get_size (buf);
505 rtpvorbispay->need_headers = FALSE;
507 /* serialize to base64 */
508 configuration = g_base64_encode (config, configlen);
510 /* store for later re-sending */
511 if (rtpvorbispay->config_data)
512 g_free (rtpvorbispay->config_data);
513 rtpvorbispay->config_size = configlen - 4 - 3 - 2;
514 rtpvorbispay->config_data = g_malloc (rtpvorbispay->config_size);
515 rtpvorbispay->config_extra_len = extralen;
516 memcpy (rtpvorbispay->config_data, config + 4 + 3 + 2,
517 rtpvorbispay->config_size);
521 /* configure payloader settings */
522 cstr = g_strdup_printf ("%d", rtpvorbispay->channels);
523 gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "VORBIS",
526 gst_rtp_base_payload_set_outcaps (basepayload, "encoding-params",
527 G_TYPE_STRING, cstr, "configuration", G_TYPE_STRING, configuration, NULL);
529 g_free (configuration);
536 GST_DEBUG_OBJECT (rtpvorbispay, "finish headers");
542 gst_rtp_vorbis_pay_parse_id (GstRTPBasePayload * basepayload, guint8 * data,
545 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
547 gint32 rate, version;
549 if (G_UNLIKELY (size < 16))
552 if (G_UNLIKELY (memcmp (data, "\001vorbis", 7)))
556 if (G_UNLIKELY ((version = GST_READ_UINT32_LE (data)) != 0))
557 goto invalid_version;
560 if (G_UNLIKELY ((channels = *data++) < 1))
561 goto invalid_channels;
563 if (G_UNLIKELY ((rate = GST_READ_UINT32_LE (data)) < 1))
566 /* all fine, store the values */
567 rtpvorbispay->channels = channels;
568 rtpvorbispay->rate = rate;
575 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
576 ("Identification packet is too short, need at least 16, got %d", size),
582 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
583 ("Invalid header start in identification packet"), (NULL));
588 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
589 ("Invalid version, expected 0, got %d", version), (NULL));
594 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
595 ("Invalid rate %d", rate), (NULL));
600 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
601 ("Invalid channels %d", channels), (NULL));
607 gst_rtp_vorbis_pay_payload_buffer (GstRtpVorbisPay * rtpvorbispay, guint8 VDT,
608 GstBuffer * buffer, guint8 * data, guint size, GstClockTime timestamp,
609 GstClockTime duration, guint not_in_length)
611 GstFlowReturn ret = GST_FLOW_OK;
614 GstClockTime newduration;
617 guint8 *ppos, *payload;
619 GstRTPBuffer rtp = { NULL };
621 /* size increases with packet length and 2 bytes size eader. */
622 newduration = rtpvorbispay->payload_duration;
623 if (duration != GST_CLOCK_TIME_NONE)
624 newduration += duration;
626 newsize = rtpvorbispay->payload_pos + 2 + size;
627 packet_len = gst_rtp_buffer_calc_packet_len (newsize, 0, 0);
629 /* check buffer filled against length and max latency */
630 flush = gst_rtp_base_payload_is_filled (GST_RTP_BASE_PAYLOAD (rtpvorbispay),
631 packet_len, newduration);
632 /* we can store up to 15 vorbis packets in one RTP packet. */
633 flush |= (rtpvorbispay->payload_pkts == 15);
634 /* flush if we have a new VDT */
635 if (rtpvorbispay->packet)
636 flush |= (rtpvorbispay->payload_VDT != VDT);
638 ret = gst_rtp_vorbis_pay_flush_packet (rtpvorbispay);
640 if (ret != GST_FLOW_OK)
643 /* create new packet if we must */
644 if (!rtpvorbispay->packet) {
645 gst_rtp_vorbis_pay_init_packet (rtpvorbispay, VDT, timestamp);
648 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
649 payload = gst_rtp_buffer_get_payload (&rtp);
650 ppos = payload + rtpvorbispay->payload_pos;
653 /* put buffer in packet, it either fits completely or needs to be fragmented
654 * over multiple RTP packets. */
656 plen = MIN (rtpvorbispay->payload_left - 2, size);
658 GST_LOG_OBJECT (rtpvorbispay, "append %u bytes", plen);
660 /* data is copied in the payload with a 2 byte length header */
661 ppos[0] = ((plen - not_in_length) >> 8) & 0xff;
662 ppos[1] = ((plen - not_in_length) & 0xff);
664 memcpy (&ppos[2], data, plen);
667 if (!rtpvorbispay->packet_buffers
668 || rtpvorbispay->packet_buffers->data != (gpointer) buffer)
669 rtpvorbispay->packet_buffers =
670 g_list_prepend (rtpvorbispay->packet_buffers,
671 gst_buffer_ref (buffer));
675 for (l = rtpvorbispay->headers; l; l = l->next)
676 rtpvorbispay->packet_buffers =
677 g_list_prepend (rtpvorbispay->packet_buffers,
678 gst_buffer_ref (l->data));
681 /* only first (only) configuration cuts length field */
682 /* NOTE: spec (if any) is not clear on this ... */
688 rtpvorbispay->payload_pos += plen + 2;
689 rtpvorbispay->payload_left -= plen + 2;
693 /* last fragment, set F to 0x3. */
694 rtpvorbispay->payload_F = 0x3;
696 /* fragment continues, set F to 0x2. */
697 rtpvorbispay->payload_F = 0x2;
700 /* fragmented packet starts, set F to 0x1, mark ourselves as
702 rtpvorbispay->payload_F = 0x1;
707 gst_rtp_buffer_unmap (&rtp);
708 /* fragmented packets are always flushed and have ptks of 0 */
709 rtpvorbispay->payload_pkts = 0;
710 ret = gst_rtp_vorbis_pay_flush_packet (rtpvorbispay);
713 /* start new packet and get pointers. VDT stays the same. */
714 gst_rtp_vorbis_pay_init_packet (rtpvorbispay,
715 rtpvorbispay->payload_VDT, timestamp);
716 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
717 payload = gst_rtp_buffer_get_payload (&rtp);
718 ppos = payload + rtpvorbispay->payload_pos;
721 /* unfragmented packet, update stats for next packet, size == 0 and we
722 * exit the while loop */
723 rtpvorbispay->payload_pkts++;
724 if (duration != GST_CLOCK_TIME_NONE)
725 rtpvorbispay->payload_duration += duration;
727 } while (size && ret == GST_FLOW_OK);
730 gst_rtp_buffer_unmap (&rtp);
738 gst_rtp_vorbis_pay_handle_buffer (GstRTPBasePayload * basepayload,
741 GstRtpVorbisPay *rtpvorbispay;
746 GstClockTime duration, timestamp;
749 rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
751 gst_buffer_map (buffer, &map, GST_MAP_READ);
754 duration = GST_BUFFER_DURATION (buffer);
755 timestamp = GST_BUFFER_PTS (buffer);
757 GST_LOG_OBJECT (rtpvorbispay, "size %" G_GSIZE_FORMAT
758 ", duration %" GST_TIME_FORMAT, size, GST_TIME_ARGS (duration));
760 if (G_UNLIKELY (size < 1))
763 /* find packet type */
767 /* identification, we need to parse this in order to get the clock rate. */
768 if (G_UNLIKELY (!gst_rtp_vorbis_pay_parse_id (basepayload, data, size)))
769 goto parse_id_failed;
771 } else if (data[0] == 3) {
774 } else if (data[0] == 5) {
783 /* we need to collect the headers and construct a config string from them */
785 GST_DEBUG_OBJECT (rtpvorbispay, "collecting header");
786 /* append header to the list of headers */
787 gst_buffer_unmap (buffer, &map);
788 rtpvorbispay->headers = g_list_append (rtpvorbispay->headers, buffer);
791 } else if (rtpvorbispay->headers && rtpvorbispay->need_headers) {
792 if (!gst_rtp_vorbis_pay_finish_headers (basepayload))
796 /* there is a config request, see if we need to insert it */
797 if (rtpvorbispay->config_interval > 0 && rtpvorbispay->config_data) {
798 gboolean send_config = FALSE;
800 if (rtpvorbispay->last_config != -1) {
803 GST_LOG_OBJECT (rtpvorbispay,
804 "now %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
805 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpvorbispay->last_config));
807 /* calculate diff between last config in milliseconds */
808 if (timestamp > rtpvorbispay->last_config) {
809 diff = timestamp - rtpvorbispay->last_config;
814 GST_DEBUG_OBJECT (rtpvorbispay,
815 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
817 /* bigger than interval, queue config */
818 /* FIXME should convert timestamps to running time */
819 if (GST_TIME_AS_SECONDS (diff) >= rtpvorbispay->config_interval) {
820 GST_DEBUG_OBJECT (rtpvorbispay, "time to send config");
824 /* no known previous config time, send now */
825 GST_DEBUG_OBJECT (rtpvorbispay, "no previous config time, send now");
830 /* we need to send config now first */
831 /* different TDT type forces flush */
832 gst_rtp_vorbis_pay_payload_buffer (rtpvorbispay, 1,
833 NULL, rtpvorbispay->config_data, rtpvorbispay->config_size,
834 timestamp, GST_CLOCK_TIME_NONE, rtpvorbispay->config_extra_len);
836 if (timestamp != -1) {
837 rtpvorbispay->last_config = timestamp;
843 gst_rtp_vorbis_pay_payload_buffer (rtpvorbispay, VDT, buffer, data, size,
844 timestamp, duration, 0);
846 gst_buffer_unmap (buffer, &map);
847 gst_buffer_unref (buffer);
855 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
856 ("Invalid packet size (1 < %" G_GSIZE_FORMAT ")", size), (NULL));
857 gst_buffer_unmap (buffer, &map);
858 gst_buffer_unref (buffer);
863 gst_buffer_unmap (buffer, &map);
864 gst_buffer_unref (buffer);
865 return GST_FLOW_ERROR;
869 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
870 (NULL), ("Ignoring unknown header received"));
871 gst_buffer_unmap (buffer, &map);
872 gst_buffer_unref (buffer);
877 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
878 (NULL), ("Error initializing header config"));
879 gst_buffer_unmap (buffer, &map);
880 gst_buffer_unref (buffer);
886 gst_rtp_vorbis_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
888 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (payload);
890 switch (GST_EVENT_TYPE (event)) {
891 case GST_EVENT_FLUSH_STOP:
892 gst_rtp_vorbis_pay_clear_packet (rtpvorbispay);
897 /* false to let parent handle event as well */
898 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
901 static GstStateChangeReturn
902 gst_rtp_vorbis_pay_change_state (GstElement * element,
903 GstStateChange transition)
905 GstRtpVorbisPay *rtpvorbispay;
906 GstStateChangeReturn ret;
908 rtpvorbispay = GST_RTP_VORBIS_PAY (element);
910 switch (transition) {
915 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
917 switch (transition) {
918 case GST_STATE_CHANGE_PAUSED_TO_READY:
919 gst_rtp_vorbis_pay_cleanup (rtpvorbispay);
928 gst_rtp_vorbis_pay_set_property (GObject * object, guint prop_id,
929 const GValue * value, GParamSpec * pspec)
931 GstRtpVorbisPay *rtpvorbispay;
933 rtpvorbispay = GST_RTP_VORBIS_PAY (object);
936 case PROP_CONFIG_INTERVAL:
937 rtpvorbispay->config_interval = g_value_get_uint (value);
945 gst_rtp_vorbis_pay_get_property (GObject * object, guint prop_id,
946 GValue * value, GParamSpec * pspec)
948 GstRtpVorbisPay *rtpvorbispay;
950 rtpvorbispay = GST_RTP_VORBIS_PAY (object);
953 case PROP_CONFIG_INTERVAL:
954 g_value_set_uint (value, rtpvorbispay->config_interval);
962 gst_rtp_vorbis_pay_plugin_init (GstPlugin * plugin)
964 return gst_element_register (plugin, "rtpvorbispay",
965 GST_RANK_SECONDARY, GST_TYPE_RTP_VORBIS_PAY);