Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:06 2009

## Asterisk developer's documentation

# app_intercom.c File Reference

Use /dev/dsp as an intercom. More...

`#include <stdio.h>`
`#include <unistd.h>`
`#include <errno.h>`
`#include <sys/ioctl.h>`
`#include <string.h>`
`#include <stdlib.h>`
`#include <sys/time.h>`
`#include <netinet/in.h>`
`#include <soundcard.h>`
`#include "asterisk.h"`
`#include "asterisk/lock.h"`
`#include "asterisk/file.h"`
`#include "asterisk/frame.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/pbx.h"`
`#include "asterisk/module.h"`
`#include "asterisk/translate.h"`

Include dependency graph for app_intercom.c:

Go to the source code of this file.

## Defines

#define | BUFFER_SIZE 32
#define | DEV_DSP "/dev/dsp"

## Functions

| AST_MUTEX_DEFINE_STATIC (sound_lock)
static int | create_audio (void)
char * | description (void)
| Provides a description of the module.
static int | intercom_exec (struct ast_channel *chan, void *data)
char * | key ()
| Returns the ASTERISK_GPL_KEY.
int | load_module (void)
| Initialize the module.
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.
static int | write_audio (short *data, int len)

## Variables

static char * | app = "Intercom"
static char * | descrip
| LOCAL_USER_DECL
static char or by hangup | n
static int | sound = -1
| STANDARD_LOCAL_USER
static char * | synopsis = "(Obsolete) Send to Intercom"
static char * | tdesc = "Intercom using /dev/dsp for output"

## Detailed Description

Use /dev/dsp as an intercom.

Definition in file app_intercom.c.

## Define Documentation

#define BUFFER_SIZE 32

Definition at line 63 of file app_intercom.c.

Referenced by g726tolin_framein(), and lintog726_framein().

#define DEV_DSP "/dev/dsp"

Definition at line 59 of file app_intercom.c.

## Function Documentation

AST_MUTEX_DEFINE_STATIC | ( | sound_lock |  | ) |

static int create_audio | ( | void |  | ) | `[static]`

Definition at line 101 of file app_intercom.c.

    00102 {
    00103    int fmt, desired, res, fd;
    00104    fd = open(DEV_DSP, O_WRONLY);
    00105    if (fd < 0) {
    00106       ast_log(LOG_WARNING, "Unable to open %s: %s\n", DEV_DSP, strerror(errno));
    00107       close(fd);
    00108       return -1;
    00109    }
    00110    fmt = AFMT_S16_LE;
    00111    res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
    00112    if (res < 0) {
    00113       ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
    00114       close(fd);
    00115       return -1;
    00116    }
    00117    fmt = 0;
    00118    res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
    00119    if (res < 0) {
    00120       ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
    00121       close(fd);
    00122       return -1;
    00123    }
    00124    /* 8000 Hz desired */
    00125    desired = 8000;
    00126    fmt = desired;
    00127    res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
    00128    if (res < 0) {
    00129       ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
    00130       close(fd);
    00131       return -1;
    00132    }
    00133    if (fmt != desired) {
    00134       ast_log(LOG_WARNING, "Requested %d Hz, got %d Hz -- sound may be choppy\n", desired, fmt);
    00135    }
    00136 #if 1
    00137    /* 2 bytes * 15 units of 2^5 = 32 bytes per buffer */
    00138    fmt = ((BUFFER_SIZE) << 16) | (0x0005);
    00139    res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
    00140    if (res < 0) {
    00141       ast_log(LOG_WARNING, "Unable to set fragment size -- sound may be choppy\n");
    00142    }
    00143 #endif
    00144    sound = fd;
    00145    return 0;
    00146 }

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 219 of file app_intercom.c.

    00220 {
    00221    return tdesc;
    00222 }

static int intercom_exec | ( | struct ast_channel * | chan,
|  | void * | data |
| ) |  |  | `[static]`

Definition at line 148 of file app_intercom.c.

    00149 {
    00150    int res = 0;
    00151    struct localuser *u;
    00152    struct ast_frame *f;
    00153    int oreadformat;
    00154    LOCAL_USER_ADD(u);
    00155    /* Remember original read format */
    00156    oreadformat = chan->readformat;
    00157    /* Set mode to signed linear */
    00158    res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
    00159    if (res < 0) {
    00160       ast_log(LOG_WARNING, "Unable to set format to signed linear on channel %s\n", chan->name);
    00161       LOCAL_USER_REMOVE(u);
    00162       return -1;
    00163    }
    00164    /* Read packets from the channel */
    00165    while(!res) {
    00166       res = ast_waitfor(chan, -1);
    00167       if (res > 0) {
    00168          res = 0;
    00169          f = ast_read(chan);
    00170          if (f) {
    00171             if (f->frametype == AST_FRAME_DTMF) {
    00172                ast_frfree(f);
    00173                break;
    00174             } else {
    00175                if (f->frametype == AST_FRAME_VOICE) {
    00176                   if (f->subclass == AST_FORMAT_SLINEAR) {
    00177                      res = write_audio(f->data, f->datalen);
    00178                      if (res > 0)
    00179                         res = 0;
    00180                   } else
    00181                      ast_log(LOG_DEBUG, "Unable to handle non-signed linear frame (%d)\n", f->subclass);
    00182                }
    00183             }
    00184             ast_frfree(f);
    00185          } else
    00186             res = -1;
    00187       }
    00188    }
    00189
    00190    if (!res)
    00191       ast_set_read_format(chan, oreadformat);
    00192
    00193    LOCAL_USER_REMOVE(u);
    00194
    00195    return res;
    00196 }

