Filtered.cxx 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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"
21
#include "Filtered.hxx"
22
#include "Interface.hxx"
23 24 25 26 27 28 29 30
#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"

31 32 33
bool
FilteredAudioOutput::SupportsEnableDisable() const noexcept
{
34
	return output->SupportsEnableDisable();
35 36 37 38 39
}

bool
FilteredAudioOutput::SupportsPause() const noexcept
{
40
	return output->SupportsPause();
41 42
}

43 44 45 46 47 48 49 50 51 52 53 54
const std::map<std::string, std::string>
FilteredAudioOutput::GetAttributes() const noexcept
{
	return output->GetAttributes();
}

void
FilteredAudioOutput::SetAttribute(std::string &&_name, std::string &&_value)
{
	output->SetAttribute(std::move(_name), std::move(_value));
}

55
void
56
FilteredAudioOutput::Enable()
57 58
{
	try {
59
		output->Enable();
60
	} catch (...) {
61 62
		std::throw_with_nested(FormatRuntimeError("Failed to enable output %s",
							  GetLogName()));
63 64 65 66
	}
}

void
67
FilteredAudioOutput::Disable() noexcept
68
{
69
	output->Disable();
70 71
}

72
void
73
FilteredAudioOutput::ConfigureConvertFilter()
74 75 76
{
	try {
		convert_filter_set(convert_filter.Get(), out_audio_format);
77
	} catch (...) {
78 79
		std::throw_with_nested(FormatRuntimeError("Failed to convert for %s",
							  GetLogName()));
80 81 82
	}
}

83
void
84
FilteredAudioOutput::OpenOutputAndConvert(AudioFormat desired_audio_format)
85 86 87 88
{
	out_audio_format = desired_audio_format;

	try {
89
		output->Open(out_audio_format);
90
	} catch (...) {
91 92
		std::throw_with_nested(FormatRuntimeError("Failed to open %s",
							  GetLogName()));
93 94 95
	}

	FormatDebug(output_domain,
96 97
		    "opened %s audio_format=%s",
		    GetLogName(),
98 99 100
		    ToString(out_audio_format).c_str());

	try {
101
		ConfigureConvertFilter();
102
	} catch (...) {
103
		output->Close();
104 105 106 107 108 109 110 111

		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 */

112
			LogError(std::current_exception());
113 114 115 116 117 118 119
			FormatError(output_domain, "Retrying without DSD");

			desired_audio_format.format = SampleFormat::FLOAT;
			OpenOutputAndConvert(desired_audio_format);
			return;
		}

120
		throw;
121 122 123 124
	}
}

void
125
FilteredAudioOutput::CloseOutput(bool drain) noexcept
126 127
{
	if (drain)
128
		Drain();
129
	else
130
		Cancel();
131

132
	output->Close();
133 134
}

135
void
136
FilteredAudioOutput::OpenSoftwareMixer() noexcept
137 138 139 140 141
{
	if (mixer != nullptr && mixer->IsPlugin(software_mixer_plugin))
		software_mixer_set_filter(*mixer, volume_filter.Get());
}

142
void
143
FilteredAudioOutput::CloseSoftwareMixer() noexcept
144 145 146 147 148 149
{
	if (mixer != nullptr && mixer->IsPlugin(software_mixer_plugin))
		software_mixer_set_filter(*mixer, nullptr);
}

void
150
FilteredAudioOutput::Close(bool drain) noexcept
151 152
{
	CloseOutput(drain);
153
	CloseSoftwareMixer();
154

155
	FormatDebug(output_domain, "closed %s", GetLogName());
156 157
}

158 159 160
std::chrono::steady_clock::duration
FilteredAudioOutput::Delay() noexcept
{
161
	return output->Delay();
162 163
}

164
void
165 166
FilteredAudioOutput::SendTag(const Tag &tag)
{
167
	output->SendTag(tag);
168 169 170 171 172
}

size_t
FilteredAudioOutput::Play(const void *data, size_t size)
{
173
	return output->Play(data, size);
174 175 176 177 178
}

void
FilteredAudioOutput::Drain()
{
179
	output->Drain();
180 181 182 183
}

void
FilteredAudioOutput::Cancel() noexcept
184
{
185
	output->Cancel();
186 187
}

188 189 190 191 192 193
void
FilteredAudioOutput::BeginPause() noexcept
{
	Cancel();
}

194
bool
195
FilteredAudioOutput::IteratePause() noexcept
196 197
{
	try {
198
		return output->Pause();
199 200
	} catch (...) {
		FormatError(std::current_exception(), "Failed to pause %s",
201
			    GetLogName());
202
		return false;
203 204
	}
}