Kaynağa Gözat

LibAudio: Use `NonnullOwnPtr` to keep track of LoaderPlugin streams

This doesn't have any immediate uses, but this adapts the code a bit
more to `Core::Stream` conventions (as most functions there use
NonnullOwnPtr to handle streams) and it makes it a bit clearer that this
pointer isn't actually supposed to be null. In fact, MP3LoaderPlugin
and FlacLoaderPlugin apparently forgot to check for that completely
before starting to decode data.
Tim Schumacher 2 yıl önce
ebeveyn
işleme
312a41fddf

+ 1 - 1
Userland/Libraries/LibAudio/FlacLoader.cpp

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

+ 1 - 1
Userland/Libraries/LibAudio/FlacLoader.h

@@ -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(OwnPtr<Core::Stream::SeekableStream> stream);
+    explicit FlacLoaderPlugin(NonnullOwnPtr<Core::Stream::SeekableStream> stream);
     virtual ~FlacLoaderPlugin() override = default;
 
     static Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> try_create(StringView path);

+ 1 - 1
Userland/Libraries/LibAudio/Loader.cpp

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

+ 2 - 2
Userland/Libraries/LibAudio/Loader.h

@@ -30,7 +30,7 @@ using MaybeLoaderError = Result<void, LoaderError>;
 
 class LoaderPlugin {
 public:
-    explicit LoaderPlugin(OwnPtr<Core::Stream::SeekableStream> stream);
+    explicit LoaderPlugin(NonnullOwnPtr<Core::Stream::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:
-    OwnPtr<Core::Stream::SeekableStream> m_stream;
+    NonnullOwnPtr<Core::Stream::SeekableStream> m_stream;
 
     Vector<PictureData> m_pictures;
 };

+ 1 - 1
Userland/Libraries/LibAudio/MP3Loader.cpp

@@ -14,7 +14,7 @@ namespace Audio {
 DSP::MDCT<12> MP3LoaderPlugin::s_mdct_12;
 DSP::MDCT<36> MP3LoaderPlugin::s_mdct_36;
 
-MP3LoaderPlugin::MP3LoaderPlugin(OwnPtr<Core::Stream::SeekableStream> stream)
+MP3LoaderPlugin::MP3LoaderPlugin(NonnullOwnPtr<Core::Stream::SeekableStream> stream)
     : LoaderPlugin(move(stream))
 {
 }

+ 1 - 1
Userland/Libraries/LibAudio/MP3Loader.h

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

+ 1 - 7
Userland/Libraries/LibAudio/WavLoader.cpp

@@ -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(OwnPtr<Core::Stream::SeekableStream> stream)
+WavLoaderPlugin::WavLoaderPlugin(NonnullOwnPtr<Core::Stream::SeekableStream> stream)
     : LoaderPlugin(move(stream))
 {
 }
@@ -142,9 +142,6 @@ LoaderSamples WavLoaderPlugin::samples_from_pcm_data(Bytes const& data, size_t s
 
 LoaderSamples WavLoaderPlugin::get_more_samples(size_t max_samples_to_read_from_input)
 {
-    if (!m_stream)
-        return LoaderError { LoaderError::Category::Internal, static_cast<size_t>(m_loaded_samples), "No stream; initialization failed" };
-
     auto remaining_samples = m_total_samples - m_loaded_samples;
     if (remaining_samples <= 0)
         return FixedArray<Sample> {};
@@ -189,9 +186,6 @@ MaybeLoaderError WavLoaderPlugin::seek(int sample_index)
 // Specification reference: http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
 MaybeLoaderError WavLoaderPlugin::parse_header()
 {
-    if (!m_stream)
-        return LoaderError { LoaderError::Category::Internal, 0, "No stream" };
-
     bool ok = true;
     size_t bytes_read = 0;
 

+ 1 - 1
Userland/Libraries/LibAudio/WavLoader.h

@@ -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(OwnPtr<Core::Stream::SeekableStream> stream);
+    explicit WavLoaderPlugin(NonnullOwnPtr<Core::Stream::SeekableStream> stream);
     static Result<NonnullOwnPtr<WavLoaderPlugin>, LoaderError> try_create(StringView path);
     static Result<NonnullOwnPtr<WavLoaderPlugin>, LoaderError> try_create(Bytes buffer);