char* key | ( | void |  | ) |

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

     char *key(void) {
             return ASTERISK_GPL_KEY;
     }

Returns:
  ASTERISK_GPL_KEY

Definition at line 231 of file app_intercom.c.

    00232 {
    00233    return ASTERISK_GPL_KEY;
    00234 }

int load_module | ( | void |  | ) |

Initialize the module.

This function is called at module load time. Put all code in here that needs to set up your module's hardware, software, registrations, etc.

Returns:
  This function should return 0 on success and non-zero on failure. If the module is not loaded successfully, Asterisk will call its unload_module() function.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
  int Always 0.

TE STUFF END

Definition at line 212 of file app_intercom.c.

    00213 {
    00214    if (create_audio())
    00215       return -1;
    00216    return ast_register_application(app, intercom_exec, synopsis, descrip);
    00217 }

int unload_module | ( | void |  | ) |

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
  Zero on success, or non-zero on error.

Definition at line 198 of file app_intercom.c.

    00199 {
    00200    int res;
    00201
    00202    if (sound > -1)
    00203       close(sound);
    00204
    00205    res = ast_unregister_application(app);
    00206
    00207    STANDARD_HANGUP_LOCALUSERS;
    00208
    00209    return res;
    00210 }

int usecount | ( | void |  | ) |

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
  The module's usecount.

Definition at line 224 of file app_intercom.c.

    00225 {
    00226    int res;
    00227    STANDARD_USECOUNT(res);
    00228    return res;
    00229 }

static int write_audio | ( | short * | data,
|  | int | len |
| ) |  |  | `[static]`

Definition at line 81 of file app_intercom.c.

    00082 {
    00083    int res;
    00084    struct audio_buf_info info;
    00085    ast_mutex_lock(&sound_lock);
    00086    if (sound < 0) {
    00087       ast_log(LOG_WARNING, "Sound device closed?\n");
    00088       ast_mutex_unlock(&sound_lock);
    00089       return -1;
    00090    }
    00091     if (ioctl(sound, SNDCTL_DSP_GETOSPACE, &info)) {
    00092       ast_log(LOG_WARNING, "Unable to read output space\n");
    00093       ast_mutex_unlock(&sound_lock);
    00094         return -1;
    00095     }
    00096    res = write(sound, data, len);
    00097    ast_mutex_unlock(&sound_lock);
    00098    return res;
    00099 }

## Variable Documentation

char* app = "Intercom"`[static]`

Definition at line 67 of file app_intercom.c.

char* descrip`[static]`

Initial value:

    "  Intercom(): Sends the user to the intercom (i.e. /dev/dsp).  This program\n"
    "is generally considered  obselete by the chan_oss module.  User can terminate\n"with a DTMF tone

Definition at line 70 of file app_intercom.c.

LOCAL_USER_DECL

Definition at line 76 of file app_intercom.c.

char or by hangup n`[static]`

Definition at line 70 of file app_intercom.c.

Referenced by action_originate(), add_route(), ast_callerid_split(), ast_frame_dump(), ast_osp_lookup(), ast_osp_validate(), ast_privacy_check(), ast_privacy_set(), ast_yy_scan_bytes(), base_encode(), child_handler(), cliinput(), collect_function_digits(), do_monitor(), extract_uri(), festival_exec(), handle_response(), handle_setcallerid(), iax2_call(), initreqprep(), ivr_dispatch(), packsms7(), parse_ok_contact(), parse_register_contact(), reqprep(), rpt(), rpt_exec(), rpt_master(), search_linked_libs(), setrdnis_exec(), sms_debug(), sms_log(), socket_receive_file_to_buff(), transmit_notify_request_with_callerid(), unpacksms16(), unpacksms8(), and zt_call().

int sound = -1`[static]`

Definition at line 79 of file app_intercom.c.

STANDARD_LOCAL_USER

Definition at line 74 of file app_intercom.c.

char* synopsis = "(Obsolete) Send to Intercom"`[static]`

Definition at line 69 of file app_intercom.c.

char* tdesc = "Intercom using /dev/dsp for output"`[static]`

Definition at line 65 of file app_intercom.c.