36c3435bbc60132758c679e60bbfafdf0a59521a
[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         <support_level>extended</support_level> 
31  ***/
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include <sys/socket.h>
38 #include <sys/time.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/module.h"
48 #include "asterisk/pbx.h"
49 #include "asterisk/utils.h"
50 #include "asterisk/format_cache.h"
51
52 static const char tdesc[] = "Network Broadcast Sound Driver";
53
54 static char context[AST_MAX_EXTENSION] = "default";
55 static const char type[] = "NBS";
56
57 /* NBS creates private structures on demand */
58    
59 struct nbs_pvt {
60         NBS *nbs;
61         struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
62         char app[16];                                   /* Our app */
63         char stream[80];                                /* Our stream */
64         struct ast_module_user *u;              /*! for holding a reference to this module */
65 };
66
67 static struct ast_channel *nbs_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
68 static int nbs_call(struct ast_channel *ast, const char *dest, int timeout);
69 static int nbs_hangup(struct ast_channel *ast);
70 static struct ast_frame *nbs_xread(struct ast_channel *ast);
71 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame);
72
73 static struct ast_channel_tech nbs_tech = {
74         .type = type,
75         .description = tdesc,
76         .requester = nbs_request,
77         .call = nbs_call,
78         .hangup = nbs_hangup,
79         .read = nbs_xread,
80         .write = nbs_xwrite,
81 };
82
83 static int nbs_call(struct ast_channel *ast, const char *dest, int timeout)
84 {
85         struct nbs_pvt *p;
86
87         p = ast_channel_tech_pvt(ast);
88
89         if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
90                 ast_log(LOG_WARNING, "nbs_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
91                 return -1;
92         }
93         /* When we call, it just works, really, there's no destination...  Just
94            ring the phone and wait for someone to answer */
95         ast_debug(1, "Calling %s on %s\n", dest, ast_channel_name(ast));
96
97         /* If we can't connect, return congestion */
98         if (nbs_connect(p->nbs)) {
99                 ast_log(LOG_WARNING, "NBS Connection failed on %s\n", ast_channel_name(ast));
100                 ast_queue_control(ast, AST_CONTROL_CONGESTION);
101         } else {
102                 ast_setstate(ast, AST_STATE_RINGING);
103                 ast_queue_control(ast, AST_CONTROL_ANSWER);
104         }
105
106         return 0;
107 }
108
109 static void nbs_destroy(struct nbs_pvt *p)
110 {
111         if (p->nbs)
112                 nbs_delstream(p->nbs);
113         ast_module_user_remove(p->u);
114         ast_free(p);
115 }
116
117 static struct nbs_pvt *nbs_alloc(const char *data)
118 {
119         struct nbs_pvt *p;
120         int flags = 0;
121         char stream[256];
122         char *opts;
123
124         ast_copy_string(stream, data, sizeof(stream));
125         if ((opts = strchr(stream, ':'))) {
126                 *opts = '\0';
127                 opts++;
128         } else
129                 opts = "";
130         p = ast_calloc(1, sizeof(*p));
131         if (p) {
132                 if (!ast_strlen_zero(opts)) {
133                         if (strchr(opts, 'm'))
134                                 flags |= NBS_FLAG_MUTE;
135                         if (strchr(opts, 'o'))
136                                 flags |= NBS_FLAG_OVERSPEAK;
137                         if (strchr(opts, 'e'))
138                                 flags |= NBS_FLAG_EMERGENCY;
139                         if (strchr(opts, 'O'))
140                                 flags |= NBS_FLAG_OVERRIDE;
141                 } else
142                         flags = NBS_FLAG_OVERSPEAK;
143                 
144                 ast_copy_string(p->stream, stream, sizeof(p->stream));
145                 p->nbs = nbs_newstream("asterisk", stream, flags);
146                 if (!p->nbs) {
147                         ast_log(LOG_WARNING, "Unable to allocate new NBS stream '%s' with flags %d\n", stream, flags);
148                         ast_free(p);
149                         p = NULL;
150                 } else {
151                         /* Set for 8000 hz mono, 640 samples */
152                         nbs_setbitrate(p->nbs, 8000);
153                         nbs_setchannels(p->nbs, 1);
154                         nbs_setblocksize(p->nbs, 640);
155                         nbs_setblocking(p->nbs, 0);
156                 }
157         }
158         return p;
159 }
160
161 static int nbs_hangup(struct ast_channel *ast)
162 {
163         struct nbs_pvt *p;
164         p = ast_channel_tech_pvt(ast);
165         ast_debug(1, "nbs_hangup(%s)\n", ast_channel_name(ast));
166         if (!ast_channel_tech_pvt(ast)) {
167                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
168                 return 0;
169         }
170         nbs_destroy(p);
171         ast_channel_tech_pvt_set(ast, NULL);
172         ast_setstate(ast, AST_STATE_DOWN);
173         return 0;
174 }
175
176 static struct ast_frame  *nbs_xread(struct ast_channel *ast)
177 {
178         ast_debug(1, "Returning null frame on %s\n", ast_channel_name(ast));
179
180         return &ast_null_frame;
181 }
182
183 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame)
184 {
185         struct nbs_pvt *p = ast_channel_tech_pvt(ast);
186         if (ast_channel_state(ast) != AST_STATE_UP) {
187                 /* Don't try tos end audio on-hook */
188                 return 0;
189         }
190         if (nbs_write(p->nbs, frame->data.ptr, frame->datalen / 2) < 0) 
191                 return -1;
192         return 0;
193 }
194
195 static struct ast_channel *nbs_new(struct nbs_pvt *i, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
196 {
197         struct ast_channel *tmp;
198         tmp = ast_channel_alloc(1, state, 0, 0, "", "s", context, assignedids, requestor, 0, "NBS/%s", i->stream);
199         if (tmp) {
200                 ast_channel_tech_set(tmp, &nbs_tech);
201                 ast_channel_set_fd(tmp, 0, nbs_fd(i->nbs));
202
203                 ast_channel_nativeformats_set(tmp, nbs_tech.capabilities);
204                 ast_channel_set_rawreadformat(tmp, ast_format_slin);
205                 ast_channel_set_rawwriteformat(tmp, ast_format_slin);
206                 ast_channel_set_writeformat(tmp, ast_format_slin);
207                 ast_channel_set_readformat(tmp, ast_format_slin);
208                 if (state == AST_STATE_RING)
209                         ast_channel_rings_set(tmp, 1);
210                 ast_channel_tech_pvt_set(tmp, i);
211                 ast_channel_context_set(tmp, context);
212                 ast_channel_exten_set(tmp, "s");
213                 ast_channel_language_set(tmp, "");
214                 i->owner = tmp;
215                 i->u = ast_module_user_add(tmp);
216                 ast_channel_unlock(tmp);
217                 if (state != AST_STATE_DOWN) {
218                         if (ast_pbx_start(tmp)) {
219                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
220                                 ast_hangup(tmp);
221                         }
222                 }
223         } else
224                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
225         return tmp;
226 }
227
228
229 static struct ast_channel *nbs_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
230 {
231         struct nbs_pvt *p;
232         struct ast_channel *tmp = NULL;
233
234         if (ast_format_cap_iscompatible_format(cap, ast_format_slin) == AST_FORMAT_CMP_NOT_EQUAL) {
235                 struct ast_str *cap_buf = ast_str_alloca(64);
236
237                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n",
238                         ast_format_cap_get_names(cap, &cap_buf));
239                 return NULL;
240         }
241         p = nbs_alloc(data);
242         if (p) {
243                 tmp = nbs_new(p, AST_STATE_DOWN, assignedids, requestor);
244                 if (!tmp)
245                         nbs_destroy(p);
246         }
247         return tmp;
248 }
249
250 static int unload_module(void)
251 {
252         /* First, take us out of the channel loop */
253         ast_channel_unregister(&nbs_tech);
254         ao2_ref(nbs_tech.capabilities, -1);
255         nbs_tech.capabilities = NULL;
256         return 0;
257 }
258
259 static int load_module(void)
260 {
261         if (!(nbs_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
262                 return AST_MODULE_LOAD_FAILURE;
263         }
264         ast_format_cap_append(nbs_tech.capabilities, ast_format_slin, 0);
265         /* Make sure we can register our channel type */
266         if (ast_channel_register(&nbs_tech)) {
267                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
268                 return AST_MODULE_LOAD_DECLINE;
269         }
270         return AST_MODULE_LOAD_SUCCESS;
271 }
272
273 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Network Broadcast Sound Support");
274