2 * Copyright (C) 2009-2011 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>
12 #include <sys/soundcard.h>
21 #include "buffer_tree.h"
23 #include "write_common.h"
24 #include "oss_write.cmdline.h"
27 /** Data specific to the oss writer. */
28 struct private_oss_write_data
{
29 /** The file handle of the device. */
31 /** Four bytes for stereo streams, two bytes for mono streams. */
35 static int get_oss_format(enum sample_format sf
)
38 case SF_S8
: return AFMT_S8
;
39 case SF_U8
: return AFMT_U8
;
40 case SF_S16_LE
: return AFMT_S16_LE
;
41 case SF_S16_BE
: return AFMT_S16_BE
;
42 case SF_U16_LE
: return AFMT_U16_LE
;
43 case SF_U16_BE
: return AFMT_U16_BE
;
44 default: return AFMT_S16_LE
;
48 static void oss_pre_select(struct sched
*s
, struct task
*t
)
50 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
51 struct private_oss_write_data
*powd
= wn
->private_data
;
52 int ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
57 return sched_min_delay(s
);
58 para_fd_set(powd
->fd
, &s
->wfds
, &s
->max_fileno
);
61 static void oss_close(struct writer_node
*wn
)
63 struct private_oss_write_data
*powd
= wn
->private_data
;
72 * The Open Sound System Programmer's Guide sayeth:
74 * Set sampling parameters always so that number of channels (mono/stereo) is
75 * set before selecting sampling rate (speed). Failing to do this will make
76 * your program incompatible with cards such as the SoundBlaster Pro which
77 * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
78 * selects 44.1 kHz speed and then sets the device to stereo mode will
79 * incorrectly believe that the device is still in 44.1 kHz mode when actually
80 * the speed is decreased to 22.05 kHz.
82 static int oss_init(struct writer_node
*wn
, unsigned sample_rate
,
83 unsigned channels
, int sample_format
)
87 struct oss_write_args_info
*conf
= wn
->conf
;
88 struct private_oss_write_data
*powd
= para_calloc(sizeof(*powd
));
90 PARA_INFO_LOG("opening %s\n", conf
->device_arg
);
91 ret
= para_open(conf
->device_arg
, O_WRONLY
, 0);
95 ret
= mark_fd_nonblocking(powd
->fd
);
99 sample_format
= format
= get_oss_format(sample_format
);
100 ret
= ioctl(powd
->fd
, SNDCTL_DSP_SETFMT
, &format
);
102 ret
= -ERRNO_TO_PARA_ERROR(errno
);
105 ret
= -E_BAD_SAMPLE_FORMAT
;
106 if (format
!= sample_format
)
108 /* set number of channels */
110 ret
= ioctl(powd
->fd
, SNDCTL_DSP_CHANNELS
, &ch
);
112 ret
= -ERRNO_TO_PARA_ERROR(errno
);
115 ret
= -E_BAD_CHANNEL_COUNT
;
118 if (format
== SF_U8
|| format
== SF_S8
)
119 powd
->bytes_per_frame
= ch
;
121 powd
->bytes_per_frame
= ch
* 2;
126 * If we request a higher sampling rate than is supported by the
127 * device, the the highest possible speed is automatically used. The
128 * value actually used is returned as the new value of the argument.
131 ret
= ioctl(powd
->fd
, SNDCTL_DSP_SPEED
, &rate
);
133 ret
= -ERRNO_TO_PARA_ERROR(errno
);
136 if (rate
!= sample_rate
) {
137 unsigned min
= PARA_MIN(rate
, sample_rate
),
138 max
= PARA_MAX(rate
, sample_rate
);
140 * Check whether the returned sample rate differs significantly
141 * from the requested one.
143 ret
= -E_BAD_SAMPLERATE
;
144 if (100 * max
> 110 * min
) /* more than 10% deviation */
146 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", rate
,
149 wn
->min_iqs
= powd
->bytes_per_frame
;
150 wn
->private_data
= powd
;
159 static void oss_post_select(__a_unused
struct sched
*s
,
162 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
163 struct private_oss_write_data
*powd
= wn
->private_data
;
164 struct btr_node
*btrn
= wn
->btrn
;
165 size_t frames
, bytes
;
166 int ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
174 int32_t rate
, ch
, format
;
175 get_btr_sample_rate(btrn
, &rate
);
176 get_btr_channels(btrn
, &ch
);
177 get_btr_sample_format(btrn
, &format
);
178 ret
= oss_init(wn
, rate
, ch
, format
);
183 btr_merge(btrn
, wn
->min_iqs
);
184 bytes
= btr_next_buffer(btrn
, &data
);
185 frames
= bytes
/ powd
->bytes_per_frame
;
186 if (!frames
) { /* eof and less than a single frame available */
187 ret
= -E_WRITE_COMMON_EOF
;
191 if (!FD_ISSET(powd
->fd
, &s
->wfds
))
193 ret
= write_nonblock(powd
->fd
, data
, frames
* powd
->bytes_per_frame
);
196 btr_consume(btrn
, ret
);
201 btr_remove_node(btrn
);
204 __malloc
static void *oss_parse_config_or_die(const char *options
)
206 struct oss_write_args_info
*conf
= para_calloc(sizeof(*conf
));
208 /* exits on errors */
209 oss_cmdline_parser_string(options
, conf
, "oss_write");
213 static void oss_free_config(void *conf
)
215 oss_cmdline_parser_free(conf
);
219 * The init function of the oss writer.
221 * \param w Pointer to the writer to initialize.
225 void oss_write_init(struct writer
*w
)
227 struct oss_write_args_info dummy
;
229 oss_cmdline_parser_init(&dummy
);
230 w
->close
= oss_close
;
231 w
->pre_select
= oss_pre_select
;
232 w
->post_select
= oss_post_select
;
233 w
->parse_config_or_die
= oss_parse_config_or_die
;
234 w
->free_config
= oss_free_config
;
236 w
->help
= (struct ggo_help
) {
237 .short_help
= oss_write_args_info_help
,
238 .detailed_help
= oss_write_args_info_detailed_help
240 oss_cmdline_parser_free(&dummy
);