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"
|
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"
|
2014-10-25 21:25:49 +02:00
|
|
|
#include "pcm/Domain.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"
|
2015-08-15 15:55:46 +02:00
|
|
|
#include "player/Control.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"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2014-08-12 16:09:07 +02:00
|
|
|
#include "util/ConstBuffer.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
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.unlock();
|
2013-01-10 10:44:04 +01:00
|
|
|
audio_output_client_notify.Signal();
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.lock();
|
2008-09-24 07:20:26 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
inline bool
|
|
|
|
AudioOutput::Enable()
|
2009-10-23 10:55:52 +02:00
|
|
|
{
|
2014-01-29 09:12:41 +01:00
|
|
|
if (really_enabled)
|
2009-10-23 10:55:52 +02:00
|
|
|
return true;
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.unlock();
|
|
|
|
Error error;
|
|
|
|
bool success = ao_plugin_enable(this, error);
|
|
|
|
mutex.lock();
|
2009-10-29 17:06:40 +01:00
|
|
|
if (!success) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(error,
|
|
|
|
"Failed to enable \"%s\" [%s]",
|
2014-01-29 09:12:41 +01:00
|
|
|
name, plugin.name);
|
2009-10-23 10:55:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
really_enabled = true;
|
2009-10-23 10:55:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.unlock();
|
|
|
|
ao_plugin_disable(this);
|
|
|
|
mutex.lock();
|
2009-10-23 10:55:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
inline AudioFormat
|
2016-09-04 14:32:09 +02:00
|
|
|
AudioOutput::OpenFilter(AudioFormat &format)
|
|
|
|
try {
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(format.IsValid());
|
2011-03-16 23:37:41 +01:00
|
|
|
|
2010-05-02 15:57:59 +02:00
|
|
|
/* the replay_gain filter cannot fail here */
|
2016-09-04 14:32:09 +02:00
|
|
|
if (prepared_replay_gain_filter != nullptr)
|
2016-06-22 11:15:49 +02:00
|
|
|
replay_gain_filter_instance =
|
2016-09-04 14:32:09 +02:00
|
|
|
prepared_replay_gain_filter->Open(format);
|
2013-12-24 10:52:30 +01:00
|
|
|
|
2016-09-04 14:32:09 +02:00
|
|
|
if (prepared_other_replay_gain_filter != nullptr)
|
2016-06-22 11:15:49 +02:00
|
|
|
other_replay_gain_filter_instance =
|
2016-09-04 14:32:09 +02:00
|
|
|
prepared_other_replay_gain_filter->Open(format);
|
2010-05-02 15:57:59 +02:00
|
|
|
|
2016-09-04 14:32:09 +02:00
|
|
|
filter_instance = prepared_filter->Open(format);
|
2010-05-02 15:57:59 +02:00
|
|
|
|
2016-06-22 11:15:49 +02:00
|
|
|
if (mixer != nullptr && mixer->IsPlugin(software_mixer_plugin))
|
|
|
|
software_mixer_set_filter(*mixer, volume_filter.Get());
|
|
|
|
|
|
|
|
return filter_instance->GetOutAudioFormat();
|
2016-09-04 14:32:09 +02:00
|
|
|
} catch (...) {
|
|
|
|
CloseFilter();
|
|
|
|
throw;
|
2010-05-02 17:38:02 +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-06-22 11:15:49 +02:00
|
|
|
delete replay_gain_filter_instance;
|
2016-09-04 15:09:05 +02:00
|
|
|
replay_gain_filter_instance = nullptr;
|
|
|
|
|
2016-06-22 11:15:49 +02:00
|
|
|
delete other_replay_gain_filter_instance;
|
2016-09-04 15:09:05 +02:00
|
|
|
other_replay_gain_filter_instance = nullptr;
|
|
|
|
|
2016-06-22 11:15:49 +02:00
|
|
|
delete filter_instance;
|
2016-09-04 15:09:05 +02:00
|
|
|
filter_instance = nullptr;
|
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
|
|
|
{
|
|
|
|
bool success;
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2009-11-10 17:57:14 +01:00
|
|
|
struct audio_format_string af_string;
|
2009-07-06 08:04:18 +02:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
assert(!open);
|
|
|
|
assert(pipe != nullptr);
|
|
|
|
assert(current_chunk == nullptr);
|
|
|
|
assert(in_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) */
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
if (!Enable())
|
2009-10-23 10:55:52 +02:00
|
|
|
/* still no luck */
|
|
|
|
return;
|
|
|
|
|
2009-07-06 10:01:47 +02:00
|
|
|
/* open the filter */
|
|
|
|
|
2016-09-04 14:32:09 +02:00
|
|
|
AudioFormat filter_audio_format;
|
|
|
|
try {
|
|
|
|
filter_audio_format = OpenFilter(in_audio_format);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
FormatError(e, "Failed to open filter for \"%s\" [%s]",
|
2014-01-29 09:12:41 +01:00
|
|
|
name, plugin.name);
|
2009-07-06 10:01:47 +02:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
fail_timer.Update();
|
2009-07-06 10:01:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(filter_audio_format.IsValid());
|
2011-03-16 23:37:41 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
out_audio_format = filter_audio_format;
|
|
|
|
out_audio_format.ApplyMask(config_audio_format);
|
2009-07-06 10:01:47 +02:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.unlock();
|
2014-10-25 21:25:49 +02:00
|
|
|
|
|
|
|
const AudioFormat retry_audio_format = out_audio_format;
|
|
|
|
|
|
|
|
retry_without_dsd:
|
2014-01-29 09:12:41 +01:00
|
|
|
success = ao_plugin_open(this, out_audio_format, error);
|
|
|
|
mutex.lock();
|
2009-07-06 08:04:18 +02:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
assert(!open);
|
2009-07-06 08:04:18 +02:00
|
|
|
|
|
|
|
if (!success) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(error, "Failed to open \"%s\" [%s]",
|
2014-01-29 09:12:41 +01:00
|
|
|
name, plugin.name);
|
2009-07-06 08:04:18 +02:00
|
|
|
|
2014-10-24 00:28:58 +02:00
|
|
|
mutex.unlock();
|
2014-01-29 09:12:41 +01:00
|
|
|
CloseFilter();
|
2014-10-24 00:28:58 +02:00
|
|
|
mutex.lock();
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
fail_timer.Update();
|
2009-07-06 08:04:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-04 14:32:09 +02:00
|
|
|
try {
|
|
|
|
convert_filter_set(convert_filter.Get(), out_audio_format);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
FormatError(e, "Failed to convert for \"%s\" [%s]",
|
2014-01-29 09:12:41 +01:00
|
|
|
name, plugin.name);
|
2013-11-11 16:15:38 +01:00
|
|
|
|
2014-10-24 00:28:58 +02:00
|
|
|
mutex.unlock();
|
2014-10-23 23:19:40 +02:00
|
|
|
ao_plugin_close(this);
|
2014-10-25 21:25:49 +02:00
|
|
|
|
|
|
|
if (error.IsDomain(pcm_domain) &&
|
|
|
|
out_audio_format.format == SampleFormat::DSD) {
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
// TODO: clean up this workaround
|
|
|
|
|
|
|
|
FormatError(output_domain, "Retrying without DSD");
|
|
|
|
|
|
|
|
out_audio_format = retry_audio_format;
|
|
|
|
out_audio_format.format = SampleFormat::FLOAT;
|
|
|
|
|
|
|
|
/* clear the Error to allow reusing it */
|
|
|
|
error.Clear();
|
|
|
|
|
|
|
|
/* sorry for the "goto" - this is a workaround
|
|
|
|
for the stable branch that should be as
|
|
|
|
unintrusive as possible */
|
|
|
|
goto retry_without_dsd;
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
CloseFilter();
|
2014-10-24 00:28:58 +02:00
|
|
|
mutex.lock();
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
fail_timer.Update();
|
2013-11-11 16:15:38 +01:00
|
|
|
return;
|
|
|
|
}
|
2009-07-06 08:04:18 +02:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
open = true;
|
2009-07-06 08:04:18 +02:00
|
|
|
|
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
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
if (in_audio_format != out_audio_format)
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(output_domain, "converting from %s",
|
2014-01-29 09:12:41 +01:00
|
|
|
audio_format_to_string(in_audio_format,
|
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
|
|
|
pipe = nullptr;
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
current_chunk = nullptr;
|
|
|
|
open = false;
|
2009-10-29 17:06:40 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.unlock();
|
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
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.lock();
|
2009-10-29 17:06:40 +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);
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
void
|
|
|
|
AudioOutput::ReopenFilter()
|
2009-07-06 10:01:47 +02:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2009-07-06 10:01:47 +02:00
|
|
|
|
2014-10-24 00:28:58 +02:00
|
|
|
mutex.unlock();
|
2014-01-29 09:12:41 +01:00
|
|
|
CloseFilter();
|
2014-10-24 00:28:58 +02:00
|
|
|
mutex.lock();
|
|
|
|
|
2016-09-04 14:32:09 +02:00
|
|
|
AudioFormat filter_audio_format;
|
|
|
|
try {
|
|
|
|
filter_audio_format = OpenFilter(in_audio_format);
|
|
|
|
convert_filter_set(convert_filter.Get(), out_audio_format);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
FormatError(e,
|
2013-09-27 22:31:24 +02:00
|
|
|
"Failed to open filter for \"%s\" [%s]",
|
2014-01-29 09:12:41 +01:00
|
|
|
name, plugin.name);
|
2009-07-06 10:01:47 +02:00
|
|
|
|
2016-09-04 15:09:51 +02:00
|
|
|
Close(false);
|
2009-07-06 10:01:47 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
void
|
|
|
|
AudioOutput::Reopen()
|
2009-07-06 10:01:02 +02:00
|
|
|
{
|
2014-01-29 09:12:41 +01:00
|
|
|
if (!config_audio_format.IsFullyDefined()) {
|
|
|
|
if (open) {
|
|
|
|
const MusicPipe *mp = pipe;
|
|
|
|
Close(true);
|
|
|
|
pipe = mp;
|
2009-07-06 10:01:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* no audio format is configured: copy in->out, let
|
|
|
|
the output's open() method determine the effective
|
|
|
|
out_audio_format */
|
2014-01-29 09:12:41 +01:00
|
|
|
out_audio_format = in_audio_format;
|
|
|
|
out_audio_format.ApplyMask(config_audio_format);
|
2009-07-06 10:01:02 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
if (open)
|
2009-07-06 10:01:47 +02:00
|
|
|
/* the audio format has changed, and all filters have
|
|
|
|
to be reconfigured */
|
2014-01-29 09:12:41 +01:00
|
|
|
ReopenFilter();
|
2009-07-06 10:01:47 +02:00
|
|
|
else
|
2014-01-29 09:12:41 +01:00
|
|
|
Open();
|
2009-07-06 10:01:02 +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
|
|
|
|
AudioOutput::WaitForDelay()
|
2010-11-05 08:02:38 +01:00
|
|
|
{
|
|
|
|
while (true) {
|
2014-01-29 09:12:41 +01:00
|
|
|
unsigned delay = ao_plugin_delay(this);
|
2010-11-05 08:02:38 +01:00
|
|
|
if (delay == 0)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-12 16:09:07 +02:00
|
|
|
static ConstBuffer<void>
|
2014-08-12 15:56:41 +02:00
|
|
|
ao_chunk_data(AudioOutput *ao, const MusicChunk *chunk,
|
2013-02-01 18:40:36 +01:00
|
|
|
Filter *replay_gain_filter,
|
2014-08-12 16:09:07 +02:00
|
|
|
unsigned *replay_gain_serial_p)
|
2008-09-24 07:20:26 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(chunk != nullptr);
|
2013-01-04 21:38:46 +01:00
|
|
|
assert(!chunk->IsEmpty());
|
|
|
|
assert(chunk->CheckFormat(ao->in_audio_format));
|
2008-11-02 20:16:56 +01:00
|
|
|
|
2014-08-12 16:09:07 +02:00
|
|
|
ConstBuffer<void> data(chunk->data, chunk->length);
|
2010-05-02 16:32:38 +02:00
|
|
|
|
|
|
|
(void)ao;
|
|
|
|
|
2014-08-12 16:09:07 +02:00
|
|
|
assert(data.size % ao->in_audio_format.GetFrameSize() == 0);
|
2010-05-02 16:32:38 +02:00
|
|
|
|
2014-08-12 16:09:07 +02:00
|
|
|
if (!data.IsEmpty() && replay_gain_filter != nullptr) {
|
2016-06-22 11:15:49 +02:00
|
|
|
replay_gain_filter_set_mode(replay_gain_filter,
|
|
|
|
ao->replay_gain_mode);
|
|
|
|
|
2010-05-02 15:57:59 +02:00
|
|
|
if (chunk->replay_gain_serial != *replay_gain_serial_p) {
|
|
|
|
replay_gain_filter_set_info(replay_gain_filter,
|
|
|
|
chunk->replay_gain_serial != 0
|
|
|
|
? &chunk->replay_gain_info
|
2013-10-19 18:19:03 +02:00
|
|
|
: nullptr);
|
2010-05-02 15:57:59 +02:00
|
|
|
*replay_gain_serial_p = chunk->replay_gain_serial;
|
|
|
|
}
|
|
|
|
|
2016-09-04 14:32:09 +02:00
|
|
|
try {
|
|
|
|
data = replay_gain_filter->FilterPCM(data);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
FormatError(e, "\"%s\" [%s] failed to filter",
|
2014-01-29 00:53:49 +01:00
|
|
|
ao->name, ao->plugin.name);
|
2016-09-04 14:32:09 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2010-05-02 15:57:59 +02:00
|
|
|
}
|
|
|
|
|
2010-05-02 16:32:38 +02:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2014-08-12 16:09:07 +02:00
|
|
|
static ConstBuffer<void>
|
|
|
|
ao_filter_chunk(AudioOutput *ao, const MusicChunk *chunk)
|
2010-05-02 16:32:38 +02:00
|
|
|
{
|
2014-08-12 16:09:07 +02:00
|
|
|
ConstBuffer<void> data =
|
2016-06-22 11:15:49 +02:00
|
|
|
ao_chunk_data(ao, chunk, ao->replay_gain_filter_instance,
|
2014-08-12 16:09:07 +02:00
|
|
|
&ao->replay_gain_serial);
|
|
|
|
if (data.IsEmpty())
|
2010-05-02 16:32:38 +02:00
|
|
|
return data;
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2010-05-02 15:31:31 +02:00
|
|
|
/* cross-fade */
|
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (chunk->other != nullptr) {
|
2014-08-12 16:09:07 +02:00
|
|
|
ConstBuffer<void> other_data =
|
2010-05-02 15:57:59 +02:00
|
|
|
ao_chunk_data(ao, chunk->other,
|
2016-06-22 11:15:49 +02:00
|
|
|
ao->other_replay_gain_filter_instance,
|
2014-08-12 16:09:07 +02:00
|
|
|
&ao->other_replay_gain_serial);
|
|
|
|
if (other_data.IsNull())
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2010-05-02 15:57:59 +02:00
|
|
|
|
2014-08-12 16:09:07 +02:00
|
|
|
if (other_data.IsEmpty())
|
2010-05-02 15:31:31 +02:00
|
|
|
return data;
|
|
|
|
|
|
|
|
/* if the "other" chunk is longer, then that trailer
|
|
|
|
is used as-is, without mixing; it is part of the
|
|
|
|
"next" song being faded in, and if there's a rest,
|
|
|
|
it means cross-fading ends here */
|
|
|
|
|
2014-08-12 16:09:07 +02:00
|
|
|
if (data.size > other_data.size)
|
|
|
|
data.size = other_data.size;
|
2010-05-02 15:31:31 +02:00
|
|
|
|
2014-09-24 21:49:20 +02:00
|
|
|
float mix_ratio = chunk->mix_ratio;
|
|
|
|
if (mix_ratio >= 0)
|
|
|
|
/* reverse the mix ratio (because the
|
|
|
|
arguments to pcm_mix() are reversed), but
|
|
|
|
only if the mix ratio is non-negative; a
|
|
|
|
negative mix ratio is a MixRamp special
|
|
|
|
case */
|
|
|
|
mix_ratio = 1.0 - mix_ratio;
|
|
|
|
|
2014-08-12 16:09:07 +02:00
|
|
|
void *dest = ao->cross_fade_buffer.Get(other_data.size);
|
|
|
|
memcpy(dest, other_data.data, other_data.size);
|
|
|
|
if (!pcm_mix(ao->cross_fade_dither, dest, data.data, data.size,
|
2013-08-03 21:00:50 +02:00
|
|
|
ao->in_audio_format.format,
|
2014-09-24 21:49:20 +02:00
|
|
|
mix_ratio)) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(output_domain,
|
|
|
|
"Cannot cross-fade format %s",
|
|
|
|
sample_format_to_string(ao->in_audio_format.format));
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2011-10-20 02:25:40 +02:00
|
|
|
}
|
2010-05-02 15:31:31 +02:00
|
|
|
|
2014-08-12 16:09:07 +02:00
|
|
|
data.data = dest;
|
|
|
|
data.size = other_data.size;
|
2010-05-02 15:31:31 +02:00
|
|
|
}
|
|
|
|
|
2010-05-02 16:32:38 +02:00
|
|
|
/* apply filter chain */
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2016-09-04 14:32:09 +02:00
|
|
|
try {
|
|
|
|
return ao->filter_instance->FilterPCM(data);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
FormatError(e, "\"%s\" [%s] failed to filter",
|
2014-01-29 00:53:49 +01:00
|
|
|
ao->name, ao->plugin.name);
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2010-05-02 16:32:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
inline bool
|
2014-08-12 15:56:41 +02:00
|
|
|
AudioOutput::PlayChunk(const MusicChunk *chunk)
|
2010-05-02 16:32:38 +02:00
|
|
|
{
|
2016-06-22 11:15:49 +02:00
|
|
|
assert(filter_instance != nullptr);
|
2010-05-02 16:32:38 +02:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
if (tags && gcc_unlikely(chunk->tag != nullptr)) {
|
|
|
|
mutex.unlock();
|
2014-12-26 22:27:01 +01:00
|
|
|
ao_plugin_send_tag(this, *chunk->tag);
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.lock();
|
2010-05-02 16:32:38 +02:00
|
|
|
}
|
|
|
|
|
2014-08-12 16:09:07 +02:00
|
|
|
auto data = ConstBuffer<char>::FromVoid(ao_filter_chunk(this, chunk));
|
|
|
|
if (data.IsNull()) {
|
2014-01-29 09:12:41 +01:00
|
|
|
Close(false);
|
2009-07-06 10:01:47 +02:00
|
|
|
|
|
|
|
/* don't automatically reopen this device for 10
|
|
|
|
seconds */
|
2014-01-29 09:12:41 +01:00
|
|
|
fail_timer.Update();
|
2009-07-06 10:01:47 +02:00
|
|
|
return false;
|
2008-12-24 03:08:39 +01:00
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
while (!data.IsEmpty() && command == Command::NONE) {
|
2014-01-29 09:12:41 +01:00
|
|
|
if (!WaitForDelay())
|
2010-11-05 08:02:38 +01:00
|
|
|
break;
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.unlock();
|
2014-08-12 16:09:07 +02:00
|
|
|
size_t nbytes = ao_plugin_play(this, data.data, data.size,
|
|
|
|
error);
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.lock();
|
2009-02-23 09:29:56 +01:00
|
|
|
if (nbytes == 0) {
|
|
|
|
/* play()==0 means failure */
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(error, "\"%s\" [%s] failed to play",
|
2014-01-29 09:12:41 +01:00
|
|
|
name, plugin.name);
|
2009-02-26 22:04:59 +01:00
|
|
|
|
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-08-12 16:09:07 +02:00
|
|
|
assert(nbytes <= data.size);
|
2014-01-29 09:12:41 +01:00
|
|
|
assert(nbytes % out_audio_format.GetFrameSize() == 0);
|
2009-02-23 09:29:56 +01:00
|
|
|
|
2014-08-12 16:09:07 +02:00
|
|
|
data.data += nbytes;
|
|
|
|
data.size -= nbytes;
|
2008-10-29 20:40:33 +01:00
|
|
|
}
|
|
|
|
|
2009-03-09 19:25:26 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-12 15:56:41 +02:00
|
|
|
inline const MusicChunk *
|
2014-01-29 09:12:41 +01:00
|
|
|
AudioOutput::GetNextChunk() const
|
2009-11-02 20:20:14 +01:00
|
|
|
{
|
2014-01-29 09:12:41 +01:00
|
|
|
return current_chunk != nullptr
|
2009-11-02 20:20:14 +01:00
|
|
|
/* continue the previous play() call */
|
2014-01-29 09:12:41 +01:00
|
|
|
? current_chunk->next
|
2009-11-02 20:20:14 +01:00
|
|
|
/* get the first chunk from the pipe */
|
2014-01-29 09:12:41 +01:00
|
|
|
: pipe->Peek();
|
2009-11-02 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
inline bool
|
|
|
|
AudioOutput::Play()
|
2009-03-09 19:25:26 +01:00
|
|
|
{
|
2014-01-29 09:12:41 +01:00
|
|
|
assert(pipe != nullptr);
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2014-08-12 15:56:41 +02:00
|
|
|
const MusicChunk *chunk = GetNextChunk();
|
2013-10-19 18:19:03 +02:00
|
|
|
if (chunk == nullptr)
|
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
|
|
|
current_chunk_finished = false;
|
2009-03-09 19:25:26 +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
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
while (chunk != nullptr && command == Command::NONE) {
|
2014-01-29 09:12:41 +01:00
|
|
|
assert(!current_chunk_finished);
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
current_chunk = chunk;
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
if (!PlayChunk(chunk)) {
|
|
|
|
assert(current_chunk == nullptr);
|
2009-03-09 19:25:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
assert(current_chunk == chunk);
|
2009-03-09 19:25:26 +01:00
|
|
|
chunk = chunk->next;
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
assert(in_playback_loop);
|
|
|
|
in_playback_loop = false;
|
2013-11-06 23:47:30 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
current_chunk_finished = true;
|
2009-03-25 18:04:41 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.unlock();
|
|
|
|
player_control->LockSignal();
|
|
|
|
mutex.lock();
|
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
|
|
|
{
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.unlock();
|
|
|
|
ao_plugin_cancel(this);
|
|
|
|
mutex.lock();
|
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;
|
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.unlock();
|
|
|
|
bool success = ao_plugin_pause(this);
|
|
|
|
mutex.lock();
|
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
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.lock();
|
2009-10-29 17:06:40 +01:00
|
|
|
|
2008-09-24 07:20:26 +02:00
|
|
|
while (1) {
|
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:
|
2014-01-29 09:12:41 +01:00
|
|
|
Enable();
|
|
|
|
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:
|
2014-01-29 09:12:41 +01:00
|
|
|
Open();
|
|
|
|
CommandFinished();
|
2008-09-24 07:20:26 +02:00
|
|
|
break;
|
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::REOPEN:
|
2014-01-29 09:12:41 +01:00
|
|
|
Reopen();
|
|
|
|
CommandFinished();
|
2009-07-06 10:01:02 +02:00
|
|
|
break;
|
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
case Command::CLOSE:
|
2014-01-29 09:12:41 +01:00
|
|
|
assert(open);
|
|
|
|
assert(pipe != nullptr);
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
Close(false);
|
|
|
|
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) {
|
|
|
|
assert(current_chunk == nullptr);
|
|
|
|
assert(pipe->Peek() == nullptr);
|
2009-11-09 22:16:26 +01:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
mutex.unlock();
|
|
|
|
ao_plugin_drain(this);
|
|
|
|
mutex.lock();
|
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:
|
2014-01-29 09:12:41 +01:00
|
|
|
current_chunk = nullptr;
|
2011-07-20 18:35:24 +02:00
|
|
|
|
2014-01-29 09:12:41 +01:00
|
|
|
if (open) {
|
|
|
|
mutex.unlock();
|
|
|
|
ao_plugin_cancel(this);
|
|
|
|
mutex.lock();
|
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:
|
2014-01-29 09:12:41 +01:00
|
|
|
current_chunk = nullptr;
|
|
|
|
CommandFinished();
|
|
|
|
mutex.unlock();
|
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
|
|
|
}
|