Filtered.hxx 5.61 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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 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 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
	 * 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 174 175
	const std::map<std::string, std::string> GetAttributes() const noexcept;
	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 186 187
	/**
	 * Invoke OutputPlugin::close().
	 *
	 * Caller must not lock the mutex.
	 */
188
	void Close(bool drain) noexcept;
189

190 191
	void ConfigureConvertFilter();

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

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

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

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

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

229 230
	void BeginPause() noexcept;
	bool IteratePause() noexcept;
231

232
	void EndPause() noexcept{
233
	}
234 235
};

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

247
#endif