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 <chrono>
29

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

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

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

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

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

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

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

75 76 77 78 79 80 81 82
	/**
	 * 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;

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

91 92 93 94
	/**
	 * The filter object of this audio output.  This is an
	 * instance of chain_filter_plugin.
	 */
95
	PreparedFilter *prepared_filter = nullptr;
96

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

103 104 105 106
	/**
	 * The replay_gain_filter_plugin instance of this audio
	 * output.
	 */
107
	PreparedFilter *prepared_replay_gain_filter = nullptr;
108

109 110 111 112 113
	/**
	 * The replay_gain_filter_plugin instance of this audio
	 * output, to be applied to the second chunk during
	 * cross-fading.
	 */
114
	PreparedFilter *prepared_other_replay_gain_filter = nullptr;
115

116 117 118 119 120 121
	/**
	 * 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.
	 */
122
	FilterObserver convert_filter;
123

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

131
	~FilteredAudioOutput();
132

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

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

143 144
	void BeginDestroy() noexcept;
	void FinishDestroy() noexcept;
145

146 147 148 149
	const char *GetName() const {
		return name;
	}

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

154 155 156 157 158 159 160 161 162 163 164 165
	/**
	 * 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;

166 167 168 169 170
	/**
	 * Throws #std::runtime_error on error.
	 */
	void Enable();

171
	void Disable() noexcept;
172

173 174 175 176 177
	/**
	 * Invoke OutputPlugin::close().
	 *
	 * Caller must not lock the mutex.
	 */
178
	void Close(bool drain) noexcept;
179

180 181
	void ConfigureConvertFilter();

182 183 184 185
	/**
	 * Invoke OutputPlugin::open() and configure the
	 * #ConvertFilter.
	 *
186
	 * Throws #std::runtime_error on error.
187
	 *
188
	 * Caller must not lock the mutex.
189
	 */
190
	void OpenOutputAndConvert(AudioFormat audio_format);
191

192 193 194 195 196
	/**
	 * Close the output plugin.
	 *
	 * Mutex must not be locked.
	 */
197
	void CloseOutput(bool drain) noexcept;
198

199 200 201 202 203
	/**
	 * Mutex must not be locked.
	 */
	void OpenSoftwareMixer() noexcept;

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

209 210 211 212 213 214 215 216 217 218
	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;

219 220
	void BeginPause() noexcept;
	bool IteratePause() noexcept;
221

222
	void EndPause() noexcept{
223
	}
224 225
};

226 227 228 229
/**
 * Notify object used by the thread's client, i.e. we will send a
 * notify signal to this object, expecting the caller to wait on it.
 */
230 231
extern struct notify audio_output_client_notify;

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

241
#endif