Filtered.hxx 5.4 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20 21
#ifndef MPD_FILTERED_AUDIO_OUTPUT_HXX
#define MPD_FILTERED_AUDIO_OUTPUT_HXX
22

23
#include "AudioFormat.hxx"
24
#include "filter/Observer.hxx"
25

26
#include <memory>
27
#include <string>
28
#include <map>
29
#include <chrono>
30

31
class PreparedFilter;
32
class MusicPipe;
33
class EventLoop;
34 35
class Mixer;
class MixerListener;
36
struct MixerPlugin;
37
struct MusicChunk;
38
struct ConfigBlock;
39
class AudioOutput;
40
struct ReplayGainConfig;
41
struct Tag;
42

43
struct FilteredAudioOutput {
44 45
	const char *const plugin_name;

46 47 48
	/**
	 * The device's configured display name.
	 */
49 50
	const char *name;

51 52 53 54 55 56 57 58
private:
	/**
	 * A string describing this devicee in log messages.  It is
	 * usually in the form "NAME (PLUGIN)".
	 */
	std::string log_name;

public:
59 60 61
	/**
	 * The plugin which implements this output device.
	 */
62
	std::unique_ptr<AudioOutput> output;
63

64 65
	/**
	 * The #mixer object associated with this audio output device.
66
	 * May be nullptr if none is available, or if software volume is
67 68
	 * configured.
	 */
69
	Mixer *mixer = nullptr;
70

71 72 73
	/**
	 * The configured audio format.
	 */
74
	AudioFormat config_audio_format;
75

76 77 78 79 80 81 82 83
	/**
	 * The #AudioFormat which is emitted by the #Filter, with
	 * #config_audio_format already applied.  This is used to
	 * decide whether this object needs to be closed and reopened
	 * upon #AudioFormat changes.
	 */
	AudioFormat filter_audio_format;

84 85
	/**
	 * The audio_format which is really sent to the device.  This
86 87
	 * is basically config_audio_format (if configured) or
	 * in_audio_format, but may have been modified by
88 89
	 * plugin->open().
	 */
90
	AudioFormat out_audio_format;
91

92 93 94 95
	/**
	 * The filter object of this audio output.  This is an
	 * instance of chain_filter_plugin.
	 */
96
	std::unique_ptr<PreparedFilter> prepared_filter;
97

98 99 100 101
	/**
	 * The #VolumeFilter instance of this audio output.  It is
	 * used by the #SoftwareMixer.
	 */
102
	FilterObserver volume_filter;
103

104 105 106 107
	/**
	 * The replay_gain_filter_plugin instance of this audio
	 * output.
	 */
108
	std::unique_ptr<PreparedFilter> prepared_replay_gain_filter;
109

110 111 112 113 114
	/**
	 * The replay_gain_filter_plugin instance of this audio
	 * output, to be applied to the second chunk during
	 * cross-fading.
	 */
115
	std::unique_ptr<PreparedFilter> prepared_other_replay_gain_filter;
116

117 118 119 120 121 122
	/**
	 * The convert_filter_plugin instance of this audio output.
	 * It is the last item in the filter chain, and is responsible
	 * for converting the input data into the appropriate format
	 * for this audio output.
	 */
123
	FilterObserver convert_filter;
124

125 126 127
	/**
	 * Throws #std::runtime_error on error.
	 */
128 129
	FilteredAudioOutput(const char *_plugin_name,
			    std::unique_ptr<AudioOutput> &&_output,
130
			    const ConfigBlock &block);
131

132
	~FilteredAudioOutput();
133

134
private:
135
	void Configure(const ConfigBlock &block);
136

137
public:
138 139
	void Setup(EventLoop &event_loop,
		   const ReplayGainConfig &replay_gain_config,
140
		   const MixerPlugin *mixer_plugin,
141 142 143
		   MixerListener &mixer_listener,
		   const ConfigBlock &block);

144 145 146 147
	const char *GetName() const {
		return name;
	}

148 149 150 151
	const char *GetPluginName() const noexcept {
		return plugin_name;
	}

152 153 154 155
	const char *GetLogName() const noexcept {
		return log_name.c_str();
	}

156 157 158 159 160 161 162 163 164 165 166 167
	/**
	 * Does the plugin support enabling/disabling a device?
	 */
	gcc_pure
	bool SupportsEnableDisable() const noexcept;

	/**
	 * Does the plugin support pausing a device?
	 */
	gcc_pure
	bool SupportsPause() const noexcept;

168 169 170
	const std::map<std::string, std::string> GetAttributes() const noexcept;
	void SetAttribute(std::string &&name, std::string &&value);

171 172 173 174 175
	/**
	 * Throws #std::runtime_error on error.
	 */
	void Enable();

176
	void Disable() noexcept;
177

178 179 180 181 182
	/**
	 * Invoke OutputPlugin::close().
	 *
	 * Caller must not lock the mutex.
	 */
183
	void Close(bool drain) noexcept;
184

185 186
	void ConfigureConvertFilter();

187 188 189 190
	/**
	 * Invoke OutputPlugin::open() and configure the
	 * #ConvertFilter.
	 *
191
	 * Throws #std::runtime_error on error.
192
	 *
193
	 * Caller must not lock the mutex.
194
	 */
195
	void OpenOutputAndConvert(AudioFormat audio_format);
196

197 198 199 200 201
	/**
	 * Close the output plugin.
	 *
	 * Mutex must not be locked.
	 */
202
	void CloseOutput(bool drain) noexcept;
203

204 205 206 207 208
	/**
	 * Mutex must not be locked.
	 */
	void OpenSoftwareMixer() noexcept;

209 210 211
	/**
	 * Mutex must not be locked.
	 */
212
	void CloseSoftwareMixer() noexcept;
213

214 215 216 217 218 219 220 221 222 223
	gcc_pure
	std::chrono::steady_clock::duration Delay() noexcept;

	void SendTag(const Tag &tag);

	size_t Play(const void *data, size_t size);

	void Drain();
	void Cancel() noexcept;

224 225
	void BeginPause() noexcept;
	bool IteratePause() noexcept;
226

227
	void EndPause() noexcept{
228
	}
229 230
};

231 232 233
/**
 * Throws #std::runtime_error on error.
 */
234
std::unique_ptr<FilteredAudioOutput>
235 236 237
audio_output_new(EventLoop &event_loop,
		 const ReplayGainConfig &replay_gain_config,
		 const ConfigBlock &block,
238
		 MixerListener &mixer_listener);
239

240
#endif