1 /*
2 * GStreamer
3 * Copyright (C) 2013 Sebastian Dröge <sebastian@centricular.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /**
22 * SECTION:element-rtpstreampay
23 *
24 * Implements stream payloading of RTP and RTCP packets for connection-oriented
25 * transport protocols according to RFC4571.
26 * <refsect2>
27 * <title>Example launch line</title>
28 * |[
29 * gst-launch-1.0 audiotestsrc ! "audio/x-raw,rate=48000" ! vorbisenc ! rtpvorbispay config-interval=1 ! rtpstreampay ! tcpserversink port=5678
30 * gst-launch-1.0 tcpclientsrc port=5678 host=127.0.0.1 do-timestamp=true ! "application/x-rtp-stream,media=audio,clock-rate=48000,encoding-name=VORBIS" ! rtpstreamdepay ! rtpvorbisdepay ! decodebin ! audioconvert ! audioresample ! autoaudiosink
31 * ]|
32 * </refsect2>
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include "gstrtpstreampay.h"
40
41 #define GST_CAT_DEFAULT gst_rtp_stream_pay_debug
42 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
43
44 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_PAD_SINK,
46 GST_PAD_ALWAYS,
47 GST_STATIC_CAPS ("application/x-rtp; application/x-rtcp; "
48 "application/x-srtp; application/x-srtcp")
49 );
50
51 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
52 GST_PAD_SRC,
53 GST_PAD_ALWAYS,
54 GST_STATIC_CAPS ("application/x-rtp-stream; application/x-rtcp-stream; "
55 "application/x-srtp-stream; application/x-srtcp-stream")
56 );
57
58 #define parent_class gst_rtp_stream_pay_parent_class
59 G_DEFINE_TYPE (GstRtpStreamPay, gst_rtp_stream_pay, GST_TYPE_ELEMENT);
60
61 static gboolean gst_rtp_stream_pay_sink_query (GstPad * pad, GstObject * parent,
62 GstQuery * query);
63 static GstFlowReturn gst_rtp_stream_pay_sink_chain (GstPad * pad,
64 GstObject * parent, GstBuffer * inbuf);
65 static gboolean gst_rtp_stream_pay_sink_event (GstPad * pad, GstObject * parent,
66 GstEvent * event);
67
68 static void
gst_rtp_stream_pay_class_init(GstRtpStreamPayClass * klass)69 gst_rtp_stream_pay_class_init (GstRtpStreamPayClass * klass)
70 {
71 GstElementClass *gstelement_class;
72
73 GST_DEBUG_CATEGORY_INIT (gst_rtp_stream_pay_debug, "rtpstreampay", 0,
74 "RTP stream payloader");
75
76 gstelement_class = (GstElementClass *) klass;
77
78 gst_element_class_set_static_metadata (gstelement_class,
79 "RTP Stream Payloading", "Codec/Payloader/Network",
80 "Payloads RTP/RTCP packets for streaming protocols according to RFC4571",
81 "Sebastian Dröge <sebastian@centricular.com>");
82
83 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
84 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
85 }
86
87 static void
gst_rtp_stream_pay_init(GstRtpStreamPay * self)88 gst_rtp_stream_pay_init (GstRtpStreamPay * self)
89 {
90 self->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
91 gst_pad_set_chain_function (self->sinkpad,
92 GST_DEBUG_FUNCPTR (gst_rtp_stream_pay_sink_chain));
93 gst_pad_set_event_function (self->sinkpad,
94 GST_DEBUG_FUNCPTR (gst_rtp_stream_pay_sink_event));
95 gst_pad_set_query_function (self->sinkpad,
96 GST_DEBUG_FUNCPTR (gst_rtp_stream_pay_sink_query));
97 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
98
99 self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
100 gst_pad_use_fixed_caps (self->srcpad);
101 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
102 }
103
104 static GstCaps *
gst_rtp_stream_pay_sink_get_caps(GstRtpStreamPay * self,GstCaps * filter)105 gst_rtp_stream_pay_sink_get_caps (GstRtpStreamPay * self, GstCaps * filter)
106 {
107 GstCaps *peerfilter = NULL, *peercaps, *templ;
108 GstCaps *res;
109 GstStructure *structure;
110 guint i, n;
111
112 if (filter) {
113 peerfilter = gst_caps_copy (filter);
114 n = gst_caps_get_size (peerfilter);
115 for (i = 0; i < n; i++) {
116 structure = gst_caps_get_structure (peerfilter, i);
117
118 if (gst_structure_has_name (structure, "application/x-rtp"))
119 gst_structure_set_name (structure, "application/x-rtp-stream");
120 else if (gst_structure_has_name (structure, "application/x-rtcp"))
121 gst_structure_set_name (structure, "application/x-rtcp-stream");
122 else if (gst_structure_has_name (structure, "application/x-srtp"))
123 gst_structure_set_name (structure, "application/x-srtp-stream");
124 else
125 gst_structure_set_name (structure, "application/x-srtcp-stream");
126 }
127 }
128
129 templ = gst_pad_get_pad_template_caps (self->sinkpad);
130 peercaps = gst_pad_peer_query_caps (self->srcpad, peerfilter);
131
132 if (peercaps) {
133 /* Rename structure names */
134 peercaps = gst_caps_make_writable (peercaps);
135 n = gst_caps_get_size (peercaps);
136 for (i = 0; i < n; i++) {
137 structure = gst_caps_get_structure (peercaps, i);
138
139 if (gst_structure_has_name (structure, "application/x-rtp-stream"))
140 gst_structure_set_name (structure, "application/x-rtp");
141 else if (gst_structure_has_name (structure, "application/x-rtcp-stream"))
142 gst_structure_set_name (structure, "application/x-rtcp");
143 else if (gst_structure_has_name (structure, "application/x-srtp-stream"))
144 gst_structure_set_name (structure, "application/x-srtp");
145 else
146 gst_structure_set_name (structure, "application/x-srtcp");
147 }
148
149 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
150 gst_caps_unref (peercaps);
151 } else {
152 res = templ;
153 }
154
155 if (filter) {
156 GstCaps *intersection;
157
158 intersection =
159 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
160 gst_caps_unref (res);
161 res = intersection;
162
163 gst_caps_unref (peerfilter);
164 }
165
166 return res;
167 }
168
169 static gboolean
gst_rtp_stream_pay_sink_query(GstPad * pad,GstObject * parent,GstQuery * query)170 gst_rtp_stream_pay_sink_query (GstPad * pad, GstObject * parent,
171 GstQuery * query)
172 {
173 GstRtpStreamPay *self = GST_RTP_STREAM_PAY (parent);
174 gboolean ret;
175
176 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
177 gst_query_type_get_name (GST_QUERY_TYPE (query)));
178
179 switch (GST_QUERY_TYPE (query)) {
180 case GST_QUERY_CAPS:
181 {
182 GstCaps *caps;
183
184 gst_query_parse_caps (query, &caps);
185 caps = gst_rtp_stream_pay_sink_get_caps (self, caps);
186 gst_query_set_caps_result (query, caps);
187 gst_caps_unref (caps);
188 ret = TRUE;
189 break;
190 }
191 default:
192 ret = gst_pad_query_default (pad, parent, query);
193 }
194
195 return ret;
196 }
197
198 static gboolean
gst_rtp_stream_pay_sink_set_caps(GstRtpStreamPay * self,GstCaps * caps)199 gst_rtp_stream_pay_sink_set_caps (GstRtpStreamPay * self, GstCaps * caps)
200 {
201 GstCaps *othercaps;
202 GstStructure *structure;
203 gboolean ret;
204
205 othercaps = gst_caps_copy (caps);
206 structure = gst_caps_get_structure (othercaps, 0);
207
208 if (gst_structure_has_name (structure, "application/x-rtp"))
209 gst_structure_set_name (structure, "application/x-rtp-stream");
210 else if (gst_structure_has_name (structure, "application/x-rtcp"))
211 gst_structure_set_name (structure, "application/x-rtcp-stream");
212 else if (gst_structure_has_name (structure, "application/x-srtp"))
213 gst_structure_set_name (structure, "application/x-srtp-stream");
214 else
215 gst_structure_set_name (structure, "application/x-srtcp-stream");
216
217 ret = gst_pad_set_caps (self->srcpad, othercaps);
218 gst_caps_unref (othercaps);
219
220 return ret;
221 }
222
223 static gboolean
gst_rtp_stream_pay_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)224 gst_rtp_stream_pay_sink_event (GstPad * pad, GstObject * parent,
225 GstEvent * event)
226 {
227 GstRtpStreamPay *self = GST_RTP_STREAM_PAY (parent);
228 gboolean ret;
229
230 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
231
232 switch (GST_EVENT_TYPE (event)) {
233 case GST_EVENT_CAPS:
234 {
235 GstCaps *caps;
236
237 gst_event_parse_caps (event, &caps);
238 ret = gst_rtp_stream_pay_sink_set_caps (self, caps);
239 gst_event_unref (event);
240 break;
241 }
242 default:
243 ret = gst_pad_event_default (pad, parent, event);
244 break;
245 }
246
247 return ret;
248 }
249
250 static GstFlowReturn
gst_rtp_stream_pay_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * inbuf)251 gst_rtp_stream_pay_sink_chain (GstPad * pad, GstObject * parent,
252 GstBuffer * inbuf)
253 {
254 GstRtpStreamPay *self = GST_RTP_STREAM_PAY (parent);
255 GstBuffer *outbuf;
256 gsize size;
257 guint8 size16[2];
258
259 size = gst_buffer_get_size (inbuf);
260 if (size > G_MAXUINT16) {
261 GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
262 ("Only buffers up to %d bytes supported, got %" G_GSIZE_FORMAT,
263 G_MAXUINT16, size));
264 gst_buffer_unref (inbuf);
265 return GST_FLOW_ERROR;
266 }
267
268 outbuf = gst_buffer_new_and_alloc (2);
269
270 GST_WRITE_UINT16_BE (size16, size);
271 gst_buffer_fill (outbuf, 0, size16, 2);
272
273 gst_buffer_copy_into (outbuf, inbuf, GST_BUFFER_COPY_ALL, 0, -1);
274
275 gst_buffer_unref (inbuf);
276
277 return gst_pad_push (self->srcpad, outbuf);
278 }
279
280 gboolean
gst_rtp_stream_pay_plugin_init(GstPlugin * plugin)281 gst_rtp_stream_pay_plugin_init (GstPlugin * plugin)
282 {
283 return gst_element_register (plugin, "rtpstreampay",
284 GST_RANK_NONE, GST_TYPE_RTP_STREAM_PAY);
285 }
286