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