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

AK: Move Stream and SeekableStream from LibCore

`Stream` will be qualified as `AK::Stream` until we remove the
`Core::Stream` namespace. `IODevice` now reuses the `SeekMode` that is
defined by `SeekableStream`, since defining its own would require us to
qualify it with `AK::SeekMode` everywhere.
This commit is contained in:
Tim Schumacher 2023-01-22 05:09:11 +01:00 committed by Andrew Kaster
parent 5f2ea31816
commit 8464da1439
96 changed files with 620 additions and 586 deletions

View file

@ -25,7 +25,7 @@
namespace Audio {
FlacLoaderPlugin::FlacLoaderPlugin(NonnullOwnPtr<Core::Stream::SeekableStream> stream)
FlacLoaderPlugin::FlacLoaderPlugin(NonnullOwnPtr<SeekableStream> stream)
: LoaderPlugin(move(stream))
{
}
@ -60,7 +60,7 @@ MaybeLoaderError FlacLoaderPlugin::initialize()
// 11.5 STREAM
MaybeLoaderError FlacLoaderPlugin::parse_header()
{
auto bit_input = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*m_stream)));
auto bit_input = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*m_stream)));
// A mixture of VERIFY and the non-crashing TRY().
#define FLAC_VERIFY(check, category, msg) \
@ -79,7 +79,7 @@ MaybeLoaderError FlacLoaderPlugin::parse_header()
auto streaminfo = TRY(next_meta_block(*bit_input));
FLAC_VERIFY(streaminfo.type == FlacMetadataBlockType::STREAMINFO, LoaderError::Category::Format, "First block must be STREAMINFO");
auto streaminfo_data_memory = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(streaminfo.data.bytes()));
auto streaminfo_data = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*streaminfo_data_memory)));
auto streaminfo_data = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*streaminfo_data_memory)));
// 11.10 METADATA_BLOCK_STREAMINFO
m_min_block_size = LOADER_TRY(streaminfo_data->read_bits<u16>(16));
@ -150,7 +150,7 @@ MaybeLoaderError FlacLoaderPlugin::parse_header()
MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
{
auto memory_stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(block.data.bytes()));
auto picture_block_bytes = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*memory_stream)));
auto picture_block_bytes = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*memory_stream)));
PictureData picture {};
@ -159,12 +159,12 @@ MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
auto const mime_string_length = LOADER_TRY(picture_block_bytes->read_bits(32));
// Note: We are seeking before reading the value to ensure that we stayed inside buffer's size.
auto offset_before_seeking = memory_stream->offset();
LOADER_TRY(memory_stream->seek(mime_string_length, Core::Stream::SeekMode::FromCurrentPosition));
LOADER_TRY(memory_stream->seek(mime_string_length, SeekMode::FromCurrentPosition));
picture.mime_string = { block.data.bytes().data() + offset_before_seeking, (size_t)mime_string_length };
auto const description_string_length = LOADER_TRY(picture_block_bytes->read_bits(32));
offset_before_seeking = memory_stream->offset();
LOADER_TRY(memory_stream->seek(description_string_length, Core::Stream::SeekMode::FromCurrentPosition));
LOADER_TRY(memory_stream->seek(description_string_length, SeekMode::FromCurrentPosition));
picture.description_string = Vector<u32> { Span<u32> { reinterpret_cast<u32*>(block.data.bytes().data() + offset_before_seeking), (size_t)description_string_length } };
picture.width = LOADER_TRY(picture_block_bytes->read_bits(32));
@ -175,7 +175,7 @@ MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
auto const picture_size = LOADER_TRY(picture_block_bytes->read_bits(32));
offset_before_seeking = memory_stream->offset();
LOADER_TRY(memory_stream->seek(picture_size, Core::Stream::SeekMode::FromCurrentPosition));
LOADER_TRY(memory_stream->seek(picture_size, SeekMode::FromCurrentPosition));
picture.data = Vector<u8> { Span<u8> { block.data.bytes().data() + offset_before_seeking, (size_t)picture_size } };
m_pictures.append(move(picture));
@ -187,7 +187,7 @@ MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block)
{
auto memory_stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(block.data.bytes()));
auto seektable_bytes = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*memory_stream)));
auto seektable_bytes = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*memory_stream)));
for (size_t i = 0; i < block.length / 18; ++i) {
// 11.14. SEEKPOINT
FlacSeekPoint seekpoint {
@ -259,7 +259,7 @@ MaybeLoaderError FlacLoaderPlugin::seek(int int_sample_index)
// No seektable or no fitting entry: Perform normal forward read
if (!maybe_target_seekpoint.has_value()) {
if (sample_index < m_loaded_samples) {
LOADER_TRY(m_stream->seek(m_data_start_location, Core::Stream::SeekMode::SetPosition));
LOADER_TRY(m_stream->seek(m_data_start_location, SeekMode::SetPosition));
m_loaded_samples = 0;
}
auto to_read = sample_index - m_loaded_samples;
@ -279,7 +279,7 @@ MaybeLoaderError FlacLoaderPlugin::seek(int int_sample_index)
dbgln_if(AFLACLOADER_DEBUG, "Seeking to seektable: sample index {}, byte offset {}, sample count {}", target_seekpoint.sample_index, target_seekpoint.byte_offset, target_seekpoint.num_samples);
auto position = target_seekpoint.byte_offset + m_data_start_location;
if (m_stream->seek(static_cast<i64>(position), Core::Stream::SeekMode::SetPosition).is_error())
if (m_stream->seek(static_cast<i64>(position), SeekMode::SetPosition).is_error())
return LoaderError { LoaderError::Category::IO, m_loaded_samples, DeprecatedString::formatted("Invalid seek position {}", position) };
auto remaining_samples_after_seekpoint = sample_index - m_data_start_location;
@ -333,7 +333,7 @@ MaybeLoaderError FlacLoaderPlugin::next_frame(Span<Sample> target_vector)
} \
} while (0)
auto bit_stream = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*m_stream)));
auto bit_stream = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*m_stream)));
// TODO: Check the CRC-16 checksum (and others) by keeping track of read data

