webrtc/nice: support consent-freshness RFC7675
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtpgstpay.c
1 /* GStreamer
2  * Copyright (C) <2010> 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
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/video/video.h>
28
29 #include "gstrtpelements.h"
30 #include "gstrtpgstpay.h"
31 #include "gstrtputils.h"
32
33 GST_DEBUG_CATEGORY_STATIC (gst_rtp_pay_debug);
34 #define GST_CAT_DEFAULT gst_rtp_pay_debug
35
36 /*
37  *  0                   1                   2                   3
38  *  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
39  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40  * |C| CV  |D|0|0|0|     ETYPE     |  MBZ                          |
41  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42  * |                          Frag_offset                          |
43  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44  *
45  * C: caps inlined flag
46  *   When C set, first part of payload contains caps definition. Caps definition
47  *   starts with variable-length length prefix and then a string of that length.
48  *   the length is encoded in big endian 7 bit chunks, the top 1 bit of a byte
49  *   is the continuation marker and the 7 next bits the data. A continuation
50  *   marker of 1 means that the next byte contains more data.
51  *
52  * CV: caps version, 0 = caps from SDP, 1 - 7 inlined caps
53  * D: delta unit buffer
54  * ETYPE: type of event. Payload contains the event, prefixed with a
55  *        variable length field.
56  *   0 = NO event
57  *   1 = GST_EVENT_TAG
58  *   2 = GST_EVENT_CUSTOM_DOWNSTREAM
59  *   3 = GST_EVENT_CUSTOM_BOTH
60  *   4 = GST_EVENT_STREAM_START
61  */
62
63 static GstStaticPadTemplate gst_rtp_gst_pay_sink_template =
64 GST_STATIC_PAD_TEMPLATE ("sink",
65     GST_PAD_SINK,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS_ANY);
68
69 static GstStaticPadTemplate gst_rtp_gst_pay_src_template =
70 GST_STATIC_PAD_TEMPLATE ("src",
71     GST_PAD_SRC,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS ("application/x-rtp, "
74         "media = (string) \"application\", "
75         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
76         "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
77     );
78
79 enum
80 {
81   PROP_0,
82   PROP_CONFIG_INTERVAL
83 };
84
85 #define DEFAULT_CONFIG_INTERVAL               0
86
87 static void gst_rtp_gst_pay_set_property (GObject * object, guint prop_id,
88     const GValue * value, GParamSpec * pspec);
89 static void gst_rtp_gst_pay_get_property (GObject * object, guint prop_id,
90     GValue * value, GParamSpec * pspec);
91 static void gst_rtp_gst_pay_finalize (GObject * obj);
92 static GstStateChangeReturn gst_rtp_gst_pay_change_state (GstElement * element,
93     GstStateChange transition);
94
95 static gboolean gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload,
96     GstCaps * caps);
97 static GstFlowReturn gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * payload,
98     GstBuffer * buffer);
99 static gboolean gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload,
100     GstEvent * event);
101 static gboolean gst_rtp_gst_pay_src_event (GstRTPBasePayload * payload,
102     GstEvent * event);
103
104 #define gst_rtp_gst_pay_parent_class parent_class
105 G_DEFINE_TYPE (GstRtpGSTPay, gst_rtp_gst_pay, GST_TYPE_RTP_BASE_PAYLOAD);
106 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpgstpay, "rtpgstpay", GST_RANK_NONE,
107     GST_TYPE_RTP_GST_PAY, rtp_element_init (plugin));
108
109 static void
110 gst_rtp_gst_pay_class_init (GstRtpGSTPayClass * klass)
111 {
112   GObjectClass *gobject_class;
113   GstElementClass *gstelement_class;
114   GstRTPBasePayloadClass *gstrtpbasepayload_class;
115
116   gobject_class = (GObjectClass *) klass;
117   gstelement_class = (GstElementClass *) klass;
118   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
119
120   gobject_class->set_property = gst_rtp_gst_pay_set_property;
121   gobject_class->get_property = gst_rtp_gst_pay_get_property;
122   gobject_class->finalize = gst_rtp_gst_pay_finalize;
123
124   g_object_class_install_property (G_OBJECT_CLASS (klass),
125       PROP_CONFIG_INTERVAL,
126       g_param_spec_uint ("config-interval",
127           "Caps/Tags Send Interval",
128           "Interval for sending caps and TAG events in seconds (0 = disabled)",
129           0, 3600, DEFAULT_CONFIG_INTERVAL,
130           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
131       );
132
133   gstelement_class->change_state = gst_rtp_gst_pay_change_state;
134
135   gst_element_class_add_static_pad_template (gstelement_class,
136       &gst_rtp_gst_pay_src_template);
137   gst_element_class_add_static_pad_template (gstelement_class,
138       &gst_rtp_gst_pay_sink_template);
139
140   gst_element_class_set_static_metadata (gstelement_class,
141       "RTP GStreamer payloader", "Codec/Payloader/Network/RTP",
142       "Payload GStreamer buffers as RTP packets",
143       "Wim Taymans <wim.taymans@gmail.com>");
144
145   gstrtpbasepayload_class->set_caps = gst_rtp_gst_pay_setcaps;
146   gstrtpbasepayload_class->handle_buffer = gst_rtp_gst_pay_handle_buffer;
147   gstrtpbasepayload_class->sink_event = gst_rtp_gst_pay_sink_event;
148   gstrtpbasepayload_class->src_event = gst_rtp_gst_pay_src_event;
149
150   GST_DEBUG_CATEGORY_INIT (gst_rtp_pay_debug, "rtpgstpay", 0,
151       "rtpgstpay element");
152 }
153
154 static void
155 gst_rtp_gst_pay_init (GstRtpGSTPay * rtpgstpay)
156 {
157   rtpgstpay->adapter = gst_adapter_new ();
158   rtpgstpay->pending_buffers = NULL;
159   gst_rtp_base_payload_set_options (GST_RTP_BASE_PAYLOAD (rtpgstpay),
160       "application", TRUE, "X-GST", 90000);
161   rtpgstpay->last_config = GST_CLOCK_TIME_NONE;
162   rtpgstpay->taglist = NULL;
163   rtpgstpay->config_interval = DEFAULT_CONFIG_INTERVAL;
164 }
165
166 static void
167 gst_rtp_gst_pay_reset (GstRtpGSTPay * rtpgstpay, gboolean full)
168 {
169   rtpgstpay->last_config = GST_CLOCK_TIME_NONE;
170   gst_adapter_clear (rtpgstpay->adapter);
171   rtpgstpay->flags &= 0x70;
172   rtpgstpay->etype = 0;
173   if (rtpgstpay->pending_buffers)
174     gst_buffer_list_unref (rtpgstpay->pending_buffers);
175   rtpgstpay->pending_buffers = NULL;
176   if (full) {
177     if (rtpgstpay->taglist)
178       gst_tag_list_unref (rtpgstpay->taglist);
179     rtpgstpay->taglist = NULL;
180     g_free (rtpgstpay->stream_id);
181     rtpgstpay->stream_id = NULL;
182     rtpgstpay->current_CV = 0;
183     rtpgstpay->next_CV = 0;
184   }
185   rtpgstpay->received_buffer = FALSE;
186 }
187
188 static void
189 gst_rtp_gst_pay_finalize (GObject * obj)
190 {
191   GstRtpGSTPay *rtpgstpay;
192
193   rtpgstpay = GST_RTP_GST_PAY (obj);
194
195   gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
196
197   g_object_unref (rtpgstpay->adapter);
198
199   G_OBJECT_CLASS (parent_class)->finalize (obj);
200 }
201
202 static void
203 gst_rtp_gst_pay_set_property (GObject * object, guint prop_id,
204     const GValue * value, GParamSpec * pspec)
205 {
206   GstRtpGSTPay *rtpgstpay;
207
208   rtpgstpay = GST_RTP_GST_PAY (object);
209
210   switch (prop_id) {
211     case PROP_CONFIG_INTERVAL:
212       rtpgstpay->config_interval = g_value_get_uint (value);
213       break;
214     default:
215       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216       break;
217   }
218 }
219
220 static void
221 gst_rtp_gst_pay_get_property (GObject * object, guint prop_id,
222     GValue * value, GParamSpec * pspec)
223 {
224   GstRtpGSTPay *rtpgstpay;
225
226   rtpgstpay = GST_RTP_GST_PAY (object);
227
228   switch (prop_id) {
229     case PROP_CONFIG_INTERVAL:
230       g_value_set_uint (value, rtpgstpay->config_interval);
231       break;
232     default:
233       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234       break;
235   }
236 }
237
238 static GstStateChangeReturn
239 gst_rtp_gst_pay_change_state (GstElement * element, GstStateChange transition)
240 {
241   GstRtpGSTPay *rtpgstpay;
242   GstStateChangeReturn ret;
243
244   rtpgstpay = GST_RTP_GST_PAY (element);
245
246   switch (transition) {
247     case GST_STATE_CHANGE_READY_TO_PAUSED:
248       gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
249       break;
250     default:
251       break;
252   }
253
254   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
255
256   switch (transition) {
257     case GST_STATE_CHANGE_PAUSED_TO_READY:
258       gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
259       break;
260     default:
261       break;
262   }
263   return ret;
264 }
265
266 #define RTP_HEADER_LEN 12
267
268 static gboolean
269 gst_rtp_gst_pay_create_from_adapter (GstRtpGSTPay * rtpgstpay,
270     GstClockTime timestamp)
271 {
272   guint avail, mtu;
273   guint frag_offset;
274
275   avail = gst_adapter_available (rtpgstpay->adapter);
276   if (avail == 0)
277     return FALSE;
278
279   mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpgstpay);
280
281   if (!rtpgstpay->pending_buffers)
282     rtpgstpay->pending_buffers =
283         gst_buffer_list_new_sized ((avail / (mtu - (RTP_HEADER_LEN + 8))) + 1);
284   frag_offset = 0;
285
286   while (avail) {
287     guint towrite;
288     guint8 *payload;
289     guint payload_len;
290     guint packet_len;
291     GstBuffer *outbuf;
292     GstRTPBuffer rtp = { NULL };
293     GstBuffer *paybuf;
294
295
296     /* this will be the total length of the packet */
297     packet_len = gst_rtp_buffer_calc_packet_len (8 + avail, 0, 0);
298
299     /* fill one MTU or all available bytes */
300     towrite = MIN (packet_len, mtu);
301
302     /* this is the payload length */
303     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
304
305     /* create buffer to hold the header */
306     outbuf =
307         gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
308         (rtpgstpay), 8, 0, 0);
309
310     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
311     payload = gst_rtp_buffer_get_payload (&rtp);
312
313     GST_DEBUG_OBJECT (rtpgstpay, "new packet len %u, frag %u", packet_len,
314         frag_offset);
315
316     /*
317      *  0                   1                   2                   3
318      *  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
319      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
320      * |C| CV  |D|0|0|0|     ETYPE     |  MBZ                          |
321      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
322      * |                          Frag_offset                          |
323      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
324      */
325     payload[0] = rtpgstpay->flags;
326     payload[1] = rtpgstpay->etype;
327     payload[2] = payload[3] = 0;
328     payload[4] = frag_offset >> 24;
329     payload[5] = frag_offset >> 16;
330     payload[6] = frag_offset >> 8;
331     payload[7] = frag_offset & 0xff;
332
333     payload += 8;
334     payload_len -= 8;
335
336     frag_offset += payload_len;
337     avail -= payload_len;
338
339     if (avail == 0) {
340       gst_rtp_buffer_set_marker (&rtp, TRUE);
341       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MARKER);
342     }
343
344     gst_rtp_buffer_unmap (&rtp);
345
346     /* create a new buf to hold the payload */
347     GST_DEBUG_OBJECT (rtpgstpay, "take %u bytes from adapter", payload_len);
348     paybuf = gst_adapter_take_buffer_fast (rtpgstpay->adapter, payload_len);
349
350     if (GST_BUFFER_FLAG_IS_SET (paybuf, GST_BUFFER_FLAG_DELTA_UNIT))
351       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
352
353     /* create a new group to hold the rtp header and the payload */
354     gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpgstpay), outbuf, paybuf, 0);
355     outbuf = gst_buffer_append (outbuf, paybuf);
356
357     GST_BUFFER_PTS (outbuf) = timestamp;
358
359     /* and add to list */
360     gst_buffer_list_insert (rtpgstpay->pending_buffers, -1, outbuf);
361   }
362
363   rtpgstpay->flags &= 0x70;
364   rtpgstpay->etype = 0;
365
366   return TRUE;
367 }
368
369 static GstFlowReturn
370 gst_rtp_gst_pay_flush (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
371 {
372   GstFlowReturn ret = GST_FLOW_OK;
373
374   gst_rtp_gst_pay_create_from_adapter (rtpgstpay, timestamp);
375
376   if (!rtpgstpay->received_buffer) {
377     GST_DEBUG_OBJECT (rtpgstpay,
378         "Can't flush without having received a buffer yet");
379     return GST_FLOW_OK;
380   }
381
382   if (rtpgstpay->pending_buffers) {
383     /* push the whole buffer list at once */
384     ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpgstpay),
385         rtpgstpay->pending_buffers);
386     rtpgstpay->pending_buffers = NULL;
387   }
388
389   return ret;
390 }
391
392 static GstBuffer *
393 make_data_buffer (GstRtpGSTPay * rtpgstpay, gchar * data, guint size)
394 {
395   guint plen;
396   guint8 *ptr;
397   GstBuffer *outbuf;
398   GstMapInfo map;
399
400   /* calculate length */
401   plen = 1;
402   while (size >> (7 * plen))
403     plen++;
404
405   outbuf = gst_buffer_new_allocate (NULL, plen + size, NULL);
406
407   gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
408   ptr = map.data;
409
410   /* write length */
411   while (plen) {
412     plen--;
413     *ptr++ = ((plen > 0) ? 0x80 : 0) | ((size >> (7 * plen)) & 0x7f);
414   }
415   /* copy data */
416   memcpy (ptr, data, size);
417   gst_buffer_unmap (outbuf, &map);
418
419   return outbuf;
420 }
421
422 static void
423 gst_rtp_gst_pay_send_caps (GstRtpGSTPay * rtpgstpay, guint8 cv, GstCaps * caps)
424 {
425   gchar *capsstr;
426   guint capslen;
427   GstBuffer *outbuf;
428
429   if (rtpgstpay->flags == ((1 << 7) | (cv << 4))) {
430     /* If caps for the current CV are pending in the adapter already, do
431      * nothing at all here
432      */
433     return;
434   } else if (rtpgstpay->flags & (1 << 7)) {
435     /* Create a new standalone caps packet if caps were already pending.
436      * The next caps are going to be merged with the following buffer or
437      * sent standalone if another event is sent first */
438     gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
439   }
440
441   capsstr = gst_caps_to_string (caps);
442   capslen = strlen (capsstr);
443   /* for 0 byte */
444   capslen++;
445
446   GST_DEBUG_OBJECT (rtpgstpay, "sending caps=%s", capsstr);
447
448   /* make a data buffer of it */
449   outbuf = make_data_buffer (rtpgstpay, capsstr, capslen);
450   g_free (capsstr);
451
452   /* store in adapter, we don't flush yet, buffer might follow */
453   rtpgstpay->flags = (1 << 7) | (cv << 4);
454   gst_adapter_push (rtpgstpay->adapter, outbuf);
455 }
456
457 static gboolean
458 gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
459 {
460   GstRtpGSTPay *rtpgstpay;
461   gboolean res;
462   gchar *capsstr, *capsenc, *capsver;
463   guint capslen;
464
465   rtpgstpay = GST_RTP_GST_PAY (payload);
466
467   capsstr = gst_caps_to_string (caps);
468   capslen = strlen (capsstr);
469
470   /* encode without 0 byte */
471   capsenc = g_base64_encode ((guchar *) capsstr, capslen);
472   GST_DEBUG_OBJECT (payload, "caps=%s, caps(base64)=%s", capsstr, capsenc);
473   g_free (capsstr);
474
475   /* Send the new caps */
476   rtpgstpay->current_CV = rtpgstpay->next_CV;
477   rtpgstpay->next_CV = (rtpgstpay->next_CV + 1) & 0x7;
478   gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
479
480   /* make caps for SDP */
481   capsver = g_strdup_printf ("%d", rtpgstpay->current_CV);
482   res =
483       gst_rtp_base_payload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
484       "capsversion", G_TYPE_STRING, capsver, NULL);
485   g_free (capsenc);
486   g_free (capsver);
487
488   return res;
489 }
490
491 static void
492 gst_rtp_gst_pay_send_event (GstRtpGSTPay * rtpgstpay, guint etype,
493     GstEvent * event)
494 {
495   const GstStructure *s;
496   gchar *estr;
497   guint elen;
498   GstBuffer *outbuf;
499
500   /* Create the standalone caps packet if an inlined caps was pending */
501   gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
502
503   s = gst_event_get_structure (event);
504
505   estr = gst_structure_to_string (s);
506   elen = strlen (estr);
507   /* for 0 byte */
508   elen++;
509   outbuf = make_data_buffer (rtpgstpay, estr, elen);
510   GST_DEBUG_OBJECT (rtpgstpay, "sending event=%s", estr);
511   g_free (estr);
512
513   rtpgstpay->etype = etype;
514   gst_adapter_push (rtpgstpay->adapter, outbuf);
515   /* Create the event packet now to avoid conflict with data/caps packets */
516   gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
517 }
518
519 static gboolean
520 gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
521 {
522   gboolean ret;
523   GstRtpGSTPay *rtpgstpay;
524   guint etype = 0;
525
526   rtpgstpay = GST_RTP_GST_PAY (payload);
527
528   if (gst_video_event_is_force_key_unit (event)) {
529     g_atomic_int_set (&rtpgstpay->force_config, TRUE);
530   }
531
532   ret =
533       GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload,
534       gst_event_ref (event));
535
536   switch (GST_EVENT_TYPE (event)) {
537     case GST_EVENT_FLUSH_STOP:
538       gst_rtp_gst_pay_reset (rtpgstpay, FALSE);
539       break;
540     case GST_EVENT_TAG:{
541       GstTagList *tags;
542
543       gst_event_parse_tag (event, &tags);
544
545       if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
546         GstTagList *old;
547
548         GST_DEBUG_OBJECT (rtpgstpay, "storing stream tags %" GST_PTR_FORMAT,
549             tags);
550         if ((old = rtpgstpay->taglist))
551           gst_tag_list_unref (old);
552         rtpgstpay->taglist = gst_tag_list_ref (tags);
553       }
554       etype = 1;
555       break;
556     }
557     case GST_EVENT_CUSTOM_DOWNSTREAM:
558       etype = 2;
559       break;
560     case GST_EVENT_CUSTOM_BOTH:
561       etype = 3;
562       break;
563     case GST_EVENT_STREAM_START:{
564       const gchar *stream_id = NULL;
565
566       if (rtpgstpay->taglist)
567         gst_tag_list_unref (rtpgstpay->taglist);
568       rtpgstpay->taglist = NULL;
569
570       gst_event_parse_stream_start (event, &stream_id);
571       if (stream_id) {
572         g_free (rtpgstpay->stream_id);
573         rtpgstpay->stream_id = g_strdup (stream_id);
574       }
575       etype = 4;
576       break;
577     }
578     default:
579       GST_LOG_OBJECT (rtpgstpay, "no event for %s",
580           GST_EVENT_TYPE_NAME (event));
581       break;
582   }
583   if (etype) {
584     GST_DEBUG_OBJECT (rtpgstpay, "make event type %d for %s",
585         etype, GST_EVENT_TYPE_NAME (event));
586     gst_rtp_gst_pay_send_event (rtpgstpay, etype, event);
587     /* Do not send stream-start right away since caps/new-segment were not yet
588        sent, so our data would be considered invalid */
589     if (etype != 4) {
590       /* flush the adapter immediately */
591       gst_rtp_gst_pay_flush (rtpgstpay, GST_CLOCK_TIME_NONE);
592     }
593   }
594
595   gst_event_unref (event);
596
597   return ret;
598 }
599
600 static gboolean
601 gst_rtp_gst_pay_src_event (GstRTPBasePayload * payload, GstEvent * event)
602 {
603   GstRtpGSTPay *rtpgstpay;
604
605   rtpgstpay = GST_RTP_GST_PAY (payload);
606
607   if (gst_video_event_is_force_key_unit (event)) {
608     g_atomic_int_set (&rtpgstpay->force_config, TRUE);
609   }
610
611   return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->src_event (payload, event);
612 }
613
614 static void
615 gst_rtp_gst_pay_send_config (GstRtpGSTPay * rtpgstpay,
616     GstClockTime running_time)
617 {
618   GstPad *pad = GST_RTP_BASE_PAYLOAD_SINKPAD (rtpgstpay);
619   GstCaps *caps = NULL;
620   GstEvent *tag = NULL;
621   GstEvent *stream_start = NULL;
622
623   GST_DEBUG_OBJECT (rtpgstpay, "time to send config");
624   /* Send tags */
625   if (rtpgstpay->taglist && !gst_tag_list_is_empty (rtpgstpay->taglist))
626     tag = gst_event_new_tag (gst_tag_list_ref (rtpgstpay->taglist));
627   if (tag) {
628     /* Send start-stream to clear tags */
629     if (rtpgstpay->stream_id)
630       stream_start = gst_event_new_stream_start (rtpgstpay->stream_id);
631     if (stream_start) {
632       gst_rtp_gst_pay_send_event (rtpgstpay, 4, stream_start);
633       gst_event_unref (stream_start);
634     }
635     gst_rtp_gst_pay_send_event (rtpgstpay, 1, tag);
636     gst_event_unref (tag);
637   }
638   /* send caps */
639   caps = gst_pad_get_current_caps (pad);
640   if (caps) {
641     gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
642     gst_caps_unref (caps);
643   }
644   rtpgstpay->last_config = running_time;
645 }
646
647 static GstFlowReturn
648 gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * basepayload,
649     GstBuffer * buffer)
650 {
651   GstFlowReturn ret;
652   GstRtpGSTPay *rtpgstpay;
653   GstClockTime timestamp, running_time;
654
655   rtpgstpay = GST_RTP_GST_PAY (basepayload);
656
657   rtpgstpay->received_buffer = TRUE;
658
659   timestamp = GST_BUFFER_PTS (buffer);
660   running_time =
661       gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
662       timestamp);
663
664   /* check if we need to send the caps and taglist now */
665   if (rtpgstpay->config_interval > 0
666       || g_atomic_int_compare_and_exchange (&rtpgstpay->force_config, TRUE,
667           FALSE)) {
668     GST_DEBUG_OBJECT (rtpgstpay,
669         "running time %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
670         GST_TIME_ARGS (running_time), GST_TIME_ARGS (rtpgstpay->last_config));
671
672     if (running_time != GST_CLOCK_TIME_NONE &&
673         rtpgstpay->last_config != GST_CLOCK_TIME_NONE) {
674       guint64 diff;
675
676       /* calculate diff between last SPS/PPS in milliseconds */
677       if (running_time > rtpgstpay->last_config)
678         diff = running_time - rtpgstpay->last_config;
679       else
680         diff = 0;
681
682       GST_DEBUG_OBJECT (rtpgstpay,
683           "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
684
685       /* bigger than interval, queue SPS/PPS */
686       if (GST_TIME_AS_SECONDS (diff) >= rtpgstpay->config_interval)
687         gst_rtp_gst_pay_send_config (rtpgstpay, running_time);
688     } else {
689       gst_rtp_gst_pay_send_config (rtpgstpay, running_time);
690     }
691   }
692
693   /* caps always from SDP for now */
694   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
695     rtpgstpay->flags |= (1 << 3);
696
697   gst_adapter_push (rtpgstpay->adapter, buffer);
698   ret = gst_rtp_gst_pay_flush (rtpgstpay, timestamp);
699
700   return ret;
701 }