mvp_plugin.c 6.74 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * This project's homepage is: http://www.musicpd.org
 *
 * Media MVP audio output based on code from MVPMC project:
 * http://mvpmc.sourceforge.net/
 *
 * 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.
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

22
#include "../output_api.h"
23

24
#include <glib.h>
Max Kellermann's avatar
Max Kellermann committed
25 26 27 28
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
29 30 31 32 33 34
#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
35

36 37 38 39 40
typedef struct {
	unsigned long dsp_status;
	unsigned long stream_decode_type;
	unsigned long sample_rate;
	unsigned long bit_rate;
41
	unsigned long raw[64 / sizeof(unsigned long)];
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
} 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*)

typedef struct _MvpData {
66 67
	struct audio_output *audio_output;
	struct audio_format audio_format;
68 69 70
	int fd;
} MvpData;

Max Kellermann's avatar
Max Kellermann committed
71
static unsigned pcmfrequencies[][3] = {
72 73 74 75 76 77 78 79 80 81 82 83 84 85
	{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}
};

Max Kellermann's avatar
Max Kellermann committed
86 87
static const unsigned numfrequencies =
	sizeof(pcmfrequencies) / sizeof(pcmfrequencies[0]);
88

89
static bool mvp_testDefault(void)
90
{
91 92 93 94
	int fd;

	fd = open("/dev/adec_pcm", O_WRONLY);

95
	if (fd) {
96
		close(fd);
97
		return true;
98 99
	}

100 101
	g_warning("Error opening PCM device \"/dev/adec_pcm\": %s\n",
		  strerror(errno));
102

103
	return false;
104 105
}

106 107 108
static void *mvp_initDriver(mpd_unused struct audio_output *audio_output,
			    mpd_unused const struct audio_format *audio_format,
			    mpd_unused ConfigParam *param)
109
{
110
	MvpData *md = g_new(MvpData, 1);
111
	md->audio_output = audio_output;
112
	md->fd = -1;
113

114
	return md;
115 116
}

117
static void mvp_finishDriver(void *data)
118
{
119
	MvpData *md = data;
120
	free(md);
121 122
}

123
static int mvp_setPcmParams(MvpData * md, unsigned long rate, int channels,
124
			    int big_endian, unsigned bits)
125
{
Max Kellermann's avatar
Max Kellermann committed
126 127
	unsigned iloop;
	unsigned mix[5];
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

	if (channels == 1)
		mix[0] = 1;
	else if (channels == 2)
		mix[0] = 0;
	else
		return -1;

	/* 0,1=24bit(24) , 2,3=16bit */
	if (bits == 16)
		mix[1] = 2;
	else if (bits == 24)
		mix[1] = 0;
	else
		return -1;

Avuton Olrich's avatar
Avuton Olrich committed
144
	mix[3] = 0;	/* stream type? */
145 146 147 148 149 150 151 152 153 154 155

	if (big_endian == 1)
		mix[4] = 1;
	else if (big_endian == 0)
		mix[4] = 0;
	else
		return -1;

	/*
	 * if there is an exact match for the frequency, use it.
	 */
156 157
	for (iloop = 0; iloop < numfrequencies; iloop++) {
		if (rate == pcmfrequencies[iloop][1]) {
158 159 160 161 162 163
			mix[2] = pcmfrequencies[iloop][0];
			break;
		}
	}

	if (iloop >= numfrequencies) {
164 165
		g_warning("Can not find suitable output frequency for %ld\n",
			  rate);
166 167 168
		return -1;
	}

169
	if (ioctl(md->fd, MVP_SET_AUD_FORMAT, &mix) < 0) {
170
		g_warning("Can not set audio format\n");
171
		return -1;
172 173 174
	}

	if (ioctl(md->fd, MVP_SET_AUD_SYNC, 2) != 0) {
175
		g_warning("Can not set audio sync\n");
176
		return -1;
177
	}
178

179
	if (ioctl(md->fd, MVP_SET_AUD_PLAY, 0) < 0) {
180
		g_warning("Can not set audio play mode\n");
181
		return -1;
182
	}
183 184 185 186

	return 0;
}

