468f778155092654ab9524e56670c63349cbb7e7
[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  */
27
28
29 #include <ptlib.h>
30 #include <h323.h>
31 #include <h323pdu.h>
32 #include <mediafmt.h>
33 #include <lid.h>
34
35 #include <list>
36 #include <string>
37 #include <algorithm>
38
39 #include "chan_h323.h"
40
41 /**  These need to be redefined here because the C++
42          side of this driver is blind to the asterisk headers */
43         
44 /*! G.723.1 compression */
45 #define AST_FORMAT_G723_1       (1 << 0)
46 /*! GSM compression */
47 #define AST_FORMAT_GSM          (1 << 1)
48 /*! Raw mu-law data (G.711) */
49 #define AST_FORMAT_ULAW         (1 << 2)
50 /*! Raw A-law data (G.711) */
51 #define AST_FORMAT_ALAW         (1 << 3)
52 /*! MPEG-2 layer 3 */
53 #define AST_FORMAT_MP3          (1 << 4)
54 /*! ADPCM (whose?) */
55 #define AST_FORMAT_ADPCM        (1 << 5)
56 /*! Raw 16-bit Signed Linear (8000 Hz) PCM */
57 #define AST_FORMAT_SLINEAR      (1 << 6)
58 /*! LPC10, 180 samples/frame */
59 #define AST_FORMAT_LPC10        (1 << 7)
60 /*! G.729A audio */
61 #define AST_FORMAT_G729A        (1 << 8)
62 /*! SpeeX Free Compression */
63 #define AST_FORMAT_SPEEX        (1 << 9)
64 /*! ILBC Free Codec */
65 #define AST_FORMAT_ILBC         (1 << 10)
66
67 /**This class describes the G.723.1 codec capability.
68  */
69 class H323_G7231Capability : public H323AudioCapability
70 {
71     PCLASSINFO(H323_G7231Capability, H323AudioCapability);
72   public:
73     H323_G7231Capability(
74       BOOL annexA = TRUE  /// Enable Annex A silence insertion descriptors
75     );
76     Comparison Compare(const PObject & obj) const;
77
78     PObject * Clone() const;
79   
80         virtual H323Codec * CreateCodec(
81       H323Codec::Direction direction  /// Direction in which this instance runs
82     ) const;
83
84         unsigned GetSubType() const;
85     PString GetFormatName() const;
86     BOOL OnSendingPDU(
87       H245_AudioCapability & pdu,  /// PDU to set information on
88       unsigned packetSize          /// Packet size to use in capability
89     ) const;
90
91     BOOL OnReceivedPDU(
92       const H245_AudioCapability & pdu,  /// PDU to get information from
93       unsigned & packetSize              /// Packet size to use in capability
94     );
95   
96   protected:
97     BOOL annexA;
98 };
99
100 class MyH323EndPoint : public H323EndPoint {
101
102         PCLASSINFO(MyH323EndPoint, H323EndPoint);
103
104         public:
105
106         int MakeCall(const PString &, PString &, unsigned int *, unsigned int, char *);
107         BOOL ClearCall(const PString &);
108
109         void OnClosedLogicalChannel(H323Connection &, const H323Channel &);
110         void OnConnectionEstablished(H323Connection &, const PString &);
111         void OnConnectionCleared(H323Connection &, const PString &);
112         H323Connection * CreateConnection(unsigned, void *);
113         void SendUserTone(const PString &, char);
114         H323Capabilities GetCapabilities(void);
115
116         PStringArray SupportedPrefixes; 
117         
118     void SetEndpointTypeInfo( H225_EndpointType & info ) const;
119     void SetGateway(void);
120 };
121
122   
123 class MyH323Connection : public H323Connection {
124
125         PCLASSINFO(MyH323Connection, H323Connection);
126
127         public:
128         MyH323Connection(MyH323EndPoint &, unsigned, unsigned);
129         ~MyH323Connection();
130
131         H323Channel * CreateRealTimeLogicalChannel(const H323Capability &, H323Channel::Directions, unsigned, 
132                                                                                            const H245_H2250LogicalChannelParameters *);
133         H323Connection::AnswerCallResponse OnAnswerCall(const PString &, const H323SignalPDU &, H323SignalPDU &);
134         BOOL OnAlerting(const H323SignalPDU &, const PString &);
135         BOOL OnSendReleaseComplete(H323SignalPDU &);
136         BOOL OnReceivedSignalSetup(const H323SignalPDU &);
137         void OnReceivedReleaseComplete(const H323SignalPDU &);
138         BOOL OnReceivedFacility(const H323SignalPDU &);
139         BOOL OnSendSignalSetup(H323SignalPDU &);
140         BOOL OnStartLogicalChannel(H323Channel &);
141         BOOL OnClosingLogicalChannel(H323Channel &);
142         void SendUserInputTone(char, unsigned);
143         void OnUserInputTone(char, unsigned, unsigned, unsigned);
144         void OnUserInputString(const PString &value);
145
146         PString sourceAliases;
147         PString destAliases;
148         PString sourceE164;
149         PString destE164;
150
151         PIPSocket::Address externalIpAddress;   // IP address of media server
152     PIPSocket::Address remoteIpAddress;         // IP Address of remote endpoint
153         WORD                       externalPort;                // local media server Data port (control is dataPort+1)
154         WORD                       remotePort;                  // remote endpoint Data port (control is dataPort+1)
155 };
156
157
158 #if 0
159 class MyGatekeeperServer : public H323GatekeeperServer
160 {
161     PCLASSINFO(MyGatekeeperServer, H323GatekeeperServer);
162   public:
163     MyGatekeeperServer(MyH323EndPoint & ep);
164
165     // Overrides
166     virtual H323GatekeeperCall * CreateCall(
167       const OpalGloballyUniqueID & callIdentifier,
168       H323GatekeeperCall::Direction direction
169     );
170     virtual BOOL TranslateAliasAddressToSignalAddress(
171       const H225_AliasAddress & alias,
172       H323TransportAddress & address
173     );
174
175     // new functions
176     BOOL Initialise();
177
178   private:
179     class RouteMap : public PObject {
180         PCLASSINFO(RouteMap, PObject);
181       public:
182         RouteMap(
183           const PString & alias,
184           const PString & host
185         );
186         RouteMap(
187           const RouteMap & map
188         ) : alias(map.alias), regex(map.alias), host(map.host) { }
189
190         void PrintOn(
191           ostream & strm
192         ) const;
193
194         BOOL IsValid() const;
195
196         BOOL IsMatch(
197           const PString & alias
198         ) const;
199
200         const H323TransportAddress & GetHost() const { return host; }
201
202       private:
203         PString              alias;
204         PRegularExpression   regex;
205         H323TransportAddress host;
206     };
207     PList<RouteMap> routes;
208
209     PMutex reconfigurationMutex;
210 };
211
212 #endif
213
214 /**
215  * The MyProcess is a necessary descendant PProcess class so that the H323EndPoint 
216  * objected to be created from within that class. (Who owns main() problem). 
217  */
218 class MyProcess : public PProcess {
219
220         PCLASSINFO(MyProcess, PProcess);
221     
222         public:
223         MyProcess();
224         ~MyProcess();
225
226         void Main(); 
227         
228         
229 };
230
231
232 /** 
233  * This class handles the termination of a call.
234  * Note that OpenH323 Library requires that the termination
235  * of a call should be done inside a separate thread of execution.
236  */
237 class ClearCallThread : public PThread {
238
239         PCLASSINFO(ClearCallThread, PThread);
240
241         public:
242         ClearCallThread(const char *tc);
243         ~ClearCallThread();    
244         
245         void Main();
246         
247         protected:
248         PString token;
249 };
250
251