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