2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2017-02-27 22:55:20 +01:00
|
|
|
#include "Control.hxx"
|
2017-08-07 21:50:13 +02:00
|
|
|
#include "Filtered.hxx"
|
2019-09-26 13:02:34 +02:00
|
|
|
#include "Client.hxx"
|
2021-06-24 20:22:48 +02:00
|
|
|
#include "Domain.hxx"
|
|
|
|
#include "lib/fmt/ExceptionFormatter.hxx"
|
2022-08-18 14:29:07 +02:00
|
|
|
#include "mixer/Mixer.hxx"
|
2017-05-22 23:40:20 +02:00
|
|
|
#include "config/Block.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2008-09-24 07:20:26 +02:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2008-09-24 07:20:26 +02:00
|
|
|
|
2016-12-27 22:47:20 +01:00
|
|
|
/** after a failure, wait this duration before
|
2013-10-20 13:52:26 +02:00
|
|
|
automatically reopening the device */
|
2016-12-27 22:47:20 +01:00
|
|
|
static constexpr PeriodClock::Duration REOPEN_AFTER = std::chrono::seconds(10);
|
2009-02-28 20:43:23 +01:00
|
|
|
|
2018-01-04 10:14:26 +01:00
|
|
|
AudioOutputControl::AudioOutputControl(std::unique_ptr<FilteredAudioOutput> _output,
|
2021-10-22 20:18:06 +02:00
|
|
|
AudioOutputClient &_client,
|
|
|
|
const ConfigBlock &block)
|
2019-09-26 13:02:34 +02:00
|
|
|
:output(std::move(_output)),
|
|
|
|
name(output->GetName()),
|
|
|
|
client(_client),
|
2021-10-22 20:18:06 +02:00
|
|
|
thread(BIND_THIS_METHOD(Task)),
|
|
|
|
tags(block.GetBlockValue("tags", true)),
|
|
|
|
always_on(block.GetBlockValue("always_on", false)),
|
|
|
|
enabled(block.GetBlockValue("enabled", true))
|
2017-02-27 22:55:20 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-22 20:10:42 +02:00
|
|
|
AudioOutputControl::AudioOutputControl(AudioOutputControl &&src,
|
2020-11-13 17:35:36 +01:00
|
|
|
AudioOutputClient &_client) noexcept
|
2021-10-22 20:10:42 +02:00
|
|
|
:output(src.Steal()),
|
2020-11-13 17:35:36 +01:00
|
|
|
name(output->GetName()),
|
|
|
|
client(_client),
|
2021-10-22 20:17:24 +02:00
|
|
|
thread(BIND_THIS_METHOD(Task)),
|
|
|
|
tags(src.tags),
|
|
|
|
always_on(src.always_on)
|
2020-11-13 17:35:36 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-04 10:14:26 +01:00
|
|
|
AudioOutputControl::~AudioOutputControl() noexcept
|
|
|
|
{
|
2020-01-18 21:16:51 +01:00
|
|
|
StopThread();
|
2018-01-04 10:14:26 +01:00
|
|
|
}
|
|
|
|
|
2019-09-26 13:02:34 +02:00
|
|
|
std::unique_ptr<FilteredAudioOutput>
|
|
|
|
AudioOutputControl::Steal() noexcept
|
|
|
|
{
|
|
|
|
assert(!IsDummy());
|
|
|
|
|
|
|
|
/* close and disable the output */
|
|
|
|
{
|
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
|
|
|
if (really_enabled && output->SupportsEnableDisable())
|
|
|
|
CommandWait(lock, Command::DISABLE);
|
|
|
|
|
|
|
|
enabled = really_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stop the thread */
|
|
|
|
StopThread();
|
|
|
|
|
|
|
|
/* now we can finally remove it */
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2019-09-26 13:02:34 +02:00
|
|
|
return std::exchange(output, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioOutputControl::ReplaceDummy(std::unique_ptr<FilteredAudioOutput> new_output,
|
|
|
|
bool _enabled) noexcept
|
|
|
|
{
|
|
|
|
assert(IsDummy());
|
|
|
|
assert(new_output);
|
|
|
|
|
|
|
|
{
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2019-09-26 13:02:34 +02:00
|
|
|
output = std::move(new_output);
|
|
|
|
enabled = _enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
client.ApplyEnabled();
|
|
|
|
}
|
|
|
|
|
2017-12-19 10:36:32 +01:00
|
|
|
const char *
|
|
|
|
AudioOutputControl::GetPluginName() const noexcept
|
|
|
|
{
|
2019-09-26 13:02:34 +02:00
|
|
|
return output ? output->GetPluginName() : "dummy";
|
2017-12-19 10:36:32 +01:00
|
|
|
}
|
|
|
|
|
2017-08-08 14:02:58 +02:00
|
|
|
const char *
|
|
|
|
AudioOutputControl::GetLogName() const noexcept
|
|
|
|
{
|
2019-09-26 13:02:34 +02:00
|
|
|
assert(!IsDummy());
|
|
|
|
|
2021-04-26 21:33:51 +02:00
|
|
|
return output ? output->GetLogName() : name.c_str();
|
2017-08-08 14:02:58 +02:00
|
|
|
}
|
|
|
|
|
2017-02-27 22:55:20 +01:00
|
|
|
Mixer *
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::GetMixer() const noexcept
|
2017-02-27 22:55:20 +01:00
|
|
|
{
|
2019-09-26 13:02:34 +02:00
|
|
|
return output ? output->mixer : nullptr;
|
2017-02-27 22:55:20 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 23:13:36 +01:00
|
|
|
std::map<std::string, std::string>
|
2017-12-19 08:45:34 +01:00
|
|
|
AudioOutputControl::GetAttributes() const noexcept
|
|
|
|
{
|
2019-09-26 13:02:34 +02:00
|
|
|
return output
|
|
|
|
? output->GetAttributes()
|
|
|
|
: std::map<std::string, std::string>{};
|
2017-12-19 08:45:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-26 13:02:34 +02:00
|
|
|
AudioOutputControl::SetAttribute(std::string &&attribute_name,
|
|
|
|
std::string &&value)
|
2017-12-19 08:45:34 +01:00
|
|
|
{
|
2019-09-26 13:02:34 +02:00
|
|
|
if (!output)
|
|
|
|
throw std::runtime_error("Cannot set attribute on dummy output");
|
|
|
|
|
|
|
|
output->SetAttribute(std::move(attribute_name), std::move(value));
|
2017-12-19 08:45:34 +01:00
|
|
|
}
|
|
|
|
|
2017-02-27 22:55:20 +01:00
|
|
|
bool
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::LockSetEnabled(bool new_value) noexcept
|
2017-02-27 22:55:20 +01:00
|
|
|
{
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2017-02-27 22:55:20 +01:00
|
|
|
|
2017-05-22 23:40:20 +02:00
|
|
|
if (new_value == enabled)
|
2017-02-27 22:55:20 +01:00
|
|
|
return false;
|
|
|
|
|
2017-05-22 23:40:20 +02:00
|
|
|
enabled = new_value;
|
2017-02-27 22:55:20 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::LockToggleEnabled() noexcept
|
2017-02-27 22:55:20 +01:00
|
|
|
{
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2017-05-22 23:40:20 +02:00
|
|
|
return enabled = !enabled;
|
2017-02-27 22:55:20 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
2019-04-26 18:24:26 +02:00
|
|
|
AudioOutputControl::WaitForCommand(std::unique_lock<Mutex> &lock) noexcept
|
2008-09-24 07:20:26 +02:00
|
|
|
{
|
2019-05-07 20:01:45 +02:00
|
|
|
client_cond.wait(lock, [this]{ return IsCommandFinished(); });
|
2008-09-24 07:20:26 +02:00
|
|
|
}
|
|
|
|
|
2017-02-27 22:55:20 +01:00
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::CommandAsync(Command cmd) noexcept
|
2008-09-24 07:20:26 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(IsCommandFinished());
|
|
|
|
|
|
|
|
command = cmd;
|
2019-04-25 18:33:09 +02:00
|
|
|
wake_cond.notify_one();
|
2008-09-24 07:20:26 +02:00
|
|
|
}
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
2019-04-26 18:24:26 +02:00
|
|
|
AudioOutputControl::CommandWait(std::unique_lock<Mutex> &lock,
|
|
|
|
Command cmd) noexcept
|
2008-09-24 07:23:19 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
CommandAsync(cmd);
|
2019-04-26 18:24:26 +02:00
|
|
|
WaitForCommand(lock);
|
2008-09-24 07:23:19 +02:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::LockCommandWait(Command cmd) noexcept
|
2011-01-10 21:54:43 +01:00
|
|
|
{
|
2019-04-26 18:24:26 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
|
|
|
CommandWait(lock, cmd);
|
2011-01-10 21:54:43 +01:00
|
|
|
}
|
|
|
|
|
2009-10-23 10:55:52 +02:00
|
|
|
void
|
2017-04-28 21:45:47 +02:00
|
|
|
AudioOutputControl::EnableAsync()
|
2009-10-23 10:55:52 +02:00
|
|
|
{
|
2019-09-26 13:02:34 +02:00
|
|
|
if (!output)
|
|
|
|
return;
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
if (!thread.IsDefined()) {
|
2017-08-08 15:54:49 +02:00
|
|
|
if (!output->SupportsEnableDisable()) {
|
2009-10-23 10:55:52 +02:00
|
|
|
/* don't bother to start the thread now if the
|
|
|
|
device doesn't even have a enable() method;
|
|
|
|
just assign the variable and we're done */
|
2017-06-08 09:49:30 +02:00
|
|
|
really_enabled = true;
|
2009-10-23 10:55:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
StartThread();
|
2009-10-23 10:55:52 +02:00
|
|
|
}
|
|
|
|
|
2016-12-14 08:41:22 +01:00
|
|
|
CommandAsync(Command::ENABLE);
|
2009-10-23 10:55:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::DisableAsync() noexcept
|
2009-10-23 10:55:52 +02:00
|
|
|
{
|
2019-09-26 13:02:34 +02:00
|
|
|
if (!output)
|
|
|
|
return;
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
if (!thread.IsDefined()) {
|
2017-08-08 15:54:49 +02:00
|
|
|
if (!output->SupportsEnableDisable())
|
2017-06-08 09:49:30 +02:00
|
|
|
really_enabled = false;
|
2009-10-23 10:55:52 +02:00
|
|
|
else
|
|
|
|
/* if there's no thread yet, the device cannot
|
|
|
|
be enabled */
|
2017-06-08 09:49:30 +02:00
|
|
|
assert(!really_enabled);
|
2009-10-23 10:55:52 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-14 08:41:22 +01:00
|
|
|
CommandAsync(Command::DISABLE);
|
2009-10-23 10:55:52 +02:00
|
|
|
}
|
|
|
|
|
2017-02-27 22:55:20 +01:00
|
|
|
void
|
|
|
|
AudioOutputControl::EnableDisableAsync()
|
|
|
|
{
|
2017-06-08 09:49:30 +02:00
|
|
|
if (enabled == really_enabled)
|
2017-04-28 21:45:47 +02:00
|
|
|
return;
|
2017-02-27 22:55:20 +01:00
|
|
|
|
2017-05-22 23:40:20 +02:00
|
|
|
if (enabled)
|
2017-04-28 21:45:47 +02:00
|
|
|
EnableAsync();
|
|
|
|
else
|
|
|
|
DisableAsync();
|
2017-02-27 22:55:20 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
inline bool
|
2019-04-26 18:24:26 +02:00
|
|
|
AudioOutputControl::Open(std::unique_lock<Mutex> &lock,
|
|
|
|
const AudioFormat audio_format,
|
2017-05-23 00:17:23 +02:00
|
|
|
const MusicPipe &mp) noexcept
|
2008-09-09 10:02:34 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(allow_play);
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(audio_format.IsValid());
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
fail_timer.Reset();
|
2008-10-29 22:32:50 +01:00
|
|
|
|
2017-06-08 22:25:45 +02:00
|
|
|
if (open && audio_format == request.audio_format) {
|
2017-05-23 00:00:00 +02:00
|
|
|
assert(request.pipe == &mp || (always_on && pause));
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2017-05-23 00:00:00 +02:00
|
|
|
if (!pause)
|
2016-12-21 13:01:38 +01:00
|
|
|
/* already open, already the right parameters
|
|
|
|
- nothing needs to be done */
|
|
|
|
return true;
|
2008-09-09 10:02:34 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 17:00:29 +01:00
|
|
|
request.audio_format = audio_format;
|
2016-12-20 23:19:12 +01:00
|
|
|
request.pipe = ∓
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2017-10-30 08:39:44 +01:00
|
|
|
if (!thread.IsDefined()) {
|
|
|
|
try {
|
|
|
|
StartThread();
|
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2019-04-26 18:24:26 +02:00
|
|
|
CommandWait(lock, Command::OPEN);
|
2017-06-08 22:25:45 +02:00
|
|
|
const bool open2 = open;
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2017-04-28 21:45:47 +02:00
|
|
|
if (open2 && output->mixer != nullptr) {
|
2018-01-04 08:40:53 +01:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2016-09-09 12:52:51 +02:00
|
|
|
try {
|
2022-08-18 14:29:07 +02:00
|
|
|
output->mixer->LockOpen();
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
2021-06-24 20:22:48 +02:00
|
|
|
FmtError(output_domain,
|
|
|
|
"Failed to open mixer for '{}': {}",
|
|
|
|
GetName(), std::current_exception());
|
2016-09-09 12:52:51 +02:00
|
|
|
}
|
2009-10-20 22:10:56 +02:00
|
|
|
}
|
2009-03-26 18:23:23 +01:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
return open2;
|
2008-09-09 10:02:34 +02:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
2019-04-26 18:24:26 +02:00
|
|
|
AudioOutputControl::CloseWait(std::unique_lock<Mutex> &lock) noexcept
|
2009-10-29 22:39:42 +01:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(allow_play);
|
2010-11-07 15:30:18 +01:00
|
|
|
|
2019-09-26 13:02:34 +02:00
|
|
|
if (IsDummy())
|
|
|
|
return;
|
|
|
|
|
2017-04-28 21:45:47 +02:00
|
|
|
if (output->mixer != nullptr)
|
2022-08-18 14:29:07 +02:00
|
|
|
output->mixer->LockAutoClose();
|
2009-10-29 22:39:42 +01:00
|
|
|
|
2017-06-08 22:25:45 +02:00
|
|
|
assert(!open || !fail_timer.IsDefined());
|
2009-10-29 22:39:42 +01:00
|
|
|
|
2017-06-08 22:25:45 +02:00
|
|
|
if (open)
|
2019-04-26 18:24:26 +02:00
|
|
|
CommandWait(lock, Command::CLOSE);
|
2013-11-24 20:20:57 +01:00
|
|
|
else
|
2014-01-28 11:39:12 +01:00
|
|
|
fail_timer.Reset();
|
2009-10-29 22:39:42 +01:00
|
|
|
}
|
|
|
|
|
2009-03-07 19:55:57 +01:00
|
|
|
bool
|
2017-04-28 21:45:47 +02:00
|
|
|
AudioOutputControl::LockUpdate(const AudioFormat audio_format,
|
|
|
|
const MusicPipe &mp,
|
2017-05-23 00:17:23 +02:00
|
|
|
bool force) noexcept
|
2008-10-29 22:17:44 +01:00
|
|
|
{
|
2019-04-26 18:24:26 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
2009-10-29 17:06:40 +01:00
|
|
|
|
2017-06-08 09:49:30 +02:00
|
|
|
if (enabled && really_enabled) {
|
2016-12-29 23:20:26 +01:00
|
|
|
if (force || !fail_timer.IsDefined() ||
|
2015-06-20 15:37:19 +02:00
|
|
|
fail_timer.Check(REOPEN_AFTER * 1000)) {
|
2019-04-26 18:24:26 +02:00
|
|
|
return Open(lock, audio_format, mp);
|
2009-10-29 17:06:40 +01:00
|
|
|
}
|
2014-01-28 11:39:12 +01:00
|
|
|
} else if (IsOpen())
|
2019-04-26 18:24:26 +02:00
|
|
|
CloseWait(lock);
|
2009-03-07 19:55:57 +01:00
|
|
|
|
|
|
|
return false;
|
2008-10-29 22:17:44 +01:00
|
|
|
}
|
|
|
|
|
2017-02-27 22:55:20 +01:00
|
|
|
bool
|
2017-05-23 00:03:06 +02:00
|
|
|
AudioOutputControl::IsChunkConsumed(const MusicChunk &chunk) const noexcept
|
2017-02-27 22:55:20 +01:00
|
|
|
{
|
2017-06-08 22:25:45 +02:00
|
|
|
if (!open)
|
2017-05-23 00:03:06 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return source.IsChunkConsumed(chunk);
|
2017-02-27 22:55:20 +01:00
|
|
|
}
|
|
|
|
|
2017-05-23 00:03:06 +02:00
|
|
|
bool
|
|
|
|
AudioOutputControl::LockIsChunkConsumed(const MusicChunk &chunk) const noexcept
|
2017-02-27 22:55:20 +01:00
|
|
|
{
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2017-05-23 00:03:06 +02:00
|
|
|
return IsChunkConsumed(chunk);
|
2017-02-27 22:55:20 +01:00
|
|
|
}
|
|
|
|
|
2009-02-16 00:43:06 +01:00
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::LockPlay() noexcept
|
2008-09-09 10:02:34 +02:00
|
|
|
{
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(allow_play);
|
2011-09-01 07:13:21 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
if (IsOpen() && !in_playback_loop && !woken_for_play) {
|
|
|
|
woken_for_play = true;
|
2019-04-25 18:33:09 +02:00
|
|
|
wake_cond.notify_one();
|
2013-11-06 23:47:30 +01:00
|
|
|
}
|
2008-09-09 10:02:34 +02:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::LockPauseAsync() noexcept
|
2008-09-29 16:43:55 +02:00
|
|
|
{
|
2021-04-26 21:33:51 +02:00
|
|
|
if (output && output->mixer != nullptr && !output->SupportsPause())
|
2009-04-21 22:17:52 +02:00
|
|
|
/* the device has no pause mode: close the mixer,
|
|
|
|
unless its "global" flag is set (checked by
|
2022-08-18 14:29:07 +02:00
|
|
|
Mixer::LockAutoClose()) */
|
|
|
|
output->mixer->LockAutoClose();
|
2009-04-21 22:17:52 +02:00
|
|
|
|
2020-10-01 15:29:13 +02:00
|
|
|
if (output)
|
|
|
|
output->Interrupt();
|
|
|
|
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2013-04-17 01:19:25 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(allow_play);
|
|
|
|
if (IsOpen())
|
2014-12-24 22:11:57 +01:00
|
|
|
CommandAsync(Command::PAUSE);
|
2008-09-29 16:43:55 +02:00
|
|
|
}
|
|
|
|
|
2009-11-09 22:16:26 +01:00
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::LockDrainAsync() noexcept
|
2009-11-09 22:16:26 +01:00
|
|
|
{
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2013-04-17 01:19:25 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(allow_play);
|
|
|
|
if (IsOpen())
|
2014-12-24 22:11:57 +01:00
|
|
|
CommandAsync(Command::DRAIN);
|
2009-11-09 22:16:26 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::LockCancelAsync() noexcept
|
2008-09-09 10:02:34 +02:00
|
|
|
{
|
2020-10-01 15:29:13 +02:00
|
|
|
if (output)
|
|
|
|
output->Interrupt();
|
|
|
|
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2011-09-01 07:13:21 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
if (IsOpen()) {
|
|
|
|
allow_play = false;
|
2014-12-24 22:11:57 +01:00
|
|
|
CommandAsync(Command::CANCEL);
|
2011-09-01 07:13:21 +02:00
|
|
|
}
|
2011-09-01 07:53:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::LockAllowPlay() noexcept
|
2011-09-01 07:53:42 +02:00
|
|
|
{
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2011-09-01 07:53:42 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
allow_play = true;
|
|
|
|
if (IsOpen())
|
2019-04-25 18:33:09 +02:00
|
|
|
wake_cond.notify_one();
|
2008-09-09 10:02:34 +02:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:29:33 +01:00
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::LockRelease() noexcept
|
2010-03-03 20:29:33 +01:00
|
|
|
{
|
2020-01-20 17:34:38 +01:00
|
|
|
if (!output)
|
|
|
|
return;
|
|
|
|
|
2020-10-01 15:29:13 +02:00
|
|
|
output->Interrupt();
|
|
|
|
|
2018-11-12 11:30:05 +01:00
|
|
|
if (output->mixer != nullptr &&
|
|
|
|
(!always_on || !output->SupportsPause()))
|
|
|
|
/* the device has no pause mode: close the mixer,
|
|
|
|
unless its "global" flag is set (checked by
|
2022-08-18 14:29:07 +02:00
|
|
|
Mixer::LockAutoClose()) */
|
|
|
|
output->mixer->LockAutoClose();
|
2018-11-12 11:30:05 +01:00
|
|
|
|
2019-04-26 18:24:26 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
2018-11-12 11:30:05 +01:00
|
|
|
|
|
|
|
assert(!open || !fail_timer.IsDefined());
|
|
|
|
assert(allow_play);
|
|
|
|
|
|
|
|
if (IsOpen())
|
2019-04-26 18:24:26 +02:00
|
|
|
CommandWait(lock, Command::RELEASE);
|
2010-03-03 20:29:33 +01:00
|
|
|
else
|
2018-11-12 11:30:05 +01:00
|
|
|
fail_timer.Reset();
|
2010-03-03 20:29:33 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::LockCloseWait() noexcept
|
2010-11-04 21:51:02 +01:00
|
|
|
{
|
2017-06-08 22:25:45 +02:00
|
|
|
assert(!open || !fail_timer.IsDefined());
|
2010-11-04 21:51:02 +01:00
|
|
|
|
2020-10-01 15:29:13 +02:00
|
|
|
if (output)
|
|
|
|
output->Interrupt();
|
|
|
|
|
2019-04-26 18:24:26 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
|
|
|
CloseWait(lock);
|
2010-11-04 21:51:02 +01:00
|
|
|
}
|
|
|
|
|
2017-02-27 22:55:20 +01:00
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::BeginDestroy() noexcept
|
2017-02-27 22:55:20 +01:00
|
|
|
{
|
2017-04-28 21:45:47 +02:00
|
|
|
if (thread.IsDefined()) {
|
2020-10-01 15:29:13 +02:00
|
|
|
if (output)
|
|
|
|
output->Interrupt();
|
|
|
|
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2020-01-18 21:43:14 +01:00
|
|
|
if (!killed) {
|
|
|
|
killed = true;
|
2019-04-26 14:28:30 +02:00
|
|
|
CommandAsync(Command::KILL);
|
2020-01-18 21:43:14 +01:00
|
|
|
}
|
2017-04-28 21:45:47 +02:00
|
|
|
}
|
2017-02-27 22:55:20 +01:00
|
|
|
}
|
2020-01-18 21:16:51 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
AudioOutputControl::StopThread() noexcept
|
|
|
|
{
|
|
|
|
BeginDestroy();
|
|
|
|
|
|
|
|
if (thread.IsDefined())
|
|
|
|
thread.Join();
|
|
|
|
|
|
|
|
assert(IsCommandFinished());
|
|
|
|
}
|