2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "audio.h"
|
2004-10-20 18:48:22 +02:00
|
|
|
#include "audioOutput.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "conf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "sig_handlers.h"
|
2004-11-02 22:06:44 +01:00
|
|
|
#include "command.h"
|
2004-11-02 22:48:53 +01:00
|
|
|
#include "playerData.h"
|
2005-08-23 14:01:37 +02:00
|
|
|
#include "utils.h"
|
2006-07-31 01:32:47 +02:00
|
|
|
#include "state_file.h"
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2006-03-18 04:58:31 +01:00
|
|
|
#define AUDIO_DEVICE_STATE "audio_device_state:"
|
|
|
|
#define AUDIO_DEVICE_STATE_LEN 19 /* strlen(AUDIO_DEVICE_STATE) */
|
2007-12-28 03:56:25 +01:00
|
|
|
#define AUDIO_BUFFER_SIZE 2*MPD_PATH_MAX
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-01-14 04:07:53 +01:00
|
|
|
static AudioFormat audio_format;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-01-14 04:07:53 +01:00
|
|
|
static AudioFormat *audio_configFormat;
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2007-01-14 04:07:53 +01:00
|
|
|
static AudioOutput *audioOutputArray;
|
2008-03-26 11:37:54 +01:00
|
|
|
static unsigned int audioOutputArraySize;
|
2006-08-01 12:07:12 +02:00
|
|
|
|
|
|
|
#define DEVICE_OFF 0x00
|
|
|
|
#define DEVICE_ENABLE 0x01 /* currently off, but to be turned on */
|
|
|
|
#define DEVICE_ON 0x03
|
|
|
|
#define DEVICE_DISABLE 0x04 /* currently on, but to be turned off */
|
|
|
|
|
2004-11-02 22:06:44 +01:00
|
|
|
/* the audioEnabledArray should be stuck into shared memory, and then disable
|
|
|
|
and enable in playAudio() routine */
|
2007-01-14 04:07:53 +01:00
|
|
|
static mpd_uint8 *audioDeviceStates;
|
2004-11-02 22:06:44 +01:00
|
|
|
|
2007-01-14 04:07:53 +01:00
|
|
|
static mpd_uint8 audioOpened;
|
2004-11-02 22:06:44 +01:00
|
|
|
|
2007-01-14 04:07:53 +01:00
|
|
|
static mpd_sint32 audioBufferSize;
|
|
|
|
static char *audioBuffer;
|
|
|
|
static mpd_sint32 audioBufferPos;
|
2004-11-09 01:13:42 +01:00
|
|
|
|
2008-03-26 11:37:54 +01:00
|
|
|
unsigned int audio_device_count(void)
|
2006-08-01 12:07:07 +02:00
|
|
|
{
|
|
|
|
size_t nr = 0;
|
|
|
|
ConfigParam *param = NULL;
|
|
|
|
|
|
|
|
while ((param = getNextConfigParam(CONF_AUDIO_OUTPUT, param)))
|
|
|
|
nr++;
|
|
|
|
if (!nr)
|
|
|
|
nr = 1; /* we'll always have at least one device */
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void copyAudioFormat(AudioFormat * dest, AudioFormat * src)
|
|
|
|
{
|
|
|
|
if (!src)
|
|
|
|
return;
|
2004-10-22 18:16:39 +02:00
|
|
|
|
2004-11-02 18:05:27 +01:00
|
|
|
memcpy(dest, src, sizeof(AudioFormat));
|
|
|
|
}
|
|
|
|
|
2006-04-05 11:54:43 +02:00
|
|
|
int cmpAudioFormat(AudioFormat * f1, AudioFormat * f2)
|
|
|
|
{
|
2006-08-07 22:00:00 +02:00
|
|
|
if (f1 && f2 && (f1->sampleRate == f2->sampleRate) &&
|
|
|
|
(f1->bits == f2->bits) && (f1->channels == f2->channels))
|
2006-04-05 11:54:43 +02:00
|
|
|
return 0;
|
|
|
|
return 1;
|
2004-05-10 04:20:15 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void loadAudioDrivers(void)
|
|
|
|
{
|
2004-10-20 18:48:22 +02:00
|
|
|
initAudioOutputPlugins();
|
2007-06-12 19:49:31 +02:00
|
|
|
loadAudioOutputPlugin(&shoutPlugin);
|
2007-05-30 18:52:56 +02:00
|
|
|
loadAudioOutputPlugin(&nullPlugin);
|
2007-06-13 16:15:30 +02:00
|
|
|
loadAudioOutputPlugin(&fifoPlugin);
|
2005-03-05 06:22:30 +01:00
|
|
|
loadAudioOutputPlugin(&alsaPlugin);
|
2004-10-20 18:48:22 +02:00
|
|
|
loadAudioOutputPlugin(&aoPlugin);
|
2004-11-02 03:03:00 +01:00
|
|
|
loadAudioOutputPlugin(&ossPlugin);
|
2005-03-13 20:23:09 +01:00
|
|
|
loadAudioOutputPlugin(&osxPlugin);
|
2006-07-13 21:03:49 +02:00
|
|
|
loadAudioOutputPlugin(&pulsePlugin);
|
2005-08-11 14:34:38 +02:00
|
|
|
loadAudioOutputPlugin(&mvpPlugin);
|
2006-10-18 04:49:13 +02:00
|
|
|
loadAudioOutputPlugin(&jackPlugin);
|
2006-07-16 16:57:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure initPlayerData is called before this function!! */
|
2006-07-20 18:02:40 +02:00
|
|
|
void initAudioDriver(void)
|
|
|
|
{
|
|
|
|
ConfigParam *param = NULL;
|
2008-03-26 11:37:54 +01:00
|
|
|
unsigned int i;
|
2006-07-16 16:57:26 +02:00
|
|
|
|
|
|
|
loadAudioDrivers();
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-08-01 12:07:07 +02:00
|
|
|
audioOutputArraySize = audio_device_count();
|
2006-08-01 12:07:12 +02:00
|
|
|
audioDeviceStates = (getPlayerData())->audioDeviceStates;
|
2006-08-26 08:25:57 +02:00
|
|
|
audioOutputArray = xmalloc(sizeof(AudioOutput) * audioOutputArraySize);
|
2004-11-02 22:48:53 +01:00
|
|
|
|
2006-10-03 04:57:01 +02:00
|
|
|
for (i = 0; i < audioOutputArraySize; i++)
|
|
|
|
{
|
2006-08-01 12:07:16 +02:00
|
|
|
AudioOutput *output = &audioOutputArray[i];
|
2008-03-26 11:37:54 +01:00
|
|
|
unsigned int j;
|
2006-03-18 04:58:31 +01:00
|
|
|
|
2006-10-03 04:57:01 +02:00
|
|
|
param = getNextConfigParam(CONF_AUDIO_OUTPUT, param);
|
|
|
|
|
2006-10-03 05:06:30 +02:00
|
|
|
/* only allow param to be NULL if there just one audioOutput */
|
|
|
|
assert(param || (audioOutputArraySize == 1));
|
|
|
|
|
2006-10-03 04:57:01 +02:00
|
|
|
if (!initAudioOutput(output, param)) {
|
|
|
|
if (param)
|
|
|
|
{
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("problems configuring output device "
|
2006-10-03 04:57:01 +02:00
|
|
|
"defined at line %i\n", param->line);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("No audio_output specified and unable to "
|
2006-10-03 04:57:01 +02:00
|
|
|
"detect a default audio output device\n");
|
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2006-03-18 04:58:31 +01:00
|
|
|
|
|
|
|
/* require output names to be unique: */
|
2006-10-03 04:57:01 +02:00
|
|
|
for (j = 0; j < i; j++) {
|
2006-08-01 12:07:16 +02:00
|
|
|
if (!strcmp(output->name, audioOutputArray[j].name)) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("output devices with identical "
|
2006-07-20 18:02:40 +02:00
|
|
|
"names: %s\n", output->name);
|
2006-03-18 04:58:31 +01:00
|
|
|
}
|
|
|
|
}
|
2006-10-03 04:57:01 +02:00
|
|
|
audioDeviceStates[i] = DEVICE_ENABLE;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void getOutputAudioFormat(AudioFormat * inAudioFormat,
|
|
|
|
AudioFormat * outAudioFormat)
|
2004-05-10 04:20:15 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
if (audio_configFormat) {
|
|
|
|
copyAudioFormat(outAudioFormat, audio_configFormat);
|
|
|
|
} else
|
|
|
|
copyAudioFormat(outAudioFormat, inAudioFormat);
|
2004-05-10 04:20:15 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void initAudioConfig(void)
|
|
|
|
{
|
|
|
|
ConfigParam *param = getConfigParam(CONF_AUDIO_OUTPUT_FORMAT);
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (NULL == param || NULL == param->value)
|
|
|
|
return;
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2006-08-26 08:25:57 +02:00
|
|
|
audio_configFormat = xmalloc(sizeof(AudioFormat));
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (0 != parseAudioConfig(audio_configFormat, param->value)) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("error parsing \"%s\" at line %i\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
CONF_AUDIO_OUTPUT_FORMAT, param->line);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2004-10-23 03:04:58 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int parseAudioConfig(AudioFormat * audioFormat, char *conf)
|
|
|
|
{
|
|
|
|
char *test;
|
|
|
|
|
|
|
|
memset(audioFormat, 0, sizeof(AudioFormat));
|
2004-10-23 03:04:58 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
audioFormat->sampleRate = strtol(conf, &test, 10);
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (*test != ':') {
|
|
|
|
ERROR("error parsing audio output format: %s\n", conf);
|
2004-10-23 03:04:58 +02:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*switch(audioFormat->sampleRate) {
|
|
|
|
case 48000:
|
|
|
|
case 44100:
|
|
|
|
case 32000:
|
|
|
|
case 16000:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("sample rate %i can not be used for audio output\n",
|
|
|
|
(int)audioFormat->sampleRate);
|
|
|
|
return -1
|
|
|
|
} */
|
|
|
|
|
|
|
|
if (audioFormat->sampleRate <= 0) {
|
|
|
|
ERROR("sample rate %i is not >= 0\n",
|
|
|
|
(int)audioFormat->sampleRate);
|
2004-10-23 03:04:58 +02:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
2008-03-26 11:37:17 +01:00
|
|
|
audioFormat->bits = (mpd_sint8)strtol(test + 1, &test, 10);
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (*test != ':') {
|
|
|
|
ERROR("error parsing audio output format: %s\n", conf);
|
2004-10-23 03:04:58 +02:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (audioFormat->bits) {
|
|
|
|
case 16:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("bits %i can not be used for audio output\n",
|
|
|
|
(int)audioFormat->bits);
|
2004-10-23 03:04:58 +02:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2008-03-26 11:37:17 +01:00
|
|
|
audioFormat->channels = (mpd_sint8)strtol(test + 1, &test, 10);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (*test != '\0') {
|
|
|
|
ERROR("error parsing audio output format: %s\n", conf);
|
2004-10-23 03:04:58 +02:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (audioFormat->channels) {
|
2004-10-23 03:04:58 +02:00
|
|
|
case 1:
|
2006-07-20 18:02:40 +02:00
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("channels %i can not be used for audio output\n",
|
|
|
|
(int)audioFormat->channels);
|
2004-10-23 03:04:58 +02:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-10-23 03:04:58 +02:00
|
|
|
|
|
|
|
return 0;
|
2004-05-10 04:20:15 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void finishAudioConfig(void)
|
|
|
|
{
|
|
|
|
if (audio_configFormat)
|
|
|
|
free(audio_configFormat);
|
2004-05-10 04:20:15 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void finishAudioDriver(void)
|
|
|
|
{
|
2008-03-26 11:37:54 +01:00
|
|
|
unsigned int i;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < audioOutputArraySize; i++) {
|
2006-08-01 12:07:16 +02:00
|
|
|
finishAudioOutput(&audioOutputArray[i]);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free(audioOutputArray);
|
|
|
|
audioOutputArray = NULL;
|
2004-11-03 00:08:00 +01:00
|
|
|
audioOutputArraySize = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int isCurrentAudioFormat(AudioFormat * audioFormat)
|
|
|
|
{
|
|
|
|
if (!audioFormat)
|
|
|
|
return 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (cmpAudioFormat(audioFormat, &audio_format) != 0)
|
|
|
|
return 0;
|
2004-10-20 19:11:04 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-08-01 14:00:17 +02:00
|
|
|
static void syncAudioDeviceStates(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2007-06-09 23:40:56 +02:00
|
|
|
AudioOutput *audioOutput;
|
2008-03-26 11:37:54 +01:00
|
|
|
unsigned int i;
|
2004-11-09 01:13:42 +01:00
|
|
|
|
2006-08-01 14:00:17 +02:00
|
|
|
if (!audio_format.channels)
|
|
|
|
return;
|
2007-06-09 23:40:56 +02:00
|
|
|
|
|
|
|
for (i = 0; i < audioOutputArraySize; ++i) {
|
|
|
|
audioOutput = &audioOutputArray[i];
|
2006-08-01 12:07:12 +02:00
|
|
|
switch (audioDeviceStates[i]) {
|
2006-08-07 19:51:15 +02:00
|
|
|
case DEVICE_ON:
|
|
|
|
/* This will reopen only if the audio format changed */
|
2007-06-09 23:40:56 +02:00
|
|
|
if (openAudioOutput(audioOutput, &audio_format) < 0)
|
|
|
|
audioDeviceStates[i] = DEVICE_ENABLE;
|
2006-08-07 19:51:15 +02:00
|
|
|
break;
|
2006-08-01 12:07:12 +02:00
|
|
|
case DEVICE_ENABLE:
|
2007-06-09 23:40:56 +02:00
|
|
|
if (openAudioOutput(audioOutput, &audio_format) == 0)
|
|
|
|
audioDeviceStates[i] = DEVICE_ON;
|
2006-08-01 12:07:12 +02:00
|
|
|
break;
|
|
|
|
case DEVICE_DISABLE:
|
2007-06-09 23:40:56 +02:00
|
|
|
dropBufferedAudioOutput(audioOutput);
|
|
|
|
closeAudioOutput(audioOutput);
|
2006-08-01 12:07:12 +02:00
|
|
|
audioDeviceStates[i] = DEVICE_OFF;
|
|
|
|
break;
|
|
|
|
}
|
2004-11-09 01:13:42 +01:00
|
|
|
}
|
2006-08-01 14:00:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int flushAudioBuffer(void)
|
|
|
|
{
|
2008-03-26 11:37:54 +01:00
|
|
|
int ret = -1, err;
|
|
|
|
unsigned int i;
|
2006-08-01 14:00:17 +02:00
|
|
|
|
|
|
|
if (audioBufferPos == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
syncAudioDeviceStates();
|
|
|
|
|
2006-10-03 04:22:37 +02:00
|
|
|
for (i = 0; i < audioOutputArraySize; ++i) {
|
2006-08-01 14:00:17 +02:00
|
|
|
if (audioDeviceStates[i] != DEVICE_ON)
|
|
|
|
continue;
|
|
|
|
err = playAudioOutput(&audioOutputArray[i], audioBuffer,
|
|
|
|
audioBufferPos);
|
|
|
|
if (!err)
|
|
|
|
ret = 0;
|
|
|
|
else if (err < 0)
|
|
|
|
/* device should already be closed if the play
|
|
|
|
* func returned an error */
|
|
|
|
audioDeviceStates[i] = DEVICE_ENABLE;
|
|
|
|
}
|
2004-11-09 01:13:42 +01:00
|
|
|
|
|
|
|
audioBufferPos = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int openAudioDevice(AudioFormat * audioFormat)
|
|
|
|
{
|
2004-10-28 07:14:55 +02:00
|
|
|
int ret = -1;
|
2008-03-26 11:37:54 +01:00
|
|
|
unsigned int i;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!audioOutputArray)
|
|
|
|
return -1;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-08-07 19:55:15 +02:00
|
|
|
if (!audioOpened || !isCurrentAudioFormat(audioFormat)) {
|
2004-11-09 01:13:42 +01:00
|
|
|
flushAudioBuffer();
|
2004-10-28 07:14:55 +02:00
|
|
|
copyAudioFormat(&audio_format, audioFormat);
|
2006-07-20 18:02:40 +02:00
|
|
|
audioBufferSize = (audio_format.bits >> 3) *
|
|
|
|
audio_format.channels;
|
|
|
|
audioBufferSize *= audio_format.sampleRate >> 5;
|
2006-08-26 08:25:57 +02:00
|
|
|
audioBuffer = xrealloc(audioBuffer, audioBufferSize);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-08-01 14:00:17 +02:00
|
|
|
syncAudioDeviceStates();
|
|
|
|
|
2006-10-03 04:22:37 +02:00
|
|
|
for (i = 0; i < audioOutputArraySize; ++i) {
|
2006-08-01 14:00:17 +02:00
|
|
|
if (audioOutputArray[i].open)
|
2006-07-20 18:02:40 +02:00
|
|
|
ret = 0;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ret == 0)
|
|
|
|
audioOpened = 1;
|
2004-11-02 22:06:44 +01:00
|
|
|
else {
|
|
|
|
/* close all devices if there was an error */
|
2006-10-03 04:22:37 +02:00
|
|
|
for (i = 0; i < audioOutputArraySize; ++i) {
|
2006-08-01 12:07:16 +02:00
|
|
|
closeAudioOutput(&audioOutputArray[i]);
|
2004-11-02 22:06:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
audioOpened = 0;
|
|
|
|
}
|
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
return ret;
|
2004-10-20 18:16:50 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-01-01 11:10:02 +01:00
|
|
|
int playAudio(const char *playChunk, int size)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-01-26 13:46:21 +01:00
|
|
|
int send_size;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
while (size > 0) {
|
2008-01-26 13:46:21 +01:00
|
|
|
send_size = audioBufferSize - audioBufferPos;
|
|
|
|
send_size = send_size < size ? send_size : size;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
memcpy(audioBuffer + audioBufferPos, playChunk, send_size);
|
|
|
|
audioBufferPos += send_size;
|
|
|
|
size -= send_size;
|
|
|
|
playChunk += send_size;
|
2004-11-02 22:06:44 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (audioBufferPos == audioBufferSize) {
|
|
|
|
if (flushAudioBuffer() < 0)
|
|
|
|
return -1;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-09 01:13:42 +01:00
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int isAudioDeviceOpen(void)
|
|
|
|
{
|
2004-11-02 22:06:44 +01:00
|
|
|
return audioOpened;
|
2004-06-20 00:27:29 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void dropBufferedAudio(void)
|
|
|
|
{
|
2008-03-26 11:37:54 +01:00
|
|
|
unsigned int i;
|
2005-03-05 15:01:13 +01:00
|
|
|
|
2006-08-01 14:00:17 +02:00
|
|
|
syncAudioDeviceStates();
|
2005-03-05 15:01:13 +01:00
|
|
|
audioBufferPos = 0;
|
2006-08-01 14:00:17 +02:00
|
|
|
|
2006-10-03 04:22:37 +02:00
|
|
|
for (i = 0; i < audioOutputArraySize; ++i) {
|
2006-08-01 14:00:17 +02:00
|
|
|
if (audioDeviceStates[i] == DEVICE_ON)
|
2006-08-01 12:07:16 +02:00
|
|
|
dropBufferedAudioOutput(&audioOutputArray[i]);
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void closeAudioDevice(void)
|
|
|
|
{
|
2008-03-26 11:37:54 +01:00
|
|
|
unsigned int i;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2004-11-09 01:13:42 +01:00
|
|
|
flushAudioBuffer();
|
|
|
|
|
|
|
|
free(audioBuffer);
|
|
|
|
audioBuffer = NULL;
|
|
|
|
audioBufferSize = 0;
|
|
|
|
|
2006-10-03 04:22:37 +02:00
|
|
|
for (i = 0; i < audioOutputArraySize; ++i) {
|
2006-08-01 14:00:17 +02:00
|
|
|
if (audioDeviceStates[i] == DEVICE_ON)
|
|
|
|
audioDeviceStates[i] = DEVICE_ENABLE;
|
2006-08-01 12:07:16 +02:00
|
|
|
closeAudioOutput(&audioOutputArray[i]);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2004-11-02 22:06:44 +01:00
|
|
|
|
|
|
|
audioOpened = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-10-25 22:09:03 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void sendMetadataToAudioDevice(MpdTag * tag)
|
|
|
|
{
|
2008-03-26 11:37:54 +01:00
|
|
|
unsigned int i;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-10-03 04:22:37 +02:00
|
|
|
for (i = 0; i < audioOutputArraySize; ++i) {
|
2006-08-01 12:07:16 +02:00
|
|
|
sendMetadataToAudioOutput(&audioOutputArray[i], tag);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2004-10-25 22:09:03 +02:00
|
|
|
}
|
2004-11-02 22:06:44 +01:00
|
|
|
|
2008-03-26 11:37:54 +01:00
|
|
|
int enableAudioDevice(int fd, unsigned int device)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-03-26 11:37:17 +01:00
|
|
|
if (device >= audioOutputArraySize) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_ARG, "audio output device id %i "
|
2006-07-20 18:02:40 +02:00
|
|
|
"doesn't exist\n", device);
|
2004-11-02 22:06:44 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-01 12:07:12 +02:00
|
|
|
if (!(audioDeviceStates[device] & 0x01))
|
|
|
|
audioDeviceStates[device] = DEVICE_ENABLE;
|
2004-11-02 22:06:44 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-26 11:37:54 +01:00
|
|
|
int disableAudioDevice(int fd, unsigned int device)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-03-26 11:37:17 +01:00
|
|
|
if (device >= audioOutputArraySize) {
|
2006-07-30 05:43:38 +02:00
|
|
|
commandError(fd, ACK_ERROR_ARG, "audio output device id %i "
|
2006-07-20 18:02:40 +02:00
|
|
|
"doesn't exist\n", device);
|
2004-11-02 22:06:44 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2006-08-01 12:07:12 +02:00
|
|
|
if (audioDeviceStates[device] & 0x01)
|
|
|
|
audioDeviceStates[device] = DEVICE_DISABLE;
|
2004-11-02 22:06:44 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-03 00:08:00 +01:00
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
void printAudioDevices(int fd)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-03-26 11:37:54 +01:00
|
|
|
unsigned int i;
|
2004-11-03 00:08:00 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < audioOutputArraySize; i++) {
|
2006-07-30 05:43:38 +02:00
|
|
|
fdprintf(fd,
|
|
|
|
"outputid: %i\noutputname: %s\noutputenabled: %i\n",
|
|
|
|
i,
|
2006-08-01 12:07:16 +02:00
|
|
|
audioOutputArray[i].name,
|
2006-08-01 12:07:12 +02:00
|
|
|
audioDeviceStates[i] & 0x01);
|
2004-11-03 00:08:00 +01:00
|
|
|
}
|
|
|
|
}
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2006-07-31 01:32:47 +02:00
|
|
|
void saveAudioDevicesState(FILE *fp)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-03-26 11:37:54 +01:00
|
|
|
unsigned int i;
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2006-03-18 04:58:31 +01:00
|
|
|
assert(audioOutputArraySize != 0);
|
2006-05-20 21:56:29 +02:00
|
|
|
for (i = 0; i < audioOutputArraySize; i++) {
|
2006-07-31 01:32:39 +02:00
|
|
|
fprintf(fp, AUDIO_DEVICE_STATE "%d:%s\n",
|
2006-08-01 12:07:12 +02:00
|
|
|
audioDeviceStates[i] & 0x01,
|
2006-08-01 12:07:16 +02:00
|
|
|
audioOutputArray[i].name);
|
2005-08-23 14:01:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-31 01:32:47 +02:00
|
|
|
void readAudioDevicesState(FILE *fp)
|
2006-03-18 04:58:31 +01:00
|
|
|
{
|
|
|
|
char buffer[AUDIO_BUFFER_SIZE];
|
2008-03-26 11:37:54 +01:00
|
|
|
unsigned int i;
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2006-03-18 04:58:31 +01:00
|
|
|
assert(audioOutputArraySize != 0);
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (myFgets(buffer, AUDIO_BUFFER_SIZE, fp)) {
|
2006-03-18 04:58:31 +01:00
|
|
|
char *c, *name;
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (strncmp(buffer, AUDIO_DEVICE_STATE, AUDIO_DEVICE_STATE_LEN))
|
2006-03-18 04:58:31 +01:00
|
|
|
continue;
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
c = strchr(buffer, ':');
|
2006-03-18 04:58:31 +01:00
|
|
|
if (!c || !(++c))
|
|
|
|
goto errline;
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
name = strchr(c, ':');
|
2006-03-18 04:58:31 +01:00
|
|
|
if (!name || !(++name))
|
|
|
|
goto errline;
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2006-10-03 04:22:37 +02:00
|
|
|
for (i = 0; i < audioOutputArraySize; ++i) {
|
2006-08-01 12:07:16 +02:00
|
|
|
if (!strcmp(name, audioOutputArray[i].name)) {
|
2006-08-01 12:07:12 +02:00
|
|
|
/* devices default to on */
|
|
|
|
if (!atoi(c))
|
|
|
|
audioDeviceStates[i] = DEVICE_DISABLE;
|
2006-03-18 04:58:31 +01:00
|
|
|
break;
|
2005-08-23 14:01:37 +02:00
|
|
|
}
|
|
|
|
}
|
2006-03-18 04:58:31 +01:00
|
|
|
continue;
|
2006-08-10 00:18:06 +02:00
|
|
|
errline:
|
2006-03-18 04:58:31 +01:00
|
|
|
/* nonfatal */
|
|
|
|
ERROR("invalid line in state_file: %s\n", buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|