2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 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"
|
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"
|
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
|
|
|
|
AudioOutput::CommandFinished()
|
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);
|
2016-11-09 11:56:01 +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
|
|
|
|
AudioOutput::Disable()
|
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);
|
2014-01-29 09:12:41 +01:00
|
|
|
ao_plugin_disable(this);
|
2009-10-23 10:55:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
void
|
|
|
|
AudioOutput::CloseFilter()
|
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
|
|
|
|
AudioOutput::Open()
|
2009-07-06 08:04:18 +02:00
|
|
|
{
|
2016-12-21 17:00:29 +01:00
|
|
|
assert(request.audio_format.IsValid());
|
2009-07-06 08:04:18 +02:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
fail_timer.Reset();
|
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 {
|
|
|
|
f = source.Open(request.audio_format, *request.pipe,
|
|
|
|
prepared_replay_gain_filter,
|
|
|
|
prepared_other_replay_gain_filter,
|
|
|
|
prepared_filter)
|
|
|
|
.WithMask(config_audio_format);
|
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
|
|
|
|
2016-12-20 16:37:36 +01:00
|
|
|
if (open && f != filter_audio_format) {
|
|
|
|
/* 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
|
|
|
|
2016-12-20 16:37:36 +01:00
|
|
|
filter_audio_format = f;
|
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;
|
2016-12-24 14:05:11 +01:00
|
|
|
}
|
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 {
|
2016-11-09 11:56:01 +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
|
|
|
}
|
|
|
|
|
2016-09-04 14:32:09 +02:00
|
|
|
try {
|
|
|
|
convert_filter_set(convert_filter.Get(), out_audio_format);
|
|
|
|
} catch (const std::runtime_error &e) {
|
2014-10-23 23:19:40 +02: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
|
|
|
|
2016-12-13 21:46:27 +01:00
|
|
|
struct audio_format_string af_string;
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(output_domain,
|
|
|
|
"opened plugin=%s name=\"%s\" audio_format=%s",
|
2014-01-29 09:12:41 +01:00
|
|
|
plugin.name, name,
|
|
|
|
audio_format_to_string(out_audio_format, &af_string));
|
2009-07-06 08:04:18 +02:00
|
|
|
|
2016-12-20 16:37:36 +01:00
|
|
|
if (source.GetInputAudioFormat() != out_audio_format)
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(output_domain, "converting from %s",
|
2016-12-20 16:37:36 +01:00
|
|
|
audio_format_to_string(source.GetInputAudioFormat(),
|
2013-09-27 22:31:24 +02:00
|
|
|
&af_string));
|
2009-07-06 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
void
|
|
|
|
AudioOutput::Close(bool drain)
|
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
|
|
|
|
AudioOutput::CloseOutput(bool drain)
|
|
|
|
{
|
|
|
|
if (drain)
|
|
|
|
ao_plugin_drain(this);
|
|
|
|
else
|
|
|
|
ao_plugin_cancel(this);
|
|
|
|
|
|
|
|
ao_plugin_close(this);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
AudioOutput::WaitForDelay()
|
2010-11-05 08:02:38 +01:00
|
|
|
{
|
|
|
|
while (true) {
|
2016-12-28 21:44:18 +01:00
|
|
|
const auto delay = ao_plugin_delay(this);
|
|
|
|
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
|
|
|
|
AudioOutput::FillSourceOrClose()
|
|
|
|
try {
|
2016-12-26 20:02:15 +01: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]",
|
|
|
|
name, plugin.name);
|
|
|
|
|
|
|
|
Close(false);
|
|
|
|
|
|
|
|
/* don't automatically reopen this device for 10
|
|
|
|
seconds */
|
|
|
|
fail_timer.Update();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
inline bool
|
2016-12-26 13:53:22 +01:00
|
|
|
AudioOutput::PlayChunk()
|
2010-05-02 16:32:38 +02:00
|
|
|
{
|
2016-12-26 13:53:22 +01:00
|
|
|
if (tags) {
|
|
|
|
const auto *tag = source.ReadTag();
|
|
|
|
if (tag != nullptr) {
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
try {
|
|
|
|
ao_plugin_send_tag(this, *tag);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
FormatError(e, "Failed to send tag to \"%s\" [%s]",
|
|
|
|
name, plugin.name);
|
|
|
|
}
|
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) {
|
|
|
|
const auto data = source.PeekData();
|
|
|
|
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);
|
2016-11-09 11:56:01 +01:00
|
|
|
nbytes = ao_plugin_play(this, data.data, data.size);
|
2016-10-28 20:33:57 +02:00
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
FormatError(e, "\"%s\" [%s] failed to play",
|
2014-01-29 09:12:41 +01:00
|
|
|
name, plugin.name);
|
2009-02-26 22:04:59 +01:00
|
|
|
|
2016-10-28 20:33:57 +02:00
|
|
|
nbytes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nbytes == 0) {
|
2014-01-29 09:12:41 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
assert(nbytes % out_audio_format.GetFrameSize() == 0);
|
2009-02-23 09:29:56 +01:00
|
|
|
|
2016-12-26 13:53:22 +01: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
|
|
|
|
AudioOutput::Play()
|
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);
|
2016-12-14 12:14:57 +01: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);
|
2016-12-14 12:14:57 +01: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
|
|
|
|
AudioOutput::Pause()
|
2008-09-29 16:43:55 +02:00
|
|
|
{
|
2016-10-28 20:41:53 +02:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
ao_plugin_cancel(this);
|
|
|
|
}
|
2009-02-16 01:38:10 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
pause = true;
|
|
|
|
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;
|
|
|
|
|
2016-10-28 20:41:53 +02:00
|
|
|
bool success;
|
2016-11-02 10:53:15 +01:00
|
|
|
try {
|
2016-10-28 20:41:53 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
success = ao_plugin_pause(this);
|
2016-11-02 10:53:15 +01:00
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
FormatError(e, "\"%s\" [%s] failed to pause",
|
|
|
|
name, plugin.name);
|
|
|
|
success = false;
|
2016-10-28 20:41:53 +02:00
|
|
|
}
|
2009-10-29 17:06:40 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
if (!success) {
|
|
|
|
Close(false);
|
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
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
pause = false;
|
2008-09-29 16:43:55 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
inline void
|
|
|
|
AudioOutput::Task()
|
2008-09-24 07:20:26 +02:00
|
|
|
{
|
2014-01-29 09:12:41 +01:00
|
|
|
FormatThreadName("output:%s", name);
|
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 {
|
|
|
|
Enable();
|
|
|
|
} 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:
|
2014-01-29 09:12:41 +01:00
|
|
|
Disable();
|
|
|
|
CommandFinished();
|
2009-10-23 10:55:52 +02:00
|
|
|
break;
|
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::OPEN:
|
2016-12-27 07:36:12 +01:00
|
|
|
last_error = nullptr;
|
|
|
|
|
2016-12-27 14:55:56 +01:00
|
|
|
try {
|
|
|
|
Open();
|
|
|
|
} 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-07-06 10:01:02 +02:00
|
|
|
break;
|
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::CLOSE:
|
2016-12-27 07:57:22 +01:00
|
|
|
if (open)
|
|
|
|
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:
|
2014-01-29 09:12:41 +01: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;
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
Pause();
|
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:
|
2014-01-29 09:12:41 +01:00
|
|
|
if (open) {
|
2016-10-28 20:41:53 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2014-01-29 09:12:41 +01:00
|
|
|
ao_plugin_drain(this);
|
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:
|
2016-12-20 16:37:36 +01:00
|
|
|
source.Cancel();
|
2011-07-20 18:35:24 +02:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
if (open) {
|
2016-10-28 20:41:53 +02:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2014-01-29 09:12:41 +01:00
|
|
|
ao_plugin_cancel(this);
|
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:
|
2016-12-14 08:29:09 +01:00
|
|
|
Disable();
|
2016-12-20 16:37:36 +01: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
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
if (open && allow_play && Play())
|
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;
|
|
|
|
cond.wait(mutex);
|
2013-11-06 23:47:30 +01:00
|
|
|
}
|
2008-09-24 07:20:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
void
|
|
|
|
AudioOutput::Task(void *arg)
|
|
|
|
{
|
|
|
|
AudioOutput *ao = (AudioOutput *)arg;
|
|
|
|
ao->Task();
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
|
|
|
AudioOutput::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
|
|
|
|
2016-06-17 19:06:30 +02:00
|
|
|
thread.Start(Task, this);
|
2008-09-24 07:20:26 +02:00
|
|
|
}
|