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