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