chan_alsa.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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, void *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, 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 %d\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(%ld 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 %ld: %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, 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. c->tech_pvt = 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. int off = 0;
  375. ast_mutex_lock(&alsalock);
  376. f.frametype = AST_FRAME_NULL;
  377. f.subclass.integer = 0;
  378. f.samples = 0;
  379. f.datalen = 0;
  380. f.data.ptr = NULL;
  381. f.offset = 0;
  382. f.src = "Console";
  383. f.mallocd = 0;
  384. f.delivery.tv_sec = 0;
  385. f.delivery.tv_usec = 0;
  386. if (noaudiocapture) {
  387. /* Return null frame to asterisk*/
  388. ast_mutex_unlock(&alsalock);
  389. return &f;
  390. }
  391. state = snd_pcm_state(alsa.icard);
  392. if ((state != SND_PCM_STATE_PREPARED) && (state != SND_PCM_STATE_RUNNING)) {
  393. snd_pcm_prepare(alsa.icard);
  394. }
  395. buf = __buf + AST_FRIENDLY_OFFSET / 2;
  396. r = snd_pcm_readi(alsa.icard, buf + readpos, left);
  397. if (r == -EPIPE) {
  398. #if DEBUG
  399. ast_log(LOG_ERROR, "XRUN read\n");
  400. #endif
  401. snd_pcm_prepare(alsa.icard);
  402. } else if (r == -ESTRPIPE) {
  403. ast_log(LOG_ERROR, "-ESTRPIPE\n");
  404. snd_pcm_prepare(alsa.icard);
  405. } else if (r < 0) {
  406. ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
  407. } else if (r >= 0) {
  408. off -= r;
  409. }
  410. /* Update positions */
  411. readpos += r;
  412. left -= r;
  413. if (readpos >= FRAME_SIZE) {
  414. /* A real frame */
  415. readpos = 0;
  416. left = FRAME_SIZE;
  417. if (chan->_state != AST_STATE_UP) {
  418. /* Don't transmit unless it's up */
  419. ast_mutex_unlock(&alsalock);
  420. return &f;
  421. }
  422. if (mute) {
  423. /* Don't transmit if muted */
  424. ast_mutex_unlock(&alsalock);
  425. return &f;
  426. }
  427. f.frametype = AST_FRAME_VOICE;
  428. ast_format_set(&f.subclass.format, AST_FORMAT_SLINEAR, 0);
  429. f.samples = FRAME_SIZE;
  430. f.datalen = FRAME_SIZE * 2;
  431. f.data.ptr = buf;
  432. f.offset = AST_FRIENDLY_OFFSET;
  433. f.src = "Console";
  434. f.mallocd = 0;
  435. }
  436. ast_mutex_unlock(&alsalock);
  437. return &f;
  438. }
  439. static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  440. {
  441. struct chan_alsa_pvt *p = newchan->tech_pvt;
  442. ast_mutex_lock(&alsalock);
  443. p->owner = newchan;
  444. ast_mutex_unlock(&alsalock);
  445. return 0;
  446. }
  447. static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen)
  448. {
  449. int res = 0;
  450. ast_mutex_lock(&alsalock);
  451. switch (cond) {
  452. case AST_CONTROL_BUSY:
  453. case AST_CONTROL_CONGESTION:
  454. case AST_CONTROL_RINGING:
  455. case AST_CONTROL_INCOMPLETE:
  456. case -1:
  457. res = -1; /* Ask for inband indications */
  458. break;
  459. case AST_CONTROL_PROGRESS:
  460. case AST_CONTROL_PROCEEDING:
  461. case AST_CONTROL_VIDUPDATE:
  462. case AST_CONTROL_SRCUPDATE:
  463. break;
  464. case AST_CONTROL_HOLD:
  465. ast_verbose(" << Console Has Been Placed on Hold >> \n");
  466. ast_moh_start(chan, data, mohinterpret);
  467. break;
  468. case AST_CONTROL_UNHOLD:
  469. ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
  470. ast_moh_stop(chan);
  471. break;
  472. default:
  473. ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, chan->name);
  474. res = -1;
  475. }
  476. ast_mutex_unlock(&alsalock);
  477. return res;
  478. }
  479. static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state, const char *linkedid)
  480. {
  481. struct ast_channel *tmp = NULL;
  482. if (!(tmp = ast_channel_alloc(1, state, 0, 0, "", p->exten, p->context, linkedid, 0, "ALSA/%s", indevname)))
  483. return NULL;
  484. tmp->tech = &alsa_tech;
  485. ast_channel_set_fd(tmp, 0, readdev);
  486. ast_format_set(&tmp->readformat, AST_FORMAT_SLINEAR, 0);
  487. ast_format_set(&tmp->writeformat, AST_FORMAT_SLINEAR, 0);
  488. ast_format_cap_add(tmp->nativeformats, &tmp->writeformat);
  489. tmp->tech_pvt = p;
  490. if (!ast_strlen_zero(p->context))
  491. ast_copy_string(tmp->context, p->context, sizeof(tmp->context));
  492. if (!ast_strlen_zero(p->exten))
  493. ast_copy_string(tmp->exten, p->exten, sizeof(tmp->exten));
  494. if (!ast_strlen_zero(language))
  495. ast_string_field_set(tmp, language, language);
  496. p->owner = tmp;
  497. ast_module_ref(ast_module_info->self);
  498. ast_jb_configure(tmp, &global_jbconf);
  499. if (state != AST_STATE_DOWN) {
  500. if (ast_pbx_start(tmp)) {
  501. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
  502. ast_hangup(tmp);
  503. tmp = NULL;
  504. }
  505. }
  506. return tmp;
  507. }
  508. static struct ast_channel *alsa_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause)
  509. {
  510. struct ast_format tmpfmt;
  511. char buf[256];
  512. struct ast_channel *tmp = NULL;
  513. ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0);
  514. if (!(ast_format_cap_iscompatible(cap, &tmpfmt))) {
  515. ast_log(LOG_NOTICE, "Asked to get a channel of format '%s'\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
  516. return NULL;
  517. }
  518. ast_mutex_lock(&alsalock);
  519. if (alsa.owner) {
  520. ast_log(LOG_NOTICE, "Already have a call on the ALSA channel\n");
  521. *cause = AST_CAUSE_BUSY;
  522. } else if (!(tmp = alsa_new(&alsa, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL))) {
  523. ast_log(LOG_WARNING, "Unable to create new ALSA channel\n");
  524. }
  525. ast_mutex_unlock(&alsalock);
  526. return tmp;
  527. }
  528. static char *autoanswer_complete(const char *line, const char *word, int pos, int state)
  529. {
  530. switch (state) {
  531. case 0:
  532. if (!ast_strlen_zero(word) && !strncasecmp(word, "on", MIN(strlen(word), 2)))
  533. return ast_strdup("on");
  534. case 1:
  535. if (!ast_strlen_zero(word) && !strncasecmp(word, "off", MIN(strlen(word), 3)))
  536. return ast_strdup("off");
  537. default:
  538. return NULL;
  539. }
  540. return NULL;
  541. }
  542. static char *console_autoanswer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  543. {
  544. char *res = CLI_SUCCESS;
  545. switch (cmd) {
  546. case CLI_INIT:
  547. e->command = "console autoanswer";
  548. e->usage =
  549. "Usage: console autoanswer [on|off]\n"
  550. " Enables or disables autoanswer feature. If used without\n"
  551. " argument, displays the current on/off status of autoanswer.\n"
  552. " The default value of autoanswer is in 'alsa.conf'.\n";
  553. return NULL;
  554. case CLI_GENERATE:
  555. return autoanswer_complete(a->line, a->word, a->pos, a->n);
  556. }
  557. if ((a->argc != 2) && (a->argc != 3))
  558. return CLI_SHOWUSAGE;
  559. ast_mutex_lock(&alsalock);
  560. if (a->argc == 2) {
  561. ast_cli(a->fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
  562. } else {
  563. if (!strcasecmp(a->argv[2], "on"))
  564. autoanswer = -1;
  565. else if (!strcasecmp(a->argv[2], "off"))
  566. autoanswer = 0;
  567. else
  568. res = CLI_SHOWUSAGE;
  569. }
  570. ast_mutex_unlock(&alsalock);
  571. return res;
  572. }
  573. static char *console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  574. {
  575. char *res = CLI_SUCCESS;
  576. switch (cmd) {
  577. case CLI_INIT:
  578. e->command = "console answer";
  579. e->usage =
  580. "Usage: console answer\n"
  581. " Answers an incoming call on the console (ALSA) channel.\n";
  582. return NULL;
  583. case CLI_GENERATE:
  584. return NULL;
  585. }
  586. if (a->argc != 2)
  587. return CLI_SHOWUSAGE;
  588. ast_mutex_lock(&alsalock);
  589. if (!alsa.owner) {
  590. ast_cli(a->fd, "No one is calling us\n");
  591. res = CLI_FAILURE;
  592. } else {
  593. if (mute) {
  594. ast_verbose( " << Muted >> \n" );
  595. }
  596. hookstate = 1;
  597. grab_owner();
  598. if (alsa.owner) {
  599. ast_queue_control(alsa.owner, AST_CONTROL_ANSWER);
  600. ast_channel_unlock(alsa.owner);
  601. }
  602. }
  603. if (!noaudiocapture) {
  604. snd_pcm_prepare(alsa.icard);
  605. snd_pcm_start(alsa.icard);
  606. }
  607. ast_mutex_unlock(&alsalock);
  608. return res;
  609. }
  610. static char *console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  611. {
  612. int tmparg = 3;
  613. char *res = CLI_SUCCESS;
  614. switch (cmd) {
  615. case CLI_INIT:
  616. e->command = "console send text";
  617. e->usage =
  618. "Usage: console send text <message>\n"
  619. " Sends a text message for display on the remote terminal.\n";
  620. return NULL;
  621. case CLI_GENERATE:
  622. return NULL;
  623. }
  624. if (a->argc < 3)
  625. return CLI_SHOWUSAGE;
  626. ast_mutex_lock(&alsalock);
  627. if (!alsa.owner) {
  628. ast_cli(a->fd, "No channel active\n");
  629. res = CLI_FAILURE;
  630. } else {
  631. struct ast_frame f = { AST_FRAME_TEXT };
  632. char text2send[256] = "";
  633. while (tmparg < a->argc) {
  634. strncat(text2send, a->argv[tmparg++], sizeof(text2send) - strlen(text2send) - 1);
  635. strncat(text2send, " ", sizeof(text2send) - strlen(text2send) - 1);
  636. }
  637. text2send[strlen(text2send) - 1] = '\n';
  638. f.data.ptr = text2send;
  639. f.datalen = strlen(text2send) + 1;
  640. grab_owner();
  641. if (alsa.owner) {
  642. ast_queue_frame(alsa.owner, &f);
  643. ast_queue_control(alsa.owner, AST_CONTROL_ANSWER);
  644. ast_channel_unlock(alsa.owner);
  645. }
  646. }
  647. ast_mutex_unlock(&alsalock);
  648. return res;
  649. }
  650. static char *console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  651. {
  652. char *res = CLI_SUCCESS;
  653. switch (cmd) {
  654. case CLI_INIT:
  655. e->command = "console hangup";
  656. e->usage =
  657. "Usage: console hangup\n"
  658. " Hangs up any call currently placed on the console.\n";
  659. return NULL;
  660. case CLI_GENERATE:
  661. return NULL;
  662. }
  663. if (a->argc != 2)
  664. return CLI_SHOWUSAGE;
  665. ast_mutex_lock(&alsalock);
  666. if (!alsa.owner && !hookstate) {
  667. ast_cli(a->fd, "No call to hangup\n");
  668. res = CLI_FAILURE;
  669. } else {
  670. hookstate = 0;
  671. grab_owner();
  672. if (alsa.owner) {
  673. ast_queue_hangup_with_cause(alsa.owner, AST_CAUSE_NORMAL_CLEARING);
  674. ast_channel_unlock(alsa.owner);
  675. }
  676. }
  677. ast_mutex_unlock(&alsalock);
  678. return res;
  679. }
  680. static char *console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  681. {
  682. char tmp[256], *tmp2;
  683. char *mye, *myc;
  684. const char *d;
  685. char *res = CLI_SUCCESS;
  686. switch (cmd) {
  687. case CLI_INIT:
  688. e->command = "console dial";
  689. e->usage =
  690. "Usage: console dial [extension[@context]]\n"
  691. " Dials a given extension (and context if specified)\n";
  692. return NULL;
  693. case CLI_GENERATE:
  694. return NULL;
  695. }
  696. if ((a->argc != 2) && (a->argc != 3))
  697. return CLI_SHOWUSAGE;
  698. ast_mutex_lock(&alsalock);
  699. if (alsa.owner) {
  700. if (a->argc == 3) {
  701. if (alsa.owner) {
  702. for (d = a->argv[2]; *d; d++) {
  703. struct ast_frame f = { .frametype = AST_FRAME_DTMF, .subclass.integer = *d };
  704. ast_queue_frame(alsa.owner, &f);
  705. }
  706. }
  707. } else {
  708. ast_cli(a->fd, "You're already in a call. You can use this only to dial digits until you hangup\n");
  709. res = CLI_FAILURE;
  710. }
  711. } else {
  712. mye = exten;
  713. myc = context;
  714. if (a->argc == 3) {
  715. char *stringp = NULL;
  716. ast_copy_string(tmp, a->argv[2], sizeof(tmp));
  717. stringp = tmp;
  718. strsep(&stringp, "@");
  719. tmp2 = strsep(&stringp, "@");
  720. if (!ast_strlen_zero(tmp))
  721. mye = tmp;
  722. if (!ast_strlen_zero(tmp2))
  723. myc = tmp2;
  724. }
  725. if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
  726. ast_copy_string(alsa.exten, mye, sizeof(alsa.exten));
  727. ast_copy_string(alsa.context, myc, sizeof(alsa.context));
  728. hookstate = 1;
  729. alsa_new(&alsa, AST_STATE_RINGING, NULL);
  730. } else
  731. ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc);
  732. }
  733. ast_mutex_unlock(&alsalock);
  734. return res;
  735. }
  736. static char *console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  737. {
  738. int toggle = 0;
  739. char *res = CLI_SUCCESS;
  740. switch (cmd) {
  741. case CLI_INIT:
  742. e->command = "console {mute|unmute} [toggle]";
  743. e->usage =
  744. "Usage: console {mute|unmute} [toggle]\n"
  745. " Mute/unmute the microphone.\n";
  746. return NULL;
  747. case CLI_GENERATE:
  748. return NULL;
  749. }
  750. if (a->argc > 3) {
  751. return CLI_SHOWUSAGE;
  752. }
  753. if (a->argc == 3) {
  754. if (strcasecmp(a->argv[2], "toggle"))
  755. return CLI_SHOWUSAGE;
  756. toggle = 1;
  757. }
  758. if (a->argc < 2) {
  759. return CLI_SHOWUSAGE;
  760. }
  761. if (!strcasecmp(a->argv[1], "mute")) {
  762. mute = toggle ? !mute : 1;
  763. } else if (!strcasecmp(a->argv[1], "unmute")) {
  764. mute = toggle ? !mute : 0;
  765. } else {
  766. return CLI_SHOWUSAGE;
  767. }
  768. ast_cli(a->fd, "Console mic is %s\n", mute ? "off" : "on");
  769. return res;
  770. }
  771. static struct ast_cli_entry cli_alsa[] = {
  772. AST_CLI_DEFINE(console_answer, "Answer an incoming console call"),
  773. AST_CLI_DEFINE(console_hangup, "Hangup a call on the console"),
  774. AST_CLI_DEFINE(console_dial, "Dial an extension on the console"),
  775. AST_CLI_DEFINE(console_sendtext, "Send text to the remote device"),
  776. AST_CLI_DEFINE(console_autoanswer, "Sets/displays autoanswer"),
  777. AST_CLI_DEFINE(console_mute, "Disable/Enable mic input"),
  778. };
  779. static int load_module(void)
  780. {
  781. struct ast_config *cfg;
  782. struct ast_variable *v;
  783. struct ast_flags config_flags = { 0 };
  784. struct ast_format tmpfmt;
  785. if (!(alsa_tech.capabilities = ast_format_cap_alloc())) {
  786. return AST_MODULE_LOAD_DECLINE;
  787. }
  788. ast_format_cap_add(alsa_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
  789. /* Copy the default jb config over global_jbconf */
  790. memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
  791. strcpy(mohinterpret, "default");
  792. if (!(cfg = ast_config_load(config, config_flags))) {
  793. ast_log(LOG_ERROR, "Unable to read ALSA configuration file %s. Aborting.\n", config);
  794. return AST_MODULE_LOAD_DECLINE;
  795. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  796. ast_log(LOG_ERROR, "%s is in an invalid format. Aborting.\n", config);
  797. return AST_MODULE_LOAD_DECLINE;
  798. }
  799. v = ast_variable_browse(cfg, "general");
  800. for (; v; v = v->next) {
  801. /* handle jb conf */
  802. if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) {
  803. continue;
  804. }
  805. if (!strcasecmp(v->name, "autoanswer")) {
  806. autoanswer = ast_true(v->value);
  807. } else if (!strcasecmp(v->name, "mute")) {
  808. mute = ast_true(v->value);
  809. } else if (!strcasecmp(v->name, "noaudiocapture")) {
  810. noaudiocapture = ast_true(v->value);
  811. } else if (!strcasecmp(v->name, "silencesuppression")) {
  812. silencesuppression = ast_true(v->value);
  813. } else if (!strcasecmp(v->name, "silencethreshold")) {
  814. silencethreshold = atoi(v->value);
  815. } else if (!strcasecmp(v->name, "context")) {
  816. ast_copy_string(context, v->value, sizeof(context));
  817. } else if (!strcasecmp(v->name, "language")) {
  818. ast_copy_string(language, v->value, sizeof(language));
  819. } else if (!strcasecmp(v->name, "extension")) {
  820. ast_copy_string(exten, v->value, sizeof(exten));
  821. } else if (!strcasecmp(v->name, "input_device")) {
  822. ast_copy_string(indevname, v->value, sizeof(indevname));
  823. } else if (!strcasecmp(v->name, "output_device")) {
  824. ast_copy_string(outdevname, v->value, sizeof(outdevname));
  825. } else if (!strcasecmp(v->name, "mohinterpret")) {
  826. ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
  827. }
  828. }
  829. ast_config_destroy(cfg);
  830. if (soundcard_init() < 0) {
  831. ast_verb(2, "No sound card detected -- console channel will be unavailable\n");
  832. ast_verb(2, "Turn off ALSA support by adding 'noload=chan_alsa.so' in /etc/asterisk/modules.conf\n");
  833. return AST_MODULE_LOAD_DECLINE;
  834. }
  835. if (ast_channel_register(&alsa_tech)) {
  836. ast_log(LOG_ERROR, "Unable to register channel class 'Console'\n");
  837. return AST_MODULE_LOAD_FAILURE;
  838. }
  839. ast_cli_register_multiple(cli_alsa, ARRAY_LEN(cli_alsa));
  840. return AST_MODULE_LOAD_SUCCESS;
  841. }
  842. static int unload_module(void)
  843. {
  844. ast_channel_unregister(&alsa_tech);
  845. ast_cli_unregister_multiple(cli_alsa, ARRAY_LEN(cli_alsa));
  846. if (alsa.icard)
  847. snd_pcm_close(alsa.icard);
  848. if (alsa.ocard)
  849. snd_pcm_close(alsa.ocard);
  850. if (alsa.owner)
  851. ast_softhangup(alsa.owner, AST_SOFTHANGUP_APPUNLOAD);
  852. if (alsa.owner)
  853. return -1;
  854. alsa_tech.capabilities = ast_format_cap_destroy(alsa_tech.capabilities);
  855. return 0;
  856. }
  857. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ALSA Console Channel Driver",
  858. .load = load_module,
  859. .unload = unload_module,
  860. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  861. );