Add ClientSettings struct for client settings

Add "player" command line argument to choose the player backend.
Currently there are only for Android different players available.
This commit is contained in:
badaix 2020-02-09 19:57:10 +01:00
parent 83ab1cb260
commit eb3a240120
5 changed files with 129 additions and 70 deletions

View file

@ -58,9 +58,9 @@ endif
ifeq ($(TARGET), ANDROID) ifeq ($(TARGET), ANDROID)
CXX = $(PROGRAM_PREFIX)clang++ 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++ 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) else ifeq ($(TARGET), OPENWRT)

View file

@ -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 <http://www.gnu.org/licenses/>.
***/
#ifndef CLIENT_SETTINGS_HPP
#define CLIENT_SETTINGS_HPP
#include <string>
#include <vector>
#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

View file

@ -39,9 +39,9 @@
using namespace std; using namespace std;
Controller::Controller(const std::string& hostId, size_t instance, std::shared_ptr<MetadataAdapter> meta) Controller::Controller(const ClientSettings& settings, std::shared_ptr<MetadataAdapter> meta)
: MessageReceiver(), hostId_(hostId), instance_(instance), active_(false), latency_(0), stream_(nullptr), decoder_(nullptr), player_(nullptr), meta_(meta), : MessageReceiver(), settings_(settings), active_(false), stream_(nullptr), decoder_(nullptr), player_(nullptr), meta_(meta), serverSettings_(nullptr),
serverSettings_(nullptr), async_exception_(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"; LOG(NOTICE) << TAG("state") << "sampleformat: " << sampleFormat_.rate << ":" << sampleFormat_.bits << ":" << sampleFormat_.channels << "\n";
stream_ = make_shared<Stream>(sampleFormat_); stream_ = make_shared<Stream>(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 #ifdef HAS_ALSA
player_ = make_unique<AlsaPlayer>(pcmDevice_, stream_); if (!player_ && (player_name.empty() || (player_name == "alsa")))
#elif HAS_OPENSL player_ = make_unique<AlsaPlayer>(pcm_device, stream_);
player_ = make_unique<OpenslPlayer>(pcmDevice_, stream_);
#elif HAS_OBOE
player_ = make_unique<OboePlayer>(pcmDevice_, stream_);
#elif HAS_COREAUDIO
player_ = make_unique<CoreAudioPlayer>(pcmDevice_, stream_);
#else
throw SnapException("No audio player support");
#endif #endif
#ifdef HAS_OBOE
if (!player_ && (player_name.empty() || (player_name == "oboe")))
player_ = make_unique<OboePlayer>(pcm_device, stream_);
#endif
#ifdef HAS_OPENSL
if (!player_ && (player_name.empty() || (player_name == "opensl")))
player_ = make_unique<OpenslPlayer>(pcm_device, stream_);
#endif
#ifdef HAS_COREAUDIO
if (!player_ && (player_name.empty() || (player_name == "coreaudio")))
player_ = make_unique<CoreAudioPlayer>(pcm_device, stream_);
#endif
if (!player_)
throw SnapException("No audio player support");
player_->setVolume(serverSettings_->getVolume() / 100.); player_->setVolume(serverSettings_->getVolume() / 100.);
player_->setMute(serverSettings_->isMuted()); player_->setMute(serverSettings_->isMuted());
player_->start(); 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; clientConnection_.reset(new ClientConnection(this, settings_.server.host, settings_.server.port));
latency_ = latency;
clientConnection_.reset(new ClientConnection(this, host, port));
controllerThread_ = thread(&Controller::worker, this); 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; clientConnection_.reset(new ClientConnection(this, settings_.server.host, settings_.server.port));
latency_ = latency;
clientConnection_.reset(new ClientConnection(this, host, port));
worker(); worker();
// controllerThread_ = thread(&Controller::worker, this); // controllerThread_ = thread(&Controller::worker, this);
} }
@ -210,11 +216,11 @@ void Controller::worker()
clientConnection_->start(); clientConnection_->start();
string macAddress = clientConnection_->getMacAddress(); string macAddress = clientConnection_->getMacAddress();
if (hostId_.empty()) if (settings_.host_id.empty())
hostId_ = ::getHostId(macAddress); settings_.host_id = ::getHostId(macAddress);
/// Say hello to the server /// Say hello to the server
msg::Hello hello(macAddress, hostId_, instance_); msg::Hello hello(macAddress, settings_.host_id, settings_.instance);
clientConnection_->send(&hello); clientConnection_->send(&hello);
/// Do initial time sync with the server /// Do initial time sync with the server

