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| 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
55 static GstStaticPadTemplate gst_rtp_gst_pay_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
61 static GstStaticPadTemplate gst_rtp_gst_pay_src_template =
62 GST_STATIC_PAD_TEMPLATE ("src",
65 GST_STATIC_CAPS ("application/x-rtp, "
66 "media = (string) \"application\", "
67 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
68 "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
71 static void gst_rtp_gst_pay_finalize (GObject * obj);
73 static gboolean gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload,
75 static GstFlowReturn gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * payload,
78 #define gst_rtp_gst_pay_parent_class parent_class
79 G_DEFINE_TYPE (GstRtpGSTPay, gst_rtp_gst_pay, GST_TYPE_RTP_BASE_PAYLOAD);
82 gst_rtp_gst_pay_class_init (GstRtpGSTPayClass * klass)
84 GObjectClass *gobject_class;
85 GstElementClass *gstelement_class;
86 GstRTPBasePayloadClass *gstrtpbasepayload_class;
88 gobject_class = (GObjectClass *) klass;
89 gstelement_class = (GstElementClass *) klass;
90 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
92 gobject_class->finalize = gst_rtp_gst_pay_finalize;
94 gst_element_class_add_pad_template (gstelement_class,
95 gst_static_pad_template_get (&gst_rtp_gst_pay_src_template));
96 gst_element_class_add_pad_template (gstelement_class,
97 gst_static_pad_template_get (&gst_rtp_gst_pay_sink_template));
99 gst_element_class_set_static_metadata (gstelement_class,
100 "RTP GStreamer payloader", "Codec/Payloader/Network/RTP",
101 "Payload GStreamer buffers as RTP packets",
102 "Wim Taymans <wim.taymans@gmail.com>");
104 gstrtpbasepayload_class->set_caps = gst_rtp_gst_pay_setcaps;
105 gstrtpbasepayload_class->handle_buffer = gst_rtp_gst_pay_handle_buffer;
107 GST_DEBUG_CATEGORY_INIT (gst_rtp_pay_debug, "rtpgstpay", 0,
108 "rtpgstpay element");
112 gst_rtp_gst_pay_init (GstRtpGSTPay * rtpgstpay)
117 gst_rtp_gst_pay_finalize (GObject * obj)
119 GstRtpGSTPay *rtpgstpay;
121 rtpgstpay = GST_RTP_GST_PAY (obj);
123 g_free (rtpgstpay->capsstr);
125 G_OBJECT_CLASS (parent_class)->finalize (obj);
129 gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
131 GstRtpGSTPay *rtpgstpay;
133 gchar *capsenc, *capsver;
135 rtpgstpay = GST_RTP_GST_PAY (payload);
137 g_free (rtpgstpay->capsstr);
138 rtpgstpay->capsstr = gst_caps_to_string (caps);
139 rtpgstpay->capslen = strlen (rtpgstpay->capsstr);
140 rtpgstpay->current_CV = rtpgstpay->next_CV;
142 /* encode without 0 byte */
143 capsenc = g_base64_encode ((guchar *) rtpgstpay->capsstr, rtpgstpay->capslen);
144 GST_DEBUG_OBJECT (payload, "caps=%s, caps(base64)=%s",
145 rtpgstpay->capsstr, capsenc);
147 rtpgstpay->capslen++;
149 capsver = g_strdup_printf ("%d", rtpgstpay->current_CV);
151 gst_rtp_base_payload_set_options (payload, "application", TRUE, "X-GST",
154 gst_rtp_base_payload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
155 "capsversion", G_TYPE_STRING, capsver, NULL);
163 gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * basepayload,
166 GstRtpGSTPay *rtpgstpay;
172 GstClockTime timestamp;
177 guint capslen_prefix_len;
179 rtpgstpay = GST_RTP_GST_PAY (basepayload);
181 gst_buffer_map (buffer, &map, GST_MAP_READ);
182 timestamp = GST_BUFFER_TIMESTAMP (buffer);
186 /* caps always from SDP for now */
188 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
191 capsstr = rtpgstpay->capsstr;
192 capslen = rtpgstpay->capslen;
194 /* start of buffer, calculate length */
195 capslen_prefix_len = 1;
196 while (capslen >> (7 * capslen_prefix_len))
197 capslen_prefix_len++;
199 GST_DEBUG_OBJECT (rtpgstpay, "sending inline caps");
200 rtpgstpay->next_CV++;
204 capslen_prefix_len = 0;
207 flags |= (rtpgstpay->current_CV << 4);
211 * 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
212 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
213 * |C| CV |D|X|Y|Z| MBZ |
214 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
216 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
222 GST_DEBUG_OBJECT (basepayload, "buffer size=%u", left);
229 GstRTPBuffer rtp = { NULL };
231 /* this will be the total lenght of the packet */
233 gst_rtp_buffer_calc_packet_len (8 + capslen + capslen_prefix_len + left,
236 /* fill one MTU or all available bytes */
237 towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpgstpay));
239 /* this is the payload length */
240 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
242 /* create buffer to hold the payload */
243 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
245 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
246 payload = gst_rtp_buffer_get_payload (&rtp);
248 GST_DEBUG_OBJECT (basepayload, "new packet len %u, frag %u", packet_len,
252 payload[1] = payload[2] = payload[3] = 0;
253 payload[4] = frag_offset >> 24;
254 payload[5] = frag_offset >> 16;
255 payload[6] = frag_offset >> 8;
256 payload[7] = frag_offset & 0xff;
264 /* we need to write caps */
265 if (frag_offset == 0) {
266 /* write caps length */
267 while (capslen_prefix_len) {
268 capslen_prefix_len--;
269 *payload++ = ((capslen_prefix_len > 0) ? 0x80 : 0) |
270 ((capslen >> (7 * capslen_prefix_len)) & 0x7f);
276 tocopy = MIN (payload_len, capslen);
277 GST_DEBUG_OBJECT (basepayload, "copy %u bytes from caps to payload",
279 memcpy (payload, capsstr, tocopy);
284 payload_len -= tocopy;
285 frag_offset += tocopy;
288 rtpgstpay->capslen = 0;
289 g_free (rtpgstpay->capsstr);
290 rtpgstpay->capsstr = NULL;
295 /* no more caps, continue with data */
296 GST_DEBUG_OBJECT (basepayload, "copy %u bytes from buffer to payload",
298 memcpy (payload, ptr, payload_len);
302 frag_offset += payload_len;
306 gst_rtp_buffer_set_marker (&rtp, TRUE);
308 gst_rtp_buffer_unmap (&rtp);
310 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
312 ret = gst_rtp_base_payload_push (basepayload, outbuf);
314 gst_buffer_unmap (buffer, &map);
315 gst_buffer_unref (buffer);
321 gst_rtp_gst_pay_plugin_init (GstPlugin * plugin)
323 return gst_element_register (plugin, "rtpgstpay",
324 GST_RANK_NONE, GST_TYPE_RTP_GST_PAY);