chan_alsa.c 26 KB

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