chan_alsa.c 27 KB

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