187 188
static bool
mvp_openDevice(void *data, struct audio_format *audioFormat)
189
{
190
	MvpData *md = data;
191 192 193 194
	long long int stc = 0;
	int mix[5] = { 0, 2, 7, 1, 0 };

	if ((md->fd = open("/dev/adec_pcm", O_RDWR | O_NONBLOCK)) < 0) {
195 196
		g_warning("Error opening /dev/adec_pcm: %s\n",
			  strerror(errno));
197
		return false;
198 199
	}
	if (ioctl(md->fd, MVP_SET_AUD_SRC, 1) < 0) {
200 201
		g_warning("Error setting audio source: %s\n",
			  strerror(errno));
202
		return false;
203 204
	}
	if (ioctl(md->fd, MVP_SET_AUD_STREAMTYPE, 0) < 0) {
205 206
		g_warning("Error setting audio streamtype: %s\n",
			  strerror(errno));
207
		return false;
208 209
	}
	if (ioctl(md->fd, MVP_SET_AUD_FORMAT, &mix) < 0) {
210 211
		g_warning("Error setting audio format: %s\n",
			  strerror(errno));
212
		return false;
213 214 215
	}
	ioctl(md->fd, MVP_SET_AUD_STC, &stc);
	if (ioctl(md->fd, MVP_SET_AUD_BYPASS, 1) < 0) {
216 217
		g_warning("Error setting audio streamtype: %s\n",
			  strerror(errno));
218
		return false;
219
	}
220
#ifdef WORDS_BIGENDIAN
221 222
	mvp_setPcmParams(md, audioFormat->sample_rate, audioFormat->channels,
			 0, audioFormat->bits);
223
#else
224 225
	mvp_setPcmParams(md, audioFormat->sample_rate, audioFormat->channels,
			 1, audioFormat->bits);
226
#endif
227
	md->audio_format = *audioFormat;
228
	return true;
229 230
}

231
static void mvp_closeDevice(void *data)
232
{
233
	MvpData *md = data;
234 235
	if (md->fd >= 0)
		close(md->fd);
236 237 238
	md->fd = -1;
}

239
static void mvp_dropBufferedAudio(void *data)
240
{
241
	MvpData *md = data;
242 243 244 245
	if (md->fd >= 0) {
		ioctl(md->fd, MVP_SET_AUD_RESET, 0x11);
		close(md->fd);
		md->fd = -1;
246
		audio_output_closed(md->audio_output);
247
	}
248 249
}

250 251
static bool
mvp_playAudio(void *data, const char *playChunk, size_t size)
252
{
253
	MvpData *md = data;
254
	ssize_t ret;
255 256 257

	/* reopen the device since it was closed by dropBufferedAudio */
	if (md->fd < 0)
258
		mvp_openDevice(md, &md->audio_format);
259 260 261 262 263 264

	while (size > 0) {
		ret = write(md->fd, playChunk, size);
		if (ret < 0) {
			if (errno == EINTR)
				continue;
265 266
			g_warning("closing mvp PCM device due to write error: "
				  "%s\n", strerror(errno));
267
			return false;
268 269 270 271
		}
		playChunk += ret;
		size -= ret;
	}
272
	return true;
273 274
}

275
const struct audio_output_plugin mvpPlugin = {
276 277 278 279 280 281 282 283
	.name = "mvp",
	.test_default_device = mvp_testDefault,
	.init = mvp_initDriver,
	.finish = mvp_finishDriver,
	.open = mvp_openDevice,
	.play = mvp_playAudio,
	.cancel = mvp_dropBufferedAudio,
	.close = mvp_closeDevice,
284
};