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>
38 <conflict>winarch</conflict>
43 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
45 #include <ctype.h> /* isalnum() used here */
47 #include <sys/ioctl.h>
50 #include <linux/soundcard.h>
51 #elif defined(__FreeBSD__)
52 #include <sys/soundcard.h>
54 #include <soundcard.h>
57 #include "asterisk/channel.h"
58 #include "asterisk/file.h"
59 #include "asterisk/callerid.h"
60 #include "asterisk/module.h"
61 #include "asterisk/pbx.h"
62 #include "asterisk/cli.h"
63 #include "asterisk/causes.h"
64 #include "asterisk/musiconhold.h"
65 #include "asterisk/app.h"
67 /* ringtones we use */
73 /*! Global jitterbuffer configuration - by default, jb is disabled */
74 static struct ast_jb_conf default_jbconf =
78 .resync_threshold = -1,
81 static struct ast_jb_conf global_jbconf;
84 * Basic mode of operation:
86 * we have one keyboard (which receives commands from the keyboard)
87 * and multiple headset's connected to audio cards.
88 * Cards/Headsets are named as the sections of oss.conf.
89 * The section called [general] contains the default parameters.
91 * At any time, the keyboard is attached to one card, and you
92 * can switch among them using the command 'console foo'
93 * where 'foo' is the name of the card you want.
95 * oss.conf parameters are
99 ; General config options, with default values shown.
100 ; You should use one section per device, with [general] being used
101 ; for the first device and also as a template for other devices.
103 ; All but 'debug' can go also in the device-specific sections.
105 ; debug = 0x0 ; misc debug flags, default is 0
107 ; Set the device to use for I/O
110 ; Optional mixer command to run upon startup (e.g. to set
111 ; volume levels, mutes, etc.
114 ; Software mic volume booster (or attenuator), useful for sound
115 ; cards or microphones with poor sensitivity. The volume level
116 ; is in dB, ranging from -20.0 to +20.0
117 ; boost = n ; mic volume boost in dB
119 ; Set the callerid for outgoing calls
120 ; callerid = John Doe <555-1234>
122 ; autoanswer = no ; no autoanswer on call
123 ; autohangup = yes ; hangup when other party closes
124 ; extension = s ; default extension to call
125 ; context = default ; default context for outgoing calls
126 ; language = "" ; default language
128 ; Default Music on Hold class to use when this channel is placed on hold in
129 ; the case that the music class is not set on the channel with
130 ; Set(CHANNEL(musicclass)=whatever) in the dialplan and the peer channel
131 ; putting this one on hold did not suggest a class to use.
133 ; mohinterpret=default
135 ; If you set overridecontext to 'yes', then the whole dial string
136 ; will be interpreted as an extension, which is extremely useful
137 ; to dial SIP, IAX and other extensions which use the '@' character.
138 ; The default is 'no' just for backward compatibility, but the
139 ; suggestion is to change it.
140 ; overridecontext = no ; if 'no', the last @ will start the context
141 ; if 'yes' the whole string is an extension.
143 ; low level device parameters in case you have problems with the
144 ; device driver on your operating system. You should not touch these
145 ; unless you know what you are doing.
146 ; queuesize = 10 ; frames in device driver
147 ; frags = 8 ; argument to SETFRAGMENT
149 ;------------------------------ JITTER BUFFER CONFIGURATION --------------------------
150 ; jbenable = yes ; Enables the use of a jitterbuffer on the receiving side of an
151 ; OSS channel. Defaults to "no". An enabled jitterbuffer will
152 ; be used only if the sending side can create and the receiving
153 ; side can not accept jitter. The OSS channel can't accept jitter,
154 ; thus an enabled jitterbuffer on the receive OSS side will always
155 ; be used if the sending side can create jitter.
157 ; jbmaxsize = 200 ; Max length of the jitterbuffer in milliseconds.
159 ; jbresyncthreshold = 1000 ; Jump in the frame timestamps over which the jitterbuffer is
160 ; resynchronized. Useful to improve the quality of the voice, with
161 ; big jumps in/broken timestamps, usualy sent from exotic devices
162 ; and programs. Defaults to 1000.
164 ; jbimpl = fixed ; Jitterbuffer implementation, used on the receiving side of an OSS
165 ; channel. Two implementations are currenlty available - "fixed"
166 ; (with size always equals to jbmax-size) and "adaptive" (with
167 ; variable size, actually the new jb of IAX2). Defaults to fixed.
169 ; jblog = no ; Enables jitterbuffer frame logging. Defaults to "no".
170 ;-----------------------------------------------------------------------------------
173 ; device = /dev/dsp1 ; alternate device
177 .. and so on for the other cards.
182 * Helper macros to parse config arguments. They will go in a common
183 * header file if their usage is globally accepted. In the meantime,
184 * we define them here. Typical usage is as below.
185 * Remember to open a block right before M_START (as it declares
186 * some variables) and use the M_* macros WITHOUT A SEMICOLON:
189 * M_START(v->name, v->value)
191 * M_BOOL("dothis", x->flag1)
192 * M_STR("name", x->somestring)
193 * M_F("bar", some_c_code)
194 * M_END(some_final_statement)
195 * ... other code in the block
198 * XXX NOTE these macros should NOT be replicated in other parts of asterisk.
199 * Likely we will come up with a better way of doing config file parsing.
201 #define M_START(var, val) \
202 const char *__s = var; const char *__val = val;
204 #define M_F(tag, f) if (!strcasecmp((__s), tag)) { f; } else
205 #define M_BOOL(tag, dst) M_F(tag, (dst) = ast_true(__val) )
206 #define M_UINT(tag, dst) M_F(tag, (dst) = strtoul(__val, NULL, 0) )
207 #define M_STR(tag, dst) M_F(tag, ast_copy_string(dst, __val, sizeof(dst)))
210 * The following parameters are used in the driver:
212 * FRAME_SIZE the size of an audio frame, in samples.
213 * 160 is used almost universally, so you should not change it.
215 * FRAGS the argument for the SETFRAGMENT ioctl.
216 * Overridden by the 'frags' parameter in oss.conf
218 * Bits 0-7 are the base-2 log of the device's block size,
219 * bits 16-31 are the number of blocks in the driver's queue.
220 * There are a lot of differences in the way this parameter
221 * is supported by different drivers, so you may need to
222 * experiment a bit with the value.
223 * A good default for linux is 30 blocks of 64 bytes, which
224 * results in 6 frames of 320 bytes (160 samples).
225 * FreeBSD works decently with blocks of 256 or 512 bytes,
226 * leaving the number unspecified.
227 * Note that this only refers to the device buffer size,
228 * this module will then try to keep the lenght of audio
229 * buffered within small constraints.
231 * QUEUE_SIZE The max number of blocks actually allowed in the device
232 * driver's buffer, irrespective of the available number.
233 * Overridden by the 'queuesize' parameter in oss.conf
235 * Should be >=2, and at most as large as the hw queue above
236 * (otherwise it will never be full).
239 #define FRAME_SIZE 160
240 #define QUEUE_SIZE 10
242 #if defined(__FreeBSD__)
245 #define FRAGS ( ( (6 * 5) << 16 ) | 0x6 )
249 * XXX text message sizes are probably 256 chars, but i am
250 * not sure if there is a suitable definition anywhere.
252 #define TEXT_SIZE 256
255 #define TRYOPEN 1 /* try to open on startup */
257 #define O_CLOSE 0x444 /* special 'close' mode for device */
258 /* Which device to use */
259 #if defined( __OpenBSD__ ) || defined( __NetBSD__ )
260 #define DEV_DSP "/dev/audio"
262 #define DEV_DSP "/dev/dsp"
266 #define MIN(a,b) ((a) < (b) ? (a) : (b))
269 #define MAX(a,b) ((a) > (b) ? (a) : (b))
272 static char *config = "oss.conf"; /* default config file */
274 static int oss_debug;
277 * Each sound is made of 'datalen' samples of sound, repeated as needed to
278 * generate 'samplen' samples of data, then followed by 'silencelen' samples
279 * of silence. The loop is repeated if 'repeat' is set.
291 static struct sound sounds[] = {
292 { AST_CONTROL_RINGING, "RINGING", ringtone, sizeof(ringtone)/2, 16000, 32000, 1 },
293 { AST_CONTROL_BUSY, "BUSY", busy, sizeof(busy)/2, 4000, 4000, 1 },
294 { AST_CONTROL_CONGESTION, "CONGESTION", busy, sizeof(busy)/2, 2000, 2000, 1 },
295 { AST_CONTROL_RING, "RING10", ring10, sizeof(ring10)/2, 16000, 32000, 1 },
296 { AST_CONTROL_ANSWER, "ANSWER", answer, sizeof(answer)/2, 2200, 0, 0 },
297 { -1, NULL, 0, 0, 0, 0 }, /* end marker */
300 struct video_desc; /* opaque type for video support */
303 * \brief descriptor for one of our channels.
305 * There is one used for 'default' values (from the [general] entry in
306 * the configuration file), and then one instance for each device
307 * (the default is cloned from [general], others are only created
308 * if the relevant section exists).
310 struct chan_oss_pvt {
311 struct chan_oss_pvt *next;
315 * cursound indicates which in struct sound we play. -1 means nothing,
316 * any other value is a valid sound, in which case sampsent indicates
317 * the next sample to send in [0..samplen + silencelen]
318 * nosound is set to disable the audio data from the channel
319 * (so we can play the tones etc.).
321 int sndcmd[2]; /*!< Sound command pipe */
322 int cursound; /*!< index of sound to send */
323 int sampsent; /*!< # of sound samples sent */
324 int nosound; /*!< set to block audio from the PBX */
326 int total_blocks; /*!< total blocks in the output device */
328 enum { M_UNSET, M_FULL, M_READ, M_WRITE } duplex;
332 char *mixer_cmd; /*!< initial command to issue to the mixer */
333 unsigned int queuesize; /*!< max fragments in queue */
334 unsigned int frags; /*!< parameter for SETFRAGMENT */
336 int warned; /*!< various flags used for warnings */
337 #define WARN_used_blocks 1
340 int w_errors; /*!< overfull in the write path */
341 struct timeval lastopen;
346 /*! boost support. BOOST_SCALE * 10 ^(BOOST_MAX/20) must
347 * be representable in 16 bits to avoid overflows.
349 #define BOOST_SCALE (1<<9)
350 #define BOOST_MAX 40 /*!< slightly less than 7 bits */
351 int boost; /*!< input boost, scaled by BOOST_SCALE */
352 char device[64]; /*!< device to open */
356 struct ast_channel *owner;
358 struct video_desc *env; /*!< parameters for video support */
360 char ext[AST_MAX_EXTENSION];
361 char ctx[AST_MAX_CONTEXT];
362 char language[MAX_LANGUAGE];
363 char cid_name[256]; /*XXX */
364 char cid_num[256]; /*XXX */
365 char mohinterpret[MAX_MUSICCLASS];
367 /*! buffers used in oss_write */
368 char oss_write_buf[FRAME_SIZE * 2];
370 /*! buffers used in oss_read - AST_FRIENDLY_OFFSET space for headers
371 * plus enough room for a full frame
373 char oss_read_buf[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
374 int readpos; /*!< read position above */
375 struct ast_frame read_f; /*!< returned by oss_read */
378 /*! forward declaration */
379 static struct chan_oss_pvt *find_desc(char *dev);
381 /*! \brief return the pointer to the video descriptor */
382 static attribute_unused struct video_desc *get_video_desc(struct ast_channel *c)
384 struct chan_oss_pvt *o = c->tech_pvt;
385 return o ? o->env : NULL;
387 static struct chan_oss_pvt oss_default = {
390 .duplex = M_UNSET, /* XXX check this */
393 .queuesize = QUEUE_SIZE,
397 .readpos = AST_FRIENDLY_OFFSET, /* start here on reads */
398 .lastopen = { 0, 0 },
399 .boost = BOOST_SCALE,
402 static char *oss_active; /*!< the active device */
404 static int setformat(struct chan_oss_pvt *o, int mode);
406 static struct ast_channel *oss_request(const char *type, int format, void *data
408 static int oss_digit_begin(struct ast_channel *c, char digit);
409 static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration);
410 static int oss_text(struct ast_channel *c, const char *text);
411 static int oss_hangup(struct ast_channel *c);
412 static int oss_answer(struct ast_channel *c);
413 static struct ast_frame *oss_read(struct ast_channel *chan);
414 static int oss_call(struct ast_channel *c, char *dest, int timeout);
415 static int oss_write(struct ast_channel *chan, struct ast_frame *f);
416 static int oss_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
417 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
418 static char tdesc[] = "OSS Console Channel Driver";
420 #ifdef HAVE_VIDEO_CONSOLE
421 #include "console_video.c"
423 #define CONSOLE_VIDEO_CMDS \
425 /* provide replacements for some symbols used */
426 #define console_write_video NULL
427 #define console_video_start(x, y) {}
428 #define console_video_uninit(x) {}
429 #define console_video_config(x, y, z) 1 /* pretend nothing recognised */
430 #define console_video_cli(x, y, z) 0 /* pretend nothing recognised */
431 #define CONSOLE_FORMAT_VIDEO 0
434 static const struct ast_channel_tech oss_tech = {
436 .description = tdesc,
437 .capabilities = AST_FORMAT_SLINEAR | CONSOLE_FORMAT_VIDEO,
438 .requester = oss_request,
439 .send_digit_begin = oss_digit_begin,
440 .send_digit_end = oss_digit_end,
441 .send_text = oss_text,
442 .hangup = oss_hangup,
443 .answer = oss_answer,
447 .write_video = console_write_video,
448 .indicate = oss_indicate,
453 * \brief returns a pointer to the descriptor with the given name
455 static struct chan_oss_pvt *find_desc(char *dev)
457 struct chan_oss_pvt *o = NULL;
460 ast_log(LOG_WARNING, "null dev\n");
462 for (o = oss_default.next; o && o->name && dev && strcmp(o->name, dev) != 0; o = o->next);
465 ast_log(LOG_WARNING, "could not find <%s>\n", dev ? dev : "--no-device--");
471 * \brief split a string in extension-context, returns pointers to malloc'ed
474 * If we do not have 'overridecontext' then the last @ is considered as
475 * a context separator, and the context is overridden.
476 * This is usually not very necessary as you can play with the dialplan,
477 * and it is nice not to need it because you have '@' in SIP addresses.
479 * \return the buffer address.
481 static char *ast_ext_ctx(const char *src, char **ext, char **ctx)
483 struct chan_oss_pvt *o = find_desc(oss_active);
485 if (ext == NULL || ctx == NULL)
486 return NULL; /* error */
490 if (src && *src != '\0')
491 *ext = ast_strdup(src);
496 if (!o->overridecontext) {
497 /* parse from the right */
498 *ctx = strrchr(*ext, '@');
507 * \brief Returns the number of blocks used in the audio output channel
509 static int used_blocks(struct chan_oss_pvt *o)
511 struct audio_buf_info info;
513 if (ioctl(o->sounddev, SNDCTL_DSP_GETOSPACE, &info)) {
514 if (!(o->warned & WARN_used_blocks)) {
515 ast_log(LOG_WARNING, "Error reading output space\n");
516 o->warned |= WARN_used_blocks;
521 if (o->total_blocks == 0) {
522 if (0) /* debugging */
523 ast_log(LOG_WARNING, "fragtotal %d size %d avail %d\n", info.fragstotal, info.fragsize, info.fragments);
524 o->total_blocks = info.fragments;
527 return o->total_blocks - info.fragments;
530 /*! Write an exactly FRAME_SIZE sized frame */
531 static int soundcard_writeframe(struct chan_oss_pvt *o, short *data)
536 setformat(o, O_RDWR);
538 return 0; /* not fatal */
540 * Nothing complex to manage the audio device queue.
541 * If the buffer is full just drop the extra, otherwise write.
542 * XXX in some cases it might be useful to write anyways after
543 * a number of failures, to restart the output chain.
545 res = used_blocks(o);
546 if (res > o->queuesize) { /* no room to write a block */
547 if (o->w_errors++ == 0 && (oss_debug & 0x4))
548 ast_log(LOG_WARNING, "write: used %d blocks (%d)\n", res, o->w_errors);
552 return write(o->sounddev, (void *)data, FRAME_SIZE * 2);
556 * \brief Handler for 'sound writable' events from the sound thread.
558 * Builds a frame from the high level description of the sounds,
559 * and passes it to the audio device.
560 * The actual sound is made of 1 or more sequences of sound samples
561 * (s->datalen, repeated to make s->samplen samples) followed by
562 * s->silencelen samples of silence. The position in the sequence is stored
563 * in o->sampsent, which goes between 0 .. s->samplen+s->silencelen.
564 * In case we fail to write a frame, don't update o->sampsent.
566 static void send_sound(struct chan_oss_pvt *o)
568 short myframe[FRAME_SIZE];
570 int l_sampsent = o->sampsent;
573 if (o->cursound < 0) /* no sound to send */
576 s = &sounds[o->cursound];
578 for (ofs = 0; ofs < FRAME_SIZE; ofs += l) {
579 l = s->samplen - l_sampsent; /* # of available samples */
581 start = l_sampsent % s->datalen; /* source offset */
582 l = MIN(l, FRAME_SIZE - ofs); /* don't overflow the frame */
583 l = MIN(l, s->datalen - start); /* don't overflow the source */
584 bcopy(s->data + start, myframe + ofs, l * 2);
586 ast_log(LOG_WARNING, "send_sound sound %d/%d of %d into %d\n", l_sampsent, l, s->samplen, ofs);
588 } else { /* end of samples, maybe some silence */
589 static const short silence[FRAME_SIZE] = { 0, };
593 l = MIN(l, FRAME_SIZE - ofs);
594 bcopy(silence, myframe + ofs, l * 2);
596 } else { /* silence is over, restart sound if loop */
597 if (s->repeat == 0) { /* last block */
599 o->nosound = 0; /* allow audio data */
600 if (ofs < FRAME_SIZE) /* pad with silence */
601 bcopy(silence, myframe + ofs, (FRAME_SIZE - ofs) * 2);
607 l = soundcard_writeframe(o, myframe);
609 o->sampsent = l_sampsent; /* update status */
612 static void *sound_thread(void *arg)
615 struct chan_oss_pvt *o = (struct chan_oss_pvt *) arg;
618 * Just in case, kick the driver by trying to read from it.
619 * Ignore errors - this read is almost guaranteed to fail.
621 read(o->sounddev, ign, sizeof(ign));
625 struct timeval *to = NULL, t;
629 FD_SET(o->sndcmd[0], &rfds);
630 maxfd = o->sndcmd[0]; /* pipe from the main process */
631 if (o->cursound > -1 && o->sounddev < 0)
632 setformat(o, O_RDWR); /* need the channel, try to reopen */
633 else if (o->cursound == -1 && o->owner == NULL)
634 setformat(o, O_CLOSE); /* can close */
635 if (o->sounddev > -1) {
636 if (!o->owner) { /* no one owns the audio, so we must drain it */
637 FD_SET(o->sounddev, &rfds);
638 maxfd = MAX(o->sounddev, maxfd);
640 if (o->cursound > -1) {
642 * We would like to use select here, but the device
643 * is always writable, so this would become busy wait.
644 * So we rather set a timeout to 1/2 of the frame size.
647 t.tv_usec = (1000000 * FRAME_SIZE) / (5 * DEFAULT_SAMPLE_RATE);
651 /* ast_select emulates linux behaviour in terms of timeout handling */
652 res = ast_select(maxfd + 1, &rfds, &wfds, NULL, to);
654 ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
658 if (FD_ISSET(o->sndcmd[0], &rfds)) {
659 /* read which sound to play from the pipe */
662 read(o->sndcmd[0], &what, sizeof(what));
663 for (i = 0; sounds[i].ind != -1; i++) {
664 if (sounds[i].ind == what) {
667 o->nosound = 1; /* block audio from pbx */
671 if (sounds[i].ind == -1)
672 ast_log(LOG_WARNING, "invalid sound index: %d\n", what);
674 if (o->sounddev > -1) {
675 if (FD_ISSET(o->sounddev, &rfds)) /* read and ignore errors */
676 read(o->sounddev, ign, sizeof(ign));
677 if (to != NULL) /* maybe it is possible to write */
681 return NULL; /* Never reached */
685 * reset and close the device if opened,
686 * then open and initialize it in the desired mode,
687 * trigger reads and writes so we can start using it.
689 static int setformat(struct chan_oss_pvt *o, int mode)
691 int fmt, desired, res, fd;
693 if (o->sounddev >= 0) {
694 ioctl(o->sounddev, SNDCTL_DSP_RESET, 0);
699 if (mode == O_CLOSE) /* we are done */
701 if (ast_tvdiff_ms(ast_tvnow(), o->lastopen) < 1000)
702 return -1; /* don't open too often */
703 o->lastopen = ast_tvnow();
704 fd = o->sounddev = open(o->device, mode | O_NONBLOCK);
706 ast_log(LOG_WARNING, "Unable to re-open DSP device %s: %s\n", o->device, strerror(errno));
710 ast_channel_set_fd(o->owner, 0, fd);
712 #if __BYTE_ORDER == __LITTLE_ENDIAN
717 res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
719 ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
724 res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
725 /* Check to see if duplex set (FreeBSD Bug) */
726 res = ioctl(fd, SNDCTL_DSP_GETCAPS, &fmt);
727 if (res == 0 && (fmt & DSP_CAP_DUPLEX)) {
728 ast_verb(2, "Console is full duplex\n");
743 res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
745 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
748 fmt = desired = DEFAULT_SAMPLE_RATE; /* 8000 Hz desired */
749 res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
752 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
755 if (fmt != desired) {
756 if (!(o->warned & WARN_speed)) {
758 "Requested %d Hz, got %d Hz -- sound may be choppy\n",
760 o->warned |= WARN_speed;
764 * on Freebsd, SETFRAGMENT does not work very well on some cards.
765 * Default to use 256 bytes, let the user override
769 res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
771 if (!(o->warned & WARN_frag)) {
773 "Unable to set fragment size -- sound may be choppy\n");
774 o->warned |= WARN_frag;
778 /* on some cards, we need SNDCTL_DSP_SETTRIGGER to start outputting */
779 res = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT;
780 res = ioctl(fd, SNDCTL_DSP_SETTRIGGER, &res);
781 /* it may fail if we are in half duplex, never mind */
786 * some of the standard methods supported by channels.
788 static int oss_digit_begin(struct ast_channel *c, char digit)
793 static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration)
795 /* no better use for received digits than print them */
796 ast_verbose(" << Console Received digit %c of duration %u ms >> \n",
801 static int oss_text(struct ast_channel *c, const char *text)
803 /* print received messages */
804 ast_verbose(" << Console Received text %s >> \n", text);
808 /*! \brief Play ringtone 'x' on device 'o' */
809 static void ring(struct chan_oss_pvt *o, int x)
811 write(o->sndcmd[1], &x, sizeof(x));
816 * \brief handler for incoming calls. Either autoanswer, or start ringing
818 static int oss_call(struct ast_channel *c, char *dest, int timeout)
820 struct chan_oss_pvt *o = c->tech_pvt;
821 struct ast_frame f = { 0, };
822 AST_DECLARE_APP_ARGS(args,
826 char *parse = ast_strdupa(dest);
828 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
830 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);
831 if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "answer") == 0) {
832 f.frametype = AST_FRAME_CONTROL;
833 f.subclass = AST_CONTROL_ANSWER;
834 ast_queue_frame(c, &f);
835 } else if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "noanswer") == 0) {
836 f.frametype = AST_FRAME_CONTROL;
837 f.subclass = AST_CONTROL_RINGING;
838 ast_queue_frame(c, &f);
839 ring(o, AST_CONTROL_RING);
840 } else if (o->autoanswer) {
841 ast_verbose(" << Auto-answered >> \n");
842 f.frametype = AST_FRAME_CONTROL;
843 f.subclass = AST_CONTROL_ANSWER;
844 ast_queue_frame(c, &f);
846 ast_verbose("<< Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
847 f.frametype = AST_FRAME_CONTROL;
848 f.subclass = AST_CONTROL_RINGING;
849 ast_queue_frame(c, &f);
850 ring(o, AST_CONTROL_RING);
856 * \brief remote side answered the phone
858 static int oss_answer(struct ast_channel *c)
860 struct chan_oss_pvt *o = c->tech_pvt;
862 ast_verbose(" << Console call has been answered >> \n");
864 /* play an answer tone (XXX do we really need it ?) */
865 ring(o, AST_CONTROL_ANSWER);
867 ast_setstate(c, AST_STATE_UP);
873 static int oss_hangup(struct ast_channel *c)
875 struct chan_oss_pvt *o = c->tech_pvt;
881 ast_verbose(" << Hangup on console >> \n");
882 console_video_uninit(o->env);
883 ast_module_unref(ast_module_info->self);
885 if (o->autoanswer || o->autohangup) {
886 /* Assume auto-hangup too */
888 setformat(o, O_CLOSE);
890 /* Make congestion noise */
891 ring(o, AST_CONTROL_CONGESTION);
897 /*! \brief used for data coming from the network */
898 static int oss_write(struct ast_channel *c, struct ast_frame *f)
901 struct chan_oss_pvt *o = c->tech_pvt;
903 /* Immediately return if no sound is enabled */
906 /* Stop any currently playing sound */
909 * we could receive a block which is not a multiple of our
910 * FRAME_SIZE, so buffer it locally and write to the device
911 * in FRAME_SIZE chunks.
912 * Keep the residue stored for future use.
914 src = 0; /* read position into f->data */
915 while (src < f->datalen) {
916 /* Compute spare room in the buffer */
917 int l = sizeof(o->oss_write_buf) - o->oss_write_dst;
919 if (f->datalen - src >= l) { /* enough to fill a frame */
920 memcpy(o->oss_write_buf + o->oss_write_dst, f->data + src, l);
921 soundcard_writeframe(o, (short *) o->oss_write_buf);
923 o->oss_write_dst = 0;
924 } else { /* copy residue */
925 l = f->datalen - src;
926 memcpy(o->oss_write_buf + o->oss_write_dst, f->data + src, l);
927 src += l; /* but really, we are done */
928 o->oss_write_dst += l;
934 static struct ast_frame *oss_read(struct ast_channel *c)
937 struct chan_oss_pvt *o = c->tech_pvt;
938 struct ast_frame *f = &o->read_f;
940 /* XXX can be simplified returning &ast_null_frame */
941 /* prepare a NULL frame in case we don't have enough data to return */
942 bzero(f, sizeof(struct ast_frame));
943 f->frametype = AST_FRAME_NULL;
944 f->src = oss_tech.type;
946 res = read(o->sounddev, o->oss_read_buf + o->readpos, sizeof(o->oss_read_buf) - o->readpos);
947 if (res < 0) /* audio data not ready, return a NULL frame */
951 if (o->readpos < sizeof(o->oss_read_buf)) /* not enough samples */
957 o->readpos = AST_FRIENDLY_OFFSET; /* reset read pointer for next frame */
958 if (c->_state != AST_STATE_UP) /* drop data if frame is not up */
960 /* ok we can build and deliver the frame to the caller */
961 f->frametype = AST_FRAME_VOICE;
962 f->subclass = AST_FORMAT_SLINEAR;
963 f->samples = FRAME_SIZE;
964 f->datalen = FRAME_SIZE * 2;
965 f->data = o->oss_read_buf + AST_FRIENDLY_OFFSET;
966 if (o->boost != BOOST_SCALE) { /* scale and clip values */
968 int16_t *p = (int16_t *) f->data;
969 for (i = 0; i < f->samples; i++) {
970 x = (p[i] * o->boost) / BOOST_SCALE;
979 f->offset = AST_FRIENDLY_OFFSET;
983 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
985 struct chan_oss_pvt *o = newchan->tech_pvt;
990 static int oss_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen)
992 struct chan_oss_pvt *o = c->tech_pvt;
996 case AST_CONTROL_BUSY:
997 case AST_CONTROL_CONGESTION:
998 case AST_CONTROL_RINGING:
1004 o->nosound = 0; /* when cursound is -1 nosound must be 0 */
1007 case AST_CONTROL_VIDUPDATE:
1011 case AST_CONTROL_HOLD:
1012 ast_verbose(" << Console Has Been Placed on Hold >> \n");
1013 ast_moh_start(c, data, o->mohinterpret);
1016 case AST_CONTROL_UNHOLD:
1017 ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
1022 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, c->name);
1033 * \brief allocate a new channel.
1035 static struct ast_channel *oss_new(struct chan_oss_pvt *o, char *ext, char *ctx, int state)
1037 struct ast_channel *c;
1039 c = ast_channel_alloc(1, state, o->cid_num, o->cid_name, "", ext, ctx, 0, "OSS/%s", o->device + 5);
1042 c->tech = &oss_tech;
1043 if (o->sounddev < 0)
1044 setformat(o, O_RDWR);
1045 ast_channel_set_fd(c, 0, o->sounddev); /* -1 if device closed, override later */
1046 c->nativeformats = AST_FORMAT_SLINEAR;
1047 /* if the console makes the call, add video to the offer */
1048 if (state == AST_STATE_RINGING)
1049 c->nativeformats |= CONSOLE_FORMAT_VIDEO;
1051 c->readformat = AST_FORMAT_SLINEAR;
1052 c->writeformat = AST_FORMAT_SLINEAR;
1055 if (!ast_strlen_zero(o->language))
1056 ast_string_field_set(c, language, o->language);
1057 /* Don't use ast_set_callerid() here because it will
1058 * generate a needless NewCallerID event */
1059 c->cid.cid_ani = ast_strdup(o->cid_num);
1060 if (!ast_strlen_zero(ext))
1061 c->cid.cid_dnid = ast_strdup(ext);
1064 ast_module_ref(ast_module_info->self);
1065 ast_jb_configure(c, &global_jbconf);
1066 if (state != AST_STATE_DOWN) {
1067 if (ast_pbx_start(c)) {
1068 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", c->name);
1070 o->owner = c = NULL;
1071 /* XXX what about the channel itself ? */
1074 console_video_start(get_video_desc(c), c); /* XXX cleanup */
1079 static struct ast_channel *oss_request(const char *type, int format, void *data, int *cause)
1081 struct ast_channel *c;
1082 struct chan_oss_pvt *o;
1083 AST_DECLARE_APP_ARGS(args,
1087 char *parse = ast_strdupa(data);
1089 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
1090 o = find_desc(args.name);
1092 ast_log(LOG_WARNING, "oss_request ty <%s> data 0x%p <%s>\n", type, data, (char *) data);
1094 ast_log(LOG_NOTICE, "Device %s not found\n", args.name);
1095 /* XXX we could default to 'dsp' perhaps ? */
1098 if ((format & AST_FORMAT_SLINEAR) == 0) {
1099 ast_log(LOG_NOTICE, "Format 0x%x unsupported\n", format);
1103 ast_log(LOG_NOTICE, "Already have a call (chan %p) on the OSS channel\n", o->owner);
1104 *cause = AST_CAUSE_BUSY;
1107 c = oss_new(o, NULL, NULL, AST_STATE_DOWN);
1109 ast_log(LOG_WARNING, "Unable to create new OSS channel\n");
1115 static void store_config_core(struct chan_oss_pvt *o, const char *var, const char *value);
1117 /*! Generic console command handler. Basically a wrapper for a subset
1118 * of config file options which are also available from the CLI
1120 static char *console_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1122 struct chan_oss_pvt *o = find_desc(oss_active);
1123 const char *var, *value;
1126 e->command = CONSOLE_VIDEO_CMDS;
1127 e->usage = "Usage: " CONSOLE_VIDEO_CMDS "...\n"
1128 " Generic handler for console commands.\n";
1135 if (a->argc < e->args)
1136 return CLI_SHOWUSAGE;
1138 ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n",
1142 var = a->argv[e->args-1];
1143 value = a->argc > e->args ? a->argv[e->args] : NULL;
1144 if (value) /* handle setting */
1145 store_config_core(o, var, value);
1146 if (console_video_cli(o->env, var, a->fd)) /* print video-related values */
1148 /* handle other values */
1149 if (!strcasecmp(var, "device")) {
1150 ast_cli(a->fd, "device is [%s]\n", o->device);
1155 static char *console_autoanswer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1157 struct chan_oss_pvt *o = find_desc(oss_active);
1161 e->command = "console autoanswer [on|off]";
1163 "Usage: console autoanswer [on|off]\n"
1164 " Enables or disables autoanswer feature. If used without\n"
1165 " argument, displays the current on/off status of autoanswer.\n"
1166 " The default value of autoanswer is in 'oss.conf'.\n";
1173 if (a->argc == e->args - 1) {
1174 ast_cli(a->fd, "Auto answer is %s.\n", o->autoanswer ? "on" : "off");
1177 if (a->argc != e->args)
1178 return CLI_SHOWUSAGE;
1180 ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n",
1184 if (!strcasecmp(a->argv[e->args-1], "on"))
1186 else if (!strcasecmp(a->argv[e->args - 1], "off"))
1189 return CLI_SHOWUSAGE;
1194 * \brief answer command from the console
1196 static char *console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1198 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
1199 struct chan_oss_pvt *o = find_desc(oss_active);
1203 e->command = "console answer";
1205 "Usage: console answer\n"
1206 " Answers an incoming call on the console (OSS) channel.\n";
1210 return NULL; /* no completion */
1212 if (a->argc != e->args)
1213 return CLI_SHOWUSAGE;
1215 ast_cli(a->fd, "No one is calling us\n");
1221 ast_queue_frame(o->owner, &f);
1226 * \brief Console send text CLI command
1228 * \note concatenate all arguments into a single string. argv is NULL-terminated
1229 * so we can use it right away
1231 static char *console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1233 struct chan_oss_pvt *o = find_desc(oss_active);
1234 char buf[TEXT_SIZE];
1236 if (cmd == CLI_INIT) {
1237 e->command = "console send text";
1239 "Usage: console send text <message>\n"
1240 " Sends a text message for display on the remote terminal.\n";
1242 } else if (cmd == CLI_GENERATE)
1245 if (a->argc < e->args + 1)
1246 return CLI_SHOWUSAGE;
1248 ast_cli(a->fd, "Not in a call\n");
1251 ast_join(buf, sizeof(buf) - 1, a->argv + e->args);
1252 if (!ast_strlen_zero(buf)) {
1253 struct ast_frame f = { 0, };
1254 int i = strlen(buf);
1256 f.frametype = AST_FRAME_TEXT;
1260 ast_queue_frame(o->owner, &f);
1265 static char *console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1267 struct chan_oss_pvt *o = find_desc(oss_active);
1269 if (cmd == CLI_INIT) {
1270 e->command = "console hangup";
1272 "Usage: console hangup\n"
1273 " Hangs up any call currently placed on the console.\n";
1275 } else if (cmd == CLI_GENERATE)
1278 if (a->argc != e->args)
1279 return CLI_SHOWUSAGE;
1282 if (!o->owner && !o->hookstate) { /* XXX maybe only one ? */
1283 ast_cli(a->fd, "No call to hang up\n");
1288 ast_queue_hangup(o->owner);
1289 setformat(o, O_CLOSE);
1293 static char *console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1295 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH };
1296 struct chan_oss_pvt *o = find_desc(oss_active);
1298 if (cmd == CLI_INIT) {
1299 e->command = "console flash";
1301 "Usage: console flash\n"
1302 " Flashes the call currently placed on the console.\n";
1304 } else if (cmd == CLI_GENERATE)
1307 if (a->argc != e->args)
1308 return CLI_SHOWUSAGE;
1310 o->nosound = 0; /* when cursound is -1 nosound must be 0 */
1311 if (!o->owner) { /* XXX maybe !o->hookstate too ? */
1312 ast_cli(a->fd, "No call to flash\n");
1316 if (o->owner) /* XXX must be true, right ? */
1317 ast_queue_frame(o->owner, &f);
1321 static char *console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1323 char *s = NULL, *mye = NULL, *myc = NULL;
1324 struct chan_oss_pvt *o = find_desc(oss_active);
1326 if (cmd == CLI_INIT) {
1327 e->command = "console dial";
1329 "Usage: console dial [extension[@context]]\n"
1330 " Dials a given extension (and context if specified)\n";
1332 } else if (cmd == CLI_GENERATE)
1335 if (a->argc > e->args + 1)
1336 return CLI_SHOWUSAGE;
1337 if (o->owner) { /* already in a call */
1339 struct ast_frame f = { AST_FRAME_DTMF, 0 };
1341 if (a->argc == e->args) { /* argument is mandatory here */
1342 ast_cli(a->fd, "Already in a call. You can only dial digits until you hangup.\n");
1345 s = a->argv[e->args];
1346 /* send the string one char at a time */
1347 for (i = 0; i < strlen(s); i++) {
1349 ast_queue_frame(o->owner, &f);
1353 /* if we have an argument split it into extension and context */
1354 if (a->argc == e->args + 1)
1355 s = ast_ext_ctx(a->argv[e->args], &mye, &myc);
1356 /* supply default values if needed */
1361 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
1363 oss_new(o, mye, myc, AST_STATE_RINGING);
1365 ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc);
1371 static char *console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1373 struct chan_oss_pvt *o = find_desc(oss_active);
1376 if (cmd == CLI_INIT) {
1377 e->command = "console {mute|unmute}";
1379 "Usage: console {mute|unmute}\n"
1380 " Mute/unmute the microphone.\n";
1382 } else if (cmd == CLI_GENERATE)
1385 if (a->argc != e->args)
1386 return CLI_SHOWUSAGE;
1387 s = a->argv[e->args-1];
1388 if (!strcasecmp(s, "mute"))
1390 else if (!strcasecmp(s, "unmute"))
1393 return CLI_SHOWUSAGE;
1397 static char *console_transfer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1399 struct chan_oss_pvt *o = find_desc(oss_active);
1400 struct ast_channel *b = NULL;
1401 char *tmp, *ext, *ctx;
1405 e->command = "console transfer";
1407 "Usage: console transfer <extension>[@context]\n"
1408 " Transfers the currently connected call to the given extension (and\n"
1409 " context if specified)\n";
1416 return CLI_SHOWUSAGE;
1419 if (o->owner == NULL || (b = ast_bridged_channel(o->owner)) == NULL) {
1420 ast_cli(a->fd, "There is no call to transfer\n");
1424 tmp = ast_ext_ctx(a->argv[2], &ext, &ctx);
1425 if (ctx == NULL) /* supply default context if needed */
1426 ctx = o->owner->context;
1427 if (!ast_exists_extension(b, ctx, ext, 1, b->cid.cid_num))
1428 ast_cli(a->fd, "No such extension exists\n");
1430 ast_cli(a->fd, "Whee, transferring %s to %s@%s.\n", b->name, ext, ctx);
1431 if (ast_async_goto(b, ctx, ext, 1))
1432 ast_cli(a->fd, "Failed to transfer :(\n");
1439 static char *console_active(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1443 e->command = "console active";
1445 "Usage: console active [device]\n"
1446 " If used without a parameter, displays which device is the current\n"
1447 " console. If a device is specified, the console sound device is changed to\n"
1448 " the device specified.\n";
1455 ast_cli(a->fd, "active console is [%s]\n", oss_active);
1456 else if (a->argc != 3)
1457 return CLI_SHOWUSAGE;
1459 struct chan_oss_pvt *o;
1460 if (strcmp(a->argv[2], "show") == 0) {
1461 for (o = oss_default.next; o; o = o->next)
1462 ast_cli(a->fd, "device [%s] exists\n", o->name);
1465 o = find_desc(a->argv[2]);
1467 ast_cli(a->fd, "No device [%s] exists\n", a->argv[2]);
1469 oss_active = o->name;
1475 * \brief store the boost factor
1477 static void store_boost(struct chan_oss_pvt *o, const char *s)
1480 if (sscanf(s, "%lf", &boost) != 1) {
1481 ast_log(LOG_WARNING, "invalid boost <%s>\n", s);
1484 if (boost < -BOOST_MAX) {
1485 ast_log(LOG_WARNING, "boost %s too small, using %d\n", s, -BOOST_MAX);
1487 } else if (boost > BOOST_MAX) {
1488 ast_log(LOG_WARNING, "boost %s too large, using %d\n", s, BOOST_MAX);
1491 boost = exp(log(10) * boost / 20) * BOOST_SCALE;
1493 ast_log(LOG_WARNING, "setting boost %s to %d\n", s, o->boost);
1496 static char *console_boost(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1498 struct chan_oss_pvt *o = find_desc(oss_active);
1502 e->command = "console boost";
1504 "Usage: console boost [boost in dB]\n"
1505 " Sets or display mic boost in dB\n";
1512 ast_cli(a->fd, "boost currently %5.1f\n", 20 * log10(((double) o->boost / (double) BOOST_SCALE)));
1513 else if (a->argc == 3)
1514 store_boost(o, a->argv[2]);
1518 static struct ast_cli_entry cli_oss[] = {
1519 AST_CLI_DEFINE(console_answer, "Answer an incoming console call"),
1520 AST_CLI_DEFINE(console_hangup, "Hangup a call on the console"),
1521 AST_CLI_DEFINE(console_flash, "Flash a call on the console"),
1522 AST_CLI_DEFINE(console_dial, "Dial an extension on the console"),
1523 AST_CLI_DEFINE(console_mute, "Disable/Enable mic input"),
1524 AST_CLI_DEFINE(console_transfer, "Transfer a call to a different extension"),
1525 AST_CLI_DEFINE(console_cmd, "Generic console command"),
1526 AST_CLI_DEFINE(console_sendtext, "Send text to the remote device"),
1527 AST_CLI_DEFINE(console_autoanswer, "Sets/displays autoanswer"),
1528 AST_CLI_DEFINE(console_boost, "Sets/displays mic boost in dB"),
1529 AST_CLI_DEFINE(console_active, "Sets/displays active console"),
1533 * store the mixer argument from the config file, filtering possibly
1534 * invalid or dangerous values (the string is used as argument for
1535 * system("mixer %s")
1537 static void store_mixer(struct chan_oss_pvt *o, const char *s)
1541 for (i = 0; i < strlen(s); i++) {
1542 if (!isalnum(s[i]) && index(" \t-/", s[i]) == NULL) {
1543 ast_log(LOG_WARNING, "Suspect char %c in mixer cmd, ignoring:\n\t%s\n", s[i], s);
1548 ast_free(o->mixer_cmd);
1549 o->mixer_cmd = ast_strdup(s);
1550 ast_log(LOG_WARNING, "setting mixer %s\n", s);
1554 * store the callerid components
1556 static void store_callerid(struct chan_oss_pvt *o, const char *s)
1558 ast_callerid_split(s, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
1561 static void store_config_core(struct chan_oss_pvt *o, const char *var, const char *value)
1563 M_START(var, value);
1565 /* handle jb conf */
1566 if (!ast_jb_read_conf(&global_jbconf, (char *)var,(char *) value))
1569 if (!console_video_config(&o->env, var, value))
1571 M_BOOL("autoanswer", o->autoanswer)
1572 M_BOOL("autohangup", o->autohangup)
1573 M_BOOL("overridecontext", o->overridecontext)
1574 M_STR("device", o->device)
1575 M_UINT("frags", o->frags)
1576 M_UINT("debug", oss_debug)
1577 M_UINT("queuesize", o->queuesize)
1578 M_STR("context", o->ctx)
1579 M_STR("language", o->language)
1580 M_STR("mohinterpret", o->mohinterpret)
1581 M_STR("extension", o->ext)
1582 M_F("mixer", store_mixer(o, value))
1583 M_F("callerid", store_callerid(o, value))
1584 M_F("boost", store_boost(o, value))
1590 * grab fields from the config file, init the descriptor and open the device.
1592 static struct chan_oss_pvt *store_config(struct ast_config *cfg, char *ctg)
1594 struct ast_variable *v;
1595 struct chan_oss_pvt *o;
1601 if (!(o = ast_calloc(1, sizeof(*o))))
1604 /* "general" is also the default thing */
1605 if (strcmp(ctg, "general") == 0) {
1606 o->name = ast_strdup("dsp");
1607 oss_active = o->name;
1610 o->name = ast_strdup(ctg);
1613 strcpy(o->mohinterpret, "default");
1615 o->lastopen = ast_tvnow(); /* don't leave it 0 or tvdiff may wrap */
1616 /* fill other fields from configuration */
1617 for (v = ast_variable_browse(cfg, ctg); v; v = v->next) {
1618 store_config_core(o, v->name, v->value);
1620 if (ast_strlen_zero(o->device))
1621 ast_copy_string(o->device, DEV_DSP, sizeof(o->device));
1625 asprintf(&cmd, "mixer %s", o->mixer_cmd);
1626 ast_log(LOG_WARNING, "running [%s]\n", cmd);
1630 if (o == &oss_default) /* we are done with the default */
1635 if (setformat(o, O_RDWR) < 0) { /* open device */
1636 ast_verb(1, "Device %s not detected\n", ctg);
1637 ast_verb(1, "Turn off OSS support by adding " "'noload=chan_oss.so' in /etc/asterisk/modules.conf\n");
1640 if (o->duplex != M_FULL)
1641 ast_log(LOG_WARNING, "XXX I don't work right with non " "full-duplex sound cards XXX\n");
1642 #endif /* TRYOPEN */
1643 if (pipe(o->sndcmd) != 0) {
1644 ast_log(LOG_ERROR, "Unable to create pipe\n");
1647 ast_pthread_create_background(&o->sthread, NULL, sound_thread, o);
1648 /* link into list of devices */
1649 if (o != &oss_default) {
1650 o->next = oss_default.next;
1651 oss_default.next = o;
1656 if (o != &oss_default)
1661 static int load_module(void)
1663 struct ast_config *cfg = NULL;
1665 struct ast_flags config_flags = { 0 };
1667 /* Copy the default jb config over global_jbconf */
1668 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
1670 /* load config file */
1671 if (!(cfg = ast_config_load(config, config_flags))) {
1672 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
1673 return AST_MODULE_LOAD_DECLINE;
1677 store_config(cfg, ctg);
1678 } while ( (ctg = ast_category_browse(cfg, ctg)) != NULL);
1680 ast_config_destroy(cfg);
1682 if (find_desc(oss_active) == NULL) {
1683 ast_log(LOG_NOTICE, "Device %s not found\n", oss_active);
1684 /* XXX we could default to 'dsp' perhaps ? */
1685 /* XXX should cleanup allocated memory etc. */
1686 return AST_MODULE_LOAD_FAILURE;
1689 if (ast_channel_register(&oss_tech)) {
1690 ast_log(LOG_ERROR, "Unable to register channel type 'OSS'\n");
1691 return AST_MODULE_LOAD_FAILURE;
1694 ast_cli_register_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry));
1696 return AST_MODULE_LOAD_SUCCESS;
1700 static int unload_module(void)
1702 struct chan_oss_pvt *o;
1704 ast_channel_unregister(&oss_tech);
1705 ast_cli_unregister_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry));
1707 for (o = oss_default.next; o; o = o->next) {
1709 if (o->sndcmd[0] > 0) {
1710 close(o->sndcmd[0]);
1711 close(o->sndcmd[1]);
1714 ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
1715 if (o->owner) /* XXX how ??? */
1717 /* XXX what about the thread ? */
1718 /* XXX what about the memory allocated ? */
1723 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "OSS Console Channel Driver");