Filtered.hxx 5.73 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 FilterFactory;
32
class PreparedFilter;
33
class MusicPipe;
34
class EventLoop;
35 36
class Mixer;
class MixerListener;
37
struct MixerPlugin;
38
struct MusicChunk;
39
struct ConfigBlock;
40
class AudioOutput;
41
struct AudioOutputDefaults;
42
struct ReplayGainConfig;
43
struct Tag;
44

45
struct FilteredAudioOutput {
46 47
	const char *const plugin_name;

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

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

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

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

73 74 75
	/**
	 * The configured audio format.
	 */
76
	AudioFormat config_audio_format;
77

78 79 80 81 82 83 84 85
	/**
	 * 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;

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

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

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

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

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

119 120 121 122 123 124
	/**
	 * 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.
	 */
125
	FilterObserver convert_filter;
126

127 128 129
	/**
	 * Throws #std::runtime_error on error.
	 */
130 131
	FilteredAudioOutput(const char *_plugin_name,
			    std::unique_ptr<AudioOutput> &&_output,
132
			    const ConfigBlock &block,
133
			    const AudioOutputDefaults &defaults,
134
			    FilterFactory *filter_factory);
135

136
	~FilteredAudioOutput();
137

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

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

151 152 153 154
	const char *GetName() const {
		return name;
	}

155 156 157 158
	const char *GetPluginName() const noexcept {
		return plugin_name;
	}

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

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

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

178 179 180 181 182
	/**
	 * Throws #std::runtime_error on error.
	 */
	void Enable();

183
	void Disable() noexcept;
184

185 186 187 188 189
	/**
	 * Invoke OutputPlugin::close().
	 *
	 * Caller must not lock the mutex.
	 */
190
	void Close(bool drain) noexcept;
191

192 193
	void ConfigureConvertFilter();

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

204 205 206 207 208
	/**
	 * Close the output plugin.
	 *
	 * Mutex must not be locked.
	 */
209
	void CloseOutput(bool drain) noexcept;
210

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

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

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

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

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

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

249
#endif