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