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 "gstrtpelements.h"
30 #include "gstrtpgstpay.h"
31 #include "gstrtputils.h"
33 GST_DEBUG_CATEGORY_STATIC (gst_rtp_pay_debug);
34 #define GST_CAT_DEFAULT gst_rtp_pay_debug
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
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.
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.
58 * 2 = GST_EVENT_CUSTOM_DOWNSTREAM
59 * 3 = GST_EVENT_CUSTOM_BOTH
60 * 4 = GST_EVENT_STREAM_START
63 static GstStaticPadTemplate gst_rtp_gst_pay_sink_template =
64 GST_STATIC_PAD_TEMPLATE ("sink",
69 static GstStaticPadTemplate gst_rtp_gst_pay_src_template =
70 GST_STATIC_PAD_TEMPLATE ("src",
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\"")
85 #define DEFAULT_CONFIG_INTERVAL 0
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);
95 static gboolean gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload,
97 static GstFlowReturn gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * payload,
99 static gboolean gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload,
101 static gboolean gst_rtp_gst_pay_src_event (GstRTPBasePayload * payload,
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));
110 gst_rtp_gst_pay_class_init (GstRtpGSTPayClass * klass)
112 GObjectClass *gobject_class;
113 GstElementClass *gstelement_class;
114 GstRTPBasePayloadClass *gstrtpbasepayload_class;
116 gobject_class = (GObjectClass *) klass;
117 gstelement_class = (GstElementClass *) klass;
118 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
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;
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)
133 gstelement_class->change_state = gst_rtp_gst_pay_change_state;
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);
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>");
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;
150 GST_DEBUG_CATEGORY_INIT (gst_rtp_pay_debug, "rtpgstpay", 0,
151 "rtpgstpay element");
155 gst_rtp_gst_pay_init (GstRtpGSTPay * rtpgstpay)
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;
167 gst_rtp_gst_pay_reset (GstRtpGSTPay * rtpgstpay, gboolean full)
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 gst_buffer_list_unref (rtpgstpay->pending_buffers);
175 rtpgstpay->pending_buffers = NULL;
177 if (rtpgstpay->taglist)
178 gst_tag_list_unref (rtpgstpay->taglist);
179 rtpgstpay->taglist = NULL;
180 g_free (rtpgstpay->stream_id);
181 rtpgstpay->stream_id = NULL;
182 rtpgstpay->current_CV = 0;
183 rtpgstpay->next_CV = 0;
185 rtpgstpay->received_buffer = FALSE;
189 gst_rtp_gst_pay_finalize (GObject * obj)
191 GstRtpGSTPay *rtpgstpay;
193 rtpgstpay = GST_RTP_GST_PAY (obj);
195 gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
197 g_object_unref (rtpgstpay->adapter);
199 G_OBJECT_CLASS (parent_class)->finalize (obj);
203 gst_rtp_gst_pay_set_property (GObject * object, guint prop_id,
204 const GValue * value, GParamSpec * pspec)
206 GstRtpGSTPay *rtpgstpay;
208 rtpgstpay = GST_RTP_GST_PAY (object);
211 case PROP_CONFIG_INTERVAL:
212 rtpgstpay->config_interval = g_value_get_uint (value);
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 gst_rtp_gst_pay_get_property (GObject * object, guint prop_id,
222 GValue * value, GParamSpec * pspec)
224 GstRtpGSTPay *rtpgstpay;
226 rtpgstpay = GST_RTP_GST_PAY (object);
229 case PROP_CONFIG_INTERVAL:
230 g_value_set_uint (value, rtpgstpay->config_interval);
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 static GstStateChangeReturn
239 gst_rtp_gst_pay_change_state (GstElement * element, GstStateChange transition)
241 GstRtpGSTPay *rtpgstpay;
242 GstStateChangeReturn ret;
244 rtpgstpay = GST_RTP_GST_PAY (element);
246 switch (transition) {
247 case GST_STATE_CHANGE_READY_TO_PAUSED:
248 gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
254 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
256 switch (transition) {
257 case GST_STATE_CHANGE_PAUSED_TO_READY:
258 gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
266 #define RTP_HEADER_LEN 12
269 gst_rtp_gst_pay_create_from_adapter (GstRtpGSTPay * rtpgstpay,
270 GstClockTime timestamp)
275 avail = gst_adapter_available (rtpgstpay->adapter);
279 mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpgstpay);
281 if (!rtpgstpay->pending_buffers)
282 rtpgstpay->pending_buffers =
283 gst_buffer_list_new_sized ((avail / (mtu - (RTP_HEADER_LEN + 8))) + 1);
292 GstRTPBuffer rtp = { NULL };
296 /* this will be the total length of the packet */
297 packet_len = gst_rtp_buffer_calc_packet_len (8 + avail, 0, 0);
299 /* fill one MTU or all available bytes */
300 towrite = MIN (packet_len, mtu);
302 /* this is the payload length */
303 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
305 /* create buffer to hold the header */
307 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
308 (rtpgstpay), 8, 0, 0);
310 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
311 payload = gst_rtp_buffer_get_payload (&rtp);
313 GST_DEBUG_OBJECT (rtpgstpay, "new packet len %u, frag %u", packet_len,
318 * 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
319 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
320 * |C| CV |D|0|0|0| ETYPE | MBZ |
321 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
323 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
325 payload[0] = rtpgstpay->flags;
326 payload[1] = rtpgstpay->etype;
327 payload[2] = payload[3] = 0;
328 payload[4] = frag_offset >> 24;
329 payload[5] = frag_offset >> 16;
330 payload[6] = frag_offset >> 8;
331 payload[7] = frag_offset & 0xff;
336 frag_offset += payload_len;
337 avail -= payload_len;
340 gst_rtp_buffer_set_marker (&rtp, TRUE);
341 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MARKER);
344 gst_rtp_buffer_unmap (&rtp);
346 /* create a new buf to hold the payload */
347 GST_DEBUG_OBJECT (rtpgstpay, "take %u bytes from adapter", payload_len);
348 paybuf = gst_adapter_take_buffer_fast (rtpgstpay->adapter, payload_len);
350 if (GST_BUFFER_FLAG_IS_SET (paybuf, GST_BUFFER_FLAG_DELTA_UNIT))
351 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
353 /* create a new group to hold the rtp header and the payload */
354 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpgstpay), outbuf, paybuf, 0);
355 outbuf = gst_buffer_append (outbuf, paybuf);
357 GST_BUFFER_PTS (outbuf) = timestamp;
359 /* and add to list */
360 gst_buffer_list_insert (rtpgstpay->pending_buffers, -1, outbuf);
363 rtpgstpay->flags &= 0x70;
364 rtpgstpay->etype = 0;
370 gst_rtp_gst_pay_flush (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
372 GstFlowReturn ret = GST_FLOW_OK;
374 gst_rtp_gst_pay_create_from_adapter (rtpgstpay, timestamp);
376 if (!rtpgstpay->received_buffer) {
377 GST_DEBUG_OBJECT (rtpgstpay,
378 "Can't flush without having received a buffer yet");
382 if (rtpgstpay->pending_buffers) {
383 /* push the whole buffer list at once */
384 ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpgstpay),
385 rtpgstpay->pending_buffers);
386 rtpgstpay->pending_buffers = NULL;
393 make_data_buffer (GstRtpGSTPay * rtpgstpay, gchar * data, guint size)
400 /* calculate length */
402 while (size >> (7 * plen))
405 outbuf = gst_buffer_new_allocate (NULL, plen + size, NULL);
407 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
413 *ptr++ = ((plen > 0) ? 0x80 : 0) | ((size >> (7 * plen)) & 0x7f);
416 memcpy (ptr, data, size);
417 gst_buffer_unmap (outbuf, &map);
423 gst_rtp_gst_pay_send_caps (GstRtpGSTPay * rtpgstpay, guint8 cv, GstCaps * caps)
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
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);
441 capsstr = gst_caps_to_string (caps);
442 capslen = strlen (capsstr);
446 GST_DEBUG_OBJECT (rtpgstpay, "sending caps=%s", capsstr);
448 /* make a data buffer of it */
449 outbuf = make_data_buffer (rtpgstpay, capsstr, capslen);
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);
458 gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
460 GstRtpGSTPay *rtpgstpay;
462 gchar *capsstr, *capsenc, *capsver;
465 rtpgstpay = GST_RTP_GST_PAY (payload);
467 capsstr = gst_caps_to_string (caps);
468 capslen = strlen (capsstr);
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);
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);
480 /* make caps for SDP */
481 capsver = g_strdup_printf ("%d", rtpgstpay->current_CV);
483 gst_rtp_base_payload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
484 "capsversion", G_TYPE_STRING, capsver, NULL);
492 gst_rtp_gst_pay_send_event (GstRtpGSTPay * rtpgstpay, guint etype,
495 const GstStructure *s;
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);
503 s = gst_event_get_structure (event);
505 estr = gst_structure_to_string (s);
506 elen = strlen (estr);
509 outbuf = make_data_buffer (rtpgstpay, estr, elen);
510 GST_DEBUG_OBJECT (rtpgstpay, "sending event=%s", estr);
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);
520 gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
523 GstRtpGSTPay *rtpgstpay;
526 rtpgstpay = GST_RTP_GST_PAY (payload);
528 if (gst_video_event_is_force_key_unit (event)) {
529 g_atomic_int_set (&rtpgstpay->force_config, TRUE);
533 GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload,
534 gst_event_ref (event));
536 switch (GST_EVENT_TYPE (event)) {
537 case GST_EVENT_FLUSH_STOP:
538 gst_rtp_gst_pay_reset (rtpgstpay, FALSE);
543 gst_event_parse_tag (event, &tags);
545 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
548 GST_DEBUG_OBJECT (rtpgstpay, "storing stream tags %" GST_PTR_FORMAT,
550 if ((old = rtpgstpay->taglist))
551 gst_tag_list_unref (old);
552 rtpgstpay->taglist = gst_tag_list_ref (tags);
557 case GST_EVENT_CUSTOM_DOWNSTREAM:
560 case GST_EVENT_CUSTOM_BOTH:
563 case GST_EVENT_STREAM_START:{
564 const gchar *stream_id = NULL;
566 if (rtpgstpay->taglist)
567 gst_tag_list_unref (rtpgstpay->taglist);
568 rtpgstpay->taglist = NULL;
570 gst_event_parse_stream_start (event, &stream_id);
572 g_free (rtpgstpay->stream_id);
573 rtpgstpay->stream_id = g_strdup (stream_id);
579 GST_LOG_OBJECT (rtpgstpay, "no event for %s",
580 GST_EVENT_TYPE_NAME (event));
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 */
590 /* flush the adapter immediately */
591 gst_rtp_gst_pay_flush (rtpgstpay, GST_CLOCK_TIME_NONE);
595 gst_event_unref (event);
601 gst_rtp_gst_pay_src_event (GstRTPBasePayload * payload, GstEvent * event)
603 GstRtpGSTPay *rtpgstpay;
605 rtpgstpay = GST_RTP_GST_PAY (payload);
607 if (gst_video_event_is_force_key_unit (event)) {
608 g_atomic_int_set (&rtpgstpay->force_config, TRUE);
611 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->src_event (payload, event);
615 gst_rtp_gst_pay_send_config (GstRtpGSTPay * rtpgstpay,
616 GstClockTime running_time)
618 GstPad *pad = GST_RTP_BASE_PAYLOAD_SINKPAD (rtpgstpay);
619 GstCaps *caps = NULL;
620 GstEvent *tag = NULL;
621 GstEvent *stream_start = NULL;
623 GST_DEBUG_OBJECT (rtpgstpay, "time to send config");
625 if (rtpgstpay->taglist && !gst_tag_list_is_empty (rtpgstpay->taglist))
626 tag = gst_event_new_tag (gst_tag_list_ref (rtpgstpay->taglist));
628 /* Send start-stream to clear tags */
629 if (rtpgstpay->stream_id)
630 stream_start = gst_event_new_stream_start (rtpgstpay->stream_id);
632 gst_rtp_gst_pay_send_event (rtpgstpay, 4, stream_start);
633 gst_event_unref (stream_start);
635 gst_rtp_gst_pay_send_event (rtpgstpay, 1, tag);
636 gst_event_unref (tag);
639 caps = gst_pad_get_current_caps (pad);
641 gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
642 gst_caps_unref (caps);
644 rtpgstpay->last_config = running_time;
648 gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * basepayload,
652 GstRtpGSTPay *rtpgstpay;
653 GstClockTime timestamp, running_time;
655 rtpgstpay = GST_RTP_GST_PAY (basepayload);
657 rtpgstpay->received_buffer = TRUE;
659 timestamp = GST_BUFFER_PTS (buffer);
661 gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
664 /* check if we need to send the caps and taglist now */
665 if (rtpgstpay->config_interval > 0
666 || g_atomic_int_compare_and_exchange (&rtpgstpay->force_config, TRUE,
668 GST_DEBUG_OBJECT (rtpgstpay,
669 "running time %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
670 GST_TIME_ARGS (running_time), GST_TIME_ARGS (rtpgstpay->last_config));
672 if (running_time != GST_CLOCK_TIME_NONE &&
673 rtpgstpay->last_config != GST_CLOCK_TIME_NONE) {
676 /* calculate diff between last SPS/PPS in milliseconds */
677 if (running_time > rtpgstpay->last_config)
678 diff = running_time - rtpgstpay->last_config;
682 GST_DEBUG_OBJECT (rtpgstpay,
683 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
685 /* bigger than interval, queue SPS/PPS */
686 if (GST_TIME_AS_SECONDS (diff) >= rtpgstpay->config_interval)
687 gst_rtp_gst_pay_send_config (rtpgstpay, running_time);
689 gst_rtp_gst_pay_send_config (rtpgstpay, running_time);
693 /* caps always from SDP for now */
694 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
695 rtpgstpay->flags |= (1 << 3);
697 gst_adapter_push (rtpgstpay->adapter, buffer);
698 ret = gst_rtp_gst_pay_flush (rtpgstpay, timestamp);