2016-12-20 16:37:36 +01:00
|
|
|
/*
|
2019-06-17 11:17:30 +02:00
|
|
|
* Copyright 2003-2019 The Music Player Daemon Project
|
2016-12-20 16:37:36 +01:00
|
|
|
* 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 "SharedPipeConsumer.hxx"
|
|
|
|
#include "AudioFormat.hxx"
|
|
|
|
#include "ReplayGainMode.hxx"
|
2019-06-17 11:10:33 +02:00
|
|
|
#include "pcm/Buffer.hxx"
|
|
|
|
#include "pcm/Dither.hxx"
|
2019-05-07 19:23:01 +02:00
|
|
|
#include "thread/Mutex.hxx"
|
2016-12-26 13:53:22 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
|
|
|
|
|
|
|
#include <utility>
|
2017-12-27 11:42:14 +01:00
|
|
|
#include <memory>
|
2016-12-20 16:37:36 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
2016-12-26 13:53:22 +01:00
|
|
|
#include <stdint.h>
|
2016-12-20 16:37:36 +01:00
|
|
|
|
|
|
|
struct MusicChunk;
|
2016-12-26 13:53:22 +01:00
|
|
|
struct Tag;
|
2016-12-20 16:37:36 +01:00
|
|
|
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.
|
|
|
|
*/
|
2017-01-23 17:55:04 +01:00
|
|
|
unsigned replay_gain_serial;
|
2016-12-20 16:37:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The serial number of the last replay gain info by the
|
|
|
|
* "other" chunk during cross-fading.
|
|
|
|
*/
|
2017-01-23 17:55:04 +01:00
|
|
|
unsigned other_replay_gain_serial;
|
2016-12-20 16:37:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The replay_gain_filter_plugin instance of this audio
|
|
|
|
* output.
|
|
|
|
*/
|
2018-01-02 09:59:22 +01:00
|
|
|
std::unique_ptr<Filter> replay_gain_filter;
|
2016-12-20 16:37:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The replay_gain_filter_plugin instance of this audio
|
|
|
|
* output, to be applied to the second chunk during
|
|
|
|
* cross-fading.
|
|
|
|
*/
|
2018-01-02 09:59:22 +01:00
|
|
|
std::unique_ptr<Filter> other_replay_gain_filter;
|
2016-12-20 16:37:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-01-02 09:59:22 +01:00
|
|
|
std::unique_ptr<Filter> filter;
|
2016-12-20 16:37:36 +01:00
|
|
|
|
2016-12-26 13:53:22 +01:00
|
|
|
/**
|
|
|
|
* The #MusicChunk currently being processed (see
|
|
|
|
* #pending_tag, #pending_data).
|
|
|
|
*/
|
|
|
|
const MusicChunk *current_chunk = nullptr;
|
|
|
|
|
|
|
|
/**
|
2019-08-27 19:07:29 +02:00
|
|
|
* The #Tag to be processed by the #AudioOutput. It is owned
|
|
|
|
* by #current_chunk (MusicChunk::tag).
|
2016-12-26 13:53:22 +01:00
|
|
|
*/
|
|
|
|
const Tag *pending_tag;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filtered #MusicChunk PCM data to be processed by the
|
|
|
|
* #AudioOutput.
|
|
|
|
*/
|
|
|
|
ConstBuffer<uint8_t> pending_data;
|
|
|
|
|
2016-12-20 16:37:36 +01:00
|
|
|
public:
|
2017-12-27 11:42:14 +01:00
|
|
|
AudioOutputSource() noexcept;
|
|
|
|
~AudioOutputSource() noexcept;
|
|
|
|
|
2017-05-23 00:17:23 +02:00
|
|
|
void SetReplayGainMode(ReplayGainMode _mode) noexcept {
|
2016-12-20 16:37:36 +01:00
|
|
|
replay_gain_mode = _mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsOpen() const {
|
|
|
|
return in_audio_format.IsDefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
const AudioFormat &GetInputAudioFormat() const {
|
2016-12-26 20:10:00 +01:00
|
|
|
assert(IsOpen());
|
|
|
|
|
2016-12-20 16:37:36 +01:00
|
|
|
return in_audio_format;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioFormat Open(AudioFormat audio_format, const MusicPipe &_pipe,
|
|
|
|
PreparedFilter *prepared_replay_gain_filter,
|
|
|
|
PreparedFilter *prepared_other_replay_gain_filter,
|
2017-12-27 11:21:43 +01:00
|
|
|
PreparedFilter &prepared_filter);
|
2016-12-20 16:37:36 +01:00
|
|
|
|
2017-05-08 14:44:49 +02:00
|
|
|
void Close() noexcept;
|
|
|
|
void Cancel() noexcept;
|
2016-12-20 16:37:36 +01:00
|
|
|
|
2016-12-26 13:53:22 +01:00
|
|
|
/**
|
|
|
|
* Ensure that ReadTag() or PeekData() return any input.
|
|
|
|
*
|
2019-07-03 22:10:26 +02:00
|
|
|
* Throws on error
|
2016-12-26 13:53:22 +01:00
|
|
|
*
|
2016-12-26 20:02:15 +01:00
|
|
|
* @param mutex the #Mutex which protects the
|
|
|
|
* #SharedPipeConsumer; it is locked by the caller, and may be
|
|
|
|
* unlocked temporarily by this method
|
2016-12-26 13:53:22 +01:00
|
|
|
* @return true if any input is available, false if the source
|
|
|
|
* has (temporarily?) run empty
|
|
|
|
*/
|
2016-12-26 20:02:15 +01:00
|
|
|
bool Fill(Mutex &mutex);
|
2016-12-20 16:37:36 +01:00
|
|
|
|
2016-12-26 13:53:22 +01:00
|
|
|
/**
|
|
|
|
* Reads the #Tag to be processed. Be sure to call Fill()
|
|
|
|
* successfully before calling this metohd.
|
|
|
|
*/
|
|
|
|
const Tag *ReadTag() noexcept {
|
|
|
|
assert(current_chunk != nullptr);
|
2016-12-20 16:37:36 +01:00
|
|
|
|
2016-12-26 13:53:22 +01:00
|
|
|
return std::exchange(pending_tag, nullptr);
|
|
|
|
}
|
2016-12-20 16:37:36 +01:00
|
|
|
|
2016-12-26 13:53:22 +01:00
|
|
|
/**
|
|
|
|
* 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();
|
2016-12-20 16:37:36 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 13:53:22 +01:00
|
|
|
/**
|
|
|
|
* Mark portions of the PeekData() return value as "consumed".
|
|
|
|
*/
|
|
|
|
void ConsumeData(size_t nbytes) noexcept;
|
|
|
|
|
2017-05-08 14:44:49 +02:00
|
|
|
bool IsChunkConsumed(const MusicChunk &chunk) const noexcept {
|
2016-12-20 16:37:36 +01:00
|
|
|
assert(IsOpen());
|
|
|
|
|
|
|
|
return pipe.IsConsumed(chunk);
|
|
|
|
}
|
|
|
|
|
2017-05-08 14:44:49 +02:00
|
|
|
void ClearTailChunk(const MusicChunk &chunk) noexcept {
|
2016-12-20 16:37:36 +01:00
|
|
|
pipe.ClearTail(chunk);
|
|
|
|
}
|
|
|
|
|
2018-01-01 19:22:45 +01:00
|
|
|
/**
|
|
|
|
* Wrapper for Filter::Flush().
|
|
|
|
*/
|
|
|
|
ConstBuffer<void> Flush();
|
|
|
|
|
2016-12-20 16:37:36 +01:00
|
|
|
private:
|
|
|
|
void OpenFilter(AudioFormat audio_format,
|
|
|
|
PreparedFilter *prepared_replay_gain_filter,
|
|
|
|
PreparedFilter *prepared_other_replay_gain_filter,
|
2017-12-27 11:21:43 +01:00
|
|
|
PreparedFilter &prepared_filter);
|
2016-12-20 16:37:36 +01:00
|
|
|
|
2017-05-08 14:44:49 +02:00
|
|
|
void CloseFilter() noexcept;
|
2016-12-20 16:37:36 +01:00
|
|
|
|
|
|
|
ConstBuffer<void> GetChunkData(const MusicChunk &chunk,
|
|
|
|
Filter *replay_gain_filter,
|
|
|
|
unsigned *replay_gain_serial_p);
|
2016-12-26 13:53:22 +01:00
|
|
|
|
|
|
|
ConstBuffer<void> FilterChunk(const MusicChunk &chunk);
|
2019-08-27 19:04:51 +02:00
|
|
|
|
|
|
|
void DropCurrentChunk() noexcept {
|
|
|
|
assert(current_chunk != nullptr);
|
|
|
|
|
|
|
|
pipe.Consume(*std::exchange(current_chunk, nullptr));
|
|
|
|
}
|
2016-12-20 16:37:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|