a7604834e1b7a03d9f362c1714553e744595b6d9
[asterisk/asterisk.git] / channels / h323 / ast_h323.h
1 /*
2  * h323wrap.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
65
66 /**This class describes the G.723.1 codec capability.
67  */
68 class H323_G7231Capability : public H323AudioCapability
69 {
70     PCLASSINFO(H323_G7231Capability, H323AudioCapability);
71   public:
72     H323_G7231Capability(
73       BOOL annexA = TRUE  /// Enable Annex A silence insertion descriptors
74     );
75     Comparison Compare(const PObject & obj) const;
76
77     PObject * Clone() const;
78   
79         virtual H323Codec * CreateCodec(
80       H323Codec::Direction direction  /// Direction in which this instance runs
81     ) const;
82
83         unsigned GetSubType() const;
84     PString GetFormatName() const;
85     BOOL OnSendingPDU(
86       H245_AudioCapability & pdu,  /// PDU to set information on
87       unsigned packetSize          /// Packet size to use in capability
88     ) const;
89
90     BOOL OnReceivedPDU(
91       const H245_AudioCapability & pdu,  /// PDU to get information from
92       unsigned & packetSize              /// Packet size to use in capability
93     );
94   
95   protected:
96     BOOL annexA;
97 };
98
99 class MyH323EndPoint : public H323EndPoint {
100
101         PCLASSINFO(MyH323EndPoint, H323EndPoint);
102
103         public:
104
105         int MakeCall(const PString &, PString &, unsigned int *, unsigned int);
106         BOOL ClearCall(const PString &);
107 //      BOOL OnIncomingCall( H323Connection & connection, const H323SignalPDU &, H323SignalPDU &);
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, WORD);
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 OnReceivedSignalSetup(const H323SignalPDU &);
136         void OnReceivedReleaseComplete(const H323SignalPDU &);
137         BOOL OnSendSignalSetup(H323SignalPDU &);
138         BOOL OnStartLogicalChannel(H323Channel &);
139         BOOL OnClosingLogicalChannel(H323Channel &);
140         void SendUserInputTone(char, unsigned);
141         void OnUserInputTone(char, unsigned, unsigned, unsigned);
142         void OnUserInputString(const PString &value);
143
144         PString sourceAliases;
145         PString destAliases;
146         PString sourceE164;
147         PString destE164;
148
149         PIPSocket::Address externalIpAddress;   // IP address of media server
150     PIPSocket::Address remoteIpAddress;         // IP Address of remote endpoint
151         WORD                       externalPort;                // local media server Data port (control is dataPort+1)
152         WORD                       remotePort;                  // remote endpoint Data port (control is dataPort+1)
153 };
154
155
156 #if 0
157 class MyGatekeeperServer : public H323GatekeeperServer
158 {
159     PCLASSINFO(MyGatekeeperServer, H323GatekeeperServer);
160   public:
161     MyGatekeeperServer(MyH323EndPoint & ep);
162
163     // Overrides
164     virtual H323GatekeeperCall * CreateCall(
165       const OpalGloballyUniqueID & callIdentifier,
166       H323GatekeeperCall::Direction direction
167     );
168     virtual BOOL TranslateAliasAddressToSignalAddress(
169       const H225_AliasAddress & alias,
170       H323TransportAddress & address
171     );
172
173     // new functions
174     BOOL Initialise();
175
176   private:
177     class RouteMap : public PObject {
178         PCLASSINFO(RouteMap, PObject);
179       public:
180         RouteMap(
181           const PString & alias,
182           const PString & host
183         );
184         RouteMap(
185           const RouteMap & map
186         ) : alias(map.alias), regex(map.alias), host(map.host) { }
187
188         void PrintOn(
189           ostream & strm
190         ) const;
191
192         BOOL IsValid() const;
193
194         BOOL IsMatch(
195           const PString & alias
196         ) const;
197
198         const H323TransportAddress & GetHost() const { return host; }
199
200       private:
201         PString              alias;
202         PRegularExpression   regex;
203         H323TransportAddress host;
204     };
205     PList<RouteMap> routes;
206
207     PMutex reconfigurationMutex;
208 };
209
210 #endif
211
212 /**
213  * The MyProcess is a necessary descendant PProcess class so that the H323EndPoint 
214  * objected to be created from within that class. (Who owns main() problem). 
215  */
216 class MyProcess : public PProcess {
217
218         PCLASSINFO(MyProcess, PProcess);
219     
220         public:
221         MyProcess();
222         ~MyProcess();
223
224         void Main(); 
225         
226         
227 };
228
229
230 /** 
231  * This class handles the termination of a call.
232  * Note that OpenH323 Library requires that the termination
233  * of a call should be done inside a separate thread of execution.
234  */
235 class ClearCallThread : public PThread {
236
237         PCLASSINFO(ClearCallThread, PThread);
238
239         public:
240         ClearCallThread(const char *tc);
241         ~ClearCallThread();    
242         
243         void Main();
244         
245         protected:
246         PString token;
247 };
248
249