Skip to content

Commit

Permalink
Merge branch 'master' of github.com:BlumAmir/wavplayeralsa
Browse files Browse the repository at this point in the history
  • Loading branch information
blumamir committed Apr 15, 2020
2 parents c7dc436 + d982fd7 commit 4de815b
Show file tree
Hide file tree
Showing 6 changed files with 574 additions and 432 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ include_directories(thirdparty/mqtt_cpp)

set (SOURCES
src/wavplayeralsa.cpp
src/alsa_frames_transfer.cc
src/web_sockets_api.cc
src/http_api.cc
src/mqtt_api.cc
src/audio_files_manager.cc
src/current_song_controller.cc
src/services/alsa_service.cc
)

set(CMAKE_BUILD_TYPE Debug)
Expand Down
131 changes: 66 additions & 65 deletions src/current_song_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ namespace wavplayeralsa
{

CurrentSongController::CurrentSongController(
boost::asio::io_service &io_service,
MqttApi *mqtt_service,
WebSocketsApi *ws_service,
AlsaFramesTransfer *alsa_service)
:
ios_(io_service),
mqtt_service_(mqtt_service),
ws_service_(ws_service),
alsa_service_(alsa_service),
play_seq_id_(0),
throttle_timer_(io_service)
boost::asio::io_service &io_service,
MqttApi *mqtt_service,
WebSocketsApi *ws_service,
AlsaPlaybackServiceFactory *alsa_playback_service_factory
) :
ios_(io_service),
mqtt_service_(mqtt_service),
ws_service_(ws_service),
alsa_playback_service_factory_(alsa_playback_service_factory),
play_seq_id_(0),
throttle_timer_(io_service)
{

}
Expand Down Expand Up @@ -61,87 +61,88 @@ namespace wavplayeralsa
std::stringstream &out_msg,
uint32_t *play_seq_id)
{
bool prev_file_was_playing = false;
std::string prev_file_id;

if(alsa_service_ != nullptr) {
prev_file_id = alsa_service_->GetFileId();
prev_file_was_playing = alsa_service_->Stop();
delete alsa_service_;
alsa_service_ = nullptr;
}

// create a new unique id for this play
uint32_t new_play_seq_id = play_seq_id_ + 1;
play_seq_id_ = new_play_seq_id;
if(play_seq_id != nullptr)
{
*play_seq_id = play_seq_id_;
}

if(file_id == alsa_service_->GetFileId()) {
boost::filesystem::path songPathInWavDir(file_id);
boost::filesystem::path songFullPath = wav_dir_ / songPathInWavDir;
std::string canonicalFullPath;
try {
canonicalFullPath = boost::filesystem::canonical(songFullPath).string();
alsa_service_ = alsa_playback_service_factory_->CreateAlsaPlaybackService(
canonicalFullPath,
file_id,
new_play_seq_id
);
}
catch(const std::runtime_error &e) {
out_msg << "failed loading new audio file '" << file_id << "'. currently no audio file is loaded in the player and it is not playing. " <<
"reason for failure: " << e.what();
return false;
}

if(file_id == prev_file_id) {
out_msg << "changed position of the current file '" << file_id << "'. new position in ms is: " << start_offset_ms << std::endl;
}
else {

// create the canonical full path of the file to play
boost::filesystem::path songPathInWavDir(file_id);
boost::filesystem::path songFullPath = wav_dir_ / songPathInWavDir;
std::string canonicalFullPath;
try {
canonicalFullPath = boost::filesystem::canonical(songFullPath).string();
static const int SECONDS_PER_HOUR = (60 * 60);
uint64_t start_offset_sec = start_offset_ms / 1000;
uint64_t hours = start_offset_sec / SECONDS_PER_HOUR;
start_offset_sec = start_offset_sec - hours * SECONDS_PER_HOUR;
uint64_t minutes = start_offset_sec / 60;
uint64_t seconds = start_offset_sec % 60;
if(prev_file_was_playing && !prev_file_id.empty()) {
out_msg << "audio file successfully changed from '" << prev_file_id << "' to '" << file_id << "' and will be played ";
}
catch (const std::exception &e) {
out_msg << "loading new audio file '" << file_id << "' failed. error: " << e.what();
return false;
}

try {
const std::string prev_file = alsa_service_->GetFileId();
bool prev_file_was_playing = alsa_service_->LoadNewFile(canonicalFullPath, file_id);

// message printing
const int SECONDS_PER_HOUR = (60 * 60);
uint64_t start_offset_sec = start_offset_ms / 1000;
uint64_t hours = start_offset_sec / SECONDS_PER_HOUR;
start_offset_sec = start_offset_sec - hours * SECONDS_PER_HOUR;
uint64_t minutes = start_offset_sec / 60;
uint64_t seconds = start_offset_sec % 60;
if(prev_file_was_playing && !prev_file.empty()) {
out_msg << "audio file successfully changed from '" << prev_file << "' to '" << file_id << "' and will be played ";
}
else {
out_msg << "will play audio file '" << file_id << "' ";
}
out_msg << "starting at position " << start_offset_ms << " ms " <<
"(" << hours << ":" <<
std::setfill('0') << std::setw(2) << minutes << ":" <<
std::setfill('0') << std::setw(2) << seconds << ")";
}
catch(const std::runtime_error &e) {
out_msg << "loading new audio file '" << file_id << "' failed. currently no audio file is loaded in the player and it is not playing. " <<
"reason for failure: " << e.what();
return false;
else {
out_msg << "will play audio file '" << file_id << "' ";
}
out_msg << "starting at position " << start_offset_ms << " ms " <<
"(" << hours << ":" <<
std::setfill('0') << std::setw(2) << minutes << ":" <<
std::setfill('0') << std::setw(2) << seconds << ")";
}

uint32_t new_play_seq_id = play_seq_id_ + 1;
try {
alsa_service_->StartPlay(start_offset_ms, new_play_seq_id);
alsa_service_->Play(start_offset_ms);
}
catch(const std::runtime_error &e) {
out_msg << "playing new audio file '" << file_id << "' failed. currently player is not playing. " <<
"reason for failure: " << e.what();
return false;
}

play_seq_id_ = new_play_seq_id;
if(play_seq_id != nullptr)
{
*play_seq_id = play_seq_id_;
}

return true;
}

bool CurrentSongController::StopPlayRequest(
std::stringstream &out_msg,
uint32_t *play_seq_id)
{

bool was_playing = false;
try {
std::string current_file_id;
if(alsa_service_ != nullptr) {
current_file_id = alsa_service_->GetFileId();
was_playing = alsa_service_->Stop();
}
catch(const std::runtime_error &e) {
out_msg << "Unable to stop current audio file successfully, error: " << e.what();
return false;
delete alsa_service_;
alsa_service_ = nullptr;
}

const std::string &current_file_id = alsa_service_->GetFileId();
if(current_file_id.empty() || !was_playing) {
out_msg << "no audio file is being played, so stop had no effect";
}
Expand Down
7 changes: 4 additions & 3 deletions src/current_song_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "player_actions_ifc.h"
#include "mqtt_api.h"
#include "web_sockets_api.h"
#include "alsa_frames_transfer.h"
#include "services/alsa_service.h"

using json = nlohmann::json;

Expand All @@ -25,7 +25,7 @@ namespace wavplayeralsa {
CurrentSongController(boost::asio::io_service &io_service,
MqttApi *mqtt_service,
WebSocketsApi *ws_service,
AlsaFramesTransfer *alsa_service);
AlsaPlaybackServiceFactory *alsa_playback_service_factory);

void Initialize(const std::string &player_uuid, const std::string &wav_dir);

Expand Down Expand Up @@ -55,7 +55,8 @@ namespace wavplayeralsa {
boost::asio::io_service &ios_;
MqttApi *mqtt_service_;
WebSocketsApi *ws_service_;
AlsaFramesTransfer *alsa_service_ = nullptr;
AlsaPlaybackServiceFactory *alsa_playback_service_factory_;
IAlsaPlaybackService *alsa_service_ = nullptr;

private:
// static config
Expand Down
Loading

0 comments on commit 4de815b

Please sign in to comment.