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.
27 #include <gst/audio/audio.h>
29 #include "gstrtpg722depay.h"
30 #include "gstrtpchannels.h"
31 #include "gstrtputils.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\", " "clock-rate = (int) 8000, "
50 /* "channels = (int) [1, MAX]" */
51 /* "channel-order = (string) ANY" */
52 "encoding-name = (string) \"G722\";"
54 "media = (string) \"audio\", "
55 "payload = (int) " GST_RTP_PAYLOAD_G722_STRING ", "
56 "clock-rate = (int) [ 1, MAX ]"
57 /* "channels = (int) [1, MAX]" */
58 /* "emphasis = (string) ANY" */
59 /* "channel-order = (string) ANY" */
63 #define gst_rtp_g722_depay_parent_class parent_class
64 G_DEFINE_TYPE (GstRtpG722Depay, gst_rtp_g722_depay,
65 GST_TYPE_RTP_BASE_DEPAYLOAD);
67 static gboolean gst_rtp_g722_depay_setcaps (GstRTPBaseDepayload * depayload,
69 static GstBuffer *gst_rtp_g722_depay_process (GstRTPBaseDepayload * depayload,
73 gst_rtp_g722_depay_class_init (GstRtpG722DepayClass * klass)
75 GstElementClass *gstelement_class;
76 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
78 GST_DEBUG_CATEGORY_INIT (rtpg722depay_debug, "rtpg722depay", 0,
79 "G722 RTP Depayloader");
81 gstelement_class = (GstElementClass *) klass;
82 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
84 gst_element_class_add_static_pad_template (gstelement_class,
85 &gst_rtp_g722_depay_src_template);
86 gst_element_class_add_static_pad_template (gstelement_class,
87 &gst_rtp_g722_depay_sink_template);
89 gst_element_class_set_static_metadata (gstelement_class,
90 "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
91 "Extracts G722 audio from RTP packets",
92 "Wim Taymans <wim.taymans@gmail.com>");
94 gstrtpbasedepayload_class->set_caps = gst_rtp_g722_depay_setcaps;
95 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_g722_depay_process;
99 gst_rtp_g722_depay_init (GstRtpG722Depay * rtpg722depay)
104 gst_rtp_g722_depay_parse_int (GstStructure * structure, const gchar * field,
110 if ((str = gst_structure_get_string (structure, field)))
113 if (gst_structure_get_int (structure, field, &res))
120 gst_rtp_g722_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
122 GstStructure *structure;
123 GstRtpG722Depay *rtpg722depay;
124 gint clock_rate, payload, samplerate;
129 const gchar *channel_order;
130 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 /* FIXME: Do something with the channel order */
184 /* add channel positions */
185 channel_order = gst_structure_get_string (structure, "channel-order");
187 order = gst_rtp_channels_get_by_order (channels, channel_order);
189 gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0),
192 GstAudioChannelPosition *pos;
194 GST_ELEMENT_WARNING (rtpg722depay, STREAM, DECODE,
195 (NULL), ("Unknown channel order '%s' for %d channels",
196 GST_STR_NULL (channel_order), channels));
197 /* create default NONE layout */
198 pos = gst_rtp_channels_create_default (channels);
199 gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0), pos);
204 res = gst_pad_set_caps (depayload->srcpad, srccaps);
205 gst_caps_unref (srccaps);
212 GST_ERROR_OBJECT (depayload, "no clock-rate specified");
218 gst_rtp_g722_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
220 GstRtpG722Depay *rtpg722depay;
225 rtpg722depay = GST_RTP_G722_DEPAY (depayload);
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);
237 if (marker && outbuf) {
238 /* mark talk spurt with RESYNC */
239 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
243 gst_rtp_drop_meta (GST_ELEMENT_CAST (rtpg722depay), outbuf,
244 g_quark_from_static_string (GST_META_TAG_AUDIO_STR));
252 GST_ELEMENT_WARNING (rtpg722depay, STREAM, DECODE,
253 ("Empty Payload."), (NULL));
259 gst_rtp_g722_depay_plugin_init (GstPlugin * plugin)
261 return gst_element_register (plugin, "rtpg722depay",
262 GST_RANK_SECONDARY, GST_TYPE_RTP_G722_DEPAY);