Rename ooh323.conf to chan_ooh323.conf, make module support both names
[asterisk/asterisk.git] / addons / ooh323cDriver.c
1 /*
2  * Copyright (C) 2004-2005 by Objective Systems, Inc.
3  *
4  * This software is furnished under an open source license and may be 
5  * used and copied only in accordance with the terms of this license. 
6  * The text of the license may generally be found in the root 
7  * directory of this installation in the COPYING file.  It 
8  * can also be viewed online at the following URL:
9  *
10  *   http://www.obj-sys.com/open/license.html
11  *
12  * Any redistributions of this file including modified versions must 
13  * maintain this copyright notice.
14  *
15  *****************************************************************************/
16
17 #include "ooh323cDriver.h"
18
19 #include <asterisk/pbx.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/options.h>
22
23 extern OOBOOL gH323Debug;
24 /* ooh323c stack thread. */
25 static pthread_t ooh323c_thread = AST_PTHREADT_NULL;
26 static int grxframes = 240;
27
28 static int gtxframes = 20;
29
30 int ooh323c_start_receive_channel(ooCallData *call, ooLogicalChannel *pChannel);
31 int ooh323c_start_transmit_channel(ooCallData *call, ooLogicalChannel *pChannel);
32 int ooh323c_stop_receive_channel(ooCallData *call, ooLogicalChannel *pChannel);
33 int ooh323c_stop_transmit_channel(ooCallData *call, ooLogicalChannel *pChannel);
34
35 void* ooh323c_stack_thread(void* dummy)
36 {
37
38   ooMonitorChannels();
39   return dummy;
40 }
41
42 int ooh323c_start_stack_thread()
43 {
44    if(ast_pthread_create(&ooh323c_thread, NULL, ooh323c_stack_thread, NULL) < 0)
45    {
46       ast_log(LOG_ERROR, "Unable to start ooh323c thread.\n");
47       return -1;
48    }
49    return 0;
50 }
51
52 int ooh323c_stop_stack_thread(void)
53 {
54    if(ooh323c_thread !=  AST_PTHREADT_NULL)
55    {
56       ooStopMonitor();
57       pthread_join(ooh323c_thread, NULL);
58       ooh323c_thread =  AST_PTHREADT_NULL;
59    }
60    return 0;
61 }
62
63 int ooh323c_set_capability
64    (struct ast_codec_pref *prefs, int capability, int dtmf)
65 {
66    int ret, x, format=0;
67    if(gH323Debug)
68      ast_verbose("\tAdding capabilities to H323 endpoint\n");
69    
70    for(x=0; 0 != (format=ast_codec_pref_index(prefs, x)); x++)
71    {
72       if(format & AST_FORMAT_ULAW)
73       {
74          if(gH323Debug)
75             ast_verbose("\tAdding g711 ulaw capability to H323 endpoint\n");
76          ret= ooH323EpAddG711Capability(OO_G711ULAW64K, gtxframes, grxframes, 
77                                      OORXANDTX, &ooh323c_start_receive_channel,
78                                      &ooh323c_start_transmit_channel,
79                                      &ooh323c_stop_receive_channel, 
80                                      &ooh323c_stop_transmit_channel);
81       }
82       if(format & AST_FORMAT_ALAW)
83       {
84          if(gH323Debug)
85             ast_verbose("\tAdding g711 alaw capability to H323 endpoint\n");
86          ret= ooH323EpAddG711Capability(OO_G711ALAW64K, gtxframes, grxframes, 
87                                      OORXANDTX, &ooh323c_start_receive_channel,
88                                      &ooh323c_start_transmit_channel,
89                                      &ooh323c_stop_receive_channel, 
90                                      &ooh323c_stop_transmit_channel);
91       }
92
93       if(format & AST_FORMAT_G729A)
94       {
95          if(gH323Debug)
96             ast_verbose("\tAdding g729A capability to H323 endpoint\n");
97          ret = ooH323EpAddG729Capability(OO_G729A, 2, 24, 
98                                      OORXANDTX, &ooh323c_start_receive_channel,
99                                      &ooh323c_start_transmit_channel,
100                                      &ooh323c_stop_receive_channel, 
101                                      &ooh323c_stop_transmit_channel);
102
103          if(gH323Debug)
104             ast_verbose("\tAdding g729 capability to H323 endpoint\n");
105          ret |= ooH323EpAddG729Capability(OO_G729, 2, 24, 
106                                      OORXANDTX, &ooh323c_start_receive_channel,
107                                      &ooh323c_start_transmit_channel,
108                                      &ooh323c_stop_receive_channel, 
109                                      &ooh323c_stop_transmit_channel);
110       }
111
112       if(format & AST_FORMAT_G723_1)
113       {
114          if(gH323Debug)
115             ast_verbose("\tAdding g7231 capability to H323 endpoint\n");
116          ret = ooH323EpAddG7231Capability(OO_G7231, 4, 7, FALSE, 
117                                      OORXANDTX, &ooh323c_start_receive_channel,
118                                      &ooh323c_start_transmit_channel,
119                                      &ooh323c_stop_receive_channel, 
120                                      &ooh323c_stop_transmit_channel);
121
122       }
123
124       if(format & AST_FORMAT_H263)
125       {
126          if(gH323Debug)
127             ast_verbose("\tAdding h263 capability to H323 endpoint\n");
128          ret = ooH323EpAddH263VideoCapability(OO_H263VIDEO, 1, 0, 0, 0, 0, 320*1024, 
129                                      OORXANDTX, &ooh323c_start_receive_channel,
130                                      &ooh323c_start_transmit_channel,
131                                      &ooh323c_stop_receive_channel, 
132                                      &ooh323c_stop_transmit_channel);
133
134       }
135
136       if(format & AST_FORMAT_GSM)
137       {
138          if(gH323Debug)
139             ast_verbose("\tAdding gsm capability to H323 endpoint\n");
140          ret = ooH323EpAddGSMCapability(OO_GSMFULLRATE, 4, FALSE, FALSE, 
141                                      OORXANDTX, &ooh323c_start_receive_channel,
142                                      &ooh323c_start_transmit_channel,
143                                      &ooh323c_stop_receive_channel, 
144                                      &ooh323c_stop_transmit_channel);
145
146       }
147       
148    }
149    
150    if(dtmf & H323_DTMF_RFC2833)
151       ret |= ooH323EpEnableDTMFRFC2833(0);
152    else if(dtmf & H323_DTMF_H245ALPHANUMERIC)
153       ret |= ooH323EpEnableDTMFH245Alphanumeric();
154    else if(dtmf & H323_DTMF_H245SIGNAL)
155       ret |= ooH323EpEnableDTMFH245Signal();
156
157    return ret;
158 }
159
160 int ooh323c_set_capability_for_call
161    (ooCallData *call, struct ast_codec_pref *prefs, int capability, int dtmf)
162 {
163    int ret, x, txframes;
164    int format=0;
165    if(gH323Debug)
166      ast_verbose("\tAdding capabilities to call(%s, %s)\n", call->callType, 
167                                                             call->callToken);
168    if(dtmf & H323_DTMF_RFC2833)
169       ret |= ooCallEnableDTMFRFC2833(call,0);
170    else if(dtmf & H323_DTMF_H245ALPHANUMERIC)
171       ret |= ooCallEnableDTMFH245Alphanumeric(call);
172    else if(dtmf & H323_DTMF_H245SIGNAL)
173       ret |= ooCallEnableDTMFH245Signal(call);
174
175
176    for(x=0; 0 !=(format=ast_codec_pref_index(prefs, x)); x++)
177    {
178       if(format & AST_FORMAT_ULAW)
179       {
180          if(gH323Debug)
181             ast_verbose("\tAdding g711 ulaw capability to call(%s, %s)\n", 
182                                               call->callType, call->callToken);
183          txframes = prefs->framing[x];
184          ret= ooCallAddG711Capability(call, OO_G711ULAW64K, txframes, 
185                                       grxframes, OORXANDTX, 
186                                       &ooh323c_start_receive_channel,
187                                       &ooh323c_start_transmit_channel,
188                                       &ooh323c_stop_receive_channel, 
189                                       &ooh323c_stop_transmit_channel);
190       }
191       if(format & AST_FORMAT_ALAW)
192       {
193          if(gH323Debug)
194             ast_verbose("\tAdding g711 alaw capability to call(%s, %s)\n",
195                                             call->callType, call->callToken);
196          txframes = prefs->framing[x];
197          ret= ooCallAddG711Capability(call, OO_G711ALAW64K, txframes, 
198                                      grxframes, OORXANDTX, 
199                                      &ooh323c_start_receive_channel,
200                                      &ooh323c_start_transmit_channel,
201                                      &ooh323c_stop_receive_channel, 
202                                      &ooh323c_stop_transmit_channel);
203       }
204
205       if(format & AST_FORMAT_G729A)
206       {
207          if(gH323Debug)
208             ast_verbose("\tAdding g729A capability to call(%s, %s)\n",
209                                             call->callType, call->callToken);
210          txframes = (prefs->framing[x])/10;
211          ret= ooCallAddG729Capability(call, OO_G729A, txframes, 24, 
212                                      OORXANDTX, &ooh323c_start_receive_channel,
213                                      &ooh323c_start_transmit_channel,
214                                      &ooh323c_stop_receive_channel, 
215                                      &ooh323c_stop_transmit_channel);
216
217          if(gH323Debug)
218             ast_verbose("\tAdding g729 capability to call(%s, %s)\n",
219                                             call->callType, call->callToken);
220          ret|= ooCallAddG729Capability(call, OO_G729, txframes, 24, 
221                                      OORXANDTX, &ooh323c_start_receive_channel,
222                                      &ooh323c_start_transmit_channel,
223                                      &ooh323c_stop_receive_channel, 
224                                      &ooh323c_stop_transmit_channel);
225       }
226
227       if(format & AST_FORMAT_G723_1)
228       {
229          if(gH323Debug)
230             ast_verbose("\tAdding g7231 capability to call (%s, %s)\n",
231                                            call->callType, call->callToken);
232          ret = ooCallAddG7231Capability(call, OO_G7231, 4, 7, FALSE, 
233                                      OORXANDTX, &ooh323c_start_receive_channel,
234                                      &ooh323c_start_transmit_channel,
235                                      &ooh323c_stop_receive_channel, 
236                                      &ooh323c_stop_transmit_channel);
237
238       }
239
240       if(format & AST_FORMAT_H263)
241       {
242          if(gH323Debug)
243             ast_verbose("\tAdding h263 capability to call (%s, %s)\n",
244                                            call->callType, call->callToken);
245          ret = ooCallAddH263VideoCapability(call, OO_H263VIDEO, 1, 0, 0, 0, 0, 320*1024, 
246                                      OORXANDTX, &ooh323c_start_receive_channel,
247                                      &ooh323c_start_transmit_channel,
248                                      &ooh323c_stop_receive_channel, 
249                                      &ooh323c_stop_transmit_channel);
250
251       }
252
253       if(format & AST_FORMAT_GSM)
254       {
255          if(gH323Debug)
256             ast_verbose("\tAdding gsm capability to call(%s, %s)\n", 
257                                              call->callType, call->callToken);
258          ret = ooCallAddGSMCapability(call, OO_GSMFULLRATE, 4, FALSE, FALSE, 
259                                      OORXANDTX, &ooh323c_start_receive_channel,
260                                      &ooh323c_start_transmit_channel,
261                                      &ooh323c_stop_receive_channel, 
262                                      &ooh323c_stop_transmit_channel);
263       }
264    }
265 }
266
267 int ooh323c_set_aliases(ooAliases * aliases)
268 {
269    ooAliases *cur = aliases;
270    while(cur)
271    {
272       switch(cur->type)
273       { 
274       case T_H225AliasAddress_dialedDigits:
275          ooH323EpAddAliasDialedDigits(cur->value);
276          break;
277       case T_H225AliasAddress_h323_ID:
278          ooH323EpAddAliasH323ID(cur->value);
279          break;
280       case T_H225AliasAddress_url_ID:
281          ooH323EpAddAliasURLID(cur->value);
282          break;
283       case T_H225AliasAddress_email_ID:
284          ooH323EpAddAliasEmailID(cur->value);
285          break;
286       default:
287          ast_debug(1, "Ignoring unknown alias type\n");
288       }
289       cur = cur->next;
290    }
291    return 1;
292 }
293    
294 int ooh323c_start_receive_channel(ooCallData *call, ooLogicalChannel *pChannel)
295 {
296    int fmt=-1;
297    fmt = convertH323CapToAsteriskCap(pChannel->chanCap->cap);
298    if(fmt>0)
299       ooh323_set_read_format(call, fmt);
300    else{
301      ast_log(LOG_ERROR, "Invalid capability type for receive channel %s\n",
302                                                           call->callToken);
303      return -1;
304    }
305    return 1;
306 }
307
308 int ooh323c_start_transmit_channel(ooCallData *call, ooLogicalChannel *pChannel)
309 {
310    int fmt=-1;
311    fmt = convertH323CapToAsteriskCap(pChannel->chanCap->cap);
312    if(fmt>0)
313       ooh323_set_write_format(call, fmt);
314    else{
315       ast_log(LOG_ERROR, "Invalid capability type for receive channel %s\n",
316                                                           call->callToken);
317       return -1;
318    }
319    setup_rtp_connection(call, pChannel->remoteIP, pChannel->remoteMediaPort);
320     return 1;
321 }
322
323 int ooh323c_stop_receive_channel(ooCallData *call, ooLogicalChannel *pChannel)
324 {
325    return 1;
326 }
327
328 int ooh323c_stop_transmit_channel(ooCallData *call, ooLogicalChannel *pChannel)
329 {
330    close_rtp_connection(call);
331    return 1;
332 }
333
334 int convertH323CapToAsteriskCap(int cap)
335 {
336
337    switch(cap)
338    {
339       case OO_G711ULAW64K:
340          return AST_FORMAT_ULAW;
341       case OO_G711ALAW64K:
342          return AST_FORMAT_ALAW;
343       case OO_GSMFULLRATE:
344          return AST_FORMAT_GSM;
345       case OO_G729:
346          return AST_FORMAT_G729A;
347       case OO_G729A:
348          return AST_FORMAT_G729A;
349       case OO_G7231:
350          return AST_FORMAT_G723_1;
351       case OO_H263VIDEO:
352          return AST_FORMAT_H263;
353       default:
354          ast_debug(1, "Cap %d is not supported by driver yet\n", cap);
355          return -1;
356    }
357
358    return -1;
359 }
360
361