summaryrefslogtreecommitdiffstats
path: root/src/multimedia/audio/qsamplecache_p.h
blob: b171c6f886f4a4c49f92454b4080209b210f2b26 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QSAMPLECACHE_P_H
#define QSAMPLECACHE_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/qmutex.h>
#include <QtCore/qobject.h>
#include <QtCore/qpointer.h>
#include <QtCore/qset.h>
#include <QtCore/qthread.h>
#include <QtCore/qfuture.h>
#include <QtCore/qurl.h>
#include <QtCore/private/qglobal_p.h>
#include <QtMultimedia/qaudioformat.h>
#include <QtMultimedia/private/qmaybe_p.h>

#include <memory>
#include <optional>

QT_BEGIN_NAMESPACE

class QSampleCache;

class Q_MULTIMEDIA_EXPORT QSample
{
public:
    friend class QSampleCache;
    enum State : uint8_t {
        Creating,
        Error,
        Ready,
    };
    using SharedSamplePromise = QSharedPointer<QPromise<QMaybe<QSample *, QSample::State>>>;
    ~QSample();

    State state() const;
    const QByteArray& data() const { Q_ASSERT(state() == Ready); return m_soundData; }
    const QAudioFormat& format() const { Q_ASSERT(state() == Ready); return m_audioFormat; }

    void setError();
    void setData(QByteArray, QAudioFormat);

    QSample(QUrl url, QSampleCache *parent);

private:
    QSample();

    // clang-format off
    QSampleCache *m_parent;
    QByteArray   m_soundData;
    QAudioFormat m_audioFormat;
    const QUrl   m_url;
    State        m_state = State::Creating;
    // clang-format on

    friend class QSampleCache;
    void clearParent();
};

using SharedSamplePtr = std::shared_ptr<QSample>;
using WeakSamplePtr = std::weak_ptr<QSample>;

class Q_MULTIMEDIA_EXPORT QSampleCache : public QObject
{
public:
    friend class QSample;

    enum class SampleSourceType
    {
        File,
        NetworkManager,
    };

    explicit QSampleCache(QObject *parent = nullptr);
    ~QSampleCache() override;

    QFuture<SharedSamplePtr> requestSampleFuture(const QUrl &);

    bool isCached(const QUrl& url) const;

    // For tests only
    void setSampleSourceType(SampleSourceType sampleSourceType)
    {
        m_sampleSourceType = sampleSourceType;
    }

private:
    std::unique_ptr<QIODevice> createStreamForSample(QSample &sample);

private:
    using SharedSamplePromise = std::shared_ptr<QPromise<SharedSamplePtr>>;

    mutable QRecursiveMutex m_mutex;

    std::map<QUrl, WeakSamplePtr> m_loadedSamples;
    std::map<QUrl, std::pair<SharedSamplePtr, QList<SharedSamplePromise>>> m_pendingSamples;

    void removeUnreferencedSample(const QUrl &url);

    using SampleLoadResult = std::optional<std::pair<QByteArray, QAudioFormat>>;

    static SampleLoadResult loadSample(QByteArray);

#if QT_CONFIG(thread)
    static SampleLoadResult
    loadSample(const QUrl &, std::optional<SampleSourceType> forceSourceType = std::nullopt);
    QThreadPool m_threadPool;
#endif
    QFuture<SampleLoadResult> loadSampleAsync(const QUrl &);

    std::optional<SampleSourceType> m_sampleSourceType;
};

QT_END_NAMESPACE

#endif // QSAMPLECACHE_P_H