2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 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
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2017-02-27 22:55:20 +01:00
|
|
|
#include "Control.hxx"
|
2014-01-28 11:42:54 +01:00
|
|
|
#include "Internal.hxx"
|
2016-12-14 12:14:57 +01:00
|
|
|
#include "Client.hxx"
|
2017-08-07 16:26:50 +02:00
|
|
|
#include "OutputPlugin.hxx"
|
2014-01-28 11:42:54 +01:00
|
|
|
#include "Domain.hxx"
|
2013-01-10 10:44:04 +01:00
|
|
|
#include "notify.hxx"
|
2014-01-24 16:31:52 +01:00
|
|
|
#include "filter/plugins/ConvertFilterPlugin.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
|
|
|
|
2016-10-28 20:41:53 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2013-01-10 10:44:04 +01:00
|
|
|
audio_output_client_notify.Signal();
|
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-08-07 18:33:07 +02:00
|
|
|
output->OpenSoftwareMixer();
|
2017-06-08 21:49:41 +02: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) {
|
2016-12-27 14:55:56 +01:00
|
|
|
try {
|
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-27 14:55:56 +01:00
|
|
|
} catch (...) {
|
2017-08-07 17:08:49 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2017-08-07 18:32:43 +02:00
|
|
|
output->CloseSoftwareMixer();
|
2016-12-27 14:55:56 +01:00
|
|
|
throw;
|
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-06-08 22:25:45 +02:00
|
|
|
convert_filter_set(output->convert_filter.Get(),
|
|
|
|
output->out_audio_format);
|
2017-01-15 01:18:34 +01:00
|
|
|
} catch (const std::runtime_error &e) {
|
2017-06-08 22:25:45 +02:00
|
|
|
open = false;
|
2017-08-07 16:25:23 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
output->Close(false);
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:18:34 +01:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to convert for \"%s\" [%s]",
|
2017-06-08 22:25:45 +02:00
|
|
|
GetName(), output->plugin.name));
|
2017-01-15 01:18:34 +01:00
|
|
|
}
|
2016-12-24 14:05:11 +01:00
|
|
|
}
|
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;
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
LogError(e);
|
|
|
|
fail_timer.Update();
|
|
|
|
last_error = std::current_exception();
|
|
|
|
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,
|
|
|
|
output->prepared_replay_gain_filter,
|
|
|
|
output->prepared_other_replay_gain_filter,
|
|
|
|
output->prepared_filter);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to open filter for \"%s\" [%s]",
|
|
|
|
GetName(), output->plugin.name));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-23 00:08:36 +02:00
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
LogError(e);
|
|
|
|
fail_timer.Update();
|
|
|
|
last_error = std::current_exception();
|
|
|
|
}
|
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-04-28 21:45:47 +02:00
|
|
|
const auto delay = ao_plugin_delay(*output);
|
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;
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
(void)cond.timed_wait(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
|
2017-04-28 21:45:47 +02:00
|
|
|
AudioOutputControl::FillSourceOrClose()
|
2016-12-26 13:53:22 +01:00
|
|
|
try {
|
2017-05-23 00:03:06 +02:00
|
|
|
return source.Fill(mutex);
|
2016-12-26 13:53:22 +01:00
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
FormatError(e, "Failed to filter for output \"%s\" [%s]",
|
2017-04-28 21:45:47 +02:00
|
|
|
GetName(), output->plugin.name);
|
2016-12-26 13:53:22 +01:00
|
|
|
|
2017-08-07 16:34:29 +02:00
|
|
|
InternalClose(false);
|
2016-12-26 13:53:22 +01:00
|
|
|
|
|
|
|
/* don't automatically reopen this device for 10
|
|
|
|
seconds */
|
|
|
|
fail_timer.Update();
|
|
|
|
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-05-22 23:40:20 +02:00
|
|
|
if (tags) {
|
2017-05-23 00:03:06 +02:00
|
|
|
const auto *tag = source.ReadTag();
|
2016-12-26 13:53:22 +01:00
|
|
|
if (tag != nullptr) {
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
try {
|
2017-04-28 21:45:47 +02:00
|
|
|
ao_plugin_send_tag(*output, *tag);
|
2016-12-26 13:53:22 +01:00
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
FormatError(e, "Failed to send tag to \"%s\" [%s]",
|
2017-04-28 21:45:47 +02:00
|
|
|
GetName(), output->plugin.name);
|
2016-12-26 13:53:22 +01:00
|
|
|
}
|
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();
|
2016-12-26 13:53:22 +01:00
|
|
|
if (data.IsEmpty())
|
|
|
|
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-04-28 21:45:47 +02:00
|
|
|
nbytes = ao_plugin_play(*output, data.data, data.size);
|
2017-02-10 22:24:36 +01:00
|
|
|
assert(nbytes <= data.size);
|
2016-10-28 20:33:57 +02:00
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
FormatError(e, "\"%s\" [%s] failed to play",
|
2017-04-28 21:45:47 +02:00
|
|
|
GetName(), output->plugin.name);
|
2009-02-26 22:04:59 +01:00
|
|
|
|
2016-10-28 20:33:57 +02:00
|
|
|
nbytes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nbytes == 0) {
|
2017-08-07 16:34:29 +02:00
|
|
|
InternalClose(false);
|
2009-03-09 19:08:35 +01:00
|
|
|
|
|
|
|
/* don't automatically reopen this device for
|
|
|
|
10 seconds */
|
2014-01-29 09:12:41 +01:00
|
|
|
assert(!fail_timer.IsDefined());
|
|
|
|
fail_timer.Update();
|
2010-11-04 23:40:43 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-10 22:41:11 +01:00
|
|
|
void
|
2017-04-28 21:45:47 +02:00
|
|
|
AudioOutputControl::Task()
|
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();
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
LogError(e,
|
|
|
|
"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
|
|
|
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<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
|
|
|
|
audio_output_all_pause() has
|
|
|
|
submitted the PAUSE command; bail
|
|
|
|
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
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::DRAIN:
|
2017-06-08 22:25:45 +02:00
|
|
|
if (open) {
|
2016-10-28 20:41:53 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2017-04-28 21:45:47 +02:00
|
|
|
ao_plugin_drain(*output);
|
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-04-28 21:45:47 +02:00
|
|
|
ao_plugin_cancel(*output);
|
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;
|
2017-06-08 22:23:26 +02:00
|
|
|
cond.wait(mutex);
|
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
|
|
|
|
2017-02-10 22:41:11 +01:00
|
|
|
thread.Start();
|
2008-09-24 07:20:26 +02:00
|
|
|
}
|