Filtered.hxx 5.75 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "pcm/AudioFormat.hxx"
24
#include "filter/Observer.hxx"
25

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

31
class FilterFactory;
32
class PreparedFilter;
33
class EventLoop;
34 35
class Mixer;
class MixerListener;
36
struct MixerPlugin;
37
struct ConfigBlock;
38
class AudioOutput;
39
struct AudioOutputDefaults;
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
	 * The filter object of this audio output.  This is a chain of
	 * #PreparedTwoFilter instances.
95
	 */
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
	 * Throws on error.
127
	 */
128 129
	FilteredAudioOutput(const char *_plugin_name,
			    std::unique_ptr<AudioOutput> &&_output,
130
			    const ConfigBlock &block,
131
			    const AudioOutputDefaults &defaults,
132
			    FilterFactory *filter_factory);
133

134
	~FilteredAudioOutput();
135

136
private:
137
	void Configure(const ConfigBlock &block,
138
		       const AudioOutputDefaults &defaults,
139
		       FilterFactory *filter_factory);
140

141
public:
142 143
	void Setup(EventLoop &event_loop,
		   const ReplayGainConfig &replay_gain_config,
144
		   const MixerPlugin *mixer_plugin,
145
		   MixerListener &mixer_listener,
146 147
		   const ConfigBlock &block,
		   const AudioOutputDefaults &defaults);
148

149 150 151 152
	const char *GetName() const {
		return name;
	}

153 154 155 156
	const char *GetPluginName() const noexcept {
		return plugin_name;
	}

157 158 159 160
	const char *GetLogName() const noexcept {
		return log_name.c_str();
	}

161 162 163 164 165 166 167 168 169 170 171 172
	/**
	 * 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;

173
	std::map<std::string, std::string> GetAttributes() const noexcept;
174 175
	void SetAttribute(std::string &&name, std::string &&value);

176
	/**
177
	 * Throws on error.
178 179 180
	 */
	void Enable();

181
	void Disable() noexcept;
182

183
	/**
184 185
	 * Close everything: the output (via CloseOutput()) and the
	 * software mixer (via CloseSoftwareMixer()).
186 187 188
	 *
	 * Caller must not lock the mutex.
	 */
189
	void Close(bool drain) noexcept;
190

191 192
	void ConfigureConvertFilter();

193 194 195 196
	/**
	 * Invoke OutputPlugin::open() and configure the
	 * #ConvertFilter.
	 *
197
	 * Throws on error.
198
	 *
199
	 * Caller must not lock the mutex.
200
	 */
201
	void OpenOutputAndConvert(AudioFormat audio_format);
202

203
	/**
204
	 * Invoke AudioOutput::Close(), but nothing else.
205 206 207
	 *
	 * Mutex must not be locked.
	 */
208
	void CloseOutput(bool drain) noexcept;
209

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

215 216 217
	/**
	 * Mutex must not be locked.
	 */
218
	void CloseSoftwareMixer() noexcept;
219

220 221
	void Interrupt() noexcept;

222 223 224 225 226 227 228 229 230 231
	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;

232
	void BeginPause() noexcept;
233
	bool IteratePause();
234

235
	void EndPause() noexcept{
236
	}
237 238
};

239
/**
240
 * Throws on error.
241
 */
242
std::unique_ptr<FilteredAudioOutput>
243
audio_output_new(EventLoop &event_loop, EventLoop &rt_event_loop,
244 245
		 const ReplayGainConfig &replay_gain_config,
		 const ConfigBlock &block,
246
		 const AudioOutputDefaults &defaults,
247
		 FilterFactory *filter_factory,
248
		 MixerListener &mixer_listener);
249

250
#endif