summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLucas CHOLLET <lucas.chollet@free.fr>2022-10-13 16:05:57 +0200
committerLinus Groh <mail@linusgroh.de>2022-10-14 23:11:23 +0200
commitc837a1a8de492f42d5b5fd0e4b61da4681328ccf (patch)
tree4ed84c69ea49da1842a24ed43b08eef0b3248880 /Userland
parent754b129f4a4428ed9a3bb378c2a23f9e6e731ff6 (diff)
downloadserenity-c837a1a8de492f42d5b5fd0e4b61da4681328ccf.zip
LibAudio: Factorize stream initialisation to base class `LoaderPlugin`
All actual plugins follow the same logic to initialize their stream, this commit factorizes all of this to their base class: `LoaderPlugin`.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibAudio/FlacLoader.cpp11
-rw-r--r--Userland/Libraries/LibAudio/FlacLoader.h6
-rw-r--r--Userland/Libraries/LibAudio/Loader.cpp20
-rw-r--r--Userland/Libraries/LibAudio/Loader.h9
-rw-r--r--Userland/Libraries/LibAudio/MP3Loader.cpp2
-rw-r--r--Userland/Libraries/LibAudio/MP3Loader.h3
-rw-r--r--Userland/Libraries/LibAudio/WavLoader.cpp11
-rw-r--r--Userland/Libraries/LibAudio/WavLoader.h7
8 files changed, 40 insertions, 29 deletions
diff --git a/Userland/Libraries/LibAudio/FlacLoader.cpp b/Userland/Libraries/LibAudio/FlacLoader.cpp
index 19ea92d51d..b91c8433f5 100644
--- a/Userland/Libraries/LibAudio/FlacLoader.cpp
+++ b/Userland/Libraries/LibAudio/FlacLoader.cpp
@@ -26,21 +26,18 @@
namespace Audio {
FlacLoaderPlugin::FlacLoaderPlugin(StringView path)
- : m_path(path)
+ : LoaderPlugin(path)
{
}
-FlacLoaderPlugin::FlacLoaderPlugin(Bytes& buffer)
- : m_backing_memory(buffer)
+FlacLoaderPlugin::FlacLoaderPlugin(Bytes buffer)
+ : LoaderPlugin(buffer)
{
}
MaybeLoaderError FlacLoaderPlugin::initialize()
{
- 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));
+ LOADER_TRY(LoaderPlugin::initialize());
TRY(parse_header());
TRY(reset());
diff --git a/Userland/Libraries/LibAudio/FlacLoader.h b/Userland/Libraries/LibAudio/FlacLoader.h
index 812d5e4cbb..4f94d58e7b 100644
--- a/Userland/Libraries/LibAudio/FlacLoader.h
+++ b/Userland/Libraries/LibAudio/FlacLoader.h
@@ -48,7 +48,7 @@ ALWAYS_INLINE ErrorOr<i32> decode_unsigned_exp_golomb(u8 order, BigEndianInputBi
class FlacLoaderPlugin : public LoaderPlugin {
public:
explicit FlacLoaderPlugin(StringView path);
- explicit FlacLoaderPlugin(Bytes& buffer);
+ explicit FlacLoaderPlugin(Bytes buffer);
~FlacLoaderPlugin()
{
}
@@ -95,8 +95,6 @@ private:
ALWAYS_INLINE ErrorOr<u32, LoaderError> convert_sample_rate_code(u8 sample_rate_code);
ALWAYS_INLINE ErrorOr<PcmSampleFormat, LoaderError> convert_bit_depth_code(u8 bit_depth_code);
- StringView m_path;
-
// Data obtained directly from the FLAC metadata: many values have specific bit counts
u32 m_sample_rate { 0 }; // 20 bit
u8 m_num_channels { 0 }; // 3 bit
@@ -113,8 +111,6 @@ 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<Core::Stream::SeekableStream> m_stream;
- Optional<Bytes> m_backing_memory;
Optional<FlacFrameHeader> m_current_frame;
// Whatever the last get_more_samples() call couldn't return gets stored here.
Vector<Sample, FLAC_BUFFER_SIZE> m_unread_data;
diff --git a/Userland/Libraries/LibAudio/Loader.cpp b/Userland/Libraries/LibAudio/Loader.cpp
index f98212f0ab..163d48e20e 100644
--- a/Userland/Libraries/LibAudio/Loader.cpp
+++ b/Userland/Libraries/LibAudio/Loader.cpp
@@ -11,6 +11,26 @@
namespace Audio {
+LoaderPlugin::LoaderPlugin(StringView path)
+ : m_path(path)
+{
+}
+
+LoaderPlugin::LoaderPlugin(Bytes buffer)
+ : m_backing_memory(buffer)
+{
+}
+
+MaybeLoaderError LoaderPlugin::initialize()
+{
+ 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));
+
+ return {};
+}
+
Loader::Loader(NonnullOwnPtr<LoaderPlugin> plugin)
: m_plugin(move(plugin))
{
diff --git a/Userland/Libraries/LibAudio/Loader.h b/Userland/Libraries/LibAudio/Loader.h
index 737d7dfe96..4f66a5be1c 100644
--- a/Userland/Libraries/LibAudio/Loader.h
+++ b/Userland/Libraries/LibAudio/Loader.h
@@ -18,6 +18,7 @@
#include <LibAudio/LoaderError.h>
#include <LibAudio/Sample.h>
#include <LibAudio/SampleFormats.h>
+#include <LibCore/Stream.h>
namespace Audio {
@@ -28,6 +29,8 @@ using MaybeLoaderError = Result<void, LoaderError>;
class LoaderPlugin {
public:
+ explicit LoaderPlugin(StringView path);
+ explicit LoaderPlugin(Bytes buffer);
virtual ~LoaderPlugin() = default;
virtual MaybeLoaderError initialize() = 0;
@@ -53,6 +56,12 @@ public:
// Human-readable name of the file format, of the form <full abbreviation> (.<ending>)
virtual String format_name() = 0;
virtual PcmSampleFormat pcm_format() = 0;
+
+protected:
+ StringView m_path;
+ OwnPtr<Core::Stream::SeekableStream> m_stream;
+ // The constructor might set this so that we can initialize the data stream later.
+ Optional<Bytes> m_backing_memory;
};
class Loader : public RefCounted<Loader> {
diff --git a/Userland/Libraries/LibAudio/MP3Loader.cpp b/Userland/Libraries/LibAudio/MP3Loader.cpp
index e44a5a4f74..61340c00a7 100644
--- a/Userland/Libraries/LibAudio/MP3Loader.cpp
+++ b/Userland/Libraries/LibAudio/MP3Loader.cpp
@@ -20,7 +20,7 @@ MP3LoaderPlugin::MP3LoaderPlugin(StringView path)
}
MP3LoaderPlugin::MP3LoaderPlugin(Bytes buffer)
- : m_backing_memory(buffer)
+ : LoaderPlugin(buffer)
{
}
diff --git a/Userland/Libraries/LibAudio/MP3Loader.h b/Userland/Libraries/LibAudio/MP3Loader.h
index 7e83c0e929..a0ef5ea8eb 100644
--- a/Userland/Libraries/LibAudio/MP3Loader.h
+++ b/Userland/Libraries/LibAudio/MP3Loader.h
@@ -72,9 +72,6 @@ private:
AK::Optional<MP3::MP3Frame> m_current_frame;
u32 m_current_frame_read;
- StringView m_path;
- OwnPtr<Core::Stream::SeekableStream> m_stream;
- Optional<Bytes const&> m_backing_memory;
OwnPtr<Core::Stream::BigEndianInputBitStream> m_bitstream;
DuplexMemoryStream m_bit_reservoir;
};
diff --git a/Userland/Libraries/LibAudio/WavLoader.cpp b/Userland/Libraries/LibAudio/WavLoader.cpp
index 458eb68eba..62d0d06753 100644
--- a/Userland/Libraries/LibAudio/WavLoader.cpp
+++ b/Userland/Libraries/LibAudio/WavLoader.cpp
@@ -19,23 +19,20 @@ namespace Audio {
static constexpr size_t const maximum_wav_size = 1 * GiB; // FIXME: is there a more appropriate size limit?
WavLoaderPlugin::WavLoaderPlugin(StringView path)
- : m_path(path)
+ : LoaderPlugin(path)
{
}
MaybeLoaderError WavLoaderPlugin::initialize()
{
- 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));
+ LOADER_TRY(LoaderPlugin::initialize());
TRY(parse_header());
return {};
}
-WavLoaderPlugin::WavLoaderPlugin(Bytes const& buffer)
- : m_backing_memory(buffer)
+WavLoaderPlugin::WavLoaderPlugin(Bytes buffer)
+ : LoaderPlugin(buffer)
{
}
diff --git a/Userland/Libraries/LibAudio/WavLoader.h b/Userland/Libraries/LibAudio/WavLoader.h
index ff96120d57..437f7eb54f 100644
--- a/Userland/Libraries/LibAudio/WavLoader.h
+++ b/Userland/Libraries/LibAudio/WavLoader.h
@@ -30,7 +30,7 @@ static constexpr unsigned const WAVE_FORMAT_EXTENSIBLE = 0xFFFE; // Determined b
class WavLoaderPlugin : public LoaderPlugin {
public:
explicit WavLoaderPlugin(StringView path);
- explicit WavLoaderPlugin(Bytes const& buffer);
+ explicit WavLoaderPlugin(Bytes buffer);
virtual MaybeLoaderError initialize() override;
@@ -56,11 +56,6 @@ private:
template<typename SampleReader>
MaybeLoaderError read_samples_from_stream(Core::Stream::Stream& stream, SampleReader read_sample, FixedArray<Sample>& samples) const;
- StringView m_path;
- OwnPtr<Core::Stream::SeekableStream> m_stream;
- // The constructor might set this so that we can initialize the data stream later.
- Optional<Bytes const&> m_backing_memory;
-
u32 m_sample_rate { 0 };
u16 m_num_channels { 0 };
PcmSampleFormat m_sample_format;