diff --git a/client/Makefile b/client/Makefile index 91a9cc61..f06063e0 100644 --- a/client/Makefile +++ b/client/Makefile @@ -58,9 +58,9 @@ endif ifeq ($(TARGET), ANDROID) CXX = $(PROGRAM_PREFIX)clang++ -CXXFLAGS += -pthread -fPIC -DHAS_TREMOR -DHAS_OBOE -I$(NDK_DIR)/usr/local/include +CXXFLAGS += -pthread -fPIC -DHAS_TREMOR -DHAS_OPENSL -DHAS_OBOE -I$(NDK_DIR)/usr/local/include LDFLAGS = -L$(NDK_DIR)/usr/local/lib -pie -lvorbisidec -logg -lopus -lFLAC -lOpenSLES -loboe -latomic -llog -static-libstdc++ -OBJ += player/oboe_player.o +OBJ += player/opensl_player.o player/oboe_player.o else ifeq ($(TARGET), OPENWRT) diff --git a/client/client_settings.hpp b/client/client_settings.hpp new file mode 100644 index 00000000..84200736 --- /dev/null +++ b/client/client_settings.hpp @@ -0,0 +1,57 @@ +/*** + This file is part of snapcast + Copyright (C) 2014-2020 Johannes Pohl + + 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 3 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. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +***/ + +#ifndef CLIENT_SETTINGS_HPP +#define CLIENT_SETTINGS_HPP + +#include +#include + +#include "player/pcm_device.hpp" + + +struct ClientSettings +{ + struct ServerSettings + { + std::string host{""}; + size_t port{1704}; + }; + + struct PlayerSettings + { + std::string player_name{""}; + int latency{0}; + PcmDevice pcm_device; + }; + + struct LoggingSettings + { + bool debug{false}; + std::string debug_logfile{""}; + }; + + size_t instance{1}; + std::string host_id; + + ServerSettings server; + PlayerSettings player; + LoggingSettings logging; +}; + +#endif diff --git a/client/controller.cpp b/client/controller.cpp index 621869c3..c1c98fa5 100644 --- a/client/controller.cpp +++ b/client/controller.cpp @@ -39,9 +39,9 @@ using namespace std; -Controller::Controller(const std::string& hostId, size_t instance, std::shared_ptr meta) - : MessageReceiver(), hostId_(hostId), instance_(instance), active_(false), latency_(0), stream_(nullptr), decoder_(nullptr), player_(nullptr), meta_(meta), - serverSettings_(nullptr), async_exception_(nullptr) +Controller::Controller(const ClientSettings& settings, std::shared_ptr meta) + : MessageReceiver(), settings_(settings), active_(false), stream_(nullptr), decoder_(nullptr), player_(nullptr), meta_(meta), serverSettings_(nullptr), + async_exception_(nullptr) { } @@ -124,19 +124,29 @@ void Controller::onMessageReceived(ClientConnection* /*connection*/, const msg:: LOG(NOTICE) << TAG("state") << "sampleformat: " << sampleFormat_.rate << ":" << sampleFormat_.bits << ":" << sampleFormat_.channels << "\n"; stream_ = make_shared(sampleFormat_); - stream_->setBufferLen(serverSettings_->getBufferMs() - latency_); + stream_->setBufferLen(serverSettings_->getBufferMs() - settings_.player.latency); + const auto& pcm_device = settings_.player.pcm_device; + const auto& player_name = settings_.player.player_name; + player_ = nullptr; #ifdef HAS_ALSA - player_ = make_unique(pcmDevice_, stream_); -#elif HAS_OPENSL - player_ = make_unique(pcmDevice_, stream_); -#elif HAS_OBOE - player_ = make_unique(pcmDevice_, stream_); -#elif HAS_COREAUDIO - player_ = make_unique(pcmDevice_, stream_); -#else - throw SnapException("No audio player support"); + if (!player_ && (player_name.empty() || (player_name == "alsa"))) + player_ = make_unique(pcm_device, stream_); #endif +#ifdef HAS_OBOE + if (!player_ && (player_name.empty() || (player_name == "oboe"))) + player_ = make_unique(pcm_device, stream_); +#endif +#ifdef HAS_OPENSL + if (!player_ && (player_name.empty() || (player_name == "opensl"))) + player_ = make_unique(pcm_device, stream_); +#endif +#ifdef HAS_COREAUDIO + if (!player_ && (player_name.empty() || (player_name == "coreaudio"))) + player_ = make_unique(pcm_device, stream_); +#endif + if (!player_) + throw SnapException("No audio player support"); player_->setVolume(serverSettings_->getVolume() / 100.); player_->setMute(serverSettings_->isMuted()); player_->start(); @@ -171,20 +181,16 @@ bool Controller::sendTimeSyncMessage(long after) } -void Controller::start(const PcmDevice& pcmDevice, const std::string& host, size_t port, int latency) +void Controller::start() { - pcmDevice_ = pcmDevice; - latency_ = latency; - clientConnection_.reset(new ClientConnection(this, host, port)); + clientConnection_.reset(new ClientConnection(this, settings_.server.host, settings_.server.port)); controllerThread_ = thread(&Controller::worker, this); } -void Controller::run(const PcmDevice& pcmDevice, const std::string& host, size_t port, int latency) +void Controller::run() { - pcmDevice_ = pcmDevice; - latency_ = latency; - clientConnection_.reset(new ClientConnection(this, host, port)); + clientConnection_.reset(new ClientConnection(this, settings_.server.host, settings_.server.port)); worker(); // controllerThread_ = thread(&Controller::worker, this); } @@ -210,11 +216,11 @@ void Controller::worker() clientConnection_->start(); string macAddress = clientConnection_->getMacAddress(); - if (hostId_.empty()) - hostId_ = ::getHostId(macAddress); + if (settings_.host_id.empty()) + settings_.host_id = ::getHostId(macAddress); /// Say hello to the server - msg::Hello hello(macAddress, hostId_, instance_); + msg::Hello hello(macAddress, settings_.host_id, settings_.instance); clientConnection_->send(&hello); /// Do initial time sync with the server diff --git a/client/controller.hpp b/client/controller.hpp index e7646ef1..3ca2c791 100644 --- a/client/controller.hpp +++ b/client/controller.hpp @@ -23,19 +23,22 @@ #include "message/message.hpp" #include "message/server_settings.hpp" #include "message/stream_tags.hpp" -#include "player/pcm_device.hpp" #include #include #ifdef HAS_ALSA #include "player/alsa_player.hpp" -#elif HAS_OPENSL +#endif +#ifdef HAS_OPENSL #include "player/opensl_player.hpp" -#elif HAS_OBOE +#endif +#ifdef HAS_OBOE #include "player/oboe_player.hpp" -#elif HAS_COREAUDIO +#endif +#ifdef HAS_COREAUDIO #include "player/coreaudio_player.hpp" #endif #include "client_connection.hpp" +#include "client_settings.hpp" #include "metadata.hpp" #include "stream.hpp" @@ -50,9 +53,9 @@ class Controller : public MessageReceiver { public: - Controller(const std::string& clientId, size_t instance, std::shared_ptr meta); - void start(const PcmDevice& pcmDevice, const std::string& host, size_t port, int latency); - void run(const PcmDevice& pcmDevice, const std::string& host, size_t port, int latency); + Controller(const ClientSettings& settings, std::shared_ptr meta); + void start(); + void run(); void stop(); /// Implementation of MessageReceiver. @@ -66,14 +69,11 @@ public: private: void worker(); bool sendTimeSyncMessage(long after = 1000); - std::string hostId_; + ClientSettings settings_; std::string meta_callback_; - size_t instance_; std::atomic active_; std::thread controllerThread_; SampleFormat sampleFormat_; - PcmDevice pcmDevice_; - int latency_; std::unique_ptr clientConnection_; std::shared_ptr stream_; std::unique_ptr decoder_; diff --git a/client/snapclient.cpp b/client/snapclient.cpp index 7a2ce54c..a95e18a1 100644 --- a/client/snapclient.cpp +++ b/client/snapclient.cpp @@ -30,6 +30,7 @@ #ifdef HAS_DAEMON #include "common/daemon.hpp" #endif +#include "client_settings.hpp" #include "common/aixlog.hpp" #include "common/signal_handler.hpp" #include "common/str_compat.hpp" @@ -66,6 +67,7 @@ PcmDevice getPcmDevice(const std::string& soundcard) #endif PcmDevice pcmDevice; + pcmDevice.name = soundcard; return pcmDevice; } @@ -79,33 +81,31 @@ int main(int argc, char** argv) try { string meta_script(""); - string soundcard("default"); - string host(""); - size_t port(1704); - int latency(0); - size_t instance(1); + ClientSettings settings; + string pcm_device("default"); + OptionParser op("Allowed options"); auto helpSwitch = op.add("", "help", "produce help message"); auto groffSwitch = op.add("", "groff", "produce groff message"); - auto debugOption = op.add, Attribute::hidden>("", "debug", "enable debug logging", ""); + auto debugOption = op.add, Attribute::hidden>("", "debug", "enable debug logging", ""); // TODO: &settings.logging.debug); auto versionSwitch = op.add("v", "version", "show version number"); #if defined(HAS_ALSA) - auto listSwitch = op.add("l", "list", "list pcm devices"); - /*auto soundcardValue =*/op.add>("s", "soundcard", "index or name of the soundcard", "default", &soundcard); + auto listSwitch = op.add("l", "list", "list PCM devices"); + /*auto soundcardValue =*/op.add>("s", "soundcard", "index or name of the pcm device", "default", &pcm_device); #endif auto metaStderr = op.add("e", "mstderr", "send metadata to stderr"); - // auto metaHook = op.add>("m", "mhook", "script to call on meta tags", "", &meta_script); - /*auto hostValue =*/op.add>("h", "host", "server hostname or ip address", "", &host); - /*auto portValue =*/op.add>("p", "port", "server port", 1704, &port); + /*auto hostValue =*/op.add>("h", "host", "server hostname or ip address", "", &settings.server.host); + /*auto portValue =*/op.add>("p", "port", "server port", 1704, &settings.server.port); #ifdef HAS_DAEMON int processPriority(-3); - auto daemonOption = op.add>("d", "daemon", "daemonize, optional process priority [-20..19]", -3, &processPriority); + auto daemonOption = op.add>("d", "daemon", "daemonize, optional process priority [-20..19]", processPriority, &processPriority); auto userValue = op.add>("", "user", "the user[:group] to run snapclient as when daemonized"); #endif - /*auto latencyValue =*/op.add>("", "latency", "latency of the soundcard", 0, &latency); - /*auto instanceValue =*/op.add>("i", "instance", "instance id", 1, &instance); - auto hostIdValue = op.add>("", "hostID", "unique host id", ""); + /*auto latencyValue =*/op.add>("", "latency", "latency of the PCM device", 0, &settings.player.latency); + /*auto instanceValue =*/op.add>("i", "instance", "instance id", 1, &settings.instance); + /*auto hostIdValue =*/op.add>("", "hostID", "unique host id", "", &settings.host_id); + op.add>("", "player", "audio backend", "", &settings.player.player_name); try { @@ -154,10 +154,6 @@ int main(int argc, char** argv) exit(EXIT_SUCCESS); } - if (instance <= 0) - std::invalid_argument("instance id must be >= 1"); - - // XXX: Only one metadata option must be set AixLog::Log::init("snapclient", AixLog::Severity::trace, AixLog::Type::special); @@ -170,7 +166,7 @@ int main(int argc, char** argv) } else { - AixLog::Log::instance().add_logsink(AixLog::Severity::info, AixLog::Type::all, "%Y-%m-%d %H-%M-%S [#severity]"); + AixLog::Log::instance().add_logsink(AixLog::Severity::info, AixLog::Type::all, "%Y-%m-%d %H-%M-%S [#severity] (#tag_func)"); } #ifdef HAS_DAEMON @@ -178,8 +174,8 @@ int main(int argc, char** argv) if (daemonOption->is_set()) { string pidFile = "/var/run/snapclient/pid"; - if (instance != 1) - pidFile += "." + cpt::to_string(instance); + if (settings.instance != 1) + pidFile += "." + cpt::to_string(settings.instance); string user = ""; string group = ""; @@ -205,11 +201,11 @@ int main(int argc, char** argv) } #endif - PcmDevice pcmDevice = getPcmDevice(soundcard); + settings.player.pcm_device = getPcmDevice(pcm_device); #if defined(HAS_ALSA) - if (pcmDevice.idx == -1) + if (settings.player.pcm_device.idx == -1) { - cout << "soundcard \"" << soundcard << "\" not found\n"; + cout << "PCM device \"" << pcm_device << "\" not found\n"; // exit(EXIT_FAILURE); } #endif @@ -226,7 +222,7 @@ int main(int argc, char** argv) controller->stop(); } }); - if (host.empty()) + if (settings.server.host.empty()) { #if defined(HAS_AVAHI) || defined(HAS_BONJOUR) BrowseZeroConf browser; @@ -240,11 +236,11 @@ int main(int argc, char** argv) { if (browser.browse("_snapcast._tcp", avahiResult, 5000)) { - host = avahiResult.ip; - port = avahiResult.port; + settings.server.host = avahiResult.ip; + settings.server.port = avahiResult.port; if (avahiResult.ip_version == IPVersion::IPv6) - host += "%" + cpt::to_string(avahiResult.iface_idx); - LOG(INFO) << "Found server " << host << ":" << port << "\n"; + settings.server.host += "%" + cpt::to_string(avahiResult.iface_idx); + LOG(INFO) << "Found server " << settings.server.host << ":" << settings.server.port << "\n"; break; } } @@ -264,9 +260,9 @@ int main(int argc, char** argv) if (metaStderr) meta.reset(new MetaStderrAdapter); - controller = make_shared(hostIdValue->value(), instance, meta); - LOG(INFO) << "Latency: " << latency << "\n"; - controller->run(pcmDevice, host, port, latency); + controller = make_shared(settings, meta); + LOG(INFO) << "Latency: " << settings.player.latency << "\n"; + controller->run(); // signal_handler.wait(); // controller->stop(); }