rtpvrawpay: Add missing break
[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 void
68 gst_rtp_stream_depay_class_init (GstRtpStreamDepayClass * klass)
69 {
70   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
71   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
72
73   GST_DEBUG_CATEGORY_INIT (gst_rtp_stream_depay_debug, "rtpstreamdepay", 0,
74       "RTP stream depayloader");
75
76   gst_element_class_add_pad_template (gstelement_class,
77       gst_static_pad_template_get (&src_template));
78   gst_element_class_add_pad_template (gstelement_class,
79       gst_static_pad_template_get (&sink_template));
80
81   gst_element_class_set_static_metadata (gstelement_class,
82       "RTP Stream Depayloading", "Codec/Depayloader/Network",
83       "Depayloads RTP/RTCP packets for streaming protocols according to RFC4571",
84       "Sebastian Dröge <sebastian@centricular.com>");
85
86   parse_class->set_sink_caps =
87       GST_DEBUG_FUNCPTR (gst_rtp_stream_depay_set_sink_caps);
88   parse_class->get_sink_caps =
89       GST_DEBUG_FUNCPTR (gst_rtp_stream_depay_get_sink_caps);
90   parse_class->handle_frame =
91       GST_DEBUG_FUNCPTR (gst_rtp_stream_depay_handle_frame);
92 }
93
94 static void
95 gst_rtp_stream_depay_init (GstRtpStreamDepay * self)
96 {
97   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (self), 2);
98 }
99
100 static gboolean
101 gst_rtp_stream_depay_set_sink_caps (GstBaseParse * parse, GstCaps * caps)
102 {
103   GstCaps *othercaps;
104   GstStructure *structure;
105   gboolean ret;
106
107   othercaps = gst_caps_copy (caps);
108   structure = gst_caps_get_structure (othercaps, 0);
109
110   if (gst_structure_has_name (structure, "application/x-rtp-stream"))
111     gst_structure_set_name (structure, "application/x-rtp");
112   else if (gst_structure_has_name (structure, "application/x-rtcp-stream"))
113     gst_structure_set_name (structure, "application/x-rtcp");
114   else if (gst_structure_has_name (structure, "application/x-srtp-stream"))
115     gst_structure_set_name (structure, "application/x-srtp");
116   else
117     gst_structure_set_name (structure, "application/x-srtcp");
118
119   ret = gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), othercaps);
120   gst_caps_unref (othercaps);
121
122   return ret;
123 }
124
125 static GstCaps *
126 gst_rtp_stream_depay_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
127 {
128   GstCaps *peerfilter = NULL, *peercaps, *templ;
129   GstCaps *res;
130   GstStructure *structure;
131   guint i, n;
132
133   if (filter) {
134     peerfilter = gst_caps_copy (filter);
135     n = gst_caps_get_size (peerfilter);
136     for (i = 0; i < n; i++) {
137       structure = gst_caps_get_structure (peerfilter, 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
150   templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
151   peercaps =
152       gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), peerfilter);
153
154   if (peercaps) {
155     /* Rename structure names */
156     peercaps = gst_caps_make_writable (peercaps);
157     n = gst_caps_get_size (peercaps);
158     for (i = 0; i < n; i++) {
159       structure = gst_caps_get_structure (peercaps, i);
160
161       if (gst_structure_has_name (structure, "application/x-rtp"))
162         gst_structure_set_name (structure, "application/x-rtp-stream");
163       else if (gst_structure_has_name (structure, "application/x-rtcp"))
164         gst_structure_set_name (structure, "application/x-rtcp-stream");
165       else if (gst_structure_has_name (structure, "application/x-srtp"))
166         gst_structure_set_name (structure, "application/x-srtp-stream");
167       else
168         gst_structure_set_name (structure, "application/x-srtcp-stream");
169     }
170
171     res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
172     gst_caps_unref (peercaps);
173   } else {
174     res = templ;
175   }
176
177   if (filter) {
178     GstCaps *intersection;
179
180     intersection =
181         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
182     gst_caps_unref (res);
183     res = intersection;
184
185     gst_caps_unref (peerfilter);
186   }
187
188   return res;
189 }
190
191 static GstFlowReturn
192 gst_rtp_stream_depay_handle_frame (GstBaseParse * parse,
193     GstBaseParseFrame * frame, gint * skipsize)
194 {
195   gsize buf_size;
196   guint16 size;
197
198   if (gst_buffer_extract (frame->buffer, 0, &size, 2) != 2)
199     return GST_FLOW_ERROR;
200
201   size = GUINT16_FROM_BE (size);
202   buf_size = gst_buffer_get_size (frame->buffer);
203
204   /* Need more data */
205   if (size + 2 > buf_size)
206     return GST_FLOW_OK;
207
208   frame->out_buffer =
209       gst_buffer_copy_region (frame->buffer, GST_BUFFER_COPY_ALL, 2, size);
210
211   return gst_base_parse_finish_frame (parse, frame, size + 2);
212 }
213
214 gboolean
215 gst_rtp_stream_depay_plugin_init (GstPlugin * plugin)
216 {
217   return gst_element_register (plugin, "rtpstreamdepay",
218       GST_RANK_NONE, GST_TYPE_RTP_STREAM_DEPAY);
219 }