View file

@ -23,19 +23,22 @@
#include "message/message.hpp" #include "message/message.hpp"
#include "message/server_settings.hpp" #include "message/server_settings.hpp"
#include "message/stream_tags.hpp" #include "message/stream_tags.hpp"
#include "player/pcm_device.hpp"
#include <atomic> #include <atomic>
#include <thread> #include <thread>
#ifdef HAS_ALSA #ifdef HAS_ALSA
#include "player/alsa_player.hpp" #include "player/alsa_player.hpp"
#elif HAS_OPENSL #endif
#ifdef HAS_OPENSL
#include "player/opensl_player.hpp" #include "player/opensl_player.hpp"
#elif HAS_OBOE #endif
#ifdef HAS_OBOE
#include "player/oboe_player.hpp" #include "player/oboe_player.hpp"
#elif HAS_COREAUDIO #endif
#ifdef HAS_COREAUDIO
#include "player/coreaudio_player.hpp" #include "player/coreaudio_player.hpp"
#endif #endif
#include "client_connection.hpp" #include "client_connection.hpp"
#include "client_settings.hpp"
#include "metadata.hpp" #include "metadata.hpp"
#include "stream.hpp" #include "stream.hpp"
@ -50,9 +53,9 @@
class Controller : public MessageReceiver class Controller : public MessageReceiver
{ {
public: public:
Controller(const std::string& clientId, size_t instance, std::shared_ptr<MetadataAdapter> meta); Controller(const ClientSettings& settings, std::shared_ptr<MetadataAdapter> meta);
void start(const PcmDevice& pcmDevice, const std::string& host, size_t port, int latency); void start();
void run(const PcmDevice& pcmDevice, const std::string& host, size_t port, int latency); void run();
void stop(); void stop();
/// Implementation of MessageReceiver. /// Implementation of MessageReceiver.
@ -66,14 +69,11 @@ public:
private: private:
void worker(); void worker();
bool sendTimeSyncMessage(long after = 1000); bool sendTimeSyncMessage(long after = 1000);
std::string hostId_; ClientSettings settings_;
std::string meta_callback_; std::string meta_callback_;
size_t instance_;
std::atomic<bool> active_; std::atomic<bool> active_;
std::thread controllerThread_; std::thread controllerThread_;
SampleFormat sampleFormat_; SampleFormat sampleFormat_;
PcmDevice pcmDevice_;
int latency_;
std::unique_ptr<ClientConnection> clientConnection_; std::unique_ptr<ClientConnection> clientConnection_;
std::shared_ptr<Stream> stream_; std::shared_ptr<Stream> stream_;
std::unique_ptr<decoder::Decoder> decoder_; std::unique_ptr<decoder::Decoder> decoder_;

View file

@ -30,6 +30,7 @@
#ifdef HAS_DAEMON #ifdef HAS_DAEMON
#include "common/daemon.hpp" #include "common/daemon.hpp"
#endif #endif
#include "client_settings.hpp"
#include "common/aixlog.hpp" #include "common/aixlog.hpp"
#include "common/signal_handler.hpp" #include "common/signal_handler.hpp"
#include "common/str_compat.hpp" #include "common/str_compat.hpp"
@ -66,6 +67,7 @@ PcmDevice getPcmDevice(const std::string& soundcard)
#endif #endif
PcmDevice pcmDevice; PcmDevice pcmDevice;
pcmDevice.name = soundcard;
return pcmDevice; return pcmDevice;
} }
@ -79,33 +81,31 @@ int main(int argc, char** argv)
try try
{ {
string meta_script(""); string meta_script("");
string soundcard("default"); ClientSettings settings;
string host(""); string pcm_device("default");
size_t port(1704);
int latency(0);
size_t instance(1);
OptionParser op("Allowed options"); OptionParser op("Allowed options");
auto helpSwitch = op.add<Switch>("", "help", "produce help message"); auto helpSwitch = op.add<Switch>("", "help", "produce help message");
auto groffSwitch = op.add<Switch, Attribute::hidden>("", "groff", "produce groff message"); auto groffSwitch = op.add<Switch, Attribute::hidden>("", "groff", "produce groff message");
auto debugOption = op.add<Implicit<string>, Attribute::hidden>("", "debug", "enable debug logging", ""); auto debugOption = op.add<Implicit<string>, Attribute::hidden>("", "debug", "enable debug logging", ""); // TODO: &settings.logging.debug);
auto versionSwitch = op.add<Switch>("v", "version", "show version number"); auto versionSwitch = op.add<Switch>("v", "version", "show version number");
#if defined(HAS_ALSA) #if defined(HAS_ALSA)
auto listSwitch = op.add<Switch>("l", "list", "list pcm devices"); auto listSwitch = op.add<Switch>("l", "list", "list PCM devices");
/*auto soundcardValue =*/op.add<Value<string>>("s", "soundcard", "index or name of the soundcard", "default", &soundcard); /*auto soundcardValue =*/op.add<Value<string>>("s", "soundcard", "index or name of the pcm device", "default", &pcm_device);
#endif #endif
auto metaStderr = op.add<Switch>("e", "mstderr", "send metadata to stderr"); auto metaStderr = op.add<Switch>("e", "mstderr", "send metadata to stderr");
// auto metaHook = op.add<Value<string>>("m", "mhook", "script to call on meta tags", "", &meta_script); /*auto hostValue =*/op.add<Value<string>>("h", "host", "server hostname or ip address", "", &settings.server.host);
/*auto hostValue =*/op.add<Value<string>>("h", "host", "server hostname or ip address", "", &host); /*auto portValue =*/op.add<Value<size_t>>("p", "port", "server port", 1704, &settings.server.port);
/*auto portValue =*/op.add<Value<size_t>>("p", "port", "server port", 1704, &port);
#ifdef HAS_DAEMON #ifdef HAS_DAEMON
int processPriority(-3); int processPriority(-3);
auto daemonOption = op.add<Implicit<int>>("d", "daemon", "daemonize, optional process priority [-20..19]", -3, &processPriority); auto daemonOption = op.add<Implicit<int>>("d", "daemon", "daemonize, optional process priority [-20..19]", processPriority, &processPriority);
auto userValue = op.add<Value<string>>("", "user", "the user[:group] to run snapclient as when daemonized"); auto userValue = op.add<Value<string>>("", "user", "the user[:group] to run snapclient as when daemonized");
#endif #endif
/*auto latencyValue =*/op.add<Value<int>>("", "latency", "latency of the soundcard", 0, &latency); /*auto latencyValue =*/op.add<Value<int>>("", "latency", "latency of the PCM device", 0, &settings.player.latency);
/*auto instanceValue =*/op.add<Value<size_t>>("i", "instance", "instance id", 1, &instance); /*auto instanceValue =*/op.add<Value<size_t>>("i", "instance", "instance id", 1, &settings.instance);
auto hostIdValue = op.add<Value<string>>("", "hostID", "unique host id", ""); /*auto hostIdValue =*/op.add<Value<string>>("", "hostID", "unique host id", "", &settings.host_id);
op.add<Value<string>>("", "player", "audio backend", "", &settings.player.player_name);
try try
{ {
@ -154,10 +154,6 @@ int main(int argc, char** argv)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
if (instance <= 0)
std::invalid_argument("instance id must be >= 1");
// XXX: Only one metadata option must be set // XXX: Only one metadata option must be set
AixLog::Log::init<AixLog::SinkNative>("snapclient", AixLog::Severity::trace, AixLog::Type::special); AixLog::Log::init<AixLog::SinkNative>("snapclient", AixLog::Severity::trace, AixLog::Type::special);
@ -170,7 +166,7 @@ int main(int argc, char** argv)
} }
else else
{ {
AixLog::Log::instance().add_logsink<AixLog::SinkCout>(AixLog::Severity::info, AixLog::Type::all, "%Y-%m-%d %H-%M-%S [#severity]"); AixLog::Log::instance().add_logsink<AixLog::SinkCout>(AixLog::Severity::info, AixLog::Type::all, "%Y-%m-%d %H-%M-%S [#severity] (#tag_func)");
} }
#ifdef HAS_DAEMON #ifdef HAS_DAEMON
@ -178,8 +174,8 @@ int main(int argc, char** argv)
if (daemonOption->is_set()) if (daemonOption->is_set())
{ {
string pidFile = "/var/run/snapclient/pid"; string pidFile = "/var/run/snapclient/pid";
if (instance != 1) if (settings.instance != 1)
pidFile += "." + cpt::to_string(instance); pidFile += "." + cpt::to_string(settings.instance);
string user = ""; string user = "";
string group = ""; string group = "";
@ -205,11 +201,11 @@ int main(int argc, char** argv)
} }
#endif #endif
PcmDevice pcmDevice = getPcmDevice(soundcard); settings.player.pcm_device = getPcmDevice(pcm_device);
#if defined(HAS_ALSA) #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); // exit(EXIT_FAILURE);
} }
#endif #endif
@ -226,7 +222,7 @@ int main(int argc, char** argv)
controller->stop(); controller->stop();
} }
}); });
if (host.empty()) if (settings.server.host.empty())
{ {
#if defined(HAS_AVAHI) || defined(HAS_BONJOUR) #if defined(HAS_AVAHI) || defined(HAS_BONJOUR)
BrowseZeroConf browser; BrowseZeroConf browser;
@ -240,11 +236,11 @@ int main(int argc, char** argv)
{ {
if (browser.browse("_snapcast._tcp", avahiResult, 5000)) if (browser.browse("_snapcast._tcp", avahiResult, 5000))
{ {
host = avahiResult.ip; settings.server.host = avahiResult.ip;
port = avahiResult.port; settings.server.port = avahiResult.port;
if (avahiResult.ip_version == IPVersion::IPv6) if (avahiResult.ip_version == IPVersion::IPv6)
host += "%" + cpt::to_string(avahiResult.iface_idx); settings.server.host += "%" + cpt::to_string(avahiResult.iface_idx);
LOG(INFO) << "Found server " << host << ":" << port << "\n"; LOG(INFO) << "Found server " << settings.server.host << ":" << settings.server.port << "\n";
break; break;
} }
} }
@ -264,9 +260,9 @@ int main(int argc, char** argv)
if (metaStderr) if (metaStderr)
meta.reset(new MetaStderrAdapter); meta.reset(new MetaStderrAdapter);
controller = make_shared<Controller>(hostIdValue->value(), instance, meta); controller = make_shared<Controller>(settings, meta);
LOG(INFO) << "Latency: " << latency << "\n"; LOG(INFO) << "Latency: " << settings.player.latency << "\n";
controller->run(pcmDevice, host, port, latency); controller->run();
// signal_handler.wait(); // signal_handler.wait();
// controller->stop(); // controller->stop();
} }