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  * \ingroup channel_drivers
00025  */
00026 
00027 /*! \li \ref chan_alsa.c uses the configuration file \ref alsa.conf
00028  * \addtogroup configuration_file
00029  */
00030 
00031 /*! \page alsa.conf alsa.conf
00032  * \verbinclude alsa.conf.sample
00033  */
00034 
00035 /*** MODULEINFO
00036    <depend>alsa</depend>
00037    <support_level>extended</support_level>
00038  ***/
00039 
00040 #include "asterisk.h"
00041 
00042 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419592 $")
00043 
00044 #include <fcntl.h>
00045 #include <sys/ioctl.h>
00046 #include <sys/time.h>
00047 
00048 #define ALSA_PCM_NEW_HW_PARAMS_API
00049 #define ALSA_PCM_NEW_SW_PARAMS_API
00050 #include <alsa/asoundlib.h>
00051 
00052 #include "asterisk/frame.h"
00053 #include "asterisk/channel.h"
00054 #include "asterisk/module.h"
00055 #include "asterisk/pbx.h"
00056 #include "asterisk/config.h"
00057 #include "asterisk/cli.h"
00058 #include "asterisk/utils.h"
00059 #include "asterisk/causes.h"
00060 #include "asterisk/endian.h"
00061 #include "asterisk/stringfields.h"
00062 #include "asterisk/abstract_jb.h"
00063 #include "asterisk/musiconhold.h"
00064 #include "asterisk/poll-compat.h"
00065 #include "asterisk/stasis_channels.h"
00066 #include "asterisk/format_cache.h"
00067 
00068 /*! Global jitterbuffer configuration - by default, jb is disabled
00069  *  \note Values shown here match the defaults shown in alsa.conf.sample */
00070 static struct ast_jb_conf default_jbconf = {
00071    .flags = 0,
00072    .max_size = 200,
00073    .resync_threshold = 1000,
00074    .impl = "fixed",
00075    .target_extra = 40,
00076 };
00077 static struct ast_jb_conf global_jbconf;
00078 
00079 #define DEBUG 0
00080 /* Which device to use */
00081 #define ALSA_INDEV "default"
00082 #define ALSA_OUTDEV "default"
00083 #define DESIRED_RATE 8000
00084 
00085 /* Lets use 160 sample frames, just like GSM.  */
00086 #define FRAME_SIZE 160
00087 #define PERIOD_FRAMES 80      /* 80 Frames, at 2 bytes each */
00088 
00089 /* When you set the frame size, you have to come up with
00090    the right buffer format as well. */
00091 /* 5 64-byte frames = one frame */
00092 #define BUFFER_FMT ((buffersize * 10) << 16) | (0x0006);
00093 
00094 /* Don't switch between read/write modes faster than every 300 ms */
00095 #define MIN_SWITCH_TIME 600
00096 
00097 #if __BYTE_ORDER == __LITTLE_ENDIAN
00098 static snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
00099 #else
00100 static snd_pcm_format_t format = SND_PCM_FORMAT_S16_BE;
00101 #endif
00102 
00103 static char indevname[50] = ALSA_INDEV;
00104 static char outdevname[50] = ALSA_OUTDEV;
00105 
00106 static int silencesuppression = 0;
00107 static int silencethreshold = 1000;
00108 
00109 AST_MUTEX_DEFINE_STATIC(alsalock);
00110 
00111 static const char tdesc[] = "ALSA Console Channel Driver";
00112 static const char config[] = "alsa.conf";
00113 
00114 static char context[AST_MAX_CONTEXT] = "default";
00115 static char language[MAX_LANGUAGE] = "";
00116 static char exten[AST_MAX_EXTENSION] = "s";
00117 static char mohinterpret[MAX_MUSICCLASS];
00118 
00119 static int hookstate = 0;
00120 
00121 static struct chan_alsa_pvt {
00122    /* We only have one ALSA structure -- near sighted perhaps, but it
00123       keeps this driver as simple as possible -- as it should be. */
00124    struct ast_channel *owner;
00125    char exten[AST_MAX_EXTENSION];
00126    char context[AST_MAX_CONTEXT];
00127    snd_pcm_t *icard, *ocard;
00128 
00129 } alsa;
00130 
00131 /* Number of buffers...  Each is FRAMESIZE/8 ms long.  For example
00132    with 160 sample frames, and a buffer size of 3, we have a 60ms buffer, 
00133    usually plenty. */
00134 
00135 #define MAX_BUFFER_SIZE 100
00136 
00137 /* File descriptors for sound device */
00138 static int readdev = -1;
00139 static int writedev = -1;
00140 
00141 static int autoanswer = 1;
00142 static int mute = 0;
00143 static int noaudiocapture = 0;
00144 
00145 static struct ast_channel *alsa_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
00146 static int alsa_digit(struct ast_channel *c, char digit, unsigned int duration);
00147 static int alsa_text(struct ast_channel *c, const char *text);
00148 static int alsa_hangup(struct ast_channel *c);
00149 static int alsa_answer(struct ast_channel *c);
00150 static struct ast_frame *alsa_read(struct ast_channel *chan);
00151 static int alsa_call(struct ast_channel *c, const char *dest, int timeout);
00152 static int alsa_write(struct ast_channel *chan, struct ast_frame *f);
00153 static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
00154 static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00155 
00156 static struct ast_channel_tech alsa_tech = {
00157    .type = "Console",
00158    .description = tdesc,
00159    .requester = alsa_request,
00160    .send_digit_end = alsa_digit,
00161    .send_text = alsa_text,
00162    .hangup = alsa_hangup,
00163    .answer = alsa_answer,
00164    .read = alsa_read,
00165    .call = alsa_call,
00166    .write = alsa_write,
00167    .indicate = alsa_indicate,
00168    .fixup = alsa_fixup,
00169 };
00170 
00171 static snd_pcm_t *alsa_card_init(char *dev, snd_pcm_stream_t stream)
00172 {
00173    int err;
00174    int direction;
00175    snd_pcm_t *handle = NULL;
00176    snd_pcm_hw_params_t *hwparams = NULL;
00177    snd_pcm_sw_params_t *swparams = NULL;
00178    struct pollfd pfd;
00179    snd_pcm_uframes_t period_size = PERIOD_FRAMES * 4;
00180    snd_pcm_uframes_t buffer_size = 0;
00181    unsigned int rate = DESIRED_RATE;
00182    snd_pcm_uframes_t start_threshold, stop_threshold;
00183 
00184    err = snd_pcm_open(&handle, dev, stream, SND_PCM_NONBLOCK);
00185    if (err < 0) {
00186       ast_log(LOG_ERROR, "snd_pcm_open failed: %s\n", snd_strerror(err));
00187       return NULL;
00188    } else {
00189       ast_debug(1, "Opening device %s in %s mode\n", dev, (stream == SND_PCM_STREAM_CAPTURE) ? "read" : "write");
00190    }
00191 
00192    hwparams = ast_alloca(snd_pcm_hw_params_sizeof());
00193    memset(hwparams, 0, snd_pcm_hw_params_sizeof());
00194    snd_pcm_hw_params_any(handle, hwparams);
00195 
00196    err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
00197    if (err < 0)
00198       ast_log(LOG_ERROR, "set_access failed: %s\n", snd_strerror(err));
00199 
00200    err = snd_pcm_hw_params_set_format(handle, hwparams, format);
00201    if (err < 0)
00202       ast_log(LOG_ERROR, "set_format failed: %s\n", snd_strerror(err));
00203 
00204    err = snd_pcm_hw_params_set_channels(handle, hwparams, 1);
00205    if (err < 0)
00206       ast_log(LOG_ERROR, "set_channels failed: %s\n", snd_strerror(err));
00207 
00208    direction = 0;
00209    err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &rate, &direction);
00210    if (rate != DESIRED_RATE)
00211       ast_log(LOG_WARNING, "Rate not correct, requested %d, got %u\n", DESIRED_RATE, rate);
00212 
00213    direction = 0;
00214    err = snd_pcm_hw_params_set_period_size_near(handle, hwparams, &period_size, &direction);
00215    if (err < 0)
00216       ast_log(LOG_ERROR, "period_size(%lu frames) is bad: %s\n", period_size, snd_strerror(err));
00217    else {
00218       ast_debug(1, "Period size is %d\n", err);
00219    }
00220 
00221    buffer_size = 4096 * 2;    /* period_size * 16; */
00222    err = snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &buffer_size);
00223    if (err < 0)
00224       ast_log(LOG_WARNING, "Problem setting buffer size of %lu: %s\n", buffer_size, snd_strerror(err));
00225    else {
00226       ast_debug(1, "Buffer size is set to %d frames\n", err);
00227    }
00228 
00229    err = snd_pcm_hw_params(handle, hwparams);
00230    if (err < 0)
00231       ast_log(LOG_ERROR, "Couldn't set the new hw params: %s\n", snd_strerror(err));
00232 
00233    swparams = ast_alloca(snd_pcm_sw_params_sizeof());
00234    memset(swparams, 0, snd_pcm_sw_params_sizeof());
00235    snd_pcm_sw_params_current(handle, swparams);
00236 
00237    if (stream == SND_PCM_STREAM_PLAYBACK)
00238       start_threshold = period_size;
00239    else
00240       start_threshold = 1;
00241 
00242    err = snd_pcm_sw_params_set_start_threshold(handle, swparams, start_threshold);
00243    if (err < 0)
00244       ast_log(LOG_ERROR, "start threshold: %s\n", snd_strerror(err));
00245 
00246    if (stream == SND_PCM_STREAM_PLAYBACK)
00247       stop_threshold = buffer_size;
00248    else
00249       stop_threshold = buffer_size;
00250 
00251    err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, stop_threshold);
00252    if (err < 0)
00253       ast_log(LOG_ERROR, "stop threshold: %s\n", snd_strerror(err));
00254 
00255    err = snd_pcm_sw_params(handle, swparams);
00256    if (err < 0)
00257       ast_log(LOG_ERROR, "sw_params: %s\n", snd_strerror(err));
00258 
00259    err = snd_pcm_poll_descriptors_count(handle);
00260    if (err <= 0)
00261       ast_log(LOG_ERROR, "Unable to get a poll descriptors count, error is %s\n", snd_strerror(err));
00262    if (err != 1) {
00263       ast_debug(1, "Can't handle more than one device\n");
00264    }
00265 
00266    snd_pcm_poll_descriptors(handle, &pfd, err);
00267    ast_debug(1, "Acquired fd %d from the poll descriptor\n", pfd.fd);
00268 
00269    if (stream == SND_PCM_STREAM_CAPTURE)
00270       readdev = pfd.fd;
00271    else
00272       writedev = pfd.fd;
00273 
00274    return handle;
00275 }
00276 
00277 static int soundcard_init(void)
00278 {
00279    if (!noaudiocapture) {
00280       alsa.icard = alsa_card_init(indevname, SND_PCM_STREAM_CAPTURE);
00281       if (!alsa.icard) {
00282          ast_log(LOG_ERROR, "Problem opening alsa capture device\n");
00283          return -1;
00284       }
00285    }
00286 
00287    alsa.ocard = alsa_card_init(outdevname, SND_PCM_STREAM_PLAYBACK);
00288 
00289    if (!alsa.ocard) {
00290       ast_log(LOG_ERROR, "Problem opening ALSA playback device\n");
00291       return -1;
00292    }
00293 
00294    return writedev;
00295 }
00296 
00297 static int alsa_digit(struct ast_channel *c, char digit, unsigned int duration)
00298 {
00299    ast_mutex_lock(&alsalock);
00300    ast_verbose(" << Console Received digit %c of duration %u ms >> \n", 
00301       digit, duration);
00302    ast_mutex_unlock(&alsalock);
00303 
00304    return 0;
00305 }
00306 
00307 static int alsa_text(struct ast_channel *c, const char *text)
00308 {
00309    ast_mutex_lock(&alsalock);
00310    ast_verbose(" << Console Received text %s >> \n", text);
00311    ast_mutex_unlock(&alsalock);
00312 
00313    return 0;
00314 }
00315 
00316 static void grab_owner(void)
00317 {
00318    while (alsa.owner && ast_channel_trylock(alsa.owner)) {
00319       DEADLOCK_AVOIDANCE(&alsalock);
00320    }
00321 }
00322 
00323 static int alsa_call(struct ast_channel *c, const char *dest, int timeout)
00324 {
00325    struct ast_frame f = { AST_FRAME_CONTROL };
00326 
00327    ast_mutex_lock(&alsalock);
00328    ast_verbose(" << Call placed to '%s' on console >> \n", dest);
00329    if (autoanswer) {
00330       ast_verbose(" << Auto-answered >> \n");
00331       if (mute) {
00332          ast_verbose( " << Muted >> \n" );
00333       }
00334       grab_owner();
00335       if (alsa.owner) {
00336          f.subclass.integer = AST_CONTROL_ANSWER;
00337          ast_queue_frame(alsa.owner, &f);
00338          ast_channel_unlock(alsa.owner);
00339       }
00340    } else {
00341       ast_verbose(" << Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
00342       grab_owner();
00343       if (alsa.owner) {
00344          f.subclass.integer = AST_CONTROL_RINGING;
00345          ast_queue_frame(alsa.owner, &f);
00346          ast_channel_unlock(alsa.owner);
00347          ast_indicate(alsa.owner, AST_CONTROL_RINGING);
00348       }
00349    }
00350    if (!noaudiocapture) {
00351       snd_pcm_prepare(alsa.icard);
00352       snd_pcm_start(alsa.icard);
00353    }
00354    ast_mutex_unlock(&alsalock);
00355 
00356    return 0;
00357 }
00358 
00359 static int alsa_answer(struct ast_channel *c)
00360 {
00361    ast_mutex_lock(&alsalock);
00362    ast_verbose(" << Console call has been answered >> \n");
00363    ast_setstate(c, AST_STATE_UP);
00364    if (!noaudiocapture) {
00365       snd_pcm_prepare(alsa.icard);
00366       snd_pcm_start(alsa.icard);
00367    }
00368    ast_mutex_unlock(&alsalock);
00369 
00370    return 0;
00371 }
00372 
00373 static int alsa_hangup(struct ast_channel *c)
00374 {
00375    ast_mutex_lock(&alsalock);
00376    ast_channel_tech_pvt_set(c, NULL);
00377    alsa.owner = NULL;
00378    ast_verbose(" << Hangup on console >> \n");
00379    ast_module_unref(ast_module_info->self);
00380    hookstate = 0;
00381    if (!noaudiocapture) {
00382       snd_pcm_drop(alsa.icard);
00383    }
00384    ast_mutex_unlock(&alsalock);
00385 
00386    return 0;
00387 }
00388 
00389 static int alsa_write(struct ast_channel *chan, struct ast_frame *f)
00390 {
00391    static char sizbuf[8000];
00392    static int sizpos = 0;
00393    int len = sizpos;
00394    int res = 0;
00395    /* size_t frames = 0; */
00396    snd_pcm_state_t state;
00397 
00398    ast_mutex_lock(&alsalock);
00399 
00400    /* We have to digest the frame in 160-byte portions */
00401    if (f->datalen > sizeof(sizbuf) - sizpos) {
00402       ast_log(LOG_WARNING, "Frame too large\n");
00403       res = -1;
00404    } else {
00405       memcpy(sizbuf + sizpos, f->data.ptr, f->datalen);
00406       len += f->datalen;
00407       state = snd_pcm_state(alsa.ocard);
00408       if (state == SND_PCM_STATE_XRUN)
00409          snd_pcm_prepare(alsa.ocard);
00410       while ((res = snd_pcm_writei(alsa.ocard, sizbuf, len / 2)) == -EAGAIN) {
00411          usleep(1);
00412       }
00413       if (res == -EPIPE) {
00414 #if DEBUG
00415          ast_debug(1, "XRUN write\n");
00416 #endif
00417          snd_pcm_prepare(alsa.ocard);
00418          while ((res = snd_pcm_writei(alsa.ocard, sizbuf, len / 2)) == -EAGAIN) {
00419             usleep(1);
00420          }
00421          if (res != len / 2) {
00422             ast_log(LOG_ERROR, "Write error: %s\n", snd_strerror(res));
00423             res = -1;
00424          } else if (res < 0) {
00425             ast_log(LOG_ERROR, "Write error %s\n", snd_strerror(res));
00426             res = -1;
00427          }
00428       } else {
00429          if (res == -ESTRPIPE)
00430             ast_log(LOG_ERROR, "You've got some big problems\n");
00431          else if (res < 0)
00432             ast_log(LOG_NOTICE, "Error %d on write\n", res);
00433       }
00434    }
00435    ast_mutex_unlock(&alsalock);
00436 
00437    return res >= 0 ? 0 : res;
00438 }
00439 
00440 
00441 static struct ast_frame *alsa_read(struct ast_channel *chan)
00442 {
00443    static struct ast_frame f;
00444    static short __buf[FRAME_SIZE + AST_FRIENDLY_OFFSET / 2];
00445    short *buf;
00446    static int readpos = 0;
00447    static int left = FRAME_SIZE;
00448    snd_pcm_state_t state;
00449    int r = 0;
00450 
00451    ast_mutex_lock(&alsalock);
00452    f.frametype = AST_FRAME_NULL;
00453    f.subclass.integer = 0;
00454    f.samples = 0;
00455    f.datalen = 0;
00456    f.data.ptr = NULL;
00457    f.offset = 0;
00458    f.src = "Console";
00459    f.mallocd = 0;
00460    f.delivery.tv_sec = 0;
00461    f.delivery.tv_usec = 0;
00462 
00463    if (noaudiocapture) {
00464       /* Return null frame to asterisk*/
00465       ast_mutex_unlock(&alsalock);
00466       return &f;
00467    }
00468 
00469    state = snd_pcm_state(alsa.icard);
00470    if ((state != SND_PCM_STATE_PREPARED) && (state != SND_PCM_STATE_RUNNING)) {
00471       snd_pcm_prepare(alsa.icard);
00472    }
00473 
00474    buf = __buf + AST_FRIENDLY_OFFSET / 2;
00475 
00476    r = snd_pcm_readi(alsa.icard, buf + readpos, left);
00477    if (r == -EPIPE) {
00478 #if DEBUG
00479       ast_log(LOG_ERROR, "XRUN read\n");
00480 #endif
00481       snd_pcm_prepare(alsa.icard);
00482    } else if (r == -ESTRPIPE) {
00483       ast_log(LOG_ERROR, "-ESTRPIPE\n");
00484       snd_pcm_prepare(alsa.icard);
00485    } else if (r < 0) {
00486       ast_log(LOG_ERROR, "Read error: %s\n", snd_strerror(r));
00487    }
00488 
00489    /* Return NULL frame on error */
00490    if (r < 0) {
00491       ast_mutex_unlock(&alsalock);
00492       return &f;
00493    }
00494 
00495    /* Update positions */
00496    readpos += r;
00497    left -= r;
00498 
00499    if (readpos >= FRAME_SIZE) {
00500       /* A real frame */
00501       readpos = 0;
00502       left = FRAME_SIZE;
00503       if (ast_channel_state(chan) != AST_STATE_UP) {
00504          /* Don't transmit unless it's up */
00505          ast_mutex_unlock(&alsalock);
00506          return &f;
00507       }
00508       if (mute) {
00509          /* Don't transmit if muted */
00510          ast_mutex_unlock(&alsalock);
00511          return &f;
00512       }
00513 
00514       f.frametype = AST_FRAME_VOICE;
00515       f.subclass.format = ast_format_slin;
00516       f.samples = FRAME_SIZE;
00517       f.datalen = FRAME_SIZE * 2;
00518       f.data.ptr = buf;
00519       f.offset = AST_FRIENDLY_OFFSET;
00520       f.src = "Console";
00521       f.mallocd = 0;
00522 
00523    }
00524    ast_mutex_unlock(&alsalock);
00525 
00526    return &f;
00527 }
00528 
00529 static int alsa_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
00530 {
00531    struct chan_alsa_pvt *p = ast_channel_tech_pvt(newchan);
00532 
00533    ast_mutex_lock(&alsalock);
00534    p->owner = newchan;
00535    ast_mutex_unlock(&alsalock);
00536 
00537    return 0;
00538 }
00539 
00540 static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen)
00541 {
00542    int res = 0;
00543 
00544    ast_mutex_lock(&alsalock);
00545 
00546    switch (cond) {
00547    case AST_CONTROL_BUSY:
00548    case AST_CONTROL_CONGESTION:
00549    case AST_CONTROL_RINGING:
00550    case AST_CONTROL_INCOMPLETE:
00551    case AST_CONTROL_PVT_CAUSE_CODE:
00552    case -1:
00553       res = -1;  /* Ask for inband indications */
00554       break;
00555    case AST_CONTROL_PROGRESS:
00556    case AST_CONTROL_PROCEEDING:
00557    case AST_CONTROL_VIDUPDATE:
00558    case AST_CONTROL_SRCUPDATE:
00559       break;
00560    case AST_CONTROL_HOLD:
00561       ast_verbose(" << Console Has Been Placed on Hold >> \n");
00562       ast_moh_start(chan, data, mohinterpret);
00563       break;
00564    case AST_CONTROL_UNHOLD:
00565       ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
00566       ast_moh_stop(chan);
00567       break;
00568    default:
00569       ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, ast_channel_name(chan));
00570       res = -1;
00571    }
00572 
00573    ast_mutex_unlock(&alsalock);
00574 
00575    return res;
00576 }
00577 
00578 static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
00579 {
00580    struct ast_channel *tmp = NULL;
00581 
00582    if (!(tmp = ast_channel_alloc(1, state, 0, 0, "", p->exten, p->context, assignedids, requestor, 0, "ALSA/%s", indevname)))
00583       return NULL;
00584 
00585    ast_channel_stage_snapshot(tmp);
00586 
00587    ast_channel_tech_set(tmp, &alsa_tech);
00588    ast_channel_set_fd(tmp, 0, readdev);
00589    ast_channel_set_readformat(tmp, ast_format_slin);
00590    ast_channel_set_writeformat(tmp, ast_format_slin);
00591    ast_channel_nativeformats_set(tmp, alsa_tech.capabilities);
00592 
00593    ast_channel_tech_pvt_set(tmp, p);
00594    if (!ast_strlen_zero(p->context))
00595       ast_channel_context_set(tmp, p->context);
00596    if (!ast_strlen_zero(p->exten))
00597       ast_channel_exten_set(tmp, p->exten);
00598    if (!ast_strlen_zero(language))
00599       ast_channel_language_set(tmp, language);
00600    p->owner = tmp;
00601    ast_module_ref(ast_module_info->self);
00602    ast_jb_configure(tmp, &global_jbconf);
00603 
00604    ast_channel_stage_snapshot_done(tmp);
00605    ast_channel_unlock(tmp);
00606 
00607    if (state != AST_STATE_DOWN) {
00608       if (ast_pbx_start(tmp)) {
00609          ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
00610          ast_hangup(tmp);
00611          tmp = NULL;
00612       }
00613    }
00614 
00615    return tmp;
00616 }
00617 
00618 static struct ast_channel *alsa_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
00619 {
00620    struct ast_channel *tmp = NULL;
00621 
00622    if (ast_format_cap_iscompatible_format(cap, ast_format_slin) == AST_FORMAT_CMP_NOT_EQUAL) {
00623       struct ast_str *codec_buf = ast_str_alloca(64);
00624       ast_log(LOG_NOTICE, "Asked to get a channel of format '%s'\n", ast_format_cap_get_names(cap, &codec_buf));
00625       return NULL;
00626    }
00627 
00628    ast_mutex_lock(&alsalock);
00629 
00630    if (alsa.owner) {
00631       ast_log(LOG_NOTICE, "Already have a call on the ALSA channel\n");
00632       *cause = AST_CAUSE_BUSY;
00633    } else if (!(tmp = alsa_new(&alsa, AST_STATE_DOWN, assignedids, requestor))) {
00634       ast_log(LOG_WARNING, "Unable to create new ALSA channel\n");
00635    }
00636 
00637    ast_mutex_unlock(&alsalock);
00638 
00639    return tmp;
00640 }
00641 
00642 static char *autoanswer_complete(const char *line, const char *word, int pos, int state)
00643 {
00644    switch (state) {
00645       case 0:
00646          if (!ast_strlen_zero(word) && !strncasecmp(word, "on", MIN(strlen(word), 2)))
00647             return ast_strdup("on");
00648       case 1:
00649          if (!ast_strlen_zero(word) && !strncasecmp(word, "off", MIN(strlen(word), 3)))
00650             return ast_strdup("off");
00651       default:
00652          return NULL;
00653    }
00654 
00655    return NULL;
00656 }
00657 
00658 static char *console_autoanswer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00659 {
00660    char *res = CLI_SUCCESS;
00661 
00662    switch (cmd) {
00663    case CLI_INIT:
00664       e->command = "console autoanswer";
00665       e->usage =
00666          "Usage: console autoanswer [on|off]\n"
00667          "       Enables or disables autoanswer feature.  If used without\n"
00668          "       argument, displays the current on/off status of autoanswer.\n"
00669          "       The default value of autoanswer is in 'alsa.conf'.\n";
00670       return NULL;
00671    case CLI_GENERATE:
00672       return autoanswer_complete(a->line, a->word, a->pos, a->n);
00673    }
00674 
00675    if ((a->argc != 2) && (a->argc != 3))
00676       return CLI_SHOWUSAGE;
00677 
00678    ast_mutex_lock(&alsalock);
00679    if (a->argc == 2) {
00680       ast_cli(a->fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
00681    } else {
00682       if (!strcasecmp(a->argv[2], "on"))
00683          autoanswer = -1;
00684       else if (!strcasecmp(a->argv[2], "off"))
00685          autoanswer = 0;
00686       else
00687          res = CLI_SHOWUSAGE;
00688    }
00689    ast_mutex_unlock(&alsalock);
00690 
00691    return res;
00692 }
00693 
00694 static char *console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00695 {
00696    char *res = CLI_SUCCESS;
00697 
00698    switch (cmd) {
00699    case CLI_INIT:
00700       e->command = "console answer";
00701       e->usage =
00702          "Usage: console answer\n"
00703          "       Answers an incoming call on the console (ALSA) channel.\n";
00704 
00705       return NULL;
00706    case CLI_GENERATE:
00707       return NULL; 
00708    }
00709 
00710    if (a->argc != 2)
00711       return CLI_SHOWUSAGE;
00712 
00713    ast_mutex_lock(&alsalock);
00714 
00715    if (!alsa.owner) {
00716       ast_cli(a->fd, "No one is calling us\n");
00717       res = CLI_FAILURE;
00718    } else {
00719       if (mute) {
00720          ast_verbose( " << Muted >> \n" );
00721       }
00722       hookstate = 1;
00723       grab_owner();
00724       if (alsa.owner) {
00725          ast_queue_control(alsa.owner, AST_CONTROL_ANSWER);
00726          ast_channel_unlock(alsa.owner);
00727       }
00728    }
00729 
00730    if (!noaudiocapture) {
00731       snd_pcm_prepare(alsa.icard);
00732       snd_pcm_start(alsa.icard);
00733    }
00734 
00735    ast_mutex_unlock(&alsalock);
00736 
00737    return res;
00738 }
00739 
00740 static char *console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00741 {
00742    int tmparg = 3;
00743    char *res = CLI_SUCCESS;
00744 
00745    switch (cmd) {
00746    case CLI_INIT:
00747       e->command = "console send text";
00748       e->usage =
00749          "Usage: console send text <message>\n"
00750          "       Sends a text message for display on the remote terminal.\n";
00751       return NULL;
00752    case CLI_GENERATE:
00753       return NULL; 
00754    }
00755 
00756    if (a->argc < 3)
00757       return CLI_SHOWUSAGE;
00758 
00759    ast_mutex_lock(&alsalock);
00760 
00761    if (!alsa.owner) {
00762       ast_cli(a->fd, "No channel active\n");
00763       res = CLI_FAILURE;
00764    } else {
00765       struct ast_frame f = { AST_FRAME_TEXT };
00766       char text2send[256] = "";
00767 
00768       while (tmparg < a->argc) {
00769          strncat(text2send, a->argv[tmparg++], sizeof(text2send) - strlen(text2send) - 1);
00770          strncat(text2send, " ", sizeof(text2send) - strlen(text2send) - 1);
00771       }
00772 
00773       text2send[strlen(text2send) - 1] = '\n';
00774       f.data.ptr = text2send;
00775       f.datalen = strlen(text2send) + 1;
00776       grab_owner();
00777       if (alsa.owner) {
00778          ast_queue_frame(alsa.owner, &f);
00779          ast_queue_control(alsa.owner, AST_CONTROL_ANSWER);
00780          ast_channel_unlock(alsa.owner);
00781       }
00782    }
00783 
00784    ast_mutex_unlock(&alsalock);
00785 
00786    return res;
00787 }
00788 
00789 static char *console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00790 {
00791    char *res = CLI_SUCCESS;
00792 
00793    switch (cmd) {
00794    case CLI_INIT:
00795       e->command = "console hangup";
00796       e->usage =
00797          "Usage: console hangup\n"
00798          "       Hangs up any call currently placed on the console.\n";
00799       return NULL;
00800    case CLI_GENERATE:
00801       return NULL; 
00802    }
00803  
00804 
00805    if (a->argc != 2)
00806       return CLI_SHOWUSAGE;
00807 
00808    ast_mutex_lock(&alsalock);
00809 
00810    if (!alsa.owner && !hookstate) {
00811       ast_cli(a->fd, "No call to hangup\n");
00812       res = CLI_FAILURE;
00813    } else {
00814       hookstate = 0;
00815       grab_owner();
00816       if (alsa.owner) {
00817          ast_queue_hangup_with_cause(alsa.owner, AST_CAUSE_NORMAL_CLEARING);
00818          ast_channel_unlock(alsa.owner);
00819       }
00820    }
00821 
00822    ast_mutex_unlock(&alsalock);
00823 
00824    return res;
00825 }
00826 
00827 static char *console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00828 {
00829    char tmp[256], *tmp2;
00830    char *mye, *myc;
00831    const char *d;
00832    char *res = CLI_SUCCESS;
00833 
00834    switch (cmd) {
00835    case CLI_INIT:
00836       e->command = "console dial";
00837       e->usage =
00838          "Usage: console dial [extension[@context]]\n"
00839          "       Dials a given extension (and context if specified)\n";
00840       return NULL;
00841    case CLI_GENERATE:
00842       return NULL;
00843    }
00844 
00845    if ((a->argc != 2) && (a->argc != 3))
00846       return CLI_SHOWUSAGE;
00847 
00848    ast_mutex_lock(&alsalock);
00849 
00850    if (alsa.owner) {
00851       if (a->argc == 3) {
00852          if (alsa.owner) {
00853             for (d = a->argv[2]; *d; d++) {
00854                struct ast_frame f = { .frametype = AST_FRAME_DTMF, .subclass.integer = *d };
00855 
00856                ast_queue_frame(alsa.owner, &f);
00857             }
00858          }
00859       } else {
00860          ast_cli(a->fd, "You're already in a call.  You can use this only to dial digits until you hangup\n");
00861          res = CLI_FAILURE;
00862       }
00863    } else {
00864       mye = exten;
00865       myc = context;
00866       if (a->argc == 3) {
00867          char *stringp = NULL;
00868 
00869          ast_copy_string(tmp, a->argv[2], sizeof(tmp));
00870          stringp = tmp;
00871          strsep(&stringp, "@");
00872          tmp2 = strsep(&stringp, "@");
00873          if (!ast_strlen_zero(tmp))
00874             mye = tmp;
00875          if (!ast_strlen_zero(tmp2))
00876             myc = tmp2;
00877       }
00878       if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
00879          ast_copy_string(alsa.exten, mye, sizeof(alsa.exten));
00880          ast_copy_string(alsa.context, myc, sizeof(alsa.context));
00881          hookstate = 1;
00882          alsa_new(&alsa, AST_STATE_RINGING, NULL, NULL);
00883       } else
00884          ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc);
00885    }
00886 
00887    ast_mutex_unlock(&alsalock);
00888 
00889    return res;
00890 }
00891 
00892 static char *console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00893 {
00894    int toggle = 0;
00895    char *res = CLI_SUCCESS;
00896 
00897    switch (cmd) {
00898    case CLI_INIT:
00899       e->command = "console {mute|unmute} [toggle]";
00900       e->usage =
00901          "Usage: console {mute|unmute} [toggle]\n"
00902          "       Mute/unmute the microphone.\n";
00903       return NULL;
00904    case CLI_GENERATE:
00905       return NULL;
00906    }
00907 
00908 
00909    if (a->argc > 3) {
00910       return CLI_SHOWUSAGE;
00911    }
00912 
00913    if (a->argc == 3) {
00914       if (strcasecmp(a->argv[2], "toggle"))
00915          return CLI_SHOWUSAGE;
00916       toggle = 1;
00917    }
00918 
00919    if (a->argc < 2) {
00920       return CLI_SHOWUSAGE;
00921    }
00922 
00923    if (!strcasecmp(a->argv[1], "mute")) {
00924       mute = toggle ? !mute : 1;
00925    } else if (!strcasecmp(a->argv[1], "unmute")) {
00926       mute = toggle ? !mute : 0;
00927    } else {
00928       return CLI_SHOWUSAGE;
00929    }
00930 
00931    ast_cli(a->fd, "Console mic is %s\n", mute ? "off" : "on");
00932 
00933    return res;
00934 }
00935 
00936 static struct ast_cli_entry cli_alsa[] = {
00937    AST_CLI_DEFINE(console_answer, "Answer an incoming console call"),
00938    AST_CLI_DEFINE(console_hangup, "Hangup a call on the console"),
00939    AST_CLI_DEFINE(console_dial, "Dial an extension on the console"),
00940    AST_CLI_DEFINE(console_sendtext, "Send text to the remote device"),
00941    AST_CLI_DEFINE(console_autoanswer, "Sets/displays autoanswer"),
00942    AST_CLI_DEFINE(console_mute, "Disable/Enable mic input"),
00943 };
00944 
00945 /*!
00946  * \brief Load the module
00947  *
00948  * Module loading including tests for configuration or dependencies.
00949  * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
00950  * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
00951  * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the 
00952  * configuration file or other non-critical problem return 
00953  * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
00954  */
00955 static int load_module(void)
00956 {
00957    struct ast_config *cfg;
00958    struct ast_variable *v;
00959    struct ast_flags config_flags = { 0 };
00960 
00961    if (!(alsa_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
00962       return AST_MODULE_LOAD_DECLINE;
00963    }
00964    ast_format_cap_append(alsa_tech.capabilities, ast_format_slin, 0);
00965 
00966    /* Copy the default jb config over global_jbconf */
00967    memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
00968 
00969    strcpy(mohinterpret, "default");
00970 
00971    if (!(cfg = ast_config_load(config, config_flags))) {
00972       ast_log(LOG_ERROR, "Unable to read ALSA configuration file %s.  Aborting.\n", config);
00973       return AST_MODULE_LOAD_DECLINE;
00974    } else if (cfg == CONFIG_STATUS_FILEINVALID) {
00975       ast_log(LOG_ERROR, "%s is in an invalid format.  Aborting.\n", config);
00976       return AST_MODULE_LOAD_DECLINE;
00977    }
00978 
00979    v = ast_variable_browse(cfg, "general");
00980    for (; v; v = v->next) {
00981       /* handle jb conf */
00982       if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) {
00983          continue;
00984       }
00985 
00986       if (!strcasecmp(v->name, "autoanswer")) {
00987          autoanswer = ast_true(v->value);
00988       } else if (!strcasecmp(v->name, "mute")) {
00989          mute = ast_true(v->value);
00990       } else if (!strcasecmp(v->name, "noaudiocapture")) {
00991          noaudiocapture = ast_true(v->value);
00992       } else if (!strcasecmp(v->name, "silencesuppression")) {
00993          silencesuppression = ast_true(v->value);
00994       } else if (!strcasecmp(v->name, "silencethreshold")) {
00995          silencethreshold = atoi(v->value);
00996       } else if (!strcasecmp(v->name, "context")) {
00997          ast_copy_string(context, v->value, sizeof(context));
00998       } else if (!strcasecmp(v->name, "language")) {
00999          ast_copy_string(language, v->value, sizeof(language));
01000       } else if (!strcasecmp(v->name, "extension")) {
01001          ast_copy_string(exten, v->value, sizeof(exten));
01002       } else if (!strcasecmp(v->name, "input_device")) {
01003          ast_copy_string(indevname, v->value, sizeof(indevname));
01004       } else if (!strcasecmp(v->name, "output_device")) {
01005          ast_copy_string(outdevname, v->value, sizeof(outdevname));
01006       } else if (!strcasecmp(v->name, "mohinterpret")) {
01007          ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
01008       }
01009    }
01010    ast_config_destroy(cfg);
01011 
01012    if (soundcard_init() < 0) {
01013       ast_verb(2, "No sound card detected -- console channel will be unavailable\n");
01014       ast_verb(2, "Turn off ALSA support by adding 'noload=chan_alsa.so' in /etc/asterisk/modules.conf\n");
01015       return AST_MODULE_LOAD_DECLINE;
01016    }
01017 
01018    if (ast_channel_register(&alsa_tech)) {
01019       ast_log(LOG_ERROR, "Unable to register channel class 'Console'\n");
01020       return AST_MODULE_LOAD_FAILURE;
01021    }
01022 
01023    ast_cli_register_multiple(cli_alsa, ARRAY_LEN(cli_alsa));
01024 
01025    return AST_MODULE_LOAD_SUCCESS;
01026 }
01027 
01028 static int unload_module(void)
01029 {
01030    ast_channel_unregister(&alsa_tech);
01031    ast_cli_unregister_multiple(cli_alsa, ARRAY_LEN(cli_alsa));
01032 
01033    if (alsa.icard)
01034       snd_pcm_close(alsa.icard);
01035    if (alsa.ocard)
01036       snd_pcm_close(alsa.ocard);
01037    if (alsa.owner)
01038       ast_softhangup(alsa.owner, AST_SOFTHANGUP_APPUNLOAD);
01039    if (alsa.owner)
01040       return -1;
01041 
01042    ao2_cleanup(alsa_tech.capabilities);
01043    alsa_tech.capabilities = NULL;
01044 
01045    return 0;
01046 }
01047 
01048 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ALSA Console Channel Driver",
01049       .support_level = AST_MODULE_SUPPORT_EXTENDED,
01050       .load = load_module,
01051       .unload = unload_module,
01052       .load_pri = AST_MODPRI_CHANNEL_DRIVER,
01053    );

Generated on Thu Apr 16 06:27:18 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6