26 : device_id(device_id),
27 sink(
std::move(sink)),
28 sources(
std::move(sources)),
43 assert(this->
file !=
nullptr);
44 auto as = this->
file->Update();
50 auto pos = this->
file->Position();
54 .AddArg(std::to_string(pos)));
77 auto pos = this->
file->Position();
79 .AddArg(std::to_string(pos)));
96 assert(this->
file !=
nullptr);
97 this->
file = std::make_unique<NoAudio>();
127 assert(this->
file !=
nullptr);
142 assert(this->
file !=
nullptr);
159 std::uint64_t pos = 0;
177 Debug() <<
"Seek failure" << std::endl;
197 assert(this->
file !=
nullptr);
200 this->
file->SetPlaying(playing);
231 if (pos_str.find(
'-') != std::string::npos) {
238 pos = std::stoull(pos_str, &cpos);
246 auto sl = pos_str.length();
254 assert(this->
file !=
nullptr);
256 this->
file->SetPosition(pos);
262 .AddArg(std::to_string(pos)));
269 switch (this->
file->CurrentState()) {
292 if (this->
io !=
nullptr) this->
io->
Respond(
id, rs);
297 std::uint64_t secs = micros / 1000 / 1000;
300 bool announce = this->
last_pos < secs;
301 if (announce) this->
last_pos = secs;
308 std::unique_ptr<AudioSource> source = this->
LoadSource(path);
309 assert(source !=
nullptr);
312 return std::make_unique<PipeAudio>(std::move(source), std::move(
sink));
317 size_t extpoint = path.find_last_of(
'.');
318 std::string ext = path.substr(extpoint + 1);
320 auto ibuilder = this->
sources.find(ext);
321 if (ibuilder == this->
sources.end()) {
322 throw FileError(
"Unknown file format: " + ext);
325 return (ibuilder->second)(path);
Declarations of the playd Error exception set.
Declaration of the AudioSource class.
The Audio is currently playing.
bool Update()
Instructs the Player to perform a cycle of work.
static std::uint64_t PosParse(const std::string &pos_str)
Parses pos_str as a seek timestamp.
const std::string MSG_CMD_NEEDS_LOADED
Message shown when a command that works only when a file is loaded is fired when there isn't anything...
std::function< std::unique_ptr< AudioSink >(const AudioSource &, int)> SinkFn
Type for functions that construct sinks.
Response Load(const std::string &tag, const std::string &path)
Loads a file.
const std::string MSG_LOAD_EMPTY_PATH
Message shown when one tries to Load an empty path.
The Audio has ended and can't play without a seek.
void PosRaw(const std::string &tag, std::uint64_t pos)
Performs an actual seek.
Constant human-readable messages used within playd.
Declaration of the Audio class.
void Respond(int id, Response rs) const
Outputs a response, if there is a ResponseSink attached.
Response Quit(const std::string &tag)
Quits playd.
virtual void Respond(size_t id, const Response &response) const
Outputs a response.
const ResponseSink * io
The sink for responses.
const std::string MSG_CMD_PLAYER_CLOSING
Message shown when a command is sent to a closing Player.
An Error signifying that playd can't read a file.
static const std::string NOREQUEST
The tag for unsolicited messages (not from responses).
const std::string & Message() const
The human-readable message for this error.
static Response Success(const std::string &tag)
Shortcut for constructing a final response to a successful request.
bool CanAnnounceTime(std::uint64_t micros)
Determines whether we can broadcast a POS response.
Abstract class for anything that can be sent a response.
bool dead
Whether the Player is closing.
Response Eject(const std::string &tag)
Ejects the current loaded song, if any.
int device_id
The sink's device ID.
std::unique_ptr< AudioSource > LoadSource(const std::string &path) const
Loads a file, creating an AudioSource.
SinkFn sink
The sink create function.
The loaded file just changed.
std::map< std::string, SourceFn > sources
The file formats map.
The loaded file is playing.
The loaded file just ended.
The loaded file just ejected.
static Response Failure(const std::string &tag, const std::string &msg)
Shortcut for constructing a final response to a failed request.
Player(int device_id, SinkFn sink, std::map< std::string, SourceFn > sources)
Constructs a Player.
A dummy Audio implementation representing a lack of file.
Declaration of the AudioSink class.
Response Pos(const std::string &tag, const std::string &pos_str)
Seeks to a given position in the current file.
std::unique_ptr< Audio > LoadRaw(const std::string &path) const
Loads a file, creating an Audio for it.
void DumpState(size_t id, const std::string &tag) const
Emits a response for the current audio state to the sink.
std::uint64_t last_pos
The last-sent position.
Declaration of the Player class, and associated types.
An Error signifying that playd can't seek in a file.
Response Dump(size_t id, const std::string &tag) const
Dumps the current player state to the given ID.
Declaration of classes pertaining to responses to the client.
Response SetPlaying(const std::string &tag, bool playing)
Tells the audio file to start or stop playing.
Class for telling the human what playd is doing.
std::unique_ptr< Audio > file
The loaded audio file.
Response End(const std::string &tag)
Ends a file, stopping and rewinding.
const std::string MSG_SEEK_INVALID_VALUE
Message shown when a seek command has an invalid time value.
Code
Enumeration of all possible response codes.
The loaded file has stopped.
void SetIo(ResponseSink &io)
Sets the ResponseSink to which this Player shall send responses.
An Error signifying that no audio is loaded.
Server sending current song time.
The Audio has been stopped, or not yet played.
static Response Invalid(const std::string &tag, const std::string &msg)
Shortcut for constructing a final response to a invalid request.