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