| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QSAMPLECACHE_P_H |
| 5 | #define QSAMPLECACHE_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtCore/qmutex.h> |
| 19 | #include <QtCore/qobject.h> |
| 20 | #include <QtCore/qpointer.h> |
| 21 | #include <QtCore/qset.h> |
| 22 | #include <QtCore/qthread.h> |
| 23 | #include <QtCore/qfuture.h> |
| 24 | #include <QtCore/qurl.h> |
| 25 | #include <QtCore/private/qglobal_p.h> |
| 26 | #include <QtCore/private/qexpected_p.h> |
| 27 | #include <QtMultimedia/qaudioformat.h> |
| 28 | |
| 29 | #include <memory> |
| 30 | #include <optional> |
| 31 | |
| 32 | QT_BEGIN_NAMESPACE |
| 33 | |
| 34 | class QSampleCache; |
| 35 | |
| 36 | class Q_MULTIMEDIA_EXPORT QSample |
| 37 | { |
| 38 | public: |
| 39 | friend class QSampleCache; |
| 40 | enum State : uint8_t { |
| 41 | Creating, |
| 42 | Error, |
| 43 | Ready, |
| 44 | }; |
| 45 | using SharedSamplePromise = QSharedPointer<QPromise<q23::expected<QSample *, QSample::State>>>; |
| 46 | ~QSample(); |
| 47 | |
| 48 | State state() const; |
| 49 | const QByteArray& data() const { Q_ASSERT(state() == Ready); return m_soundData; } |
| 50 | const QAudioFormat& format() const { Q_ASSERT(state() == Ready); return m_audioFormat; } |
| 51 | |
| 52 | void setError(); |
| 53 | void setData(QByteArray, QAudioFormat); |
| 54 | |
| 55 | QSample(QUrl url, QSampleCache *parent); |
| 56 | |
| 57 | private: |
| 58 | QSample(); |
| 59 | |
| 60 | // clang-format off |
| 61 | QSampleCache *m_parent; |
| 62 | QByteArray m_soundData; |
| 63 | QAudioFormat m_audioFormat; |
| 64 | const QUrl m_url; |
| 65 | State m_state = State::Creating; |
| 66 | // clang-format on |
| 67 | |
| 68 | friend class QSampleCache; |
| 69 | void clearParent(); |
| 70 | }; |
| 71 | |
| 72 | using SharedSamplePtr = std::shared_ptr<QSample>; |
| 73 | using WeakSamplePtr = std::weak_ptr<QSample>; |
| 74 | |
| 75 | class Q_MULTIMEDIA_EXPORT QSampleCache : public QObject |
| 76 | { |
| 77 | public: |
| 78 | friend class QSample; |
| 79 | |
| 80 | enum class SampleSourceType |
| 81 | { |
| 82 | File, |
| 83 | NetworkManager, |
| 84 | }; |
| 85 | |
| 86 | explicit QSampleCache(QObject *parent = nullptr); |
| 87 | ~QSampleCache() override; |
| 88 | |
| 89 | QFuture<SharedSamplePtr> requestSampleFuture(const QUrl &); |
| 90 | |
| 91 | bool isCached(const QUrl& url) const; |
| 92 | |
| 93 | // For tests only |
| 94 | void setSampleSourceType(SampleSourceType sampleSourceType) |
| 95 | { |
| 96 | m_sampleSourceType = sampleSourceType; |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | std::unique_ptr<QIODevice> createStreamForSample(QSample &sample); |
| 101 | |
| 102 | private: |
| 103 | using SharedSamplePromise = std::shared_ptr<QPromise<SharedSamplePtr>>; |
| 104 | |
| 105 | mutable QRecursiveMutex m_mutex; |
| 106 | |
| 107 | std::map<QUrl, WeakSamplePtr> m_loadedSamples; |
| 108 | std::map<QUrl, std::pair<SharedSamplePtr, QList<SharedSamplePromise>>> m_pendingSamples; |
| 109 | |
| 110 | void removeUnreferencedSample(const QUrl &url); |
| 111 | |
| 112 | using SampleLoadResult = std::optional<std::pair<QByteArray, QAudioFormat>>; |
| 113 | |
| 114 | static SampleLoadResult loadSample(QByteArray); |
| 115 | |
| 116 | #if QT_CONFIG(thread) |
| 117 | static SampleLoadResult |
| 118 | loadSample(const QUrl &, std::optional<SampleSourceType> forceSourceType = std::nullopt); |
| 119 | QThreadPool m_threadPool; |
| 120 | #endif |
| 121 | QFuture<SampleLoadResult> loadSampleAsync(const QUrl &); |
| 122 | |
| 123 | std::optional<SampleSourceType> m_sampleSourceType; |
| 124 | }; |
| 125 | |
| 126 | QT_END_NAMESPACE |
| 127 | |
| 128 | #endif // QSAMPLECACHE_P_H |
| 129 | |