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/qobject.h>
19#include <QtCore/qthread.h>
20#include <QtCore/qurl.h>
21#include <QtCore/qmutex.h>
22#include <QtCore/qmap.h>
23#include <QtCore/qset.h>
24#include <qaudioformat.h>
25#include <qnetworkreply.h>
26#include <private/qglobal_p.h>
27
28QT_BEGIN_NAMESPACE
29
30class QIODevice;
31class QNetworkAccessManager;
32class QSampleCache;
33class QWaveDecoder;
34
35// Lives in application thread
36class Q_MULTIMEDIA_EXPORT QSample : public QObject
37{
38 Q_OBJECT
39public:
40 friend class QSampleCache;
41 enum State
42 {
43 Creating,
44 Loading,
45 Error,
46 Ready,
47 };
48
49 State state() const;
50 // These are not (currently) locked because they are only meant to be called after these
51 // variables are updated to their final states
52 const QByteArray& data() const { Q_ASSERT(state() == Ready); return m_soundData; }
53 const QAudioFormat& format() const { Q_ASSERT(state() == Ready); return m_audioFormat; }
54 void release();
55
56Q_SIGNALS:
57 void error();
58 void ready();
59
60protected:
61 QSample(const QUrl& url, QSampleCache *parent);
62
63private Q_SLOTS:
64 void load();
65 void loadingError(QNetworkReply::NetworkError);
66 void decoderError();
67 void readSample();
68 void decoderReady();
69
70private:
71 void onReady();
72 void cleanup();
73 void addRef();
74 void loadIfNecessary();
75 QSample();
76 ~QSample();
77
78 mutable QMutex m_mutex;
79 QSampleCache *m_parent;
80 QByteArray m_soundData;
81 QAudioFormat m_audioFormat;
82 QIODevice *m_stream;
83 QWaveDecoder *m_waveDecoder;
84 QUrl m_url;
85 qint64 m_sampleReadLength;
86 State m_state;
87 int m_ref;
88};
89
90class Q_MULTIMEDIA_EXPORT QSampleCache : public QObject
91{
92 Q_OBJECT
93public:
94 friend class QSample;
95
96 QSampleCache(QObject *parent = nullptr);
97 ~QSampleCache();
98
99 QSample* requestSample(const QUrl& url);
100 void setCapacity(qint64 capacity);
101
102 bool isLoading() const;
103 bool isCached(const QUrl& url) const;
104
105private:
106 QMap<QUrl, QSample*> m_samples;
107 QSet<QSample*> m_staleSamples;
108 QNetworkAccessManager *m_networkAccessManager;
109 mutable QRecursiveMutex m_mutex;
110 qint64 m_capacity;
111 qint64 m_usage;
112 QThread m_loadingThread;
113
114 QNetworkAccessManager& networkAccessManager();
115 void refresh(qint64 usageChange);
116 bool notifyUnreferencedSample(QSample* sample);
117 void removeUnreferencedSample(QSample* sample);
118 void unloadSample(QSample* sample);
119
120 void loadingRelease();
121 int m_loadingRefCount;
122 QMutex m_loadingMutex;
123};
124
125QT_END_NAMESPACE
126
127#endif // QSAMPLECACHE_P_H
128

source code of qtmultimedia/src/multimedia/audio/qsamplecache_p.h