rtpmp4gdepay: streamtype is not put by all RTSP server, not make it optional
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp4gdepay.c
1 /* GStreamer
2  * Copyright (C) <2005> 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 #include <stdlib.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpmp4gdepay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpmp4gdepay_debug);
31 #define GST_CAT_DEFAULT (rtpmp4gdepay_debug)
32
33 static GstStaticPadTemplate gst_rtp_mp4g_depay_src_template =
34     GST_STATIC_PAD_TEMPLATE ("src",
35     GST_PAD_SRC,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS ("video/mpeg,"
38         "mpegversion=(int) 4,"
39         "systemstream=(boolean)false;"
40         "audio/mpeg," "mpegversion=(int) 4, " "stream-format=(string)raw")
41     );
42
43 static GstStaticPadTemplate gst_rtp_mp4g_depay_sink_template =
44 GST_STATIC_PAD_TEMPLATE ("sink",
45     GST_PAD_SINK,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("application/x-rtp, "
48         "media = (string) { \"video\", \"audio\", \"application\" }, "
49         "clock-rate = (int) [1, MAX ], "
50         "encoding-name = (string) \"MPEG4-GENERIC\", "
51         /* required string params */
52         /* "streamtype = (string) { \"4\", \"5\" }, "  Not set by Wowza    4 = video, 5 = audio */
53         /* "profile-level-id = (string) [1,MAX], " */
54         /* "config = (string) [1,MAX]" */
55         "mode = (string) { \"generic\", \"CELP-cbr\", \"CELP-vbr\", \"AAC-lbr\", \"AAC-hbr\" } "
56         /* Optional general parameters */
57         /* "objecttype = (string) [1,MAX], " */
58         /* "constantsize = (string) [1,MAX], " *//* constant size of each AU */
59         /* "constantduration = (string) [1,MAX], " *//* constant duration of each AU */
60         /* "maxdisplacement = (string) [1,MAX], " */
61         /* "de-interleavebuffersize = (string) [1,MAX], " */
62         /* Optional configuration parameters */
63         /* "sizelength = (string) [1, 32], " */
64         /* "indexlength = (string) [1, 32], " */
65         /* "indexdeltalength = (string) [1, 32], " */
66         /* "ctsdeltalength = (string) [1, 32], " */
67         /* "dtsdeltalength = (string) [1, 32], " */
68         /* "randomaccessindication = (string) {0, 1}, " */
69         /* "streamstateindication = (string) [0, 32], " */
70         /* "auxiliarydatasizelength = (string) [0, 32]" */ )
71     );
72
73 /* simple bitstream parser */
74 typedef struct
75 {
76   const guint8 *data;
77   const guint8 *end;
78   gint head;                    /* bitpos in the cache of next bit */
79   guint64 cache;                /* cached bytes */
80 } GstBsParse;
81
82 static void
83 gst_bs_parse_init (GstBsParse * bs, const guint8 * data, guint size)
84 {
85   bs->data = data;
86   bs->end = data + size;
87   bs->head = 0;
88   bs->cache = 0xffffffff;
89 }
90
91 static guint32
92 gst_bs_parse_read (GstBsParse * bs, guint n)
93 {
94   guint32 res = 0;
95   gint shift;
96
97   if (n == 0)
98     return res;
99
100   /* fill up the cache if we need to */
101   while (bs->head < n) {
102     if (bs->data >= bs->end) {
103       /* we're at the end, can't produce more than head number of bits */
104       n = bs->head;
105       break;
106     }
107     /* shift bytes in cache, moving the head bits of the cache left */
108     bs->cache = (bs->cache << 8) | *bs->data++;
109     bs->head += 8;
110   }
111
112   /* bring the required bits down and truncate */
113   if ((shift = bs->head - n) > 0)
114     res = bs->cache >> shift;
115   else
116     res = bs->cache;
117
118   /* mask out required bits */
119   if (n < 32)
120     res &= (1 << n) - 1;
121
122   bs->head = shift;
123
124   return res;
125 }
126
127
128 #define gst_rtp_mp4g_depay_parent_class parent_class
129 G_DEFINE_TYPE (GstRtpMP4GDepay, gst_rtp_mp4g_depay,
130     GST_TYPE_RTP_BASE_DEPAYLOAD);
131
132 static void gst_rtp_mp4g_depay_finalize (GObject * object);
133
134 static gboolean gst_rtp_mp4g_depay_setcaps (GstRTPBaseDepayload * depayload,
135     GstCaps * caps);
136 static GstBuffer *gst_rtp_mp4g_depay_process (GstRTPBaseDepayload * depayload,
137     GstBuffer * buf);
138 static gboolean gst_rtp_mp4g_depay_handle_event (GstRTPBaseDepayload * filter,
139     GstEvent * event);
140
141 static GstStateChangeReturn gst_rtp_mp4g_depay_change_state (GstElement *
142     element, GstStateChange transition);
143
144
145 static void
146 gst_rtp_mp4g_depay_class_init (GstRtpMP4GDepayClass * klass)
147 {
148   GObjectClass *gobject_class;
149   GstElementClass *gstelement_class;
150   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
151
152   gobject_class = (GObjectClass *) klass;
153   gstelement_class = (GstElementClass *) klass;
154   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
155
156   gobject_class->finalize = gst_rtp_mp4g_depay_finalize;
157
158   gstelement_class->change_state = gst_rtp_mp4g_depay_change_state;
159
160   gstrtpbasedepayload_class->process = gst_rtp_mp4g_depay_process;
161   gstrtpbasedepayload_class->set_caps = gst_rtp_mp4g_depay_setcaps;
162   gstrtpbasedepayload_class->handle_event = gst_rtp_mp4g_depay_handle_event;
163
164   gst_element_class_add_pad_template (gstelement_class,
165       gst_static_pad_template_get (&gst_rtp_mp4g_depay_src_template));
166   gst_element_class_add_pad_template (gstelement_class,
167       gst_static_pad_template_get (&gst_rtp_mp4g_depay_sink_template));
168
169   gst_element_class_set_static_metadata (gstelement_class,
170       "RTP MPEG4 ES depayloader", "Codec/Depayloader/Network/RTP",
171       "Extracts MPEG4 elementary streams from RTP packets (RFC 3640)",
172       "Wim Taymans <wim.taymans@gmail.com>");
173
174   GST_DEBUG_CATEGORY_INIT (rtpmp4gdepay_debug, "rtpmp4gdepay", 0,
175       "MP4-generic RTP Depayloader");
176 }
177
178 static void
179 gst_rtp_mp4g_depay_init (GstRtpMP4GDepay * rtpmp4gdepay)
180 {
181   rtpmp4gdepay->adapter = gst_adapter_new ();
182   rtpmp4gdepay->packets = g_queue_new ();
183 }
184
185 static void
186 gst_rtp_mp4g_depay_finalize (GObject * object)
187 {
188   GstRtpMP4GDepay *rtpmp4gdepay;
189
190   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
191
192   g_object_unref (rtpmp4gdepay->adapter);
193   rtpmp4gdepay->adapter = NULL;
194   g_queue_free (rtpmp4gdepay->packets);
195   rtpmp4gdepay->packets = NULL;
196
197   G_OBJECT_CLASS (parent_class)->finalize (object);
198 }
199
200 static gint
201 gst_rtp_mp4g_depay_parse_int (GstStructure * structure, const gchar * field,
202     gint def)
203 {
204   const gchar *str;
205   gint res;
206
207   if ((str = gst_structure_get_string (structure, field)))
208     return atoi (str);
209
210   if (gst_structure_get_int (structure, field, &res))
211     return res;
212
213   return def;
214 }
215
216 static gboolean
217 gst_rtp_mp4g_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
218 {
219   GstStructure *structure;
220   GstRtpMP4GDepay *rtpmp4gdepay;
221   GstCaps *srccaps = NULL;
222   const gchar *str;
223   gint clock_rate;
224   gint someint;
225   gboolean res;
226
227   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
228
229   structure = gst_caps_get_structure (caps, 0);
230
231   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
232     clock_rate = 90000;         /* default */
233   depayload->clock_rate = clock_rate;
234
235   if ((str = gst_structure_get_string (structure, "media"))) {
236     if (strcmp (str, "audio") == 0) {
237       srccaps = gst_caps_new_simple ("audio/mpeg",
238           "mpegversion", G_TYPE_INT, 4, "stream-format", G_TYPE_STRING, "raw",
239           NULL);
240     } else if (strcmp (str, "video") == 0) {
241       srccaps = gst_caps_new_simple ("video/mpeg",
242           "mpegversion", G_TYPE_INT, 4,
243           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
244     }
245   }
246   if (srccaps == NULL)
247     goto unknown_media;
248
249   /* these values are optional and have a default value of 0 (no header) */
250   rtpmp4gdepay->sizelength =
251       gst_rtp_mp4g_depay_parse_int (structure, "sizelength", 0);
252   rtpmp4gdepay->indexlength =
253       gst_rtp_mp4g_depay_parse_int (structure, "indexlength", 0);
254   rtpmp4gdepay->indexdeltalength =
255       gst_rtp_mp4g_depay_parse_int (structure, "indexdeltalength", 0);
256   rtpmp4gdepay->ctsdeltalength =
257       gst_rtp_mp4g_depay_parse_int (structure, "ctsdeltalength", 0);
258   rtpmp4gdepay->dtsdeltalength =
259       gst_rtp_mp4g_depay_parse_int (structure, "dtsdeltalength", 0);
260   someint =
261       gst_rtp_mp4g_depay_parse_int (structure, "randomaccessindication", 0);
262   rtpmp4gdepay->randomaccessindication = someint > 0 ? 1 : 0;
263   rtpmp4gdepay->streamstateindication =
264       gst_rtp_mp4g_depay_parse_int (structure, "streamstateindication", 0);
265   rtpmp4gdepay->auxiliarydatasizelength =
266       gst_rtp_mp4g_depay_parse_int (structure, "auxiliarydatasizelength", 0);
267   rtpmp4gdepay->constantSize =
268       gst_rtp_mp4g_depay_parse_int (structure, "constantsize", 0);
269   rtpmp4gdepay->constantDuration =
270       gst_rtp_mp4g_depay_parse_int (structure, "constantduration", 0);
271   rtpmp4gdepay->maxDisplacement =
272       gst_rtp_mp4g_depay_parse_int (structure, "maxdisplacement", 0);
273
274
275   /* get config string */
276   if ((str = gst_structure_get_string (structure, "config"))) {
277     GValue v = { 0 };
278
279     g_value_init (&v, GST_TYPE_BUFFER);
280     if (gst_value_deserialize (&v, str)) {
281       GstBuffer *buffer;
282
283       buffer = gst_value_get_buffer (&v);
284       gst_caps_set_simple (srccaps,
285           "codec_data", GST_TYPE_BUFFER, buffer, NULL);
286       g_value_unset (&v);
287     } else {
288       g_warning ("cannot convert config to buffer");
289     }
290   }
291
292   res = gst_pad_set_caps (depayload->srcpad, srccaps);
293   gst_caps_unref (srccaps);
294
295   return res;
296
297   /* ERRORS */
298 unknown_media:
299   {
300     GST_DEBUG_OBJECT (rtpmp4gdepay, "Unknown media type");
301     return FALSE;
302   }
303 }
304
305 static void
306 gst_rtp_mp4g_depay_clear_queue (GstRtpMP4GDepay * rtpmp4gdepay)
307 {
308   GstBuffer *outbuf;
309
310   while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets)))
311     gst_buffer_unref (outbuf);
312 }
313
314 static void
315 gst_rtp_mp4g_depay_reset (GstRtpMP4GDepay * rtpmp4gdepay)
316 {
317   gst_adapter_clear (rtpmp4gdepay->adapter);
318   rtpmp4gdepay->max_AU_index = -1;
319   rtpmp4gdepay->next_AU_index = -1;
320   rtpmp4gdepay->prev_AU_index = -1;
321   rtpmp4gdepay->prev_rtptime = -1;
322   rtpmp4gdepay->last_AU_index = -1;
323   gst_rtp_mp4g_depay_clear_queue (rtpmp4gdepay);
324 }
325
326 static void
327 gst_rtp_mp4g_depay_flush_queue (GstRtpMP4GDepay * rtpmp4gdepay)
328 {
329   GstBuffer *outbuf;
330   gboolean discont = FALSE;
331   guint AU_index;
332
333   while ((outbuf = g_queue_pop_head (rtpmp4gdepay->packets))) {
334     AU_index = GST_BUFFER_OFFSET (outbuf);
335
336     GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
337
338     if (rtpmp4gdepay->next_AU_index != AU_index) {
339       GST_DEBUG_OBJECT (rtpmp4gdepay, "discont, expected AU_index %u",
340           rtpmp4gdepay->next_AU_index);
341       discont = TRUE;
342     }
343
344     if (discont) {
345       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
346       discont = FALSE;
347     }
348
349     GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing AU_index %u", AU_index);
350     gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpmp4gdepay), outbuf);
351     rtpmp4gdepay->next_AU_index = AU_index + 1;
352   }
353 }
354
355 static void
356 gst_rtp_mp4g_depay_queue (GstRtpMP4GDepay * rtpmp4gdepay, GstBuffer * outbuf)
357 {
358   guint AU_index = GST_BUFFER_OFFSET (outbuf);
359
360   if (rtpmp4gdepay->next_AU_index == -1) {
361     GST_DEBUG_OBJECT (rtpmp4gdepay, "Init AU counter %u", AU_index);
362     rtpmp4gdepay->next_AU_index = AU_index;
363   }
364
365   if (rtpmp4gdepay->next_AU_index == AU_index) {
366     GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing expected AU_index %u", AU_index);
367
368     /* we received the expected packet, push it and flush as much as we can from
369      * the queue */
370     gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpmp4gdepay), outbuf);
371     rtpmp4gdepay->next_AU_index++;
372
373     while ((outbuf = g_queue_peek_head (rtpmp4gdepay->packets))) {
374       AU_index = GST_BUFFER_OFFSET (outbuf);
375
376       GST_DEBUG_OBJECT (rtpmp4gdepay, "next available AU_index %u", AU_index);
377
378       if (rtpmp4gdepay->next_AU_index == AU_index) {
379         GST_DEBUG_OBJECT (rtpmp4gdepay, "pushing expected AU_index %u",
380             AU_index);
381         outbuf = g_queue_pop_head (rtpmp4gdepay->packets);
382         gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpmp4gdepay),
383             outbuf);
384         rtpmp4gdepay->next_AU_index++;
385       } else {
386         GST_DEBUG_OBJECT (rtpmp4gdepay, "waiting for next AU_index %u",
387             rtpmp4gdepay->next_AU_index);
388         break;
389       }
390     }
391   } else {
392     GList *list;
393
394     GST_DEBUG_OBJECT (rtpmp4gdepay, "queueing AU_index %u", AU_index);
395
396     /* loop the list to skip strictly smaller AU_index buffers */
397     for (list = rtpmp4gdepay->packets->head; list; list = g_list_next (list)) {
398       guint idx;
399       gint gap;
400
401       idx = GST_BUFFER_OFFSET (GST_BUFFER_CAST (list->data));
402
403       /* compare the new seqnum to the one in the buffer */
404       gap = (gint) (idx - AU_index);
405
406       GST_DEBUG_OBJECT (rtpmp4gdepay, "compare with AU_index %u, gap %d", idx,
407           gap);
408
409       /* AU_index <= idx, we can stop looking */
410       if (G_LIKELY (gap > 0))
411         break;
412     }
413     if (G_LIKELY (list))
414       g_queue_insert_before (rtpmp4gdepay->packets, list, outbuf);
415     else
416       g_queue_push_tail (rtpmp4gdepay->packets, outbuf);
417   }
418 }
419
420 static GstBuffer *
421 gst_rtp_mp4g_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
422 {
423   GstRtpMP4GDepay *rtpmp4gdepay;
424   GstBuffer *outbuf = NULL;
425   GstClockTime timestamp;
426   GstRTPBuffer rtp = { NULL };
427
428   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
429
430   /* flush remaining data on discont */
431   if (GST_BUFFER_IS_DISCONT (buf)) {
432     GST_DEBUG_OBJECT (rtpmp4gdepay, "received DISCONT");
433     gst_adapter_clear (rtpmp4gdepay->adapter);
434   }
435
436   timestamp = GST_BUFFER_TIMESTAMP (buf);
437
438   {
439     gint payload_len, payload_AU;
440     guint8 *payload;
441     guint32 rtptime;
442     guint AU_headers_len;
443     guint AU_size, AU_index, AU_index_delta, payload_AU_size;
444     gboolean M;
445
446     gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
447     payload_len = gst_rtp_buffer_get_payload_len (&rtp);
448     payload = gst_rtp_buffer_get_payload (&rtp);
449
450     GST_DEBUG_OBJECT (rtpmp4gdepay, "received payload of %d", payload_len);
451
452     rtptime = gst_rtp_buffer_get_timestamp (&rtp);
453     M = gst_rtp_buffer_get_marker (&rtp);
454
455     if (rtpmp4gdepay->sizelength > 0) {
456       gint num_AU_headers, AU_headers_bytes, i;
457       GstBsParse bs;
458
459       if (payload_len < 2)
460         goto short_payload;
461
462       /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
463        * |AU-headers-length|AU-header|AU-header|      |AU-header|padding|
464        * |                 |   (1)   |   (2)   |      |   (n) * | bits  |
465        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
466        *
467        * The length is 2 bytes and contains the length of the following
468        * AU-headers in bits.
469        */
470       AU_headers_len = (payload[0] << 8) | payload[1];
471       AU_headers_bytes = (AU_headers_len + 7) / 8;
472       num_AU_headers = AU_headers_len / 16;
473
474       GST_DEBUG_OBJECT (rtpmp4gdepay, "AU headers len %d, bytes %d, num %d",
475           AU_headers_len, AU_headers_bytes, num_AU_headers);
476
477       /* skip header */
478       payload += 2;
479       payload_len -= 2;
480
481       if (payload_len < AU_headers_bytes)
482         goto short_payload;
483
484       /* skip special headers, point to first payload AU */
485       payload_AU = 2 + AU_headers_bytes;
486       payload_AU_size = payload_len - AU_headers_bytes;
487
488       if (G_UNLIKELY (rtpmp4gdepay->auxiliarydatasizelength)) {
489         gint aux_size;
490
491         /* point the bitstream parser to the first auxiliary data bit */
492         gst_bs_parse_init (&bs, payload + AU_headers_bytes,
493             payload_len - AU_headers_bytes);
494         aux_size =
495             gst_bs_parse_read (&bs, rtpmp4gdepay->auxiliarydatasizelength);
496         /* convert to bytes */
497         aux_size = (aux_size + 7) / 8;
498         /* AU data then follows auxiliary data */
499         if (payload_AU_size < aux_size)
500           goto short_payload;
501         payload_AU += aux_size;
502         payload_AU_size -= aux_size;
503       }
504
505       /* point the bitstream parser to the first AU header bit */
506       gst_bs_parse_init (&bs, payload, payload_len);
507       AU_index = AU_index_delta = 0;
508
509       for (i = 0; i < num_AU_headers && payload_AU_size > 0; i++) {
510         /* parse AU header
511          *  +---------------------------------------+
512          *  |     AU-size                           |
513          *  +---------------------------------------+
514          *  |     AU-Index / AU-Index-delta         |
515          *  +---------------------------------------+
516          *  |     CTS-flag                          |
517          *  +---------------------------------------+
518          *  |     CTS-delta                         |
519          *  +---------------------------------------+
520          *  |     DTS-flag                          |
521          *  +---------------------------------------+
522          *  |     DTS-delta                         |
523          *  +---------------------------------------+
524          *  |     RAP-flag                          |
525          *  +---------------------------------------+
526          *  |     Stream-state                      |
527          *  +---------------------------------------+
528          */
529         AU_size = gst_bs_parse_read (&bs, rtpmp4gdepay->sizelength);
530
531         /* calculate the AU_index, which is only on the first AU of the packet
532          * and the AU_index_delta on the other AUs. This will be used to
533          * reconstruct the AU ordering when interleaving. */
534         if (i == 0) {
535           AU_index = gst_bs_parse_read (&bs, rtpmp4gdepay->indexlength);
536
537           GST_DEBUG_OBJECT (rtpmp4gdepay, "AU index %u", AU_index);
538
539           if (AU_index == 0 && rtpmp4gdepay->prev_AU_index == 0) {
540             gint diff;
541             gint cd;
542
543             /* if we see two consecutive packets with AU_index of 0, we can
544              * assume we have constantDuration packets. Since we don't have
545              * the index we must use the AU duration to calculate the
546              * index. Get the diff between the timestamps first, this can be
547              * positive or negative. */
548             if (rtpmp4gdepay->prev_rtptime <= rtptime)
549               diff = rtptime - rtpmp4gdepay->prev_rtptime;
550             else
551               diff = -(rtpmp4gdepay->prev_rtptime - rtptime);
552
553             /* if no constantDuration was given, make one */
554             if (rtpmp4gdepay->constantDuration != 0) {
555               cd = rtpmp4gdepay->constantDuration;
556               GST_DEBUG_OBJECT (depayload, "using constantDuration %d", cd);
557             } else if (rtpmp4gdepay->prev_AU_num > 0) {
558               /* use number of packets and of previous frame */
559               cd = diff / rtpmp4gdepay->prev_AU_num;
560               GST_DEBUG_OBJECT (depayload, "guessing constantDuration %d", cd);
561             } else {
562               /* assume this frame has the same number of packets as the
563                * previous one */
564               cd = diff / num_AU_headers;
565               GST_DEBUG_OBJECT (depayload, "guessing constantDuration %d", cd);
566             }
567
568             if (cd > 0) {
569               /* get the number of packets by dividing with the duration */
570               diff /= cd;
571             } else {
572               diff = 0;
573             }
574
575             rtpmp4gdepay->last_AU_index += diff;
576             rtpmp4gdepay->prev_AU_index = AU_index;
577
578             AU_index = rtpmp4gdepay->last_AU_index;
579
580             GST_DEBUG_OBJECT (rtpmp4gdepay, "diff %d, AU index %u", diff,
581                 AU_index);
582           } else {
583             rtpmp4gdepay->prev_AU_index = AU_index;
584             rtpmp4gdepay->last_AU_index = AU_index;
585           }
586
587           /* keep track of the higest AU_index */
588           if (rtpmp4gdepay->max_AU_index != -1
589               && rtpmp4gdepay->max_AU_index <= AU_index) {
590             GST_DEBUG_OBJECT (rtpmp4gdepay, "new interleave group, flushing");
591             /* a new interleave group started, flush */
592             gst_rtp_mp4g_depay_flush_queue (rtpmp4gdepay);
593           }
594           if (G_UNLIKELY (!rtpmp4gdepay->maxDisplacement &&
595                   rtpmp4gdepay->max_AU_index != -1
596                   && rtpmp4gdepay->max_AU_index >= AU_index)) {
597             GstBuffer *outbuf;
598
599             /* some broken non-interleaved streams have AU-index jumping around
600              * all over the place, apparently assuming receiver disregards */
601             GST_DEBUG_OBJECT (rtpmp4gdepay, "non-interleaved broken AU indices;"
602                 " forcing continuous flush");
603             /* reset AU to avoid repeated DISCONT in such case */
604             outbuf = g_queue_peek_head (rtpmp4gdepay->packets);
605             if (G_LIKELY (outbuf)) {
606               rtpmp4gdepay->next_AU_index = GST_BUFFER_OFFSET (outbuf);
607               gst_rtp_mp4g_depay_flush_queue (rtpmp4gdepay);
608             }
609             /* rebase next_AU_index to current rtp's first AU_index */
610             rtpmp4gdepay->next_AU_index = AU_index;
611           }
612           rtpmp4gdepay->prev_rtptime = rtptime;
613           rtpmp4gdepay->prev_AU_num = num_AU_headers;
614         } else {
615           AU_index_delta =
616               gst_bs_parse_read (&bs, rtpmp4gdepay->indexdeltalength);
617           AU_index += AU_index_delta + 1;
618         }
619         /* keep track of highest AU_index */
620         if (rtpmp4gdepay->max_AU_index == -1
621             || AU_index > rtpmp4gdepay->max_AU_index)
622           rtpmp4gdepay->max_AU_index = AU_index;
623
624         /* the presentation time offset, a 2s-complement value, we need this to
625          * calculate the timestamp on the output packet. */
626         if (rtpmp4gdepay->ctsdeltalength > 0) {
627           if (gst_bs_parse_read (&bs, 1))
628             gst_bs_parse_read (&bs, rtpmp4gdepay->ctsdeltalength);
629         }
630         /* the decoding time offset, a 2s-complement value */
631         if (rtpmp4gdepay->dtsdeltalength > 0) {
632           if (gst_bs_parse_read (&bs, 1))
633             gst_bs_parse_read (&bs, rtpmp4gdepay->dtsdeltalength);
634         }
635         /* RAP-flag to indicate that the AU contains a keyframe */
636         if (rtpmp4gdepay->randomaccessindication)
637           gst_bs_parse_read (&bs, 1);
638         /* stream-state */
639         if (rtpmp4gdepay->streamstateindication > 0)
640           gst_bs_parse_read (&bs, rtpmp4gdepay->streamstateindication);
641
642         GST_DEBUG_OBJECT (rtpmp4gdepay, "size %d, index %d, delta %d", AU_size,
643             AU_index, AU_index_delta);
644
645         /* fragmented pakets have the AU_size set to the size of the
646          * unfragmented AU. */
647         if (AU_size > payload_AU_size)
648           AU_size = payload_AU_size;
649
650         /* collect stuff in the adapter, strip header from payload and push in
651          * the adapter */
652         outbuf =
653             gst_rtp_buffer_get_payload_subbuffer (&rtp, payload_AU, AU_size);
654         gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
655
656         if (M) {
657           guint avail;
658
659           /* packet is complete, flush */
660           avail = gst_adapter_available (rtpmp4gdepay->adapter);
661
662           outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
663
664           /* copy some of the fields we calculated above on the buffer. We also
665            * copy the AU_index so that we can sort the packets in our queue. */
666           GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
667           GST_BUFFER_OFFSET (outbuf) = AU_index;
668
669           /* make sure we don't use the timestamp again for other AUs in this
670            * RTP packet. */
671           timestamp = -1;
672
673           GST_DEBUG_OBJECT (depayload,
674               "pushing buffer of size %" G_GSIZE_FORMAT,
675               gst_buffer_get_size (outbuf));
676
677           gst_rtp_mp4g_depay_queue (rtpmp4gdepay, outbuf);
678
679         }
680         payload_AU += AU_size;
681         payload_AU_size -= AU_size;
682       }
683     } else {
684       /* push complete buffer in adapter */
685       outbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 0, payload_len);
686       gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
687
688       /* if this was the last packet of the VOP, create and push a buffer */
689       if (M) {
690         guint avail;
691
692         avail = gst_adapter_available (rtpmp4gdepay->adapter);
693
694         outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
695
696         GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %"
697             G_GSIZE_FORMAT, gst_buffer_get_size (outbuf));
698
699         gst_rtp_buffer_unmap (&rtp);
700         return outbuf;
701       }
702     }
703   }
704
705   gst_rtp_buffer_unmap (&rtp);
706   return NULL;
707
708   /* ERRORS */
709 short_payload:
710   {
711     GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
712         ("Packet payload was too short."), (NULL));
713     gst_rtp_buffer_unmap (&rtp);
714     return NULL;
715   }
716 }
717
718 static gboolean
719 gst_rtp_mp4g_depay_handle_event (GstRTPBaseDepayload * filter, GstEvent * event)
720 {
721   gboolean ret;
722   GstRtpMP4GDepay *rtpmp4gdepay;
723
724   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (filter);
725
726   switch (GST_EVENT_TYPE (event)) {
727     case GST_EVENT_FLUSH_STOP:
728       gst_rtp_mp4g_depay_reset (rtpmp4gdepay);
729       break;
730     default:
731       break;
732   }
733
734   ret =
735       GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (filter, event);
736
737   return ret;
738 }
739
740 static GstStateChangeReturn
741 gst_rtp_mp4g_depay_change_state (GstElement * element,
742     GstStateChange transition)
743 {
744   GstRtpMP4GDepay *rtpmp4gdepay;
745   GstStateChangeReturn ret;
746
747   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
748
749   switch (transition) {
750     case GST_STATE_CHANGE_READY_TO_PAUSED:
751       gst_rtp_mp4g_depay_reset (rtpmp4gdepay);
752       break;
753     default:
754       break;
755   }
756
757   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
758
759   switch (transition) {
760     case GST_STATE_CHANGE_PAUSED_TO_READY:
761       gst_rtp_mp4g_depay_reset (rtpmp4gdepay);
762       break;
763     default:
764       break;
765   }
766   return ret;
767 }
768
769 gboolean
770 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
771 {
772   return gst_element_register (plugin, "rtpmp4gdepay",
773       GST_RANK_SECONDARY, GST_TYPE_RTP_MP4G_DEPAY);
774 }