2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01: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.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2014-08-30 00:02:11 +02:00
|
|
|
#ifndef MPD_PLAYER_CONTROL_HXX
|
|
|
|
#define MPD_PLAYER_CONTROL_HXX
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2016-12-14 12:14:57 +01:00
|
|
|
#include "output/Client.hxx"
|
2020-01-18 20:07:09 +01:00
|
|
|
#include "pcm/AudioFormat.hxx"
|
2013-01-10 09:30:01 +01:00
|
|
|
#include "thread/Mutex.hxx"
|
|
|
|
#include "thread/Cond.hxx"
|
2013-10-17 18:42:14 +02:00
|
|
|
#include "thread/Thread.hxx"
|
2013-10-29 00:14:27 +01:00
|
|
|
#include "CrossFade.hxx"
|
2014-08-27 18:50:16 +02:00
|
|
|
#include "Chrono.hxx"
|
2016-11-25 12:51:55 +01:00
|
|
|
#include "ReplayGainConfig.hxx"
|
|
|
|
#include "ReplayGainMode.hxx"
|
2018-09-21 18:09:14 +02:00
|
|
|
#include "MusicChunkPtr.hxx"
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2020-03-13 01:08:53 +01:00
|
|
|
#include <cstdint>
|
2016-09-08 10:21:34 +02:00
|
|
|
#include <exception>
|
2017-11-26 11:46:14 +01:00
|
|
|
#include <memory>
|
2016-09-08 10:21:34 +02:00
|
|
|
|
2018-09-21 18:09:14 +02:00
|
|
|
struct Tag;
|
2014-02-21 08:55:52 +01:00
|
|
|
class PlayerListener;
|
2017-12-29 16:23:19 +01:00
|
|
|
class PlayerOutputs;
|
2019-05-08 18:39:00 +02:00
|
|
|
class InputCacheManager;
|
2014-01-07 21:39:47 +01:00
|
|
|
class DetachedSong;
|
2009-10-31 19:22:56 +01:00
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
enum class PlayerState : uint8_t {
|
|
|
|
STOP,
|
|
|
|
PAUSE,
|
|
|
|
PLAY
|
2008-08-26 08:27:08 +02:00
|
|
|
};
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
enum class PlayerCommand : uint8_t {
|
|
|
|
NONE,
|
|
|
|
EXIT,
|
|
|
|
STOP,
|
|
|
|
PAUSE,
|
2017-12-20 11:24:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Seek to a certain position in the specified song. This
|
|
|
|
* command can also be used to change the current song or
|
2017-11-26 11:27:06 +01:00
|
|
|
* start playback. It "finishes" immediately, but
|
|
|
|
* PlayerControl::seeking will be set until seeking really
|
|
|
|
* completes (or fails).
|
2017-12-20 11:24:35 +01:00
|
|
|
*/
|
2013-09-27 22:07:20 +02:00
|
|
|
SEEK,
|
2017-12-20 11:24:35 +01:00
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
CLOSE_AUDIO,
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2009-10-23 10:55:52 +02:00
|
|
|
/**
|
2014-01-28 11:34:09 +01:00
|
|
|
* At least one AudioOutput.enabled flag has been modified;
|
2009-10-23 10:55:52 +02:00
|
|
|
* commit those changes to the output threads.
|
|
|
|
*/
|
2013-09-27 22:07:20 +02:00
|
|
|
UPDATE_AUDIO,
|
2009-10-23 10:55:52 +02:00
|
|
|
|
2013-10-28 10:12:21 +01:00
|
|
|
/** PlayerControl.next_song has been updated */
|
2013-09-27 22:07:20 +02:00
|
|
|
QUEUE,
|
2008-10-12 00:07:54 +02:00
|
|
|
|
|
|
|
/**
|
2013-10-28 10:12:21 +01:00
|
|
|
* cancel pre-decoding PlayerControl.next_song; if the player
|
2008-10-12 00:07:54 +02:00
|
|
|
* has already started playing this song, it will completely
|
|
|
|
* stop
|
|
|
|
*/
|
2013-09-27 22:07:20 +02:00
|
|
|
CANCEL,
|
2009-10-08 22:09:25 +02:00
|
|
|
|
|
|
|
/**
|
2013-10-28 10:12:21 +01:00
|
|
|
* Refresh status information in the #PlayerControl struct,
|
2009-10-08 22:09:25 +02:00
|
|
|
* e.g. elapsed_time.
|
|
|
|
*/
|
2013-09-27 22:07:20 +02:00
|
|
|
REFRESH,
|
2008-08-26 08:27:07 +02:00
|
|
|
};
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
enum class PlayerError : uint8_t {
|
|
|
|
NONE,
|
2012-08-08 22:22:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The decoder has failed to decode the song.
|
|
|
|
*/
|
2013-09-27 22:07:20 +02:00
|
|
|
DECODER,
|
2012-08-08 22:22:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The audio output has failed.
|
|
|
|
*/
|
2013-09-27 22:07:20 +02:00
|
|
|
OUTPUT,
|
2008-11-03 21:49:40 +01:00
|
|
|
};
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2018-09-21 17:22:35 +02:00
|
|
|
struct PlayerStatus {
|
2013-09-27 22:07:20 +02:00
|
|
|
PlayerState state;
|
2009-10-08 20:48:07 +02:00
|
|
|
uint16_t bit_rate;
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat audio_format;
|
2014-08-29 12:43:47 +02:00
|
|
|
SignedSongTime total_time;
|
2014-08-29 13:07:38 +02:00
|
|
|
SongTime elapsed_time;
|
2009-10-08 20:48:07 +02:00
|
|
|
};
|
|
|
|
|
2018-09-21 18:00:23 +02:00
|
|
|
class PlayerControl final : public AudioOutputClient {
|
|
|
|
friend class Player;
|
|
|
|
|
2014-02-21 08:55:52 +01:00
|
|
|
PlayerListener &listener;
|
|
|
|
|
2017-12-29 16:23:19 +01:00
|
|
|
PlayerOutputs &outputs;
|
2014-01-27 08:20:25 +01:00
|
|
|
|
2019-05-08 18:39:00 +02:00
|
|
|
InputCacheManager *const input_cache;
|
|
|
|
|
2014-08-30 00:03:14 +02:00
|
|
|
const unsigned buffer_chunks;
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2016-12-03 14:12:08 +01:00
|
|
|
/**
|
|
|
|
* The "audio_output_format" setting.
|
|
|
|
*/
|
|
|
|
const AudioFormat configured_audio_format;
|
|
|
|
|
2013-10-17 18:42:14 +02:00
|
|
|
/**
|
|
|
|
* The handle of the player thread.
|
|
|
|
*/
|
|
|
|
Thread thread;
|
2009-01-25 13:44:33 +01:00
|
|
|
|
2009-10-31 17:02:12 +01:00
|
|
|
/**
|
2013-10-21 23:22:16 +02:00
|
|
|
* This lock protects #command, #state, #error, #tagged_song.
|
2009-10-31 17:02:12 +01:00
|
|
|
*/
|
2013-01-20 17:48:23 +01:00
|
|
|
mutable Mutex mutex;
|
2009-10-31 17:02:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger this object after you have modified #command.
|
|
|
|
*/
|
2013-01-10 09:30:01 +01:00
|
|
|
Cond cond;
|
2009-10-31 17:02:12 +01:00
|
|
|
|
2013-01-25 23:53:43 +01:00
|
|
|
/**
|
|
|
|
* This object gets signalled when the player thread has
|
|
|
|
* finished the #command. It wakes up the client that waits
|
|
|
|
* (i.e. the main thread).
|
|
|
|
*/
|
|
|
|
Cond client_cond;
|
|
|
|
|
2012-08-08 22:18:08 +02:00
|
|
|
/**
|
|
|
|
* The error that occurred in the player thread. This
|
2016-09-08 10:22:31 +02:00
|
|
|
* attribute is only valid if #error_type is not
|
2013-09-27 22:07:20 +02:00
|
|
|
* #PlayerError::NONE. The object must be freed when this
|
|
|
|
* object transitions back to #PlayerError::NONE.
|
2012-08-08 22:18:08 +02:00
|
|
|
*/
|
2016-09-08 10:21:34 +02:00
|
|
|
std::exception_ptr error;
|
2012-08-08 22:18:08 +02:00
|
|
|
|
2017-12-20 20:27:44 +01:00
|
|
|
/**
|
|
|
|
* The next queued song.
|
|
|
|
*
|
|
|
|
* This is a duplicate, and must be freed when this attribute
|
|
|
|
* is cleared.
|
|
|
|
*/
|
|
|
|
std::unique_ptr<DetachedSong> next_song;
|
|
|
|
|
2013-10-21 23:22:16 +02:00
|
|
|
/**
|
2014-01-07 21:39:47 +01:00
|
|
|
* A copy of the current #DetachedSong after its tags have
|
|
|
|
* been updated by the decoder (for example, a radio stream
|
|
|
|
* that has sent a new tag after switching to the next song).
|
2014-02-21 08:55:52 +01:00
|
|
|
* This shall be used by PlayerListener::OnPlayerTagModified()
|
|
|
|
* to update the current #DetachedSong in the queue.
|
2013-10-21 23:22:16 +02:00
|
|
|
*
|
|
|
|
* Protected by #mutex. Set by the PlayerThread and consumed
|
|
|
|
* by the main thread.
|
|
|
|
*/
|
2017-11-26 11:46:14 +01:00
|
|
|
std::unique_ptr<DetachedSong> tagged_song;
|
2013-10-21 23:22:16 +02:00
|
|
|
|
2017-12-20 20:27:44 +01:00
|
|
|
PlayerCommand command = PlayerCommand::NONE;
|
|
|
|
PlayerState state = PlayerState::STOP;
|
2013-10-29 00:14:27 +01:00
|
|
|
|
2017-12-20 20:27:44 +01:00
|
|
|
PlayerError error_type = PlayerError::NONE;
|
2013-10-29 00:14:27 +01:00
|
|
|
|
2016-11-25 12:51:55 +01:00
|
|
|
ReplayGainMode replay_gain_mode = ReplayGainMode::OFF;
|
|
|
|
|
2017-11-26 11:27:06 +01:00
|
|
|
/**
|
|
|
|
* Is the player currently busy with the SEEK command?
|
|
|
|
*/
|
|
|
|
bool seeking = false;
|
|
|
|
|
2012-08-25 08:44:31 +02:00
|
|
|
/**
|
|
|
|
* If this flag is set, then the player will be auto-paused at
|
|
|
|
* the end of the song, before the next song starts to play.
|
|
|
|
*
|
|
|
|
* This is a copy of the queue's "single" flag most of the
|
|
|
|
* time.
|
|
|
|
*/
|
2016-12-03 13:08:00 +01:00
|
|
|
bool border_pause = false;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2017-12-20 20:27:19 +01:00
|
|
|
/**
|
|
|
|
* If this flag is set, then the player thread is currently
|
|
|
|
* occupied and will not be able to respond quickly to
|
|
|
|
* commands (e.g. waiting for the decoder thread to finish
|
|
|
|
* seeking). This is used to skip #PlayerCommand::REFRESH to
|
|
|
|
* avoid blocking the main thread.
|
|
|
|
*/
|
|
|
|
bool occupied = false;
|
|
|
|
|
|
|
|
struct ScopeOccupied {
|
|
|
|
PlayerControl &pc;
|
|
|
|
|
|
|
|
explicit ScopeOccupied(PlayerControl &_pc) noexcept:pc(_pc) {
|
|
|
|
assert(!pc.occupied);
|
|
|
|
pc.occupied = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopeOccupied() noexcept {
|
|
|
|
assert(pc.occupied);
|
|
|
|
pc.occupied = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-20 20:27:44 +01:00
|
|
|
AudioFormat audio_format;
|
|
|
|
uint16_t bit_rate;
|
|
|
|
|
|
|
|
SignedSongTime total_time;
|
|
|
|
SongTime elapsed_time;
|
|
|
|
|
|
|
|
SongTime seek_time;
|
|
|
|
|
|
|
|
CrossFadeSettings cross_fade;
|
|
|
|
|
|
|
|
const ReplayGainConfig replay_gain_config;
|
|
|
|
|
2018-09-21 20:50:15 +02:00
|
|
|
FloatDuration total_play_time = FloatDuration::zero();
|
2017-12-20 20:27:44 +01:00
|
|
|
|
2018-09-21 18:00:23 +02:00
|
|
|
public:
|
2014-02-21 08:55:52 +01:00
|
|
|
PlayerControl(PlayerListener &_listener,
|
2017-12-29 16:23:19 +01:00
|
|
|
PlayerOutputs &_outputs,
|
2019-05-08 18:39:00 +02:00
|
|
|
InputCacheManager *_input_cache,
|
2014-01-27 08:20:25 +01:00
|
|
|
unsigned buffer_chunks,
|
2016-12-03 14:12:08 +01:00
|
|
|
AudioFormat _configured_audio_format,
|
2017-11-26 12:02:08 +01:00
|
|
|
const ReplayGainConfig &_replay_gain_config) noexcept;
|
|
|
|
~PlayerControl() noexcept;
|
2008-09-24 07:14:11 +02:00
|
|
|
|
2018-09-23 17:28:41 +02:00
|
|
|
void Kill() noexcept;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like CheckRethrowError(), but locks and unlocks the object.
|
|
|
|
*/
|
|
|
|
void LockCheckRethrowError() const {
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2018-09-23 17:28:41 +02:00
|
|
|
CheckRethrowError();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LockClearError() noexcept;
|
|
|
|
|
|
|
|
PlayerError GetErrorType() const noexcept {
|
|
|
|
return error_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LockUpdateAudio() noexcept;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws on error.
|
|
|
|
*
|
|
|
|
* @param song the song to be queued
|
|
|
|
*/
|
|
|
|
void Play(std::unique_ptr<DetachedSong> song);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param song the song to be queued; the given instance will be owned
|
|
|
|
* and freed by the player
|
|
|
|
*/
|
|
|
|
void LockEnqueueSong(std::unique_ptr<DetachedSong> song) noexcept;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes the player thread seek the specified song to a position.
|
|
|
|
*
|
|
|
|
* Throws on error.
|
|
|
|
*
|
|
|
|
* @param song the song to be queued; the given instance will be owned
|
|
|
|
* and freed by the player
|
|
|
|
*/
|
|
|
|
void LockSeek(std::unique_ptr<DetachedSong> song, SongTime t);
|
|
|
|
|
|
|
|
void LockStop() noexcept;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* see PlayerCommand::CANCEL
|
|
|
|
*/
|
|
|
|
void LockCancel() noexcept;
|
|
|
|
|
|
|
|
void LockSetPause(bool pause_flag) noexcept;
|
|
|
|
|
|
|
|
void LockPause() noexcept;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the player's #border_pause flag.
|
|
|
|
*/
|
|
|
|
void LockSetBorderPause(bool border_pause) noexcept;
|
|
|
|
void SetCrossFade(FloatDuration duration) noexcept;
|
|
|
|
|
|
|
|
auto GetCrossFade() const noexcept {
|
|
|
|
return cross_fade.duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetMixRampDb(float mixramp_db) noexcept;
|
|
|
|
|
|
|
|
float GetMixRampDb() const noexcept {
|
|
|
|
return cross_fade.mixramp_db;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetMixRampDelay(FloatDuration mixramp_delay) noexcept;
|
|
|
|
|
|
|
|
auto GetMixRampDelay() const noexcept {
|
|
|
|
return cross_fade.mixramp_delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LockSetReplayGainMode(ReplayGainMode _mode) noexcept {
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2018-09-23 17:28:41 +02:00
|
|
|
replay_gain_mode = _mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like ReadTaggedSong(), but locks and unlocks the object.
|
|
|
|
*/
|
|
|
|
std::unique_ptr<DetachedSong> LockReadTaggedSong() noexcept;
|
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2018-09-23 17:28:41 +02:00
|
|
|
PlayerStatus LockGetStatus() noexcept;
|
|
|
|
|
|
|
|
PlayerState GetState() const noexcept {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SyncInfo {
|
|
|
|
PlayerState state;
|
|
|
|
bool has_next_song;
|
|
|
|
};
|
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2018-09-23 17:28:41 +02:00
|
|
|
SyncInfo LockGetSyncInfo() const noexcept {
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2018-09-23 17:28:41 +02:00
|
|
|
return {state, next_song != nullptr};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto GetTotalPlayTime() const noexcept {
|
|
|
|
return total_play_time;
|
|
|
|
}
|
|
|
|
|
2018-09-23 17:19:49 +02:00
|
|
|
private:
|
2013-01-20 17:48:23 +01:00
|
|
|
/**
|
|
|
|
* Signals the object. The object should be locked prior to
|
|
|
|
* calling this function.
|
|
|
|
*/
|
2017-11-26 12:02:08 +01:00
|
|
|
void Signal() noexcept {
|
2019-04-25 18:33:09 +02:00
|
|
|
cond.notify_one();
|
2013-01-20 17:48:23 +01:00
|
|
|
}
|
2008-10-12 00:07:54 +02:00
|
|
|
|
2013-01-20 17:48:23 +01:00
|
|
|
/**
|
|
|
|
* Signals the object. The object is temporarily locked by
|
|
|
|
* this function.
|
|
|
|
*/
|
2017-11-26 12:02:08 +01:00
|
|
|
void LockSignal() noexcept {
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2013-01-20 17:48:23 +01:00
|
|
|
Signal();
|
|
|
|
}
|
2004-02-25 01:08:48 +01:00
|
|
|
|
2013-01-20 17:48:23 +01:00
|
|
|
/**
|
|
|
|
* Waits for a signal on the object. This function is only
|
|
|
|
* valid in the player thread. The object must be locked
|
|
|
|
* prior to calling this function.
|
|
|
|
*/
|
2019-04-26 18:47:22 +02:00
|
|
|
void Wait(std::unique_lock<Mutex> &lock) noexcept {
|
2013-10-17 18:42:14 +02:00
|
|
|
assert(thread.IsInside());
|
2013-01-25 23:53:43 +01:00
|
|
|
|
2019-04-26 18:47:22 +02:00
|
|
|
cond.wait(lock);
|
2013-01-20 17:48:23 +01:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-01-25 23:53:43 +01:00
|
|
|
/**
|
|
|
|
* Wake up the client waiting for command completion.
|
|
|
|
*
|
|
|
|
* Caller must lock the object.
|
|
|
|
*/
|
2017-11-26 12:02:08 +01:00
|
|
|
void ClientSignal() noexcept {
|
2013-10-17 18:42:14 +02:00
|
|
|
assert(thread.IsInside());
|
2013-01-25 23:53:43 +01:00
|
|
|
|
2019-04-25 18:33:09 +02:00
|
|
|
client_cond.notify_one();
|
2013-01-25 23:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The client calls this method to wait for command
|
|
|
|
* completion.
|
|
|
|
*
|
|
|
|
* Caller must lock the object.
|
|
|
|
*/
|
2019-04-26 18:47:22 +02:00
|
|
|
void ClientWait(std::unique_lock<Mutex> &lock) noexcept {
|
2013-10-17 18:42:14 +02:00
|
|
|
assert(!thread.IsInside());
|
2013-01-25 23:53:43 +01:00
|
|
|
|
2019-04-26 18:47:22 +02:00
|
|
|
client_cond.wait(lock);
|
2013-01-25 23:53:43 +01:00
|
|
|
}
|
|
|
|
|
2013-09-27 07:58:48 +02:00
|
|
|
/**
|
|
|
|
* A command has been finished. This method clears the
|
|
|
|
* command and signals the client.
|
|
|
|
*
|
|
|
|
* To be called from the player thread. Caller must lock the
|
|
|
|
* object.
|
|
|
|
*/
|
2017-11-26 12:02:08 +01:00
|
|
|
void CommandFinished() noexcept {
|
2013-09-27 22:07:20 +02:00
|
|
|
assert(command != PlayerCommand::NONE);
|
2013-09-27 07:58:48 +02:00
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
command = PlayerCommand::NONE;
|
2013-09-27 07:58:48 +02:00
|
|
|
ClientSignal();
|
|
|
|
}
|
|
|
|
|
2017-11-26 12:02:08 +01:00
|
|
|
void LockCommandFinished() noexcept {
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2015-11-05 00:38:09 +01:00
|
|
|
CommandFinished();
|
|
|
|
}
|
|
|
|
|
2016-12-14 12:54:19 +01:00
|
|
|
/**
|
|
|
|
* Checks if the size of the #MusicPipe is below the #threshold. If
|
|
|
|
* not, it attempts to synchronize with all output threads, and waits
|
|
|
|
* until another #MusicChunk is finished.
|
|
|
|
*
|
|
|
|
* Caller must lock the mutex.
|
|
|
|
*
|
|
|
|
* @param threshold the maximum number of chunks in the pipe
|
|
|
|
* @return true if there are less than #threshold chunks in the pipe
|
|
|
|
*/
|
2019-04-26 18:47:22 +02:00
|
|
|
bool WaitOutputConsumed(std::unique_lock<Mutex> &lock,
|
|
|
|
unsigned threshold) noexcept;
|
2016-12-14 12:54:19 +01:00
|
|
|
|
2017-11-26 12:02:08 +01:00
|
|
|
bool LockWaitOutputConsumed(unsigned threshold) noexcept {
|
2019-04-26 18:47:22 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
|
|
|
return WaitOutputConsumed(lock, threshold);
|
2016-12-14 12:54:19 +01:00
|
|
|
}
|
|
|
|
|
2013-09-27 09:20:53 +02:00
|
|
|
/**
|
|
|
|
* Wait for the command to be finished by the player thread.
|
|
|
|
*
|
|
|
|
* To be called from the main thread. Caller must lock the
|
|
|
|
* object.
|
|
|
|
*/
|
2019-04-26 18:47:22 +02:00
|
|
|
void WaitCommandLocked(std::unique_lock<Mutex> &lock) noexcept {
|
2013-09-27 22:07:20 +02:00
|
|
|
while (command != PlayerCommand::NONE)
|
2019-04-26 18:47:22 +02:00
|
|
|
ClientWait(lock);
|
2013-09-27 09:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a command to the player thread and synchronously wait
|
|
|
|
* for it to finish.
|
|
|
|
*
|
|
|
|
* To be called from the main thread. Caller must lock the
|
|
|
|
* object.
|
|
|
|
*/
|
2019-04-26 18:47:22 +02:00
|
|
|
void SynchronousCommand(std::unique_lock<Mutex> &lock,
|
|
|
|
PlayerCommand cmd) noexcept {
|
2013-09-27 22:07:20 +02:00
|
|
|
assert(command == PlayerCommand::NONE);
|
2013-09-27 09:20:53 +02:00
|
|
|
|
|
|
|
command = cmd;
|
|
|
|
Signal();
|
2019-04-26 18:47:22 +02:00
|
|
|
WaitCommandLocked(lock);
|
2013-09-27 09:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a command to the player thread and synchronously wait
|
|
|
|
* for it to finish.
|
|
|
|
*
|
|
|
|
* To be called from the main thread. This method locks the
|
|
|
|
* object.
|
|
|
|
*/
|
2017-05-08 14:44:49 +02:00
|
|
|
void LockSynchronousCommand(PlayerCommand cmd) noexcept {
|
2019-04-26 18:47:22 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
|
|
|
SynchronousCommand(lock, cmd);
|
2013-09-27 09:20:53 +02:00
|
|
|
}
|
|
|
|
|
2019-04-26 18:47:22 +02:00
|
|
|
void PauseLocked(std::unique_lock<Mutex> &lock) noexcept;
|
2013-09-27 09:20:53 +02:00
|
|
|
|
2017-11-26 12:02:08 +01:00
|
|
|
void ClearError() noexcept {
|
2015-11-11 17:34:37 +01:00
|
|
|
error_type = PlayerError::NONE;
|
2016-09-08 10:21:34 +02:00
|
|
|
error = std::exception_ptr();
|
2015-11-11 17:34:37 +01:00
|
|
|
}
|
|
|
|
|
2017-11-26 12:02:08 +01:00
|
|
|
bool ApplyBorderPause() noexcept {
|
2016-12-14 13:09:48 +01:00
|
|
|
if (border_pause)
|
|
|
|
state = PlayerState::PAUSE;
|
|
|
|
return border_pause;
|
|
|
|
}
|
|
|
|
|
2013-01-20 17:48:23 +01:00
|
|
|
/**
|
|
|
|
* Set the error. Discards any previous error condition.
|
|
|
|
*
|
|
|
|
* Caller must lock the object.
|
|
|
|
*
|
2013-09-27 22:07:20 +02:00
|
|
|
* @param type the error type; must not be #PlayerError::NONE
|
2013-01-20 17:48:23 +01:00
|
|
|
*/
|
2017-11-26 12:02:08 +01:00
|
|
|
void SetError(PlayerError type, std::exception_ptr &&_error) noexcept;
|
2007-06-01 20:10:13 +02:00
|
|
|
|
2016-12-14 13:01:17 +01:00
|
|
|
/**
|
|
|
|
* Set the error and set state to PlayerState::PAUSE.
|
|
|
|
*/
|
2017-11-26 12:02:08 +01:00
|
|
|
void SetOutputError(std::exception_ptr &&_error) noexcept {
|
2016-12-14 13:01:17 +01:00
|
|
|
SetError(PlayerError::OUTPUT, std::move(_error));
|
|
|
|
|
|
|
|
/* pause: the user may resume playback as soon as an
|
|
|
|
audio output becomes available */
|
|
|
|
state = PlayerState::PAUSE;
|
|
|
|
}
|
|
|
|
|
2017-11-26 12:02:08 +01:00
|
|
|
void LockSetOutputError(std::exception_ptr &&_error) noexcept {
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> lock(mutex);
|
2016-12-14 13:01:17 +01:00
|
|
|
SetOutputError(std::move(_error));
|
|
|
|
}
|
|
|
|
|
2013-10-17 19:34:59 +02:00
|
|
|
/**
|
2016-09-08 10:21:34 +02:00
|
|
|
* Checks whether an error has occurred, and if so, rethrows
|
|
|
|
* it.
|
2013-10-17 19:34:59 +02:00
|
|
|
*
|
|
|
|
* Caller must lock the object.
|
|
|
|
*/
|
2016-09-08 10:21:34 +02:00
|
|
|
void CheckRethrowError() const {
|
2013-10-17 19:34:59 +02:00
|
|
|
if (error_type != PlayerError::NONE)
|
2016-09-08 10:21:34 +02:00
|
|
|
std::rethrow_exception(error);
|
2013-10-17 19:34:59 +02:00
|
|
|
}
|
2009-10-23 10:55:52 +02:00
|
|
|
|
2013-10-21 23:22:16 +02:00
|
|
|
/**
|
|
|
|
* Set the #tagged_song attribute to a newly allocated copy of
|
2014-01-07 21:39:47 +01:00
|
|
|
* the given #DetachedSong. Locks and unlocks the object.
|
2013-10-21 23:22:16 +02:00
|
|
|
*/
|
2017-11-26 12:02:08 +01:00
|
|
|
void LockSetTaggedSong(const DetachedSong &song) noexcept;
|
2013-10-21 23:22:16 +02:00
|
|
|
|
2017-11-26 12:02:08 +01:00
|
|
|
void ClearTaggedSong() noexcept;
|
2013-10-21 23:22:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read and clear the #tagged_song attribute.
|
|
|
|
*
|
|
|
|
* Caller must lock the object.
|
|
|
|
*/
|
2017-11-26 11:46:14 +01:00
|
|
|
std::unique_ptr<DetachedSong> ReadTaggedSong() noexcept;
|
2013-10-21 23:22:16 +02:00
|
|
|
|
2019-04-26 18:47:22 +02:00
|
|
|
void EnqueueSongLocked(std::unique_lock<Mutex> &lock,
|
|
|
|
std::unique_ptr<DetachedSong> song) noexcept;
|
2013-09-27 09:20:53 +02:00
|
|
|
|
2016-09-08 10:29:49 +02:00
|
|
|
/**
|
2017-12-20 12:20:09 +01:00
|
|
|
* Throws on error.
|
2016-09-08 10:29:49 +02:00
|
|
|
*/
|
2019-04-26 18:47:22 +02:00
|
|
|
void SeekLocked(std::unique_lock<Mutex> &lock,
|
|
|
|
std::unique_ptr<DetachedSong> song, SongTime t);
|
2015-11-11 16:56:24 +01:00
|
|
|
|
2018-09-21 18:00:23 +02:00
|
|
|
/**
|
|
|
|
* Caller must lock the object.
|
|
|
|
*/
|
|
|
|
void CancelPendingSeek() noexcept {
|
|
|
|
if (!seeking)
|
|
|
|
return;
|
|
|
|
|
|
|
|
seeking = false;
|
|
|
|
ClientSignal();
|
|
|
|
}
|
|
|
|
|
2018-09-21 18:09:14 +02:00
|
|
|
void LockUpdateSongTag(DetachedSong &song,
|
|
|
|
const Tag &new_tag) noexcept;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plays a #MusicChunk 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.
|
|
|
|
*
|
|
|
|
* Player lock is not held.
|
|
|
|
*
|
|
|
|
* Throws on error.
|
|
|
|
*/
|
|
|
|
void PlayChunk(DetachedSong &song, MusicChunkPtr chunk,
|
|
|
|
const AudioFormat &format);
|
|
|
|
|
2016-12-14 12:14:57 +01:00
|
|
|
/* virtual methods from AudioOutputClient */
|
|
|
|
void ChunksConsumed() override {
|
|
|
|
LockSignal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApplyEnabled() override {
|
|
|
|
LockUpdateAudio();
|
|
|
|
}
|
2017-02-10 22:41:11 +01:00
|
|
|
|
2017-11-26 12:02:08 +01:00
|
|
|
void RunThread() noexcept;
|
2013-01-20 17:48:23 +01:00
|
|
|
};
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|