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 lenght 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 */
302 outbuf = gst_rtp_buffer_new_allocate (8, 0, 0);
304 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
305 payload = gst_rtp_buffer_get_payload (&rtp);
307 GST_DEBUG_OBJECT (rtpgstpay, "new packet len %u, frag %u", packet_len,
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
317 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
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;
330 frag_offset += payload_len;
331 avail -= payload_len;
334 gst_rtp_buffer_set_marker (&rtp, TRUE);
336 gst_rtp_buffer_unmap (&rtp);
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);
342 /* create a new group to hold the rtp header and the payload */
343 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpgstpay), outbuf, paybuf, 0);
344 outbuf = gst_buffer_append (outbuf, paybuf);
346 GST_BUFFER_PTS (outbuf) = timestamp;
348 /* and add to list */
349 gst_buffer_list_insert (list, -1, outbuf);
352 rtpgstpay->flags &= 0x70;
353 rtpgstpay->etype = 0;
354 rtpgstpay->pending_buffers = g_list_append (rtpgstpay->pending_buffers, list);
360 gst_rtp_gst_pay_flush (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
362 GstFlowReturn ret = GST_FLOW_OK;
365 gst_rtp_gst_pay_create_from_adapter (rtpgstpay, timestamp);
367 iter = rtpgstpay->pending_buffers;
369 GstBufferList *list = iter->data;
371 rtpgstpay->pending_buffers = iter =
372 g_list_delete_link (rtpgstpay->pending_buffers, iter);
374 /* push the whole buffer list at once */
375 ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpgstpay),
377 if (ret != GST_FLOW_OK)
385 make_data_buffer (GstRtpGSTPay * rtpgstpay, gchar * data, guint size)
392 /* calculate length */
394 while (size >> (7 * plen))
397 outbuf = gst_buffer_new_allocate (NULL, plen + size, NULL);
399 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
405 *ptr++ = ((plen > 0) ? 0x80 : 0) | ((size >> (7 * plen)) & 0x7f);
408 memcpy (ptr, data, size);
409 gst_buffer_unmap (outbuf, &map);
415 gst_rtp_gst_pay_send_caps (GstRtpGSTPay * rtpgstpay, guint8 cv, GstCaps * caps)
421 if (rtpgstpay->flags & (1 << 7))
424 capsstr = gst_caps_to_string (caps);
425 capslen = strlen (capsstr);
429 GST_DEBUG_OBJECT (rtpgstpay, "sending caps=%s", capsstr);
431 /* make a data buffer of it */
432 outbuf = make_data_buffer (rtpgstpay, capsstr, capslen);
435 /* store in adapter, we don't flush yet, buffer might follow */
436 rtpgstpay->flags = (1 << 7) | (cv << 4);
437 gst_adapter_push (rtpgstpay->adapter, outbuf);
441 gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
443 GstRtpGSTPay *rtpgstpay;
445 gchar *capsstr, *capsenc, *capsver;
448 rtpgstpay = GST_RTP_GST_PAY (payload);
450 capsstr = gst_caps_to_string (caps);
451 capslen = strlen (capsstr);
453 /* encode without 0 byte */
454 capsenc = g_base64_encode ((guchar *) capsstr, capslen);
455 GST_DEBUG_OBJECT (payload, "caps=%s, caps(base64)=%s", capsstr, capsenc);
458 /* Send the new caps */
459 rtpgstpay->current_CV = rtpgstpay->next_CV;
460 rtpgstpay->next_CV = (rtpgstpay->next_CV + 1) & 0x7;
461 gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
463 /* make caps for SDP */
464 capsver = g_strdup_printf ("%d", rtpgstpay->current_CV);
466 gst_rtp_base_payload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
467 "capsversion", G_TYPE_STRING, capsver, NULL);
475 gst_rtp_gst_pay_send_event (GstRtpGSTPay * rtpgstpay, guint etype,
478 const GstStructure *s;
483 /* Create the standalone caps packet if an inlined caps was pending */
484 gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
486 s = gst_event_get_structure (event);
488 estr = gst_structure_to_string (s);
489 elen = strlen (estr);
492 outbuf = make_data_buffer (rtpgstpay, estr, elen);
493 GST_DEBUG_OBJECT (rtpgstpay, "sending event=%s", estr);
496 rtpgstpay->etype = etype;
497 gst_adapter_push (rtpgstpay->adapter, outbuf);
498 /* Create the event packet now to avoid conflict with data/caps packets */
499 gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
503 gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
506 GstRtpGSTPay *rtpgstpay;
509 rtpgstpay = GST_RTP_GST_PAY (payload);
511 if (gst_video_event_is_force_key_unit (event)) {
512 g_atomic_int_set (&rtpgstpay->force_config, TRUE);
516 GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload,
517 gst_event_ref (event));
519 switch (GST_EVENT_TYPE (event)) {
520 case GST_EVENT_FLUSH_STOP:
521 gst_rtp_gst_pay_reset (rtpgstpay, FALSE);
526 gst_event_parse_tag (event, &tags);
528 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
531 GST_DEBUG_OBJECT (rtpgstpay, "storing stream tags %" GST_PTR_FORMAT,
533 if ((old = rtpgstpay->taglist))
534 gst_tag_list_unref (old);
535 rtpgstpay->taglist = gst_tag_list_ref (tags);
540 case GST_EVENT_CUSTOM_DOWNSTREAM:
543 case GST_EVENT_CUSTOM_BOTH:
546 case GST_EVENT_STREAM_START:{
547 const gchar *stream_id = NULL;
549 if (rtpgstpay->taglist)
550 gst_tag_list_unref (rtpgstpay->taglist);
551 rtpgstpay->taglist = NULL;
553 gst_event_parse_stream_start (event, &stream_id);
555 g_free (rtpgstpay->stream_id);
556 rtpgstpay->stream_id = g_strdup (stream_id);
562 GST_LOG_OBJECT (rtpgstpay, "no event for %s",
563 GST_EVENT_TYPE_NAME (event));
567 GST_DEBUG_OBJECT (rtpgstpay, "make event type %d for %s",
568 etype, GST_EVENT_TYPE_NAME (event));
569 gst_rtp_gst_pay_send_event (rtpgstpay, etype, event);
570 /* Do not send stream-start right away since caps/new-segment were not yet
571 sent, so our data would be considered invalid */
573 /* flush the adapter immediately */
574 gst_rtp_gst_pay_flush (rtpgstpay, GST_CLOCK_TIME_NONE);
578 gst_event_unref (event);
584 gst_rtp_gst_pay_src_event (GstRTPBasePayload * payload, GstEvent * event)
586 GstRtpGSTPay *rtpgstpay;
588 rtpgstpay = GST_RTP_GST_PAY (payload);
590 if (gst_video_event_is_force_key_unit (event)) {
591 g_atomic_int_set (&rtpgstpay->force_config, TRUE);
594 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->src_event (payload, event);
598 gst_rtp_gst_pay_send_config (GstRtpGSTPay * rtpgstpay,
599 GstClockTime running_time)
601 GstPad *pad = GST_RTP_BASE_PAYLOAD_SINKPAD (rtpgstpay);
602 GstCaps *caps = NULL;
603 GstEvent *tag = NULL;
604 GstEvent *stream_start = NULL;
606 GST_DEBUG_OBJECT (rtpgstpay, "time to send config");
608 if (rtpgstpay->taglist && !gst_tag_list_is_empty (rtpgstpay->taglist))
609 tag = gst_event_new_tag (gst_tag_list_ref (rtpgstpay->taglist));
611 /* Send start-stream to clear tags */
612 if (rtpgstpay->stream_id)
613 stream_start = gst_event_new_stream_start (rtpgstpay->stream_id);
615 gst_rtp_gst_pay_send_event (rtpgstpay, 4, stream_start);
616 gst_event_unref (stream_start);
618 gst_rtp_gst_pay_send_event (rtpgstpay, 1, tag);
619 gst_event_unref (tag);
622 caps = gst_pad_get_current_caps (pad);
624 gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
625 gst_caps_unref (caps);
627 rtpgstpay->last_config = running_time;
631 gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * basepayload,
635 GstRtpGSTPay *rtpgstpay;
636 GstClockTime timestamp, running_time;
638 rtpgstpay = GST_RTP_GST_PAY (basepayload);
640 timestamp = GST_BUFFER_PTS (buffer);
642 gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
645 /* check if we need to send the caps and taglist now */
646 if (rtpgstpay->config_interval > 0
647 || g_atomic_int_compare_and_exchange (&rtpgstpay->force_config, TRUE,
649 GST_DEBUG_OBJECT (rtpgstpay,
650 "running time %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
651 GST_TIME_ARGS (running_time), GST_TIME_ARGS (rtpgstpay->last_config));
653 if (running_time != GST_CLOCK_TIME_NONE &&
654 rtpgstpay->last_config != GST_CLOCK_TIME_NONE) {
657 /* calculate diff between last SPS/PPS in milliseconds */
658 if (running_time > rtpgstpay->last_config)
659 diff = running_time - rtpgstpay->last_config;
663 GST_DEBUG_OBJECT (rtpgstpay,
664 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
666 /* bigger than interval, queue SPS/PPS */
667 if (GST_TIME_AS_SECONDS (diff) >= rtpgstpay->config_interval)
668 gst_rtp_gst_pay_send_config (rtpgstpay, running_time);
670 gst_rtp_gst_pay_send_config (rtpgstpay, running_time);
674 /* caps always from SDP for now */
675 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
676 rtpgstpay->flags |= (1 << 3);
678 gst_adapter_push (rtpgstpay->adapter, buffer);
679 ret = gst_rtp_gst_pay_flush (rtpgstpay, timestamp);
685 gst_rtp_gst_pay_plugin_init (GstPlugin * plugin)
687 return gst_element_register (plugin, "rtpgstpay",
688 GST_RANK_NONE, GST_TYPE_RTP_GST_PAY);