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 "playerData.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "log.h"
|
2008-04-12 06:17:12 +02:00
|
|
|
#include "utils.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:07:01 +02:00
|
|
|
unsigned int buffered_before_play;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-05-27 15:12:02 +02:00
|
|
|
#define DEFAULT_BUFFER_SIZE 2048
|
|
|
|
#define DEFAULT_BUFFER_BEFORE_PLAY 10
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2008-04-12 06:17:55 +02:00
|
|
|
static PlayerData playerData_pd;
|
2008-04-13 03:15:50 +02:00
|
|
|
PlayerControl pc;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void initPlayerData(void)
|
|
|
|
{
|
2004-10-28 07:14:55 +02:00
|
|
|
float perc = DEFAULT_BUFFER_BEFORE_PLAY;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *test;
|
2004-02-24 00:41:20 +01:00
|
|
|
int crossfade = 0;
|
2004-10-28 07:14:55 +02:00
|
|
|
size_t bufferSize = DEFAULT_BUFFER_SIZE;
|
2008-04-12 06:18:12 +02:00
|
|
|
unsigned int buffered_chunks;
|
2006-07-20 18:02:40 +02:00
|
|
|
ConfigParam *param;
|
2006-08-01 12:07:07 +02:00
|
|
|
size_t device_array_size = audio_device_count() * sizeof(mpd_sint8);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
param = getConfigParam(CONF_AUDIO_BUFFER_SIZE);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (param) {
|
2004-10-28 07:14:55 +02:00
|
|
|
bufferSize = strtol(param->value, &test, 10);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (*test != '\0' || bufferSize <= 0) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("buffer size \"%s\" is not a positive integer, "
|
2006-07-20 18:02:40 +02:00
|
|
|
"line %i\n", param->value, param->line);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
bufferSize *= 1024;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
buffered_chunks = bufferSize / CHUNK_SIZE;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (buffered_chunks >= 1 << 15) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("buffer size \"%li\" is too big\n", (long)bufferSize);
|
2004-04-01 02:33:35 +02:00
|
|
|
}
|
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
param = getConfigParam(CONF_BUFFER_BEFORE_PLAY);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (param) {
|
2004-10-28 07:14:55 +02:00
|
|
|
perc = strtod(param->value, &test);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (*test != '%' || perc < 0 || perc > 100) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("buffered before play \"%s\" is not a positive "
|
2006-07-20 18:02:40 +02:00
|
|
|
"percentage and less than 100 percent, line %i"
|
|
|
|
"\n", param->value, param->line);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
buffered_before_play = (perc / 100) * buffered_chunks;
|
|
|
|
if (buffered_before_play > buffered_chunks) {
|
2004-02-25 22:10:56 +01:00
|
|
|
buffered_before_play = buffered_chunks;
|
2008-04-12 06:07:01 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:17:55 +02:00
|
|
|
playerData_pd.audioDeviceStates = xmalloc(device_array_size);
|
|
|
|
|
2008-04-12 06:18:04 +02:00
|
|
|
initOutputBuffer(&(playerData_pd.buffer), buffered_chunks);
|
2008-04-12 06:17:55 +02:00
|
|
|
|
2008-04-13 03:15:50 +02:00
|
|
|
notifyInit(&pc.notify);
|
|
|
|
pc.error = PLAYER_ERROR_NOERROR;
|
|
|
|
pc.state = PLAYER_STATE_STOP;
|
|
|
|
pc.queueState = PLAYER_QUEUE_BLANK;
|
|
|
|
pc.queueLockState = PLAYER_QUEUE_UNLOCKED;
|
|
|
|
pc.crossFade = crossfade;
|
|
|
|
pc.softwareVolume = 1000;
|
2008-04-12 06:17:55 +02:00
|
|
|
|
|
|
|
notifyInit(&playerData_pd.decoderControl.notify);
|
|
|
|
playerData_pd.decoderControl.stop = 0;
|
|
|
|
playerData_pd.decoderControl.start = 0;
|
|
|
|
playerData_pd.decoderControl.state = DECODE_STATE_STOP;
|
|
|
|
playerData_pd.decoderControl.seek = 0;
|
|
|
|
playerData_pd.decoderControl.error = DECODE_ERROR_NOERROR;
|
|
|
|
playerData_pd.decoderControl.current_song = NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
PlayerData *getPlayerData(void)
|
|
|
|
{
|
2008-04-12 06:17:55 +02:00
|
|
|
return &playerData_pd;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void freePlayerData(void)
|
|
|
|
{
|
2006-08-15 01:31:08 +02:00
|
|
|
/* We don't want to release this memory until we know our player and
|
|
|
|
* decoder have exited. Otherwise, their signal handlers will want to
|
|
|
|
* access playerData_pd and we need to keep it available for them */
|
|
|
|
waitpid(-1, NULL, 0);
|
2008-04-12 06:17:12 +02:00
|
|
|
|
2008-04-12 06:18:38 +02:00
|
|
|
output_buffer_free(&playerData_pd.buffer);
|
2008-04-12 06:17:55 +02:00
|
|
|
free(playerData_pd.audioDeviceStates);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|