Formatting fixes for chan_alsa (issue #7807 reported by Mithraen with more mods done...
[asterisk/asterisk.git] / channels / chan_alsa.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * By Matthew Fredrickson <creslin@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file 
20  * \brief ALSA sound card channel driver 
21  *
22  * \author Matthew Fredrickson <creslin@digium.com>
23  *
24  * \par See also
25  * \arg Config_alsa
26  *
27  * \ingroup channel_drivers
28  */
29
30 /*** MODULEINFO
31         <depend>asound</depend>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
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>
46
47 #define ALSA_PCM_NEW_HW_PARAMS_API
48 #define ALSA_PCM_NEW_SW_PARAMS_API
49 #include <alsa/asoundlib.h>
50
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"
65
66 #include "busy.h"
67 #include "ringtone.h"
68 #include "ring10.h"
69 #include "answer.h"
70
71 #ifdef ALSA_MONITOR
72 #include "alsa-monitor.h"
73 #endif
74
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 = ""
81 };
82 static struct ast_jb_conf global_jbconf;
83
84 #define DEBUG 0
85 /* Which device to use */
86 #define ALSA_INDEV "hw:0,0"
87 #define ALSA_OUTDEV "hw:0,0"
88 #define DESIRED_RATE 8000
89
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 */
93
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);
98
99 /* Don't switch between read/write modes faster than every 300 ms */
100 #define MIN_SWITCH_TIME 600
101
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
107
108 /* static int block = O_NONBLOCK; */
109 static char indevname[50] = ALSA_INDEV;
110 static char outdevname[50] = ALSA_OUTDEV;
111
112 #if 0
113 static struct timeval lasttime;
114 #endif
115
116 static int usecnt;
117 static int silencesuppression = 0;
118 static int silencethreshold = 1000;
119
120 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
121 AST_MUTEX_DEFINE_STATIC(alsalock);
122
123 static const char tdesc[] = "ALSA Console Channel Driver";
124 static const char config[] = "alsa.conf";
125
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];
130
131 static int hookstate = 0;
132
133 static short silence[FRAME_SIZE] = { 0, };
134
135 struct sound {
136         int ind;
137         short *data;
138         int datalen;
139         int samplen;
140         int silencelen;
141         int repeat;
142 };
143
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},
150 };
151
152 /* Sound command pipe */
153 static int sndcmd[2];
154
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];
161 #if 0
162         snd_pcm_t *card;
163 #endif
164         snd_pcm_t *icard, *ocard;
165
166 } alsa;
167
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, 
170    usually plenty. */
171
172 pthread_t sthread;
173
174 #define MAX_BUFFER_SIZE 100
175
176 /* File descriptors for sound device */
177 static int readdev = -1;
178 static int writedev = -1;
179
180 static int autoanswer = 1;
181
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;
187
188 /* ZZ */
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);
199
200 static const struct ast_channel_tech alsa_tech = {
201         .type = "Console",
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,
209         .read = alsa_read,
210         .call = alsa_call,
211         .write = alsa_write,
212         .indicate = alsa_indicate,
213         .fixup = alsa_fixup,
214 };
215
216 static int send_sound(void)
217 {
218         short myframe[FRAME_SIZE];
219         int total = FRAME_SIZE;
220         short *frame = NULL;
221         int amt = 0, res, myoff;
222         snd_pcm_state_t state;
223
224         if (cursound == -1)
225                 return 0;
226         
227         res = total;
228         if (sampsent < sounds[cursound].samplen) {
229                 myoff = 0;
230                 while (total) {
231                         amt = total;
232                         if (amt > (sounds[cursound].datalen - offset))
233                                 amt = sounds[cursound].datalen - offset;
234                         memcpy(myframe + myoff, sounds[cursound].data + offset, amt * 2);
235                         total -= amt;
236                         offset += amt;
237                         sampsent += amt;
238                         myoff += amt;
239                         if (offset >= sounds[cursound].datalen)
240                                 offset = 0;
241                 }
242                 /* Set it up for silence */
243                 if (sampsent >= sounds[cursound].samplen)
244                         silencelen = sounds[cursound].silencelen;
245                 frame = myframe;
246         } else {
247                 if (silencelen > 0) {
248                         frame = silence;
249                         silencelen -= res;
250                 } else {
251                         if (sounds[cursound].repeat) {
252                                 /* Start over */
253                                 sampsent = 0;
254                                 offset = 0;
255                         } else {
256                                 cursound = -1;
257                                 nosound = 0;
258                         }
259                         return 0;
260                 }
261         }
262         
263         if (res == 0 || !frame)
264                 return 0;
265
266 #ifdef ALSA_MONITOR
267         alsa_monitor_write((char *) frame, res * 2);
268 #endif
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);
273         if (res > 0)
274                 return 0;
275         return 0;
276 }
277
278 static void *sound_thread(void *unused)
279 {
280         fd_set rfds;
281         fd_set wfds;
282         int max, res;
283
284         for (;;) {
285                 FD_ZERO(&rfds);
286                 FD_ZERO(&wfds);
287                 max = sndcmd[0];
288                 FD_SET(sndcmd[0], &rfds);
289                 if (cursound > -1) {
290                         FD_SET(writedev, &wfds);
291                         if (writedev > max)
292                                 max = writedev;
293                 }
294 #ifdef ALSA_MONITOR
295                 if (!alsa.owner) {
296                         FD_SET(readdev, &rfds);
297                         if (readdev > max)
298                                 max = readdev;
299                 }
300 #endif
301                 res = ast_select(max + 1, &rfds, &wfds, NULL, NULL);
302                 if (res < 1) {
303                         ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
304                         continue;
305                 }
306 #ifdef ALSA_MONITOR
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];
311                         int r;
312
313                         state = snd_pcm_state(alsa.ocard);
314                         if (state == SND_PCM_STATE_XRUN) {
315                                 snd_pcm_prepare(alsa.ocard);
316                         }
317                         r = snd_pcm_readi(alsa.icard, buf, FRAME_SIZE);
318                         if (r == -EPIPE) {
319 #if DEBUG
320                                 ast_log(LOG_ERROR, "XRUN read\n");
321 #endif
322                                 snd_pcm_prepare(alsa.icard);
323                         } else if (r == -ESTRPIPE) {
324                                 ast_log(LOG_ERROR, "-ESTRPIPE\n");
325                                 snd_pcm_prepare(alsa.icard);
326                         } else if (r < 0) {
327                                 ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
328                         } else
329                                 alsa_monitor_read((char *) buf, r * 2);
330                 }
331 #endif
332                 if (FD_ISSET(sndcmd[0], &rfds)) {
333                         read(sndcmd[0], &cursound, sizeof(cursound));
334                         silencelen = 0;
335                         offset = 0;
336                         sampsent = 0;
337                 }
338                 if (FD_ISSET(writedev, &wfds))
339                         if (send_sound())
340                                 ast_log(LOG_WARNING, "Failed to write sound\n");
341         }
342         /* Never reached */
343         return NULL;
344 }
345
346 static snd_pcm_t *alsa_card_init(char *dev, snd_pcm_stream_t stream)
347 {
348         int err;
349         int direction;
350         snd_pcm_t *handle = NULL;
351         snd_pcm_hw_params_t *hwparams = NULL;
352         snd_pcm_sw_params_t *swparams = NULL;
353         struct pollfd pfd;
354         snd_pcm_uframes_t period_size = PERIOD_FRAMES * 4;
355         /* int period_bytes = 0; */
356         snd_pcm_uframes_t buffer_size = 0;
357
358         unsigned int rate = DESIRED_RATE;
359 #if 0
360         unsigned int per_min = 1;
361 #endif
362         /* unsigned int per_max = 8; */
363         snd_pcm_uframes_t start_threshold, stop_threshold;
364
365         err = snd_pcm_open(&handle, dev, stream, O_NONBLOCK);
366         if (err < 0) {
367                 ast_log(LOG_ERROR, "snd_pcm_open failed: %s\n", snd_strerror(err));
368                 return NULL;
369         } else
370                 ast_log(LOG_DEBUG, "Opening device %s in %s mode\n", dev, (stream == SND_PCM_STREAM_CAPTURE) ? "read" : "write");
371
372         snd_pcm_hw_params_alloca(&hwparams);
373         snd_pcm_hw_params_any(handle, hwparams);
374
375         err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
376         if (err < 0)
377                 ast_log(LOG_ERROR, "set_access failed: %s\n", snd_strerror(err));
378
379         err = snd_pcm_hw_params_set_format(handle, hwparams, format);
380         if (err < 0)
381                 ast_log(LOG_ERROR, "set_format failed: %s\n", snd_strerror(err));
382
383         err = snd_pcm_hw_params_set_channels(handle, hwparams, 1);
384         if (err < 0)
385                 ast_log(LOG_ERROR, "set_channels failed: %s\n", snd_strerror(err));
386
387         direction = 0;
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);
391
392         direction = 0;
393         err = snd_pcm_hw_params_set_period_size_near(handle, hwparams, &period_size, &direction);
394         if (err < 0)
395                 ast_log(LOG_ERROR, "period_size(%ld frames) is bad: %s\n", period_size, snd_strerror(err));
396         else
397                 ast_log(LOG_DEBUG, "Period size is %d\n", err);
398
399         buffer_size = 4096 * 2;         /* period_size * 16; */
400         err = snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &buffer_size);
401         if (err < 0)
402                 ast_log(LOG_WARNING, "Problem setting buffer size of %ld: %s\n", buffer_size, snd_strerror(err));
403         else
404                 ast_log(LOG_DEBUG, "Buffer size is set to %d frames\n", err);
405
406 #if 0
407         direction = 0;
408         err = snd_pcm_hw_params_set_periods_min(handle, hwparams, &per_min, &direction);
409         if (err < 0)
410                 ast_log(LOG_ERROR, "periods_min: %s\n", snd_strerror(err));
411
412         err = snd_pcm_hw_params_set_periods_max(handle, hwparams, &per_max, 0);
413         if (err < 0)
414                 ast_log(LOG_ERROR, "periods_max: %s\n", snd_strerror(err));
415 #endif
416
417         err = snd_pcm_hw_params(handle, hwparams);
418         if (err < 0)
419                 ast_log(LOG_ERROR, "Couldn't set the new hw params: %s\n", snd_strerror(err));
420
421         snd_pcm_sw_params_alloca(&swparams);
422         snd_pcm_sw_params_current(handle, swparams);
423
424 #if 1
425         if (stream == SND_PCM_STREAM_PLAYBACK)
426                 start_threshold = period_size;
427         else
428                 start_threshold = 1;
429
430         err = snd_pcm_sw_params_set_start_threshold(handle, swparams, start_threshold);
431         if (err < 0)
432                 ast_log(LOG_ERROR, "start threshold: %s\n", snd_strerror(err));
433 #endif
434
435 #if 1
436         if (stream == SND_PCM_STREAM_PLAYBACK)
437                 stop_threshold = buffer_size;
438         else
439                 stop_threshold = buffer_size;
440
441         err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, stop_threshold);
442         if (err < 0)
443                 ast_log(LOG_ERROR, "stop threshold: %s\n", snd_strerror(err));
444 #endif
445 #if 0
446         err = snd_pcm_sw_params_set_xfer_align(handle, swparams, PERIOD_FRAMES);
447         if (err < 0)
448                 ast_log(LOG_ERROR, "Unable to set xfer alignment: %s\n", snd_strerror(err));
449 #endif
450
451 #if 0
452         err = snd_pcm_sw_params_set_silence_threshold(handle, swparams, silencethreshold);
453         if (err < 0)
454                 ast_log(LOG_ERROR, "Unable to set silence threshold: %s\n", snd_strerror(err));
455 #endif
456         err = snd_pcm_sw_params(handle, swparams);
457         if (err < 0)
458                 ast_log(LOG_ERROR, "sw_params: %s\n", snd_strerror(err));
459
460         err = snd_pcm_poll_descriptors_count(handle);
461         if (err <= 0)
462                 ast_log(LOG_ERROR, "Unable to get a poll descriptors count, error is %s\n", snd_strerror(err));
463         if (err != 1)
464                 ast_log(LOG_DEBUG, "Can't handle more than one device\n");
465
466         snd_pcm_poll_descriptors(handle, &pfd, err);
467         ast_log(LOG_DEBUG, "Acquired fd %d from the poll descriptor\n", pfd.fd);
468
469         if (stream == SND_PCM_STREAM_CAPTURE)
470                 readdev = pfd.fd;
471         else
472                 writedev = pfd.fd;
473
474         return handle;
475 }
476
477 static int soundcard_init(void)
478 {
479         alsa.icard = alsa_card_init(indevname, SND_PCM_STREAM_CAPTURE);
480         alsa.ocard = alsa_card_init(outdevname, SND_PCM_STREAM_PLAYBACK);
481
482         if (!alsa.icard || !alsa.ocard) {
483                 ast_log(LOG_ERROR, "Problem opening alsa I/O devices\n");
484                 return -1;
485         }
486
487         return readdev;
488 }
489
490 static int alsa_digit(struct ast_channel *c, char digit)
491 {
492         ast_mutex_lock(&alsalock);
493         ast_verbose(" << Console Received digit %c >> \n", digit);
494         ast_mutex_unlock(&alsalock);
495         return 0;
496 }
497
498 static int alsa_text(struct ast_channel *c, const char *text)
499 {
500         ast_mutex_lock(&alsalock);
501         ast_verbose(" << Console Received text %s >> \n", text);
502         ast_mutex_unlock(&alsalock);
503         return 0;
504 }
505
506 static void grab_owner(void)
507 {
508         while (alsa.owner && ast_mutex_trylock(&alsa.owner->lock)) {
509                 ast_mutex_unlock(&alsalock);
510                 usleep(1);
511                 ast_mutex_lock(&alsalock);
512         }
513 }
514
515 static int alsa_call(struct ast_channel *c, char *dest, int timeout)
516 {
517         int res = 3;
518         struct ast_frame f = { AST_FRAME_CONTROL };
519         ast_mutex_lock(&alsalock);
520         ast_verbose(" << Call placed to '%s' on console >> \n", dest);
521         if (autoanswer) {
522                 ast_verbose(" << Auto-answered >> \n");
523                 grab_owner();
524                 if (alsa.owner) {
525                         f.subclass = AST_CONTROL_ANSWER;
526                         ast_queue_frame(alsa.owner, &f);
527                         ast_mutex_unlock(&alsa.owner->lock);
528                 }
529         } else {
530                 ast_verbose(" << Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
531                 grab_owner();
532                 if (alsa.owner) {
533                         f.subclass = AST_CONTROL_RINGING;
534                         ast_queue_frame(alsa.owner, &f);
535                         ast_mutex_unlock(&alsa.owner->lock);
536                 }
537                 write(sndcmd[1], &res, sizeof(res));
538         }
539         snd_pcm_prepare(alsa.icard);
540         snd_pcm_start(alsa.icard);
541         ast_mutex_unlock(&alsalock);
542         return 0;
543 }
544
545 static void answer_sound(void)
546 {
547         int res;
548         nosound = 1;
549         res = 4;
550         write(sndcmd[1], &res, sizeof(res));
551
552 }
553
554 static int alsa_answer(struct ast_channel *c)
555 {
556         ast_mutex_lock(&alsalock);
557         ast_verbose(" << Console call has been answered >> \n");
558         answer_sound();
559         ast_setstate(c, AST_STATE_UP);
560         cursound = -1;
561         snd_pcm_prepare(alsa.icard);
562         snd_pcm_start(alsa.icard);
563         ast_mutex_unlock(&alsalock);
564         return 0;
565 }
566
567 static int alsa_hangup(struct ast_channel *c)
568 {
569         int res;
570         ast_mutex_lock(&alsalock);
571         cursound = -1;
572         c->tech_pvt = NULL;
573         alsa.owner = NULL;
574         ast_verbose(" << Hangup on console >> \n");
575         ast_mutex_lock(&usecnt_lock);
576         usecnt--;
577         ast_mutex_unlock(&usecnt_lock);
578         if (hookstate) {
579                 hookstate = 0;
580                 if (!autoanswer) {
581                         /* Congestion noise */
582                         res = 2;
583                         write(sndcmd[1], &res, sizeof(res));
584                 }
585         }
586         snd_pcm_drop(alsa.icard);
587         ast_mutex_unlock(&alsalock);
588         return 0;
589 }
590
591 static int alsa_write(struct ast_channel *chan, struct ast_frame *f)
592 {
593         static char sizbuf[8000];
594         static int sizpos = 0;
595         int len = sizpos;
596         int pos;
597         int res = 0;
598         /* size_t frames = 0; */
599         snd_pcm_state_t state;
600
601         /* Immediately return if no sound is enabled */
602         if (nosound)
603                 return 0;
604
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);
610                 cursound = -1;
611         }
612
613
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");
617                 res = -1;
618         } else {
619                 memcpy(sizbuf + sizpos, f->data, f->datalen);
620                 len += f->datalen;
621                 pos = 0;
622 #ifdef ALSA_MONITOR
623                 alsa_monitor_write(sizbuf, len);
624 #endif
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);
629                 if (res == -EPIPE) {
630 #if DEBUG
631                         ast_log(LOG_DEBUG, "XRUN write\n");
632 #endif
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));
637                                 res = -1;
638                         } else if (res < 0) {
639                                 ast_log(LOG_ERROR, "Write error %s\n", snd_strerror(res));
640                                 res = -1;
641                         }
642                 } else {
643                         if (res == -ESTRPIPE)
644                                 ast_log(LOG_ERROR, "You've got some big problems\n");
645                         else if (res < 0)
646                                 ast_log(LOG_NOTICE, "Error %d on write\n", res);
647                 }
648         }
649         ast_mutex_unlock(&alsalock);
650         if (res > 0)
651                 res = 0;
652         return res;
653 }
654
655
656 static struct ast_frame *alsa_read(struct ast_channel *chan)
657 {
658         static struct ast_frame f;
659         static short __buf[FRAME_SIZE + AST_FRIENDLY_OFFSET / 2];
660         short *buf;
661         static int readpos = 0;
662         static int left = FRAME_SIZE;
663         snd_pcm_state_t state;
664         int r = 0;
665         int off = 0;
666
667         ast_mutex_lock(&alsalock);
668         /* Acknowledge any pending cmd */
669         f.frametype = AST_FRAME_NULL;
670         f.subclass = 0;
671         f.samples = 0;
672         f.datalen = 0;
673         f.data = NULL;
674         f.offset = 0;
675         f.src = "Console";
676         f.mallocd = 0;
677         f.delivery.tv_sec = 0;
678         f.delivery.tv_usec = 0;
679
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);
683         }
684
685         buf = __buf + AST_FRIENDLY_OFFSET / 2;
686
687         r = snd_pcm_readi(alsa.icard, buf + readpos, left);
688         if (r == -EPIPE) {
689 #if DEBUG
690                 ast_log(LOG_ERROR, "XRUN read\n");
691 #endif
692                 snd_pcm_prepare(alsa.icard);
693         } else if (r == -ESTRPIPE) {
694                 ast_log(LOG_ERROR, "-ESTRPIPE\n");
695                 snd_pcm_prepare(alsa.icard);
696         } else if (r < 0) {
697                 ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
698         } else if (r >= 0) {
699                 off -= r;
700         }
701         /* Update positions */
702         readpos += r;
703         left -= r;
704
705         if (readpos >= FRAME_SIZE) {
706                 /* A real frame */
707                 readpos = 0;
708                 left = FRAME_SIZE;
709                 if (chan->_state != AST_STATE_UP) {
710                         /* Don't transmit unless it's up */
711                         ast_mutex_unlock(&alsalock);
712                         return &f;
713                 }
714                 f.frametype = AST_FRAME_VOICE;
715                 f.subclass = AST_FORMAT_SLINEAR;
716                 f.samples = FRAME_SIZE;
717                 f.datalen = FRAME_SIZE * 2;
718                 f.data = buf;
719                 f.offset = AST_FRIENDLY_OFFSET;
720                 f.src = "Console";
721                 f.mallocd = 0;
722 #ifdef ALSA_MONITOR
723                 alsa_monitor_read((char *) buf, FRAME_SIZE * 2);
724 #endif
725
726         }
727         ast_mutex_unlock(&alsalock);
728         return &f;
729 }
730
731 static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
732 {
733         struct chan_alsa_pvt *p = newchan->tech_pvt;
734         ast_mutex_lock(&alsalock);
735         p->owner = newchan;
736         ast_mutex_unlock(&alsalock);
737         return 0;
738 }
739
740 static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen)
741 {
742         int res = 0;
743
744         ast_mutex_lock(&alsalock);
745
746         switch (cond) {
747                 case AST_CONTROL_BUSY:
748                         res = 1;
749                         break;
750                 case AST_CONTROL_CONGESTION:
751                         res = 2;
752                         break;
753                 case AST_CONTROL_RINGING:
754                         break;
755                 case -1:
756                         res = -1;
757                         break;
758                 case AST_CONTROL_VIDUPDATE:
759                         res = -1;
760                         break;
761                 case AST_CONTROL_HOLD:
762                         ast_verbose(" << Console Has Been Placed on Hold >> \n");
763                         ast_moh_start(chan, data, mohinterpret);
764                         break;
765                 case AST_CONTROL_UNHOLD:
766                         ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
767                         ast_moh_stop(chan);
768                         break;
769                 default:
770                         ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, chan->name);
771                         res = -1;
772         }
773
774         if (res > -1)
775                 write(sndcmd[1], &res, sizeof(res));
776
777         ast_mutex_unlock(&alsalock);
778
779         return res;
780 }
781
782 static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state)
783 {
784         struct ast_channel *tmp = NULL;
785         
786         if (!(tmp = ast_channel_alloc(1)))
787                 return NULL;
788
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;
795         tmp->tech_pvt = p;
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);
802         p->owner = tmp;
803         ast_setstate(tmp, state);
804         ast_mutex_lock(&usecnt_lock);
805         usecnt++;
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);
812                         ast_hangup(tmp);
813                         tmp = NULL;
814                 }
815         }
816
817         return tmp;
818 }
819
820 static struct ast_channel *alsa_request(const char *type, int format, void *data, int *cause)
821 {
822         int oldformat = format;
823         struct ast_channel *tmp = NULL;
824
825         format &= AST_FORMAT_SLINEAR;
826         if (!format) {
827                 ast_log(LOG_NOTICE, "Asked to get a channel of format '%d'\n", oldformat);
828                 return NULL;
829         }
830
831         ast_mutex_lock(&alsalock);
832
833         if (alsa.owner) {
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");
838
839         ast_mutex_unlock(&alsalock);
840
841         return tmp;
842 }
843
844 static int console_autoanswer(int fd, int argc, char *argv[])
845 {
846         int res = RESULT_SUCCESS;
847
848         if ((argc != 1) && (argc != 2))
849                 return RESULT_SHOWUSAGE;
850
851         ast_mutex_lock(&alsalock);
852
853         if (argc == 1) {
854                 ast_cli(fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
855         } else {
856                 if (!strcasecmp(argv[1], "on"))
857                         autoanswer = -1;
858                 else if (!strcasecmp(argv[1], "off"))
859                         autoanswer = 0;
860                 else
861                         res = RESULT_SHOWUSAGE;
862         }
863
864         ast_mutex_unlock(&alsalock);
865
866         return res;
867 }
868
869 static char *autoanswer_complete(const char *line, const char *word, int pos, int state)
870 {
871 #ifndef MIN
872 #define MIN(a,b) ((a) < (b) ? (a) : (b))
873 #endif
874         switch (state) {
875                 case 0:
876                         if (!ast_strlen_zero(word) && !strncasecmp(word, "on", MIN(strlen(word), 2)))
877                                 return ast_strdup("on");
878                 case 1:
879                         if (!ast_strlen_zero(word) && !strncasecmp(word, "off", MIN(strlen(word), 3)))
880                                 return ast_strdup("off");
881                 default:
882                         return NULL;
883         }
884         return NULL;
885 }
886
887 static const char autoanswer_usage[] =
888         "Usage: autoanswer [on|off]\n"
889         "       Enables or disables autoanswer feature.  If used without\n"
890         "       argument, displays the current on/off status of autoanswer.\n" "       The default value of autoanswer is in 'alsa.conf'.\n";
891
892 static int console_answer(int fd, int argc, char *argv[])
893 {
894         int res = RESULT_SUCCESS;
895
896         if (argc != 1)
897                 return RESULT_SHOWUSAGE;
898
899         ast_mutex_lock(&alsalock);
900
901         if (!alsa.owner) {
902                 ast_cli(fd, "No one is calling us\n");
903                 res = RESULT_FAILURE;
904         } else {
905                 hookstate = 1;
906                 cursound = -1;
907                 grab_owner();
908                 if (alsa.owner) {
909                         struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
910                         ast_queue_frame(alsa.owner, &f);
911                         ast_mutex_unlock(&alsa.owner->lock);
912                 }
913                 answer_sound();
914         }
915
916         snd_pcm_prepare(alsa.icard);
917         snd_pcm_start(alsa.icard);
918
919         ast_mutex_unlock(&alsalock);
920
921         return RESULT_SUCCESS;
922 }
923
924 static char sendtext_usage[] = "Usage: send text <message>\n" "       Sends a text message for display on the remote terminal.\n";
925
926 static int console_sendtext(int fd, int argc, char *argv[])
927 {
928         int tmparg = 2;
929         int res = RESULT_SUCCESS;
930
931         if (argc < 2)
932                 return RESULT_SHOWUSAGE;
933
934         ast_mutex_lock(&alsalock);
935
936         if (!alsa.owner) {
937                 ast_cli(fd, "No one is calling us\n");
938                 res = RESULT_FAILURE;
939         } else {
940                 struct ast_frame f = { AST_FRAME_TEXT, 0 };
941                 char text2send[256] = "";
942                 text2send[0] = '\0';
943                 while (tmparg < argc) {
944                         strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send) - 1);
945                         strncat(text2send, " ", sizeof(text2send) - strlen(text2send) - 1);
946                 }
947                 text2send[strlen(text2send) - 1] = '\n';
948                 f.data = text2send;
949                 f.datalen = strlen(text2send) + 1;
950                 grab_owner();
951                 if (alsa.owner) {
952                         ast_queue_frame(alsa.owner, &f);
953                         f.frametype = AST_FRAME_CONTROL;
954                         f.subclass = AST_CONTROL_ANSWER;
955                         f.data = NULL;
956                         f.datalen = 0;
957                         ast_queue_frame(alsa.owner, &f);
958                         ast_mutex_unlock(&alsa.owner->lock);
959                 }
960         }
961
962         ast_mutex_unlock(&alsalock);
963
964         return res;
965 }
966
967 static char answer_usage[] = "Usage: answer\n" "       Answers an incoming call on the console (ALSA) channel.\n";
968
969 static int console_hangup(int fd, int argc, char *argv[])
970 {
971         int res = RESULT_SUCCESS;
972
973         if (argc != 1)
974                 return RESULT_SHOWUSAGE;
975
976         cursound = -1;
977
978         ast_mutex_lock(&alsalock);
979
980         if (!alsa.owner && !hookstate) {
981                 ast_cli(fd, "No call to hangup up\n");
982                 res = RESULT_FAILURE;
983         } else {
984                 hookstate = 0;
985                 grab_owner();
986                 if (alsa.owner) {
987                         ast_queue_hangup(alsa.owner);
988                         ast_mutex_unlock(&alsa.owner->lock);
989                 }
990         }
991
992         ast_mutex_unlock(&alsalock);
993
994         return res;
995 }
996
997 static char hangup_usage[] = "Usage: hangup\n" "       Hangs up any call currently placed on the console.\n";
998
999
1000 static int console_dial(int fd, int argc, char *argv[])
1001 {
1002         char tmp[256], *tmp2;
1003         char *mye, *myc;
1004         char *d;
1005         int res = RESULT_SUCCESS;
1006
1007         if ((argc != 1) && (argc != 2))
1008                 return RESULT_SHOWUSAGE;
1009
1010         ast_mutex_lock(&alsalock);
1011
1012         if (alsa.owner) {
1013                 if (argc == 2) {
1014                         d = argv[1];
1015                         grab_owner();
1016                         if (alsa.owner) {
1017                                 struct ast_frame f = { AST_FRAME_DTMF };
1018                                 while (*d) {
1019                                         f.subclass = *d;
1020                                         ast_queue_frame(alsa.owner, &f);
1021                                         d++;
1022                                 }
1023                                 ast_mutex_unlock(&alsa.owner->lock);
1024                         }
1025                 } else {
1026                         ast_cli(fd, "You're already in a call.  You can use this only to dial digits until you hangup\n");
1027                         res = RESULT_FAILURE;
1028                 }
1029         } else {
1030                 mye = exten;
1031                 myc = context;
1032                 if (argc == 2) {
1033                         char *stringp = NULL;
1034                         strncpy(tmp, argv[1], sizeof(tmp) - 1);
1035                         stringp = tmp;
1036                         strsep(&stringp, "@");
1037                         tmp2 = strsep(&stringp, "@");
1038                         if (!ast_strlen_zero(tmp))
1039                                 mye = tmp;
1040                         if (!ast_strlen_zero(tmp2))
1041                                 myc = tmp2;
1042                 }
1043                 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
1044                         strncpy(alsa.exten, mye, sizeof(alsa.exten) - 1);
1045                         strncpy(alsa.context, myc, sizeof(alsa.context) - 1);
1046                         hookstate = 1;
1047                         alsa_new(&alsa, AST_STATE_RINGING);
1048                 } else
1049                         ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
1050         }
1051
1052         ast_mutex_unlock(&alsalock);
1053
1054         return res;
1055 }
1056
1057 static char dial_usage[] = "Usage: dial [extension[@context]]\n" "       Dials a given extension (and context if specified)\n";
1058
1059
1060 static struct ast_cli_entry myclis[] = {
1061         {{"answer", NULL}, console_answer, "Answer an incoming console call", answer_usage},
1062         {{"hangup", NULL}, console_hangup, "Hangup a call on the console", hangup_usage},
1063         {{"dial", NULL}, console_dial, "Dial an extension on the console", dial_usage},
1064         {{"send", "text", NULL}, console_sendtext, "Send text to the remote device", sendtext_usage},
1065         {{"autoanswer", NULL}, console_autoanswer, "Sets/displays autoanswer", autoanswer_usage, autoanswer_complete}
1066 };
1067
1068 static int load_module(void)
1069 {
1070         int res, x;
1071         struct ast_config *cfg;
1072         struct ast_variable *v;
1073
1074         /* Copy the default jb config over global_jbconf */
1075         memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
1076
1077         strcpy(mohinterpret, "default");
1078
1079         if ((cfg = ast_config_load(config))) {
1080                 v = ast_variable_browse(cfg, "general");
1081                 for (; v; v = v->next) {
1082                         /* handle jb conf */
1083                         if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
1084                                 continue;
1085
1086                         if (!strcasecmp(v->name, "autoanswer"))
1087                                 autoanswer = ast_true(v->value);
1088                         else if (!strcasecmp(v->name, "silencesuppression"))
1089                                 silencesuppression = ast_true(v->value);
1090                         else if (!strcasecmp(v->name, "silencethreshold"))
1091                                 silencethreshold = atoi(v->value);
1092                         else if (!strcasecmp(v->name, "context"))
1093                                 ast_copy_string(context, v->value, sizeof(context));
1094                         else if (!strcasecmp(v->name, "language"))
1095                                 ast_copy_string(language, v->value, sizeof(language));
1096                         else if (!strcasecmp(v->name, "extension"))
1097                                 ast_copy_string(exten, v->value, sizeof(exten));
1098                         else if (!strcasecmp(v->name, "input_device"))
1099                                 ast_copy_string(indevname, v->value, sizeof(indevname));
1100                         else if (!strcasecmp(v->name, "output_device"))
1101                                 ast_copy_string(outdevname, v->value, sizeof(outdevname));
1102                         else if (!strcasecmp(v->name, "mohinterpret"))
1103                                 ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
1104                 }
1105                 ast_config_destroy(cfg);
1106         }
1107         res = pipe(sndcmd);
1108         if (res) {
1109                 ast_log(LOG_ERROR, "Unable to create pipe\n");
1110                 return -1;
1111         }
1112         res = soundcard_init();
1113         if (res < 0) {
1114                 if (option_verbose > 1) {
1115                         ast_verbose(VERBOSE_PREFIX_2 "No sound card detected -- console channel will be unavailable\n");
1116                         ast_verbose(VERBOSE_PREFIX_2 "Turn off ALSA support by adding 'noload=chan_alsa.so' in /etc/asterisk/modules.conf\n");
1117                 }
1118                 return 0;
1119         }
1120
1121         res = ast_channel_register(&alsa_tech);
1122         if (res < 0) {
1123                 ast_log(LOG_ERROR, "Unable to register channel class 'Console'\n");
1124                 return -1;
1125         }
1126         for (x = 0; x < sizeof(myclis) / sizeof(struct ast_cli_entry); x++)
1127                 ast_cli_register(myclis + x);
1128         ast_pthread_create(&sthread, NULL, sound_thread, NULL);
1129 #ifdef ALSA_MONITOR
1130         if (alsa_monitor_start())
1131                 ast_log(LOG_ERROR, "Problem starting Monitoring\n");
1132 #endif
1133         return 0;
1134 }
1135
1136 static int unload_module(void)
1137 {
1138         int x;
1139
1140         ast_channel_unregister(&alsa_tech);
1141         for (x = 0; x < sizeof(myclis) / sizeof(struct ast_cli_entry); x++)
1142                 ast_cli_unregister(myclis + x);
1143         if (alsa.icard)
1144                 snd_pcm_close(alsa.icard);
1145         if (alsa.ocard)
1146                 snd_pcm_close(alsa.ocard);
1147         if (sndcmd[0] > 0) {
1148                 close(sndcmd[0]);
1149                 close(sndcmd[1]);
1150         }
1151         if (alsa.owner)
1152                 ast_softhangup(alsa.owner, AST_SOFTHANGUP_APPUNLOAD);
1153         if (alsa.owner)
1154                 return -1;
1155         return 0;
1156 }
1157
1158 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ALSA Console Channel Driver");