output/*: use std::span instead of ConstBuffer

This commit is contained in:
Max Kellermann 2022-07-04 19:04:49 +02:00
parent e8667f99be
commit 596ff7e6bf
8 changed files with 41 additions and 48 deletions

View File

@ -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 <string.h>
@ -130,7 +129,7 @@ AudioOutputSource::CloseFilter() noexcept
filter.reset();
}
ConstBuffer<void>
std::span<const std::byte>
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<void> data(chunk.data, chunk.length);
std::span<const std::byte> 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<void>
std::span<const std::byte>
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<void>
std::span<const std::byte>
AudioOutputSource::Flush()
{
return filter

View File

@ -26,11 +26,11 @@
#include "pcm/Buffer.hxx"
#include "pcm/Dither.hxx"
#include "thread/Mutex.hxx"
#include "util/ConstBuffer.hxx"
#include <cassert>
#include <cstdint>
#include <memory>
#include <span>
#include <utility>
struct MusicChunk;
@ -115,7 +115,7 @@ class AudioOutputSource {
* Filtered #MusicChunk PCM data to be processed by the
* #AudioOutput.
*/
ConstBuffer<uint8_t> pending_data;
std::span<const std::byte> pending_data;
public:
AudioOutputSource() noexcept;
@ -174,8 +174,8 @@ public:
* Be sure to call Fill() successfully before calling this
* metohd.
*/
ConstBuffer<void> PeekData() const noexcept {
return pending_data.ToVoid();
std::span<const std::byte> PeekData() const noexcept {
return pending_data;
}
/**
@ -196,7 +196,7 @@ public:
/**
* Wrapper for Filter::Flush().
*/
ConstBuffer<void> Flush();
std::span<const std::byte> Flush();
private:
void OpenFilter(AudioFormat audio_format,
@ -206,11 +206,11 @@ private:
void CloseFilter() noexcept;
ConstBuffer<void> GetChunkData(const MusicChunk &chunk,
std::span<const std::byte> GetChunkData(const MusicChunk &chunk,
Filter *replay_gain_filter,
unsigned *replay_gain_serial_p);
ConstBuffer<void> FilterChunk(const MusicChunk &chunk);
std::span<const std::byte> FilterChunk(const MusicChunk &chunk);
void DropCurrentChunk() noexcept {
assert(current_chunk != nullptr);

View File

@ -265,9 +265,9 @@ AudioOutputControl::PlayChunk(std::unique_lock<Mutex> &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<Mutex> &lock) noexcept
}
static void
PlayFull(FilteredAudioOutput &output, ConstBuffer<void> _buffer)
PlayFull(FilteredAudioOutput &output, std::span<const std::byte> buffer)
{
auto buffer = ConstBuffer<uint8_t>::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);

View File

@ -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"

View File

@ -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 <atomic>
#include <cassert>
#include <span>
#include <jack/jack.h>
#include <jack/types.h>
@ -287,7 +287,7 @@ JackOutput::GetAvailable() const noexcept
* Call jack_ringbuffer_read_advance() on all buffers in the list.
*/
static void
MultiReadAdvance(ConstBuffer<jack_ringbuffer_t *> buffers,
MultiReadAdvance(std::span<jack_ringbuffer_t *const> 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<jack_port_t *> ports, jack_nframes_t nframes)
MultiWriteSilence(std::span<jack_port_t *const> ports, jack_nframes_t nframes)
{
for (auto *i : ports)
WriteSilence(*i, nframes);

View File

@ -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 <boost/lockfree/spsc_queue.hpp>
#include <memory>
#include <span>
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<uint8_t> *ring_buffer;
boost::lockfree::spsc_queue<std::byte> *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<uint8_t>(ring_buffer_size);
ring_buffer = new boost::lockfree::spsc_queue<std::byte>(ring_buffer_size);
pause = false;
started = false;
@ -777,17 +777,17 @@ OSXOutput::Play(const void *chunk, size_t size)
pause = false;
ConstBuffer<uint8_t> input((const uint8_t *)chunk, size);
std::span<const std::byte> input((const std::byte *)chunk, size);
#ifdef ENABLE_DSD
if (dop_enabled) {
input = ConstBuffer<uint8_t>::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);

View File

@ -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"

View File

@ -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<void> input) noexcept {
std::size_t Push(std::span<const std::byte> input) noexcept {
empty.store(false);
std::size_t consumed =
spsc_buffer.push(static_cast<const BYTE *>(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<void> input(chunk, size);
std::span<const std::byte> 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();