1// Copyright (C) 2016 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#include "qsamplecache_p.h"
5#include "qwavedecoder.h"
6
7#include <QtNetwork/QNetworkAccessManager>
8#include <QtNetwork/QNetworkReply>
9#include <QtNetwork/QNetworkRequest>
10
11#include <QtCore/QDebug>
12#include <QtCore/qloggingcategory.h>
13
14static Q_LOGGING_CATEGORY(qLcSampleCache, "qt.multimedia.samplecache")
15
16#include <mutex>
17
18QT_BEGIN_NAMESPACE
19
20
21/*!
22 \class QSampleCache
23 \internal
24
25 When you want to get a sound sample data, you need to request the QSample reference from QSampleCache.
26
27
28 \code
29 QSample *m_sample; // class member.
30
31 private Q_SLOTS:
32 void decoderError();
33 void sampleReady();
34 \endcode
35
36 \code
37 Q_GLOBAL_STATIC(QSampleCache, sampleCache) //declare a singleton manager
38 \endcode
39
40 \code
41 m_sample = sampleCache()->requestSample(url);
42 switch(m_sample->state()) {
43 case QSample::Ready:
44 sampleReady();
45 break;
46 case QSample::Error:
47 decoderError();
48 break;
49 default:
50 connect(m_sample, SIGNAL(error()), this, SLOT(decoderError()));
51 connect(m_sample, SIGNAL(ready()), this, SLOT(sampleReady()));
52 break;
53 }
54 \endcode
55
56 When you no longer need the sound sample data, you need to release it:
57
58 \code
59 if (m_sample) {
60 m_sample->release();
61 m_sample = 0;
62 }
63 \endcode
64*/
65
66QSampleCache::QSampleCache(QObject *parent)
67 : QObject(parent)
68 , m_networkAccessManager(nullptr)
69 , m_capacity(0)
70 , m_usage(0)
71 , m_loadingRefCount(0)
72{
73 m_loadingThread.setObjectName(QLatin1String("QSampleCache::LoadingThread"));
74}
75
76QNetworkAccessManager& QSampleCache::networkAccessManager()
77{
78 if (!m_networkAccessManager)
79 m_networkAccessManager = new QNetworkAccessManager();
80 return *m_networkAccessManager;
81}
82
83QSampleCache::~QSampleCache()
84{
85 const std::lock_guard<QRecursiveMutex> locker(m_mutex);
86
87 m_loadingThread.quit();
88 m_loadingThread.wait();
89
90 // Killing the loading thread means that no samples can be
91 // deleted using deleteLater. And some samples that had deleteLater
92 // already called won't have been processed (m_staleSamples)
93 for (auto it = m_samples.cbegin(), end = m_samples.cend(); it != end; ++it)
94 delete it.value();
95
96 const auto copyStaleSamples = m_staleSamples; //deleting a sample does affect the m_staleSamples list, but we create a copy
97 for (QSample* sample : copyStaleSamples)
98 delete sample;
99
100 delete m_networkAccessManager;
101}
102
103void QSampleCache::loadingRelease()
104{
105 QMutexLocker locker(&m_loadingMutex);
106 m_loadingRefCount--;
107 if (m_loadingRefCount == 0) {
108 if (m_loadingThread.isRunning()) {
109 if (m_networkAccessManager) {
110 m_networkAccessManager->deleteLater();
111 m_networkAccessManager = nullptr;
112 }
113 m_loadingThread.exit();
114 }
115 }
116}
117
118bool QSampleCache::isLoading() const
119{
120 return m_loadingThread.isRunning();
121}
122
123bool QSampleCache::isCached(const QUrl &url) const
124{
125 const std::lock_guard<QRecursiveMutex> locker(m_mutex);
126 return m_samples.contains(key: url);
127}
128
129QSample* QSampleCache::requestSample(const QUrl& url)
130{
131 //lock and add first to make sure live loadingThread will not be killed during this function call
132 m_loadingMutex.lock();
133 const bool needsThreadStart = m_loadingRefCount == 0;
134 m_loadingRefCount++;
135 m_loadingMutex.unlock();
136
137 qCDebug(qLcSampleCache) << "QSampleCache: request sample [" << url << "]";
138 std::unique_lock<QRecursiveMutex> locker(m_mutex);
139 QMap<QUrl, QSample*>::iterator it = m_samples.find(key: url);
140 QSample* sample;
141 if (it == m_samples.end()) {
142 if (needsThreadStart) {
143 // Previous thread might be finishing, need to wait for it. If not, this is a no-op.
144 m_loadingThread.wait();
145 m_loadingThread.start();
146 }
147 sample = new QSample(url, this);
148 m_samples.insert(key: url, value: sample);
149#if QT_CONFIG(thread)
150 sample->moveToThread(thread: &m_loadingThread);
151#endif
152 } else {
153 sample = *it;
154 }
155
156 sample->addRef();
157 locker.unlock();
158
159 sample->loadIfNecessary();
160 return sample;
161}
162
163void QSampleCache::setCapacity(qint64 capacity)
164{
165 const std::lock_guard<QRecursiveMutex> locker(m_mutex);
166 if (m_capacity == capacity)
167 return;
168 qCDebug(qLcSampleCache) << "QSampleCache: capacity changes from " << m_capacity << "to " << capacity;
169 if (m_capacity > 0 && capacity <= 0) { //memory management strategy changed
170 for (QMap<QUrl, QSample*>::iterator it = m_samples.begin(); it != m_samples.end();) {
171 QSample* sample = *it;
172 if (sample->m_ref == 0) {
173 unloadSample(sample);
174 it = m_samples.erase(it);
175 } else {
176 ++it;
177 }
178 }
179 }
180
181 m_capacity = capacity;
182 refresh(usageChange: 0);
183}
184
185// Called locked
186void QSampleCache::unloadSample(QSample *sample)
187{
188 m_usage -= sample->m_soundData.size();
189 m_staleSamples.insert(value: sample);
190 sample->deleteLater();
191}
192
193// Called in both threads
194void QSampleCache::refresh(qint64 usageChange)
195{
196 const std::lock_guard<QRecursiveMutex> locker(m_mutex);
197 m_usage += usageChange;
198 if (m_capacity <= 0 || m_usage <= m_capacity)
199 return;
200
201 qint64 recoveredSize = 0;
202
203 //free unused samples to keep usage under capacity limit.
204 for (QMap<QUrl, QSample*>::iterator it = m_samples.begin(); it != m_samples.end();) {
205 QSample* sample = *it;
206 if (sample->m_ref > 0) {
207 ++it;
208 continue;
209 }
210 recoveredSize += sample->m_soundData.size();
211 unloadSample(sample);
212 it = m_samples.erase(it);
213 if (m_usage <= m_capacity)
214 return;
215 }
216
217 qCDebug(qLcSampleCache) << "QSampleCache: refresh(" << usageChange
218 << ") recovered size =" << recoveredSize
219 << "new usage =" << m_usage;
220
221 if (m_usage > m_capacity)
222 qWarning() << "QSampleCache: usage[" << m_usage << " out of limit[" << m_capacity << "]";
223}
224
225// Called in both threads
226void QSampleCache::removeUnreferencedSample(QSample *sample)
227{
228 const std::lock_guard<QRecursiveMutex> locker(m_mutex);
229 m_staleSamples.remove(value: sample);
230}
231
232// Called in loader thread (since this lives in that thread)
233// Also called from application thread after loader thread dies.
234QSample::~QSample()
235{
236 // Remove ourselves from our parent
237 m_parent->removeUnreferencedSample(sample: this);
238
239 QMutexLocker locker(&m_mutex);
240 qCDebug(qLcSampleCache) << "~QSample" << this << ": deleted [" << m_url << "]" << QThread::currentThread();
241 cleanup();
242}
243
244// Called in application thread
245void QSample::loadIfNecessary()
246{
247 QMutexLocker locker(&m_mutex);
248 if (m_state == QSample::Error || m_state == QSample::Creating) {
249 m_state = QSample::Loading;
250 QMetaObject::invokeMethod(obj: this, member: "load", c: Qt::QueuedConnection);
251 } else {
252 qobject_cast<QSampleCache*>(object: m_parent)->loadingRelease();
253 }
254}
255
256// Called in application thread
257bool QSampleCache::notifyUnreferencedSample(QSample* sample)
258{
259 if (m_loadingThread.isRunning())
260 m_loadingThread.wait();
261
262 const std::lock_guard<QRecursiveMutex> locker(m_mutex);
263
264 if (m_capacity > 0)
265 return false;
266 m_samples.remove(key: sample->m_url);
267 unloadSample(sample);
268 return true;
269}
270
271// Called in application thread
272void QSample::release()
273{
274 QMutexLocker locker(&m_mutex);
275 qCDebug(qLcSampleCache) << "Sample:: release" << this << QThread::currentThread() << m_ref;
276 if (--m_ref == 0) {
277 locker.unlock();
278 m_parent->notifyUnreferencedSample(sample: this);
279 }
280}
281
282// Called in dtor and when stream is loaded
283// must be called locked.
284void QSample::cleanup()
285{
286 qCDebug(qLcSampleCache) << "QSample: cleanup";
287 if (m_waveDecoder) {
288 m_waveDecoder->disconnect(receiver: this);
289 m_waveDecoder->deleteLater();
290 }
291 if (m_stream) {
292 m_stream->disconnect(receiver: this);
293 m_stream->deleteLater();
294 }
295
296 m_waveDecoder = nullptr;
297 m_stream = nullptr;
298}
299
300// Called in application thread
301void QSample::addRef()
302{
303 m_ref++;
304}
305
306// Called in loading thread
307void QSample::readSample()
308{
309#if QT_CONFIG(thread)
310 Q_ASSERT(QThread::currentThread()->objectName() == QLatin1String("QSampleCache::LoadingThread"));
311#endif
312 QMutexLocker m(&m_mutex);
313 qint64 read = m_waveDecoder->read(data: m_soundData.data() + m_sampleReadLength,
314 maxlen: qMin(a: m_waveDecoder->bytesAvailable(),
315 b: qint64(m_waveDecoder->size() - m_sampleReadLength)));
316 qCDebug(qLcSampleCache) << "QSample: readSample" << read;
317 if (read > 0)
318 m_sampleReadLength += read;
319 if (m_sampleReadLength < m_waveDecoder->size())
320 return;
321 Q_ASSERT(m_sampleReadLength == qint64(m_soundData.size()));
322 onReady();
323}
324
325// Called in loading thread
326void QSample::decoderReady()
327{
328#if QT_CONFIG(thread)
329 Q_ASSERT(QThread::currentThread()->objectName() == QLatin1String("QSampleCache::LoadingThread"));
330#endif
331 QMutexLocker m(&m_mutex);
332 qCDebug(qLcSampleCache) << "QSample: decoder ready";
333 m_parent->refresh(usageChange: m_waveDecoder->size());
334
335 m_soundData.resize(size: m_waveDecoder->size());
336 m_sampleReadLength = 0;
337 qint64 read = m_waveDecoder->read(data: m_soundData.data(), maxlen: m_waveDecoder->size());
338 qCDebug(qLcSampleCache) << " bytes read" << read;
339 if (read > 0)
340 m_sampleReadLength += read;
341 if (m_sampleReadLength >= m_waveDecoder->size())
342 onReady();
343}
344
345// Called in all threads
346QSample::State QSample::state() const
347{
348 QMutexLocker m(&m_mutex);
349 return m_state;
350}
351
352// Called in loading thread
353// Essentially a second ctor, doesn't need locks (?)
354void QSample::load()
355{
356#if QT_CONFIG(thread)
357 Q_ASSERT(QThread::currentThread()->objectName() == QLatin1String("QSampleCache::LoadingThread"));
358#endif
359 qCDebug(qLcSampleCache) << "QSample: load [" << m_url << "]";
360 m_stream = m_parent->networkAccessManager().get(request: QNetworkRequest(m_url));
361 connect(asender: m_stream, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), SLOT(loadingError(QNetworkReply::NetworkError)));
362 m_waveDecoder = new QWaveDecoder(m_stream);
363 connect(asender: m_waveDecoder, SIGNAL(formatKnown()), SLOT(decoderReady()));
364 connect(asender: m_waveDecoder, SIGNAL(parsingError()), SLOT(decoderError()));
365 connect(asender: m_waveDecoder, SIGNAL(readyRead()), SLOT(readSample()));
366
367 m_waveDecoder->open(mode: QIODevice::ReadOnly);
368}
369
370void QSample::loadingError(QNetworkReply::NetworkError errorCode)
371{
372#if QT_CONFIG(thread)
373 Q_ASSERT(QThread::currentThread()->objectName() == QLatin1String("QSampleCache::LoadingThread"));
374#endif
375 QMutexLocker m(&m_mutex);
376 qCDebug(qLcSampleCache) << "QSample: loading error" << errorCode;
377 cleanup();
378 m_state = QSample::Error;
379 qobject_cast<QSampleCache*>(object: m_parent)->loadingRelease();
380 emit error();
381}
382
383// Called in loading thread
384void QSample::decoderError()
385{
386#if QT_CONFIG(thread)
387 Q_ASSERT(QThread::currentThread()->objectName() == QLatin1String("QSampleCache::LoadingThread"));
388#endif
389 QMutexLocker m(&m_mutex);
390 qCDebug(qLcSampleCache) << "QSample: decoder error";
391 cleanup();
392 m_state = QSample::Error;
393 qobject_cast<QSampleCache*>(object: m_parent)->loadingRelease();
394 emit error();
395}
396
397// Called in loading thread from decoder when sample is done. Locked already.
398void QSample::onReady()
399{
400#if QT_CONFIG(thread)
401 Q_ASSERT(QThread::currentThread()->objectName() == QLatin1String("QSampleCache::LoadingThread"));
402#endif
403 m_audioFormat = m_waveDecoder->audioFormat();
404 qCDebug(qLcSampleCache) << "QSample: load ready format:" << m_audioFormat;
405 cleanup();
406 m_state = QSample::Ready;
407 qobject_cast<QSampleCache*>(object: m_parent)->loadingRelease();
408 emit ready();
409}
410
411// Called in application thread, then moved to loader thread
412QSample::QSample(const QUrl& url, QSampleCache *parent)
413 : m_parent(parent)
414 , m_stream(nullptr)
415 , m_waveDecoder(nullptr)
416 , m_url(url)
417 , m_sampleReadLength(0)
418 , m_state(Creating)
419 , m_ref(0)
420{
421}
422
423QT_END_NAMESPACE
424
425#include "moc_qsamplecache_p.cpp"
426

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