|
@@ -11,26 +11,11 @@
|
|
|
|
|
|
namespace Audio {
|
|
|
|
|
|
-LoaderPlugin::LoaderPlugin(StringView path)
|
|
|
- : m_path(path)
|
|
|
+LoaderPlugin::LoaderPlugin(OwnPtr<Core::Stream::SeekableStream> stream)
|
|
|
+ : m_stream(move(stream))
|
|
|
{
|
|
|
}
|
|
|
|
|
|
-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::BufferedFile::create(LOADER_TRY(Core::Stream::File::open(m_path, Core::Stream::OpenMode::Read))));
|
|
|
-
|
|
|
- return {};
|
|
|
-}
|
|
|
-
|
|
|
Loader::Loader(NonnullOwnPtr<LoaderPlugin> plugin)
|
|
|
: m_plugin(move(plugin))
|
|
|
{
|
|
@@ -38,35 +23,47 @@ Loader::Loader(NonnullOwnPtr<LoaderPlugin> plugin)
|
|
|
|
|
|
Result<NonnullOwnPtr<LoaderPlugin>, LoaderError> Loader::try_create(StringView path)
|
|
|
{
|
|
|
- NonnullOwnPtr<LoaderPlugin> plugin = adopt_own(*new WavLoaderPlugin(path));
|
|
|
- auto initstate0 = plugin->initialize();
|
|
|
- if (!initstate0.is_error())
|
|
|
- return plugin;
|
|
|
+ {
|
|
|
+ auto plugin = WavLoaderPlugin::try_create(path);
|
|
|
+ if (!plugin.is_error())
|
|
|
+ return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
|
|
|
+ }
|
|
|
|
|
|
- plugin = adopt_own(*new FlacLoaderPlugin(path));
|
|
|
- auto initstate1 = plugin->initialize();
|
|
|
- if (!initstate1.is_error())
|
|
|
- return plugin;
|
|
|
+ {
|
|
|
+ auto plugin = FlacLoaderPlugin::try_create(path);
|
|
|
+ if (!plugin.is_error())
|
|
|
+ return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
|
|
|
+ }
|
|
|
|
|
|
- plugin = adopt_own(*new MP3LoaderPlugin(path));
|
|
|
- auto initstate2 = plugin->initialize();
|
|
|
- if (!initstate2.is_error())
|
|
|
- return plugin;
|
|
|
+ {
|
|
|
+ auto plugin = MP3LoaderPlugin::try_create(path);
|
|
|
+ if (!plugin.is_error())
|
|
|
+ return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
|
|
|
+ }
|
|
|
|
|
|
return LoaderError { "No loader plugin available" };
|
|
|
}
|
|
|
|
|
|
Result<NonnullOwnPtr<LoaderPlugin>, LoaderError> Loader::try_create(Bytes buffer)
|
|
|
{
|
|
|
- NonnullOwnPtr<LoaderPlugin> plugin = adopt_own(*new WavLoaderPlugin(buffer));
|
|
|
- if (auto initstate = plugin->initialize(); !initstate.is_error())
|
|
|
- return plugin;
|
|
|
- plugin = adopt_own(*new FlacLoaderPlugin(buffer));
|
|
|
- if (auto initstate = plugin->initialize(); !initstate.is_error())
|
|
|
- return plugin;
|
|
|
- plugin = adopt_own(*new MP3LoaderPlugin(buffer));
|
|
|
- if (auto initstate = plugin->initialize(); !initstate.is_error())
|
|
|
- return plugin;
|
|
|
+ {
|
|
|
+ auto plugin = WavLoaderPlugin::try_create(buffer);
|
|
|
+ if (!plugin.is_error())
|
|
|
+ return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ auto plugin = FlacLoaderPlugin::try_create(buffer);
|
|
|
+ if (!plugin.is_error())
|
|
|
+ return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ auto plugin = MP3LoaderPlugin::try_create(buffer);
|
|
|
+ if (!plugin.is_error())
|
|
|
+ return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
|
|
|
+ }
|
|
|
+
|
|
|
return LoaderError { "No loader plugin available" };
|
|
|
}
|
|
|
|