Filtered.cxx 4.4 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "Filtered.hxx"
21
#include "Interface.hxx"
22 23 24 25 26 27 28 29
#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"

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

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

42 43 44 45 46 47 48 49 50 51 52 53
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));
}

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

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

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

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

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

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

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

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

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

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

119
		throw;
120 121 122 123
	}
}

void
124
FilteredAudioOutput::CloseOutput(bool drain) noexcept
125
{
126 127 128 129 130 131 132 133
	if (drain) {
		try {
			Drain();
		} catch (...) {
			FormatError(std::current_exception(),
				    "Failed to drain %s", GetLogName());
		}
	} else
134
		Cancel();
135

136
	output->Close();
137 138
}

139
void
140
FilteredAudioOutput::OpenSoftwareMixer() noexcept
141 142 143 144 145
{
	if (mixer != nullptr && mixer->IsPlugin(software_mixer_plugin))
		software_mixer_set_filter(*mixer, volume_filter.Get());
}

146
void
147
FilteredAudioOutput::CloseSoftwareMixer() noexcept
148 149 150 151 152 153
{
	if (mixer != nullptr && mixer->IsPlugin(software_mixer_plugin))
		software_mixer_set_filter(*mixer, nullptr);
}

void
154
FilteredAudioOutput::Close(bool drain) noexcept
155 156
{
	CloseOutput(drain);
157
	CloseSoftwareMixer();
158

159
	FormatDebug(output_domain, "closed %s", GetLogName());
160 161
}

162 163 164
std::chrono::steady_clock::duration
FilteredAudioOutput::Delay() noexcept
{
165
	return output->Delay();
166 167
}

168
void
169 170
FilteredAudioOutput::SendTag(const Tag &tag)
{
171
	output->SendTag(tag);
172 173 174 175 176
}

size_t
FilteredAudioOutput::Play(const void *data, size_t size)
{
177
	return output->Play(data, size);
178 179 180 181 182
}

void
FilteredAudioOutput::Drain()
{
183
	output->Drain();
184 185 186 187
}

void
FilteredAudioOutput::Cancel() noexcept
188
{
189
	output->Cancel();
190 191
}

192 193 194 195 196 197
void
FilteredAudioOutput::BeginPause() noexcept
{
	Cancel();
}

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