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