2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2007, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * FreeBSD changes and multiple device support by Luigi Rizzo, 2005.05.25
9 * note-this code best seen with ts=8 (8-spaces tabs) in the editor
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
22 // #define HAVE_VIDEO_CONSOLE // uncomment to enable video
25 * \brief Channel driver for OSS sound cards
27 * \author Mark Spencer <markster@digium.com>
31 * \arg \ref Config_oss
33 * \ingroup channel_drivers
37 <depend>ossaudio</depend>
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44 #include <ctype.h> /* isalnum() used here */
46 #include <sys/ioctl.h>
49 #include <linux/soundcard.h>
50 #elif defined(__FreeBSD__)
51 #include <sys/soundcard.h>
53 #include <soundcard.h>
56 #include "asterisk/channel.h"
57 #include "asterisk/file.h"
58 #include "asterisk/callerid.h"
59 #include "asterisk/module.h"
60 #include "asterisk/pbx.h"
61 #include "asterisk/cli.h"
62 #include "asterisk/causes.h"
63 #include "asterisk/musiconhold.h"
64 #include "asterisk/app.h"
66 /* ringtones we use */
72 /*! Global jitterbuffer configuration - by default, jb is disabled */
73 static struct ast_jb_conf default_jbconf =
77 .resync_threshold = -1,
80 static struct ast_jb_conf global_jbconf;
83 * Basic mode of operation:
85 * we have one keyboard (which receives commands from the keyboard)
86 * and multiple headset's connected to audio cards.
87 * Cards/Headsets are named as the sections of oss.conf.
88 * The section called [general] contains the default parameters.
90 * At any time, the keyboard is attached to one card, and you
91 * can switch among them using the command 'console foo'
92 * where 'foo' is the name of the card you want.
94 * oss.conf parameters are
98 ; General config options, with default values shown.
99 ; You should use one section per device, with [general] being used
100 ; for the first device and also as a template for other devices.
102 ; All but 'debug' can go also in the device-specific sections.
104 ; debug = 0x0 ; misc debug flags, default is 0
106 ; Set the device to use for I/O
109 ; Optional mixer command to run upon startup (e.g. to set
110 ; volume levels, mutes, etc.
113 ; Software mic volume booster (or attenuator), useful for sound
114 ; cards or microphones with poor sensitivity. The volume level
115 ; is in dB, ranging from -20.0 to +20.0
116 ; boost = n ; mic volume boost in dB
118 ; Set the callerid for outgoing calls
119 ; callerid = John Doe <555-1234>
121 ; autoanswer = no ; no autoanswer on call
122 ; autohangup = yes ; hangup when other party closes
123 ; extension = s ; default extension to call
124 ; context = default ; default context for outgoing calls
125 ; language = "" ; default language
127 ; Default Music on Hold class to use when this channel is placed on hold in
128 ; the case that the music class is not set on the channel with
129 ; Set(CHANNEL(musicclass)=whatever) in the dialplan and the peer channel
130 ; putting this one on hold did not suggest a class to use.
132 ; mohinterpret=default
134 ; If you set overridecontext to 'yes', then the whole dial string
135 ; will be interpreted as an extension, which is extremely useful
136 ; to dial SIP, IAX and other extensions which use the '@' character.
137 ; The default is 'no' just for backward compatibility, but the
138 ; suggestion is to change it.
139 ; overridecontext = no ; if 'no', the last @ will start the context
140 ; if 'yes' the whole string is an extension.
142 ; low level device parameters in case you have problems with the
143 ; device driver on your operating system. You should not touch these
144 ; unless you know what you are doing.
145 ; queuesize = 10 ; frames in device driver
146 ; frags = 8 ; argument to SETFRAGMENT
148 ;------------------------------ JITTER BUFFER CONFIGURATION --------------------------
149 ; jbenable = yes ; Enables the use of a jitterbuffer on the receiving side of an
150 ; OSS channel. Defaults to "no". An enabled jitterbuffer will
151 ; be used only if the sending side can create and the receiving
152 ; side can not accept jitter. The OSS channel can't accept jitter,
153 ; thus an enabled jitterbuffer on the receive OSS side will always
154 ; be used if the sending side can create jitter.
156 ; jbmaxsize = 200 ; Max length of the jitterbuffer in milliseconds.
158 ; jbresyncthreshold = 1000 ; Jump in the frame timestamps over which the jitterbuffer is
159 ; resynchronized. Useful to improve the quality of the voice, with
160 ; big jumps in/broken timestamps, usualy sent from exotic devices
161 ; and programs. Defaults to 1000.
163 ; jbimpl = fixed ; Jitterbuffer implementation, used on the receiving side of an OSS
164 ; channel. Two implementations are currenlty available - "fixed"
165 ; (with size always equals to jbmax-size) and "adaptive" (with
166 ; variable size, actually the new jb of IAX2). Defaults to fixed.
168 ; jblog = no ; Enables jitterbuffer frame logging. Defaults to "no".
169 ;-----------------------------------------------------------------------------------
172 ; device = /dev/dsp1 ; alternate device
176 .. and so on for the other cards.
181 * Helper macros to parse config arguments. They will go in a common
182 * header file if their usage is globally accepted. In the meantime,
183 * we define them here. Typical usage is as below.
184 * Remember to open a block right before M_START (as it declares
185 * some variables) and use the M_* macros WITHOUT A SEMICOLON:
188 * M_START(v->name, v->value)
190 * M_BOOL("dothis", x->flag1)
191 * M_STR("name", x->somestring)
192 * M_F("bar", some_c_code)
193 * M_END(some_final_statement)
194 * ... other code in the block
197 * XXX NOTE these macros should NOT be replicated in other parts of asterisk.
198 * Likely we will come up with a better way of doing config file parsing.
200 #define M_START(var, val) \
201 const char *__s = var; const char *__val = val;
203 #define M_F(tag, f) if (!strcasecmp((__s), tag)) { f; } else
204 #define M_BOOL(tag, dst) M_F(tag, (dst) = ast_true(__val) )
205 #define M_UINT(tag, dst) M_F(tag, (dst) = strtoul(__val, NULL, 0) )
206 #define M_STR(tag, dst) M_F(tag, ast_copy_string(dst, __val, sizeof(dst)))
209 * The following parameters are used in the driver:
211 * FRAME_SIZE the size of an audio frame, in samples.
212 * 160 is used almost universally, so you should not change it.
214 * FRAGS the argument for the SETFRAGMENT ioctl.
215 * Overridden by the 'frags' parameter in oss.conf
217 * Bits 0-7 are the base-2 log of the device's block size,
218 * bits 16-31 are the number of blocks in the driver's queue.
219 * There are a lot of differences in the way this parameter
220 * is supported by different drivers, so you may need to
221 * experiment a bit with the value.
222 * A good default for linux is 30 blocks of 64 bytes, which
223 * results in 6 frames of 320 bytes (160 samples).
224 * FreeBSD works decently with blocks of 256 or 512 bytes,
225 * leaving the number unspecified.
226 * Note that this only refers to the device buffer size,
227 * this module will then try to keep the lenght of audio
228 * buffered within small constraints.
230 * QUEUE_SIZE The max number of blocks actually allowed in the device
231 * driver's buffer, irrespective of the available number.
232 * Overridden by the 'queuesize' parameter in oss.conf
234 * Should be >=2, and at most as large as the hw queue above
235 * (otherwise it will never be full).
238 #define FRAME_SIZE 160
239 #define QUEUE_SIZE 10
241 #if defined(__FreeBSD__)
244 #define FRAGS ( ( (6 * 5) << 16 ) | 0x6 )
248 * XXX text message sizes are probably 256 chars, but i am
249 * not sure if there is a suitable definition anywhere.
251 #define TEXT_SIZE 256
254 #define TRYOPEN 1 /* try to open on startup */
256 #define O_CLOSE 0x444 /* special 'close' mode for device */
257 /* Which device to use */
258 #if defined( __OpenBSD__ ) || defined( __NetBSD__ )
259 #define DEV_DSP "/dev/audio"
261 #define DEV_DSP "/dev/dsp"
265 #define MIN(a,b) ((a) < (b) ? (a) : (b))
268 #define MAX(a,b) ((a) > (b) ? (a) : (b))
271 static char *config = "oss.conf"; /* default config file */
273 static int oss_debug;
276 * Each sound is made of 'datalen' samples of sound, repeated as needed to
277 * generate 'samplen' samples of data, then followed by 'silencelen' samples
278 * of silence. The loop is repeated if 'repeat' is set.
290 static struct sound sounds[] = {
291 { AST_CONTROL_RINGING, "RINGING", ringtone, sizeof(ringtone)/2, 16000, 32000, 1 },
292 { AST_CONTROL_BUSY, "BUSY", busy, sizeof(busy)/2, 4000, 4000, 1 },
293 { AST_CONTROL_CONGESTION, "CONGESTION", busy, sizeof(busy)/2, 2000, 2000, 1 },
294 { AST_CONTROL_RING, "RING10", ring10, sizeof(ring10)/2, 16000, 32000, 1 },
295 { AST_CONTROL_ANSWER, "ANSWER", answer, sizeof(answer)/2, 2200, 0, 0 },
296 { -1, NULL, 0, 0, 0, 0 }, /* end marker */
299 struct video_desc; /* opaque type for video support */
302 * \brief descriptor for one of our channels.
304 * There is one used for 'default' values (from the [general] entry in
305 * the configuration file), and then one instance for each device
306 * (the default is cloned from [general], others are only created
307 * if the relevant section exists).
309 struct chan_oss_pvt {
310 struct chan_oss_pvt *next;
314 * cursound indicates which in struct sound we play. -1 means nothing,
315 * any other value is a valid sound, in which case sampsent indicates
316 * the next sample to send in [0..samplen + silencelen]
317 * nosound is set to disable the audio data from the channel
318 * (so we can play the tones etc.).
320 int sndcmd[2]; /*!< Sound command pipe */
321 int cursound; /*!< index of sound to send */
322 int sampsent; /*!< # of sound samples sent */
323 int nosound; /*!< set to block audio from the PBX */
325 int total_blocks; /*!< total blocks in the output device */
327 enum { M_UNSET, M_FULL, M_READ, M_WRITE } duplex;
331 char *mixer_cmd; /*!< initial command to issue to the mixer */
332 unsigned int queuesize; /*!< max fragments in queue */
333 unsigned int frags; /*!< parameter for SETFRAGMENT */
335 int warned; /*!< various flags used for warnings */
336 #define WARN_used_blocks 1
339 int w_errors; /*!< overfull in the write path */
340 struct timeval lastopen;
345 /*! boost support. BOOST_SCALE * 10 ^(BOOST_MAX/20) must
346 * be representable in 16 bits to avoid overflows.
348 #define BOOST_SCALE (1<<9)
349 #define BOOST_MAX 40 /*!< slightly less than 7 bits */
350 int boost; /*!< input boost, scaled by BOOST_SCALE */
351 char device[64]; /*!< device to open */
355 struct ast_channel *owner;
357 struct video_desc *env; /*!< parameters for video support */
359 char ext[AST_MAX_EXTENSION];
360 char ctx[AST_MAX_CONTEXT];
361 char language[MAX_LANGUAGE];
362 char cid_name[256]; /*XXX */
363 char cid_num[256]; /*XXX */
364 char mohinterpret[MAX_MUSICCLASS];
366 /*! buffers used in oss_write */
367 char oss_write_buf[FRAME_SIZE * 2];
369 /*! buffers used in oss_read - AST_FRIENDLY_OFFSET space for headers
370 * plus enough room for a full frame
372 char oss_read_buf[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
373 int readpos; /*!< read position above */
374 struct ast_frame read_f; /*!< returned by oss_read */
377 /*! forward declaration */
378 static struct chan_oss_pvt *find_desc(char *dev);
380 /*! \brief return the pointer to the video descriptor */
381 static attribute_unused struct video_desc *get_video_desc(struct ast_channel *c)
383 struct chan_oss_pvt *o = c->tech_pvt;
384 return o ? o->env : NULL;
386 static struct chan_oss_pvt oss_default = {
389 .duplex = M_UNSET, /* XXX check this */
392 .queuesize = QUEUE_SIZE,
396 .readpos = AST_FRIENDLY_OFFSET, /* start here on reads */
397 .lastopen = { 0, 0 },
398 .boost = BOOST_SCALE,
401 static char *oss_active; /*!< the active device */
403 static int setformat(struct chan_oss_pvt *o, int mode);
405 static struct ast_channel *oss_request(const char *type, int format, void *data
407 static int oss_digit_begin(struct ast_channel *c, char digit);
408 static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration);
409 static int oss_text(struct ast_channel *c, const char *text);
410 static int oss_hangup(struct ast_channel *c);
411 static int oss_answer(struct ast_channel *c);
412 static struct ast_frame *oss_read(struct ast_channel *chan);
413 static int oss_call(struct ast_channel *c, char *dest, int timeout);
414 static int oss_write(struct ast_channel *chan, struct ast_frame *f);
415 static int oss_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
416 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
417 static char tdesc[] = "OSS Console Channel Driver";
419 #ifdef HAVE_VIDEO_CONSOLE
420 #include "console_video.c"
422 #define CONSOLE_VIDEO_CMDS \
424 /* provide replacements for some symbols used */
425 #define console_write_video NULL
426 #define console_video_start(x, y) {}
427 #define console_video_uninit(x) {}
428 #define console_video_config(x, y, z) 1 /* pretend nothing recognised */
429 #define console_video_cli(x, y, z) 0 /* pretend nothing recognised */
430 #define CONSOLE_FORMAT_VIDEO 0
433 static const struct ast_channel_tech oss_tech = {
435 .description = tdesc,
436 .capabilities = AST_FORMAT_SLINEAR | CONSOLE_FORMAT_VIDEO,
437 .requester = oss_request,
438 .send_digit_begin = oss_digit_begin,
439 .send_digit_end = oss_digit_end,
440 .send_text = oss_text,
441 .hangup = oss_hangup,
442 .answer = oss_answer,
446 .write_video = console_write_video,
447 .indicate = oss_indicate,
452 * \brief returns a pointer to the descriptor with the given name
454 static struct chan_oss_pvt *find_desc(char *dev)
456 struct chan_oss_pvt *o = NULL;
459 ast_log(LOG_WARNING, "null dev\n");
461 for (o = oss_default.next; o && o->name && dev && strcmp(o->name, dev) != 0; o = o->next);
464 ast_log(LOG_WARNING, "could not find <%s>\n", dev ? dev : "--no-device--");
470 * \brief split a string in extension-context, returns pointers to malloc'ed
473 * If we do not have 'overridecontext' then the last @ is considered as
474 * a context separator, and the context is overridden.
475 * This is usually not very necessary as you can play with the dialplan,
476 * and it is nice not to need it because you have '@' in SIP addresses.
478 * \return the buffer address.
480 static char *ast_ext_ctx(const char *src, char **ext, char **ctx)
482 struct chan_oss_pvt *o = find_desc(oss_active);
484 if (ext == NULL || ctx == NULL)
485 return NULL; /* error */
489 if (src && *src != '\0')
490 *ext = ast_strdup(src);
495 if (!o->overridecontext) {
496 /* parse from the right */
497 *ctx = strrchr(*ext, '@');
506 * \brief Returns the number of blocks used in the audio output channel
508 static int used_blocks(struct chan_oss_pvt *o)
510 struct audio_buf_info info;
512 if (ioctl(o->sounddev, SNDCTL_DSP_GETOSPACE, &info)) {
513 if (!(o->warned & WARN_used_blocks)) {
514 ast_log(LOG_WARNING, "Error reading output space\n");
515 o->warned |= WARN_used_blocks;
520 if (o->total_blocks == 0) {
521 if (0) /* debugging */
522 ast_log(LOG_WARNING, "fragtotal %d size %d avail %d\n", info.fragstotal, info.fragsize, info.fragments);
523 o->total_blocks = info.fragments;
526 return o->total_blocks - info.fragments;
529 /*! Write an exactly FRAME_SIZE sized frame */
530 static int soundcard_writeframe(struct chan_oss_pvt *o, short *data)
535 setformat(o, O_RDWR);
537 return 0; /* not fatal */
539 * Nothing complex to manage the audio device queue.
540 * If the buffer is full just drop the extra, otherwise write.
541 * XXX in some cases it might be useful to write anyways after
542 * a number of failures, to restart the output chain.
544 res = used_blocks(o);
545 if (res > o->queuesize) { /* no room to write a block */
546 if (o->w_errors++ == 0 && (oss_debug & 0x4))
547 ast_log(LOG_WARNING, "write: used %d blocks (%d)\n", res, o->w_errors);
551 return write(o->sounddev, (void *)data, FRAME_SIZE * 2);
555 * \brief Handler for 'sound writable' events from the sound thread.
557 * Builds a frame from the high level description of the sounds,
558 * and passes it to the audio device.
559 * The actual sound is made of 1 or more sequences of sound samples
560 * (s->datalen, repeated to make s->samplen samples) followed by
561 * s->silencelen samples of silence. The position in the sequence is stored
562 * in o->sampsent, which goes between 0 .. s->samplen+s->silencelen.
563 * In case we fail to write a frame, don't update o->sampsent.
565 static void send_sound(struct chan_oss_pvt *o)
567 short myframe[FRAME_SIZE];
569 int l_sampsent = o->sampsent;
572 if (o->cursound < 0) /* no sound to send */
575 s = &sounds[o->cursound];
577 for (ofs = 0; ofs < FRAME_SIZE; ofs += l) {
578 l = s->samplen - l_sampsent; /* # of available samples */
580 start = l_sampsent % s->datalen; /* source offset */
581 l = MIN(l, FRAME_SIZE - ofs); /* don't overflow the frame */
582 l = MIN(l, s->datalen - start); /* don't overflow the source */
583 bcopy(s->data + start, myframe + ofs, l * 2);
585 ast_log(LOG_WARNING, "send_sound sound %d/%d of %d into %d\n", l_sampsent, l, s->samplen, ofs);
587 } else { /* end of samples, maybe some silence */
588 static const short silence[FRAME_SIZE] = { 0, };
592 l = MIN(l, FRAME_SIZE - ofs);
593 bcopy(silence, myframe + ofs, l * 2);
595 } else { /* silence is over, restart sound if loop */
596 if (s->repeat == 0) { /* last block */
598 o->nosound = 0; /* allow audio data */
599 if (ofs < FRAME_SIZE) /* pad with silence */
600 bcopy(silence, myframe + ofs, (FRAME_SIZE - ofs) * 2);
606 l = soundcard_writeframe(o, myframe);
608 o->sampsent = l_sampsent; /* update status */
611 static void *sound_thread(void *arg)
614 struct chan_oss_pvt *o = (struct chan_oss_pvt *) arg;
617 * Just in case, kick the driver by trying to read from it.
618 * Ignore errors - this read is almost guaranteed to fail.
620 read(o->sounddev, ign, sizeof(ign));
624 struct timeval *to = NULL, t;
628 FD_SET(o->sndcmd[0], &rfds);
629 maxfd = o->sndcmd[0]; /* pipe from the main process */
630 if (o->cursound > -1 && o->sounddev < 0)
631 setformat(o, O_RDWR); /* need the channel, try to reopen */
632 else if (o->cursound == -1 && o->owner == NULL)
633 setformat(o, O_CLOSE); /* can close */
634 if (o->sounddev > -1) {
635 if (!o->owner) { /* no one owns the audio, so we must drain it */
636 FD_SET(o->sounddev, &rfds);
637 maxfd = MAX(o->sounddev, maxfd);
639 if (o->cursound > -1) {
641 * We would like to use select here, but the device
642 * is always writable, so this would become busy wait.
643 * So we rather set a timeout to 1/2 of the frame size.
646 t.tv_usec = (1000000 * FRAME_SIZE) / (5 * DEFAULT_SAMPLE_RATE);
650 /* ast_select emulates linux behaviour in terms of timeout handling */
651 res = ast_select(maxfd + 1, &rfds, &wfds, NULL, to);
653 ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
657 if (FD_ISSET(o->sndcmd[0], &rfds)) {
658 /* read which sound to play from the pipe */
661 read(o->sndcmd[0], &what, sizeof(what));
662 for (i = 0; sounds[i].ind != -1; i++) {
663 if (sounds[i].ind == what) {
666 o->nosound = 1; /* block audio from pbx */
670 if (sounds[i].ind == -1)
671 ast_log(LOG_WARNING, "invalid sound index: %d\n", what);
673 if (o->sounddev > -1) {
674 if (FD_ISSET(o->sounddev, &rfds)) /* read and ignore errors */
675 read(o->sounddev, ign, sizeof(ign));
676 if (to != NULL) /* maybe it is possible to write */
680 return NULL; /* Never reached */
684 * reset and close the device if opened,
685 * then open and initialize it in the desired mode,
686 * trigger reads and writes so we can start using it.
688 static int setformat(struct chan_oss_pvt *o, int mode)
690 int fmt, desired, res, fd;
692 if (o->sounddev >= 0) {
693 ioctl(o->sounddev, SNDCTL_DSP_RESET, 0);
698 if (mode == O_CLOSE) /* we are done */
700 if (ast_tvdiff_ms(ast_tvnow(), o->lastopen) < 1000)
701 return -1; /* don't open too often */
702 o->lastopen = ast_tvnow();
703 fd = o->sounddev = open(o->device, mode | O_NONBLOCK);
705 ast_log(LOG_WARNING, "Unable to re-open DSP device %s: %s\n", o->device, strerror(errno));
709 ast_channel_set_fd(o->owner, 0, fd);
711 #if __BYTE_ORDER == __LITTLE_ENDIAN
716 res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
718 ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
723 res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
724 /* Check to see if duplex set (FreeBSD Bug) */
725 res = ioctl(fd, SNDCTL_DSP_GETCAPS, &fmt);
726 if (res == 0 && (fmt & DSP_CAP_DUPLEX)) {
727 ast_verb(2, "Console is full duplex\n");
742 res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
744 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
747 fmt = desired = DEFAULT_SAMPLE_RATE; /* 8000 Hz desired */
748 res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
751 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
754 if (fmt != desired) {
755 if (!(o->warned & WARN_speed)) {
757 "Requested %d Hz, got %d Hz -- sound may be choppy\n",
759 o->warned |= WARN_speed;
763 * on Freebsd, SETFRAGMENT does not work very well on some cards.
764 * Default to use 256 bytes, let the user override
768 res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
770 if (!(o->warned & WARN_frag)) {
772 "Unable to set fragment size -- sound may be choppy\n");
773 o->warned |= WARN_frag;
777 /* on some cards, we need SNDCTL_DSP_SETTRIGGER to start outputting */
778 res = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT;
779 res = ioctl(fd, SNDCTL_DSP_SETTRIGGER, &res);
780 /* it may fail if we are in half duplex, never mind */
785 * some of the standard methods supported by channels.
787 static int oss_digit_begin(struct ast_channel *c, char digit)
792 static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration)
794 /* no better use for received digits than print them */
795 ast_verbose(" << Console Received digit %c of duration %u ms >> \n",
800 static int oss_text(struct ast_channel *c, const char *text)
802 /* print received messages */
803 ast_verbose(" << Console Received text %s >> \n", text);
807 /*! \brief Play ringtone 'x' on device 'o' */
808 static void ring(struct chan_oss_pvt *o, int x)
810 write(o->sndcmd[1], &x, sizeof(x));
815 * \brief handler for incoming calls. Either autoanswer, or start ringing
817 static int oss_call(struct ast_channel *c, char *dest, int timeout)
819 struct chan_oss_pvt *o = c->tech_pvt;
820 struct ast_frame f = { 0, };
821 AST_DECLARE_APP_ARGS(args,
825 char *parse = ast_strdupa(dest);
827 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
829 ast_verbose(" << Call to device '%s' dnid '%s' rdnis '%s' on console from '%s' <%s> >>\n", dest, c->cid.cid_dnid, c->cid.cid_rdnis, c->cid.cid_name, c->cid.cid_num);
830 if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "answer") == 0) {
831 f.frametype = AST_FRAME_CONTROL;
832 f.subclass = AST_CONTROL_ANSWER;
833 ast_queue_frame(c, &f);
834 } else if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "noanswer") == 0) {
835 f.frametype = AST_FRAME_CONTROL;
836 f.subclass = AST_CONTROL_RINGING;
837 ast_queue_frame(c, &f);
838 ring(o, AST_CONTROL_RING);
839 } else if (o->autoanswer) {
840 ast_verbose(" << Auto-answered >> \n");
841 f.frametype = AST_FRAME_CONTROL;
842 f.subclass = AST_CONTROL_ANSWER;
843 ast_queue_frame(c, &f);
845 ast_verbose("<< Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
846 f.frametype = AST_FRAME_CONTROL;
847 f.subclass = AST_CONTROL_RINGING;
848 ast_queue_frame(c, &f);
849 ring(o, AST_CONTROL_RING);
855 * \brief remote side answered the phone
857 static int oss_answer(struct ast_channel *c)
859 struct chan_oss_pvt *o = c->tech_pvt;
861 ast_verbose(" << Console call has been answered >> \n");
863 /* play an answer tone (XXX do we really need it ?) */
864 ring(o, AST_CONTROL_ANSWER);
866 ast_setstate(c, AST_STATE_UP);
872 static int oss_hangup(struct ast_channel *c)
874 struct chan_oss_pvt *o = c->tech_pvt;
880 ast_verbose(" << Hangup on console >> \n");
881 console_video_uninit(o->env);
882 ast_module_unref(ast_module_info->self);
884 if (o->autoanswer || o->autohangup) {
885 /* Assume auto-hangup too */
887 setformat(o, O_CLOSE);
889 /* Make congestion noise */
890 ring(o, AST_CONTROL_CONGESTION);
896 /*! \brief used for data coming from the network */
897 static int oss_write(struct ast_channel *c, struct ast_frame *f)
900 struct chan_oss_pvt *o = c->tech_pvt;
902 /* Immediately return if no sound is enabled */
905 /* Stop any currently playing sound */
908 * we could receive a block which is not a multiple of our
909 * FRAME_SIZE, so buffer it locally and write to the device
910 * in FRAME_SIZE chunks.
911 * Keep the residue stored for future use.
913 src = 0; /* read position into f->data */
914 while (src < f->datalen) {
915 /* Compute spare room in the buffer */
916 int l = sizeof(o->oss_write_buf) - o->oss_write_dst;
918 if (f->datalen - src >= l) { /* enough to fill a frame */
919 memcpy(o->oss_write_buf + o->oss_write_dst, f->data + src, l);
920 soundcard_writeframe(o, (short *) o->oss_write_buf);
922 o->oss_write_dst = 0;
923 } else { /* copy residue */
924 l = f->datalen - src;
925 memcpy(o->oss_write_buf + o->oss_write_dst, f->data + src, l);
926 src += l; /* but really, we are done */
927 o->oss_write_dst += l;
933 static struct ast_frame *oss_read(struct ast_channel *c)
936 struct chan_oss_pvt *o = c->tech_pvt;
937 struct ast_frame *f = &o->read_f;
939 /* XXX can be simplified returning &ast_null_frame */
940 /* prepare a NULL frame in case we don't have enough data to return */
941 bzero(f, sizeof(struct ast_frame));
942 f->frametype = AST_FRAME_NULL;
943 f->src = oss_tech.type;
945 res = read(o->sounddev, o->oss_read_buf + o->readpos, sizeof(o->oss_read_buf) - o->readpos);
946 if (res < 0) /* audio data not ready, return a NULL frame */
950 if (o->readpos < sizeof(o->oss_read_buf)) /* not enough samples */
956 o->readpos = AST_FRIENDLY_OFFSET; /* reset read pointer for next frame */
957 if (c->_state != AST_STATE_UP) /* drop data if frame is not up */
959 /* ok we can build and deliver the frame to the caller */
960 f->frametype = AST_FRAME_VOICE;
961 f->subclass = AST_FORMAT_SLINEAR;
962 f->samples = FRAME_SIZE;
963 f->datalen = FRAME_SIZE * 2;
964 f->data = o->oss_read_buf + AST_FRIENDLY_OFFSET;
965 if (o->boost != BOOST_SCALE) { /* scale and clip values */
967 int16_t *p = (int16_t *) f->data;
968 for (i = 0; i < f->samples; i++) {
969 x = (p[i] * o->boost) / BOOST_SCALE;
978 f->offset = AST_FRIENDLY_OFFSET;
982 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
984 struct chan_oss_pvt *o = newchan->tech_pvt;
989 static int oss_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen)
991 struct chan_oss_pvt *o = c->tech_pvt;
995 case AST_CONTROL_BUSY:
996 case AST_CONTROL_CONGESTION:
997 case AST_CONTROL_RINGING:
1003 o->nosound = 0; /* when cursound is -1 nosound must be 0 */
1006 case AST_CONTROL_VIDUPDATE:
1010 case AST_CONTROL_HOLD:
1011 ast_verbose(" << Console Has Been Placed on Hold >> \n");
1012 ast_moh_start(c, data, o->mohinterpret);
1015 case AST_CONTROL_UNHOLD:
1016 ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
1021 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, c->name);
1032 * \brief allocate a new channel.
1034 static struct ast_channel *oss_new(struct chan_oss_pvt *o, char *ext, char *ctx, int state)
1036 struct ast_channel *c;
1038 c = ast_channel_alloc(1, state, o->cid_num, o->cid_name, "", ext, ctx, 0, "OSS/%s", o->device + 5);
1041 c->tech = &oss_tech;
1042 if (o->sounddev < 0)
1043 setformat(o, O_RDWR);
1044 ast_channel_set_fd(c, 0, o->sounddev); /* -1 if device closed, override later */
1045 c->nativeformats = AST_FORMAT_SLINEAR;
1046 /* if the console makes the call, add video to the offer */
1047 if (state == AST_STATE_RINGING)
1048 c->nativeformats |= CONSOLE_FORMAT_VIDEO;
1050 c->readformat = AST_FORMAT_SLINEAR;
1051 c->writeformat = AST_FORMAT_SLINEAR;
1054 if (!ast_strlen_zero(o->language))
1055 ast_string_field_set(c, language, o->language);
1056 /* Don't use ast_set_callerid() here because it will
1057 * generate a needless NewCallerID event */
1058 c->cid.cid_ani = ast_strdup(o->cid_num);
1059 if (!ast_strlen_zero(ext))
1060 c->cid.cid_dnid = ast_strdup(ext);
1063 ast_module_ref(ast_module_info->self);
1064 ast_jb_configure(c, &global_jbconf);
1065 if (state != AST_STATE_DOWN) {
1066 if (ast_pbx_start(c)) {
1067 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", c->name);
1069 o->owner = c = NULL;
1070 /* XXX what about the channel itself ? */
1073 console_video_start(get_video_desc(c), c); /* XXX cleanup */
1078 static struct ast_channel *oss_request(const char *type, int format, void *data, int *cause)
1080 struct ast_channel *c;
1081 struct chan_oss_pvt *o;
1082 AST_DECLARE_APP_ARGS(args,
1086 char *parse = ast_strdupa(data);
1088 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
1089 o = find_desc(args.name);
1091 ast_log(LOG_WARNING, "oss_request ty <%s> data 0x%p <%s>\n", type, data, (char *) data);
1093 ast_log(LOG_NOTICE, "Device %s not found\n", args.name);
1094 /* XXX we could default to 'dsp' perhaps ? */
1097 if ((format & AST_FORMAT_SLINEAR) == 0) {
1098 ast_log(LOG_NOTICE, "Format 0x%x unsupported\n", format);
1102 ast_log(LOG_NOTICE, "Already have a call (chan %p) on the OSS channel\n", o->owner);
1103 *cause = AST_CAUSE_BUSY;
1106 c = oss_new(o, NULL, NULL, AST_STATE_DOWN);
1108 ast_log(LOG_WARNING, "Unable to create new OSS channel\n");
1114 static void store_config_core(struct chan_oss_pvt *o, const char *var, const char *value);
1116 /*! Generic console command handler. Basically a wrapper for a subset
1117 * of config file options which are also available from the CLI
1119 static char *console_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1121 struct chan_oss_pvt *o = find_desc(oss_active);
1122 const char *var, *value;
1125 e->command = CONSOLE_VIDEO_CMDS;
1126 e->usage = "Usage: " CONSOLE_VIDEO_CMDS "...\n"
1127 " Generic handler for console commands.\n";
1134 if (a->argc < e->args)
1135 return CLI_SHOWUSAGE;
1137 ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n",
1141 var = a->argv[e->args-1];
1142 value = a->argc > e->args ? a->argv[e->args] : NULL;
1143 if (value) /* handle setting */
1144 store_config_core(o, var, value);
1145 if (console_video_cli(o->env, var, a->fd)) /* print video-related values */
1147 /* handle other values */
1148 if (!strcasecmp(var, "device")) {
1149 ast_cli(a->fd, "device is [%s]\n", o->device);
1154 static char *console_autoanswer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1156 struct chan_oss_pvt *o = find_desc(oss_active);
1160 e->command = "console autoanswer [on|off]";
1162 "Usage: console autoanswer [on|off]\n"
1163 " Enables or disables autoanswer feature. If used without\n"
1164 " argument, displays the current on/off status of autoanswer.\n"
1165 " The default value of autoanswer is in 'oss.conf'.\n";
1172 if (a->argc == e->args - 1) {
1173 ast_cli(a->fd, "Auto answer is %s.\n", o->autoanswer ? "on" : "off");
1176 if (a->argc != e->args)
1177 return CLI_SHOWUSAGE;
1179 ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n",
1183 if (!strcasecmp(a->argv[e->args-1], "on"))
1185 else if (!strcasecmp(a->argv[e->args - 1], "off"))
1188 return CLI_SHOWUSAGE;
1193 * \brief answer command from the console
1195 static char *console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1197 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
1198 struct chan_oss_pvt *o = find_desc(oss_active);
1202 e->command = "console answer";
1204 "Usage: console answer\n"
1205 " Answers an incoming call on the console (OSS) channel.\n";
1209 return NULL; /* no completion */
1211 if (a->argc != e->args)
1212 return CLI_SHOWUSAGE;
1214 ast_cli(a->fd, "No one is calling us\n");
1220 ast_queue_frame(o->owner, &f);
1225 * \brief Console send text CLI command
1227 * \note concatenate all arguments into a single string. argv is NULL-terminated
1228 * so we can use it right away
1230 static char *console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1232 struct chan_oss_pvt *o = find_desc(oss_active);
1233 char buf[TEXT_SIZE];
1235 if (cmd == CLI_INIT) {
1236 e->command = "console send text";
1238 "Usage: console send text <message>\n"
1239 " Sends a text message for display on the remote terminal.\n";
1241 } else if (cmd == CLI_GENERATE)
1244 if (a->argc < e->args + 1)
1245 return CLI_SHOWUSAGE;
1247 ast_cli(a->fd, "Not in a call\n");
1250 ast_join(buf, sizeof(buf) - 1, a->argv + e->args);
1251 if (!ast_strlen_zero(buf)) {
1252 struct ast_frame f = { 0, };
1253 int i = strlen(buf);
1255 f.frametype = AST_FRAME_TEXT;
1259 ast_queue_frame(o->owner, &f);
1264 static char *console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1266 struct chan_oss_pvt *o = find_desc(oss_active);
1268 if (cmd == CLI_INIT) {
1269 e->command = "console hangup";
1271 "Usage: console hangup\n"
1272 " Hangs up any call currently placed on the console.\n";
1274 } else if (cmd == CLI_GENERATE)
1277 if (a->argc != e->args)
1278 return CLI_SHOWUSAGE;
1281 if (!o->owner && !o->hookstate) { /* XXX maybe only one ? */
1282 ast_cli(a->fd, "No call to hang up\n");
1287 ast_queue_hangup(o->owner);
1288 setformat(o, O_CLOSE);
1292 static char *console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1294 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH };
1295 struct chan_oss_pvt *o = find_desc(oss_active);
1297 if (cmd == CLI_INIT) {
1298 e->command = "console flash";
1300 "Usage: console flash\n"
1301 " Flashes the call currently placed on the console.\n";
1303 } else if (cmd == CLI_GENERATE)
1306 if (a->argc != e->args)
1307 return CLI_SHOWUSAGE;
1309 o->nosound = 0; /* when cursound is -1 nosound must be 0 */
1310 if (!o->owner) { /* XXX maybe !o->hookstate too ? */
1311 ast_cli(a->fd, "No call to flash\n");
1315 if (o->owner) /* XXX must be true, right ? */
1316 ast_queue_frame(o->owner, &f);
1320 static char *console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1322 char *s = NULL, *mye = NULL, *myc = NULL;
1323 struct chan_oss_pvt *o = find_desc(oss_active);
1325 if (cmd == CLI_INIT) {
1326 e->command = "console dial";
1328 "Usage: console dial [extension[@context]]\n"
1329 " Dials a given extension (and context if specified)\n";
1331 } else if (cmd == CLI_GENERATE)
1334 if (a->argc > e->args + 1)
1335 return CLI_SHOWUSAGE;
1336 if (o->owner) { /* already in a call */
1338 struct ast_frame f = { AST_FRAME_DTMF, 0 };
1340 if (a->argc == e->args) { /* argument is mandatory here */
1341 ast_cli(a->fd, "Already in a call. You can only dial digits until you hangup.\n");
1344 s = a->argv[e->args];
1345 /* send the string one char at a time */
1346 for (i = 0; i < strlen(s); i++) {
1348 ast_queue_frame(o->owner, &f);
1352 /* if we have an argument split it into extension and context */
1353 if (a->argc == e->args + 1)
1354 s = ast_ext_ctx(a->argv[e->args], &mye, &myc);
1355 /* supply default values if needed */
1360 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
1362 oss_new(o, mye, myc, AST_STATE_RINGING);
1364 ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc);
1370 static char *console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1372 struct chan_oss_pvt *o = find_desc(oss_active);
1375 if (cmd == CLI_INIT) {
1376 e->command = "console {mute|unmute}";
1378 "Usage: console {mute|unmute}\n"
1379 " Mute/unmute the microphone.\n";
1381 } else if (cmd == CLI_GENERATE)
1384 if (a->argc != e->args)
1385 return CLI_SHOWUSAGE;
1386 s = a->argv[e->args-1];
1387 if (!strcasecmp(s, "mute"))
1389 else if (!strcasecmp(s, "unmute"))
1392 return CLI_SHOWUSAGE;
1396 static char *console_transfer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1398 struct chan_oss_pvt *o = find_desc(oss_active);
1399 struct ast_channel *b = NULL;
1400 char *tmp, *ext, *ctx;
1404 e->command = "console transfer";
1406 "Usage: console transfer <extension>[@context]\n"
1407 " Transfers the currently connected call to the given extension (and\n"
1408 " context if specified)\n";
1415 return CLI_SHOWUSAGE;
1418 if (o->owner == NULL || (b = ast_bridged_channel(o->owner)) == NULL) {
1419 ast_cli(a->fd, "There is no call to transfer\n");
1423 tmp = ast_ext_ctx(a->argv[2], &ext, &ctx);
1424 if (ctx == NULL) /* supply default context if needed */
1425 ctx = o->owner->context;
1426 if (!ast_exists_extension(b, ctx, ext, 1, b->cid.cid_num))
1427 ast_cli(a->fd, "No such extension exists\n");
1429 ast_cli(a->fd, "Whee, transferring %s to %s@%s.\n", b->name, ext, ctx);
1430 if (ast_async_goto(b, ctx, ext, 1))
1431 ast_cli(a->fd, "Failed to transfer :(\n");
1438 static char *console_active(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1442 e->command = "console active";
1444 "Usage: console active [device]\n"
1445 " If used without a parameter, displays which device is the current\n"
1446 " console. If a device is specified, the console sound device is changed to\n"
1447 " the device specified.\n";
1454 ast_cli(a->fd, "active console is [%s]\n", oss_active);
1455 else if (a->argc != 3)
1456 return CLI_SHOWUSAGE;
1458 struct chan_oss_pvt *o;
1459 if (strcmp(a->argv[2], "show") == 0) {
1460 for (o = oss_default.next; o; o = o->next)
1461 ast_cli(a->fd, "device [%s] exists\n", o->name);
1464 o = find_desc(a->argv[2]);
1466 ast_cli(a->fd, "No device [%s] exists\n", a->argv[2]);
1468 oss_active = o->name;
1474 * \brief store the boost factor
1476 static void store_boost(struct chan_oss_pvt *o, const char *s)
1479 if (sscanf(s, "%lf", &boost) != 1) {
1480 ast_log(LOG_WARNING, "invalid boost <%s>\n", s);
1483 if (boost < -BOOST_MAX) {
1484 ast_log(LOG_WARNING, "boost %s too small, using %d\n", s, -BOOST_MAX);
1486 } else if (boost > BOOST_MAX) {
1487 ast_log(LOG_WARNING, "boost %s too large, using %d\n", s, BOOST_MAX);
1490 boost = exp(log(10) * boost / 20) * BOOST_SCALE;
1492 ast_log(LOG_WARNING, "setting boost %s to %d\n", s, o->boost);
1495 static char *console_boost(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1497 struct chan_oss_pvt *o = find_desc(oss_active);
1501 e->command = "console boost";
1503 "Usage: console boost [boost in dB]\n"
1504 " Sets or display mic boost in dB\n";
1511 ast_cli(a->fd, "boost currently %5.1f\n", 20 * log10(((double) o->boost / (double) BOOST_SCALE)));
1512 else if (a->argc == 3)
1513 store_boost(o, a->argv[2]);
1517 static struct ast_cli_entry cli_oss[] = {
1518 AST_CLI_DEFINE(console_answer, "Answer an incoming console call"),
1519 AST_CLI_DEFINE(console_hangup, "Hangup a call on the console"),
1520 AST_CLI_DEFINE(console_flash, "Flash a call on the console"),
1521 AST_CLI_DEFINE(console_dial, "Dial an extension on the console"),
1522 AST_CLI_DEFINE(console_mute, "Disable/Enable mic input"),
1523 AST_CLI_DEFINE(console_transfer, "Transfer a call to a different extension"),
1524 AST_CLI_DEFINE(console_cmd, "Generic console command"),
1525 AST_CLI_DEFINE(console_sendtext, "Send text to the remote device"),
1526 AST_CLI_DEFINE(console_autoanswer, "Sets/displays autoanswer"),
1527 AST_CLI_DEFINE(console_boost, "Sets/displays mic boost in dB"),
1528 AST_CLI_DEFINE(console_active, "Sets/displays active console"),
1532 * store the mixer argument from the config file, filtering possibly
1533 * invalid or dangerous values (the string is used as argument for
1534 * system("mixer %s")
1536 static void store_mixer(struct chan_oss_pvt *o, const char *s)
1540 for (i = 0; i < strlen(s); i++) {
1541 if (!isalnum(s[i]) && index(" \t-/", s[i]) == NULL) {
1542 ast_log(LOG_WARNING, "Suspect char %c in mixer cmd, ignoring:\n\t%s\n", s[i], s);
1547 ast_free(o->mixer_cmd);
1548 o->mixer_cmd = ast_strdup(s);
1549 ast_log(LOG_WARNING, "setting mixer %s\n", s);
1553 * store the callerid components
1555 static void store_callerid(struct chan_oss_pvt *o, const char *s)
1557 ast_callerid_split(s, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
1560 static void store_config_core(struct chan_oss_pvt *o, const char *var, const char *value)
1562 M_START(var, value);
1564 /* handle jb conf */
1565 if (!ast_jb_read_conf(&global_jbconf, (char *)var,(char *) value))
1568 if (!console_video_config(&o->env, var, value))
1570 M_BOOL("autoanswer", o->autoanswer)
1571 M_BOOL("autohangup", o->autohangup)
1572 M_BOOL("overridecontext", o->overridecontext)
1573 M_STR("device", o->device)
1574 M_UINT("frags", o->frags)
1575 M_UINT("debug", oss_debug)
1576 M_UINT("queuesize", o->queuesize)
1577 M_STR("context", o->ctx)
1578 M_STR("language", o->language)
1579 M_STR("mohinterpret", o->mohinterpret)
1580 M_STR("extension", o->ext)
1581 M_F("mixer", store_mixer(o, value))
1582 M_F("callerid", store_callerid(o, value))
1583 M_F("boost", store_boost(o, value))
1589 * grab fields from the config file, init the descriptor and open the device.
1591 static struct chan_oss_pvt *store_config(struct ast_config *cfg, char *ctg)
1593 struct ast_variable *v;
1594 struct chan_oss_pvt *o;
1600 if (!(o = ast_calloc(1, sizeof(*o))))
1603 /* "general" is also the default thing */
1604 if (strcmp(ctg, "general") == 0) {
1605 o->name = ast_strdup("dsp");
1606 oss_active = o->name;
1609 o->name = ast_strdup(ctg);
1612 strcpy(o->mohinterpret, "default");
1614 o->lastopen = ast_tvnow(); /* don't leave it 0 or tvdiff may wrap */
1615 /* fill other fields from configuration */
1616 for (v = ast_variable_browse(cfg, ctg); v; v = v->next) {
1617 store_config_core(o, v->name, v->value);
1619 if (ast_strlen_zero(o->device))
1620 ast_copy_string(o->device, DEV_DSP, sizeof(o->device));
1624 asprintf(&cmd, "mixer %s", o->mixer_cmd);
1625 ast_log(LOG_WARNING, "running [%s]\n", cmd);
1629 if (o == &oss_default) /* we are done with the default */
1634 if (setformat(o, O_RDWR) < 0) { /* open device */
1635 ast_verb(1, "Device %s not detected\n", ctg);
1636 ast_verb(1, "Turn off OSS support by adding " "'noload=chan_oss.so' in /etc/asterisk/modules.conf\n");
1639 if (o->duplex != M_FULL)
1640 ast_log(LOG_WARNING, "XXX I don't work right with non " "full-duplex sound cards XXX\n");
1641 #endif /* TRYOPEN */
1642 if (pipe(o->sndcmd) != 0) {
1643 ast_log(LOG_ERROR, "Unable to create pipe\n");
1646 ast_pthread_create_background(&o->sthread, NULL, sound_thread, o);
1647 /* link into list of devices */
1648 if (o != &oss_default) {
1649 o->next = oss_default.next;
1650 oss_default.next = o;
1655 if (o != &oss_default)
1660 static int load_module(void)
1662 struct ast_config *cfg = NULL;
1664 struct ast_flags config_flags = { 0 };
1666 /* Copy the default jb config over global_jbconf */
1667 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
1669 /* load config file */
1670 if (!(cfg = ast_config_load(config, config_flags))) {
1671 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
1672 return AST_MODULE_LOAD_DECLINE;
1676 store_config(cfg, ctg);
1677 } while ( (ctg = ast_category_browse(cfg, ctg)) != NULL);
1679 ast_config_destroy(cfg);
1681 if (find_desc(oss_active) == NULL) {
1682 ast_log(LOG_NOTICE, "Device %s not found\n", oss_active);
1683 /* XXX we could default to 'dsp' perhaps ? */
1684 /* XXX should cleanup allocated memory etc. */
1685 return AST_MODULE_LOAD_FAILURE;
1688 if (ast_channel_register(&oss_tech)) {
1689 ast_log(LOG_ERROR, "Unable to register channel type 'OSS'\n");
1690 return AST_MODULE_LOAD_FAILURE;
1693 ast_cli_register_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry));
1695 return AST_MODULE_LOAD_SUCCESS;
1699 static int unload_module(void)
1701 struct chan_oss_pvt *o;
1703 ast_channel_unregister(&oss_tech);
1704 ast_cli_unregister_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry));
1706 for (o = oss_default.next; o; o = o->next) {
1708 if (o->sndcmd[0] > 0) {
1709 close(o->sndcmd[0]);
1710 close(o->sndcmd[1]);
1713 ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
1714 if (o->owner) /* XXX how ??? */
1716 /* XXX what about the thread ? */
1717 /* XXX what about the memory allocated ? */
1722 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "OSS Console Channel Driver");