webrtc/nice: Support domain name as connection-address of ICE candidate
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtpstreamdepay.c
1 /* GStreamer
2  * Copyright (C) 2013 Sebastian Dröge <sebastian@centricular.com>
3  *
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.
8  *
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.
13  *
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:element-rtpstreamdepay
22  * @title: rtpstreamdepay
23  *
24  * Implements stream depayloading of RTP and RTCP packets for connection-oriented
25  * transport protocols according to RFC4571.
26  *
27  * ## Example launch line
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  *
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include "gstrtpelements.h"
40 #include "gstrtpstreamdepay.h"
41
42 GST_DEBUG_CATEGORY (gst_rtp_stream_depay_debug);
43 #define GST_CAT_DEFAULT gst_rtp_stream_depay_debug
44
45 static GstStaticPadTemplate src_template =
46     GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
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 sink_template =
53     GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK,
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_depay_parent_class
60 G_DEFINE_TYPE (GstRtpStreamDepay, gst_rtp_stream_depay, GST_TYPE_BASE_PARSE);
61 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpstreamdepay, "rtpstreamdepay",
62     GST_RANK_NONE, GST_TYPE_RTP_STREAM_DEPAY, rtp_element_init (plugin));
63
64 static gboolean gst_rtp_stream_depay_set_sink_caps (GstBaseParse * parse,
65     GstCaps * caps);
66 static GstCaps *gst_rtp_stream_depay_get_sink_caps (GstBaseParse * parse,
67     GstCaps * filter);
68 static GstFlowReturn gst_rtp_stream_depay_handle_frame (GstBaseParse * parse,
69     GstBaseParseFrame * frame, gint * skipsize);
70
71 static gboolean gst_rtp_stream_depay_sink_activate (GstPad * pad,
72     GstObject * parent);
73
74 static void
75 gst_rtp_stream_depay_class_init (GstRtpStreamDepayClass * klass)
76 {
77   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
78   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
79
80   GST_DEBUG_CATEGORY_INIT (gst_rtp_stream_depay_debug, "rtpstreamdepay", 0,
81       "RTP stream depayloader");
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   gst_element_class_set_static_metadata (gstelement_class,
87       "RTP Stream Depayloading", "Codec/Depayloader/Network",
88       "Depayloads RTP/RTCP packets for streaming protocols according to RFC4571",
89       "Sebastian Dröge <sebastian@centricular.com>");
90
91   parse_class->set_sink_caps =
92       GST_DEBUG_FUNCPTR (gst_rtp_stream_depay_set_sink_caps);
93   parse_class->get_sink_caps =
94       GST_DEBUG_FUNCPTR (gst_rtp_stream_depay_get_sink_caps);
95   parse_class->handle_frame =
96       GST_DEBUG_FUNCPTR (gst_rtp_stream_depay_handle_frame);
97 }
98
99 static void
100 gst_rtp_stream_depay_init (GstRtpStreamDepay * self)
101 {
102   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (self), 2);
103
104   /* Force activation in push mode. We need to get a caps event from upstream
105    * to know the full RTP caps. */
106   gst_pad_set_activate_function (GST_BASE_PARSE_SINK_PAD (self),
107       gst_rtp_stream_depay_sink_activate);
108 }
109
110 static gboolean
111 gst_rtp_stream_depay_set_sink_caps (GstBaseParse * parse, GstCaps * caps)
112 {
113   GstCaps *othercaps;
114   GstStructure *structure;
115   gboolean ret;
116
117   othercaps = gst_caps_copy (caps);
118   structure = gst_caps_get_structure (othercaps, 0);
119
120   if (gst_structure_has_name (structure, "application/x-rtp-stream"))
121     gst_structure_set_name (structure, "application/x-rtp");
122   else if (gst_structure_has_name (structure, "application/x-rtcp-stream"))
123     gst_structure_set_name (structure, "application/x-rtcp");
124   else if (gst_structure_has_name (structure, "application/x-srtp-stream"))
125     gst_structure_set_name (structure, "application/x-srtp");
126   else
127     gst_structure_set_name (structure, "application/x-srtcp");
128
129   ret = gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), othercaps);
130   gst_caps_unref (othercaps);
131
132   return ret;
133 }
134
135 static GstCaps *
136 gst_rtp_stream_depay_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
137 {
138   GstCaps *peerfilter = NULL, *peercaps, *templ;
139   GstCaps *res;
140   GstStructure *structure;
141   guint i, n;
142
143   if (filter) {
144     peerfilter = gst_caps_copy (filter);
145     n = gst_caps_get_size (peerfilter);
146     for (i = 0; i < n; i++) {
147       structure = gst_caps_get_structure (peerfilter, i);
148
149       if (gst_structure_has_name (structure, "application/x-rtp-stream"))
150         gst_structure_set_name (structure, "application/x-rtp");
151       else if (gst_structure_has_name (structure, "application/x-rtcp-stream"))
152         gst_structure_set_name (structure, "application/x-rtcp");
153       else if (gst_structure_has_name (structure, "application/x-srtp-stream"))
154         gst_structure_set_name (structure, "application/x-srtp");
155       else
156         gst_structure_set_name (structure, "application/x-srtcp");
157     }
158   }
159
160   templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
161   peercaps =
162       gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), peerfilter);
163
164   if (peercaps) {
165     /* Rename structure names */
166     peercaps = gst_caps_make_writable (peercaps);
167     n = gst_caps_get_size (peercaps);
168     for (i = 0; i < n; i++) {
169       structure = gst_caps_get_structure (peercaps, i);
170
171       if (gst_structure_has_name (structure, "application/x-rtp"))
172         gst_structure_set_name (structure, "application/x-rtp-stream");
173       else if (gst_structure_has_name (structure, "application/x-rtcp"))
174         gst_structure_set_name (structure, "application/x-rtcp-stream");
175       else if (gst_structure_has_name (structure, "application/x-srtp"))
176         gst_structure_set_name (structure, "application/x-srtp-stream");
177       else
178         gst_structure_set_name (structure, "application/x-srtcp-stream");
179     }
180
181     res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
182     gst_caps_unref (peercaps);
183   } else {
184     res = templ;
185   }
186
187   if (filter) {
188     GstCaps *intersection;
189
190     intersection =
191         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
192     gst_caps_unref (res);
193     res = intersection;
194
195     gst_caps_unref (peerfilter);
196   }
197
198   return res;
199 }
200
201 static GstFlowReturn
202 gst_rtp_stream_depay_handle_frame (GstBaseParse * parse,
203     GstBaseParseFrame * frame, gint * skipsize)
204 {
205   gsize buf_size;
206   guint16 size;
207
208   if (gst_buffer_extract (frame->buffer, 0, &size, 2) != 2)
209     return GST_FLOW_ERROR;
210
211   size = GUINT16_FROM_BE (size);
212   buf_size = gst_buffer_get_size (frame->buffer);
213
214   /* Need more data */
215   if (size + 2 > buf_size)
216     return GST_FLOW_OK;
217
218   frame->out_buffer =
219       gst_buffer_copy_region (frame->buffer, GST_BUFFER_COPY_ALL, 2, size);
220
221   return gst_base_parse_finish_frame (parse, frame, size + 2);
222 }
223
224 static gboolean
225 gst_rtp_stream_depay_sink_activate (GstPad * pad, GstObject * parent)
226 {
227   return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
228 }