2017-08-07 16:26:50 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2017-08-07 21:50:13 +02:00
|
|
|
#include "Filtered.hxx"
|
2017-08-07 16:26:50 +02:00
|
|
|
#include "OutputPlugin.hxx"
|
|
|
|
#include "Domain.hxx"
|
|
|
|
#include "Log.hxx"
|
|
|
|
#include "mixer/MixerInternal.hxx"
|
|
|
|
#include "mixer/plugins/SoftwareMixerPlugin.hxx"
|
|
|
|
#include "filter/plugins/ConvertFilterPlugin.hxx"
|
|
|
|
#include "util/RuntimeError.hxx"
|
|
|
|
#include "util/StringBuffer.hxx"
|
|
|
|
|
2017-08-08 15:54:49 +02:00
|
|
|
bool
|
|
|
|
FilteredAudioOutput::SupportsEnableDisable() const noexcept
|
|
|
|
{
|
|
|
|
assert((plugin.enable == nullptr) == (plugin.disable == nullptr));
|
|
|
|
|
|
|
|
return plugin.enable != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FilteredAudioOutput::SupportsPause() const noexcept
|
|
|
|
{
|
|
|
|
return plugin.pause != nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-07 16:26:50 +02:00
|
|
|
void
|
2017-08-07 18:47:39 +02:00
|
|
|
FilteredAudioOutput::Enable()
|
2017-08-07 16:26:50 +02:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
ao_plugin_enable(*this);
|
|
|
|
} catch (const std::runtime_error &e) {
|
2017-08-08 14:02:58 +02:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to enable output %s",
|
|
|
|
GetLogName()));
|
2017-08-07 16:26:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-08-07 18:47:39 +02:00
|
|
|
FilteredAudioOutput::Disable() noexcept
|
2017-08-07 16:26:50 +02:00
|
|
|
{
|
|
|
|
ao_plugin_disable(*this);
|
|
|
|
}
|
|
|
|
|
2017-08-07 18:39:12 +02:00
|
|
|
void
|
2017-08-07 18:47:39 +02:00
|
|
|
FilteredAudioOutput::ConfigureConvertFilter()
|
2017-08-07 18:39:12 +02:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
convert_filter_set(convert_filter.Get(), out_audio_format);
|
|
|
|
} catch (const std::runtime_error &e) {
|
2017-08-08 14:02:58 +02:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to convert for %s",
|
|
|
|
GetLogName()));
|
2017-08-07 18:39:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 16:26:50 +02:00
|
|
|
void
|
2017-08-07 18:47:39 +02:00
|
|
|
FilteredAudioOutput::OpenOutputAndConvert(AudioFormat desired_audio_format)
|
2017-08-07 16:26:50 +02:00
|
|
|
{
|
|
|
|
out_audio_format = desired_audio_format;
|
|
|
|
|
|
|
|
try {
|
|
|
|
ao_plugin_open(*this, out_audio_format);
|
|
|
|
} catch (const std::runtime_error &e) {
|
2017-08-08 14:02:58 +02:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to open %s",
|
|
|
|
GetLogName()));
|
2017-08-07 16:26:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FormatDebug(output_domain,
|
2017-08-08 14:02:58 +02:00
|
|
|
"opened %s audio_format=%s",
|
|
|
|
GetLogName(),
|
2017-08-07 16:26:50 +02:00
|
|
|
ToString(out_audio_format).c_str());
|
|
|
|
|
|
|
|
try {
|
2017-08-07 18:39:12 +02:00
|
|
|
ConfigureConvertFilter();
|
2017-08-07 16:26:50 +02:00
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
ao_plugin_close(*this);
|
|
|
|
|
|
|
|
if (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 */
|
|
|
|
|
|
|
|
LogError(e);
|
|
|
|
FormatError(output_domain, "Retrying without DSD");
|
|
|
|
|
|
|
|
desired_audio_format.format = SampleFormat::FLOAT;
|
|
|
|
OpenOutputAndConvert(desired_audio_format);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-07 18:39:12 +02:00
|
|
|
throw;
|
2017-08-07 16:26:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-08-07 18:47:39 +02:00
|
|
|
FilteredAudioOutput::CloseOutput(bool drain) noexcept
|
2017-08-07 16:26:50 +02:00
|
|
|
{
|
|
|
|
if (drain)
|
2017-08-08 14:27:19 +02:00
|
|
|
Drain();
|
2017-08-07 16:26:50 +02:00
|
|
|
else
|
2017-08-08 14:27:19 +02:00
|
|
|
Cancel();
|
2017-08-07 16:26:50 +02:00
|
|
|
|
|
|
|
ao_plugin_close(*this);
|
|
|
|
}
|
|
|
|
|
2017-08-07 18:33:07 +02:00
|
|
|
void
|
2017-08-07 18:47:39 +02:00
|
|
|
FilteredAudioOutput::OpenSoftwareMixer() noexcept
|
2017-08-07 18:33:07 +02:00
|
|
|
{
|
|
|
|
if (mixer != nullptr && mixer->IsPlugin(software_mixer_plugin))
|
|
|
|
software_mixer_set_filter(*mixer, volume_filter.Get());
|
|
|
|
}
|
|
|
|
|
2017-08-07 16:26:50 +02:00
|
|
|
void
|
2017-08-07 18:47:39 +02:00
|
|
|
FilteredAudioOutput::CloseSoftwareMixer() noexcept
|
2017-08-07 16:26:50 +02:00
|
|
|
{
|
|
|
|
if (mixer != nullptr && mixer->IsPlugin(software_mixer_plugin))
|
|
|
|
software_mixer_set_filter(*mixer, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-08-07 18:47:39 +02:00
|
|
|
FilteredAudioOutput::Close(bool drain) noexcept
|
2017-08-07 16:26:50 +02:00
|
|
|
{
|
|
|
|
CloseOutput(drain);
|
2017-08-07 18:32:43 +02:00
|
|
|
CloseSoftwareMixer();
|
2017-08-07 16:26:50 +02:00
|
|
|
|
2017-08-08 14:02:58 +02:00
|
|
|
FormatDebug(output_domain, "closed %s", GetLogName());
|
2017-08-07 16:26:50 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 14:27:19 +02:00
|
|
|
std::chrono::steady_clock::duration
|
|
|
|
FilteredAudioOutput::Delay() noexcept
|
|
|
|
{
|
|
|
|
return ao_plugin_delay(*this);
|
|
|
|
}
|
|
|
|
|
2017-08-07 16:26:50 +02:00
|
|
|
void
|
2017-08-08 14:27:19 +02:00
|
|
|
FilteredAudioOutput::SendTag(const Tag &tag)
|
|
|
|
{
|
|
|
|
ao_plugin_send_tag(*this, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
FilteredAudioOutput::Play(const void *data, size_t size)
|
|
|
|
{
|
|
|
|
return ao_plugin_play(*this, data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FilteredAudioOutput::Drain()
|
|
|
|
{
|
|
|
|
ao_plugin_drain(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FilteredAudioOutput::Cancel() noexcept
|
2017-08-07 16:26:50 +02:00
|
|
|
{
|
|
|
|
ao_plugin_cancel(*this);
|
|
|
|
}
|
|
|
|
|
2017-08-08 14:27:19 +02:00
|
|
|
void
|
|
|
|
FilteredAudioOutput::BeginPause() noexcept
|
|
|
|
{
|
|
|
|
Cancel();
|
|
|
|
}
|
|
|
|
|
2017-08-07 16:26:50 +02:00
|
|
|
bool
|
2017-08-07 18:47:39 +02:00
|
|
|
FilteredAudioOutput::IteratePause() noexcept
|
2017-08-07 16:26:50 +02:00
|
|
|
{
|
|
|
|
try {
|
2017-08-07 17:40:13 +02:00
|
|
|
return ao_plugin_pause(*this);
|
2017-08-07 16:26:50 +02:00
|
|
|
} catch (const std::runtime_error &e) {
|
2017-08-08 14:02:58 +02:00
|
|
|
FormatError(e, "Failed to pause %s",
|
|
|
|
GetLogName());
|
2017-08-07 17:40:13 +02:00
|
|
|
return false;
|
2017-08-07 16:26:50 +02:00
|
|
|
}
|
|
|
|
}
|