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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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
59 static GstStaticPadTemplate gst_rtp_gst_pay_sink_template =
60 GST_STATIC_PAD_TEMPLATE ("sink",
65 static GstStaticPadTemplate gst_rtp_gst_pay_src_template =
66 GST_STATIC_PAD_TEMPLATE ("src",
69 GST_STATIC_CAPS ("application/x-rtp, "
70 "media = (string) \"application\", "
71 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
72 "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
75 static void gst_rtp_gst_pay_finalize (GObject * obj);
77 static gboolean gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload,
79 static GstFlowReturn gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * payload,
81 static gboolean gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload,
84 #define gst_rtp_gst_pay_parent_class parent_class
85 G_DEFINE_TYPE (GstRtpGSTPay, gst_rtp_gst_pay, GST_TYPE_RTP_BASE_PAYLOAD);
88 gst_rtp_gst_pay_class_init (GstRtpGSTPayClass * klass)
90 GObjectClass *gobject_class;
91 GstElementClass *gstelement_class;
92 GstRTPBasePayloadClass *gstrtpbasepayload_class;
94 gobject_class = (GObjectClass *) klass;
95 gstelement_class = (GstElementClass *) klass;
96 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
98 gobject_class->finalize = gst_rtp_gst_pay_finalize;
100 gst_element_class_add_pad_template (gstelement_class,
101 gst_static_pad_template_get (&gst_rtp_gst_pay_src_template));
102 gst_element_class_add_pad_template (gstelement_class,
103 gst_static_pad_template_get (&gst_rtp_gst_pay_sink_template));
105 gst_element_class_set_static_metadata (gstelement_class,
106 "RTP GStreamer payloader", "Codec/Payloader/Network/RTP",
107 "Payload GStreamer buffers as RTP packets",
108 "Wim Taymans <wim.taymans@gmail.com>");
110 gstrtpbasepayload_class->set_caps = gst_rtp_gst_pay_setcaps;
111 gstrtpbasepayload_class->handle_buffer = gst_rtp_gst_pay_handle_buffer;
112 gstrtpbasepayload_class->sink_event = gst_rtp_gst_pay_sink_event;
114 GST_DEBUG_CATEGORY_INIT (gst_rtp_pay_debug, "rtpgstpay", 0,
115 "rtpgstpay element");
119 gst_rtp_gst_pay_init (GstRtpGSTPay * rtpgstpay)
121 rtpgstpay->adapter = gst_adapter_new ();
122 gst_rtp_base_payload_set_options (GST_RTP_BASE_PAYLOAD (rtpgstpay),
123 "application", TRUE, "X-GST", 90000);
127 gst_rtp_gst_pay_finalize (GObject * obj)
129 GstRtpGSTPay *rtpgstpay;
131 rtpgstpay = GST_RTP_GST_PAY (obj);
133 g_object_unref (rtpgstpay->adapter);
135 G_OBJECT_CLASS (parent_class)->finalize (obj);
139 gst_rtp_gst_pay_flush (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
146 avail = gst_adapter_available (rtpgstpay->adapter);
154 GstRTPBuffer rtp = { NULL };
157 /* this will be the total lenght of the packet */
158 packet_len = gst_rtp_buffer_calc_packet_len (8 + avail, 0, 0);
160 /* fill one MTU or all available bytes */
161 towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpgstpay));
163 /* this is the payload length */
164 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
166 /* create buffer to hold the payload */
167 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
169 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
170 payload = gst_rtp_buffer_get_payload (&rtp);
172 GST_DEBUG_OBJECT (rtpgstpay, "new packet len %u, frag %u", packet_len,
177 * 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
178 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
179 * |C| CV |D|0|0|0| ETYPE | MBZ |
180 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
182 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
184 payload[0] = rtpgstpay->flags;
185 payload[1] = rtpgstpay->etype;
186 payload[2] = payload[3] = 0;
187 payload[4] = frag_offset >> 24;
188 payload[5] = frag_offset >> 16;
189 payload[6] = frag_offset >> 8;
190 payload[7] = frag_offset & 0xff;
195 GST_DEBUG_OBJECT (rtpgstpay, "copy %u bytes from adapter", payload_len);
197 gst_adapter_copy (rtpgstpay->adapter, payload, 0, payload_len);
198 gst_adapter_flush (rtpgstpay->adapter, payload_len);
200 frag_offset += payload_len;
201 avail -= payload_len;
204 gst_rtp_buffer_set_marker (&rtp, TRUE);
206 gst_rtp_buffer_unmap (&rtp);
208 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
210 ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpgstpay), outbuf);
211 if (ret != GST_FLOW_OK)
214 rtpgstpay->flags &= 0x70;
215 rtpgstpay->etype = 0;
222 GST_DEBUG_OBJECT (rtpgstpay, "push failed %d (%s)", ret,
223 gst_flow_get_name (ret));
224 gst_adapter_clear (rtpgstpay->adapter);
225 rtpgstpay->flags &= 0x70;
226 rtpgstpay->etype = 0;
232 make_data_buffer (GstRtpGSTPay * rtpgstpay, gchar * data, guint size)
239 /* calculate length */
241 while (size >> (7 * plen))
244 outbuf = gst_buffer_new_allocate (NULL, plen + size, NULL);
246 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
252 *ptr++ = ((plen > 0) ? 0x80 : 0) | ((size >> (7 * plen)) & 0x7f);
255 memcpy (ptr, data, size);
256 gst_buffer_unmap (outbuf, &map);
262 gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
264 GstRtpGSTPay *rtpgstpay;
266 gchar *capsstr, *capsenc, *capsver;
270 rtpgstpay = GST_RTP_GST_PAY (payload);
272 capsstr = gst_caps_to_string (caps);
273 capslen = strlen (capsstr);
275 rtpgstpay->current_CV = rtpgstpay->next_CV;
277 /* encode without 0 byte */
278 capsenc = g_base64_encode ((guchar *) capsstr, capslen);
279 GST_DEBUG_OBJECT (payload, "caps=%s, caps(base64)=%s", capsstr, capsenc);
283 /* make a data buffer of it */
284 outbuf = make_data_buffer (rtpgstpay, capsstr, capslen);
287 /* store in adapter, we don't flush yet, buffer might follow */
288 rtpgstpay->flags = (1 << 7) | (rtpgstpay->current_CV << 4);
289 rtpgstpay->next_CV = (rtpgstpay->next_CV + 1) & 0x7;
290 gst_adapter_push (rtpgstpay->adapter, outbuf);
292 /* make caps for SDP */
293 capsver = g_strdup_printf ("%d", rtpgstpay->current_CV);
295 gst_rtp_base_payload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
296 "capsversion", G_TYPE_STRING, capsver, NULL);
304 gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
307 GstRtpGSTPay *rtpgstpay;
310 rtpgstpay = GST_RTP_GST_PAY (payload);
312 ret = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
314 switch (GST_EVENT_TYPE (event)) {
318 case GST_EVENT_CUSTOM_DOWNSTREAM:
321 case GST_EVENT_CUSTOM_BOTH:
326 GST_LOG_OBJECT (rtpgstpay, "no event for %s",
327 GST_EVENT_TYPE_NAME (event));
331 const GstStructure *s;
336 /* make sure the adapter is flushed */
337 gst_rtp_gst_pay_flush (rtpgstpay, GST_CLOCK_TIME_NONE);
339 GST_DEBUG_OBJECT (rtpgstpay, "make event type %d for %s",
340 etype, GST_EVENT_TYPE_NAME (event));
341 s = gst_event_get_structure (event);
343 estr = gst_structure_to_string (s);
344 elen = strlen (estr);
345 outbuf = make_data_buffer (rtpgstpay, estr, elen);
348 rtpgstpay->etype = etype;
349 gst_adapter_push (rtpgstpay->adapter, outbuf);
350 /* flush the adapter immediately */
351 gst_rtp_gst_pay_flush (rtpgstpay, GST_CLOCK_TIME_NONE);
358 gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * basepayload,
362 GstRtpGSTPay *rtpgstpay;
363 GstClockTime timestamp;
365 rtpgstpay = GST_RTP_GST_PAY (basepayload);
367 timestamp = GST_BUFFER_TIMESTAMP (buffer);
369 /* caps always from SDP for now */
370 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
371 rtpgstpay->flags |= (1 << 3);
373 gst_adapter_push (rtpgstpay->adapter, buffer);
374 ret = gst_rtp_gst_pay_flush (rtpgstpay, timestamp);
380 gst_rtp_gst_pay_plugin_init (GstPlugin * plugin)
382 return gst_element_register (plugin, "rtpgstpay",
383 GST_RANK_NONE, GST_TYPE_RTP_GST_PAY);