summaryrefslogtreecommitdiff
path: root/oss_write.c
blob: 48545630a5d10865c3fc174fdfc7e1bbc6b7c7a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
 *
 * Licensed under the GPL v2. For licencing details see COPYING.
 */

/** \file oss_write.c Paraslash's oss output plugin. */

#include <sys/ioctl.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/soundcard.h>

#include "para.h"
#include "fd.h"
#include "string.h"
#include "list.h"
#include "sched.h"
#include "ggo.h"
#include "write.h"
#include "oss_write.cmdline.h"
#include "error.h"

/** Always use 16 bit little endian. */
#define FORMAT AFMT_S16_LE

/** Data specific to the oss writer. */
struct private_oss_write_data {
	/** The file handle of the device. */
	int fd;
	/**
	 * The samplerate given by command line option or the decoder
	 * of the writer node group.
	 */
	int samplerate;
	/**
	 * The number of channels, given by command line option or the
	 * decoder of the writer node group.
	 */
	int channels;
	/** Four bytes for stereo streams, two bytes for mono streams. */
	int bytes_per_frame;
};

static int oss_pre_select(struct sched *s, struct writer_node *wn)
{
	struct private_oss_write_data *powd = wn->private_data;
	struct writer_node_group *wng = wn->wng;

	if (*wng->loaded - wn->written < powd->bytes_per_frame)
		return 0;
	para_fd_set(powd->fd, &s->wfds, &s->max_fileno);
	return 1;
}

static int oss_post_select(struct sched *s, struct writer_node *wn)
{
	int ret;
	struct private_oss_write_data *powd = wn->private_data;
	struct writer_node_group *wng = wn->wng;
	size_t frames, bytes = *wng->loaded - wn->written;
	char *data = *wng->bufp + wn->written;

	if (*wng->input_error < 0 && bytes < powd->bytes_per_frame) {
		wn->written = *wng->loaded;
		return *wng->input_error;
	}
	frames = bytes / powd->bytes_per_frame;
	if (!frames) /* less than a single frame available */
		goto out;
	if (!FD_ISSET(powd->fd, &s->wfds))
		goto out;
	ret = write_nonblock(powd->fd, data, frames * powd->bytes_per_frame, 0);
	if (ret < 0)
		return ret;
	wn->written += ret;
out:
	return 1;
}

static void oss_close(struct writer_node *wn)
{
	struct private_oss_write_data *powd = wn->private_data;

	if (powd->fd >= 0)
		close(powd->fd);
	free(powd);
}

/*
 * The Open Sound System Programmer's Guide sayeth:
 *
 * Set sampling parameters always so that number of channels (mono/stereo) is
 * set before selecting sampling rate (speed).  Failing to do this will make
 * your program incompatible with cards such as the SoundBlaster Pro which
 * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
 * selects 44.1 kHz speed and then sets the device to stereo mode will
 * incorrectly believe that the device is still in 44.1 kHz mode when actually
 * the speed is decreased to 22.05 kHz.
 */
static int oss_open(struct writer_node *wn)
{
	int ret, format = FORMAT, channels, samplerate;
	struct oss_write_args_info *conf = wn->conf;
	struct writer_node_group *wng = wn->wng;
	struct private_oss_write_data *powd;

	PARA_INFO_LOG("opening %s\n", conf->device_arg);
	ret = para_open(conf->device_arg, O_WRONLY, 0);
	if (ret < 0)
		return ret;
	powd = para_calloc(sizeof(*powd));
	wn->private_data = powd;
	powd->fd = ret;
	ret = mark_fd_nonblocking(powd->fd);
	if (ret < 0)
		goto err;
	/* set PCM format */
	ret = ioctl(powd->fd, SNDCTL_DSP_SETFMT, &format);
	if (ret < 0) {
		ret = -ERRNO_TO_PARA_ERROR(errno);
		goto err;
	}
	ret = -E_BAD_SAMPLE_FORMAT;
	if (format != FORMAT)
		goto err;
	/* set number of channels */
	if (!conf->channels_given && wng->channels)
		channels = *wng->channels;
	else
		channels = conf->channels_arg;
	ret = -E_BAD_CHANNEL_COUNT;
	if (channels == 0)
		goto err;
	powd->channels = channels;
	ret = ioctl(powd->fd, SNDCTL_DSP_CHANNELS, &channels);
	if (ret < 0) {
		ret = -ERRNO_TO_PARA_ERROR(errno);
		goto err;
	}
	if (powd->channels != channels)
		goto err;
	powd->bytes_per_frame = channels * 2;

	/*
	 * Set sampling rate
	 *
	 * If we request a higher sampling rate than is supported by the
	 * device, the the highest possible speed is automatically used. The
	 * value actually used is returned as the new value of the argument.
	 */
	if (!conf->samplerate_given && wng->samplerate)
		samplerate = *wng->samplerate;
	else
		samplerate = conf->samplerate_arg;
	powd->samplerate = samplerate;
	ret = ioctl(powd->fd, SNDCTL_DSP_SPEED, &samplerate);
	if (ret < 0) {
		ret = -ERRNO_TO_PARA_ERROR(errno);
		goto err;
	}
	if (samplerate != powd->samplerate) {
		int min = PARA_MIN(samplerate, powd->samplerate),
			max = PARA_MAX(samplerate, powd->samplerate);
		/*
		 * Check whether the returned sample rate differs significantly
		 * from the requested one.
		 */
		ret = -E_BAD_SAMPLERATE;
		if (100 * max > 110 * min) /* more than 10% deviation */
			goto err;
		PARA_NOTICE_LOG("using %dHz rather than %dHz\n", samplerate,
			powd->samplerate);
	}

	return 1;
err:
	close(powd->fd);
	powd->fd = -1;
	return ret;
}

__malloc static void *oss_parse_config(const char *options)
{
	int ret;
	struct oss_write_args_info *conf = para_calloc(sizeof(*conf));

	PARA_INFO_LOG("options: %s, %zd\n", options, strcspn(options, " \t"));
	ret = oss_cmdline_parser_string(options, conf, "oss_write");
	if (ret)
		goto err_out;
	return conf;
err_out:
	free(conf);
	return NULL;
}

/**
 * The init function of the oss writer.
 *
 * \param w Pointer to the writer to initialize.
 *
 * \sa struct writer.
 */
void oss_write_init(struct writer *w)
{
	struct oss_write_args_info dummy;

	oss_cmdline_parser_init(&dummy);
	w->open = oss_open;
	w->close = oss_close;
	w->pre_select = oss_pre_select;
	w->post_select = oss_post_select;
	w->parse_config = oss_parse_config;
	w->shutdown = NULL;
	w->help = (struct ggo_help) {
		.short_help = oss_write_args_info_help,
		.detailed_help = oss_write_args_info_detailed_help
	};
	oss_cmdline_parser_free(&dummy);
}