2016-06-22 11:15:49 +02:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2016-06-22 11:15:49 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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 2 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, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Observer.hxx"
|
2018-01-01 18:48:34 +01:00
|
|
|
#include "Filter.hxx"
|
|
|
|
#include "Prepared.hxx"
|
2016-06-22 11:15:49 +02:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2016-06-22 11:15:49 +02:00
|
|
|
|
|
|
|
class FilterObserver::PreparedProxy final : public PreparedFilter {
|
|
|
|
FilterObserver &observer;
|
|
|
|
|
2017-12-27 09:17:15 +01:00
|
|
|
std::unique_ptr<PreparedFilter> prepared_filter;
|
2016-06-22 11:15:49 +02:00
|
|
|
Proxy *child = nullptr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PreparedProxy(FilterObserver &_observer,
|
2019-08-26 21:04:35 +02:00
|
|
|
std::unique_ptr<PreparedFilter> _prepared_filter) noexcept
|
2016-06-22 11:15:49 +02:00
|
|
|
:observer(_observer),
|
2017-12-27 09:17:15 +01:00
|
|
|
prepared_filter(std::move(_prepared_filter)) {}
|
2016-06-22 11:15:49 +02:00
|
|
|
|
2020-02-01 04:37:53 +01:00
|
|
|
~PreparedProxy() noexcept override {
|
2016-06-22 11:15:49 +02:00
|
|
|
assert(child == nullptr);
|
|
|
|
assert(observer.proxy == this);
|
|
|
|
|
|
|
|
observer.proxy = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-05-31 07:46:50 +02:00
|
|
|
PreparedProxy(const PreparedProxy &) = delete;
|
|
|
|
PreparedProxy &operator=(const PreparedProxy &) = delete;
|
|
|
|
|
2020-03-12 20:56:11 +01:00
|
|
|
void Clear([[maybe_unused]] Proxy *_child) noexcept {
|
2016-06-22 11:15:49 +02:00
|
|
|
assert(child == _child);
|
|
|
|
child = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-08-26 21:04:35 +02:00
|
|
|
Filter *Get() noexcept;
|
2016-06-22 11:15:49 +02:00
|
|
|
|
2017-12-27 11:42:14 +01:00
|
|
|
std::unique_ptr<Filter> Open(AudioFormat &af) override;
|
2016-06-22 11:15:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class FilterObserver::Proxy final : public Filter {
|
|
|
|
PreparedProxy &parent;
|
|
|
|
|
2017-12-27 11:42:14 +01:00
|
|
|
std::unique_ptr<Filter> filter;
|
2016-06-22 11:15:49 +02:00
|
|
|
|
|
|
|
public:
|
2019-08-26 21:04:35 +02:00
|
|
|
Proxy(PreparedProxy &_parent, std::unique_ptr<Filter> _filter) noexcept
|
2016-06-22 11:15:49 +02:00
|
|
|
:Filter(_filter->GetOutAudioFormat()),
|
2017-12-27 11:42:14 +01:00
|
|
|
parent(_parent), filter(std::move(_filter)) {}
|
2016-06-22 11:15:49 +02:00
|
|
|
|
2020-02-01 04:37:53 +01:00
|
|
|
~Proxy() noexcept override {
|
2016-06-22 11:15:49 +02:00
|
|
|
parent.Clear(this);
|
|
|
|
}
|
|
|
|
|
2021-05-31 07:46:50 +02:00
|
|
|
Proxy(const Proxy &) = delete;
|
|
|
|
Proxy &operator=(const Proxy &) = delete;
|
|
|
|
|
2019-08-26 21:04:35 +02:00
|
|
|
Filter *Get() noexcept {
|
2017-12-27 11:42:14 +01:00
|
|
|
return filter.get();
|
2016-06-22 11:15:49 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 10:10:17 +01:00
|
|
|
void Reset() noexcept override {
|
2018-01-02 22:13:14 +01:00
|
|
|
filter->Reset();
|
|
|
|
}
|
|
|
|
|
2022-07-04 18:12:12 +02:00
|
|
|
std::span<const std::byte> FilterPCM(std::span<const std::byte> src) override {
|
2016-09-04 14:32:09 +02:00
|
|
|
return filter->FilterPCM(src);
|
2016-06-22 11:15:49 +02:00
|
|
|
}
|
2018-01-01 19:22:45 +01:00
|
|
|
|
2022-07-04 18:12:12 +02:00
|
|
|
std::span<const std::byte> Flush() override {
|
2018-01-01 19:22:45 +01:00
|
|
|
return filter->Flush();
|
|
|
|
}
|
2016-06-22 11:15:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Filter *
|
2019-08-26 21:04:35 +02:00
|
|
|
FilterObserver::PreparedProxy::Get() noexcept
|
2016-06-22 11:15:49 +02:00
|
|
|
{
|
|
|
|
return child != nullptr
|
|
|
|
? child->Get()
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2017-12-27 11:42:14 +01:00
|
|
|
std::unique_ptr<Filter>
|
2016-09-04 14:32:09 +02:00
|
|
|
FilterObserver::PreparedProxy::Open(AudioFormat &af)
|
2016-06-22 11:15:49 +02:00
|
|
|
{
|
|
|
|
assert(child == nullptr);
|
|
|
|
|
2017-12-27 11:42:14 +01:00
|
|
|
auto c = std::make_unique<Proxy>(*this, prepared_filter->Open(af));
|
|
|
|
child = c.get();
|
|
|
|
return c;
|
2016-06-22 11:15:49 +02:00
|
|
|
}
|
|
|
|
|
2017-12-27 09:17:15 +01:00
|
|
|
std::unique_ptr<PreparedFilter>
|
|
|
|
FilterObserver::Set(std::unique_ptr<PreparedFilter> pf)
|
2016-06-22 11:15:49 +02:00
|
|
|
{
|
|
|
|
assert(proxy == nullptr);
|
|
|
|
|
2017-12-27 09:17:15 +01:00
|
|
|
auto p = std::make_unique<PreparedProxy>(*this, std::move(pf));
|
|
|
|
proxy = p.get();
|
|
|
|
return p;
|
2016-06-22 11:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Filter *
|
2019-08-26 21:04:35 +02:00
|
|
|
FilterObserver::Get() noexcept
|
2016-06-22 11:15:49 +02:00
|
|
|
{
|
|
|
|
return proxy != nullptr
|
|
|
|
? proxy->Get()
|
|
|
|
: nullptr;
|
|
|
|
}
|