2 * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
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>
13 #include <sys/soundcard.h>
22 #include "buffer_tree.h"
24 #include "write_common.h"
25 #include "oss_write.cmdline.h"
28 /** Always use 16 bit little endian. */
29 #define FORMAT AFMT_S16_LE
31 /** Data specific to the oss writer. */
32 struct private_oss_write_data
{
33 /** The file handle of the device. */
36 * The samplerate given by command line option or the decoder
37 * of the writer node group.
41 * The number of channels, given by command line option or the
42 * decoder of the writer node group.
45 /** Four bytes for stereo streams, two bytes for mono streams. */
49 static int oss_pre_select(struct sched
*s
, struct writer_node
*wn
)
51 struct private_oss_write_data
*powd
= wn
->private_data
;
52 struct writer_node_group
*wng
= wn
->wng
;
54 if (*wng
->loaded
- wn
->written
< powd
->bytes_per_frame
)
56 para_fd_set(powd
->fd
, &s
->wfds
, &s
->max_fileno
);
60 static void oss_pre_select_btr(struct sched
*s
, struct task
*t
)
62 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
63 struct private_oss_write_data
*powd
= wn
->private_data
;
64 int ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
73 para_fd_set(powd
->fd
, &s
->wfds
, &s
->max_fileno
);
76 s
->timeout
.tv_sec
= 0;
77 s
->timeout
.tv_usec
= 1;
80 static int oss_post_select(struct sched
*s
, struct writer_node
*wn
)
83 struct private_oss_write_data
*powd
= wn
->private_data
;
84 struct writer_node_group
*wng
= wn
->wng
;
85 size_t frames
, bytes
= *wng
->loaded
- wn
->written
;
86 char *data
= *wng
->bufp
+ wn
->written
;
88 if (*wng
->input_error
< 0 && bytes
< powd
->bytes_per_frame
) {
89 wn
->written
= *wng
->loaded
;
90 return *wng
->input_error
;
92 frames
= bytes
/ powd
->bytes_per_frame
;
93 if (!frames
) /* less than a single frame available */
95 if (!FD_ISSET(powd
->fd
, &s
->wfds
))
97 ret
= write_nonblock(powd
->fd
, data
, frames
* powd
->bytes_per_frame
, 0);
105 static void oss_close(struct writer_node
*wn
)
107 struct private_oss_write_data
*powd
= wn
->private_data
;
110 oss_cmdline_parser_free(wn
->conf
);
115 * The Open Sound System Programmer's Guide sayeth:
117 * Set sampling parameters always so that number of channels (mono/stereo) is
118 * set before selecting sampling rate (speed). Failing to do this will make
119 * your program incompatible with cards such as the SoundBlaster Pro which
120 * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
121 * selects 44.1 kHz speed and then sets the device to stereo mode will
122 * incorrectly believe that the device is still in 44.1 kHz mode when actually
123 * the speed is decreased to 22.05 kHz.
125 static int oss_init(struct writer_node
*wn
, unsigned samplerate
, unsigned channels
)
127 int ret
, format
= FORMAT
, ch
, rate
;
128 struct oss_write_args_info
*conf
= wn
->conf
;
129 struct private_oss_write_data
*powd
= wn
->private_data
;
131 PARA_INFO_LOG("opening %s\n", conf
->device_arg
);
132 ret
= para_open(conf
->device_arg
, O_WRONLY
, 0);
136 ret
= mark_fd_nonblocking(powd
->fd
);
140 ret
= ioctl(powd
->fd
, SNDCTL_DSP_SETFMT
, &format
);
142 ret
= -ERRNO_TO_PARA_ERROR(errno
);
145 ret
= -E_BAD_SAMPLE_FORMAT
;
146 if (format
!= FORMAT
)
148 /* set number of channels */
149 if (!conf
->channels_given
&& channels
!= 0)
152 ch
= conf
->channels_arg
;
153 ret
= -E_BAD_CHANNEL_COUNT
;
157 ret
= ioctl(powd
->fd
, SNDCTL_DSP_CHANNELS
, &ch
);
159 ret
= -ERRNO_TO_PARA_ERROR(errno
);
162 if (powd
->channels
!= ch
)
164 powd
->bytes_per_frame
= ch
* 2;
169 * If we request a higher sampling rate than is supported by the
170 * device, the the highest possible speed is automatically used. The
171 * value actually used is returned as the new value of the argument.
173 if (!conf
->samplerate_given
&& samplerate
!= 0)
176 rate
= conf
->samplerate_arg
;
177 powd
->samplerate
= rate
;
178 ret
= ioctl(powd
->fd
, SNDCTL_DSP_SPEED
, &rate
);
180 ret
= -ERRNO_TO_PARA_ERROR(errno
);
183 if (rate
!= powd
->samplerate
) {
184 int min
= PARA_MIN(rate
, powd
->samplerate
),
185 max
= PARA_MAX(rate
, powd
->samplerate
);
187 * Check whether the returned sample rate differs significantly
188 * from the requested one.
190 ret
= -E_BAD_SAMPLERATE
;
191 if (100 * max
> 110 * min
) /* more than 10% deviation */
193 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", rate
,
196 wn
->min_iqs
= powd
->bytes_per_frame
;
204 static void oss_post_select_btr(__a_unused
struct sched
*s
,
207 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
208 struct oss_write_args_info
*conf
= wn
->conf
;
209 struct private_oss_write_data
*powd
= wn
->private_data
;
210 struct btr_node
*btrn
= wn
->btrn
;
211 size_t frames
, bytes
;
212 int ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
222 if (!conf
->samplerate_given
) /* config option trumps btr_exec */
223 ret
= get_btr_samplerate(wn
->btrn
, &rate
);
225 rate
= conf
->samplerate_arg
;
227 if (!conf
->channels_given
)
228 ret
= get_btr_channels(wn
->btrn
, &ch
);
230 ch
= conf
->channels_arg
;
231 ret
= oss_init(wn
, rate
, ch
);
236 bytes
= btr_next_buffer(btrn
, &data
);
237 frames
= bytes
/ powd
->bytes_per_frame
;
238 if (!frames
) { /* eof and less than a single frame available */
243 if (!FD_ISSET(powd
->fd
, &s
->wfds
))
245 ret
= write_nonblock(powd
->fd
, data
, frames
* powd
->bytes_per_frame
, 0);
248 btr_consume(btrn
, ret
);
254 static int oss_open(struct writer_node
*wn
)
256 struct writer_node_group
*wng
;
257 struct private_oss_write_data
*powd
;
259 powd
= para_calloc(sizeof(*powd
));
260 wn
->private_data
= powd
;
263 return oss_init(wn
, wng
->samplerate
? *wng
->samplerate
: 0,
264 wng
->channels
? *wng
->channels
: 0);
270 __malloc
static void *oss_parse_config(const char *options
)
273 struct oss_write_args_info
*conf
= para_calloc(sizeof(*conf
));
275 PARA_INFO_LOG("options: %s, %zd\n", options
, strcspn(options
, " \t"));
276 ret
= oss_cmdline_parser_string(options
, conf
, "oss_write");
285 static void oss_free_config(void *conf
)
287 oss_cmdline_parser_free(conf
);
291 * The init function of the oss writer.
293 * \param w Pointer to the writer to initialize.
297 void oss_write_init(struct writer
*w
)
299 struct oss_write_args_info dummy
;
301 oss_cmdline_parser_init(&dummy
);
303 w
->close
= oss_close
;
304 w
->pre_select
= oss_pre_select
;
305 w
->pre_select_btr
= oss_pre_select_btr
;
306 w
->post_select
= oss_post_select
;
307 w
->post_select_btr
= oss_post_select_btr
;
308 w
->parse_config
= oss_parse_config
;
309 w
->free_config
= oss_free_config
;
311 w
->help
= (struct ggo_help
) {
312 .short_help
= oss_write_args_info_help
,
313 .detailed_help
= oss_write_args_info_detailed_help
315 oss_cmdline_parser_free(&dummy
);