summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibAudio/Loader.cpp
blob: 7b662326ef426a3586d47e801ffc78d021b02cb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * Copyright (c) 2018-2021, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibAudio/FlacLoader.h>
#include <LibAudio/Loader.h>
#include <LibAudio/MP3Loader.h>
#include <LibAudio/WavLoader.h>

namespace Audio {

LoaderPlugin::LoaderPlugin(OwnPtr<Core::Stream::SeekableStream> stream)
    : m_stream(move(stream))
{
}

Loader::Loader(NonnullOwnPtr<LoaderPlugin> plugin)
    : m_plugin(move(plugin))
{
}

Result<NonnullOwnPtr<LoaderPlugin>, LoaderError> Loader::try_create(StringView path)
{
    {
        auto plugin = WavLoaderPlugin::try_create(path);
        if (!plugin.is_error())
            return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
    }

    {
        auto plugin = FlacLoaderPlugin::try_create(path);
        if (!plugin.is_error())
            return NonnullOwnPtr<LoaderPlugin>(plugin.release_value());
    }

    {
        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)
{
    {
        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" };
}

}