mvp_plugin.c 7.71 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17 18 19
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
/*
21 22
 * Media MVP audio output based on code from MVPMC project:
 * http://mvpmc.sourceforge.net/
23 24
 */

25
#include "config.h"
26 27
#include "output_api.h"
#include "fd_util.h"
28

29
#include <glib.h>
30

Max Kellermann's avatar
Max Kellermann committed
31 32 33 34
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
35 36 37 38 39 40
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "mvp"
Max Kellermann's avatar
Max Kellermann committed
41

42 43 44 45 46
typedef struct {
	unsigned long dsp_status;
	unsigned long stream_decode_type;
	unsigned long sample_rate;
	unsigned long bit_rate;
47
	unsigned long raw[64 / sizeof(unsigned long)];
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
} aud_status_t;

#define MVP_SET_AUD_STOP		_IOW('a',1,int)
#define MVP_SET_AUD_PLAY		_IOW('a',2,int)
#define MVP_SET_AUD_PAUSE	_IOW('a',3,int)
#define MVP_SET_AUD_UNPAUSE	_IOW('a',4,int)
#define MVP_SET_AUD_SRC		_IOW('a',5,int)
#define MVP_SET_AUD_MUTE		_IOW('a',6,int)
#define MVP_SET_AUD_BYPASS	_IOW('a',8,int)
#define MVP_SET_AUD_CHANNEL	_IOW('a',9,int)
#define MVP_GET_AUD_STATUS	_IOR('a',10,aud_status_t)
#define MVP_SET_AUD_VOLUME	_IOW('a',13,int)
#define MVP_GET_AUD_VOLUME	_IOR('a',14,int)
#define MVP_SET_AUD_STREAMTYPE	_IOW('a',15,int)
#define MVP_SET_AUD_FORMAT	_IOW('a',16,int)
#define MVP_GET_AUD_SYNC		_IOR('a',21,pts_sync_data_t*)
#define MVP_SET_AUD_STC		_IOW('a',22,long long int *)
#define MVP_SET_AUD_SYNC		_IOW('a',23,int)
#define MVP_SET_AUD_END_STREAM	_IOW('a',25,int)
#define MVP_SET_AUD_RESET	_IOW('a',26,int)
#define MVP_SET_AUD_DAC_CLK	_IOW('a',27,int)
#define MVP_GET_AUD_REGS		_IOW('a',28,aud_ctl_regs_t*)

Max Kellermann's avatar
Max Kellermann committed
71
struct mvp_data {
72
	struct audio_format audio_format;
73
	int fd;
Max Kellermann's avatar
Max Kellermann committed
74
};
75

76
static const unsigned mvp_sample_rates[][3] = {
77 78 79 80 81 82 83 84 85 86 87 88 89 90
	{9, 8000, 32000},
	{10, 11025, 44100},
	{11, 12000, 48000},
	{1, 16000, 32000},
	{2, 22050, 44100},
	{3, 24000, 48000},
	{5, 32000, 32000},
	{0, 44100, 44100},
	{7, 48000, 48000},
	{13, 64000, 32000},
	{14, 88200, 44100},
	{15, 96000, 48000}
};

91 92 93 94 95 96 97 98 99
/**
 * The quark used for GError.domain.
 */
