1 /* GStreamer
2 * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
3 *
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.
8 *
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.
13 *
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.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/audio/audio.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28
29 #include "gstrtpelements.h"
30 #include "gstrtpg722pay.h"
31 #include "gstrtpchannels.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpg722pay_debug);
34 #define GST_CAT_DEFAULT (rtpg722pay_debug)
35
36 static GstStaticPadTemplate gst_rtp_g722_pay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
38 GST_PAD_SINK,
39 GST_PAD_ALWAYS,
40 GST_STATIC_CAPS ("audio/G722, " "rate = (int) 16000, " "channels = (int) 1")
41 );
42
43 static GstStaticPadTemplate gst_rtp_g722_pay_src_template =
44 GST_STATIC_PAD_TEMPLATE ("src",
45 GST_PAD_SRC,
46 GST_PAD_ALWAYS,
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 "encoding-params = (string) 1, "
52 "clock-rate = (int) 8000; "
53 "application/x-rtp, "
54 "media = (string) \"audio\", "
55 "encoding-name = (string) \"G722\", "
56 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
57 "encoding-params = (string) 1, " "clock-rate = (int) 8000")
58 );
59
60 static gboolean gst_rtp_g722_pay_setcaps (GstRTPBasePayload * basepayload,
61 GstCaps * caps);
62 static GstCaps *gst_rtp_g722_pay_getcaps (GstRTPBasePayload * rtppayload,
63 GstPad * pad, GstCaps * filter);
64
65 #define gst_rtp_g722_pay_parent_class parent_class
66 G_DEFINE_TYPE (GstRtpG722Pay, gst_rtp_g722_pay,
67 GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
68 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpg722pay, "rtpg722pay",
69 GST_RANK_SECONDARY, GST_TYPE_RTP_G722_PAY, rtp_element_init (plugin));
70
71 static void
gst_rtp_g722_pay_class_init(GstRtpG722PayClass * klass)72 gst_rtp_g722_pay_class_init (GstRtpG722PayClass * klass)
73 {
74 GstElementClass *gstelement_class;
75 GstRTPBasePayloadClass *gstrtpbasepayload_class;
76
77 GST_DEBUG_CATEGORY_INIT (rtpg722pay_debug, "rtpg722pay", 0,
78 "G722 RTP Payloader");
79
80 gstelement_class = (GstElementClass *) klass;
81 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
82
83 gst_element_class_add_static_pad_template (gstelement_class,
84 &gst_rtp_g722_pay_src_template);
85 gst_element_class_add_static_pad_template (gstelement_class,
86 &gst_rtp_g722_pay_sink_template);
87
88 gst_element_class_set_static_metadata (gstelement_class,
89 "RTP audio payloader", "Codec/Payloader/Network/RTP",
90 "Payload-encode Raw audio into RTP packets (RFC 3551)",
91 "Wim Taymans <wim.taymans@gmail.com>");
92
93 gstrtpbasepayload_class->set_caps = gst_rtp_g722_pay_setcaps;
94 gstrtpbasepayload_class->get_caps = gst_rtp_g722_pay_getcaps;
95 }
96
97 static void
gst_rtp_g722_pay_init(GstRtpG722Pay * rtpg722pay)98 gst_rtp_g722_pay_init (GstRtpG722Pay * rtpg722pay)
99 {
100 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
101
102 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpg722pay);
103
104 GST_RTP_BASE_PAYLOAD (rtpg722pay)->pt = GST_RTP_PAYLOAD_G722;
105
106 /* tell rtpbaseaudiopayload that this is a sample based codec */
107 gst_rtp_base_audio_payload_set_sample_based (rtpbaseaudiopayload);
108 }
109
110 static gboolean
gst_rtp_g722_pay_setcaps(GstRTPBasePayload * basepayload,GstCaps * caps)111 gst_rtp_g722_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
112 {
113 GstRtpG722Pay *rtpg722pay;
114 GstStructure *structure;
115 gint rate, channels, clock_rate;
116 gboolean res;
117 gchar *params;
118 #if 0
119 GstAudioChannelPosition *pos;
120 const GstRTPChannelOrder *order;
121 #endif
122 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
123
124 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (basepayload);
125 rtpg722pay = GST_RTP_G722_PAY (basepayload);
126
127 structure = gst_caps_get_structure (caps, 0);
128
129 /* first parse input caps */
130 if (!gst_structure_get_int (structure, "rate", &rate))
131 goto no_rate;
132
133 if (!gst_structure_get_int (structure, "channels", &channels))
134 goto no_channels;
135
136 /* FIXME: Do something with the channel positions */
137 #if 0
138 /* get the channel order */
139 pos = gst_audio_get_channel_positions (structure);
140 if (pos)
141 order = gst_rtp_channels_get_by_pos (channels, pos);
142 else
143 order = NULL;
144 #endif
145
146 /* Clock rate is always 8000 Hz for G722 according to
147 * RFC 3551 although the sampling rate is 16000 Hz */
148 clock_rate = 8000;
149
150 gst_rtp_base_payload_set_options (basepayload, "audio",
151 basepayload->pt != GST_RTP_PAYLOAD_G722, "G722", clock_rate);
152 params = g_strdup_printf ("%d", channels);
153
154 #if 0
155 if (!order && channels > 2) {
156 GST_ELEMENT_WARNING (rtpg722pay, STREAM, DECODE,
157 (NULL), ("Unknown channel order for %d channels", channels));
158 }
159
160 if (order && order->name) {
161 res = gst_rtp_base_payload_set_outcaps (basepayload,
162 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
163 channels, "channel-order", G_TYPE_STRING, order->name, NULL);
164 } else {
165 #endif
166 res = gst_rtp_base_payload_set_outcaps (basepayload,
167 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
168 channels, NULL);
169 #if 0
170 }
171 #endif
172
173 g_free (params);
174 #if 0
175 g_free (pos);
176 #endif
177
178 rtpg722pay->rate = rate;
179 rtpg722pay->channels = channels;
180
181 /* bits-per-sample is 4 * channels for G722, but as the RTP clock runs at
182 * half speed (8 instead of 16 khz), pretend it's 8 bits per sample
183 * channels. */
184 gst_rtp_base_audio_payload_set_samplebits_options (rtpbaseaudiopayload,
185 8 * rtpg722pay->channels);
186
187 return res;
188
189 /* ERRORS */
190 no_rate:
191 {
192 GST_DEBUG_OBJECT (rtpg722pay, "no rate given");
193 return FALSE;
194 }
195 no_channels:
196 {
197 GST_DEBUG_OBJECT (rtpg722pay, "no channels given");
198 return FALSE;
199 }
200 }
201
202 static GstCaps *
gst_rtp_g722_pay_getcaps(GstRTPBasePayload * rtppayload,GstPad * pad,GstCaps * filter)203 gst_rtp_g722_pay_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
204 GstCaps * filter)
205 {
206 GstCaps *otherpadcaps;
207 GstCaps *caps;
208
209 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
210 caps = gst_pad_get_pad_template_caps (pad);
211
212 if (otherpadcaps) {
213 if (!gst_caps_is_empty (otherpadcaps)) {
214 caps = gst_caps_make_writable (caps);
215 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
216 gst_caps_set_simple (caps, "rate", G_TYPE_INT, 16000, NULL);
217 }
218 gst_caps_unref (otherpadcaps);
219 }
220
221 if (filter) {
222 GstCaps *tmp;
223
224 GST_DEBUG_OBJECT (rtppayload, "Intersect %" GST_PTR_FORMAT " and filter %"
225 GST_PTR_FORMAT, caps, filter);
226 tmp = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
227 gst_caps_unref (caps);
228 caps = tmp;
229 }
230
231 return caps;
232 }
233