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/rtp/gstrtpbuffer.h>
29 #include "gstrtpg722pay.h"
30 #include "gstrtpchannels.h"
32 GST_DEBUG_CATEGORY_STATIC (rtpg722pay_debug);
33 #define GST_CAT_DEFAULT (rtpg722pay_debug)
35 static GstStaticPadTemplate gst_rtp_g722_pay_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
39 GST_STATIC_CAPS ("audio/G722, " "rate = (int) 16000, " "channels = (int) 1")
42 static GstStaticPadTemplate gst_rtp_g722_pay_src_template =
43 GST_STATIC_PAD_TEMPLATE ("src",
46 GST_STATIC_CAPS ("application/x-rtp, "
47 "media = (string) \"audio\", "
48 "encoding-name = (string) \"G722\", "
49 "payload = (int) " GST_RTP_PAYLOAD_G722_STRING ", "
50 "clock-rate = (int) 8000")
53 static gboolean gst_rtp_g722_pay_setcaps (GstRTPBasePayload * basepayload,
55 static GstCaps *gst_rtp_g722_pay_getcaps (GstRTPBasePayload * rtppayload,
56 GstPad * pad, GstCaps * filter);
58 #define gst_rtp_g722_pay_parent_class parent_class
59 G_DEFINE_TYPE (GstRtpG722Pay, gst_rtp_g722_pay,
60 GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
63 gst_rtp_g722_pay_class_init (GstRtpG722PayClass * klass)
65 GstElementClass *gstelement_class;
66 GstRTPBasePayloadClass *gstrtpbasepayload_class;
68 GST_DEBUG_CATEGORY_INIT (rtpg722pay_debug, "rtpg722pay", 0,
69 "G722 RTP Payloader");
71 gstelement_class = (GstElementClass *) klass;
72 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
74 gst_element_class_add_pad_template (gstelement_class,
75 gst_static_pad_template_get (&gst_rtp_g722_pay_src_template));
76 gst_element_class_add_pad_template (gstelement_class,
77 gst_static_pad_template_get (&gst_rtp_g722_pay_sink_template));
79 gst_element_class_set_static_metadata (gstelement_class,
80 "RTP audio payloader", "Codec/Payloader/Network/RTP",
81 "Payload-encode Raw audio into RTP packets (RFC 3551)",
82 "Wim Taymans <wim.taymans@gmail.com>");
84 gstrtpbasepayload_class->set_caps = gst_rtp_g722_pay_setcaps;
85 gstrtpbasepayload_class->get_caps = gst_rtp_g722_pay_getcaps;
89 gst_rtp_g722_pay_init (GstRtpG722Pay * rtpg722pay)
91 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
93 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpg722pay);
95 /* tell rtpbaseaudiopayload that this is a sample based codec */
96 gst_rtp_base_audio_payload_set_sample_based (rtpbaseaudiopayload);
100 gst_rtp_g722_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
102 GstRtpG722Pay *rtpg722pay;
103 GstStructure *structure;
104 gint rate, channels, clock_rate;
108 GstAudioChannelPosition *pos;
109 const GstRTPChannelOrder *order;
111 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
113 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (basepayload);
114 rtpg722pay = GST_RTP_G722_PAY (basepayload);
116 structure = gst_caps_get_structure (caps, 0);
118 /* first parse input caps */
119 if (!gst_structure_get_int (structure, "rate", &rate))
122 if (!gst_structure_get_int (structure, "channels", &channels))
125 /* FIXME: Do something with the channel positions */
127 /* get the channel order */
128 pos = gst_audio_get_channel_positions (structure);
130 order = gst_rtp_channels_get_by_pos (channels, pos);
135 /* Clock rate is always 8000 Hz for G722 according to
136 * RFC 3551 although the sampling rate is 16000 Hz */
139 gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "G722",
141 params = g_strdup_printf ("%d", channels);
144 if (!order && channels > 2) {
145 GST_ELEMENT_WARNING (rtpg722pay, STREAM, DECODE,
146 (NULL), ("Unknown channel order for %d channels", channels));
149 if (order && order->name) {
150 res = gst_rtp_base_payload_set_outcaps (basepayload,
151 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
152 channels, "channel-order", G_TYPE_STRING, order->name, NULL);
155 res = gst_rtp_base_payload_set_outcaps (basepayload,
156 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
167 rtpg722pay->rate = rate;
168 rtpg722pay->channels = channels;
170 /* bits-per-sample is 4 * channels for G722, but as the RTP clock runs at
171 * half speed (8 instead of 16 khz), pretend it's 8 bits per sample
173 gst_rtp_base_audio_payload_set_samplebits_options (rtpbaseaudiopayload,
174 8 * rtpg722pay->channels);
181 GST_DEBUG_OBJECT (rtpg722pay, "no rate given");
186 GST_DEBUG_OBJECT (rtpg722pay, "no channels given");
192 gst_rtp_g722_pay_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
195 GstCaps *otherpadcaps;
198 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
199 caps = gst_pad_get_pad_template_caps (pad);
202 if (!gst_caps_is_empty (otherpadcaps)) {
203 caps = gst_caps_make_writable (caps);
204 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
205 gst_caps_set_simple (caps, "rate", G_TYPE_INT, 16000, NULL);
207 gst_caps_unref (otherpadcaps);
213 gst_rtp_g722_pay_plugin_init (GstPlugin * plugin)
215 return gst_element_register (plugin, "rtpg722pay",
216 GST_RANK_SECONDARY, GST_TYPE_RTP_G722_PAY);