1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:27:44 +00:00

LibAudio: Extract loader stream creation from the plugins

This removes a lot of duplicated stream creation code from the plugins,
and also simplifies the way that the appropriate plugin is found. This
mirrors the ImageDecoderPlugin design and necessitates new sniffing
methods on the loaders.
This commit is contained in:
kleines Filmröllchen 2023-06-24 17:04:38 +02:00 committed by Sam Atkins
parent dfd48ab643
commit 5f1dbbaaa6
15 changed files with 124 additions and 131 deletions

View file

@ -8,6 +8,7 @@
#include "MP3HuffmanTables.h"
#include "MP3Tables.h"
#include "MP3Types.h"
#include <AK/Endian.h>
#include <AK/FixedArray.h>
#include <LibCore/File.h>
@ -21,23 +22,34 @@ MP3LoaderPlugin::MP3LoaderPlugin(NonnullOwnPtr<SeekableStream> stream)
{
}
Result<NonnullOwnPtr<MP3LoaderPlugin>, LoaderError> MP3LoaderPlugin::create(StringView path)
bool MP3LoaderPlugin::sniff(SeekableStream& stream)
{
auto stream = LOADER_TRY(Core::InputBufferedFile::create(LOADER_TRY(Core::File::open(path, Core::File::OpenMode::Read))));
auto loader = make<MP3LoaderPlugin>(move(stream));
auto maybe_bit_stream = try_make<BigEndianInputBitStream>(MaybeOwned<Stream>(stream));
if (maybe_bit_stream.is_error())
return false;
auto bit_stream = maybe_bit_stream.release_value();
LOADER_TRY(loader->initialize());
auto synchronization_result = synchronize(*bit_stream, 0);
if (synchronization_result.is_error())
return false;
auto maybe_mp3 = stream.read_value<BigEndian<u16>>();
if (maybe_mp3.is_error())
return false;
return loader;
ErrorOr<int> id = bit_stream->read_bit();
if (id.is_error() || id.value() != 1)
return false;
auto raw_layer = bit_stream->read_bits(2);
if (raw_layer.is_error())
return false;
auto layer = MP3::Tables::LayerNumberLookup[raw_layer.value()];
return layer == 3;
}
Result<NonnullOwnPtr<MP3LoaderPlugin>, LoaderError> MP3LoaderPlugin::create(Bytes buffer)
ErrorOr<NonnullOwnPtr<LoaderPlugin>, LoaderError> MP3LoaderPlugin::create(NonnullOwnPtr<SeekableStream> stream)
{
auto stream = LOADER_TRY(try_make<FixedMemoryStream>(buffer));
auto loader = make<MP3LoaderPlugin>(move(stream));
LOADER_TRY(loader->initialize());
return loader;
}