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-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:07:01 +02:00
|
|
|
unsigned int buffered_before_play;
|
|
|
|
unsigned int buffered_chunks;
|
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
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static PlayerData *playerData_pd;
|
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 shmid;
|
|
|
|
int crossfade = 0;
|
2004-10-28 07:14:55 +02:00
|
|
|
size_t bufferSize = DEFAULT_BUFFER_SIZE;
|
2004-02-24 00:41:20 +01:00
|
|
|
size_t allocationSize;
|
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:13:24 +02:00
|
|
|
allocationSize = buffered_chunks * sizeof(OutputBufferChunk); /*actual buffer */
|
2006-07-20 18:02:40 +02:00
|
|
|
allocationSize += buffered_chunks * sizeof(float); /*for times */
|
|
|
|
allocationSize += buffered_chunks * sizeof(mpd_sint16); /*for chunkSize */
|
|
|
|
allocationSize += buffered_chunks * sizeof(mpd_sint16); /*for bitRate */
|
|
|
|
allocationSize += sizeof(PlayerData); /*for playerData struct */
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-08-01 12:07:12 +02:00
|
|
|
/* for audioDeviceStates[] */
|
2006-08-01 12:07:07 +02:00
|
|
|
allocationSize += device_array_size;
|
|
|
|
|
2006-08-06 08:40:11 +02:00
|
|
|
if ((shmid = shmget(IPC_PRIVATE, allocationSize, IPC_CREAT | 0600)) < 0)
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("problems shmget'ing\n");
|
2006-08-06 08:40:11 +02:00
|
|
|
if (!(playerData_pd = shmat(shmid, NULL, 0)))
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("problems shmat'ing\n");
|
2006-08-06 08:40:11 +02:00
|
|
|
if (shmctl(shmid, IPC_RMID, NULL) < 0)
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("problems shmctl'ing\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-08-01 12:07:12 +02:00
|
|
|
playerData_pd->audioDeviceStates = (mpd_uint8 *)playerData_pd +
|
2006-08-01 12:07:07 +02:00
|
|
|
allocationSize - device_array_size;
|
2008-03-26 11:38:12 +01:00
|
|
|
|
|
|
|
initOutputBuffer(&(playerData_pd->buffer),
|
2008-04-12 06:13:24 +02:00
|
|
|
(OutputBufferChunk*)(((char *)playerData_pd) + sizeof(PlayerData)));
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
playerData_pd->playerControl.stop = 0;
|
|
|
|
playerData_pd->playerControl.pause = 0;
|
|
|
|
playerData_pd->playerControl.play = 0;
|
|
|
|
playerData_pd->playerControl.error = PLAYER_ERROR_NOERROR;
|
|
|
|
playerData_pd->playerControl.lockQueue = 0;
|
|
|
|
playerData_pd->playerControl.unlockQueue = 0;
|
|
|
|
playerData_pd->playerControl.state = PLAYER_STATE_STOP;
|
|
|
|
playerData_pd->playerControl.queueState = PLAYER_QUEUE_BLANK;
|
|
|
|
playerData_pd->playerControl.queueLockState = PLAYER_QUEUE_UNLOCKED;
|
|
|
|
playerData_pd->playerControl.seek = 0;
|
2006-03-16 07:41:48 +01:00
|
|
|
playerData_pd->playerControl.closeAudio = 0;
|
2008-04-12 06:08:29 +02:00
|
|
|
playerData_pd->playerControl.current_song = NULL;
|
|
|
|
playerData_pd->playerControl.errored_song = NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
playerData_pd->playerControl.crossFade = crossfade;
|
2004-04-11 13:48:04 +02:00
|
|
|
playerData_pd->playerControl.softwareVolume = 1000;
|
2004-02-24 00:41:20 +01:00
|
|
|
playerData_pd->playerControl.totalPlayTime = 0;
|
|
|
|
|
2008-04-12 06:14:32 +02:00
|
|
|
notifyInit(&playerData_pd->decoderControl.notify);
|
2004-02-24 00:41:20 +01:00
|
|
|
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;
|
2008-04-12 06:08:29 +02:00
|
|
|
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)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
return playerData_pd;
|
|
|
|
}
|
|
|
|
|
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);
|
2004-02-24 00:41:20 +01:00
|
|
|
shmdt(playerData_pd);
|
|
|
|
}
|