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