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 <gst/tag/tag.h>
25 #include <gst/rtp/gstrtpbuffer.h>
26 #include <gst/video/video.h>
27
28 #include <string.h>
29 #include "gstrtptheoradepay.h"
30 #include "gstrtputils.h"
31
32 GST_DEBUG_CATEGORY_STATIC (rtptheoradepay_debug);
33 #define GST_CAT_DEFAULT (rtptheoradepay_debug)
34
35 static GstStaticPadTemplate gst_rtp_theora_depay_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
37 GST_PAD_SINK,
38 GST_PAD_ALWAYS,
39 GST_STATIC_CAPS ("application/x-rtp, "
40 "media = (string) \"video\", "
41 "clock-rate = (int) 90000, " "encoding-name = (string) \"THEORA\""
42 /* All required parameters
43 *
44 * "sampling = (string) { "YCbCr-4:2:0", "YCbCr-4:2:2", "YCbCr-4:4:4" } "
45 * "width = (string) [1, 1048561] (multiples of 16) "
46 * "height = (string) [1, 1048561] (multiples of 16) "
47 * "delivery-method = (string) { inline, in_band, out_band/<specific_name> } "
48 * "configuration = (string) ANY"
49 */
50 /* All optional parameters
51 *
52 * "configuration-uri ="
53 */
54 )
55 );
56
57 static GstStaticPadTemplate gst_rtp_theora_depay_src_template =
58 GST_STATIC_PAD_TEMPLATE ("src",
59 GST_PAD_SRC,
60 GST_PAD_ALWAYS,
61 GST_STATIC_CAPS ("video/x-theora")
62 );
63
64 #define gst_rtp_theora_depay_parent_class parent_class
65 G_DEFINE_TYPE (GstRtpTheoraDepay, gst_rtp_theora_depay,
66 GST_TYPE_RTP_BASE_DEPAYLOAD);
67
68 static gboolean gst_rtp_theora_depay_setcaps (GstRTPBaseDepayload * depayload,
69 GstCaps * caps);
70 static GstBuffer *gst_rtp_theora_depay_process (GstRTPBaseDepayload * depayload,
71 GstRTPBuffer * rtp);
72 static gboolean gst_rtp_theora_depay_packet_lost (GstRTPBaseDepayload *
73 depayload, GstEvent * event);
74
75 static void gst_rtp_theora_depay_finalize (GObject * object);
76
77 static GstStateChangeReturn gst_rtp_theora_depay_change_state (GstElement *
78 element, GstStateChange transition);
79
80 static void
gst_rtp_theora_depay_class_init(GstRtpTheoraDepayClass * klass)81 gst_rtp_theora_depay_class_init (GstRtpTheoraDepayClass * klass)
82 {
83 GObjectClass *gobject_class;
84 GstElementClass *gstelement_class;
85 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
86
87 gobject_class = (GObjectClass *) klass;
88 gstelement_class = (GstElementClass *) klass;
89 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
90
91 gobject_class->finalize = gst_rtp_theora_depay_finalize;
92
93 gstelement_class->change_state = gst_rtp_theora_depay_change_state;
94
95 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_theora_depay_process;
96 gstrtpbasedepayload_class->set_caps = gst_rtp_theora_depay_setcaps;
97 gstrtpbasedepayload_class->packet_lost = gst_rtp_theora_depay_packet_lost;
98
99 gst_element_class_add_static_pad_template (gstelement_class,
100 &gst_rtp_theora_depay_sink_template);
101 gst_element_class_add_static_pad_template (gstelement_class,
102 &gst_rtp_theora_depay_src_template);
103
104 gst_element_class_set_static_metadata (gstelement_class,
105 "RTP Theora depayloader", "Codec/Depayloader/Network/RTP",
106 "Extracts Theora video from RTP packets (draft-01 of RFC XXXX)",
107 "Wim Taymans <wim.taymans@gmail.com>");
108
109 GST_DEBUG_CATEGORY_INIT (rtptheoradepay_debug, "rtptheoradepay", 0,
110 "Theora RTP Depayloader");
111 }
112
113 static void
gst_rtp_theora_depay_init(GstRtpTheoraDepay * rtptheoradepay)114 gst_rtp_theora_depay_init (GstRtpTheoraDepay * rtptheoradepay)
115 {
116 rtptheoradepay->adapter = gst_adapter_new ();
117 }
118
119 static void
free_config(GstRtpTheoraConfig * config)120 free_config (GstRtpTheoraConfig * config)
121 {
122 g_list_free_full (config->headers, (GDestroyNotify) gst_buffer_unref);
123 g_free (config);
124 }
125
126 static void
free_indents(GstRtpTheoraDepay * rtptheoradepay)127 free_indents (GstRtpTheoraDepay * rtptheoradepay)
128 {
129 g_list_free_full (rtptheoradepay->configs, (GDestroyNotify) free_config);
130 rtptheoradepay->configs = NULL;
131 }
132
133 static void
gst_rtp_theora_depay_finalize(GObject * object)134 gst_rtp_theora_depay_finalize (GObject * object)
135 {
136 GstRtpTheoraDepay *rtptheoradepay = GST_RTP_THEORA_DEPAY (object);
137
138 g_object_unref (rtptheoradepay->adapter);
139
140 G_OBJECT_CLASS (parent_class)->finalize (object);
141 }
142
143 static gboolean
gst_rtp_theora_depay_parse_configuration(GstRtpTheoraDepay * rtptheoradepay,GstBuffer * confbuf)144 gst_rtp_theora_depay_parse_configuration (GstRtpTheoraDepay * rtptheoradepay,
145 GstBuffer * confbuf)
146 {
147 GstBuffer *buf;
148 guint32 num_headers;
149 GstMapInfo map;
150 guint8 *data;
151 gsize size;
152 gint i, j;
153
154 gst_buffer_map (confbuf, &map, GST_MAP_READ);
155 data = map.data;
156 size = map.size;
157
158 GST_DEBUG_OBJECT (rtptheoradepay, "config size %" G_GSIZE_FORMAT, size);
159
160 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161 * | Number of packed headers |
162 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
163 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
164 * | Packed header |
165 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
166 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
167 * | Packed header |
168 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
169 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
170 * | .... |
171 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
172 */
173 if (size < 4)
174 goto too_small;
175
176 num_headers = GST_READ_UINT32_BE (data);
177 size -= 4;
178 data += 4;
179
180 GST_DEBUG_OBJECT (rtptheoradepay, "have %u headers", num_headers);
181
182 /* 0 1 2 3
183 * 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
184 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
185 * | Ident | length ..
186 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
187 * .. | n. of headers | length1 | length2 ..
188 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
189 * .. | Identification Header ..
190 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
191 * .................................................................
192 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
193 * .. | Comment Header ..
194 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
195 * .................................................................
196 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
197 * .. Comment Header |
198 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
199 * | Setup Header ..
200 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
201 * .................................................................
202 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203 * .. Setup Header |
204 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
205 */
206 for (i = 0; i < num_headers; i++) {
207 guint32 ident;
208 guint16 length;
209 guint8 n_headers, b;
210 GstRtpTheoraConfig *conf;
211 guint *h_sizes;
212 guint extra = 1;
213
214 if (size < 6)
215 goto too_small;
216
217 ident = (data[0] << 16) | (data[1] << 8) | data[2];
218 length = (data[3] << 8) | data[4];
219 n_headers = data[5];
220 size -= 6;
221 data += 6;
222
223 GST_DEBUG_OBJECT (rtptheoradepay,
224 "header %d, ident 0x%08x, length %u, left %" G_GSIZE_FORMAT, i, ident,
225 length, size);
226
227 /* FIXME check if we already got this ident */
228
229 /* length might also include count of following size fields */
230 if (size < length && size + 1 != length)
231 goto too_small;
232
233 /* read header sizes we read 2 sizes, the third size (for which we allocate
234 * space) must be derived from the total packed header length. */
235 h_sizes = g_newa (guint, n_headers + 1);
236 for (j = 0; j < n_headers; j++) {
237 guint h_size;
238
239 h_size = 0;
240 do {
241 if (size < 1)
242 goto too_small;
243 b = *data++;
244 size--;
245 extra++;
246 h_size = (h_size << 7) | (b & 0x7f);
247 } while (b & 0x80);
248 GST_DEBUG_OBJECT (rtptheoradepay, "headers %d: size: %u", j, h_size);
249 h_sizes[j] = h_size;
250 length -= h_size;
251 }
252 /* last header length is the remaining space */
253 GST_DEBUG_OBJECT (rtptheoradepay, "last header size: %u", length);
254 h_sizes[j] = length;
255
256 GST_DEBUG_OBJECT (rtptheoradepay, "preparing headers");
257 conf = g_new0 (GstRtpTheoraConfig, 1);
258 conf->ident = ident;
259
260 for (j = 0; j <= n_headers; j++) {
261 guint h_size;
262
263 h_size = h_sizes[j];
264 if (size < h_size) {
265 if (j != n_headers || size + extra != h_size) {
266 free_config (conf);
267 goto too_small;
268 } else {
269 /* otherwise means that overall length field contained total length,
270 * including extra fields */
271 h_size -= extra;
272 }
273 }
274
275 GST_DEBUG_OBJECT (rtptheoradepay, "reading header %d, size %u", j,
276 h_size);
277
278 buf =
279 gst_buffer_copy_region (confbuf, GST_BUFFER_COPY_ALL, data - map.data,
280 h_size);
281 conf->headers = g_list_append (conf->headers, buf);
282 data += h_size;
283 size -= h_size;
284 }
285 rtptheoradepay->configs = g_list_append (rtptheoradepay->configs, conf);
286 }
287
288 gst_buffer_unmap (confbuf, &map);
289 gst_buffer_unref (confbuf);
290
291 return TRUE;
292
293 /* ERRORS */
294 too_small:
295 {
296 GST_DEBUG_OBJECT (rtptheoradepay, "configuration too small");
297 gst_buffer_unmap (confbuf, &map);
298 gst_buffer_unref (confbuf);
299 return FALSE;
300 }
301 }
302
303 static gboolean
gst_rtp_theora_depay_parse_inband_configuration(GstRtpTheoraDepay * rtptheoradepay,guint ident,guint8 * configuration,guint size,guint length)304 gst_rtp_theora_depay_parse_inband_configuration (GstRtpTheoraDepay *
305 rtptheoradepay, guint ident, guint8 * configuration, guint size,
306 guint length)
307 {
308 GstBuffer *confbuf;
309 GstMapInfo map;
310
311 if (G_UNLIKELY (size < 4))
312 return FALSE;
313
314 /* transform inline to out-of-band and parse that one */
315 confbuf = gst_buffer_new_and_alloc (size + 9);
316 gst_buffer_map (confbuf, &map, GST_MAP_WRITE);
317 /* 1 header */
318 GST_WRITE_UINT32_BE (map.data, 1);
319 /* write Ident */
320 GST_WRITE_UINT24_BE (map.data + 4, ident);
321 /* write sort-of-length */
322 GST_WRITE_UINT16_BE (map.data + 7, length);
323 /* copy remainder */
324 memcpy (map.data + 9, configuration, size);
325 gst_buffer_unmap (confbuf, &map);
326
327 return gst_rtp_theora_depay_parse_configuration (rtptheoradepay, confbuf);
328 }
329
330 static gboolean
gst_rtp_theora_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)331 gst_rtp_theora_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
332 {
333 GstStructure *structure;
334 GstRtpTheoraDepay *rtptheoradepay;
335 GstCaps *srccaps;
336 const gchar *configuration;
337 gboolean res;
338
339 rtptheoradepay = GST_RTP_THEORA_DEPAY (depayload);
340
341 rtptheoradepay->needs_keyframe = FALSE;
342
343 structure = gst_caps_get_structure (caps, 0);
344
345 /* read and parse configuration string */
346 configuration = gst_structure_get_string (structure, "configuration");
347 if (configuration) {
348 GstBuffer *confbuf;
349 guint8 *data;
350 gsize size;
351
352 /* deserialize base64 to buffer */
353 data = g_base64_decode (configuration, &size);
354
355 confbuf = gst_buffer_new ();
356 gst_buffer_append_memory (confbuf,
357 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
358
359 if (!gst_rtp_theora_depay_parse_configuration (rtptheoradepay, confbuf))
360 goto invalid_configuration;
361 }
362
363 /* set caps on pad and on header */
364 srccaps = gst_caps_new_empty_simple ("video/x-theora");
365 res = gst_pad_set_caps (depayload->srcpad, srccaps);
366 gst_caps_unref (srccaps);
367
368 /* Clock rate is always 90000 according to draft-barbato-avt-rtp-theora-01 */
369 depayload->clock_rate = 90000;
370
371 return res;
372
373 /* ERRORS */
374 invalid_configuration:
375 {
376 GST_ERROR_OBJECT (rtptheoradepay, "invalid configuration specified");
377 return FALSE;
378 }
379 }
380
381 static gboolean
gst_rtp_theora_depay_switch_codebook(GstRtpTheoraDepay * rtptheoradepay,guint32 ident)382 gst_rtp_theora_depay_switch_codebook (GstRtpTheoraDepay * rtptheoradepay,
383 guint32 ident)
384 {
385 GList *walk;
386 gboolean res = FALSE;
387
388 for (walk = rtptheoradepay->configs; walk; walk = g_list_next (walk)) {
389 GstRtpTheoraConfig *conf = (GstRtpTheoraConfig *) walk->data;
390
391 if (conf->ident == ident) {
392 GList *headers;
393
394 /* FIXME, remove pads, create new pad.. */
395
396 /* push out all the headers */
397 for (headers = conf->headers; headers; headers = g_list_next (headers)) {
398 GstBuffer *header = GST_BUFFER_CAST (headers->data);
399
400 gst_buffer_ref (header);
401 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtptheoradepay),
402 header);
403 }
404 /* remember the current config */
405 rtptheoradepay->config = conf;
406 res = TRUE;
407 }
408 }
409 if (!res) {
410 /* we don't know about the headers, figure out an alternative method for
411 * getting the codebooks. FIXME, fail for now. */
412 }
413 return res;
414 }
415
416 static GstBuffer *
gst_rtp_theora_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)417 gst_rtp_theora_depay_process (GstRTPBaseDepayload * depayload,
418 GstRTPBuffer * rtp)
419 {
420 GstRtpTheoraDepay *rtptheoradepay;
421 GstBuffer *outbuf;
422 GstFlowReturn ret;
423 gint payload_len;
424 GstMapInfo map;
425 GstBuffer *payload_buffer = NULL;
426 guint8 *payload;
427 guint32 header, ident;
428 guint8 F, TDT, packets;
429 guint length;
430
431 rtptheoradepay = GST_RTP_THEORA_DEPAY (depayload);
432
433 payload_len = gst_rtp_buffer_get_payload_len (rtp);
434
435 GST_DEBUG_OBJECT (depayload, "got RTP packet of size %d", payload_len);
436
437 /* we need at least 4 bytes for the packet header */
438 if (G_UNLIKELY (payload_len < 4))
439 goto packet_short;
440
441 payload = gst_rtp_buffer_get_payload (rtp);
442
443 header = GST_READ_UINT32_BE (payload);
444 /*
445 * 0 1 2 3
446 * 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
447 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
448 * | Ident | F |TDT|# pkts.|
449 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
450 *
451 * F: Fragment type (0=none, 1=start, 2=cont, 3=end)
452 * TDT: Theora data type (0=theora, 1=config, 2=comment, 3=reserved)
453 * pkts: number of packets.
454 */
455 TDT = (header & 0x30) >> 4;
456 if (G_UNLIKELY (TDT == 3))
457 goto ignore_reserved;
458
459 ident = (header >> 8) & 0xffffff;
460 F = (header & 0xc0) >> 6;
461 packets = (header & 0xf);
462
463 GST_DEBUG_OBJECT (depayload, "ident: 0x%08x, F: %d, TDT: %d, packets: %d",
464 ident, F, TDT, packets);
465
466 if (TDT == 0) {
467 gboolean do_switch = FALSE;
468
469 /* we have a raw payload, find the codebook for the ident */
470 if (!rtptheoradepay->config) {
471 /* we don't have an active codebook, find the codebook and
472 * activate it */
473 do_switch = TRUE;
474 } else if (rtptheoradepay->config->ident != ident) {
475 /* codebook changed */
476 do_switch = TRUE;
477 }
478 if (do_switch) {
479 if (!gst_rtp_theora_depay_switch_codebook (rtptheoradepay, ident))
480 goto switch_failed;
481 }
482 }
483
484 /* fragmented packets, assemble */
485 if (F != 0) {
486 GstBuffer *vdata;
487
488 if (F == 1) {
489 /* if we start a packet, clear adapter and start assembling. */
490 gst_adapter_clear (rtptheoradepay->adapter);
491 GST_DEBUG_OBJECT (depayload, "start assemble");
492 rtptheoradepay->assembling = TRUE;
493 }
494
495 if (!rtptheoradepay->assembling)
496 goto no_output;
497
498 /* skip header and length. */
499 vdata = gst_rtp_buffer_get_payload_subbuffer (rtp, 6, -1);
500
501 GST_DEBUG_OBJECT (depayload, "assemble theora packet");
502 gst_adapter_push (rtptheoradepay->adapter, vdata);
503
504 /* packet is not complete, we are done */
505 if (F != 3)
506 goto no_output;
507
508 /* construct assembled buffer */
509 length = gst_adapter_available (rtptheoradepay->adapter);
510 payload_buffer = gst_adapter_take_buffer (rtptheoradepay->adapter, length);
511 } else {
512 length = 0;
513 payload_buffer = gst_rtp_buffer_get_payload_subbuffer (rtp, 4, -1);
514 }
515
516 GST_DEBUG_OBJECT (depayload, "assemble done, payload_len %d", payload_len);
517
518 gst_buffer_map (payload_buffer, &map, GST_MAP_READ);
519 payload = map.data;
520 payload_len = map.size;
521
522 /* we not assembling anymore now */
523 rtptheoradepay->assembling = FALSE;
524 gst_adapter_clear (rtptheoradepay->adapter);
525
526 /* payload now points to a length with that many theora data bytes.
527 * Iterate over the packets and send them out.
528 *
529 * 0 1 2 3
530 * 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
531 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
532 * | length | theora data ..
533 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
534 * .. theora data |
535 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
536 * | length | next theora packet data ..
537 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
538 * .. theora data |
539 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+*
540 */
541 while (payload_len >= 2) {
542 /* If length is not 0, we have a reassembled packet for which we
543 * calculated the length already and don't have to skip over the
544 * length field anymore
545 */
546 if (length == 0) {
547 length = GST_READ_UINT16_BE (payload);
548 payload += 2;
549 payload_len -= 2;
550 }
551
552 GST_DEBUG_OBJECT (depayload, "read length %u, avail: %d", length,
553 payload_len);
554
555 /* skip packet if something odd happens */
556 if (G_UNLIKELY (length > payload_len))
557 goto length_short;
558
559 /* handle in-band configuration */
560 if (G_UNLIKELY (TDT == 1)) {
561 GST_DEBUG_OBJECT (rtptheoradepay, "in-band configuration");
562 if (!gst_rtp_theora_depay_parse_inband_configuration (rtptheoradepay,
563 ident, payload, payload_len, length))
564 goto invalid_configuration;
565 goto no_output;
566 }
567
568 /* create buffer for packet */
569 outbuf =
570 gst_buffer_copy_region (payload_buffer, GST_BUFFER_COPY_ALL,
571 payload - map.data, length);
572
573 if (payload_len > 0 && (payload[0] & 0xC0) == 0x0) {
574 rtptheoradepay->needs_keyframe = FALSE;
575 } else {
576 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
577 }
578
579 payload += length;
580 payload_len -= length;
581 /* make sure to read next length */
582 length = 0;
583
584 ret = gst_rtp_base_depayload_push (depayload, outbuf);
585 if (ret != GST_FLOW_OK)
586 break;
587 }
588
589 if (rtptheoradepay->needs_keyframe)
590 goto request_keyframe;
591
592 out:
593 no_output:
594
595 if (payload_buffer) {
596 gst_buffer_unmap (payload_buffer, &map);
597 gst_buffer_unref (payload_buffer);
598 }
599
600 return NULL;
601
602 /* ERORRS */
603 switch_failed:
604 {
605 GST_ELEMENT_WARNING (rtptheoradepay, STREAM, DECODE,
606 (NULL), ("Could not switch codebooks"));
607 goto request_config;
608 }
609 packet_short:
610 {
611 GST_ELEMENT_WARNING (rtptheoradepay, STREAM, DECODE,
612 (NULL), ("Packet was too short (%d < 4)", payload_len));
613 goto request_keyframe;
614 }
615 ignore_reserved:
616 {
617 GST_WARNING_OBJECT (rtptheoradepay, "reserved TDT ignored");
618 goto out;
619 }
620 length_short:
621 {
622 GST_ELEMENT_WARNING (rtptheoradepay, STREAM, DECODE,
623 (NULL), ("Packet contains invalid data"));
624 goto request_keyframe;
625 }
626 invalid_configuration:
627 {
628 /* fatal, as we otherwise risk carrying on without output */
629 GST_ELEMENT_ERROR (rtptheoradepay, STREAM, DECODE,
630 (NULL), ("Packet contains invalid configuration"));
631 goto request_config;
632 }
633 request_config:
634 {
635 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
636 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
637 gst_structure_new ("GstForceKeyUnit",
638 "all-headers", G_TYPE_BOOLEAN, TRUE, NULL)));
639 goto out;
640 }
641 request_keyframe:
642 {
643 rtptheoradepay->needs_keyframe = TRUE;
644 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
645 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
646 gst_structure_new_empty ("GstForceKeyUnit")));
647 goto out;
648 }
649 }
650
651 static GstStateChangeReturn
gst_rtp_theora_depay_change_state(GstElement * element,GstStateChange transition)652 gst_rtp_theora_depay_change_state (GstElement * element,
653 GstStateChange transition)
654 {
655 GstRtpTheoraDepay *rtptheoradepay;
656 GstStateChangeReturn ret;
657
658 rtptheoradepay = GST_RTP_THEORA_DEPAY (element);
659
660 switch (transition) {
661 case GST_STATE_CHANGE_NULL_TO_READY:
662 break;
663 case GST_STATE_CHANGE_READY_TO_PAUSED:
664 break;
665 default:
666 break;
667 }
668
669 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
670
671 switch (transition) {
672 case GST_STATE_CHANGE_PAUSED_TO_READY:
673 free_indents (rtptheoradepay);
674 break;
675 case GST_STATE_CHANGE_READY_TO_NULL:
676 break;
677 default:
678 break;
679 }
680 return ret;
681 }
682
683 gboolean
gst_rtp_theora_depay_plugin_init(GstPlugin * plugin)684 gst_rtp_theora_depay_plugin_init (GstPlugin * plugin)
685 {
686 return gst_element_register (plugin, "rtptheoradepay",
687 GST_RANK_SECONDARY, GST_TYPE_RTP_THEORA_DEPAY);
688 }
689
690 static gboolean
gst_rtp_theora_depay_packet_lost(GstRTPBaseDepayload * depayload,GstEvent * event)691 gst_rtp_theora_depay_packet_lost (GstRTPBaseDepayload * depayload,
692 GstEvent * event)
693 {
694 GstRtpTheoraDepay *rtptheoradepay = GST_RTP_THEORA_DEPAY (depayload);
695 guint seqnum = 0;
696
697 gst_structure_get_uint (gst_event_get_structure (event), "seqnum", &seqnum);
698 GST_LOG_OBJECT (depayload, "Requested keyframe because frame with seqnum %u"
699 " is missing", seqnum);
700 rtptheoradepay->needs_keyframe = TRUE;
701
702 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
703 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
704 gst_structure_new_empty ("GstForceKeyUnit")));
705
706 return TRUE;
707 }
708