mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
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`.
This commit is contained in:
parent
754b129f4a
commit
c837a1a8de
Notes:
sideshowbarker
2024-07-17 05:47:13 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/c837a1a8de Pull-request: https://github.com/SerenityOS/serenity/pull/15568 Reviewed-by: https://github.com/linusg ✅ Reviewed-by: https://github.com/sin-ack ✅
10 changed files with 42 additions and 31 deletions
|
@ -11,7 +11,7 @@
|
|||
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
|
||||
{
|
||||
auto flac_data = ByteBuffer::copy(data, size).release_value();
|
||||
auto flac = make<Audio::FlacLoaderPlugin>(flac_data);
|
||||
auto flac = make<Audio::FlacLoaderPlugin>(flac_data.bytes());
|
||||
|
||||
if (flac->initialize().is_error())
|
||||
return 1;
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
|
||||
{
|
||||
auto flac_data = ByteBuffer::copy(data, size).release_value();
|
||||
auto mp3 = make<Audio::MP3LoaderPlugin>(flac_data);
|
||||
auto mp3 = make<Audio::MP3LoaderPlugin>(flac_data.bytes());
|
||||
|
||||
if (mp3->initialize().is_error())
|
||||
return 1;
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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))
|
||||
{
|
||||
|
|
|
@ -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> {
|
||||
|
|
|
@ -20,7 +20,7 @@ MP3LoaderPlugin::MP3LoaderPlugin(StringView path)
|
|||
}
|
||||
|
||||
MP3LoaderPlugin::MP3LoaderPlugin(Bytes buffer)
|
||||
: m_backing_memory(buffer)
|
||||
: LoaderPlugin(buffer)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue