From 596ff7e6bfcc1757b720641f1ebf38b3abc5837f Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 4 Jul 2022 19:04:49 +0200 Subject: [PATCH] output/*: use std::span instead of ConstBuffer --- src/output/Source.cxx | 28 +++++++++---------- src/output/Source.hxx | 14 +++++----- src/output/Thread.cxx | 14 ++++------ src/output/plugins/AlsaOutputPlugin.cxx | 1 - src/output/plugins/JackOutputPlugin.cxx | 6 ++-- src/output/plugins/OSXOutputPlugin.cxx | 14 +++++----- src/output/plugins/OssOutputPlugin.cxx | 1 - .../plugins/wasapi/WasapiOutputPlugin.cxx | 11 ++++---- 8 files changed, 41 insertions(+), 48 deletions(-) diff --git a/src/output/Source.cxx b/src/output/Source.cxx index d768d00cc..d29d446a0 100644 --- a/src/output/Source.cxx +++ b/src/output/Source.cxx @@ -24,7 +24,6 @@ #include "filter/plugins/ReplayGainFilterPlugin.hxx" #include "pcm/Mix.hxx" #include "thread/Mutex.hxx" -#include "util/ConstBuffer.hxx" #include "util/RuntimeError.hxx" #include @@ -130,7 +129,7 @@ AudioOutputSource::CloseFilter() noexcept filter.reset(); } -ConstBuffer +std::span AudioOutputSource::GetChunkData(const MusicChunk &chunk, Filter *current_replay_gain_filter, unsigned *replay_gain_serial_p) @@ -138,9 +137,9 @@ AudioOutputSource::GetChunkData(const MusicChunk &chunk, assert(!chunk.IsEmpty()); assert(chunk.CheckFormat(in_audio_format)); - ConstBuffer data(chunk.data, chunk.length); + std::span data(chunk.data, chunk.length); - assert(data.size % in_audio_format.GetFrameSize() == 0); + assert(data.size() % in_audio_format.GetFrameSize() == 0); if (!data.empty() && current_replay_gain_filter != nullptr) { replay_gain_filter_set_mode(*current_replay_gain_filter, @@ -160,7 +159,7 @@ AudioOutputSource::GetChunkData(const MusicChunk &chunk, return data; } -ConstBuffer +std::span AudioOutputSource::FilterChunk(const MusicChunk &chunk) { auto data = GetChunkData(chunk, replay_gain_filter.get(), @@ -182,8 +181,8 @@ AudioOutputSource::FilterChunk(const MusicChunk &chunk) "next" song being faded in, and if there's a rest, it means cross-fading ends here */ - if (data.size > other_data.size) - data.size = other_data.size; + if (data.size() > other_data.size()) + data = data.first(other_data.size()); float mix_ratio = chunk.mix_ratio; if (mix_ratio >= 0) @@ -194,16 +193,15 @@ AudioOutputSource::FilterChunk(const MusicChunk &chunk) case */ mix_ratio = 1.0f - mix_ratio; - void *dest = cross_fade_buffer.Get(other_data.size); - memcpy(dest, other_data.data, other_data.size); - if (!pcm_mix(cross_fade_dither, dest, data.data, data.size, + void *dest = cross_fade_buffer.Get(other_data.size()); + memcpy(dest, other_data.data(), other_data.size()); + if (!pcm_mix(cross_fade_dither, dest, data.data(), data.size(), in_audio_format.format, mix_ratio)) throw FormatRuntimeError("Cannot cross-fade format %s", sample_format_to_string(in_audio_format.format)); - data.data = dest; - data.size = other_data.size; + data = {(const std::byte *)dest, other_data.size()}; } /* apply filter chain */ @@ -232,7 +230,7 @@ AudioOutputSource::Fill(Mutex &mutex) that may take a while */ const ScopeUnlock unlock(mutex); - pending_data = pending_data.FromVoid(FilterChunk(*current_chunk)); + pending_data = FilterChunk(*current_chunk); } catch (...) { current_chunk = nullptr; throw; @@ -244,13 +242,13 @@ AudioOutputSource::Fill(Mutex &mutex) void AudioOutputSource::ConsumeData(size_t nbytes) noexcept { - pending_data.skip_front(nbytes); + pending_data = pending_data.subspan(nbytes); if (pending_data.empty()) DropCurrentChunk(); } -ConstBuffer +std::span AudioOutputSource::Flush() { return filter diff --git a/src/output/Source.hxx b/src/output/Source.hxx index cb3dc22c1..485f3aaea 100644 --- a/src/output/Source.hxx +++ b/src/output/Source.hxx @@ -26,11 +26,11 @@ #include "pcm/Buffer.hxx" #include "pcm/Dither.hxx" #include "thread/Mutex.hxx" -#include "util/ConstBuffer.hxx" #include #include #include +#include #include struct MusicChunk; @@ -115,7 +115,7 @@ class AudioOutputSource { * Filtered #MusicChunk PCM data to be processed by the * #AudioOutput. */ - ConstBuffer pending_data; + std::span pending_data; public: AudioOutputSource() noexcept; @@ -174,8 +174,8 @@ public: * Be sure to call Fill() successfully before calling this * metohd. */ - ConstBuffer PeekData() const noexcept { - return pending_data.ToVoid(); + std::span PeekData() const noexcept { + return pending_data; } /** @@ -196,7 +196,7 @@ public: /** * Wrapper for Filter::Flush(). */ - ConstBuffer Flush(); + std::span Flush(); private: void OpenFilter(AudioFormat audio_format, @@ -206,11 +206,11 @@ private: void CloseFilter() noexcept; - ConstBuffer GetChunkData(const MusicChunk &chunk, + std::span GetChunkData(const MusicChunk &chunk, Filter *replay_gain_filter, unsigned *replay_gain_serial_p); - ConstBuffer FilterChunk(const MusicChunk &chunk); + std::span FilterChunk(const MusicChunk &chunk); void DropCurrentChunk() noexcept { assert(current_chunk != nullptr); diff --git a/src/output/Thread.cxx b/src/output/Thread.cxx index 88e164846..48724e56e 100644 --- a/src/output/Thread.cxx +++ b/src/output/Thread.cxx @@ -265,9 +265,9 @@ AudioOutputControl::PlayChunk(std::unique_lock &lock) noexcept try { const ScopeUnlock unlock(mutex); - nbytes = output->Play(data.data, data.size); + nbytes = output->Play(data.data(), data.size()); assert(nbytes > 0); - assert(nbytes <= data.size); + assert(nbytes <= data.size()); } catch (AudioOutputInterrupted) { caught_interrupted = true; return false; @@ -377,15 +377,13 @@ AudioOutputControl::InternalPause(std::unique_lock &lock) noexcept } static void -PlayFull(FilteredAudioOutput &output, ConstBuffer _buffer) +PlayFull(FilteredAudioOutput &output, std::span buffer) { - auto buffer = ConstBuffer::FromVoid(_buffer); - while (!buffer.empty()) { - size_t nbytes = output.Play(buffer.data, buffer.size); + size_t nbytes = output.Play(buffer.data(), buffer.size()); assert(nbytes > 0); - buffer.skip_front(nbytes); + buffer = buffer.subspan(nbytes); } } @@ -404,7 +402,7 @@ AudioOutputControl::InternalDrain() noexcept while (true) { auto buffer = source.Flush(); - if (buffer.IsNull()) + if (buffer.data() == nullptr) break; PlayFull(*output, buffer); diff --git a/src/output/plugins/AlsaOutputPlugin.cxx b/src/output/plugins/AlsaOutputPlugin.cxx index 7ebc3c9a3..6e8d08967 100644 --- a/src/output/plugins/AlsaOutputPlugin.cxx +++ b/src/output/plugins/AlsaOutputPlugin.cxx @@ -35,7 +35,6 @@ #include "util/Manual.hxx" #include "util/RuntimeError.hxx" #include "util/Domain.hxx" -#include "util/ConstBuffer.hxx" #include "event/MultiSocketMonitor.hxx" #include "event/InjectEvent.hxx" #include "event/FineTimerEvent.hxx" diff --git a/src/output/plugins/JackOutputPlugin.cxx b/src/output/plugins/JackOutputPlugin.cxx index 214253e2c..f8069a30d 100644 --- a/src/output/plugins/JackOutputPlugin.cxx +++ b/src/output/plugins/JackOutputPlugin.cxx @@ -24,7 +24,6 @@ #include "output/Features.h" #include "thread/Mutex.hxx" #include "util/ScopeExit.hxx" -#include "util/ConstBuffer.hxx" #include "util/IterableSplitString.hxx" #include "util/RuntimeError.hxx" #include "util/Domain.hxx" @@ -32,6 +31,7 @@ #include #include +#include #include #include @@ -287,7 +287,7 @@ JackOutput::GetAvailable() const noexcept * Call jack_ringbuffer_read_advance() on all buffers in the list. */ static void -MultiReadAdvance(ConstBuffer buffers, +MultiReadAdvance(std::span buffers, size_t size) { for (auto *i : buffers) @@ -316,7 +316,7 @@ WriteSilence(jack_port_t &port, jack_nframes_t nframes) * Write a specific amount of "silence" to all ports in the list. */ static void -MultiWriteSilence(ConstBuffer ports, jack_nframes_t nframes) +MultiWriteSilence(std::span ports, jack_nframes_t nframes) { for (auto *i : ports) WriteSilence(*i, nframes); diff --git a/src/output/plugins/OSXOutputPlugin.cxx b/src/output/plugins/OSXOutputPlugin.cxx index 29b5e5c91..ab809ae43 100644 --- a/src/output/plugins/OSXOutputPlugin.cxx +++ b/src/output/plugins/OSXOutputPlugin.cxx @@ -28,7 +28,6 @@ #include "util/RuntimeError.hxx" #include "util/Domain.hxx" #include "util/Manual.hxx" -#include "util/ConstBuffer.hxx" #include "pcm/Export.hxx" #include "thread/Mutex.hxx" #include "thread/Cond.hxx" @@ -46,6 +45,7 @@ #include #include +#include static constexpr unsigned MPD_OSX_BUFFER_TIME_MS = 100; @@ -96,7 +96,7 @@ struct OSXOutput final : AudioOutput { AudioComponentInstance au; AudioStreamBasicDescription asbd; - boost::lockfree::spsc_queue *ring_buffer; + boost::lockfree::spsc_queue *ring_buffer; OSXOutput(const ConfigBlock &block); @@ -620,7 +620,7 @@ osx_render(void *vdata, int count = in_number_frames * od->asbd.mBytesPerFrame; buffer_list->mBuffers[0].mDataByteSize = - od->ring_buffer->pop((uint8_t *)buffer_list->mBuffers[0].mData, + od->ring_buffer->pop((std::byte *)buffer_list->mBuffers[0].mData, count); return noErr; } @@ -764,7 +764,7 @@ OSXOutput::Open(AudioFormat &audio_format) MPD_OSX_BUFFER_TIME_MS * pcm_export->GetOutputFrameSize() * asbd.mSampleRate / 1000); } #endif - ring_buffer = new boost::lockfree::spsc_queue(ring_buffer_size); + ring_buffer = new boost::lockfree::spsc_queue(ring_buffer_size); pause = false; started = false; @@ -777,17 +777,17 @@ OSXOutput::Play(const void *chunk, size_t size) pause = false; - ConstBuffer input((const uint8_t *)chunk, size); + std::span input((const std::byte *)chunk, size); #ifdef ENABLE_DSD if (dop_enabled) { - input = ConstBuffer::FromVoid(pcm_export->Export(input.ToVoid())); + input = pcm_export->Export(input); if (input.empty()) return size; } #endif - size_t bytes_written = ring_buffer->push(input.data, input.size); + size_t bytes_written = ring_buffer->push(input.data(), input.size()); if (!started) { OSStatus status = AudioOutputUnitStart(au); diff --git a/src/output/plugins/OssOutputPlugin.cxx b/src/output/plugins/OssOutputPlugin.cxx index b79aaa548..25a7723d7 100644 --- a/src/output/plugins/OssOutputPlugin.cxx +++ b/src/output/plugins/OssOutputPlugin.cxx @@ -23,7 +23,6 @@ #include "pcm/Export.hxx" #include "io/UniqueFileDescriptor.hxx" #include "system/Error.hxx" -#include "util/ConstBuffer.hxx" #include "util/Domain.hxx" #include "util/ByteOrder.hxx" #include "util/Manual.hxx" diff --git a/src/output/plugins/wasapi/WasapiOutputPlugin.cxx b/src/output/plugins/wasapi/WasapiOutputPlugin.cxx index 992dc2e67..3fc4de1b5 100644 --- a/src/output/plugins/wasapi/WasapiOutputPlugin.cxx +++ b/src/output/plugins/wasapi/WasapiOutputPlugin.cxx @@ -35,7 +35,6 @@ #include "thread/Name.hxx" #include "thread/Thread.hxx" #include "util/AllocatedString.hxx" -#include "util/ConstBuffer.hxx" #include "util/Domain.hxx" #include "util/RuntimeError.hxx" #include "util/ScopeExit.hxx" @@ -228,12 +227,12 @@ public: SetStatus(Status::PAUSE); } - std::size_t Push(ConstBuffer input) noexcept { + std::size_t Push(std::span input) noexcept { empty.store(false); std::size_t consumed = - spsc_buffer.push(static_cast(input.data), - input.size); + spsc_buffer.push((const BYTE *)input.data(), + input.size()); if (!playing) { playing = true; @@ -717,7 +716,7 @@ WasapiOutput::Play(const void *chunk, size_t size) not_interrupted.test_and_set(); - ConstBuffer input(chunk, size); + std::span input{(const std::byte*)chunk, size}; if (pcm_export) { input = pcm_export->Export(input); } @@ -725,7 +724,7 @@ WasapiOutput::Play(const void *chunk, size_t size) return size; do { - const size_t consumed_size = thread->Push({input.data, input.size}); + const size_t consumed_size = thread->Push(input); if (consumed_size == 0) { thread->Wait();