1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* 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"
#include "Internal.hxx"
#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"
void
AudioOutput::Enable()
{
try {
ao_plugin_enable(*this);
} catch (const std::runtime_error &e) {
std::throw_with_nested(FormatRuntimeError("Failed to enable output \"%s\" [%s]",
name, plugin.name));
}
}
void
AudioOutput::Disable() noexcept
{
ao_plugin_disable(*this);
}
void
AudioOutput::OpenOutputAndConvert(AudioFormat desired_audio_format)
{
out_audio_format = desired_audio_format;
try {
ao_plugin_open(*this, out_audio_format);
} catch (const std::runtime_error &e) {
std::throw_with_nested(FormatRuntimeError("Failed to open \"%s\" [%s]",
name, plugin.name));
}
FormatDebug(output_domain,
"opened plugin=%s name=\"%s\" audio_format=%s",
plugin.name, name,
ToString(out_audio_format).c_str());
try {
convert_filter_set(convert_filter.Get(), out_audio_format);
} 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;
}
std::throw_with_nested(FormatRuntimeError("Failed to convert for \"%s\" [%s]",
name, plugin.name));
}
}
void
AudioOutput::CloseOutput(bool drain) noexcept
{
if (drain)
ao_plugin_drain(*this);
else
ao_plugin_cancel(*this);
ao_plugin_close(*this);
}
void
AudioOutput::CloseFilter() noexcept
{
if (mixer != nullptr && mixer->IsPlugin(software_mixer_plugin))
software_mixer_set_filter(*mixer, nullptr);
}
void
AudioOutput::Close(bool drain) noexcept
{
CloseOutput(drain);
CloseFilter();
FormatDebug(output_domain, "closed plugin=%s name=\"%s\"",
plugin.name, name);
}
void
AudioOutput::BeginPause() noexcept
{
ao_plugin_cancel(*this);
}
bool
AudioOutput::IteratePause() noexcept
{
try {
return ao_plugin_pause(*this);
} catch (const std::runtime_error &e) {
FormatError(e, "\"%s\" [%s] failed to pause",
name, plugin.name);
return false;
}
}