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"
47 #include "gstrtputils.h"
49 GST_DEBUG_CATEGORY_STATIC (rtpL16depay_debug);
50 #define GST_CAT_DEFAULT (rtpL16depay_debug)
52 static GstStaticPadTemplate gst_rtp_L16_depay_src_template =
53 GST_STATIC_PAD_TEMPLATE ("src",
56 GST_STATIC_CAPS ("audio/x-raw, "
57 "format = (string) S16BE, "
58 "layout = (string) interleaved, "
59 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
62 static GstStaticPadTemplate gst_rtp_L16_depay_sink_template =
63 GST_STATIC_PAD_TEMPLATE ("sink",
66 GST_STATIC_CAPS ("application/x-rtp, "
67 "media = (string) \"audio\", " "clock-rate = (int) [ 1, MAX ], "
68 /* "channels = (int) [1, MAX]" */
69 /* "emphasis = (string) ANY" */
70 /* "channel-order = (string) ANY" */
71 "encoding-name = (string) \"L16\";"
73 "media = (string) \"audio\", "
74 "payload = (int) { " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
75 GST_RTP_PAYLOAD_L16_MONO_STRING " }," "clock-rate = (int) [ 1, MAX ]"
76 /* "channels = (int) [1, MAX]" */
77 /* "emphasis = (string) ANY" */
78 /* "channel-order = (string) ANY" */
82 #define gst_rtp_L16_depay_parent_class parent_class
83 G_DEFINE_TYPE (GstRtpL16Depay, gst_rtp_L16_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
85 static gboolean gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload,
87 static GstBuffer *gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload,
91 gst_rtp_L16_depay_class_init (GstRtpL16DepayClass * klass)
93 GstElementClass *gstelement_class;
94 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
96 gstelement_class = (GstElementClass *) klass;
97 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
99 gstrtpbasedepayload_class->set_caps = gst_rtp_L16_depay_setcaps;
100 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_L16_depay_process;
102 gst_element_class_add_static_pad_template (gstelement_class,
103 &gst_rtp_L16_depay_src_template);
104 gst_element_class_add_static_pad_template (gstelement_class,
105 &gst_rtp_L16_depay_sink_template);
107 gst_element_class_set_static_metadata (gstelement_class,
108 "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
109 "Extracts raw audio from RTP packets",
110 "Zeeshan Ali <zak147@yahoo.com>," "Wim Taymans <wim.taymans@gmail.com>");
112 GST_DEBUG_CATEGORY_INIT (rtpL16depay_debug, "rtpL16depay", 0,
113 "Raw Audio RTP Depayloader");
117 gst_rtp_L16_depay_init (GstRtpL16Depay * rtpL16depay)
122 gst_rtp_L16_depay_parse_int (GstStructure * structure, const gchar * field,
128 if ((str = gst_structure_get_string (structure, field)))
131 if (gst_structure_get_int (structure, field, &res))
138 gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
140 GstStructure *structure;
141 GstRtpL16Depay *rtpL16depay;
142 gint clock_rate, payload;
146 const gchar *channel_order;
147 const GstRTPChannelOrder *order;
150 rtpL16depay = GST_RTP_L16_DEPAY (depayload);
152 structure = gst_caps_get_structure (caps, 0);
155 gst_structure_get_int (structure, "payload", &payload);
157 case GST_RTP_PAYLOAD_L16_STEREO:
161 case GST_RTP_PAYLOAD_L16_MONO:
166 /* no fixed mapping, we need clock-rate */
172 /* caps can overwrite defaults */
174 gst_rtp_L16_depay_parse_int (structure, "clock-rate", clock_rate);
179 gst_rtp_L16_depay_parse_int (structure, "encoding-params", channels);
181 channels = gst_rtp_L16_depay_parse_int (structure, "channels", channels);
183 /* channels defaults to 1 otherwise */
188 depayload->clock_rate = clock_rate;
190 info = &rtpL16depay->info;
191 gst_audio_info_init (info);
192 info->finfo = gst_audio_format_get_info (GST_AUDIO_FORMAT_S16BE);
193 info->rate = clock_rate;
194 info->channels = channels;
195 info->bpf = (info->finfo->width / 8) * channels;
197 /* add channel positions */
198 channel_order = gst_structure_get_string (structure, "channel-order");
200 order = gst_rtp_channels_get_by_order (channels, channel_order);
201 rtpL16depay->order = order;
203 memcpy (info->position, order->pos,
204 sizeof (GstAudioChannelPosition) * channels);
205 gst_audio_channel_positions_to_valid_order (info->position, info->channels);
207 GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
208 (NULL), ("Unknown channel order '%s' for %d channels",
209 GST_STR_NULL (channel_order), channels));
210 /* create default NONE layout */
211 gst_rtp_channels_create_default (channels, info->position);
214 srccaps = gst_audio_info_to_caps (info);
215 res = gst_pad_set_caps (depayload->srcpad, srccaps);
216 gst_caps_unref (srccaps);
223 GST_ERROR_OBJECT (depayload, "no clock-rate specified");
229 gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
231 GstRtpL16Depay *rtpL16depay;
237 rtpL16depay = GST_RTP_L16_DEPAY (depayload);
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 info = &rtpL16depay->info;
257 if (payload_len % info->bpf != 0)
258 goto wrong_payload_size;
260 if (rtpL16depay->order &&
261 !gst_audio_buffer_reorder_channels (outbuf,
262 info->finfo->format, info->channels,
263 info->position, rtpL16depay->order->pos)) {
267 gst_rtp_drop_non_audio_meta (rtpL16depay, outbuf);
274 GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
275 ("Empty Payload."), (NULL));
280 GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
281 ("Wrong Payload Size."), (NULL));
286 GST_ELEMENT_ERROR (rtpL16depay, STREAM, DECODE,
287 ("Channel reordering failed."), (NULL));
293 gst_rtp_L16_depay_plugin_init (GstPlugin * plugin)
295 return gst_element_register (plugin, "rtpL16depay",
296 GST_RANK_SECONDARY, GST_TYPE_RTP_L16_DEPAY);