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