2009-03-11 09:35:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 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"
|
2015-08-15 15:55:46 +02:00
|
|
|
#include "Thread.hxx"
|
|
|
|
#include "Listener.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "decoder/DecoderThread.hxx"
|
|
|
|
#include "decoder/DecoderControl.hxx"
|
2013-01-04 10:16:16 +01:00
|
|
|
#include "MusicPipe.hxx"
|
|
|
|
#include "MusicBuffer.hxx"
|
|
|
|
#include "MusicChunk.hxx"
|
2016-07-05 17:55:59 +02:00
|
|
|
#include "pcm/Silence.hxx"
|
2014-01-07 21:39:47 +01:00
|
|
|
#include "DetachedSong.hxx"
|
2013-01-04 10:31:59 +01:00
|
|
|
#include "CrossFade.hxx"
|
2015-08-15 15:55:46 +02:00
|
|
|
#include "Control.hxx"
|
2014-01-27 08:20:25 +01:00
|
|
|
#include "output/MultipleOutputs.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/Tag.hxx"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2017-10-25 20:26:09 +02:00
|
|
|
#include "system/PeriodClock.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
2014-01-23 10:07:14 +01:00
|
|
|
#include "thread/Name.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2017-12-19 10:56:23 +01:00
|
|
|
#include <exception>
|
2016-11-09 12:31:23 +01:00
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain player_domain("player");
|
2008-12-29 17:29:14 +01:00
|
|
|
|
2013-09-27 22:22:11 +02:00
|
|
|
class Player {
|
2013-10-28 10:12:21 +01:00
|
|
|
PlayerControl &pc;
|
2009-11-03 21:08:48 +01:00
|
|
|
|
2013-10-28 10:09:21 +01:00
|
|
|
DecoderControl &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
|
|
|
|
2017-12-20 18:43:56 +01:00
|
|
|
/**
|
|
|
|
* the song currently being played
|
|
|
|
*/
|
|
|
|
std::unique_ptr<DetachedSong> song;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The tag of the "next" song during cross-fade. It is
|
|
|
|
* postponed, and sent to the output thread when the new song
|
|
|
|
* really begins.
|
|
|
|
*/
|
|
|
|
std::unique_ptr<Tag> cross_fade_tag;
|
|
|
|
|
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
|
|
|
*/
|
2017-12-20 13:35:07 +01:00
|
|
|
bool buffering = true;
|
2008-10-12 00:02:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* true if the decoder is starting and did not provide data
|
|
|
|
* yet
|
|
|
|
*/
|
2017-12-20 13:35:07 +01:00
|
|
|
bool decoder_starting = false;
|
2008-10-12 00:02:23 +02:00
|
|
|
|
2013-11-06 23:30:29 +01:00
|
|
|
/**
|
|
|
|
* Did we wake up the DecoderThread recently? This avoids
|
|
|
|
* duplicate wakeup calls.
|
|
|
|
*/
|
2017-12-20 13:35:07 +01:00
|
|
|
bool decoder_woken = false;
|
2013-11-06 23:30:29 +01:00
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
/**
|
|
|
|
* is the player paused?
|
|
|
|
*/
|
2017-12-20 13:35:07 +01:00
|
|
|
bool paused = false;
|
2008-10-12 00:02:23 +02:00
|
|
|
|
2008-10-12 00:07:54 +02:00
|
|
|
/**
|
|
|
|
* is there a new song in pc.next_song?
|
|
|
|
*/
|
2017-12-20 13:35:07 +01:00
|
|
|
bool queued = true;
|
2008-10-12 00:07:54 +02:00
|
|
|
|
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.
|
|
|
|
*/
|
2017-12-20 13:35:07 +01:00
|
|
|
bool output_open = false;
|
2011-10-06 20:30:46 +02:00
|
|
|
|
2008-10-12 00:02:23 +02:00
|
|
|
/**
|
2015-10-27 21:12:20 +01:00
|
|
|
* Is cross-fading to the next song enabled?
|
2008-10-12 00:02:23 +02:00
|
|
|
*/
|
2015-10-27 21:05:10 +01:00
|
|
|
enum class CrossFadeState : uint8_t {
|
2015-10-27 21:12:20 +01:00
|
|
|
/**
|
|
|
|
* The initial state: we don't know yet if we will
|
|
|
|
* cross-fade; it will be determined soon.
|
|
|
|
*/
|
2015-10-27 21:05:10 +01:00
|
|
|
UNKNOWN,
|
2015-10-27 21:12:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cross-fading is disabled for the transition to the
|
|
|
|
* next song.
|
|
|
|
*/
|
2015-10-27 21:05:10 +01:00
|
|
|
DISABLED,
|
2015-10-27 21:12:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cross-fading is enabled (but may not yet be in
|
|
|
|
* progress), will start near the end of the current
|
|
|
|
* song.
|
|
|
|
*/
|
2015-10-27 21:05:10 +01:00
|
|
|
ENABLED,
|
2009-03-07 23:11:43 +01:00
|
|
|
|
2015-10-27 21:02:53 +01:00
|
|
|
/**
|
|
|
|
* Currently cross-fading to the next song.
|
|
|
|
*/
|
|
|
|
ACTIVE,
|
2017-12-20 13:35:07 +01:00
|
|
|
} xfade_state = CrossFadeState::UNKNOWN;
|
2009-03-09 19:14:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of chunks used for crossfading.
|
|
|
|
*/
|
2017-12-20 13:35:07 +01:00
|
|
|
unsigned cross_fade_chunks = 0;
|
2009-03-09 19:14:06 +01:00
|
|
|
|
2009-03-07 23:11:43 +01:00
|
|
|
/**
|
|
|
|
* The current audio format for the audio outputs.
|
|
|
|
*/
|
2017-08-23 17:43:49 +02:00
|
|
|
AudioFormat play_audio_format = AudioFormat::Undefined();
|
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
|
2014-01-27 08:20:25 +01:00
|
|
|
* MultipleOutputs::GetElapsedTime() didn't return a usable
|
2009-10-30 16:28:15 +01:00
|
|
|
* 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
|
|
|
*/
|
2017-12-20 13:35:07 +01:00
|
|
|
SongTime elapsed_time = SongTime::zero();
|
2013-01-02 20:36:28 +01:00
|
|
|
|
2017-10-25 20:26:09 +02:00
|
|
|
PeriodClock throttle_silence_log;
|
|
|
|
|
2013-09-27 22:22:11 +02:00
|
|
|
public:
|
2013-10-28 10:12:21 +01:00
|
|
|
Player(PlayerControl &_pc, DecoderControl &_dc,
|
2017-12-20 13:32:38 +01:00
|
|
|
MusicBuffer &_buffer) noexcept
|
2017-12-20 13:35:07 +01:00
|
|
|
:pc(_pc), dc(_dc), buffer(_buffer) {}
|
2013-09-26 23:57:31 +02:00
|
|
|
|
2013-09-27 22:22:11 +02:00
|
|
|
private:
|
2015-10-27 22:05:41 +01:00
|
|
|
/**
|
|
|
|
* Reset cross-fading to the initial state. A check to
|
|
|
|
* re-enable it at an appropriate time will be scheduled.
|
|
|
|
*/
|
2017-12-20 13:32:38 +01:00
|
|
|
void ResetCrossFade() noexcept {
|
2015-10-27 22:05:41 +01:00
|
|
|
xfade_state = CrossFadeState::UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2017-12-20 13:32:38 +01:00
|
|
|
void ClearAndDeletePipe() noexcept {
|
2013-09-26 23:57:31 +02:00
|
|
|
pipe->Clear(buffer);
|
|
|
|
delete pipe;
|
|
|
|
}
|
|
|
|
|
2017-12-20 13:32:38 +01:00
|
|
|
void ClearAndReplacePipe(MusicPipe *_pipe) noexcept {
|
2015-10-27 22:11:56 +01:00
|
|
|
ResetCrossFade();
|
2013-09-26 23:57:31 +02:00
|
|
|
ClearAndDeletePipe();
|
|
|
|
pipe = _pipe;
|
|
|
|
}
|
|
|
|
|
2017-12-20 13:32:38 +01:00
|
|
|
void ReplacePipe(MusicPipe *_pipe) noexcept {
|
2015-10-27 22:11:56 +01:00
|
|
|
ResetCrossFade();
|
2013-09-26 23:57:31 +02:00
|
|
|
delete pipe;
|
|
|
|
pipe = _pipe;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the decoder.
|
|
|
|
*
|
2017-12-22 01:01:04 +01:00
|
|
|
* Caller must lock the mutex.
|
2013-09-26 23:57:31 +02:00
|
|
|
*/
|
2017-12-20 13:32:38 +01:00
|
|
|
void StartDecoder(MusicPipe &pipe) noexcept;
|
2013-09-26 23:57:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The decoder has acknowledged the "START" command (see
|
2015-10-28 18:20:48 +01:00
|
|
|
* ActivateDecoder()). This function checks if the decoder
|
2017-12-20 13:27:13 +01:00
|
|
|
* initialization has completed yet. If not, it will wait
|
|
|
|
* some more.
|
2013-09-26 23:57:31 +02:00
|
|
|
*
|
2016-12-14 20:13:18 +01:00
|
|
|
* Caller must lock the mutex.
|
2017-12-20 13:27:13 +01:00
|
|
|
*
|
|
|
|
* @return false if the decoder has failed, true on success
|
|
|
|
* (though the decoder startup may or may not yet be finished)
|
2013-09-26 23:57:31 +02:00
|
|
|
*/
|
2017-12-20 13:32:38 +01:00
|
|
|
bool CheckDecoderStartup() noexcept;
|
2013-09-26 23:57:31 +02:00
|
|
|
|
2015-11-05 00:34:42 +01:00
|
|
|
/**
|
|
|
|
* Call CheckDecoderStartup() repeatedly until the decoder has
|
|
|
|
* finished startup. Returns false on decoder error (and
|
|
|
|
* finishes the #PlayerCommand).
|
|
|
|
*
|
|
|
|
* This method does not check for commands. It is only
|
|
|
|
* allowed to be used while a command is being handled.
|
2017-12-20 13:27:13 +01:00
|
|
|
*
|
2017-12-22 00:41:05 +01:00
|
|
|
* Caller must lock the mutex.
|
|
|
|
*
|
2017-12-20 13:27:13 +01:00
|
|
|
* @return false if the decoder has failed
|
2015-11-05 00:34:42 +01:00
|
|
|
*/
|
2017-12-20 13:32:38 +01:00
|
|
|
bool WaitDecoderStartup() noexcept {
|
2015-11-05 00:34:42 +01:00
|
|
|
while (decoder_starting) {
|
|
|
|
if (!CheckDecoderStartup()) {
|
2016-04-21 14:02:34 +02:00
|
|
|
/* if decoder startup fails, make sure
|
|
|
|
the previous song is not being
|
|
|
|
played anymore */
|
2016-12-14 20:13:18 +01:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
|
|
|
pc.outputs.Cancel();
|
|
|
|
}
|
2016-04-21 14:02:34 +02:00
|
|
|
|
2016-12-14 20:13:18 +01:00
|
|
|
pc.CommandFinished();
|
2015-11-05 00:34:42 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
/**
|
|
|
|
* Stop the decoder and clears (and frees) its music pipe.
|
|
|
|
*
|
2017-12-22 00:46:03 +01:00
|
|
|
* Caller must lock the mutex.
|
2013-09-26 23:57:31 +02:00
|
|
|
*/
|
2017-12-20 13:32:38 +01:00
|
|
|
void StopDecoder() noexcept;
|
2013-09-26 23:57:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2017-06-03 21:33:44 +02:00
|
|
|
bool IsDecoderAtCurrentSong() const noexcept {
|
2013-09-26 23:57:31 +02:00
|
|
|
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
|
2017-06-03 21:33:44 +02:00
|
|
|
bool IsDecoderAtNextSong() const noexcept {
|
2013-09-26 23:57:31 +02:00
|
|
|
return dc.pipe != nullptr && !IsDecoderAtCurrentSong();
|
|
|
|
}
|
|
|
|
|
2017-12-21 23:59:42 +01:00
|
|
|
/**
|
|
|
|
* Invoke DecoderControl::Seek() and update our state or
|
|
|
|
* handle errors.
|
|
|
|
*
|
|
|
|
* Caller must lock the mutex.
|
|
|
|
*
|
|
|
|
* @return false if the decoder has failed
|
|
|
|
*/
|
|
|
|
bool SeekDecoder(SongTime seek_time) noexcept;
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
/**
|
2013-09-27 22:07:20 +02:00
|
|
|
* This is the handler for the #PlayerCommand::SEEK command.
|
2013-09-26 23:57:31 +02:00
|
|
|
*
|
2017-12-22 01:05:48 +01:00
|
|
|
* Caller must lock the mutex.
|
2017-12-20 13:27:13 +01:00
|
|
|
*
|
|
|
|
* @return false if the decoder has failed
|
2013-09-26 23:57:31 +02:00
|
|
|
*/
|
2017-12-20 13:32:38 +01:00
|
|
|
bool SeekDecoder() noexcept;
|
2013-09-26 23:57:31 +02:00
|
|
|
|
2015-10-27 23:28:13 +01:00
|
|
|
/**
|
|
|
|
* Check if the decoder has reported an error, and forward it
|
|
|
|
* to PlayerControl::SetError().
|
|
|
|
*
|
|
|
|
* @return false if an error has occurred
|
|
|
|
*/
|
2017-12-20 13:32:38 +01:00
|
|
|
bool ForwardDecoderError() noexcept;
|
2015-10-27 23:28:13 +01:00
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
/**
|
2015-10-28 18:20:48 +01:00
|
|
|
* After the decoder has been started asynchronously, activate
|
|
|
|
* it for playback. That is, make the currently decoded song
|
|
|
|
* active (assign it to #song), clear PlayerControl::next_song
|
|
|
|
* and #queued, initialize #elapsed_time, and set
|
|
|
|
* #decoder_starting.
|
|
|
|
*
|
|
|
|
* When returning, the decoder may not have completed startup
|
|
|
|
* yet, therefore we don't know the audio format yet. To
|
|
|
|
* finish decoder startup, call CheckDecoderStartup().
|
2013-09-26 23:57:31 +02:00
|
|
|
*
|
2017-12-22 00:57:40 +01:00
|
|
|
* Caller must lock the mutex.
|
2013-09-26 23:57:31 +02:00
|
|
|
*/
|
2017-12-20 13:32:38 +01:00
|
|
|
void ActivateDecoder() noexcept;
|
2013-09-26 23:57:31 +02:00
|
|
|
|
|
|
|
/**
|
2014-01-27 08:20:25 +01:00
|
|
|
* Wrapper for MultipleOutputs::Open(). Upon failure, it
|
|
|
|
* pauses the player.
|
2013-09-26 23:57:31 +02:00
|
|
|
*
|
2016-12-14 20:23:23 +01:00
|
|
|
* Caller must lock the mutex.
|
|
|
|
*
|
2013-09-26 23:57:31 +02:00
|
|
|
* @return true on success
|
|
|
|
*/
|
2017-12-20 13:32:38 +01:00
|
|
|
bool OpenOutput() noexcept;
|
2013-09-26 23:57:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
2017-12-20 13:32:38 +01:00
|
|
|
bool PlayNextChunk() noexcept;
|
2013-09-26 23:57:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2017-12-20 13:27:13 +01:00
|
|
|
*
|
|
|
|
* @return false on error
|
2013-09-26 23:57:31 +02:00
|
|
|
*/
|
2017-12-20 13:32:38 +01:00
|
|
|
bool SendSilence() noexcept;
|
2013-09-26 23:57:31 +02:00
|
|
|
|
2017-12-22 01:16:38 +01:00
|
|
|
unsigned UnlockCheckOutputs() noexcept {
|
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
|
|
|
return pc.outputs.Check();
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
/**
|
|
|
|
* Player lock must be held before calling.
|
2017-12-28 08:30:01 +01:00
|
|
|
*
|
|
|
|
* @return false to stop playback
|
2013-09-26 23:57:31 +02:00
|
|
|
*/
|
2017-12-28 08:30:01 +01:00
|
|
|
bool ProcessCommand() noexcept;
|
2013-09-26 23:57:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-12-20 13:32:38 +01:00
|
|
|
void SongBorder() noexcept;
|
2013-09-26 23:57:31 +02:00
|
|
|
|
2013-09-27 22:22:11 +02:00
|
|
|
public:
|
2013-09-26 23:57:31 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-12-20 13:32:38 +01:00
|
|
|
void Run() noexcept;
|
2008-10-12 00:02:23 +02:00
|
|
|
};
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
void
|
2017-12-20 13:32:38 +01:00
|
|
|
Player::StartDecoder(MusicPipe &_pipe) noexcept
|
2009-11-03 21:01:56 +01:00
|
|
|
{
|
2013-09-27 22:07:20 +02:00
|
|
|
assert(queued || pc.command == PlayerCommand::SEEK);
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(pc.next_song != nullptr);
|
2009-11-03 21:01:56 +01:00
|
|
|
|
2017-12-22 00:59:50 +01:00
|
|
|
/* copy ReplayGain parameters to the decoder */
|
|
|
|
dc.replay_gain_mode = pc.replay_gain_mode;
|
2016-11-25 12:51:55 +01:00
|
|
|
|
2015-11-05 00:29:03 +01:00
|
|
|
SongTime start_time = pc.next_song->GetStartTime() + pc.seek_time;
|
2011-10-05 22:13:13 +02:00
|
|
|
|
2017-11-26 12:11:07 +01:00
|
|
|
dc.Start(std::make_unique<DetachedSong>(*pc.next_song),
|
2014-08-28 13:04:45 +02:00
|
|
|
start_time, pc.next_song->GetEndTime(),
|
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
|
2017-12-20 13:32:38 +01:00
|
|
|
Player::StopDecoder() noexcept
|
2010-11-05 19:08:54 +01:00
|
|
|
{
|
2017-12-20 20:27:19 +01:00
|
|
|
const PlayerControl::ScopeOccupied occupied(pc);
|
|
|
|
|
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;
|
2015-10-27 22:11:56 +01:00
|
|
|
|
|
|
|
/* just in case we've been cross-fading: cancel it
|
|
|
|
now, because we just deleted the new song's decoder
|
|
|
|
pipe */
|
|
|
|
ResetCrossFade();
|
2009-03-06 00:42:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-27 23:28:13 +01:00
|
|
|
bool
|
2017-12-20 13:32:38 +01:00
|
|
|
Player::ForwardDecoderError() noexcept
|
2015-10-27 23:28:13 +01:00
|
|
|
{
|
2016-09-08 17:14:11 +02:00
|
|
|
try {
|
|
|
|
dc.CheckRethrowError();
|
|
|
|
} catch (...) {
|
|
|
|
pc.SetError(PlayerError::DECODER, std::current_exception());
|
2015-10-27 23:28:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-28 18:24:42 +01:00
|
|
|
void
|
2017-12-20 13:32:38 +01:00
|
|
|
Player::ActivateDecoder() noexcept
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2013-09-27 22:07:20 +02:00
|
|
|
assert(queued || pc.command == PlayerCommand::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
|
|
|
|
2017-12-22 00:57:40 +01:00
|
|
|
pc.ClearTaggedSong();
|
2013-10-21 23:22:16 +02:00
|
|
|
|
2017-12-22 00:57:40 +01:00
|
|
|
song = std::exchange(pc.next_song, nullptr);
|
2015-10-27 23:25:50 +01:00
|
|
|
|
2017-12-22 00:57:40 +01:00
|
|
|
elapsed_time = pc.seek_time;
|
2009-11-03 21:02:54 +01:00
|
|
|
|
2017-12-22 00:57:40 +01:00
|
|
|
/* set the "starting" flag, which will be cleared by
|
2017-12-27 14:26:14 +01:00
|
|
|
CheckDecoderStartup() */
|
2017-12-22 00:57:40 +01:00
|
|
|
decoder_starting = true;
|
2009-11-03 21:02:54 +01:00
|
|
|
|
2017-12-22 00:57:40 +01:00
|
|
|
/* update PlayerControl's song information */
|
|
|
|
pc.total_time = song->GetDuration();
|
|
|
|
pc.bit_rate = 0;
|
|
|
|
pc.audio_format.Clear();
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2017-12-22 00:57:40 +01:00
|
|
|
{
|
|
|
|
/* call syncPlaylistWithQueue() in the main thread */
|
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
|
|
|
pc.listener.OnPlayerSync();
|
2016-12-14 12:54:37 +01:00
|
|
|
}
|
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.
|
|
|
|
*/
|
2014-08-29 12:43:47 +02:00
|
|
|
static SignedSongTime
|
2017-12-20 13:32:38 +01:00
|
|
|
real_song_duration(const DetachedSong &song,
|
|
|
|
SignedSongTime decoder_duration) noexcept
|
2009-12-25 23:12:10 +01:00
|
|
|
{
|
2014-08-29 12:43:47 +02:00
|
|
|
if (decoder_duration.IsNegative())
|
2009-12-25 23:12:10 +01:00
|
|
|
/* the decoder plugin didn't provide information; fall
|
2013-07-28 13:25:12 +02:00
|
|
|
back to Song::GetDuration() */
|
2014-08-29 12:43:47 +02:00
|
|
|
return song.GetDuration();
|
2009-12-25 23:12:10 +01:00
|
|
|
|
2014-08-28 06:54:19 +02:00
|
|
|
const SongTime start_time = song.GetStartTime();
|
|
|
|
const SongTime end_time = song.GetEndTime();
|
2014-01-07 21:39:47 +01:00
|
|
|
|
2014-08-29 12:43:47 +02:00
|
|
|
if (end_time.IsPositive() && end_time < SongTime(decoder_duration))
|
|
|
|
return SignedSongTime(end_time - start_time);
|
2009-12-25 23:12:10 +01:00
|
|
|
|
2014-08-29 12:43:47 +02:00
|
|
|
return SignedSongTime(SongTime(decoder_duration) - start_time);
|
2009-12-25 23:12:10 +01:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
bool
|
2017-12-20 13:32:38 +01:00
|
|
|
Player::OpenOutput() noexcept
|
2011-10-06 20:55:52 +02:00
|
|
|
{
|
2013-09-26 23:57:31 +02:00
|
|
|
assert(play_audio_format.IsDefined());
|
2013-09-27 22:07:20 +02:00
|
|
|
assert(pc.state == PlayerState::PLAY ||
|
|
|
|
pc.state == PlayerState::PAUSE);
|
2011-10-06 20:55:52 +02:00
|
|
|
|
2016-11-09 12:31:23 +01:00
|
|
|
try {
|
2016-12-14 20:23:23 +01:00
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
2016-11-09 12:31:23 +01:00
|
|
|
pc.outputs.Open(play_audio_format, buffer);
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
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
|
|
|
|
2016-12-14 20:23:23 +01:00
|
|
|
pc.SetOutputError(std::current_exception());
|
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;
|
|
|
|
}
|
2016-11-09 12:31:23 +01:00
|
|
|
|
|
|
|
output_open = true;
|
|
|
|
paused = false;
|
|
|
|
|
2016-12-14 20:23:23 +01:00
|
|
|
pc.state = PlayerState::PLAY;
|
2016-11-09 12:31:23 +01:00
|
|
|
|
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
|
|
|
|
return true;
|
2011-10-06 20:55:52 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
bool
|
2017-12-20 13:32:38 +01:00
|
|
|
Player::CheckDecoderStartup() noexcept
|
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
|
|
|
|
2015-10-27 23:28:13 +01:00
|
|
|
if (!ForwardDecoderError()) {
|
2009-03-07 23:11:43 +01:00
|
|
|
/* the decoder failed */
|
|
|
|
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 23:57:31 +02:00
|
|
|
if (output_open &&
|
2016-12-14 20:13:18 +01:00
|
|
|
!pc.WaitOutputConsumed(1))
|
2009-03-09 19:25:26 +01:00
|
|
|
/* the output devices havn't finished playing
|
|
|
|
all chunks yet - wait for that */
|
|
|
|
return true;
|
|
|
|
|
2016-12-14 20:13:18 +01:00
|
|
|
pc.total_time = real_song_duration(*dc.song,
|
|
|
|
dc.total_time);
|
|
|
|
pc.audio_format = dc.in_audio_format;
|
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
|
|
|
|
2016-12-14 20:13:18 +01:00
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (!paused && !OpenOutput()) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(player_domain,
|
|
|
|
"problems opening audio device "
|
2014-01-07 21:39:47 +01:00
|
|
|
"while playing \"%s\"",
|
|
|
|
dc.song->GetURI());
|
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();
|
2009-03-07 23:11:43 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
bool
|
2017-12-20 13:32:38 +01:00
|
|
|
Player::SendSilence() noexcept
|
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
|
|
|
|
2014-08-12 15:56:41 +02:00
|
|
|
MusicChunk *chunk = buffer.Allocate();
|
2013-09-27 00:03:22 +02:00
|
|
|
if (chunk == nullptr) {
|
2016-07-05 17:44:45 +02:00
|
|
|
/* this is non-fatal, because this means that the
|
|
|
|
decoder has filled to buffer completely meanwhile;
|
|
|
|
by ignoring the error, we work around this race
|
|
|
|
condition */
|
|
|
|
LogDebug(player_domain, "Failed to allocate silence buffer");
|
|
|
|
return true;
|
2009-03-10 20:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
2017-12-03 10:47:56 +01:00
|
|
|
chunk->bit_rate = 0;
|
2014-08-29 13:15:33 +02:00
|
|
|
chunk->time = SignedSongTime::Negative(); /* undefined time stamp */
|
2009-03-10 20:43:19 +01:00
|
|
|
chunk->length = num_frames * frame_size;
|
2017-12-03 11:15:11 +01:00
|
|
|
chunk->replay_gain_serial = MusicChunk::IGNORE_REPLAY_GAIN;
|
2016-07-05 17:55:59 +02:00
|
|
|
PcmSilence({chunk->data, chunk->length}, play_audio_format.format);
|
2009-03-10 20:43:19 +01:00
|
|
|
|
2016-11-09 12:31:23 +01:00
|
|
|
try {
|
|
|
|
pc.outputs.Play(chunk);
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
2013-09-26 23:57:31 +02:00
|
|
|
buffer.Return(chunk);
|
2009-03-10 20:43:19 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-21 23:59:42 +01:00
|
|
|
bool
|
|
|
|
Player::SeekDecoder(SongTime seek_time) noexcept
|
|
|
|
{
|
|
|
|
assert(song);
|
|
|
|
assert(!decoder_starting);
|
|
|
|
|
|
|
|
if (!pc.total_time.IsNegative()) {
|
|
|
|
const SongTime total_time(pc.total_time);
|
|
|
|
if (seek_time > total_time)
|
|
|
|
seek_time = total_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const PlayerControl::ScopeOccupied occupied(pc);
|
|
|
|
|
|
|
|
dc.Seek(song->GetStartTime() + seek_time);
|
|
|
|
} catch (...) {
|
|
|
|
/* decoder failure */
|
|
|
|
pc.SetError(PlayerError::DECODER, std::current_exception());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed_time = seek_time;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
inline bool
|
2017-12-20 13:32:38 +01:00
|
|
|
Player::SeekDecoder() noexcept
|
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
|
|
|
|
2017-12-22 01:05:48 +01:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
|
|
|
pc.outputs.Cancel();
|
|
|
|
}
|
2017-12-22 01:04:44 +01:00
|
|
|
|
|
|
|
if (!dc.IsCurrentSong(*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 */
|
|
|
|
|
2017-12-22 01:04:44 +01: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);
|
2015-10-28 18:24:42 +01:00
|
|
|
ActivateDecoder();
|
2015-11-05 00:41:54 +01:00
|
|
|
|
2017-12-22 00:58:42 +01:00
|
|
|
if (!WaitDecoderStartup())
|
2015-11-05 00:41:54 +01:00
|
|
|
return false;
|
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
|
|
|
}
|
|
|
|
|
2017-11-26 11:46:14 +01:00
|
|
|
pc.next_song.reset();
|
2013-09-26 23:57:31 +02:00
|
|
|
queued = false;
|
2008-08-26 08:27:18 +02:00
|
|
|
|
2015-11-05 00:41:54 +01:00
|
|
|
/* wait for the decoder to complete initialization
|
|
|
|
(just in case that happens to be still in
|
|
|
|
progress) */
|
2009-03-25 19:48:14 +01:00
|
|
|
|
2017-12-22 00:42:10 +01:00
|
|
|
if (!WaitDecoderStartup())
|
2015-11-05 00:41:54 +01:00
|
|
|
return false;
|
2009-03-25 19:48:14 +01:00
|
|
|
|
2015-11-05 00:41:54 +01:00
|
|
|
/* send the SEEK command */
|
2009-03-11 09:35:16 +01:00
|
|
|
|
2017-12-27 14:28:56 +01:00
|
|
|
if (!SeekDecoder(pc.seek_time)) {
|
|
|
|
pc.CommandFinished();
|
2015-11-05 00:41:54 +01:00
|
|
|
return false;
|
2017-12-27 14:28:56 +01:00
|
|
|
}
|
2015-11-05 00:41:54 +01:00
|
|
|
}
|
2009-10-30 16:28:15 +01:00
|
|
|
|
2017-12-22 01:04:44 +01:00
|
|
|
pc.CommandFinished();
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2015-10-27 22:11:56 +01:00
|
|
|
assert(xfade_state == CrossFadeState::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
|
|
|
|
|
|
|
return true;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2017-12-28 08:30:01 +01:00
|
|
|
inline bool
|
2017-12-20 13:32:38 +01:00
|
|
|
Player::ProcessCommand() noexcept
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2013-09-26 22:53:40 +02:00
|
|
|
switch (pc.command) {
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::NONE:
|
2017-12-28 08:30:01 +01:00
|
|
|
break;
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::STOP:
|
|
|
|
case PlayerCommand::EXIT:
|
|
|
|
case PlayerCommand::CLOSE_AUDIO:
|
2017-12-28 08:30:01 +01:00
|
|
|
return false;
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::UPDATE_AUDIO:
|
2016-12-14 12:54:37 +01:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
|
|
|
pc.outputs.EnableDisable();
|
|
|
|
}
|
|
|
|
|
2013-09-27 07:58:48 +02:00
|
|
|
pc.CommandFinished();
|
2009-10-23 10:55:52 +02:00
|
|
|
break;
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::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;
|
2013-09-27 07:58:48 +02:00
|
|
|
pc.CommandFinished();
|
2015-06-21 15:38:48 +02:00
|
|
|
|
2017-12-22 01:01:04 +01:00
|
|
|
if (dc.IsIdle())
|
|
|
|
StartDecoder(*new MusicPipe());
|
2015-06-21 15:38:48 +02:00
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::PAUSE:
|
2013-09-26 23:57:31 +02:00
|
|
|
paused = !paused;
|
|
|
|
if (paused) {
|
2013-09-27 22:07:20 +02:00
|
|
|
pc.state = PlayerState::PAUSE;
|
2016-12-14 20:24:52 +01:00
|
|
|
|
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
|
|
|
pc.outputs.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-27 22:07:20 +02:00
|
|
|
pc.state = PlayerState::PLAY;
|
2008-08-26 08:27:09 +02:00
|
|
|
} else {
|
2016-12-14 20:23:23 +01:00
|
|
|
OpenOutput();
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
2009-03-10 21:00:52 +01:00
|
|
|
|
2013-09-27 07:58:48 +02:00
|
|
|
pc.CommandFinished();
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::SEEK:
|
2017-12-28 08:30:01 +01:00
|
|
|
return SeekDecoder();
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::CANCEL:
|
2017-12-28 08:30:01 +01: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 */
|
2017-12-28 08:30:01 +01:00
|
|
|
return false;
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2017-12-22 00:46:03 +01: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 23:57:31 +02:00
|
|
|
StopDecoder();
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2017-11-26 11:46:14 +01:00
|
|
|
pc.next_song.reset();
|
2013-09-26 23:57:31 +02:00
|
|
|
queued = false;
|
2013-09-27 07:58:48 +02:00
|
|
|
pc.CommandFinished();
|
2008-10-12 00:07:54 +02:00
|
|
|
break;
|
2009-10-08 22:09:25 +02:00
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::REFRESH:
|
2013-09-26 23:57:31 +02:00
|
|
|
if (output_open && !paused) {
|
2016-12-14 12:54:37 +01:00
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
2014-01-27 08:20:25 +01:00
|
|
|
pc.outputs.Check();
|
2009-10-31 17:02:12 +01:00
|
|
|
}
|
2009-10-08 22:09:25 +02:00
|
|
|
|
2014-08-29 13:13:08 +02:00
|
|
|
pc.elapsed_time = !pc.outputs.GetElapsedTime().IsNegative()
|
|
|
|
? SongTime(pc.outputs.GetElapsedTime())
|
2014-08-29 13:07:38 +02:00
|
|
|
: elapsed_time;
|
2009-10-30 16:28:15 +01:00
|
|
|
|
2013-09-27 07:58:48 +02:00
|
|
|
pc.CommandFinished();
|
2009-10-08 22:09:25 +02:00
|
|
|
break;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
2017-12-28 08:30:01 +01:00
|
|
|
|
|
|
|
return true;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2009-07-23 12:26:26 +02:00
|
|
|
static void
|
2017-12-20 13:32:38 +01:00
|
|
|
update_song_tag(PlayerControl &pc, DetachedSong &song,
|
|
|
|
const Tag &new_tag) noexcept
|
2009-07-23 12:26:26 +02:00
|
|
|
{
|
2014-01-08 00:36:59 +01: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;
|
|
|
|
|
2014-01-07 21:39:47 +01:00
|
|
|
song.SetTag(new_tag);
|
2009-07-23 12:26:26 +02:00
|
|
|
|
2014-01-08 00:36:59 +01:00
|
|
|
pc.LockSetTaggedSong(song);
|
2013-10-21 23:22:16 +02:00
|
|
|
|
2009-07-23 12:26:26 +02:00
|
|
|
/* the main thread will update the playlist version when he
|
|
|
|
receives this event */
|
2014-02-21 08:55:52 +01:00
|
|
|
pc.listener.OnPlayerTagModified();
|
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
|
|
|
/**
|
2014-08-12 15:56:41 +02:00
|
|
|
* Plays a #MusicChunk object (after applying software volume). If
|
2009-03-11 09:35:16 +01:00
|
|
|
* 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
|
|
|
*/
|
2016-11-09 12:31:23 +01:00
|
|
|
static void
|
2013-10-28 10:12:21 +01:00
|
|
|
play_chunk(PlayerControl &pc,
|
2014-08-12 15:56:41 +02:00
|
|
|
DetachedSong &song, MusicChunk *chunk,
|
2013-09-26 22:45:49 +02:00
|
|
|
MusicBuffer &buffer,
|
2017-12-20 13:32:38 +01:00
|
|
|
const AudioFormat format) noexcept
|
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-10-21 23:22:16 +02:00
|
|
|
update_song_tag(pc, song, *chunk->tag);
|
2008-11-02 17:13:26 +01:00
|
|
|
|
2013-10-11 05:12:27 +02:00
|
|
|
if (chunk->IsEmpty()) {
|
2013-09-26 22:45:49 +02:00
|
|
|
buffer.Return(chunk);
|
2016-11-09 12:31:23 +01:00
|
|
|
return;
|
2009-06-02 08:37:30 +02:00
|
|
|
}
|
2009-03-17 20:28:37 +01:00
|
|
|
|
2016-12-14 12:54:37 +01:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> lock(pc.mutex);
|
2016-12-14 12:54:37 +01:00
|
|
|
pc.bit_rate = chunk->bit_rate;
|
|
|
|
}
|
2009-03-17 20:28:37 +01:00
|
|
|
|
2009-03-11 09:35:16 +01:00
|
|
|
/* send the chunk to the audio outputs */
|
|
|
|
|
2016-11-09 12:31:23 +01:00
|
|
|
pc.outputs.Play(chunk);
|
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();
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
inline bool
|
2017-12-20 13:32:38 +01:00
|
|
|
Player::PlayNextChunk() noexcept
|
2009-03-09 19:14:06 +01:00
|
|
|
{
|
2016-12-14 12:54:19 +01:00
|
|
|
if (!pc.LockWaitOutputConsumed(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;
|
|
|
|
|
2015-10-27 21:02:53 +01:00
|
|
|
/* activate cross-fading? */
|
|
|
|
if (xfade_state == CrossFadeState::ENABLED &&
|
|
|
|
IsDecoderAtNextSong() &&
|
|
|
|
pipe->GetSize() <= cross_fade_chunks) {
|
|
|
|
/* beginning of the cross fade - adjust
|
|
|
|
cross_fade_chunks which might be bigger than the
|
|
|
|
remaining number of chunks in the old song */
|
|
|
|
cross_fade_chunks = pipe->GetSize();
|
|
|
|
xfade_state = CrossFadeState::ACTIVE;
|
|
|
|
}
|
|
|
|
|
2014-08-12 15:56:41 +02:00
|
|
|
MusicChunk *chunk = nullptr;
|
2015-10-27 21:02:53 +01:00
|
|
|
if (xfade_state == CrossFadeState::ACTIVE) {
|
2009-03-09 19:14:06 +01:00
|
|
|
/* perform cross fade */
|
|
|
|
|
2015-10-27 21:02:53 +01:00
|
|
|
assert(IsDecoderAtNextSong());
|
|
|
|
|
|
|
|
unsigned cross_fade_position = pipe->GetSize();
|
|
|
|
assert(cross_fade_position <= cross_fade_chunks);
|
2009-03-09 19:14:06 +01:00
|
|
|
|
2015-10-27 21:02:53 +01:00
|
|
|
MusicChunk *other_chunk = dc.pipe->Shift();
|
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 */
|
2017-12-20 14:43:29 +01:00
|
|
|
cross_fade_tag = Tag::Merge(std::move(cross_fade_tag),
|
|
|
|
std::move(other_chunk->tag));
|
2010-03-17 21:57:35 +01:00
|
|
|
|
2013-10-30 16:50:34 +01:00
|
|
|
if (pc.cross_fade.mixramp_delay <= 0) {
|
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 {
|
2013-10-30 17:23:49 +01:00
|
|
|
chunk->mix_ratio = -1;
|
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()) {
|
2014-08-12 15:56:41 +02:00
|
|
|
/* the "other" chunk was a MusicChunk
|
2011-01-07 23:45:51 +01:00
|
|
|
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
|
|
|
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> lock(pc.mutex);
|
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-27 22:19:26 +02:00
|
|
|
xfade_state = CrossFadeState::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();
|
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 */
|
|
|
|
|
2015-10-27 21:02:53 +01:00
|
|
|
if (xfade_state != CrossFadeState::ACTIVE && cross_fade_tag != nullptr) {
|
2017-12-20 14:43:29 +01:00
|
|
|
chunk->tag = Tag::Merge(std::move(chunk->tag),
|
|
|
|
std::move(cross_fade_tag));
|
2013-09-26 23:57:31 +02:00
|
|
|
cross_fade_tag = nullptr;
|
2010-03-17 21:57:35 +01:00
|
|
|
}
|
|
|
|
|
2009-03-09 19:14:06 +01:00
|
|
|
/* play the current chunk */
|
|
|
|
|
2016-11-09 12:31:23 +01:00
|
|
|
try {
|
|
|
|
play_chunk(pc, *song, chunk, buffer, play_audio_format);
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
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
|
|
|
|
|
|
|
/* pause: the user may resume playback as soon as an
|
|
|
|
audio output becomes available */
|
2013-09-26 23:57:31 +02:00
|
|
|
paused = true;
|
2009-04-25 11:55:36 +02:00
|
|
|
|
2016-12-14 13:01:17 +01:00
|
|
|
pc.LockSetOutputError(std::current_exception());
|
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
|
|
|
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> lock(pc.mutex);
|
2016-12-14 12:54:37 +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
|
|
|
if (!dc.IsIdle() &&
|
|
|
|
dc.pipe->GetSize() <= (pc.buffered_before_play +
|
2013-11-06 23:30:29 +01:00
|
|
|
buffer.GetSize() * 3) / 4) {
|
|
|
|
if (!decoder_woken) {
|
|
|
|
decoder_woken = true;
|
|
|
|
dc.Signal();
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
decoder_woken = false;
|
2009-03-09 19:14:06 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-28 18:24:42 +01:00
|
|
|
inline void
|
2017-12-20 13:32:38 +01:00
|
|
|
Player::SongBorder() noexcept
|
2009-03-09 19:15:14 +01:00
|
|
|
{
|
2014-01-07 21:39:47 +01:00
|
|
|
FormatDefault(player_domain, "played \"%s\"", song->GetURI());
|
2009-07-06 22:29:58 +02:00
|
|
|
|
2017-10-25 20:26:09 +02:00
|
|
|
throttle_silence_log.Reset();
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
ReplacePipe(dc.pipe);
|
2009-03-09 19:15:14 +01:00
|
|
|
|
2014-01-27 08:20:25 +01:00
|
|
|
pc.outputs.SongBorder();
|
2010-01-02 21:16:51 +01:00
|
|
|
|
2017-12-22 00:57:40 +01:00
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(pc.mutex);
|
|
|
|
ActivateDecoder();
|
|
|
|
}
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2016-12-14 13:09:48 +01:00
|
|
|
const bool border_pause = pc.LockApplyBorderPause();
|
2013-02-04 10:15:34 +01:00
|
|
|
if (border_pause) {
|
2013-09-26 23:57:31 +02:00
|
|
|
paused = true;
|
2013-02-04 10:15:34 +01:00
|
|
|
idle_add(IDLE_PLAYER);
|
2016-12-14 13:09:48 +01:00
|
|
|
}
|
2009-03-09 19:15:14 +01:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
inline void
|
2017-12-20 13:32:38 +01:00
|
|
|
Player::Run() noexcept
|
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
|
|
|
|
2017-12-22 01:16:38 +01:00
|
|
|
const std::lock_guard<Mutex> lock(pc.mutex);
|
2017-12-22 01:01:04 +01:00
|
|
|
|
|
|
|
StartDecoder(*pipe);
|
2015-10-28 18:24:42 +01:00
|
|
|
ActivateDecoder();
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
pc.state = PlayerState::PLAY;
|
2011-10-05 22:13:13 +02:00
|
|
|
|
2013-09-27 07:58:48 +02:00
|
|
|
pc.CommandFinished();
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2009-01-21 16:31:15 +01:00
|
|
|
while (true) {
|
2017-12-28 08:30:01 +01:00
|
|
|
if (!ProcessCommand()) {
|
2017-12-22 01:16:38 +01:00
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
2014-01-27 08:20:25 +01:00
|
|
|
pc.outputs.Cancel();
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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 &&
|
2017-12-22 01:13:01 +01:00
|
|
|
!dc.IsIdle()) {
|
2008-08-26 08:27:09 +02:00
|
|
|
/* not enough decoded buffer space yet */
|
2009-03-10 21:19:51 +01:00
|
|
|
|
2017-12-22 01:13:01 +01:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
|
|
|
if (!paused && output_open &&
|
|
|
|
pc.outputs.Check() < 4 &&
|
|
|
|
!SendSilence())
|
|
|
|
break;
|
|
|
|
}
|
2009-03-10 21:19:51 +01:00
|
|
|
|
2009-10-31 17:02:12 +01:00
|
|
|
/* XXX race condition: check decoder again */
|
2013-09-26 22:53:40 +02:00
|
|
|
dc.WaitForDecoder();
|
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
|
|
|
|
2017-12-22 01:16:38 +01:00
|
|
|
if (!CheckDecoderStartup())
|
2009-03-07 23:11:43 +01:00
|
|
|
break;
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-03-07 23:11:43 +01:00
|
|
|
continue;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2017-12-22 01:13:01 +01:00
|
|
|
if (dc.IsIdle() && 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() &&
|
2013-09-27 22:19:26 +02:00
|
|
|
xfade_state == CrossFadeState::UNKNOWN &&
|
2017-12-22 01:13:01 +01:00
|
|
|
!dc.IsStarting()) {
|
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 =
|
2014-08-29 23:41:34 +02:00
|
|
|
pc.cross_fade.Calculate(dc.total_time,
|
2013-10-29 00:14:27 +01:00
|
|
|
dc.replay_gain_db,
|
|
|
|
dc.replay_gain_prev_db,
|
|
|
|
dc.GetMixRampStart(),
|
|
|
|
dc.GetMixRampPreviousEnd(),
|
|
|
|
dc.out_audio_format,
|
|
|
|
play_audio_format,
|
|
|
|
buffer.GetSize() -
|
|
|
|
pc.buffered_before_play);
|
2015-10-27 21:02:53 +01:00
|
|
|
if (cross_fade_chunks > 0)
|
2013-09-27 22:19:26 +02:00
|
|
|
xfade_state = CrossFadeState::ENABLED;
|
2015-10-27 21:02:53 +01:00
|
|
|
else
|
2008-08-26 08:27:09 +02:00
|
|
|
/* cross fading is disabled or the
|
|
|
|
next song is too short */
|
2013-09-27 22:19:26 +02:00
|
|
|
xfade_state = CrossFadeState::DISABLED;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (paused) {
|
2013-09-27 22:07:20 +02:00
|
|
|
if (pc.command == PlayerCommand::NONE)
|
2013-09-26 22:53:40 +02:00
|
|
|
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
|
|
|
|
2017-12-22 01:16:38 +01:00
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
2013-09-26 23:57:31 +02:00
|
|
|
PlayNextChunk();
|
2017-12-22 01:16:38 +01:00
|
|
|
} else if (UnlockCheckOutputs() > 0) {
|
2009-03-09 19:25:26 +01:00
|
|
|
/* not enough data from decoder, but the
|
|
|
|
output thread is still busy, so it's
|
|
|
|
okay */
|
|
|
|
|
2013-11-08 11:54:30 +01:00
|
|
|
/* wake up the decoder (just in case it's
|
|
|
|
waiting for space in the MusicBuffer) and
|
|
|
|
wait for it */
|
|
|
|
dc.Signal();
|
|
|
|
dc.WaitForDecoder();
|
|
|
|
continue;
|
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 */
|
|
|
|
|
2017-12-22 01:16:38 +01:00
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
2015-10-28 18:24:42 +01:00
|
|
|
SongBorder();
|
2017-12-22 01:16:38 +01:00
|
|
|
} else if (dc.IsIdle()) {
|
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 */
|
2017-12-22 01:16:38 +01:00
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
2014-01-27 08:20:25 +01:00
|
|
|
pc.outputs.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) */
|
2017-12-22 01:16:38 +01:00
|
|
|
const ScopeUnlock unlock(pc.mutex);
|
2017-10-25 20:26:09 +02:00
|
|
|
|
|
|
|
if (throttle_silence_log.CheckUpdate(std::chrono::seconds(5)))
|
|
|
|
FormatWarning(player_domain, "Decoder is too slow; playing silence to avoid xrun");
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (!SendSilence())
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-12-20 14:43:29 +01:00
|
|
|
cross_fade_tag.reset();
|
2010-03-17 21:57:35 +01:00
|
|
|
|
2013-12-24 11:58:10 +01:00
|
|
|
if (song != nullptr) {
|
2014-01-07 21:39:47 +01:00
|
|
|
FormatDefault(player_domain, "played \"%s\"", song->GetURI());
|
2017-11-26 11:46:14 +01:00
|
|
|
song.reset();
|
2013-12-24 11:58:10 +01:00
|
|
|
}
|
2012-08-09 22:19:39 +02:00
|
|
|
|
2013-10-21 23:22:16 +02:00
|
|
|
pc.ClearTaggedSong();
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
if (queued) {
|
2013-09-27 00:03:22 +02:00
|
|
|
assert(pc.next_song != nullptr);
|
2017-11-26 11:46:14 +01:00
|
|
|
pc.next_song.reset();
|
2009-11-03 23:32:37 +01:00
|
|
|
}
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
pc.state = PlayerState::STOP;
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:57:31 +02:00
|
|
|
static void
|
2013-10-28 10:12:21 +01:00
|
|
|
do_play(PlayerControl &pc, DecoderControl &dc,
|
2017-12-20 13:32:38 +01:00
|
|
|
MusicBuffer &buffer) noexcept
|
2013-09-26 23:57:31 +02:00
|
|
|
{
|
2013-09-27 22:22:11 +02:00
|
|
|
Player player(pc, dc, buffer);
|
2013-09-26 23:57:31 +02:00
|
|
|
player.Run();
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:41:11 +01:00
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::RunThread() noexcept
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2014-01-23 10:07:14 +01:00
|
|
|
SetThreadName("player");
|
|
|
|
|
2017-02-10 22:41:11 +01:00
|
|
|
DecoderControl dc(mutex, cond,
|
|
|
|
configured_audio_format,
|
|
|
|
replay_gain_config);
|
2013-10-19 18:48:38 +02:00
|
|
|
decoder_thread_start(dc);
|
2009-01-25 13:44:39 +01:00
|
|
|
|
2017-02-10 22:41:11 +01:00
|
|
|
MusicBuffer buffer(buffer_chunks);
|
2009-03-09 19:12:06 +01:00
|
|
|
|
2017-12-22 09:35:31 +01:00
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
while (1) {
|
2017-02-10 22:41:11 +01:00
|
|
|
switch (command) {
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::SEEK:
|
|
|
|
case PlayerCommand::QUEUE:
|
2017-02-10 22:41:11 +01:00
|
|
|
assert(next_song != nullptr);
|
2009-02-10 08:18:28 +01:00
|
|
|
|
2017-12-22 09:35:31 +01:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
do_play(*this, dc, buffer);
|
|
|
|
listener.OnPlayerSync();
|
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::STOP:
|
2017-12-22 09:35:31 +01:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
outputs.Cancel();
|
|
|
|
}
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2009-03-09 19:25:26 +01:00
|
|
|
/* fall through */
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::PAUSE:
|
2017-11-26 11:46:14 +01:00
|
|
|
next_song.reset();
|
2012-08-09 22:19:39 +02:00
|
|
|
|
2017-02-10 22:41:11 +01:00
|
|
|
CommandFinished();
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::CLOSE_AUDIO:
|
2017-12-22 09:35:31 +01:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
outputs.Release();
|
|
|
|
}
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2017-02-10 22:41:11 +01:00
|
|
|
CommandFinished();
|
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;
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::UPDATE_AUDIO:
|
2017-12-22 09:35:31 +01:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
outputs.EnableDisable();
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:41:11 +01:00
|
|
|
CommandFinished();
|
2009-10-23 10:55:52 +02:00
|
|
|
break;
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::EXIT:
|
2017-12-22 09:35:31 +01:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
dc.Quit();
|
|
|
|
outputs.Close();
|
|
|
|
}
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2017-12-22 09:35:31 +01:00
|
|
|
CommandFinished();
|
2013-10-17 18:42:14 +02:00
|
|
|
return;
|
2008-08-26 08:27:16 +02:00
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::CANCEL:
|
2017-11-26 11:46:14 +01:00
|
|
|
next_song.reset();
|
2012-08-09 22:19:39 +02:00
|
|
|
|
2017-02-10 22:41:11 +01:00
|
|
|
CommandFinished();
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::REFRESH:
|
2009-10-08 22:09:25 +02:00
|
|
|
/* no-op when not playing */
|
2017-02-10 22:41:11 +01:00
|
|
|
CommandFinished();
|
2009-10-08 22:09:25 +02:00
|
|
|
break;
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerCommand::NONE:
|
2017-02-10 22:41:11 +01:00
|
|
|
Wait();
|
2008-08-26 08:27:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
void
|
2014-08-16 10:33:19 +02:00
|
|
|
StartPlayerThread(PlayerControl &pc)
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2013-10-17 18:42:14 +02:00
|
|
|
assert(!pc.thread.IsDefined());
|
|
|
|
|
2017-02-10 22:41:11 +01:00
|
|
|
pc.thread.Start();
|
2008-08-26 08:27:09 +02:00
|
|
|
}
|