2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-09-24 07:20:26 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-09-24 07:20:26 +02:00
|
|
|
*/
|
|
|
|
|
2017-02-27 22:55:20 +01:00
|
|
|
#include "Control.hxx"
|
2017-08-07 21:50:13 +02:00
|
|
|
#include "Filtered.hxx"
|
2016-12-14 12:14:57 +01:00
|
|
|
#include "Client.hxx"
|
2014-01-28 11:42:54 +01:00
|
|
|
#include "Domain.hxx"
|
2016-06-22 11:15:49 +02:00
|
|
|
#include "mixer/MixerInternal.hxx"
|
2014-01-14 09:59:04 +01:00
|
|
|
#include "thread/Util.hxx"
|
2014-01-30 18:37:30 +01:00
|
|
|
#include "thread/Slack.hxx"
|
2014-01-23 10:07:14 +01:00
|
|
|
#include "thread/Name.hxx"
|
2017-01-17 22:04:31 +01:00
|
|
|
#include "util/StringBuffer.hxx"
|
2016-12-13 22:15:52 +01:00
|
|
|
#include "util/ScopeExit.hxx"
|
2016-12-20 17:35:32 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2016-09-04 15:11:01 +02:00
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.h>
|
2009-02-25 19:53:38 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::CommandFinished() noexcept
|
2008-09-24 07:20:26 +02:00
|
|
|
{
|
2014-12-24 22:11:57 +01:00
|
|
|
assert(command != Command::NONE);
|
|
|
|
command = Command::NONE;
|
2009-10-29 17:06:40 +01:00
|
|
|
|
2019-04-25 18:33:09 +02:00
|
|
|
client_cond.notify_one();
|
2008-09-24 07:20:26 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
inline void
|
2017-06-08 22:25:45 +02:00
|
|
|
AudioOutputControl::InternalOpen2(const AudioFormat in_audio_format)
|
2009-07-06 08:04:18 +02:00
|
|
|
{
|
2017-06-08 13:20:55 +02:00
|
|
|
assert(in_audio_format.IsValid());
|
2010-11-07 15:30:18 +01:00
|
|
|
|
2017-06-08 22:25:45 +02:00
|
|
|
const auto cf = in_audio_format.WithMask(output->config_audio_format);
|
2017-01-15 01:21:14 +01:00
|
|
|
|
2017-08-07 17:35:43 +02:00
|
|
|
if (open && cf != output->filter_audio_format)
|
2016-12-20 16:37:36 +01:00
|
|
|
/* if the filter's output format changes, the output
|
|
|
|
must be reopened as well */
|
2017-08-07 17:35:43 +02:00
|
|
|
InternalCloseOutput(true);
|
2011-03-16 23:37:41 +01:00
|
|
|
|
2017-06-08 22:25:45 +02:00
|
|
|
output->filter_audio_format = cf;
|
2016-12-13 21:46:27 +01:00
|
|
|
|
2016-12-24 14:05:11 +01:00
|
|
|
if (!open) {
|
2017-08-07 18:36:04 +02:00
|
|
|
{
|
2017-08-07 17:08:49 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2017-06-08 22:25:45 +02:00
|
|
|
output->OpenOutputAndConvert(output->filter_audio_format);
|
2016-12-24 14:05:11 +01:00
|
|
|
}
|
2016-12-27 14:55:56 +01:00
|
|
|
|
|
|
|
open = true;
|
2017-06-08 22:25:45 +02:00
|
|
|
} else if (in_audio_format != output->out_audio_format) {
|
2017-01-15 01:18:34 +01:00
|
|
|
/* reconfigure the final ConvertFilter for its new
|
|
|
|
input AudioFormat */
|
|
|
|
|
|
|
|
try {
|
2017-08-07 18:39:12 +02:00
|
|
|
output->ConfigureConvertFilter();
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
2017-06-08 22:25:45 +02:00
|
|
|
open = false;
|
2017-08-07 16:25:23 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
2017-08-07 18:36:04 +02:00
|
|
|
output->CloseOutput(false);
|
2017-08-07 16:25:23 +02:00
|
|
|
}
|
|
|
|
|
2017-08-07 18:39:12 +02:00
|
|
|
throw;
|
2017-01-15 01:18:34 +01:00
|
|
|
}
|
2016-12-24 14:05:11 +01:00
|
|
|
}
|
2017-08-07 18:36:04 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
output->OpenSoftwareMixer();
|
|
|
|
}
|
2016-12-13 21:46:27 +01:00
|
|
|
}
|
|
|
|
|
2017-06-08 09:51:13 +02:00
|
|
|
inline bool
|
|
|
|
AudioOutputControl::InternalEnable() noexcept
|
|
|
|
{
|
2017-06-08 09:49:30 +02:00
|
|
|
if (really_enabled)
|
|
|
|
/* already enabled */
|
|
|
|
return true;
|
|
|
|
|
2017-06-08 09:51:13 +02:00
|
|
|
last_error = nullptr;
|
|
|
|
|
|
|
|
try {
|
2017-08-07 17:39:20 +02:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
output->Enable();
|
|
|
|
}
|
|
|
|
|
2017-06-08 09:49:30 +02:00
|
|
|
really_enabled = true;
|
2017-06-08 09:51:13 +02:00
|
|
|
return true;
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
2018-01-02 17:15:59 +01:00
|
|
|
Failure(std::current_exception());
|
2017-06-08 09:51:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 09:32:07 +02:00
|
|
|
inline void
|
|
|
|
AudioOutputControl::InternalDisable() noexcept
|
|
|
|
{
|
2017-06-08 09:49:30 +02:00
|
|
|
if (!really_enabled)
|
|
|
|
return;
|
|
|
|
|
2017-08-07 16:25:58 +02:00
|
|
|
InternalCheckClose(false);
|
2017-06-08 09:34:24 +02:00
|
|
|
|
2017-06-08 09:49:30 +02:00
|
|
|
really_enabled = false;
|
2017-08-07 17:39:20 +02:00
|
|
|
|
|
|
|
const ScopeUnlock unlock(mutex);
|
2017-06-08 09:32:07 +02:00
|
|
|
output->Disable();
|
|
|
|
}
|
|
|
|
|
2017-05-23 00:08:36 +02:00
|
|
|
inline void
|
2017-06-08 21:54:10 +02:00
|
|
|
AudioOutputControl::InternalOpen(const AudioFormat in_audio_format,
|
2017-05-23 00:08:36 +02:00
|
|
|
const MusicPipe &pipe) noexcept
|
|
|
|
{
|
2017-06-08 09:51:13 +02:00
|
|
|
/* enable the device (just in case the last enable has failed) */
|
|
|
|
if (!InternalEnable())
|
|
|
|
return;
|
|
|
|
|
2017-05-23 00:08:36 +02:00
|
|
|
last_error = nullptr;
|
|
|
|
fail_timer.Reset();
|
2017-05-23 10:58:30 +02:00
|
|
|
skip_delay = true;
|
2017-05-23 00:08:36 +02:00
|
|
|
|
2017-06-08 21:54:10 +02:00
|
|
|
AudioFormat f;
|
|
|
|
|
2017-05-23 00:08:36 +02:00
|
|
|
try {
|
2017-06-08 21:54:10 +02:00
|
|
|
try {
|
|
|
|
f = source.Open(in_audio_format, pipe,
|
2017-12-27 09:17:15 +01:00
|
|
|
output->prepared_replay_gain_filter.get(),
|
|
|
|
output->prepared_other_replay_gain_filter.get(),
|
2017-12-27 11:21:43 +01:00
|
|
|
*output->prepared_filter);
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
2017-08-08 14:02:58 +02:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to open filter for %s",
|
|
|
|
GetLogName()));
|
2017-06-08 21:54:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-06-08 22:25:45 +02:00
|
|
|
InternalOpen2(f);
|
2017-06-08 21:54:10 +02:00
|
|
|
} catch (...) {
|
2017-05-23 00:03:06 +02:00
|
|
|
source.Close();
|
2017-06-08 21:54:10 +02:00
|
|
|
throw;
|
|
|
|
}
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
2018-01-02 17:15:59 +01:00
|
|
|
Failure(std::current_exception());
|
2017-05-23 00:08:36 +02:00
|
|
|
}
|
2017-06-08 21:54:10 +02:00
|
|
|
|
|
|
|
if (f != in_audio_format || f != output->out_audio_format)
|
|
|
|
FormatDebug(output_domain, "converting in=%s -> f=%s -> out=%s",
|
|
|
|
ToString(in_audio_format).c_str(),
|
|
|
|
ToString(f).c_str(),
|
|
|
|
ToString(output->out_audio_format).c_str());
|
2017-05-23 00:08:36 +02:00
|
|
|
}
|
|
|
|
|
2017-08-07 17:35:43 +02:00
|
|
|
inline void
|
|
|
|
AudioOutputControl::InternalCloseOutput(bool drain) noexcept
|
|
|
|
{
|
|
|
|
assert(IsOpen());
|
|
|
|
|
|
|
|
open = false;
|
|
|
|
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
output->CloseOutput(drain);
|
|
|
|
}
|
|
|
|
|
2017-06-08 21:59:48 +02:00
|
|
|
inline void
|
2017-08-07 16:34:29 +02:00
|
|
|
AudioOutputControl::InternalClose(bool drain) noexcept
|
2017-06-08 21:59:48 +02:00
|
|
|
{
|
2017-08-07 16:34:29 +02:00
|
|
|
assert(IsOpen());
|
2017-06-08 21:59:48 +02:00
|
|
|
|
2017-06-08 22:25:45 +02:00
|
|
|
open = false;
|
2017-08-07 16:25:23 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
output->Close(drain);
|
|
|
|
}
|
|
|
|
|
2017-05-23 00:03:06 +02:00
|
|
|
source.Close();
|
2017-06-08 21:59:48 +02:00
|
|
|
}
|
|
|
|
|
2017-08-07 16:34:29 +02:00
|
|
|
inline void
|
|
|
|
AudioOutputControl::InternalCheckClose(bool drain) noexcept
|
|
|
|
{
|
|
|
|
if (IsOpen())
|
|
|
|
InternalClose(drain);
|
|
|
|
}
|
|
|
|
|
2010-11-05 08:02:38 +01:00
|
|
|
/**
|
|
|
|
* Wait until the output's delay reaches zero.
|
|
|
|
*
|
|
|
|
* @return true if playback should be continued, false if a command
|
|
|
|
* was issued
|
|
|
|
*/
|
2014-01-29 09:12:41 +01:00
|
|
|
inline bool
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::WaitForDelay() noexcept
|
2010-11-05 08:02:38 +01:00
|
|
|
{
|
|
|
|
while (true) {
|
2017-08-08 14:27:19 +02:00
|
|
|
const auto delay = output->Delay();
|
2016-12-28 21:44:18 +01:00
|
|
|
if (delay <= std::chrono::steady_clock::duration::zero())
|
2010-11-05 08:02:38 +01:00
|
|
|
return true;
|
|
|
|
|
2019-04-25 18:33:09 +02:00
|
|
|
(void)wake_cond.wait_for(mutex, delay);
|
2010-11-05 08:02:38 +01:00
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
if (command != Command::NONE)
|
2010-11-05 08:02:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 13:53:22 +01:00
|
|
|
bool
|
2018-01-04 09:54:35 +01:00
|
|
|
AudioOutputControl::FillSourceOrClose() noexcept
|
2016-12-26 13:53:22 +01:00
|
|
|
try {
|
2017-05-23 00:03:06 +02:00
|
|
|
return source.Fill(mutex);
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
FormatError(std::current_exception(),
|
|
|
|
"Failed to filter for %s", GetLogName());
|
2018-01-02 17:15:10 +01:00
|
|
|
InternalCloseError(std::current_exception());
|
2016-12-26 13:53:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
inline bool
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutputControl::PlayChunk() noexcept
|
2010-05-02 16:32:38 +02:00
|
|
|
{
|
2017-11-04 04:38:49 +01:00
|
|
|
// ensure pending tags are flushed in all cases
|
|
|
|
const auto *tag = source.ReadTag();
|
|
|
|
if (tags && tag != nullptr) {
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
try {
|
2017-11-05 17:48:41 +01:00
|
|
|
output->SendTag(*tag);
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
FormatError(std::current_exception(),
|
|
|
|
"Failed to send tag to %s",
|
2017-11-05 17:48:41 +01:00
|
|
|
GetLogName());
|
2016-11-07 09:25:47 +01:00
|
|
|
}
|
2010-05-02 16:32:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-26 13:53:22 +01:00
|
|
|
while (command == Command::NONE) {
|
2017-05-23 00:03:06 +02:00
|
|
|
const auto data = source.PeekData();
|
2017-11-10 19:24:33 +01:00
|
|
|
if (data.empty())
|
2016-12-26 13:53:22 +01:00
|
|
|
break;
|
2008-12-24 03:08:39 +01:00
|
|
|
|
2017-05-23 10:58:30 +02:00
|
|
|
if (skip_delay)
|
|
|
|
skip_delay = false;
|
|
|
|
else if (!WaitForDelay())
|
2010-11-05 08:02:38 +01:00
|
|
|
break;
|
|
|
|
|
2016-10-28 20:41:53 +02:00
|
|
|
size_t nbytes;
|
|
|
|
|
2016-10-28 20:33:57 +02:00
|
|
|
try {
|
2016-10-28 20:41:53 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2017-08-08 14:27:19 +02:00
|
|
|
nbytes = output->Play(data.data, data.size);
|
2018-01-02 17:47:46 +01:00
|
|
|
assert(nbytes > 0);
|
2017-02-10 22:24:36 +01:00
|
|
|
assert(nbytes <= data.size);
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
FormatError(std::current_exception(),
|
|
|
|
"Failed to play on %s", GetLogName());
|
2018-01-02 17:15:10 +01:00
|
|
|
InternalCloseError(std::current_exception());
|
2009-03-09 19:25:26 +01:00
|
|
|
return false;
|
2009-02-23 09:29:56 +01:00
|
|
|
}
|
|
|
|
|
2017-04-28 21:45:47 +02:00
|
|
|
assert(nbytes % output->out_audio_format.GetFrameSize() == 0);
|
2009-02-23 09:29:56 +01:00
|
|
|
|
2017-05-23 00:03:06 +02:00
|
|
|
source.ConsumeData(nbytes);
|
2008-10-29 20:40:33 +01:00
|
|
|
}
|
|
|
|
|
2009-03-09 19:25:26 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
inline bool
|
2017-05-23 10:59:16 +02:00
|
|
|
AudioOutputControl::InternalPlay() noexcept
|
2009-03-09 19:25:26 +01:00
|
|
|
{
|
2016-12-26 13:53:22 +01:00
|
|
|
if (!FillSourceOrClose())
|
2009-11-02 20:20:14 +01:00
|
|
|
/* no chunk available */
|
|
|
|
return false;
|
2009-10-29 23:35:27 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
assert(!in_playback_loop);
|
|
|
|
in_playback_loop = true;
|
2013-11-06 23:47:30 +01:00
|
|
|
|
2016-12-13 22:15:52 +01:00
|
|
|
AtScopeExit(this) {
|
|
|
|
assert(in_playback_loop);
|
|
|
|
in_playback_loop = false;
|
|
|
|
};
|
|
|
|
|
2016-12-13 22:39:20 +01:00
|
|
|
unsigned n = 0;
|
|
|
|
|
2016-12-13 22:18:33 +01:00
|
|
|
do {
|
2016-12-13 22:17:06 +01:00
|
|
|
if (command != Command::NONE)
|
|
|
|
return true;
|
|
|
|
|
2016-12-13 22:39:20 +01:00
|
|
|
if (++n >= 64) {
|
|
|
|
/* wake up the player every now and then to
|
|
|
|
give it a chance to refill the pipe before
|
|
|
|
it runs empty */
|
|
|
|
const ScopeUnlock unlock(mutex);
|
2017-05-23 00:31:13 +02:00
|
|
|
client.ChunksConsumed();
|
2016-12-13 22:39:20 +01:00
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
|
2016-12-26 13:53:22 +01:00
|
|
|
if (!PlayChunk())
|
2009-03-09 19:25:26 +01:00
|
|
|
break;
|
2016-12-26 13:53:22 +01:00
|
|
|
} while (FillSourceOrClose());
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2016-10-28 20:41:53 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2017-05-23 00:31:13 +02:00
|
|
|
client.ChunksConsumed();
|
2009-10-29 23:35:27 +01:00
|
|
|
|
|
|
|
return true;
|
2008-09-24 07:20:26 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 22:01:20 +02:00
|
|
|
inline void
|
2017-05-23 10:59:16 +02:00
|
|
|
AudioOutputControl::InternalPause() noexcept
|
2017-04-28 22:01:20 +02:00
|
|
|
{
|
2017-08-07 17:40:40 +02:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
output->BeginPause();
|
|
|
|
}
|
|
|
|
|
2017-05-23 00:00:00 +02:00
|
|
|
pause = true;
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
CommandFinished();
|
2009-02-16 01:38:10 +01:00
|
|
|
|
|
|
|
do {
|
2014-01-29 09:12:41 +01:00
|
|
|
if (!WaitForDelay())
|
2010-11-05 08:02:38 +01:00
|
|
|
break;
|
|
|
|
|
2017-08-07 17:40:40 +02:00
|
|
|
bool success;
|
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
success = output->IteratePause();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!success) {
|
2017-08-07 16:34:29 +02:00
|
|
|
InternalClose(false);
|
2009-02-16 01:38:10 +01:00
|
|
|
break;
|
2017-05-23 00:03:06 +02:00
|
|
|
}
|
2014-12-24 22:11:57 +01:00
|
|
|
} while (command == Command::NONE);
|
2009-08-14 11:52:12 +02:00
|
|
|
|
2017-05-23 00:00:00 +02:00
|
|
|
pause = false;
|
2017-08-07 17:40:40 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
output->EndPause();
|
|
|
|
}
|
2017-05-23 10:58:30 +02:00
|
|
|
|
|
|
|
skip_delay = true;
|
2008-09-29 16:43:55 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 19:22:45 +01:00
|
|
|
static void
|
|
|
|
PlayFull(FilteredAudioOutput &output, ConstBuffer<void> _buffer)
|
|
|
|
{
|
|
|
|
auto buffer = ConstBuffer<uint8_t>::FromVoid(_buffer);
|
|
|
|
|
|
|
|
while (!buffer.empty()) {
|
|
|
|
size_t nbytes = output.Play(buffer.data, buffer.size);
|
|
|
|
assert(nbytes > 0);
|
|
|
|
|
|
|
|
buffer.skip_front(nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-02 17:12:00 +01:00
|
|
|
inline void
|
|
|
|
AudioOutputControl::InternalDrain() noexcept
|
|
|
|
{
|
2018-01-01 19:22:45 +01:00
|
|
|
try {
|
|
|
|
/* flush the filter and play its remaining output */
|
|
|
|
|
2018-01-29 22:30:56 +01:00
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
|
2018-01-01 19:22:45 +01:00
|
|
|
while (true) {
|
|
|
|
auto buffer = source.Flush();
|
|
|
|
if (buffer.IsNull())
|
|
|
|
break;
|
|
|
|
|
|
|
|
PlayFull(*output, buffer);
|
|
|
|
}
|
2018-01-29 22:30:56 +01:00
|
|
|
|
|
|
|
output->Drain();
|
2018-01-01 19:22:45 +01:00
|
|
|
} catch (...) {
|
|
|
|
FormatError(std::current_exception(),
|
|
|
|
"Failed to flush filter on %s", GetLogName());
|
|
|
|
InternalCloseError(std::current_exception());
|
|
|
|
return;
|
|
|
|
}
|
2018-01-02 17:12:00 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:41:11 +01:00
|
|
|
void
|
2018-01-04 09:54:35 +01:00
|
|
|
AudioOutputControl::Task() noexcept
|
2008-09-24 07:20:26 +02:00
|
|
|
{
|
2017-04-28 21:45:47 +02:00
|
|
|
FormatThreadName("output:%s", GetName());
|
2014-01-23 10:07:14 +01:00
|
|
|
|
2016-09-04 15:11:01 +02:00
|
|
|
try {
|
|
|
|
SetThreadRealtime();
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception(),
|
2016-09-04 15:11:01 +02:00
|
|
|
"OutputThread could not get realtime scheduling, continuing anyway");
|
2015-06-23 17:16:46 +02:00
|
|
|
}
|
2016-09-04 15:11:01 +02:00
|
|
|
|
2014-01-30 18:37:30 +01:00
|
|
|
SetThreadTimerSlackUS(100);
|
2014-01-14 09:59:04 +01:00
|
|
|
|
2019-04-25 18:53:38 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
2009-10-29 17:06:40 +01:00
|
|
|
|
2016-12-27 07:53:02 +01:00
|
|
|
while (true) {
|
2014-01-29 09:12:41 +01:00
|
|
|
switch (command) {
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::NONE:
|
2008-09-24 07:20:26 +02:00
|
|
|
break;
|
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::ENABLE:
|
2017-06-08 09:51:13 +02:00
|
|
|
InternalEnable();
|
2014-01-29 09:12:41 +01:00
|
|
|
CommandFinished();
|
2009-10-23 10:55:52 +02:00
|
|
|
break;
|
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::DISABLE:
|
2017-06-08 09:32:07 +02:00
|
|
|
InternalDisable();
|
2014-01-29 09:12:41 +01:00
|
|
|
CommandFinished();
|
2009-10-23 10:55:52 +02:00
|
|
|
break;
|
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::OPEN:
|
2017-05-23 00:08:36 +02:00
|
|
|
InternalOpen(request.audio_format, *request.pipe);
|
2014-01-29 09:12:41 +01:00
|
|
|
CommandFinished();
|
2009-07-06 10:01:02 +02:00
|
|
|
break;
|
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::CLOSE:
|
2017-08-07 16:25:58 +02:00
|
|
|
InternalCheckClose(false);
|
2014-01-29 09:12:41 +01:00
|
|
|
CommandFinished();
|
2008-09-24 07:20:26 +02:00
|
|
|
break;
|
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::PAUSE:
|
2017-06-08 22:25:45 +02:00
|
|
|
if (!open) {
|
2009-10-21 08:07:07 +02:00
|
|
|
/* the output has failed after
|
2018-11-12 11:51:01 +01:00
|
|
|
the PAUSE command was submitted; bail
|
2009-10-21 08:07:07 +02:00
|
|
|
out */
|
2014-01-29 09:12:41 +01:00
|
|
|
CommandFinished();
|
2009-10-21 08:07:07 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-05-23 10:59:16 +02:00
|
|
|
InternalPause();
|
2009-06-29 22:20:46 +02:00
|
|
|
/* don't "break" here: this might cause
|
2014-01-29 09:12:41 +01:00
|
|
|
Play() to be called when command==CLOSE
|
2009-06-29 22:20:46 +02:00
|
|
|
ends the paused state - "continue" checks
|
|
|
|
the new command first */
|
|
|
|
continue;
|
2008-09-29 16:43:55 +02:00
|
|
|
|
2018-11-12 11:30:05 +01:00
|
|
|
case Command::RELEASE:
|
|
|
|
if (!open) {
|
|
|
|
/* the output has failed after
|
|
|
|
the PAUSE command was submitted; bail
|
|
|
|
out */
|
|
|
|
CommandFinished();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (always_on) {
|
|
|
|
/* in "always_on" mode, the output is
|
2018-11-12 12:21:59 +01:00
|
|
|
paused instead of being closed;
|
2018-11-12 12:24:25 +01:00
|
|
|
however we need to flush the
|
2018-11-12 12:21:59 +01:00
|
|
|
AudioOutputSource because its data
|
|
|
|
have been invalidated by stopping
|
|
|
|
the actual playback */
|
2018-11-12 12:24:25 +01:00
|
|
|
source.Cancel();
|
2018-11-12 11:30:05 +01:00
|
|
|
InternalPause();
|
|
|
|
} else {
|
|
|
|
InternalClose(false);
|
|
|
|
CommandFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* don't "break" here: this might cause
|
|
|
|
Play() to be called when command==CLOSE
|
|
|
|
ends the paused state - "continue" checks
|
|
|
|
the new command first */
|
|
|
|
continue;
|
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::DRAIN:
|
2018-01-02 17:12:00 +01:00
|
|
|
if (open)
|
|
|
|
InternalDrain();
|
2009-11-09 22:16:26 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
CommandFinished();
|
2009-11-09 22:16:26 +01:00
|
|
|
continue;
|
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::CANCEL:
|
2017-05-23 00:03:06 +02:00
|
|
|
source.Cancel();
|
2011-07-20 18:35:24 +02:00
|
|
|
|
2017-06-08 22:25:45 +02:00
|
|
|
if (open) {
|
2016-10-28 20:41:53 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2017-08-08 14:27:19 +02:00
|
|
|
output->Cancel();
|
2011-07-20 18:35:24 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
CommandFinished();
|
2009-03-09 19:25:26 +01:00
|
|
|
continue;
|
2008-09-24 07:20:26 +02:00
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::KILL:
|
2017-06-08 09:32:07 +02:00
|
|
|
InternalDisable();
|
2017-05-23 00:03:06 +02:00
|
|
|
source.Cancel();
|
2014-01-29 09:12:41 +01:00
|
|
|
CommandFinished();
|
2013-10-17 18:42:14 +02:00
|
|
|
return;
|
2008-09-24 07:20:26 +02:00
|
|
|
}
|
|
|
|
|
2017-06-08 22:25:45 +02:00
|
|
|
if (open && allow_play && InternalPlay())
|
2009-10-29 23:35:27 +01:00
|
|
|
/* don't wait for an event if there are more
|
|
|
|
chunks in the pipe */
|
|
|
|
continue;
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
if (command == Command::NONE) {
|
2014-01-29 09:12:41 +01:00
|
|
|
woken_for_play = false;
|
2019-04-25 18:53:38 +02:00
|
|
|
wake_cond.wait(lock);
|
2013-11-06 23:47:30 +01:00
|
|
|
}
|
2008-09-24 07:20:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
2017-04-28 21:45:47 +02:00
|
|
|
AudioOutputControl::StartThread()
|
2008-09-24 07:20:26 +02:00
|
|
|
{
|
2014-12-24 22:11:57 +01:00
|
|
|
assert(command == Command::NONE);
|
2008-09-24 07:20:26 +02:00
|
|
|
|
2018-01-04 08:40:53 +01:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2017-02-10 22:41:11 +01:00
|
|
|
thread.Start();
|
2008-09-24 07:20:26 +02:00
|
|
|
}
|