1712942d48541f18e924a09bbfe64c8cb49acfdf
[asterisk/asterisk.git] / channels / chan_nbs.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, 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 /*** MODULEINFO
29         <depend>nbs</depend>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include <sys/socket.h>
37 #include <sys/time.h>
38 #include <errno.h>
39 #include <arpa/inet.h>
40 #include <fcntl.h>
41 #include <sys/ioctl.h>
42 #include <nbs.h>
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 tdesc[] = "Network Broadcast Sound Driver";
54
55 /* Only linear is allowed */
56 static int prefformat = AST_FORMAT_SLINEAR;
57
58 static char context[AST_MAX_EXTENSION] = "default";
59 static char type[] = "NBS";
60
61 /* NBS creates private structures on demand */
62    
63 struct nbs_pvt {
64         NBS *nbs;
65         struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
66         char app[16];                                   /* Our app */
67         char stream[80];                                /* Our stream */
68         struct ast_frame fr;                    /* "null" frame */
69         struct ast_module_user *u;              /*! for holding a reference to this module */
70 };
71
72 static struct ast_channel *nbs_request(const char *type, int format, void *data, int *cause);
73 static int nbs_call(struct ast_channel *ast, char *dest, int timeout);
74 static int nbs_hangup(struct ast_channel *ast);
75 static struct ast_frame *nbs_xread(struct ast_channel *ast);
76 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame);
77
78 static const struct ast_channel_tech nbs_tech = {
79         .type = type,
80         .description = tdesc,
81         .capabilities = AST_FORMAT_SLINEAR,
82         .requester = nbs_request,
83         .call = nbs_call,
84         .hangup = nbs_hangup,
85         .read = nbs_xread,
86         .write = nbs_xwrite,
87 };
88
89 static int nbs_call(struct ast_channel *ast, char *dest, int timeout)
90 {
91         struct nbs_pvt *p;
92
93         p = ast->tech_pvt;
94
95         if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
96                 ast_log(LOG_WARNING, "nbs_call called on %s, neither down nor reserved\n", ast->name);
97                 return -1;
98         }
99         /* When we call, it just works, really, there's no destination...  Just
100            ring the phone and wait for someone to answer */
101         ast_debug(1, "Calling %s on %s\n", dest, ast->name);
102
103         /* If we can't connect, return congestion */
104         if (nbs_connect(p->nbs)) {
105                 ast_log(LOG_WARNING, "NBS Connection failed on %s\n", ast->name);
106                 ast_queue_control(ast, AST_CONTROL_CONGESTION);
107         } else {
108                 ast_setstate(ast, AST_STATE_RINGING);
109                 ast_queue_control(ast, AST_CONTROL_ANSWER);
110         }
111
112         return 0;
113 }
114
115 static void nbs_destroy(struct nbs_pvt *p)
116 {
117         if (p->nbs)
118                 nbs_delstream(p->nbs);
119         ast_module_user_remove(p->u);
120         ast_free(p);
121 }
122
123 static struct nbs_pvt *nbs_alloc(void *data)
124 {
125         struct nbs_pvt *p;
126         int flags = 0;
127         char stream[256];
128         char *opts;
129
130         ast_copy_string(stream, data, sizeof(stream));
131         if ((opts = strchr(stream, ':'))) {
132                 *opts = '\0';
133                 opts++;
134         } else
135                 opts = "";
136         p = ast_calloc(1, sizeof(*p));
137         if (p) {
138                 if (!ast_strlen_zero(opts)) {
139                         if (strchr(opts, 'm'))
140                                 flags |= NBS_FLAG_MUTE;
141                         if (strchr(opts, 'o'))
142                                 flags |= NBS_FLAG_OVERSPEAK;
143                         if (strchr(opts, 'e'))
144                                 flags |= NBS_FLAG_EMERGENCY;
145                         if (strchr(opts, 'O'))
146                                 flags |= NBS_FLAG_OVERRIDE;
147                 } else
148                         flags = NBS_FLAG_OVERSPEAK;
149                 
150                 ast_copy_string(p->stream, stream, sizeof(p->stream));
151                 p->nbs = nbs_newstream("asterisk", stream, flags);
152                 if (!p->nbs) {
153                         ast_log(LOG_WARNING, "Unable to allocate new NBS stream '%s' with flags %d\n", stream, flags);
154                         ast_free(p);
155                         p = NULL;
156                 } else {
157                         /* Set for 8000 hz mono, 640 samples */
158                         nbs_setbitrate(p->nbs, 8000);
159                         nbs_setchannels(p->nbs, 1);
160                         nbs_setblocksize(p->nbs, 640);
161                         nbs_setblocking(p->nbs, 0);
162                 }
163         }
164         return p;
165 }
166
167 static int nbs_hangup(struct ast_channel *ast)
168 {
169         struct nbs_pvt *p;
170         p = ast->tech_pvt;
171         ast_debug(1, "nbs_hangup(%s)\n", ast->name);
172         if (!ast->tech_pvt) {
173                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
174                 return 0;
175         }
176         nbs_destroy(p);
177         ast->tech_pvt = NULL;
178         ast_setstate(ast, AST_STATE_DOWN);
179         return 0;
180 }
181
182 static struct ast_frame  *nbs_xread(struct ast_channel *ast)
183 {
184         struct nbs_pvt *p = ast->tech_pvt;
185         
186
187         /* Some nice norms */
188         p->fr.datalen = 0;
189         p->fr.samples = 0;
190         p->fr.data =  NULL;
191         p->fr.src = type;
192         p->fr.offset = 0;
193         p->fr.mallocd=0;
194         p->fr.delivery.tv_sec = 0;
195         p->fr.delivery.tv_usec = 0;
196
197         ast_debug(1, "Returning null frame on %s\n", ast->name);
198
199         return &p->fr;
200 }
201
202 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame)
203 {
204         struct nbs_pvt *p = ast->tech_pvt;
205         /* Write a frame of (presumably voice) data */
206         if (frame->frametype != AST_FRAME_VOICE) {
207                 if (frame->frametype != AST_FRAME_IMAGE)
208                         ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
209                 return 0;
210         }
211         if (!(frame->subclass &
212                 (AST_FORMAT_SLINEAR))) {
213                 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
214                 return 0;
215         }
216         if (ast->_state != AST_STATE_UP) {
217                 /* Don't try tos end audio on-hook */
218                 return 0;
219         }
220         if (nbs_write(p->nbs, frame->data, frame->datalen / 2) < 0) 
221                 return -1;
222         return 0;
223 }
224
225 static struct ast_channel *nbs_new(struct nbs_pvt *i, int state)
226 {
227         struct ast_channel *tmp;
228         tmp = ast_channel_alloc(1, state, 0, 0, "", "s", context, 0, "NBS/%s", i->stream);
229         if (tmp) {
230                 tmp->tech = &nbs_tech;
231                 ast_channel_set_fd(tmp, 0, nbs_fd(i->nbs));
232                 tmp->nativeformats = prefformat;
233                 tmp->rawreadformat = prefformat;
234                 tmp->rawwriteformat = prefformat;
235                 tmp->writeformat = prefformat;
236                 tmp->readformat = prefformat;
237                 if (state == AST_STATE_RING)
238                         tmp->rings = 1;
239                 tmp->tech_pvt = i;
240                 ast_copy_string(tmp->context, context, sizeof(tmp->context));
241                 ast_copy_string(tmp->exten, "s",  sizeof(tmp->exten));
242                 ast_string_field_set(tmp, language, "");
243                 i->owner = tmp;
244                 i->u = ast_module_user_add(tmp);
245                 if (state != AST_STATE_DOWN) {
246                         if (ast_pbx_start(tmp)) {
247                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
248                                 ast_hangup(tmp);
249                         }
250                 }
251         } else
252                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
253         return tmp;
254 }
255
256
257 static struct ast_channel *nbs_request(const char *type, int format, void *data, int *cause)
258 {
259         int oldformat;
260         struct nbs_pvt *p;
261         struct ast_channel *tmp = NULL;
262         
263         oldformat = format;
264         format &= (AST_FORMAT_SLINEAR);
265         if (!format) {
266                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
267                 return NULL;
268         }
269         p = nbs_alloc(data);
270         if (p) {
271                 tmp = nbs_new(p, AST_STATE_DOWN);
272                 if (!tmp)
273                         nbs_destroy(p);
274         }
275         return tmp;
276 }
277
278 static int unload_module(void)
279 {
280         /* First, take us out of the channel loop */
281         ast_channel_unregister(&nbs_tech);
282         return 0;
283 }
284
285 static int load_module(void)
286 {
287         /* Make sure we can register our channel type */
288         if (ast_channel_register(&nbs_tech)) {
289                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
290                 return -1;
291         }
292         return 0;
293 }
294
295 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Network Broadcast Sound Support");