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"
29 #include "gstrtputils.h"
31 GST_DEBUG_CATEGORY_STATIC (gst_rtp_pay_debug);
32 #define GST_CAT_DEFAULT gst_rtp_pay_debug
36 * 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
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * |C| CV |D|0|0|0| ETYPE | MBZ |
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * C: caps inlined flag
44 * When C set, first part of payload contains caps definition. Caps definition
45 * starts with variable-length length prefix and then a string of that length.
46 * the length is encoded in big endian 7 bit chunks, the top 1 bit of a byte
47 * is the continuation marker and the 7 next bits the data. A continuation
48 * marker of 1 means that the next byte contains more data.
50 * CV: caps version, 0 = caps from SDP, 1 - 7 inlined caps
51 * D: delta unit buffer
52 * ETYPE: type of event. Payload contains the event, prefixed with a
53 * variable length field.
56 * 2 = GST_EVENT_CUSTOM_DOWNSTREAM
57 * 3 = GST_EVENT_CUSTOM_BOTH
58 * 4 = GST_EVENT_STREAM_START
61 static GstStaticPadTemplate gst_rtp_gst_pay_sink_template =
62 GST_STATIC_PAD_TEMPLATE ("sink",
67 static GstStaticPadTemplate gst_rtp_gst_pay_src_template =
68 GST_STATIC_PAD_TEMPLATE ("src",
71 GST_STATIC_CAPS ("application/x-rtp, "
72 "media = (string) \"application\", "
73 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
74 "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_static_pad_template (gstelement_class,
130 &gst_rtp_gst_pay_src_template);
131 gst_element_class_add_static_pad_template (gstelement_class,
132 &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 g_free (rtpgstpay->stream_id);
175 rtpgstpay->stream_id = NULL;
176 rtpgstpay->current_CV = 0;
177 rtpgstpay->next_CV = 0;
182 gst_rtp_gst_pay_finalize (GObject * obj)
184 GstRtpGSTPay *rtpgstpay;
186 rtpgstpay = GST_RTP_GST_PAY (obj);
188 gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
190 g_object_unref (rtpgstpay->adapter);
192 G_OBJECT_CLASS (parent_class)->finalize (obj);
196 gst_rtp_gst_pay_set_property (GObject * object, guint prop_id,
197 const GValue * value, GParamSpec * pspec)
199 GstRtpGSTPay *rtpgstpay;
201 rtpgstpay = GST_RTP_GST_PAY (object);
204 case PROP_CONFIG_INTERVAL:
205 rtpgstpay->config_interval = g_value_get_uint (value);
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214 gst_rtp_gst_pay_get_property (GObject * object, guint prop_id,
215 GValue * value, GParamSpec * pspec)
217 GstRtpGSTPay *rtpgstpay;
219 rtpgstpay = GST_RTP_GST_PAY (object);
222 case PROP_CONFIG_INTERVAL:
223 g_value_set_uint (value, rtpgstpay->config_interval);
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 static GstStateChangeReturn
232 gst_rtp_gst_pay_change_state (GstElement * element, GstStateChange transition)
234 GstRtpGSTPay *rtpgstpay;
235 GstStateChangeReturn ret;
237 rtpgstpay = GST_RTP_GST_PAY (element);
239 switch (transition) {
240 case GST_STATE_CHANGE_READY_TO_PAUSED:
241 gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
247 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
249 switch (transition) {
250 case GST_STATE_CHANGE_PAUSED_TO_READY:
251 gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
259 #define RTP_HEADER_LEN 12
262 gst_rtp_gst_pay_create_from_adapter (GstRtpGSTPay * rtpgstpay,
263 GstClockTime timestamp)
269 avail = gst_adapter_available (rtpgstpay->adapter);
273 mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpgstpay);
275 list = gst_buffer_list_new_sized ((avail / (mtu - (RTP_HEADER_LEN + 8))) + 1);
284 GstRTPBuffer rtp = { NULL };
288 /* this will be the total lenght of the packet */
289 packet_len = gst_rtp_buffer_calc_packet_len (8 + avail, 0, 0);
291 /* fill one MTU or all available bytes */
292 towrite = MIN (packet_len, mtu);
294 /* this is the payload length */
295 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
297 /* create buffer to hold the header */
298 outbuf = gst_rtp_buffer_new_allocate (8, 0, 0);
300 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
301 payload = gst_rtp_buffer_get_payload (&rtp);
303 GST_DEBUG_OBJECT (rtpgstpay, "new packet len %u, frag %u", packet_len,
308 * 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
309 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
310 * |C| CV |D|0|0|0| ETYPE | MBZ |
311 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
313 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
315 payload[0] = rtpgstpay->flags;
316 payload[1] = rtpgstpay->etype;
317 payload[2] = payload[3] = 0;
318 payload[4] = frag_offset >> 24;
319 payload[5] = frag_offset >> 16;
320 payload[6] = frag_offset >> 8;
321 payload[7] = frag_offset & 0xff;
326 frag_offset += payload_len;
327 avail -= payload_len;
330 gst_rtp_buffer_set_marker (&rtp, TRUE);
332 gst_rtp_buffer_unmap (&rtp);
334 /* create a new buf to hold the payload */
335 GST_DEBUG_OBJECT (rtpgstpay, "take %u bytes from adapter", payload_len);
336 paybuf = gst_adapter_take_buffer_fast (rtpgstpay->adapter, payload_len);
338 /* create a new group to hold the rtp header and the payload */
339 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpgstpay), outbuf, paybuf, 0);
340 outbuf = gst_buffer_append (outbuf, paybuf);
342 GST_BUFFER_PTS (outbuf) = timestamp;
344 /* and add to list */
345 gst_buffer_list_insert (list, -1, outbuf);
348 rtpgstpay->flags &= 0x70;
349 rtpgstpay->etype = 0;
350 rtpgstpay->pending_buffers = g_list_append (rtpgstpay->pending_buffers, list);
356 gst_rtp_gst_pay_flush (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
358 GstFlowReturn ret = GST_FLOW_OK;
361 gst_rtp_gst_pay_create_from_adapter (rtpgstpay, timestamp);
363 iter = rtpgstpay->pending_buffers;
365 GstBufferList *list = iter->data;
367 rtpgstpay->pending_buffers = iter =
368 g_list_delete_link (rtpgstpay->pending_buffers, iter);
370 /* push the whole buffer list at once */
371 ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpgstpay),
373 if (ret != GST_FLOW_OK)
381 make_data_buffer (GstRtpGSTPay * rtpgstpay, gchar * data, guint size)
388 /* calculate length */
390 while (size >> (7 * plen))
393 outbuf = gst_buffer_new_allocate (NULL, plen + size, NULL);
395 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
401 *ptr++ = ((plen > 0) ? 0x80 : 0) | ((size >> (7 * plen)) & 0x7f);
404 memcpy (ptr, data, size);
405 gst_buffer_unmap (outbuf, &map);
411 gst_rtp_gst_pay_send_caps (GstRtpGSTPay * rtpgstpay, guint8 cv, GstCaps * caps)
417 if (rtpgstpay->flags & (1 << 7))
420 capsstr = gst_caps_to_string (caps);
421 capslen = strlen (capsstr);
425 GST_DEBUG_OBJECT (rtpgstpay, "sending caps=%s", capsstr);
427 /* make a data buffer of it */
428 outbuf = make_data_buffer (rtpgstpay, capsstr, capslen);
431 /* store in adapter, we don't flush yet, buffer might follow */
432 rtpgstpay->flags = (1 << 7) | (cv << 4);
433 gst_adapter_push (rtpgstpay->adapter, outbuf);
437 gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
439 GstRtpGSTPay *rtpgstpay;
441 gchar *capsstr, *capsenc, *capsver;
444 rtpgstpay = GST_RTP_GST_PAY (payload);
446 capsstr = gst_caps_to_string (caps);
447 capslen = strlen (capsstr);
449 /* encode without 0 byte */
450 capsenc = g_base64_encode ((guchar *) capsstr, capslen);
451 GST_DEBUG_OBJECT (payload, "caps=%s, caps(base64)=%s", capsstr, capsenc);
454 /* Send the new caps */
455 rtpgstpay->current_CV = rtpgstpay->next_CV;
456 rtpgstpay->next_CV = (rtpgstpay->next_CV + 1) & 0x7;
457 gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
459 /* make caps for SDP */
460 capsver = g_strdup_printf ("%d", rtpgstpay->current_CV);
462 gst_rtp_base_payload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
463 "capsversion", G_TYPE_STRING, capsver, NULL);
471 gst_rtp_gst_pay_send_event (GstRtpGSTPay * rtpgstpay, guint etype,
474 const GstStructure *s;
479 /* Create the standalone caps packet if an inlined caps was pending */
480 gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
482 s = gst_event_get_structure (event);
484 estr = gst_structure_to_string (s);
485 elen = strlen (estr);
488 outbuf = make_data_buffer (rtpgstpay, estr, elen);
489 GST_DEBUG_OBJECT (rtpgstpay, "sending event=%s", estr);
492 rtpgstpay->etype = etype;
493 gst_adapter_push (rtpgstpay->adapter, outbuf);
494 /* Create the event packet now to avoid conflict with data/caps packets */
495 gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
499 gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
502 GstRtpGSTPay *rtpgstpay;
505 rtpgstpay = GST_RTP_GST_PAY (payload);
508 GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload,
509 gst_event_ref (event));
511 switch (GST_EVENT_TYPE (event)) {
512 case GST_EVENT_FLUSH_STOP:
513 gst_rtp_gst_pay_reset (rtpgstpay, FALSE);
518 gst_event_parse_tag (event, &tags);
520 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
523 GST_DEBUG_OBJECT (rtpgstpay, "storing stream tags %" GST_PTR_FORMAT,
525 if ((old = rtpgstpay->taglist))
526 gst_tag_list_unref (old);
527 rtpgstpay->taglist = gst_tag_list_ref (tags);
532 case GST_EVENT_CUSTOM_DOWNSTREAM:
535 case GST_EVENT_CUSTOM_BOTH:
538 case GST_EVENT_STREAM_START:{
539 const gchar *stream_id = NULL;
541 if (rtpgstpay->taglist)
542 gst_tag_list_unref (rtpgstpay->taglist);
543 rtpgstpay->taglist = NULL;
545 gst_event_parse_stream_start (event, &stream_id);
547 g_free (rtpgstpay->stream_id);
548 rtpgstpay->stream_id = g_strdup (stream_id);
554 GST_LOG_OBJECT (rtpgstpay, "no event for %s",
555 GST_EVENT_TYPE_NAME (event));
559 GST_DEBUG_OBJECT (rtpgstpay, "make event type %d for %s",
560 etype, GST_EVENT_TYPE_NAME (event));
561 gst_rtp_gst_pay_send_event (rtpgstpay, etype, event);
562 /* Do not send stream-start right away since caps/new-segment were not yet
563 sent, so our data would be considered invalid */
565 /* flush the adapter immediately */
566 gst_rtp_gst_pay_flush (rtpgstpay, GST_CLOCK_TIME_NONE);
570 gst_event_unref (event);
576 gst_rtp_gst_pay_send_config (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
578 GstPad *pad = GST_RTP_BASE_PAYLOAD_SINKPAD (rtpgstpay);
579 GstCaps *caps = NULL;
580 GstEvent *tag = NULL;
581 GstEvent *stream_start = NULL;
583 GST_DEBUG_OBJECT (rtpgstpay, "time to send config");
585 if (rtpgstpay->taglist && !gst_tag_list_is_empty (rtpgstpay->taglist))
586 tag = gst_event_new_tag (gst_tag_list_ref (rtpgstpay->taglist));
588 /* Send start-stream to clear tags */
589 if (rtpgstpay->stream_id)
590 stream_start = gst_event_new_stream_start (rtpgstpay->stream_id);
592 gst_rtp_gst_pay_send_event (rtpgstpay, 4, stream_start);
593 gst_event_unref (stream_start);
595 gst_rtp_gst_pay_send_event (rtpgstpay, 1, tag);
596 gst_event_unref (tag);
599 caps = gst_pad_get_current_caps (pad);
601 gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
602 gst_caps_unref (caps);
604 rtpgstpay->last_config = timestamp;
608 gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * basepayload,
612 GstRtpGSTPay *rtpgstpay;
613 GstClockTime timestamp;
615 rtpgstpay = GST_RTP_GST_PAY (basepayload);
617 timestamp = GST_BUFFER_PTS (buffer);
619 /* check if we need to send the caps and taglist now */
620 if (rtpgstpay->config_interval > 0) {
621 GST_DEBUG_OBJECT (rtpgstpay,
622 "timestamp %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
623 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpgstpay->last_config));
625 if (timestamp != GST_CLOCK_TIME_NONE &&
626 rtpgstpay->last_config != GST_CLOCK_TIME_NONE) {
629 /* calculate diff between last SPS/PPS in milliseconds */
630 if (timestamp > rtpgstpay->last_config)
631 diff = timestamp - rtpgstpay->last_config;
635 GST_DEBUG_OBJECT (rtpgstpay,
636 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
638 /* bigger than interval, queue SPS/PPS */
639 if (GST_TIME_AS_SECONDS (diff) >= rtpgstpay->config_interval)
640 gst_rtp_gst_pay_send_config (rtpgstpay, timestamp);
642 gst_rtp_gst_pay_send_config (rtpgstpay, timestamp);
646 /* caps always from SDP for now */
647 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
648 rtpgstpay->flags |= (1 << 3);
650 gst_adapter_push (rtpgstpay->adapter, buffer);
651 ret = gst_rtp_gst_pay_flush (rtpgstpay, timestamp);
657 gst_rtp_gst_pay_plugin_init (GstPlugin * plugin)
659 return gst_element_register (plugin, "rtpgstpay",
660 GST_RANK_NONE, GST_TYPE_RTP_GST_PAY);