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