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>
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.
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.
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
25 #include "gstrtpelements.h"
26 #include "gstrtpvp8depay.h"
27 #include "gstrtputils.h"
29 #include <gst/video/video.h>
33 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_depay_debug);
34 #define GST_CAT_DEFAULT gst_rtp_vp8_depay_debug
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,
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,
47 static gboolean gst_rtp_vp8_depay_packet_lost (GstRTPBaseDepayload * depay,
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));
54 static GstStaticPadTemplate gst_rtp_vp8_depay_src_template =
55 GST_STATIC_PAD_TEMPLATE ("src",
58 GST_STATIC_CAPS ("video/x-vp8"));
60 static GstStaticPadTemplate gst_rtp_vp8_depay_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
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\" }"));
69 #define DEFAULT_WAIT_FOR_KEYFRAME FALSE
70 #define DEFAULT_REQUEST_KEYFRAME FALSE
75 PROP_WAIT_FOR_KEYFRAME,
76 PROP_REQUEST_KEYFRAME,
79 #define PICTURE_ID_NONE (UINT_MAX)
80 #define IS_PICTURE_ID_15BITS(pid) (((guint)(pid) & 0x8000) != 0)
83 gst_rtp_vp8_depay_init (GstRtpVP8Depay * self)
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;
93 gst_rtp_vp8_depay_class_init (GstRtpVP8DepayClass * gst_rtp_vp8_depay_class)
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);
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);
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>");
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;
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));
121 * GstRtpVP8Depay:request-keyframe:
123 * Request new keyframe when packet loss is detected
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));
133 element_class->change_state = gst_rtp_vp8_depay_change_state;
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;
139 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_depay_debug, "rtpvp8depay", 0,
140 "VP8 Video RTP Depayloader");
144 gst_rtp_vp8_depay_dispose (GObject * object)
146 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
148 if (self->adapter != NULL)
149 g_object_unref (self->adapter);
150 self->adapter = NULL;
152 /* release any references held by the object here */
154 if (G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose)
155 G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose (object);
159 gst_rtp_vp8_depay_set_property (GObject * object, guint prop_id,
160 const GValue * value, GParamSpec * pspec)
162 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
165 case PROP_WAIT_FOR_KEYFRAME:
166 self->wait_for_keyframe = g_value_get_boolean (value);
168 case PROP_REQUEST_KEYFRAME:
169 self->request_keyframe = g_value_get_boolean (value);
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178 gst_rtp_vp8_depay_get_property (GObject * object, guint prop_id,
179 GValue * value, GParamSpec * pspec)
181 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
184 case PROP_WAIT_FOR_KEYFRAME:
185 g_value_set_boolean (value, self->wait_for_keyframe);
187 case PROP_REQUEST_KEYFRAME:
188 g_value_set_boolean (value, self->request_keyframe);
191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
197 picture_id_compare (guint16 id0, guint16 id1)
199 guint shift = 16 - (IS_PICTURE_ID_15BITS (id1) ? 15 : 7);
202 return ((gint16) (id1 - id0)) >> shift;
206 send_last_lost_event (GstRtpVP8Depay * self)
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;
221 send_new_lost_event (GstRtpVP8Depay * self, GstClockTime timestamp,
222 guint new_picture_id, const gchar * reason)
226 if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
227 GST_WARNING_OBJECT (self, "Can't create lost event with invalid timestmap");
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));
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);
240 GST_RTP_BASE_DEPAYLOAD_CLASS (gst_rtp_vp8_depay_parent_class)
241 ->packet_lost (GST_RTP_BASE_DEPAYLOAD_CAST (self), event);
243 gst_event_unref (event);
247 send_last_lost_event_if_needed (GstRtpVP8Depay * self, guint new_picture_id)
249 if (self->last_picture_id == PICTURE_ID_NONE)
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);
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");
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);
279 gst_event_replace (&self->last_lost_event, NULL);
284 gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depay, GstRTPBuffer * rtp)
286 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
290 guint picture_id = PICTURE_ID_NONE;
291 guint size = gst_rtp_buffer_get_payload_len (rtp);
294 gboolean frame_start;
295 gboolean sent_lost_event = FALSE;
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;
302 if (self->wait_for_keyframe)
303 self->waiting_for_keyframe = TRUE;
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,
311 /* At least one header and one vp8 byte */
312 if (G_UNLIKELY (size < 2))
315 data = gst_rtp_buffer_get_payload (rtp);
317 s_bit = (data[0] >> 4) & 0x1;
318 part_id = (data[0] >> 0) & 0x7;
320 /* Check X optional header */
321 if ((data[0] & 0x80) != 0) {
323 /* Check I optional header */
324 if ((data[1] & 0x80) != 0) {
325 if (G_UNLIKELY (size < 3))
328 /* Check for 16 bits PictureID */
329 picture_id = data[2];
330 if ((data[2] & 0x80) != 0) {
331 if (G_UNLIKELY (size < 4))
334 picture_id = (picture_id << 8) | data[3];
337 /* Check L optional header */
338 if ((data[1] & 0x40) != 0)
340 /* Check T or K optional headers */
341 if ((data[1] & 0x20) != 0 || (data[1] & 0x10) != 0)
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))
350 frame_start = (s_bit == 1) && (part_id == 0);
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;
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,
364 send_new_lost_event (self, GST_BUFFER_PTS (rtp->buffer), picture_id,
365 "Incomplete frame detected");
366 sent_lost_event = TRUE;
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;
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,
389 GST_LOG_OBJECT (depay, "Found the start of the frame");
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;
396 self->started = TRUE;
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;
403 /* Marker indicates that it was the last rtp packet for this frame */
404 if (gst_rtp_buffer_get_marker (rtp)) {
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)
413 gst_adapter_copy (self->adapter, &header, 0, 10);
415 out = gst_adapter_take_buffer (self->adapter,
416 gst_adapter_available (self->adapter));
418 self->started = FALSE;
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);
427 if (self->waiting_for_keyframe) {
428 gst_buffer_unref (out);
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,
436 guint profile, width, height;
438 GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
439 GST_DEBUG_OBJECT (self, "Processed keyframe");
441 profile = (header[0] & 0x0e) >> 1;
442 width = GST_READ_UINT16_LE (header + 6) & 0x3fff;
443 height = GST_READ_UINT16_LE (header + 8) & 0x3fff;
445 if (G_UNLIKELY (self->last_width != width ||
446 self->last_height != height || self->last_profile != profile)) {
447 gchar profile_str[3];
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);
457 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
458 gst_caps_unref (srccaps);
460 self->last_width = width;
461 self->last_height = height;
462 self->last_profile = profile;
464 self->waiting_for_keyframe = FALSE;
467 if (picture_id != PICTURE_ID_NONE)
468 self->stop_lost_events = TRUE;
470 self->last_pushed_was_lost_event = FALSE;
479 GST_DEBUG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
480 gst_adapter_clear (self->adapter);
481 self->started = FALSE;
486 static GstStateChangeReturn
487 gst_rtp_vp8_depay_change_state (GstElement * element, GstStateChange transition)
489 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (element);
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;
507 GST_ELEMENT_CLASS (gst_rtp_vp8_depay_parent_class)->change_state (element,
512 gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
514 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
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;
530 GST_RTP_BASE_DEPAYLOAD_CLASS
531 (gst_rtp_vp8_depay_parent_class)->handle_event (depay, event);
535 gst_rtp_vp8_depay_packet_lost (GstRTPBaseDepayload * depay, GstEvent * event)
537 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
538 const GstStructure *s;
539 gboolean might_have_been_fec;
540 gboolean unref_event = FALSE;
543 s = gst_event_get_structure (event);
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);
553 } else if (self->last_picture_id != PICTURE_ID_NONE) {
556 if (!gst_event_is_writable (event)) {
557 event = gst_event_copy (event);
561 s = gst_event_writable_structure (event);
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");
568 self->last_pushed_was_lost_event = TRUE;
571 GST_RTP_BASE_DEPAYLOAD_CLASS
572 (gst_rtp_vp8_depay_parent_class)->packet_lost (depay, event);
575 gst_event_unref (event);