9818750afb80dab832ddebcff9f97d517509ef37
[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 class MyH323EndPoint : public H323EndPoint {
102
103         PCLASSINFO(MyH323EndPoint, H323EndPoint);
104
105         public:
106
107         int MakeCall(const PString &, PString &, unsigned int *, unsigned int, char *);
108         BOOL ClearCall(const PString &);
109
110         void OnClosedLogicalChannel(H323Connection &, const H323Channel &);
111         void OnConnectionEstablished(H323Connection &, const PString &);
112         void OnConnectionCleared(H323Connection &, const PString &);
113         H323Connection * CreateConnection(unsigned, void *);
114         H323Connection * SetupTransfer(const PString &, const PString &, const PString &, PString &, void *);
115         void SendUserTone(const PString &, char);
116         H323Capabilities GetCapabilities(void);
117         BOOL OnConnectionForwarded(H323Connection &, const PString &, const H323SignalPDU &);
118  
119         BOOL ForwardConnection(H323Connection &, const PString &, const H323SignalPDU &);
120
121         PStringArray SupportedPrefixes; 
122         
123     void SetEndpointTypeInfo( H225_EndpointType & info ) const;
124     void SetGateway(void);
125
126 };
127
128   
129 class MyH323Connection : public H323Connection {
130
131         PCLASSINFO(MyH323Connection, H323Connection);
132
133         public:
134         MyH323Connection(MyH323EndPoint &, unsigned, unsigned);
135         ~MyH323Connection();
136
137         H323Channel * CreateRealTimeLogicalChannel(const H323Capability &, H323Channel::Directions, unsigned, 
138                                                                                            const H245_H2250LogicalChannelParameters *);
139         H323Connection::AnswerCallResponse OnAnswerCall(const PString &, const H323SignalPDU &, H323SignalPDU &);
140         void OnReceivedReleaseComplete(const H323SignalPDU &);
141         BOOL OnAlerting(const H323SignalPDU &, const PString &);
142         BOOL OnSendReleaseComplete(H323SignalPDU &);
143         BOOL OnReceivedSignalSetup(const H323SignalPDU &);
144         BOOL OnReceivedFacility(const H323SignalPDU &);
145         BOOL OnSendSignalSetup(H323SignalPDU &);
146         BOOL OnStartLogicalChannel(H323Channel &);
147         BOOL OnClosingLogicalChannel(H323Channel &);    
148         void SendUserInputTone(char, unsigned);
149         void OnUserInputTone(char, unsigned, unsigned, unsigned);
150         void OnUserInputString(const PString &value);
151
152         PString sourceAliases;
153         PString destAliases;
154         PString sourceE164;
155         PString destE164;
156
157         PIPSocket::Address externalIpAddress;   // IP address of media server
158     PIPSocket::Address remoteIpAddress;         // IP Address of remote endpoint
159         WORD                       externalPort;                // local media server Data port (control is dataPort+1)
160         WORD                       remotePort;                  // remote endpoint Data port (control is dataPort+1)
161         WORD                       sessionId;
162         BOOL                       bridging;                    // Used to help determine which IP to use
163 };
164
165
166 #if 0
167 class MyGatekeeperServer : public H323GatekeeperServer
168 {
169     PCLASSINFO(MyGatekeeperServer, H323GatekeeperServer);
170   public:
171     MyGatekeeperServer(MyH323EndPoint & ep);
172
173     // Overrides
174     virtual H323GatekeeperCall * CreateCall(
175       const OpalGloballyUniqueID & callIdentifier,
176       H323GatekeeperCall::Direction direction
177     );
178     virtual BOOL TranslateAliasAddressToSignalAddress(
179       const H225_AliasAddress & alias,
180       H323TransportAddress & address
181     );
182
183     // new functions
184     BOOL Initialise();
185
186   private:
187     class RouteMap : public PObject {
188         PCLASSINFO(RouteMap, PObject);
189       public:
190         RouteMap(
191           const PString & alias,
192           const PString & host
193         );
194         RouteMap(
195           const RouteMap & map
196         ) : alias(map.alias), regex(map.alias), host(map.host) { }
197
198         void PrintOn(
199           ostream & strm
200         ) const;
201
202         BOOL IsValid() const;
203
204         BOOL IsMatch(
205           const PString & alias
206         ) const;
207
208         const H323TransportAddress & GetHost() const { return host; }
209
210       private:
211         PString              alias;
212         PRegularExpression   regex;
213         H323TransportAddress host;
214     };
215     PList<RouteMap> routes;
216
217     PMutex reconfigurationMutex;
218 };
219
220 #endif
221
222 /**
223  * The MyProcess is a necessary descendant PProcess class so that the H323EndPoint 
224  * objected to be created from within that class. (Who owns main() problem). 
225  */
226 class MyProcess : public PProcess {
227
228         PCLASSINFO(MyProcess, PProcess);
229     
230         public:
231         MyProcess();
232         ~MyProcess();
233
234         void Main(); 
235         
236         
237 };
238
239
240 /** 
241  * This class handles the termination of a call.
242  * Note that OpenH323 Library requires that the termination
243  * of a call should be done inside a separate thread of execution.
244  */
245 class ClearCallThread : public PThread {
246
247         PCLASSINFO(ClearCallThread, PThread);
248
249         public:
250         ClearCallThread(const char *tc);
251         ~ClearCallThread();    
252         
253         void Main();
254         
255         protected:
256         PString token;
257 };
258
259