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-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.
43 #include <gst/audio/audio.h>
45 #include "gstrtpL16depay.h"
46 #include "gstrtpchannels.h"
48 GST_DEBUG_CATEGORY_STATIC (rtpL16depay_debug);
49 #define GST_CAT_DEFAULT (rtpL16depay_debug)
51 static GstStaticPadTemplate gst_rtp_L16_depay_src_template =
52 GST_STATIC_PAD_TEMPLATE ("src",
55 GST_STATIC_CAPS ("audio/x-raw, "
56 "format = (string) S16BE, "
57 "layout = (string) interleaved, "
58 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
61 static GstStaticPadTemplate gst_rtp_L16_depay_sink_template =
62 GST_STATIC_PAD_TEMPLATE ("sink",
65 GST_STATIC_CAPS ("application/x-rtp, "
66 "media = (string) \"audio\", " "clock-rate = (int) [ 1, MAX ], "
67 /* "channels = (int) [1, MAX]" */
68 /* "emphasis = (string) ANY" */
69 /* "channel-order = (string) ANY" */
70 "encoding-name = (string) \"L16\";"
72 "media = (string) \"audio\", "
73 "payload = (int) { " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
74 GST_RTP_PAYLOAD_L16_MONO_STRING " }," "clock-rate = (int) [ 1, MAX ]"
75 /* "channels = (int) [1, MAX]" */
76 /* "emphasis = (string) ANY" */
77 /* "channel-order = (string) ANY" */
81 #define gst_rtp_L16_depay_parent_class parent_class
82 G_DEFINE_TYPE (GstRtpL16Depay, gst_rtp_L16_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
84 static gboolean gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload,
86 static GstBuffer *gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload,
90 gst_rtp_L16_depay_class_init (GstRtpL16DepayClass * klass)
92 GstElementClass *gstelement_class;
93 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
95 gstelement_class = (GstElementClass *) klass;
96 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
98 gstrtpbasedepayload_class->set_caps = gst_rtp_L16_depay_setcaps;
99 gstrtpbasedepayload_class->process = gst_rtp_L16_depay_process;
101 gst_element_class_add_pad_template (gstelement_class,
102 gst_static_pad_template_get (&gst_rtp_L16_depay_src_template));
103 gst_element_class_add_pad_template (gstelement_class,
104 gst_static_pad_template_get (&gst_rtp_L16_depay_sink_template));
106 gst_element_class_set_static_metadata (gstelement_class,
107 "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
108 "Extracts raw audio from RTP packets",
109 "Zeeshan Ali <zak147@yahoo.com>," "Wim Taymans <wim.taymans@gmail.com>");
111 GST_DEBUG_CATEGORY_INIT (rtpL16depay_debug, "rtpL16depay", 0,
112 "Raw Audio RTP Depayloader");
116 gst_rtp_L16_depay_init (GstRtpL16Depay * rtpL16depay)
121 gst_rtp_L16_depay_parse_int (GstStructure * structure, const gchar * field,
127 if ((str = gst_structure_get_string (structure, field)))
130 if (gst_structure_get_int (structure, field, &res))
137 gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
139 GstStructure *structure;
140 GstRtpL16Depay *rtpL16depay;
141 gint clock_rate, payload;
145 const gchar *channel_order;
146 const GstRTPChannelOrder *order;
149 rtpL16depay = GST_RTP_L16_DEPAY (depayload);
151 structure = gst_caps_get_structure (caps, 0);
154 gst_structure_get_int (structure, "payload", &payload);
156 case GST_RTP_PAYLOAD_L16_STEREO:
160 case GST_RTP_PAYLOAD_L16_MONO:
165 /* no fixed mapping, we need clock-rate */
171 /* caps can overwrite defaults */
173 gst_rtp_L16_depay_parse_int (structure, "clock-rate", clock_rate);
178 gst_rtp_L16_depay_parse_int (structure, "encoding-params", channels);
180 channels = gst_rtp_L16_depay_parse_int (structure, "channels", channels);
182 /* channels defaults to 1 otherwise */
187 depayload->clock_rate = clock_rate;
189 info = &rtpL16depay->info;
190 gst_audio_info_init (info);
191 info->finfo = gst_audio_format_get_info (GST_AUDIO_FORMAT_S16BE);
192 info->rate = clock_rate;
193 info->channels = channels;
194 info->bpf = (info->finfo->width / 8) * channels;
196 /* add channel positions */
197 channel_order = gst_structure_get_string (structure, "channel-order");
199 order = gst_rtp_channels_get_by_order (channels, channel_order);
200 rtpL16depay->order = order;
202 memcpy (info->position, order->pos,
203 sizeof (GstAudioChannelPosition) * channels);
204 gst_audio_channel_positions_to_valid_order (info->position, info->channels);
206 GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
207 (NULL), ("Unknown channel order '%s' for %d channels",
208 GST_STR_NULL (channel_order), channels));
209 /* create default NONE layout */
210 gst_rtp_channels_create_default (channels, info->position);
213 srccaps = gst_audio_info_to_caps (info);
214 res = gst_pad_set_caps (depayload->srcpad, srccaps);
215 gst_caps_unref (srccaps);
222 GST_ERROR_OBJECT (depayload, "no clock-rate specified");
228 gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
230 GstRtpL16Depay *rtpL16depay;
234 GstRTPBuffer rtp = { NULL };
236 rtpL16depay = GST_RTP_L16_DEPAY (depayload);
238 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
239 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
241 if (payload_len <= 0)
244 GST_DEBUG_OBJECT (rtpL16depay, "got payload of %d bytes", payload_len);
246 outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
247 marker = gst_rtp_buffer_get_marker (&rtp);
250 /* mark talk spurt with RESYNC */
251 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
254 outbuf = gst_buffer_make_writable (outbuf);
255 if (rtpL16depay->order &&
256 !gst_audio_buffer_reorder_channels (outbuf,
257 rtpL16depay->info.finfo->format, rtpL16depay->info.channels,
258 rtpL16depay->info.position, rtpL16depay->order->pos)) {
262 gst_rtp_buffer_unmap (&rtp);
269 GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
270 ("Empty Payload."), (NULL));
271 gst_rtp_buffer_unmap (&rtp);
276 GST_ELEMENT_ERROR (rtpL16depay, STREAM, DECODE,
277 ("Channel reordering failed."), (NULL));
278 gst_rtp_buffer_unmap (&rtp);
284 gst_rtp_L16_depay_plugin_init (GstPlugin * plugin)
286 return gst_element_register (plugin, "rtpL16depay",
287 GST_RANK_SECONDARY, GST_TYPE_RTP_L16_DEPAY);