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>
28 #include "gstrtpgstpay.h"
30 GST_DEBUG_CATEGORY_STATIC (gst_rtp_pay_debug);
31 #define GST_CAT_DEFAULT gst_rtp_pay_debug
35 * 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
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * |C| CV |D|0|0|0| ETYPE | MBZ |
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * C: caps inlined flag
43 * When C set, first part of payload contains caps definition. Caps definition
44 * starts with variable-length length prefix and then a string of that length.
45 * the length is encoded in big endian 7 bit chunks, the top 1 bit of a byte
46 * is the continuation marker and the 7 next bits the data. A continuation
47 * marker of 1 means that the next byte contains more data.
49 * CV: caps version, 0 = caps from SDP, 1 - 7 inlined caps
50 * D: delta unit buffer
51 * ETYPE: type of event. Payload contains the event, prefixed with a
52 * variable length field.
55 * 2 = GST_EVENT_CUSTOM_DOWNSTREAM
56 * 3 = GST_EVENT_CUSTOM_BOTH
57 * 4 = GST_EVENT_STREAM_START
60 static GstStaticPadTemplate gst_rtp_gst_pay_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
66 static GstStaticPadTemplate gst_rtp_gst_pay_src_template =
67 GST_STATIC_PAD_TEMPLATE ("src",
70 GST_STATIC_CAPS ("application/x-rtp, "
71 "media = (string) \"application\", "
72 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
73 "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
83 #define DEFAULT_CONFIG_INTERVAL 0
85 static void gst_rtp_gst_pay_set_property (GObject * object, guint prop_id,
86 const GValue * value, GParamSpec * pspec);
87 static void gst_rtp_gst_pay_get_property (GObject * object, guint prop_id,
88 GValue * value, GParamSpec * pspec);
89 static void gst_rtp_gst_pay_finalize (GObject * obj);
90 static GstStateChangeReturn gst_rtp_gst_pay_change_state (GstElement * element,
91 GstStateChange transition);
93 static gboolean gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload,
95 static GstFlowReturn gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * payload,
97 static gboolean gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload,
100 #define gst_rtp_gst_pay_parent_class parent_class
101 G_DEFINE_TYPE (GstRtpGSTPay, gst_rtp_gst_pay, GST_TYPE_RTP_BASE_PAYLOAD);
104 gst_rtp_gst_pay_class_init (GstRtpGSTPayClass * klass)
106 GObjectClass *gobject_class;
107 GstElementClass *gstelement_class;
108 GstRTPBasePayloadClass *gstrtpbasepayload_class;
110 gobject_class = (GObjectClass *) klass;
111 gstelement_class = (GstElementClass *) klass;
112 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
114 gobject_class->set_property = gst_rtp_gst_pay_set_property;
115 gobject_class->get_property = gst_rtp_gst_pay_get_property;
116 gobject_class->finalize = gst_rtp_gst_pay_finalize;
118 g_object_class_install_property (G_OBJECT_CLASS (klass),
119 PROP_CONFIG_INTERVAL,
120 g_param_spec_uint ("config-interval",
121 "Caps/Tags Send Interval",
122 "Interval for sending caps and TAG events in seconds (0 = disabled)",
123 0, 3600, DEFAULT_CONFIG_INTERVAL,
124 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
127 gstelement_class->change_state = gst_rtp_gst_pay_change_state;
129 gst_element_class_add_pad_template (gstelement_class,
130 gst_static_pad_template_get (&gst_rtp_gst_pay_src_template));
131 gst_element_class_add_pad_template (gstelement_class,
132 gst_static_pad_template_get (&gst_rtp_gst_pay_sink_template));
134 gst_element_class_set_static_metadata (gstelement_class,
135 "RTP GStreamer payloader", "Codec/Payloader/Network/RTP",
136 "Payload GStreamer buffers as RTP packets",
137 "Wim Taymans <wim.taymans@gmail.com>");
139 gstrtpbasepayload_class->set_caps = gst_rtp_gst_pay_setcaps;
140 gstrtpbasepayload_class->handle_buffer = gst_rtp_gst_pay_handle_buffer;
141 gstrtpbasepayload_class->sink_event = gst_rtp_gst_pay_sink_event;
143 GST_DEBUG_CATEGORY_INIT (gst_rtp_pay_debug, "rtpgstpay", 0,
144 "rtpgstpay element");
148 gst_rtp_gst_pay_init (GstRtpGSTPay * rtpgstpay)
150 rtpgstpay->adapter = gst_adapter_new ();
151 rtpgstpay->pending_buffers = NULL;
152 gst_rtp_base_payload_set_options (GST_RTP_BASE_PAYLOAD (rtpgstpay),
153 "application", TRUE, "X-GST", 90000);
154 rtpgstpay->last_config = GST_CLOCK_TIME_NONE;
155 rtpgstpay->taglist = NULL;
156 rtpgstpay->config_interval = DEFAULT_CONFIG_INTERVAL;
160 gst_rtp_gst_pay_reset (GstRtpGSTPay * rtpgstpay, gboolean full)
162 rtpgstpay->last_config = GST_CLOCK_TIME_NONE;
163 gst_adapter_clear (rtpgstpay->adapter);
164 rtpgstpay->flags &= 0x70;
165 rtpgstpay->etype = 0;
166 if (rtpgstpay->pending_buffers)
167 g_list_free_full (rtpgstpay->pending_buffers,
168 (GDestroyNotify) gst_buffer_list_unref);
169 rtpgstpay->pending_buffers = NULL;
171 if (rtpgstpay->taglist)
172 gst_tag_list_unref (rtpgstpay->taglist);
173 rtpgstpay->taglist = NULL;
174 if (rtpgstpay->stream_id)
175 g_free (rtpgstpay->stream_id);
176 rtpgstpay->stream_id = NULL;
177 rtpgstpay->current_CV = 0;
178 rtpgstpay->next_CV = 0;
183 gst_rtp_gst_pay_finalize (GObject * obj)
185 GstRtpGSTPay *rtpgstpay;
187 rtpgstpay = GST_RTP_GST_PAY (obj);
189 gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
191 g_object_unref (rtpgstpay->adapter);
193 G_OBJECT_CLASS (parent_class)->finalize (obj);
197 gst_rtp_gst_pay_set_property (GObject * object, guint prop_id,
198 const GValue * value, GParamSpec * pspec)
200 GstRtpGSTPay *rtpgstpay;
202 rtpgstpay = GST_RTP_GST_PAY (object);
205 case PROP_CONFIG_INTERVAL:
206 rtpgstpay->config_interval = g_value_get_uint (value);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 gst_rtp_gst_pay_get_property (GObject * object, guint prop_id,
216 GValue * value, GParamSpec * pspec)
218 GstRtpGSTPay *rtpgstpay;
220 rtpgstpay = GST_RTP_GST_PAY (object);
223 case PROP_CONFIG_INTERVAL:
224 g_value_set_uint (value, rtpgstpay->config_interval);
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232 static GstStateChangeReturn
233 gst_rtp_gst_pay_change_state (GstElement * element, GstStateChange transition)
235 GstRtpGSTPay *rtpgstpay;
236 GstStateChangeReturn ret;
238 rtpgstpay = GST_RTP_GST_PAY (element);
240 switch (transition) {
241 case GST_STATE_CHANGE_READY_TO_PAUSED:
242 gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
248 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
250 switch (transition) {
251 case GST_STATE_CHANGE_PAUSED_TO_READY:
252 gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
262 gst_rtp_gst_pay_create_from_adapter (GstRtpGSTPay * rtpgstpay,
263 GstClockTime timestamp)
269 avail = gst_adapter_available (rtpgstpay->adapter);
273 list = gst_buffer_list_new ();
282 GstRTPBuffer rtp = { NULL };
286 /* this will be the total lenght of the packet */
287 packet_len = gst_rtp_buffer_calc_packet_len (8 + avail, 0, 0);
289 /* fill one MTU or all available bytes */
290 towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpgstpay));
292 /* this is the payload length */
293 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
295 /* create buffer to hold the header */
296 outbuf = gst_rtp_buffer_new_allocate (8, 0, 0);
298 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
299 payload = gst_rtp_buffer_get_payload (&rtp);
301 GST_DEBUG_OBJECT (rtpgstpay, "new packet len %u, frag %u", packet_len,
306 * 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
307 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
308 * |C| CV |D|0|0|0| ETYPE | MBZ |
309 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
311 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
313 payload[0] = rtpgstpay->flags;
314 payload[1] = rtpgstpay->etype;
315 payload[2] = payload[3] = 0;
316 payload[4] = frag_offset >> 24;
317 payload[5] = frag_offset >> 16;
318 payload[6] = frag_offset >> 8;
319 payload[7] = frag_offset & 0xff;
324 frag_offset += payload_len;
325 avail -= payload_len;
328 gst_rtp_buffer_set_marker (&rtp, TRUE);
330 gst_rtp_buffer_unmap (&rtp);
332 /* create a new buf to hold the payload */
333 GST_DEBUG_OBJECT (rtpgstpay, "take %u bytes from adapter", payload_len);
334 paybuf = gst_adapter_take_buffer_fast (rtpgstpay->adapter, payload_len);
336 /* create a new group to hold the rtp header and the payload */
337 gst_buffer_append (outbuf, paybuf);
339 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
341 /* and add to list */
342 gst_buffer_list_insert (list, -1, outbuf);
345 rtpgstpay->flags &= 0x70;
346 rtpgstpay->etype = 0;
347 rtpgstpay->pending_buffers = g_list_append (rtpgstpay->pending_buffers, list);
353 gst_rtp_gst_pay_flush (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
355 GstFlowReturn ret = GST_FLOW_OK;
358 gst_rtp_gst_pay_create_from_adapter (rtpgstpay, timestamp);
360 iter = rtpgstpay->pending_buffers;
362 GstBufferList *list = iter->data;
364 rtpgstpay->pending_buffers = iter =
365 g_list_delete_link (rtpgstpay->pending_buffers, iter);
367 /* push the whole buffer list at once */
368 ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpgstpay),
370 if (ret != GST_FLOW_OK)
378 make_data_buffer (GstRtpGSTPay * rtpgstpay, gchar * data, guint size)
385 /* calculate length */
387 while (size >> (7 * plen))
390 outbuf = gst_buffer_new_allocate (NULL, plen + size, NULL);
392 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
398 *ptr++ = ((plen > 0) ? 0x80 : 0) | ((size >> (7 * plen)) & 0x7f);
401 memcpy (ptr, data, size);
402 gst_buffer_unmap (outbuf, &map);
408 gst_rtp_gst_pay_send_caps (GstRtpGSTPay * rtpgstpay, guint8 cv, GstCaps * caps)
414 capsstr = gst_caps_to_string (caps);
415 capslen = strlen (capsstr);
419 GST_DEBUG_OBJECT (rtpgstpay, "sending caps=%s", capsstr);
421 /* make a data buffer of it */
422 outbuf = make_data_buffer (rtpgstpay, capsstr, capslen);
425 /* store in adapter, we don't flush yet, buffer might follow */
426 rtpgstpay->flags = (1 << 7) | (cv << 4);
427 gst_adapter_push (rtpgstpay->adapter, outbuf);
431 gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
433 GstRtpGSTPay *rtpgstpay;
435 gchar *capsstr, *capsenc, *capsver;
438 rtpgstpay = GST_RTP_GST_PAY (payload);
440 capsstr = gst_caps_to_string (caps);
441 capslen = strlen (capsstr);
443 /* encode without 0 byte */
444 capsenc = g_base64_encode ((guchar *) capsstr, capslen);
445 GST_DEBUG_OBJECT (payload, "caps=%s, caps(base64)=%s", capsstr, capsenc);
448 /* Send the new caps */
449 rtpgstpay->current_CV = rtpgstpay->next_CV;
450 rtpgstpay->next_CV = (rtpgstpay->next_CV + 1) & 0x7;
451 gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
453 /* make caps for SDP */
454 capsver = g_strdup_printf ("%d", rtpgstpay->current_CV);
456 gst_rtp_base_payload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
457 "capsversion", G_TYPE_STRING, capsver, NULL);
465 gst_rtp_gst_pay_send_event (GstRtpGSTPay * rtpgstpay, guint etype,
468 const GstStructure *s;
473 /* Create the standalone caps packet if an inlined caps was pending */
474 gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
476 s = gst_event_get_structure (event);
478 estr = gst_structure_to_string (s);
479 elen = strlen (estr);
480 outbuf = make_data_buffer (rtpgstpay, estr, elen);
481 GST_DEBUG_OBJECT (rtpgstpay, "sending event=%s", estr);
484 rtpgstpay->etype = etype;
485 gst_adapter_push (rtpgstpay->adapter, outbuf);
486 /* Create the event packet now to avoid conflict with data/caps packets */
487 gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
491 gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
494 GstRtpGSTPay *rtpgstpay;
497 rtpgstpay = GST_RTP_GST_PAY (payload);
500 GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload,
501 gst_event_ref (event));
503 switch (GST_EVENT_TYPE (event)) {
504 case GST_EVENT_FLUSH_STOP:
505 gst_rtp_gst_pay_reset (rtpgstpay, FALSE);
510 gst_event_parse_tag (event, &tags);
512 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
515 GST_DEBUG_OBJECT (rtpgstpay, "storing stream tags %" GST_PTR_FORMAT,
517 if ((old = rtpgstpay->taglist))
518 gst_tag_list_unref (old);
519 rtpgstpay->taglist = gst_tag_list_ref (tags);
524 case GST_EVENT_CUSTOM_DOWNSTREAM:
527 case GST_EVENT_CUSTOM_BOTH:
530 case GST_EVENT_STREAM_START:{
531 const gchar *stream_id = NULL;
533 if (rtpgstpay->taglist)
534 gst_tag_list_unref (rtpgstpay->taglist);
535 rtpgstpay->taglist = NULL;
537 gst_event_parse_stream_start (event, &stream_id);
539 if (rtpgstpay->stream_id)
540 g_free (rtpgstpay->stream_id);
541 rtpgstpay->stream_id = g_strdup (stream_id);
547 GST_LOG_OBJECT (rtpgstpay, "no event for %s",
548 GST_EVENT_TYPE_NAME (event));
552 GST_DEBUG_OBJECT (rtpgstpay, "make event type %d for %s",
553 etype, GST_EVENT_TYPE_NAME (event));
554 gst_rtp_gst_pay_send_event (rtpgstpay, etype, event);
555 /* Do not send stream-start right away since caps/new-segment were not yet
556 sent, so our data would be considered invalid */
558 /* flush the adapter immediately */
559 gst_rtp_gst_pay_flush (rtpgstpay, GST_CLOCK_TIME_NONE);
563 gst_event_unref (event);
569 gst_rtp_gst_pay_send_config (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
571 GstPad *pad = GST_RTP_BASE_PAYLOAD_SINKPAD (rtpgstpay);
572 GstCaps *caps = NULL;
573 GstEvent *tag = NULL;
574 GstEvent *stream_start = NULL;
576 GST_DEBUG_OBJECT (rtpgstpay, "time to send config");
578 if (rtpgstpay->taglist && !gst_tag_list_is_empty (rtpgstpay->taglist))
579 tag = gst_event_new_tag (gst_tag_list_ref (rtpgstpay->taglist));
581 /* Send start-stream to clear tags */
582 if (rtpgstpay->stream_id)
583 stream_start = gst_event_new_stream_start (rtpgstpay->stream_id);
585 gst_rtp_gst_pay_send_event (rtpgstpay, 4, stream_start);
586 gst_event_unref (stream_start);
588 gst_rtp_gst_pay_send_event (rtpgstpay, 1, tag);
589 gst_event_unref (tag);
592 caps = gst_pad_get_current_caps (pad);
594 gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
595 gst_caps_unref (caps);
597 rtpgstpay->last_config = timestamp;
601 gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * basepayload,
605 GstRtpGSTPay *rtpgstpay;
606 GstClockTime timestamp;
608 rtpgstpay = GST_RTP_GST_PAY (basepayload);
610 timestamp = GST_BUFFER_TIMESTAMP (buffer);
612 /* check if we need to send the caps and taglist now */
613 if (rtpgstpay->config_interval > 0) {
614 GST_DEBUG_OBJECT (rtpgstpay,
615 "timestamp %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
616 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpgstpay->last_config));
618 if (timestamp != GST_CLOCK_TIME_NONE &&
619 rtpgstpay->last_config != GST_CLOCK_TIME_NONE) {
622 /* calculate diff between last SPS/PPS in milliseconds */
623 if (timestamp > rtpgstpay->last_config)
624 diff = timestamp - rtpgstpay->last_config;
628 GST_DEBUG_OBJECT (rtpgstpay,
629 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
631 /* bigger than interval, queue SPS/PPS */
632 if (GST_TIME_AS_SECONDS (diff) >= rtpgstpay->config_interval)
633 gst_rtp_gst_pay_send_config (rtpgstpay, timestamp);
635 gst_rtp_gst_pay_send_config (rtpgstpay, timestamp);
639 /* caps always from SDP for now */
640 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
641 rtpgstpay->flags |= (1 << 3);
643 gst_adapter_push (rtpgstpay->adapter, buffer);
644 ret = gst_rtp_gst_pay_flush (rtpgstpay, timestamp);
650 gst_rtp_gst_pay_plugin_init (GstPlugin * plugin)
652 return gst_element_register (plugin, "rtpgstpay",
653 GST_RANK_NONE, GST_TYPE_RTP_GST_PAY);