From 597a614ce695c6e14326fdd36edd80d168d62da7 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Thu, 13 Oct 2022 12:13:15 +0200 Subject: [PATCH] LibAudio: Remove the last occurrence of `Core::File` in the Flac plugin The design is deeply inspired from what is done in the Wav plugin. --- Userland/Libraries/LibAudio/FlacLoader.cpp | 24 ++++++---------------- Userland/Libraries/LibAudio/FlacLoader.h | 4 ++-- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/Userland/Libraries/LibAudio/FlacLoader.cpp b/Userland/Libraries/LibAudio/FlacLoader.cpp index fd89e5e7e8..19ea92d51d 100644 --- a/Userland/Libraries/LibAudio/FlacLoader.cpp +++ b/Userland/Libraries/LibAudio/FlacLoader.cpp @@ -26,33 +26,21 @@ namespace Audio { FlacLoaderPlugin::FlacLoaderPlugin(StringView path) - : m_file(Core::File::construct(path)) + : m_path(path) { - if (!m_file->open(Core::OpenMode::ReadOnly)) { - m_error = LoaderError { String::formatted("Can't open file: {}", m_file->error_string()) }; - return; - } - - auto maybe_stream = Core::Stream::BufferedFile::create(MUST(Core::Stream::File::open(path, Core::Stream::OpenMode::Read)), FLAC_BUFFER_SIZE); - if (maybe_stream.is_error()) - m_error = LoaderError { "Can't open file stream" }; - else - m_stream = maybe_stream.release_value(); } FlacLoaderPlugin::FlacLoaderPlugin(Bytes& buffer) + : m_backing_memory(buffer) { - auto maybe_stream = Core::Stream::MemoryStream::construct(buffer); - if (maybe_stream.is_error()) - m_error = LoaderError { "Can't open memory stream" }; - else - m_stream = maybe_stream.release_value(); } MaybeLoaderError FlacLoaderPlugin::initialize() { - if (m_error.has_value()) - return m_error.release_value(); + if (m_backing_memory.has_value()) + m_stream = LOADER_TRY(Core::Stream::MemoryStream::construct(m_backing_memory.value())); + else + m_stream = LOADER_TRY(Core::Stream::File::open(m_path, Core::Stream::OpenMode::Read)); TRY(parse_header()); TRY(reset()); diff --git a/Userland/Libraries/LibAudio/FlacLoader.h b/Userland/Libraries/LibAudio/FlacLoader.h index 58373a3ada..812d5e4cbb 100644 --- a/Userland/Libraries/LibAudio/FlacLoader.h +++ b/Userland/Libraries/LibAudio/FlacLoader.h @@ -95,8 +95,7 @@ private: ALWAYS_INLINE ErrorOr convert_sample_rate_code(u8 sample_rate_code); ALWAYS_INLINE ErrorOr convert_bit_depth_code(u8 bit_depth_code); - RefPtr m_file; - Optional m_error {}; + StringView m_path; // Data obtained directly from the FLAC metadata: many values have specific bit counts u32 m_sample_rate { 0 }; // 20 bit @@ -115,6 +114,7 @@ private: // keep track of the start of the data in the FLAC stream to seek back more easily u64 m_data_start_location { 0 }; OwnPtr m_stream; + Optional m_backing_memory; Optional m_current_frame; // Whatever the last get_more_samples() call couldn't return gets stored here. Vector m_unread_data;