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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
27 #include <gst/audio/audio.h>
29 #include "gstrtpL16depay.h"
30 #include "gstrtpchannels.h"
32 GST_DEBUG_CATEGORY_STATIC (rtpL16depay_debug);
33 #define GST_CAT_DEFAULT (rtpL16depay_debug)
35 static GstStaticPadTemplate gst_rtp_L16_depay_src_template =
36 GST_STATIC_PAD_TEMPLATE ("src",
39 GST_STATIC_CAPS ("audio/x-raw, "
40 "format = (string) S16BE, "
41 "layout = (string) interleaved, "
42 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
45 static GstStaticPadTemplate gst_rtp_L16_depay_sink_template =
46 GST_STATIC_PAD_TEMPLATE ("sink",
49 GST_STATIC_CAPS ("application/x-rtp, "
50 "media = (string) \"audio\", "
51 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
52 "clock-rate = (int) [ 1, MAX ], "
53 /* "channels = (int) [1, MAX]" */
54 /* "emphasis = (string) ANY" */
55 /* "channel-order = (string) ANY" */
56 "encoding-name = (string) \"L16\";"
58 "media = (string) \"audio\", "
59 "payload = (int) { " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
60 GST_RTP_PAYLOAD_L16_MONO_STRING " }," "clock-rate = (int) [ 1, MAX ]"
61 /* "channels = (int) [1, MAX]" */
62 /* "emphasis = (string) ANY" */
63 /* "channel-order = (string) ANY" */
67 #define gst_rtp_L16_depay_parent_class parent_class
68 G_DEFINE_TYPE (GstRtpL16Depay, gst_rtp_L16_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
70 static gboolean gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload,
72 static GstBuffer *gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload,
76 gst_rtp_L16_depay_class_init (GstRtpL16DepayClass * klass)
78 GstElementClass *gstelement_class;
79 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
81 gstelement_class = (GstElementClass *) klass;
82 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
84 gstrtpbasedepayload_class->set_caps = gst_rtp_L16_depay_setcaps;
85 gstrtpbasedepayload_class->process = gst_rtp_L16_depay_process;
87 gst_element_class_add_pad_template (gstelement_class,
88 gst_static_pad_template_get (&gst_rtp_L16_depay_src_template));
89 gst_element_class_add_pad_template (gstelement_class,
90 gst_static_pad_template_get (&gst_rtp_L16_depay_sink_template));
92 gst_element_class_set_static_metadata (gstelement_class,
93 "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
94 "Extracts raw audio from RTP packets",
95 "Zeeshan Ali <zak147@yahoo.com>," "Wim Taymans <wim.taymans@gmail.com>");
97 GST_DEBUG_CATEGORY_INIT (rtpL16depay_debug, "rtpL16depay", 0,
98 "Raw Audio RTP Depayloader");
102 gst_rtp_L16_depay_init (GstRtpL16Depay * rtpL16depay)
107 gst_rtp_L16_depay_parse_int (GstStructure * structure, const gchar * field,
113 if ((str = gst_structure_get_string (structure, field)))
116 if (gst_structure_get_int (structure, field, &res))
123 gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
125 GstStructure *structure;
126 GstRtpL16Depay *rtpL16depay;
127 gint clock_rate, payload;
131 const gchar *channel_order;
132 const GstRTPChannelOrder *order;
135 rtpL16depay = GST_RTP_L16_DEPAY (depayload);
137 structure = gst_caps_get_structure (caps, 0);
140 gst_structure_get_int (structure, "payload", &payload);
142 case GST_RTP_PAYLOAD_L16_STEREO:
146 case GST_RTP_PAYLOAD_L16_MONO:
151 /* no fixed mapping, we need clock-rate */
157 /* caps can overwrite defaults */
159 gst_rtp_L16_depay_parse_int (structure, "clock-rate", clock_rate);
164 gst_rtp_L16_depay_parse_int (structure, "encoding-params", channels);
166 channels = gst_rtp_L16_depay_parse_int (structure, "channels", channels);
168 /* channels defaults to 1 otherwise */
173 depayload->clock_rate = clock_rate;
175 info = &rtpL16depay->info;
176 gst_audio_info_init (info);
177 info->finfo = gst_audio_format_get_info (GST_AUDIO_FORMAT_S16BE);
178 info->rate = clock_rate;
179 info->channels = channels;
180 info->bpf = (info->finfo->width / 8) * channels;
182 /* add channel positions */
183 channel_order = gst_structure_get_string (structure, "channel-order");
185 order = gst_rtp_channels_get_by_order (channels, channel_order);
186 rtpL16depay->order = order;
188 memcpy (info->position, order->pos,
189 sizeof (GstAudioChannelPosition) * channels);
190 gst_audio_channel_positions_to_valid_order (info->position, info->channels);
192 GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
193 (NULL), ("Unknown channel order '%s' for %d channels",
194 GST_STR_NULL (channel_order), channels));
195 /* create default NONE layout */
196 gst_rtp_channels_create_default (channels, info->position);
199 srccaps = gst_audio_info_to_caps (info);
200 res = gst_pad_set_caps (depayload->srcpad, srccaps);
201 gst_caps_unref (srccaps);
208 GST_ERROR_OBJECT (depayload, "no clock-rate specified");
214 gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
216 GstRtpL16Depay *rtpL16depay;
220 GstRTPBuffer rtp = { NULL };
222 rtpL16depay = GST_RTP_L16_DEPAY (depayload);
224 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
225 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
227 if (payload_len <= 0)
230 GST_DEBUG_OBJECT (rtpL16depay, "got payload of %d bytes", payload_len);
232 outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
233 marker = gst_rtp_buffer_get_marker (&rtp);
236 /* mark talk spurt with DISCONT */
237 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
240 outbuf = gst_buffer_make_writable (outbuf);
241 if (rtpL16depay->order &&
242 !gst_audio_buffer_reorder_channels (outbuf,
243 rtpL16depay->info.finfo->format, rtpL16depay->info.channels,
244 rtpL16depay->info.position, rtpL16depay->order->pos)) {
248 gst_rtp_buffer_unmap (&rtp);
255 GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
256 ("Empty Payload."), (NULL));
257 gst_rtp_buffer_unmap (&rtp);
262 GST_ELEMENT_ERROR (rtpL16depay, STREAM, DECODE,
263 ("Channel reordering failed."), (NULL));
264 gst_rtp_buffer_unmap (&rtp);
270 gst_rtp_L16_depay_plugin_init (GstPlugin * plugin)
272 return gst_element_register (plugin, "rtpL16depay",
273 GST_RANK_SECONDARY, GST_TYPE_RTP_L16_DEPAY);