1 /* GStreamer
2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.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-rtpL16depay
22 * @title: rtpL16depay
23 * @see_also: rtpL16pay
24 *
25 * Extract raw audio from RTP packets according to RFC 3551.
26 * For detailed information see: http://www.rfc-editor.org/rfc/rfc3551.txt
27 *
28 * ## Example pipeline
29 * |[
30 * gst-launch-1.0 udpsrc caps='application/x-rtp, media=(string)audio, clock-rate=(int)44100, encoding-name=(string)L16, encoding-params=(string)1, channels=(int)1, payload=(int)96' ! rtpL16depay ! pulsesink
31 * ]| This example pipeline will depayload an RTP raw audio stream. Refer to
32 * the rtpL16pay example to create the RTP stream.
33 *
34 */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <string.h>
41 #include <stdlib.h>
42
43 #include <gst/audio/audio.h>
44
45 #include "gstrtpelements.h"
46 #include "gstrtpL16depay.h"
47 #include "gstrtpchannels.h"
48 #include "gstrtputils.h"
49
50 GST_DEBUG_CATEGORY_STATIC (rtpL16depay_debug);
51 #define GST_CAT_DEFAULT (rtpL16depay_debug)
52
53 static GstStaticPadTemplate gst_rtp_L16_depay_src_template =
54 GST_STATIC_PAD_TEMPLATE ("src",
55 GST_PAD_SRC,
56 GST_PAD_ALWAYS,
57 GST_STATIC_CAPS ("audio/x-raw, "
58 "format = (string) S16BE, "
59 "layout = (string) interleaved, "
60 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
61 );
62
63 static GstStaticPadTemplate gst_rtp_L16_depay_sink_template =
64 GST_STATIC_PAD_TEMPLATE ("sink",
65 GST_PAD_SINK,
66 GST_PAD_ALWAYS,
67 GST_STATIC_CAPS ("application/x-rtp, "
68 "media = (string) \"audio\", " "clock-rate = (int) [ 1, MAX ], "
69 /* "channels = (int) [1, MAX]" */
70 /* "emphasis = (string) ANY" */
71 /* "channel-order = (string) ANY" */
72 "encoding-name = (string) \"L16\";"
73 "application/x-rtp, "
74 "media = (string) \"audio\", "
75 "payload = (int) { " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
76 GST_RTP_PAYLOAD_L16_MONO_STRING " }," "clock-rate = (int) [ 1, MAX ]"
77 /* "channels = (int) [1, MAX]" */
78 /* "emphasis = (string) ANY" */
79 /* "channel-order = (string) ANY" */
80 )
81 );
82
83 #define gst_rtp_L16_depay_parent_class parent_class
84 G_DEFINE_TYPE (GstRtpL16Depay, gst_rtp_L16_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
85 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpL16depay, "rtpL16depay",
86 GST_RANK_SECONDARY, GST_TYPE_RTP_L16_DEPAY, rtp_element_init (plugin));
87
88 static gboolean gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload,
89 GstCaps * caps);
90 static GstBuffer *gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload,
91 GstRTPBuffer * rtp);
92
93 static void
gst_rtp_L16_depay_class_init(GstRtpL16DepayClass * klass)94 gst_rtp_L16_depay_class_init (GstRtpL16DepayClass * klass)
95 {
96 GstElementClass *gstelement_class;
97 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
98
99 gstelement_class = (GstElementClass *) klass;
100 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
101
102 gstrtpbasedepayload_class->set_caps = gst_rtp_L16_depay_setcaps;
103 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_L16_depay_process;
104
105 gst_element_class_add_static_pad_template (gstelement_class,
106 &gst_rtp_L16_depay_src_template);
107 gst_element_class_add_static_pad_template (gstelement_class,
108 &gst_rtp_L16_depay_sink_template);
109
110 gst_element_class_set_static_metadata (gstelement_class,
111 "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
112 "Extracts raw audio from RTP packets",
113 "Zeeshan Ali <zak147@yahoo.com>," "Wim Taymans <wim.taymans@gmail.com>");
114
115 GST_DEBUG_CATEGORY_INIT (rtpL16depay_debug, "rtpL16depay", 0,
116 "Raw Audio RTP Depayloader");
117 }
118
119 static void
gst_rtp_L16_depay_init(GstRtpL16Depay * rtpL16depay)120 gst_rtp_L16_depay_init (GstRtpL16Depay * rtpL16depay)
121 {
122 }
123
124 static gint
gst_rtp_L16_depay_parse_int(GstStructure * structure,const gchar * field,gint def)125 gst_rtp_L16_depay_parse_int (GstStructure * structure, const gchar * field,
126 gint def)
127 {
128 const gchar *str;
129 gint res;
130
131 if ((str = gst_structure_get_string (structure, field)))
132 return atoi (str);
133
134 if (gst_structure_get_int (structure, field, &res))
135 return res;
136
137 return def;
138 }
139
140 static gboolean
gst_rtp_L16_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)141 gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
142 {
143 GstStructure *structure;
144 GstRtpL16Depay *rtpL16depay;
145 gint clock_rate, payload;
146 gint channels;
147 GstCaps *srccaps;
148 gboolean res;
149 const gchar *channel_order;
150 const GstRTPChannelOrder *order;
151 GstAudioInfo *info;
152
153 rtpL16depay = GST_RTP_L16_DEPAY (depayload);
154
155 structure = gst_caps_get_structure (caps, 0);
156
157 payload = 96;
158 gst_structure_get_int (structure, "payload", &payload);
159 switch (payload) {
160 case GST_RTP_PAYLOAD_L16_STEREO:
161 channels = 2;
162 clock_rate = 44100;
163 break;
164 case GST_RTP_PAYLOAD_L16_MONO:
165 channels = 1;
166 clock_rate = 44100;
167 break;
168 default:
169 /* no fixed mapping, we need clock-rate */
170 channels = 0;
171 clock_rate = 0;
172 break;
173 }
174
175 /* caps can overwrite defaults */
176 clock_rate =
177 gst_rtp_L16_depay_parse_int (structure, "clock-rate", clock_rate);
178 if (clock_rate == 0)
179 goto no_clockrate;
180
181 channels =
182 gst_rtp_L16_depay_parse_int (structure, "encoding-params", channels);
183 if (channels == 0) {
184 channels = gst_rtp_L16_depay_parse_int (structure, "channels", channels);
185 if (channels == 0) {
186 /* channels defaults to 1 otherwise */
187 channels = 1;
188 }
189 }
190
191 depayload->clock_rate = clock_rate;
192
193 info = &rtpL16depay->info;
194 gst_audio_info_init (info);
195 info->finfo = gst_audio_format_get_info (GST_AUDIO_FORMAT_S16BE);
196 info->rate = clock_rate;
197 info->channels = channels;
198 info->bpf = (info->finfo->width / 8) * channels;
199
200 /* add channel positions */
201 channel_order = gst_structure_get_string (structure, "channel-order");
202
203 order = gst_rtp_channels_get_by_order (channels, channel_order);
204 rtpL16depay->order = order;
205 if (order) {
206 memcpy (info->position, order->pos,
207 sizeof (GstAudioChannelPosition) * channels);
208 gst_audio_channel_positions_to_valid_order (info->position, info->channels);
209 } else {
210 GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
211 (NULL), ("Unknown channel order '%s' for %d channels",
212 GST_STR_NULL (channel_order), channels));
213 /* create default NONE layout */
214 gst_rtp_channels_create_default (channels, info->position);
215 info->flags |= GST_AUDIO_FLAG_UNPOSITIONED;
216 }
217
218 srccaps = gst_audio_info_to_caps (info);
219 res = gst_pad_set_caps (depayload->srcpad, srccaps);
220 gst_caps_unref (srccaps);
221
222 return res;
223
224 /* ERRORS */
225 no_clockrate:
226 {
227 GST_ERROR_OBJECT (depayload, "no clock-rate specified");
228 return FALSE;
229 }
230 }
231
232 static GstBuffer *
gst_rtp_L16_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)233 gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
234 {
235 GstRtpL16Depay *rtpL16depay;
236 GstBuffer *outbuf;
237 gint payload_len;
238 gboolean marker;
239 GstAudioInfo *info;
240
241 rtpL16depay = GST_RTP_L16_DEPAY (depayload);
242
243 payload_len = gst_rtp_buffer_get_payload_len (rtp);
244
245 if (payload_len <= 0)
246 goto empty_packet;
247
248 GST_DEBUG_OBJECT (rtpL16depay, "got payload of %d bytes", payload_len);
249
250 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
251 marker = gst_rtp_buffer_get_marker (rtp);
252
253 if (marker) {
254 /* mark talk spurt with RESYNC */
255 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
256 }
257
258 outbuf = gst_buffer_make_writable (outbuf);
259 info = &rtpL16depay->info;
260
261 if (payload_len % info->bpf != 0)
262 goto wrong_payload_size;
263
264 if (rtpL16depay->order &&
265 !gst_audio_buffer_reorder_channels (outbuf,
266 info->finfo->format, info->channels,
267 info->position, rtpL16depay->order->pos)) {
268 goto reorder_failed;
269 }
270
271 gst_rtp_drop_non_audio_meta (rtpL16depay, outbuf);
272
273 return outbuf;
274
275 /* ERRORS */
276 empty_packet:
277 {
278 GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
279 ("Empty Payload."), (NULL));
280 return NULL;
281 }
282 wrong_payload_size:
283 {
284 GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
285 ("Wrong Payload Size."), (NULL));
286 gst_buffer_unref (outbuf);
287 return NULL;
288 }
289 reorder_failed:
290 {
291 GST_ELEMENT_ERROR (rtpL16depay, STREAM, DECODE,
292 ("Channel reordering failed."), (NULL));
293 gst_buffer_unref (outbuf);
294 return NULL;
295 }
296 }
297