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

/*
 * Functions for dealing with all configured (enabled) audion outputs
 * at once.
 *
 */

#ifndef OUTPUT_ALL_H
#define OUTPUT_ALL_H

29
#include "Control.hxx"
30
#include "AudioFormat.hxx"
31
#include "ReplayGainMode.hxx"
32
#include "Chrono.hxx"
33 34 35 36 37 38 39 40
#include "Compiler.h"

#include <vector>

#include <assert.h>

class MusicBuffer;
class MusicPipe;
41
class EventLoop;
42
class MixerListener;
43
class AudioOutputClient;
44
struct MusicChunk;
45
struct AudioOutput;
46
struct ReplayGainConfig;
47 48

class MultipleOutputs {
49 50
	MixerListener &mixer_listener;

51
	std::vector<AudioOutputControl *> outputs;
52

53
	AudioFormat input_audio_format = AudioFormat::Undefined();
54 55 56 57

	/**
	 * The #MusicBuffer object where consumed chunks are returned.
	 */
58
	MusicBuffer *buffer = nullptr;
59 60 61 62 63

	/**
	 * The #MusicPipe object which feeds all audio outputs.  It is
	 * filled by audio_output_all_play().
	 */
64
	MusicPipe *pipe = nullptr;
65 66 67 68 69

	/**
	 * The "elapsed_time" stamp of the most recently finished
	 * chunk.
	 */
70
	SignedSongTime elapsed_time = SignedSongTime::Negative();
71 72 73 74 75 76

public:
	/**
	 * Load audio outputs from the configuration file and
	 * initialize them.
	 */
77
	MultipleOutputs(MixerListener &_mixer_listener);
78 79
	~MultipleOutputs();

80 81
	void Configure(EventLoop &event_loop,
		       const ReplayGainConfig &replay_gain_config,
82
		       AudioOutputClient &client);
83

84 85 86 87
	void AddNullOutput(EventLoop &event_loop,
			   const ReplayGainConfig &replay_gain_config,
			   AudioOutputClient &client);

88 89 90 91 92
	/**
	 * Returns the total number of audio output devices, including
	 * those which are disabled right now.
	 */
	gcc_pure
93
	unsigned Size() const noexcept {
94 95 96 97 98 99
		return outputs.size();
	}

	/**
	 * Returns the "i"th audio output device.
	 */
100
	const AudioOutputControl &Get(unsigned i) const {
101 102 103 104 105
		assert(i < Size());

		return *outputs[i];
	}

106
	AudioOutputControl &Get(unsigned i) {
107 108 109 110 111 112 113 114 115 116
		assert(i < Size());

		return *outputs[i];
	}

	/**
	 * Returns the audio output device with the specified name.
	 * Returns nullptr if the name does not exist.
	 */
	gcc_pure
Max Kellermann's avatar
Max Kellermann committed
117
	AudioOutputControl *FindByName(const char *name) noexcept;
118 119 120 121 122 123 124 125 126 127

	/**
	 * Checks the "enabled" flag of all audio outputs, and if one has
	 * changed, commit the change.
	 */
	void EnableDisable();

	/**
	 * Opens all audio outputs which are not disabled.
	 *
128 129
	 * Throws #std::runtime_error on error.
	 *
130
	 * @param audio_format the preferred audio format
Max Kellermann's avatar
Max Kellermann committed
131
	 * @param _buffer the #music_buffer where consumed #MusicChunk objects
132 133
	 * should be returned
	 */
134
	void Open(const AudioFormat audio_format, MusicBuffer &_buffer);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

	/**
	 * Closes all audio outputs.
	 */
	void Close();

	/**
	 * Closes all audio outputs.  Outputs with the "always_on"
	 * flag are put into pause mode.
	 */
	void Release();

	void SetReplayGainMode(ReplayGainMode mode);

	/**
150
	 * Enqueue a #MusicChunk object for playing, i.e. pushes it to a
151 152
	 * #MusicPipe.
	 *
153 154
	 * Throws #std::runtime_error on error (all closed then).
	 *
155
	 * @param chunk the #MusicChunk object to be played
156
	 */
157
	void Play(MusicChunk *chunk);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

	/**
	 * Checks if the output devices have drained their music pipe, and
	 * returns the consumed music chunks to the #music_buffer.
	 *
	 * @return the number of chunks to play left in the #MusicPipe
	 */
	unsigned Check();

	/**
	 * Puts all audio outputs into pause mode.  Most implementations will
	 * simply close it then.
	 */
	void Pause();

	/**
	 * Drain all audio outputs.
	 */
	void Drain();

	/**
	 * Try to cancel data which may still be in the device's buffers.
	 */
	void Cancel();

	/**
	 * Indicate that a new song will begin now.
	 */
	void SongBorder();

	/**
	 * Returns the "elapsed_time" stamp of the most recently finished
	 * chunk.  A negative value is returned when no chunk has been
	 * finished yet.
	 */
	gcc_pure
194
	SignedSongTime GetElapsedTime() const noexcept {
195 196 197 198 199 200 201 202
		return elapsed_time;
	}

	/**
	 * Returns the average volume of all available mixers (range
	 * 0..100).  Returns -1 if no mixer can be queried.
	 */
	gcc_pure
203
	int GetVolume() const noexcept;
204 205 206 207 208 209 210

	/**
	 * Sets the volume on all available mixers.
	 *
	 * @param volume the volume (range 0..100)
	 * @return true on success, false on failure
	 */
211
	bool SetVolume(unsigned volume) noexcept;
212 213 214 215 216 217 218

	/**
	 * Similar to GetVolume(), but gets the volume only for
	 * software mixers.  See #software_mixer_plugin.  This
	 * function fails if no software mixer is configured.
	 */
	gcc_pure
219
	int GetSoftwareVolume() const noexcept;
220 221 222 223 224 225 226

	/**
	 * Similar to SetVolume(), but sets the volume only for
	 * software mixers.  See #software_mixer_plugin.  This
	 * function cannot fail, because the underlying software
	 * mixers cannot fail either.
	 */
227
	void SetSoftwareVolume(unsigned volume) noexcept;
228 229 230 231 232 233 234

private:
	/**
	 * Determine if all (active) outputs have finished the current
	 * command.
	 */
	gcc_pure
235
	bool AllFinished() const noexcept;
236

237
	void WaitAll() noexcept;
238 239 240 241 242 243 244 245 246 247 248 249

	/**
	 * Signals all audio outputs which are open.
	 */
	void AllowPlay();

	/**
	 * Opens all output devices which are enabled, but closed.
	 *
	 * @return true if there is at least open output device which
	 * is open
	 */
250
	bool Update(bool force);
251 252 253 254

	/**
	 * Has this chunk been consumed by all audio outputs?
	 */
255
	bool IsChunkConsumed(const MusicChunk *chunk) const noexcept;
256 257 258 259 260 261

	/**
	 * There's only one chunk left in the pipe (#pipe), and all
	 * audio outputs have consumed it already.  Clear the
	 * reference.
	 */
262
	void ClearTailChunk(const MusicChunk *chunk, bool *locked);
263 264 265
};

#endif