A Player contains a loaded audio file and a command API for manipulating it. More...
#include <player.hpp>
Public Types | |
using | SinkFn = std::function< std::unique_ptr< AudioSink >(const AudioSource &, int)> |
Type for functions that construct sinks. | |
using | SourceFn = std::function< std::unique_ptr< AudioSource >(const std::string &)> |
Type for functions that construct sources. | |
Public Member Functions | |
Player (int device_id, SinkFn sink, std::map< std::string, SourceFn > sources) | |
Constructs a Player. More... | |
Player (const Player &)=delete | |
Deleted copy constructor. | |
Player & | operator= (const Player &)=delete |
Deleted copy-assignment constructor. | |
void | SetIo (ResponseSink &io) |
Sets the ResponseSink to which this Player shall send responses. More... | |
bool | Update () |
Instructs the Player to perform a cycle of work. More... | |
Response | SetPlaying (const std::string &tag, bool playing) |
Tells the audio file to start or stop playing. More... | |
Response | Dump (size_t id, const std::string &tag) const |
Dumps the current player state to the given ID. More... | |
Response | Eject (const std::string &tag) |
Ejects the current loaded song, if any. More... | |
Response | End (const std::string &tag) |
Ends a file, stopping and rewinding. More... | |
Response | Load (const std::string &tag, const std::string &path) |
Loads a file. More... | |
Response | Pos (const std::string &tag, const std::string &pos_str) |
Seeks to a given position in the current file. More... | |
Response | Quit (const std::string &tag) |
Quits playd. More... | |
Private Member Functions | |
void | PosRaw (const std::string &tag, std::uint64_t pos) |
Performs an actual seek. More... | |
void | DumpState (size_t id, const std::string &tag) const |
Emits a response for the current audio state to the sink. More... | |
void | Respond (int id, Response rs) const |
Outputs a response, if there is a ResponseSink attached. More... | |
bool | CanAnnounceTime (std::uint64_t micros) |
Determines whether we can broadcast a POS response. More... | |
std::unique_ptr< Audio > | LoadRaw (const std::string &path) const |
Loads a file, creating an Audio for it. More... | |
std::unique_ptr< AudioSource > | LoadSource (const std::string &path) const |
Loads a file, creating an AudioSource. More... | |
Static Private Member Functions | |
static std::uint64_t | PosParse (const std::string &pos_str) |
Parses pos_str as a seek timestamp. More... | |
Private Attributes | |
int | device_id |
The sink's device ID. | |
SinkFn | sink |
The sink create function. | |
std::map< std::string, SourceFn > | sources |
The file formats map. | |
std::unique_ptr< Audio > | file |
The loaded audio file. | |
bool | dead |
Whether the Player is closing. | |
const ResponseSink * | io |
The sink for responses. | |
std::uint64_t | last_pos |
The last-sent position. | |
A Player contains a loaded audio file and a command API for manipulating it.
Definition at line 31 of file player.hpp.
Constructs a Player.
device_id | The device ID to which sinks shall output. |
sink | The function to be used for building sinks. |
sources | The map of file extensions to functions used for building sources. |
Definition at line 25 of file player.cpp.
|
private |
Determines whether we can broadcast a POS response.
To prevent spewing massive amounts of POS responses, we only send a broadcast if the number of seconds has changed since the last time CanAnnounceTime() was called.
This is not idempotent. A CanAnnounceTime(x) less than one second before a CanAnnounceTime(x) will always be false.
micros | The value of the POS response, in microseconds. |
Definition at line 295 of file player.cpp.
References last_pos.
Referenced by Update().
Response Player::Dump | ( | size_t | id, |
const std::string & | tag | ||
) | const |
Dumps the current player state to the given ID.
id | The ID of the connection to which the Player should route any responses. For broadcasts, use 0. |
tag | The tag of the request calling this command. For unsolicited dumps, use Response::NOREQUEST. |
Definition at line 65 of file player.cpp.
References dead, DumpState(), Response::Failure(), file, Response::FLOAD, MSG_CMD_PLAYER_CLOSING, Audio::NONE, Response::POS, Respond(), and Response::Success().
Referenced by Load(), and Connection::RunCommand().
|
private |
Emits a response for the current audio state to the sink.
id | The ID of the connection to which the Player should route any responses. For broadcasts, use 0. |
tag | The tag of the request calling this command. For unsolicited dumps, use Response::NOREQUEST. |
Definition at line 265 of file player.cpp.
References Audio::AT_END, Response::EJECT, Response::END, file, Audio::NONE, Response::PLAY, Audio::PLAYING, Respond(), Response::STOP, and Audio::STOPPED.
Referenced by Dump(), Eject(), and SetPlaying().
Response Player::Eject | ( | const std::string & | tag | ) |
Ejects the current loaded song, if any.
tag | The tag of the request calling this command. For unsolicited ejects, use Response::NOREQUEST. |
Definition at line 85 of file player.cpp.
References dead, DumpState(), Response::Failure(), file, MSG_CMD_PLAYER_CLOSING, Audio::NONE, and Response::Success().
Referenced by Load(), Quit(), and Connection::RunCommand().
Response Player::End | ( | const std::string & | tag | ) |
Ends a file, stopping and rewinding.
tag | The tag of the request calling this command. For unsolicited ends, use Response::NOREQUEST. |
Definition at line 104 of file player.cpp.
References dead, Response::END, Response::Failure(), MSG_CMD_PLAYER_CLOSING, Response::NOREQUEST, PosRaw(), Respond(), SetPlaying(), and Response::Success().
Referenced by Pos(), Connection::RunCommand(), and Update().
Response Player::Load | ( | const std::string & | tag, |
const std::string & | path | ||
) |
Loads a file.
tag | The tag of the request calling this command. For unsolicited loads, use Response::NOREQUEST. |
path | The absolute path to a track to load. |
Definition at line 122 of file player.cpp.
References dead, Dump(), Eject(), Response::Failure(), file, Response::Invalid(), last_pos, LoadRaw(), Error::Message(), MSG_CMD_PLAYER_CLOSING, MSG_LOAD_EMPTY_PATH, Response::NOREQUEST, and Response::Success().
Referenced by Connection::RunCommand().
|
private |
Loads a file, creating an Audio for it.
path | The path to a file. |
Definition at line 306 of file player.cpp.
References device_id, LoadSource(), and sink.
Referenced by Load().
|
private |
Loads a file, creating an AudioSource.
path | The path to the file to load. |
Definition at line 315 of file player.cpp.
References sources.
Referenced by LoadRaw().
Response Player::Pos | ( | const std::string & | tag, |
const std::string & | pos_str | ||
) |
Seeks to a given position in the current file.
tag | The tag of the request calling this command. For unsolicited seeks, use Response::NOREQUEST. |
pos_str | A string containing a timestamp, in microseconds |
Definition at line 155 of file player.cpp.
References dead, End(), Response::Failure(), Response::Invalid(), Error::Message(), MSG_CMD_NEEDS_LOADED, MSG_CMD_PLAYER_CLOSING, PosParse(), PosRaw(), and Response::Success().
Referenced by Connection::RunCommand().
|
staticprivate |
Parses pos_str as a seek timestamp.
pos_str | The time string to be parsed. |
std::out_of_range | See http://www.cplusplus.com/reference/string/stoull/#exceptions |
std::invalid_argument | See http://www.cplusplus.com/reference/string/stoull/#exceptions |
SeekError | Raised if checks beyond those done by stoull fail. |
Definition at line 223 of file player.cpp.
References MSG_SEEK_INVALID_VALUE.
Referenced by Pos().
|
private |
Performs an actual seek.
This does not do any EOF handling.
tag | The tag of the request calling this command. For unsolicited seeks, use Response::NOREQUEST. |
pos | The new position, in microseconds. |
SeekError | Raised if the seek is out of range (usually EOF). |
Definition at line 252 of file player.cpp.
References file, last_pos, Response::POS, and Respond().
Referenced by End(), and Pos().
Response Player::Quit | ( | const std::string & | tag | ) |
Quits playd.
tag | The tag of the request calling this command. For unsolicited quits, use Response::NOREQUEST. |
Definition at line 210 of file player.cpp.
References dead, Eject(), Response::Failure(), MSG_CMD_PLAYER_CLOSING, and Response::Success().
|
private |
Outputs a response, if there is a ResponseSink attached.
Otherwise, this method does nothing.
id | The ID of the client receiving this response. Use 0 for broadcasts. |
response | The Response to output. |
Definition at line 290 of file player.cpp.
References io, and ResponseSink::Respond().
Referenced by Dump(), DumpState(), End(), PosRaw(), and Update().
void Player::SetIo | ( | ResponseSink & | io | ) |
Sets the ResponseSink to which this Player shall send responses.
This sink shall be the target for WelcomeClient, as well as any responses generated by RunCommand or Update.
io | The response sink (invariably the IO system). |
Definition at line 36 of file player.cpp.
References io.
Response Player::SetPlaying | ( | const std::string & | tag, |
bool | playing | ||
) |
Tells the audio file to start or stop playing.
tag | The tag of the request calling this command. For unsolicited state changes, use Response::NOREQUEST. |
playing | True if playing; false otherwise. |
Definition at line 188 of file player.cpp.
References dead, DumpState(), Response::Failure(), file, Response::Invalid(), Error::Message(), MSG_CMD_PLAYER_CLOSING, Response::NOREQUEST, and Response::Success().
Referenced by End(), and Connection::RunCommand().
bool Player::Update | ( | ) |
Instructs the Player to perform a cycle of work.
This includes decoding the next frame and responding to commands.
Definition at line 41 of file player.cpp.
References Audio::AT_END, CanAnnounceTime(), dead, End(), file, Response::NOREQUEST, Audio::PLAYING, Response::POS, and Respond().
Referenced by IoCore::UpdatePlayer().