1 /* GStreamer
2 * Copyright (C) <2006> Wim Taymans <wim.taymans@gmail.com>
3 *
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.
8 *
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.
13 *
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.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/audio/audio.h>
28
29 #include "fnv1hash.h"
30 #include "gstrtpvorbispay.h"
31 #include "gstrtputils.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpvorbispay_debug);
34 #define GST_CAT_DEFAULT (rtpvorbispay_debug)
35
36 /* references:
37 * http://www.rfc-editor.org/rfc/rfc5215.txt
38 */
39
40 static GstStaticPadTemplate gst_rtp_vorbis_pay_src_template =
41 GST_STATIC_PAD_TEMPLATE ("src",
42 GST_PAD_SRC,
43 GST_PAD_ALWAYS,
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
49 *
50 * "encoding-params = (string) <num channels>"
51 * "configuration = (string) ANY"
52 */
53 )
54 );
55
56 static GstStaticPadTemplate gst_rtp_vorbis_pay_sink_template =
57 GST_STATIC_PAD_TEMPLATE ("sink",
58 GST_PAD_SINK,
59 GST_PAD_ALWAYS,
60 GST_STATIC_CAPS ("audio/x-vorbis")
61 );
62
63 #define DEFAULT_CONFIG_INTERVAL 0
64
65 enum
66 {
67 PROP_0,
68 PROP_CONFIG_INTERVAL
69 };
70
71 #define gst_rtp_vorbis_pay_parent_class parent_class
72 G_DEFINE_TYPE (GstRtpVorbisPay, gst_rtp_vorbis_pay, GST_TYPE_RTP_BASE_PAYLOAD);
73
74 static gboolean gst_rtp_vorbis_pay_setcaps (GstRTPBasePayload * basepayload,
75 GstCaps * caps);
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,
79 GstBuffer * buffer);
80 static gboolean gst_rtp_vorbis_pay_sink_event (GstRTPBasePayload * payload,
81 GstEvent * event);
82
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 *
86 basepayload);
87
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);
92
93 static void
gst_rtp_vorbis_pay_class_init(GstRtpVorbisPayClass * klass)94 gst_rtp_vorbis_pay_class_init (GstRtpVorbisPayClass * klass)
95 {
96 GObjectClass *gobject_class;
97 GstElementClass *gstelement_class;
98 GstRTPBasePayloadClass *gstrtpbasepayload_class;
99
100 gobject_class = (GObjectClass *) klass;
101 gstelement_class = (GstElementClass *) klass;
102 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
103
104 gstelement_class->change_state = gst_rtp_vorbis_pay_change_state;
105
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;
109
110 gobject_class->set_property = gst_rtp_vorbis_pay_set_property;
111 gobject_class->get_property = gst_rtp_vorbis_pay_get_property;
112
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);
117
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>");
123
124 GST_DEBUG_CATEGORY_INIT (rtpvorbispay_debug, "rtpvorbispay", 0,
125 "Vorbis RTP Payloader");
126
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)
133 );
134 }
135
136 static void
gst_rtp_vorbis_pay_init(GstRtpVorbisPay * rtpvorbispay)137 gst_rtp_vorbis_pay_init (GstRtpVorbisPay * rtpvorbispay)
138 {
139 rtpvorbispay->last_config = GST_CLOCK_TIME_NONE;
140 }
141
142 static void
gst_rtp_vorbis_pay_clear_packet(GstRtpVorbisPay * rtpvorbispay)143 gst_rtp_vorbis_pay_clear_packet (GstRtpVorbisPay * rtpvorbispay)
144 {
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;
151 }
152
153 static void
gst_rtp_vorbis_pay_cleanup(GstRtpVorbisPay * rtpvorbispay)154 gst_rtp_vorbis_pay_cleanup (GstRtpVorbisPay * rtpvorbispay)
155 {
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;
162 }
163
164 static gboolean
gst_rtp_vorbis_pay_setcaps(GstRTPBasePayload * basepayload,GstCaps * caps)165 gst_rtp_vorbis_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
166 {
167 GstRtpVorbisPay *rtpvorbispay;
168 GstStructure *s;
169 const GValue *array;
170 gint asize, i;
171 GstBuffer *buf;
172 GstMapInfo map;
173
174 rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
175
176 s = gst_caps_get_structure (caps, 0);
177
178 rtpvorbispay->need_headers = TRUE;
179
180 if ((array = gst_structure_get_value (s, "streamheader")) == NULL)
181 goto done;
182
183 if (G_VALUE_TYPE (array) != GST_TYPE_ARRAY)
184 goto done;
185
186 if ((asize = gst_value_array_get_size (array)) < 3)
187 goto done;
188
189 for (i = 0; i < asize; i++) {
190 const GValue *value;
191
192 value = gst_value_array_get_value (array, i);
193 if ((buf = gst_value_get_buffer (value)) == NULL)
194 goto null_buffer;
195
196 gst_buffer_map (buf, &map, GST_MAP_READ);
197 if (map.size < 1)
198 goto invalid_streamheader;
199
200 /* no data packets allowed */
201 if ((map.data[0] & 1) == 0)
202 goto invalid_streamheader;
203
204 /* we need packets with id 1, 3, 5 */
205 if (map.data[0] != (i * 2) + 1)
206 goto invalid_streamheader;
207
208 if (i == 0) {
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,
211 map.size)))
212 goto parse_id_failed;
213 }
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);
218 }
219 if (!gst_rtp_vorbis_pay_finish_headers (basepayload))
220 goto finish_failed;
221
222 done:
223 return TRUE;
224
225 /* ERRORS */
226 null_buffer:
227 {
228 GST_WARNING_OBJECT (rtpvorbispay, "streamheader with null buffer received");
229 return FALSE;
230 }
231 invalid_streamheader:
232 {
233 GST_WARNING_OBJECT (rtpvorbispay, "unable to parse initial header");
234 gst_buffer_unmap (buf, &map);
235 return FALSE;
236 }
237 parse_id_failed:
238 {
239 GST_WARNING_OBJECT (rtpvorbispay, "unable to parse initial header");
240 gst_buffer_unmap (buf, &map);
241 return FALSE;
242 }
243 finish_failed:
244 {
245 GST_WARNING_OBJECT (rtpvorbispay, "unable to finish headers");
246 return FALSE;
247 }
248 }
249
250 static void
gst_rtp_vorbis_pay_reset_packet(GstRtpVorbisPay * rtpvorbispay,guint8 VDT)251 gst_rtp_vorbis_pay_reset_packet (GstRtpVorbisPay * rtpvorbispay, guint8 VDT)
252 {
253 guint payload_len;
254 GstRTPBuffer rtp = { NULL };
255
256 GST_LOG_OBJECT (rtpvorbispay, "reset packet");
257
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;
267 }
268
269 static void
gst_rtp_vorbis_pay_init_packet(GstRtpVorbisPay * rtpvorbispay,guint8 VDT,GstClockTime timestamp)270 gst_rtp_vorbis_pay_init_packet (GstRtpVorbisPay * rtpvorbispay, guint8 VDT,
271 GstClockTime timestamp)
272 {
273 GST_LOG_OBJECT (rtpvorbispay, "starting new packet, VDT: %d", VDT);
274
275 gst_rtp_vorbis_pay_clear_packet (rtpvorbispay);
276
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);
282
283 GST_BUFFER_PTS (rtpvorbispay->packet) = timestamp;
284 }
285
286 static GstFlowReturn
gst_rtp_vorbis_pay_flush_packet(GstRtpVorbisPay * rtpvorbispay)287 gst_rtp_vorbis_pay_flush_packet (GstRtpVorbisPay * rtpvorbispay)
288 {
289 GstFlowReturn ret;
290 guint8 *payload;
291 guint hlen;
292 GstRTPBuffer rtp = { NULL };
293 GList *l;
294
295 /* check for empty packet */
296 if (!rtpvorbispay->packet || rtpvorbispay->payload_pos <= 4)
297 return GST_FLOW_OK;
298
299 GST_LOG_OBJECT (rtpvorbispay, "flushing packet");
300
301 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
302
303 /* fix header */
304 payload = gst_rtp_buffer_get_payload (&rtp);
305 /*
306 * 0 1 2 3
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
311 *
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.
315 */
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);
322
323 gst_rtp_buffer_unmap (&rtp);
324
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);
328
329 GST_BUFFER_DURATION (rtpvorbispay->packet) = rtpvorbispay->payload_duration;
330
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_audio_meta (rtpvorbispay, rtpvorbispay->packet, buf);
334 gst_buffer_unref (buf);
335 }
336 g_list_free (rtpvorbispay->packet_buffers);
337 rtpvorbispay->packet_buffers = NULL;
338
339 /* push, this gives away our ref to the packet, so clear it. */
340 ret =
341 gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpvorbispay),
342 rtpvorbispay->packet);
343 rtpvorbispay->packet = NULL;
344
345 return ret;
346 }
347
348 static gboolean
gst_rtp_vorbis_pay_finish_headers(GstRTPBasePayload * basepayload)349 gst_rtp_vorbis_pay_finish_headers (GstRTPBasePayload * basepayload)
350 {
351 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
352 GList *walk;
353 guint length, size, n_headers, configlen, extralen;
354 gchar *cstr, *configuration;
355 guint8 *data, *config;
356 guint32 ident;
357 gboolean res;
358
359 GST_DEBUG_OBJECT (rtpvorbispay, "finish headers");
360
361 if (!rtpvorbispay->headers)
362 goto no_headers;
363
364 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
365 * | Number of packed headers |
366 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
367 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
368 * | Packed header |
369 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
370 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
371 * | Packed header |
372 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
373 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
374 * | .... |
375 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
376 *
377 * We only construct a config containing 1 packed header like this:
378 *
379 * 0 1 2 3
380 * 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
381 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
382 * | Ident | length ..
383 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
384 * .. | n. of headers | length1 | length2 ..
385 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
386 * .. | Identification Header ..
387 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
388 * .................................................................
389 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
390 * .. | Comment Header ..
391 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
392 * .................................................................
393 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
394 * .. Comment Header |
395 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
396 * | Setup Header ..
397 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
398 * .................................................................
399 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
400 * .. Setup Header |
401 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
402 */
403
404 /* we need 4 bytes for the number of headers (which is always 1), 3 bytes for
405 * the ident, 2 bytes for length, 1 byte for n. of headers. */
406 size = 4 + 3 + 2 + 1;
407
408 /* count the size of the headers first and update the hash */
409 length = 0;
410 n_headers = 0;
411 ident = fnv1_hash_32_new ();
412 extralen = 1;
413 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
414 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
415 GstMapInfo map;
416 guint bsize;
417
418 bsize = gst_buffer_get_size (buf);
419 length += bsize;
420 n_headers++;
421
422 /* count number of bytes needed for length fields, we don't need this for
423 * the last header. */
424 if (g_list_next (walk)) {
425 do {
426 size++;
427 extralen++;
428 bsize >>= 7;
429 } while (bsize);
430 }
431 /* update hash */
432 gst_buffer_map (buf, &map, GST_MAP_READ);
433 ident = fnv1_hash_32_update (ident, map.data, map.size);
434 gst_buffer_unmap (buf, &map);
435 }
436
437 /* packet length is header size + packet length */
438 configlen = size + length;
439 config = data = g_malloc (configlen);
440
441 /* number of packed headers, we only pack 1 header */
442 data[0] = 0;
443 data[1] = 0;
444 data[2] = 0;
445 data[3] = 1;
446
447 ident = fnv1_hash_32_to_24 (ident);
448 rtpvorbispay->payload_ident = ident;
449 GST_DEBUG_OBJECT (rtpvorbispay, "ident 0x%08x", ident);
450
451 /* take lower 3 bytes */
452 data[4] = (ident >> 16) & 0xff;
453 data[5] = (ident >> 8) & 0xff;
454 data[6] = ident & 0xff;
455
456 /* store length of all vorbis headers */
457 data[7] = ((length) >> 8) & 0xff;
458 data[8] = (length) & 0xff;
459
460 /* store number of headers minus one. */
461 data[9] = n_headers - 1;
462 data += 10;
463
464 /* store length for each header */
465 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
466 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
467 guint bsize, size, temp;
468 guint flag;
469
470 /* only need to store the length when it's not the last header */
471 if (!g_list_next (walk))
472 break;
473
474 bsize = gst_buffer_get_size (buf);
475
476 /* calc size */
477 size = 0;
478 do {
479 size++;
480 bsize >>= 7;
481 } while (bsize);
482 temp = size;
483
484 bsize = gst_buffer_get_size (buf);
485 /* write the size backwards */
486 flag = 0;
487 while (size) {
488 size--;
489 data[size] = (bsize & 0x7f) | flag;
490 bsize >>= 7;
491 flag = 0x80; /* Flag bit on all bytes of the length except the last */
492 }
493 data += temp;
494 }
495
496 /* copy header data */
497 for (walk = rtpvorbispay->headers; walk; walk = g_list_next (walk)) {
498 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
499
500 gst_buffer_extract (buf, 0, data, gst_buffer_get_size (buf));
501 data += gst_buffer_get_size (buf);
502 }
503 rtpvorbispay->need_headers = FALSE;
504
505 /* serialize to base64 */
506 configuration = g_base64_encode (config, configlen);
507
508 /* store for later re-sending */
509 g_free (rtpvorbispay->config_data);
510 rtpvorbispay->config_size = configlen - 4 - 3 - 2;
511 rtpvorbispay->config_data = g_malloc (rtpvorbispay->config_size);
512 rtpvorbispay->config_extra_len = extralen;
513 memcpy (rtpvorbispay->config_data, config + 4 + 3 + 2,
514 rtpvorbispay->config_size);
515
516 g_free (config);
517
518 /* configure payloader settings */
519 cstr = g_strdup_printf ("%d", rtpvorbispay->channels);
520 gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "VORBIS",
521 rtpvorbispay->rate);
522 res =
523 gst_rtp_base_payload_set_outcaps (basepayload, "encoding-params",
524 G_TYPE_STRING, cstr, "configuration", G_TYPE_STRING, configuration, NULL);
525 g_free (cstr);
526 g_free (configuration);
527
528 return res;
529
530 /* ERRORS */
531 no_headers:
532 {
533 GST_DEBUG_OBJECT (rtpvorbispay, "finish headers");
534 return FALSE;
535 }
536 }
537
538 static gboolean
gst_rtp_vorbis_pay_parse_id(GstRTPBasePayload * basepayload,guint8 * data,guint size)539 gst_rtp_vorbis_pay_parse_id (GstRTPBasePayload * basepayload, guint8 * data,
540 guint size)
541 {
542 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
543 guint8 channels;
544 gint32 rate, version;
545
546 if (G_UNLIKELY (size < 16))
547 goto too_short;
548
549 if (G_UNLIKELY (memcmp (data, "\001vorbis", 7)))
550 goto invalid_start;
551 data += 7;
552
553 if (G_UNLIKELY ((version = GST_READ_UINT32_LE (data)) != 0))
554 goto invalid_version;
555 data += 4;
556
557 if (G_UNLIKELY ((channels = *data++) < 1))
558 goto invalid_channels;
559
560 if (G_UNLIKELY ((rate = GST_READ_UINT32_LE (data)) < 1))
561 goto invalid_rate;
562
563 /* all fine, store the values */
564 rtpvorbispay->channels = channels;
565 rtpvorbispay->rate = rate;
566
567 return TRUE;
568
569 /* ERRORS */
570 too_short:
571 {
572 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
573 ("Identification packet is too short, need at least 16, got %d", size),
574 (NULL));
575 return FALSE;
576 }
577 invalid_start:
578 {
579 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
580 ("Invalid header start in identification packet"), (NULL));
581 return FALSE;
582 }
583 invalid_version:
584 {
585 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
586 ("Invalid version, expected 0, got %d", version), (NULL));
587 return FALSE;
588 }
589 invalid_rate:
590 {
591 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
592 ("Invalid rate %d", rate), (NULL));
593 return FALSE;
594 }
595 invalid_channels:
596 {
597 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
598 ("Invalid channels %d", channels), (NULL));
599 return FALSE;
600 }
601 }
602
603 static GstFlowReturn
gst_rtp_vorbis_pay_payload_buffer(GstRtpVorbisPay * rtpvorbispay,guint8 VDT,GstBuffer * buffer,guint8 * data,guint size,GstClockTime timestamp,GstClockTime duration,guint not_in_length)604 gst_rtp_vorbis_pay_payload_buffer (GstRtpVorbisPay * rtpvorbispay, guint8 VDT,
605 GstBuffer * buffer, guint8 * data, guint size, GstClockTime timestamp,
606 GstClockTime duration, guint not_in_length)
607 {
608 GstFlowReturn ret = GST_FLOW_OK;
609 guint newsize;
610 guint packet_len;
611 GstClockTime newduration;
612 gboolean flush;
613 guint plen;
614 guint8 *ppos, *payload;
615 gboolean fragmented;
616 GstRTPBuffer rtp = { NULL };
617
618 /* size increases with packet length and 2 bytes size eader. */
619 newduration = rtpvorbispay->payload_duration;
620 if (duration != GST_CLOCK_TIME_NONE)
621 newduration += duration;
622
623 newsize = rtpvorbispay->payload_pos + 2 + size;
624 packet_len = gst_rtp_buffer_calc_packet_len (newsize, 0, 0);
625
626 /* check buffer filled against length and max latency */
627 flush = gst_rtp_base_payload_is_filled (GST_RTP_BASE_PAYLOAD (rtpvorbispay),
628 packet_len, newduration);
629 /* we can store up to 15 vorbis packets in one RTP packet. */
630 flush |= (rtpvorbispay->payload_pkts == 15);
631 /* flush if we have a new VDT */
632 if (rtpvorbispay->packet)
633 flush |= (rtpvorbispay->payload_VDT != VDT);
634 if (flush)
635 ret = gst_rtp_vorbis_pay_flush_packet (rtpvorbispay);
636
637 if (ret != GST_FLOW_OK)
638 goto done;
639
640 /* create new packet if we must */
641 if (!rtpvorbispay->packet) {
642 gst_rtp_vorbis_pay_init_packet (rtpvorbispay, VDT, timestamp);
643 }
644
645 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
646 payload = gst_rtp_buffer_get_payload (&rtp);
647 ppos = payload + rtpvorbispay->payload_pos;
648 fragmented = FALSE;
649
650 /* put buffer in packet, it either fits completely or needs to be fragmented
651 * over multiple RTP packets. */
652 do {
653 plen = MIN (rtpvorbispay->payload_left - 2, size);
654
655 GST_LOG_OBJECT (rtpvorbispay, "append %u bytes", plen);
656
657 /* data is copied in the payload with a 2 byte length header */
658 ppos[0] = ((plen - not_in_length) >> 8) & 0xff;
659 ppos[1] = ((plen - not_in_length) & 0xff);
660 if (plen)
661 memcpy (&ppos[2], data, plen);
662
663 if (buffer) {
664 if (!rtpvorbispay->packet_buffers
665 || rtpvorbispay->packet_buffers->data != (gpointer) buffer)
666 rtpvorbispay->packet_buffers =
667 g_list_prepend (rtpvorbispay->packet_buffers,
668 gst_buffer_ref (buffer));
669 } else {
670 GList *l;
671
672 for (l = rtpvorbispay->headers; l; l = l->next)
673 rtpvorbispay->packet_buffers =
674 g_list_prepend (rtpvorbispay->packet_buffers,
675 gst_buffer_ref (l->data));
676 }
677
678 /* only first (only) configuration cuts length field */
679 /* NOTE: spec (if any) is not clear on this ... */
680 not_in_length = 0;
681
682 size -= plen;
683 data += plen;
684
685 rtpvorbispay->payload_pos += plen + 2;
686 rtpvorbispay->payload_left -= plen + 2;
687
688 if (fragmented) {
689 if (size == 0)
690 /* last fragment, set F to 0x3. */
691 rtpvorbispay->payload_F = 0x3;
692 else
693 /* fragment continues, set F to 0x2. */
694 rtpvorbispay->payload_F = 0x2;
695 } else {
696 if (size > 0) {
697 /* fragmented packet starts, set F to 0x1, mark ourselves as
698 * fragmented. */
699 rtpvorbispay->payload_F = 0x1;
700 fragmented = TRUE;
701 }
702 }
703 if (fragmented) {
704 gst_rtp_buffer_unmap (&rtp);
705 /* fragmented packets are always flushed and have ptks of 0 */
706 rtpvorbispay->payload_pkts = 0;
707 ret = gst_rtp_vorbis_pay_flush_packet (rtpvorbispay);
708
709 if (size > 0) {
710 /* start new packet and get pointers. VDT stays the same. */
711 gst_rtp_vorbis_pay_init_packet (rtpvorbispay,
712 rtpvorbispay->payload_VDT, timestamp);
713 gst_rtp_buffer_map (rtpvorbispay->packet, GST_MAP_WRITE, &rtp);
714 payload = gst_rtp_buffer_get_payload (&rtp);
715 ppos = payload + rtpvorbispay->payload_pos;
716 }
717 } else {
718 /* unfragmented packet, update stats for next packet, size == 0 and we
719 * exit the while loop */
720 rtpvorbispay->payload_pkts++;
721 if (duration != GST_CLOCK_TIME_NONE)
722 rtpvorbispay->payload_duration += duration;
723 }
724 } while (size && ret == GST_FLOW_OK);
725
726 if (rtp.buffer)
727 gst_rtp_buffer_unmap (&rtp);
728
729 done:
730
731 return ret;
732 }
733
734 static GstFlowReturn
gst_rtp_vorbis_pay_handle_buffer(GstRTPBasePayload * basepayload,GstBuffer * buffer)735 gst_rtp_vorbis_pay_handle_buffer (GstRTPBasePayload * basepayload,
736 GstBuffer * buffer)
737 {
738 GstRtpVorbisPay *rtpvorbispay;
739 GstFlowReturn ret;
740 GstMapInfo map;
741 gsize size;
742 guint8 *data;
743 GstClockTime duration, timestamp;
744 guint8 VDT;
745
746 rtpvorbispay = GST_RTP_VORBIS_PAY (basepayload);
747
748 gst_buffer_map (buffer, &map, GST_MAP_READ);
749 data = map.data;
750 size = map.size;
751 duration = GST_BUFFER_DURATION (buffer);
752 timestamp = GST_BUFFER_PTS (buffer);
753
754 GST_LOG_OBJECT (rtpvorbispay, "size %" G_GSIZE_FORMAT
755 ", duration %" GST_TIME_FORMAT, size, GST_TIME_ARGS (duration));
756
757 if (G_UNLIKELY (size < 1))
758 goto wrong_size;
759
760 /* find packet type */
761 if (data[0] & 1) {
762 /* header */
763 if (data[0] == 1) {
764 /* identification, we need to parse this in order to get the clock rate. */
765 if (G_UNLIKELY (!gst_rtp_vorbis_pay_parse_id (basepayload, data, size)))
766 goto parse_id_failed;
767 VDT = 1;
768 } else if (data[0] == 3) {
769 /* comment */
770 VDT = 2;
771 } else if (data[0] == 5) {
772 /* setup */
773 VDT = 1;
774 } else
775 goto unknown_header;
776 } else
777 /* data */
778 VDT = 0;
779
780 /* we need to collect the headers and construct a config string from them */
781 if (VDT != 0) {
782 rtpvorbispay->need_headers = TRUE;
783 if (!rtpvorbispay->need_headers && VDT == 1) {
784 GST_INFO_OBJECT (rtpvorbispay, "getting new headers, replace existing");
785 g_list_free_full (rtpvorbispay->headers,
786 (GDestroyNotify) gst_buffer_unref);
787 rtpvorbispay->headers = NULL;
788 }
789 GST_DEBUG_OBJECT (rtpvorbispay, "collecting header");
790 /* append header to the list of headers, or replace
791 * if the same type of header was already in there.
792 *
793 * This prevents storing an infinite amount of e.g. comment headers, there
794 * must only be one */
795 gst_buffer_unmap (buffer, &map);
796
797 if (rtpvorbispay->headers) {
798 gboolean found = FALSE;
799 GList *l;
800 guint8 new_header_type;
801
802 gst_buffer_extract (buffer, 0, &new_header_type, 1);
803
804 for (l = rtpvorbispay->headers; l; l = l->next) {
805 GstBuffer *header = l->data;
806 guint8 header_type;
807
808 if (gst_buffer_extract (header, 0, &header_type, 1)
809 && header_type == new_header_type) {
810 found = TRUE;
811 gst_buffer_unref (header);
812 l->data = buffer;
813 break;
814 }
815 }
816 if (!found)
817 rtpvorbispay->headers = g_list_append (rtpvorbispay->headers, buffer);
818 } else {
819 rtpvorbispay->headers = g_list_append (rtpvorbispay->headers, buffer);
820 }
821
822 ret = GST_FLOW_OK;
823 goto done;
824 } else if (rtpvorbispay->headers && rtpvorbispay->need_headers) {
825 if (!gst_rtp_vorbis_pay_finish_headers (basepayload))
826 goto header_error;
827 }
828
829 /* there is a config request, see if we need to insert it */
830 if (rtpvorbispay->config_interval > 0 && rtpvorbispay->config_data) {
831 gboolean send_config = FALSE;
832 GstClockTime running_time =
833 gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
834 timestamp);
835
836 if (rtpvorbispay->last_config != -1) {
837 guint64 diff;
838
839 GST_LOG_OBJECT (rtpvorbispay,
840 "now %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
841 GST_TIME_ARGS (running_time),
842 GST_TIME_ARGS (rtpvorbispay->last_config));
843
844 /* calculate diff between last config in milliseconds */
845 if (running_time > rtpvorbispay->last_config) {
846 diff = running_time - rtpvorbispay->last_config;
847 } else {
848 diff = 0;
849 }
850
851 GST_DEBUG_OBJECT (rtpvorbispay,
852 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
853
854 /* bigger than interval, queue config */
855 if (GST_TIME_AS_SECONDS (diff) >= rtpvorbispay->config_interval) {
856 GST_DEBUG_OBJECT (rtpvorbispay, "time to send config");
857 send_config = TRUE;
858 }
859 } else {
860 /* no known previous config time, send now */
861 GST_DEBUG_OBJECT (rtpvorbispay, "no previous config time, send now");
862 send_config = TRUE;
863 }
864
865 if (send_config) {
866 /* we need to send config now first */
867 /* different TDT type forces flush */
868 gst_rtp_vorbis_pay_payload_buffer (rtpvorbispay, 1,
869 NULL, rtpvorbispay->config_data, rtpvorbispay->config_size,
870 timestamp, GST_CLOCK_TIME_NONE, rtpvorbispay->config_extra_len);
871
872 if (running_time != -1) {
873 rtpvorbispay->last_config = running_time;
874 }
875 }
876 }
877
878 ret =
879 gst_rtp_vorbis_pay_payload_buffer (rtpvorbispay, VDT, buffer, data, size,
880 timestamp, duration, 0);
881
882 gst_buffer_unmap (buffer, &map);
883 gst_buffer_unref (buffer);
884
885 done:
886 return ret;
887
888 /* ERRORS */
889 wrong_size:
890 {
891 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
892 ("Invalid packet size (1 < %" G_GSIZE_FORMAT ")", size), (NULL));
893 gst_buffer_unmap (buffer, &map);
894 gst_buffer_unref (buffer);
895 return GST_FLOW_OK;
896 }
897 parse_id_failed:
898 {
899 gst_buffer_unmap (buffer, &map);
900 gst_buffer_unref (buffer);
901 return GST_FLOW_ERROR;
902 }
903 unknown_header:
904 {
905 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
906 (NULL), ("Ignoring unknown header received"));
907 gst_buffer_unmap (buffer, &map);
908 gst_buffer_unref (buffer);
909 return GST_FLOW_OK;
910 }
911 header_error:
912 {
913 GST_ELEMENT_WARNING (rtpvorbispay, STREAM, DECODE,
914 (NULL), ("Error initializing header config"));
915 gst_buffer_unmap (buffer, &map);
916 gst_buffer_unref (buffer);
917 return GST_FLOW_OK;
918 }
919 }
920
921 static gboolean
gst_rtp_vorbis_pay_sink_event(GstRTPBasePayload * payload,GstEvent * event)922 gst_rtp_vorbis_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
923 {
924 GstRtpVorbisPay *rtpvorbispay = GST_RTP_VORBIS_PAY (payload);
925
926 switch (GST_EVENT_TYPE (event)) {
927 case GST_EVENT_FLUSH_STOP:
928 gst_rtp_vorbis_pay_clear_packet (rtpvorbispay);
929 break;
930 default:
931 break;
932 }
933 /* false to let parent handle event as well */
934 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
935 }
936
937 static GstStateChangeReturn
gst_rtp_vorbis_pay_change_state(GstElement * element,GstStateChange transition)938 gst_rtp_vorbis_pay_change_state (GstElement * element,
939 GstStateChange transition)
940 {
941 GstRtpVorbisPay *rtpvorbispay;
942 GstStateChangeReturn ret;
943
944 rtpvorbispay = GST_RTP_VORBIS_PAY (element);
945
946 switch (transition) {
947 default:
948 break;
949 }
950
951 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
952
953 switch (transition) {
954 case GST_STATE_CHANGE_PAUSED_TO_READY:
955 gst_rtp_vorbis_pay_cleanup (rtpvorbispay);
956 break;
957 default:
958 break;
959 }
960 return ret;
961 }
962
963 static void
gst_rtp_vorbis_pay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)964 gst_rtp_vorbis_pay_set_property (GObject * object, guint prop_id,
965 const GValue * value, GParamSpec * pspec)
966 {
967 GstRtpVorbisPay *rtpvorbispay;
968
969 rtpvorbispay = GST_RTP_VORBIS_PAY (object);
970
971 switch (prop_id) {
972 case PROP_CONFIG_INTERVAL:
973 rtpvorbispay->config_interval = g_value_get_uint (value);
974 break;
975 default:
976 break;
977 }
978 }
979
980 static void
gst_rtp_vorbis_pay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)981 gst_rtp_vorbis_pay_get_property (GObject * object, guint prop_id,
982 GValue * value, GParamSpec * pspec)
983 {
984 GstRtpVorbisPay *rtpvorbispay;
985
986 rtpvorbispay = GST_RTP_VORBIS_PAY (object);
987
988 switch (prop_id) {
989 case PROP_CONFIG_INTERVAL:
990 g_value_set_uint (value, rtpvorbispay->config_interval);
991 break;
992 default:
993 break;
994 }
995 }
996
997 gboolean
gst_rtp_vorbis_pay_plugin_init(GstPlugin * plugin)998 gst_rtp_vorbis_pay_plugin_init (GstPlugin * plugin)
999 {
1000 return gst_element_register (plugin, "rtpvorbispay",
1001 GST_RANK_SECONDARY, GST_TYPE_RTP_VORBIS_PAY);
1002 }
1003