chan_nbs.c 7.7 KB

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