2 * Copyright (C) 2009-2014 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>
12 #include <sys/soundcard.h>
20 #include "buffer_tree.h"
22 #include "write_common.h"
23 #include "oss_write.cmdline.h"
26 /** Data specific to the oss writer. */
27 struct private_oss_write_data
{
28 /** The file handle of the device. */
30 /** Four bytes for stereo streams, two bytes for mono streams. */
35 * We keep one bit of static storage to make sure only one instance of the oss
36 * writer is running at any given time.
38 static bool sound_device_busy
;
40 static bool sound_device_is_busy(void)
42 return sound_device_busy
;
45 static void set_sound_device_busy(void)
47 assert(!sound_device_busy
);
48 sound_device_busy
= true;
51 static void set_sound_device_idle(void)
53 assert(sound_device_busy
);
54 sound_device_busy
= false;
57 static int get_oss_format(enum sample_format sf
)
60 case SF_S8
: return AFMT_S8
;
61 case SF_U8
: return AFMT_U8
;
62 case SF_S16_LE
: return AFMT_S16_LE
;
63 case SF_S16_BE
: return AFMT_S16_BE
;
64 case SF_U16_LE
: return AFMT_U16_LE
;
65 case SF_U16_BE
: return AFMT_U16_BE
;
66 default: return AFMT_S16_LE
;
70 static void oss_pre_select(struct sched
*s
, void *context
)
72 struct writer_node
*wn
= context
;
73 struct private_oss_write_data
*powd
= wn
->private_data
;
74 int ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
76 if (ret
== 0 || (sound_device_is_busy() && !powd
))
79 return sched_min_delay(s
);
80 para_fd_set(powd
->fd
, &s
->wfds
, &s
->max_fileno
);
83 static void oss_close(struct writer_node
*wn
)
85 struct private_oss_write_data
*powd
= wn
->private_data
;
91 set_sound_device_idle();
95 * The Open Sound System Programmer's Guide sayeth:
97 * Set sampling parameters always so that number of channels (mono/stereo) is
98 * set before selecting sampling rate (speed). Failing to do this will make
99 * your program incompatible with cards such as the SoundBlaster Pro which
100 * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
101 * selects 44.1 kHz speed and then sets the device to stereo mode will
102 * incorrectly believe that the device is still in 44.1 kHz mode when actually
103 * the speed is decreased to 22.05 kHz.
105 static int oss_init(struct writer_node
*wn
, unsigned sample_rate
,
106 unsigned channels
, int sample_format
)
110 struct oss_write_args_info
*conf
= wn
->conf
;
111 struct private_oss_write_data
*powd
= para_calloc(sizeof(*powd
));
113 PARA_INFO_LOG("opening %s\n", conf
->device_arg
);
114 ret
= para_open(conf
->device_arg
, O_WRONLY
, 0);
118 ret
= mark_fd_nonblocking(powd
->fd
);
122 sample_format
= format
= get_oss_format(sample_format
);
123 ret
= ioctl(powd
->fd
, SNDCTL_DSP_SETFMT
, &format
);
125 ret
= -ERRNO_TO_PARA_ERROR(errno
);
128 ret
= -E_BAD_SAMPLE_FORMAT
;
129 if (format
!= sample_format
)
131 /* set number of channels */
133 ret
= ioctl(powd
->fd
, SNDCTL_DSP_CHANNELS
, &ch
);
135 ret
= -ERRNO_TO_PARA_ERROR(errno
);
138 ret
= -E_BAD_CHANNEL_COUNT
;
141 if (format
== SF_U8
|| format
== SF_S8
)
142 powd
->bytes_per_frame
= ch
;
144 powd
->bytes_per_frame
= ch
* 2;
149 * If we request a higher sampling rate than is supported by the
150 * device, the highest possible speed is automatically used. The
151 * value actually used is returned as the new value of the argument.
154 ret
= ioctl(powd
->fd
, SNDCTL_DSP_SPEED
, &rate
);
156 ret
= -ERRNO_TO_PARA_ERROR(errno
);
159 if (rate
!= sample_rate
) {
160 unsigned min
= PARA_MIN(rate
, sample_rate
),
161 max
= PARA_MAX(rate
, sample_rate
);
163 * Check whether the returned sample rate differs significantly
164 * from the requested one.
166 ret
= -E_BAD_SAMPLERATE
;
167 if (100 * max
> 110 * min
) /* more than 10% deviation */
169 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", rate
,
172 wn
->min_iqs
= powd
->bytes_per_frame
;
173 wn
->private_data
= powd
;
179 PARA_ERROR_LOG("failed to init %s: %s\n", conf
->device_arg
,
180 para_strerror(-ret
));
184 static int oss_post_select(__a_unused
struct sched
*s
, void *context
)
186 struct writer_node
*wn
= context
;
187 struct private_oss_write_data
*powd
= wn
->private_data
;
188 struct btr_node
*btrn
= wn
->btrn
;
189 size_t frames
, bytes
;
194 ret
= task_get_notification(wn
->task
);
197 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
201 int32_t rate
, ch
, format
;
203 if (sound_device_is_busy())
205 get_btr_sample_rate(btrn
, &rate
);
206 get_btr_channels(btrn
, &ch
);
207 get_btr_sample_format(btrn
, &format
);
208 ret
= oss_init(wn
, rate
, ch
, format
);
211 set_sound_device_busy();
214 btr_merge(btrn
, wn
->min_iqs
);
215 bytes
= btr_next_buffer(btrn
, &data
);
216 frames
= bytes
/ powd
->bytes_per_frame
;
217 if (!frames
) { /* eof and less than a single frame available */
218 ret
= -E_WRITE_COMMON_EOF
;
222 if (!FD_ISSET(powd
->fd
, &s
->wfds
))
224 /* get maximal number of bytes that can be written */
225 ret
= ioctl(powd
->fd
, SNDCTL_DSP_GETOSPACE
, &abi
);
227 size_t max_frames
= abi
.bytes
/ powd
->bytes_per_frame
;
230 /* cap number of frames to avoid sound artefacts */
231 frames
= PARA_MIN(frames
, max_frames
);
233 ret
= xwrite(powd
->fd
, data
, frames
* powd
->bytes_per_frame
);
236 btr_consume(btrn
, ret
);
240 btr_remove_node(&wn
->btrn
);
244 __malloc
static void *oss_parse_config_or_die(int argc
, char **argv
)
246 struct oss_write_args_info
*conf
= para_calloc(sizeof(*conf
));
248 /* exits on errors */
249 oss_write_cmdline_parser(argc
, argv
, conf
);
253 static void oss_free_config(void *conf
)
255 oss_write_cmdline_parser_free(conf
);
259 * The init function of the oss writer.
261 * \param w Pointer to the writer to initialize.
265 void oss_write_init(struct writer
*w
)
267 struct oss_write_args_info dummy
;
269 oss_write_cmdline_parser_init(&dummy
);
270 w
->close
= oss_close
;
271 w
->pre_select
= oss_pre_select
;
272 w
->post_select
= oss_post_select
;
273 w
->parse_config_or_die
= oss_parse_config_or_die
;
274 w
->free_config
= oss_free_config
;
275 w
->help
= (struct ggo_help
)DEFINE_GGO_HELP(oss_write
);
276 oss_write_cmdline_parser_free(&dummy
);