Merged revisions 80789 via svnmerge from
[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 <stdio.h>
37 #include <string.h>
38 #include <sys/socket.h>
39 #include <sys/time.h>
40 #include <errno.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <arpa/inet.h>
44 #include <fcntl.h>
45 #include <sys/ioctl.h>
46 #include <nbs.h>
47
48 #include "asterisk/lock.h"
49 #include "asterisk/channel.h"
50 #include "asterisk/config.h"
51 #include "asterisk/logger.h"
52 #include "asterisk/module.h"
53 #include "asterisk/pbx.h"
54 #include "asterisk/options.h"
55 #include "asterisk/utils.h"
56
57 static const char tdesc[] = "Network Broadcast Sound Driver";
58
59 /* Only linear is allowed */
60 static int prefformat = AST_FORMAT_SLINEAR;
61
62 static char context[AST_MAX_EXTENSION] = "default";
63 static char type[] = "NBS";
64
65 /* NBS creates private structures on demand */
66    
67 struct nbs_pvt {
68         NBS *nbs;
69         struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
70         char app[16];                                   /* Our app */
71         char stream[80];                                /* Our stream */
72         struct ast_frame fr;                    /* "null" frame */
73         struct ast_module_user *u;              /*! for holding a reference to this module */
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         ast_debug(1, "Calling %s on %s\n", dest, ast->name);
106
107         /* If we can't connect, return congestion */
108         if (nbs_connect(p->nbs)) {
109                 ast_log(LOG_WARNING, "NBS Connection failed on %s\n", ast->name);
110                 ast_queue_control(ast, AST_CONTROL_CONGESTION);
111         } else {
112                 ast_setstate(ast, AST_STATE_RINGING);
113                 ast_queue_control(ast, AST_CONTROL_ANSWER);
114         }
115
116         return 0;
117 }
118
119 static void nbs_destroy(struct nbs_pvt *p)
120 {
121         if (p->nbs)
122                 nbs_delstream(p->nbs);
123         ast_module_user_remove(p->u);
124         ast_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
134         ast_copy_string(stream, data, sizeof(stream));
135         if ((opts = strchr(stream, ':'))) {
136                 *opts = '\0';
137                 opts++;
138         } else
139                 opts = "";
140         p = ast_calloc(1, sizeof(*p));
141         if (p) {
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                 ast_copy_string(p->stream, stream, sizeof(p->stream));
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                         ast_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         ast_debug(1, "nbs_hangup(%s)\n", ast->name);
176         if (!ast->tech_pvt) {
177                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
178                 return 0;
179         }
180         nbs_destroy(p);
181         ast->tech_pvt = NULL;
182         ast_setstate(ast, AST_STATE_DOWN);
183         return 0;
184 }
185
186 static struct ast_frame  *nbs_xread(struct ast_channel *ast)
187 {
188         struct nbs_pvt *p = ast->tech_pvt;
189         
190
191         /* Some nice norms */
192         p->fr.datalen = 0;
193         p->fr.samples = 0;
194         p->fr.data =  NULL;
195         p->fr.src = type;
196         p->fr.offset = 0;
197         p->fr.mallocd=0;
198         p->fr.delivery.tv_sec = 0;
199         p->fr.delivery.tv_usec = 0;
200
201         ast_debug(1, "Returning null frame on %s\n", ast->name);
202
203         return &p->fr;
204 }
205
206 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame)
207 {
208         struct nbs_pvt *p = ast->tech_pvt;
209         /* Write a frame of (presumably voice) data */
210         if (frame->frametype != AST_FRAME_VOICE) {
211                 if (frame->frametype != AST_FRAME_IMAGE)
212                         ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
213                 return 0;
214         }
215         if (!(frame->subclass &
216                 (AST_FORMAT_SLINEAR))) {
217                 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
218                 return 0;
219         }
220         if (ast->_state != AST_STATE_UP) {
221                 /* Don't try tos end audio on-hook */
222                 return 0;
223         }
224         if (nbs_write(p->nbs, frame->data, frame->datalen / 2) < 0) 
225                 return -1;
226         return 0;
227 }
228
229 static struct ast_channel *nbs_new(struct nbs_pvt *i, int state)
230 {
231         struct ast_channel *tmp;
232         tmp = ast_channel_alloc(1, state, 0, 0, "", "s", context, 0, "NBS/%s", i->stream);
233         if (tmp) {
234                 tmp->tech = &nbs_tech;
235                 ast_channel_set_fd(tmp, 0, nbs_fd(i->nbs));
236                 tmp->nativeformats = prefformat;
237                 tmp->rawreadformat = prefformat;
238                 tmp->rawwriteformat = prefformat;
239                 tmp->writeformat = prefformat;
240                 tmp->readformat = prefformat;
241                 if (state == AST_STATE_RING)
242                         tmp->rings = 1;
243                 tmp->tech_pvt = i;
244                 ast_copy_string(tmp->context, context, sizeof(tmp->context));
245                 ast_copy_string(tmp->exten, "s",  sizeof(tmp->exten));
246                 ast_string_field_set(tmp, language, "");
247                 i->owner = tmp;
248                 i->u = ast_module_user_add(tmp);
249                 if (state != AST_STATE_DOWN) {
250                         if (ast_pbx_start(tmp)) {
251                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
252                                 ast_hangup(tmp);
253                         }
254                 }
255         } else
256                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
257         return tmp;
258 }
259
260
261 static struct ast_channel *nbs_request(const char *type, int format, void *data, int *cause)
262 {
263         int oldformat;
264         struct nbs_pvt *p;
265         struct ast_channel *tmp = NULL;
266         
267         oldformat = format;
268         format &= (AST_FORMAT_SLINEAR);
269         if (!format) {
270                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
271                 return NULL;
272         }
273         p = nbs_alloc(data);
274         if (p) {
275                 tmp = nbs_new(p, AST_STATE_DOWN);
276                 if (!tmp)
277                         nbs_destroy(p);
278         }
279         return tmp;
280 }
281
282 static int unload_module(void)
283 {
284         /* First, take us out of the channel loop */
285         ast_channel_unregister(&nbs_tech);
286         return 0;
287 }
288
289 static int load_module(void)
290 {
291         /* Make sure we can register our channel type */
292         if (ast_channel_register(&nbs_tech)) {
293                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
294                 return -1;
295         }
296         return 0;
297 }
298
299 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Network Broadcast Sound Support");