app_intercom.c 4.5 KB

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