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"
|
2008-09-07 19:19:55 +02:00
|
|
|
#include "audio_format.h"
|
2008-09-07 22:41:22 +02:00
|
|
|
#include "output_api.h"
|
2008-09-09 10:02:34 +02:00
|
|
|
#include "output_control.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "log.h"
|
2008-04-12 06:19:26 +02:00
|
|
|
#include "path.h"
|
2008-09-07 14:04:16 +02:00
|
|
|
#include "client.h"
|
2008-09-07 22:41:17 +02:00
|
|
|
#include "utils.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:"
|
2007-12-28 03:56:25 +01:00
|
|
|
#define AUDIO_BUFFER_SIZE 2*MPD_PATH_MAX
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-10 11:44:06 +02:00
|
|
|
static struct audio_format audio_configFormat;
|
2004-05-10 04:20:15 +02:00
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static struct audio_output *audioOutputArray;
|
2008-03-26 11:37:54 +01:00
|
|
|
static unsigned int audioOutputArraySize;
|
2006-08-01 12:07:12 +02:00
|
|
|
|
2008-06-02 12:10:26 +02:00
|
|
|
enum ad_state {
|
|
|
|
DEVICE_OFF = 0x00,
|
|
|
|
DEVICE_ENABLE = 0x01, /* currently off, but to be turned on */
|
|
|
|
DEVICE_ON = 0x03,
|
|
|
|
DEVICE_DISABLE = 0x04 /* currently on, but to be turned off */
|
|
|
|
};
|
2006-08-01 12:07:12 +02:00
|
|
|
|
2004-11-02 22:06:44 +01:00
|
|
|
/* the audioEnabledArray should be stuck into shared memory, and then disable
|
|
|
|
and enable in playAudio() routine */
|
2008-06-02 12:10:26 +02:00
|
|
|
static enum ad_state *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
|
|
|
|
2008-09-10 11:43:49 +02:00
|
|
|
static struct {
|
|
|
|
struct audio_format format;
|
|
|
|
|
|
|
|
size_t size, position;
|
|
|
|
char *buffer;
|
|
|
|
} audio_buffer;
|
2004-11-09 01:13:42 +01:00
|
|
|
|
2008-06-02 12:10:26 +02:00
|
|
|
static unsigned int audio_output_count(void)
|
2006-08-01 12:07:07 +02:00
|
|
|
{
|
2008-03-26 11:38:21 +01:00
|
|
|
unsigned int nr = 0;
|
2006-08-01 12:07:07 +02:00
|
|
|
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-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
|
|
|
|
2008-06-02 12:10:26 +02:00
|
|
|
audioOutputArraySize = audio_output_count();
|
|
|
|
audioDeviceStates = xmalloc(sizeof(enum ad_state) *
|
|
|
|
audioOutputArraySize);
|
2008-09-07 22:41:22 +02:00
|
|
|
audioOutputArray = xmalloc(sizeof(struct audio_output) * audioOutputArraySize);
|
2004-11-02 22:48:53 +01:00
|
|
|
|
2006-10-03 04:57:01 +02:00
|
|
|
for (i = 0; i < audioOutputArraySize; i++)
|
|
|
|
{
|
2008-09-07 22:41:22 +02:00
|
|
|
struct audio_output *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));
|
|
|
|
|
2008-09-09 10:03:24 +02:00
|
|
|
if (!audio_output_init(output, param)) {
|
2006-10-03 04:57:01 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-09-07 19:19:55 +02:00
|
|
|
void getOutputAudioFormat(const struct audio_format *inAudioFormat,
|
|
|
|
struct audio_format *outAudioFormat)
|
2004-05-10 04:20:15 +02:00
|
|
|
{
|
2008-09-10 11:44:06 +02:00
|
|
|
*outAudioFormat = audio_format_defined(&audio_configFormat)
|
|
|
|
? audio_configFormat
|
2008-09-09 10:04:42 +02:00
|
|
|
: *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
|
|
|
|
2008-09-10 11:44:06 +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
|
|
|
}
|
|
|
|
|
2008-09-07 19:19:55 +02:00
|
|
|
int parseAudioConfig(struct audio_format *audioFormat, char *conf)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
char *test;
|
|
|
|
|
2008-09-07 19:19:55 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2008-09-10 11:44:06 +02:00
|
|
|
audio_format_clear(&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++) {
|
2008-09-09 10:03:24 +02:00
|
|
|
audio_output_finish(&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
|
|
|
}
|
|
|
|
|
2008-09-07 19:19:55 +02:00
|
|
|
int isCurrentAudioFormat(const struct audio_format *audioFormat)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-10 11:43:40 +02:00
|
|
|
assert(audioFormat != NULL);
|
|
|
|
|
2008-09-10 11:43:49 +02:00
|
|
|
return audio_format_equals(audioFormat, &audio_buffer.format);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-08-01 14:00:17 +02:00
|
|
|
static void syncAudioDeviceStates(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-07 22:41:22 +02:00
|
|
|
struct audio_output *audioOutput;
|
2008-03-26 11:37:54 +01:00
|
|
|
unsigned int i;
|
2004-11-09 01:13:42 +01:00
|
|
|
|
2008-09-10 11:43:49 +02:00
|
|
|
if (!audio_format_defined(&audio_buffer.format))
|
2006-08-01 14:00:17 +02:00
|
|
|
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]) {
|
2008-06-02 12:10:26 +02:00
|
|
|
case DEVICE_OFF:
|
|
|
|
break;
|
2006-08-07 19:51:15 +02:00
|
|
|
case DEVICE_ON:
|
|
|
|
/* This will reopen only if the audio format changed */
|
2008-09-10 11:43:49 +02:00
|
|
|
if (audio_output_open(audioOutput,
|
|
|
|
&audio_buffer.format) < 0)
|
2007-06-09 23:40:56 +02:00
|
|
|
audioDeviceStates[i] = DEVICE_ENABLE;
|
2006-08-07 19:51:15 +02:00
|
|
|
break;
|
2006-08-01 12:07:12 +02:00
|
|
|
case DEVICE_ENABLE:
|
2008-09-10 11:43:49 +02:00
|
|
|
if (audio_output_open(audioOutput,
|
|
|
|
&audio_buffer.format) == 0)
|
2007-06-09 23:40:56 +02:00
|
|
|
audioDeviceStates[i] = DEVICE_ON;
|
2006-08-01 12:07:12 +02:00
|
|
|
break;
|
|
|
|
case DEVICE_DISABLE:
|
2008-09-09 10:03:24 +02:00
|
|
|
audio_output_cancel(audioOutput);
|
|
|
|
audio_output_close(audioOutput);
|
2006-08-01 12:07:12 +02:00
|
|
|
audioDeviceStates[i] = DEVICE_OFF;
|
|
|
|
}
|
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
|
|
|
|
2008-09-10 11:43:49 +02:00
|
|
|
if (audio_buffer.position == 0)
|
2006-08-01 14:00:17 +02:00
|
|
|
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;
|
2008-09-10 11:43:49 +02:00
|
|
|
err = audio_output_play(&audioOutputArray[i],
|
|
|
|
audio_buffer.buffer,
|
|
|
|
audio_buffer.position);
|
2006-08-01 14:00:17 +02:00
|
|
|
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
|
|
|
|
2008-09-10 11:43:49 +02:00
|
|
|
audio_buffer.position = 0;
|
2004-11-09 01:13:42 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-10 11:43:56 +02:00
|
|
|
static size_t audio_buffer_size(const struct audio_format *af)
|
|
|
|
{
|
|
|
|
return (af->bits >> 3) * af->channels * (af->sampleRate >> 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_buffer_resize(size_t size)
|
|
|
|
{
|
2008-09-10 11:46:54 +02:00
|
|
|
assert(audio_buffer.position == 0);
|
|
|
|
assert((audio_buffer.size == 0) == (audio_buffer.buffer == NULL));
|
|
|
|
|
2008-09-10 11:43:56 +02:00
|
|
|
if (audio_buffer.size == size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (audio_buffer.buffer != NULL)
|
|
|
|
free(audio_buffer.buffer);
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
audio_buffer.buffer = xmalloc(size);
|
|
|
|
else
|
|
|
|
audio_buffer.buffer = NULL;
|
|
|
|
|
|
|
|
audio_buffer.size = size;
|
|
|
|
}
|
|
|
|
|
2008-09-07 19:19:55 +02:00
|
|
|
int openAudioDevice(const struct audio_format *audioFormat)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
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
|
|
|
|
2008-09-10 11:43:40 +02:00
|
|
|
if (!audioOpened ||
|
|
|
|
(audioFormat != NULL && !isCurrentAudioFormat(audioFormat))) {
|
2004-11-09 01:13:42 +01:00
|
|
|
flushAudioBuffer();
|
2008-09-09 10:04:42 +02:00
|
|
|
if (audioFormat != NULL)
|
2008-09-10 11:43:49 +02:00
|
|
|
audio_buffer.format = *audioFormat;
|
2008-09-10 11:43:56 +02:00
|
|
|
audio_buffer_resize(audio_buffer_size(&audio_buffer.format));
|
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) {
|
2008-09-09 10:03:24 +02:00
|
|
|
audio_output_close(&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-04-12 06:16:08 +02:00
|
|
|
int playAudio(const char *playChunk, size_t size)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-04-12 06:16:08 +02:00
|
|
|
size_t send_size;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
while (size > 0) {
|
2008-09-10 11:43:49 +02:00
|
|
|
send_size = audio_buffer.size - audio_buffer.position;
|
2008-01-26 13:46:21 +01:00
|
|
|
send_size = send_size < size ? send_size : size;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2008-09-10 11:46:54 +02:00
|
|
|
assert(send_size > 0);
|
|
|
|
|
2008-09-10 11:43:49 +02:00
|
|
|
memcpy(audio_buffer.buffer + audio_buffer.position, playChunk, send_size);
|
|
|
|
audio_buffer.position += send_size;
|
2008-01-26 13:46:21 +01:00
|
|
|
size -= send_size;
|
|
|
|
playChunk += send_size;
|
2004-11-02 22:06:44 +01:00
|
|
|
|
2008-09-10 11:43:49 +02:00
|
|
|
if (audio_buffer.position == audio_buffer.size) {
|
2006-07-20 18:02:40 +02:00
|
|
|
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
|
|
|
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();
|
2008-09-10 11:43:49 +02:00
|
|
|
audio_buffer.position = 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)
|
2008-09-09 10:03:24 +02:00
|
|
|
audio_output_cancel(&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
|
|
|
|
2008-09-10 11:44:02 +02:00
|
|
|
if (audio_buffer.buffer != NULL) {
|
|
|
|
flushAudioBuffer();
|
|
|
|
free(audio_buffer.buffer);
|
|
|
|
audio_buffer.buffer = NULL;
|
|
|
|
audio_buffer.size = 0;
|
|
|
|
}
|
2004-11-09 01:13:42 +01: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)
|
|
|
|
audioDeviceStates[i] = DEVICE_ENABLE;
|
2008-09-09 10:03:24 +02:00
|
|
|
audio_output_close(&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
|
|
|
|
2008-08-29 09:38:11 +02:00
|
|
|
void sendMetadataToAudioDevice(const struct tag *tag)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
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) {
|
2008-09-09 10:03:24 +02:00
|
|
|
audio_output_send_tag(&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-09-07 13:51:50 +02:00
|
|
|
int enableAudioDevice(unsigned int device)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-07 13:51:50 +02:00
|
|
|
if (device >= audioOutputArraySize)
|
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-09-07 13:51:50 +02:00
|
|
|
int disableAudioDevice(unsigned int device)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-07 13:51:50 +02:00
|
|
|
if (device >= audioOutputArraySize)
|
2004-11-02 22:06:44 +01:00
|
|
|
return -1;
|
2008-09-07 13:51:50 +02:00
|
|
|
|
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
|
|
|
|
2008-09-07 14:04:16 +02:00
|
|
|
void printAudioDevices(struct client *client)
|
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++) {
|
2008-09-07 14:04:16 +02:00
|
|
|
client_printf(client,
|
|
|
|
"outputid: %i\n"
|
|
|
|
"outputname: %s\n"
|
|
|
|
"outputenabled: %i\n",
|
|
|
|
i,
|
|
|
|
audioOutputArray[i].name,
|
|
|
|
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
|
|
|
|
2008-09-23 20:48:12 +02:00
|
|
|
if (prefixcmp(buffer, AUDIO_DEVICE_STATE))
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|