chan_oss.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Use /dev/dsp as a channel, and the console to command it :).
  5. *
  6. * The full-duplex "simulation" is pretty weak. This is generally a
  7. * VERY BADLY WRITTEN DRIVER so please don't use it as a model for
  8. * writing a driver.
  9. *
  10. * Copyright (C) 1999, Mark Spencer
  11. *
  12. * Mark Spencer <markster@linux-support.net>
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License
  16. */
  17. #include <asterisk/lock.h>
  18. #include <asterisk/frame.h>
  19. #include <asterisk/logger.h>
  20. #include <asterisk/channel.h>
  21. #include <asterisk/module.h>
  22. #include <asterisk/channel_pvt.h>
  23. #include <asterisk/options.h>
  24. #include <asterisk/pbx.h>
  25. #include <asterisk/config.h>
  26. #include <asterisk/cli.h>
  27. #include <asterisk/utils.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <errno.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/time.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include "asterisk/endian.h"
  37. #ifdef __linux
  38. #include <linux/soundcard.h>
  39. #elif defined(__FreeBSD__)
  40. #include <sys/soundcard.h>
  41. #else
  42. #include <soundcard.h>
  43. #endif
  44. #include "busy.h"
  45. #include "ringtone.h"
  46. #include "ring10.h"
  47. #include "answer.h"
  48. /* Which device to use */
  49. #if defined( __OpenBSD__ ) || defined( __NetBSD__ )
  50. #define DEV_DSP "/dev/audio"
  51. #else
  52. #define DEV_DSP "/dev/dsp"
  53. #endif
  54. /* Lets use 160 sample frames, just like GSM. */
  55. #define FRAME_SIZE 160
  56. /* When you set the frame size, you have to come up with
  57. the right buffer format as well. */
  58. /* 5 64-byte frames = one frame */
  59. #define BUFFER_FMT ((buffersize * 10) << 16) | (0x0006);
  60. /* Don't switch between read/write modes faster than every 300 ms */
  61. #define MIN_SWITCH_TIME 600
  62. static struct timeval lasttime;
  63. static int usecnt;
  64. static int silencesuppression = 0;
  65. static int silencethreshold = 1000;
  66. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  67. static char *type = "Console";
  68. static char *desc = "OSS Console Channel Driver";
  69. static char *tdesc = "OSS Console Channel Driver";
  70. static char *config = "oss.conf";
  71. static char context[AST_MAX_EXTENSION] = "default";
  72. static char language[MAX_LANGUAGE] = "";
  73. static char exten[AST_MAX_EXTENSION] = "s";
  74. static int hookstate=0;
  75. static short silence[FRAME_SIZE] = {0, };
  76. struct sound {
  77. int ind;
  78. short *data;
  79. int datalen;
  80. int samplen;
  81. int silencelen;
  82. int repeat;
  83. };
  84. static struct sound sounds[] = {
  85. { AST_CONTROL_RINGING, ringtone, sizeof(ringtone)/2, 16000, 32000, 1 },
  86. { AST_CONTROL_BUSY, busy, sizeof(busy)/2, 4000, 4000, 1 },
  87. { AST_CONTROL_CONGESTION, busy, sizeof(busy)/2, 2000, 2000, 1 },
  88. { AST_CONTROL_RING, ring10, sizeof(ring10)/2, 16000, 32000, 1 },
  89. { AST_CONTROL_ANSWER, answer, sizeof(answer)/2, 2200, 0, 0 },
  90. };
  91. /* Sound command pipe */
  92. static int sndcmd[2];
  93. static struct chan_oss_pvt {
  94. /* We only have one OSS structure -- near sighted perhaps, but it
  95. keeps this driver as simple as possible -- as it should be. */
  96. struct ast_channel *owner;
  97. char exten[AST_MAX_EXTENSION];
  98. char context[AST_MAX_EXTENSION];
  99. } oss;
  100. static int time_has_passed(void)
  101. {
  102. struct timeval tv;
  103. int ms;
  104. gettimeofday(&tv, NULL);
  105. ms = (tv.tv_sec - lasttime.tv_sec) * 1000 +
  106. (tv.tv_usec - lasttime.tv_usec) / 1000;
  107. if (ms > MIN_SWITCH_TIME)
  108. return -1;
  109. return 0;
  110. }
  111. /* Number of buffers... Each is FRAMESIZE/8 ms long. For example
  112. with 160 sample frames, and a buffer size of 3, we have a 60ms buffer,
  113. usually plenty. */
  114. static pthread_t sthread;
  115. #define MAX_BUFFER_SIZE 100
  116. static int buffersize = 3;
  117. static int full_duplex = 0;
  118. /* Are we reading or writing (simulated full duplex) */
  119. static int readmode = 1;
  120. /* File descriptor for sound device */
  121. static int sounddev = -1;
  122. static int autoanswer = 1;
  123. #if 0
  124. static int calc_loudness(short *frame)
  125. {
  126. int sum = 0;
  127. int x;
  128. for (x=0;x<FRAME_SIZE;x++) {
  129. if (frame[x] < 0)
  130. sum -= frame[x];
  131. else
  132. sum += frame[x];
  133. }
  134. sum = sum/FRAME_SIZE;
  135. return sum;
  136. }
  137. #endif
  138. static int cursound = -1;
  139. static int sampsent = 0;
  140. static int silencelen=0;
  141. static int offset=0;
  142. static int nosound=0;
  143. static int send_sound(void)
  144. {
  145. short myframe[FRAME_SIZE];
  146. int total = FRAME_SIZE;
  147. short *frame = NULL;
  148. int amt=0;
  149. int res;
  150. int myoff;
  151. audio_buf_info abi;
  152. if (cursound > -1) {
  153. res = ioctl(sounddev, SNDCTL_DSP_GETOSPACE ,&abi);
  154. if (res) {
  155. ast_log(LOG_WARNING, "Unable to read output space\n");
  156. return -1;
  157. }
  158. /* Calculate how many samples we can send, max */
  159. if (total > (abi.fragments * abi.fragsize / 2))
  160. total = abi.fragments * abi.fragsize / 2;
  161. res = total;
  162. if (sampsent < sounds[cursound].samplen) {
  163. myoff=0;
  164. while(total) {
  165. amt = total;
  166. if (amt > (sounds[cursound].datalen - offset))
  167. amt = sounds[cursound].datalen - offset;
  168. memcpy(myframe + myoff, sounds[cursound].data + offset, amt * 2);
  169. total -= amt;
  170. offset += amt;
  171. sampsent += amt;
  172. myoff += amt;
  173. if (offset >= sounds[cursound].datalen)
  174. offset = 0;
  175. }
  176. /* Set it up for silence */
  177. if (sampsent >= sounds[cursound].samplen)
  178. silencelen = sounds[cursound].silencelen;
  179. frame = myframe;
  180. } else {
  181. if (silencelen > 0) {
  182. frame = silence;
  183. silencelen -= res;
  184. } else {
  185. if (sounds[cursound].repeat) {
  186. /* Start over */
  187. sampsent = 0;
  188. offset = 0;
  189. } else {
  190. cursound = -1;
  191. nosound = 0;
  192. }
  193. }
  194. }
  195. if (frame)
  196. res = write(sounddev, frame, res * 2);
  197. if (res > 0)
  198. return 0;
  199. return res;
  200. }
  201. return 0;
  202. }
  203. static void *sound_thread(void *unused)
  204. {
  205. fd_set rfds;
  206. fd_set wfds;
  207. int max;
  208. int res;
  209. char ign[4096];
  210. if (read(sounddev, ign, sizeof(sounddev)) < 0)
  211. ast_log(LOG_WARNING, "Read error on sound device: %s\n", strerror(errno));
  212. for(;;) {
  213. FD_ZERO(&rfds);
  214. FD_ZERO(&wfds);
  215. max = sndcmd[0];
  216. FD_SET(sndcmd[0], &rfds);
  217. if (!oss.owner) {
  218. FD_SET(sounddev, &rfds);
  219. if (sounddev > max)
  220. max = sounddev;
  221. }
  222. if (cursound > -1) {
  223. FD_SET(sounddev, &wfds);
  224. if (sounddev > max)
  225. max = sounddev;
  226. }
  227. res = ast_select(max + 1, &rfds, &wfds, NULL, NULL);
  228. if (res < 1) {
  229. ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
  230. continue;
  231. }
  232. if (FD_ISSET(sndcmd[0], &rfds)) {
  233. read(sndcmd[0], &cursound, sizeof(cursound));
  234. silencelen = 0;
  235. offset = 0;
  236. sampsent = 0;
  237. }
  238. if (FD_ISSET(sounddev, &rfds)) {
  239. /* Ignore read */
  240. if (read(sounddev, ign, sizeof(ign)) < 0)
  241. ast_log(LOG_WARNING, "Read error on sound device: %s\n", strerror(errno));
  242. }
  243. if (FD_ISSET(sounddev, &wfds))
  244. if (send_sound())
  245. ast_log(LOG_WARNING, "Failed to write sound\n");
  246. }
  247. /* Never reached */
  248. return NULL;
  249. }
  250. #if 0
  251. static int silence_suppress(short *buf)
  252. {
  253. #define SILBUF 3
  254. int loudness;
  255. static int silentframes = 0;
  256. static char silbuf[FRAME_SIZE * 2 * SILBUF];
  257. static int silbufcnt=0;
  258. if (!silencesuppression)
  259. return 0;
  260. loudness = calc_loudness((short *)(buf));
  261. if (option_debug)
  262. ast_log(LOG_DEBUG, "loudness is %d\n", loudness);
  263. if (loudness < silencethreshold) {
  264. silentframes++;
  265. silbufcnt++;
  266. /* Keep track of the last few bits of silence so we can play
  267. them as lead-in when the time is right */
  268. if (silbufcnt >= SILBUF) {
  269. /* Make way for more buffer */
  270. memmove(silbuf, silbuf + FRAME_SIZE * 2, FRAME_SIZE * 2 * (SILBUF - 1));
  271. silbufcnt--;
  272. }
  273. memcpy(silbuf + FRAME_SIZE * 2 * silbufcnt, buf, FRAME_SIZE * 2);
  274. if (silentframes > 10) {
  275. /* We've had plenty of silence, so compress it now */
  276. return 1;
  277. }
  278. } else {
  279. silentframes=0;
  280. /* Write any buffered silence we have, it may have something
  281. important */
  282. if (silbufcnt) {
  283. write(sounddev, silbuf, silbufcnt * FRAME_SIZE);
  284. silbufcnt = 0;
  285. }
  286. }
  287. return 0;
  288. }
  289. #endif
  290. static int setformat(void)
  291. {
  292. int fmt, desired, res, fd = sounddev;
  293. static int warnedalready = 0;
  294. static int warnedalready2 = 0;
  295. #if __BYTE_ORDER == __LITTLE_ENDIAN
  296. fmt = AFMT_S16_LE;
  297. #else
  298. fmt = AFMT_S16_BE;
  299. #endif
  300. res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
  301. if (res < 0) {
  302. ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
  303. return -1;
  304. }
  305. res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
  306. /* Check to see if duplex set (FreeBSD Bug)*/
  307. res = ioctl(fd, SNDCTL_DSP_GETCAPS, &fmt);
  308. if ((fmt & DSP_CAP_DUPLEX) && !res) {
  309. if (option_verbose > 1)
  310. ast_verbose(VERBOSE_PREFIX_2 "Console is full duplex\n");
  311. full_duplex = -1;
  312. }
  313. fmt = 0;
  314. res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
  315. if (res < 0) {
  316. ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
  317. return -1;
  318. }
  319. /* 8000 Hz desired */
  320. desired = 8000;
  321. fmt = desired;
  322. res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
  323. if (res < 0) {
  324. ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
  325. return -1;
  326. }
  327. if (fmt != desired) {
  328. if (!warnedalready++)
  329. ast_log(LOG_WARNING, "Requested %d Hz, got %d Hz -- sound may be choppy\n", desired, fmt);
  330. }
  331. #if 1
  332. fmt = BUFFER_FMT;
  333. res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
  334. if (res < 0) {
  335. if (!warnedalready2++)
  336. ast_log(LOG_WARNING, "Unable to set fragment size -- sound may be choppy\n");
  337. }
  338. #endif
  339. return 0;
  340. }
  341. static int soundcard_setoutput(int force)
  342. {
  343. /* Make sure the soundcard is in output mode. */
  344. int fd = sounddev;
  345. if (full_duplex || (!readmode && !force))
  346. return 0;
  347. readmode = 0;
  348. if (force || time_has_passed()) {
  349. ioctl(sounddev, SNDCTL_DSP_RESET, 0);
  350. /* Keep the same fd reserved by closing the sound device and copying stdin at the same
  351. time. */
  352. /* dup2(0, sound); */
  353. close(sounddev);
  354. fd = open(DEV_DSP, O_WRONLY |O_NONBLOCK);
  355. if (fd < 0) {
  356. ast_log(LOG_WARNING, "Unable to re-open DSP device: %s\n", strerror(errno));
  357. return -1;
  358. }
  359. /* dup2 will close the original and make fd be sound */
  360. if (dup2(fd, sounddev) < 0) {
  361. ast_log(LOG_WARNING, "dup2() failed: %s\n", strerror(errno));
  362. return -1;
  363. }
  364. if (setformat()) {
  365. return -1;
  366. }
  367. return 0;
  368. }
  369. return 1;
  370. }
  371. static int soundcard_setinput(int force)
  372. {
  373. int fd = sounddev;
  374. if (full_duplex || (readmode && !force))
  375. return 0;
  376. readmode = -1;
  377. if (force || time_has_passed()) {
  378. ioctl(sounddev, SNDCTL_DSP_RESET, 0);
  379. close(sounddev);
  380. /* dup2(0, sound); */
  381. fd = open(DEV_DSP, O_RDONLY | O_NONBLOCK);
  382. if (fd < 0) {
  383. ast_log(LOG_WARNING, "Unable to re-open DSP device: %s\n", strerror(errno));
  384. return -1;
  385. }
  386. /* dup2 will close the original and make fd be sound */
  387. if (dup2(fd, sounddev) < 0) {
  388. ast_log(LOG_WARNING, "dup2() failed: %s\n", strerror(errno));
  389. return -1;
  390. }
  391. if (setformat()) {
  392. return -1;
  393. }
  394. return 0;
  395. }
  396. return 1;
  397. }
  398. static int soundcard_init(void)
  399. {
  400. /* Assume it's full duplex for starters */
  401. int fd = open(DEV_DSP, O_RDWR | O_NONBLOCK);
  402. if (fd < 0) {
  403. ast_log(LOG_WARNING, "Unable to open %s: %s\n", DEV_DSP, strerror(errno));
  404. return fd;
  405. }
  406. gettimeofday(&lasttime, NULL);
  407. sounddev = fd;
  408. setformat();
  409. if (!full_duplex)
  410. soundcard_setinput(1);
  411. return sounddev;
  412. }
  413. static int oss_digit(struct ast_channel *c, char digit)
  414. {
  415. ast_verbose( " << Console Received digit %c >> \n", digit);
  416. return 0;
  417. }
  418. static int oss_text(struct ast_channel *c, char *text)
  419. {
  420. ast_verbose( " << Console Received text %s >> \n", text);
  421. return 0;
  422. }
  423. static int oss_call(struct ast_channel *c, char *dest, int timeout)
  424. {
  425. int res = 3;
  426. struct ast_frame f = { 0, };
  427. ast_verbose( " << Call placed to '%s' on console >> \n", dest);
  428. if (autoanswer) {
  429. ast_verbose( " << Auto-answered >> \n" );
  430. f.frametype = AST_FRAME_CONTROL;
  431. f.subclass = AST_CONTROL_ANSWER;
  432. ast_queue_frame(c, &f);
  433. } else {
  434. nosound = 1;
  435. ast_verbose( " << Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
  436. f.frametype = AST_FRAME_CONTROL;
  437. f.subclass = AST_CONTROL_RINGING;
  438. ast_queue_frame(c, &f);
  439. write(sndcmd[1], &res, sizeof(res));
  440. }
  441. return 0;
  442. }
  443. static void answer_sound(void)
  444. {
  445. int res;
  446. nosound = 1;
  447. res = 4;
  448. write(sndcmd[1], &res, sizeof(res));
  449. }
  450. static int oss_answer(struct ast_channel *c)
  451. {
  452. ast_verbose( " << Console call has been answered >> \n");
  453. answer_sound();
  454. ast_setstate(c, AST_STATE_UP);
  455. cursound = -1;
  456. nosound=0;
  457. return 0;
  458. }
  459. static int oss_hangup(struct ast_channel *c)
  460. {
  461. int res = 0;
  462. cursound = -1;
  463. c->pvt->pvt = NULL;
  464. oss.owner = NULL;
  465. ast_verbose( " << Hangup on console >> \n");
  466. ast_mutex_lock(&usecnt_lock);
  467. usecnt--;
  468. ast_mutex_unlock(&usecnt_lock);
  469. if (hookstate) {
  470. if (autoanswer) {
  471. /* Assume auto-hangup too */
  472. hookstate = 0;
  473. } else {
  474. /* Make congestion noise */
  475. res = 2;
  476. write(sndcmd[1], &res, sizeof(res));
  477. }
  478. }
  479. return 0;
  480. }
  481. static int soundcard_writeframe(short *data)
  482. {
  483. /* Write an exactly FRAME_SIZE sized of frame */
  484. static int bufcnt = 0;
  485. static short buffer[FRAME_SIZE * MAX_BUFFER_SIZE * 5];
  486. struct audio_buf_info info;
  487. int res;
  488. int fd = sounddev;
  489. static int warned=0;
  490. if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info)) {
  491. if (!warned)
  492. ast_log(LOG_WARNING, "Error reading output space\n");
  493. bufcnt = buffersize;
  494. warned++;
  495. }
  496. if ((info.fragments >= buffersize * 5) && (bufcnt == buffersize)) {
  497. /* We've run out of stuff, buffer again */
  498. bufcnt = 0;
  499. }
  500. if (bufcnt == buffersize) {
  501. /* Write sample immediately */
  502. res = write(fd, ((void *)data), FRAME_SIZE * 2);
  503. } else {
  504. /* Copy the data into our buffer */
  505. res = FRAME_SIZE * 2;
  506. memcpy(buffer + (bufcnt * FRAME_SIZE), data, FRAME_SIZE * 2);
  507. bufcnt++;
  508. if (bufcnt == buffersize) {
  509. res = write(fd, ((void *)buffer), FRAME_SIZE * 2 * buffersize);
  510. }
  511. }
  512. return res;
  513. }
  514. static int oss_write(struct ast_channel *chan, struct ast_frame *f)
  515. {
  516. int res;
  517. static char sizbuf[8000];
  518. static int sizpos = 0;
  519. int len = sizpos;
  520. int pos;
  521. /* Immediately return if no sound is enabled */
  522. if (nosound)
  523. return 0;
  524. /* Stop any currently playing sound */
  525. cursound = -1;
  526. if (!full_duplex) {
  527. /* If we're half duplex, we have to switch to read mode
  528. to honor immediate needs if necessary */
  529. res = soundcard_setinput(1);
  530. if (res < 0) {
  531. ast_log(LOG_WARNING, "Unable to set device to input mode\n");
  532. return -1;
  533. }
  534. return 0;
  535. }
  536. res = soundcard_setoutput(0);
  537. if (res < 0) {
  538. ast_log(LOG_WARNING, "Unable to set output device\n");
  539. return -1;
  540. } else if (res > 0) {
  541. /* The device is still in read mode, and it's too soon to change it,
  542. so just pretend we wrote it */
  543. return 0;
  544. }
  545. /* We have to digest the frame in 160-byte portions */
  546. if (f->datalen > sizeof(sizbuf) - sizpos) {
  547. ast_log(LOG_WARNING, "Frame too large\n");
  548. return -1;
  549. }
  550. memcpy(sizbuf + sizpos, f->data, f->datalen);
  551. len += f->datalen;
  552. pos = 0;
  553. while(len - pos > FRAME_SIZE * 2) {
  554. soundcard_writeframe((short *)(sizbuf + pos));
  555. pos += FRAME_SIZE * 2;
  556. }
  557. if (len - pos)
  558. memmove(sizbuf, sizbuf + pos, len - pos);
  559. sizpos = len - pos;
  560. return 0;
  561. }
  562. static struct ast_frame *oss_read(struct ast_channel *chan)
  563. {
  564. static struct ast_frame f;
  565. static char buf[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
  566. static int readpos = 0;
  567. int res;
  568. #if 0
  569. ast_log(LOG_DEBUG, "oss_read()\n");
  570. #endif
  571. f.frametype = AST_FRAME_NULL;
  572. f.subclass = 0;
  573. f.samples = 0;
  574. f.datalen = 0;
  575. f.data = NULL;
  576. f.offset = 0;
  577. f.src = type;
  578. f.mallocd = 0;
  579. f.delivery.tv_sec = 0;
  580. f.delivery.tv_usec = 0;
  581. res = soundcard_setinput(0);
  582. if (res < 0) {
  583. ast_log(LOG_WARNING, "Unable to set input mode\n");
  584. return NULL;
  585. }
  586. if (res > 0) {
  587. /* Theoretically shouldn't happen, but anyway, return a NULL frame */
  588. return &f;
  589. }
  590. res = read(sounddev, buf + AST_FRIENDLY_OFFSET + readpos, FRAME_SIZE * 2 - readpos);
  591. if (res < 0) {
  592. ast_log(LOG_WARNING, "Error reading from sound device (If you're running 'artsd' then kill it): %s\n", strerror(errno));
  593. #if 0
  594. CRASH;
  595. #endif
  596. return NULL;
  597. }
  598. readpos += res;
  599. if (readpos >= FRAME_SIZE * 2) {
  600. /* A real frame */
  601. readpos = 0;
  602. if (chan->_state != AST_STATE_UP) {
  603. /* Don't transmit unless it's up */
  604. return &f;
  605. }
  606. f.frametype = AST_FRAME_VOICE;
  607. f.subclass = AST_FORMAT_SLINEAR;
  608. f.samples = FRAME_SIZE;
  609. f.datalen = FRAME_SIZE * 2;
  610. f.data = buf + AST_FRIENDLY_OFFSET;
  611. f.offset = AST_FRIENDLY_OFFSET;
  612. f.src = type;
  613. f.mallocd = 0;
  614. f.delivery.tv_sec = 0;
  615. f.delivery.tv_usec = 0;
  616. #if 0
  617. { static int fd = -1;
  618. if (fd < 0)
  619. fd = open("output.raw", O_RDWR | O_TRUNC | O_CREAT);
  620. write(fd, f.data, f.datalen);
  621. }
  622. #endif
  623. }
  624. return &f;
  625. }
  626. static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  627. {
  628. struct chan_oss_pvt *p = newchan->pvt->pvt;
  629. p->owner = newchan;
  630. return 0;
  631. }
  632. static int oss_indicate(struct ast_channel *chan, int cond)
  633. {
  634. int res;
  635. switch(cond) {
  636. case AST_CONTROL_BUSY:
  637. res = 1;
  638. break;
  639. case AST_CONTROL_CONGESTION:
  640. res = 2;
  641. break;
  642. case AST_CONTROL_RINGING:
  643. res = 0;
  644. break;
  645. case -1:
  646. cursound = -1;
  647. return 0;
  648. default:
  649. ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, chan->name);
  650. return -1;
  651. }
  652. if (res > -1) {
  653. write(sndcmd[1], &res, sizeof(res));
  654. }
  655. return 0;
  656. }
  657. static struct ast_channel *oss_new(struct chan_oss_pvt *p, int state)
  658. {
  659. struct ast_channel *tmp;
  660. tmp = ast_channel_alloc(1);
  661. if (tmp) {
  662. snprintf(tmp->name, sizeof(tmp->name), "OSS/%s", DEV_DSP + 5);
  663. tmp->type = type;
  664. tmp->fds[0] = sounddev;
  665. tmp->nativeformats = AST_FORMAT_SLINEAR;
  666. tmp->pvt->pvt = p;
  667. tmp->pvt->send_digit = oss_digit;
  668. tmp->pvt->send_text = oss_text;
  669. tmp->pvt->hangup = oss_hangup;
  670. tmp->pvt->answer = oss_answer;
  671. tmp->pvt->read = oss_read;
  672. tmp->pvt->call = oss_call;
  673. tmp->pvt->write = oss_write;
  674. tmp->pvt->indicate = oss_indicate;
  675. tmp->pvt->fixup = oss_fixup;
  676. if (strlen(p->context))
  677. strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
  678. if (strlen(p->exten))
  679. strncpy(tmp->exten, p->exten, sizeof(tmp->exten)-1);
  680. if (strlen(language))
  681. strncpy(tmp->language, language, sizeof(tmp->language)-1);
  682. p->owner = tmp;
  683. ast_setstate(tmp, state);
  684. ast_mutex_lock(&usecnt_lock);
  685. usecnt++;
  686. ast_mutex_unlock(&usecnt_lock);
  687. ast_update_use_count();
  688. if (state != AST_STATE_DOWN) {
  689. if (ast_pbx_start(tmp)) {
  690. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
  691. ast_hangup(tmp);
  692. tmp = NULL;
  693. }
  694. }
  695. }
  696. return tmp;
  697. }
  698. static struct ast_channel *oss_request(char *type, int format, void *data)
  699. {
  700. int oldformat = format;
  701. struct ast_channel *tmp;
  702. format &= AST_FORMAT_SLINEAR;
  703. if (!format) {
  704. ast_log(LOG_NOTICE, "Asked to get a channel of format '%d'\n", oldformat);
  705. return NULL;
  706. }
  707. if (oss.owner) {
  708. ast_log(LOG_NOTICE, "Already have a call on the OSS channel\n");
  709. return NULL;
  710. }
  711. tmp= oss_new(&oss, AST_STATE_DOWN);
  712. if (!tmp) {
  713. ast_log(LOG_WARNING, "Unable to create new OSS channel\n");
  714. }
  715. return tmp;
  716. }
  717. static int console_autoanswer(int fd, int argc, char *argv[])
  718. {
  719. if ((argc != 1) && (argc != 2))
  720. return RESULT_SHOWUSAGE;
  721. if (argc == 1) {
  722. ast_cli(fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
  723. return RESULT_SUCCESS;
  724. } else {
  725. if (!strcasecmp(argv[1], "on"))
  726. autoanswer = -1;
  727. else if (!strcasecmp(argv[1], "off"))
  728. autoanswer = 0;
  729. else
  730. return RESULT_SHOWUSAGE;
  731. }
  732. return RESULT_SUCCESS;
  733. }
  734. static char *autoanswer_complete(char *line, char *word, int pos, int state)
  735. {
  736. #ifndef MIN
  737. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  738. #endif
  739. switch(state) {
  740. case 0:
  741. if (strlen(word) && !strncasecmp(word, "on", MIN(strlen(word), 2)))
  742. return strdup("on");
  743. case 1:
  744. if (strlen(word) && !strncasecmp(word, "off", MIN(strlen(word), 3)))
  745. return strdup("off");
  746. default:
  747. return NULL;
  748. }
  749. return NULL;
  750. }
  751. static char autoanswer_usage[] =
  752. "Usage: autoanswer [on|off]\n"
  753. " Enables or disables autoanswer feature. If used without\n"
  754. " argument, displays the current on/off status of autoanswer.\n"
  755. " The default value of autoanswer is in 'oss.conf'.\n";
  756. static int console_answer(int fd, int argc, char *argv[])
  757. {
  758. struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
  759. if (argc != 1)
  760. return RESULT_SHOWUSAGE;
  761. if (!oss.owner) {
  762. ast_cli(fd, "No one is calling us\n");
  763. return RESULT_FAILURE;
  764. }
  765. hookstate = 1;
  766. cursound = -1;
  767. ast_queue_frame(oss.owner, &f);
  768. answer_sound();
  769. return RESULT_SUCCESS;
  770. }
  771. static char sendtext_usage[] =
  772. "Usage: send text <message>\n"
  773. " Sends a text message for display on the remote terminal.\n";
  774. static int console_sendtext(int fd, int argc, char *argv[])
  775. {
  776. int tmparg = 2;
  777. char text2send[256] = "";
  778. struct ast_frame f = { 0, };
  779. if (argc < 2)
  780. return RESULT_SHOWUSAGE;
  781. if (!oss.owner) {
  782. ast_cli(fd, "No one is calling us\n");
  783. return RESULT_FAILURE;
  784. }
  785. if (strlen(text2send))
  786. ast_cli(fd, "Warning: message already waiting to be sent, overwriting\n");
  787. text2send[0] = '\0';
  788. while(tmparg < argc) {
  789. strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send) - 1);
  790. strncat(text2send, " ", sizeof(text2send) - strlen(text2send) - 1);
  791. }
  792. if (strlen(text2send)) {
  793. text2send[strlen(text2send) - 1] = '\n';
  794. f.frametype = AST_FRAME_TEXT;
  795. f.subclass = 0;
  796. f.data = text2send;
  797. f.datalen = strlen(text2send);
  798. ast_queue_frame(oss.owner, &f);
  799. }
  800. return RESULT_SUCCESS;
  801. }
  802. static char answer_usage[] =
  803. "Usage: answer\n"
  804. " Answers an incoming call on the console (OSS) channel.\n";
  805. static int console_hangup(int fd, int argc, char *argv[])
  806. {
  807. if (argc != 1)
  808. return RESULT_SHOWUSAGE;
  809. cursound = -1;
  810. if (!oss.owner && !hookstate) {
  811. ast_cli(fd, "No call to hangup up\n");
  812. return RESULT_FAILURE;
  813. }
  814. hookstate = 0;
  815. if (oss.owner) {
  816. ast_queue_hangup(oss.owner);
  817. }
  818. return RESULT_SUCCESS;
  819. }
  820. static char hangup_usage[] =
  821. "Usage: hangup\n"
  822. " Hangs up any call currently placed on the console.\n";
  823. static int console_dial(int fd, int argc, char *argv[])
  824. {
  825. char tmp[256], *tmp2;
  826. char *mye, *myc;
  827. int x;
  828. struct ast_frame f = { AST_FRAME_DTMF, 0 };
  829. if ((argc != 1) && (argc != 2))
  830. return RESULT_SHOWUSAGE;
  831. if (oss.owner) {
  832. if (argc == 2) {
  833. for (x=0;x<strlen(argv[1]);x++) {
  834. f.subclass = argv[1][x];
  835. ast_queue_frame(oss.owner, &f);
  836. }
  837. } else {
  838. ast_cli(fd, "You're already in a call. You can use this only to dial digits until you hangup\n");
  839. return RESULT_FAILURE;
  840. }
  841. return RESULT_SUCCESS;
  842. }
  843. mye = exten;
  844. myc = context;
  845. if (argc == 2) {
  846. char *stringp=NULL;
  847. strncpy(tmp, argv[1], sizeof(tmp)-1);
  848. stringp=tmp;
  849. strsep(&stringp, "@");
  850. tmp2 = strsep(&stringp, "@");
  851. if (strlen(tmp))
  852. mye = tmp;
  853. if (tmp2 && strlen(tmp2))
  854. myc = tmp2;
  855. }
  856. if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
  857. strncpy(oss.exten, mye, sizeof(oss.exten)-1);
  858. strncpy(oss.context, myc, sizeof(oss.context)-1);
  859. hookstate = 1;
  860. oss_new(&oss, AST_STATE_RINGING);
  861. } else
  862. ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
  863. return RESULT_SUCCESS;
  864. }
  865. static char dial_usage[] =
  866. "Usage: dial [extension[@context]]\n"
  867. " Dials a given extensison (and context if specified)\n";
  868. static int console_transfer(int fd, int argc, char *argv[])
  869. {
  870. char tmp[256];
  871. char *context;
  872. if (argc != 2)
  873. return RESULT_SHOWUSAGE;
  874. if (oss.owner && oss.owner->bridge) {
  875. strncpy(tmp, argv[1], sizeof(tmp) - 1);
  876. context = strchr(tmp, '@');
  877. if (context) {
  878. *context = '\0';
  879. context++;
  880. } else
  881. context = oss.owner->context;
  882. if (ast_exists_extension(oss.owner->bridge, context, tmp, 1, oss.owner->bridge->callerid)) {
  883. ast_cli(fd, "Whee, transferring %s to %s@%s.\n",
  884. oss.owner->bridge->name, tmp, context);
  885. if (ast_async_goto(oss.owner->bridge, context, tmp, 1))
  886. ast_cli(fd, "Failed to transfer :(\n");
  887. } else {
  888. ast_cli(fd, "No such extension exists\n");
  889. }
  890. } else {
  891. ast_cli(fd, "There is no call to transfer\n");
  892. }
  893. return RESULT_SUCCESS;
  894. }
  895. static char transfer_usage[] =
  896. "Usage: transfer <extension>[@context]\n"
  897. " Transfers the currently connected call to the given extension (and\n"
  898. "context if specified)\n";
  899. static struct ast_cli_entry myclis[] = {
  900. { { "answer", NULL }, console_answer, "Answer an incoming console call", answer_usage },
  901. { { "hangup", NULL }, console_hangup, "Hangup a call on the console", hangup_usage },
  902. { { "dial", NULL }, console_dial, "Dial an extension on the console", dial_usage },
  903. { { "transfer", NULL }, console_transfer, "Transfer a call to a different extension", transfer_usage },
  904. { { "send", "text", NULL }, console_sendtext, "Send text to the remote device", sendtext_usage },
  905. { { "autoanswer", NULL }, console_autoanswer, "Sets/displays autoanswer", autoanswer_usage, autoanswer_complete }
  906. };
  907. int load_module()
  908. {
  909. int res;
  910. int x;
  911. struct ast_config *cfg;
  912. struct ast_variable *v;
  913. res = pipe(sndcmd);
  914. if (res) {
  915. ast_log(LOG_ERROR, "Unable to create pipe\n");
  916. return -1;
  917. }
  918. res = soundcard_init();
  919. if (res < 0) {
  920. if (option_verbose > 1) {
  921. ast_verbose(VERBOSE_PREFIX_2 "No sound card detected -- console channel will be unavailable\n");
  922. ast_verbose(VERBOSE_PREFIX_2 "Turn off OSS support by adding 'noload=chan_oss.so' in /etc/asterisk/modules.conf\n");
  923. }
  924. return 0;
  925. }
  926. if (!full_duplex)
  927. ast_log(LOG_WARNING, "XXX I don't work right with non-full duplex sound cards XXX\n");
  928. res = ast_channel_register(type, tdesc, AST_FORMAT_SLINEAR, oss_request);
  929. if (res < 0) {
  930. ast_log(LOG_ERROR, "Unable to register channel class '%s'\n", type);
  931. return -1;
  932. }
  933. for (x=0;x<sizeof(myclis)/sizeof(struct ast_cli_entry); x++)
  934. ast_cli_register(myclis + x);
  935. if ((cfg = ast_load(config))) {
  936. v = ast_variable_browse(cfg, "general");
  937. while(v) {
  938. if (!strcasecmp(v->name, "autoanswer"))
  939. autoanswer = ast_true(v->value);
  940. else if (!strcasecmp(v->name, "silencesuppression"))
  941. silencesuppression = ast_true(v->value);
  942. else if (!strcasecmp(v->name, "silencethreshold"))
  943. silencethreshold = atoi(v->value);
  944. else if (!strcasecmp(v->name, "context"))
  945. strncpy(context, v->value, sizeof(context)-1);
  946. else if (!strcasecmp(v->name, "language"))
  947. strncpy(language, v->value, sizeof(language)-1);
  948. else if (!strcasecmp(v->name, "extension"))
  949. strncpy(exten, v->value, sizeof(exten)-1);
  950. v=v->next;
  951. }
  952. ast_destroy(cfg);
  953. }
  954. ast_pthread_create(&sthread, NULL, sound_thread, NULL);
  955. return 0;
  956. }
  957. int unload_module()
  958. {
  959. int x;
  960. for (x=0;x<sizeof(myclis)/sizeof(struct ast_cli_entry); x++)
  961. ast_cli_unregister(myclis + x);
  962. close(sounddev);
  963. if (sndcmd[0] > 0) {
  964. close(sndcmd[0]);
  965. close(sndcmd[1]);
  966. }
  967. if (oss.owner)
  968. ast_softhangup(oss.owner, AST_SOFTHANGUP_APPUNLOAD);
  969. if (oss.owner)
  970. return -1;
  971. return 0;
  972. }
  973. char *description()
  974. {
  975. return desc;
  976. }
  977. int usecount()
  978. {
  979. int res;
  980. ast_mutex_lock(&usecnt_lock);
  981. res = usecnt;
  982. ast_mutex_unlock(&usecnt_lock);
  983. return res;
  984. }
  985. char *key()
  986. {
  987. return ASTERISK_GPL_KEY;
  988. }