chan_alsa.c 34 KB

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