2 * Copyright (C) 2009 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file oss_write.c Paraslash's oss output plugin. */
10 #include <sys/ioctl.h>
11 #include <sys/soundcard.h>
19 #include "buffer_tree.h"
21 #include "write_common.h"
22 #include "oss_write.cmdline.h"
25 /** Data specific to the oss writer. */
26 struct private_oss_write_data
{
27 /** The file handle of the device. */
29 /** Four bytes for stereo streams, two bytes for mono streams. */
34 * We keep one bit of static storage to make sure only one instance of the oss
35 * writer is running at any given time.
37 static bool sound_device_busy
;
39 static bool sound_device_is_busy(void)
41 return sound_device_busy
;
44 static void set_sound_device_busy(void)
46 assert(!sound_device_busy
);
47 sound_device_busy
= true;
50 static void set_sound_device_idle(void)
52 assert(sound_device_busy
);
53 sound_device_busy
= false;
56 static int get_oss_format(enum sample_format sf
)
59 case SF_S8
: return AFMT_S8
;
60 case SF_U8
: return AFMT_U8
;
61 case SF_S16_LE
: return AFMT_S16_LE
;
62 case SF_S16_BE
: return AFMT_S16_BE
;
63 case SF_U16_LE
: return AFMT_U16_LE
;
64 case SF_U16_BE
: return AFMT_U16_BE
;
65 default: return AFMT_S16_LE
;
69 static void oss_pre_select(struct sched
*s
, void *context
)
71 struct writer_node
*wn
= context
;
72 struct private_oss_write_data
*powd
= wn
->private_data
;
73 int ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
75 if (ret
== 0 || (sound_device_is_busy() && !powd
))
78 return sched_min_delay(s
);
79 para_fd_set(powd
->fd
, &s
->wfds
, &s
->max_fileno
);
82 static void oss_close(struct writer_node
*wn
)
84 struct private_oss_write_data
*powd
= wn
->private_data
;
90 set_sound_device_idle();
94 * The Open Sound System Programmer's Guide sayeth:
96 * Set sampling parameters always so that number of channels (mono/stereo) is
97 * set before selecting sampling rate (speed). Failing to do this will make
98 * your program incompatible with cards such as the SoundBlaster Pro which
99 * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
100 * selects 44.1 kHz speed and then sets the device to stereo mode will
101 * incorrectly believe that the device is still in 44.1 kHz mode when actually
102 * the speed is decreased to 22.05 kHz.
104 static int oss_init(struct writer_node
*wn
, unsigned sample_rate
,
105 unsigned channels
, int sample_format
)
109 struct oss_write_args_info
*conf
= wn
->conf
;
110 struct private_oss_write_data
*powd
= para_calloc(sizeof(*powd
));
112 PARA_INFO_LOG("opening %s\n", conf
->device_arg
);
113 ret
= para_open(conf
->device_arg
, O_WRONLY
, 0);
117 ret
= mark_fd_nonblocking(powd
->fd
);
121 sample_format
= format
= get_oss_format(sample_format
);
122 ret
= ioctl(powd
->fd
, SNDCTL_DSP_SETFMT
, &format
);
124 ret
= -ERRNO_TO_PARA_ERROR(errno
);
127 ret
= -E_BAD_SAMPLE_FORMAT
;
128 if (format
!= sample_format
)
130 /* set number of channels */
132 ret
= ioctl(powd
->fd
, SNDCTL_DSP_CHANNELS
, &ch
);
134 ret
= -ERRNO_TO_PARA_ERROR(errno
);
137 ret
= -E_BAD_CHANNEL_COUNT
;
140 if (format
== SF_U8
|| format
== SF_S8
)
141 powd
->bytes_per_frame
= ch
;
143 powd
->bytes_per_frame
= ch
* 2;
148 * If we request a higher sampling rate than is supported by the
149 * device, the highest possible speed is automatically used. The
150 * value actually used is returned as the new value of the argument.
153 ret
= ioctl(powd
->fd
, SNDCTL_DSP_SPEED
, &rate
);
155 ret
= -ERRNO_TO_PARA_ERROR(errno
);
158 if (rate
!= sample_rate
) {
159 unsigned min
= PARA_MIN(rate
, sample_rate
),
160 max
= PARA_MAX(rate
, sample_rate
);
162 * Check whether the returned sample rate differs significantly
163 * from the requested one.
165 ret
= -E_BAD_SAMPLERATE
;
166 if (100 * max
> 110 * min
) /* more than 10% deviation */
168 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", rate
,
171 wn
->min_iqs
= powd
->bytes_per_frame
;
172 wn
->private_data
= powd
;
178 PARA_ERROR_LOG("failed to init %s: %s\n", conf
->device_arg
,
179 para_strerror(-ret
));
183 static int oss_post_select(__a_unused
struct sched
*s
, void *context
)
185 struct writer_node
*wn
= context
;
186 struct private_oss_write_data
*powd
= wn
->private_data
;
187 struct btr_node
*btrn
= wn
->btrn
;
188 size_t frames
, bytes
;
193 ret
= task_get_notification(wn
->task
);
196 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
200 int32_t rate
, ch
, format
;
202 if (sound_device_is_busy())
204 get_btr_sample_rate(btrn
, &rate
);
205 get_btr_channels(btrn
, &ch
);
206 get_btr_sample_format(btrn
, &format
);
207 ret
= oss_init(wn
, rate
, ch
, format
);
210 set_sound_device_busy();
213 btr_merge(btrn
, wn
->min_iqs
);
214 bytes
= btr_next_buffer(btrn
, &data
);
215 frames
= bytes
/ powd
->bytes_per_frame
;
216 if (!frames
) { /* eof and less than a single frame available */
217 ret
= -E_WRITE_COMMON_EOF
;
221 if (!FD_ISSET(powd
->fd
, &s
->wfds
))
223 /* get maximal number of bytes that can be written */
224 ret
= ioctl(powd
->fd
, SNDCTL_DSP_GETOSPACE
, &abi
);
226 size_t max_frames
= abi
.bytes
/ powd
->bytes_per_frame
;
229 /* cap number of frames to avoid sound artefacts */
230 frames
= PARA_MIN(frames
, max_frames
);
232 ret
= xwrite(powd
->fd
, data
, frames
* powd
->bytes_per_frame
);
235 btr_consume(btrn
, ret
);
239 btr_remove_node(&wn
->btrn
);
243 __malloc
static void *oss_parse_config_or_die(int argc
, char **argv
)
245 struct oss_write_args_info
*conf
= para_calloc(sizeof(*conf
));
247 /* exits on errors */
248 oss_write_cmdline_parser(argc
, argv
, conf
);
252 static void oss_free_config(void *conf
)
254 oss_write_cmdline_parser_free(conf
);
258 * The init function of the oss writer.
260 * \param w Pointer to the writer to initialize.
264 void oss_write_init(struct writer
*w
)
266 struct oss_write_args_info dummy
;
268 oss_write_cmdline_parser_init(&dummy
);
269 w
->close
= oss_close
;
270 w
->pre_select
= oss_pre_select
;
271 w
->post_select
= oss_post_select
;
272 w
->parse_config_or_die
= oss_parse_config_or_die
;
273 w
->free_config
= oss_free_config
;
274 w
->help
= (struct ggo_help
)DEFINE_GGO_HELP(oss_write
);
275 oss_write_cmdline_parser_free(&dummy
);