chan_nbs.c 8.0 KB

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