2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
|
|
|
* (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu)
|
|
|
|
* 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 "decode.h"
|
2004-02-27 02:35:23 +01:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "player.h"
|
|
|
|
#include "playerData.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "pcm_utils.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "log.h"
|
2004-04-11 20:27:12 +02:00
|
|
|
#include "sig_handlers.h"
|
2004-05-18 15:13:55 +02:00
|
|
|
#include "ls.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2004-04-12 20:51:16 +02:00
|
|
|
volatile int * volatile decode_pid = NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
void decodeSigHandler(int sig) {
|
|
|
|
if(sig==SIGCHLD) {
|
|
|
|
int status;
|
2004-02-27 23:25:06 +01:00
|
|
|
if(decode_pid && *decode_pid==wait3(&status,WNOHANG,NULL)) {
|
2004-04-12 01:07:43 +02:00
|
|
|
if(WIFSIGNALED(status) && WTERMSIG(status)!=SIGTERM &&
|
|
|
|
WTERMSIG(status)!=SIGINT)
|
|
|
|
{
|
|
|
|
ERROR("decode process died from signal: %i\n",
|
2004-02-24 00:41:20 +01:00
|
|
|
WTERMSIG(status));
|
|
|
|
}
|
2004-02-27 23:25:06 +01:00
|
|
|
*decode_pid = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(sig==SIGTERM) {
|
2004-02-27 23:25:06 +01:00
|
|
|
if(decode_pid) {
|
|
|
|
int pid = *decode_pid;
|
|
|
|
if(pid>0) kill(pid,SIGTERM);
|
|
|
|
}
|
2004-04-03 01:34:16 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopDecode(DecoderControl * dc) {
|
2004-02-27 23:25:06 +01:00
|
|
|
if(decode_pid && *decode_pid>0 &&
|
2004-05-19 04:14:20 +02:00
|
|
|
(dc->start || dc->state!=DECODE_STATE_STOP))
|
2004-02-27 23:25:06 +01:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
dc->stop = 1;
|
2004-05-20 02:21:58 +02:00
|
|
|
while(decode_pid && *decode_pid>0 && dc->stop) my_usleep(10000);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void quitDecode(PlayerControl * pc, DecoderControl * dc) {
|
|
|
|
stopDecode(dc);
|
|
|
|
pc->state = PLAYER_STATE_STOP;
|
2004-05-20 05:44:33 +02:00
|
|
|
dc->seek = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
pc->play = 0;
|
|
|
|
pc->stop = 0;
|
|
|
|
pc->pause = 0;
|
|
|
|
kill(getppid(),SIGUSR1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int calculateCrossFadeChunks(PlayerControl * pc, AudioFormat * af) {
|
2004-03-05 02:29:08 +01:00
|
|
|
long chunks;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
if(pc->crossFade<=0) return 0;
|
|
|
|
|
|
|
|
chunks = (af->sampleRate*af->bits*af->channels/8.0/CHUNK_SIZE);
|
|
|
|
chunks = (chunks*pc->crossFade+0.5);
|
|
|
|
|
2004-02-25 22:10:56 +01:00
|
|
|
if(chunks>(buffered_chunks-buffered_before_play)) {
|
|
|
|
chunks = buffered_chunks-buffered_before_play;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2004-02-25 18:53:48 +01:00
|
|
|
if(chunks<0) chunks = 0;
|
|
|
|
|
2004-03-05 02:29:08 +01:00
|
|
|
return (int)chunks;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2004-05-19 04:14:20 +02:00
|
|
|
#define handleDecodeStart() \
|
2004-05-20 05:44:33 +02:00
|
|
|
if(decodeWaitedOn) { \
|
|
|
|
if(dc->state!=DECODE_STATE_START && *decode_pid > 0 && \
|
|
|
|
dc->error==DECODE_ERROR_NOERROR) \
|
|
|
|
{ \
|
|
|
|
decodeWaitedOn = 0; \
|
|
|
|
if(openAudioDevice(&(cb->audioFormat))<0) { \
|
|
|
|
strncpy(pc->erroredFile,pc->file,MAXPATHLEN); \
|
|
|
|
pc->erroredFile[MAXPATHLEN] = '\0'; \
|
|
|
|
pc->error = PLAYER_ERROR_AUDIO; \
|
|
|
|
quitDecode(pc,dc); \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
pc->totalTime = dc->totalTime; \
|
|
|
|
pc->sampleRate = dc->audioFormat.sampleRate; \
|
|
|
|
pc->bits = dc->audioFormat.bits; \
|
|
|
|
pc->channels = dc->audioFormat.channels; \
|
|
|
|
} \
|
|
|
|
else if(dc->state!=DECODE_STATE_START || *decode_pid <= 0) { \
|
2004-05-19 04:14:20 +02:00
|
|
|
strncpy(pc->erroredFile,pc->file,MAXPATHLEN); \
|
|
|
|
pc->erroredFile[MAXPATHLEN] = '\0'; \
|
2004-05-20 05:44:33 +02:00
|
|
|
pc->error = PLAYER_ERROR_FILE; \
|
2004-05-19 04:14:20 +02:00
|
|
|
quitDecode(pc,dc); \
|
|
|
|
return; \
|
2004-05-20 05:44:33 +02:00
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
my_usleep(10000); \
|
|
|
|
continue; \
|
|
|
|
} \
|
2004-05-19 04:14:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int waitOnDecode(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb,
|
|
|
|
int * decodeWaitedOn)
|
|
|
|
{
|
2004-05-20 02:21:58 +02:00
|
|
|
while(decode_pid && *decode_pid>0 && dc->start) my_usleep(10000);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
if(dc->start || dc->error!=DECODE_ERROR_NOERROR) {
|
2004-02-25 19:46:41 +01:00
|
|
|
strncpy(pc->erroredFile,pc->file,MAXPATHLEN);
|
2004-03-27 03:52:59 +01:00
|
|
|
pc->erroredFile[MAXPATHLEN] = '\0';
|
2004-02-24 00:41:20 +01:00
|
|
|
pc->error = PLAYER_ERROR_FILE;
|
|
|
|
quitDecode(pc,dc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-05-19 04:14:20 +02:00
|
|
|
pc->totalTime = pc->fileTime;
|
|
|
|
pc->elapsedTime = 0;
|
|
|
|
pc->bitRate = 0;
|
|
|
|
pc->sampleRate = 0;
|
|
|
|
pc->bits = 0;
|
|
|
|
pc->channels = 0;
|
|
|
|
*decodeWaitedOn = 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-19 04:14:20 +02:00
|
|
|
int decodeSeek(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb,
|
|
|
|
int * decodeWaitedOn)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
2004-05-30 15:33:13 +02:00
|
|
|
if(decode_pid && *decode_pid>0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
cb->next = -1;
|
2004-05-19 04:14:20 +02:00
|
|
|
if(dc->state==DECODE_STATE_STOP || dc->error ||
|
2004-02-24 00:41:20 +01:00
|
|
|
strcmp(dc->file,pc->file)!=0)
|
|
|
|
{
|
|
|
|
stopDecode(dc);
|
2004-03-23 02:12:30 +01:00
|
|
|
cb->begin = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
cb->end = 0;
|
2004-03-23 02:12:30 +01:00
|
|
|
cb->wrap = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
dc->error = 0;
|
|
|
|
dc->start = 1;
|
2004-05-19 04:14:20 +02:00
|
|
|
waitOnDecode(pc,dc,cb,decodeWaitedOn);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-05-30 15:33:13 +02:00
|
|
|
if(*decode_pid>0 && dc->state!=DECODE_STATE_STOP &&
|
|
|
|
dc->seekable)
|
|
|
|
{
|
2004-03-18 23:20:26 +01:00
|
|
|
dc->seekWhere = pc->seekWhere > pc->totalTime-0.1 ?
|
|
|
|
pc->totalTime-0.1 :
|
|
|
|
pc->seekWhere;
|
|
|
|
dc->seekWhere = 0 > dc->seekWhere ? 0 : dc->seekWhere;
|
2004-05-30 15:33:13 +02:00
|
|
|
dc->seekError = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
dc->seek = 1;
|
2004-05-30 15:33:13 +02:00
|
|
|
while(*decode_pid>0 && dc->seek) my_usleep(10000);
|
|
|
|
if(!dc->seekError) {
|
|
|
|
pc->elapsedTime = dc->seekWhere;
|
|
|
|
pc->beginTime = pc->elapsedTime;
|
|
|
|
ret = 0;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pc->seek = 0;
|
2004-05-19 04:14:20 +02:00
|
|
|
|
|
|
|
return ret;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define processDecodeInput() \
|
2004-04-15 07:07:04 +02:00
|
|
|
if(pc->cycleLogFiles) { \
|
|
|
|
myfprintfCloseAndOpenLogFile(); \
|
|
|
|
pc->cycleLogFiles = 0; \
|
|
|
|
} \
|
2004-02-24 00:41:20 +01:00
|
|
|
if(pc->lockQueue) { \
|
|
|
|
pc->queueLockState = PLAYER_QUEUE_LOCKED; \
|
|
|
|
pc->lockQueue = 0; \
|
|
|
|
} \
|
|
|
|
if(pc->unlockQueue) { \
|
|
|
|
pc->queueLockState = PLAYER_QUEUE_UNLOCKED; \
|
|
|
|
pc->unlockQueue = 0; \
|
|
|
|
} \
|
|
|
|
if(pc->pause) { \
|
|
|
|
pause = !pause; \
|
2004-03-26 23:25:01 +01:00
|
|
|
if(pause) pc->state = PLAYER_STATE_PAUSE; \
|
2004-03-20 20:47:05 +01:00
|
|
|
else { \
|
2004-05-10 04:20:15 +02:00
|
|
|
if(openAudioDevice(NULL)<0) { \
|
2004-03-20 20:47:05 +01:00
|
|
|
strncpy(pc->erroredFile,pc->file,MAXPATHLEN); \
|
2004-03-27 05:55:25 +01:00
|
|
|
pc->erroredFile[MAXPATHLEN] = '\0'; \
|
2004-03-20 20:47:05 +01:00
|
|
|
pc->error = PLAYER_ERROR_AUDIO; \
|
|
|
|
quitDecode(pc,dc); \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
pc->state = PLAYER_STATE_PLAY; \
|
|
|
|
} \
|
2004-02-24 00:41:20 +01:00
|
|
|
pc->pause = 0; \
|
|
|
|
kill(getppid(),SIGUSR1); \
|
2004-05-10 04:20:15 +02:00
|
|
|
if(pause) closeAudioDevice(); \
|
2004-02-24 00:41:20 +01:00
|
|
|
} \
|
|
|
|
if(pc->seek) { \
|
|
|
|
pc->totalPlayTime+= pc->elapsedTime-pc->beginTime; \
|
2004-05-19 04:14:20 +02:00
|
|
|
if(decodeSeek(pc,dc,cb,&decodeWaitedOn) == 0) { \
|
|
|
|
doCrossFade = 0; \
|
|
|
|
nextChunk = -1; \
|
2004-05-20 05:44:33 +02:00
|
|
|
bbp = 0; \
|
2004-05-19 04:14:20 +02:00
|
|
|
} \
|
2004-02-24 00:41:20 +01:00
|
|
|
} \
|
|
|
|
if(pc->stop) { \
|
|
|
|
pc->totalPlayTime+= pc->elapsedTime-pc->beginTime; \
|
|
|
|
quitDecode(pc,dc); \
|
|
|
|
return; \
|
|
|
|
}
|
|
|
|
|
2004-05-18 15:13:55 +02:00
|
|
|
void decodeStart(PlayerControl * pc, OutputBuffer * cb, DecoderControl * dc) {
|
|
|
|
int ret;
|
|
|
|
InputStream inStream;
|
2004-05-31 03:21:17 +02:00
|
|
|
InputPlugin * plugin;
|
2004-04-11 20:27:12 +02:00
|
|
|
|
2004-05-18 15:13:55 +02:00
|
|
|
strncpy(dc->file,pc->file,MAXPATHLEN);
|
|
|
|
dc->file[MAXPATHLEN] = '\0';
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-05-18 15:13:55 +02:00
|
|
|
if(openInputStream(&inStream,dc->file) < 0) {
|
|
|
|
dc->error = DECODE_ERROR_FILE;
|
|
|
|
dc->start = 0;
|
|
|
|
dc->stop = 0;
|
|
|
|
dc->state = DECODE_STATE_STOP;
|
|
|
|
return;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-05-30 15:33:13 +02:00
|
|
|
dc->seekable = inStream.seekable;
|
2004-05-19 04:14:20 +02:00
|
|
|
dc->state = DECODE_STATE_START;
|
|
|
|
dc->start = 0;
|
|
|
|
|
|
|
|
while(!inputStreamAtEOF(&inStream) && bufferInputStream(&inStream) < 0
|
2004-05-30 15:33:13 +02:00
|
|
|
&& !dc->stop);
|
2004-05-19 04:14:20 +02:00
|
|
|
|
|
|
|
if(dc->stop) {
|
|
|
|
dc->state = DECODE_STATE_STOP;
|
|
|
|
dc->stop = 0;
|
|
|
|
return;
|
|
|
|
}
|
2004-05-18 15:13:55 +02:00
|
|
|
|
2004-05-31 03:21:17 +02:00
|
|
|
ret = DECODE_ERROR_UNKTYPE;
|
|
|
|
if(isRemoteUrl(pc->file)) {
|
|
|
|
plugin = getInputPluginFromMimeType(inStream.mime);
|
|
|
|
if(plugin == NULL) {
|
|
|
|
plugin = getInputPluginFromSuffix(getSuffix(dc->file));
|
2004-05-20 05:44:33 +02:00
|
|
|
}
|
2004-05-31 03:21:17 +02:00
|
|
|
if(plugin && (plugin->streamTypes & INPUT_PLUGIN_STREAM_URL) &&
|
|
|
|
plugin->streamDecodeFunc)
|
2004-05-18 15:13:55 +02:00
|
|
|
{
|
2004-05-31 03:21:17 +02:00
|
|
|
ret = plugin->streamDecodeFunc(cb, dc, &inStream);
|
2004-05-18 15:13:55 +02:00
|
|
|
}
|
|
|
|
}
|
2004-05-31 03:21:17 +02:00
|
|
|
else {
|
|
|
|
plugin = getInputPluginFromSuffix(getSuffix(dc->file));
|
|
|
|
if(plugin && (plugin->streamTypes && INPUT_PLUGIN_STREAM_FILE))
|
|
|
|
{
|
|
|
|
if(plugin->streamDecodeFunc) {
|
|
|
|
ret = plugin->streamDecodeFunc(cb, dc,
|
|
|
|
&inStream);
|
|
|
|
}
|
|
|
|
else if(plugin->fileDecodeFunc) {
|
|
|
|
closeInputStream(&inStream);
|
|
|
|
ret = plugin->fileDecodeFunc(cb, dc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-18 15:19:30 +02:00
|
|
|
if(ret<0 || ret == DECODE_ERROR_UNKTYPE) {
|
2004-05-18 15:13:55 +02:00
|
|
|
strncpy(pc->erroredFile, dc->file, MAXPATHLEN);
|
|
|
|
pc->erroredFile[MAXPATHLEN] = '\0';
|
|
|
|
if(ret != DECODE_ERROR_UNKTYPE) dc->error = DECODE_ERROR_FILE;
|
2004-05-20 05:44:33 +02:00
|
|
|
else {
|
|
|
|
dc->error = DECODE_ERROR_UNKTYPE;
|
|
|
|
closeInputStream(&inStream);
|
|
|
|
}
|
2004-05-18 15:13:55 +02:00
|
|
|
dc->stop = 0;
|
|
|
|
dc->state = DECODE_STATE_STOP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int decoderInit(PlayerControl * pc, OutputBuffer * cb, DecoderControl * dc) {
|
|
|
|
|
|
|
|
int pid;
|
|
|
|
decode_pid = &(pc->decode_pid);
|
|
|
|
|
|
|
|
blockSignals();
|
|
|
|
pid = fork();
|
|
|
|
|
|
|
|
if(pid==0) {
|
|
|
|
/* CHILD */
|
|
|
|
unblockSignals();
|
|
|
|
|
|
|
|
while(1) {
|
2004-04-15 07:07:04 +02:00
|
|
|
if(dc->cycleLogFiles) {
|
|
|
|
myfprintfCloseAndOpenLogFile();
|
|
|
|
dc->cycleLogFiles = 0;
|
|
|
|
}
|
2004-05-30 15:33:13 +02:00
|
|
|
else if(dc->start || dc->seek) decodeStart(pc, cb, dc);
|
2004-05-20 05:44:33 +02:00
|
|
|
else if(dc->stop) {
|
|
|
|
dc->state = DECODE_STATE_STOP;
|
|
|
|
dc->stop = 0;
|
|
|
|
}
|
2004-04-01 05:48:51 +02:00
|
|
|
else my_usleep(10000);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2004-04-03 01:34:16 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2004-02-24 00:41:20 +01:00
|
|
|
/* END OF CHILD */
|
|
|
|
}
|
2004-02-29 09:10:52 +01:00
|
|
|
else if(pid<0) {
|
2004-04-11 20:27:12 +02:00
|
|
|
unblockSignals();
|
2004-02-25 19:46:41 +01:00
|
|
|
strncpy(pc->erroredFile,pc->file,MAXPATHLEN);
|
2004-03-27 03:52:59 +01:00
|
|
|
pc->erroredFile[MAXPATHLEN] = '\0';
|
2004-02-24 00:41:20 +01:00
|
|
|
pc->error = PLAYER_ERROR_SYSTEM;
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-11 20:27:12 +02:00
|
|
|
|
|
|
|
*decode_pid = pid;
|
|
|
|
unblockSignals();
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-19 04:14:20 +02:00
|
|
|
void decodeParent(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb) {
|
|
|
|
int pause = 0;
|
|
|
|
int quit = 0;
|
|
|
|
int bbp = buffered_before_play;
|
|
|
|
int doCrossFade = 0;
|
|
|
|
int crossFadeChunks = 0;
|
|
|
|
int fadePosition;
|
|
|
|
int nextChunk = -1;
|
|
|
|
int test;
|
|
|
|
int decodeWaitedOn = 0;
|
|
|
|
char silence[CHUNK_SIZE];
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-05-19 04:14:20 +02:00
|
|
|
memset(silence,0,CHUNK_SIZE);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-05-19 04:14:20 +02:00
|
|
|
if(waitOnDecode(pc,dc,cb,&decodeWaitedOn)<0) return;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-05-19 04:14:20 +02:00
|
|
|
pc->state = PLAYER_STATE_PLAY;
|
|
|
|
pc->play = 0;
|
|
|
|
pc->beginTime = pc->elapsedTime;
|
|
|
|
kill(getppid(),SIGUSR1);
|
2004-05-20 05:44:33 +02:00
|
|
|
|
2004-05-19 04:14:20 +02:00
|
|
|
while(*decode_pid>0 && !cb->wrap && cb->end-cb->begin<bbp &&
|
|
|
|
dc->state!=DECODE_STATE_STOP)
|
|
|
|
{
|
|
|
|
processDecodeInput();
|
|
|
|
if(quit) return;
|
|
|
|
my_usleep(10000);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2004-05-19 04:14:20 +02:00
|
|
|
while(!quit) {
|
|
|
|
processDecodeInput();
|
|
|
|
handleDecodeStart();
|
|
|
|
if(dc->state==DECODE_STATE_STOP &&
|
|
|
|
pc->queueState==PLAYER_QUEUE_FULL &&
|
|
|
|
pc->queueLockState==PLAYER_QUEUE_UNLOCKED)
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2004-05-19 04:14:20 +02:00
|
|
|
cb->next = cb->end;
|
|
|
|
dc->start = 1;
|
|
|
|
pc->queueState = PLAYER_QUEUE_DECODE;
|
|
|
|
kill(getppid(),SIGUSR1);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-05-19 04:14:20 +02:00
|
|
|
if(cb->next>=0 && doCrossFade==0 && !dc->start) {
|
|
|
|
nextChunk = -1;
|
|
|
|
if(isCurrentAudioFormat(&(cb->audioFormat))) {
|
|
|
|
doCrossFade = 1;
|
|
|
|
crossFadeChunks =
|
|
|
|
calculateCrossFadeChunks(pc,
|
|
|
|
&(cb->audioFormat));
|
|
|
|
if(!crossFadeChunks ||
|
2004-05-10 14:35:18 +02:00
|
|
|
pc->crossFade>=dc->totalTime)
|
2004-05-19 04:14:20 +02:00
|
|
|
{
|
|
|
|
doCrossFade = -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
2004-05-19 04:14:20 +02:00
|
|
|
else doCrossFade = -1;
|
|
|
|
}
|
|
|
|
if(pause) my_usleep(10000);
|
|
|
|
else if((cb->begin!=cb->end || cb->wrap) &&
|
2004-02-24 00:41:20 +01:00
|
|
|
cb->begin!=cb->next)
|
2004-05-19 04:14:20 +02:00
|
|
|
{
|
|
|
|
if(doCrossFade==1 && cb->next>=0 &&
|
2004-02-24 00:41:20 +01:00
|
|
|
((cb->next>cb->begin &&
|
|
|
|
(fadePosition=cb->next-cb->begin)
|
|
|
|
<=crossFadeChunks) ||
|
|
|
|
(cb->begin>cb->next &&
|
|
|
|
(fadePosition=cb->next-cb->begin+
|
2004-02-25 22:10:56 +01:00
|
|
|
buffered_chunks)<=crossFadeChunks)))
|
2004-05-19 04:14:20 +02:00
|
|
|
{
|
|
|
|
if(nextChunk<0) {
|
|
|
|
crossFadeChunks = fadePosition;
|
|
|
|
}
|
|
|
|
test = cb->end;
|
|
|
|
if(cb->wrap) test+=buffered_chunks;
|
|
|
|
nextChunk = cb->begin+crossFadeChunks;
|
|
|
|
if(nextChunk<test) {
|
|
|
|
if(nextChunk>=buffered_chunks)
|
|
|
|
{
|
|
|
|
nextChunk -= buffered_chunks;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-05-19 04:14:20 +02:00
|
|
|
pcm_mix(cb->chunks+cb->begin*CHUNK_SIZE,
|
2004-02-24 00:41:20 +01:00
|
|
|
cb->chunks+nextChunk*
|
|
|
|
CHUNK_SIZE,
|
|
|
|
cb->chunkSize[
|
|
|
|
cb->begin],
|
|
|
|
cb->chunkSize[
|
|
|
|
nextChunk],
|
2004-05-10 14:35:18 +02:00
|
|
|
&(cb->audioFormat),
|
2004-02-24 00:41:20 +01:00
|
|
|
((float)fadePosition)/
|
|
|
|
crossFadeChunks);
|
2004-05-19 04:14:20 +02:00
|
|
|
if(cb->chunkSize[nextChunk]>
|
2004-02-24 00:41:20 +01:00
|
|
|
cb->chunkSize[cb->begin]
|
|
|
|
)
|
2004-05-19 04:14:20 +02:00
|
|
|
{
|
|
|
|
cb->chunkSize[cb->begin]
|
2004-02-24 00:41:20 +01:00
|
|
|
= cb->chunkSize
|
|
|
|
[nextChunk];
|
|
|
|
}
|
2004-02-28 00:13:26 +01:00
|
|
|
}
|
2004-05-19 04:14:20 +02:00
|
|
|
else {
|
|
|
|
if(dc->state==DECODE_STATE_STOP)
|
|
|
|
{
|
|
|
|
doCrossFade = -1;
|
|
|
|
}
|
|
|
|
else continue;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
2004-05-30 15:33:13 +02:00
|
|
|
pc->elapsedTime = cb->times[cb->begin];
|
2004-05-19 04:14:20 +02:00
|
|
|
pc->bitRate = cb->bitRate[cb->begin];
|
|
|
|
pcm_volumeChange(cb->chunks+cb->begin*
|
|
|
|
CHUNK_SIZE,
|
|
|
|
cb->chunkSize[cb->begin],
|
|
|
|
&(cb->audioFormat),
|
|
|
|
pc->softwareVolume);
|
|
|
|
if(playAudio(cb->chunks+cb->begin*CHUNK_SIZE,
|
|
|
|
cb->chunkSize[cb->begin])<0)
|
|
|
|
{
|
|
|
|
quit = 1;
|
|
|
|
}
|
|
|
|
cb->begin++;
|
|
|
|
if(cb->begin>=buffered_chunks) {
|
|
|
|
cb->begin = 0;
|
|
|
|
cb->wrap = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(cb->next==cb->begin) {
|
|
|
|
pc->totalPlayTime+= pc->elapsedTime-
|
|
|
|
pc->beginTime;
|
|
|
|
if(doCrossFade==1 && nextChunk>=0) {
|
|
|
|
nextChunk = cb->begin+crossFadeChunks;
|
|
|
|
test = cb->end;
|
|
|
|
if(cb->wrap) test+=buffered_chunks;
|
|
|
|
if(nextChunk<test) {
|
|
|
|
if(nextChunk>=buffered_chunks)
|
|
|
|
{
|
|
|
|
nextChunk -= buffered_chunks;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-05-19 04:14:20 +02:00
|
|
|
cb->begin = nextChunk;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-05-19 04:14:20 +02:00
|
|
|
while(pc->queueState==PLAYER_QUEUE_DECODE ||
|
|
|
|
pc->queueLockState==PLAYER_QUEUE_LOCKED)
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2004-05-19 04:14:20 +02:00
|
|
|
processDecodeInput();
|
|
|
|
if(quit) {
|
|
|
|
quitDecode(pc,dc);
|
|
|
|
return;
|
|
|
|
}
|
2004-05-20 02:21:58 +02:00
|
|
|
my_usleep(10000);
|
2004-05-19 04:14:20 +02:00
|
|
|
}
|
|
|
|
if(pc->queueState!=PLAYER_QUEUE_PLAY) {
|
2004-02-24 00:41:20 +01:00
|
|
|
quit = 1;
|
|
|
|
break;
|
|
|
|
}
|
2004-05-18 21:32:05 +02:00
|
|
|
else {
|
2004-05-19 04:14:20 +02:00
|
|
|
cb->next = -1;
|
|
|
|
if(waitOnDecode(pc,dc,cb,&decodeWaitedOn)<0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nextChunk = -1;
|
|
|
|
doCrossFade = 0;
|
|
|
|
crossFadeChunks = 0;
|
|
|
|
pc->queueState = PLAYER_QUEUE_EMPTY;
|
|
|
|
kill(getppid(),SIGUSR1);
|
2004-05-18 21:32:05 +02:00
|
|
|
}
|
2004-05-19 04:14:20 +02:00
|
|
|
pc->beginTime = cb->times[cb->begin];
|
|
|
|
}
|
|
|
|
else if(*decode_pid<=0 ||
|
|
|
|
(dc->state==DECODE_STATE_STOP && !dc->start))
|
|
|
|
{
|
|
|
|
quit = 1;
|
|
|
|
break;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-05-19 04:14:20 +02:00
|
|
|
else {
|
|
|
|
if(playAudio(silence, CHUNK_SIZE) < 0) quit = 1;
|
|
|
|
}
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-05-19 04:14:20 +02:00
|
|
|
pc->totalPlayTime+= pc->elapsedTime-pc->beginTime; \
|
|
|
|
quitDecode(pc,dc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decode w/ buffering
|
|
|
|
* this will fork another process
|
|
|
|
* child process does decoding
|
|
|
|
* parent process does playing audio
|
|
|
|
*/
|
|
|
|
void decode() {
|
|
|
|
OutputBuffer * cb;
|
|
|
|
PlayerControl * pc;
|
|
|
|
DecoderControl * dc;
|
|
|
|
|
|
|
|
cb = &(getPlayerData()->buffer);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-05-19 04:14:20 +02:00
|
|
|
cb->begin = 0;
|
|
|
|
cb->end = 0;
|
|
|
|
cb->wrap = 0;
|
|
|
|
pc = &(getPlayerData()->playerControl);
|
|
|
|
dc = &(getPlayerData()->decoderControl);
|
|
|
|
dc->error = 0;
|
|
|
|
cb->next = -1;
|
2004-05-20 05:44:33 +02:00
|
|
|
dc->seek = 0;
|
|
|
|
dc->stop = 0;
|
|
|
|
dc->start = 1;
|
2004-05-19 04:14:20 +02:00
|
|
|
|
|
|
|
if(decode_pid==NULL || *decode_pid<=0) {
|
|
|
|
if(decoderInit(pc,cb,dc)<0) return;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2004-05-19 04:14:20 +02:00
|
|
|
decodeParent(pc, dc, cb);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-05-19 04:14:20 +02:00
|
|
|
/* vim:set shiftwidth=8 tabstop=8 expandtab: */
|