View file

@ -47,7 +47,7 @@ ALWAYS_INLINE ErrorOr<i32> decode_unsigned_exp_golomb(u8 order, BigEndianInputBi
// https://datatracker.ietf.org/doc/html/draft-ietf-cellar-flac-03 (newer IETF draft that uses incompatible numberings and names)
class FlacLoaderPlugin : public LoaderPlugin {
public:
explicit FlacLoaderPlugin(NonnullOwnPtr<Core::Stream::SeekableStream> stream);
explicit FlacLoaderPlugin(NonnullOwnPtr<SeekableStream> stream);
virtual ~FlacLoaderPlugin() override = default;
static Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> create(StringView path);

View file

@ -11,7 +11,7 @@
namespace Audio {
LoaderPlugin::LoaderPlugin(NonnullOwnPtr<Core::Stream::SeekableStream> stream)
LoaderPlugin::LoaderPlugin(NonnullOwnPtr<SeekableStream> stream)
: m_stream(move(stream))
{
}

View file

@ -30,7 +30,7 @@ using MaybeLoaderError = Result<void, LoaderError>;
class LoaderPlugin {
public:
explicit LoaderPlugin(NonnullOwnPtr<Core::Stream::SeekableStream> stream);
explicit LoaderPlugin(NonnullOwnPtr<SeekableStream> stream);
virtual ~LoaderPlugin() = default;
virtual LoaderSamples get_more_samples(size_t max_bytes_to_read_from_input = 128 * KiB) = 0;
@ -58,7 +58,7 @@ public:
Vector<PictureData> const& pictures() const { return m_pictures; };
protected:
NonnullOwnPtr<Core::Stream::SeekableStream> m_stream;
NonnullOwnPtr<SeekableStream> m_stream;
Vector<PictureData> m_pictures;
};

View file

@ -14,7 +14,7 @@ namespace Audio {
DSP::MDCT<12> MP3LoaderPlugin::s_mdct_12;
DSP::MDCT<36> MP3LoaderPlugin::s_mdct_36;
MP3LoaderPlugin::MP3LoaderPlugin(NonnullOwnPtr<Core::Stream::SeekableStream> stream)
MP3LoaderPlugin::MP3LoaderPlugin(NonnullOwnPtr<SeekableStream> stream)
: LoaderPlugin(move(stream))
{
}
@ -41,7 +41,7 @@ Result<NonnullOwnPtr<MP3LoaderPlugin>, LoaderError> MP3LoaderPlugin::create(Byte
MaybeLoaderError MP3LoaderPlugin::initialize()
{
m_bitstream = LOADER_TRY(Core::Stream::BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*m_stream)));
m_bitstream = LOADER_TRY(Core::Stream::BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*m_stream)));
TRY(synchronize());
@ -55,7 +55,7 @@ MaybeLoaderError MP3LoaderPlugin::initialize()
TRY(build_seek_table());
LOADER_TRY(m_stream->seek(0, Core::Stream::SeekMode::SetPosition));
LOADER_TRY(m_stream->seek(0, SeekMode::SetPosition));
return {};
}
@ -76,7 +76,7 @@ MaybeLoaderError MP3LoaderPlugin::seek(int const position)
{
for (auto const& seek_entry : m_seek_table) {
if (seek_entry.get<1>() >= position) {
LOADER_TRY(m_stream->seek(seek_entry.get<0>(), Core::Stream::SeekMode::SetPosition));
LOADER_TRY(m_stream->seek(seek_entry.get<0>(), SeekMode::SetPosition));
m_loaded_samples = seek_entry.get<1>();
break;
}
@ -140,7 +140,7 @@ MaybeLoaderError MP3LoaderPlugin::build_seek_table()
m_bitstream->align_to_byte_boundary();
while (!synchronize().is_error()) {
auto const frame_pos = -2 + LOADER_TRY(m_stream->seek(0, Core::Stream::SeekMode::FromCurrentPosition));
auto const frame_pos = -2 + LOADER_TRY(m_stream->seek(0, SeekMode::FromCurrentPosition));
auto error_or_header = read_header();
if (error_or_header.is_error() || error_or_header.value().id != 1 || error_or_header.value().layer != 3) {
@ -152,7 +152,7 @@ MaybeLoaderError MP3LoaderPlugin::build_seek_table()
if (frame_count % 10 == 0)
m_seek_table.append({ frame_pos, sample_count });
LOADER_TRY(m_stream->seek(error_or_header.value().frame_size - 6, Core::Stream::SeekMode::FromCurrentPosition));
LOADER_TRY(m_stream->seek(error_or_header.value().frame_size - 6, SeekMode::FromCurrentPosition));
// TODO: This is just here to clear the bitstream buffer.
// Bitstream should have a method to sync its state to the underlying stream.
@ -242,7 +242,7 @@ ErrorOr<MP3::MP3Frame, LoaderError> MP3LoaderPlugin::read_frame_data(MP3::Header
TRY(m_bit_reservoir.discard(old_reservoir_size - frame.main_data_begin));
auto reservoir_stream = TRY(Core::Stream::BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(m_bit_reservoir)));
auto reservoir_stream = TRY(Core::Stream::BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(m_bit_reservoir)));
for (size_t granule_index = 0; granule_index < 2; granule_index++) {
for (size_t channel_index = 0; channel_index < header.channel_count(); channel_index++) {

View file

@ -22,7 +22,7 @@ struct ScaleFactorBand;
class MP3LoaderPlugin : public LoaderPlugin {
public:
explicit MP3LoaderPlugin(NonnullOwnPtr<Core::Stream::SeekableStream> stream);
explicit MP3LoaderPlugin(NonnullOwnPtr<SeekableStream> stream);
virtual ~MP3LoaderPlugin() = default;
static Result<NonnullOwnPtr<MP3LoaderPlugin>, LoaderError> create(StringView path);

View file

@ -18,7 +18,7 @@ namespace Audio {
static constexpr size_t const maximum_wav_size = 1 * GiB; // FIXME: is there a more appropriate size limit?
WavLoaderPlugin::WavLoaderPlugin(NonnullOwnPtr<Core::Stream::SeekableStream> stream)
WavLoaderPlugin::WavLoaderPlugin(NonnullOwnPtr<SeekableStream> stream)
: LoaderPlugin(move(stream))
{
}
@ -51,7 +51,7 @@ MaybeLoaderError WavLoaderPlugin::initialize()
}
template<typename SampleReader>
MaybeLoaderError WavLoaderPlugin::read_samples_from_stream(Core::Stream::Stream& stream, SampleReader read_sample, FixedArray<Sample>& samples) const
MaybeLoaderError WavLoaderPlugin::read_samples_from_stream(AK::Stream& stream, SampleReader read_sample, FixedArray<Sample>& samples) const
{
switch (m_num_channels) {
case 1:
@ -72,7 +72,7 @@ MaybeLoaderError WavLoaderPlugin::read_samples_from_stream(Core::Stream::Stream&
}
// There's no i24 type + we need to do the endianness conversion manually anyways.
static ErrorOr<double> read_sample_int24(Core::Stream::Stream& stream)
static ErrorOr<double> read_sample_int24(AK::Stream& stream)
{
u8 byte = 0;
TRY(stream.read(Bytes { &byte, 1 }));
@ -93,7 +93,7 @@ static ErrorOr<double> read_sample_int24(Core::Stream::Stream& stream)
}
template<typename T>
static ErrorOr<double> read_sample(Core::Stream::Stream& stream)
static ErrorOr<double> read_sample(AK::Stream& stream)
{
T sample { 0 };
TRY(stream.read(Bytes { &sample, sizeof(T) }));
@ -177,7 +177,7 @@ MaybeLoaderError WavLoaderPlugin::seek(int sample_index)
size_t sample_offset = m_byte_offset_of_data_samples + static_cast<size_t>(sample_index * m_num_channels * (pcm_bits_per_sample(m_sample_format) / 8));
LOADER_TRY(m_stream->seek(sample_offset, Core::Stream::SeekMode::SetPosition));
LOADER_TRY(m_stream->seek(sample_offset, SeekMode::SetPosition));
m_loaded_samples = sample_index;
return {};

View file

@ -29,7 +29,7 @@ static constexpr unsigned const WAVE_FORMAT_EXTENSIBLE = 0xFFFE; // Determined b
// Parses and reads audio data from a WAV file.
class WavLoaderPlugin : public LoaderPlugin {
public:
explicit WavLoaderPlugin(NonnullOwnPtr<Core::Stream::SeekableStream> stream);
explicit WavLoaderPlugin(NonnullOwnPtr<SeekableStream> stream);
static Result<NonnullOwnPtr<WavLoaderPlugin>, LoaderError> create(StringView path);
static Result<NonnullOwnPtr<WavLoaderPlugin>, LoaderError> create(Bytes buffer);
@ -55,7 +55,7 @@ private:
LoaderSamples samples_from_pcm_data(Bytes const& data, size_t samples_to_read) const;
template<typename SampleReader>
MaybeLoaderError read_samples_from_stream(Core::Stream::Stream& stream, SampleReader read_sample, FixedArray<Sample>& samples) const;
MaybeLoaderError read_samples_from_stream(AK::Stream& stream, SampleReader read_sample, FixedArray<Sample>& samples) const;
u32 m_sample_rate { 0 };
u16 m_num_channels { 0 };