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>
28 #include <gst/audio/multichannel.h>
30 #include "gstrtpg722depay.h"
31 #include "gstrtpchannels.h"
33 GST_DEBUG_CATEGORY_STATIC (rtpg722depay_debug);
34 #define GST_CAT_DEFAULT (rtpg722depay_debug)
36 static GstStaticPadTemplate gst_rtp_g722_depay_src_template =
37 GST_STATIC_PAD_TEMPLATE ("src",
40 GST_STATIC_CAPS ("audio/G722, "
41 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
44 static GstStaticPadTemplate gst_rtp_g722_depay_sink_template =
45 GST_STATIC_PAD_TEMPLATE ("sink",
48 GST_STATIC_CAPS ("application/x-rtp, "
49 "media = (string) \"audio\", "
50 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
51 "clock-rate = (int) 8000, "
52 /* "channels = (int) [1, MAX]" */
53 /* "channel-order = (string) ANY" */
54 "encoding-name = (string) \"G722\";"
56 "media = (string) \"audio\", "
57 "payload = (int) " GST_RTP_PAYLOAD_G722_STRING ", "
58 "clock-rate = (int) [ 1, MAX ]"
59 /* "channels = (int) [1, MAX]" */
60 /* "emphasis = (string) ANY" */
61 /* "channel-order = (string) ANY" */
65 #define gst_rtp_g722_depay_parent_class parent_class
66 G_DEFINE_TYPE (GstRtpG722Depay, gst_rtp_g722_depay,
67 GST_TYPE_RTP_BASE_DEPAYLOAD);
69 static gboolean gst_rtp_g722_depay_setcaps (GstRTPBaseDepayload * depayload,
71 static GstBuffer *gst_rtp_g722_depay_process (GstRTPBaseDepayload * depayload,
75 gst_rtp_g722_depay_class_init (GstRtpG722DepayClass * klass)
77 GstElementClass *gstelement_class;
78 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
80 GST_DEBUG_CATEGORY_INIT (rtpg722depay_debug, "rtpg722depay", 0,
81 "G722 RTP Depayloader");
83 gstelement_class = (GstElementClass *) klass;
84 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
86 gst_element_class_add_pad_template (gstelement_class,
87 gst_static_pad_template_get (&gst_rtp_g722_depay_src_template));
88 gst_element_class_add_pad_template (gstelement_class,
89 gst_static_pad_template_get (&gst_rtp_g722_depay_sink_template));
91 gst_element_class_set_details_simple (gstelement_class,
92 "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
93 "Extracts G722 audio from RTP packets",
94 "Wim Taymans <wim.taymans@gmail.com>");
96 gstrtpbasedepayload_class->set_caps = gst_rtp_g722_depay_setcaps;
97 gstrtpbasedepayload_class->process = gst_rtp_g722_depay_process;
101 gst_rtp_g722_depay_init (GstRtpG722Depay * rtpg722depay)
106 gst_rtp_g722_depay_parse_int (GstStructure * structure, const gchar * field,
112 if ((str = gst_structure_get_string (structure, field)))
115 if (gst_structure_get_int (structure, field, &res))
122 gst_rtp_g722_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
124 GstStructure *structure;
125 GstRtpG722Depay *rtpg722depay;
126 gint clock_rate, payload, samplerate;
130 const gchar *channel_order;
131 const GstRTPChannelOrder *order;
133 rtpg722depay = GST_RTP_G722_DEPAY (depayload);
135 structure = gst_caps_get_structure (caps, 0);
138 gst_structure_get_int (structure, "payload", &payload);
140 case GST_RTP_PAYLOAD_G722:
146 /* no fixed mapping, we need clock-rate */
153 /* caps can overwrite defaults */
155 gst_rtp_g722_depay_parse_int (structure, "clock-rate", clock_rate);
159 if (clock_rate == 8000)
163 samplerate = clock_rate;
166 gst_rtp_g722_depay_parse_int (structure, "encoding-params", channels);
168 channels = gst_rtp_g722_depay_parse_int (structure, "channels", channels);
170 /* channels defaults to 1 otherwise */
175 depayload->clock_rate = clock_rate;
176 rtpg722depay->rate = samplerate;
177 rtpg722depay->channels = channels;
179 srccaps = gst_caps_new_simple ("audio/G722",
180 "rate", G_TYPE_INT, samplerate, "channels", G_TYPE_INT, channels, NULL);
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);
187 gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0),
190 GstAudioChannelPosition *pos;
192 GST_ELEMENT_WARNING (rtpg722depay, STREAM, DECODE,
193 (NULL), ("Unknown channel order '%s' for %d channels",
194 GST_STR_NULL (channel_order), channels));
195 /* create default NONE layout */
196 pos = gst_rtp_channels_create_default (channels);
197 gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0), pos);
201 res = gst_pad_set_caps (depayload->srcpad, srccaps);
202 gst_caps_unref (srccaps);
209 GST_ERROR_OBJECT (depayload, "no clock-rate specified");
215 gst_rtp_g722_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
217 GstRtpG722Depay *rtpg722depay;
221 GstRTPBuffer rtp = { NULL };
223 rtpg722depay = GST_RTP_G722_DEPAY (depayload);
225 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
227 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
229 if (payload_len <= 0)
232 GST_DEBUG_OBJECT (rtpg722depay, "got payload of %d bytes", payload_len);
234 outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
235 marker = gst_rtp_buffer_get_marker (&rtp);
236 gst_rtp_buffer_unmap (&rtp);
238 if (marker && outbuf) {
239 /* mark talk spurt with DISCONT */
240 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
248 GST_ELEMENT_WARNING (rtpg722depay, STREAM, DECODE,
249 ("Empty Payload."), (NULL));
250 gst_rtp_buffer_unmap (&rtp);
256 gst_rtp_g722_depay_plugin_init (GstPlugin * plugin)
258 return gst_element_register (plugin, "rtpg722depay",
259 GST_RANK_SECONDARY, GST_TYPE_RTP_G722_DEPAY);