use auto-build for cdr modules
[asterisk/asterisk.git] / channels / chan_nbs.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Network broadcast sound support channel driver
22  * 
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \ingroup channel_drivers
26  */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/socket.h>
31 #include <sys/time.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <arpa/inet.h>
36 #include <fcntl.h>
37 #include <sys/ioctl.h>
38 #include <nbs.h>
39
40 #include "asterisk.h"
41
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43
44 #include "asterisk/lock.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/config.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/module.h"
49 #include "asterisk/pbx.h"
50 #include "asterisk/options.h"
51 #include "asterisk/utils.h"
52
53 static const char desc[] = "Network Broadcast Sound Support";
54 static const char tdesc[] = "Network Broadcast Sound Driver";
55
56 static int usecnt =0;
57
58 /* Only linear is allowed */
59 static int prefformat = AST_FORMAT_SLINEAR;
60
61 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
62
63 static char context[AST_MAX_EXTENSION] = "default";
64 static char type[] = "NBS";
65
66 /* NBS creates private structures on demand */
67    
68 struct nbs_pvt {
69         NBS *nbs;
70         struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
71         char app[16];                                   /* Our app */
72         char stream[80];                                /* Our stream */
73         struct ast_frame fr;                    /* "null" frame */
74 };
75
76 static struct ast_channel *nbs_request(const char *type, int format, void *data, int *cause);
77 static int nbs_call(struct ast_channel *ast, char *dest, int timeout);
78 static int nbs_hangup(struct ast_channel *ast);
79 static struct ast_frame *nbs_xread(struct ast_channel *ast);
80 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame);
81
82 static const struct ast_channel_tech nbs_tech = {
83         .type = type,
84         .description = tdesc,
85         .capabilities = AST_FORMAT_SLINEAR,
86         .requester = nbs_request,
87         .call = nbs_call,
88         .hangup = nbs_hangup,
89         .read = nbs_xread,
90         .write = nbs_xwrite,
91 };
92
93 static int nbs_call(struct ast_channel *ast, char *dest, int timeout)
94 {
95         struct nbs_pvt *p;
96
97         p = ast->tech_pvt;
98
99         if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
100                 ast_log(LOG_WARNING, "nbs_call called on %s, neither down nor reserved\n", ast->name);
101                 return -1;
102         }
103         /* When we call, it just works, really, there's no destination...  Just
104            ring the phone and wait for someone to answer */
105         if (option_debug)
106                 ast_log(LOG_DEBUG, "Calling %s on %s\n", dest, ast->name);
107
108         /* If we can't connect, return congestion */
109         if (nbs_connect(p->nbs)) {
110                 ast_log(LOG_WARNING, "NBS Connection failed on %s\n", ast->name);
111                 ast_queue_control(ast, AST_CONTROL_CONGESTION);
112         } else {
113                 ast_setstate(ast, AST_STATE_RINGING);
114                 ast_queue_control(ast, AST_CONTROL_ANSWER);
115         }
116
117         return 0;
118 }
119
120 static void nbs_destroy(struct nbs_pvt *p)
121 {
122         if (p->nbs)
123                 nbs_delstream(p->nbs);
124         free(p);
125 }
126
127 static struct nbs_pvt *nbs_alloc(void *data)
128 {
129         struct nbs_pvt *p;
130         int flags = 0;
131         char stream[256] = "";
132         char *opts;
133         strncpy(stream, data, sizeof(stream) - 1);
134         if ((opts = strchr(stream, ':'))) {
135                 *opts = '\0';
136                 opts++;
137         } else
138                 opts = "";
139         p = malloc(sizeof(struct nbs_pvt));
140         if (p) {
141                 memset(p, 0, sizeof(struct nbs_pvt));
142                 if (!ast_strlen_zero(opts)) {
143                         if (strchr(opts, 'm'))
144                                 flags |= NBS_FLAG_MUTE;
145                         if (strchr(opts, 'o'))
146                                 flags |= NBS_FLAG_OVERSPEAK;
147                         if (strchr(opts, 'e'))
148                                 flags |= NBS_FLAG_EMERGENCY;
149                         if (strchr(opts, 'O'))
150                                 flags |= NBS_FLAG_OVERRIDE;
151                 } else
152                         flags = NBS_FLAG_OVERSPEAK;
153                 
154                 strncpy(p->stream, stream, sizeof(p->stream) - 1);
155                 p->nbs = nbs_newstream("asterisk", stream, flags);
156                 if (!p->nbs) {
157                         ast_log(LOG_WARNING, "Unable to allocate new NBS stream '%s' with flags %d\n", stream, flags);
158                         free(p);
159                         p = NULL;
160                 } else {
161                         /* Set for 8000 hz mono, 640 samples */
162                         nbs_setbitrate(p->nbs, 8000);
163                         nbs_setchannels(p->nbs, 1);
164                         nbs_setblocksize(p->nbs, 640);
165                         nbs_setblocking(p->nbs, 0);
166                 }
167         }
168         return p;
169 }
170
171 static int nbs_hangup(struct ast_channel *ast)
172 {
173         struct nbs_pvt *p;
174         p = ast->tech_pvt;
175         if (option_debug)
176                 ast_log(LOG_DEBUG, "nbs_hangup(%s)\n", ast->name);
177         if (!ast->tech_pvt) {
178                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
179                 return 0;
180         }
181         nbs_destroy(p);
182         ast->tech_pvt = NULL;
183         ast_setstate(ast, AST_STATE_DOWN);
184         return 0;
185 }
186
187 static struct ast_frame  *nbs_xread(struct ast_channel *ast)
188 {
189         struct nbs_pvt *p = ast->tech_pvt;
190         
191
192         /* Some nice norms */
193         p->fr.datalen = 0;
194         p->fr.samples = 0;
195         p->fr.data =  NULL;
196         p->fr.src = type;
197         p->fr.offset = 0;
198         p->fr.mallocd=0;
199         p->fr.delivery.tv_sec = 0;
200         p->fr.delivery.tv_usec = 0;
201
202         ast_log(LOG_DEBUG, "Returning null frame on %s\n", ast->name);
203
204         return &p->fr;
205 }
206
207 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame)
208 {
209         struct nbs_pvt *p = ast->tech_pvt;
210         /* Write a frame of (presumably voice) data */
211         if (frame->frametype != AST_FRAME_VOICE) {
212                 if (frame->frametype != AST_FRAME_IMAGE)
213                         ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
214                 return 0;
215         }
216         if (!(frame->subclass &
217                 (AST_FORMAT_SLINEAR))) {
218                 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
219                 return 0;
220         }
221         if (ast->_state != AST_STATE_UP) {
222                 /* Don't try tos end audio on-hook */
223                 return 0;
224         }
225         if (nbs_write(p->nbs, frame->data, frame->datalen / 2) < 0) 
226                 return -1;
227         return 0;
228 }
229
230 static struct ast_channel *nbs_new(struct nbs_pvt *i, int state)
231 {
232         struct ast_channel *tmp;
233         tmp = ast_channel_alloc(1);
234         if (tmp) {
235                 tmp->tech = &nbs_tech;
236                 ast_string_field_build(tmp, name, "NBS/%s", i->stream);
237                 tmp->fds[0] = nbs_fd(i->nbs);
238                 tmp->nativeformats = prefformat;
239                 tmp->rawreadformat = prefformat;
240                 tmp->rawwriteformat = prefformat;
241                 tmp->writeformat = prefformat;
242                 tmp->readformat = prefformat;
243                 ast_setstate(tmp, state);
244                 if (state == AST_STATE_RING)
245                         tmp->rings = 1;
246                 tmp->tech_pvt = i;
247                 strncpy(tmp->context, context, sizeof(tmp->context)-1);
248                 strncpy(tmp->exten, "s",  sizeof(tmp->exten) - 1);
249                 ast_string_field_set(tmp, language, "");
250                 i->owner = tmp;
251                 ast_mutex_lock(&usecnt_lock);
252                 usecnt++;
253                 ast_mutex_unlock(&usecnt_lock);
254                 ast_update_use_count();
255                 if (state != AST_STATE_DOWN) {
256                         if (ast_pbx_start(tmp)) {
257                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
258                                 ast_hangup(tmp);
259                         }
260                 }
261         } else
262                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
263         return tmp;
264 }
265
266
267 static struct ast_channel *nbs_request(const char *type, int format, void *data, int *cause)
268 {
269         int oldformat;
270         struct nbs_pvt *p;
271         struct ast_channel *tmp = NULL;
272         
273         oldformat = format;
274         format &= (AST_FORMAT_SLINEAR);
275         if (!format) {
276                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
277                 return NULL;
278         }
279         p = nbs_alloc(data);
280         if (p) {
281                 tmp = nbs_new(p, AST_STATE_DOWN);
282                 if (!tmp)
283                         nbs_destroy(p);
284         }
285         return tmp;
286 }
287
288 static int __unload_module(void)
289 {
290         /* First, take us out of the channel loop */
291         ast_channel_unregister(&nbs_tech);
292         return 0;
293 }
294
295 int unload_module(void)
296 {
297         return __unload_module();
298 }
299
300 int load_module()
301 {
302         /* Make sure we can register our channel type */
303         if (ast_channel_register(&nbs_tech)) {
304                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
305                 __unload_module();
306                 return -1;
307         }
308         return 0;
309 }
310
311 int usecount()
312 {
313         return usecnt;
314 }
315
316 char *description()
317 {
318         return (char *) desc;
319 }
320
321 char *key()
322 {
323         return ASTERISK_GPL_KEY;
324 }