2 * Asterisk -- A telephony toolkit for Linux.
4 * Copyright (C) 2002, Linux Support Services
6 * By Matthew Fredrickson <creslin@linux-support.net>
8 * This program is free software, distributed under the terms of
9 * the GNU General Public License
12 #include <asterisk/frame.h>
13 #include <asterisk/logger.h>
14 #include <asterisk/channel.h>
15 #include <asterisk/module.h>
16 #include <asterisk/channel_pvt.h>
17 #include <asterisk/options.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/config.h>
20 #include <asterisk/cli.h>
24 #include <sys/ioctl.h>
29 #include <alsa/asoundlib.h>
36 #include "alsa-monitor.h"
40 /* Which device to use */
41 #define ALSA_INDEV "default"
42 #define ALSA_OUTDEV "default"
43 #define DESIRED_RATE 8000
45 /* Lets use 160 sample frames, just like GSM. */
46 #define FRAME_SIZE 160
47 #define PERIOD_FRAMES 80 /* 80 Frames, at 2 bytes each */
49 /* When you set the frame size, you have to come up with
50 the right buffer format as well. */
51 /* 5 64-byte frames = one frame */
52 #define BUFFER_FMT ((buffersize * 10) << 16) | (0x0006);
54 /* Don't switch between read/write modes faster than every 300 ms */
55 #define MIN_SWITCH_TIME 600
57 static snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
58 //static int block = O_NONBLOCK;
59 static char indevname[50] = ALSA_INDEV;
60 static char outdevname[50] = ALSA_OUTDEV;
62 static struct timeval lasttime;
65 static int needanswer = 0;
66 static int needringing = 0;
67 static int needhangup = 0;
68 static int silencesuppression = 0;
69 static int silencethreshold = 1000;
71 static char digits[80] = "";
72 static char text2send[80] = "";
74 static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
76 static char *type = "Console";
77 static char *desc = "ALSA Console Channel Driver";
78 static char *tdesc = "ALSA Console Channel Driver";
79 static char *config = "alsa.conf";
81 static char context[AST_MAX_EXTENSION] = "default";
82 static char language[MAX_LANGUAGE] = "";
83 static char exten[AST_MAX_EXTENSION] = "s";
90 static short silence[FRAME_SIZE] = {0, };
101 static struct sound sounds[] = {
102 { AST_CONTROL_RINGING, ringtone, sizeof(ringtone)/2, 16000, 32000, 1 },
103 { AST_CONTROL_BUSY, busy, sizeof(busy)/2, 4000, 4000, 1 },
104 { AST_CONTROL_CONGESTION, busy, sizeof(busy)/2, 2000, 2000, 1 },
105 { AST_CONTROL_RING, ring10, sizeof(ring10)/2, 16000, 32000, 1 },
106 { AST_CONTROL_ANSWER, answer, sizeof(answer)/2, 2200, 0, 0 },
109 /* Sound command pipe */
110 static int sndcmd[2];
112 static struct chan_alsa_pvt {
113 /* We only have one ALSA structure -- near sighted perhaps, but it
114 keeps this driver as simple as possible -- as it should be. */
115 struct ast_channel *owner;
116 char exten[AST_MAX_EXTENSION];
117 char context[AST_MAX_EXTENSION];
121 snd_pcm_t *icard, *ocard;
125 static int time_has_passed(void)
129 gettimeofday(&tv, NULL);
130 ms = (tv.tv_sec - lasttime.tv_sec) * 1000 +
131 (tv.tv_usec - lasttime.tv_usec) / 1000;
132 if (ms > MIN_SWITCH_TIME)
137 /* Number of buffers... Each is FRAMESIZE/8 ms long. For example
138 with 160 sample frames, and a buffer size of 3, we have a 60ms buffer,
143 #define MAX_BUFFER_SIZE 100
144 //static int buffersize = 3;
146 //static int full_duplex = 0;
148 /* Are we reading or writing (simulated full duplex) */
149 //static int readmode = 1;
151 /* File descriptors for sound device */
152 static int readdev = -1;
153 static int writedev = -1;
155 static int autoanswer = 1;
157 static int calc_loudness(short *frame)
161 for (x=0;x<FRAME_SIZE;x++) {
167 sum = sum/FRAME_SIZE;
171 static int cursound = -1;
172 static int sampsent = 0;
173 static int silencelen=0;
175 static int nosound=0;
177 static int send_sound(void)
179 short myframe[FRAME_SIZE];
180 int total = FRAME_SIZE;
185 snd_pcm_state_t state;
189 if (sampsent < sounds[cursound].samplen) {
193 if (amt > (sounds[cursound].datalen - offset))
194 amt = sounds[cursound].datalen - offset;
195 memcpy(myframe + myoff, sounds[cursound].data + offset, amt * 2);
200 if (offset >= sounds[cursound].datalen)
203 /* Set it up for silence */
204 if (sampsent >= sounds[cursound].samplen)
205 silencelen = sounds[cursound].silencelen;
208 if (silencelen > 0) {
212 if (sounds[cursound].repeat) {
224 if (res == 0 || !frame) {
228 alsa_monitor_write((char *)frame, res * 2);
230 state = snd_pcm_state(alsa.ocard);
231 if (state == SND_PCM_STATE_XRUN) {
232 snd_pcm_prepare(alsa.ocard);
234 res = snd_pcm_writei(alsa.ocard, frame, res);
242 static void *sound_thread(void *unused)
252 FD_SET(sndcmd[0], &rfds);
254 FD_SET(writedev, &wfds);
260 FD_SET(readdev, &rfds);
265 res = ast_select(max + 1, &rfds, &wfds, NULL, NULL);
267 ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
271 if (FD_ISSET(readdev, &rfds)) {
272 /* Keep the pipe going with read audio */
273 snd_pcm_state_t state;
274 short buf[FRAME_SIZE];
277 state = snd_pcm_state(alsa.ocard);
278 if (state == SND_PCM_STATE_XRUN) {
279 snd_pcm_prepare(alsa.ocard);
281 r = snd_pcm_readi(alsa.icard, buf, FRAME_SIZE);
284 ast_log(LOG_ERROR, "XRUN read\n");
286 snd_pcm_prepare(alsa.icard);
287 } else if (r == -ESTRPIPE) {
288 ast_log(LOG_ERROR, "-ESTRPIPE\n");
289 snd_pcm_prepare(alsa.icard);
291 ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
293 alsa_monitor_read((char *)buf, r * 2);
296 if (FD_ISSET(sndcmd[0], &rfds)) {
297 read(sndcmd[0], &cursound, sizeof(cursound));
302 if (FD_ISSET(writedev, &wfds))
304 ast_log(LOG_WARNING, "Failed to write sound\n");
310 static snd_pcm_t *alsa_card_init(char *dev, snd_pcm_stream_t stream)
313 snd_pcm_t *handle = NULL;
314 snd_pcm_hw_params_t *hwparams = NULL;
315 snd_pcm_sw_params_t *swparams = NULL;
317 int period_size = PERIOD_FRAMES * 4;
318 //int period_bytes = 0;
321 unsigned int rate = DESIRED_RATE;
322 unsigned int per_min = 1;
323 //unsigned int per_max = 8;
324 snd_pcm_uframes_t start_threshold, stop_threshold;
326 err = snd_pcm_open(&handle, dev, stream, O_NONBLOCK);
328 ast_log(LOG_ERROR, "snd_pcm_open failed: %s\n", snd_strerror(err));
331 ast_log(LOG_DEBUG, "Opening device %s in %s mode\n", dev, (stream == SND_PCM_STREAM_CAPTURE) ? "read" : "write");
334 snd_pcm_hw_params_alloca(&hwparams);
335 snd_pcm_hw_params_any(handle, hwparams);
337 err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
339 ast_log(LOG_ERROR, "set_access failed: %s\n", snd_strerror(err));
342 err = snd_pcm_hw_params_set_format(handle, hwparams, format);
344 ast_log(LOG_ERROR, "set_format failed: %s\n", snd_strerror(err));
347 err = snd_pcm_hw_params_set_channels(handle, hwparams, 1);
349 ast_log(LOG_ERROR, "set_channels failed: %s\n", snd_strerror(err));
352 rate = snd_pcm_hw_params_set_rate_near(handle, hwparams, rate, 0);
354 if (rate != DESIRED_RATE) {
355 ast_log(LOG_WARNING, "Rate not correct, requested %d, got %d\n", DESIRED_RATE, rate);
358 err = snd_pcm_hw_params_set_period_size_near(handle, hwparams, period_size, 0);
360 ast_log(LOG_ERROR, "period_size(%d frames) is bad: %s\n", period_size, snd_strerror(err));
362 ast_log(LOG_DEBUG, "Period size is %d\n", err);
366 buffer_size = 4096 * 2; //period_size * 16;
367 err = snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, buffer_size);
369 ast_log(LOG_WARNING, "Problem setting buffer size of %d: %s\n", buffer_size, snd_strerror(err));
371 ast_log(LOG_DEBUG, "Buffer size is set to %d frames\n", err);
375 err = snd_pcm_hw_params_set_periods_min(handle, hwparams, &per_min, 0);
377 ast_log(LOG_ERROR, "periods_min: %s\n", snd_strerror(err));
381 err = snd_pcm_hw_params_set_periods_max(handle, hwparams, &per_max, 0);
383 ast_log(LOG_ERROR, "periods_max: %s\n", snd_strerror(err));
387 err = snd_pcm_hw_params(handle, hwparams);
389 ast_log(LOG_ERROR, "Couldn't set the new hw params: %s\n", snd_strerror(err));
392 snd_pcm_sw_params_alloca(&swparams);
393 snd_pcm_sw_params_current(handle, swparams);
396 if (stream == SND_PCM_STREAM_PLAYBACK) {
397 start_threshold = period_size;
402 err = snd_pcm_sw_params_set_start_threshold(handle, swparams, start_threshold);
404 ast_log(LOG_ERROR, "start threshold: %s\n", snd_strerror(err));
409 if (stream == SND_PCM_STREAM_PLAYBACK) {
410 stop_threshold = buffer_size;
412 stop_threshold = buffer_size;
414 err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, stop_threshold);
416 ast_log(LOG_ERROR, "stop threshold: %s\n", snd_strerror(err));
420 err = snd_pcm_sw_params_set_xfer_align(handle, swparams, PERIOD_FRAMES);
422 ast_log(LOG_ERROR, "Unable to set xfer alignment: %s\n", snd_strerror(err));
426 err = snd_pcm_sw_params_set_silence_threshold(handle, swparams, buffer_size);
428 ast_log(LOG_ERROR, "Unable to set silence threshold: %s\n", snd_strerror(err));
431 err = snd_pcm_sw_params(handle, swparams);
433 ast_log(LOG_ERROR, "sw_params: %s\n", snd_strerror(err));
436 err = snd_pcm_poll_descriptors_count(handle);
438 ast_log(LOG_ERROR, "Unable to get a poll descriptors count, error is %s\n", snd_strerror(err));
442 ast_log(LOG_DEBUG, "Can't handle more than one device\n");
445 snd_pcm_poll_descriptors(handle, &pfd, err);
446 ast_log(LOG_DEBUG, "Acquired fd %d from the poll descriptor\n", pfd.fd);
448 if (stream == SND_PCM_STREAM_CAPTURE)
456 static int soundcard_init(void)
458 alsa.icard = alsa_card_init(indevname, SND_PCM_STREAM_CAPTURE);
459 alsa.ocard = alsa_card_init(outdevname, SND_PCM_STREAM_PLAYBACK);
461 if (!alsa.icard || !alsa.ocard) {
462 ast_log(LOG_ERROR, "Problem opening alsa I/O devices\n");
469 static int alsa_digit(struct ast_channel *c, char digit)
471 ast_verbose( " << Console Received digit %c >> \n", digit);
475 static int alsa_text(struct ast_channel *c, char *text)
477 ast_verbose( " << Console Received text %s >> \n", text);
481 static int alsa_call(struct ast_channel *c, char *dest, int timeout)
484 ast_verbose( " << Call placed to '%s' on console >> \n", dest);
486 ast_verbose( " << Auto-answered >> \n" );
489 ast_verbose( " << Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
491 write(sndcmd[1], &res, sizeof(res));
496 static void answer_sound(void)
501 write(sndcmd[1], &res, sizeof(res));
505 static int alsa_answer(struct ast_channel *c)
507 ast_verbose( " << Console call has been answered >> \n");
509 ast_setstate(c, AST_STATE_UP);
514 static int alsa_hangup(struct ast_channel *c)
520 ast_verbose( " << Hangup on console >> \n");
521 ast_pthread_mutex_lock(&usecnt_lock);
523 ast_pthread_mutex_unlock(&usecnt_lock);
528 write(sndcmd[1], &res, sizeof(res));
534 static int soundcard_writeframe(short *data)
536 /* Write an exactly FRAME_SIZE sized of frame */
537 static int bufcnt = 0;
538 static short buffer[FRAME_SIZE * MAX_BUFFER_SIZE * 5];
539 struct audio_buf_info info;
543 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info)) {
545 ast_log(LOG_WARNING, "Error reading output space\n");
549 if ((info.fragments >= buffersize * 5) && (bufcnt == buffersize)) {
550 /* We've run out of stuff, buffer again */
553 if (bufcnt == buffersize) {
554 /* Write sample immediately */
555 res = write(fd, ((void *)data), FRAME_SIZE * 2);
557 /* Copy the data into our buffer */
558 res = FRAME_SIZE * 2;
559 memcpy(buffer + (bufcnt * FRAME_SIZE), data, FRAME_SIZE * 2);
561 if (bufcnt == buffersize) {
562 res = write(fd, ((void *)buffer), FRAME_SIZE * 2 * buffersize);
569 static int alsa_write(struct ast_channel *chan, struct ast_frame *f)
572 static char sizbuf[8000];
573 static int sizpos = 0;
577 snd_pcm_state_t state;
578 /* Immediately return if no sound is enabled */
581 /* Stop any currently playing sound */
582 if (cursound != -1) {
583 snd_pcm_drop(alsa.ocard);
584 snd_pcm_prepare(alsa.ocard);
589 /* We have to digest the frame in 160-byte portions */
590 if (f->datalen > sizeof(sizbuf) - sizpos) {
591 ast_log(LOG_WARNING, "Frame too large\n");
594 memcpy(sizbuf + sizpos, f->data, f->datalen);
598 alsa_monitor_write(sizbuf, len);
600 state = snd_pcm_state(alsa.ocard);
601 if (state == SND_PCM_STATE_XRUN) {
602 snd_pcm_prepare(alsa.ocard);
604 res = snd_pcm_writei(alsa.ocard, sizbuf, len/2);
607 ast_log(LOG_DEBUG, "XRUN write\n");
609 snd_pcm_prepare(alsa.ocard);
610 res = snd_pcm_writei(alsa.ocard, sizbuf, len/2);
612 ast_log(LOG_ERROR, "Write error: %s\n", snd_strerror(res));
614 } else if (res < 0) {
615 ast_log(LOG_ERROR, "Write error %s\n", snd_strerror(res));
619 if (res == -ESTRPIPE) {
620 ast_log(LOG_ERROR, "You've got some big problems\n");
628 static struct ast_frame *alsa_read(struct ast_channel *chan)
630 static struct ast_frame f;
631 static short __buf[FRAME_SIZE + AST_FRIENDLY_OFFSET/2];
633 static int readpos = 0;
634 static int left = FRAME_SIZE;
638 snd_pcm_state_t state;
642 /* Acknowledge any pending cmd */
643 res = read(cmd[0], &b, sizeof(b));
647 f.frametype = AST_FRAME_NULL;
657 f.frametype = AST_FRAME_CONTROL;
658 f.subclass = AST_CONTROL_RINGING;
667 if (strlen(text2send)) {
668 f.frametype = AST_FRAME_TEXT;
671 f.datalen = strlen(text2send);
672 strcpy(text2send,"");
675 if (strlen(digits)) {
676 f.frametype = AST_FRAME_DTMF;
677 f.subclass = digits[0];
678 for (res=0;res<strlen(digits);res++)
679 digits[res] = digits[res + 1];
685 f.frametype = AST_FRAME_CONTROL;
686 f.subclass = AST_CONTROL_ANSWER;
687 ast_setstate(chan, AST_STATE_UP);
695 state = snd_pcm_state(alsa.ocard);
696 if (state == SND_PCM_STATE_XRUN) {
697 snd_pcm_prepare(alsa.ocard);
700 buf = __buf + AST_FRIENDLY_OFFSET/2;
702 r = snd_pcm_readi(alsa.icard, buf + readpos, left);
705 ast_log(LOG_ERROR, "XRUN read\n");
707 snd_pcm_prepare(alsa.icard);
708 } else if (r == -ESTRPIPE) {
709 ast_log(LOG_ERROR, "-ESTRPIPE\n");
710 snd_pcm_prepare(alsa.icard);
712 ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
717 /* Update positions */
721 if (readpos >= FRAME_SIZE) {
725 if (chan->_state != AST_STATE_UP) {
726 /* Don't transmit unless it's up */
729 f.frametype = AST_FRAME_VOICE;
730 f.subclass = AST_FORMAT_SLINEAR;
731 f.samples = FRAME_SIZE;
732 f.datalen = FRAME_SIZE * 2;
734 f.offset = AST_FRIENDLY_OFFSET;
738 alsa_monitor_read((char *)buf, FRAME_SIZE * 2);
742 { static int fd = -1;
744 fd = open("output.raw", O_RDWR | O_TRUNC | O_CREAT);
745 write(fd, f.data, f.datalen);
752 static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
754 struct chan_alsa_pvt *p = newchan->pvt->pvt;
759 static int alsa_indicate(struct ast_channel *chan, int cond)
763 case AST_CONTROL_BUSY:
766 case AST_CONTROL_CONGESTION:
769 case AST_CONTROL_RINGING:
773 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, chan->name);
777 write(sndcmd[1], &res, sizeof(res));
782 static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state)
784 struct ast_channel *tmp;
785 tmp = ast_channel_alloc(0);
787 snprintf(tmp->name, sizeof(tmp->name), "ALSA/%s", indevname);
789 tmp->fds[0] = readdev;
790 tmp->fds[1] = cmd[0];
791 tmp->nativeformats = AST_FORMAT_SLINEAR;
793 tmp->pvt->send_digit = alsa_digit;
794 tmp->pvt->send_text = alsa_text;
795 tmp->pvt->hangup = alsa_hangup;
796 tmp->pvt->answer = alsa_answer;
797 tmp->pvt->read = alsa_read;
798 tmp->pvt->call = alsa_call;
799 tmp->pvt->write = alsa_write;
800 tmp->pvt->indicate = alsa_indicate;
801 tmp->pvt->fixup = alsa_fixup;
802 if (strlen(p->context))
803 strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
804 if (strlen(p->exten))
805 strncpy(tmp->exten, p->exten, sizeof(tmp->exten)-1);
806 if (strlen(language))
807 strncpy(tmp->language, language, sizeof(tmp->language)-1);
809 ast_setstate(tmp, state);
810 ast_pthread_mutex_lock(&usecnt_lock);
812 ast_pthread_mutex_unlock(&usecnt_lock);
813 ast_update_use_count();
814 if (state != AST_STATE_DOWN) {
815 if (ast_pbx_start(tmp)) {
816 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
825 static struct ast_channel *alsa_request(char *type, int format, void *data)
827 int oldformat = format;
828 struct ast_channel *tmp;
829 format &= AST_FORMAT_SLINEAR;
831 ast_log(LOG_NOTICE, "Asked to get a channel of format '%d'\n", oldformat);
835 ast_log(LOG_NOTICE, "Already have a call on the ALSA channel\n");
838 tmp= alsa_new(&alsa, AST_STATE_DOWN);
840 ast_log(LOG_WARNING, "Unable to create new ALSA channel\n");
845 static int console_autoanswer(int fd, int argc, char *argv[])
847 if ((argc != 1) && (argc != 2))
848 return RESULT_SHOWUSAGE;
850 ast_cli(fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
851 return RESULT_SUCCESS;
853 if (!strcasecmp(argv[1], "on"))
855 else if (!strcasecmp(argv[1], "off"))
858 return RESULT_SHOWUSAGE;
860 return RESULT_SUCCESS;
863 static char *autoanswer_complete(char *line, char *word, int pos, int state)
866 #define MIN(a,b) ((a) < (b) ? (a) : (b))
870 if (strlen(word) && !strncasecmp(word, "on", MIN(strlen(word), 2)))
873 if (strlen(word) && !strncasecmp(word, "off", MIN(strlen(word), 3)))
874 return strdup("off");
881 static char autoanswer_usage[] =
882 "Usage: autoanswer [on|off]\n"
883 " Enables or disables autoanswer feature. If used without\n"
884 " argument, displays the current on/off status of autoanswer.\n"
885 " The default value of autoanswer is in 'alsa.conf'.\n";
887 static int console_answer(int fd, int argc, char *argv[])
890 return RESULT_SHOWUSAGE;
892 ast_cli(fd, "No one is calling us\n");
893 return RESULT_FAILURE;
899 return RESULT_SUCCESS;
902 static char sendtext_usage[] =
903 "Usage: send text <message>\n"
904 " Sends a text message for display on the remote terminal.\n";
906 static int console_sendtext(int fd, int argc, char *argv[])
910 return RESULT_SHOWUSAGE;
912 ast_cli(fd, "No one is calling us\n");
913 return RESULT_FAILURE;
915 if (strlen(text2send))
916 ast_cli(fd, "Warning: message already waiting to be sent, overwriting\n");
917 strcpy(text2send, "");
918 while(tmparg <= argc) {
919 strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send));
920 strncat(text2send, " ", sizeof(text2send) - strlen(text2send));
923 return RESULT_SUCCESS;
926 static char answer_usage[] =
928 " Answers an incoming call on the console (ALSA) channel.\n";
930 static int console_hangup(int fd, int argc, char *argv[])
933 return RESULT_SHOWUSAGE;
935 if (!alsa.owner && !hookstate) {
936 ast_cli(fd, "No call to hangup up\n");
937 return RESULT_FAILURE;
942 return RESULT_SUCCESS;
945 static char hangup_usage[] =
947 " Hangs up any call currently placed on the console.\n";
950 static int console_dial(int fd, int argc, char *argv[])
952 char tmp[256], *tmp2;
955 if ((argc != 1) && (argc != 2))
956 return RESULT_SHOWUSAGE;
959 strncat(digits, argv[1], sizeof(digits) - strlen(digits));
960 /* Wake up the polling thread */
961 write(cmd[1], &b, sizeof(b));
963 ast_cli(fd, "You're already in a call. You can use this only to dial digits until you hangup\n");
964 return RESULT_FAILURE;
966 return RESULT_SUCCESS;
972 strncpy(tmp, argv[1], sizeof(tmp)-1);
974 strsep(&stringp, "@");
975 tmp2 = strsep(&stringp, "@");
978 if (tmp2 && strlen(tmp2))
981 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
982 strncpy(alsa.exten, mye, sizeof(alsa.exten)-1);
983 strncpy(alsa.context, myc, sizeof(alsa.context)-1);
985 alsa_new(&alsa, AST_STATE_UP);
987 ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
988 return RESULT_SUCCESS;
991 static char dial_usage[] =
992 "Usage: dial [extension[@context]]\n"
993 " Dials a given extensison (";
996 static struct ast_cli_entry myclis[] = {
997 { { "answer", NULL }, console_answer, "Answer an incoming console call", answer_usage },
998 { { "hangup", NULL }, console_hangup, "Hangup a call on the console", hangup_usage },
999 { { "dial", NULL }, console_dial, "Dial an extension on the console", dial_usage },
1000 { { "send", "text", NULL }, console_sendtext, "Send text to the remote device", sendtext_usage },
1001 { { "autoanswer", NULL }, console_autoanswer, "Sets/displays autoanswer", autoanswer_usage, autoanswer_complete }
1009 struct ast_config *cfg;
1010 struct ast_variable *v;
1014 ast_log(LOG_ERROR, "Unable to create pipe\n");
1017 flags = fcntl(cmd[0], F_GETFL);
1018 fcntl(cmd[0], F_SETFL, flags | O_NONBLOCK);
1019 flags = fcntl(cmd[1], F_GETFL);
1020 fcntl(cmd[1], F_SETFL, flags | O_NONBLOCK);
1021 res = soundcard_init();
1025 if (option_verbose > 1) {
1026 ast_verbose(VERBOSE_PREFIX_2 "No sound card detected -- console channel will be unavailable\n");
1027 ast_verbose(VERBOSE_PREFIX_2 "Turn off ALSA support by adding 'noload=chan_alsa.so' in /etc/asterisk/modules.conf\n");
1033 ast_log(LOG_WARNING, "XXX I don't work right with non-full duplex sound cards XXX\n");
1035 res = ast_channel_register(type, tdesc, AST_FORMAT_SLINEAR, alsa_request);
1037 ast_log(LOG_ERROR, "Unable to register channel class '%s'\n", type);
1040 for (x=0;x<sizeof(myclis)/sizeof(struct ast_cli_entry); x++)
1041 ast_cli_register(myclis + x);
1042 if ((cfg = ast_load(config))) {
1043 v = ast_variable_browse(cfg, "general");
1045 if (!strcasecmp(v->name, "autoanswer"))
1046 autoanswer = ast_true(v->value);
1047 else if (!strcasecmp(v->name, "silencesuppression"))
1048 silencesuppression = ast_true(v->value);
1049 else if (!strcasecmp(v->name, "silencethreshold"))
1050 silencethreshold = atoi(v->value);
1051 else if (!strcasecmp(v->name, "context"))
1052 strncpy(context, v->value, sizeof(context)-1);
1053 else if (!strcasecmp(v->name, "language"))
1054 strncpy(language, v->value, sizeof(language)-1);
1055 else if (!strcasecmp(v->name, "extension"))
1056 strncpy(exten, v->value, sizeof(exten)-1);
1057 else if (!strcasecmp(v->name, "input_device"))
1058 strncpy(indevname, v->value, sizeof(indevname)-1);
1059 else if (!strcasecmp(v->name, "output_device"))
1060 strncpy(outdevname, v->value, sizeof(outdevname)-1);
1065 pthread_create(&sthread, NULL, sound_thread, NULL);
1067 if (alsa_monitor_start()) {
1068 ast_log(LOG_ERROR, "Problem starting Monitoring\n");
1079 for (x=0;x<sizeof(myclis)/sizeof(struct ast_cli_entry); x++)
1080 ast_cli_unregister(myclis + x);
1087 if (sndcmd[0] > 0) {
1092 ast_softhangup(alsa.owner, AST_SOFTHANGUP_APPUNLOAD);
1106 ast_pthread_mutex_lock(&usecnt_lock);
1108 ast_pthread_mutex_unlock(&usecnt_lock);
1114 return ASTERISK_GPL_KEY;