Loader.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2018-2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibAudio/FlacLoader.h>
  7. #include <LibAudio/Loader.h>
  8. #include <LibAudio/MP3Loader.h>
  9. #include <LibAudio/WavLoader.h>
  10. namespace Audio {
  11. LoaderPlugin::LoaderPlugin(OwnPtr<Core::Stream::SeekableStream> stream)
  12. : m_stream(move(stream))
  13. {
  14. }
  15. Loader::Loader(NonnullOwnPtr<LoaderPlugin> plugin)
  16. : m_plugin(move(plugin))
  17. {
  18. }
  19. Result<NonnullOwnPtr<LoaderPlugin>, LoaderError> Loader::try_create(StringView path)
  20. {
  21. {
  22. auto plugin = WavLoaderPlugin::try_create(path);
  23. if (!plugin.is_error())
  24. return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
  25. }
  26. {
  27. auto plugin = FlacLoaderPlugin::try_create(path);
  28. if (!plugin.is_error())
  29. return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
  30. }
  31. {
  32. auto plugin = MP3LoaderPlugin::try_create(path);
  33. if (!plugin.is_error())
  34. return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
  35. }
  36. return LoaderError { "No loader plugin available" };
  37. }
  38. Result<NonnullOwnPtr<LoaderPlugin>, LoaderError> Loader::try_create(Bytes buffer)
  39. {
  40. {
  41. auto plugin = WavLoaderPlugin::try_create(buffer);
  42. if (!plugin.is_error())
  43. return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
  44. }
  45. {
  46. auto plugin = FlacLoaderPlugin::try_create(buffer);
  47. if (!plugin.is_error())
  48. return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
  49. }
  50. {
  51. auto plugin = MP3LoaderPlugin::try_create(buffer);
  52. if (!plugin.is_error())
  53. return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
  54. }
  55. return LoaderError { "No loader plugin available" };
  56. }
  57. }