Send seek event to baseparse when aacparse seek failed in push mode
[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 length 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 =
303         gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
304         (rtpgstpay), 8, 0, 0);
305
306     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
307     payload = gst_rtp_buffer_get_payload (&rtp);
308
309     GST_DEBUG_OBJECT (rtpgstpay, "new packet len %u, frag %u", packet_len,
310         frag_offset);
311
312     /*
313      *  0                   1                   2                   3
314      *  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
315      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
316      * |C| CV  |D|0|0|0|     ETYPE     |  MBZ                          |
317      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
318      * |                          Frag_offset                          |
319      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
320      */
321     payload[0] = rtpgstpay->flags;
322     payload[1] = rtpgstpay->etype;
323     payload[2] = payload[3] = 0;
324     payload[4] = frag_offset >> 24;
325     payload[5] = frag_offset >> 16;
326     payload[6] = frag_offset >> 8;
327     payload[7] = frag_offset & 0xff;
328
329     payload += 8;
330     payload_len -= 8;
331
332     frag_offset += payload_len;
333     avail -= payload_len;
334
335     if (avail == 0)
336       gst_rtp_buffer_set_marker (&rtp, TRUE);
337
338     gst_rtp_buffer_unmap (&rtp);
339
340     /* create a new buf to hold the payload */
341     GST_DEBUG_OBJECT (rtpgstpay, "take %u bytes from adapter", payload_len);
342     paybuf = gst_adapter_take_buffer_fast (rtpgstpay->adapter, payload_len);
343
344     if (GST_BUFFER_FLAG_IS_SET (paybuf, GST_BUFFER_FLAG_DELTA_UNIT))
345       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
346
347     /* create a new group to hold the rtp header and the payload */
348     gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpgstpay), outbuf, paybuf, 0);
349     outbuf = gst_buffer_append (outbuf, paybuf);
350
351     GST_BUFFER_PTS (outbuf) = timestamp;
352
353     /* and add to list */
354     gst_buffer_list_insert (list, -1, outbuf);
355   }
356
357   rtpgstpay->flags &= 0x70;
358   rtpgstpay->etype = 0;
359   rtpgstpay->pending_buffers = g_list_append (rtpgstpay->pending_buffers, list);
360
361   return TRUE;
362 }
363
364 static GstFlowReturn
365 gst_rtp_gst_pay_flush (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
366 {
367   GstFlowReturn ret = GST_FLOW_OK;
368   GList *iter;
369
370   gst_rtp_gst_pay_create_from_adapter (rtpgstpay, timestamp);
371
372   iter = rtpgstpay->pending_buffers;
373   while (iter) {
374     GstBufferList *list = iter->data;
375
376     rtpgstpay->pending_buffers = iter =
377         g_list_delete_link (rtpgstpay->pending_buffers, iter);
378
379     /* push the whole buffer list at once */
380     ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpgstpay),
381         list);
382     if (ret != GST_FLOW_OK)
383       break;
384   }
385
386   return ret;
387 }
388
389 static GstBuffer *
390 make_data_buffer (GstRtpGSTPay * rtpgstpay, gchar * data, guint size)
391 {
392   guint plen;
393   guint8 *ptr;
394   GstBuffer *outbuf;
395   GstMapInfo map;
396
397   /* calculate length */
398   plen = 1;
399   while (size >> (7 * plen))
400     plen++;
401
402   outbuf = gst_buffer_new_allocate (NULL, plen + size, NULL);
403
404   gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
405   ptr = map.data;
406
407   /* write length */
408   while (plen) {
409     plen--;
410     *ptr++ = ((plen > 0) ? 0x80 : 0) | ((size >> (7 * plen)) & 0x7f);
411   }
412   /* copy data */
413   memcpy (ptr, data, size);
414   gst_buffer_unmap (outbuf, &map);
415
416   return outbuf;
417 }
418
419 static void
420 gst_rtp_gst_pay_send_caps (GstRtpGSTPay * rtpgstpay, guint8 cv, GstCaps * caps)
421 {
422   gchar *capsstr;
423   guint capslen;
424   GstBuffer *outbuf;
425
426   if (rtpgstpay->flags == ((1 << 7) | (cv << 4))) {
427     /* If caps for the current CV are pending in the adapter already, do
428      * nothing at all here
429      */
430     return;
431   } else if (rtpgstpay->flags & (1 << 7)) {
432     /* Create a new standalone caps packet if caps were already pending.
433      * The next caps are going to be merged with the following buffer or
434      * sent standalone if another event is sent first */
435     gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
436   }
437
438   capsstr = gst_caps_to_string (caps);
439   capslen = strlen (capsstr);
440   /* for 0 byte */
441   capslen++;
442
443   GST_DEBUG_OBJECT (rtpgstpay, "sending caps=%s", capsstr);
444
445   /* make a data buffer of it */
446   outbuf = make_data_buffer (rtpgstpay, capsstr, capslen);
447   g_free (capsstr);
448
449   /* store in adapter, we don't flush yet, buffer might follow */
450   rtpgstpay->flags = (1 << 7) | (cv << 4);
451   gst_adapter_push (rtpgstpay->adapter, outbuf);
452 }
453
454 static gboolean
455 gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
456 {
457   GstRtpGSTPay *rtpgstpay;
458   gboolean res;
459   gchar *capsstr, *capsenc, *capsver;
460   guint capslen;
461
462   rtpgstpay = GST_RTP_GST_PAY (payload);
463
464   capsstr = gst_caps_to_string (caps);
465   capslen = strlen (capsstr);
466
467   /* encode without 0 byte */
468   capsenc = g_base64_encode ((guchar *) capsstr, capslen);
469   GST_DEBUG_OBJECT (payload, "caps=%s, caps(base64)=%s", capsstr, capsenc);
470   g_free (capsstr);
471
472   /* Send the new caps */
473   rtpgstpay->current_CV = rtpgstpay->next_CV;
474   rtpgstpay->next_CV = (rtpgstpay->next_CV + 1) & 0x7;
475   gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
476
477   /* make caps for SDP */
478   capsver = g_strdup_printf ("%d", rtpgstpay->current_CV);
479   res =
480       gst_rtp_base_payload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
481       "capsversion", G_TYPE_STRING, capsver, NULL);
482   g_free (capsenc);
483   g_free (capsver);
484
485   return res;
486 }
487
488 static void
489 gst_rtp_gst_pay_send_event (GstRtpGSTPay * rtpgstpay, guint etype,
490     GstEvent * event)
491 {
492   const GstStructure *s;
493   gchar *estr;
494   guint elen;
495   GstBuffer *outbuf;
496
497   /* Create the standalone caps packet if an inlined caps was pending */
498   gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
499
500   s = gst_event_get_structure (event);
501
502   estr = gst_structure_to_string (s);
503   elen = strlen (estr);
504   /* for 0 byte */
505   elen++;
506   outbuf = make_data_buffer (rtpgstpay, estr, elen);
507   GST_DEBUG_OBJECT (rtpgstpay, "sending event=%s", estr);
508   g_free (estr);
509
510   rtpgstpay->etype = etype;
511   gst_adapter_push (rtpgstpay->adapter, outbuf);
512   /* Create the event packet now to avoid conflict with data/caps packets */
513   gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
514 }
515
516 static gboolean
517 gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
518 {
519   gboolean ret;
520   GstRtpGSTPay *rtpgstpay;
521   guint etype = 0;
522
523   rtpgstpay = GST_RTP_GST_PAY (payload);
524
525   if (gst_video_event_is_force_key_unit (event)) {
526     g_atomic_int_set (&rtpgstpay->force_config, TRUE);
527   }
528
529   ret =
530       GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload,
531       gst_event_ref (event));
532
533   switch (GST_EVENT_TYPE (event)) {
534     case GST_EVENT_FLUSH_STOP:
535       gst_rtp_gst_pay_reset (rtpgstpay, FALSE);
536       break;
537     case GST_EVENT_TAG:{
538       GstTagList *tags;
539
540       gst_event_parse_tag (event, &tags);
541
542       if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
543         GstTagList *old;
544
545         GST_DEBUG_OBJECT (rtpgstpay, "storing stream tags %" GST_PTR_FORMAT,
546             tags);
547         if ((old = rtpgstpay->taglist))
548           gst_tag_list_unref (old);
549         rtpgstpay->taglist = gst_tag_list_ref (tags);
550       }
551       etype = 1;
552       break;
553     }
554     case GST_EVENT_CUSTOM_DOWNSTREAM:
555       etype = 2;
556       break;
557     case GST_EVENT_CUSTOM_BOTH:
558       etype = 3;
559       break;
560     case GST_EVENT_STREAM_START:{
561       const gchar *stream_id = NULL;
562
563       if (rtpgstpay->taglist)
564         gst_tag_list_unref (rtpgstpay->taglist);
565       rtpgstpay->taglist = NULL;
566
567       gst_event_parse_stream_start (event, &stream_id);
568       if (stream_id) {
569         g_free (rtpgstpay->stream_id);
570         rtpgstpay->stream_id = g_strdup (stream_id);
571       }
572       etype = 4;
573       break;
574     }
575     default:
576       GST_LOG_OBJECT (rtpgstpay, "no event for %s",
577           GST_EVENT_TYPE_NAME (event));
578       break;
579   }
580   if (etype) {
581     GST_DEBUG_OBJECT (rtpgstpay, "make event type %d for %s",
582         etype, GST_EVENT_TYPE_NAME (event));
583     gst_rtp_gst_pay_send_event (rtpgstpay, etype, event);
584     /* Do not send stream-start right away since caps/new-segment were not yet
585        sent, so our data would be considered invalid */
586     if (etype != 4) {
587       /* flush the adapter immediately */
588       gst_rtp_gst_pay_flush (rtpgstpay, GST_CLOCK_TIME_NONE);
589     }
590   }
591
592   gst_event_unref (event);
593
594   return ret;
595 }
596
597 static gboolean
598 gst_rtp_gst_pay_src_event (GstRTPBasePayload * payload, GstEvent * event)
599 {
600   GstRtpGSTPay *rtpgstpay;
601
602   rtpgstpay = GST_RTP_GST_PAY (payload);
603
604   if (gst_video_event_is_force_key_unit (event)) {
605     g_atomic_int_set (&rtpgstpay->force_config, TRUE);
606   }
607
608   return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->src_event (payload, event);
609 }
610
611 static void
612 gst_rtp_gst_pay_send_config (GstRtpGSTPay * rtpgstpay,
613     GstClockTime running_time)
614 {
615   GstPad *pad = GST_RTP_BASE_PAYLOAD_SINKPAD (rtpgstpay);
616   GstCaps *caps = NULL;
617   GstEvent *tag = NULL;
618   GstEvent *stream_start = NULL;
619
620   GST_DEBUG_OBJECT (rtpgstpay, "time to send config");
621   /* Send tags */
622   if (rtpgstpay->taglist && !gst_tag_list_is_empty (rtpgstpay->taglist))
623     tag = gst_event_new_tag (gst_tag_list_ref (rtpgstpay->taglist));
624   if (tag) {
625     /* Send start-stream to clear tags */
626     if (rtpgstpay->stream_id)
627       stream_start = gst_event_new_stream_start (rtpgstpay->stream_id);
628     if (stream_start) {
629       gst_rtp_gst_pay_send_event (rtpgstpay, 4, stream_start);
630       gst_event_unref (stream_start);
631     }
632     gst_rtp_gst_pay_send_event (rtpgstpay, 1, tag);
633     gst_event_unref (tag);
634   }
635   /* send caps */
636   caps = gst_pad_get_current_caps (pad);
637   if (caps) {
638     gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
639     gst_caps_unref (caps);
640   }
641   rtpgstpay->last_config = running_time;
642 }
643
644 static GstFlowReturn
645 gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * basepayload,
646     GstBuffer * buffer)
647 {
648   GstFlowReturn ret;
649   GstRtpGSTPay *rtpgstpay;
650   GstClockTime timestamp, running_time;
651
652   rtpgstpay = GST_RTP_GST_PAY (basepayload);
653
654   timestamp = GST_BUFFER_PTS (buffer);
655   running_time =
656       gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
657       timestamp);
658
659   /* check if we need to send the caps and taglist now */
660   if (rtpgstpay->config_interval > 0
661       || g_atomic_int_compare_and_exchange (&rtpgstpay->force_config, TRUE,
662           FALSE)) {
663     GST_DEBUG_OBJECT (rtpgstpay,
664         "running time %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
665         GST_TIME_ARGS (running_time), GST_TIME_ARGS (rtpgstpay->last_config));
666
667     if (running_time != GST_CLOCK_TIME_NONE &&
668         rtpgstpay->last_config != GST_CLOCK_TIME_NONE) {
669       guint64 diff;
670
671       /* calculate diff between last SPS/PPS in milliseconds */
672       if (running_time > rtpgstpay->last_config)
673         diff = running_time - rtpgstpay->last_config;
674       else
675         diff = 0;
676
677       GST_DEBUG_OBJECT (rtpgstpay,
678           "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
679
680       /* bigger than interval, queue SPS/PPS */
681       if (GST_TIME_AS_SECONDS (diff) >= rtpgstpay->config_interval)
682         gst_rtp_gst_pay_send_config (rtpgstpay, running_time);
683     } else {
684       gst_rtp_gst_pay_send_config (rtpgstpay, running_time);
685     }
686   }
687
688   /* caps always from SDP for now */
689   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
690     rtpgstpay->flags |= (1 << 3);
691
692   gst_adapter_push (rtpgstpay->adapter, buffer);
693   ret = gst_rtp_gst_pay_flush (rtpgstpay, timestamp);
694
695   return ret;
696 }
697
698 gboolean
699 gst_rtp_gst_pay_plugin_init (GstPlugin * plugin)
700 {
701   return gst_element_register (plugin, "rtpgstpay",
702       GST_RANK_NONE, GST_TYPE_RTP_GST_PAY);
703 }