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"
|
2013-07-30 08:34:10 +02:00
|
|
|
#include "OutputAPI.hxx"
|
2014-01-28 11:42:54 +01:00
|
|
|
#include "Domain.hxx"
|
2013-04-09 01:24:52 +02:00
|
|
|
#include "pcm/PcmMix.hxx"
|
2013-01-10 10:44:04 +01:00
|
|
|
#include "notify.hxx"
|
2014-01-24 16:31:52 +01:00
|
|
|
#include "filter/FilterInternal.hxx"
|
|
|
|
#include "filter/plugins/ConvertFilterPlugin.hxx"
|
|
|
|
#include "filter/plugins/ReplayGainFilterPlugin.hxx"
|
2016-06-22 11:15:49 +02:00
|
|
|
#include "mixer/MixerInternal.hxx"
|
|
|
|
#include "mixer/plugins/SoftwareMixerPlugin.hxx"
|
2013-01-04 10:16:16 +01:00
|
|
|
#include "MusicPipe.hxx"
|
|
|
|
#include "MusicChunk.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"
|
2014-08-12 16:09:07 +02:00
|
|
|
#include "util/ConstBuffer.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"
|
2013-10-15 09:21:13 +02:00
|
|
|
#include "Compiler.h"
|
2008-09-24 07:20:26 +02:00
|
|
|
|
2016-09-04 15:11:01 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-27 14:55:56 +01:00
|
|
|
inline void
|
2014-01-29 09:12:41 +01:00
|
|
|
AudioOutput::Enable()
|
2009-10-23 10:55:52 +02:00
|
|
|
{
|
2014-01-29 09:12:41 +01:00
|
|
|
if (really_enabled)
|
2016-12-27 14:55:56 +01:00
|
|
|
return;
|
2009-10-23 10:55:52 +02:00
|
|
|
|
2016-10-28 20:33:57 +02:00
|
|
|
try {
|
2016-10-28 20:41:53 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2017-01-25 09:48:59 +01:00
|
|
|
ao_plugin_enable(*this);
|
2016-10-28 20:33:57 +02:00
|
|
|
} catch (const std::runtime_error &e) {
|
2016-12-27 14:55:56 +01:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to enable output \"%s\" [%s]",
|
|
|
|
name, plugin.name));
|
2009-10-23 10:55:52 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
really_enabled = true;
|
2009-10-23 10:55:52 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
inline void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutput::Disable() noexcept
|
2009-10-23 10:55:52 +02:00
|
|
|
{
|
2014-01-29 09:12:41 +01:00
|
|
|
if (open)
|
|
|
|
Close(false);
|
2009-10-23 10:55:52 +02:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
if (really_enabled) {
|
|
|
|
really_enabled = false;
|
2009-10-29 17:06:40 +01:00
|
|
|
|
2016-10-28 20:41:53 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2017-01-25 09:48:59 +01:00
|
|
|
ao_plugin_disable(*this);
|
2009-10-23 10:55:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutput::CloseFilter() noexcept
|
2010-05-02 17:38:02 +02:00
|
|
|
{
|
2016-06-22 11:15:49 +02:00
|
|
|
if (mixer != nullptr && mixer->IsPlugin(software_mixer_plugin))
|
|
|
|
software_mixer_set_filter(*mixer, nullptr);
|
2010-05-02 15:57:59 +02:00
|
|
|
|
2016-12-20 16:37:36 +01:00
|
|
|
source.Close();
|
2010-05-02 17:38:02 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
inline void
|
2017-04-28 21:45:47 +02:00
|
|
|
AudioOutput::Open(AudioFormat audio_format, const MusicPipe &pipe)
|
2009-07-06 08:04:18 +02:00
|
|
|
{
|
2017-04-28 21:45:47 +02:00
|
|
|
assert(audio_format.IsValid());
|
2010-11-07 15:30:18 +01:00
|
|
|
|
2009-10-23 10:55:52 +02:00
|
|
|
/* enable the device (just in case the last enable has failed) */
|
2016-12-27 14:55:56 +01:00
|
|
|
Enable();
|
2009-10-23 10:55:52 +02:00
|
|
|
|
2016-12-20 16:37:36 +01:00
|
|
|
AudioFormat f;
|
2016-12-21 17:00:29 +01:00
|
|
|
|
2016-12-20 16:37:36 +01:00
|
|
|
try {
|
2017-04-28 21:45:47 +02:00
|
|
|
f = source.Open(audio_format, pipe,
|
2016-12-20 16:37:36 +01:00
|
|
|
prepared_replay_gain_filter,
|
|
|
|
prepared_other_replay_gain_filter,
|
2017-01-15 01:21:14 +01:00
|
|
|
prepared_filter);
|
2016-12-27 14:44:39 +01:00
|
|
|
|
|
|
|
if (mixer != nullptr && mixer->IsPlugin(software_mixer_plugin))
|
|
|
|
software_mixer_set_filter(*mixer, volume_filter.Get());
|
2016-12-20 16:37:36 +01:00
|
|
|
} catch (const std::runtime_error &e) {
|
2016-12-27 14:55:56 +01:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to open filter for \"%s\" [%s]",
|
|
|
|
name, plugin.name));
|
2016-12-20 16:37:36 +01:00
|
|
|
}
|
2009-07-06 10:01:47 +02:00
|
|
|
|
2017-01-15 01:21:14 +01:00
|
|
|
const auto cf = f.WithMask(config_audio_format);
|
|
|
|
|
|
|
|
if (open && cf != 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 */
|
|
|
|
CloseOutput(true);
|
|
|
|
open = false;
|
2016-12-24 14:05:11 +01:00
|
|
|
}
|
2011-03-16 23:37:41 +01:00
|
|
|
|
2017-01-15 01:21:14 +01:00
|
|
|
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 {
|
|
|
|
OpenOutputAndConvert(filter_audio_format);
|
|
|
|
} catch (...) {
|
2016-12-24 14:05:11 +01:00
|
|
|
CloseFilter();
|
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-01-15 01:18:34 +01:00
|
|
|
} else if (f != out_audio_format) {
|
|
|
|
/* reconfigure the final ConvertFilter for its new
|
|
|
|
input AudioFormat */
|
|
|
|
|
|
|
|
try {
|
|
|
|
convert_filter_set(convert_filter.Get(),
|
|
|
|
out_audio_format);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
Close(false);
|
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to convert for \"%s\" [%s]",
|
|
|
|
name, plugin.name));
|
|
|
|
}
|
2016-12-24 14:05:11 +01:00
|
|
|
}
|
2017-01-15 01:23:49 +01:00
|
|
|
|
2017-01-17 22:04:31 +01:00
|
|
|
if (f != source.GetInputAudioFormat() || f != out_audio_format)
|
2017-01-15 01:23:49 +01:00
|
|
|
FormatDebug(output_domain, "converting in=%s -> f=%s -> out=%s",
|
2017-01-17 22:04:31 +01:00
|
|
|
ToString(source.GetInputAudioFormat()).c_str(),
|
|
|
|
ToString(f).c_str(),
|
|
|
|
ToString(out_audio_format).c_str());
|
2016-12-13 21:46:27 +01:00
|
|
|
}
|
|
|
|
|
2016-12-27 14:55:56 +01:00
|
|
|
void
|
2016-12-13 21:46:27 +01:00
|
|
|
AudioOutput::OpenOutputAndConvert(AudioFormat desired_audio_format)
|
|
|
|
{
|
|
|
|
out_audio_format = desired_audio_format;
|
2014-10-25 21:25:49 +02:00
|
|
|
|
2016-10-28 20:33:57 +02:00
|
|
|
try {
|
2017-01-25 09:48:59 +01:00
|
|
|
ao_plugin_open(*this, out_audio_format);
|
2016-10-28 20:33:57 +02:00
|
|
|
} catch (const std::runtime_error &e) {
|
2016-12-27 14:55:56 +01:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to open \"%s\" [%s]",
|
|
|
|
name, plugin.name));
|
2016-10-28 20:33:57 +02:00
|
|
|
}
|
|
|
|
|
2017-01-15 01:23:49 +01:00
|
|
|
FormatDebug(output_domain,
|
|
|
|
"opened plugin=%s name=\"%s\" audio_format=%s",
|
|
|
|
plugin.name, name,
|
2017-01-17 22:04:31 +01:00
|
|
|
ToString(out_audio_format).c_str());
|
2017-01-15 01:23:49 +01:00
|
|
|
|
2016-09-04 14:32:09 +02:00
|
|
|
try {
|
|
|
|
convert_filter_set(convert_filter.Get(), out_audio_format);
|
|
|
|
} catch (const std::runtime_error &e) {
|
2017-01-25 09:48:59 +01:00
|
|
|
ao_plugin_close(*this);
|
2014-10-25 21:25:49 +02:00
|
|
|
|
2016-11-09 11:59:31 +01:00
|
|
|
if (out_audio_format.format == SampleFormat::DSD) {
|
2014-10-25 21:25:49 +02:00
|
|
|
/* if the audio output supports DSD, but not
|
|
|
|
the given sample rate, it asks MPD to
|
|
|
|
resample; resampling DSD however is not
|
|
|
|
implemented; our last resort is to give up
|
|
|
|
DSD and fall back to PCM */
|
|
|
|
|
2016-12-27 14:55:56 +01:00
|
|
|
LogError(e);
|
2014-10-25 21:25:49 +02:00
|
|
|
FormatError(output_domain, "Retrying without DSD");
|
|
|
|
|
2016-12-13 21:46:27 +01:00
|
|
|
desired_audio_format.format = SampleFormat::FLOAT;
|
2016-12-27 14:55:56 +01:00
|
|
|
OpenOutputAndConvert(desired_audio_format);
|
|
|
|
return;
|
2014-10-25 21:25:49 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 14:55:56 +01:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to convert for \"%s\" [%s]",
|
|
|
|
name, plugin.name));
|
2013-11-11 16:15:38 +01:00
|
|
|
}
|
2009-07-06 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2017-05-23 00:08:36 +02:00
|
|
|
inline void
|
|
|
|
AudioOutputControl::InternalOpen(const AudioFormat audio_format,
|
|
|
|
const MusicPipe &pipe) noexcept
|
|
|
|
{
|
|
|
|
last_error = nullptr;
|
|
|
|
fail_timer.Reset();
|
|
|
|
|
|
|
|
try {
|
|
|
|
output->Open(audio_format, pipe);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
LogError(e);
|
|
|
|
fail_timer.Update();
|
|
|
|
last_error = std::current_exception();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutput::Close(bool drain) noexcept
|
2009-02-10 22:09:07 +01:00
|
|
|
{
|
2014-01-29 09:12:41 +01:00
|
|
|
assert(open);
|
2009-02-10 22:09:07 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
open = false;
|
2009-10-29 17:06:40 +01:00
|
|
|
|
2016-10-28 20:41:53 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2014-10-23 23:24:01 +02:00
|
|
|
CloseOutput(drain);
|
2014-01-29 09:12:41 +01:00
|
|
|
CloseFilter();
|
2009-03-08 04:13:55 +01:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(output_domain, "closed plugin=%s name=\"%s\"",
|
2014-01-29 09:12:41 +01:00
|
|
|
plugin.name, name);
|
2009-02-10 22:09:07 +01:00
|
|
|
}
|
|
|
|
|
2014-10-23 23:24:01 +02:00
|
|
|
inline void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutput::CloseOutput(bool drain) noexcept
|
2014-10-23 23:24:01 +02:00
|
|
|
{
|
|
|
|
if (drain)
|
2017-01-25 09:48:59 +01:00
|
|
|
ao_plugin_drain(*this);
|
2014-10-23 23:24:01 +02:00
|
|
|
else
|
2017-01-25 09:48:59 +01:00
|
|
|
ao_plugin_cancel(*this);
|
2014-10-23 23:24:01 +02:00
|
|
|
|
2017-01-25 09:48:59 +01:00
|
|
|
ao_plugin_close(*this);
|
2014-10-23 23:24:01 +02:00
|
|
|
}
|
|
|
|
|
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-04-28 21:45:47 +02:00
|
|
|
return output->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-04-28 21:45:47 +02:00
|
|
|
output->Close(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-04-28 21:45:47 +02:00
|
|
|
const auto *tag = output->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-04-28 21:45:47 +02:00
|
|
|
const auto data = output->source.PeekData();
|
2016-12-26 13:53:22 +01:00
|
|
|
if (data.IsEmpty())
|
|
|
|
break;
|
2008-12-24 03:08:39 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
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-04-28 21:45:47 +02:00
|
|
|
output->Close(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-04-28 21:45:47 +02:00
|
|
|
output->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
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
inline void
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutput::BeginPause() noexcept
|
2008-09-29 16:43:55 +02:00
|
|
|
{
|
2016-10-28 20:41:53 +02:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
2017-01-25 09:48:59 +01:00
|
|
|
ao_plugin_cancel(*this);
|
2016-10-28 20:41:53 +02:00
|
|
|
}
|
2009-02-16 01:38:10 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
pause = true;
|
2017-04-28 22:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-05-23 00:17:23 +02:00
|
|
|
AudioOutput::IteratePause() noexcept
|
2017-04-28 22:01:20 +02:00
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
success = ao_plugin_pause(*this);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
FormatError(e, "\"%s\" [%s] failed to pause",
|
|
|
|
name, plugin.name);
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
Close(false);
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2017-05-23 10:59:16 +02:00
|
|
|
AudioOutputControl::InternalPause() noexcept
|
2017-04-28 22:01:20 +02:00
|
|
|
{
|
2017-04-28 21:45:47 +02:00
|
|
|
output->BeginPause();
|
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-04-28 21:45:47 +02:00
|
|
|
if (!output->IteratePause())
|
2009-02-16 01:38:10 +01:00
|
|
|
break;
|
2014-12-24 22:11:57 +01:00
|
|
|
} while (command == Command::NONE);
|
2009-08-14 11:52:12 +02:00
|
|
|
|
2017-04-28 21:45:47 +02:00
|
|
|
output->EndPause();
|
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:
|
2016-12-27 07:36:12 +01:00
|
|
|
last_error = nullptr;
|
|
|
|
|
2016-12-27 14:55:56 +01:00
|
|
|
try {
|
2017-04-28 21:45:47 +02:00
|
|
|
output->Enable();
|
2016-12-27 14:55:56 +01:00
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
LogError(e);
|
|
|
|
fail_timer.Update();
|
2016-12-27 07:36:12 +01:00
|
|
|
last_error = std::current_exception();
|
2016-12-27 14:55:56 +01:00
|
|
|
}
|
|
|
|
|
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-04-28 21:45:47 +02:00
|
|
|
output->Disable();
|
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-04-28 21:45:47 +02:00
|
|
|
if (IsOpen())
|
|
|
|
output->Close(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-04-28 21:45:47 +02:00
|
|
|
if (!output->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-04-28 21:45:47 +02:00
|
|
|
if (output->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-04-28 21:45:47 +02:00
|
|
|
output->source.Cancel();
|
2011-07-20 18:35:24 +02:00
|
|
|
|
2017-04-28 21:45:47 +02:00
|
|
|
if (output->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-04-28 21:45:47 +02:00
|
|
|
output->Disable();
|
|
|
|
output->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-05-23 10:59:16 +02:00
|
|
|
if (output->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-04-28 21:45:47 +02:00
|
|
|
cond.wait(output->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
|
|
|
}
|