2cbb743dd427844a5826e3e2f6d0eb624c4745e8
[asterisk/asterisk.git] / channels / h323 / caps_h323.cxx
1 #include <ptlib.h>
2 #include <h323.h>
3 #include <h245.h>
4 #include "caps_h323.h"
5
6 #define DEFINE_G711_CAPABILITY(cls, code, capName) \
7 class cls : public AST_G711Capability { \
8 public: \
9         cls() : AST_G711Capability(240, code) { } \
10 }; \
11 H323_REGISTER_CAPABILITY(cls, capName) \
12
13 DEFINE_G711_CAPABILITY(AST_G711ALaw64Capability, H323_G711Capability::ALaw, OPAL_G711_ALAW_64K);
14 DEFINE_G711_CAPABILITY(AST_G711uLaw64Capability, H323_G711Capability::muLaw, OPAL_G711_ULAW_64K);
15 H323_REGISTER_CAPABILITY(AST_G7231Capability, OPAL_G7231);
16 H323_REGISTER_CAPABILITY(AST_G729Capability,  OPAL_G729);
17 H323_REGISTER_CAPABILITY(AST_G729ACapability, OPAL_G729A);
18 H323_REGISTER_CAPABILITY(AST_GSM0610Capability, OPAL_GSM0610);
19 H323_REGISTER_CAPABILITY(AST_CiscoG726Capability, CISCO_G726r32);
20
21 OPAL_MEDIA_FORMAT_DECLARE(OpalG711ALaw64kFormat,
22         OPAL_G711_ALAW_64K,
23         OpalMediaFormat::DefaultAudioSessionID,
24         RTP_DataFrame::PCMA,
25         TRUE,   // Needs jitter
26         64000,  // bits/sec
27         8,              // bytes/frame
28         8,              // 1 millisecond/frame
29         OpalMediaFormat::AudioTimeUnits,
30         0);
31 OPAL_MEDIA_FORMAT_DECLARE(OpalG711uLaw64kFormat,
32         OPAL_G711_ULAW_64K,
33         OpalMediaFormat::DefaultAudioSessionID,
34         RTP_DataFrame::PCMU,
35         TRUE,   // Needs jitter
36         64000,  // bits/sec
37         8,              // bytes/frame
38         8,              // 1 millisecond/frame
39         OpalMediaFormat::AudioTimeUnits,
40         0);
41 OPAL_MEDIA_FORMAT_DECLARE(OpalG729Format,
42         OPAL_G729,
43         OpalMediaFormat::DefaultAudioSessionID,
44         RTP_DataFrame::G729,
45         TRUE,   // Needs jitter
46         8000,   // bits/sec
47         10,             // bytes
48         80,             // 10 milliseconds
49         OpalMediaFormat::AudioTimeUnits,
50         0);
51 OPAL_MEDIA_FORMAT_DECLARE(OpalG729AFormat,
52         OPAL_G729 "A",
53         OpalMediaFormat::DefaultAudioSessionID,
54         RTP_DataFrame::G729,
55         TRUE,   // Needs jitter
56         8000,   // bits/sec
57         10,             // bytes
58         80,             // 10 milliseconds
59         OpalMediaFormat::AudioTimeUnits,
60         0);
61 OPAL_MEDIA_FORMAT_DECLARE(OpalG7231_6k3Format,
62         OPAL_G7231_6k3,
63         OpalMediaFormat::DefaultAudioSessionID,
64         RTP_DataFrame::G7231,
65         TRUE,   // Needs jitter
66         6400,   // bits/sec
67         24,             // bytes
68         240,    // 30 milliseconds
69         OpalMediaFormat::AudioTimeUnits,
70         0);
71 OPAL_MEDIA_FORMAT_DECLARE(OpalG7231A_6k3Format,
72         OPAL_G7231A_6k3,
73         OpalMediaFormat::DefaultAudioSessionID,
74         RTP_DataFrame::G7231,
75         TRUE,   // Needs jitter
76         6400,   // bits/sec
77         24,             // bytes
78         240,    // 30 milliseconds
79         OpalMediaFormat::AudioTimeUnits,
80         0);
81 OPAL_MEDIA_FORMAT_DECLARE(OpalGSM0610Format,
82         OPAL_GSM0610,
83         OpalMediaFormat::DefaultAudioSessionID,
84         RTP_DataFrame::GSM,
85         TRUE,   // Needs jitter
86         13200,  // bits/sec
87         33,             // bytes
88         160,    // 20 milliseconds
89         OpalMediaFormat::AudioTimeUnits,
90         0);
91 OPAL_MEDIA_FORMAT_DECLARE(OpalCiscoG726Format,
92         CISCO_G726r32,
93         OpalMediaFormat::DefaultAudioSessionID,
94         RTP_DataFrame::G726,
95         TRUE,   // Needs jitter
96         32000,  // bits/sec
97         4,              // bytes
98         8,              // 1 millisecond
99         OpalMediaFormat::AudioTimeUnits,
100         0);
101
102 /*
103  * Capability: G.711
104  */
105 AST_G711Capability::AST_G711Capability(int rx_frames, H323_G711Capability::Mode m, H323_G711Capability::Speed s)
106         : H323AudioCapability(rx_frames, 30) // 240ms max, 30ms desired
107 {
108         mode = m;
109         speed = s;
110 }
111
112
113 PObject * AST_G711Capability::Clone() const
114 {
115         return new AST_G711Capability(*this);
116 }
117
118
119 unsigned AST_G711Capability::GetSubType() const
120 {
121         static const unsigned G711SubType[2][2] = {
122                 { H245_AudioCapability::e_g711Alaw64k, H245_AudioCapability::e_g711Alaw56k },
123                 { H245_AudioCapability::e_g711Ulaw64k, H245_AudioCapability::e_g711Ulaw56k }
124         };
125         return G711SubType[mode][speed];
126 }
127
128
129 PString AST_G711Capability::GetFormatName() const
130 {
131         static const char * const G711Name[2][2] = {
132                 { OPAL_G711_ALAW_64K, OPAL_G711_ALAW_56K },
133                 { OPAL_G711_ULAW_64K, OPAL_G711_ULAW_56K },
134         };
135         return G711Name[mode][speed];
136 }
137
138
139 H323Codec * AST_G711Capability::CreateCodec(H323Codec::Direction direction) const
140 {
141         return NULL;
142 }
143
144
145 /*
146  * Capability: G.723.1
147  */
148 AST_G7231Capability::AST_G7231Capability(int rx_frames, BOOL annexA_)
149         : H323AudioCapability(rx_frames, 4)
150 {
151         annexA = annexA_;
152 }
153
154 PObject::Comparison AST_G7231Capability::Compare(const PObject & obj) const
155 {
156         Comparison result = H323AudioCapability::Compare(obj);
157         if (result != EqualTo) {
158                 return result;
159         }
160         PINDEX otherAnnexA = ((const AST_G7231Capability &)obj).annexA;
161         if (annexA < otherAnnexA) {
162                 return LessThan;
163         }
164         if (annexA > otherAnnexA) {
165                 return GreaterThan;
166         }
167         return EqualTo;
168 }
169
170 PObject * AST_G7231Capability::Clone() const
171 {
172         return new AST_G7231Capability(*this);
173 }
174
175 PString AST_G7231Capability::GetFormatName() const
176 {
177         return (annexA ? OPAL_G7231 "A" : OPAL_G7231);
178 }
179
180 unsigned AST_G7231Capability::GetSubType() const
181 {
182         return H245_AudioCapability::e_g7231;
183 }
184
185 BOOL AST_G7231Capability::OnSendingPDU(H245_AudioCapability & cap,
186                                                                                 unsigned packetSize) const
187 {
188         cap.SetTag(H245_AudioCapability::e_g7231);
189         H245_AudioCapability_g7231 & g7231 = cap;
190         g7231.m_maxAl_sduAudioFrames = packetSize;
191         g7231.m_silenceSuppression = annexA;
192         return TRUE;
193 }
194
195 BOOL AST_G7231Capability::OnReceivedPDU(const H245_AudioCapability & cap,
196                                                                                 unsigned & packetSize)
197 {
198         if (cap.GetTag() != H245_AudioCapability::e_g7231) {
199                 return FALSE;
200         }
201         const H245_AudioCapability_g7231 & g7231 = cap;
202         packetSize = g7231.m_maxAl_sduAudioFrames;
203         annexA = g7231.m_silenceSuppression;
204         return TRUE;
205 }
206
207 H323Codec * AST_G7231Capability::CreateCodec(H323Codec::Direction direction) const
208 {
209         return NULL;
210 }
211
212 /*
213  * Capability: G.729
214  */
215 AST_G729Capability::AST_G729Capability(int rx_frames)
216         : H323AudioCapability(rx_frames, 2)
217 {
218 }
219
220 PObject * AST_G729Capability::Clone() const
221 {
222         return new AST_G729Capability(*this);
223 }
224
225 unsigned AST_G729Capability::GetSubType() const
226 {
227         return H245_AudioCapability::e_g729;
228 }
229
230 PString AST_G729Capability::GetFormatName() const
231 {
232         return OPAL_G729;
233 }
234
235 H323Codec * AST_G729Capability::CreateCodec(H323Codec::Direction direction) const
236 {
237         return NULL;
238 }
239
240 /*
241  * Capability: G.729A
242  */
243 AST_G729ACapability::AST_G729ACapability(int rx_frames)
244         : H323AudioCapability(rx_frames, 6)
245 {
246 }
247
248 PObject * AST_G729ACapability::Clone() const
249 {
250         return new AST_G729ACapability(*this);
251 }
252
253 unsigned AST_G729ACapability::GetSubType() const
254 {
255         return H245_AudioCapability::e_g729AnnexA;
256 }
257
258 PString AST_G729ACapability::GetFormatName() const
259 {
260         return OPAL_G729A;
261 }
262
263 H323Codec * AST_G729ACapability::CreateCodec(H323Codec::Direction direction) const
264 {
265         return NULL;
266 }
267
268 /*
269  * Capability: GSM full rate
270  */
271 AST_GSM0610Capability::AST_GSM0610Capability(int rx_frames, int comfortNoise_, int scrambled_)
272         : H323AudioCapability(rx_frames, 2)
273 {
274         comfortNoise = comfortNoise_;
275         scrambled = scrambled_;
276 }
277
278 PObject * AST_GSM0610Capability::Clone() const
279 {
280         return new AST_GSM0610Capability(*this);
281 }
282
283 unsigned AST_GSM0610Capability::GetSubType() const
284 {
285         return H245_AudioCapability::e_gsmFullRate;
286 }
287
288 BOOL AST_GSM0610Capability::OnSendingPDU(H245_AudioCapability & cap,
289                                                                                 unsigned packetSize) const
290 {
291         cap.SetTag(H245_AudioCapability::e_gsmFullRate);
292         H245_GSMAudioCapability & gsm = cap;
293         gsm.m_audioUnitSize = packetSize * 33;
294         gsm.m_comfortNoise = comfortNoise;
295         gsm.m_scrambled = scrambled;
296         return TRUE;
297 }
298
299 BOOL AST_GSM0610Capability::OnReceivedPDU(const H245_AudioCapability & cap,
300                                                                                 unsigned & packetSize)
301 {
302         if (cap.GetTag() != H245_AudioCapability::e_gsmFullRate)
303                 return FALSE;
304         const H245_GSMAudioCapability & gsm = cap;
305         packetSize = (gsm.m_audioUnitSize + 32) / 33;
306         comfortNoise = gsm.m_comfortNoise;
307         scrambled = gsm.m_scrambled;
308
309         return TRUE;
310 }
311
312 PString AST_GSM0610Capability::GetFormatName() const
313 {
314         return OPAL_GSM0610;
315 }
316
317 H323Codec * AST_GSM0610Capability::CreateCodec(H323Codec::Direction direction) const
318 {
319         return NULL;
320 }
321
322 /*
323  * Capability: G.726 32 Kbps
324  */
325 AST_CiscoG726Capability::AST_CiscoG726Capability(int rx_frames)
326         : H323NonStandardAudioCapability(rx_frames, 240,
327                 181, 0, 18,
328                 (const BYTE *)CISCO_G726r32, sizeof(CISCO_G726r32)-1, 0)
329 {
330 }
331
332 PObject *AST_CiscoG726Capability::Clone() const
333 {
334         return new AST_CiscoG726Capability(*this);
335 }
336
337 H323Codec *AST_CiscoG726Capability::CreateCodec(H323Codec::Direction direction) const
338 {
339         return NULL;
340 }
341
342 PString AST_CiscoG726Capability::GetFormatName() const
343 {
344         return PString(CISCO_G726r32);
345 }