Make callbacks rvalues

This commit is contained in:
badaix 2025-02-09 21:02:22 +01:00 committed by Johannes Pohl
parent 164b3abf40
commit fa83fc155d
5 changed files with 65 additions and 64 deletions

View file

@ -76,7 +76,7 @@ jobs:
mkdir -p build/doxygen mkdir -p build/doxygen
doxygen 2>&1 | tee build/doxygen.log doxygen 2>&1 | tee build/doxygen.log
WARNINGS=$(cat build/doxygen.log | sort | uniq | grep -e ": warning: " | wc -l) WARNINGS=$(cat build/doxygen.log | sort | uniq | grep -e ": warning: " | wc -l)
MAX_ALLOWED=535 MAX_ALLOWED=437
echo "Doxygen finished with $WARNINGS warnings, max allowed: $MAX_ALLOWED" echo "Doxygen finished with $WARNINGS warnings, max allowed: $MAX_ALLOWED"
if [ "$WARNINGS" -gt "$MAX_ALLOWED" ]; then exit $WARNINGS; else exit 0; fi; if [ "$WARNINGS" -gt "$MAX_ALLOWED" ]; then exit $WARNINGS; else exit 0; fi;

View file

@ -1,6 +1,6 @@
/*** /***
This file is part of snapcast This file is part of snapcast
Copyright (C) 2014-2024 Johannes Pohl Copyright (C) 2014-2025 Johannes Pohl
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -105,7 +105,7 @@ void MetaStream::onStateChanged(const PcmStream* pcmStream, ReaderState state)
// if (active_stream_->getProperties().playback_status == PlaybackStatus::kPaused) // if (active_stream_->getProperties().playback_status == PlaybackStatus::kPaused)
// return; // return;
auto switch_stream = [this](std::shared_ptr<PcmStream> new_stream) auto switch_stream = [this](const std::shared_ptr<PcmStream>& new_stream)
{ {
if (new_stream == active_stream_) if (new_stream == active_stream_)
return; return;
@ -209,31 +209,31 @@ void MetaStream::onResync(const PcmStream* pcmStream, double ms)
// Setter for properties // Setter for properties
void MetaStream::setShuffle(bool shuffle, ResultHandler handler) void MetaStream::setShuffle(bool shuffle, ResultHandler&& handler)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_); std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->setShuffle(shuffle, std::move(handler)); active_stream_->setShuffle(shuffle, std::move(handler));
} }
void MetaStream::setLoopStatus(LoopStatus status, ResultHandler handler) void MetaStream::setLoopStatus(LoopStatus status, ResultHandler&& handler)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_); std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->setLoopStatus(status, std::move(handler)); active_stream_->setLoopStatus(status, std::move(handler));
} }
void MetaStream::setVolume(uint16_t volume, ResultHandler handler) void MetaStream::setVolume(uint16_t volume, ResultHandler&& handler)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_); std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->setVolume(volume, std::move(handler)); active_stream_->setVolume(volume, std::move(handler));
} }
void MetaStream::setMute(bool mute, ResultHandler handler) void MetaStream::setMute(bool mute, ResultHandler&& handler)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_); std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->setMute(mute, std::move(handler)); active_stream_->setMute(mute, std::move(handler));
} }
void MetaStream::setRate(float rate, ResultHandler handler) void MetaStream::setRate(float rate, ResultHandler&& handler)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_); std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->setRate(rate, std::move(handler)); active_stream_->setRate(rate, std::move(handler));
@ -241,53 +241,53 @@ void MetaStream::setRate(float rate, ResultHandler handler)
// Control commands // Control commands
void MetaStream::setPosition(std::chrono::milliseconds position, ResultHandler handler) void MetaStream::setPosition(std::chrono::milliseconds position, ResultHandler&& handler)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_); std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->setPosition(position, std::move(handler)); active_stream_->setPosition(position, std::move(handler));
} }
void MetaStream::seek(std::chrono::milliseconds offset, ResultHandler handler) void MetaStream::seek(std::chrono::milliseconds offset, ResultHandler&& handler)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_); std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->seek(offset, std::move(handler)); active_stream_->seek(offset, std::move(handler));
} }
void MetaStream::next(ResultHandler handler) void MetaStream::next(ResultHandler&& handler)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_); std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->next(std::move(handler)); active_stream_->next(std::move(handler));
} }
void MetaStream::previous(ResultHandler handler) void MetaStream::previous(ResultHandler&& handler)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_); std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->previous(std::move(handler)); active_stream_->previous(std::move(handler));
} }
void MetaStream::pause(ResultHandler handler) void MetaStream::pause(ResultHandler&& handler)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_); std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->pause(std::move(handler)); active_stream_->pause(std::move(handler));
} }
void MetaStream::playPause(ResultHandler handler) void MetaStream::playPause(ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "PlayPause\n"; LOG(DEBUG, LOG_TAG) << "PlayPause\n";
std::lock_guard<std::recursive_mutex> lock(mutex_); std::lock_guard<std::recursive_mutex> lock(mutex_);
if (active_stream_->getState() == ReaderState::kIdle) if (active_stream_->getState() == ReaderState::kIdle)
play(handler); play(std::move(handler));
else else
active_stream_->playPause(std::move(handler)); active_stream_->playPause(std::move(handler));
} }
void MetaStream::stop(ResultHandler handler) void MetaStream::stop(ResultHandler&& handler)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_); std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->stop(std::move(handler)); active_stream_->stop(std::move(handler));
} }
void MetaStream::play(ResultHandler handler) void MetaStream::play(ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "Play\n"; LOG(DEBUG, LOG_TAG) << "Play\n";
std::lock_guard<std::recursive_mutex> lock(mutex_); std::lock_guard<std::recursive_mutex> lock(mutex_);

View file

@ -1,6 +1,6 @@
/*** /***
This file is part of snapcast This file is part of snapcast
Copyright (C) 2014-2024 Johannes Pohl Copyright (C) 2014-2025 Johannes Pohl
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -41,30 +41,31 @@ namespace streamreader
class MetaStream : public PcmStream, public PcmStream::Listener class MetaStream : public PcmStream, public PcmStream::Listener
{ {
public: public:
/// ctor. Encoded PCM data is passed to the PcmStream::Listener /// c'tor. Encoded PCM data is passed to the PcmStream::Listener
MetaStream(PcmStream::Listener* pcmListener, const std::vector<std::shared_ptr<PcmStream>>& streams, boost::asio::io_context& ioc, MetaStream(PcmStream::Listener* pcmListener, const std::vector<std::shared_ptr<PcmStream>>& streams, boost::asio::io_context& ioc,
const ServerSettings& server_settings, const StreamUri& uri); const ServerSettings& server_settings, const StreamUri& uri);
/// d'tor
virtual ~MetaStream(); virtual ~MetaStream();
void start() override; void start() override;
void stop() override; void stop() override;
// Setter for properties // Setter for properties
void setShuffle(bool shuffle, ResultHandler handler) override; void setShuffle(bool shuffle, ResultHandler&& handler) override;
void setLoopStatus(LoopStatus status, ResultHandler handler) override; void setLoopStatus(LoopStatus status, ResultHandler&& handler) override;
void setVolume(uint16_t volume, ResultHandler handler) override; void setVolume(uint16_t volume, ResultHandler&& handler) override;
void setMute(bool mute, ResultHandler handler) override; void setMute(bool mute, ResultHandler&& handler) override;
void setRate(float rate, ResultHandler handler) override; void setRate(float rate, ResultHandler&& handler) override;
// Control commands // Control commands
void setPosition(std::chrono::milliseconds position, ResultHandler handler) override; void setPosition(std::chrono::milliseconds position, ResultHandler&& handler) override;
void seek(std::chrono::milliseconds offset, ResultHandler handler) override; void seek(std::chrono::milliseconds offset, ResultHandler&& handler) override;
void next(ResultHandler handler) override; void next(ResultHandler&& handler) override;
void previous(ResultHandler handler) override; void previous(ResultHandler&& handler) override;
void pause(ResultHandler handler) override; void pause(ResultHandler&& handler) override;
void playPause(ResultHandler handler) override; void playPause(ResultHandler&& handler) override;
void stop(ResultHandler handler) override; void stop(ResultHandler&& handler) override;
void play(ResultHandler handler) override; void play(ResultHandler&& handler) override;
protected: protected:
/// Implementation of PcmStream::Listener /// Implementation of PcmStream::Listener
@ -74,7 +75,7 @@ protected:
void onChunkEncoded(const PcmStream* pcmStream, std::shared_ptr<msg::PcmChunk> chunk, double duration) override; void onChunkEncoded(const PcmStream* pcmStream, std::shared_ptr<msg::PcmChunk> chunk, double duration) override;
void onResync(const PcmStream* pcmStream, double ms) override; void onResync(const PcmStream* pcmStream, double ms) override;
protected: private:
std::vector<std::shared_ptr<PcmStream>> streams_; std::vector<std::shared_ptr<PcmStream>> streams_;
std::recursive_mutex active_mutex_; std::recursive_mutex active_mutex_;
std::shared_ptr<PcmStream> active_stream_; std::shared_ptr<PcmStream> active_stream_;

View file

@ -228,8 +228,8 @@ void PcmStream::start()
{ {
LOG(DEBUG, LOG_TAG) << "Start: " << name_ << ", type: " << uri_.scheme << ", sampleformat: " << sampleFormat_.toString() << ", codec: " << getCodec() LOG(DEBUG, LOG_TAG) << "Start: " << name_ << ", type: " << uri_.scheme << ", sampleformat: " << sampleFormat_.toString() << ", codec: " << getCodec()
<< "\n"; << "\n";
encoder_->init([this, self = shared_from_this()](const encoder::Encoder& encoder, std::shared_ptr<msg::PcmChunk> chunk, double duration) encoder_->init([this, self = shared_from_this()](const encoder::Encoder& encoder, const std::shared_ptr<msg::PcmChunk>& chunk, double duration)
{ chunkEncoded(encoder, std::move(chunk), duration); }, sampleFormat_); { chunkEncoded(encoder, chunk, duration); }, sampleFormat_);
if (stream_ctrl_) if (stream_ctrl_)
{ {
@ -378,7 +378,7 @@ const Properties& PcmStream::getProperties() const
} }
void PcmStream::setShuffle(bool shuffle, ResultHandler handler) void PcmStream::setShuffle(bool shuffle, ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "setShuffle: " << shuffle << "\n"; LOG(DEBUG, LOG_TAG) << "setShuffle: " << shuffle << "\n";
if (!properties_.can_control) if (!properties_.can_control)
@ -387,7 +387,7 @@ void PcmStream::setShuffle(bool shuffle, ResultHandler handler)
} }
void PcmStream::setLoopStatus(LoopStatus status, ResultHandler handler) void PcmStream::setLoopStatus(LoopStatus status, ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "setLoopStatus: " << status << "\n"; LOG(DEBUG, LOG_TAG) << "setLoopStatus: " << status << "\n";
if (!properties_.can_control) if (!properties_.can_control)
@ -396,7 +396,7 @@ void PcmStream::setLoopStatus(LoopStatus status, ResultHandler handler)
} }
void PcmStream::setVolume(uint16_t volume, ResultHandler handler) void PcmStream::setVolume(uint16_t volume, ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "setVolume: " << volume << "\n"; LOG(DEBUG, LOG_TAG) << "setVolume: " << volume << "\n";
if (!properties_.can_control) if (!properties_.can_control)
@ -405,7 +405,7 @@ void PcmStream::setVolume(uint16_t volume, ResultHandler handler)
} }
void PcmStream::setMute(bool mute, ResultHandler handler) void PcmStream::setMute(bool mute, ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "setMute: " << mute << "\n"; LOG(DEBUG, LOG_TAG) << "setMute: " << mute << "\n";
if (!properties_.can_control) if (!properties_.can_control)
@ -414,7 +414,7 @@ void PcmStream::setMute(bool mute, ResultHandler handler)
} }
void PcmStream::setRate(float rate, ResultHandler handler) void PcmStream::setRate(float rate, ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "setRate: " << rate << "\n"; LOG(DEBUG, LOG_TAG) << "setRate: " << rate << "\n";
if (!properties_.can_control) if (!properties_.can_control)
@ -423,7 +423,7 @@ void PcmStream::setRate(float rate, ResultHandler handler)
} }
void PcmStream::setPosition(std::chrono::milliseconds position, ResultHandler handler) void PcmStream::setPosition(std::chrono::milliseconds position, ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "setPosition\n"; LOG(DEBUG, LOG_TAG) << "setPosition\n";
if (!properties_.can_seek) if (!properties_.can_seek)
@ -437,7 +437,7 @@ void PcmStream::setPosition(std::chrono::milliseconds position, ResultHandler ha
} }
void PcmStream::seek(std::chrono::milliseconds offset, ResultHandler handler) void PcmStream::seek(std::chrono::milliseconds offset, ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "seek\n"; LOG(DEBUG, LOG_TAG) << "seek\n";
if (!properties_.can_seek) if (!properties_.can_seek)
@ -451,7 +451,7 @@ void PcmStream::seek(std::chrono::milliseconds offset, ResultHandler handler)
} }
void PcmStream::next(ResultHandler handler) void PcmStream::next(ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "next\n"; LOG(DEBUG, LOG_TAG) << "next\n";
if (!properties_.can_go_next) if (!properties_.can_go_next)
@ -460,7 +460,7 @@ void PcmStream::next(ResultHandler handler)
} }
void PcmStream::previous(ResultHandler handler) void PcmStream::previous(ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "previous\n"; LOG(DEBUG, LOG_TAG) << "previous\n";
if (!properties_.can_go_previous) if (!properties_.can_go_previous)
@ -469,7 +469,7 @@ void PcmStream::previous(ResultHandler handler)
} }
void PcmStream::pause(ResultHandler handler) void PcmStream::pause(ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "pause\n"; LOG(DEBUG, LOG_TAG) << "pause\n";
if (!properties_.can_pause) if (!properties_.can_pause)
@ -478,7 +478,7 @@ void PcmStream::pause(ResultHandler handler)
} }
void PcmStream::playPause(ResultHandler handler) void PcmStream::playPause(ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "playPause\n"; LOG(DEBUG, LOG_TAG) << "playPause\n";
if (!properties_.can_pause) if (!properties_.can_pause)
@ -487,7 +487,7 @@ void PcmStream::playPause(ResultHandler handler)
} }
void PcmStream::stop(ResultHandler handler) void PcmStream::stop(ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "stop\n"; LOG(DEBUG, LOG_TAG) << "stop\n";
if (!properties_.can_control) if (!properties_.can_control)
@ -496,7 +496,7 @@ void PcmStream::stop(ResultHandler handler)
} }
void PcmStream::play(ResultHandler handler) void PcmStream::play(ResultHandler&& handler)
{ {
LOG(DEBUG, LOG_TAG) << "play\n"; LOG(DEBUG, LOG_TAG) << "play\n";
if (!properties_.can_play) if (!properties_.can_play)
@ -505,13 +505,13 @@ void PcmStream::play(ResultHandler handler)
} }
void PcmStream::sendRequest(const std::string& method, const jsonrpcpp::Parameter& params, ResultHandler handler) void PcmStream::sendRequest(const std::string& method, const jsonrpcpp::Parameter& params, ResultHandler&& handler)
{ {
if (!stream_ctrl_) if (!stream_ctrl_)
return handler({ControlErrc::can_not_control}); return handler({ControlErrc::can_not_control});
jsonrpcpp::Request req(++req_id_, method, params); jsonrpcpp::Request req(++req_id_, method, params);
stream_ctrl_->command(req, [handler](const jsonrpcpp::Response& response) stream_ctrl_->command(req, [handler = std::move(handler)](const jsonrpcpp::Response& response)
{ {
if (response.error().code() != 0) if (response.error().code() != 0)
handler({static_cast<ControlErrc>(response.error().code()), response.error().data()}); handler({static_cast<ControlErrc>(response.error().code()), response.error().data()});

View file

@ -152,33 +152,33 @@ public:
// Setter for properties // Setter for properties
/// Set shuffle property /// Set shuffle property
virtual void setShuffle(bool shuffle, ResultHandler handler); virtual void setShuffle(bool shuffle, ResultHandler&& handler);
/// Set loop property /// Set loop property
virtual void setLoopStatus(LoopStatus status, ResultHandler handler); virtual void setLoopStatus(LoopStatus status, ResultHandler&& handler);
/// Set volume property /// Set volume property
virtual void setVolume(uint16_t volume, ResultHandler handler); virtual void setVolume(uint16_t volume, ResultHandler&& handler);
/// Set mute property /// Set mute property
virtual void setMute(bool mute, ResultHandler handler); virtual void setMute(bool mute, ResultHandler&& handler);
/// Set playback rate property /// Set playback rate property
virtual void setRate(float rate, ResultHandler handler); virtual void setRate(float rate, ResultHandler&& handler);
// Control commands // Control commands
/// Set position /// Set position
virtual void setPosition(std::chrono::milliseconds position, ResultHandler handler); virtual void setPosition(std::chrono::milliseconds position, ResultHandler&& handler);
/// Seek /// Seek
virtual void seek(std::chrono::milliseconds offset, ResultHandler handler); virtual void seek(std::chrono::milliseconds offset, ResultHandler&& handler);
/// Play next /// Play next
virtual void next(ResultHandler handler); virtual void next(ResultHandler&& handler);
/// Play previous /// Play previous
virtual void previous(ResultHandler handler); virtual void previous(ResultHandler&& handler);
/// Pause /// Pause
virtual void pause(ResultHandler handler); virtual void pause(ResultHandler&& handler);
/// Toggle play/pause /// Toggle play/pause
virtual void playPause(ResultHandler handler); virtual void playPause(ResultHandler&& handler);
/// Stop /// Stop
virtual void stop(ResultHandler handler); virtual void stop(ResultHandler&& handler);
/// Play /// Play
virtual void play(ResultHandler handler); virtual void play(ResultHandler&& handler);
/// Get stream reader state (idle/playing) /// Get stream reader state (idle/playing)
virtual ReaderState getState() const; virtual ReaderState getState() const;
@ -218,7 +218,7 @@ protected:
/// Log message received from control script via stderr /// Log message received from control script via stderr
void onControlLog(std::string line); void onControlLog(std::string line);
/// Send request to stream control script /// Send request to stream control script
void sendRequest(const std::string& method, const jsonrpcpp::Parameter& params, ResultHandler handler); void sendRequest(const std::string& method, const jsonrpcpp::Parameter& params, ResultHandler&& handler);
/// Executor for synchronous IO /// Executor for synchronous IO
boost::asio::strand<boost::asio::any_io_executor> strand_; boost::asio::strand<boost::asio::any_io_executor> strand_;