chan_alsa.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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>alsa</depend>
  30. <support_level>extended</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include <fcntl.h>
  35. #include <sys/ioctl.h>
  36. #include <sys/time.h>
  37. #define ALSA_PCM_NEW_HW_PARAMS_API
  38. #define ALSA_PCM_NEW_SW_PARAMS_API
  39. #include <alsa/asoundlib.h>
  40. #include "asterisk/frame.h"
  41. #include "asterisk/channel.h"
  42. #include "asterisk/module.h"
  43. #include "asterisk/pbx.h"
  44. #include "asterisk/config.h"
  45. #include "asterisk/cli.h"
  46. #include "asterisk/utils.h"
  47. #include "asterisk/causes.h"
  48. #include "asterisk/endian.h"
  49. #include "asterisk/stringfields.h"
  50. #include "asterisk/abstract_jb.h"
  51. #include "asterisk/musiconhold.h"
  52. #include "asterisk/poll-compat.h"
  53. /*! Global jitterbuffer configuration - by default, jb is disabled
  54. * \note Values shown here match the defaults shown in alsa.conf.sample */
  55. static struct ast_jb_conf default_jbconf = {
  56. .flags = 0,
  57. .max_size = 200,
  58. .resync_threshold = 1000,
  59. .impl = "fixed",
  60. .target_extra = 40,
  61. };
  62. static struct ast_jb_conf global_jbconf;
  63. #define DEBUG 0
  64. /* Which device to use */
  65. #define ALSA_INDEV "default"
  66. #define ALSA_OUTDEV "default"
  67. #define DESIRED_RATE 8000
  68. /* Lets use 160 sample frames, just like GSM. */
  69. #define FRAME_SIZE 160
  70. #define PERIOD_FRAMES 80 /* 80 Frames, at 2 bytes each */
  71. /* When you set the frame size, you have to come up with
  72. the right buffer format as well. */
  73. /* 5 64-byte frames = one frame */
  74. #define BUFFER_FMT ((buffersize * 10) << 16) | (0x0006);
  75. /* Don't switch between read/write modes faster than every 300 ms */
  76. #define MIN_SWITCH_TIME 600
  77. #if __BYTE_ORDER == __LITTLE_ENDIAN
  78. static snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
  79. #else
  80. static snd_pcm_format_t format = SND_PCM_FORMAT_S16_BE;
  81. #endif
  82. static char indevname[50] = ALSA_INDEV;
  83. static char outdevname[50] = ALSA_OUTDEV;
  84. static int silencesuppression = 0;
  85. static int silencethreshold = 1000;
  86. AST_MUTEX_DEFINE_STATIC(alsalock);
  87. static const char tdesc[] = "ALSA Console Channel Driver";
  88. static const char config[] = "alsa.conf";
  89. static char context[AST_MAX_CONTEXT] = "default";
  90. static char language[MAX_LANGUAGE] = "";
  91. static char exten[AST_MAX_EXTENSION] = "s";
  92. static char mohinterpret[MAX_MUSICCLASS];
  93. static int hookstate = 0;
  94. static struct chan_alsa_pvt {
  95. /* We only have one ALSA structure -- near sighted perhaps, but it
  96. keeps this driver as simple as possible -- as it should be. */
  97. struct ast_channel *owner;
  98. char exten[AST_MAX_EXTENSION];
  99. char context[AST_MAX_CONTEXT];
  100. snd_pcm_t *icard, *ocard;
  101. } alsa;
  102. /* Number of buffers... Each is FRAMESIZE/8 ms long. For example
  103. with 160 sample frames, and a buffer size of 3, we have a 60ms buffer,
  104. usually plenty. */
  105. #define MAX_BUFFER_SIZE 100
  106. /* File descriptors for sound device */
  107. static int readdev = -1;
  108. static int writedev = -1;
  109. static int autoanswer = 1;
  110. static int mute = 0;
  111. static int noaudiocapture = 0;
  112. static struct ast_channel *alsa_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause);
  113. static int alsa_digit(struct ast_channel *c, char digit, unsigned int duration);
  114. static int alsa_text(struct ast_channel *c, const char *text);
  115. static int alsa_hangup(struct ast_channel *c);
  116. static int alsa_answer(struct ast_channel *c);
  117. static struct ast_frame *alsa_read(struct ast_channel *chan);
  118. static int alsa_call(struct ast_channel *c, const char *dest, int timeout);
  119. static int alsa_write(struct ast_channel *chan, struct ast_frame *f);
  120. static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
  121. static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  122. static struct ast_channel_tech alsa_tech = {
  123. .type = "Console",
  124. .description = tdesc,
  125. .requester = alsa_request,
  126. .send_digit_end = alsa_digit,
  127. .send_text = alsa_text,
  128. .hangup = alsa_hangup,
  129. .answer = alsa_answer,
  130. .read = alsa_read,
  131. .call = alsa_call,
  132. .write = alsa_write,
  133. .indicate = alsa_indicate,
  134. .fixup = alsa_fixup,
  135. };
  136. static snd_pcm_t *alsa_card_init(char *dev, snd_pcm_stream_t stream)
  137. {
  138. int err;
  139. int direction;
  140. snd_pcm_t *handle = NULL;
  141. snd_pcm_hw_params_t *hwparams = NULL;
  142. snd_pcm_sw_params_t *swparams = NULL;
  143. struct pollfd pfd;
  144. snd_pcm_uframes_t period_size = PERIOD_FRAMES * 4;
  145. snd_pcm_uframes_t buffer_size = 0;
  146. unsigned int rate = DESIRED_RATE;
  147. snd_pcm_uframes_t start_threshold, stop_threshold;
  148. err = snd_pcm_open(&handle, dev, stream, SND_PCM_NONBLOCK);
  149. if (err < 0) {
  150. ast_log(LOG_ERROR, "snd_pcm_open failed: %s\n", snd_strerror(err));
  151. return NULL;
  152. } else {
  153. ast_debug(1, "Opening device %s in %s mode\n", dev, (stream == SND_PCM_STREAM_CAPTURE) ? "read" : "write");
  154. }
  155. hwparams = ast_alloca(snd_pcm_hw_params_sizeof());
  156. memset(hwparams, 0, snd_pcm_hw_params_sizeof());
  157. snd_pcm_hw_params_any(handle, hwparams);
  158. err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
  159. if (err < 0)
  160. ast_log(LOG_ERROR, "set_access failed: %s\n", snd_strerror(err));
  161. err = snd_pcm_hw_params_set_format(handle, hwparams, format);
  162. if (err < 0)
  163. ast_log(LOG_ERROR, "set_format failed: %s\n", snd_strerror(err));
  164. err = snd_pcm_hw_params_set_channels(handle, hwparams, 1);
  165. if (err < 0)
  166. ast_log(LOG_ERROR, "set_channels failed: %s\n", snd_strerror(err));
  167. direction = 0;
  168. err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &rate, &direction);
  169. if (rate != DESIRED_RATE)
  170. ast_log(LOG_WARNING, "Rate not correct, requested %d, got %u\n", DESIRED_RATE, rate);
  171. direction = 0;
  172. err = snd_pcm_hw_params_set_period_size_near(handle, hwparams, &period_size, &direction);
  173. if (err < 0)
  174. ast_log(LOG_ERROR, "period_size(%lu frames) is bad: %s\n", period_size, snd_strerror(err));
  175. else {
  176. ast_debug(1, "Period size is %d\n", err);
  177. }
  178. buffer_size = 4096 * 2; /* period_size * 16; */
  179. err = snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &buffer_size);
  180. if (err < 0)
  181. ast_log(LOG_WARNING, "Problem setting buffer size of %lu: %s\n", buffer_size, snd_strerror(err));
  182. else {
  183. ast_debug(1, "Buffer size is set to %d frames\n", err);
  184. }
  185. err = snd_pcm_hw_params(handle, hwparams);
  186. if (err < 0)
  187. ast_log(LOG_ERROR, "Couldn't set the new hw params: %s\n", snd_strerror(err));
  188. swparams = ast_alloca(snd_pcm_sw_params_sizeof());
  189. memset(swparams, 0, snd_pcm_sw_params_sizeof());
  190. snd_pcm_sw_params_current(handle, swparams);
  191. if (stream == SND_PCM_STREAM_PLAYBACK)
  192. start_threshold = period_size;
  193. else
  194. start_threshold = 1;
  195. err = snd_pcm_sw_params_set_start_threshold(handle, swparams, start_threshold);
  196. if (err < 0)
  197. ast_log(LOG_ERROR, "start threshold: %s\n", snd_strerror(err));
  198. if (stream == SND_PCM_STREAM_PLAYBACK)
  199. stop_threshold = buffer_size;
  200. else
  201. stop_threshold = buffer_size;
  202. err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, stop_threshold);
  203. if (err < 0)
  204. ast_log(LOG_ERROR, "stop threshold: %s\n", snd_strerror(err));
  205. err = snd_pcm_sw_params(handle, swparams);
  206. if (err < 0)
  207. ast_log(LOG_ERROR, "sw_params: %s\n", snd_strerror(err));
  208. err = snd_pcm_poll_descriptors_count(handle);
  209. if (err <= 0)
  210. ast_log(LOG_ERROR, "Unable to get a poll descriptors count, error is %s\n", snd_strerror(err));
  211. if (err != 1) {
  212. ast_debug(1, "Can't handle more than one device\n");
  213. }
  214. snd_pcm_poll_descriptors(handle, &pfd, err);
  215. ast_debug(1, "Acquired fd %d from the poll descriptor\n", pfd.fd);
  216. if (stream == SND_PCM_STREAM_CAPTURE)
  217. readdev = pfd.fd;
  218. else
  219. writedev = pfd.fd;
  220. return handle;
  221. }
  222. static int soundcard_init(void)
  223. {
  224. if (!noaudiocapture) {
  225. alsa.icard = alsa_card_init(indevname, SND_PCM_STREAM_CAPTURE);
  226. if (!alsa.icard) {
  227. ast_log(LOG_ERROR, "Problem opening alsa capture device\n");
  228. return -1;
  229. }
  230. }
  231. alsa.ocard = alsa_card_init(outdevname, SND_PCM_STREAM_PLAYBACK);
  232. if (!alsa.ocard) {
  233. ast_log(LOG_ERROR, "Problem opening ALSA playback device\n");
  234. return -1;
  235. }
  236. return writedev;
  237. }
  238. static int alsa_digit(struct ast_channel *c, char digit, unsigned int duration)
  239. {
  240. ast_mutex_lock(&alsalock);
  241. ast_verbose(" << Console Received digit %c of duration %u ms >> \n",
  242. digit, duration);
  243. ast_mutex_unlock(&alsalock);
  244. return 0;
  245. }
  246. static int alsa_text(struct ast_channel *c, const char *text)
  247. {
  248. ast_mutex_lock(&alsalock);
  249. ast_verbose(" << Console Received text %s >> \n", text);
  250. ast_mutex_unlock(&alsalock);
  251. return 0;
  252. }
  253. static void grab_owner(void)
  254. {
  255. while (alsa.owner && ast_channel_trylock(alsa.owner)) {
  256. DEADLOCK_AVOIDANCE(&alsalock);
  257. }
  258. }
  259. static int alsa_call(struct ast_channel *c, const char *dest, int timeout)
  260. {
  261. struct ast_frame f = { AST_FRAME_CONTROL };
  262. ast_mutex_lock(&alsalock);
  263. ast_verbose(" << Call placed to '%s' on console >> \n", dest);
  264. if (autoanswer) {
  265. ast_verbose(" << Auto-answered >> \n");
  266. if (mute) {
  267. ast_verbose( " << Muted >> \n" );
  268. }
  269. grab_owner();
  270. if (alsa.owner) {
  271. f.subclass.integer = AST_CONTROL_ANSWER;
  272. ast_queue_frame(alsa.owner, &f);
  273. ast_channel_unlock(alsa.owner);
  274. }
  275. } else {
  276. ast_verbose(" << Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
  277. grab_owner();
  278. if (alsa.owner) {
  279. f.subclass.integer = AST_CONTROL_RINGING;
  280. ast_queue_frame(alsa.owner, &f);
  281. ast_channel_unlock(alsa.owner);
  282. ast_indicate(alsa.owner, AST_CONTROL_RINGING);
  283. }
  284. }
  285. if (!noaudiocapture) {
  286. snd_pcm_prepare(alsa.icard);
  287. snd_pcm_start(alsa.icard);
  288. }
  289. ast_mutex_unlock(&alsalock);
  290. return 0;
  291. }
  292. static int alsa_answer(struct ast_channel *c)
  293. {
  294. ast_mutex_lock(&alsalock);
  295. ast_verbose(" << Console call has been answered >> \n");
  296. ast_setstate(c, AST_STATE_UP);
  297. if (!noaudiocapture) {
  298. snd_pcm_prepare(alsa.icard);
  299. snd_pcm_start(alsa.icard);
  300. }
  301. ast_mutex_unlock(&alsalock);
  302. return 0;
  303. }
  304. static int alsa_hangup(struct ast_channel *c)
  305. {
  306. ast_mutex_lock(&alsalock);
  307. ast_channel_tech_pvt_set(c, NULL);
  308. alsa.owner = NULL;
  309. ast_verbose(" << Hangup on console >> \n");
  310. ast_module_unref(ast_module_info->self);
  311. hookstate = 0;
  312. if (!noaudiocapture) {
  313. snd_pcm_drop(alsa.icard);
  314. }
  315. ast_mutex_unlock(&alsalock);
  316. return 0;
  317. }
  318. static int alsa_write(struct ast_channel *chan, struct ast_frame *f)
  319. {
  320. static char sizbuf[8000];
  321. static int sizpos = 0;
  322. int len = sizpos;
  323. int res = 0;
  324. /* size_t frames = 0; */
  325. snd_pcm_state_t state;
  326. ast_mutex_lock(&alsalock);
  327. /* We have to digest the frame in 160-byte portions */
  328. if (f->datalen > sizeof(sizbuf) - sizpos) {
  329. ast_log(LOG_WARNING, "Frame too large\n");
  330. res = -1;
  331. } else {
  332. memcpy(sizbuf + sizpos, f->data.ptr, f->datalen);
  333. len += f->datalen;
  334. state = snd_pcm_state(alsa.ocard);
  335. if (state == SND_PCM_STATE_XRUN)
  336. snd_pcm_prepare(alsa.ocard);
  337. while ((res = snd_pcm_writei(alsa.ocard, sizbuf, len / 2)) == -EAGAIN) {
  338. usleep(1);
  339. }
  340. if (res == -EPIPE) {
  341. #if DEBUG
  342. ast_debug(1, "XRUN write\n");
  343. #endif
  344. snd_pcm_prepare(alsa.ocard);
  345. while ((res = snd_pcm_writei(alsa.ocard, sizbuf, len / 2)) == -EAGAIN) {
  346. usleep(1);
  347. }
  348. if (res != len / 2) {
  349. ast_log(LOG_ERROR, "Write error: %s\n", snd_strerror(res));
  350. res = -1;
  351. } else if (res < 0) {
  352. ast_log(LOG_ERROR, "Write error %s\n", snd_strerror(res));
  353. res = -1;
  354. }
  355. } else {
  356. if (res == -ESTRPIPE)
  357. ast_log(LOG_ERROR, "You've got some big problems\n");
  358. else if (res < 0)
  359. ast_log(LOG_NOTICE, "Error %d on write\n", res);
  360. }
  361. }
  362. ast_mutex_unlock(&alsalock);
  363. return res >= 0 ? 0 : res;
  364. }
  365. static struct ast_frame *alsa_read(struct ast_channel *chan)
  366. {
  367. static struct ast_frame f;
  368. static short __buf[FRAME_SIZE + AST_FRIENDLY_OFFSET / 2];
  369. short *buf;
  370. static int readpos = 0;
  371. static int left = FRAME_SIZE;
  372. snd_pcm_state_t state;
  373. int r = 0;
  374. ast_mutex_lock(&alsalock);
  375. f.frametype = AST_FRAME_NULL;
  376. f.subclass.integer = 0;
  377. f.samples = 0;
  378. f.datalen = 0;
  379. f.data.ptr = NULL;
  380. f.offset = 0;
  381. f.src = "Console";
  382. f.mallocd = 0;
  383. f.delivery.tv_sec = 0;
  384. f.delivery.tv_usec = 0;
  385. if (noaudiocapture) {
  386. /* Return null frame to asterisk*/
  387. ast_mutex_unlock(&alsalock);
  388. return &f;
  389. }
  390. state = snd_pcm_state(alsa.icard);
  391. if ((state != SND_PCM_STATE_PREPARED) && (state != SND_PCM_STATE_RUNNING)) {
  392. snd_pcm_prepare(alsa.icard);
  393. }
  394. buf = __buf + AST_FRIENDLY_OFFSET / 2;
  395. r = snd_pcm_readi(alsa.icard, buf + readpos, left);
  396. if (r == -EPIPE) {
  397. #if DEBUG
  398. ast_log(LOG_ERROR, "XRUN read\n");
  399. #endif
  400. snd_pcm_prepare(alsa.icard);
  401. } else if (r == -ESTRPIPE) {
  402. ast_log(LOG_ERROR, "-ESTRPIPE\n");
  403. snd_pcm_prepare(alsa.icard);
  404. } else if (r < 0) {
  405. ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
  406. }
  407. /* Return NULL frame on error */
  408. if (r < 0) {
  409. ast_mutex_unlock(&alsalock);
  410. return &f;
  411. }
  412. /* Update positions */
  413. readpos += r;
  414. left -= r;
  415. if (readpos >= FRAME_SIZE) {
  416. /* A real frame */
  417. readpos = 0;
  418. left = FRAME_SIZE;
  419. if (ast_channel_state(chan) != AST_STATE_UP) {
  420. /* Don't transmit unless it's up */
  421. ast_mutex_unlock(&alsalock);
  422. return &f;
  423. }
  424. if (mute) {
  425. /* Don't transmit if muted */
  426. ast_mutex_unlock(&alsalock);
  427. return &f;
  428. }
  429. f.frametype = AST_FRAME_VOICE;
  430. ast_format_set(&f.subclass.format, AST_FORMAT_SLINEAR, 0);
  431. f.samples = FRAME_SIZE;
  432. f.datalen = FRAME_SIZE * 2;
  433. f.data.ptr = buf;
  434. f.offset = AST_FRIENDLY_OFFSET;
  435. f.src = "Console";
  436. f.mallocd = 0;
  437. }
  438. ast_mutex_unlock(&alsalock);
  439. return &f;
  440. }
  441. static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  442. {
  443. struct chan_alsa_pvt *p = ast_channel_tech_pvt(newchan);
  444. ast_mutex_lock(&alsalock);
  445. p->owner = newchan;
  446. ast_mutex_unlock(&alsalock);
  447. return 0;
  448. }
  449. static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen)
  450. {
  451. int res = 0;
  452. ast_mutex_lock(&alsalock);
  453. switch (cond) {
  454. case AST_CONTROL_BUSY:
  455. case AST_CONTROL_CONGESTION:
  456. case AST_CONTROL_RINGING:
  457. case AST_CONTROL_INCOMPLETE:
  458. case AST_CONTROL_PVT_CAUSE_CODE:
  459. case -1:
  460. res = -1; /* Ask for inband indications */
  461. break;
  462. case AST_CONTROL_PROGRESS:
  463. case AST_CONTROL_PROCEEDING:
  464. case AST_CONTROL_VIDUPDATE:
  465. case AST_CONTROL_SRCUPDATE:
  466. break;
  467. case AST_CONTROL_HOLD:
  468. ast_verbose(" << Console Has Been Placed on Hold >> \n");
  469. ast_moh_start(chan, data, mohinterpret);
  470. break;
  471. case AST_CONTROL_UNHOLD:
  472. ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
  473. ast_moh_stop(chan);
  474. break;
  475. default:
  476. ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, ast_channel_name(chan));
  477. res = -1;
  478. }
  479. ast_mutex_unlock(&alsalock);
  480. return res;
  481. }
  482. static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state, const char *linkedid)
  483. {
  484. struct ast_channel *tmp = NULL;
  485. if (!(tmp = ast_channel_alloc(1, state, 0, 0, "", p->exten, p->context, linkedid, 0, "ALSA/%s", indevname)))
  486. return NULL;
  487. ast_channel_tech_set(tmp, &alsa_tech);
  488. ast_channel_set_fd(tmp, 0, readdev);
  489. ast_format_set(ast_channel_readformat(tmp), AST_FORMAT_SLINEAR, 0);
  490. ast_format_set(ast_channel_writeformat(tmp), AST_FORMAT_SLINEAR, 0);
  491. ast_format_cap_add(ast_channel_nativeformats(tmp), ast_channel_writeformat(tmp));
  492. ast_channel_tech_pvt_set(tmp, p);
  493. if (!ast_strlen_zero(p->context))
  494. ast_channel_context_set(tmp, p->context);
  495. if (!ast_strlen_zero(p->exten))
  496. ast_channel_exten_set(tmp, p->exten);
  497. if (!ast_strlen_zero(language))
  498. ast_channel_language_set(tmp, language);
  499. p->owner = tmp;
  500. ast_module_ref(ast_module_info->self);
  501. ast_jb_configure(tmp, &global_jbconf);
  502. if (state != AST_STATE_DOWN) {
  503. if (ast_pbx_start(tmp)) {
  504. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
  505. ast_hangup(tmp);
  506. tmp = NULL;
  507. }
  508. }
  509. return tmp;
  510. }
  511. static struct ast_channel *alsa_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause)
  512. {
  513. struct ast_format tmpfmt;
  514. char buf[256];
  515. struct ast_channel *tmp = NULL;
  516. ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0);
  517. if (!(ast_format_cap_iscompatible(cap, &tmpfmt))) {
  518. ast_log(LOG_NOTICE, "Asked to get a channel of format '%s'\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
  519. return NULL;
  520. }
  521. ast_mutex_lock(&alsalock);
  522. if (alsa.owner) {
  523. ast_log(LOG_NOTICE, "Already have a call on the ALSA channel\n");
  524. *cause = AST_CAUSE_BUSY;
  525. } else if (!(tmp = alsa_new(&alsa, AST_STATE_DOWN, requestor ? ast_channel_linkedid(requestor) : NULL))) {
  526. ast_log(LOG_WARNING, "Unable to create new ALSA channel\n");
  527. }
  528. ast_mutex_unlock(&alsalock);
  529. return tmp;
  530. }
  531. static char *autoanswer_complete(const char *line, const char *word, int pos, int state)
  532. {
  533. switch (state) {
  534. case 0:
  535. if (!ast_strlen_zero(word) && !strncasecmp(word, "on", MIN(strlen(word), 2)))
  536. return ast_strdup("on");
  537. case 1:
  538. if (!ast_strlen_zero(word) && !strncasecmp(word, "off", MIN(strlen(word), 3)))
  539. return ast_strdup("off");
  540. default:
  541. return NULL;
  542. }
  543. return NULL;
  544. }
  545. static char *console_autoanswer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  546. {
  547. char *res = CLI_SUCCESS;
  548. switch (cmd) {
  549. case CLI_INIT:
  550. e->command = "console autoanswer";
  551. e->usage =
  552. "Usage: console autoanswer [on|off]\n"
  553. " Enables or disables autoanswer feature. If used without\n"
  554. " argument, displays the current on/off status of autoanswer.\n"
  555. " The default value of autoanswer is in 'alsa.conf'.\n";
  556. return NULL;
  557. case CLI_GENERATE:
  558. return autoanswer_complete(a->line, a->word, a->pos, a->n);
  559. }
  560. if ((a->argc != 2) && (a->argc != 3))
  561. return CLI_SHOWUSAGE;
  562. ast_mutex_lock(&alsalock);
  563. if (a->argc == 2) {
  564. ast_cli(a->fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
  565. } else {
  566. if (!strcasecmp(a->argv[2], "on"))
  567. autoanswer = -1;
  568. else if (!strcasecmp(a->argv[2], "off"))
  569. autoanswer = 0;
  570. else
  571. res = CLI_SHOWUSAGE;
  572. }
  573. ast_mutex_unlock(&alsalock);
  574. return res;
  575. }
  576. static char *console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  577. {
  578. char *res = CLI_SUCCESS;
  579. switch (cmd) {
  580. case CLI_INIT:
  581. e->command = "console answer";
  582. e->usage =
  583. "Usage: console answer\n"
  584. " Answers an incoming call on the console (ALSA) channel.\n";
  585. return NULL;
  586. case CLI_GENERATE:
  587. return NULL;
  588. }
  589. if (a->argc != 2)
  590. return CLI_SHOWUSAGE;
  591. ast_mutex_lock(&alsalock);
  592. if (!alsa.owner) {
  593. ast_cli(a->fd, "No one is calling us\n");
  594. res = CLI_FAILURE;
  595. } else {
  596. if (mute) {
  597. ast_verbose( " << Muted >> \n" );
  598. }
  599. hookstate = 1;
  600. grab_owner();
  601. if (alsa.owner) {
  602. ast_queue_control(alsa.owner, AST_CONTROL_ANSWER);
  603. ast_channel_unlock(alsa.owner);
  604. }
  605. }
  606. if (!noaudiocapture) {
  607. snd_pcm_prepare(alsa.icard);
  608. snd_pcm_start(alsa.icard);
  609. }
  610. ast_mutex_unlock(&alsalock);
  611. return res;
  612. }
  613. static char *console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  614. {
  615. int tmparg = 3;
  616. char *res = CLI_SUCCESS;
  617. switch (cmd) {
  618. case CLI_INIT:
  619. e->command = "console send text";
  620. e->usage =
  621. "Usage: console send text <message>\n"
  622. " Sends a text message for display on the remote terminal.\n";
  623. return NULL;
  624. case CLI_GENERATE:
  625. return NULL;
  626. }
  627. if (a->argc < 3)
  628. return CLI_SHOWUSAGE;
  629. ast_mutex_lock(&alsalock);
  630. if (!alsa.owner) {
  631. ast_cli(a->fd, "No channel active\n");
  632. res = CLI_FAILURE;
  633. } else {
  634. struct ast_frame f = { AST_FRAME_TEXT };
  635. char text2send[256] = "";
  636. while (tmparg < a->argc) {
  637. strncat(text2send, a->argv[tmparg++], sizeof(text2send) - strlen(text2send) - 1);
  638. strncat(text2send, " ", sizeof(text2send) - strlen(text2send) - 1);
  639. }
  640. text2send[strlen(text2send) - 1] = '\n';
  641. f.data.ptr = text2send;
  642. f.datalen = strlen(text2send) + 1;
  643. grab_owner();
  644. if (alsa.owner) {
  645. ast_queue_frame(alsa.owner, &f);
  646. ast_queue_control(alsa.owner, AST_CONTROL_ANSWER);
  647. ast_channel_unlock(alsa.owner);
  648. }
  649. }
  650. ast_mutex_unlock(&alsalock);
  651. return res;
  652. }
  653. static char *console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  654. {
  655. char *res = CLI_SUCCESS;
  656. switch (cmd) {
  657. case CLI_INIT:
  658. e->command = "console hangup";
  659. e->usage =
  660. "Usage: console hangup\n"
  661. " Hangs up any call currently placed on the console.\n";
  662. return NULL;
  663. case CLI_GENERATE:
  664. return NULL;
  665. }
  666. if (a->argc != 2)
  667. return CLI_SHOWUSAGE;
  668. ast_mutex_lock(&alsalock);
  669. if (!alsa.owner && !hookstate) {
  670. ast_cli(a->fd, "No call to hangup\n");
  671. res = CLI_FAILURE;
  672. } else {
  673. hookstate = 0;
  674. grab_owner();
  675. if (alsa.owner) {
  676. ast_queue_hangup_with_cause(alsa.owner, AST_CAUSE_NORMAL_CLEARING);
  677. ast_channel_unlock(alsa.owner);
  678. }
  679. }
  680. ast_mutex_unlock(&alsalock);
  681. return res;
  682. }
  683. static char *console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  684. {
  685. char tmp[256], *tmp2;
  686. char *mye, *myc;
  687. const char *d;
  688. char *res = CLI_SUCCESS;
  689. switch (cmd) {
  690. case CLI_INIT:
  691. e->command = "console dial";
  692. e->usage =
  693. "Usage: console dial [extension[@context]]\n"
  694. " Dials a given extension (and context if specified)\n";
  695. return NULL;
  696. case CLI_GENERATE:
  697. return NULL;
  698. }
  699. if ((a->argc != 2) && (a->argc != 3))
  700. return CLI_SHOWUSAGE;
  701. ast_mutex_lock(&alsalock);
  702. if (alsa.owner) {
  703. if (a->argc == 3) {
  704. if (alsa.owner) {
  705. for (d = a->argv[2]; *d; d++) {
  706. struct ast_frame f = { .frametype = AST_FRAME_DTMF, .subclass.integer = *d };
  707. ast_queue_frame(alsa.owner, &f);
  708. }
  709. }
  710. } else {
  711. ast_cli(a->fd, "You're already in a call. You can use this only to dial digits until you hangup\n");
  712. res = CLI_FAILURE;
  713. }
  714. } else {
  715. mye = exten;
  716. myc = context;
  717. if (a->argc == 3) {
  718. char *stringp = NULL;
  719. ast_copy_string(tmp, a->argv[2], sizeof(tmp));
  720. stringp = tmp;
  721. strsep(&stringp, "@");
  722. tmp2 = strsep(&stringp, "@");
  723. if (!ast_strlen_zero(tmp))
  724. mye = tmp;
  725. if (!ast_strlen_zero(tmp2))
  726. myc = tmp2;
  727. }
  728. if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
  729. ast_copy_string(alsa.exten, mye, sizeof(alsa.exten));
  730. ast_copy_string(alsa.context, myc, sizeof(alsa.context));
  731. hookstate = 1;
  732. alsa_new(&alsa, AST_STATE_RINGING, NULL);
  733. } else
  734. ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc);
  735. }
  736. ast_mutex_unlock(&alsalock);
  737. return res;
  738. }
  739. static char *console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  740. {
  741. int toggle = 0;
  742. char *res = CLI_SUCCESS;
  743. switch (cmd) {
  744. case CLI_INIT:
  745. e->command = "console {mute|unmute} [toggle]";
  746. e->usage =
  747. "Usage: console {mute|unmute} [toggle]\n"
  748. " Mute/unmute the microphone.\n";
  749. return NULL;
  750. case CLI_GENERATE:
  751. return NULL;
  752. }
  753. if (a->argc > 3) {
  754. return CLI_SHOWUSAGE;
  755. }
  756. if (a->argc == 3) {
  757. if (strcasecmp(a->argv[2], "toggle"))
  758. return CLI_SHOWUSAGE;
  759. toggle = 1;
  760. }
  761. if (a->argc < 2) {
  762. return CLI_SHOWUSAGE;
  763. }
  764. if (!strcasecmp(a->argv[1], "mute")) {
  765. mute = toggle ? !mute : 1;
  766. } else if (!strcasecmp(a->argv[1], "unmute")) {
  767. mute = toggle ? !mute : 0;
  768. } else {
  769. return CLI_SHOWUSAGE;
  770. }
  771. ast_cli(a->fd, "Console mic is %s\n", mute ? "off" : "on");
  772. return res;
  773. }
  774. static struct ast_cli_entry cli_alsa[] = {
  775. AST_CLI_DEFINE(console_answer, "Answer an incoming console call"),
  776. AST_CLI_DEFINE(console_hangup, "Hangup a call on the console"),
  777. AST_CLI_DEFINE(console_dial, "Dial an extension on the console"),
  778. AST_CLI_DEFINE(console_sendtext, "Send text to the remote device"),
  779. AST_CLI_DEFINE(console_autoanswer, "Sets/displays autoanswer"),
  780. AST_CLI_DEFINE(console_mute, "Disable/Enable mic input"),
  781. };
  782. static int load_module(void)
  783. {
  784. struct ast_config *cfg;
  785. struct ast_variable *v;
  786. struct ast_flags config_flags = { 0 };
  787. struct ast_format tmpfmt;
  788. if (!(alsa_tech.capabilities = ast_format_cap_alloc())) {
  789. return AST_MODULE_LOAD_DECLINE;
  790. }
  791. ast_format_cap_add(alsa_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
  792. /* Copy the default jb config over global_jbconf */
  793. memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
  794. strcpy(mohinterpret, "default");
  795. if (!(cfg = ast_config_load(config, config_flags))) {
  796. ast_log(LOG_ERROR, "Unable to read ALSA configuration file %s. Aborting.\n", config);
  797. return AST_MODULE_LOAD_DECLINE;
  798. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  799. ast_log(LOG_ERROR, "%s is in an invalid format. Aborting.\n", config);
  800. return AST_MODULE_LOAD_DECLINE;
  801. }
  802. v = ast_variable_browse(cfg, "general");
  803. for (; v; v = v->next) {
  804. /* handle jb conf */
  805. if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) {
  806. continue;
  807. }
  808. if (!strcasecmp(v->name, "autoanswer")) {
  809. autoanswer = ast_true(v->value);
  810. } else if (!strcasecmp(v->name, "mute")) {
  811. mute = ast_true(v->value);
  812. } else if (!strcasecmp(v->name, "noaudiocapture")) {
  813. noaudiocapture = ast_true(v->value);
  814. } else if (!strcasecmp(v->name, "silencesuppression")) {
  815. silencesuppression = ast_true(v->value);
  816. } else if (!strcasecmp(v->name, "silencethreshold")) {
  817. silencethreshold = atoi(v->value);
  818. } else if (!strcasecmp(v->name, "context")) {
  819. ast_copy_string(context, v->value, sizeof(context));
  820. } else if (!strcasecmp(v->name, "language")) {
  821. ast_copy_string(language, v->value, sizeof(language));
  822. } else if (!strcasecmp(v->name, "extension")) {
  823. ast_copy_string(exten, v->value, sizeof(exten));
  824. } else if (!strcasecmp(v->name, "input_device")) {
  825. ast_copy_string(indevname, v->value, sizeof(indevname));
  826. } else if (!strcasecmp(v->name, "output_device")) {
  827. ast_copy_string(outdevname, v->value, sizeof(outdevname));
  828. } else if (!strcasecmp(v->name, "mohinterpret")) {
  829. ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
  830. }
  831. }
  832. ast_config_destroy(cfg);
  833. if (soundcard_init() < 0) {
  834. ast_verb(2, "No sound card detected -- console channel will be unavailable\n");
  835. ast_verb(2, "Turn off ALSA support by adding 'noload=chan_alsa.so' in /etc/asterisk/modules.conf\n");
  836. return AST_MODULE_LOAD_DECLINE;
  837. }
  838. if (ast_channel_register(&alsa_tech)) {
  839. ast_log(LOG_ERROR, "Unable to register channel class 'Console'\n");
  840. return AST_MODULE_LOAD_FAILURE;
  841. }
  842. ast_cli_register_multiple(cli_alsa, ARRAY_LEN(cli_alsa));
  843. return AST_MODULE_LOAD_SUCCESS;
  844. }
  845. static int unload_module(void)
  846. {
  847. ast_channel_unregister(&alsa_tech);
  848. ast_cli_unregister_multiple(cli_alsa, ARRAY_LEN(cli_alsa));
  849. if (alsa.icard)
  850. snd_pcm_close(alsa.icard);
  851. if (alsa.ocard)
  852. snd_pcm_close(alsa.ocard);
  853. if (alsa.owner)
  854. ast_softhangup(alsa.owner, AST_SOFTHANGUP_APPUNLOAD);
  855. if (alsa.owner)
  856. return -1;
  857. alsa_tech.capabilities = ast_format_cap_destroy(alsa_tech.capabilities);
  858. return 0;
  859. }
  860. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ALSA Console Channel Driver",
  861. .load = load_module,
  862. .unload = unload_module,
  863. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  864. );