Source.hxx 5.37 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
 * 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.
 */

#ifndef AUDIO_OUTPUT_SOURCE_HXX
#define AUDIO_OUTPUT_SOURCE_HXX

#include "check.h"
24
#include "util/Compiler.h"
25 26 27 28 29
#include "SharedPipeConsumer.hxx"
#include "AudioFormat.hxx"
#include "ReplayGainMode.hxx"
#include "pcm/PcmBuffer.hxx"
#include "pcm/PcmDither.hxx"
30 31 32
#include "util/ConstBuffer.hxx"

#include <utility>
33
#include <memory>
34 35

#include <assert.h>
36
#include <stdint.h>
37 38

struct MusicChunk;
39
struct Tag;
40
class Mutex;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
class Filter;
class PreparedFilter;

/**
 * Source of audio data to be played by an #AudioOutput.  It receives
 * #MusicChunk instances from a #MusicPipe (via #SharedPipeConsumer).
 * It applies configured filters, ReplayGain and returns plain PCM
 * data.
 */
class AudioOutputSource {
	/**
	 * The audio_format in which audio data is received from the
	 * player thread (which in turn receives it from the decoder).
	 */
	AudioFormat in_audio_format = AudioFormat::Undefined();

	ReplayGainMode replay_gain_mode = ReplayGainMode::OFF;

	/**
	 * A reference to the #MusicPipe and the current position.
	 */
	SharedPipeConsumer pipe;

	/**
	 * The serial number of the last replay gain info.  0 means no
	 * replay gain info was available.
	 */
68
	unsigned replay_gain_serial;
69 70 71 72 73

	/**
	 * The serial number of the last replay gain info by the
	 * "other" chunk during cross-fading.
	 */
74
	unsigned other_replay_gain_serial;
75 76 77 78 79

	/**
	 * The replay_gain_filter_plugin instance of this audio
	 * output.
	 */
80
	std::unique_ptr<Filter> replay_gain_filter;
81 82 83 84 85 86

	/**
	 * The replay_gain_filter_plugin instance of this audio
	 * output, to be applied to the second chunk during
	 * cross-fading.
	 */
87
	std::unique_ptr<Filter> other_replay_gain_filter;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

	/**
	 * The buffer used to allocate the cross-fading result.
	 */
	PcmBuffer cross_fade_buffer;

	/**
	 * The dithering state for cross-fading two streams.
	 */
	PcmDither cross_fade_dither;

	/**
	 * The filter object of this audio output.  This is an
	 * instance of chain_filter_plugin.
	 */
103
	std::unique_ptr<Filter> filter;
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	/**
	 * The #MusicChunk currently being processed (see
	 * #pending_tag, #pending_data).
	 */
	const MusicChunk *current_chunk = nullptr;

	/**
	 * The #Tag to be processed by the #AudioOutput.
	 */
	const Tag *pending_tag;

	/**
	 * Filtered #MusicChunk PCM data to be processed by the
	 * #AudioOutput.
	 */
	ConstBuffer<uint8_t> pending_data;

122
public:
123 124 125
	AudioOutputSource() noexcept;
	~AudioOutputSource() noexcept;

126
	void SetReplayGainMode(ReplayGainMode _mode) noexcept {
127 128 129 130 131 132 133 134
		replay_gain_mode = _mode;
	}

	bool IsOpen() const {
		return in_audio_format.IsDefined();
	}

	const AudioFormat &GetInputAudioFormat() const {
135 136
		assert(IsOpen());

137 138 139 140 141 142
		return in_audio_format;
	}

	AudioFormat Open(AudioFormat audio_format, const MusicPipe &_pipe,
			 PreparedFilter *prepared_replay_gain_filter,
			 PreparedFilter *prepared_other_replay_gain_filter,
143
			 PreparedFilter &prepared_filter);
144

145 146
	void Close() noexcept;
	void Cancel() noexcept;
147

148 149 150 151 152
	/**
	 * Ensure that ReadTag() or PeekData() return any input.
	 *
	 * Throws std::runtime_error on error
	 *
153 154 155
	 * @param mutex the #Mutex which protects the
	 * #SharedPipeConsumer; it is locked by the caller, and may be
	 * unlocked temporarily by this method
156 157 158
	 * @return true if any input is available, false if the source
	 * has (temporarily?) run empty
	 */
159
	bool Fill(Mutex &mutex);
160

161 162 163 164 165 166
	/**
	 * Reads the #Tag to be processed.  Be sure to call Fill()
	 * successfully before calling this metohd.
	 */
	const Tag *ReadTag() noexcept {
		assert(current_chunk != nullptr);
167

168 169
		return std::exchange(pending_tag, nullptr);
	}
170

171 172 173 174 175 176 177 178 179 180
	/**
	 * Returns the remaining filtered PCM data be played.  The
	 * caller shall use ConsumeData() to mark portions of the
	 * return value as "consumed".
	 *
	 * Be sure to call Fill() successfully before calling this
	 * metohd.
	 */
	ConstBuffer<void> PeekData() const noexcept {
		return pending_data.ToVoid();
181 182
	}

183 184 185 186 187
	/**
	 * Mark portions of the PeekData() return value as "consumed".
	 */
	void ConsumeData(size_t nbytes) noexcept;

188
	bool IsChunkConsumed(const MusicChunk &chunk) const  noexcept {
189 190 191 192 193
		assert(IsOpen());

		return pipe.IsConsumed(chunk);
	}

194
	void ClearTailChunk(const MusicChunk &chunk) noexcept {
195 196 197
		pipe.ClearTail(chunk);
	}

198 199 200 201 202
	/**
	 * Wrapper for Filter::Flush().
	 */
	ConstBuffer<void> Flush();

203 204 205 206
private:
	void OpenFilter(AudioFormat audio_format,
			PreparedFilter *prepared_replay_gain_filter,
			PreparedFilter *prepared_other_replay_gain_filter,
207
			PreparedFilter &prepared_filter);
208

209
	void CloseFilter() noexcept;
210 211 212 213

	ConstBuffer<void> GetChunkData(const MusicChunk &chunk,
				       Filter *replay_gain_filter,
				       unsigned *replay_gain_serial_p);
214 215

	ConstBuffer<void> FilterChunk(const MusicChunk &chunk);
216 217 218
};

#endif