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 "default"
87 #define ALSA_OUTDEV "default"
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;
116 static int silencesuppression = 0;
117 static int silencethreshold = 1000;
119 AST_MUTEX_DEFINE_STATIC(alsalock);
121 static const char tdesc[] = "ALSA Console Channel Driver";
122 static const char config[] = "alsa.conf";
124 static char context[AST_MAX_CONTEXT] = "default";
125 static char language[MAX_LANGUAGE] = "";
126 static char exten[AST_MAX_EXTENSION] = "s";
127 static char mohinterpret[MAX_MUSICCLASS];
129 static int hookstate = 0;
131 static short silence[FRAME_SIZE] = { 0, };
142 static struct sound sounds[] = {
143 {AST_CONTROL_RINGING, ringtone, sizeof(ringtone) / 2, 16000, 32000, 1},
144 {AST_CONTROL_BUSY, busy, sizeof(busy) / 2, 4000, 4000, 1},
145 {AST_CONTROL_CONGESTION, busy, sizeof(busy) / 2, 2000, 2000, 1},
146 {AST_CONTROL_RING, ring10, sizeof(ring10) / 2, 16000, 32000, 1},
147 {AST_CONTROL_ANSWER, answer, sizeof(answer) / 2, 2200, 0, 0},
150 /* Sound command pipe */
151 static int sndcmd[2];
153 static struct chan_alsa_pvt {
154 /* We only have one ALSA structure -- near sighted perhaps, but it
155 keeps this driver as simple as possible -- as it should be. */
156 struct ast_channel *owner;
157 char exten[AST_MAX_EXTENSION];
158 char context[AST_MAX_CONTEXT];
162 snd_pcm_t *icard, *ocard;
166 /* Number of buffers... Each is FRAMESIZE/8 ms long. For example
167 with 160 sample frames, and a buffer size of 3, we have a 60ms buffer,
172 #define MAX_BUFFER_SIZE 100
174 /* File descriptors for sound device */
175 static int readdev = -1;
176 static int writedev = -1;
178 static int autoanswer = 1;
180 static int cursound = -1;
181 static int sampsent = 0;
182 static int silencelen = 0;
183 static int offset = 0;
184 static int nosound = 0;
187 static struct ast_channel *alsa_request(const char *type, int format, void *data, int *cause);
188 static int alsa_digit(struct ast_channel *c, char digit, unsigned int duration);
189 static int alsa_text(struct ast_channel *c, const char *text);
190 static int alsa_hangup(struct ast_channel *c);
191 static int alsa_answer(struct ast_channel *c);
192 static struct ast_frame *alsa_read(struct ast_channel *chan);
193 static int alsa_call(struct ast_channel *c, char *dest, int timeout);
194 static int alsa_write(struct ast_channel *chan, struct ast_frame *f);
195 static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
196 static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
198 static const struct ast_channel_tech alsa_tech = {
200 .description = tdesc,
201 .capabilities = AST_FORMAT_SLINEAR,
202 .requester = alsa_request,
203 .send_digit_end = alsa_digit,
204 .send_text = alsa_text,
205 .hangup = alsa_hangup,
206 .answer = alsa_answer,
210 .indicate = alsa_indicate,
214 static int send_sound(void)
216 short myframe[FRAME_SIZE];
217 int total = FRAME_SIZE;
219 int amt = 0, res, myoff;
220 snd_pcm_state_t state;
226 if (sampsent < sounds[cursound].samplen) {
230 if (amt > (sounds[cursound].datalen - offset))
231 amt = sounds[cursound].datalen - offset;
232 memcpy(myframe + myoff, sounds[cursound].data + offset, amt * 2);
237 if (offset >= sounds[cursound].datalen)
240 /* Set it up for silence */
241 if (sampsent >= sounds[cursound].samplen)
242 silencelen = sounds[cursound].silencelen;
245 if (silencelen > 0) {
249 if (sounds[cursound].repeat) {
261 if (res == 0 || !frame)
265 alsa_monitor_write((char *) frame, res * 2);
267 state = snd_pcm_state(alsa.ocard);
268 if (state == SND_PCM_STATE_XRUN)
269 snd_pcm_prepare(alsa.ocard);
270 res = snd_pcm_writei(alsa.ocard, frame, res);
276 static void *sound_thread(void *unused)
286 FD_SET(sndcmd[0], &rfds);
288 FD_SET(writedev, &wfds);
294 FD_SET(readdev, &rfds);
299 res = ast_select(max + 1, &rfds, &wfds, NULL, NULL);
301 ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
305 if (FD_ISSET(readdev, &rfds)) {
306 /* Keep the pipe going with read audio */
307 snd_pcm_state_t state;
308 short buf[FRAME_SIZE];
311 state = snd_pcm_state(alsa.ocard);
312 if (state == SND_PCM_STATE_XRUN) {
313 snd_pcm_prepare(alsa.ocard);
315 r = snd_pcm_readi(alsa.icard, buf, FRAME_SIZE);
318 ast_log(LOG_ERROR, "XRUN read\n");
320 snd_pcm_prepare(alsa.icard);
321 } else if (r == -ESTRPIPE) {
322 ast_log(LOG_ERROR, "-ESTRPIPE\n");
323 snd_pcm_prepare(alsa.icard);
325 ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
327 alsa_monitor_read((char *) buf, r * 2);
330 if (FD_ISSET(sndcmd[0], &rfds)) {
331 read(sndcmd[0], &cursound, sizeof(cursound));
336 if (FD_ISSET(writedev, &wfds))
338 ast_log(LOG_WARNING, "Failed to write sound\n");
344 static snd_pcm_t *alsa_card_init(char *dev, snd_pcm_stream_t stream)
348 snd_pcm_t *handle = NULL;
349 snd_pcm_hw_params_t *hwparams = NULL;
350 snd_pcm_sw_params_t *swparams = NULL;
352 snd_pcm_uframes_t period_size = PERIOD_FRAMES * 4;
353 /* int period_bytes = 0; */
354 snd_pcm_uframes_t buffer_size = 0;
356 unsigned int rate = DESIRED_RATE;
358 unsigned int per_min = 1;
360 /* unsigned int per_max = 8; */
361 snd_pcm_uframes_t start_threshold, stop_threshold;
363 err = snd_pcm_open(&handle, dev, stream, O_NONBLOCK);
365 ast_log(LOG_ERROR, "snd_pcm_open failed: %s\n", snd_strerror(err));
369 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));
398 ast_log(LOG_DEBUG, "Period size is %d\n", err);
401 buffer_size = 4096 * 2; /* period_size * 16; */
402 err = snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &buffer_size);
404 ast_log(LOG_WARNING, "Problem setting buffer size of %ld: %s\n", buffer_size, snd_strerror(err));
407 ast_log(LOG_DEBUG, "Buffer size is set to %d frames\n", err);
412 err = snd_pcm_hw_params_set_periods_min(handle, hwparams, &per_min, &direction);
414 ast_log(LOG_ERROR, "periods_min: %s\n", snd_strerror(err));
416 err = snd_pcm_hw_params_set_periods_max(handle, hwparams, &per_max, 0);
418 ast_log(LOG_ERROR, "periods_max: %s\n", snd_strerror(err));
421 err = snd_pcm_hw_params(handle, hwparams);
423 ast_log(LOG_ERROR, "Couldn't set the new hw params: %s\n", snd_strerror(err));
425 snd_pcm_sw_params_alloca(&swparams);
426 snd_pcm_sw_params_current(handle, swparams);
429 if (stream == SND_PCM_STREAM_PLAYBACK)
430 start_threshold = period_size;
434 err = snd_pcm_sw_params_set_start_threshold(handle, swparams, start_threshold);
436 ast_log(LOG_ERROR, "start threshold: %s\n", snd_strerror(err));
440 if (stream == SND_PCM_STREAM_PLAYBACK)
441 stop_threshold = buffer_size;
443 stop_threshold = buffer_size;
445 err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, stop_threshold);
447 ast_log(LOG_ERROR, "stop threshold: %s\n", snd_strerror(err));
450 err = snd_pcm_sw_params_set_xfer_align(handle, swparams, PERIOD_FRAMES);
452 ast_log(LOG_ERROR, "Unable to set xfer alignment: %s\n", snd_strerror(err));
456 err = snd_pcm_sw_params_set_silence_threshold(handle, swparams, silencethreshold);
458 ast_log(LOG_ERROR, "Unable to set silence threshold: %s\n", snd_strerror(err));
460 err = snd_pcm_sw_params(handle, swparams);
462 ast_log(LOG_ERROR, "sw_params: %s\n", snd_strerror(err));
464 err = snd_pcm_poll_descriptors_count(handle);
466 ast_log(LOG_ERROR, "Unable to get a poll descriptors count, error is %s\n", snd_strerror(err));
469 ast_log(LOG_DEBUG, "Can't handle more than one device\n");
472 snd_pcm_poll_descriptors(handle, &pfd, err);
474 ast_log(LOG_DEBUG, "Acquired fd %d from the poll descriptor\n", pfd.fd);
476 if (stream == SND_PCM_STREAM_CAPTURE)
484 static int soundcard_init(void)
486 alsa.icard = alsa_card_init(indevname, SND_PCM_STREAM_CAPTURE);
487 alsa.ocard = alsa_card_init(outdevname, SND_PCM_STREAM_PLAYBACK);
489 if (!alsa.icard || !alsa.ocard) {
490 ast_log(LOG_ERROR, "Problem opening alsa I/O devices\n");
497 static int alsa_digit(struct ast_channel *c, char digit, unsigned int duration)
499 ast_mutex_lock(&alsalock);
500 ast_verbose(" << Console Received digit %c of duration %u ms >> \n",
502 ast_mutex_unlock(&alsalock);
506 static int alsa_text(struct ast_channel *c, const char *text)
508 ast_mutex_lock(&alsalock);
509 ast_verbose(" << Console Received text %s >> \n", text);
510 ast_mutex_unlock(&alsalock);
514 static void grab_owner(void)
516 while (alsa.owner && ast_mutex_trylock(&alsa.owner->lock)) {
517 ast_mutex_unlock(&alsalock);
519 ast_mutex_lock(&alsalock);
523 static int alsa_call(struct ast_channel *c, char *dest, int timeout)
526 struct ast_frame f = { AST_FRAME_CONTROL };
527 ast_mutex_lock(&alsalock);
528 ast_verbose(" << Call placed to '%s' on console >> \n", dest);
530 ast_verbose(" << Auto-answered >> \n");
533 f.subclass = AST_CONTROL_ANSWER;
534 ast_queue_frame(alsa.owner, &f);
535 ast_mutex_unlock(&alsa.owner->lock);
538 ast_verbose(" << Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
541 f.subclass = AST_CONTROL_RINGING;
542 ast_queue_frame(alsa.owner, &f);
543 ast_mutex_unlock(&alsa.owner->lock);
545 write(sndcmd[1], &res, sizeof(res));
547 snd_pcm_prepare(alsa.icard);
548 snd_pcm_start(alsa.icard);
549 ast_mutex_unlock(&alsalock);
553 static void answer_sound(void)
558 write(sndcmd[1], &res, sizeof(res));
562 static int alsa_answer(struct ast_channel *c)
564 ast_mutex_lock(&alsalock);
565 ast_verbose(" << Console call has been answered >> \n");
567 ast_setstate(c, AST_STATE_UP);
569 snd_pcm_prepare(alsa.icard);
570 snd_pcm_start(alsa.icard);
571 ast_mutex_unlock(&alsalock);
575 static int alsa_hangup(struct ast_channel *c)
578 ast_mutex_lock(&alsalock);
582 ast_verbose(" << Hangup on console >> \n");
583 ast_module_unref(ast_module_info->self);
587 /* Congestion noise */
589 write(sndcmd[1], &res, sizeof(res));
592 snd_pcm_drop(alsa.icard);
593 ast_mutex_unlock(&alsalock);
597 static int alsa_write(struct ast_channel *chan, struct ast_frame *f)
599 static char sizbuf[8000];
600 static int sizpos = 0;
604 /* size_t frames = 0; */
605 snd_pcm_state_t state;
607 /* Immediately return if no sound is enabled */
611 ast_mutex_lock(&alsalock);
612 /* Stop any currently playing sound */
613 if (cursound != -1) {
614 snd_pcm_drop(alsa.ocard);
615 snd_pcm_prepare(alsa.ocard);
620 /* We have to digest the frame in 160-byte portions */
621 if (f->datalen > sizeof(sizbuf) - sizpos) {
622 ast_log(LOG_WARNING, "Frame too large\n");
625 memcpy(sizbuf + sizpos, f->data, f->datalen);
629 alsa_monitor_write(sizbuf, len);
631 state = snd_pcm_state(alsa.ocard);
632 if (state == SND_PCM_STATE_XRUN)
633 snd_pcm_prepare(alsa.ocard);
634 res = snd_pcm_writei(alsa.ocard, sizbuf, len / 2);
638 ast_log(LOG_DEBUG, "XRUN write\n");
640 snd_pcm_prepare(alsa.ocard);
641 res = snd_pcm_writei(alsa.ocard, sizbuf, len / 2);
642 if (res != len / 2) {
643 ast_log(LOG_ERROR, "Write error: %s\n", snd_strerror(res));
645 } else if (res < 0) {
646 ast_log(LOG_ERROR, "Write error %s\n", snd_strerror(res));
650 if (res == -ESTRPIPE)
651 ast_log(LOG_ERROR, "You've got some big problems\n");
653 ast_log(LOG_NOTICE, "Error %d on write\n", res);
656 ast_mutex_unlock(&alsalock);
663 static struct ast_frame *alsa_read(struct ast_channel *chan)
665 static struct ast_frame f;
666 static short __buf[FRAME_SIZE + AST_FRIENDLY_OFFSET / 2];
668 static int readpos = 0;
669 static int left = FRAME_SIZE;
670 snd_pcm_state_t state;
674 ast_mutex_lock(&alsalock);
675 /* Acknowledge any pending cmd */
676 f.frametype = AST_FRAME_NULL;
684 f.delivery.tv_sec = 0;
685 f.delivery.tv_usec = 0;
687 state = snd_pcm_state(alsa.icard);
688 if ((state != SND_PCM_STATE_PREPARED) && (state != SND_PCM_STATE_RUNNING)) {
689 snd_pcm_prepare(alsa.icard);
692 buf = __buf + AST_FRIENDLY_OFFSET / 2;
694 r = snd_pcm_readi(alsa.icard, buf + readpos, left);
697 ast_log(LOG_ERROR, "XRUN read\n");
699 snd_pcm_prepare(alsa.icard);
700 } else if (r == -ESTRPIPE) {
701 ast_log(LOG_ERROR, "-ESTRPIPE\n");
702 snd_pcm_prepare(alsa.icard);
704 ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
708 /* Update positions */
712 if (readpos >= FRAME_SIZE) {
716 if (chan->_state != AST_STATE_UP) {
717 /* Don't transmit unless it's up */
718 ast_mutex_unlock(&alsalock);
721 f.frametype = AST_FRAME_VOICE;
722 f.subclass = AST_FORMAT_SLINEAR;
723 f.samples = FRAME_SIZE;
724 f.datalen = FRAME_SIZE * 2;
726 f.offset = AST_FRIENDLY_OFFSET;
730 alsa_monitor_read((char *) buf, FRAME_SIZE * 2);
734 ast_mutex_unlock(&alsalock);
738 static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
740 struct chan_alsa_pvt *p = newchan->tech_pvt;
741 ast_mutex_lock(&alsalock);
743 ast_mutex_unlock(&alsalock);
747 static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen)
751 ast_mutex_lock(&alsalock);
754 case AST_CONTROL_BUSY:
757 case AST_CONTROL_CONGESTION:
760 case AST_CONTROL_RINGING:
765 case AST_CONTROL_VIDUPDATE:
768 case AST_CONTROL_HOLD:
769 ast_verbose(" << Console Has Been Placed on Hold >> \n");
770 ast_moh_start(chan, data, mohinterpret);
772 case AST_CONTROL_UNHOLD:
773 ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
777 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, chan->name);
782 write(sndcmd[1], &res, sizeof(res));
784 ast_mutex_unlock(&alsalock);
789 static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state)
791 struct ast_channel *tmp = NULL;
793 if (!(tmp = ast_channel_alloc(1, state, 0, 0, "ALSA/%s", indevname)))
796 tmp->tech = &alsa_tech;
797 tmp->fds[0] = readdev;
798 tmp->nativeformats = AST_FORMAT_SLINEAR;
799 tmp->readformat = AST_FORMAT_SLINEAR;
800 tmp->writeformat = AST_FORMAT_SLINEAR;
802 if (!ast_strlen_zero(p->context))
803 ast_copy_string(tmp->context, p->context, sizeof(tmp->context));
804 if (!ast_strlen_zero(p->exten))
805 ast_copy_string(tmp->exten, p->exten, sizeof(tmp->exten));
806 if (!ast_strlen_zero(language))
807 ast_string_field_set(tmp, language, language);
809 ast_module_ref(ast_module_info->self);
810 ast_jb_configure(tmp, &global_jbconf);
811 if (state != AST_STATE_DOWN) {
812 if (ast_pbx_start(tmp)) {
813 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
822 static struct ast_channel *alsa_request(const char *type, int format, void *data, int *cause)
824 int oldformat = format;
825 struct ast_channel *tmp = NULL;
827 format &= AST_FORMAT_SLINEAR;
829 ast_log(LOG_NOTICE, "Asked to get a channel of format '%d'\n", oldformat);
833 ast_mutex_lock(&alsalock);
836 ast_log(LOG_NOTICE, "Already have a call on the ALSA channel\n");
837 *cause = AST_CAUSE_BUSY;
838 } else if (!(tmp = alsa_new(&alsa, AST_STATE_DOWN)))
839 ast_log(LOG_WARNING, "Unable to create new ALSA channel\n");
841 ast_mutex_unlock(&alsalock);
846 static int console_autoanswer(int fd, int argc, char *argv[])
848 int res = RESULT_SUCCESS;;
849 if ((argc != 2) && (argc != 3))
850 return RESULT_SHOWUSAGE;
851 ast_mutex_lock(&alsalock);
853 ast_cli(fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
855 if (!strcasecmp(argv[2], "on"))
857 else if (!strcasecmp(argv[2], "off"))
860 res = RESULT_SHOWUSAGE;
862 ast_mutex_unlock(&alsalock);
866 static char *autoanswer_complete(const char *line, const char *word, int pos, int state)
869 #define MIN(a,b) ((a) < (b) ? (a) : (b))
873 if (!ast_strlen_zero(word) && !strncasecmp(word, "on", MIN(strlen(word), 2)))
874 return ast_strdup("on");
876 if (!ast_strlen_zero(word) && !strncasecmp(word, "off", MIN(strlen(word), 3)))
877 return ast_strdup("off");
884 static const char autoanswer_usage[] =
885 "Usage: console autoanswer [on|off]\n"
886 " Enables or disables autoanswer feature. If used without\n"
887 " argument, displays the current on/off status of autoanswer.\n"
888 " The default value of autoanswer is in 'alsa.conf'.\n";
890 static int console_answer(int fd, int argc, char *argv[])
892 int res = RESULT_SUCCESS;
895 return RESULT_SHOWUSAGE;
897 ast_mutex_lock(&alsalock);
900 ast_cli(fd, "No one is calling us\n");
901 res = RESULT_FAILURE;
907 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
908 ast_queue_frame(alsa.owner, &f);
909 ast_mutex_unlock(&alsa.owner->lock);
914 snd_pcm_prepare(alsa.icard);
915 snd_pcm_start(alsa.icard);
917 ast_mutex_unlock(&alsalock);
919 return RESULT_SUCCESS;
922 static const char sendtext_usage[] =
923 "Usage: console send text <message>\n"
924 " Sends a text message for display on the remote terminal.\n";
926 static int console_sendtext(int fd, int argc, char *argv[])
929 int res = RESULT_SUCCESS;
932 return RESULT_SHOWUSAGE;
934 ast_mutex_lock(&alsalock);
937 ast_cli(fd, "No one is calling us\n");
938 res = RESULT_FAILURE;
940 struct ast_frame f = { AST_FRAME_TEXT, 0 };
941 char text2send[256] = "";
943 while (tmparg < argc) {
944 strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send) - 1);
945 strncat(text2send, " ", sizeof(text2send) - strlen(text2send) - 1);
947 text2send[strlen(text2send) - 1] = '\n';
949 f.datalen = strlen(text2send) + 1;
952 ast_queue_frame(alsa.owner, &f);
953 f.frametype = AST_FRAME_CONTROL;
954 f.subclass = AST_CONTROL_ANSWER;
957 ast_queue_frame(alsa.owner, &f);
958 ast_mutex_unlock(&alsa.owner->lock);
962 ast_mutex_unlock(&alsalock);
967 static const char answer_usage[] =
968 "Usage: console answer\n"
969 " Answers an incoming call on the console (ALSA) channel.\n";
971 static int console_hangup(int fd, int argc, char *argv[])
973 int res = RESULT_SUCCESS;
976 return RESULT_SHOWUSAGE;
980 ast_mutex_lock(&alsalock);
982 if (!alsa.owner && !hookstate) {
983 ast_cli(fd, "No call to hangup up\n");
984 res = RESULT_FAILURE;
989 ast_queue_hangup(alsa.owner);
990 ast_mutex_unlock(&alsa.owner->lock);
994 ast_mutex_unlock(&alsalock);
999 static const char hangup_usage[] =
1000 "Usage: console hangup\n"
1001 " Hangs up any call currently placed on the console.\n";
1003 static int console_dial(int fd, int argc, char *argv[])
1005 char tmp[256], *tmp2;
1008 int res = RESULT_SUCCESS;
1010 if ((argc != 2) && (argc != 3))
1011 return RESULT_SHOWUSAGE;
1013 ast_mutex_lock(&alsalock);
1020 struct ast_frame f = { AST_FRAME_DTMF };
1023 ast_queue_frame(alsa.owner, &f);
1026 ast_mutex_unlock(&alsa.owner->lock);
1029 ast_cli(fd, "You're already in a call. You can use this only to dial digits until you hangup\n");
1030 res = RESULT_FAILURE;
1036 char *stringp = NULL;
1037 ast_copy_string(tmp, argv[2], sizeof(tmp));
1039 strsep(&stringp, "@");
1040 tmp2 = strsep(&stringp, "@");
1041 if (!ast_strlen_zero(tmp))
1043 if (!ast_strlen_zero(tmp2))
1046 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
1047 ast_copy_string(alsa.exten, mye, sizeof(alsa.exten));
1048 ast_copy_string(alsa.context, myc, sizeof(alsa.context));
1050 alsa_new(&alsa, AST_STATE_RINGING);
1052 ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
1055 ast_mutex_unlock(&alsalock);
1060 static const char dial_usage[] =
1061 "Usage: console dial [extension[@context]]\n"
1062 " Dials a given extension (and context if specified)\n";
1064 static struct ast_cli_entry cli_alsa[] = {
1065 { { "console", "answer", NULL },
1066 console_answer, "Answer an incoming console call",
1069 { { "console", "hangup", NULL },
1070 console_hangup, "Hangup a call on the console",
1073 { { "console", "dial", NULL },
1074 console_dial, "Dial an extension on the console",
1077 { { "console", "send", "text", NULL },
1078 console_sendtext, "Send text to the remote device",
1081 { { "console", "autoanswer", NULL },
1082 console_autoanswer, "Sets/displays autoanswer",
1083 autoanswer_usage, autoanswer_complete },
1086 static int load_module(void)
1088 struct ast_config *cfg;
1089 struct ast_variable *v;
1091 /* Copy the default jb config over global_jbconf */
1092 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
1094 strcpy(mohinterpret, "default");
1096 if (!(cfg = ast_config_load(config)))
1097 return AST_MODULE_LOAD_DECLINE;
1099 v = ast_variable_browse(cfg, "general");
1100 for (; v; v = v->next) {
1101 /* handle jb conf */
1102 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
1105 if (!strcasecmp(v->name, "autoanswer"))
1106 autoanswer = ast_true(v->value);
1107 else if (!strcasecmp(v->name, "silencesuppression"))
1108 silencesuppression = ast_true(v->value);
1109 else if (!strcasecmp(v->name, "silencethreshold"))
1110 silencethreshold = atoi(v->value);
1111 else if (!strcasecmp(v->name, "context"))
1112 ast_copy_string(context, v->value, sizeof(context));
1113 else if (!strcasecmp(v->name, "language"))
1114 ast_copy_string(language, v->value, sizeof(language));
1115 else if (!strcasecmp(v->name, "extension"))
1116 ast_copy_string(exten, v->value, sizeof(exten));
1117 else if (!strcasecmp(v->name, "input_device"))
1118 ast_copy_string(indevname, v->value, sizeof(indevname));
1119 else if (!strcasecmp(v->name, "output_device"))
1120 ast_copy_string(outdevname, v->value, sizeof(outdevname));
1121 else if (!strcasecmp(v->name, "mohinterpret"))
1122 ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
1124 ast_config_destroy(cfg);
1127 ast_log(LOG_ERROR, "Unable to create pipe\n");
1128 return AST_MODULE_LOAD_FAILURE;
1131 if (soundcard_init() < 0) {
1132 if (option_verbose > 1) {
1133 ast_verbose(VERBOSE_PREFIX_2 "No sound card detected -- console channel will be unavailable\n");
1134 ast_verbose(VERBOSE_PREFIX_2 "Turn off ALSA support by adding 'noload=chan_alsa.so' in /etc/asterisk/modules.conf\n");
1136 return AST_MODULE_LOAD_DECLINE;
1139 if (ast_channel_register(&alsa_tech)) {
1140 ast_log(LOG_ERROR, "Unable to register channel class 'Console'\n");
1141 return AST_MODULE_LOAD_FAILURE;
1144 ast_cli_register_multiple(cli_alsa, sizeof(cli_alsa) / sizeof(struct ast_cli_entry));
1146 ast_pthread_create_background(&sthread, NULL, sound_thread, NULL);
1148 if (alsa_monitor_start())
1149 ast_log(LOG_ERROR, "Problem starting Monitoring\n");
1151 return AST_MODULE_LOAD_SUCCESS;
1154 static int unload_module(void)
1156 ast_channel_unregister(&alsa_tech);
1157 ast_cli_unregister_multiple(cli_alsa, sizeof(cli_alsa) / sizeof(struct ast_cli_entry));
1160 snd_pcm_close(alsa.icard);
1162 snd_pcm_close(alsa.ocard);
1163 if (sndcmd[0] > 0) {
1168 ast_softhangup(alsa.owner, AST_SOFTHANGUP_APPUNLOAD);
1174 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ALSA Console Channel Driver");