chan_alsa.c 27 KB

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