Filtered.cxx 4.1 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
void
44
FilteredAudioOutput::Enable()
45 46
{
	try {
47
		output->Enable();
48
	} catch (const std::runtime_error &e) {
49 50
		std::throw_with_nested(FormatRuntimeError("Failed to enable output %s",
							  GetLogName()));
51 52 53 54
	}
}

void
55
FilteredAudioOutput::Disable() noexcept
56
{
57
	output->Disable();
58 59
}

60
void
61
FilteredAudioOutput::ConfigureConvertFilter()
62 63 64 65
{
	try {
		convert_filter_set(convert_filter.Get(), out_audio_format);
	} catch (const std::runtime_error &e) {
66 67
		std::throw_with_nested(FormatRuntimeError("Failed to convert for %s",
							  GetLogName()));
68 69 70
	}
}

71
void
72
FilteredAudioOutput::OpenOutputAndConvert(AudioFormat desired_audio_format)
73 74 75 76
{
	out_audio_format = desired_audio_format;

	try {
77
		output->Open(out_audio_format);
78
	} catch (const std::runtime_error &e) {
79 80
		std::throw_with_nested(FormatRuntimeError("Failed to open %s",
							  GetLogName()));
81 82 83
	}

	FormatDebug(output_domain,
84 85
		    "opened %s audio_format=%s",
		    GetLogName(),
86 87 88
		    ToString(out_audio_format).c_str());

	try {
89
		ConfigureConvertFilter();
90
	} catch (const std::runtime_error &e) {
91
		output->Close();
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

		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;
		}

108
		throw;
109 110 111 112
	}
}

void
113
FilteredAudioOutput::CloseOutput(bool drain) noexcept
114 115
{
	if (drain)
116
		Drain();
117
	else
118
		Cancel();
119

120
	output->Close();
121 122
}

123
void
124
FilteredAudioOutput::OpenSoftwareMixer() noexcept
125 126 127 128 129
{
	if (mixer != nullptr && mixer->IsPlugin(software_mixer_plugin))
		software_mixer_set_filter(*mixer, volume_filter.Get());
}

130
void
131
FilteredAudioOutput::CloseSoftwareMixer() noexcept
132 133 134 135 136 137
{
	if (mixer != nullptr && mixer->IsPlugin(software_mixer_plugin))
		software_mixer_set_filter(*mixer, nullptr);
}

void
138
FilteredAudioOutput::Close(bool drain) noexcept
139 140
{
	CloseOutput(drain);
141
	CloseSoftwareMixer();
142

143
	FormatDebug(output_domain, "closed %s", GetLogName());
144 145
}

146 147 148
std::chrono::steady_clock::duration
FilteredAudioOutput::Delay() noexcept
{
149
	return output->Delay();
150 151
}

152
void
153 154
FilteredAudioOutput::SendTag(const Tag &tag)
{
155
	output->SendTag(tag);
156 157 158 159 160
}

size_t
FilteredAudioOutput::Play(const void *data, size_t size)
{
161
	return output->Play(data, size);
162 163 164 165 166
}

void
FilteredAudioOutput::Drain()
{
167
	output->Drain();
168 169 170 171
}

void
FilteredAudioOutput::Cancel() noexcept
172
{
173
	output->Cancel();
174 175
}

176 177 178 179 180 181
void
FilteredAudioOutput::BeginPause() noexcept
{
	Cancel();
}

182
bool
183
FilteredAudioOutput::IteratePause() noexcept
184 185
{
	try {
186
		return output->Pause();
187
	} catch (const std::runtime_error &e) {
188 189
		FormatError(e, "Failed to pause %s",
			    GetLogName());
190
		return false;
191 192
	}
}