app_intercom.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 Use /dev/dsp as an intercom.
  21. *
  22. * \ingroup applications
  23. */
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <errno.h>
  27. #include <sys/ioctl.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <sys/time.h>
  31. #include <netinet/in.h>
  32. #if defined(__linux__)
  33. #include <linux/soundcard.h>
  34. #elif defined(__FreeBSD__)
  35. #include <sys/soundcard.h>
  36. #else
  37. #include <soundcard.h>
  38. #endif
  39. #include "asterisk.h"
  40. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  41. #include "asterisk/lock.h"
  42. #include "asterisk/file.h"
  43. #include "asterisk/frame.h"
  44. #include "asterisk/logger.h"
  45. #include "asterisk/channel.h"
  46. #include "asterisk/pbx.h"
  47. #include "asterisk/module.h"
  48. #include "asterisk/translate.h"
  49. #ifdef __OpenBSD__
  50. #define DEV_DSP "/dev/audio"
  51. #else
  52. #define DEV_DSP "/dev/dsp"
  53. #endif
  54. /* Number of 32 byte buffers -- each buffer is 2 ms */
  55. #define BUFFER_SIZE 32
  56. static char *tdesc = "Intercom using /dev/dsp for output";
  57. static char *app = "Intercom";
  58. static char *synopsis = "(Obsolete) Send to Intercom";
  59. static char *descrip =
  60. " Intercom(): Sends the user to the intercom (i.e. /dev/dsp). This program\n"
  61. "is generally considered obselete by the chan_oss module. User can terminate\n"with a DTMF tone, or by hangup.\n";
  62. STANDARD_LOCAL_USER;
  63. LOCAL_USER_DECL;
  64. AST_MUTEX_DEFINE_STATIC(sound_lock);
  65. static int sound = -1;
  66. static int write_audio(short *data, int len)
  67. {
  68. int res;
  69. struct audio_buf_info info;
  70. ast_mutex_lock(&sound_lock);
  71. if (sound < 0) {
  72. ast_log(LOG_WARNING, "Sound device closed?\n");
  73. ast_mutex_unlock(&sound_lock);
  74. return -1;
  75. }
  76. if (ioctl(sound, SNDCTL_DSP_GETOSPACE, &info)) {
  77. ast_log(LOG_WARNING, "Unable to read output space\n");
  78. ast_mutex_unlock(&sound_lock);
  79. return -1;
  80. }
  81. res = write(sound, data, len);
  82. ast_mutex_unlock(&sound_lock);
  83. return res;
  84. }
  85. static int create_audio(void)
  86. {
  87. int fmt, desired, res, fd;
  88. fd = open(DEV_DSP, O_WRONLY);
  89. if (fd < 0) {
  90. ast_log(LOG_WARNING, "Unable to open %s: %s\n", DEV_DSP, strerror(errno));
  91. close(fd);
  92. return -1;
  93. }
  94. fmt = AFMT_S16_LE;
  95. res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
  96. if (res < 0) {
  97. ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
  98. close(fd);
  99. return -1;
  100. }
  101. fmt = 0;
  102. res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
  103. if (res < 0) {
  104. ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
  105. close(fd);
  106. return -1;
  107. }
  108. /* 8000 Hz desired */
  109. desired = 8000;
  110. fmt = desired;
  111. res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
  112. if (res < 0) {
  113. ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
  114. close(fd);
  115. return -1;
  116. }
  117. if (fmt != desired) {
  118. ast_log(LOG_WARNING, "Requested %d Hz, got %d Hz -- sound may be choppy\n", desired, fmt);
  119. }
  120. #if 1
  121. /* 2 bytes * 15 units of 2^5 = 32 bytes per buffer */
  122. fmt = ((BUFFER_SIZE) << 16) | (0x0005);
  123. res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
  124. if (res < 0) {
  125. ast_log(LOG_WARNING, "Unable to set fragment size -- sound may be choppy\n");
  126. }
  127. #endif
  128. sound = fd;
  129. return 0;
  130. }
  131. static int intercom_exec(struct ast_channel *chan, void *data)
  132. {
  133. int res = 0;
  134. struct localuser *u;
  135. struct ast_frame *f;
  136. int oreadformat;
  137. LOCAL_USER_ADD(u);
  138. /* Remember original read format */
  139. oreadformat = chan->readformat;
  140. /* Set mode to signed linear */
  141. res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
  142. if (res < 0) {
  143. ast_log(LOG_WARNING, "Unable to set format to signed linear on channel %s\n", chan->name);
  144. LOCAL_USER_REMOVE(u);
  145. return -1;
  146. }
  147. /* Read packets from the channel */
  148. while(!res) {
  149. res = ast_waitfor(chan, -1);
  150. if (res > 0) {
  151. res = 0;
  152. f = ast_read(chan);
  153. if (f) {
  154. if (f->frametype == AST_FRAME_DTMF) {
  155. ast_frfree(f);
  156. break;
  157. } else {
  158. if (f->frametype == AST_FRAME_VOICE) {
  159. if (f->subclass == AST_FORMAT_SLINEAR) {
  160. res = write_audio(f->data, f->datalen);
  161. if (res > 0)
  162. res = 0;
  163. } else
  164. ast_log(LOG_DEBUG, "Unable to handle non-signed linear frame (%d)\n", f->subclass);
  165. }
  166. }
  167. ast_frfree(f);
  168. } else
  169. res = -1;
  170. }
  171. }
  172. if (!res)
  173. ast_set_read_format(chan, oreadformat);
  174. LOCAL_USER_REMOVE(u);
  175. return res;
  176. }
  177. int unload_module(void)
  178. {
  179. int res;
  180. if (sound > -1)
  181. close(sound);
  182. res = ast_unregister_application(app);
  183. STANDARD_HANGUP_LOCALUSERS;
  184. return res;
  185. }
  186. int load_module(void)
  187. {
  188. if (create_audio())
  189. return -1;
  190. return ast_register_application(app, intercom_exec, synopsis, descrip);
  191. }
  192. char *description(void)
  193. {
  194. return tdesc;
  195. }
  196. int usecount(void)
  197. {
  198. int res;
  199. STANDARD_USECOUNT(res);
  200. return res;
  201. }
  202. char *key()
  203. {
  204. return ASTERISK_GPL_KEY;
  205. }