2008-08-26 08:27:09 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
|
|
|
* 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 "player_thread.h"
|
2008-08-26 08:44:38 +02:00
|
|
|
#include "player_control.h"
|
2008-08-26 08:44:19 +02:00
|
|
|
#include "decoder_control.h"
|
2008-08-26 08:27:09 +02:00
|
|
|
#include "audio.h"
|
|
|
|
#include "pcm_utils.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "main_notify.h"
|
|
|
|
#include "crossfade.h"
|
2008-10-08 10:49:11 +02:00
|
|
|
#include "song.h"
|
2008-11-02 14:15:47 +01:00
|
|
|
#include "pipe.h"
|
2008-11-02 17:13:26 +01:00
|
|
|
#include "idle.h"
|
2008-08-26 08:27:09 +02:00
|
|
|
|
|
|
|
enum xfade_state {
|
|
|
|
XFADE_DISABLED = -1,
|
|
|
|
XFADE_UNKNOWN = 0,
|
|
|
|
XFADE_ENABLED = 1
|
|
|
|
};
|
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
struct player {
|
|
|
|
/**
|
2008-10-12 01:21:35 +02:00
|
|
|
* are we waiting for buffered_before_play?
|
2008-10-12 00:02:23 +02:00
|
|
|
*/
|
2008-10-12 01:21:35 +02:00
|
|
|
bool buffering;
|
2008-10-12 00:02:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* true if the decoder is starting and did not provide data
|
|
|
|
* yet
|
|
|
|
*/
|
|
|
|
bool decoder_starting;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* is the player paused?
|
|
|
|
*/
|
|
|
|
bool paused;
|
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
/**
|
|
|
|
* is there a new song in pc.next_song?
|
|
|
|
*/
|
|
|
|
bool queued;
|
|
|
|
|
2008-11-02 17:10:26 +01:00
|
|
|
/**
|
|
|
|
* the song currently being played
|
|
|
|
*/
|
|
|
|
struct song *song;
|
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
/**
|
|
|
|
* is cross fading enabled?
|
|
|
|
*/
|
|
|
|
enum xfade_state xfade;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* index of the first chunk of the next song, -1 if there is
|
|
|
|
* no next song
|
|
|
|
*/
|
|
|
|
int next_song_chunk;
|
|
|
|
};
|
|
|
|
|
2008-08-26 08:45:15 +02:00
|
|
|
static void player_command_finished(void)
|
|
|
|
{
|
|
|
|
assert(pc.command != PLAYER_COMMAND_NONE);
|
|
|
|
|
|
|
|
pc.command = PLAYER_COMMAND_NONE;
|
|
|
|
wakeup_main_task();
|
|
|
|
}
|
|
|
|
|
2008-11-03 21:49:29 +01:00
|
|
|
static void player_stop_decoder(void)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2008-08-26 08:27:18 +02:00
|
|
|
dc_stop(&pc.notify);
|
2008-08-26 08:27:09 +02:00
|
|
|
pc.state = PLAYER_STATE_STOP;
|
|
|
|
wakeup_main_task();
|
|
|
|
}
|
|
|
|
|
2008-11-03 21:49:29 +01:00
|
|
|
static int player_wait_for_decoder(struct player *player)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2008-08-26 08:27:18 +02:00
|
|
|
dc_command_wait(&pc.notify);
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2008-11-08 15:48:00 +01:00
|
|
|
if (decoder_has_failed()) {
|
2008-08-26 08:27:11 +02:00
|
|
|
assert(dc.next_song == NULL || dc.next_song->url != NULL);
|
2008-08-26 08:27:09 +02:00
|
|
|
pc.errored_song = dc.next_song;
|
|
|
|
pc.error = PLAYER_ERROR_FILE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-11-03 21:49:29 +01:00
|
|
|
pc.total_time = pc.next_song->tag != NULL
|
2008-10-11 12:52:51 +02:00
|
|
|
? pc.next_song->tag->time : 0;
|
2008-11-03 21:49:29 +01:00
|
|
|
pc.bit_rate = 0;
|
2008-10-10 14:47:58 +02:00
|
|
|
audio_format_clear(&pc.audio_format);
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2008-11-02 17:10:26 +01:00
|
|
|
player->song = pc.next_song;
|
2008-10-12 00:07:54 +02:00
|
|
|
pc.next_song = NULL;
|
|
|
|
player->queued = false;
|
2008-10-12 00:02:23 +02:00
|
|
|
player->decoder_starting = true;
|
2008-08-26 08:27:09 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-03 21:49:29 +01:00
|
|
|
static bool player_seek_decoder(struct player *player)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2008-08-26 08:27:18 +02:00
|
|
|
double where;
|
2008-10-30 08:38:54 +01:00
|
|
|
bool ret;
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2008-08-26 08:40:47 +02:00
|
|
|
if (decoder_current_song() != pc.next_song) {
|
2008-08-26 08:27:18 +02:00
|
|
|
dc_stop(&pc.notify);
|
2008-10-12 00:02:23 +02:00
|
|
|
player->next_song_chunk = -1;
|
2008-11-02 14:18:34 +01:00
|
|
|
music_pipe_clear();
|
2008-08-26 08:27:18 +02:00
|
|
|
dc_start_async(pc.next_song);
|
2008-11-03 21:49:29 +01:00
|
|
|
player_wait_for_decoder(player);
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
2008-08-26 08:27:18 +02:00
|
|
|
|
2008-11-03 21:49:29 +01:00
|
|
|
where = pc.seek_where;
|
|
|
|
if (where > pc.total_time)
|
|
|
|
where = pc.total_time - 0.1;
|
2008-08-26 08:27:18 +02:00
|
|
|
if (where < 0.0)
|
|
|
|
where = 0.0;
|
|
|
|
|
|
|
|
ret = dc_seek(&pc.notify, where);
|
2008-10-30 08:38:54 +01:00
|
|
|
if (ret)
|
2008-11-03 21:49:29 +01:00
|
|
|
pc.elapsed_time = where;
|
2008-08-26 08:27:09 +02:00
|
|
|
|
|
|
|
player_command_finished();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-03 21:49:29 +01:00
|
|
|
static void player_process_command(struct player *player)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
|
|
|
switch (pc.command) {
|
|
|
|
case PLAYER_COMMAND_NONE:
|
|
|
|
case PLAYER_COMMAND_PLAY:
|
|
|
|
case PLAYER_COMMAND_STOP:
|
2008-08-26 08:27:16 +02:00
|
|
|
case PLAYER_COMMAND_EXIT:
|
2008-08-26 08:27:09 +02:00
|
|
|
case PLAYER_COMMAND_CLOSE_AUDIO:
|
|
|
|
break;
|
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
case PLAYER_COMMAND_QUEUE:
|
|
|
|
assert(pc.next_song != NULL);
|
2008-11-14 17:55:51 +01:00
|
|
|
assert(!player->queued);
|
|
|
|
assert(player->next_song_chunk == -1);
|
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
player->queued = true;
|
2008-08-26 08:27:09 +02:00
|
|
|
player_command_finished();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_COMMAND_PAUSE:
|
2008-10-12 00:02:23 +02:00
|
|
|
player->paused = !player->paused;
|
|
|
|
if (player->paused) {
|
|
|
|
audio_output_pause_all();
|
2008-08-26 08:27:09 +02:00
|
|
|
pc.state = PLAYER_STATE_PAUSE;
|
|
|
|
} else {
|
2008-10-29 20:40:27 +01:00
|
|
|
if (openAudioDevice(NULL)) {
|
2008-08-26 08:27:09 +02:00
|
|
|
pc.state = PLAYER_STATE_PLAY;
|
|
|
|
} else {
|
|
|
|
char tmp[MPD_PATH_MAX];
|
2008-08-26 08:27:11 +02:00
|
|
|
assert(dc.next_song == NULL || dc.next_song->url != NULL);
|
2008-08-26 08:27:09 +02:00
|
|
|
pc.errored_song = dc.next_song;
|
|
|
|
pc.error = PLAYER_ERROR_AUDIO;
|
|
|
|
ERROR("problems opening audio device "
|
|
|
|
"while playing \"%s\"\n",
|
2008-10-08 11:05:34 +02:00
|
|
|
song_get_url(dc.next_song, tmp));
|
2008-10-12 00:02:23 +02:00
|
|
|
player->paused = true;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
player_command_finished();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_COMMAND_SEEK:
|
|
|
|
dropBufferedAudio();
|
2008-11-03 21:49:29 +01:00
|
|
|
if (player_seek_decoder(player)) {
|
2008-10-12 00:02:23 +02:00
|
|
|
player->xfade = XFADE_UNKNOWN;
|
2008-10-12 01:21:35 +02:00
|
|
|
|
|
|
|
/* abort buffering when the user has requested
|
|
|
|
a seek */
|
|
|
|
player->buffering = false;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
break;
|
2008-10-12 00:07:54 +02:00
|
|
|
|
|
|
|
case PLAYER_COMMAND_CANCEL:
|
|
|
|
if (pc.next_song == NULL) {
|
|
|
|
/* the cancel request arrived too later, we're
|
|
|
|
already playing the queued song... stop
|
|
|
|
everything now */
|
|
|
|
pc.command = PLAYER_COMMAND_STOP;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (player->next_song_chunk != -1) {
|
|
|
|
/* the decoder is already decoding the song -
|
|
|
|
stop it and reset the position */
|
|
|
|
dc_stop(&pc.notify);
|
2008-11-13 02:09:33 +01:00
|
|
|
music_pipe_chop(player->next_song_chunk);
|
2008-10-12 00:07:54 +02:00
|
|
|
player->next_song_chunk = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pc.next_song = NULL;
|
|
|
|
player->queued = false;
|
|
|
|
player_command_finished();
|
|
|
|
break;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-02 14:15:47 +01:00
|
|
|
static int
|
2008-11-03 21:49:29 +01:00
|
|
|
play_chunk(struct song *song, struct music_chunk *chunk,
|
|
|
|
const struct audio_format *format, double sizeToTime)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2008-11-03 21:49:29 +01:00
|
|
|
pc.elapsed_time = chunk->times;
|
|
|
|
pc.bit_rate = chunk->bit_rate;
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2008-11-02 17:13:26 +01:00
|
|
|
if (chunk->tag != NULL) {
|
2008-11-02 17:07:31 +01:00
|
|
|
sendMetadataToAudioDevice(chunk->tag);
|
|
|
|
|
2008-11-02 17:13:26 +01:00
|
|
|
if (!song_is_file(song)) {
|
|
|
|
/* always update the tag of remote streams */
|
2008-11-11 20:46:55 +01:00
|
|
|
struct tag *old_tag = song->tag;
|
2008-11-02 17:13:26 +01:00
|
|
|
|
|
|
|
song->tag = tag_dup(chunk->tag);
|
|
|
|
|
2008-11-11 20:46:55 +01:00
|
|
|
if (old_tag != NULL)
|
|
|
|
tag_free(old_tag);
|
|
|
|
|
2008-11-02 17:13:26 +01:00
|
|
|
/* notify all clients that the tag of the
|
|
|
|
current song has changed */
|
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-02 20:14:55 +01:00
|
|
|
if (chunk->length == 0)
|
|
|
|
return 0;
|
|
|
|
|
2008-11-02 16:57:16 +01:00
|
|
|
pcm_volume(chunk->data, chunk->length,
|
2008-11-03 21:49:29 +01:00
|
|
|
format, pc.software_volume);
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2008-11-02 16:57:16 +01:00
|
|
|
if (!playAudio(chunk->data, chunk->length))
|
2008-08-26 08:27:09 +02:00
|
|
|
return -1;
|
|
|
|
|
2008-11-03 21:49:29 +01:00
|
|
|
pc.total_play_time += sizeToTime * chunk->length;
|
2008-08-26 08:27:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-26 08:29:37 +02:00
|
|
|
static void do_play(void)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2008-10-12 00:02:23 +02:00
|
|
|
struct player player = {
|
2008-10-12 01:21:35 +02:00
|
|
|
.buffering = true,
|
2008-10-12 00:02:23 +02:00
|
|
|
.decoder_starting = false,
|
|
|
|
.paused = false,
|
2008-10-12 00:07:54 +02:00
|
|
|
.queued = false,
|
2008-11-02 17:10:26 +01:00
|
|
|
.song = NULL,
|
2008-10-12 00:02:23 +02:00
|
|
|
.xfade = XFADE_UNKNOWN,
|
|
|
|
.next_song_chunk = -1,
|
|
|
|
};
|
2008-08-26 08:27:09 +02:00
|
|
|
unsigned int crossFadeChunks = 0;
|
|
|
|
/** the position of the next cross-faded chunk in the next
|
|
|
|
song */
|
|
|
|
int nextChunk = 0;
|
|
|
|
static const char silence[CHUNK_SIZE];
|
2008-10-29 17:29:30 +01:00
|
|
|
struct audio_format play_audio_format;
|
2008-08-26 08:27:09 +02:00
|
|
|
double sizeToTime = 0.0;
|
|
|
|
|
2008-11-02 14:18:34 +01:00
|
|
|
music_pipe_clear();
|
|
|
|
music_pipe_set_lazy(false);
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2008-08-26 08:29:37 +02:00
|
|
|
dc_start(&pc.notify, pc.next_song);
|
2008-11-03 21:49:29 +01:00
|
|
|
if (player_wait_for_decoder(&player) < 0) {
|
|
|
|
player_stop_decoder();
|
2008-10-27 10:10:40 +01:00
|
|
|
player_command_finished();
|
2008-08-26 08:27:09 +02:00
|
|
|
return;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2008-11-03 21:49:29 +01:00
|
|
|
pc.elapsed_time = 0;
|
2008-08-26 08:27:09 +02:00
|
|
|
pc.state = PLAYER_STATE_PLAY;
|
|
|
|
player_command_finished();
|
|
|
|
|
|
|
|
while (1) {
|
2008-11-03 21:49:29 +01:00
|
|
|
player_process_command(&player);
|
2008-08-26 08:27:16 +02:00
|
|
|
if (pc.command == PLAYER_COMMAND_STOP ||
|
2008-08-26 08:27:16 +02:00
|
|
|
pc.command == PLAYER_COMMAND_EXIT ||
|
2008-08-26 08:27:16 +02:00
|
|
|
pc.command == PLAYER_COMMAND_CLOSE_AUDIO) {
|
2008-08-26 08:27:09 +02:00
|
|
|
dropBufferedAudio();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-10-12 01:21:35 +02:00
|
|
|
if (player.buffering) {
|
2008-11-02 14:18:34 +01:00
|
|
|
if (music_pipe_available() < pc.buffered_before_play &&
|
2008-10-29 18:35:10 +01:00
|
|
|
!decoder_is_idle()) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* not enough decoded buffer space yet */
|
|
|
|
notify_wait(&pc.notify);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* buffering is complete */
|
2008-10-12 01:21:35 +02:00
|
|
|
player.buffering = false;
|
2008-11-02 14:18:34 +01:00
|
|
|
music_pipe_set_lazy(true);
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
if (player.decoder_starting) {
|
2008-11-08 15:48:00 +01:00
|
|
|
if (decoder_has_failed()) {
|
2008-08-26 08:27:18 +02:00
|
|
|
/* the decoder failed */
|
|
|
|
assert(dc.next_song == NULL || dc.next_song->url != NULL);
|
|
|
|
pc.errored_song = dc.next_song;
|
|
|
|
pc.error = PLAYER_ERROR_FILE;
|
|
|
|
break;
|
|
|
|
}
|
2008-08-26 08:40:47 +02:00
|
|
|
else if (!decoder_is_starting()) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* the decoder is ready and ok */
|
2008-10-12 00:02:23 +02:00
|
|
|
player.decoder_starting = false;
|
2008-11-02 16:55:43 +01:00
|
|
|
if (!openAudioDevice(&dc.out_audio_format)) {
|
2008-08-26 08:27:09 +02:00
|
|
|
char tmp[MPD_PATH_MAX];
|
2008-08-26 08:27:11 +02:00
|
|
|
assert(dc.next_song == NULL || dc.next_song->url != NULL);
|
2008-08-26 08:27:09 +02:00
|
|
|
pc.errored_song = dc.next_song;
|
|
|
|
pc.error = PLAYER_ERROR_AUDIO;
|
|
|
|
ERROR("problems opening audio device "
|
|
|
|
"while playing \"%s\"\n",
|
2008-10-08 11:05:34 +02:00
|
|
|
song_get_url(dc.next_song, tmp));
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-10-29 22:17:42 +01:00
|
|
|
if (player.paused)
|
2008-08-26 08:27:09 +02:00
|
|
|
closeAudioDevice();
|
2008-10-29 22:17:42 +01:00
|
|
|
|
2008-11-03 21:49:29 +01:00
|
|
|
pc.total_time = dc.total_time;
|
2008-11-02 16:55:43 +01:00
|
|
|
pc.audio_format = dc.in_audio_format;
|
|
|
|
play_audio_format = dc.out_audio_format;
|
|
|
|
sizeToTime = audioFormatSizeToTime(&dc.out_audio_format);
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* the decoder is not yet ready; wait
|
|
|
|
some more */
|
|
|
|
notify_wait(&pc.notify);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-13 02:06:58 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
music_pipe_check_format(&play_audio_format,
|
|
|
|
player.next_song_chunk,
|
|
|
|
&dc.out_audio_format);
|
|
|
|
#endif
|
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
if (decoder_is_idle() && !player.queued &&
|
2008-11-14 17:55:51 +01:00
|
|
|
player.next_song_chunk < 0 &&
|
2008-10-29 17:28:49 +01:00
|
|
|
pc.next_song != NULL &&
|
|
|
|
pc.command == PLAYER_COMMAND_NONE) {
|
2008-10-12 00:07:54 +02:00
|
|
|
/* the decoder has finished the current song;
|
|
|
|
request the next song from the playlist */
|
2008-11-14 17:55:51 +01:00
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
pc.next_song = NULL;
|
|
|
|
wakeup_main_task();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decoder_is_idle() && player.queued) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* the decoder has finished the current song;
|
|
|
|
make it decode the next song */
|
2008-10-12 00:07:54 +02:00
|
|
|
assert(pc.next_song != NULL);
|
2008-11-13 01:57:15 +01:00
|
|
|
assert(player.next_song_chunk == -1);
|
2008-10-12 00:07:54 +02:00
|
|
|
|
|
|
|
player.queued = false;
|
2008-11-02 16:55:53 +01:00
|
|
|
player.next_song_chunk = music_pipe_tail_index();
|
2008-08-26 08:27:18 +02:00
|
|
|
dc_start_async(pc.next_song);
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
2008-10-12 00:02:23 +02:00
|
|
|
if (player.next_song_chunk >= 0 &&
|
|
|
|
player.xfade == XFADE_UNKNOWN &&
|
2008-08-26 08:40:47 +02:00
|
|
|
!decoder_is_starting()) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* enable cross fading in this song? if yes,
|
|
|
|
calculate how many chunks will be required
|
|
|
|
for it */
|
|
|
|
crossFadeChunks =
|
2008-11-03 21:49:29 +01:00
|
|
|
cross_fade_calc(pc.cross_fade_seconds, dc.total_time,
|
2008-11-02 16:55:43 +01:00
|
|
|
&dc.out_audio_format,
|
2008-11-02 16:55:53 +01:00
|
|
|
music_pipe_size() -
|
2008-08-26 08:45:14 +02:00
|
|
|
pc.buffered_before_play);
|
2008-08-26 08:27:09 +02:00
|
|
|
if (crossFadeChunks > 0) {
|
2008-10-12 00:02:23 +02:00
|
|
|
player.xfade = XFADE_ENABLED;
|
2008-08-26 08:27:09 +02:00
|
|
|
nextChunk = -1;
|
|
|
|
} else
|
|
|
|
/* cross fading is disabled or the
|
|
|
|
next song is too short */
|
2008-10-12 00:02:23 +02:00
|
|
|
player.xfade = XFADE_DISABLED;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
if (player.paused)
|
2008-08-26 08:27:09 +02:00
|
|
|
notify_wait(&pc.notify);
|
2008-11-02 14:18:34 +01:00
|
|
|
else if (!music_pipe_is_empty() &&
|
2008-11-02 16:55:53 +01:00
|
|
|
!music_pipe_head_is(player.next_song_chunk)) {
|
|
|
|
struct music_chunk *beginChunk = music_pipe_peek();
|
2008-08-26 08:27:09 +02:00
|
|
|
unsigned int fadePosition;
|
2008-10-12 00:02:23 +02:00
|
|
|
if (player.xfade == XFADE_ENABLED &&
|
|
|
|
player.next_song_chunk >= 0 &&
|
2008-11-02 14:18:34 +01:00
|
|
|
(fadePosition = music_pipe_relative(player.next_song_chunk))
|
2008-08-26 08:27:09 +02:00
|
|
|
<= crossFadeChunks) {
|
|
|
|
/* perform cross fade */
|
|
|
|
if (nextChunk < 0) {
|
|
|
|
/* beginning of the cross fade
|
|
|
|
- adjust crossFadeChunks
|
|
|
|
which might be bigger than
|
|
|
|
the remaining number of
|
|
|
|
chunks in the old song */
|
|
|
|
crossFadeChunks = fadePosition;
|
|
|
|
}
|
2008-11-02 14:18:34 +01:00
|
|
|
nextChunk = music_pipe_absolute(crossFadeChunks);
|
2008-08-26 08:27:09 +02:00
|
|
|
if (nextChunk >= 0) {
|
2008-11-02 14:18:34 +01:00
|
|
|
music_pipe_set_lazy(true);
|
2008-08-26 08:27:09 +02:00
|
|
|
cross_fade_apply(beginChunk,
|
2008-11-02 14:18:34 +01:00
|
|
|
music_pipe_get_chunk(nextChunk),
|
2008-11-02 16:55:43 +01:00
|
|
|
&dc.out_audio_format,
|
2008-08-26 08:27:09 +02:00
|
|
|
fadePosition,
|
|
|
|
crossFadeChunks);
|
|
|
|
} else {
|
|
|
|
/* there are not enough
|
|
|
|
decoded chunks yet */
|
2008-08-26 08:40:47 +02:00
|
|
|
if (decoder_is_idle()) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* the decoder isn't
|
|
|
|
running, abort
|
|
|
|
cross fading */
|
2008-10-12 00:02:23 +02:00
|
|
|
player.xfade = XFADE_DISABLED;
|
2008-08-26 08:27:09 +02:00
|
|
|
} else {
|
|
|
|
/* wait for the
|
|
|
|
decoder */
|
2008-11-02 14:18:34 +01:00
|
|
|
music_pipe_set_lazy(false);
|
2008-11-14 17:55:45 +01:00
|
|
|
notify_signal(&dc.notify);
|
2008-08-26 08:27:09 +02:00
|
|
|
notify_wait(&pc.notify);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* play the current chunk */
|
2008-11-03 21:49:29 +01:00
|
|
|
if (play_chunk(player.song, beginChunk,
|
|
|
|
&play_audio_format, sizeToTime) < 0)
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
2008-11-02 14:18:34 +01:00
|
|
|
music_pipe_shift();
|
2008-10-10 16:47:57 +02:00
|
|
|
|
|
|
|
/* this formula should prevent that the
|
|
|
|
decoder gets woken up with each chunk; it
|
|
|
|
is more efficient to make it decode a
|
|
|
|
larger block at a time */
|
2008-11-02 16:55:53 +01:00
|
|
|
if (music_pipe_available() <= (pc.buffered_before_play + music_pipe_size() * 3) / 4)
|
2008-10-10 16:47:57 +02:00
|
|
|
notify_signal(&dc.notify);
|
2008-11-02 16:55:53 +01:00
|
|
|
} else if (music_pipe_head_is(player.next_song_chunk)) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* at the beginning of a new song */
|
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
if (player.xfade == XFADE_ENABLED && nextChunk >= 0) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* the cross-fade is finished; skip
|
|
|
|
the section which was cross-faded
|
|
|
|
(and thus already played) */
|
2008-11-02 14:18:34 +01:00
|
|
|
music_pipe_skip(crossFadeChunks);
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
player.xfade = XFADE_UNKNOWN;
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
player.next_song_chunk = -1;
|
2008-11-03 21:49:29 +01:00
|
|
|
if (player_wait_for_decoder(&player) < 0)
|
2008-08-26 08:27:09 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
wakeup_main_task();
|
2008-08-26 08:40:47 +02:00
|
|
|
} else if (decoder_is_idle()) {
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
} else {
|
2008-10-23 20:54:52 +02:00
|
|
|
size_t frame_size =
|
2008-10-29 17:29:30 +01:00
|
|
|
audio_format_frame_size(&play_audio_format);
|
2008-10-23 20:54:52 +02:00
|
|
|
/* this formula ensures that we don't send
|
|
|
|
partial frames */
|
|
|
|
unsigned num_frames = CHUNK_SIZE / frame_size;
|
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
/*DEBUG("waiting for decoded audio, play silence\n");*/
|
2008-10-29 20:40:27 +01:00
|
|
|
if (!playAudio(silence, num_frames * frame_size))
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-03 21:49:29 +01:00
|
|
|
player_stop_decoder();
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void * player_task(mpd_unused void *arg)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
switch (pc.command) {
|
|
|
|
case PLAYER_COMMAND_PLAY:
|
2008-10-12 00:07:54 +02:00
|
|
|
case PLAYER_COMMAND_QUEUE:
|
2008-08-26 08:29:37 +02:00
|
|
|
do_play();
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_COMMAND_STOP:
|
|
|
|
case PLAYER_COMMAND_SEEK:
|
|
|
|
case PLAYER_COMMAND_PAUSE:
|
|
|
|
player_command_finished();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_COMMAND_CLOSE_AUDIO:
|
|
|
|
closeAudioDevice();
|
|
|
|
player_command_finished();
|
|
|
|
break;
|
|
|
|
|
2008-08-26 08:27:16 +02:00
|
|
|
case PLAYER_COMMAND_EXIT:
|
|
|
|
closeAudioDevice();
|
|
|
|
player_command_finished();
|
|
|
|
pthread_exit(NULL);
|
|
|
|
break;
|
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
case PLAYER_COMMAND_CANCEL:
|
|
|
|
pc.next_song = NULL;
|
2008-08-26 08:27:09 +02:00
|
|
|
player_command_finished();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_COMMAND_NONE:
|
|
|
|
notify_wait(&pc.notify);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void player_create(void)
|
|
|
|
{
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_t player_thread;
|
|
|
|
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
if (pthread_create(&player_thread, &attr, player_task, NULL))
|
|
|
|
FATAL("Failed to spawn player task: %s\n", strerror(errno));
|
|
|
|
}
|