5c0c2b4a91db7e2e160f05b67b724f88c6e60a21
[asterisk/asterisk.git] / channels / h323 / ast_h323.h
1 /*
2  * ast_h323.h
3  *
4  * OpenH323 Channel Driver for ASTERISK PBX.
5  *                      By Jeremy McNamara
6  *                      For The NuFone Network 
7  * 
8  * This code has been derived from code created by
9  *              Michael Manousos and Mark Spencer
10  *
11  * This file is part of the chan_h323 driver for Asterisk
12  *
13  * chan_h323 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version. 
17  *
18  * chan_h323 is distributed WITHOUT ANY WARRANTY; without even 
19  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
20  * PURPOSE. See the GNU General Public License for more details. 
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
25  *
26  * Version Info: $Id$ 
27  */
28
29
30 #include <ptlib.h>
31 #include <h323.h>
32 #include <h323pdu.h>
33 #include <mediafmt.h>
34 #include <lid.h>
35
36 #include <list>
37 #include <string>
38 #include <algorithm>
39
40 #include "chan_h323.h"
41
42 /**  These need to be redefined here because the C++
43          side of this driver is blind to the asterisk headers */
44         
45 /*! G.723.1 compression */
46 #define AST_FORMAT_G723_1       (1 << 0)
47 /*! GSM compression */
48 #define AST_FORMAT_GSM          (1 << 1)
49 /*! Raw mu-law data (G.711) */
50 #define AST_FORMAT_ULAW         (1 << 2)
51 /*! Raw A-law data (G.711) */
52 #define AST_FORMAT_ALAW         (1 << 3)
53 /*! MPEG-2 layer 3 */
54 #define AST_FORMAT_MP3          (1 << 4)
55 /*! ADPCM (whose?) */
56 #define AST_FORMAT_ADPCM        (1 << 5)
57 /*! Raw 16-bit Signed Linear (8000 Hz) PCM */
58 #define AST_FORMAT_SLINEAR      (1 << 6)
59 /*! LPC10, 180 samples/frame */
60 #define AST_FORMAT_LPC10        (1 << 7)
61 /*! G.729A audio */
62 #define AST_FORMAT_G729A        (1 << 8)
63 /*! SpeeX Free Compression */
64 #define AST_FORMAT_SPEEX        (1 << 9)
65 /*! ILBC Free Codec */
66 #define AST_FORMAT_ILBC         (1 << 10)
67
68 /**This class describes the G.723.1 codec capability.
69  */
70 class H323_G7231Capability : public H323AudioCapability
71 {
72     PCLASSINFO(H323_G7231Capability, H323AudioCapability);
73   public:
74     H323_G7231Capability(
75       BOOL annexA = TRUE  /// Enable Annex A silence insertion descriptors
76     );
77     Comparison Compare(const PObject & obj) const;
78
79     PObject * Clone() const;
80   
81         virtual H323Codec * CreateCodec(
82       H323Codec::Direction direction  /// Direction in which this instance runs
83     ) const;
84
85         unsigned GetSubType() const;
86     PString GetFormatName() const;
87     BOOL OnSendingPDU(
88       H245_AudioCapability & pdu,  /// PDU to set information on
89       unsigned packetSize          /// Packet size to use in capability
90     ) const;
91
92     BOOL OnReceivedPDU(
93       const H245_AudioCapability & pdu,  /// PDU to get information from
94       unsigned & packetSize              /// Packet size to use in capability
95     );
96   
97   protected:
98     BOOL annexA;
99 };
100
101 /**This class describes the (fake) G729 codec capability.
102  */
103 class AST_G729Capability : public H323AudioCapability
104 {
105   PCLASSINFO(AST_G729Capability, H323AudioCapability);
106
107   public:
108   /**@name Construction */
109   //@{
110     /**Create a new G.729 capability.
111      */
112     AST_G729Capability();
113   //@}
114
115   /**@name Overrides from class PObject */
116   //@{
117     /**Create a copy of the object.
118       */
119     virtual PObject * Clone() const;
120   //@}
121
122   /**@name Operations */
123   //@{
124     /**Create the codec instance, allocating resources as required.
125      */
126     virtual H323Codec * CreateCodec(
127       H323Codec::Direction direction  /// Direction in which this instance runs
128     ) const;
129   //@}
130
131   /**@name Identification functions */
132   //@{
133     /**Get the sub-type of the capability. This is a code dependent on the
134        main type of the capability.
135
136        This returns one of the four possible combinations of mode and speed
137        using the enum values of the protocol ASN H245_AudioCapability class.
138      */
139     virtual unsigned GetSubType() const;
140
141     /**Get the name of the media data format this class represents.
142      */
143     virtual PString GetFormatName() const;
144   //@}
145 };
146
147
148 /**This class describes the VoiceAge G729A codec capability.
149  */
150 class AST_G729ACapability : public H323AudioCapability
151 {
152   PCLASSINFO(AST_G729ACapability, H323AudioCapability);
153
154   public:
155   /**@name Construction */
156   //@{
157     /**Create a new G.729A capability.
158      */
159     AST_G729ACapability();
160   //@}
161
162   /**@name Overrides from class PObject */
163   //@{
164     /**Create a copy of the object.
165       */
166     virtual PObject * Clone() const;
167   //@}
168
169   /**@name Operations */
170   //@{
171     /**Create the codec instance, allocating resources as required.
172      */
173     virtual H323Codec * CreateCodec(
174       H323Codec::Direction direction  /// Direction in which this instance runs
175     ) const;
176   //@}
177
178   /**@name Identification functions */
179   //@{
180     /**Get the sub-type of the capability. This is a code dependent on the
181        main type of the capability.
182
183        This returns one of the four possible combinations of mode and speed
184        using the enum values of the protocol ASN H245_AudioCapability class.
185      */
186     virtual unsigned GetSubType() const;
187
188     /**Get the name of the media data format this class represents.
189      */
190     virtual PString GetFormatName() const;
191   //@}
192 };
193
194
195 class MyH323EndPoint : public H323EndPoint {
196
197         PCLASSINFO(MyH323EndPoint, H323EndPoint);
198
199         public:
200
201         int MakeCall(const PString &, PString &, unsigned int *, unsigned int, char *);
202         BOOL ClearCall(const PString &);
203
204         void OnClosedLogicalChannel(H323Connection &, const H323Channel &);
205         void OnConnectionEstablished(H323Connection &, const PString &);
206         void OnConnectionCleared(H323Connection &, const PString &);
207         H323Connection * CreateConnection(unsigned, void *);
208         void SendUserTone(const PString &, char);
209         H323Capabilities GetCapabilities(void);
210         BOOL OnConnectionForwarded(H323Connection &, const PString &, const H323SignalPDU &);
211  
212         BOOL ForwardConnection(H323Connection &, const PString &, const H323SignalPDU &);
213
214         PStringArray SupportedPrefixes; 
215         
216     void SetEndpointTypeInfo( H225_EndpointType & info ) const;
217     void SetGateway(void);
218
219 };
220
221   
222 class MyH323Connection : public H323Connection {
223
224         PCLASSINFO(MyH323Connection, H323Connection);
225
226         public:
227         MyH323Connection(MyH323EndPoint &, unsigned, unsigned);
228         ~MyH323Connection();
229
230         H323Channel * CreateRealTimeLogicalChannel(const H323Capability &, H323Channel::Directions, unsigned, 
231                                                                                            const H245_H2250LogicalChannelParameters *);
232         H323Connection::AnswerCallResponse OnAnswerCall(const PString &, const H323SignalPDU &, H323SignalPDU &);
233         void OnReceivedReleaseComplete(const H323SignalPDU &);
234         BOOL OnAlerting(const H323SignalPDU &, const PString &);
235         BOOL OnSendReleaseComplete(H323SignalPDU &);
236         BOOL OnReceivedSignalSetup(const H323SignalPDU &);
237         BOOL OnReceivedFacility(const H323SignalPDU &);
238         BOOL OnSendSignalSetup(H323SignalPDU &);
239         BOOL OnStartLogicalChannel(H323Channel &);
240         BOOL OnClosingLogicalChannel(H323Channel &);    
241         void SendUserInputTone(char, unsigned);
242         void OnUserInputTone(char, unsigned, unsigned, unsigned);
243         void OnUserInputString(const PString &value);
244
245         PString sourceAliases;
246         PString destAliases;
247         PString sourceE164;
248         PString destE164;
249
250         PIPSocket::Address externalIpAddress;   // IP address of media server
251     PIPSocket::Address remoteIpAddress;         // IP Address of remote endpoint
252         WORD                       externalPort;                // local media server Data port (control is dataPort+1)
253         WORD                       remotePort;                  // remote endpoint Data port (control is dataPort+1)
254         WORD                       sessionId;
255         BOOL                       bridging;                    // Used to help determine which IP to use
256 };
257
258
259 #if 0
260 class MyGatekeeperServer : public H323GatekeeperServer
261 {
262     PCLASSINFO(MyGatekeeperServer, H323GatekeeperServer);
263   public:
264     MyGatekeeperServer(MyH323EndPoint & ep);
265
266     // Overrides
267     virtual H323GatekeeperCall * CreateCall(
268       const OpalGloballyUniqueID & callIdentifier,
269       H323GatekeeperCall::Direction direction
270     );
271     virtual BOOL TranslateAliasAddressToSignalAddress(
272       const H225_AliasAddress & alias,
273       H323TransportAddress & address
274     );
275
276     // new functions
277     BOOL Initialise();
278
279   private:
280     class RouteMap : public PObject {
281         PCLASSINFO(RouteMap, PObject);
282       public:
283         RouteMap(
284           const PString & alias,
285           const PString & host
286         );
287         RouteMap(
288           const RouteMap & map
289         ) : alias(map.alias), regex(map.alias), host(map.host) { }
290
291         void PrintOn(
292           ostream & strm
293         ) const;
294
295         BOOL IsValid() const;
296
297         BOOL IsMatch(
298           const PString & alias
299         ) const;
300
301         const H323TransportAddress & GetHost() const { return host; }
302
303       private:
304         PString              alias;
305         PRegularExpression   regex;
306         H323TransportAddress host;
307     };
308     PList<RouteMap> routes;
309
310     PMutex reconfigurationMutex;
311 };
312
313 #endif
314
315 /**
316  * The MyProcess is a necessary descendant PProcess class so that the H323EndPoint 
317  * objected to be created from within that class. (Who owns main() problem). 
318  */
319 class MyProcess : public PProcess {
320
321         PCLASSINFO(MyProcess, PProcess);
322     
323         public:
324         MyProcess();
325         ~MyProcess();
326
327         void Main(); 
328         
329         
330 };
331
332
333 /** 
334  * This class handles the termination of a call.
335  * Note that OpenH323 Library requires that the termination
336  * of a call should be done inside a separate thread of execution.
337  */
338 class ClearCallThread : public PThread {
339
340         PCLASSINFO(ClearCallThread, PThread);
341
342         public:
343         ClearCallThread(const char *tc);
344         ~ClearCallThread();    
345         
346         void Main();
347         
348         protected:
349         PString token;
350 };
351
352