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