webrtc/nice: support consent-freshness RFC7675
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtpvp8depay.c
1 /* gstrtpvp8depay.c - Source for GstRtpVP8Depay
2  * Copyright (C) 2011 Sjoerd Simons <sjoerd@luon.net>
3  * Copyright (C) 2011 Collabora Ltd.
4  *   Contact: Youness Alaoui <youness.alaoui@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "gstrtpelements.h"
26 #include "gstrtpvp8depay.h"
27 #include "gstrtputils.h"
28
29 #include <gst/video/video.h>
30
31 #include <stdio.h>
32
33 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_depay_debug);
34 #define GST_CAT_DEFAULT gst_rtp_vp8_depay_debug
35
36 static void gst_rtp_vp8_depay_dispose (GObject * object);
37 static void gst_rtp_vp8_depay_get_property (GObject * object, guint prop_id,
38     GValue * value, GParamSpec * pspec);
39 static void gst_rtp_vp8_depay_set_property (GObject * object, guint prop_id,
40     const GValue * value, GParamSpec * pspec);
41 static GstBuffer *gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depayload,
42     GstRTPBuffer * rtp);
43 static GstStateChangeReturn gst_rtp_vp8_depay_change_state (GstElement *
44     element, GstStateChange transition);
45 static gboolean gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay,
46     GstEvent * event);
47 static gboolean gst_rtp_vp8_depay_packet_lost (GstRTPBaseDepayload * depay,
48     GstEvent * event);
49
50 G_DEFINE_TYPE (GstRtpVP8Depay, gst_rtp_vp8_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
51 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpvp8depay, "rtpvp8depay",
52     GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_DEPAY, rtp_element_init (plugin));
53
54 static GstStaticPadTemplate gst_rtp_vp8_depay_src_template =
55 GST_STATIC_PAD_TEMPLATE ("src",
56     GST_PAD_SRC,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS ("video/x-vp8"));
59
60 static GstStaticPadTemplate gst_rtp_vp8_depay_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
62     GST_PAD_SINK,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("application/x-rtp, "
65         "clock-rate = (int) 90000,"
66         "media = (string) \"video\","
67         "encoding-name = (string) { \"VP8\", \"VP8-DRAFT-IETF-01\" }"));
68
69 #define DEFAULT_WAIT_FOR_KEYFRAME FALSE
70 #define DEFAULT_REQUEST_KEYFRAME FALSE
71
72 enum
73 {
74   PROP_0,
75   PROP_WAIT_FOR_KEYFRAME,
76   PROP_REQUEST_KEYFRAME,
77 };
78
79 #define PICTURE_ID_NONE (UINT_MAX)
80 #define IS_PICTURE_ID_15BITS(pid) (((guint)(pid) & 0x8000) != 0)
81
82 static void
83 gst_rtp_vp8_depay_init (GstRtpVP8Depay * self)
84 {
85   self->adapter = gst_adapter_new ();
86   self->started = FALSE;
87   self->wait_for_keyframe = DEFAULT_WAIT_FOR_KEYFRAME;
88   self->request_keyframe = DEFAULT_REQUEST_KEYFRAME;
89   self->last_pushed_was_lost_event = FALSE;
90 }
91
92 static void
93 gst_rtp_vp8_depay_class_init (GstRtpVP8DepayClass * gst_rtp_vp8_depay_class)
94 {
95   GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp8_depay_class);
96   GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp8_depay_class);
97   GstRTPBaseDepayloadClass *depay_class =
98       (GstRTPBaseDepayloadClass *) (gst_rtp_vp8_depay_class);
99
100   gst_element_class_add_static_pad_template (element_class,
101       &gst_rtp_vp8_depay_sink_template);
102   gst_element_class_add_static_pad_template (element_class,
103       &gst_rtp_vp8_depay_src_template);
104
105   gst_element_class_set_static_metadata (element_class, "RTP VP8 depayloader",
106       "Codec/Depayloader/Network/RTP",
107       "Extracts VP8 video from RTP packets)",
108       "Sjoerd Simons <sjoerd@luon.net>");
109
110   object_class->dispose = gst_rtp_vp8_depay_dispose;
111   object_class->set_property = gst_rtp_vp8_depay_set_property;
112   object_class->get_property = gst_rtp_vp8_depay_get_property;
113
114   g_object_class_install_property (object_class, PROP_WAIT_FOR_KEYFRAME,
115       g_param_spec_boolean ("wait-for-keyframe", "Wait for Keyframe",
116           "Wait for the next keyframe after packet loss",
117           DEFAULT_WAIT_FOR_KEYFRAME,
118           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
119
120   /**
121    * GstRtpVP8Depay:request-keyframe:
122    *
123    * Request new keyframe when packet loss is detected
124    *
125    * Since: 1.20
126    */
127   g_object_class_install_property (object_class, PROP_REQUEST_KEYFRAME,
128       g_param_spec_boolean ("request-keyframe", "Request Keyframe",
129           "Request new keyframe when packet loss is detected",
130           DEFAULT_REQUEST_KEYFRAME,
131           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132
133   element_class->change_state = gst_rtp_vp8_depay_change_state;
134
135   depay_class->process_rtp_packet = gst_rtp_vp8_depay_process;
136   depay_class->handle_event = gst_rtp_vp8_depay_handle_event;
137   depay_class->packet_lost = gst_rtp_vp8_depay_packet_lost;
138
139   GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_depay_debug, "rtpvp8depay", 0,
140       "VP8 Video RTP Depayloader");
141 }
142
143 static void
144 gst_rtp_vp8_depay_dispose (GObject * object)
145 {
146   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
147
148   if (self->adapter != NULL)
149     g_object_unref (self->adapter);
150   self->adapter = NULL;
151
152   /* release any references held by the object here */
153
154   if (G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose)
155     G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose (object);
156 }
157
158 static void
159 gst_rtp_vp8_depay_set_property (GObject * object, guint prop_id,
160     const GValue * value, GParamSpec * pspec)
161 {
162   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
163
164   switch (prop_id) {
165     case PROP_WAIT_FOR_KEYFRAME:
166       self->wait_for_keyframe = g_value_get_boolean (value);
167       break;
168     case PROP_REQUEST_KEYFRAME:
169       self->request_keyframe = g_value_get_boolean (value);
170       break;
171     default:
172       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173       break;
174   }
175 }
176
177 static void
178 gst_rtp_vp8_depay_get_property (GObject * object, guint prop_id,
179     GValue * value, GParamSpec * pspec)
180 {
181   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
182
183   switch (prop_id) {
184     case PROP_WAIT_FOR_KEYFRAME:
185       g_value_set_boolean (value, self->wait_for_keyframe);
186       break;
187     case PROP_REQUEST_KEYFRAME:
188       g_value_set_boolean (value, self->request_keyframe);
189       break;
190     default:
191       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
192       break;
193   }
194 }
195
196 static gint
197 picture_id_compare (guint16 id0, guint16 id1)
198 {
199   guint shift = 16 - (IS_PICTURE_ID_15BITS (id1) ? 15 : 7);
200   id0 = id0 << shift;
201   id1 = id1 << shift;
202   return ((gint16) (id1 - id0)) >> shift;
203 }
204
205 static void
206 send_last_lost_event (GstRtpVP8Depay * self)
207 {
208   if (self->last_lost_event) {
209     GST_ERROR_OBJECT (self,
210         "Sending the last stopped lost event: %" GST_PTR_FORMAT,
211         self->last_lost_event);
212     GST_RTP_BASE_DEPAYLOAD_CLASS (gst_rtp_vp8_depay_parent_class)
213         ->packet_lost (GST_RTP_BASE_DEPAYLOAD_CAST (self),
214         self->last_lost_event);
215     gst_event_replace (&self->last_lost_event, NULL);
216     self->last_pushed_was_lost_event = TRUE;
217   }
218 }
219
220 static void
221 send_new_lost_event (GstRtpVP8Depay * self, GstClockTime timestamp,
222     guint new_picture_id, const gchar * reason)
223 {
224   GstEvent *event;
225
226   if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
227     GST_WARNING_OBJECT (self, "Can't create lost event with invalid timestmap");
228     return;
229   }
230
231   event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
232       gst_structure_new ("GstRTPPacketLost",
233           "timestamp", G_TYPE_UINT64, timestamp,
234           "duration", G_TYPE_UINT64, G_GUINT64_CONSTANT (0), NULL));
235
236   GST_DEBUG_OBJECT (self, "Pushing lost event "
237       "(picids 0x%x 0x%x, reason \"%s\"): %" GST_PTR_FORMAT,
238       self->last_picture_id, new_picture_id, reason, event);
239
240   GST_RTP_BASE_DEPAYLOAD_CLASS (gst_rtp_vp8_depay_parent_class)
241       ->packet_lost (GST_RTP_BASE_DEPAYLOAD_CAST (self), event);
242
243   gst_event_unref (event);
244 }
245
246 static void
247 send_last_lost_event_if_needed (GstRtpVP8Depay * self, guint new_picture_id)
248 {
249   if (self->last_picture_id == PICTURE_ID_NONE)
250     return;
251
252   if (self->last_lost_event) {
253     gboolean send_lost_event = FALSE;
254     if (new_picture_id == PICTURE_ID_NONE) {
255       GST_DEBUG_OBJECT (self, "Dropping the last stopped lost event "
256           "(picture id does not exist): %" GST_PTR_FORMAT,
257           self->last_lost_event);
258     } else if (IS_PICTURE_ID_15BITS (self->last_picture_id) &&
259         !IS_PICTURE_ID_15BITS (new_picture_id)) {
260       GST_DEBUG_OBJECT (self, "Dropping the last stopped lost event "
261           "(picture id has less bits than before): %" GST_PTR_FORMAT,
262           self->last_lost_event);
263     } else if (picture_id_compare (self->last_picture_id, new_picture_id) != 1) {
264       GstStructure *s = gst_event_writable_structure (self->last_lost_event);
265
266       GST_DEBUG_OBJECT (self, "Sending the last stopped lost event "
267           "(gap in picture id %u %u): %" GST_PTR_FORMAT,
268           self->last_picture_id, new_picture_id, self->last_lost_event);
269       send_lost_event = TRUE;
270       /* Prevent rtpbasedepayload from dropping the event now
271        * that we have made sure the lost packet was not FEC */
272       gst_structure_remove_field (s, "might-have-been-fec");
273     }
274     if (send_lost_event)
275       GST_RTP_BASE_DEPAYLOAD_CLASS (gst_rtp_vp8_depay_parent_class)
276           ->packet_lost (GST_RTP_BASE_DEPAYLOAD_CAST (self),
277           self->last_lost_event);
278
279     gst_event_replace (&self->last_lost_event, NULL);
280   }
281 }
282
283 static GstBuffer *
284 gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depay, GstRTPBuffer * rtp)
285 {
286   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
287   GstBuffer *payload;
288   guint8 *data;
289   guint hdrsize = 1;
290   guint picture_id = PICTURE_ID_NONE;
291   guint size = gst_rtp_buffer_get_payload_len (rtp);
292   guint s_bit;
293   guint part_id;
294   gboolean frame_start;
295   gboolean sent_lost_event = FALSE;
296
297   if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (rtp->buffer))) {
298     GST_DEBUG_OBJECT (self, "Discontinuity, flushing adapter");
299     gst_adapter_clear (self->adapter);
300     self->started = FALSE;
301
302     if (self->wait_for_keyframe)
303       self->waiting_for_keyframe = TRUE;
304
305     if (self->request_keyframe)
306       gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
307           gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
308               TRUE, 0));
309   }
310
311   /* At least one header and one vp8 byte */
312   if (G_UNLIKELY (size < 2))
313     goto too_small;
314
315   data = gst_rtp_buffer_get_payload (rtp);
316
317   s_bit = (data[0] >> 4) & 0x1;
318   part_id = (data[0] >> 0) & 0x7;
319
320   /* Check X optional header */
321   if ((data[0] & 0x80) != 0) {
322     hdrsize++;
323     /* Check I optional header */
324     if ((data[1] & 0x80) != 0) {
325       if (G_UNLIKELY (size < 3))
326         goto too_small;
327       hdrsize++;
328       /* Check for 16 bits PictureID */
329       picture_id = data[2];
330       if ((data[2] & 0x80) != 0) {
331         if (G_UNLIKELY (size < 4))
332           goto too_small;
333         hdrsize++;
334         picture_id = (picture_id << 8) | data[3];
335       }
336     }
337     /* Check L optional header */
338     if ((data[1] & 0x40) != 0)
339       hdrsize++;
340     /* Check T or K optional headers */
341     if ((data[1] & 0x20) != 0 || (data[1] & 0x10) != 0)
342       hdrsize++;
343   }
344   GST_LOG_OBJECT (depay,
345       "hdrsize %u, size %u, picture id 0x%x, s %u, part_id %u", hdrsize, size,
346       picture_id, s_bit, part_id);
347   if (G_UNLIKELY (hdrsize >= size))
348     goto too_small;
349
350   frame_start = (s_bit == 1) && (part_id == 0);
351   if (frame_start) {
352     if (G_UNLIKELY (self->started)) {
353       GST_DEBUG_OBJECT (depay, "Incomplete frame, flushing adapter");
354       gst_adapter_clear (self->adapter);
355       self->started = FALSE;
356
357       if (self->wait_for_keyframe)
358         self->waiting_for_keyframe = TRUE;
359       if (self->request_keyframe)
360         gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
361             gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
362                 TRUE, 0));
363
364       send_new_lost_event (self, GST_BUFFER_PTS (rtp->buffer), picture_id,
365           "Incomplete frame detected");
366       sent_lost_event = TRUE;
367     }
368   }
369
370   if (!self->started) {
371     if (G_UNLIKELY (!frame_start)) {
372       GST_DEBUG_OBJECT (depay,
373           "The frame is missing the first packet, ignoring the packet");
374       if (self->stop_lost_events && !sent_lost_event) {
375         send_last_lost_event (self);
376         self->stop_lost_events = FALSE;
377       }
378
379       if (self->wait_for_keyframe)
380         self->waiting_for_keyframe = TRUE;
381       if (self->request_keyframe)
382         gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
383             gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
384                 TRUE, 0));
385
386       goto done;
387     }
388
389     GST_LOG_OBJECT (depay, "Found the start of the frame");
390
391     if (self->stop_lost_events && !sent_lost_event) {
392       send_last_lost_event_if_needed (self, picture_id);
393       self->stop_lost_events = FALSE;
394     }
395
396     self->started = TRUE;
397   }
398
399   payload = gst_rtp_buffer_get_payload_subbuffer (rtp, hdrsize, -1);
400   gst_adapter_push (self->adapter, payload);
401   self->last_picture_id = picture_id;
402
403   /* Marker indicates that it was the last rtp packet for this frame */
404   if (gst_rtp_buffer_get_marker (rtp)) {
405     GstBuffer *out;
406     guint8 header[10];
407
408     GST_LOG_OBJECT (depay,
409         "Found the end of the frame (%" G_GSIZE_FORMAT " bytes)",
410         gst_adapter_available (self->adapter));
411     if (gst_adapter_available (self->adapter) < 10)
412       goto too_small;
413     gst_adapter_copy (self->adapter, &header, 0, 10);
414
415     out = gst_adapter_take_buffer (self->adapter,
416         gst_adapter_available (self->adapter));
417
418     self->started = FALSE;
419
420     /* mark keyframes */
421     out = gst_buffer_make_writable (out);
422     /* Filter away all metas that are not sensible to copy */
423     gst_rtp_drop_non_video_meta (self, out);
424     if ((header[0] & 0x01)) {
425       GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
426
427       if (self->waiting_for_keyframe) {
428         gst_buffer_unref (out);
429         out = NULL;
430         GST_INFO_OBJECT (self, "Dropping inter-frame before intra-frame");
431         gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
432             gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
433                 TRUE, 0));
434       }
435     } else {
436       guint profile, width, height;
437
438       GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
439       GST_DEBUG_OBJECT (self, "Processed keyframe");
440
441       profile = (header[0] & 0x0e) >> 1;
442       width = GST_READ_UINT16_LE (header + 6) & 0x3fff;
443       height = GST_READ_UINT16_LE (header + 8) & 0x3fff;
444
445       if (G_UNLIKELY (self->last_width != width ||
446               self->last_height != height || self->last_profile != profile)) {
447         gchar profile_str[3];
448         GstCaps *srccaps;
449
450         snprintf (profile_str, 3, "%u", profile);
451         srccaps = gst_caps_new_simple ("video/x-vp8",
452             "framerate", GST_TYPE_FRACTION, 0, 1,
453             "height", G_TYPE_INT, height,
454             "width", G_TYPE_INT, width,
455             "profile", G_TYPE_STRING, profile_str, NULL);
456
457         gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
458         gst_caps_unref (srccaps);
459
460         self->last_width = width;
461         self->last_height = height;
462         self->last_profile = profile;
463       }
464       self->waiting_for_keyframe = FALSE;
465     }
466
467     if (picture_id != PICTURE_ID_NONE)
468       self->stop_lost_events = TRUE;
469
470     self->last_pushed_was_lost_event = FALSE;
471
472     return out;
473   }
474
475 done:
476   return NULL;
477
478 too_small:
479   GST_DEBUG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
480   gst_adapter_clear (self->adapter);
481   self->started = FALSE;
482
483   goto done;
484 }
485
486 static GstStateChangeReturn
487 gst_rtp_vp8_depay_change_state (GstElement * element, GstStateChange transition)
488 {
489   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (element);
490
491   switch (transition) {
492     case GST_STATE_CHANGE_READY_TO_PAUSED:
493       self->last_profile = -1;
494       self->last_height = -1;
495       self->last_width = -1;
496       self->waiting_for_keyframe = TRUE;
497       self->caps_sent = FALSE;
498       self->last_picture_id = PICTURE_ID_NONE;
499       gst_event_replace (&self->last_lost_event, NULL);
500       self->stop_lost_events = FALSE;
501       break;
502     default:
503       break;
504   }
505
506   return
507       GST_ELEMENT_CLASS (gst_rtp_vp8_depay_parent_class)->change_state (element,
508       transition);
509 }
510
511 static gboolean
512 gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
513 {
514   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
515
516   switch (GST_EVENT_TYPE (event)) {
517     case GST_EVENT_FLUSH_STOP:
518       self->last_profile = -1;
519       self->last_height = -1;
520       self->last_width = -1;
521       self->last_picture_id = PICTURE_ID_NONE;
522       gst_event_replace (&self->last_lost_event, NULL);
523       self->stop_lost_events = FALSE;
524       break;
525     default:
526       break;
527   }
528
529   return
530       GST_RTP_BASE_DEPAYLOAD_CLASS
531       (gst_rtp_vp8_depay_parent_class)->handle_event (depay, event);
532 }
533
534 static gboolean
535 gst_rtp_vp8_depay_packet_lost (GstRTPBaseDepayload * depay, GstEvent * event)
536 {
537   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
538   const GstStructure *s;
539   gboolean might_have_been_fec;
540   gboolean unref_event = FALSE;
541   gboolean ret;
542
543   s = gst_event_get_structure (event);
544
545   if (self->stop_lost_events) {
546     if (gst_structure_get_boolean (s, "might-have-been-fec",
547             &might_have_been_fec)
548         && might_have_been_fec) {
549       GST_DEBUG_OBJECT (depay, "Stopping lost event %" GST_PTR_FORMAT, event);
550       gst_event_replace (&self->last_lost_event, event);
551       return TRUE;
552     }
553   } else if (self->last_picture_id != PICTURE_ID_NONE) {
554     GstStructure *s;
555
556     if (!gst_event_is_writable (event)) {
557       event = gst_event_copy (event);
558       unref_event = TRUE;
559     }
560
561     s = gst_event_writable_structure (event);
562
563     /* We are currently processing a picture, let's make sure the
564      * base depayloader doesn't drop this lost event */
565     gst_structure_remove_field (s, "might-have-been-fec");
566   }
567
568   self->last_pushed_was_lost_event = TRUE;
569
570   ret =
571       GST_RTP_BASE_DEPAYLOAD_CLASS
572       (gst_rtp_vp8_depay_parent_class)->packet_lost (depay, event);
573
574   if (unref_event)
575     gst_event_unref (event);
576
577   return ret;
578 }