Fri Jul 3 20:35:34 2009

Asterisk developer's documentation


chan_alsa.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * By Matthew Fredrickson <creslin@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file 
00020  * \brief ALSA sound card channel driver 
00021  *
00022  * \author Matthew Fredrickson <creslin@digium.com>
00023  *
00024  * \par See also
00025  * \arg Config_alsa
00026  *
00027  * \ingroup channel_drivers
00028  */
00029 
00030 /*** MODULEINFO
00031    <depend>asound</depend>
00032  ***/
00033 
00034 #include "asterisk.h"
00035 
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00037 
00038 #include <unistd.h>
00039 #include <fcntl.h>
00040 #include <errno.h>
00041 #include <sys/ioctl.h>
00042 #include <sys/time.h>
00043 #include <string.h>
00044 #include <stdlib.h>
00045 #include <stdio.h>
00046 
00047 #define ALSA_PCM_NEW_HW_PARAMS_API
00048 #define ALSA_PCM_NEW_SW_PARAMS_API
00049 #include <alsa/asoundlib.h>
00050 
00051 #include "asterisk/frame.h"
00052 #include "asterisk/logger.h"
00053 #include "asterisk/channel.h"
00054 #include "asterisk/module.h"
00055 #include "asterisk/options.h"
00056 #include "asterisk/pbx.h"
00057 #include "asterisk/config.h"
00058 #include "asterisk/cli.h"
00059 #include "asterisk/utils.h"
00060 #include "asterisk/causes.h"
00061 #include "asterisk/endian.h"
00062 #include "asterisk/stringfields.h"
00063 #include "asterisk/abstract_jb.h"
00064 #include "asterisk/musiconhold.h"
00065 
00066 #include "busy_tone.h"
00067 #include "ring_tone.h"
00068 #include "ring10.h"
00069 #include "answer.h"
00070 
00071 #ifdef ALSA_MONITOR
00072 #include "alsa-monitor.h"
00073 #endif
00074 
00075 /*! Global jitterbuffer configuration - by default, jb is disabled */
00076 static struct ast_jb_conf default_jbconf = {
00077    .flags = 0,
00078    .max_size = -1,
00079    .resync_threshold = -1,
00080    .impl = ""
00081 };
00082 static struct ast_jb_conf global_jbconf;
00083 
00084 #define DEBUG 0
00085 /* Which device to use */
00086 #define ALSA_INDEV "default"
00087 #define ALSA_OUTDEV "default"
00088 #define DESIRED_RATE 8000
00089 
00090 /* Lets use 160 sample frames, just like GSM.  */
00091 #define FRAME_SIZE 160
00092 #define PERIOD_FRAMES 80      /* 80 Frames, at 2 bytes each */
00093 
00094 /* When you set the frame size, you have to come up with
00095    the right buffer format as well. */
00096 /* 5 64-byte frames = one frame */
00097 #define BUFFER_FMT ((buffersize * 10) << 16) | (0x0006);
00098 
00099 /* Don't switch between read/write modes faster than every 300 ms */
00100 #define MIN_SWITCH_TIME 600
00101 
00102 #if __BYTE_ORDER == __LITTLE_ENDIAN
00103 static snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
00104 #else
00105 static snd_pcm_format_t format = SND_PCM_FORMAT_S16_BE;
00106 #endif
00107 
00108 static char indevname[50] = ALSA_INDEV;
00109 static char outdevname[50] = ALSA_OUTDEV;
00110 
00111 #if 0
00112 static struct timeval lasttime;
00113 #endif
00114 
00115 static int silencesuppression = 0;
00116 static int silencethreshold = 1000;
00117 
00118 AST_MUTEX_DEFINE_STATIC(alsalock);
00119 
00120 static const char tdesc[] = "ALSA Console Channel Driver";
00121 static const char config[] = "alsa.conf";
00122 
00123 static char context[AST_MAX_CONTEXT] = "default";
00124 static char language[MAX_LANGUAGE] = "";
00125 static char exten[AST_MAX_EXTENSION] = "s";
00126 static char mohinterpret[MAX_MUSICCLASS];
00127 
00128 static int hookstate = 0;
00129 
00130 static short silence[FRAME_SIZE] = { 0, };
00131 
00132 struct sound {
00133    int ind;
00134    short *data;
00135    int datalen;
00136    int samplen;
00137    int silencelen;
00138    int repeat;
00139 };
00140 
00141 static struct sound sounds[] = {
00142    {AST_CONTROL_RINGING, ringtone, sizeof(ringtone) / 2, 16000, 32000, 1},
00143    {AST_CONTROL_BUSY, busy, sizeof(busy) / 2, 4000, 4000, 1},
00144    {AST_CONTROL_CONGESTION, busy, sizeof(busy) / 2, 2000, 2000, 1},
00145    {AST_CONTROL_RING, ring10, sizeof(ring10) / 2, 16000, 32000, 1},
00146    {AST_CONTROL_ANSWER, answer, sizeof(answer) / 2, 2200, 0, 0},
00147 };
00148 
00149 /* Sound command pipe */
00150 static int sndcmd[2];
00151 
00152 static struct chan_alsa_pvt {
00153    /* We only have one ALSA structure -- near sighted perhaps, but it
00154       keeps this driver as simple as possible -- as it should be. */
00155    struct ast_channel *owner;
00156    char exten[AST_MAX_EXTENSION];
00157    char context[AST_MAX_CONTEXT];
00158 #if 0
00159    snd_pcm_t *card;
00160 #endif
00161    snd_pcm_t *icard, *ocard;
00162 
00163 } alsa;
00164 
00165 /* Number of buffers...  Each is FRAMESIZE/8 ms long.  For example
00166    with 160 sample frames, and a buffer size of 3, we have a 60ms buffer, 
00167    usually plenty. */
00168 
00169 pthread_t sthread;
00170 
00171 #define MAX_BUFFER_SIZE 100
00172 
00173 /* File descriptors for sound device */
00174 static int readdev = -1;
00175 static int writedev = -1;
00176 
00177 static int autoanswer = 1;
00178 
00179 static int cursound = -1;
00180 static int sampsent = 0;
00181 static int silencelen = 0;
00182 static int offset = 0;
00183 static int nosound = 0;
00184 
00185 /* ZZ */
00186 static struct ast_channel *alsa_request(const char *type, int format, void *data, int *cause);
00187 static int alsa_digit(struct ast_channel *c, char digit, unsigned int duration);
00188 static int alsa_text(struct ast_channel *c, const char *text);
00189 static int alsa_hangup(struct ast_channel *c);
00190 static int alsa_answer(struct ast_channel *c);
00191 static struct ast_frame *alsa_read(struct ast_channel *chan);
00192 static int alsa_call(struct ast_channel *c, char *dest, int timeout);
00193 static int alsa_write(struct ast_channel *chan, struct ast_frame *f);
00194 static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
00195 static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00196 
00197 static const struct ast_channel_tech alsa_tech = {
00198    .type = "Console",
00199    .description = tdesc,
00200    .capabilities = AST_FORMAT_SLINEAR,
00201    .requester = alsa_request,
00202    .send_digit_end = alsa_digit,
00203    .send_text = alsa_text,
00204    .hangup = alsa_hangup,
00205    .answer = alsa_answer,
00206    .read = alsa_read,
00207    .call = alsa_call,
00208    .write = alsa_write,
00209    .indicate = alsa_indicate,
00210    .fixup = alsa_fixup,
00211 };
00212 
00213 static int send_sound(void)
00214 {
00215    short myframe[FRAME_SIZE];
00216    int total = FRAME_SIZE;
00217    short *frame = NULL;
00218    int amt = 0, res, myoff;
00219    snd_pcm_state_t state;
00220 
00221    if (cursound == -1)
00222       return 0;
00223 
00224    res = total;
00225    if (sampsent < sounds[cursound].samplen) {
00226       myoff = 0;
00227       while (total) {
00228          amt = total;
00229          if (amt > (sounds[cursound].datalen - offset))
00230             amt = sounds[cursound].datalen - offset;
00231          memcpy(myframe + myoff, sounds[cursound].data + offset, amt * 2);
00232          total -= amt;
00233          offset += amt;
00234          sampsent += amt;
00235          myoff += amt;
00236          if (offset >= sounds[cursound].datalen)
00237             offset = 0;
00238       }
00239       /* Set it up for silence */
00240       if (sampsent >= sounds[cursound].samplen)
00241          silencelen = sounds[cursound].silencelen;
00242       frame = myframe;
00243    } else {
00244       if (silencelen > 0) {
00245          frame = silence;
00246          silencelen -= res;
00247       } else {
00248          if (sounds[cursound].repeat) {
00249             /* Start over */
00250             sampsent = 0;
00251             offset = 0;
00252          } else {
00253             cursound = -1;
00254             nosound = 0;
00255          }
00256          return 0;
00257       }
00258    }
00259 
00260    if (res == 0 || !frame)
00261       return 0;
00262 
00263 #ifdef ALSA_MONITOR
00264    alsa_monitor_write((char *) frame, res * 2);
00265 #endif
00266    state = snd_pcm_state(alsa.ocard);
00267    if (state == SND_PCM_STATE_XRUN)
00268       snd_pcm_prepare(alsa.ocard);
00269    res = snd_pcm_writei(alsa.ocard, frame, res);
00270    if (res > 0)
00271       return 0;
00272    return 0;
00273 }
00274 
00275 static void *sound_thread(void *unused)
00276 {
00277    fd_set rfds;
00278    fd_set wfds;
00279    int max, res;
00280 
00281    for (;;) {
00282       FD_ZERO(&rfds);
00283       FD_ZERO(&wfds);
00284       max = sndcmd[0];
00285       FD_SET(sndcmd[0], &rfds);
00286       if (cursound > -1) {
00287          FD_SET(writedev, &wfds);
00288          if (writedev > max)
00289             max = writedev;
00290       }
00291 #ifdef ALSA_MONITOR
00292       if (!alsa.owner) {
00293          FD_SET(readdev, &rfds);
00294          if (readdev > max)
00295             max = readdev;
00296       }
00297 #endif
00298       res = ast_select(max + 1, &rfds, &wfds, NULL, NULL);
00299       if (res < 1) {
00300          ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
00301          continue;
00302       }
00303 #ifdef ALSA_MONITOR
00304       if (FD_ISSET(readdev, &rfds)) {
00305          /* Keep the pipe going with read audio */
00306          snd_pcm_state_t state;
00307          short buf[FRAME_SIZE];
00308          int r;
00309 
00310          state = snd_pcm_state(alsa.ocard);
00311          if (state == SND_PCM_STATE_XRUN) {
00312             snd_pcm_prepare(alsa.ocard);
00313          }
00314          r = snd_pcm_readi(alsa.icard, buf, FRAME_SIZE);
00315          if (r == -EPIPE) {
00316 #if DEBUG
00317             ast_log(LOG_ERROR, "XRUN read\n");
00318 #endif
00319             snd_pcm_prepare(alsa.icard);
00320          } else if (r == -ESTRPIPE) {
00321             ast_log(LOG_ERROR, "-ESTRPIPE\n");
00322             snd_pcm_prepare(alsa.icard);
00323          } else if (r < 0) {
00324             ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
00325          } else
00326             alsa_monitor_read((char *) buf, r * 2);
00327       }
00328 #endif
00329       if (FD_ISSET(sndcmd[0], &rfds)) {
00330          if (read(sndcmd[0], &cursound, sizeof(cursound)) < 0) {
00331             ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
00332          }
00333          silencelen = 0;
00334          offset = 0;
00335          sampsent = 0;
00336       }
00337       if (FD_ISSET(writedev, &wfds))
00338          if (send_sound())
00339             ast_log(LOG_WARNING, "Failed to write sound\n");
00340    }
00341    /* Never reached */
00342    return NULL;
00343 }
00344 
00345 static snd_pcm_t *alsa_card_init(char *dev, snd_pcm_stream_t stream)
00346 {
00347    int err;
00348    int direction;
00349    snd_pcm_t *handle = NULL;
00350    snd_pcm_hw_params_t *hwparams = NULL;
00351    snd_pcm_sw_params_t *swparams = NULL;
00352    struct pollfd pfd;
00353    snd_pcm_uframes_t period_size = PERIOD_FRAMES * 4;
00354    /* int period_bytes = 0; */
00355    snd_pcm_uframes_t buffer_size = 0;
00356 
00357    unsigned int rate = DESIRED_RATE;
00358 #if 0
00359    unsigned int per_min = 1;
00360 #endif
00361    /* unsigned int per_max = 8; */
00362    snd_pcm_uframes_t start_threshold, stop_threshold;
00363 
00364    err = snd_pcm_open(&handle, dev, stream, SND_PCM_NONBLOCK);
00365    if (err < 0) {
00366       ast_log(LOG_ERROR, "snd_pcm_open failed: %s\n", snd_strerror(err));
00367       return NULL;
00368    } else
00369       ast_log(LOG_DEBUG, "Opening device %s in %s mode\n", dev, (stream == SND_PCM_STREAM_CAPTURE) ? "read" : "write");
00370 
00371    hwparams = alloca(snd_pcm_hw_params_sizeof());
00372    memset(hwparams, 0, snd_pcm_hw_params_sizeof());
00373    snd_pcm_hw_params_any(handle, hwparams);
00374 
00375    err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
00376    if (err < 0)
00377       ast_log(LOG_ERROR, "set_access failed: %s\n", snd_strerror(err));
00378 
00379    err = snd_pcm_hw_params_set_format(handle, hwparams, format);
00380    if (err < 0)
00381       ast_log(LOG_ERROR, "set_format failed: %s\n", snd_strerror(err));
00382 
00383    err = snd_pcm_hw_params_set_channels(handle, hwparams, 1);
00384    if (err < 0)
00385       ast_log(LOG_ERROR, "set_channels failed: %s\n", snd_strerror(err));
00386 
00387    direction = 0;
00388    err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &rate, &direction);
00389    if (rate != DESIRED_RATE)
00390       ast_log(LOG_WARNING, "Rate not correct, requested %d, got %d\n", DESIRED_RATE, rate);
00391 
00392    direction = 0;
00393    err = snd_pcm_hw_params_set_period_size_near(handle, hwparams, &period_size, &direction);
00394    if (err < 0)
00395       ast_log(LOG_ERROR, "period_size(%ld frames) is bad: %s\n", period_size, snd_strerror(err));
00396    else
00397       ast_log(LOG_DEBUG, "Period size is %d\n", err);
00398 
00399    buffer_size = 4096 * 2;    /* period_size * 16; */
00400    err = snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &buffer_size);
00401    if (err < 0)
00402       ast_log(LOG_WARNING, "Problem setting buffer size of %ld: %s\n", buffer_size, snd_strerror(err));
00403    else
00404       ast_log(LOG_DEBUG, "Buffer size is set to %d frames\n", err);
00405 
00406 #if 0
00407    direction = 0;
00408    err = snd_pcm_hw_params_set_periods_min(handle, hwparams, &per_min, &direction);
00409    if (err < 0)
00410       ast_log(LOG_ERROR, "periods_min: %s\n", snd_strerror(err));
00411 
00412    err = snd_pcm_hw_params_set_periods_max(handle, hwparams, &per_max, 0);
00413    if (err < 0)
00414       ast_log(LOG_ERROR, "periods_max: %s\n", snd_strerror(err));
00415 #endif
00416 
00417    err = snd_pcm_hw_params(handle, hwparams);
00418    if (err < 0)
00419       ast_log(LOG_ERROR, "Couldn't set the new hw params: %s\n", snd_strerror(err));
00420 
00421    swparams = alloca(snd_pcm_sw_params_sizeof());
00422    memset(swparams, 0, snd_pcm_sw_params_sizeof());
00423    snd_pcm_sw_params_current(handle, swparams);
00424 
00425 #if 1
00426    if (stream == SND_PCM_STREAM_PLAYBACK)
00427       start_threshold = period_size;
00428    else
00429       start_threshold = 1;
00430 
00431    err = snd_pcm_sw_params_set_start_threshold(handle, swparams, start_threshold);
00432    if (err < 0)
00433       ast_log(LOG_ERROR, "start threshold: %s\n", snd_strerror(err));
00434 #endif
00435 
00436 #if 1
00437    if (stream == SND_PCM_STREAM_PLAYBACK)
00438       stop_threshold = buffer_size;
00439    else
00440       stop_threshold = buffer_size;
00441 
00442    err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, stop_threshold);
00443    if (err < 0)
00444       ast_log(LOG_ERROR, "stop threshold: %s\n", snd_strerror(err));
00445 #endif
00446 #if 0
00447    err = snd_pcm_sw_params_set_xfer_align(handle, swparams, PERIOD_FRAMES);
00448    if (err < 0)
00449       ast_log(LOG_ERROR, "Unable to set xfer alignment: %s\n", snd_strerror(err));
00450 #endif
00451 
00452 #if 0
00453    err = snd_pcm_sw_params_set_silence_threshold(handle, swparams, silencethreshold);
00454    if (err < 0)
00455       ast_log(LOG_ERROR, "Unable to set silence threshold: %s\n", snd_strerror(err));
00456 #endif
00457    err = snd_pcm_sw_params(handle, swparams);
00458    if (err < 0)
00459       ast_log(LOG_ERROR, "sw_params: %s\n", snd_strerror(err));
00460 
00461    err = snd_pcm_poll_descriptors_count(handle);
00462    if (err <= 0)
00463       ast_log(LOG_ERROR, "Unable to get a poll descriptors count, error is %s\n", snd_strerror(err));
00464    if (err != 1)
00465       ast_log(LOG_DEBUG, "Can't handle more than one device\n");
00466 
00467    snd_pcm_poll_descriptors(handle, &pfd, err);
00468    ast_log(LOG_DEBUG, "Acquired fd %d from the poll descriptor\n", pfd.fd);
00469 
00470    if (stream == SND_PCM_STREAM_CAPTURE)
00471       readdev = pfd.fd;
00472    else
00473       writedev = pfd.fd;
00474 
00475    return handle;
00476 }
00477 
00478 static int soundcard_init(void)
00479 {
00480    alsa.icard = alsa_card_init(indevname, SND_PCM_STREAM_CAPTURE);
00481    alsa.ocard = alsa_card_init(outdevname, SND_PCM_STREAM_PLAYBACK);
00482 
00483    if (!alsa.icard || !alsa.ocard) {
00484       ast_log(LOG_ERROR, "Problem opening alsa I/O devices\n");
00485       return -1;
00486    }
00487 
00488    return readdev;
00489 }
00490 
00491 static int alsa_digit(struct ast_channel *c, char digit, unsigned int duration)
00492 {
00493    ast_mutex_lock(&alsalock);
00494    ast_verbose(" << Console Received digit %c of duration %u ms >> \n", 
00495       digit, duration);
00496    ast_mutex_unlock(&alsalock);
00497    return 0;
00498 }
00499 
00500 static int alsa_text(struct ast_channel *c, const char *text)
00501 {
00502    ast_mutex_lock(&alsalock);
00503    ast_verbose(" << Console Received text %s >> \n", text);
00504    ast_mutex_unlock(&alsalock);
00505    return 0;
00506 }
00507 
00508 static void grab_owner(void)
00509 {
00510    while (alsa.owner && ast_mutex_trylock(&alsa.owner->lock)) {
00511       DEADLOCK_AVOIDANCE(&alsalock);
00512    }
00513 }
00514 
00515 static int alsa_call(struct ast_channel *c, char *dest, int timeout)
00516 {
00517    int res = 3;
00518    struct ast_frame f = { AST_FRAME_CONTROL };
00519    ast_mutex_lock(&alsalock);
00520    ast_verbose(" << Call placed to '%s' on console >> \n", dest);
00521    if (autoanswer) {
00522       ast_verbose(" << Auto-answered >> \n");
00523       grab_owner();
00524       if (alsa.owner) {
00525          f.subclass = AST_CONTROL_ANSWER;
00526          ast_queue_frame(alsa.owner, &f);
00527          ast_mutex_unlock(&alsa.owner->lock);
00528       }
00529    } else {
00530       ast_verbose(" << Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
00531       grab_owner();
00532       if (alsa.owner) {
00533          f.subclass = AST_CONTROL_RINGING;
00534          ast_queue_frame(alsa.owner, &f);
00535          ast_mutex_unlock(&alsa.owner->lock);
00536       }
00537       if (write(sndcmd[1], &res, sizeof(res)) < 0) {
00538          ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
00539       }
00540    }
00541    snd_pcm_prepare(alsa.icard);
00542    snd_pcm_start(alsa.icard);
00543    ast_mutex_unlock(&alsalock);
00544    return 0;
00545 }
00546 
00547 static void answer_sound(void)
00548 {
00549    int res;
00550 
00551    nosound = 1;
00552    res = 4;
00553    if (write(sndcmd[1], &res, sizeof(res)) < 0) {
00554       ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
00555    }
00556 }
00557 
00558 static int alsa_answer(struct ast_channel *c)
00559 {
00560    ast_mutex_lock(&alsalock);
00561    ast_verbose(" << Console call has been answered >> \n");
00562    answer_sound();
00563    ast_setstate(c, AST_STATE_UP);
00564    cursound = -1;
00565    snd_pcm_prepare(alsa.icard);
00566    snd_pcm_start(alsa.icard);
00567    ast_mutex_unlock(&alsalock);
00568    return 0;
00569 }
00570 
00571 static int alsa_hangup(struct ast_channel *c)
00572 {
00573    int res;
00574    ast_mutex_lock(&alsalock);
00575    cursound = -1;
00576    c->tech_pvt = NULL;
00577    alsa.owner = NULL;
00578    ast_verbose(" << Hangup on console >> \n");
00579    ast_module_unref(ast_module_info->self);
00580    if (hookstate) {
00581       hookstate = 0;
00582       if (!autoanswer) {
00583          /* Congestion noise */
00584          res = 2;
00585          if (write(sndcmd[1], &res, sizeof(res)) < 0) {
00586             ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
00587          }
00588       }
00589    }
00590    snd_pcm_drop(alsa.icard);
00591    ast_mutex_unlock(&alsalock);
00592    return 0;
00593 }
00594 
00595 static int alsa_write(struct ast_channel *chan, struct ast_frame *f)
00596 {
00597    static char sizbuf[8000];
00598    static int sizpos = 0;
00599    int len = sizpos;
00600    int pos;
00601    int res = 0;
00602    /* size_t frames = 0; */
00603    snd_pcm_state_t state;
00604 
00605    /* Immediately return if no sound is enabled */
00606    if (nosound)
00607       return 0;
00608 
00609    ast_mutex_lock(&alsalock);
00610    /* Stop any currently playing sound */
00611    if (cursound != -1) {
00612       snd_pcm_drop(alsa.ocard);
00613       snd_pcm_prepare(alsa.ocard);
00614       cursound = -1;
00615    }
00616 
00617 
00618    /* We have to digest the frame in 160-byte portions */
00619    if (f->datalen > sizeof(sizbuf) - sizpos) {
00620       ast_log(LOG_WARNING, "Frame too large\n");
00621       res = -1;
00622    } else {
00623       memcpy(sizbuf + sizpos, f->data, f->datalen);
00624       len += f->datalen;
00625       pos = 0;
00626 #ifdef ALSA_MONITOR
00627       alsa_monitor_write(sizbuf, len);
00628 #endif
00629       state = snd_pcm_state(alsa.ocard);
00630       if (state == SND_PCM_STATE_XRUN)
00631          snd_pcm_prepare(alsa.ocard);
00632       res = snd_pcm_writei(alsa.ocard, sizbuf, len / 2);
00633       if (res == -EPIPE) {
00634 #if DEBUG
00635          ast_log(LOG_DEBUG, "XRUN write\n");
00636 #endif
00637          snd_pcm_prepare(alsa.ocard);
00638          res = snd_pcm_writei(alsa.ocard, sizbuf, len / 2);
00639          if (res != len / 2) {
00640             ast_log(LOG_ERROR, "Write error: %s\n", snd_strerror(res));
00641             res = -1;
00642          } else if (res < 0) {
00643             ast_log(LOG_ERROR, "Write error %s\n", snd_strerror(res));
00644             res = -1;
00645          }
00646       } else {
00647          if (res == -ESTRPIPE)
00648             ast_log(LOG_ERROR, "You've got some big problems\n");
00649          else if (res < 0)
00650             ast_log(LOG_NOTICE, "Error %d on write\n", res);
00651       }
00652    }
00653    ast_mutex_unlock(&alsalock);
00654    if (res > 0)
00655       res = 0;
00656    return res;
00657 }
00658 
00659 
00660 static struct ast_frame *alsa_read(struct ast_channel *chan)
00661 {
00662    static struct ast_frame f;
00663    static short __buf[FRAME_SIZE + AST_FRIENDLY_OFFSET / 2];
00664    short *buf;
00665    static int readpos = 0;
00666    static int left = FRAME_SIZE;
00667    snd_pcm_state_t state;
00668    int r = 0;
00669    int off = 0;
00670 
00671    ast_mutex_lock(&alsalock);
00672    /* Acknowledge any pending cmd */
00673    f.frametype = AST_FRAME_NULL;
00674    f.subclass = 0;
00675    f.samples = 0;
00676    f.datalen = 0;
00677    f.data = NULL;
00678    f.offset = 0;
00679    f.src = "Console";
00680    f.mallocd = 0;
00681    f.delivery.tv_sec = 0;
00682    f.delivery.tv_usec = 0;
00683 
00684    state = snd_pcm_state(alsa.icard);
00685    if ((state != SND_PCM_STATE_PREPARED) && (state != SND_PCM_STATE_RUNNING)) {
00686       snd_pcm_prepare(alsa.icard);
00687    }
00688 
00689    buf = __buf + AST_FRIENDLY_OFFSET / 2;
00690 
00691    r = snd_pcm_readi(alsa.icard, buf + readpos, left);
00692    if (r == -EPIPE) {
00693 #if DEBUG
00694       ast_log(LOG_ERROR, "XRUN read\n");
00695 #endif
00696       snd_pcm_prepare(alsa.icard);
00697    } else if (r == -ESTRPIPE) {
00698       ast_log(LOG_ERROR, "-ESTRPIPE\n");
00699       snd_pcm_prepare(alsa.icard);
00700    } else if (r < 0) {
00701       ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
00702    } else if (r >= 0) {
00703       off -= r;
00704    }
00705    /* Update positions */
00706    readpos += r;
00707    left -= r;
00708 
00709    if (readpos >= FRAME_SIZE) {
00710       /* A real frame */
00711       readpos = 0;
00712       left = FRAME_SIZE;
00713       if (chan->_state != AST_STATE_UP) {
00714          /* Don't transmit unless it's up */
00715          ast_mutex_unlock(&alsalock);
00716          return &f;
00717       }
00718       f.frametype = AST_FRAME_VOICE;
00719       f.subclass = AST_FORMAT_SLINEAR;
00720       f.samples = FRAME_SIZE;
00721       f.datalen = FRAME_SIZE * 2;
00722       f.data = buf;
00723       f.offset = AST_FRIENDLY_OFFSET;
00724       f.src = "Console";
00725       f.mallocd = 0;
00726 #ifdef ALSA_MONITOR
00727       alsa_monitor_read((char *) buf, FRAME_SIZE * 2);
00728 #endif
00729 
00730    }
00731    ast_mutex_unlock(&alsalock);
00732    return &f;
00733 }
00734 
00735 static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
00736 {
00737    struct chan_alsa_pvt *p = newchan->tech_pvt;
00738    ast_mutex_lock(&alsalock);
00739    p->owner = newchan;
00740    ast_mutex_unlock(&alsalock);
00741    return 0;
00742 }
00743 
00744 static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen)
00745 {
00746    int res = 0;
00747 
00748    ast_mutex_lock(&alsalock);
00749 
00750    switch (cond) {
00751    case AST_CONTROL_BUSY:
00752       res = 1;
00753       break;
00754    case AST_CONTROL_CONGESTION:
00755       res = 2;
00756       break;
00757    case AST_CONTROL_RINGING:
00758    case AST_CONTROL_PROGRESS:
00759       break;
00760    case -1:
00761       res = -1;
00762       break;
00763    case AST_CONTROL_VIDUPDATE:
00764       res = -1;
00765       break;
00766    case AST_CONTROL_HOLD:
00767       ast_verbose(" << Console Has Been Placed on Hold >> \n");
00768       ast_moh_start(chan, data, mohinterpret);
00769       break;
00770    case AST_CONTROL_UNHOLD:
00771       ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
00772       ast_moh_stop(chan);
00773       break;
00774    case AST_CONTROL_SRCUPDATE:
00775       break;
00776    default:
00777       ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, chan->name);
00778       res = -1;
00779    }
00780 
00781    if (res > -1) {
00782       if (write(sndcmd[1], &res, sizeof(res)) < 0) {
00783          ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
00784       }
00785    }
00786 
00787    ast_mutex_unlock(&alsalock);
00788 
00789    return res;
00790 }
00791 
00792 static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state)
00793 {
00794    struct ast_channel *tmp = NULL;
00795    if (!(tmp = ast_channel_alloc(1, state, 0, 0, "", p->exten, p->context, 0, "ALSA/%s", indevname)))
00796       return NULL;
00797 
00798    tmp->tech = &alsa_tech;
00799    tmp->fds[0] = readdev;
00800    tmp->nativeformats = AST_FORMAT_SLINEAR;
00801    tmp->readformat = AST_FORMAT_SLINEAR;
00802    tmp->writeformat = AST_FORMAT_SLINEAR;
00803    tmp->tech_pvt = p;
00804    if (!ast_strlen_zero(p->context))
00805       ast_copy_string(tmp->context, p->context, sizeof(tmp->context));
00806    if (!ast_strlen_zero(p->exten))
00807       ast_copy_string(tmp->exten, p->exten, sizeof(tmp->exten));
00808    if (!ast_strlen_zero(language))
00809       ast_string_field_set(tmp, language, language);
00810    p->owner = tmp;
00811    ast_module_ref(ast_module_info->self);
00812    ast_jb_configure(tmp, &global_jbconf);
00813    if (state != AST_STATE_DOWN) {
00814       if (ast_pbx_start(tmp)) {
00815          ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
00816          ast_hangup(tmp);
00817          tmp = NULL;
00818       }
00819    }
00820 
00821    return tmp;
00822 }
00823 
00824 static struct ast_channel *alsa_request(const char *type, int format, void *data, int *cause)
00825 {
00826    int oldformat = format;
00827    struct ast_channel *tmp = NULL;
00828 
00829    format &= AST_FORMAT_SLINEAR;
00830    if (!format) {
00831       ast_log(LOG_NOTICE, "Asked to get a channel of format '%d'\n", oldformat);
00832       return NULL;
00833    }
00834 
00835    ast_mutex_lock(&alsalock);
00836 
00837    if (alsa.owner) {
00838       ast_log(LOG_NOTICE, "Already have a call on the ALSA channel\n");
00839       *cause = AST_CAUSE_BUSY;
00840    } else if (!(tmp = alsa_new(&alsa, AST_STATE_DOWN)))
00841       ast_log(LOG_WARNING, "Unable to create new ALSA channel\n");
00842 
00843    ast_mutex_unlock(&alsalock);
00844 
00845    return tmp;
00846 }
00847 
00848 static int console_autoanswer_deprecated(int fd, int argc, char *argv[])
00849 {
00850    int res = RESULT_SUCCESS;
00851 
00852    if ((argc != 1) && (argc != 2))
00853       return RESULT_SHOWUSAGE;
00854 
00855    ast_mutex_lock(&alsalock);
00856 
00857    if (argc == 1) {
00858       ast_cli(fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
00859    } else {
00860       if (!strcasecmp(argv[1], "on"))
00861          autoanswer = -1;
00862       else if (!strcasecmp(argv[1], "off"))
00863          autoanswer = 0;
00864       else
00865          res = RESULT_SHOWUSAGE;
00866    }
00867 
00868    ast_mutex_unlock(&alsalock);
00869 
00870    return res;
00871 }
00872 
00873 static int console_autoanswer(int fd, int argc, char *argv[])
00874 {
00875    int res = RESULT_SUCCESS;;
00876    if ((argc != 2) && (argc != 3))
00877       return RESULT_SHOWUSAGE;
00878    ast_mutex_lock(&alsalock);
00879    if (argc == 2) {
00880       ast_cli(fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
00881    } else {
00882       if (!strcasecmp(argv[2], "on"))
00883          autoanswer = -1;
00884       else if (!strcasecmp(argv[2], "off"))
00885          autoanswer = 0;
00886       else
00887          res = RESULT_SHOWUSAGE;
00888    }
00889    ast_mutex_unlock(&alsalock);
00890    return res;
00891 }
00892 
00893 static char *autoanswer_complete(const char *line, const char *word, int pos, int state)
00894 {
00895 #ifndef MIN
00896 #define MIN(a,b) ((a) < (b) ? (a) : (b))
00897 #endif
00898    switch (state) {
00899       case 0:
00900          if (!ast_strlen_zero(word) && !strncasecmp(word, "on", MIN(strlen(word), 2)))
00901             return ast_strdup("on");
00902       case 1:
00903          if (!ast_strlen_zero(word) && !strncasecmp(word, "off", MIN(strlen(word), 3)))
00904             return ast_strdup("off");
00905       default:
00906          return NULL;
00907    }
00908    return NULL;
00909 }
00910 
00911 static const char autoanswer_usage[] =
00912    "Usage: console autoanswer [on|off]\n"
00913    "       Enables or disables autoanswer feature.  If used without\n"
00914    "       argument, displays the current on/off status of autoanswer.\n"
00915    "       The default value of autoanswer is in 'alsa.conf'.\n";
00916 
00917 static int console_answer_deprecated(int fd, int argc, char *argv[])
00918 {
00919    int res = RESULT_SUCCESS;
00920 
00921    if (argc != 1)
00922       return RESULT_SHOWUSAGE;
00923 
00924    ast_mutex_lock(&alsalock);
00925 
00926    if (!alsa.owner) {
00927       ast_cli(fd, "No one is calling us\n");
00928       res = RESULT_FAILURE;
00929    } else {
00930       hookstate = 1;
00931       cursound = -1;
00932       grab_owner();
00933       if (alsa.owner) {
00934          struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
00935          ast_queue_frame(alsa.owner, &f);
00936          ast_mutex_unlock(&alsa.owner->lock);
00937       }
00938       answer_sound();
00939    }
00940 
00941    snd_pcm_prepare(alsa.icard);
00942    snd_pcm_start(alsa.icard);
00943 
00944    ast_mutex_unlock(&alsalock);
00945 
00946    return RESULT_SUCCESS;
00947 }
00948 
00949 static int console_answer(int fd, int argc, char *argv[])
00950 {
00951    int res = RESULT_SUCCESS;
00952 
00953    if (argc != 2)
00954       return RESULT_SHOWUSAGE;
00955 
00956    ast_mutex_lock(&alsalock);
00957 
00958    if (!alsa.owner) {
00959       ast_cli(fd, "No one is calling us\n");
00960       res = RESULT_FAILURE;
00961    } else {
00962       hookstate = 1;
00963       cursound = -1;
00964       grab_owner();
00965       if (alsa.owner) {
00966          struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
00967          ast_queue_frame(alsa.owner, &f);
00968          ast_mutex_unlock(&alsa.owner->lock);
00969       }
00970       answer_sound();
00971    }
00972 
00973    snd_pcm_prepare(alsa.icard);
00974    snd_pcm_start(alsa.icard);
00975 
00976    ast_mutex_unlock(&alsalock);
00977 
00978    return RESULT_SUCCESS;
00979 }
00980 
00981 static char sendtext_usage[] =
00982    "Usage: console send text <message>\n"
00983    "       Sends a text message for display on the remote terminal.\n";
00984 
00985 static int console_sendtext_deprecated(int fd, int argc, char *argv[])
00986 {
00987    int tmparg = 2;
00988    int res = RESULT_SUCCESS;
00989 
00990    if (argc < 2)
00991       return RESULT_SHOWUSAGE;
00992 
00993    ast_mutex_lock(&alsalock);
00994 
00995    if (!alsa.owner) {
00996       ast_cli(fd, "No one is calling us\n");
00997       res = RESULT_FAILURE;
00998    } else {
00999       struct ast_frame f = { AST_FRAME_TEXT, 0 };
01000       char text2send[256] = "";
01001       text2send[0] = '\0';
01002       while (tmparg < argc) {
01003          strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send) - 1);
01004          strncat(text2send, " ", sizeof(text2send) - strlen(text2send) - 1);
01005       }
01006       text2send[strlen(text2send) - 1] = '\n';
01007       f.data = text2send;
01008       f.datalen = strlen(text2send) + 1;
01009       grab_owner();
01010       if (alsa.owner) {
01011          ast_queue_frame(alsa.owner, &f);
01012          f.frametype = AST_FRAME_CONTROL;
01013          f.subclass = AST_CONTROL_ANSWER;
01014          f.data = NULL;
01015          f.datalen = 0;
01016          ast_queue_frame(alsa.owner, &f);
01017          ast_mutex_unlock(&alsa.owner->lock);
01018       }
01019    }
01020 
01021    ast_mutex_unlock(&alsalock);
01022 
01023    return res;
01024 }
01025 
01026 static int console_sendtext(int fd, int argc, char *argv[])
01027 {
01028    int tmparg = 3;
01029    int res = RESULT_SUCCESS;
01030 
01031    if (argc < 3)
01032       return RESULT_SHOWUSAGE;
01033 
01034    ast_mutex_lock(&alsalock);
01035 
01036    if (!alsa.owner) {
01037       ast_cli(fd, "No one is calling us\n");
01038       res = RESULT_FAILURE;
01039    } else {
01040       struct ast_frame f = { AST_FRAME_TEXT, 0 };
01041       char text2send[256] = "";
01042       text2send[0] = '\0';
01043       while (tmparg < argc) {
01044          strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send) - 1);
01045          strncat(text2send, " ", sizeof(text2send) - strlen(text2send) - 1);
01046       }
01047       text2send[strlen(text2send) - 1] = '\n';
01048       f.data = text2send;
01049       f.datalen = strlen(text2send) + 1;
01050       grab_owner();
01051       if (alsa.owner) {
01052          ast_queue_frame(alsa.owner, &f);
01053          f.frametype = AST_FRAME_CONTROL;
01054          f.subclass = AST_CONTROL_ANSWER;
01055          f.data = NULL;
01056          f.datalen = 0;
01057          ast_queue_frame(alsa.owner, &f);
01058          ast_mutex_unlock(&alsa.owner->lock);
01059       }
01060    }
01061 
01062    ast_mutex_unlock(&alsalock);
01063 
01064    return res;
01065 }
01066 
01067 static char answer_usage[] =
01068    "Usage: console answer\n"
01069    "       Answers an incoming call on the console (ALSA) channel.\n";
01070 
01071 static int console_hangup_deprecated(int fd, int argc, char *argv[])
01072 {
01073    int res = RESULT_SUCCESS;
01074 
01075    if (argc != 1)
01076       return RESULT_SHOWUSAGE;
01077 
01078    cursound = -1;
01079 
01080    ast_mutex_lock(&alsalock);
01081 
01082    if (!alsa.owner && !hookstate) {
01083       ast_cli(fd, "No call to hangup up\n");
01084       res = RESULT_FAILURE;
01085    } else {
01086       hookstate = 0;
01087       grab_owner();
01088       if (alsa.owner) {
01089          ast_queue_hangup(alsa.owner);
01090          ast_mutex_unlock(&alsa.owner->lock);
01091       }
01092    }
01093 
01094    ast_mutex_unlock(&alsalock);
01095 
01096    return res;
01097 }
01098 
01099 static int console_hangup(int fd, int argc, char *argv[])
01100 {
01101    int res = RESULT_SUCCESS;
01102 
01103    if (argc != 2)
01104       return RESULT_SHOWUSAGE;
01105 
01106    cursound = -1;
01107 
01108    ast_mutex_lock(&alsalock);
01109 
01110    if (!alsa.owner && !hookstate) {
01111       ast_cli(fd, "No call to hangup up\n");
01112       res = RESULT_FAILURE;
01113    } else {
01114       hookstate = 0;
01115       grab_owner();
01116       if (alsa.owner) {
01117          ast_queue_hangup(alsa.owner);
01118          ast_mutex_unlock(&alsa.owner->lock);
01119       }
01120    }
01121 
01122    ast_mutex_unlock(&alsalock);
01123 
01124    return res;
01125 }
01126 
01127 static char hangup_usage[] =
01128    "Usage: console hangup\n"
01129    "       Hangs up any call currently placed on the console.\n";
01130 
01131 static int console_dial_deprecated(int fd, int argc, char *argv[])
01132 {
01133    char tmp[256], *tmp2;
01134    char *mye, *myc;
01135    char *d;
01136    int res = RESULT_SUCCESS;
01137 
01138    if ((argc != 1) && (argc != 2))
01139       return RESULT_SHOWUSAGE;
01140 
01141    ast_mutex_lock(&alsalock);
01142 
01143    if (alsa.owner) {
01144       if (argc == 2) {
01145          d = argv[1];
01146          grab_owner();
01147          if (alsa.owner) {
01148             struct ast_frame f = { AST_FRAME_DTMF };
01149             while (*d) {
01150                f.subclass = *d;
01151                ast_queue_frame(alsa.owner, &f);
01152                d++;
01153             }
01154             ast_mutex_unlock(&alsa.owner->lock);
01155          }
01156       } else {
01157          ast_cli(fd, "You're already in a call.  You can use this only to dial digits until you hangup\n");
01158          res = RESULT_FAILURE;
01159       }
01160    } else {
01161       mye = exten;
01162       myc = context;
01163       if (argc == 2) {
01164          char *stringp = NULL;
01165          ast_copy_string(tmp, argv[1], sizeof(tmp));
01166          stringp = tmp;
01167          strsep(&stringp, "@");
01168          tmp2 = strsep(&stringp, "@");
01169          if (!ast_strlen_zero(tmp))
01170             mye = tmp;
01171          if (!ast_strlen_zero(tmp2))
01172             myc = tmp2;
01173       }
01174       if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
01175          ast_copy_string(alsa.exten, mye, sizeof(alsa.exten));
01176          ast_copy_string(alsa.context, myc, sizeof(alsa.context));
01177          hookstate = 1;
01178          alsa_new(&alsa, AST_STATE_RINGING);
01179       } else
01180          ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
01181    }
01182 
01183    ast_mutex_unlock(&alsalock);
01184 
01185    return res;
01186 }
01187 
01188 static int console_dial(int fd, int argc, char *argv[])
01189 {
01190    char tmp[256], *tmp2;
01191    char *mye, *myc;
01192    char *d;
01193    int res = RESULT_SUCCESS;
01194 
01195    if ((argc != 2) && (argc != 3))
01196       return RESULT_SHOWUSAGE;
01197 
01198    ast_mutex_lock(&alsalock);
01199 
01200    if (alsa.owner) {
01201       if (argc == 3) {
01202          d = argv[2];
01203          grab_owner();
01204          if (alsa.owner) {
01205             struct ast_frame f = { AST_FRAME_DTMF };
01206             while (*d) {
01207                f.subclass = *d;
01208                ast_queue_frame(alsa.owner, &f);
01209                d++;
01210             }
01211             ast_mutex_unlock(&alsa.owner->lock);
01212          }
01213       } else {
01214          ast_cli(fd, "You're already in a call.  You can use this only to dial digits until you hangup\n");
01215          res = RESULT_FAILURE;
01216       }
01217    } else {
01218       mye = exten;
01219       myc = context;
01220       if (argc == 3) {
01221          char *stringp = NULL;
01222          ast_copy_string(tmp, argv[2], sizeof(tmp));
01223          stringp = tmp;
01224          strsep(&stringp, "@");
01225          tmp2 = strsep(&stringp, "@");
01226          if (!ast_strlen_zero(tmp))
01227             mye = tmp;
01228          if (!ast_strlen_zero(tmp2))
01229             myc = tmp2;
01230       }
01231       if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
01232          ast_copy_string(alsa.exten, mye, sizeof(alsa.exten));
01233          ast_copy_string(alsa.context, myc, sizeof(alsa.context));
01234          hookstate = 1;
01235          alsa_new(&alsa, AST_STATE_RINGING);
01236       } else
01237          ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
01238    }
01239 
01240    ast_mutex_unlock(&alsalock);
01241 
01242    return res;
01243 }
01244 
01245 static char dial_usage[] =
01246    "Usage: console dial [extension[@context]]\n"
01247    "       Dials a given extension (and context if specified)\n";
01248 
01249 static struct ast_cli_entry cli_alsa_answer_deprecated = {
01250    { "answer", NULL },
01251    console_answer_deprecated, NULL,
01252    NULL };
01253 
01254 static struct ast_cli_entry cli_alsa_hangup_deprecated = {
01255    { "hangup", NULL },
01256    console_hangup_deprecated, NULL,
01257    NULL };
01258 
01259 static struct ast_cli_entry cli_alsa_dial_deprecated = {
01260    { "dial", NULL },
01261    console_dial_deprecated, NULL,
01262    NULL };
01263 
01264 static struct ast_cli_entry cli_alsa_send_text_deprecated = {
01265    { "send", "text", NULL },
01266    console_sendtext_deprecated, NULL,
01267    NULL };
01268 
01269 static struct ast_cli_entry cli_alsa_autoanswer_deprecated = {
01270    { "autoanswer", NULL },
01271    console_autoanswer_deprecated, NULL,
01272    NULL, autoanswer_complete };
01273 
01274 static struct ast_cli_entry cli_alsa[] = {
01275    { { "console", "answer", NULL },
01276    console_answer, "Answer an incoming console call",
01277    answer_usage, NULL, &cli_alsa_answer_deprecated },
01278 
01279    { { "console", "hangup", NULL },
01280    console_hangup, "Hangup a call on the console",
01281    hangup_usage, NULL, &cli_alsa_hangup_deprecated },
01282 
01283    { { "console", "dial", NULL },
01284    console_dial, "Dial an extension on the console",
01285    dial_usage, NULL, &cli_alsa_dial_deprecated },
01286 
01287    { { "console", "send", "text", NULL },
01288    console_sendtext, "Send text to the remote device",
01289    sendtext_usage, NULL, &cli_alsa_send_text_deprecated },
01290 
01291    { { "console", "autoanswer", NULL },
01292    console_autoanswer, "Sets/displays autoanswer",
01293    autoanswer_usage, autoanswer_complete, &cli_alsa_autoanswer_deprecated },
01294 };
01295 
01296 static int load_module(void)
01297 {
01298    int res;
01299    struct ast_config *cfg;
01300    struct ast_variable *v;
01301 
01302    /* Copy the default jb config over global_jbconf */
01303    memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
01304 
01305    strcpy(mohinterpret, "default");
01306    if (!(cfg = ast_config_load(config)))
01307       return AST_MODULE_LOAD_DECLINE;
01308 
01309    v = ast_variable_browse(cfg, "general");
01310    for (; v; v = v->next) {
01311       /* handle jb conf */
01312       if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
01313          continue;
01314 
01315       if (!strcasecmp(v->name, "autoanswer"))
01316          autoanswer = ast_true(v->value);
01317       else if (!strcasecmp(v->name, "silencesuppression"))
01318          silencesuppression = ast_true(v->value);
01319       else if (!strcasecmp(v->name, "silencethreshold"))
01320          silencethreshold = atoi(v->value);
01321       else if (!strcasecmp(v->name, "context"))
01322          ast_copy_string(context, v->value, sizeof(context));
01323       else if (!strcasecmp(v->name, "language"))
01324          ast_copy_string(language, v->value, sizeof(language));
01325       else if (!strcasecmp(v->name, "extension"))
01326          ast_copy_string(exten, v->value, sizeof(exten));
01327       else if (!strcasecmp(v->name, "input_device"))
01328          ast_copy_string(indevname, v->value, sizeof(indevname));
01329       else if (!strcasecmp(v->name, "output_device"))
01330          ast_copy_string(outdevname, v->value, sizeof(outdevname));
01331       else if (!strcasecmp(v->name, "mohinterpret"))
01332          ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
01333    }
01334 
01335    ast_config_destroy(cfg);
01336    res = pipe(sndcmd);
01337    if (res) {
01338       ast_log(LOG_ERROR, "Unable to create pipe\n");
01339       return -1;
01340    }
01341    res = soundcard_init();
01342    if (res < 0) {
01343       if (option_verbose > 1) {
01344          ast_verbose(VERBOSE_PREFIX_2 "No sound card detected -- console channel will be unavailable\n");
01345          ast_verbose(VERBOSE_PREFIX_2 "Turn off ALSA support by adding 'noload=chan_alsa.so' in /etc/asterisk/modules.conf\n");
01346       }
01347       return AST_MODULE_LOAD_DECLINE;
01348    }
01349 
01350    res = ast_channel_register(&alsa_tech);
01351    if (res < 0) {
01352       ast_log(LOG_ERROR, "Unable to register channel class 'Console'\n");
01353       return -1;
01354    }
01355    ast_cli_register_multiple(cli_alsa, sizeof(cli_alsa) / sizeof(struct ast_cli_entry));
01356 
01357    ast_pthread_create_background(&sthread, NULL, sound_thread, NULL);
01358 #ifdef ALSA_MONITOR
01359    if (alsa_monitor_start())
01360       ast_log(LOG_ERROR, "Problem starting Monitoring\n");
01361 #endif
01362    return 0;
01363 }
01364 
01365 static int unload_module(void)
01366 {
01367    ast_channel_unregister(&alsa_tech);
01368    ast_cli_unregister_multiple(cli_alsa, sizeof(cli_alsa) / sizeof(struct ast_cli_entry));
01369 
01370    if (alsa.icard)
01371       snd_pcm_close(alsa.icard);
01372    if (alsa.ocard)
01373       snd_pcm_close(alsa.ocard);
01374    if (sndcmd[0] > 0) {
01375       close(sndcmd[0]);
01376       close(sndcmd[1]);
01377    }
01378    if (alsa.owner)
01379       ast_softhangup(alsa.owner, AST_SOFTHANGUP_APPUNLOAD);
01380    if (alsa.owner)
01381       return -1;
01382    return 0;
01383 }
01384 
01385 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ALSA Console Channel Driver");

Generated on Fri Jul 3 20:35:34 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9