2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Network broadcast sound support channel driver
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup channel_drivers
34 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
38 #include <sys/socket.h>
43 #include <arpa/inet.h>
45 #include <sys/ioctl.h>
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"
57 static const char tdesc
[] = "Network Broadcast Sound Driver";
59 /* Only linear is allowed */
60 static int prefformat
= AST_FORMAT_SLINEAR
;
62 static char context
[AST_MAX_EXTENSION
] = "default";
63 static char type
[] = "NBS";
65 /* NBS creates private structures on demand */
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 */
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
);
82 static const struct ast_channel_tech nbs_tech
= {
85 .capabilities
= AST_FORMAT_SLINEAR
,
86 .requester
= nbs_request
,
93 static int nbs_call(struct ast_channel
*ast
, char *dest
, int timeout
)
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
);
103 /* When we call, it just works, really, there's no destination... Just
104 ring the phone and wait for someone to answer */
106 ast_log(LOG_DEBUG
, "Calling %s on %s\n", dest
, ast
->name
);
108 /* If we can't connect, return congestion */
109 if (nbs_connect(p
->nbs
)) {
110 ast_log(LOG_WARNING
, "NBS Connection failed on %s\n", ast
->name
);
111 ast_queue_control(ast
, AST_CONTROL_CONGESTION
);
113 ast_setstate(ast
, AST_STATE_RINGING
);
114 ast_queue_control(ast
, AST_CONTROL_ANSWER
);
120 static void nbs_destroy(struct nbs_pvt
*p
)
123 nbs_delstream(p
->nbs
);
124 ast_module_user_remove(p
->u
);
128 static struct nbs_pvt
*nbs_alloc(void *data
)
135 ast_copy_string(stream
, data
, sizeof(stream
));
136 if ((opts
= strchr(stream
, ':'))) {
141 p
= malloc(sizeof(struct nbs_pvt
));
143 memset(p
, 0, sizeof(struct nbs_pvt
));
144 if (!ast_strlen_zero(opts
)) {
145 if (strchr(opts
, 'm'))
146 flags
|= NBS_FLAG_MUTE
;
147 if (strchr(opts
, 'o'))
148 flags
|= NBS_FLAG_OVERSPEAK
;
149 if (strchr(opts
, 'e'))
150 flags
|= NBS_FLAG_EMERGENCY
;
151 if (strchr(opts
, 'O'))
152 flags
|= NBS_FLAG_OVERRIDE
;
154 flags
= NBS_FLAG_OVERSPEAK
;
156 ast_copy_string(p
->stream
, stream
, sizeof(p
->stream
));
157 p
->nbs
= nbs_newstream("asterisk", stream
, flags
);
159 ast_log(LOG_WARNING
, "Unable to allocate new NBS stream '%s' with flags %d\n", stream
, flags
);
163 /* Set for 8000 hz mono, 640 samples */
164 nbs_setbitrate(p
->nbs
, 8000);
165 nbs_setchannels(p
->nbs
, 1);
166 nbs_setblocksize(p
->nbs
, 640);
167 nbs_setblocking(p
->nbs
, 0);
173 static int nbs_hangup(struct ast_channel
*ast
)
178 ast_log(LOG_DEBUG
, "nbs_hangup(%s)\n", ast
->name
);
179 if (!ast
->tech_pvt
) {
180 ast_log(LOG_WARNING
, "Asked to hangup channel not connected\n");
184 ast
->tech_pvt
= NULL
;
185 ast_setstate(ast
, AST_STATE_DOWN
);
189 static struct ast_frame
*nbs_xread(struct ast_channel
*ast
)
191 struct nbs_pvt
*p
= ast
->tech_pvt
;
194 /* Some nice norms */
201 p
->fr
.delivery
.tv_sec
= 0;
202 p
->fr
.delivery
.tv_usec
= 0;
204 ast_log(LOG_DEBUG
, "Returning null frame on %s\n", ast
->name
);
209 static int nbs_xwrite(struct ast_channel
*ast
, struct ast_frame
*frame
)
211 struct nbs_pvt
*p
= ast
->tech_pvt
;
212 /* Write a frame of (presumably voice) data */
213 if (frame
->frametype
!= AST_FRAME_VOICE
) {
214 if (frame
->frametype
!= AST_FRAME_IMAGE
)
215 ast_log(LOG_WARNING
, "Don't know what to do with frame type '%d'\n", frame
->frametype
);
218 if (!(frame
->subclass
&
219 (AST_FORMAT_SLINEAR
))) {
220 ast_log(LOG_WARNING
, "Cannot handle frames in %d format\n", frame
->subclass
);
223 if (ast
->_state
!= AST_STATE_UP
) {
224 /* Don't try tos end audio on-hook */
227 if (nbs_write(p
->nbs
, frame
->data
, frame
->datalen
/ 2) < 0)
232 static struct ast_channel
*nbs_new(struct nbs_pvt
*i
, int state
)
234 struct ast_channel
*tmp
;
235 tmp
= ast_channel_alloc(1, state
, 0, 0, "", "s", context
, 0, "NBS/%s", i
->stream
);
237 tmp
->tech
= &nbs_tech
;
238 tmp
->fds
[0] = nbs_fd(i
->nbs
);
239 tmp
->nativeformats
= prefformat
;
240 tmp
->rawreadformat
= prefformat
;
241 tmp
->rawwriteformat
= prefformat
;
242 tmp
->writeformat
= prefformat
;
243 tmp
->readformat
= prefformat
;
244 if (state
== AST_STATE_RING
)
247 ast_copy_string(tmp
->context
, context
, sizeof(tmp
->context
));
248 ast_copy_string(tmp
->exten
, "s", sizeof(tmp
->exten
));
249 ast_string_field_set(tmp
, language
, "");
251 i
->u
= ast_module_user_add(tmp
);
252 if (state
!= AST_STATE_DOWN
) {
253 if (ast_pbx_start(tmp
)) {
254 ast_log(LOG_WARNING
, "Unable to start PBX on %s\n", tmp
->name
);
259 ast_log(LOG_WARNING
, "Unable to allocate channel structure\n");
264 static struct ast_channel
*nbs_request(const char *type
, int format
, void *data
, int *cause
)
268 struct ast_channel
*tmp
= NULL
;
271 format
&= (AST_FORMAT_SLINEAR
);
273 ast_log(LOG_NOTICE
, "Asked to get a channel of unsupported format '%d'\n", oldformat
);
278 tmp
= nbs_new(p
, AST_STATE_DOWN
);
285 static int unload_module(void)
287 /* First, take us out of the channel loop */
288 ast_channel_unregister(&nbs_tech
);
292 static int load_module(void)
294 /* Make sure we can register our channel type */
295 if (ast_channel_register(&nbs_tech
)) {
296 ast_log(LOG_ERROR
, "Unable to register channel class %s\n", type
);
302 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Network Broadcast Sound Support");