2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * By Matthew Fredrickson <creslin@digium.com>
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.
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.
20 * \brief ALSA sound card channel driver
22 * \author Matthew Fredrickson <creslin@digium.com>
27 * \ingroup channel_drivers
31 <depend>asound</depend>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41 #include <sys/ioctl.h>
47 #define ALSA_PCM_NEW_HW_PARAMS_API
48 #define ALSA_PCM_NEW_SW_PARAMS_API
49 #include <alsa/asoundlib.h>
51 #include "asterisk/frame.h"
52 #include "asterisk/logger.h"
53 #include "asterisk/channel.h"
54 #include "asterisk/module.h"
55 #include "asterisk/options.h"
56 #include "asterisk/pbx.h"
57 #include "asterisk/config.h"
58 #include "asterisk/cli.h"
59 #include "asterisk/utils.h"
60 #include "asterisk/causes.h"
61 #include "asterisk/endian.h"
62 #include "asterisk/stringfields.h"
63 #include "asterisk/abstract_jb.h"
64 #include "asterisk/musiconhold.h"
72 #include "alsa-monitor.h"
75 /*! Global jitterbuffer configuration - by default, jb is disabled */
76 static struct ast_jb_conf default_jbconf = {
79 .resync_threshold = -1,
82 static struct ast_jb_conf global_jbconf;
85 /* Which device to use */
86 #define ALSA_INDEV "hw:0,0"
87 #define ALSA_OUTDEV "hw:0,0"
88 #define DESIRED_RATE 8000
90 /* Lets use 160 sample frames, just like GSM. */
91 #define FRAME_SIZE 160
92 #define PERIOD_FRAMES 80 /* 80 Frames, at 2 bytes each */
94 /* When you set the frame size, you have to come up with
95 the right buffer format as well. */
96 /* 5 64-byte frames = one frame */
97 #define BUFFER_FMT ((buffersize * 10) << 16) | (0x0006);
99 /* Don't switch between read/write modes faster than every 300 ms */
100 #define MIN_SWITCH_TIME 600
102 #if __BYTE_ORDER == __LITTLE_ENDIAN
103 static snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
105 static snd_pcm_format_t format = SND_PCM_FORMAT_S16_BE;
108 /* static int block = O_NONBLOCK; */
109 static char indevname[50] = ALSA_INDEV;
110 static char outdevname[50] = ALSA_OUTDEV;
113 static struct timeval lasttime;
117 static int silencesuppression = 0;
118 static int silencethreshold = 1000;
120 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
121 AST_MUTEX_DEFINE_STATIC(alsalock);
123 static const char tdesc[] = "ALSA Console Channel Driver";
124 static const char config[] = "alsa.conf";
126 static char context[AST_MAX_CONTEXT] = "default";
127 static char language[MAX_LANGUAGE] = "";
128 static char exten[AST_MAX_EXTENSION] = "s";
129 static char mohinterpret[MAX_MUSICCLASS];
131 static int hookstate = 0;
133 static short silence[FRAME_SIZE] = { 0, };
144 static struct sound sounds[] = {
145 {AST_CONTROL_RINGING, ringtone, sizeof(ringtone) / 2, 16000, 32000, 1},
146 {AST_CONTROL_BUSY, busy, sizeof(busy) / 2, 4000, 4000, 1},
147 {AST_CONTROL_CONGESTION, busy, sizeof(busy) / 2, 2000, 2000, 1},
148 {AST_CONTROL_RING, ring10, sizeof(ring10) / 2, 16000, 32000, 1},
149 {AST_CONTROL_ANSWER, answer, sizeof(answer) / 2, 2200, 0, 0},
152 /* Sound command pipe */
153 static int sndcmd[2];
155 static struct chan_alsa_pvt {
156 /* We only have one ALSA structure -- near sighted perhaps, but it
157 keeps this driver as simple as possible -- as it should be. */
158 struct ast_channel *owner;
159 char exten[AST_MAX_EXTENSION];
160 char context[AST_MAX_CONTEXT];
164 snd_pcm_t *icard, *ocard;
168 /* Number of buffers... Each is FRAMESIZE/8 ms long. For example
169 with 160 sample frames, and a buffer size of 3, we have a 60ms buffer,
174 #define MAX_BUFFER_SIZE 100
176 /* File descriptors for sound device */
177 static int readdev = -1;
178 static int writedev = -1;
180 static int autoanswer = 1;
182 static int cursound = -1;
183 static int sampsent = 0;
184 static int silencelen = 0;
185 static int offset = 0;
186 static int nosound = 0;
189 static struct ast_channel *alsa_request(const char *type, int format, void *data, int *cause);
190 static int alsa_digit(struct ast_channel *c, char digit);
191 static int alsa_text(struct ast_channel *c, const char *text);
192 static int alsa_hangup(struct ast_channel *c);
193 static int alsa_answer(struct ast_channel *c);
194 static struct ast_frame *alsa_read(struct ast_channel *chan);
195 static int alsa_call(struct ast_channel *c, char *dest, int timeout);
196 static int alsa_write(struct ast_channel *chan, struct ast_frame *f);
197 static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
198 static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
200 static const struct ast_channel_tech alsa_tech = {
202 .description = tdesc,
203 .capabilities = AST_FORMAT_SLINEAR,
204 .requester = alsa_request,
205 .send_digit_end = alsa_digit,
206 .send_text = alsa_text,
207 .hangup = alsa_hangup,
208 .answer = alsa_answer,
212 .indicate = alsa_indicate,
216 static int send_sound(void)
218 short myframe[FRAME_SIZE];
219 int total = FRAME_SIZE;
221 int amt = 0, res, myoff;
222 snd_pcm_state_t state;
228 if (sampsent < sounds[cursound].samplen) {
232 if (amt > (sounds[cursound].datalen - offset))
233 amt = sounds[cursound].datalen - offset;
234 memcpy(myframe + myoff, sounds[cursound].data + offset, amt * 2);
239 if (offset >= sounds[cursound].datalen)
242 /* Set it up for silence */
243 if (sampsent >= sounds[cursound].samplen)
244 silencelen = sounds[cursound].silencelen;
247 if (silencelen > 0) {
251 if (sounds[cursound].repeat) {
263 if (res == 0 || !frame)
267 alsa_monitor_write((char *) frame, res * 2);
269 state = snd_pcm_state(alsa.ocard);
270 if (state == SND_PCM_STATE_XRUN)
271 snd_pcm_prepare(alsa.ocard);
272 res = snd_pcm_writei(alsa.ocard, frame, res);
278 static void *sound_thread(void *unused)
288 FD_SET(sndcmd[0], &rfds);
290 FD_SET(writedev, &wfds);
296 FD_SET(readdev, &rfds);
301 res = ast_select(max + 1, &rfds, &wfds, NULL, NULL);
303 ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
307 if (FD_ISSET(readdev, &rfds)) {
308 /* Keep the pipe going with read audio */
309 snd_pcm_state_t state;
310 short buf[FRAME_SIZE];
313 state = snd_pcm_state(alsa.ocard);
314 if (state == SND_PCM_STATE_XRUN) {
315 snd_pcm_prepare(alsa.ocard);
317 r = snd_pcm_readi(alsa.icard, buf, FRAME_SIZE);
320 ast_log(LOG_ERROR, "XRUN read\n");
322 snd_pcm_prepare(alsa.icard);
323 } else if (r == -ESTRPIPE) {
324 ast_log(LOG_ERROR, "-ESTRPIPE\n");
325 snd_pcm_prepare(alsa.icard);
327 ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
329 alsa_monitor_read((char *) buf, r * 2);
332 if (FD_ISSET(sndcmd[0], &rfds)) {
333 read(sndcmd[0], &cursound, sizeof(cursound));
338 if (FD_ISSET(writedev, &wfds))
340 ast_log(LOG_WARNING, "Failed to write sound\n");
346 static snd_pcm_t *alsa_card_init(char *dev, snd_pcm_stream_t stream)
350 snd_pcm_t *handle = NULL;
351 snd_pcm_hw_params_t *hwparams = NULL;
352 snd_pcm_sw_params_t *swparams = NULL;
354 snd_pcm_uframes_t period_size = PERIOD_FRAMES * 4;
355 /* int period_bytes = 0; */
356 snd_pcm_uframes_t buffer_size = 0;
358 unsigned int rate = DESIRED_RATE;
360 unsigned int per_min = 1;
362 /* unsigned int per_max = 8; */
363 snd_pcm_uframes_t start_threshold, stop_threshold;
365 err = snd_pcm_open(&handle, dev, stream, O_NONBLOCK);
367 ast_log(LOG_ERROR, "snd_pcm_open failed: %s\n", snd_strerror(err));
370 ast_log(LOG_DEBUG, "Opening device %s in %s mode\n", dev, (stream == SND_PCM_STREAM_CAPTURE) ? "read" : "write");
372 snd_pcm_hw_params_alloca(&hwparams);
373 snd_pcm_hw_params_any(handle, hwparams);
375 err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
377 ast_log(LOG_ERROR, "set_access failed: %s\n", snd_strerror(err));
379 err = snd_pcm_hw_params_set_format(handle, hwparams, format);
381 ast_log(LOG_ERROR, "set_format failed: %s\n", snd_strerror(err));
383 err = snd_pcm_hw_params_set_channels(handle, hwparams, 1);
385 ast_log(LOG_ERROR, "set_channels failed: %s\n", snd_strerror(err));
388 err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &rate, &direction);
389 if (rate != DESIRED_RATE)
390 ast_log(LOG_WARNING, "Rate not correct, requested %d, got %d\n", DESIRED_RATE, rate);
393 err = snd_pcm_hw_params_set_period_size_near(handle, hwparams, &period_size, &direction);
395 ast_log(LOG_ERROR, "period_size(%ld frames) is bad: %s\n", period_size, snd_strerror(err));
397 ast_log(LOG_DEBUG, "Period size is %d\n", err);
399 buffer_size = 4096 * 2; /* period_size * 16; */
400 err = snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &buffer_size);
402 ast_log(LOG_WARNING, "Problem setting buffer size of %ld: %s\n", buffer_size, snd_strerror(err));
404 ast_log(LOG_DEBUG, "Buffer size is set to %d frames\n", err);
408 err = snd_pcm_hw_params_set_periods_min(handle, hwparams, &per_min, &direction);
410 ast_log(LOG_ERROR, "periods_min: %s\n", snd_strerror(err));
412 err = snd_pcm_hw_params_set_periods_max(handle, hwparams, &per_max, 0);
414 ast_log(LOG_ERROR, "periods_max: %s\n", snd_strerror(err));
417 err = snd_pcm_hw_params(handle, hwparams);
419 ast_log(LOG_ERROR, "Couldn't set the new hw params: %s\n", snd_strerror(err));
421 snd_pcm_sw_params_alloca(&swparams);
422 snd_pcm_sw_params_current(handle, swparams);
425 if (stream == SND_PCM_STREAM_PLAYBACK)
426 start_threshold = period_size;
430 err = snd_pcm_sw_params_set_start_threshold(handle, swparams, start_threshold);
432 ast_log(LOG_ERROR, "start threshold: %s\n", snd_strerror(err));
436 if (stream == SND_PCM_STREAM_PLAYBACK)
437 stop_threshold = buffer_size;
439 stop_threshold = buffer_size;
441 err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, stop_threshold);
443 ast_log(LOG_ERROR, "stop threshold: %s\n", snd_strerror(err));
446 err = snd_pcm_sw_params_set_xfer_align(handle, swparams, PERIOD_FRAMES);
448 ast_log(LOG_ERROR, "Unable to set xfer alignment: %s\n", snd_strerror(err));
452 err = snd_pcm_sw_params_set_silence_threshold(handle, swparams, silencethreshold);
454 ast_log(LOG_ERROR, "Unable to set silence threshold: %s\n", snd_strerror(err));
456 err = snd_pcm_sw_params(handle, swparams);
458 ast_log(LOG_ERROR, "sw_params: %s\n", snd_strerror(err));
460 err = snd_pcm_poll_descriptors_count(handle);
462 ast_log(LOG_ERROR, "Unable to get a poll descriptors count, error is %s\n", snd_strerror(err));
464 ast_log(LOG_DEBUG, "Can't handle more than one device\n");
466 snd_pcm_poll_descriptors(handle, &pfd, err);
467 ast_log(LOG_DEBUG, "Acquired fd %d from the poll descriptor\n", pfd.fd);
469 if (stream == SND_PCM_STREAM_CAPTURE)
477 static int soundcard_init(void)
479 alsa.icard = alsa_card_init(indevname, SND_PCM_STREAM_CAPTURE);
480 alsa.ocard = alsa_card_init(outdevname, SND_PCM_STREAM_PLAYBACK);
482 if (!alsa.icard || !alsa.ocard) {
483 ast_log(LOG_ERROR, "Problem opening alsa I/O devices\n");
490 static int alsa_digit(struct ast_channel *c, char digit)
492 ast_mutex_lock(&alsalock);
493 ast_verbose(" << Console Received digit %c >> \n", digit);
494 ast_mutex_unlock(&alsalock);
498 static int alsa_text(struct ast_channel *c, const char *text)
500 ast_mutex_lock(&alsalock);
501 ast_verbose(" << Console Received text %s >> \n", text);
502 ast_mutex_unlock(&alsalock);
506 static void grab_owner(void)
508 while (alsa.owner && ast_mutex_trylock(&alsa.owner->lock)) {
509 ast_mutex_unlock(&alsalock);
511 ast_mutex_lock(&alsalock);
515 static int alsa_call(struct ast_channel *c, char *dest, int timeout)
518 struct ast_frame f = { AST_FRAME_CONTROL };
519 ast_mutex_lock(&alsalock);
520 ast_verbose(" << Call placed to '%s' on console >> \n", dest);
522 ast_verbose(" << Auto-answered >> \n");
525 f.subclass = AST_CONTROL_ANSWER;
526 ast_queue_frame(alsa.owner, &f);
527 ast_mutex_unlock(&alsa.owner->lock);
530 ast_verbose(" << Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
533 f.subclass = AST_CONTROL_RINGING;
534 ast_queue_frame(alsa.owner, &f);
535 ast_mutex_unlock(&alsa.owner->lock);
537 write(sndcmd[1], &res, sizeof(res));
539 snd_pcm_prepare(alsa.icard);
540 snd_pcm_start(alsa.icard);
541 ast_mutex_unlock(&alsalock);
545 static void answer_sound(void)
550 write(sndcmd[1], &res, sizeof(res));
554 static int alsa_answer(struct ast_channel *c)
556 ast_mutex_lock(&alsalock);
557 ast_verbose(" << Console call has been answered >> \n");
559 ast_setstate(c, AST_STATE_UP);
561 snd_pcm_prepare(alsa.icard);
562 snd_pcm_start(alsa.icard);
563 ast_mutex_unlock(&alsalock);
567 static int alsa_hangup(struct ast_channel *c)
570 ast_mutex_lock(&alsalock);
574 ast_verbose(" << Hangup on console >> \n");
575 ast_mutex_lock(&usecnt_lock);
577 ast_mutex_unlock(&usecnt_lock);
581 /* Congestion noise */
583 write(sndcmd[1], &res, sizeof(res));
586 snd_pcm_drop(alsa.icard);
587 ast_mutex_unlock(&alsalock);
591 static int alsa_write(struct ast_channel *chan, struct ast_frame *f)
593 static char sizbuf[8000];
594 static int sizpos = 0;
598 /* size_t frames = 0; */
599 snd_pcm_state_t state;
601 /* Immediately return if no sound is enabled */
605 ast_mutex_lock(&alsalock);
606 /* Stop any currently playing sound */
607 if (cursound != -1) {
608 snd_pcm_drop(alsa.ocard);
609 snd_pcm_prepare(alsa.ocard);
614 /* We have to digest the frame in 160-byte portions */
615 if (f->datalen > sizeof(sizbuf) - sizpos) {
616 ast_log(LOG_WARNING, "Frame too large\n");
619 memcpy(sizbuf + sizpos, f->data, f->datalen);
623 alsa_monitor_write(sizbuf, len);
625 state = snd_pcm_state(alsa.ocard);
626 if (state == SND_PCM_STATE_XRUN)
627 snd_pcm_prepare(alsa.ocard);
628 res = snd_pcm_writei(alsa.ocard, sizbuf, len / 2);
631 ast_log(LOG_DEBUG, "XRUN write\n");
633 snd_pcm_prepare(alsa.ocard);
634 res = snd_pcm_writei(alsa.ocard, sizbuf, len / 2);
635 if (res != len / 2) {
636 ast_log(LOG_ERROR, "Write error: %s\n", snd_strerror(res));
638 } else if (res < 0) {
639 ast_log(LOG_ERROR, "Write error %s\n", snd_strerror(res));
643 if (res == -ESTRPIPE)
644 ast_log(LOG_ERROR, "You've got some big problems\n");
646 ast_log(LOG_NOTICE, "Error %d on write\n", res);
649 ast_mutex_unlock(&alsalock);
656 static struct ast_frame *alsa_read(struct ast_channel *chan)
658 static struct ast_frame f;
659 static short __buf[FRAME_SIZE + AST_FRIENDLY_OFFSET / 2];
661 static int readpos = 0;
662 static int left = FRAME_SIZE;
663 snd_pcm_state_t state;
667 ast_mutex_lock(&alsalock);
668 /* Acknowledge any pending cmd */
669 f.frametype = AST_FRAME_NULL;
677 f.delivery.tv_sec = 0;
678 f.delivery.tv_usec = 0;
680 state = snd_pcm_state(alsa.icard);
681 if ((state != SND_PCM_STATE_PREPARED) && (state != SND_PCM_STATE_RUNNING)) {
682 snd_pcm_prepare(alsa.icard);
685 buf = __buf + AST_FRIENDLY_OFFSET / 2;
687 r = snd_pcm_readi(alsa.icard, buf + readpos, left);
690 ast_log(LOG_ERROR, "XRUN read\n");
692 snd_pcm_prepare(alsa.icard);
693 } else if (r == -ESTRPIPE) {
694 ast_log(LOG_ERROR, "-ESTRPIPE\n");
695 snd_pcm_prepare(alsa.icard);
697 ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
701 /* Update positions */
705 if (readpos >= FRAME_SIZE) {
709 if (chan->_state != AST_STATE_UP) {
710 /* Don't transmit unless it's up */
711 ast_mutex_unlock(&alsalock);
714 f.frametype = AST_FRAME_VOICE;
715 f.subclass = AST_FORMAT_SLINEAR;
716 f.samples = FRAME_SIZE;
717 f.datalen = FRAME_SIZE * 2;
719 f.offset = AST_FRIENDLY_OFFSET;
723 alsa_monitor_read((char *) buf, FRAME_SIZE * 2);
727 ast_mutex_unlock(&alsalock);
731 static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
733 struct chan_alsa_pvt *p = newchan->tech_pvt;
734 ast_mutex_lock(&alsalock);
736 ast_mutex_unlock(&alsalock);
740 static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen)
744 ast_mutex_lock(&alsalock);
747 case AST_CONTROL_BUSY:
750 case AST_CONTROL_CONGESTION:
753 case AST_CONTROL_RINGING:
758 case AST_CONTROL_VIDUPDATE:
761 case AST_CONTROL_HOLD:
762 ast_verbose(" << Console Has Been Placed on Hold >> \n");
763 ast_moh_start(chan, data, mohinterpret);
765 case AST_CONTROL_UNHOLD:
766 ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
770 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, chan->name);
775 write(sndcmd[1], &res, sizeof(res));
777 ast_mutex_unlock(&alsalock);
782 static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state)
784 struct ast_channel *tmp = NULL;
786 if (!(tmp = ast_channel_alloc(1)))
789 tmp->tech = &alsa_tech;
790 ast_string_field_build(tmp, name, "ALSA/%s", indevname);
791 tmp->fds[0] = readdev;
792 tmp->nativeformats = AST_FORMAT_SLINEAR;
793 tmp->readformat = AST_FORMAT_SLINEAR;
794 tmp->writeformat = AST_FORMAT_SLINEAR;
796 if (!ast_strlen_zero(p->context))
797 ast_copy_string(tmp->context, p->context, sizeof(tmp->context));
798 if (!ast_strlen_zero(p->exten))
799 ast_copy_string(tmp->exten, p->exten, sizeof(tmp->exten));
800 if (!ast_strlen_zero(language))
801 ast_string_field_set(tmp, language, language);
803 ast_setstate(tmp, state);
804 ast_mutex_lock(&usecnt_lock);
806 ast_mutex_unlock(&usecnt_lock);
807 ast_update_use_count();
808 ast_jb_configure(tmp, &global_jbconf);
809 if (state != AST_STATE_DOWN) {
810 if (ast_pbx_start(tmp)) {
811 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
820 static struct ast_channel *alsa_request(const char *type, int format, void *data, int *cause)
822 int oldformat = format;
823 struct ast_channel *tmp = NULL;
825 format &= AST_FORMAT_SLINEAR;
827 ast_log(LOG_NOTICE, "Asked to get a channel of format '%d'\n", oldformat);
831 ast_mutex_lock(&alsalock);
834 ast_log(LOG_NOTICE, "Already have a call on the ALSA channel\n");
835 *cause = AST_CAUSE_BUSY;
836 } else if (!(tmp = alsa_new(&alsa, AST_STATE_DOWN)))
837 ast_log(LOG_WARNING, "Unable to create new ALSA channel\n");
839 ast_mutex_unlock(&alsalock);
844 static int console_autoanswer(int fd, int argc, char *argv[])
846 int res = RESULT_SUCCESS;;
847 if ((argc != 2) && (argc != 3))
848 return RESULT_SHOWUSAGE;
849 ast_mutex_lock(&alsalock);
851 ast_cli(fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
853 if (!strcasecmp(argv[2], "on"))
855 else if (!strcasecmp(argv[2], "off"))
858 res = RESULT_SHOWUSAGE;
860 ast_mutex_unlock(&alsalock);
864 static char *autoanswer_complete(const char *line, const char *word, int pos, int state)
867 #define MIN(a,b) ((a) < (b) ? (a) : (b))
871 if (!ast_strlen_zero(word) && !strncasecmp(word, "on", MIN(strlen(word), 2)))
872 return ast_strdup("on");
874 if (!ast_strlen_zero(word) && !strncasecmp(word, "off", MIN(strlen(word), 3)))
875 return ast_strdup("off");
882 static const char autoanswer_usage[] =
883 "Usage: console autoanswer [on|off]\n"
884 " Enables or disables autoanswer feature. If used without\n"
885 " argument, displays the current on/off status of autoanswer.\n"
886 " The default value of autoanswer is in 'alsa.conf'.\n";
888 static int console_answer(int fd, int argc, char *argv[])
890 int res = RESULT_SUCCESS;
893 return RESULT_SHOWUSAGE;
895 ast_mutex_lock(&alsalock);
898 ast_cli(fd, "No one is calling us\n");
899 res = RESULT_FAILURE;
905 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
906 ast_queue_frame(alsa.owner, &f);
907 ast_mutex_unlock(&alsa.owner->lock);
912 snd_pcm_prepare(alsa.icard);
913 snd_pcm_start(alsa.icard);
915 ast_mutex_unlock(&alsalock);
917 return RESULT_SUCCESS;
920 static char sendtext_usage[] =
921 "Usage: console send text <message>\n"
922 " Sends a text message for display on the remote terminal.\n";
924 static int console_sendtext(int fd, int argc, char *argv[])
927 int res = RESULT_SUCCESS;
930 return RESULT_SHOWUSAGE;
932 ast_mutex_lock(&alsalock);
935 ast_cli(fd, "No one is calling us\n");
936 res = RESULT_FAILURE;
938 struct ast_frame f = { AST_FRAME_TEXT, 0 };
939 char text2send[256] = "";
941 while (tmparg < argc) {
942 strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send) - 1);
943 strncat(text2send, " ", sizeof(text2send) - strlen(text2send) - 1);
945 text2send[strlen(text2send) - 1] = '\n';
947 f.datalen = strlen(text2send) + 1;
950 ast_queue_frame(alsa.owner, &f);
951 f.frametype = AST_FRAME_CONTROL;
952 f.subclass = AST_CONTROL_ANSWER;
955 ast_queue_frame(alsa.owner, &f);
956 ast_mutex_unlock(&alsa.owner->lock);
960 ast_mutex_unlock(&alsalock);
965 static char answer_usage[] =
966 "Usage: console answer\n"
967 " Answers an incoming call on the console (ALSA) channel.\n";
969 static int console_hangup(int fd, int argc, char *argv[])
971 int res = RESULT_SUCCESS;
974 return RESULT_SHOWUSAGE;
978 ast_mutex_lock(&alsalock);
980 if (!alsa.owner && !hookstate) {
981 ast_cli(fd, "No call to hangup up\n");
982 res = RESULT_FAILURE;
987 ast_queue_hangup(alsa.owner);
988 ast_mutex_unlock(&alsa.owner->lock);
992 ast_mutex_unlock(&alsalock);
997 static char hangup_usage[] =
998 "Usage: console hangup\n"
999 " Hangs up any call currently placed on the console.\n";
1001 static int console_dial(int fd, int argc, char *argv[])
1003 char tmp[256], *tmp2;
1006 int res = RESULT_SUCCESS;
1008 if ((argc != 2) && (argc != 3))
1009 return RESULT_SHOWUSAGE;
1011 ast_mutex_lock(&alsalock);
1018 struct ast_frame f = { AST_FRAME_DTMF };
1021 ast_queue_frame(alsa.owner, &f);
1024 ast_mutex_unlock(&alsa.owner->lock);
1027 ast_cli(fd, "You're already in a call. You can use this only to dial digits until you hangup\n");
1028 res = RESULT_FAILURE;
1034 char *stringp = NULL;
1035 strncpy(tmp, argv[2], sizeof(tmp) - 1);
1037 strsep(&stringp, "@");
1038 tmp2 = strsep(&stringp, "@");
1039 if (!ast_strlen_zero(tmp))
1041 if (!ast_strlen_zero(tmp2))
1044 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
1045 strncpy(alsa.exten, mye, sizeof(alsa.exten) - 1);
1046 strncpy(alsa.context, myc, sizeof(alsa.context) - 1);
1048 alsa_new(&alsa, AST_STATE_RINGING);
1050 ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
1053 ast_mutex_unlock(&alsalock);
1058 static char dial_usage[] =
1059 "Usage: console dial [extension[@context]]\n"
1060 " Dials a given extension (and context if specified)\n";
1062 static struct ast_cli_entry cli_alsa[] = {
1063 { { "console", "answer", NULL },
1064 console_answer, "Answer an incoming console call",
1067 { { "console", "hangup", NULL },
1068 console_hangup, "Hangup a call on the console",
1071 { { "console", "dial", NULL },
1072 console_dial, "Dial an extension on the console",
1075 { { "console", "send", "text", NULL },
1076 console_sendtext, "Send text to the remote device",
1079 { { "console", "autoanswer", NULL },
1080 console_autoanswer, "Sets/displays autoanswer",
1081 autoanswer_usage, autoanswer_complete },
1084 static int load_module(void)
1086 struct ast_config *cfg;
1087 struct ast_variable *v;
1089 /* Copy the default jb config over global_jbconf */
1090 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
1092 strcpy(mohinterpret, "default");
1094 if (!(cfg = ast_config_load(config)))
1095 return AST_MODULE_LOAD_DECLINE;
1097 v = ast_variable_browse(cfg, "general");
1098 for (; v; v = v->next) {
1099 /* handle jb conf */
1100 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
1103 if (!strcasecmp(v->name, "autoanswer"))
1104 autoanswer = ast_true(v->value);
1105 else if (!strcasecmp(v->name, "silencesuppression"))
1106 silencesuppression = ast_true(v->value);
1107 else if (!strcasecmp(v->name, "silencethreshold"))
1108 silencethreshold = atoi(v->value);
1109 else if (!strcasecmp(v->name, "context"))
1110 ast_copy_string(context, v->value, sizeof(context));
1111 else if (!strcasecmp(v->name, "language"))
1112 ast_copy_string(language, v->value, sizeof(language));
1113 else if (!strcasecmp(v->name, "extension"))
1114 ast_copy_string(exten, v->value, sizeof(exten));
1115 else if (!strcasecmp(v->name, "input_device"))
1116 ast_copy_string(indevname, v->value, sizeof(indevname));
1117 else if (!strcasecmp(v->name, "output_device"))
1118 ast_copy_string(outdevname, v->value, sizeof(outdevname));
1119 else if (!strcasecmp(v->name, "mohinterpret"))
1120 ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
1122 ast_config_destroy(cfg);
1125 ast_log(LOG_ERROR, "Unable to create pipe\n");
1126 return AST_MODULE_LOAD_FAILURE;
1129 if (soundcard_init() < 0) {
1130 if (option_verbose > 1) {
1131 ast_verbose(VERBOSE_PREFIX_2 "No sound card detected -- console channel will be unavailable\n");
1132 ast_verbose(VERBOSE_PREFIX_2 "Turn off ALSA support by adding 'noload=chan_alsa.so' in /etc/asterisk/modules.conf\n");
1134 return AST_MODULE_LOAD_DECLINE;
1137 if (ast_channel_register(&alsa_tech)) {
1138 ast_log(LOG_ERROR, "Unable to register channel class 'Console'\n");
1139 return AST_MODULE_LOAD_FAILURE;
1142 ast_cli_register_multiple(cli_alsa, sizeof(cli_alsa) / sizeof(struct ast_cli_entry));
1144 ast_pthread_create(&sthread, NULL, sound_thread, NULL);
1146 if (alsa_monitor_start())
1147 ast_log(LOG_ERROR, "Problem starting Monitoring\n");
1149 return AST_MODULE_LOAD_SUCCESS;
1152 static int unload_module(void)
1154 ast_channel_unregister(&alsa_tech);
1155 ast_cli_unregister_multiple(cli_alsa, sizeof(cli_alsa) / sizeof(struct ast_cli_entry));
1158 snd_pcm_close(alsa.icard);
1160 snd_pcm_close(alsa.ocard);
1161 if (sndcmd[0] > 0) {
1166 ast_softhangup(alsa.owner, AST_SOFTHANGUP_APPUNLOAD);
1172 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ALSA Console Channel Driver");