static inline GQuark
mvp_output_quark(void)
{
	return g_quark_from_static_string("mvp_output");
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
/**
 * Translate a sample rate to a MVP sample rate.
 *
 * @param sample_rate the sample rate in Hz
 */
static unsigned
mvp_find_sample_rate(unsigned sample_rate)
{
	for (unsigned i = 0; i < G_N_ELEMENTS(mvp_sample_rates); ++i)
		if (mvp_sample_rates[i][1] == sample_rate)
			return mvp_sample_rates[i][0];

	return (unsigned)-1;
}

Max Kellermann's avatar
Max Kellermann committed
115 116
static bool
mvp_output_test_default_device(void)
117
{
118 119
	int fd;

120
	fd = open_cloexec("/dev/adec_pcm", O_WRONLY, 0);
121

122
	if (fd >= 0) {
123
		close(fd);
124
		return true;
125 126
	}

127 128
	g_warning("Error opening PCM device \"/dev/adec_pcm\": %s\n",
		  strerror(errno));
129

130
	return false;
131 132
}

133
static void *
Max Kellermann's avatar
Max Kellermann committed
134
mvp_output_init(G_GNUC_UNUSED const struct audio_format *audio_format,
135 136
		G_GNUC_UNUSED const struct config_param *param,
		G_GNUC_UNUSED GError **error)
137
{
Max Kellermann's avatar
Max Kellermann committed
138
	struct mvp_data *md = g_new(struct mvp_data, 1);
139
	md->fd = -1;
140

141
	return md;
142 143
}

Max Kellermann's avatar
Max Kellermann committed
144 145
static void
mvp_output_finish(void *data)
146
{
Max Kellermann's avatar
Max Kellermann committed
147
	struct mvp_data *md = data;
148
	g_free(md);
149 150
}

151
static bool
152 153
mvp_set_pcm_params(struct mvp_data *md, struct audio_format *audio_format,
		   GError **error)
154
{
Max Kellermann's avatar
Max Kellermann committed
155
	unsigned mix[5];
156

157 158
	switch (audio_format->channels) {
	case 1:
159
		mix[0] = 1;
160 161 162
		break;

	case 2:
163
		mix[0] = 0;
164 165 166
		break;

	default:
167 168 169 170 171
		g_debug("unsupported channel count %u - falling back to stereo",
			audio_format->channels);
		audio_format->channels = 2;
		mix[0] = 0;
		break;
172
	}
173 174

	/* 0,1=24bit(24) , 2,3=16bit */
175 176
	switch (audio_format->format) {
	case SAMPLE_FORMAT_S16:
177
		mix[1] = 2;
178 179
		break;

180
	case SAMPLE_FORMAT_S24_P32:
181
		mix[1] = 0;
182 183 184
		break;

	default:
185 186 187
		g_debug("unsupported sample format %s - falling back to 16 bit",
			sample_format_to_string(audio_format->format));
		audio_format->format = SAMPLE_FORMAT_S16;
188 189
		mix[1] = 2;
		break;
190
	}
191

Avuton Olrich's avatar
Avuton Olrich committed
192
	mix[3] = 0;	/* stream type? */
193
	mix[4] = G_BYTE_ORDER == G_LITTLE_ENDIAN;
194 195 196 197

	/*
	 * if there is an exact match for the frequency, use it.
	 */
198 199
	mix[2] = mvp_find_sample_rate(audio_format->sample_rate);
	if (mix[2] == (unsigned)-1) {
200 201 202
		g_set_error(error, mvp_output_quark(), 0,
			    "Can not find suitable output frequency for %u",
			    audio_format->sample_rate);
203
		return false;
204 205
	}

206
	if (ioctl(md->fd, MVP_SET_AUD_FORMAT, &mix) < 0) {
207 208
		g_set_error(error, mvp_output_quark(), errno,
			    "Can not set audio format");
209
		return false;
210 211 212
	}

	if (ioctl(md->fd, MVP_SET_AUD_SYNC, 2) != 0) {
213 214
		g_set_error(error, mvp_output_quark(), errno,
			    "Can not set audio sync");
215
		return false;
216
	}
217

218
	if (ioctl(md->fd, MVP_SET_AUD_PLAY, 0) < 0) {
219 220
		g_set_error(error, mvp_output_quark(), errno,
			    "Can not set audio play mode");
221
		return false;
222
	}
223

224
	return true;
225 226
}

227
static bool
228
mvp_output_open(void *data, struct audio_format *audio_format, GError **error)
229
{
Max Kellermann's avatar
Max Kellermann committed
230
	struct mvp_data *md = data;
231 232
	long long int stc = 0;
	int mix[5] = { 0, 2, 7, 1, 0 };
233
	bool success;
234

235
	md->fd = open_cloexec("/dev/adec_pcm", O_RDWR | O_NONBLOCK, 0);
236
	if (md->fd < 0) {
237 238 239
		g_set_error(error, mvp_output_quark(), errno,
			    "Error opening /dev/adec_pcm: %s",
			    strerror(errno));
240
		return false;
241 242
	}
	if (ioctl(md->fd, MVP_SET_AUD_SRC, 1) < 0) {
243 244 245
		g_set_error(error, mvp_output_quark(), errno,
			    "Error setting audio source: %s",
			    strerror(errno));
246
		return false;
247 248
	}
	if (ioctl(md->fd, MVP_SET_AUD_STREAMTYPE, 0) < 0) {
249 250 251
		g_set_error(error, mvp_output_quark(), errno,
			    "Error setting audio streamtype: %s",
			    strerror(errno));
252
		return false;
253 254
	}
	if (ioctl(md->fd, MVP_SET_AUD_FORMAT, &mix) < 0) {
255 256 257
		g_set_error(error, mvp_output_quark(), errno,
			    "Error setting audio format: %s",
			    strerror(errno));
258
		return false;
259 260 261
	}
	ioctl(md->fd, MVP_SET_AUD_STC, &stc);
	if (ioctl(md->fd, MVP_SET_AUD_BYPASS, 1) < 0) {
262 263 264
		g_set_error(error, mvp_output_quark(), errno,
			    "Error setting audio streamtype: %s",
			    strerror(errno));
265
		return false;
266
	}
267

268
	success = mvp_set_pcm_params(md, audio_format, error);
269 270 271
	if (!success)
		return false;

Max Kellermann's avatar
Max Kellermann committed
272
	md->audio_format = *audio_format;
273
	return true;
274 275
}

Max Kellermann's avatar
Max Kellermann committed
276
static void mvp_output_close(void *data)
277
{
Max Kellermann's avatar
Max Kellermann committed
278
	struct mvp_data *md = data;
279 280
	if (md->fd >= 0)
		close(md->fd);
281 282 283
	md->fd = -1;
}

Max Kellermann's avatar
Max Kellermann committed
284
static void mvp_output_cancel(void *data)
285
{
Max Kellermann's avatar
Max Kellermann committed
286
	struct mvp_data *md = data;
287 288 289 290 291
	if (md->fd >= 0) {
		ioctl(md->fd, MVP_SET_AUD_RESET, 0x11);
		close(md->fd);
		md->fd = -1;
	}
292 293
}

294
static size_t
295
mvp_output_play(void *data, const void *chunk, size_t size, GError **error)
296
{
Max Kellermann's avatar
Max Kellermann committed
297
	struct mvp_data *md = data;
298
	ssize_t ret;
299 300

	/* reopen the device since it was closed by dropBufferedAudio */
301 302 303
	if (md->fd < 0) {
		bool success;

304
		success = mvp_output_open(md, &md->audio_format, error);
305 306 307
		if (!success)
			return 0;
	}
308

309
	while (true) {
310
		ret = write(md->fd, chunk, size);
311 312 313
		if (ret > 0)
			return (size_t)ret;

314 315 316
		if (ret < 0) {
			if (errno == EINTR)
				continue;
317 318 319

			g_set_error(error, mvp_output_quark(), errno,
				    "Failed to write: %s", strerror(errno));
320
			return 0;
321 322
		}
	}
323 324
}

Max Kellermann's avatar
Max Kellermann committed
325
const struct audio_output_plugin mvp_output_plugin = {
326
	.name = "mvp",
Max Kellermann's avatar
Max Kellermann committed
327 328 329 330 331 332 333
	.test_default_device = mvp_output_test_default_device,
	.init = mvp_output_init,
	.finish = mvp_output_finish,
	.open = mvp_output_open,
	.close = mvp_output_close,
	.play = mvp_output_play,
	.cancel = mvp_output_cancel,
334
};