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