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