Let's also include aclocal.m4
[asterisk-bristuff.git] / channels / chan_alsa.c
blobc45740ef15c68d613b7dde68d997205382a3078f
1 /*
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.
19 /*! \file
20 * \brief ALSA sound card channel driver
22 * \author Matthew Fredrickson <creslin@digium.com>
24 * \par See also
25 * \arg Config_alsa
27 * \ingroup channel_drivers
30 /*** MODULEINFO
31 <depend>asound</depend>
32 ***/
34 #include "asterisk.h"
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <sys/ioctl.h>
42 #include <sys/time.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <stdio.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"
66 #include "busy.h"
67 #include "ringtone.h"
68 #include "ring10.h"
69 #include "answer.h"
71 #ifdef ALSA_MONITOR
72 #include "alsa-monitor.h"
73 #endif
75 /*! Global jitterbuffer configuration - by default, jb is disabled */
76 static struct ast_jb_conf default_jbconf = {
77 .flags = 0,
78 .max_size = -1,
79 .resync_threshold = -1,
80 .impl = ""
82 static struct ast_jb_conf global_jbconf;
84 #define DEBUG 0
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;
104 #else
105 static snd_pcm_format_t format = SND_PCM_FORMAT_S16_BE;
106 #endif
108 static char indevname[50] = ALSA_INDEV;
109 static char outdevname[50] = ALSA_OUTDEV;
111 #if 0
112 static struct timeval lasttime;
113 #endif
115 static int silencesuppression = 0;
116 static int silencethreshold = 1000;
118 AST_MUTEX_DEFINE_STATIC(alsalock);
120 static const char tdesc[] = "ALSA Console Channel Driver";
121 static const char config[] = "alsa.conf";
123 static char context[AST_MAX_CONTEXT] = "default";
124 static char language[MAX_LANGUAGE] = "";
125 static char exten[AST_MAX_EXTENSION] = "s";
126 static char mohinterpret[MAX_MUSICCLASS];
128 static int hookstate = 0;
130 static short silence[FRAME_SIZE] = { 0, };
132 struct sound {
133 int ind;
134 short *data;
135 int datalen;
136 int samplen;
137 int silencelen;
138 int repeat;
141 static struct sound sounds[] = {
142 {AST_CONTROL_RINGING, ringtone, sizeof(ringtone) / 2, 16000, 32000, 1},
143 {AST_CONTROL_BUSY, busy, sizeof(busy) / 2, 4000, 4000, 1},
144 {AST_CONTROL_CONGESTION, busy, sizeof(busy) / 2, 2000, 2000, 1},
145 {AST_CONTROL_RING, ring10, sizeof(ring10) / 2, 16000, 32000, 1},
146 {AST_CONTROL_ANSWER, answer, sizeof(answer) / 2, 2200, 0, 0},
149 /* Sound command pipe */
150 static int sndcmd[2];
152 static struct chan_alsa_pvt {
153 /* We only have one ALSA structure -- near sighted perhaps, but it
154 keeps this driver as simple as possible -- as it should be. */
155 struct ast_channel *owner;
156 char exten[AST_MAX_EXTENSION];
157 char context[AST_MAX_CONTEXT];
158 #if 0
159 snd_pcm_t *card;
160 #endif
161 snd_pcm_t *icard, *ocard;
163 } alsa;
165 /* Number of buffers... Each is FRAMESIZE/8 ms long. For example
166 with 160 sample frames, and a buffer size of 3, we have a 60ms buffer,
167 usually plenty. */
169 pthread_t sthread;
171 #define MAX_BUFFER_SIZE 100
173 /* File descriptors for sound device */
174 static int readdev = -1;
175 static int writedev = -1;
177 static int autoanswer = 1;
179 static int cursound = -1;
180 static int sampsent = 0;
181 static int silencelen = 0;
182 static int offset = 0;
183 static int nosound = 0;
185 /* ZZ */
186 static struct ast_channel *alsa_request(const char *type, int format, void *data, int *cause);
187 static int alsa_digit(struct ast_channel *c, char digit, unsigned int duration);
188 static int alsa_text(struct ast_channel *c, const char *text);
189 static int alsa_hangup(struct ast_channel *c);
190 static int alsa_answer(struct ast_channel *c);
191 static struct ast_frame *alsa_read(struct ast_channel *chan);
192 static int alsa_call(struct ast_channel *c, char *dest, int timeout);
193 static int alsa_write(struct ast_channel *chan, struct ast_frame *f);
194 static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
195 static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
197 static const struct ast_channel_tech alsa_tech = {
198 .type = "Console",
199 .description = tdesc,
200 .capabilities = AST_FORMAT_SLINEAR,
201 .requester = alsa_request,
202 .send_digit_end = alsa_digit,
203 .send_text = alsa_text,
204 .hangup = alsa_hangup,
205 .answer = alsa_answer,
206 .read = alsa_read,
207 .call = alsa_call,
208 .write = alsa_write,
209 .indicate = alsa_indicate,
210 .fixup = alsa_fixup,
213 static int send_sound(void)
215 short myframe[FRAME_SIZE];
216 int total = FRAME_SIZE;
217 short *frame = NULL;
218 int amt = 0, res, myoff;
219 snd_pcm_state_t state;
221 if (cursound == -1)
222 return 0;
224 res = total;
225 if (sampsent < sounds[cursound].samplen) {
226 myoff = 0;
227 while (total) {
228 amt = total;
229 if (amt > (sounds[cursound].datalen - offset))
230 amt = sounds[cursound].datalen - offset;
231 memcpy(myframe + myoff, sounds[cursound].data + offset, amt * 2);
232 total -= amt;
233 offset += amt;
234 sampsent += amt;
235 myoff += amt;
236 if (offset >= sounds[cursound].datalen)
237 offset = 0;
239 /* Set it up for silence */
240 if (sampsent >= sounds[cursound].samplen)
241 silencelen = sounds[cursound].silencelen;
242 frame = myframe;
243 } else {
244 if (silencelen > 0) {
245 frame = silence;
246 silencelen -= res;
247 } else {
248 if (sounds[cursound].repeat) {
249 /* Start over */
250 sampsent = 0;
251 offset = 0;
252 } else {
253 cursound = -1;
254 nosound = 0;
256 return 0;
260 if (res == 0 || !frame)
261 return 0;
263 #ifdef ALSA_MONITOR
264 alsa_monitor_write((char *) frame, res * 2);
265 #endif
266 state = snd_pcm_state(alsa.ocard);
267 if (state == SND_PCM_STATE_XRUN)
268 snd_pcm_prepare(alsa.ocard);
269 res = snd_pcm_writei(alsa.ocard, frame, res);
270 if (res > 0)
271 return 0;
272 return 0;
275 static void *sound_thread(void *unused)
277 fd_set rfds;
278 fd_set wfds;
279 int max, res;
281 for (;;) {
282 FD_ZERO(&rfds);
283 FD_ZERO(&wfds);
284 max = sndcmd[0];
285 FD_SET(sndcmd[0], &rfds);
286 if (cursound > -1) {
287 FD_SET(writedev, &wfds);
288 if (writedev > max)
289 max = writedev;
291 #ifdef ALSA_MONITOR
292 if (!alsa.owner) {
293 FD_SET(readdev, &rfds);
294 if (readdev > max)
295 max = readdev;
297 #endif
298 res = ast_select(max + 1, &rfds, &wfds, NULL, NULL);
299 if (res < 1) {
300 ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
301 continue;
303 #ifdef ALSA_MONITOR
304 if (FD_ISSET(readdev, &rfds)) {
305 /* Keep the pipe going with read audio */
306 snd_pcm_state_t state;
307 short buf[FRAME_SIZE];
308 int r;
310 state = snd_pcm_state(alsa.ocard);
311 if (state == SND_PCM_STATE_XRUN) {
312 snd_pcm_prepare(alsa.ocard);
314 r = snd_pcm_readi(alsa.icard, buf, FRAME_SIZE);
315 if (r == -EPIPE) {
316 #if DEBUG
317 ast_log(LOG_ERROR, "XRUN read\n");
318 #endif
319 snd_pcm_prepare(alsa.icard);
320 } else if (r == -ESTRPIPE) {
321 ast_log(LOG_ERROR, "-ESTRPIPE\n");
322 snd_pcm_prepare(alsa.icard);
323 } else if (r < 0) {
324 ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
325 } else
326 alsa_monitor_read((char *) buf, r * 2);
328 #endif
329 if (FD_ISSET(sndcmd[0], &rfds)) {
330 read(sndcmd[0], &cursound, sizeof(cursound));
331 silencelen = 0;
332 offset = 0;
333 sampsent = 0;
335 if (FD_ISSET(writedev, &wfds))
336 if (send_sound())
337 ast_log(LOG_WARNING, "Failed to write sound\n");
339 /* Never reached */
340 return NULL;
343 static snd_pcm_t *alsa_card_init(char *dev, snd_pcm_stream_t stream)
345 int err;
346 int direction;
347 snd_pcm_t *handle = NULL;
348 snd_pcm_hw_params_t *hwparams = NULL;
349 snd_pcm_sw_params_t *swparams = NULL;
350 struct pollfd pfd;
351 snd_pcm_uframes_t period_size = PERIOD_FRAMES * 4;
352 /* int period_bytes = 0; */
353 snd_pcm_uframes_t buffer_size = 0;
355 unsigned int rate = DESIRED_RATE;
356 #if 0
357 unsigned int per_min = 1;
358 #endif
359 /* unsigned int per_max = 8; */
360 snd_pcm_uframes_t start_threshold, stop_threshold;
362 err = snd_pcm_open(&handle, dev, stream, SND_PCM_NONBLOCK);
363 if (err < 0) {
364 ast_log(LOG_ERROR, "snd_pcm_open failed: %s\n", snd_strerror(err));
365 return NULL;
366 } else
367 ast_log(LOG_DEBUG, "Opening device %s in %s mode\n", dev, (stream == SND_PCM_STREAM_CAPTURE) ? "read" : "write");
369 hwparams = alloca(snd_pcm_hw_params_sizeof());
370 memset(hwparams, 0, snd_pcm_hw_params_sizeof());
371 snd_pcm_hw_params_any(handle, hwparams);
373 err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
374 if (err < 0)
375 ast_log(LOG_ERROR, "set_access failed: %s\n", snd_strerror(err));
377 err = snd_pcm_hw_params_set_format(handle, hwparams, format);
378 if (err < 0)
379 ast_log(LOG_ERROR, "set_format failed: %s\n", snd_strerror(err));
381 err = snd_pcm_hw_params_set_channels(handle, hwparams, 1);
382 if (err < 0)
383 ast_log(LOG_ERROR, "set_channels failed: %s\n", snd_strerror(err));
385 direction = 0;
386 err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &rate, &direction);
387 if (rate != DESIRED_RATE)
388 ast_log(LOG_WARNING, "Rate not correct, requested %d, got %d\n", DESIRED_RATE, rate);
390 direction = 0;
391 err = snd_pcm_hw_params_set_period_size_near(handle, hwparams, &period_size, &direction);
392 if (err < 0)
393 ast_log(LOG_ERROR, "period_size(%ld frames) is bad: %s\n", period_size, snd_strerror(err));
394 else
395 ast_log(LOG_DEBUG, "Period size is %d\n", err);
397 buffer_size = 4096 * 2; /* period_size * 16; */
398 err = snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &buffer_size);
399 if (err < 0)
400 ast_log(LOG_WARNING, "Problem setting buffer size of %ld: %s\n", buffer_size, snd_strerror(err));
401 else
402 ast_log(LOG_DEBUG, "Buffer size is set to %d frames\n", err);
404 #if 0
405 direction = 0;
406 err = snd_pcm_hw_params_set_periods_min(handle, hwparams, &per_min, &direction);
407 if (err < 0)
408 ast_log(LOG_ERROR, "periods_min: %s\n", snd_strerror(err));
410 err = snd_pcm_hw_params_set_periods_max(handle, hwparams, &per_max, 0);
411 if (err < 0)
412 ast_log(LOG_ERROR, "periods_max: %s\n", snd_strerror(err));
413 #endif
415 err = snd_pcm_hw_params(handle, hwparams);
416 if (err < 0)
417 ast_log(LOG_ERROR, "Couldn't set the new hw params: %s\n", snd_strerror(err));
419 swparams = alloca(snd_pcm_sw_params_sizeof());
420 memset(swparams, 0, snd_pcm_sw_params_sizeof());
421 snd_pcm_sw_params_current(handle, swparams);
423 #if 1
424 if (stream == SND_PCM_STREAM_PLAYBACK)
425 start_threshold = period_size;
426 else
427 start_threshold = 1;
429 err = snd_pcm_sw_params_set_start_threshold(handle, swparams, start_threshold);
430 if (err < 0)
431 ast_log(LOG_ERROR, "start threshold: %s\n", snd_strerror(err));
432 #endif
434 #if 1
435 if (stream == SND_PCM_STREAM_PLAYBACK)
436 stop_threshold = buffer_size;
437 else
438 stop_threshold = buffer_size;
440 err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, stop_threshold);
441 if (err < 0)
442 ast_log(LOG_ERROR, "stop threshold: %s\n", snd_strerror(err));
443 #endif
444 #if 0
445 err = snd_pcm_sw_params_set_xfer_align(handle, swparams, PERIOD_FRAMES);
446 if (err < 0)
447 ast_log(LOG_ERROR, "Unable to set xfer alignment: %s\n", snd_strerror(err));
448 #endif
450 #if 0
451 err = snd_pcm_sw_params_set_silence_threshold(handle, swparams, silencethreshold);
452 if (err < 0)
453 ast_log(LOG_ERROR, "Unable to set silence threshold: %s\n", snd_strerror(err));
454 #endif
455 err = snd_pcm_sw_params(handle, swparams);
456 if (err < 0)
457 ast_log(LOG_ERROR, "sw_params: %s\n", snd_strerror(err));
459 err = snd_pcm_poll_descriptors_count(handle);
460 if (err <= 0)
461 ast_log(LOG_ERROR, "Unable to get a poll descriptors count, error is %s\n", snd_strerror(err));
462 if (err != 1)
463 ast_log(LOG_DEBUG, "Can't handle more than one device\n");
465 snd_pcm_poll_descriptors(handle, &pfd, err);
466 ast_log(LOG_DEBUG, "Acquired fd %d from the poll descriptor\n", pfd.fd);
468 if (stream == SND_PCM_STREAM_CAPTURE)
469 readdev = pfd.fd;
470 else
471 writedev = pfd.fd;
473 return handle;
476 static int soundcard_init(void)
478 alsa.icard = alsa_card_init(indevname, SND_PCM_STREAM_CAPTURE);
479 alsa.ocard = alsa_card_init(outdevname, SND_PCM_STREAM_PLAYBACK);
481 if (!alsa.icard || !alsa.ocard) {
482 ast_log(LOG_ERROR, "Problem opening alsa I/O devices\n");
483 return -1;
486 return readdev;
489 static int alsa_digit(struct ast_channel *c, char digit, unsigned int duration)
491 ast_mutex_lock(&alsalock);
492 ast_verbose(" << Console Received digit %c of duration %u ms >> \n",
493 digit, duration);
494 ast_mutex_unlock(&alsalock);
495 return 0;
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);
503 return 0;
506 static void grab_owner(void)
508 while (alsa.owner && ast_mutex_trylock(&alsa.owner->lock)) {
509 DEADLOCK_AVOIDANCE(&alsalock);
513 static int alsa_call(struct ast_channel *c, char *dest, int timeout)
515 int res = 3;
516 struct ast_frame f = { AST_FRAME_CONTROL };
517 ast_mutex_lock(&alsalock);
518 ast_verbose(" << Call placed to '%s' on console >> \n", dest);
519 if (autoanswer) {
520 ast_verbose(" << Auto-answered >> \n");
521 grab_owner();
522 if (alsa.owner) {
523 f.subclass = AST_CONTROL_ANSWER;
524 ast_queue_frame(alsa.owner, &f);
525 ast_mutex_unlock(&alsa.owner->lock);
527 } else {
528 ast_verbose(" << Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
529 grab_owner();
530 if (alsa.owner) {
531 f.subclass = AST_CONTROL_RINGING;
532 ast_queue_frame(alsa.owner, &f);
533 ast_mutex_unlock(&alsa.owner->lock);
535 write(sndcmd[1], &res, sizeof(res));
537 snd_pcm_prepare(alsa.icard);
538 snd_pcm_start(alsa.icard);
539 ast_mutex_unlock(&alsalock);
540 return 0;
543 static void answer_sound(void)
545 int res;
546 nosound = 1;
547 res = 4;
548 write(sndcmd[1], &res, sizeof(res));
552 static int alsa_answer(struct ast_channel *c)
554 ast_mutex_lock(&alsalock);
555 ast_verbose(" << Console call has been answered >> \n");
556 answer_sound();
557 ast_setstate(c, AST_STATE_UP);
558 cursound = -1;
559 snd_pcm_prepare(alsa.icard);
560 snd_pcm_start(alsa.icard);
561 ast_mutex_unlock(&alsalock);
562 return 0;
565 static int alsa_hangup(struct ast_channel *c)
567 int res;
568 ast_mutex_lock(&alsalock);
569 cursound = -1;
570 c->tech_pvt = NULL;
571 alsa.owner = NULL;
572 ast_verbose(" << Hangup on console >> \n");
573 ast_module_unref(ast_module_info->self);
574 if (hookstate) {
575 hookstate = 0;
576 if (!autoanswer) {
577 /* Congestion noise */
578 res = 2;
579 write(sndcmd[1], &res, sizeof(res));
582 snd_pcm_drop(alsa.icard);
583 ast_mutex_unlock(&alsalock);
584 return 0;
587 static int alsa_write(struct ast_channel *chan, struct ast_frame *f)
589 static char sizbuf[8000];
590 static int sizpos = 0;
591 int len = sizpos;
592 int pos;
593 int res = 0;
594 /* size_t frames = 0; */
595 snd_pcm_state_t state;
597 /* Immediately return if no sound is enabled */
598 if (nosound)
599 return 0;
601 ast_mutex_lock(&alsalock);
602 /* Stop any currently playing sound */
603 if (cursound != -1) {
604 snd_pcm_drop(alsa.ocard);
605 snd_pcm_prepare(alsa.ocard);
606 cursound = -1;
610 /* We have to digest the frame in 160-byte portions */
611 if (f->datalen > sizeof(sizbuf) - sizpos) {
612 ast_log(LOG_WARNING, "Frame too large\n");
613 res = -1;
614 } else {
615 memcpy(sizbuf + sizpos, f->data, f->datalen);
616 len += f->datalen;
617 pos = 0;
618 #ifdef ALSA_MONITOR
619 alsa_monitor_write(sizbuf, len);
620 #endif
621 state = snd_pcm_state(alsa.ocard);
622 if (state == SND_PCM_STATE_XRUN)
623 snd_pcm_prepare(alsa.ocard);
624 res = snd_pcm_writei(alsa.ocard, sizbuf, len / 2);
625 if (res == -EPIPE) {
626 #if DEBUG
627 ast_log(LOG_DEBUG, "XRUN write\n");
628 #endif
629 snd_pcm_prepare(alsa.ocard);
630 res = snd_pcm_writei(alsa.ocard, sizbuf, len / 2);
631 if (res != len / 2) {
632 ast_log(LOG_ERROR, "Write error: %s\n", snd_strerror(res));
633 res = -1;
634 } else if (res < 0) {
635 ast_log(LOG_ERROR, "Write error %s\n", snd_strerror(res));
636 res = -1;
638 } else {
639 if (res == -ESTRPIPE)
640 ast_log(LOG_ERROR, "You've got some big problems\n");
641 else if (res < 0)
642 ast_log(LOG_NOTICE, "Error %d on write\n", res);
645 ast_mutex_unlock(&alsalock);
646 if (res > 0)
647 res = 0;
648 return res;
652 static struct ast_frame *alsa_read(struct ast_channel *chan)
654 static struct ast_frame f;
655 static short __buf[FRAME_SIZE + AST_FRIENDLY_OFFSET / 2];
656 short *buf;
657 static int readpos = 0;
658 static int left = FRAME_SIZE;
659 snd_pcm_state_t state;
660 int r = 0;
661 int off = 0;
663 ast_mutex_lock(&alsalock);
664 /* Acknowledge any pending cmd */
665 f.frametype = AST_FRAME_NULL;
666 f.subclass = 0;
667 f.samples = 0;
668 f.datalen = 0;
669 f.data = NULL;
670 f.offset = 0;
671 f.src = "Console";
672 f.mallocd = 0;
673 f.delivery.tv_sec = 0;
674 f.delivery.tv_usec = 0;
676 state = snd_pcm_state(alsa.icard);
677 if ((state != SND_PCM_STATE_PREPARED) && (state != SND_PCM_STATE_RUNNING)) {
678 snd_pcm_prepare(alsa.icard);
681 buf = __buf + AST_FRIENDLY_OFFSET / 2;
683 r = snd_pcm_readi(alsa.icard, buf + readpos, left);
684 if (r == -EPIPE) {
685 #if DEBUG
686 ast_log(LOG_ERROR, "XRUN read\n");
687 #endif
688 snd_pcm_prepare(alsa.icard);
689 } else if (r == -ESTRPIPE) {
690 ast_log(LOG_ERROR, "-ESTRPIPE\n");
691 snd_pcm_prepare(alsa.icard);
692 } else if (r < 0) {
693 ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
694 } else if (r >= 0) {
695 off -= r;
697 /* Update positions */
698 readpos += r;
699 left -= r;
701 if (readpos >= FRAME_SIZE) {
702 /* A real frame */
703 readpos = 0;
704 left = FRAME_SIZE;
705 if (chan->_state != AST_STATE_UP) {
706 /* Don't transmit unless it's up */
707 ast_mutex_unlock(&alsalock);
708 return &f;
710 f.frametype = AST_FRAME_VOICE;
711 f.subclass = AST_FORMAT_SLINEAR;
712 f.samples = FRAME_SIZE;
713 f.datalen = FRAME_SIZE * 2;
714 f.data = buf;
715 f.offset = AST_FRIENDLY_OFFSET;
716 f.src = "Console";
717 f.mallocd = 0;
718 #ifdef ALSA_MONITOR
719 alsa_monitor_read((char *) buf, FRAME_SIZE * 2);
720 #endif
723 ast_mutex_unlock(&alsalock);
724 return &f;
727 static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
729 struct chan_alsa_pvt *p = newchan->tech_pvt;
730 ast_mutex_lock(&alsalock);
731 p->owner = newchan;
732 ast_mutex_unlock(&alsalock);
733 return 0;
736 static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen)
738 int res = 0;
740 ast_mutex_lock(&alsalock);
742 switch (cond) {
743 case AST_CONTROL_BUSY:
744 res = 1;
745 break;
746 case AST_CONTROL_CONGESTION:
747 res = 2;
748 break;
749 case AST_CONTROL_RINGING:
750 case AST_CONTROL_PROGRESS:
751 break;
752 case -1:
753 res = -1;
754 break;
755 case AST_CONTROL_VIDUPDATE:
756 res = -1;
757 break;
758 case AST_CONTROL_HOLD:
759 ast_verbose(" << Console Has Been Placed on Hold >> \n");
760 ast_moh_start(chan, data, mohinterpret);
761 break;
762 case AST_CONTROL_UNHOLD:
763 ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
764 ast_moh_stop(chan);
765 break;
766 case AST_CONTROL_SRCUPDATE:
767 break;
768 default:
769 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, chan->name);
770 res = -1;
773 if (res > -1)
774 write(sndcmd[1], &res, sizeof(res));
776 ast_mutex_unlock(&alsalock);
778 return res;
781 static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state)
783 struct ast_channel *tmp = NULL;
785 if (!(tmp = ast_channel_alloc(1, state, 0, 0, "", p->exten, p->context, 0, "ALSA/%s", indevname)))
786 return NULL;
788 tmp->tech = &alsa_tech;
789 tmp->fds[0] = readdev;
790 tmp->nativeformats = AST_FORMAT_SLINEAR;
791 tmp->readformat = AST_FORMAT_SLINEAR;
792 tmp->writeformat = AST_FORMAT_SLINEAR;
793 tmp->tech_pvt = p;
794 if (!ast_strlen_zero(p->context))
795 ast_copy_string(tmp->context, p->context, sizeof(tmp->context));
796 if (!ast_strlen_zero(p->exten))
797 ast_copy_string(tmp->exten, p->exten, sizeof(tmp->exten));
798 if (!ast_strlen_zero(language))
799 ast_string_field_set(tmp, language, language);
800 p->owner = tmp;
801 ast_module_ref(ast_module_info->self);
802 ast_jb_configure(tmp, &global_jbconf);
803 if (state != AST_STATE_DOWN) {
804 if (ast_pbx_start(tmp)) {
805 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
806 ast_hangup(tmp);
807 tmp = NULL;
811 return tmp;
814 static struct ast_channel *alsa_request(const char *type, int format, void *data, int *cause)
816 int oldformat = format;
817 struct ast_channel *tmp = NULL;
819 format &= AST_FORMAT_SLINEAR;
820 if (!format) {
821 ast_log(LOG_NOTICE, "Asked to get a channel of format '%d'\n", oldformat);
822 return NULL;
825 ast_mutex_lock(&alsalock);
827 if (alsa.owner) {
828 ast_log(LOG_NOTICE, "Already have a call on the ALSA channel\n");
829 *cause = AST_CAUSE_BUSY;
830 } else if (!(tmp = alsa_new(&alsa, AST_STATE_DOWN)))
831 ast_log(LOG_WARNING, "Unable to create new ALSA channel\n");
833 ast_mutex_unlock(&alsalock);
835 return tmp;
838 static int console_autoanswer_deprecated(int fd, int argc, char *argv[])
840 int res = RESULT_SUCCESS;
842 if ((argc != 1) && (argc != 2))
843 return RESULT_SHOWUSAGE;
845 ast_mutex_lock(&alsalock);
847 if (argc == 1) {
848 ast_cli(fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
849 } else {
850 if (!strcasecmp(argv[1], "on"))
851 autoanswer = -1;
852 else if (!strcasecmp(argv[1], "off"))
853 autoanswer = 0;
854 else
855 res = RESULT_SHOWUSAGE;
858 ast_mutex_unlock(&alsalock);
860 return res;
863 static int console_autoanswer(int fd, int argc, char *argv[])
865 int res = RESULT_SUCCESS;;
866 if ((argc != 2) && (argc != 3))
867 return RESULT_SHOWUSAGE;
868 ast_mutex_lock(&alsalock);
869 if (argc == 2) {
870 ast_cli(fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
871 } else {
872 if (!strcasecmp(argv[2], "on"))
873 autoanswer = -1;
874 else if (!strcasecmp(argv[2], "off"))
875 autoanswer = 0;
876 else
877 res = RESULT_SHOWUSAGE;
879 ast_mutex_unlock(&alsalock);
880 return res;
883 static char *autoanswer_complete(const char *line, const char *word, int pos, int state)
885 #ifndef MIN
886 #define MIN(a,b) ((a) < (b) ? (a) : (b))
887 #endif
888 switch (state) {
889 case 0:
890 if (!ast_strlen_zero(word) && !strncasecmp(word, "on", MIN(strlen(word), 2)))
891 return ast_strdup("on");
892 case 1:
893 if (!ast_strlen_zero(word) && !strncasecmp(word, "off", MIN(strlen(word), 3)))
894 return ast_strdup("off");
895 default:
896 return NULL;
898 return NULL;
901 static const char autoanswer_usage[] =
902 "Usage: console autoanswer [on|off]\n"
903 " Enables or disables autoanswer feature. If used without\n"
904 " argument, displays the current on/off status of autoanswer.\n"
905 " The default value of autoanswer is in 'alsa.conf'.\n";
907 static int console_answer_deprecated(int fd, int argc, char *argv[])
909 int res = RESULT_SUCCESS;
911 if (argc != 1)
912 return RESULT_SHOWUSAGE;
914 ast_mutex_lock(&alsalock);
916 if (!alsa.owner) {
917 ast_cli(fd, "No one is calling us\n");
918 res = RESULT_FAILURE;
919 } else {
920 hookstate = 1;
921 cursound = -1;
922 grab_owner();
923 if (alsa.owner) {
924 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
925 ast_queue_frame(alsa.owner, &f);
926 ast_mutex_unlock(&alsa.owner->lock);
928 answer_sound();
931 snd_pcm_prepare(alsa.icard);
932 snd_pcm_start(alsa.icard);
934 ast_mutex_unlock(&alsalock);
936 return RESULT_SUCCESS;
939 static int console_answer(int fd, int argc, char *argv[])
941 int res = RESULT_SUCCESS;
943 if (argc != 2)
944 return RESULT_SHOWUSAGE;
946 ast_mutex_lock(&alsalock);
948 if (!alsa.owner) {
949 ast_cli(fd, "No one is calling us\n");
950 res = RESULT_FAILURE;
951 } else {
952 hookstate = 1;
953 cursound = -1;
954 grab_owner();
955 if (alsa.owner) {
956 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
957 ast_queue_frame(alsa.owner, &f);
958 ast_mutex_unlock(&alsa.owner->lock);
960 answer_sound();
963 snd_pcm_prepare(alsa.icard);
964 snd_pcm_start(alsa.icard);
966 ast_mutex_unlock(&alsalock);
968 return RESULT_SUCCESS;
971 static char sendtext_usage[] =
972 "Usage: console send text <message>\n"
973 " Sends a text message for display on the remote terminal.\n";
975 static int console_sendtext_deprecated(int fd, int argc, char *argv[])
977 int tmparg = 2;
978 int res = RESULT_SUCCESS;
980 if (argc < 2)
981 return RESULT_SHOWUSAGE;
983 ast_mutex_lock(&alsalock);
985 if (!alsa.owner) {
986 ast_cli(fd, "No one is calling us\n");
987 res = RESULT_FAILURE;
988 } else {
989 struct ast_frame f = { AST_FRAME_TEXT, 0 };
990 char text2send[256] = "";
991 text2send[0] = '\0';
992 while (tmparg < argc) {
993 strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send) - 1);
994 strncat(text2send, " ", sizeof(text2send) - strlen(text2send) - 1);
996 text2send[strlen(text2send) - 1] = '\n';
997 f.data = text2send;
998 f.datalen = strlen(text2send) + 1;
999 grab_owner();
1000 if (alsa.owner) {
1001 ast_queue_frame(alsa.owner, &f);
1002 f.frametype = AST_FRAME_CONTROL;
1003 f.subclass = AST_CONTROL_ANSWER;
1004 f.data = NULL;
1005 f.datalen = 0;
1006 ast_queue_frame(alsa.owner, &f);
1007 ast_mutex_unlock(&alsa.owner->lock);
1011 ast_mutex_unlock(&alsalock);
1013 return res;
1016 static int console_sendtext(int fd, int argc, char *argv[])
1018 int tmparg = 3;
1019 int res = RESULT_SUCCESS;
1021 if (argc < 3)
1022 return RESULT_SHOWUSAGE;
1024 ast_mutex_lock(&alsalock);
1026 if (!alsa.owner) {
1027 ast_cli(fd, "No one is calling us\n");
1028 res = RESULT_FAILURE;
1029 } else {
1030 struct ast_frame f = { AST_FRAME_TEXT, 0 };
1031 char text2send[256] = "";
1032 text2send[0] = '\0';
1033 while (tmparg < argc) {
1034 strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send) - 1);
1035 strncat(text2send, " ", sizeof(text2send) - strlen(text2send) - 1);
1037 text2send[strlen(text2send) - 1] = '\n';
1038 f.data = text2send;
1039 f.datalen = strlen(text2send) + 1;
1040 grab_owner();
1041 if (alsa.owner) {
1042 ast_queue_frame(alsa.owner, &f);
1043 f.frametype = AST_FRAME_CONTROL;
1044 f.subclass = AST_CONTROL_ANSWER;
1045 f.data = NULL;
1046 f.datalen = 0;
1047 ast_queue_frame(alsa.owner, &f);
1048 ast_mutex_unlock(&alsa.owner->lock);
1052 ast_mutex_unlock(&alsalock);
1054 return res;
1057 static char answer_usage[] =
1058 "Usage: console answer\n"
1059 " Answers an incoming call on the console (ALSA) channel.\n";
1061 static int console_hangup_deprecated(int fd, int argc, char *argv[])
1063 int res = RESULT_SUCCESS;
1065 if (argc != 1)
1066 return RESULT_SHOWUSAGE;
1068 cursound = -1;
1070 ast_mutex_lock(&alsalock);
1072 if (!alsa.owner && !hookstate) {
1073 ast_cli(fd, "No call to hangup up\n");
1074 res = RESULT_FAILURE;
1075 } else {
1076 hookstate = 0;
1077 grab_owner();
1078 if (alsa.owner) {
1079 ast_queue_hangup(alsa.owner);
1080 ast_mutex_unlock(&alsa.owner->lock);
1084 ast_mutex_unlock(&alsalock);
1086 return res;
1089 static int console_hangup(int fd, int argc, char *argv[])
1091 int res = RESULT_SUCCESS;
1093 if (argc != 2)
1094 return RESULT_SHOWUSAGE;
1096 cursound = -1;
1098 ast_mutex_lock(&alsalock);
1100 if (!alsa.owner && !hookstate) {
1101 ast_cli(fd, "No call to hangup up\n");
1102 res = RESULT_FAILURE;
1103 } else {
1104 hookstate = 0;
1105 grab_owner();
1106 if (alsa.owner) {
1107 ast_queue_hangup(alsa.owner);
1108 ast_mutex_unlock(&alsa.owner->lock);
1112 ast_mutex_unlock(&alsalock);
1114 return res;
1117 static char hangup_usage[] =
1118 "Usage: console hangup\n"
1119 " Hangs up any call currently placed on the console.\n";
1121 static int console_dial_deprecated(int fd, int argc, char *argv[])
1123 char tmp[256], *tmp2;
1124 char *mye, *myc;
1125 char *d;
1126 int res = RESULT_SUCCESS;
1128 if ((argc != 1) && (argc != 2))
1129 return RESULT_SHOWUSAGE;
1131 ast_mutex_lock(&alsalock);
1133 if (alsa.owner) {
1134 if (argc == 2) {
1135 d = argv[1];
1136 grab_owner();
1137 if (alsa.owner) {
1138 struct ast_frame f = { AST_FRAME_DTMF };
1139 while (*d) {
1140 f.subclass = *d;
1141 ast_queue_frame(alsa.owner, &f);
1142 d++;
1144 ast_mutex_unlock(&alsa.owner->lock);
1146 } else {
1147 ast_cli(fd, "You're already in a call. You can use this only to dial digits until you hangup\n");
1148 res = RESULT_FAILURE;
1150 } else {
1151 mye = exten;
1152 myc = context;
1153 if (argc == 2) {
1154 char *stringp = NULL;
1155 ast_copy_string(tmp, argv[1], sizeof(tmp));
1156 stringp = tmp;
1157 strsep(&stringp, "@");
1158 tmp2 = strsep(&stringp, "@");
1159 if (!ast_strlen_zero(tmp))
1160 mye = tmp;
1161 if (!ast_strlen_zero(tmp2))
1162 myc = tmp2;
1164 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
1165 ast_copy_string(alsa.exten, mye, sizeof(alsa.exten));
1166 ast_copy_string(alsa.context, myc, sizeof(alsa.context));
1167 hookstate = 1;
1168 alsa_new(&alsa, AST_STATE_RINGING);
1169 } else
1170 ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
1173 ast_mutex_unlock(&alsalock);
1175 return res;
1178 static int console_dial(int fd, int argc, char *argv[])
1180 char tmp[256], *tmp2;
1181 char *mye, *myc;
1182 char *d;
1183 int res = RESULT_SUCCESS;
1185 if ((argc != 2) && (argc != 3))
1186 return RESULT_SHOWUSAGE;
1188 ast_mutex_lock(&alsalock);
1190 if (alsa.owner) {
1191 if (argc == 3) {
1192 d = argv[2];
1193 grab_owner();
1194 if (alsa.owner) {
1195 struct ast_frame f = { AST_FRAME_DTMF };
1196 while (*d) {
1197 f.subclass = *d;
1198 ast_queue_frame(alsa.owner, &f);
1199 d++;
1201 ast_mutex_unlock(&alsa.owner->lock);
1203 } else {
1204 ast_cli(fd, "You're already in a call. You can use this only to dial digits until you hangup\n");
1205 res = RESULT_FAILURE;
1207 } else {
1208 mye = exten;
1209 myc = context;
1210 if (argc == 3) {
1211 char *stringp = NULL;
1212 ast_copy_string(tmp, argv[2], sizeof(tmp));
1213 stringp = tmp;
1214 strsep(&stringp, "@");
1215 tmp2 = strsep(&stringp, "@");
1216 if (!ast_strlen_zero(tmp))
1217 mye = tmp;
1218 if (!ast_strlen_zero(tmp2))
1219 myc = tmp2;
1221 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
1222 ast_copy_string(alsa.exten, mye, sizeof(alsa.exten));
1223 ast_copy_string(alsa.context, myc, sizeof(alsa.context));
1224 hookstate = 1;
1225 alsa_new(&alsa, AST_STATE_RINGING);
1226 } else
1227 ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
1230 ast_mutex_unlock(&alsalock);
1232 return res;
1235 static char dial_usage[] =
1236 "Usage: console dial [extension[@context]]\n"
1237 " Dials a given extension (and context if specified)\n";
1239 static struct ast_cli_entry cli_alsa_answer_deprecated = {
1240 { "answer", NULL },
1241 console_answer_deprecated, NULL,
1242 NULL };
1244 static struct ast_cli_entry cli_alsa_hangup_deprecated = {
1245 { "hangup", NULL },
1246 console_hangup_deprecated, NULL,
1247 NULL };
1249 static struct ast_cli_entry cli_alsa_dial_deprecated = {
1250 { "dial", NULL },
1251 console_dial_deprecated, NULL,
1252 NULL };
1254 static struct ast_cli_entry cli_alsa_send_text_deprecated = {
1255 { "send", "text", NULL },
1256 console_sendtext_deprecated, NULL,
1257 NULL };
1259 static struct ast_cli_entry cli_alsa_autoanswer_deprecated = {
1260 { "autoanswer", NULL },
1261 console_autoanswer_deprecated, NULL,
1262 NULL, autoanswer_complete };
1264 static struct ast_cli_entry cli_alsa[] = {
1265 { { "console", "answer", NULL },
1266 console_answer, "Answer an incoming console call",
1267 answer_usage, NULL, &cli_alsa_answer_deprecated },
1269 { { "console", "hangup", NULL },
1270 console_hangup, "Hangup a call on the console",
1271 hangup_usage, NULL, &cli_alsa_hangup_deprecated },
1273 { { "console", "dial", NULL },
1274 console_dial, "Dial an extension on the console",
1275 dial_usage, NULL, &cli_alsa_dial_deprecated },
1277 { { "console", "send", "text", NULL },
1278 console_sendtext, "Send text to the remote device",
1279 sendtext_usage, NULL, &cli_alsa_send_text_deprecated },
1281 { { "console", "autoanswer", NULL },
1282 console_autoanswer, "Sets/displays autoanswer",
1283 autoanswer_usage, autoanswer_complete, &cli_alsa_autoanswer_deprecated },
1286 static int load_module(void)
1288 int res;
1289 struct ast_config *cfg;
1290 struct ast_variable *v;
1292 /* Copy the default jb config over global_jbconf */
1293 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
1295 strcpy(mohinterpret, "default");
1297 if ((cfg = ast_config_load(config))) {
1298 v = ast_variable_browse(cfg, "general");
1299 for (; v; v = v->next) {
1300 /* handle jb conf */
1301 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
1302 continue;
1304 if (!strcasecmp(v->name, "autoanswer"))
1305 autoanswer = ast_true(v->value);
1306 else if (!strcasecmp(v->name, "silencesuppression"))
1307 silencesuppression = ast_true(v->value);
1308 else if (!strcasecmp(v->name, "silencethreshold"))
1309 silencethreshold = atoi(v->value);
1310 else if (!strcasecmp(v->name, "context"))
1311 ast_copy_string(context, v->value, sizeof(context));
1312 else if (!strcasecmp(v->name, "language"))
1313 ast_copy_string(language, v->value, sizeof(language));
1314 else if (!strcasecmp(v->name, "extension"))
1315 ast_copy_string(exten, v->value, sizeof(exten));
1316 else if (!strcasecmp(v->name, "input_device"))
1317 ast_copy_string(indevname, v->value, sizeof(indevname));
1318 else if (!strcasecmp(v->name, "output_device"))
1319 ast_copy_string(outdevname, v->value, sizeof(outdevname));
1320 else if (!strcasecmp(v->name, "mohinterpret"))
1321 ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
1323 ast_config_destroy(cfg);
1325 res = pipe(sndcmd);
1326 if (res) {
1327 ast_log(LOG_ERROR, "Unable to create pipe\n");
1328 return -1;
1330 res = soundcard_init();
1331 if (res < 0) {
1332 if (option_verbose > 1) {
1333 ast_verbose(VERBOSE_PREFIX_2 "No sound card detected -- console channel will be unavailable\n");
1334 ast_verbose(VERBOSE_PREFIX_2 "Turn off ALSA support by adding 'noload=chan_alsa.so' in /etc/asterisk/modules.conf\n");
1336 return 0;
1339 res = ast_channel_register(&alsa_tech);
1340 if (res < 0) {
1341 ast_log(LOG_ERROR, "Unable to register channel class 'Console'\n");
1342 return -1;
1344 ast_cli_register_multiple(cli_alsa, sizeof(cli_alsa) / sizeof(struct ast_cli_entry));
1346 ast_pthread_create_background(&sthread, NULL, sound_thread, NULL);
1347 #ifdef ALSA_MONITOR
1348 if (alsa_monitor_start())
1349 ast_log(LOG_ERROR, "Problem starting Monitoring\n");
1350 #endif
1351 return 0;
1354 static int unload_module(void)
1356 ast_channel_unregister(&alsa_tech);
1357 ast_cli_unregister_multiple(cli_alsa, sizeof(cli_alsa) / sizeof(struct ast_cli_entry));
1359 if (alsa.icard)
1360 snd_pcm_close(alsa.icard);
1361 if (alsa.ocard)
1362 snd_pcm_close(alsa.ocard);
1363 if (sndcmd[0] > 0) {
1364 close(sndcmd[0]);
1365 close(sndcmd[1]);
1367 if (alsa.owner)
1368 ast_softhangup(alsa.owner, AST_SOFTHANGUP_APPUNLOAD);
1369 if (alsa.owner)
1370 return -1;
1371 return 0;
1374 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ALSA Console Channel Driver");