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 * @title: rtpstreampay
24 *
25 * Implements stream payloading of RTP and RTCP packets for connection-oriented
26 * transport protocols according to RFC4571.
27 *
28 * ## Example launch line
29 * |[
30 * gst-launch-1.0 audiotestsrc ! "audio/x-raw,rate=48000" ! vorbisenc ! rtpvorbispay config-interval=1 ! rtpstreampay ! tcpserversink port=5678
31 * 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
32 * ]|
33 *
34 */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "gstrtpelements.h"
41 #include "gstrtpstreampay.h"
42
43 #define GST_CAT_DEFAULT gst_rtp_stream_pay_debug
44 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
45
46 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
47 GST_PAD_SINK,
48 GST_PAD_ALWAYS,
49 GST_STATIC_CAPS ("application/x-rtp; application/x-rtcp; "
50 "application/x-srtp; application/x-srtcp")
51 );
52
53 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
54 GST_PAD_SRC,
55 GST_PAD_ALWAYS,
56 GST_STATIC_CAPS ("application/x-rtp-stream; application/x-rtcp-stream; "
57 "application/x-srtp-stream; application/x-srtcp-stream")
58 );
59
60 #define parent_class gst_rtp_stream_pay_parent_class
61 G_DEFINE_TYPE (GstRtpStreamPay, gst_rtp_stream_pay, GST_TYPE_ELEMENT);
62 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpstreampay, "rtpstreampay",
63 GST_RANK_NONE, GST_TYPE_RTP_STREAM_PAY, rtp_element_init (plugin));
64
65 static gboolean gst_rtp_stream_pay_sink_query (GstPad * pad, GstObject * parent,
66 GstQuery * query);
67 static GstFlowReturn gst_rtp_stream_pay_sink_chain (GstPad * pad,
68 GstObject * parent, GstBuffer * inbuf);
69 static gboolean gst_rtp_stream_pay_sink_event (GstPad * pad, GstObject * parent,
70 GstEvent * event);
71
72 static void
gst_rtp_stream_pay_class_init(GstRtpStreamPayClass * klass)73 gst_rtp_stream_pay_class_init (GstRtpStreamPayClass * klass)
74 {
75 GstElementClass *gstelement_class;
76
77 GST_DEBUG_CATEGORY_INIT (gst_rtp_stream_pay_debug, "rtpstreampay", 0,
78 "RTP stream payloader");
79
80 gstelement_class = (GstElementClass *) klass;
81
82 gst_element_class_set_static_metadata (gstelement_class,
83 "RTP Stream Payloading", "Codec/Payloader/Network",
84 "Payloads RTP/RTCP packets for streaming protocols according to RFC4571",
85 "Sebastian Dröge <sebastian@centricular.com>");
86
87 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
88 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
89 }
90
91 static void
gst_rtp_stream_pay_init(GstRtpStreamPay * self)92 gst_rtp_stream_pay_init (GstRtpStreamPay * self)
93 {
94 self->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
95 gst_pad_set_chain_function (self->sinkpad,
96 GST_DEBUG_FUNCPTR (gst_rtp_stream_pay_sink_chain));
97 gst_pad_set_event_function (self->sinkpad,
98 GST_DEBUG_FUNCPTR (gst_rtp_stream_pay_sink_event));
99 gst_pad_set_query_function (self->sinkpad,
100 GST_DEBUG_FUNCPTR (gst_rtp_stream_pay_sink_query));
101 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
102
103 self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
104 gst_pad_use_fixed_caps (self->srcpad);
105 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
106 }
107
108 static GstCaps *
gst_rtp_stream_pay_sink_get_caps(GstRtpStreamPay * self,GstCaps * filter)109 gst_rtp_stream_pay_sink_get_caps (GstRtpStreamPay * self, GstCaps * filter)
110 {
111 GstCaps *peerfilter = NULL, *peercaps, *templ;
112 GstCaps *res;
113 GstStructure *structure;
114 guint i, n;
115
116 if (filter) {
117 peerfilter = gst_caps_copy (filter);
118 n = gst_caps_get_size (peerfilter);
119 for (i = 0; i < n; i++) {
120 structure = gst_caps_get_structure (peerfilter, i);
121
122 if (gst_structure_has_name (structure, "application/x-rtp"))
123 gst_structure_set_name (structure, "application/x-rtp-stream");
124 else if (gst_structure_has_name (structure, "application/x-rtcp"))
125 gst_structure_set_name (structure, "application/x-rtcp-stream");
126 else if (gst_structure_has_name (structure, "application/x-srtp"))
127 gst_structure_set_name (structure, "application/x-srtp-stream");
128 else
129 gst_structure_set_name (structure, "application/x-srtcp-stream");
130 }
131 }
132
133 templ = gst_pad_get_pad_template_caps (self->sinkpad);
134 peercaps = gst_pad_peer_query_caps (self->srcpad, peerfilter);
135
136 if (peercaps) {
137 /* Rename structure names */
138 peercaps = gst_caps_make_writable (peercaps);
139 n = gst_caps_get_size (peercaps);
140 for (i = 0; i < n; i++) {
141 structure = gst_caps_get_structure (peercaps, i);
142
143 if (gst_structure_has_name (structure, "application/x-rtp-stream"))
144 gst_structure_set_name (structure, "application/x-rtp");
145 else if (gst_structure_has_name (structure, "application/x-rtcp-stream"))
146 gst_structure_set_name (structure, "application/x-rtcp");
147 else if (gst_structure_has_name (structure, "application/x-srtp-stream"))
148 gst_structure_set_name (structure, "application/x-srtp");
149 else
150 gst_structure_set_name (structure, "application/x-srtcp");
151 }
152
153 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
154 gst_caps_unref (peercaps);
155 } else {
156 res = templ;
157 }
158
159 if (filter) {
160 GstCaps *intersection;
161
162 intersection =
163 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
164 gst_caps_unref (res);
165 res = intersection;
166
167 gst_caps_unref (peerfilter);
168 }
169
170 return res;
171 }
172
173 static gboolean
gst_rtp_stream_pay_sink_query(GstPad * pad,GstObject * parent,GstQuery * query)174 gst_rtp_stream_pay_sink_query (GstPad * pad, GstObject * parent,
175 GstQuery * query)
176 {
177 GstRtpStreamPay *self = GST_RTP_STREAM_PAY (parent);
178 gboolean ret;
179
180 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
181 gst_query_type_get_name (GST_QUERY_TYPE (query)));
182
183 switch (GST_QUERY_TYPE (query)) {
184 case GST_QUERY_CAPS:
185 {
186 GstCaps *caps;
187
188 gst_query_parse_caps (query, &caps);
189 caps = gst_rtp_stream_pay_sink_get_caps (self, caps);
190 gst_query_set_caps_result (query, caps);
191 gst_caps_unref (caps);
192 ret = TRUE;
193 break;
194 }
195 default:
196 ret = gst_pad_query_default (pad, parent, query);
197 }
198
199 return ret;
200 }
201
202 static gboolean
gst_rtp_stream_pay_sink_set_caps(GstRtpStreamPay * self,GstCaps * caps)203 gst_rtp_stream_pay_sink_set_caps (GstRtpStreamPay * self, GstCaps * caps)
204 {
205 GstCaps *othercaps;
206 GstStructure *structure;
207 gboolean ret;
208
209 othercaps = gst_caps_copy (caps);
210 structure = gst_caps_get_structure (othercaps, 0);
211
212 if (gst_structure_has_name (structure, "application/x-rtp"))
213 gst_structure_set_name (structure, "application/x-rtp-stream");
214 else if (gst_structure_has_name (structure, "application/x-rtcp"))
215 gst_structure_set_name (structure, "application/x-rtcp-stream");
216 else if (gst_structure_has_name (structure, "application/x-srtp"))
217 gst_structure_set_name (structure, "application/x-srtp-stream");
218 else
219 gst_structure_set_name (structure, "application/x-srtcp-stream");
220
221 ret = gst_pad_set_caps (self->srcpad, othercaps);
222 gst_caps_unref (othercaps);
223
224 return ret;
225 }
226
227 static gboolean
gst_rtp_stream_pay_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)228 gst_rtp_stream_pay_sink_event (GstPad * pad, GstObject * parent,
229 GstEvent * event)
230 {
231 GstRtpStreamPay *self = GST_RTP_STREAM_PAY (parent);
232 gboolean ret;
233
234 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
235
236 switch (GST_EVENT_TYPE (event)) {
237 case GST_EVENT_CAPS:
238 {
239 GstCaps *caps;
240
241 gst_event_parse_caps (event, &caps);
242 ret = gst_rtp_stream_pay_sink_set_caps (self, caps);
243 gst_event_unref (event);
244 break;
245 }
246 default:
247 ret = gst_pad_event_default (pad, parent, event);
248 break;
249 }
250
251 return ret;
252 }
253
254 static GstFlowReturn
gst_rtp_stream_pay_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * inbuf)255 gst_rtp_stream_pay_sink_chain (GstPad * pad, GstObject * parent,
256 GstBuffer * inbuf)
257 {
258 GstRtpStreamPay *self = GST_RTP_STREAM_PAY (parent);
259 GstBuffer *outbuf;
260 gsize size;
261 guint8 size16[2];
262
263 size = gst_buffer_get_size (inbuf);
264 if (size > G_MAXUINT16) {
265 GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
266 ("Only buffers up to %d bytes supported, got %" G_GSIZE_FORMAT,
267 G_MAXUINT16, size));
268 gst_buffer_unref (inbuf);
269 return GST_FLOW_ERROR;
270 }
271
272 outbuf = gst_buffer_new_and_alloc (2);
273
274 GST_WRITE_UINT16_BE (size16, size);
275 gst_buffer_fill (outbuf, 0, size16, 2);
276
277 gst_buffer_copy_into (outbuf, inbuf, GST_BUFFER_COPY_ALL, 0, -1);
278
279 gst_buffer_unref (inbuf);
280
281 return gst_pad_push (self->srcpad, outbuf);
282 }
283