2 * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/video/video.h>
29 #include "gstrtpgstpay.h"
30 #include "gstrtputils.h"
32 GST_DEBUG_CATEGORY_STATIC (gst_rtp_pay_debug);
33 #define GST_CAT_DEFAULT gst_rtp_pay_debug
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
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.
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.
57 * 2 = GST_EVENT_CUSTOM_DOWNSTREAM
58 * 3 = GST_EVENT_CUSTOM_BOTH
59 * 4 = GST_EVENT_STREAM_START
62 static GstStaticPadTemplate gst_rtp_gst_pay_sink_template =
63 GST_STATIC_PAD_TEMPLATE ("sink",
68 static GstStaticPadTemplate gst_rtp_gst_pay_src_template =
69 GST_STATIC_PAD_TEMPLATE ("src",
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\"")
84 #define DEFAULT_CONFIG_INTERVAL 0
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);
94 static gboolean gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload,
96 static GstFlowReturn gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * payload,
98 static gboolean gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload,
100 static gboolean gst_rtp_gst_pay_src_event (GstRTPBasePayload * payload,
103 #define gst_rtp_gst_pay_parent_class parent_class
104 G_DEFINE_TYPE (GstRtpGSTPay, gst_rtp_gst_pay, GST_TYPE_RTP_BASE_PAYLOAD);
107 gst_rtp_gst_pay_class_init (GstRtpGSTPayClass * klass)
109 GObjectClass *gobject_class;
110 GstElementClass *gstelement_class;
111 GstRTPBasePayloadClass *gstrtpbasepayload_class;
113 gobject_class = (GObjectClass *) klass;
114 gstelement_class = (GstElementClass *) klass;
115 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
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;
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)
130 gstelement_class->change_state = gst_rtp_gst_pay_change_state;
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);
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>");
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;
147 GST_DEBUG_CATEGORY_INIT (gst_rtp_pay_debug, "rtpgstpay", 0,
148 "rtpgstpay element");
152 gst_rtp_gst_pay_init (GstRtpGSTPay * rtpgstpay)
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;
164 gst_rtp_gst_pay_reset (GstRtpGSTPay * rtpgstpay, gboolean full)
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;
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;
186 gst_rtp_gst_pay_finalize (GObject * obj)
188 GstRtpGSTPay *rtpgstpay;
190 rtpgstpay = GST_RTP_GST_PAY (obj);
192 gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
194 g_object_unref (rtpgstpay->adapter);
196 G_OBJECT_CLASS (parent_class)->finalize (obj);
200 gst_rtp_gst_pay_set_property (GObject * object, guint prop_id,
201 const GValue * value, GParamSpec * pspec)
203 GstRtpGSTPay *rtpgstpay;
205 rtpgstpay = GST_RTP_GST_PAY (object);
208 case PROP_CONFIG_INTERVAL:
209 rtpgstpay->config_interval = g_value_get_uint (value);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 gst_rtp_gst_pay_get_property (GObject * object, guint prop_id,
219 GValue * value, GParamSpec * pspec)
221 GstRtpGSTPay *rtpgstpay;
223 rtpgstpay = GST_RTP_GST_PAY (object);
226 case PROP_CONFIG_INTERVAL:
227 g_value_set_uint (value, rtpgstpay->config_interval);
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235 static GstStateChangeReturn
236 gst_rtp_gst_pay_change_state (GstElement * element, GstStateChange transition)
238 GstRtpGSTPay *rtpgstpay;
239 GstStateChangeReturn ret;
241 rtpgstpay = GST_RTP_GST_PAY (element);
243 switch (transition) {
244 case GST_STATE_CHANGE_READY_TO_PAUSED:
245 gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
251 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
253 switch (transition) {
254 case GST_STATE_CHANGE_PAUSED_TO_READY:
255 gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
263 #define RTP_HEADER_LEN 12
266 gst_rtp_gst_pay_create_from_adapter (GstRtpGSTPay * rtpgstpay,
267 GstClockTime timestamp)
273 avail = gst_adapter_available (rtpgstpay->adapter);
277 mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpgstpay);
279 list = gst_buffer_list_new_sized ((avail / (mtu - (RTP_HEADER_LEN + 8))) + 1);
288 GstRTPBuffer rtp = { NULL };
292 /* this will be the total length of the packet */
293 packet_len = gst_rtp_buffer_calc_packet_len (8 + avail, 0, 0);
295 /* fill one MTU or all available bytes */
296 towrite = MIN (packet_len, mtu);
298 /* this is the payload length */
299 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
301 /* create buffer to hold the header */
303 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
304 (rtpgstpay), 8, 0, 0);
306 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
307 payload = gst_rtp_buffer_get_payload (&rtp);
309 GST_DEBUG_OBJECT (rtpgstpay, "new packet len %u, frag %u", packet_len,
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
319 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
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;
332 frag_offset += payload_len;
333 avail -= payload_len;
336 gst_rtp_buffer_set_marker (&rtp, TRUE);
338 gst_rtp_buffer_unmap (&rtp);
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);
344 if (GST_BUFFER_FLAG_IS_SET (paybuf, GST_BUFFER_FLAG_DELTA_UNIT))
345 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
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);
351 GST_BUFFER_PTS (outbuf) = timestamp;
353 /* and add to list */
354 gst_buffer_list_insert (list, -1, outbuf);
357 rtpgstpay->flags &= 0x70;
358 rtpgstpay->etype = 0;
359 rtpgstpay->pending_buffers = g_list_append (rtpgstpay->pending_buffers, list);
365 gst_rtp_gst_pay_flush (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
367 GstFlowReturn ret = GST_FLOW_OK;
370 gst_rtp_gst_pay_create_from_adapter (rtpgstpay, timestamp);
372 iter = rtpgstpay->pending_buffers;
374 GstBufferList *list = iter->data;
376 rtpgstpay->pending_buffers = iter =
377 g_list_delete_link (rtpgstpay->pending_buffers, iter);
379 /* push the whole buffer list at once */
380 ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpgstpay),
382 if (ret != GST_FLOW_OK)
390 make_data_buffer (GstRtpGSTPay * rtpgstpay, gchar * data, guint size)
397 /* calculate length */
399 while (size >> (7 * plen))
402 outbuf = gst_buffer_new_allocate (NULL, plen + size, NULL);
404 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
410 *ptr++ = ((plen > 0) ? 0x80 : 0) | ((size >> (7 * plen)) & 0x7f);
413 memcpy (ptr, data, size);
414 gst_buffer_unmap (outbuf, &map);
420 gst_rtp_gst_pay_send_caps (GstRtpGSTPay * rtpgstpay, guint8 cv, GstCaps * caps)
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
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);
438 capsstr = gst_caps_to_string (caps);
439 capslen = strlen (capsstr);
443 GST_DEBUG_OBJECT (rtpgstpay, "sending caps=%s", capsstr);
445 /* make a data buffer of it */
446 outbuf = make_data_buffer (rtpgstpay, capsstr, capslen);
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);
455 gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
457 GstRtpGSTPay *rtpgstpay;
459 gchar *capsstr, *capsenc, *capsver;
462 rtpgstpay = GST_RTP_GST_PAY (payload);
464 capsstr = gst_caps_to_string (caps);
465 capslen = strlen (capsstr);
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);
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);
477 /* make caps for SDP */
478 capsver = g_strdup_printf ("%d", rtpgstpay->current_CV);
480 gst_rtp_base_payload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
481 "capsversion", G_TYPE_STRING, capsver, NULL);
489 gst_rtp_gst_pay_send_event (GstRtpGSTPay * rtpgstpay, guint etype,
492 const GstStructure *s;
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);
500 s = gst_event_get_structure (event);
502 estr = gst_structure_to_string (s);
503 elen = strlen (estr);
506 outbuf = make_data_buffer (rtpgstpay, estr, elen);
507 GST_DEBUG_OBJECT (rtpgstpay, "sending event=%s", estr);
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);
517 gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
520 GstRtpGSTPay *rtpgstpay;
523 rtpgstpay = GST_RTP_GST_PAY (payload);
525 if (gst_video_event_is_force_key_unit (event)) {
526 g_atomic_int_set (&rtpgstpay->force_config, TRUE);
530 GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload,
531 gst_event_ref (event));
533 switch (GST_EVENT_TYPE (event)) {
534 case GST_EVENT_FLUSH_STOP:
535 gst_rtp_gst_pay_reset (rtpgstpay, FALSE);
540 gst_event_parse_tag (event, &tags);
542 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
545 GST_DEBUG_OBJECT (rtpgstpay, "storing stream tags %" GST_PTR_FORMAT,
547 if ((old = rtpgstpay->taglist))
548 gst_tag_list_unref (old);
549 rtpgstpay->taglist = gst_tag_list_ref (tags);
554 case GST_EVENT_CUSTOM_DOWNSTREAM:
557 case GST_EVENT_CUSTOM_BOTH:
560 case GST_EVENT_STREAM_START:{
561 const gchar *stream_id = NULL;
563 if (rtpgstpay->taglist)
564 gst_tag_list_unref (rtpgstpay->taglist);
565 rtpgstpay->taglist = NULL;
567 gst_event_parse_stream_start (event, &stream_id);
569 g_free (rtpgstpay->stream_id);
570 rtpgstpay->stream_id = g_strdup (stream_id);
576 GST_LOG_OBJECT (rtpgstpay, "no event for %s",
577 GST_EVENT_TYPE_NAME (event));
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 */
587 /* flush the adapter immediately */
588 gst_rtp_gst_pay_flush (rtpgstpay, GST_CLOCK_TIME_NONE);
592 gst_event_unref (event);
598 gst_rtp_gst_pay_src_event (GstRTPBasePayload * payload, GstEvent * event)
600 GstRtpGSTPay *rtpgstpay;
602 rtpgstpay = GST_RTP_GST_PAY (payload);
604 if (gst_video_event_is_force_key_unit (event)) {
605 g_atomic_int_set (&rtpgstpay->force_config, TRUE);
608 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->src_event (payload, event);
612 gst_rtp_gst_pay_send_config (GstRtpGSTPay * rtpgstpay,
613 GstClockTime running_time)
615 GstPad *pad = GST_RTP_BASE_PAYLOAD_SINKPAD (rtpgstpay);
616 GstCaps *caps = NULL;
617 GstEvent *tag = NULL;
618 GstEvent *stream_start = NULL;
620 GST_DEBUG_OBJECT (rtpgstpay, "time to send config");
622 if (rtpgstpay->taglist && !gst_tag_list_is_empty (rtpgstpay->taglist))
623 tag = gst_event_new_tag (gst_tag_list_ref (rtpgstpay->taglist));
625 /* Send start-stream to clear tags */
626 if (rtpgstpay->stream_id)
627 stream_start = gst_event_new_stream_start (rtpgstpay->stream_id);
629 gst_rtp_gst_pay_send_event (rtpgstpay, 4, stream_start);
630 gst_event_unref (stream_start);
632 gst_rtp_gst_pay_send_event (rtpgstpay, 1, tag);
633 gst_event_unref (tag);
636 caps = gst_pad_get_current_caps (pad);
638 gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
639 gst_caps_unref (caps);
641 rtpgstpay->last_config = running_time;
645 gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * basepayload,
649 GstRtpGSTPay *rtpgstpay;
650 GstClockTime timestamp, running_time;
652 rtpgstpay = GST_RTP_GST_PAY (basepayload);
654 timestamp = GST_BUFFER_PTS (buffer);
656 gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
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,
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));
667 if (running_time != GST_CLOCK_TIME_NONE &&
668 rtpgstpay->last_config != GST_CLOCK_TIME_NONE) {
671 /* calculate diff between last SPS/PPS in milliseconds */
672 if (running_time > rtpgstpay->last_config)
673 diff = running_time - rtpgstpay->last_config;
677 GST_DEBUG_OBJECT (rtpgstpay,
678 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
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);
684 gst_rtp_gst_pay_send_config (rtpgstpay, running_time);
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);
692 gst_adapter_push (rtpgstpay->adapter, buffer);
693 ret = gst_rtp_gst_pay_flush (rtpgstpay, timestamp);
699 gst_rtp_gst_pay_plugin_init (GstPlugin * plugin)
701 return gst_element_register (plugin, "rtpgstpay",
702 GST_RANK_NONE, GST_TYPE_RTP_GST_PAY);