2 * Copyright (C) <2010> 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.
26 #include <gst/audio/audio.h>
27 #include <gst/audio/multichannel.h>
28 #include <gst/rtp/gstrtpbuffer.h>
30 #include "gstrtpg722pay.h"
31 #include "gstrtpchannels.h"
33 GST_DEBUG_CATEGORY_STATIC (rtpg722pay_debug);
34 #define GST_CAT_DEFAULT (rtpg722pay_debug)
36 static GstStaticPadTemplate gst_rtp_g722_pay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
40 GST_STATIC_CAPS ("audio/G722, " "rate = (int) 16000, " "channels = (int) 1")
43 static GstStaticPadTemplate gst_rtp_g722_pay_src_template =
44 GST_STATIC_PAD_TEMPLATE ("src",
47 GST_STATIC_CAPS ("application/x-rtp, "
48 "media = (string) \"audio\", "
49 "encoding-name = (string) \"G722\", "
50 "payload = (int) " GST_RTP_PAYLOAD_G722_STRING ", "
51 "clock-rate = (int) 8000")
54 static gboolean gst_rtp_g722_pay_setcaps (GstBaseRTPPayload * basepayload,
56 static GstCaps *gst_rtp_g722_pay_getcaps (GstBaseRTPPayload * rtppayload,
59 GST_BOILERPLATE (GstRtpG722Pay, gst_rtp_g722_pay, GstBaseRTPAudioPayload,
60 GST_TYPE_BASE_RTP_AUDIO_PAYLOAD);
63 gst_rtp_g722_pay_base_init (gpointer klass)
65 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
67 gst_element_class_add_pad_template (element_class,
68 gst_static_pad_template_get (&gst_rtp_g722_pay_src_template));
69 gst_element_class_add_pad_template (element_class,
70 gst_static_pad_template_get (&gst_rtp_g722_pay_sink_template));
72 gst_element_class_set_details_simple (element_class, "RTP audio payloader",
73 "Codec/Payloader/Network",
74 "Payload-encode Raw audio into RTP packets (RFC 3551)",
75 "Wim Taymans <wim.taymans@gmail.com>");
79 gst_rtp_g722_pay_class_init (GstRtpG722PayClass * klass)
81 GstBaseRTPPayloadClass *gstbasertppayload_class;
83 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
85 gstbasertppayload_class->set_caps = gst_rtp_g722_pay_setcaps;
86 gstbasertppayload_class->get_caps = gst_rtp_g722_pay_getcaps;
88 GST_DEBUG_CATEGORY_INIT (rtpg722pay_debug, "rtpg722pay", 0,
89 "G722 RTP Payloader");
93 gst_rtp_g722_pay_init (GstRtpG722Pay * rtpg722pay, GstRtpG722PayClass * klass)
95 GstBaseRTPAudioPayload *basertpaudiopayload;
97 basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (rtpg722pay);
99 /* tell basertpaudiopayload that this is a sample based codec */
100 gst_base_rtp_audio_payload_set_sample_based (basertpaudiopayload);
104 gst_rtp_g722_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
106 GstRtpG722Pay *rtpg722pay;
107 GstStructure *structure;
108 gint rate, channels, clock_rate;
111 GstAudioChannelPosition *pos;
112 const GstRTPChannelOrder *order;
113 GstBaseRTPAudioPayload *basertpaudiopayload;
115 basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (basepayload);
116 rtpg722pay = GST_RTP_G722_PAY (basepayload);
118 structure = gst_caps_get_structure (caps, 0);
120 /* first parse input caps */
121 if (!gst_structure_get_int (structure, "rate", &rate))
124 if (!gst_structure_get_int (structure, "channels", &channels))
127 /* get the channel order */
128 pos = gst_audio_get_channel_positions (structure);
130 order = gst_rtp_channels_get_by_pos (channels, pos);
134 /* Clock rate is always 8000 Hz for G722 according to
135 * RFC 3551 although the sampling rate is 16000 Hz */
138 gst_basertppayload_set_options (basepayload, "audio", TRUE, "G722",
140 params = g_strdup_printf ("%d", channels);
142 if (!order && channels > 2) {
143 GST_ELEMENT_WARNING (rtpg722pay, STREAM, DECODE,
144 (NULL), ("Unknown channel order for %d channels", channels));
147 if (order && order->name) {
148 res = gst_basertppayload_set_outcaps (basepayload,
149 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
150 channels, "channel-order", G_TYPE_STRING, order->name, NULL);
152 res = gst_basertppayload_set_outcaps (basepayload,
153 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
160 rtpg722pay->rate = rate;
161 rtpg722pay->channels = channels;
163 /* octet-per-sample is 1 * channels for G722 */
164 gst_base_rtp_audio_payload_set_samplebits_options (basertpaudiopayload,
165 4 * rtpg722pay->channels);
172 GST_DEBUG_OBJECT (rtpg722pay, "no rate given");
177 GST_DEBUG_OBJECT (rtpg722pay, "no channels given");
183 gst_rtp_g722_pay_getcaps (GstBaseRTPPayload * rtppayload, GstPad * pad)
185 GstCaps *otherpadcaps;
188 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
189 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
192 if (!gst_caps_is_empty (otherpadcaps)) {
193 GstStructure *structure;
195 structure = gst_caps_get_structure (otherpadcaps, 0);
197 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
198 gst_caps_set_simple (caps, "rate", G_TYPE_INT, 16000, NULL);
200 gst_caps_unref (otherpadcaps);
206 gst_rtp_g722_pay_plugin_init (GstPlugin * plugin)
208 return gst_element_register (plugin, "rtpg722pay",
209 GST_RANK_NONE, GST_TYPE_RTP_G722_PAY);