2009-03-11 09:35:16 +01:00
|
|
|
/*
|
2013-01-02 20:36:28 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-11 09:35:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-08-26 08:27:09 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-08-26 08:27:09 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-02 20:36:28 +01:00
|
|
|
#include "PlayerThread.hxx"
|
|
|
|
#include "DecoderThread.hxx"
|
2013-01-04 08:41:16 +01:00
|
|
|
#include "DecoderControl.hxx"
|
2013-01-04 10:16:16 +01:00
|
|
|
#include "MusicPipe.hxx"
|
|
|
|
#include "MusicBuffer.hxx"
|
|
|
|
#include "MusicChunk.hxx"
|
2013-07-28 13:25:12 +02:00
|
|
|
#include "Song.hxx"
|
2013-01-02 20:36:28 +01:00
|
|
|
#include "Main.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/FatalError.hxx"
|
2013-01-04 10:31:59 +01:00
|
|
|
#include "CrossFade.hxx"
|
2013-01-04 22:31:53 +01:00
|
|
|
#include "PlayerControl.hxx"
|
2013-01-07 08:54:26 +01:00
|
|
|
#include "OutputAll.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/Tag.hxx"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2013-01-09 23:12:53 +01:00
|
|
|
#include "GlobalEvents.hxx"
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2013-01-07 08:53:08 +01:00
|
|
|
#include <cmath>
|
|
|
|
|
2008-11-24 14:47:44 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-12-29 17:29:14 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "player_thread"
|
|
|
|
|
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 {
|
2013-09-26 22:53:40 +02:00
|
|
|
player_control &pc;
|
2009-11-03 21:08:48 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
decoder_control &dc;
|
2009-10-31 19:22:56 +01:00
|
|
|
|
2013-09-26 22:45:49 +02:00
|
|
|
MusicBuffer &buffer;
|
|
|
|
|
2013-09-26 21:51:45 +02:00
|
|
|
MusicPipe *pipe;
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
/**
|
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;
|
|
|
|
|
2011-10-06 20:30:46 +02:00
|
|
|
/**
|
|
|
|
* Was any audio output opened successfully? It might have
|
|
|
|
* failed meanwhile, but was not explicitly closed by the
|
|
|
|
* player thread. When this flag is unset, some output
|
|
|
|
* methods must not be called.
|
|
|
|
*/
|
|
|
|
bool output_open;
|
|
|
|
|
2008-11-02 17:10:26 +01:00
|
|
|
/**
|
|
|
|
* the song currently being played
|
|
|
|
*/
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *song;
|
2008-11-02 17:10:26 +01:00
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
/**
|
|
|
|
* is cross fading enabled?
|
|
|
|
*/
|
|
|
|
enum xfade_state xfade;
|
2009-03-07 23:11:43 +01:00
|
|
|
|
2009-03-09 19:14:06 +01:00
|
|
|
/**
|
|
|
|
* has cross-fading begun?
|
|
|
|
*/
|
|
|
|
bool cross_fading;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of chunks used for crossfading.
|
|
|
|
*/
|
|
|
|
unsigned cross_fade_chunks;
|
|
|
|
|
2010-03-17 21:57:35 +01:00
|
|
|
/**
|
|
|
|
* The tag of the "next" song during cross-fade. It is
|
|
|
|
* postponed, and sent to the output thread when the new song
|
|
|
|
* really begins.
|
|
|
|
*/
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *cross_fade_tag;
|
2010-03-17 21:57:35 +01:00
|
|
|
|
2009-03-07 23:11:43 +01:00
|
|
|
/**
|
|
|
|
* The current audio format for the audio outputs.
|
|
|
|
*/
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat play_audio_format;
|
2009-10-30 16:28:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The time stamp of the chunk most recently sent to the
|
|
|
|
* output thread. This attribute is only used if
|
|
|
|
* audio_output_all_get_elapsed_time() didn't return a usable
|
|
|
|
* value; the output thread can estimate the elapsed time more
|
2011-03-31 21:43:53 +02:00
|
|
|
* precisely.
|
2009-10-30 16:28:15 +01:00
|
|
|
*/
|
|
|
|
float elapsed_time;
|
2013-01-02 20:36:28 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
player(player_control &_pc, decoder_control &_dc,
|
2013-09-26 22:45:49 +02:00
|
|
|
MusicBuffer &_buffer)
|
|
|
|
:pc(_pc), dc(_dc), buffer(_buffer),
|
2013-01-02 20:36:28 +01:00
|
|
|
buffering(false),
|
|
|
|
decoder_starting(false),
|
|
|
|
paused(false),
|
|
|
|
queued(true),
|
|
|
|
output_open(false),
|
2013-09-27 00:03:22 +02:00
|
|
|
song(nullptr),
|
2013-01-02 20:36:28 +01:00
|
|
|
xfade(XFADE_UNKNOWN),
|
|
|
|
cross_fading(false),
|
|
|
|
cross_fade_chunks(0),
|
2013-09-27 00:03:22 +02:00
|
|
|
cross_fade_tag(nullptr),
|
2013-01-02 20:36:28 +01:00
|
|
|
elapsed_time(0.0) {}
|
2013-09-26 23:57:31 +02:00
|
|
|
|
|
|
|
void ClearAndDeletePipe() {
|
|
|
|
pipe->Clear(buffer);
|
|
|
|
delete pipe;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearAndReplacePipe(MusicPipe *_pipe) {
|
|
|
|
ClearAndDeletePipe();
|
|
|
|
pipe = _pipe;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReplacePipe(MusicPipe *_pipe) {
|
|
|
|
delete pipe;
|
|
|
|
pipe = _pipe;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the decoder.
|
|
|
|
*
|
|
|
|
* Player lock is not held.
|
|
|
|
*/
|
|
|
|
void StartDecoder(MusicPipe &pipe);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The decoder has acknowledged the "START" command (see
|
|
|
|
* player::WaitForDecoder()). This function checks if the decoder
|
|
|
|
* initialization has completed yet.
|
|
|
|
*
|
|
|
|
* The player lock is not held.
|
|
|
|
*/
|
|
|
|
bool CheckDecoderStartup();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the decoder and clears (and frees) its music pipe.
|
|
|
|
*
|
|
|
|
* Player lock is not held.
|
|
|
|
*/
|
|
|
|
void StopDecoder();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the decoder still busy on the same song as the player?
|
|
|
|
*
|
|
|
|
* Note: this function does not check if the decoder is already
|
|
|
|
* finished.
|
|
|
|
*/
|
|
|
|
gcc_pure
|
|
|
|
bool IsDecoderAtCurrentSong() const {
|
|
|
|
assert(pipe != nullptr);
|
|
|
|
|
|
|
|
return dc.pipe == pipe;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the decoder is decoding the next song (or has begun
|
|
|
|
* decoding it, or has finished doing it), and the player hasn't
|
|
|
|
* switched to that song yet.
|
|
|
|
*/
|
|
|
|
gcc_pure
|
|
|
|
bool IsDecoderAtNextSong() const {
|
|
|
|
return dc.pipe != nullptr && !IsDecoderAtCurrentSong();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the handler for the #PLAYER_COMMAND_SEEK command.
|
|
|
|
*
|
|
|
|
* The player lock is not held.
|
|
|
|
*/
|
|
|
|
bool SeekDecoder();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* After the decoder has been started asynchronously, wait for
|
|
|
|
* the "START" command to finish. The decoder may not be
|
|
|
|
* initialized yet, i.e. there is no audio_format information
|
|
|
|
* yet.
|
|
|
|
*
|
|
|
|
* The player lock is not held.
|
|
|
|
*/
|
|
|
|
bool WaitForDecoder();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for audio_output_all_open(). Upon failure, it pauses the
|
|
|
|
* player.
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
bool OpenOutput();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains the next chunk from the music pipe, optionally applies
|
|
|
|
* cross-fading, and sends it to all audio outputs.
|
|
|
|
*
|
|
|
|
* @return true on success, false on error (playback will be stopped)
|
|
|
|
*/
|
|
|
|
bool PlayNextChunk();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a chunk of silence to the audio outputs. This is
|
|
|
|
* called when there is not enough decoded data in the pipe
|
|
|
|
* yet, to prevent underruns in the hardware buffers.
|
|
|
|
*
|
|
|
|
* The player lock is not held.
|
|
|
|
*/
|
|
|
|
bool SendSilence();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Player lock must be held before calling.
|
|
|
|
*/
|
|
|
|
void ProcessCommand();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is called at the border between two songs: the audio output
|
|
|
|
* has consumed all chunks of the current song, and we should start
|
|
|
|
* sending chunks from the next one.
|
|
|
|
*
|
|
|
|
* The player lock is not held.
|
|
|
|
*
|
|
|
|
* @return true on success, false on error (playback will be stopped)
|
|
|
|
*/
|
|
|
|
bool SongBorder();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The main loop of the player thread, during playback. This
|
|
|
|
* is basically a state machine, which multiplexes data
|
|
|
|
* between the decoder thread and the output threads.
|
|
|
|
*/
|
|
|
|
void Run();
|
2008-10-12 00:02:23 +02:00
|
|
|
};
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
static void
|
2013-09-26 22:53:40 +02:00
|
|
|
player_command_finished_locked(player_control &pc)
|
2008-08-26 08:45:15 +02:00
|
|
|
{
|
2013-09-26 22:53:40 +02:00
|
|
|
assert(pc.command != PLAYER_COMMAND_NONE);
|
2008-08-26 08:45:15 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.command = PLAYER_COMMAND_NONE;
|
|
|
|
pc.ClientSignal();
|
2009-10-31 17:02:12 +01:00
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
static void
|
2013-09-26 22:53:40 +02:00
|
|
|
player_command_finished(player_control &pc)
|
2009-10-31 17:02:12 +01:00
|
|
|
{
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2008-08-26 08:45:15 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
void
|
|
|
|
player::StartDecoder(MusicPipe &_pipe)
|
2009-11-03 21:01:56 +01:00
|
|
|
{
|
2013-09-26 23:57:31 +02:00
|
|
|
assert(queued || pc.command == PLAYER_COMMAND_SEEK);
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(pc.next_song != nullptr);
|
2009-11-03 21:01:56 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
unsigned start_ms = pc.next_song->start_ms;
|
|
|
|
if (pc.command == PLAYER_COMMAND_SEEK)
|
|
|
|
start_ms += (unsigned)(pc.seek_where * 1000);
|
2011-10-05 22:13:13 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.Start(pc.next_song->DupDetached(),
|
|
|
|
start_ms, pc.next_song->end_ms,
|
2013-09-26 23:57:31 +02:00
|
|
|
buffer, _pipe);
|
2009-11-03 21:01:56 +01:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
void
|
|
|
|
player::StopDecoder()
|
2010-11-05 19:08:54 +01:00
|
|
|
{
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.Stop();
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2013-09-27 00:03:22 +02:00
|
|
|
if (dc.pipe != nullptr) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* clear and free the decoder pipe */
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
dc.pipe->Clear(buffer);
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (dc.pipe != pipe)
|
2013-09-26 22:53:40 +02:00
|
|
|
delete dc.pipe;
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2013-09-27 00:03:22 +02:00
|
|
|
dc.pipe = nullptr;
|
2009-03-06 00:42:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
bool
|
|
|
|
player::WaitForDecoder()
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2013-09-26 23:57:31 +02:00
|
|
|
assert(queued || pc.command == PLAYER_COMMAND_SEEK);
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(pc.next_song != nullptr);
|
2009-11-03 21:02:54 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
queued = false;
|
2009-11-03 21:02:54 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
Error error = dc.LockGetError();
|
2013-08-10 18:02:44 +02:00
|
|
|
if (error.IsDefined()) {
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
|
|
|
pc.SetError(PLAYER_ERROR_DECODER, std::move(error));
|
2012-08-09 22:19:39 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.next_song->Free();
|
2013-09-27 00:03:22 +02:00
|
|
|
pc.next_song = nullptr;
|
2012-08-09 22:19:39 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-01-21 16:31:15 +01:00
|
|
|
return false;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (song != nullptr)
|
|
|
|
song->Free();
|
2012-08-09 22:19:39 +02:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
song = pc.next_song;
|
|
|
|
elapsed_time = 0.0;
|
2009-11-03 21:02:54 +01:00
|
|
|
|
|
|
|
/* set the "starting" flag, which will be cleared by
|
|
|
|
player_check_decoder_startup() */
|
2013-09-26 23:57:31 +02:00
|
|
|
decoder_starting = true;
|
2009-11-03 21:02:54 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-11-03 21:02:54 +01:00
|
|
|
|
|
|
|
/* update player_control's song information */
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.total_time = pc.next_song->GetDuration();
|
|
|
|
pc.bit_rate = 0;
|
|
|
|
pc.audio_format.Clear();
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2009-11-03 21:02:54 +01:00
|
|
|
/* clear the queued song */
|
2013-09-27 00:03:22 +02:00
|
|
|
pc.next_song = nullptr;
|
2009-03-11 09:35:16 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2009-01-03 20:49:51 +01:00
|
|
|
/* call syncPlaylistWithQueue() in the main thread */
|
2013-01-09 23:12:53 +01:00
|
|
|
GlobalEvents::Emit(GlobalEvents::PLAYLIST);
|
2009-01-03 20:49:51 +01:00
|
|
|
|
2009-01-21 16:31:15 +01:00
|
|
|
return true;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2009-12-25 23:12:10 +01:00
|
|
|
/**
|
|
|
|
* Returns the real duration of the song, comprising the duration
|
|
|
|
* indicated by the decoder plugin.
|
|
|
|
*/
|
|
|
|
static double
|
2013-07-28 13:25:12 +02:00
|
|
|
real_song_duration(const Song *song, double decoder_duration)
|
2009-12-25 23:12:10 +01:00
|
|
|
{
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(song != nullptr);
|
2009-12-25 23:12:10 +01:00
|
|
|
|
|
|
|
if (decoder_duration <= 0.0)
|
|
|
|
/* the decoder plugin didn't provide information; fall
|
2013-07-28 13:25:12 +02:00
|
|
|
back to Song::GetDuration() */
|
|
|
|
return song->GetDuration();
|
2009-12-25 23:12:10 +01:00
|
|
|
|
|
|
|
if (song->end_ms > 0 && song->end_ms / 1000.0 < decoder_duration)
|
|
|
|
return (song->end_ms - song->start_ms) / 1000.0;
|
|
|
|
|
|
|
|
return decoder_duration - song->start_ms / 1000.0;
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
bool
|
|
|
|
player::OpenOutput()
|
2011-10-06 20:55:52 +02:00
|
|
|
{
|
2013-09-26 23:57:31 +02:00
|
|
|
assert(play_audio_format.IsDefined());
|
2013-09-26 22:53:40 +02:00
|
|
|
assert(pc.state == PLAYER_STATE_PLAY ||
|
|
|
|
pc.state == PLAYER_STATE_PAUSE);
|
2011-10-06 20:55:52 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2013-09-26 23:57:31 +02:00
|
|
|
if (audio_output_all_open(play_audio_format, buffer, error)) {
|
|
|
|
output_open = true;
|
|
|
|
paused = false;
|
2011-10-06 20:55:52 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
|
|
|
pc.state = PLAYER_STATE_PLAY;
|
|
|
|
pc.Unlock();
|
2011-10-06 20:55:52 +02:00
|
|
|
|
2013-04-08 21:22:06 +02:00
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
|
2011-10-06 20:55:52 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
2013-08-10 18:02:44 +02:00
|
|
|
g_warning("%s", error.GetMessage());
|
2012-08-08 22:47:51 +02:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
output_open = false;
|
2011-10-06 20:30:46 +02:00
|
|
|
|
2011-10-06 20:55:52 +02:00
|
|
|
/* pause: the user may resume playback as soon as an
|
|
|
|
audio output becomes available */
|
2013-09-26 23:57:31 +02:00
|
|
|
paused = true;
|
2011-10-06 20:55:52 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
|
|
|
pc.SetError(PLAYER_ERROR_OUTPUT, std::move(error));
|
|
|
|
pc.state = PLAYER_STATE_PAUSE;
|
|
|
|
pc.Unlock();
|
2011-10-06 20:55:52 +02:00
|
|
|
|
2013-02-04 10:15:34 +01:00
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
|
2011-10-06 20:55:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
bool
|
|
|
|
player::CheckDecoderStartup()
|
2009-03-07 23:11:43 +01:00
|
|
|
{
|
2013-09-26 23:57:31 +02:00
|
|
|
assert(decoder_starting);
|
2009-03-07 23:11:43 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.Lock();
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
Error error = dc.GetError();
|
2013-08-10 18:02:44 +02:00
|
|
|
if (error.IsDefined()) {
|
2009-03-07 23:11:43 +01:00
|
|
|
/* the decoder failed */
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.Unlock();
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
|
|
|
pc.SetError(PLAYER_ERROR_DECODER, std::move(error));
|
|
|
|
pc.Unlock();
|
2009-03-07 23:11:43 +01:00
|
|
|
|
|
|
|
return false;
|
2013-09-26 22:53:40 +02:00
|
|
|
} else if (!dc.IsStarting()) {
|
2009-03-07 23:11:43 +01:00
|
|
|
/* the decoder is ready and ok */
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.Unlock();
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (output_open &&
|
2013-09-26 22:53:40 +02:00
|
|
|
!audio_output_all_wait(&pc, 1))
|
2009-03-09 19:25:26 +01:00
|
|
|
/* the output devices havn't finished playing
|
|
|
|
all chunks yet - wait for that */
|
|
|
|
return true;
|
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
|
|
|
pc.total_time = real_song_duration(dc.song, dc.total_time);
|
|
|
|
pc.audio_format = dc.in_audio_format;
|
|
|
|
pc.Unlock();
|
2009-11-03 21:23:48 +01:00
|
|
|
|
2013-04-08 21:22:06 +02:00
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
play_audio_format = dc.out_audio_format;
|
|
|
|
decoder_starting = false;
|
2009-03-07 23:11:43 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (!paused && !OpenOutput()) {
|
2013-09-26 22:53:40 +02:00
|
|
|
char *uri = dc.song->GetURI();
|
2009-03-07 23:11:43 +01:00
|
|
|
g_warning("problems opening audio device "
|
|
|
|
"while playing \"%s\"", uri);
|
|
|
|
g_free(uri);
|
|
|
|
|
2009-04-25 11:36:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
2009-03-07 23:11:43 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
/* the decoder is not yet ready; wait
|
|
|
|
some more */
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.WaitForDecoder();
|
|
|
|
dc.Unlock();
|
2009-03-07 23:11:43 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
bool
|
|
|
|
player::SendSilence()
|
2009-03-10 20:43:19 +01:00
|
|
|
{
|
2013-09-26 23:57:31 +02:00
|
|
|
assert(output_open);
|
|
|
|
assert(play_audio_format.IsDefined());
|
2009-03-10 20:43:19 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
struct music_chunk *chunk = buffer.Allocate();
|
2013-09-27 00:03:22 +02:00
|
|
|
if (chunk == nullptr) {
|
2009-03-10 20:43:19 +01:00
|
|
|
g_warning("Failed to allocate silence buffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2013-09-26 23:57:31 +02:00
|
|
|
chunk->audio_format = play_audio_format;
|
2009-03-10 20:43:19 +01:00
|
|
|
#endif
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
const size_t frame_size = play_audio_format.GetFrameSize();
|
2011-01-07 22:29:47 +01:00
|
|
|
/* this formula ensures that we don't send
|
|
|
|
partial frames */
|
|
|
|
unsigned num_frames = sizeof(chunk->data) / frame_size;
|
|
|
|
|
2009-11-12 18:41:25 +01:00
|
|
|
chunk->times = -1.0; /* undefined time stamp */
|
2009-03-10 20:43:19 +01:00
|
|
|
chunk->length = num_frames * frame_size;
|
|
|
|
memset(chunk->data, 0, chunk->length);
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
if (!audio_output_all_play(chunk, error)) {
|
|
|
|
g_warning("%s", error.GetMessage());
|
2013-09-26 23:57:31 +02:00
|
|
|
buffer.Return(chunk);
|
2009-03-10 20:43:19 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
inline bool
|
|
|
|
player::SeekDecoder()
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(pc.next_song != nullptr);
|
2009-03-10 18:04:09 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
const unsigned start_ms = pc.next_song->start_ms;
|
2012-08-15 22:51:48 +02:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (!dc.LockIsCurrentSong(pc.next_song)) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* the decoder is already decoding the "next" song -
|
|
|
|
stop it and start the previous song again */
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
StopDecoder();
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/* clear music chunks which might still reside in the
|
|
|
|
pipe */
|
2013-09-26 23:57:31 +02:00
|
|
|
pipe->Clear(buffer);
|
2009-01-21 16:36:30 +01:00
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/* re-start the decoder */
|
2013-09-26 23:57:31 +02:00
|
|
|
StartDecoder(*pipe);
|
|
|
|
if (!WaitForDecoder()) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* decoder failure */
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished(pc);
|
2009-01-21 16:36:30 +01:00
|
|
|
return false;
|
2009-03-10 18:04:09 +01:00
|
|
|
}
|
2009-02-10 00:17:34 +01:00
|
|
|
} else {
|
2013-09-26 23:57:31 +02:00
|
|
|
if (!IsDecoderAtCurrentSong()) {
|
2010-11-05 19:24:42 +01:00
|
|
|
/* the decoder is already decoding the "next" song,
|
|
|
|
but it is the same song file; exchange the pipe */
|
2013-09-26 23:57:31 +02:00
|
|
|
ClearAndReplacePipe(dc.pipe);
|
2010-11-05 19:24:42 +01:00
|
|
|
}
|
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.next_song->Free();
|
2013-09-27 00:03:22 +02:00
|
|
|
pc.next_song = nullptr;
|
2013-09-26 23:57:31 +02:00
|
|
|
queued = false;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
2008-08-26 08:27:18 +02:00
|
|
|
|
2009-03-25 19:48:14 +01:00
|
|
|
/* wait for the decoder to complete initialization */
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
while (decoder_starting) {
|
|
|
|
if (!CheckDecoderStartup()) {
|
2009-03-25 19:48:14 +01:00
|
|
|
/* decoder failure */
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished(pc);
|
2009-03-25 19:48:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/* send the SEEK command */
|
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
double 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;
|
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
if (!dc.Seek(where + start_ms / 1000.0)) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* decoder failure */
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished(pc);
|
2009-03-10 17:52:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
elapsed_time = where;
|
2009-10-30 16:28:15 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
xfade = XFADE_UNKNOWN;
|
2009-03-10 17:52:38 +01:00
|
|
|
|
2009-03-10 21:19:51 +01:00
|
|
|
/* re-fill the buffer after seeking */
|
2013-09-26 23:57:31 +02:00
|
|
|
buffering = true;
|
2009-03-10 17:52:38 +01:00
|
|
|
|
|
|
|
audio_output_all_cancel();
|
|
|
|
|
|
|
|
return true;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
inline void
|
|
|
|
player::ProcessCommand()
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2013-09-26 22:53:40 +02:00
|
|
|
switch (pc.command) {
|
2008-08-26 08:27:09 +02:00
|
|
|
case PLAYER_COMMAND_NONE:
|
|
|
|
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;
|
|
|
|
|
2009-10-23 10:55:52 +02:00
|
|
|
case PLAYER_COMMAND_UPDATE_AUDIO:
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2009-10-23 10:55:52 +02:00
|
|
|
audio_output_all_enable_disable();
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2009-10-23 10:55:52 +02:00
|
|
|
break;
|
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
case PLAYER_COMMAND_QUEUE:
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(pc.next_song != nullptr);
|
2013-09-26 23:57:31 +02:00
|
|
|
assert(!queued);
|
|
|
|
assert(!IsDecoderAtNextSong());
|
2008-11-14 17:55:51 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
queued = true;
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_COMMAND_PAUSE:
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
paused = !paused;
|
|
|
|
if (paused) {
|
2009-02-10 18:51:51 +01:00
|
|
|
audio_output_all_pause();
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.state = PLAYER_STATE_PAUSE;
|
2013-09-26 23:57:31 +02:00
|
|
|
} else if (!play_audio_format.IsDefined()) {
|
2009-03-10 21:00:52 +01:00
|
|
|
/* the decoder hasn't provided an audio format
|
|
|
|
yet - don't open the audio device yet */
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-03-10 21:00:52 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.state = PLAYER_STATE_PLAY;
|
2008-08-26 08:27:09 +02:00
|
|
|
} else {
|
2013-09-26 23:57:31 +02:00
|
|
|
OpenOutput();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
2009-03-10 21:00:52 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_COMMAND_SEEK:
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2013-09-26 23:57:31 +02:00
|
|
|
SeekDecoder();
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
2008-10-12 00:07:54 +02:00
|
|
|
|
|
|
|
case PLAYER_COMMAND_CANCEL:
|
2013-09-27 00:03:22 +02:00
|
|
|
if (pc.next_song == nullptr) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* the cancel request arrived too late, we're
|
2008-10-12 00:07:54 +02:00
|
|
|
already playing the queued song... stop
|
|
|
|
everything now */
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.command = PLAYER_COMMAND_STOP;
|
2008-10-12 00:07:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (IsDecoderAtNextSong()) {
|
2008-10-12 00:07:54 +02:00
|
|
|
/* the decoder is already decoding the song -
|
|
|
|
stop it and reset the position */
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2013-09-26 23:57:31 +02:00
|
|
|
StopDecoder();
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-10-31 17:02:12 +01:00
|
|
|
}
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.next_song->Free();
|
2013-09-27 00:03:22 +02:00
|
|
|
pc.next_song = nullptr;
|
2013-09-26 23:57:31 +02:00
|
|
|
queued = false;
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2008-10-12 00:07:54 +02:00
|
|
|
break;
|
2009-10-08 22:09:25 +02:00
|
|
|
|
|
|
|
case PLAYER_COMMAND_REFRESH:
|
2013-09-26 23:57:31 +02:00
|
|
|
if (output_open && !paused) {
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2009-10-08 22:09:25 +02:00
|
|
|
audio_output_all_check();
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-10-31 17:02:12 +01:00
|
|
|
}
|
2009-10-08 22:09:25 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.elapsed_time = audio_output_all_get_elapsed_time();
|
|
|
|
if (pc.elapsed_time < 0.0)
|
2013-09-26 23:57:31 +02:00
|
|
|
pc.elapsed_time = elapsed_time;
|
2009-10-30 16:28:15 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2009-10-08 22:09:25 +02:00
|
|
|
break;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-23 12:26:26 +02:00
|
|
|
static void
|
2013-07-30 20:11:57 +02:00
|
|
|
update_song_tag(Song *song, const Tag &new_tag)
|
2009-07-23 12:26:26 +02:00
|
|
|
{
|
2013-07-28 13:25:12 +02:00
|
|
|
if (song->IsFile())
|
2009-07-23 12:26:26 +02:00
|
|
|
/* don't update tags of local files, only remote
|
|
|
|
streams may change tags dynamically */
|
|
|
|
return;
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *old_tag = song->tag;
|
|
|
|
song->tag = new Tag(new_tag);
|
2009-07-23 12:26:26 +02:00
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
delete old_tag;
|
2009-07-23 12:26:26 +02:00
|
|
|
|
|
|
|
/* the main thread will update the playlist version when he
|
|
|
|
receives this event */
|
2013-01-09 23:12:53 +01:00
|
|
|
GlobalEvents::Emit(GlobalEvents::TAG);
|
2009-07-23 12:26:26 +02:00
|
|
|
|
|
|
|
/* notify all clients that the tag of the current song has
|
|
|
|
changed */
|
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
}
|
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/**
|
|
|
|
* Plays a #music_chunk object (after applying software volume). If
|
|
|
|
* it contains a (stream) tag, copy it to the current song, so MPD's
|
|
|
|
* playlist reflects the new stream tag.
|
2009-10-31 17:02:12 +01:00
|
|
|
*
|
|
|
|
* Player lock is not held.
|
2009-03-11 09:35:16 +01:00
|
|
|
*/
|
2009-01-21 16:31:15 +01:00
|
|
|
static bool
|
2013-09-26 22:53:40 +02:00
|
|
|
play_chunk(player_control &pc,
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *song, struct music_chunk *chunk,
|
2013-09-26 22:45:49 +02:00
|
|
|
MusicBuffer &buffer,
|
2013-08-03 21:00:50 +02:00
|
|
|
const AudioFormat format,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(chunk->CheckFormat(format));
|
2009-03-08 13:45:24 +01:00
|
|
|
|
2013-09-27 00:03:22 +02:00
|
|
|
if (chunk->tag != nullptr)
|
2013-07-30 20:11:57 +02:00
|
|
|
update_song_tag(song, *chunk->tag);
|
2008-11-02 17:13:26 +01:00
|
|
|
|
2009-06-02 08:37:30 +02:00
|
|
|
if (chunk->length == 0) {
|
2013-09-26 22:45:49 +02:00
|
|
|
buffer.Return(chunk);
|
2009-03-17 20:28:37 +01:00
|
|
|
return true;
|
2009-06-02 08:37:30 +02:00
|
|
|
}
|
2009-03-17 20:28:37 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
|
|
|
pc.bit_rate = chunk->bit_rate;
|
|
|
|
pc.Unlock();
|
2009-03-17 20:28:37 +01:00
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/* send the chunk to the audio outputs */
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!audio_output_all_play(chunk, error))
|
2009-01-21 16:31:15 +01:00
|
|
|
return false;
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.total_play_time += (double)chunk->length /
|
2013-08-03 21:00:50 +02:00
|
|
|
format.GetTimeToSize();
|
2009-01-21 16:31:15 +01:00
|
|
|
return true;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
inline bool
|
|
|
|
player::PlayNextChunk()
|
2009-03-09 19:14:06 +01:00
|
|
|
{
|
2013-09-26 22:53:40 +02:00
|
|
|
if (!audio_output_all_wait(&pc, 64))
|
2009-03-11 09:35:16 +01:00
|
|
|
/* the output pipe is still large enough, don't send
|
|
|
|
another chunk */
|
2009-03-09 19:25:26 +01:00
|
|
|
return true;
|
|
|
|
|
2011-01-07 22:29:47 +01:00
|
|
|
unsigned cross_fade_position;
|
2013-09-27 00:03:22 +02:00
|
|
|
struct music_chunk *chunk = nullptr;
|
2013-09-26 23:57:31 +02:00
|
|
|
if (xfade == XFADE_ENABLED && IsDecoderAtNextSong() &&
|
|
|
|
(cross_fade_position = pipe->GetSize()) <= cross_fade_chunks) {
|
2009-03-09 19:14:06 +01:00
|
|
|
/* perform cross fade */
|
2013-09-26 22:53:40 +02:00
|
|
|
music_chunk *other_chunk = dc.pipe->Shift();
|
2009-03-09 19:14:06 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (!cross_fading) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* beginning of the cross fade - adjust
|
|
|
|
crossFadeChunks which might be bigger than
|
|
|
|
the remaining number of chunks in the old
|
|
|
|
song */
|
2013-09-26 23:57:31 +02:00
|
|
|
cross_fade_chunks = cross_fade_position;
|
|
|
|
cross_fading = true;
|
2009-03-09 19:14:06 +01:00
|
|
|
}
|
|
|
|
|
2013-09-27 00:03:22 +02:00
|
|
|
if (other_chunk != nullptr) {
|
2013-09-26 23:57:31 +02:00
|
|
|
chunk = pipe->Shift();
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(chunk != nullptr);
|
|
|
|
assert(chunk->other == nullptr);
|
2009-03-09 19:14:06 +01:00
|
|
|
|
2010-03-17 21:57:35 +01:00
|
|
|
/* don't send the tags of the new song (which
|
|
|
|
is being faded in) yet; postpone it until
|
|
|
|
the current song is faded out */
|
2013-09-26 23:57:31 +02:00
|
|
|
cross_fade_tag =
|
|
|
|
Tag::MergeReplace(cross_fade_tag,
|
2010-03-17 21:57:35 +01:00
|
|
|
other_chunk->tag);
|
2013-09-27 00:03:22 +02:00
|
|
|
other_chunk->tag = nullptr;
|
2010-03-17 21:57:35 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
if (std::isnan(pc.mixramp_delay_seconds)) {
|
2010-05-02 15:31:31 +02:00
|
|
|
chunk->mix_ratio = ((float)cross_fade_position)
|
2013-09-26 23:57:31 +02:00
|
|
|
/ cross_fade_chunks;
|
2010-03-21 18:21:47 +01:00
|
|
|
} else {
|
2010-05-02 15:31:31 +02:00
|
|
|
chunk->mix_ratio = nan("");
|
2010-03-21 18:21:47 +01:00
|
|
|
}
|
2011-01-07 23:45:51 +01:00
|
|
|
|
2013-01-04 21:38:46 +01:00
|
|
|
if (other_chunk->IsEmpty()) {
|
2011-01-07 23:45:51 +01:00
|
|
|
/* the "other" chunk was a music_chunk
|
|
|
|
which had only a tag, but no music
|
|
|
|
data - we cannot cross-fade that;
|
|
|
|
but since this happens only at the
|
|
|
|
beginning of the new song, we can
|
|
|
|
easily recover by throwing it away
|
|
|
|
now */
|
2013-09-26 23:57:31 +02:00
|
|
|
buffer.Return(other_chunk);
|
2013-09-27 00:03:22 +02:00
|
|
|
other_chunk = nullptr;
|
2011-01-07 23:45:51 +01:00
|
|
|
}
|
2010-03-21 18:21:47 +01:00
|
|
|
|
2010-05-02 15:31:31 +02:00
|
|
|
chunk->other = other_chunk;
|
2009-03-09 19:14:06 +01:00
|
|
|
} else {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* there are not enough decoded chunks yet */
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.Lock();
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
if (dc.IsIdle()) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* the decoder isn't running, abort
|
2009-03-09 19:14:06 +01:00
|
|
|
cross fading */
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.Unlock();
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
xfade = XFADE_DISABLED;
|
2009-03-09 19:14:06 +01:00
|
|
|
} else {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* wait for the decoder */
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.Signal();
|
|
|
|
dc.WaitForDecoder();
|
|
|
|
dc.Unlock();
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2009-03-09 19:14:06 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 00:03:22 +02:00
|
|
|
if (chunk == nullptr)
|
2013-09-26 23:57:31 +02:00
|
|
|
chunk = pipe->Shift();
|
2009-03-09 19:14:06 +01:00
|
|
|
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(chunk != nullptr);
|
2009-03-09 19:14:06 +01:00
|
|
|
|
2010-03-17 21:57:35 +01:00
|
|
|
/* insert the postponed tag if cross-fading is finished */
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (xfade != XFADE_ENABLED && cross_fade_tag != nullptr) {
|
|
|
|
chunk->tag = Tag::MergeReplace(chunk->tag, cross_fade_tag);
|
|
|
|
cross_fade_tag = nullptr;
|
2010-03-17 21:57:35 +01:00
|
|
|
}
|
|
|
|
|
2009-03-09 19:14:06 +01:00
|
|
|
/* play the current chunk */
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2013-09-26 23:57:31 +02:00
|
|
|
if (!play_chunk(pc, song, chunk, buffer, play_audio_format, error)) {
|
2013-08-10 18:02:44 +02:00
|
|
|
g_warning("%s", error.GetMessage());
|
2012-08-08 22:47:51 +02:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
buffer.Return(chunk);
|
2009-04-25 11:55:36 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.SetError(PLAYER_ERROR_OUTPUT, std::move(error));
|
2009-10-29 22:39:48 +01:00
|
|
|
|
2009-04-25 11:55:36 +02:00
|
|
|
/* pause: the user may resume playback as soon as an
|
|
|
|
audio output becomes available */
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.state = PLAYER_STATE_PAUSE;
|
2013-09-26 23:57:31 +02:00
|
|
|
paused = true;
|
2009-04-25 11:55:36 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2013-02-04 10:15:34 +01:00
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
|
2009-03-09 19:14:06 +01:00
|
|
|
return false;
|
2009-03-09 19:25:26 +01:00
|
|
|
}
|
2009-03-09 19:14:06 +01:00
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/* this formula should prevent that the decoder gets woken up
|
|
|
|
with each chunk; it is more efficient to make it decode a
|
2009-03-09 19:14:06 +01:00
|
|
|
larger block at a time */
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.Lock();
|
|
|
|
if (!dc.IsIdle() &&
|
|
|
|
dc.pipe->GetSize() <= (pc.buffered_before_play +
|
2013-09-26 23:57:31 +02:00
|
|
|
buffer.GetSize() * 3) / 4)
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.Signal();
|
|
|
|
dc.Unlock();
|
2009-03-09 19:14:06 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
inline bool
|
|
|
|
player::SongBorder()
|
2009-03-09 19:15:14 +01:00
|
|
|
{
|
2013-09-26 23:57:31 +02:00
|
|
|
xfade = XFADE_UNKNOWN;
|
2009-03-09 19:15:14 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
char *uri = song->GetURI();
|
2009-07-06 22:29:58 +02:00
|
|
|
g_message("played \"%s\"", uri);
|
|
|
|
g_free(uri);
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
ReplacePipe(dc.pipe);
|
2009-03-09 19:15:14 +01:00
|
|
|
|
2010-01-02 21:16:51 +01:00
|
|
|
audio_output_all_song_border();
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (!WaitForDecoder())
|
2009-03-09 19:25:26 +01:00
|
|
|
return false;
|
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2012-08-25 08:44:31 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
const bool border_pause = pc.border_pause;
|
2013-02-04 10:15:34 +01:00
|
|
|
if (border_pause) {
|
2013-09-26 23:57:31 +02:00
|
|
|
paused = true;
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.state = PLAYER_STATE_PAUSE;
|
2012-08-25 08:44:31 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2012-08-25 08:44:31 +02:00
|
|
|
|
2013-02-04 10:15:34 +01:00
|
|
|
if (border_pause)
|
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
|
2009-03-09 19:25:26 +01:00
|
|
|
return true;
|
2009-03-09 19:15:14 +01:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
inline void
|
|
|
|
player::Run()
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2013-09-26 23:57:31 +02:00
|
|
|
pipe = new MusicPipe();
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
StartDecoder(*pipe);
|
|
|
|
if (!WaitForDecoder()) {
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(song == nullptr);
|
2012-08-09 22:19:39 +02:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
StopDecoder();
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished(pc);
|
2013-09-26 23:57:31 +02:00
|
|
|
delete pipe;
|
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
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
|
|
|
pc.state = PLAYER_STATE_PLAY;
|
2011-10-05 22:13:13 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
if (pc.command == PLAYER_COMMAND_SEEK)
|
2013-09-26 23:57:31 +02:00
|
|
|
elapsed_time = pc.seek_where;
|
2011-10-05 22:13:13 +02:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2009-01-21 16:31:15 +01:00
|
|
|
while (true) {
|
2013-09-26 23:57:31 +02:00
|
|
|
ProcessCommand();
|
2013-09-26 22:53:40 +02:00
|
|
|
if (pc.command == PLAYER_COMMAND_STOP ||
|
|
|
|
pc.command == PLAYER_COMMAND_EXIT ||
|
|
|
|
pc.command == PLAYER_COMMAND_CLOSE_AUDIO) {
|
|
|
|
pc.Unlock();
|
2009-02-10 18:51:51 +01:00
|
|
|
audio_output_all_cancel();
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (buffering) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* buffering at the start of the song - wait
|
|
|
|
until the buffer is large enough, to
|
|
|
|
prevent stuttering on slow machines */
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (pipe->GetSize() < pc.buffered_before_play &&
|
2013-09-26 22:53:40 +02:00
|
|
|
!dc.LockIsIdle()) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* not enough decoded buffer space yet */
|
2009-03-10 21:19:51 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (!paused && output_open &&
|
2009-03-10 21:19:51 +01:00
|
|
|
audio_output_all_check() < 4 &&
|
2013-09-26 23:57:31 +02:00
|
|
|
!SendSilence())
|
2009-03-10 21:19:51 +01:00
|
|
|
break;
|
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.Lock();
|
2009-10-31 17:02:12 +01:00
|
|
|
/* XXX race condition: check decoder again */
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.WaitForDecoder();
|
|
|
|
dc.Unlock();
|
|
|
|
pc.Lock();
|
2008-08-26 08:27:09 +02:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* buffering is complete */
|
2013-09-26 23:57:31 +02:00
|
|
|
buffering = false;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (decoder_starting) {
|
2009-03-11 09:35:16 +01:00
|
|
|
/* wait until the decoder is initialized completely */
|
2008-10-29 22:17:42 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (!CheckDecoderStartup())
|
2009-03-07 23:11:43 +01:00
|
|
|
break;
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-03-07 23:11:43 +01:00
|
|
|
continue;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2008-11-13 02:06:58 +01:00
|
|
|
#ifndef NDEBUG
|
2008-11-23 22:16:54 +01:00
|
|
|
/*
|
2008-11-13 02:06:58 +01:00
|
|
|
music_pipe_check_format(&play_audio_format,
|
2013-09-26 23:57:31 +02:00
|
|
|
next_song_chunk,
|
2013-09-26 22:53:40 +02:00
|
|
|
&dc.out_audio_format);
|
2008-11-23 22:16:54 +01:00
|
|
|
*/
|
2008-11-13 02:06:58 +01:00
|
|
|
#endif
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (dc.LockIsIdle() && queued && dc.pipe == pipe) {
|
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
|
|
|
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(dc.pipe == nullptr || dc.pipe == pipe);
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
StartDecoder(*new MusicPipe());
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2012-08-25 08:44:31 +02:00
|
|
|
if (/* no cross-fading if MPD is going to pause at the
|
|
|
|
end of the current song */
|
2013-09-26 22:53:40 +02:00
|
|
|
!pc.border_pause &&
|
2013-09-26 23:57:31 +02:00
|
|
|
IsDecoderAtNextSong() &&
|
|
|
|
xfade == XFADE_UNKNOWN &&
|
2013-09-26 22:53:40 +02:00
|
|
|
!dc.LockIsStarting()) {
|
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 */
|
2013-09-26 23:57:31 +02:00
|
|
|
cross_fade_chunks =
|
2013-09-26 22:53:40 +02:00
|
|
|
cross_fade_calc(pc.cross_fade_seconds, dc.total_time,
|
|
|
|
pc.mixramp_db,
|
|
|
|
pc.mixramp_delay_seconds,
|
|
|
|
dc.replay_gain_db,
|
|
|
|
dc.replay_gain_prev_db,
|
|
|
|
dc.mixramp_start,
|
|
|
|
dc.mixramp_prev_end,
|
|
|
|
dc.out_audio_format,
|
2013-09-26 23:57:31 +02:00
|
|
|
play_audio_format,
|
|
|
|
buffer.GetSize() -
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.buffered_before_play);
|
2013-09-26 23:57:31 +02:00
|
|
|
if (cross_fade_chunks > 0) {
|
|
|
|
xfade = XFADE_ENABLED;
|
|
|
|
cross_fading = false;
|
2008-08-26 08:27:09 +02:00
|
|
|
} else
|
|
|
|
/* cross fading is disabled or the
|
|
|
|
next song is too short */
|
2013-09-26 23:57:31 +02:00
|
|
|
xfade = XFADE_DISABLED;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (paused) {
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-11-02 20:20:13 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
if (pc.command == PLAYER_COMMAND_NONE)
|
|
|
|
pc.Wait();
|
2009-10-31 17:02:12 +01:00
|
|
|
continue;
|
2013-09-26 23:57:31 +02:00
|
|
|
} else if (!pipe->IsEmpty()) {
|
2009-03-09 19:14:06 +01:00
|
|
|
/* at least one music chunk is ready - send it
|
|
|
|
to the audio output */
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
PlayNextChunk();
|
2009-03-09 19:25:26 +01:00
|
|
|
} else if (audio_output_all_check() > 0) {
|
|
|
|
/* not enough data from decoder, but the
|
|
|
|
output thread is still busy, so it's
|
|
|
|
okay */
|
|
|
|
|
|
|
|
/* XXX synchronize in a better way */
|
|
|
|
g_usleep(10000);
|
2013-09-26 23:57:31 +02:00
|
|
|
} else if (IsDecoderAtNextSong()) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* at the beginning of a new song */
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (!SongBorder())
|
2009-01-21 16:44:32 +01:00
|
|
|
break;
|
2013-09-26 22:53:40 +02:00
|
|
|
} else if (dc.LockIsIdle()) {
|
2009-06-02 08:39:17 +02:00
|
|
|
/* check the size of the pipe again, because
|
|
|
|
the decoder thread may have added something
|
|
|
|
since we last checked */
|
2013-09-26 23:57:31 +02:00
|
|
|
if (pipe->IsEmpty()) {
|
2009-11-09 22:22:27 +01:00
|
|
|
/* wait for the hardware to finish
|
|
|
|
playback */
|
|
|
|
audio_output_all_drain();
|
2009-06-02 08:39:17 +02:00
|
|
|
break;
|
2009-11-09 22:22:27 +01:00
|
|
|
}
|
2013-09-26 23:57:31 +02:00
|
|
|
} else if (output_open) {
|
2009-03-09 19:25:26 +01:00
|
|
|
/* the decoder is too busy and hasn't provided
|
|
|
|
new PCM data in time: send silence (if the
|
|
|
|
output pipe is empty) */
|
2013-09-26 23:57:31 +02:00
|
|
|
if (!SendSilence())
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
StopDecoder();
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
ClearAndDeletePipe();
|
2009-03-11 09:20:34 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
delete cross_fade_tag;
|
2010-03-17 21:57:35 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (song != nullptr)
|
|
|
|
song->Free();
|
2012-08-09 22:19:39 +02:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-11-03 23:32:37 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (queued) {
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(pc.next_song != nullptr);
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.next_song->Free();
|
2013-09-27 00:03:22 +02:00
|
|
|
pc.next_song = nullptr;
|
2009-11-03 23:32:37 +01:00
|
|
|
}
|
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.state = PLAYER_STATE_STOP;
|
2009-11-03 23:32:37 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
static void
|
|
|
|
do_play(player_control &pc, decoder_control &dc,
|
|
|
|
MusicBuffer &buffer)
|
|
|
|
{
|
|
|
|
player player(pc, dc, buffer);
|
|
|
|
player.Run();
|
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
static gpointer
|
|
|
|
player_task(gpointer arg)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2013-09-26 22:53:40 +02:00
|
|
|
player_control &pc = *(player_control *)arg;
|
2009-10-31 19:22:56 +01:00
|
|
|
|
2013-09-26 22:48:55 +02:00
|
|
|
decoder_control dc;
|
|
|
|
decoder_thread_start(&dc);
|
2009-01-25 13:44:39 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
MusicBuffer buffer(pc.buffer_chunks);
|
2009-03-09 19:12:06 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
while (1) {
|
2013-09-26 22:53:40 +02:00
|
|
|
switch (pc.command) {
|
2011-10-05 22:13:13 +02:00
|
|
|
case PLAYER_COMMAND_SEEK:
|
2008-10-12 00:07:54 +02:00
|
|
|
case PLAYER_COMMAND_QUEUE:
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(pc.next_song != nullptr);
|
2009-02-10 08:18:28 +01:00
|
|
|
|
2013-09-26 23:58:41 +02:00
|
|
|
pc.Unlock();
|
2013-09-26 22:53:40 +02:00
|
|
|
do_play(pc, dc, buffer);
|
2013-09-26 23:58:41 +02:00
|
|
|
GlobalEvents::Emit(GlobalEvents::PLAYLIST);
|
|
|
|
pc.Lock();
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_COMMAND_STOP:
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2009-03-09 19:25:26 +01:00
|
|
|
audio_output_all_cancel();
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-03-09 19:25:26 +01:00
|
|
|
/* fall through */
|
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
case PLAYER_COMMAND_PAUSE:
|
2013-09-27 00:03:22 +02:00
|
|
|
if (pc.next_song != nullptr) {
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.next_song->Free();
|
2013-09-27 00:03:22 +02:00
|
|
|
pc.next_song = nullptr;
|
2012-08-09 22:19:39 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_COMMAND_CLOSE_AUDIO:
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2010-03-03 20:29:33 +01:00
|
|
|
audio_output_all_release();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2009-05-29 23:34:51 +02:00
|
|
|
|
2013-09-26 22:45:49 +02:00
|
|
|
assert(buffer.IsEmptyUnsafe());
|
2009-05-29 23:34:51 +02:00
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
2009-10-23 10:55:52 +02:00
|
|
|
case PLAYER_COMMAND_UPDATE_AUDIO:
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2009-10-23 10:55:52 +02:00
|
|
|
audio_output_all_enable_disable();
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Lock();
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2009-10-23 10:55:52 +02:00
|
|
|
break;
|
|
|
|
|
2008-08-26 08:27:16 +02:00
|
|
|
case PLAYER_COMMAND_EXIT:
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Unlock();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2013-09-26 22:48:55 +02:00
|
|
|
dc.Quit();
|
|
|
|
|
2009-02-10 18:51:51 +01:00
|
|
|
audio_output_all_close();
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished(pc);
|
2013-09-27 00:03:22 +02:00
|
|
|
return nullptr;
|
2008-08-26 08:27:16 +02:00
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
case PLAYER_COMMAND_CANCEL:
|
2013-09-27 00:03:22 +02:00
|
|
|
if (pc.next_song != nullptr) {
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.next_song->Free();
|
2013-09-27 00:03:22 +02:00
|
|
|
pc.next_song = nullptr;
|
2012-08-09 22:19:39 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
2009-10-08 22:09:25 +02:00
|
|
|
case PLAYER_COMMAND_REFRESH:
|
|
|
|
/* no-op when not playing */
|
2009-11-03 21:08:48 +01:00
|
|
|
player_command_finished_locked(pc);
|
2009-10-08 22:09:25 +02:00
|
|
|
break;
|
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
case PLAYER_COMMAND_NONE:
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.Wait();
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
void
|
2013-09-26 22:53:40 +02:00
|
|
|
player_create(player_control &pc)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(pc.thread == nullptr);
|
2009-01-25 13:44:33 +01:00
|
|
|
|
2013-04-17 01:49:43 +02:00
|
|
|
#if GLIB_CHECK_VERSION(2,32,0)
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.thread = g_thread_new("player", player_task, &pc);
|
2013-04-17 01:49:43 +02:00
|
|
|
#else
|
2013-09-27 00:03:22 +02:00
|
|
|
GError *e = nullptr;
|
2013-09-26 22:53:40 +02:00
|
|
|
pc.thread = g_thread_create(player_task, &pc, true, &e);
|
2013-09-27 00:03:22 +02:00
|
|
|
if (pc.thread == nullptr)
|
2013-08-07 09:35:30 +02:00
|
|
|
FatalError("Failed to spawn player task", e);
|
2013-04-17 01:49:43 +02:00
|
|
|
#endif
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|