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
38 <support_level>extended</support_level>
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__) || defined(__CYGWIN__) || defined(__GLIBC__)
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 #include "console_video.h"
69 /*! Global jitterbuffer configuration - by default, jb is disabled
70 * \note Values shown here match the defaults shown in oss.conf.sample */
71 static struct ast_jb_conf default_jbconf =
75 .resync_threshold = 1000,
79 static struct ast_jb_conf global_jbconf;
82 * Basic mode of operation:
84 * we have one keyboard (which receives commands from the keyboard)
85 * and multiple headset's connected to audio cards.
86 * Cards/Headsets are named as the sections of oss.conf.
87 * The section called [general] contains the default parameters.
89 * At any time, the keyboard is attached to one card, and you
90 * can switch among them using the command 'console foo'
91 * where 'foo' is the name of the card you want.
93 * oss.conf parameters are
97 ; General config options, with default values shown.
98 ; You should use one section per device, with [general] being used
99 ; for the first device and also as a template for other devices.
101 ; All but 'debug' can go also in the device-specific sections.
103 ; debug = 0x0 ; misc debug flags, default is 0
105 ; Set the device to use for I/O
108 ; Optional mixer command to run upon startup (e.g. to set
109 ; volume levels, mutes, etc.
112 ; Software mic volume booster (or attenuator), useful for sound
113 ; cards or microphones with poor sensitivity. The volume level
114 ; is in dB, ranging from -20.0 to +20.0
115 ; boost = n ; mic volume boost in dB
117 ; Set the callerid for outgoing calls
118 ; callerid = John Doe <555-1234>
120 ; autoanswer = no ; no autoanswer on call
121 ; autohangup = yes ; hangup when other party closes
122 ; extension = s ; default extension to call
123 ; context = default ; default context for outgoing calls
124 ; language = "" ; default language
126 ; Default Music on Hold class to use when this channel is placed on hold in
127 ; the case that the music class is not set on the channel with
128 ; Set(CHANNEL(musicclass)=whatever) in the dialplan and the peer channel
129 ; putting this one on hold did not suggest a class to use.
131 ; mohinterpret=default
133 ; If you set overridecontext to 'yes', then the whole dial string
134 ; will be interpreted as an extension, which is extremely useful
135 ; to dial SIP, IAX and other extensions which use the '@' character.
136 ; The default is 'no' just for backward compatibility, but the
137 ; suggestion is to change it.
138 ; overridecontext = no ; if 'no', the last @ will start the context
139 ; if 'yes' the whole string is an extension.
141 ; low level device parameters in case you have problems with the
142 ; device driver on your operating system. You should not touch these
143 ; unless you know what you are doing.
144 ; queuesize = 10 ; frames in device driver
145 ; frags = 8 ; argument to SETFRAGMENT
147 ;------------------------------ JITTER BUFFER CONFIGURATION --------------------------
148 ; jbenable = yes ; Enables the use of a jitterbuffer on the receiving side of an
149 ; OSS channel. Defaults to "no". An enabled jitterbuffer will
150 ; be used only if the sending side can create and the receiving
151 ; side can not accept jitter. The OSS channel can't accept jitter,
152 ; thus an enabled jitterbuffer on the receive OSS side will always
153 ; be used if the sending side can create jitter.
155 ; jbmaxsize = 200 ; Max length of the jitterbuffer in milliseconds.
157 ; jbresyncthreshold = 1000 ; Jump in the frame timestamps over which the jitterbuffer is
158 ; resynchronized. Useful to improve the quality of the voice, with
159 ; big jumps in/broken timestamps, usualy sent from exotic devices
160 ; and programs. Defaults to 1000.
162 ; jbimpl = fixed ; Jitterbuffer implementation, used on the receiving side of an OSS
163 ; channel. Two implementations are currenlty available - "fixed"
164 ; (with size always equals to jbmax-size) and "adaptive" (with
165 ; variable size, actually the new jb of IAX2). Defaults to fixed.
167 ; jblog = no ; Enables jitterbuffer frame logging. Defaults to "no".
168 ;-----------------------------------------------------------------------------------
171 ; device = /dev/dsp1 ; alternate device
175 .. and so on for the other cards.
180 * The following parameters are used in the driver:
182 * FRAME_SIZE the size of an audio frame, in samples.
183 * 160 is used almost universally, so you should not change it.
185 * FRAGS the argument for the SETFRAGMENT ioctl.
186 * Overridden by the 'frags' parameter in oss.conf
188 * Bits 0-7 are the base-2 log of the device's block size,
189 * bits 16-31 are the number of blocks in the driver's queue.
190 * There are a lot of differences in the way this parameter
191 * is supported by different drivers, so you may need to
192 * experiment a bit with the value.
193 * A good default for linux is 30 blocks of 64 bytes, which
194 * results in 6 frames of 320 bytes (160 samples).
195 * FreeBSD works decently with blocks of 256 or 512 bytes,
196 * leaving the number unspecified.
197 * Note that this only refers to the device buffer size,
198 * this module will then try to keep the lenght of audio
199 * buffered within small constraints.
201 * QUEUE_SIZE The max number of blocks actually allowed in the device
202 * driver's buffer, irrespective of the available number.
203 * Overridden by the 'queuesize' parameter in oss.conf
205 * Should be >=2, and at most as large as the hw queue above
206 * (otherwise it will never be full).
209 #define FRAME_SIZE 160
210 #define QUEUE_SIZE 10
212 #if defined(__FreeBSD__)
215 #define FRAGS ( ( (6 * 5) << 16 ) | 0x6 )
219 * XXX text message sizes are probably 256 chars, but i am
220 * not sure if there is a suitable definition anywhere.
222 #define TEXT_SIZE 256
225 #define TRYOPEN 1 /* try to open on startup */
227 #define O_CLOSE 0x444 /* special 'close' mode for device */
228 /* Which device to use */
229 #if defined( __OpenBSD__ ) || defined( __NetBSD__ )
230 #define DEV_DSP "/dev/audio"
232 #define DEV_DSP "/dev/dsp"
235 static char *config = "oss.conf"; /* default config file */
237 static int oss_debug;
240 * \brief descriptor for one of our channels.
242 * There is one used for 'default' values (from the [general] entry in
243 * the configuration file), and then one instance for each device
244 * (the default is cloned from [general], others are only created
245 * if the relevant section exists).
247 struct chan_oss_pvt {
248 struct chan_oss_pvt *next;
251 int total_blocks; /*!< total blocks in the output device */
253 enum { M_UNSET, M_FULL, M_READ, M_WRITE } duplex;
254 int autoanswer; /*!< Boolean: whether to answer the immediately upon calling */
255 int autohangup; /*!< Boolean: whether to hangup the call when the remote end hangs up */
256 int hookstate; /*!< Boolean: 1 if offhook; 0 if onhook */
257 char *mixer_cmd; /*!< initial command to issue to the mixer */
258 unsigned int queuesize; /*!< max fragments in queue */
259 unsigned int frags; /*!< parameter for SETFRAGMENT */
261 int warned; /*!< various flags used for warnings */
262 #define WARN_used_blocks 1
265 int w_errors; /*!< overfull in the write path */
266 struct timeval lastopen;
271 /*! boost support. BOOST_SCALE * 10 ^(BOOST_MAX/20) must
272 * be representable in 16 bits to avoid overflows.
274 #define BOOST_SCALE (1<<9)
275 #define BOOST_MAX 40 /*!< slightly less than 7 bits */
276 int boost; /*!< input boost, scaled by BOOST_SCALE */
277 char device[64]; /*!< device to open */
281 struct ast_channel *owner;
283 struct video_desc *env; /*!< parameters for video support */
285 char ext[AST_MAX_EXTENSION];
286 char ctx[AST_MAX_CONTEXT];
287 char language[MAX_LANGUAGE];
288 char cid_name[256]; /*!< Initial CallerID name */
289 char cid_num[256]; /*!< Initial CallerID number */
290 char mohinterpret[MAX_MUSICCLASS];
292 /*! buffers used in oss_write */
293 char oss_write_buf[FRAME_SIZE * 2];
295 /*! buffers used in oss_read - AST_FRIENDLY_OFFSET space for headers
296 * plus enough room for a full frame
298 char oss_read_buf[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
299 int readpos; /*!< read position above */
300 struct ast_frame read_f; /*!< returned by oss_read */
303 /*! forward declaration */
304 static struct chan_oss_pvt *find_desc(const char *dev);
306 static char *oss_active; /*!< the active device */
308 /*! \brief return the pointer to the video descriptor */
309 struct video_desc *get_video_desc(struct ast_channel *c)
311 struct chan_oss_pvt *o = c ? c->tech_pvt : find_desc(oss_active);
312 return o ? o->env : NULL;
314 static struct chan_oss_pvt oss_default = {
316 .duplex = M_UNSET, /* XXX check this */
319 .queuesize = QUEUE_SIZE,
323 .readpos = AST_FRIENDLY_OFFSET, /* start here on reads */
324 .lastopen = { 0, 0 },
325 .boost = BOOST_SCALE,
329 static int setformat(struct chan_oss_pvt *o, int mode);
331 static struct ast_channel *oss_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor,
332 void *data, int *cause);
333 static int oss_digit_begin(struct ast_channel *c, char digit);
334 static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration);
335 static int oss_text(struct ast_channel *c, const char *text);
336 static int oss_hangup(struct ast_channel *c);
337 static int oss_answer(struct ast_channel *c);
338 static struct ast_frame *oss_read(struct ast_channel *chan);
339 static int oss_call(struct ast_channel *c, char *dest, int timeout);
340 static int oss_write(struct ast_channel *chan, struct ast_frame *f);
341 static int oss_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
342 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
343 static char tdesc[] = "OSS Console Channel Driver";
345 /* cannot do const because need to update some fields at runtime */
346 static struct ast_channel_tech oss_tech = {
348 .description = tdesc,
349 .requester = oss_request,
350 .send_digit_begin = oss_digit_begin,
351 .send_digit_end = oss_digit_end,
352 .send_text = oss_text,
353 .hangup = oss_hangup,
354 .answer = oss_answer,
358 .write_video = console_write_video,
359 .indicate = oss_indicate,
364 * \brief returns a pointer to the descriptor with the given name
366 static struct chan_oss_pvt *find_desc(const char *dev)
368 struct chan_oss_pvt *o = NULL;
371 ast_log(LOG_WARNING, "null dev\n");
373 for (o = oss_default.next; o && o->name && dev && strcmp(o->name, dev) != 0; o = o->next);
376 ast_log(LOG_WARNING, "could not find <%s>\n", dev ? dev : "--no-device--");
382 * \brief split a string in extension-context, returns pointers to malloc'ed
385 * If we do not have 'overridecontext' then the last @ is considered as
386 * a context separator, and the context is overridden.
387 * This is usually not very necessary as you can play with the dialplan,
388 * and it is nice not to need it because you have '@' in SIP addresses.
390 * \return the buffer address.
392 static char *ast_ext_ctx(const char *src, char **ext, char **ctx)
394 struct chan_oss_pvt *o = find_desc(oss_active);
396 if (ext == NULL || ctx == NULL)
397 return NULL; /* error */
401 if (src && *src != '\0')
402 *ext = ast_strdup(src);
407 if (!o->overridecontext) {
408 /* parse from the right */
409 *ctx = strrchr(*ext, '@');
418 * \brief Returns the number of blocks used in the audio output channel
420 static int used_blocks(struct chan_oss_pvt *o)
422 struct audio_buf_info info;
424 if (ioctl(o->sounddev, SNDCTL_DSP_GETOSPACE, &info)) {
425 if (!(o->warned & WARN_used_blocks)) {
426 ast_log(LOG_WARNING, "Error reading output space\n");
427 o->warned |= WARN_used_blocks;
432 if (o->total_blocks == 0) {
433 if (0) /* debugging */
434 ast_log(LOG_WARNING, "fragtotal %d size %d avail %d\n", info.fragstotal, info.fragsize, info.fragments);
435 o->total_blocks = info.fragments;
438 return o->total_blocks - info.fragments;
441 /*! Write an exactly FRAME_SIZE sized frame */
442 static int soundcard_writeframe(struct chan_oss_pvt *o, short *data)
447 setformat(o, O_RDWR);
449 return 0; /* not fatal */
451 * Nothing complex to manage the audio device queue.
452 * If the buffer is full just drop the extra, otherwise write.
453 * XXX in some cases it might be useful to write anyways after
454 * a number of failures, to restart the output chain.
456 res = used_blocks(o);
457 if (res > o->queuesize) { /* no room to write a block */
458 if (o->w_errors++ == 0 && (oss_debug & 0x4))
459 ast_log(LOG_WARNING, "write: used %d blocks (%d)\n", res, o->w_errors);
463 return write(o->sounddev, (void *)data, FRAME_SIZE * 2);
467 * reset and close the device if opened,
468 * then open and initialize it in the desired mode,
469 * trigger reads and writes so we can start using it.
471 static int setformat(struct chan_oss_pvt *o, int mode)
473 int fmt, desired, res, fd;
475 if (o->sounddev >= 0) {
476 ioctl(o->sounddev, SNDCTL_DSP_RESET, 0);
481 if (mode == O_CLOSE) /* we are done */
483 if (ast_tvdiff_ms(ast_tvnow(), o->lastopen) < 1000)
484 return -1; /* don't open too often */
485 o->lastopen = ast_tvnow();
486 fd = o->sounddev = open(o->device, mode | O_NONBLOCK);
488 ast_log(LOG_WARNING, "Unable to re-open DSP device %s: %s\n", o->device, strerror(errno));
492 ast_channel_set_fd(o->owner, 0, fd);
494 #if __BYTE_ORDER == __LITTLE_ENDIAN
499 res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
501 ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
506 res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
507 /* Check to see if duplex set (FreeBSD Bug) */
508 res = ioctl(fd, SNDCTL_DSP_GETCAPS, &fmt);
509 if (res == 0 && (fmt & DSP_CAP_DUPLEX)) {
510 ast_verb(2, "Console is full duplex\n");
525 res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
527 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
530 fmt = desired = DEFAULT_SAMPLE_RATE; /* 8000 Hz desired */
531 res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
534 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
537 if (fmt != desired) {
538 if (!(o->warned & WARN_speed)) {
540 "Requested %d Hz, got %d Hz -- sound may be choppy\n",
542 o->warned |= WARN_speed;
546 * on Freebsd, SETFRAGMENT does not work very well on some cards.
547 * Default to use 256 bytes, let the user override
551 res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
553 if (!(o->warned & WARN_frag)) {
555 "Unable to set fragment size -- sound may be choppy\n");
556 o->warned |= WARN_frag;
560 /* on some cards, we need SNDCTL_DSP_SETTRIGGER to start outputting */
561 res = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT;
562 res = ioctl(fd, SNDCTL_DSP_SETTRIGGER, &res);
563 /* it may fail if we are in half duplex, never mind */
568 * some of the standard methods supported by channels.
570 static int oss_digit_begin(struct ast_channel *c, char digit)
575 static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration)
577 /* no better use for received digits than print them */
578 ast_verbose(" << Console Received digit %c of duration %u ms >> \n",
583 static int oss_text(struct ast_channel *c, const char *text)
585 /* print received messages */
586 ast_verbose(" << Console Received text %s >> \n", text);
591 * \brief handler for incoming calls. Either autoanswer, or start ringing
593 static int oss_call(struct ast_channel *c, char *dest, int timeout)
595 struct chan_oss_pvt *o = c->tech_pvt;
596 struct ast_frame f = { AST_FRAME_CONTROL, };
597 AST_DECLARE_APP_ARGS(args,
601 char *parse = ast_strdupa(dest);
603 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
605 ast_verbose(" << Call to device '%s' dnid '%s' rdnis '%s' on console from '%s' <%s> >>\n",
607 S_OR(c->dialed.number.str, ""),
608 S_COR(c->redirecting.from.number.valid, c->redirecting.from.number.str, ""),
609 S_COR(c->caller.id.name.valid, c->caller.id.name.str, ""),
610 S_COR(c->caller.id.number.valid, c->caller.id.number.str, ""));
611 if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "answer") == 0) {
612 f.subclass.integer = AST_CONTROL_ANSWER;
613 ast_queue_frame(c, &f);
614 } else if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "noanswer") == 0) {
615 f.subclass.integer = AST_CONTROL_RINGING;
616 ast_queue_frame(c, &f);
617 ast_indicate(c, AST_CONTROL_RINGING);
618 } else if (o->autoanswer) {
619 ast_verbose(" << Auto-answered >> \n");
620 f.subclass.integer = AST_CONTROL_ANSWER;
621 ast_queue_frame(c, &f);
624 ast_verbose("<< Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
625 f.subclass.integer = AST_CONTROL_RINGING;
626 ast_queue_frame(c, &f);
627 ast_indicate(c, AST_CONTROL_RINGING);
633 * \brief remote side answered the phone
635 static int oss_answer(struct ast_channel *c)
637 struct chan_oss_pvt *o = c->tech_pvt;
638 ast_verbose(" << Console call has been answered >> \n");
639 ast_setstate(c, AST_STATE_UP);
644 static int oss_hangup(struct ast_channel *c)
646 struct chan_oss_pvt *o = c->tech_pvt;
650 ast_verbose(" << Hangup on console >> \n");
651 console_video_uninit(o->env);
652 ast_module_unref(ast_module_info->self);
654 if (o->autoanswer || o->autohangup) {
655 /* Assume auto-hangup too */
657 setformat(o, O_CLOSE);
663 /*! \brief used for data coming from the network */
664 static int oss_write(struct ast_channel *c, struct ast_frame *f)
667 struct chan_oss_pvt *o = c->tech_pvt;
670 * we could receive a block which is not a multiple of our
671 * FRAME_SIZE, so buffer it locally and write to the device
672 * in FRAME_SIZE chunks.
673 * Keep the residue stored for future use.
675 src = 0; /* read position into f->data */
676 while (src < f->datalen) {
677 /* Compute spare room in the buffer */
678 int l = sizeof(o->oss_write_buf) - o->oss_write_dst;
680 if (f->datalen - src >= l) { /* enough to fill a frame */
681 memcpy(o->oss_write_buf + o->oss_write_dst, f->data.ptr + src, l);
682 soundcard_writeframe(o, (short *) o->oss_write_buf);
684 o->oss_write_dst = 0;
685 } else { /* copy residue */
686 l = f->datalen - src;
687 memcpy(o->oss_write_buf + o->oss_write_dst, f->data.ptr + src, l);
688 src += l; /* but really, we are done */
689 o->oss_write_dst += l;
695 static struct ast_frame *oss_read(struct ast_channel *c)
698 struct chan_oss_pvt *o = c->tech_pvt;
699 struct ast_frame *f = &o->read_f;
701 /* XXX can be simplified returning &ast_null_frame */
702 /* prepare a NULL frame in case we don't have enough data to return */
703 memset(f, '\0', sizeof(struct ast_frame));
704 f->frametype = AST_FRAME_NULL;
705 f->src = oss_tech.type;
707 res = read(o->sounddev, o->oss_read_buf + o->readpos, sizeof(o->oss_read_buf) - o->readpos);
708 if (res < 0) /* audio data not ready, return a NULL frame */
712 if (o->readpos < sizeof(o->oss_read_buf)) /* not enough samples */
718 o->readpos = AST_FRIENDLY_OFFSET; /* reset read pointer for next frame */
719 if (c->_state != AST_STATE_UP) /* drop data if frame is not up */
721 /* ok we can build and deliver the frame to the caller */
722 f->frametype = AST_FRAME_VOICE;
723 ast_format_set(&f->subclass.format, AST_FORMAT_SLINEAR, 0);
724 f->samples = FRAME_SIZE;
725 f->datalen = FRAME_SIZE * 2;
726 f->data.ptr = o->oss_read_buf + AST_FRIENDLY_OFFSET;
727 if (o->boost != BOOST_SCALE) { /* scale and clip values */
729 int16_t *p = (int16_t *) f->data.ptr;
730 for (i = 0; i < f->samples; i++) {
731 x = (p[i] * o->boost) / BOOST_SCALE;
740 f->offset = AST_FRIENDLY_OFFSET;
744 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
746 struct chan_oss_pvt *o = newchan->tech_pvt;
751 static int oss_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen)
753 struct chan_oss_pvt *o = c->tech_pvt;
757 case AST_CONTROL_INCOMPLETE:
758 case AST_CONTROL_BUSY:
759 case AST_CONTROL_CONGESTION:
760 case AST_CONTROL_RINGING:
764 case AST_CONTROL_PROGRESS:
765 case AST_CONTROL_PROCEEDING:
766 case AST_CONTROL_VIDUPDATE:
767 case AST_CONTROL_SRCUPDATE:
769 case AST_CONTROL_HOLD:
770 ast_verbose(" << Console Has Been Placed on Hold >> \n");
771 ast_moh_start(c, data, o->mohinterpret);
773 case AST_CONTROL_UNHOLD:
774 ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
778 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, c->name);
786 * \brief allocate a new channel.
788 static struct ast_channel *oss_new(struct chan_oss_pvt *o, char *ext, char *ctx, int state, const char *linkedid)
790 struct ast_channel *c;
792 c = ast_channel_alloc(1, state, o->cid_num, o->cid_name, "", ext, ctx, linkedid, 0, "Console/%s", o->device + 5);
797 setformat(o, O_RDWR);
798 ast_channel_set_fd(c, 0, o->sounddev); /* -1 if device closed, override later */
800 ast_format_set(&c->readformat, AST_FORMAT_SLINEAR, 0);
801 ast_format_set(&c->writeformat, AST_FORMAT_SLINEAR, 0);
802 ast_format_cap_add(c->nativeformats, &c->readformat);
804 /* if the console makes the call, add video to the offer */
805 /* if (state == AST_STATE_RINGING) TODO XXX CONSOLE VIDEO IS DISABLED UNTIL IT GETS A MAINTAINER
806 c->nativeformats |= console_video_formats; */
810 if (!ast_strlen_zero(o->language))
811 ast_string_field_set(c, language, o->language);
812 /* Don't use ast_set_callerid() here because it will
813 * generate a needless NewCallerID event */
814 if (!ast_strlen_zero(o->cid_num)) {
815 c->caller.ani.number.valid = 1;
816 c->caller.ani.number.str = ast_strdup(o->cid_num);
818 if (!ast_strlen_zero(ext)) {
819 c->dialed.number.str = ast_strdup(ext);
823 ast_module_ref(ast_module_info->self);
824 ast_jb_configure(c, &global_jbconf);
825 if (state != AST_STATE_DOWN) {
826 if (ast_pbx_start(c)) {
827 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", c->name);
832 console_video_start(get_video_desc(c), c); /* XXX cleanup */
837 static struct ast_channel *oss_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause)
839 struct ast_channel *c;
840 struct chan_oss_pvt *o;
841 AST_DECLARE_APP_ARGS(args,
845 char *parse = ast_strdupa(data);
847 struct ast_format tmpfmt;
849 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
850 o = find_desc(args.name);
852 ast_log(LOG_WARNING, "oss_request ty <%s> data 0x%p <%s>\n", type, data, (char *) data);
854 ast_log(LOG_NOTICE, "Device %s not found\n", args.name);
855 /* XXX we could default to 'dsp' perhaps ? */
858 if (!(ast_format_cap_iscompatible(cap, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0)))) {
859 ast_log(LOG_NOTICE, "Format %s unsupported\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
863 ast_log(LOG_NOTICE, "Already have a call (chan %p) on the OSS channel\n", o->owner);
864 *cause = AST_CAUSE_BUSY;
867 c = oss_new(o, NULL, NULL, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
869 ast_log(LOG_WARNING, "Unable to create new OSS channel\n");
875 static void store_config_core(struct chan_oss_pvt *o, const char *var, const char *value);
877 /*! Generic console command handler. Basically a wrapper for a subset
878 * of config file options which are also available from the CLI
880 static char *console_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
882 struct chan_oss_pvt *o = find_desc(oss_active);
883 const char *var, *value;
886 e->command = CONSOLE_VIDEO_CMDS;
888 "Usage: " CONSOLE_VIDEO_CMDS "...\n"
889 " Generic handler for console commands.\n";
896 if (a->argc < e->args)
897 return CLI_SHOWUSAGE;
899 ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n",
903 var = a->argv[e->args-1];
904 value = a->argc > e->args ? a->argv[e->args] : NULL;
905 if (value) /* handle setting */
906 store_config_core(o, var, value);
907 if (!console_video_cli(o->env, var, a->fd)) /* print video-related values */
909 /* handle other values */
910 if (!strcasecmp(var, "device")) {
911 ast_cli(a->fd, "device is [%s]\n", o->device);
916 static char *console_autoanswer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
918 struct chan_oss_pvt *o = find_desc(oss_active);
922 e->command = "console {set|show} autoanswer [on|off]";
924 "Usage: console {set|show} autoanswer [on|off]\n"
925 " Enables or disables autoanswer feature. If used without\n"
926 " argument, displays the current on/off status of autoanswer.\n"
927 " The default value of autoanswer is in 'oss.conf'.\n";
934 if (a->argc == e->args - 1) {
935 ast_cli(a->fd, "Auto answer is %s.\n", o->autoanswer ? "on" : "off");
938 if (a->argc != e->args)
939 return CLI_SHOWUSAGE;
941 ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n",
945 if (!strcasecmp(a->argv[e->args-1], "on"))
947 else if (!strcasecmp(a->argv[e->args - 1], "off"))
950 return CLI_SHOWUSAGE;
954 /*! \brief helper function for the answer key/cli command */
955 static char *console_do_answer(int fd)
957 struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
958 struct chan_oss_pvt *o = find_desc(oss_active);
961 ast_cli(fd, "No one is calling us\n");
965 ast_queue_frame(o->owner, &f);
970 * \brief answer command from the console
972 static char *console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
976 e->command = "console answer";
978 "Usage: console answer\n"
979 " Answers an incoming call on the console (OSS) channel.\n";
983 return NULL; /* no completion */
985 if (a->argc != e->args)
986 return CLI_SHOWUSAGE;
987 return console_do_answer(a->fd);
991 * \brief Console send text CLI command
993 * \note concatenate all arguments into a single string. argv is NULL-terminated
994 * so we can use it right away
996 static char *console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
998 struct chan_oss_pvt *o = find_desc(oss_active);
1001 if (cmd == CLI_INIT) {
1002 e->command = "console send text";
1004 "Usage: console send text <message>\n"
1005 " Sends a text message for display on the remote terminal.\n";
1007 } else if (cmd == CLI_GENERATE)
1010 if (a->argc < e->args + 1)
1011 return CLI_SHOWUSAGE;
1013 ast_cli(a->fd, "Not in a call\n");
1016 ast_join(buf, sizeof(buf) - 1, a->argv + e->args);
1017 if (!ast_strlen_zero(buf)) {
1018 struct ast_frame f = { 0, };
1019 int i = strlen(buf);
1021 f.frametype = AST_FRAME_TEXT;
1022 f.subclass.integer = 0;
1025 ast_queue_frame(o->owner, &f);
1030 static char *console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1032 struct chan_oss_pvt *o = find_desc(oss_active);
1034 if (cmd == CLI_INIT) {
1035 e->command = "console hangup";
1037 "Usage: console hangup\n"
1038 " Hangs up any call currently placed on the console.\n";
1040 } else if (cmd == CLI_GENERATE)
1043 if (a->argc != e->args)
1044 return CLI_SHOWUSAGE;
1045 if (!o->owner && !o->hookstate) { /* XXX maybe only one ? */
1046 ast_cli(a->fd, "No call to hang up\n");
1051 ast_queue_hangup_with_cause(o->owner, AST_CAUSE_NORMAL_CLEARING);
1052 setformat(o, O_CLOSE);
1056 static char *console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1058 struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_FLASH } };
1059 struct chan_oss_pvt *o = find_desc(oss_active);
1061 if (cmd == CLI_INIT) {
1062 e->command = "console flash";
1064 "Usage: console flash\n"
1065 " Flashes the call currently placed on the console.\n";
1067 } else if (cmd == CLI_GENERATE)
1070 if (a->argc != e->args)
1071 return CLI_SHOWUSAGE;
1072 if (!o->owner) { /* XXX maybe !o->hookstate too ? */
1073 ast_cli(a->fd, "No call to flash\n");
1078 ast_queue_frame(o->owner, &f);
1082 static char *console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1085 char *mye = NULL, *myc = NULL;
1086 struct chan_oss_pvt *o = find_desc(oss_active);
1088 if (cmd == CLI_INIT) {
1089 e->command = "console dial";
1091 "Usage: console dial [extension[@context]]\n"
1092 " Dials a given extension (and context if specified)\n";
1094 } else if (cmd == CLI_GENERATE)
1097 if (a->argc > e->args + 1)
1098 return CLI_SHOWUSAGE;
1099 if (o->owner) { /* already in a call */
1101 struct ast_frame f = { AST_FRAME_DTMF, { 0 } };
1104 if (a->argc == e->args) { /* argument is mandatory here */
1105 ast_cli(a->fd, "Already in a call. You can only dial digits until you hangup.\n");
1108 s = a->argv[e->args];
1109 /* send the string one char at a time */
1110 for (i = 0; i < strlen(s); i++) {
1111 f.subclass.integer = s[i];
1112 ast_queue_frame(o->owner, &f);
1116 /* if we have an argument split it into extension and context */
1117 if (a->argc == e->args + 1)
1118 s = ast_ext_ctx(a->argv[e->args], &mye, &myc);
1119 /* supply default values if needed */
1124 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
1126 oss_new(o, mye, myc, AST_STATE_RINGING, NULL);
1128 ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc);
1134 static char *console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1136 struct chan_oss_pvt *o = find_desc(oss_active);
1140 if (cmd == CLI_INIT) {
1141 e->command = "console {mute|unmute} [toggle]";
1143 "Usage: console {mute|unmute} [toggle]\n"
1144 " Mute/unmute the microphone.\n";
1146 } else if (cmd == CLI_GENERATE)
1149 if (a->argc > e->args)
1150 return CLI_SHOWUSAGE;
1151 if (a->argc == e->args) {
1152 if (strcasecmp(a->argv[e->args-1], "toggle"))
1153 return CLI_SHOWUSAGE;
1156 s = a->argv[e->args-2];
1157 if (!strcasecmp(s, "mute"))
1158 o->mute = toggle ? !o->mute : 1;
1159 else if (!strcasecmp(s, "unmute"))
1160 o->mute = toggle ? !o->mute : 0;
1162 return CLI_SHOWUSAGE;
1163 ast_cli(a->fd, "Console mic is %s\n", o->mute ? "off" : "on");
1167 static char *console_transfer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1169 struct chan_oss_pvt *o = find_desc(oss_active);
1170 struct ast_channel *b = NULL;
1171 char *tmp, *ext, *ctx;
1175 e->command = "console transfer";
1177 "Usage: console transfer <extension>[@context]\n"
1178 " Transfers the currently connected call to the given extension (and\n"
1179 " context if specified)\n";
1186 return CLI_SHOWUSAGE;
1189 if (o->owner == NULL || (b = ast_bridged_channel(o->owner)) == NULL) {
1190 ast_cli(a->fd, "There is no call to transfer\n");
1194 tmp = ast_ext_ctx(a->argv[2], &ext, &ctx);
1195 if (ctx == NULL) /* supply default context if needed */
1196 ctx = o->owner->context;
1197 if (!ast_exists_extension(b, ctx, ext, 1,
1198 S_COR(b->caller.id.number.valid, b->caller.id.number.str, NULL))) {
1199 ast_cli(a->fd, "No such extension exists\n");
1201 ast_cli(a->fd, "Whee, transferring %s to %s@%s.\n", b->name, ext, ctx);
1202 if (ast_async_goto(b, ctx, ext, 1))
1203 ast_cli(a->fd, "Failed to transfer :(\n");
1210 static char *console_active(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1214 e->command = "console {set|show} active [<device>]";
1216 "Usage: console active [device]\n"
1217 " If used without a parameter, displays which device is the current\n"
1218 " console. If a device is specified, the console sound device is changed to\n"
1219 " the device specified.\n";
1226 ast_cli(a->fd, "active console is [%s]\n", oss_active);
1227 else if (a->argc != 4)
1228 return CLI_SHOWUSAGE;
1230 struct chan_oss_pvt *o;
1231 if (strcmp(a->argv[3], "show") == 0) {
1232 for (o = oss_default.next; o; o = o->next)
1233 ast_cli(a->fd, "device [%s] exists\n", o->name);
1236 o = find_desc(a->argv[3]);
1238 ast_cli(a->fd, "No device [%s] exists\n", a->argv[3]);
1240 oss_active = o->name;
1246 * \brief store the boost factor
1248 static void store_boost(struct chan_oss_pvt *o, const char *s)
1251 if (sscanf(s, "%30lf", &boost) != 1) {
1252 ast_log(LOG_WARNING, "invalid boost <%s>\n", s);
1255 if (boost < -BOOST_MAX) {
1256 ast_log(LOG_WARNING, "boost %s too small, using %d\n", s, -BOOST_MAX);
1258 } else if (boost > BOOST_MAX) {
1259 ast_log(LOG_WARNING, "boost %s too large, using %d\n", s, BOOST_MAX);
1262 boost = exp(log(10) * boost / 20) * BOOST_SCALE;
1264 ast_log(LOG_WARNING, "setting boost %s to %d\n", s, o->boost);
1267 static char *console_boost(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1269 struct chan_oss_pvt *o = find_desc(oss_active);
1273 e->command = "console boost";
1275 "Usage: console boost [boost in dB]\n"
1276 " Sets or display mic boost in dB\n";
1283 ast_cli(a->fd, "boost currently %5.1f\n", 20 * log10(((double) o->boost / (double) BOOST_SCALE)));
1284 else if (a->argc == 3)
1285 store_boost(o, a->argv[2]);
1289 static struct ast_cli_entry cli_oss[] = {
1290 AST_CLI_DEFINE(console_answer, "Answer an incoming console call"),
1291 AST_CLI_DEFINE(console_hangup, "Hangup a call on the console"),
1292 AST_CLI_DEFINE(console_flash, "Flash a call on the console"),
1293 AST_CLI_DEFINE(console_dial, "Dial an extension on the console"),
1294 AST_CLI_DEFINE(console_mute, "Disable/Enable mic input"),
1295 AST_CLI_DEFINE(console_transfer, "Transfer a call to a different extension"),
1296 AST_CLI_DEFINE(console_cmd, "Generic console command"),
1297 AST_CLI_DEFINE(console_sendtext, "Send text to the remote device"),
1298 AST_CLI_DEFINE(console_autoanswer, "Sets/displays autoanswer"),
1299 AST_CLI_DEFINE(console_boost, "Sets/displays mic boost in dB"),
1300 AST_CLI_DEFINE(console_active, "Sets/displays active console"),
1304 * store the mixer argument from the config file, filtering possibly
1305 * invalid or dangerous values (the string is used as argument for
1306 * system("mixer %s")
1308 static void store_mixer(struct chan_oss_pvt *o, const char *s)
1312 for (i = 0; i < strlen(s); i++) {
1313 if (!isalnum(s[i]) && strchr(" \t-/", s[i]) == NULL) {
1314 ast_log(LOG_WARNING, "Suspect char %c in mixer cmd, ignoring:\n\t%s\n", s[i], s);
1319 ast_free(o->mixer_cmd);
1320 o->mixer_cmd = ast_strdup(s);
1321 ast_log(LOG_WARNING, "setting mixer %s\n", s);
1325 * store the callerid components
1327 static void store_callerid(struct chan_oss_pvt *o, const char *s)
1329 ast_callerid_split(s, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
1332 static void store_config_core(struct chan_oss_pvt *o, const char *var, const char *value)
1334 CV_START(var, value);
1336 /* handle jb conf */
1337 if (!ast_jb_read_conf(&global_jbconf, var, value))
1340 if (!console_video_config(&o->env, var, value))
1341 return; /* matched there */
1342 CV_BOOL("autoanswer", o->autoanswer);
1343 CV_BOOL("autohangup", o->autohangup);
1344 CV_BOOL("overridecontext", o->overridecontext);
1345 CV_STR("device", o->device);
1346 CV_UINT("frags", o->frags);
1347 CV_UINT("debug", oss_debug);
1348 CV_UINT("queuesize", o->queuesize);
1349 CV_STR("context", o->ctx);
1350 CV_STR("language", o->language);
1351 CV_STR("mohinterpret", o->mohinterpret);
1352 CV_STR("extension", o->ext);
1353 CV_F("mixer", store_mixer(o, value));
1354 CV_F("callerid", store_callerid(o, value)) ;
1355 CV_F("boost", store_boost(o, value));
1361 * grab fields from the config file, init the descriptor and open the device.
1363 static struct chan_oss_pvt *store_config(struct ast_config *cfg, char *ctg)
1365 struct ast_variable *v;
1366 struct chan_oss_pvt *o;
1372 if (!(o = ast_calloc(1, sizeof(*o))))
1375 /* "general" is also the default thing */
1376 if (strcmp(ctg, "general") == 0) {
1377 o->name = ast_strdup("dsp");
1378 oss_active = o->name;
1381 o->name = ast_strdup(ctg);
1384 strcpy(o->mohinterpret, "default");
1386 o->lastopen = ast_tvnow(); /* don't leave it 0 or tvdiff may wrap */
1387 /* fill other fields from configuration */
1388 for (v = ast_variable_browse(cfg, ctg); v; v = v->next) {
1389 store_config_core(o, v->name, v->value);
1391 if (ast_strlen_zero(o->device))
1392 ast_copy_string(o->device, DEV_DSP, sizeof(o->device));
1396 if (asprintf(&cmd, "mixer %s", o->mixer_cmd) < 0) {
1397 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
1399 ast_log(LOG_WARNING, "running [%s]\n", cmd);
1400 if (system(cmd) < 0) {
1401 ast_log(LOG_WARNING, "system() failed: %s\n", strerror(errno));
1407 /* if the config file requested to start the GUI, do it */
1408 if (get_gui_startup(o->env))
1409 console_video_start(o->env, NULL);
1411 if (o == &oss_default) /* we are done with the default */
1416 if (setformat(o, O_RDWR) < 0) { /* open device */
1417 ast_verb(1, "Device %s not detected\n", ctg);
1418 ast_verb(1, "Turn off OSS support by adding " "'noload=chan_oss.so' in /etc/asterisk/modules.conf\n");
1421 if (o->duplex != M_FULL)
1422 ast_log(LOG_WARNING, "XXX I don't work right with non " "full-duplex sound cards XXX\n");
1423 #endif /* TRYOPEN */
1425 /* link into list of devices */
1426 if (o != &oss_default) {
1427 o->next = oss_default.next;
1428 oss_default.next = o;
1434 if (o != &oss_default)
1440 static int load_module(void)
1442 struct ast_config *cfg = NULL;
1444 struct ast_flags config_flags = { 0 };
1445 struct ast_format tmpfmt;
1447 /* Copy the default jb config over global_jbconf */
1448 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
1450 /* load config file */
1451 if (!(cfg = ast_config_load(config, config_flags))) {
1452 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
1453 return AST_MODULE_LOAD_DECLINE;
1454 } else if (cfg == CONFIG_STATUS_FILEINVALID) {
1455 ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config);
1456 return AST_MODULE_LOAD_DECLINE;
1460 store_config(cfg, ctg);
1461 } while ( (ctg = ast_category_browse(cfg, ctg)) != NULL);
1463 ast_config_destroy(cfg);
1465 if (find_desc(oss_active) == NULL) {
1466 ast_log(LOG_NOTICE, "Device %s not found\n", oss_active);
1467 /* XXX we could default to 'dsp' perhaps ? */
1468 /* XXX should cleanup allocated memory etc. */
1469 return AST_MODULE_LOAD_FAILURE;
1472 if (!(oss_tech.capabilities = ast_format_cap_alloc())) {
1473 return AST_MODULE_LOAD_FAILURE;
1475 ast_format_cap_add(oss_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
1477 /* TODO XXX CONSOLE VIDEO IS DISABLE UNTIL IT HAS A MAINTAINER
1478 * add console_video_formats to oss_tech.capabilities once this occurs. */
1480 if (ast_channel_register(&oss_tech)) {
1481 ast_log(LOG_ERROR, "Unable to register channel type 'OSS'\n");
1482 return AST_MODULE_LOAD_DECLINE;
1485 ast_cli_register_multiple(cli_oss, ARRAY_LEN(cli_oss));
1487 return AST_MODULE_LOAD_SUCCESS;
1491 static int unload_module(void)
1493 struct chan_oss_pvt *o, *next;
1495 ast_channel_unregister(&oss_tech);
1496 ast_cli_unregister_multiple(cli_oss, ARRAY_LEN(cli_oss));
1498 o = oss_default.next;
1502 ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
1510 oss_tech.capabilities = ast_format_cap_destroy(oss_tech.capabilities);
1514 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "OSS Console Channel Driver");