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"
32 * 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
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * |C| CV |D|0|0|0| MBZ |
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * C: caps inlined flag
40 * When C set, first part of payload contains caps definition. Caps definition
41 * starts with variable-length length prefix and then a string of that length.
42 * the length is encoded in big endian 7 bit chunks, the top 1 bit of a byte
43 * is the continuation marker and the 7 next bits the data. A continuation
44 * marker of 1 means that the next byte contains more data.
46 * CV: caps version, 0 = caps from SDP, 1 - 7 inlined caps
47 * D: delta unit buffer
52 static GstStaticPadTemplate gst_rtp_gst_pay_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
58 static GstStaticPadTemplate gst_rtp_gst_pay_src_template =
59 GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS ("application/x-rtp, "
63 "media = (string) \"application\", "
64 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
65 "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
68 static gboolean gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload,
70 static GstFlowReturn gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * payload,
73 #define gst_rtp_gst_pay_parent_class parent_class
74 G_DEFINE_TYPE (GstRtpGSTPay, gst_rtp_gst_pay, GST_TYPE_RTP_BASE_PAYLOAD);
77 gst_rtp_gst_pay_class_init (GstRtpGSTPayClass * klass)
79 GstElementClass *gstelement_class;
80 GstRTPBasePayloadClass *gstrtpbasepayload_class;
82 gstelement_class = (GstElementClass *) klass;
83 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
85 gst_element_class_add_pad_template (gstelement_class,
86 gst_static_pad_template_get (&gst_rtp_gst_pay_src_template));
87 gst_element_class_add_pad_template (gstelement_class,
88 gst_static_pad_template_get (&gst_rtp_gst_pay_sink_template));
90 gst_element_class_set_details_simple (gstelement_class,
91 "RTP GStreamer payloader", "Codec/Payloader/Network/RTP",
92 "Payload GStreamer buffers as RTP packets",
93 "Wim Taymans <wim.taymans@gmail.com>");
95 gstrtpbasepayload_class->set_caps = gst_rtp_gst_pay_setcaps;
96 gstrtpbasepayload_class->handle_buffer = gst_rtp_gst_pay_handle_buffer;
100 gst_rtp_gst_pay_init (GstRtpGSTPay * rtpgstpay)
105 gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
108 gchar *capsstr, *capsenc;
110 capsstr = gst_caps_to_string (caps);
111 capsenc = g_base64_encode ((guchar *) capsstr, strlen (capsstr));
114 gst_rtp_base_payload_set_options (payload, "application", TRUE, "X-GST",
117 gst_rtp_base_payload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
125 gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * basepayload,
128 GstRtpGSTPay *rtpgstpay;
133 GstClockTime timestamp;
137 rtpgstpay = GST_RTP_GST_PAY (basepayload);
139 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
140 timestamp = GST_BUFFER_TIMESTAMP (buffer);
144 /* caps always from SDP for now */
146 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
151 * 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
152 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
153 * |C| CV |D|X|Y|Z| MBZ |
154 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
156 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
167 GstRTPBuffer rtp = { NULL };
169 /* this will be the total lenght of the packet */
170 packet_len = gst_rtp_buffer_calc_packet_len (8 + left, 0, 0);
172 /* fill one MTU or all available bytes */
173 towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpgstpay));
175 /* this is the payload length */
176 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
178 /* create buffer to hold the payload */
179 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
181 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
182 payload = gst_rtp_buffer_get_payload (&rtp);
185 payload[1] = payload[2] = payload[3] = 0;
186 payload[4] = frag_offset >> 24;
187 payload[5] = frag_offset >> 16;
188 payload[6] = frag_offset >> 8;
189 payload[7] = frag_offset & 0xff;
194 memcpy (payload, ptr, payload_len);
198 frag_offset += payload_len;
201 gst_rtp_buffer_set_marker (&rtp, TRUE);
203 gst_rtp_buffer_unmap (&rtp);
205 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
207 ret = gst_rtp_base_payload_push (basepayload, outbuf);
209 gst_buffer_unmap (buffer, data, size);
210 gst_buffer_unref (buffer);
216 gst_rtp_gst_pay_plugin_init (GstPlugin * plugin)
218 return gst_element_register (plugin, "rtpgstpay",
219 GST_RANK_NONE, GST_TYPE_RTP_GST_PAY);