chan_alsa.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Copyright (C) 2002, Linux Support Services
  5. *
  6. * By Matthew Fredrickson <creslin@linux-support.net>
  7. *
  8. * This program is free software, distributed under the terms of
  9. * the GNU General Public License
  10. */
  11. #include <asterisk/frame.h>
  12. #include <asterisk/logger.h>
  13. #include <asterisk/channel.h>
  14. #include <asterisk/module.h>
  15. #include <asterisk/channel_pvt.h>
  16. #include <asterisk/options.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/config.h>
  19. #include <asterisk/cli.h>
  20. #include <asterisk/utils.h>
  21. #include <unistd.h>
  22. #include <fcntl.h>
  23. #include <errno.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/time.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include "asterisk/endian.h"
  30. #define ALSA_PCM_NEW_HW_PARAMS_API
  31. #define ALSA_PCM_NEW_SW_PARAMS_API
  32. #include <alsa/asoundlib.h>
  33. #include "busy.h"
  34. #include "ringtone.h"
  35. #include "ring10.h"
  36. #include "answer.h"
  37. #ifdef ALSA_MONITOR
  38. #include "alsa-monitor.h"
  39. #endif
  40. #define DEBUG 0
  41. /* Which device to use */
  42. #define ALSA_INDEV "default"
  43. #define ALSA_OUTDEV "default"
  44. #define DESIRED_RATE 8000
  45. /* Lets use 160 sample frames, just like GSM. */
  46. #define FRAME_SIZE 160
  47. #define PERIOD_FRAMES 80 /* 80 Frames, at 2 bytes each */
  48. /* When you set the frame size, you have to come up with
  49. the right buffer format as well. */
  50. /* 5 64-byte frames = one frame */
  51. #define BUFFER_FMT ((buffersize * 10) << 16) | (0x0006);
  52. /* Don't switch between read/write modes faster than every 300 ms */
  53. #define MIN_SWITCH_TIME 600
  54. #if __BYTE_ORDER == __LITTLE_ENDIAN
  55. static snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
  56. #else
  57. static snd_pcm_format_t format = SND_PCM_FORMAT_S16_BE;
  58. #endif
  59. /* static int block = O_NONBLOCK; */
  60. static char indevname[50] = ALSA_INDEV;
  61. static char outdevname[50] = ALSA_OUTDEV;
  62. #if 0
  63. static struct timeval lasttime;
  64. #endif
  65. static int usecnt;
  66. static int silencesuppression = 0;
  67. static int silencethreshold = 1000;
  68. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  69. AST_MUTEX_DEFINE_STATIC(alsalock);
  70. static char *type = "Console";
  71. static char *desc = "ALSA Console Channel Driver";
  72. static char *tdesc = "ALSA Console Channel Driver";
  73. static char *config = "alsa.conf";
  74. static char context[AST_MAX_EXTENSION] = "default";
  75. static char language[MAX_LANGUAGE] = "";
  76. static char exten[AST_MAX_EXTENSION] = "s";
  77. static int hookstate=0;
  78. static short silence[FRAME_SIZE] = {0, };
  79. struct sound {
  80. int ind;
  81. short *data;
  82. int datalen;
  83. int samplen;
  84. int silencelen;
  85. int repeat;
  86. };
  87. static struct sound sounds[] = {
  88. { AST_CONTROL_RINGING, ringtone, sizeof(ringtone)/2, 16000, 32000, 1 },
  89. { AST_CONTROL_BUSY, busy, sizeof(busy)/2, 4000, 4000, 1 },
  90. { AST_CONTROL_CONGESTION, busy, sizeof(busy)/2, 2000, 2000, 1 },
  91. { AST_CONTROL_RING, ring10, sizeof(ring10)/2, 16000, 32000, 1 },
  92. { AST_CONTROL_ANSWER, answer, sizeof(answer)/2, 2200, 0, 0 },
  93. };
  94. /* Sound command pipe */
  95. static int sndcmd[2];
  96. static struct chan_alsa_pvt {
  97. /* We only have one ALSA structure -- near sighted perhaps, but it
  98. keeps this driver as simple as possible -- as it should be. */
  99. struct ast_channel *owner;
  100. char exten[AST_MAX_EXTENSION];
  101. char context[AST_MAX_EXTENSION];
  102. #if 0
  103. snd_pcm_t *card;
  104. #endif
  105. snd_pcm_t *icard, *ocard;
  106. } alsa;
  107. /* Number of buffers... Each is FRAMESIZE/8 ms long. For example
  108. with 160 sample frames, and a buffer size of 3, we have a 60ms buffer,
  109. usually plenty. */
  110. pthread_t sthread;
  111. #define MAX_BUFFER_SIZE 100
  112. /* File descriptors for sound device */
  113. static int readdev = -1;
  114. static int writedev = -1;
  115. static int autoanswer = 1;
  116. static int cursound = -1;
  117. static int sampsent = 0;
  118. static int silencelen=0;
  119. static int offset=0;
  120. static int nosound=0;
  121. static int send_sound(void)
  122. {
  123. short myframe[FRAME_SIZE];
  124. int total = FRAME_SIZE;
  125. short *frame = NULL;
  126. int amt=0;
  127. int res;
  128. int myoff;
  129. snd_pcm_state_t state;
  130. if (cursound > -1) {
  131. res = total;
  132. if (sampsent < sounds[cursound].samplen) {
  133. myoff=0;
  134. while(total) {
  135. amt = total;
  136. if (amt > (sounds[cursound].datalen - offset))
  137. amt = sounds[cursound].datalen - offset;
  138. memcpy(myframe + myoff, sounds[cursound].data + offset, amt * 2);
  139. total -= amt;
  140. offset += amt;
  141. sampsent += amt;
  142. myoff += amt;
  143. if (offset >= sounds[cursound].datalen)
  144. offset = 0;
  145. }
  146. /* Set it up for silence */
  147. if (sampsent >= sounds[cursound].samplen)
  148. silencelen = sounds[cursound].silencelen;
  149. frame = myframe;
  150. } else {
  151. if (silencelen > 0) {
  152. frame = silence;
  153. silencelen -= res;
  154. } else {
  155. if (sounds[cursound].repeat) {
  156. /* Start over */
  157. sampsent = 0;
  158. offset = 0;
  159. } else {
  160. cursound = -1;
  161. nosound = 0;
  162. }
  163. return 0;
  164. }
  165. }
  166. if (res == 0 || !frame) {
  167. return 0;
  168. }
  169. #ifdef ALSA_MONITOR
  170. alsa_monitor_write((char *)frame, res * 2);
  171. #endif
  172. state = snd_pcm_state(alsa.ocard);
  173. if (state == SND_PCM_STATE_XRUN) {
  174. snd_pcm_prepare(alsa.ocard);
  175. }
  176. res = snd_pcm_writei(alsa.ocard, frame, res);
  177. if (res > 0)
  178. return 0;
  179. return 0;
  180. }
  181. return 0;
  182. }
  183. static void *sound_thread(void *unused)
  184. {
  185. fd_set rfds;
  186. fd_set wfds;
  187. int max;
  188. int res;
  189. for(;;) {
  190. FD_ZERO(&rfds);
  191. FD_ZERO(&wfds);
  192. max = sndcmd[0];
  193. FD_SET(sndcmd[0], &rfds);
  194. if (cursound > -1) {
  195. FD_SET(writedev, &wfds);
  196. if (writedev > max)
  197. max = writedev;
  198. }
  199. #ifdef ALSA_MONITOR
  200. if (!alsa.owner) {
  201. FD_SET(readdev, &rfds);
  202. if (readdev > max)
  203. max = readdev;
  204. }
  205. #endif
  206. res = ast_select(max + 1, &rfds, &wfds, NULL, NULL);
  207. if (res < 1) {
  208. ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
  209. continue;
  210. }
  211. #ifdef ALSA_MONITOR
  212. if (FD_ISSET(readdev, &rfds)) {
  213. /* Keep the pipe going with read audio */
  214. snd_pcm_state_t state;
  215. short buf[FRAME_SIZE];
  216. int r;
  217. state = snd_pcm_state(alsa.ocard);
  218. if (state == SND_PCM_STATE_XRUN) {
  219. snd_pcm_prepare(alsa.ocard);
  220. }
  221. r = snd_pcm_readi(alsa.icard, buf, FRAME_SIZE);
  222. if (r == -EPIPE) {
  223. #if DEBUG
  224. ast_log(LOG_ERROR, "XRUN read\n");
  225. #endif
  226. snd_pcm_prepare(alsa.icard);
  227. } else if (r == -ESTRPIPE) {
  228. ast_log(LOG_ERROR, "-ESTRPIPE\n");
  229. snd_pcm_prepare(alsa.icard);
  230. } else if (r < 0) {
  231. ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
  232. } else
  233. alsa_monitor_read((char *)buf, r * 2);
  234. }
  235. #endif
  236. if (FD_ISSET(sndcmd[0], &rfds)) {
  237. read(sndcmd[0], &cursound, sizeof(cursound));
  238. silencelen = 0;
  239. offset = 0;
  240. sampsent = 0;
  241. }
  242. if (FD_ISSET(writedev, &wfds))
  243. if (send_sound())
  244. ast_log(LOG_WARNING, "Failed to write sound\n");
  245. }
  246. /* Never reached */
  247. return NULL;
  248. }
  249. static snd_pcm_t *alsa_card_init(char *dev, snd_pcm_stream_t stream)
  250. {
  251. int err;
  252. int direction;
  253. snd_pcm_t *handle = NULL;
  254. snd_pcm_hw_params_t *hwparams = NULL;
  255. snd_pcm_sw_params_t *swparams = NULL;
  256. struct pollfd pfd;
  257. snd_pcm_uframes_t period_size = PERIOD_FRAMES * 4;
  258. //int period_bytes = 0;
  259. snd_pcm_uframes_t buffer_size = 0;
  260. unsigned int rate = DESIRED_RATE;
  261. #if 0
  262. unsigned int per_min = 1;
  263. #endif
  264. //unsigned int per_max = 8;
  265. snd_pcm_uframes_t start_threshold, stop_threshold;
  266. err = snd_pcm_open(&handle, dev, stream, O_NONBLOCK);
  267. if (err < 0) {
  268. ast_log(LOG_ERROR, "snd_pcm_open failed: %s\n", snd_strerror(err));
  269. return NULL;
  270. } else {
  271. ast_log(LOG_DEBUG, "Opening device %s in %s mode\n", dev, (stream == SND_PCM_STREAM_CAPTURE) ? "read" : "write");
  272. }
  273. snd_pcm_hw_params_alloca(&hwparams);
  274. snd_pcm_hw_params_any(handle, hwparams);
  275. err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
  276. if (err < 0) {
  277. ast_log(LOG_ERROR, "set_access failed: %s\n", snd_strerror(err));
  278. }
  279. err = snd_pcm_hw_params_set_format(handle, hwparams, format);
  280. if (err < 0) {
  281. ast_log(LOG_ERROR, "set_format failed: %s\n", snd_strerror(err));
  282. }
  283. err = snd_pcm_hw_params_set_channels(handle, hwparams, 1);
  284. if (err < 0) {
  285. ast_log(LOG_ERROR, "set_channels failed: %s\n", snd_strerror(err));
  286. }
  287. direction = 0;
  288. err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &rate, &direction);
  289. if (rate != DESIRED_RATE) {
  290. ast_log(LOG_WARNING, "Rate not correct, requested %d, got %d\n", DESIRED_RATE, rate);
  291. }
  292. direction = 0;
  293. err = snd_pcm_hw_params_set_period_size_near(handle, hwparams, &period_size, &direction);
  294. if (err < 0) {
  295. ast_log(LOG_ERROR, "period_size(%ld frames) is bad: %s\n", period_size, snd_strerror(err));
  296. } else {
  297. ast_log(LOG_DEBUG, "Period size is %d\n", err);
  298. }
  299. buffer_size = 4096 * 2; //period_size * 16;
  300. err = snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &buffer_size);
  301. if (err < 0) {
  302. ast_log(LOG_WARNING, "Problem setting buffer size of %ld: %s\n", buffer_size, snd_strerror(err));
  303. } else {
  304. ast_log(LOG_DEBUG, "Buffer size is set to %d frames\n", err);
  305. }
  306. #if 0
  307. direction = 0;
  308. err = snd_pcm_hw_params_set_periods_min(handle, hwparams, &per_min, &direction);
  309. if (err < 0) {
  310. ast_log(LOG_ERROR, "periods_min: %s\n", snd_strerror(err));
  311. }
  312. err = snd_pcm_hw_params_set_periods_max(handle, hwparams, &per_max, 0);
  313. if (err < 0) {
  314. ast_log(LOG_ERROR, "periods_max: %s\n", snd_strerror(err));
  315. }
  316. #endif
  317. err = snd_pcm_hw_params(handle, hwparams);
  318. if (err < 0) {
  319. ast_log(LOG_ERROR, "Couldn't set the new hw params: %s\n", snd_strerror(err));
  320. }
  321. snd_pcm_sw_params_alloca(&swparams);
  322. snd_pcm_sw_params_current(handle, swparams);
  323. #if 1
  324. if (stream == SND_PCM_STREAM_PLAYBACK) {
  325. start_threshold = period_size;
  326. } else {
  327. start_threshold = 1;
  328. }
  329. err = snd_pcm_sw_params_set_start_threshold(handle, swparams, start_threshold);
  330. if (err < 0) {
  331. ast_log(LOG_ERROR, "start threshold: %s\n", snd_strerror(err));
  332. }
  333. #endif
  334. #if 1
  335. if (stream == SND_PCM_STREAM_PLAYBACK) {
  336. stop_threshold = buffer_size;
  337. } else {
  338. stop_threshold = buffer_size;
  339. }
  340. err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, stop_threshold);
  341. if (err < 0) {
  342. ast_log(LOG_ERROR, "stop threshold: %s\n", snd_strerror(err));
  343. }
  344. #endif
  345. #if 0
  346. err = snd_pcm_sw_params_set_xfer_align(handle, swparams, PERIOD_FRAMES);
  347. if (err < 0) {
  348. ast_log(LOG_ERROR, "Unable to set xfer alignment: %s\n", snd_strerror(err));
  349. }
  350. #endif
  351. #if 0
  352. err = snd_pcm_sw_params_set_silence_threshold(handle, swparams, silencethreshold);
  353. if (err < 0) {
  354. ast_log(LOG_ERROR, "Unable to set silence threshold: %s\n", snd_strerror(err));
  355. }
  356. #endif
  357. err = snd_pcm_sw_params(handle, swparams);
  358. if (err < 0) {
  359. ast_log(LOG_ERROR, "sw_params: %s\n", snd_strerror(err));
  360. }
  361. err = snd_pcm_poll_descriptors_count(handle);
  362. if (err <= 0) {
  363. ast_log(LOG_ERROR, "Unable to get a poll descriptors count, error is %s\n", snd_strerror(err));
  364. }
  365. if (err != 1) {
  366. ast_log(LOG_DEBUG, "Can't handle more than one device\n");
  367. }
  368. snd_pcm_poll_descriptors(handle, &pfd, err);
  369. ast_log(LOG_DEBUG, "Acquired fd %d from the poll descriptor\n", pfd.fd);
  370. if (stream == SND_PCM_STREAM_CAPTURE)
  371. readdev = pfd.fd;
  372. else
  373. writedev = pfd.fd;
  374. return handle;
  375. }
  376. static int soundcard_init(void)
  377. {
  378. alsa.icard = alsa_card_init(indevname, SND_PCM_STREAM_CAPTURE);
  379. alsa.ocard = alsa_card_init(outdevname, SND_PCM_STREAM_PLAYBACK);
  380. if (!alsa.icard || !alsa.ocard) {
  381. ast_log(LOG_ERROR, "Problem opening alsa I/O devices\n");
  382. return -1;
  383. }
  384. return readdev;
  385. }
  386. static int alsa_digit(struct ast_channel *c, char digit)
  387. {
  388. ast_mutex_lock(&alsalock);
  389. ast_verbose( " << Console Received digit %c >> \n", digit);
  390. ast_mutex_unlock(&alsalock);
  391. return 0;
  392. }
  393. static int alsa_text(struct ast_channel *c, char *text)
  394. {
  395. ast_mutex_lock(&alsalock);
  396. ast_verbose( " << Console Received text %s >> \n", text);
  397. ast_mutex_unlock(&alsalock);
  398. return 0;
  399. }
  400. static void grab_owner(void)
  401. {
  402. while(alsa.owner && ast_mutex_trylock(&alsa.owner->lock)) {
  403. ast_mutex_unlock(&alsalock);
  404. usleep(1);
  405. ast_mutex_lock(&alsalock);
  406. }
  407. }
  408. static int alsa_call(struct ast_channel *c, char *dest, int timeout)
  409. {
  410. int res = 3;
  411. struct ast_frame f = { AST_FRAME_CONTROL };
  412. ast_mutex_lock(&alsalock);
  413. ast_verbose( " << Call placed to '%s' on console >> \n", dest);
  414. if (autoanswer) {
  415. ast_verbose( " << Auto-answered >> \n" );
  416. grab_owner();
  417. if (alsa.owner) {
  418. f.subclass = AST_CONTROL_ANSWER;
  419. ast_queue_frame(alsa.owner, &f);
  420. ast_mutex_unlock(&alsa.owner->lock);
  421. }
  422. } else {
  423. ast_verbose( " << Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
  424. grab_owner();
  425. if (alsa.owner) {
  426. f.subclass = AST_CONTROL_RINGING;
  427. ast_queue_frame(alsa.owner, &f);
  428. ast_mutex_unlock(&alsa.owner->lock);
  429. }
  430. write(sndcmd[1], &res, sizeof(res));
  431. }
  432. snd_pcm_prepare(alsa.icard);
  433. snd_pcm_start(alsa.icard);
  434. ast_mutex_unlock(&alsalock);
  435. return 0;
  436. }
  437. static void answer_sound(void)
  438. {
  439. int res;
  440. nosound = 1;
  441. res = 4;
  442. write(sndcmd[1], &res, sizeof(res));
  443. }
  444. static int alsa_answer(struct ast_channel *c)
  445. {
  446. ast_mutex_lock(&alsalock);
  447. ast_verbose( " << Console call has been answered >> \n");
  448. answer_sound();
  449. ast_setstate(c, AST_STATE_UP);
  450. cursound = -1;
  451. snd_pcm_prepare(alsa.icard);
  452. snd_pcm_start(alsa.icard);
  453. ast_mutex_unlock(&alsalock);
  454. return 0;
  455. }
  456. static int alsa_hangup(struct ast_channel *c)
  457. {
  458. int res;
  459. ast_mutex_lock(&alsalock);
  460. cursound = -1;
  461. c->pvt->pvt = NULL;
  462. alsa.owner = NULL;
  463. ast_verbose( " << Hangup on console >> \n");
  464. ast_mutex_lock(&usecnt_lock);
  465. usecnt--;
  466. ast_mutex_unlock(&usecnt_lock);
  467. if (hookstate) {
  468. res = 2;
  469. write(sndcmd[1], &res, sizeof(res));
  470. }
  471. snd_pcm_drop(alsa.icard);
  472. ast_mutex_unlock(&alsalock);
  473. return 0;
  474. }
  475. static int alsa_write(struct ast_channel *chan, struct ast_frame *f)
  476. {
  477. static char sizbuf[8000];
  478. static int sizpos = 0;
  479. int len = sizpos;
  480. int pos;
  481. int res = 0;
  482. //size_t frames = 0;
  483. snd_pcm_state_t state;
  484. /* Immediately return if no sound is enabled */
  485. if (nosound)
  486. return 0;
  487. ast_mutex_lock(&alsalock);
  488. /* Stop any currently playing sound */
  489. if (cursound != -1) {
  490. snd_pcm_drop(alsa.ocard);
  491. snd_pcm_prepare(alsa.ocard);
  492. cursound = -1;
  493. }
  494. /* We have to digest the frame in 160-byte portions */
  495. if (f->datalen > sizeof(sizbuf) - sizpos) {
  496. ast_log(LOG_WARNING, "Frame too large\n");
  497. res = -1;
  498. } else {
  499. memcpy(sizbuf + sizpos, f->data, f->datalen);
  500. len += f->datalen;
  501. pos = 0;
  502. #ifdef ALSA_MONITOR
  503. alsa_monitor_write(sizbuf, len);
  504. #endif
  505. state = snd_pcm_state(alsa.ocard);
  506. if (state == SND_PCM_STATE_XRUN) {
  507. snd_pcm_prepare(alsa.ocard);
  508. }
  509. res = snd_pcm_writei(alsa.ocard, sizbuf, len/2);
  510. if (res == -EPIPE) {
  511. #if DEBUG
  512. ast_log(LOG_DEBUG, "XRUN write\n");
  513. #endif
  514. snd_pcm_prepare(alsa.ocard);
  515. res = snd_pcm_writei(alsa.ocard, sizbuf, len/2);
  516. if (res != len/2) {
  517. ast_log(LOG_ERROR, "Write error: %s\n", snd_strerror(res));
  518. res = -1;
  519. } else if (res < 0) {
  520. ast_log(LOG_ERROR, "Write error %s\n", snd_strerror(res));
  521. res = -1;
  522. }
  523. } else {
  524. if (res == -ESTRPIPE) {
  525. ast_log(LOG_ERROR, "You've got some big problems\n");
  526. }
  527. if (res > 0)
  528. res = 0;
  529. }
  530. }
  531. ast_mutex_unlock(&alsalock);
  532. return res;
  533. }
  534. static struct ast_frame *alsa_read(struct ast_channel *chan)
  535. {
  536. static struct ast_frame f;
  537. static short __buf[FRAME_SIZE + AST_FRIENDLY_OFFSET/2];
  538. short *buf;
  539. static int readpos = 0;
  540. static int left = FRAME_SIZE;
  541. snd_pcm_state_t state;
  542. int r = 0;
  543. int off = 0;
  544. ast_mutex_lock(&alsalock);
  545. /* Acknowledge any pending cmd */
  546. f.frametype = AST_FRAME_NULL;
  547. f.subclass = 0;
  548. f.samples = 0;
  549. f.datalen = 0;
  550. f.data = NULL;
  551. f.offset = 0;
  552. f.src = type;
  553. f.mallocd = 0;
  554. f.delivery.tv_sec = 0;
  555. f.delivery.tv_usec = 0;
  556. state = snd_pcm_state(alsa.icard);
  557. if ((state != SND_PCM_STATE_PREPARED) &&
  558. (state != SND_PCM_STATE_RUNNING)) {
  559. snd_pcm_prepare(alsa.icard);
  560. }
  561. buf = __buf + AST_FRIENDLY_OFFSET/2;
  562. r = snd_pcm_readi(alsa.icard, buf + readpos, left);
  563. if (r == -EPIPE) {
  564. #if DEBUG
  565. ast_log(LOG_ERROR, "XRUN read\n");
  566. #endif
  567. snd_pcm_prepare(alsa.icard);
  568. } else if (r == -ESTRPIPE) {
  569. ast_log(LOG_ERROR, "-ESTRPIPE\n");
  570. snd_pcm_prepare(alsa.icard);
  571. } else if (r < 0) {
  572. ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
  573. } else if (r >= 0) {
  574. off -= r;
  575. }
  576. /* Update positions */
  577. readpos += r;
  578. left -= r;
  579. if (readpos >= FRAME_SIZE) {
  580. /* A real frame */
  581. readpos = 0;
  582. left = FRAME_SIZE;
  583. if (chan->_state != AST_STATE_UP) {
  584. /* Don't transmit unless it's up */
  585. ast_mutex_unlock(&alsalock);
  586. return &f;
  587. }
  588. f.frametype = AST_FRAME_VOICE;
  589. f.subclass = AST_FORMAT_SLINEAR;
  590. f.samples = FRAME_SIZE;
  591. f.datalen = FRAME_SIZE * 2;
  592. f.data = buf;
  593. f.offset = AST_FRIENDLY_OFFSET;
  594. f.src = type;
  595. f.mallocd = 0;
  596. #ifdef ALSA_MONITOR
  597. alsa_monitor_read((char *)buf, FRAME_SIZE * 2);
  598. #endif
  599. }
  600. ast_mutex_unlock(&alsalock);
  601. return &f;
  602. }
  603. static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  604. {
  605. struct chan_alsa_pvt *p = newchan->pvt->pvt;
  606. ast_mutex_lock(&alsalock);
  607. p->owner = newchan;
  608. ast_mutex_unlock(&alsalock);
  609. return 0;
  610. }
  611. static int alsa_indicate(struct ast_channel *chan, int cond)
  612. {
  613. int res = 0;
  614. ast_mutex_lock(&alsalock);
  615. switch(cond) {
  616. case AST_CONTROL_BUSY:
  617. res = 1;
  618. break;
  619. case AST_CONTROL_CONGESTION:
  620. res = 2;
  621. break;
  622. case AST_CONTROL_RINGING:
  623. res = 0;
  624. break;
  625. case -1:
  626. res = -1;
  627. break;
  628. default:
  629. ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, chan->name);
  630. res = -1;
  631. }
  632. if (res > -1) {
  633. write(sndcmd[1], &res, sizeof(res));
  634. }
  635. ast_mutex_unlock(&alsalock);
  636. return res;
  637. }
  638. static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state)
  639. {
  640. struct ast_channel *tmp;
  641. tmp = ast_channel_alloc(1);
  642. if (tmp) {
  643. snprintf(tmp->name, sizeof(tmp->name), "ALSA/%s", indevname);
  644. tmp->type = type;
  645. tmp->fds[0] = readdev;
  646. tmp->nativeformats = AST_FORMAT_SLINEAR;
  647. tmp->pvt->pvt = p;
  648. tmp->pvt->send_digit = alsa_digit;
  649. tmp->pvt->send_text = alsa_text;
  650. tmp->pvt->hangup = alsa_hangup;
  651. tmp->pvt->answer = alsa_answer;
  652. tmp->pvt->read = alsa_read;
  653. tmp->pvt->call = alsa_call;
  654. tmp->pvt->write = alsa_write;
  655. tmp->pvt->indicate = alsa_indicate;
  656. tmp->pvt->fixup = alsa_fixup;
  657. if (strlen(p->context))
  658. strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
  659. if (strlen(p->exten))
  660. strncpy(tmp->exten, p->exten, sizeof(tmp->exten)-1);
  661. if (strlen(language))
  662. strncpy(tmp->language, language, sizeof(tmp->language)-1);
  663. p->owner = tmp;
  664. ast_setstate(tmp, state);
  665. ast_mutex_lock(&usecnt_lock);
  666. usecnt++;
  667. ast_mutex_unlock(&usecnt_lock);
  668. ast_update_use_count();
  669. if (state != AST_STATE_DOWN) {
  670. if (ast_pbx_start(tmp)) {
  671. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
  672. ast_hangup(tmp);
  673. tmp = NULL;
  674. }
  675. }
  676. }
  677. return tmp;
  678. }
  679. static struct ast_channel *alsa_request(char *type, int format, void *data)
  680. {
  681. int oldformat = format;
  682. struct ast_channel *tmp=NULL;
  683. format &= AST_FORMAT_SLINEAR;
  684. if (!format) {
  685. ast_log(LOG_NOTICE, "Asked to get a channel of format '%d'\n", oldformat);
  686. return NULL;
  687. }
  688. ast_mutex_lock(&alsalock);
  689. if (alsa.owner) {
  690. ast_log(LOG_NOTICE, "Already have a call on the ALSA channel\n");
  691. } else {
  692. tmp= alsa_new(&alsa, AST_STATE_DOWN);
  693. if (!tmp) {
  694. ast_log(LOG_WARNING, "Unable to create new ALSA channel\n");
  695. }
  696. }
  697. ast_mutex_unlock(&alsalock);
  698. return tmp;
  699. }
  700. static int console_autoanswer(int fd, int argc, char *argv[])
  701. {
  702. int res = RESULT_SUCCESS;;
  703. if ((argc != 1) && (argc != 2))
  704. return RESULT_SHOWUSAGE;
  705. ast_mutex_lock(&alsalock);
  706. if (argc == 1) {
  707. ast_cli(fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
  708. } else {
  709. if (!strcasecmp(argv[1], "on"))
  710. autoanswer = -1;
  711. else if (!strcasecmp(argv[1], "off"))
  712. autoanswer = 0;
  713. else
  714. res = RESULT_SHOWUSAGE;
  715. }
  716. ast_mutex_unlock(&alsalock);
  717. return res;
  718. }
  719. static char *autoanswer_complete(char *line, char *word, int pos, int state)
  720. {
  721. #ifndef MIN
  722. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  723. #endif
  724. switch(state) {
  725. case 0:
  726. if (strlen(word) && !strncasecmp(word, "on", MIN(strlen(word), 2)))
  727. return strdup("on");
  728. case 1:
  729. if (strlen(word) && !strncasecmp(word, "off", MIN(strlen(word), 3)))
  730. return strdup("off");
  731. default:
  732. return NULL;
  733. }
  734. return NULL;
  735. }
  736. static char autoanswer_usage[] =
  737. "Usage: autoanswer [on|off]\n"
  738. " Enables or disables autoanswer feature. If used without\n"
  739. " argument, displays the current on/off status of autoanswer.\n"
  740. " The default value of autoanswer is in 'alsa.conf'.\n";
  741. static int console_answer(int fd, int argc, char *argv[])
  742. {
  743. int res = RESULT_SUCCESS;
  744. if (argc != 1)
  745. return RESULT_SHOWUSAGE;
  746. ast_mutex_lock(&alsalock);
  747. if (!alsa.owner) {
  748. ast_cli(fd, "No one is calling us\n");
  749. res = RESULT_FAILURE;
  750. } else {
  751. hookstate = 1;
  752. cursound = -1;
  753. grab_owner();
  754. if (alsa.owner) {
  755. struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
  756. ast_queue_frame(alsa.owner, &f);
  757. ast_mutex_unlock(&alsa.owner->lock);
  758. }
  759. answer_sound();
  760. }
  761. snd_pcm_prepare(alsa.icard);
  762. snd_pcm_start(alsa.icard);
  763. ast_mutex_unlock(&alsalock);
  764. return RESULT_SUCCESS;
  765. }
  766. static char sendtext_usage[] =
  767. "Usage: send text <message>\n"
  768. " Sends a text message for display on the remote terminal.\n";
  769. static int console_sendtext(int fd, int argc, char *argv[])
  770. {
  771. int tmparg = 2;
  772. int res = RESULT_SUCCESS;
  773. if (argc < 2)
  774. return RESULT_SHOWUSAGE;
  775. ast_mutex_lock(&alsalock);
  776. if (!alsa.owner) {
  777. ast_cli(fd, "No one is calling us\n");
  778. res = RESULT_FAILURE;
  779. } else {
  780. struct ast_frame f = { AST_FRAME_TEXT, 0 };
  781. char text2send[256] = "";
  782. text2send[0] = '\0';
  783. while(tmparg < argc) {
  784. strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send) - 1);
  785. strncat(text2send, " ", sizeof(text2send) - strlen(text2send) - 1);
  786. }
  787. text2send[strlen(text2send) - 1] = '\n';
  788. f.data = text2send;
  789. f.datalen = strlen(text2send) + 1;
  790. grab_owner();
  791. if (alsa.owner) {
  792. ast_queue_frame(alsa.owner, &f);
  793. f.frametype = AST_FRAME_CONTROL;
  794. f.subclass = AST_CONTROL_ANSWER;
  795. f.data = NULL;
  796. f.datalen = 0;
  797. ast_queue_frame(alsa.owner, &f);
  798. ast_mutex_unlock(&alsa.owner->lock);
  799. }
  800. }
  801. ast_mutex_unlock(&alsalock);
  802. return res;
  803. }
  804. static char answer_usage[] =
  805. "Usage: answer\n"
  806. " Answers an incoming call on the console (ALSA) channel.\n";
  807. static int console_hangup(int fd, int argc, char *argv[])
  808. {
  809. int res = RESULT_SUCCESS;
  810. if (argc != 1)
  811. return RESULT_SHOWUSAGE;
  812. cursound = -1;
  813. ast_mutex_lock(&alsalock);
  814. if (!alsa.owner && !hookstate) {
  815. ast_cli(fd, "No call to hangup up\n");
  816. res = RESULT_FAILURE;
  817. } else {
  818. hookstate = 0;
  819. grab_owner();
  820. if (alsa.owner) {
  821. ast_queue_hangup(alsa.owner);
  822. ast_mutex_unlock(&alsa.owner->lock);
  823. }
  824. }
  825. ast_mutex_unlock(&alsalock);
  826. return res;
  827. }
  828. static char hangup_usage[] =
  829. "Usage: hangup\n"
  830. " Hangs up any call currently placed on the console.\n";
  831. static int console_dial(int fd, int argc, char *argv[])
  832. {
  833. char tmp[256], *tmp2;
  834. char *mye, *myc;
  835. char *d;
  836. int res = RESULT_SUCCESS;
  837. if ((argc != 1) && (argc != 2))
  838. return RESULT_SHOWUSAGE;
  839. ast_mutex_lock(&alsalock);
  840. if (alsa.owner) {
  841. if (argc == 2) {
  842. d = argv[1];
  843. grab_owner();
  844. if (alsa.owner) {
  845. struct ast_frame f = { AST_FRAME_DTMF };
  846. while(*d) {
  847. f.subclass = *d;
  848. ast_queue_frame(alsa.owner, &f);
  849. d++;
  850. }
  851. ast_mutex_unlock(&alsa.owner->lock);
  852. }
  853. } else {
  854. ast_cli(fd, "You're already in a call. You can use this only to dial digits until you hangup\n");
  855. res = RESULT_FAILURE;
  856. }
  857. } else {
  858. mye = exten;
  859. myc = context;
  860. if (argc == 2) {
  861. char *stringp=NULL;
  862. strncpy(tmp, argv[1], sizeof(tmp)-1);
  863. stringp=tmp;
  864. strsep(&stringp, "@");
  865. tmp2 = strsep(&stringp, "@");
  866. if (strlen(tmp))
  867. mye = tmp;
  868. if (tmp2 && strlen(tmp2))
  869. myc = tmp2;
  870. }
  871. if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
  872. strncpy(alsa.exten, mye, sizeof(alsa.exten)-1);
  873. strncpy(alsa.context, myc, sizeof(alsa.context)-1);
  874. hookstate = 1;
  875. alsa_new(&alsa, AST_STATE_RINGING);
  876. } else
  877. ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
  878. }
  879. ast_mutex_unlock(&alsalock);
  880. return res;
  881. }
  882. static char dial_usage[] =
  883. "Usage: dial [extension[@context]]\n"
  884. " Dials a given extension (and context if specified)\n";
  885. static struct ast_cli_entry myclis[] = {
  886. { { "answer", NULL }, console_answer, "Answer an incoming console call", answer_usage },
  887. { { "hangup", NULL }, console_hangup, "Hangup a call on the console", hangup_usage },
  888. { { "dial", NULL }, console_dial, "Dial an extension on the console", dial_usage },
  889. { { "send", "text", NULL }, console_sendtext, "Send text to the remote device", sendtext_usage },
  890. { { "autoanswer", NULL }, console_autoanswer, "Sets/displays autoanswer", autoanswer_usage, autoanswer_complete }
  891. };
  892. int load_module()
  893. {
  894. int res;
  895. int x;
  896. struct ast_config *cfg;
  897. struct ast_variable *v;
  898. if ((cfg = ast_load(config))) {
  899. v = ast_variable_browse(cfg, "general");
  900. while(v) {
  901. if (!strcasecmp(v->name, "autoanswer"))
  902. autoanswer = ast_true(v->value);
  903. else if (!strcasecmp(v->name, "silencesuppression"))
  904. silencesuppression = ast_true(v->value);
  905. else if (!strcasecmp(v->name, "silencethreshold"))
  906. silencethreshold = atoi(v->value);
  907. else if (!strcasecmp(v->name, "context"))
  908. strncpy(context, v->value, sizeof(context)-1);
  909. else if (!strcasecmp(v->name, "language"))
  910. strncpy(language, v->value, sizeof(language)-1);
  911. else if (!strcasecmp(v->name, "extension"))
  912. strncpy(exten, v->value, sizeof(exten)-1);
  913. else if (!strcasecmp(v->name, "input_device"))
  914. strncpy(indevname, v->value, sizeof(indevname)-1);
  915. else if (!strcasecmp(v->name, "output_device"))
  916. strncpy(outdevname, v->value, sizeof(outdevname)-1);
  917. v=v->next;
  918. }
  919. ast_destroy(cfg);
  920. }
  921. res = pipe(sndcmd);
  922. if (res) {
  923. ast_log(LOG_ERROR, "Unable to create pipe\n");
  924. return -1;
  925. }
  926. res = soundcard_init();
  927. if (res < 0) {
  928. if (option_verbose > 1) {
  929. ast_verbose(VERBOSE_PREFIX_2 "No sound card detected -- console channel will be unavailable\n");
  930. ast_verbose(VERBOSE_PREFIX_2 "Turn off ALSA support by adding 'noload=chan_alsa.so' in /etc/asterisk/modules.conf\n");
  931. }
  932. return 0;
  933. }
  934. #if 0
  935. if (!full_duplex)
  936. ast_log(LOG_WARNING, "XXX I don't work right with non-full duplex sound cards XXX\n");
  937. #endif
  938. res = ast_channel_register(type, tdesc, AST_FORMAT_SLINEAR, alsa_request);
  939. if (res < 0) {
  940. ast_log(LOG_ERROR, "Unable to register channel class '%s'\n", type);
  941. return -1;
  942. }
  943. for (x=0;x<sizeof(myclis)/sizeof(struct ast_cli_entry); x++)
  944. ast_cli_register(myclis + x);
  945. ast_pthread_create(&sthread, NULL, sound_thread, NULL);
  946. #ifdef ALSA_MONITOR
  947. if (alsa_monitor_start()) {
  948. ast_log(LOG_ERROR, "Problem starting Monitoring\n");
  949. }
  950. #endif
  951. return 0;
  952. }
  953. int unload_module()
  954. {
  955. int x;
  956. ast_channel_unregister(type);
  957. for (x=0;x<sizeof(myclis)/sizeof(struct ast_cli_entry); x++)
  958. ast_cli_unregister(myclis + x);
  959. snd_pcm_close(alsa.icard);
  960. snd_pcm_close(alsa.ocard);
  961. if (sndcmd[0] > 0) {
  962. close(sndcmd[0]);
  963. close(sndcmd[1]);
  964. }
  965. if (alsa.owner)
  966. ast_softhangup(alsa.owner, AST_SOFTHANGUP_APPUNLOAD);
  967. if (alsa.owner)
  968. return -1;
  969. return 0;
  970. }
  971. char *description()
  972. {
  973. return desc;
  974. }
  975. int usecount()
  976. {
  977. int res;
  978. ast_mutex_lock(&usecnt_lock);
  979. res = usecnt;
  980. ast_mutex_unlock(&usecnt_lock);
  981. return res;
  982. }
  983. char *key()
  984. {
  985. return ASTERISK_GPL_KEY;
  986. }