2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
21 * SECTION:element-rtpL16depay
22 * @see_also: rtpL16pay
24 * Extract raw audio from RTP packets according to RFC 3551.
25 * For detailed information see: http://www.rfc-editor.org/rfc/rfc3551.txt
28 * <title>Example pipeline</title>
30 * gst-launch 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.
35 * Last reviewed on 2013-04-25 (1.1.0)
45 #include <gst/audio/audio.h>
47 #include "gstrtpL16depay.h"
48 #include "gstrtpchannels.h"
50 GST_DEBUG_CATEGORY_STATIC (rtpL16depay_debug);
51 #define GST_CAT_DEFAULT (rtpL16depay_debug)
53 static GstStaticPadTemplate gst_rtp_L16_depay_src_template =
54 GST_STATIC_PAD_TEMPLATE ("src",
57 GST_STATIC_CAPS ("audio/x-raw, "
58 "format = (string) S16BE, "
59 "layout = (string) interleaved, "
60 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
63 static GstStaticPadTemplate gst_rtp_L16_depay_sink_template =
64 GST_STATIC_PAD_TEMPLATE ("sink",
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\";"
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" */
83 #define gst_rtp_L16_depay_parent_class parent_class
84 G_DEFINE_TYPE (GstRtpL16Depay, gst_rtp_L16_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
86 static gboolean gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload,
88 static GstBuffer *gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload,
92 gst_rtp_L16_depay_class_init (GstRtpL16DepayClass * klass)
94 GstElementClass *gstelement_class;
95 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
97 gstelement_class = (GstElementClass *) klass;
98 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
100 gstrtpbasedepayload_class->set_caps = gst_rtp_L16_depay_setcaps;
101 gstrtpbasedepayload_class->process = gst_rtp_L16_depay_process;
103 gst_element_class_add_pad_template (gstelement_class,
104 gst_static_pad_template_get (&gst_rtp_L16_depay_src_template));
105 gst_element_class_add_pad_template (gstelement_class,
106 gst_static_pad_template_get (&gst_rtp_L16_depay_sink_template));
108 gst_element_class_set_static_metadata (gstelement_class,
109 "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
110 "Extracts raw audio from RTP packets",
111 "Zeeshan Ali <zak147@yahoo.com>," "Wim Taymans <wim.taymans@gmail.com>");
113 GST_DEBUG_CATEGORY_INIT (rtpL16depay_debug, "rtpL16depay", 0,
114 "Raw Audio RTP Depayloader");
118 gst_rtp_L16_depay_init (GstRtpL16Depay * rtpL16depay)
123 gst_rtp_L16_depay_parse_int (GstStructure * structure, const gchar * field,
129 if ((str = gst_structure_get_string (structure, field)))
132 if (gst_structure_get_int (structure, field, &res))
139 gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
141 GstStructure *structure;
142 GstRtpL16Depay *rtpL16depay;
143 gint clock_rate, payload;
147 const gchar *channel_order;
148 const GstRTPChannelOrder *order;
151 rtpL16depay = GST_RTP_L16_DEPAY (depayload);
153 structure = gst_caps_get_structure (caps, 0);
156 gst_structure_get_int (structure, "payload", &payload);
158 case GST_RTP_PAYLOAD_L16_STEREO:
162 case GST_RTP_PAYLOAD_L16_MONO:
167 /* no fixed mapping, we need clock-rate */
173 /* caps can overwrite defaults */
175 gst_rtp_L16_depay_parse_int (structure, "clock-rate", clock_rate);
180 gst_rtp_L16_depay_parse_int (structure, "encoding-params", channels);
182 channels = gst_rtp_L16_depay_parse_int (structure, "channels", channels);
184 /* channels defaults to 1 otherwise */
189 depayload->clock_rate = clock_rate;
191 info = &rtpL16depay->info;
192 gst_audio_info_init (info);
193 info->finfo = gst_audio_format_get_info (GST_AUDIO_FORMAT_S16BE);
194 info->rate = clock_rate;
195 info->channels = channels;
196 info->bpf = (info->finfo->width / 8) * channels;
198 /* add channel positions */
199 channel_order = gst_structure_get_string (structure, "channel-order");
201 order = gst_rtp_channels_get_by_order (channels, channel_order);
202 rtpL16depay->order = order;
204 memcpy (info->position, order->pos,
205 sizeof (GstAudioChannelPosition) * channels);
206 gst_audio_channel_positions_to_valid_order (info->position, info->channels);
208 GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
209 (NULL), ("Unknown channel order '%s' for %d channels",
210 GST_STR_NULL (channel_order), channels));
211 /* create default NONE layout */
212 gst_rtp_channels_create_default (channels, info->position);
215 srccaps = gst_audio_info_to_caps (info);
216 res = gst_pad_set_caps (depayload->srcpad, srccaps);
217 gst_caps_unref (srccaps);
224 GST_ERROR_OBJECT (depayload, "no clock-rate specified");
230 gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
232 GstRtpL16Depay *rtpL16depay;
236 GstRTPBuffer rtp = { NULL };
238 rtpL16depay = GST_RTP_L16_DEPAY (depayload);
240 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
241 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
243 if (payload_len <= 0)
246 GST_DEBUG_OBJECT (rtpL16depay, "got payload of %d bytes", payload_len);
248 outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
249 marker = gst_rtp_buffer_get_marker (&rtp);
252 /* mark talk spurt with RESYNC */
253 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
256 outbuf = gst_buffer_make_writable (outbuf);
257 if (rtpL16depay->order &&
258 !gst_audio_buffer_reorder_channels (outbuf,
259 rtpL16depay->info.finfo->format, rtpL16depay->info.channels,
260 rtpL16depay->info.position, rtpL16depay->order->pos)) {
264 gst_rtp_buffer_unmap (&rtp);
271 GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
272 ("Empty Payload."), (NULL));
273 gst_rtp_buffer_unmap (&rtp);
278 GST_ELEMENT_ERROR (rtpL16depay, STREAM, DECODE,
279 ("Channel reordering failed."), (NULL));
280 gst_rtp_buffer_unmap (&rtp);
286 gst_rtp_L16_depay_plugin_init (GstPlugin * plugin)
288 return gst_element_register (plugin, "rtpL16depay",
289 GST_RANK_SECONDARY, GST_TYPE_RTP_L16_DEPAY);