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 | |
14 | static Q_LOGGING_CATEGORY(qLcSampleCache, "qt.multimedia.samplecache" ) |
15 | |
16 | #include <mutex> |
17 | |
18 | QT_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 | |
66 | QSampleCache::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 | |
76 | QNetworkAccessManager& QSampleCache::networkAccessManager() |
77 | { |
78 | if (!m_networkAccessManager) |
79 | m_networkAccessManager = new QNetworkAccessManager(); |
80 | return *m_networkAccessManager; |
81 | } |
82 | |
83 | QSampleCache::~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 | |
103 | void 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 | |
118 | bool QSampleCache::isLoading() const |
119 | { |
120 | return m_loadingThread.isRunning(); |
121 | } |
122 | |
123 | bool 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 | |
129 | QSample* 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 | if (sample->state() == QSample::Error && needsThreadStart) { |
155 | m_loadingThread.wait(); |
156 | m_loadingThread.start(); |
157 | } |
158 | } |
159 | |
160 | sample->addRef(); |
161 | locker.unlock(); |
162 | |
163 | sample->loadIfNecessary(); |
164 | return sample; |
165 | } |
166 | |
167 | void QSampleCache::setCapacity(qint64 capacity) |
168 | { |
169 | const std::lock_guard<QRecursiveMutex> locker(m_mutex); |
170 | if (m_capacity == capacity) |
171 | return; |
172 | qCDebug(qLcSampleCache) << "QSampleCache: capacity changes from " << m_capacity << "to " << capacity; |
173 | if (m_capacity > 0 && capacity <= 0) { //memory management strategy changed |
174 | for (QMap<QUrl, QSample*>::iterator it = m_samples.begin(); it != m_samples.end();) { |
175 | QSample* sample = *it; |
176 | if (sample->m_ref == 0) { |
177 | unloadSample(sample); |
178 | it = m_samples.erase(it); |
179 | } else { |
180 | ++it; |
181 | } |
182 | } |
183 | } |
184 | |
185 | m_capacity = capacity; |
186 | refresh(usageChange: 0); |
187 | } |
188 | |
189 | // Called locked |
190 | void QSampleCache::unloadSample(QSample *sample) |
191 | { |
192 | m_usage -= sample->m_soundData.size(); |
193 | m_staleSamples.insert(value: sample); |
194 | sample->deleteLater(); |
195 | } |
196 | |
197 | // Called in both threads |
198 | void QSampleCache::refresh(qint64 usageChange) |
199 | { |
200 | const std::lock_guard<QRecursiveMutex> locker(m_mutex); |
201 | m_usage += usageChange; |
202 | if (m_capacity <= 0 || m_usage <= m_capacity) |
203 | return; |
204 | |
205 | qint64 recoveredSize = 0; |
206 | |
207 | //free unused samples to keep usage under capacity limit. |
208 | for (QMap<QUrl, QSample*>::iterator it = m_samples.begin(); it != m_samples.end();) { |
209 | QSample* sample = *it; |
210 | if (sample->m_ref > 0) { |
211 | ++it; |
212 | continue; |
213 | } |
214 | recoveredSize += sample->m_soundData.size(); |
215 | unloadSample(sample); |
216 | it = m_samples.erase(it); |
217 | if (m_usage <= m_capacity) |
218 | return; |
219 | } |
220 | |
221 | qCDebug(qLcSampleCache) << "QSampleCache: refresh(" << usageChange |
222 | << ") recovered size =" << recoveredSize |
223 | << "new usage =" << m_usage; |
224 | |
225 | if (m_usage > m_capacity) |
226 | qWarning() << "QSampleCache: usage" << m_usage << "out of limit" << m_capacity; |
227 | } |
228 | |
229 | // Called in both threads |
230 | void QSampleCache::removeUnreferencedSample(QSample *sample) |
231 | { |
232 | const std::lock_guard<QRecursiveMutex> locker(m_mutex); |
233 | m_staleSamples.remove(value: sample); |
234 | } |
235 | |
236 | // Called in loader thread (since this lives in that thread) |
237 | // Also called from application thread after loader thread dies. |
238 | QSample::~QSample() |
239 | { |
240 | // Remove ourselves from our parent |
241 | m_parent->removeUnreferencedSample(sample: this); |
242 | |
243 | QMutexLocker locker(&m_mutex); |
244 | qCDebug(qLcSampleCache) << "~QSample" << this << ": deleted [" << m_url << "]" << QThread::currentThread(); |
245 | cleanup(); |
246 | } |
247 | |
248 | // Called in application thread |
249 | void QSample::loadIfNecessary() |
250 | { |
251 | QMutexLocker locker(&m_mutex); |
252 | if (m_state == QSample::Error || m_state == QSample::Creating) { |
253 | m_state = QSample::Loading; |
254 | QMetaObject::invokeMethod(object: this, function: &QSample::load, type: Qt::QueuedConnection); |
255 | } else { |
256 | m_parent->loadingRelease(); |
257 | } |
258 | } |
259 | |
260 | // Called in application thread |
261 | bool QSampleCache::notifyUnreferencedSample(QSample* sample) |
262 | { |
263 | if (m_loadingThread.isRunning()) |
264 | m_loadingThread.wait(); |
265 | |
266 | const std::lock_guard<QRecursiveMutex> locker(m_mutex); |
267 | |
268 | if (m_capacity > 0) |
269 | return false; |
270 | m_samples.remove(key: sample->m_url); |
271 | unloadSample(sample); |
272 | return true; |
273 | } |
274 | |
275 | // Called in application thread |
276 | void QSample::release() |
277 | { |
278 | QMutexLocker locker(&m_mutex); |
279 | qCDebug(qLcSampleCache) << "Sample:: release" << this << QThread::currentThread() << m_ref; |
280 | if (--m_ref == 0) { |
281 | locker.unlock(); |
282 | m_parent->notifyUnreferencedSample(sample: this); |
283 | } |
284 | } |
285 | |
286 | // Called in dtor and when stream is loaded |
287 | // must be called locked. |
288 | void QSample::cleanup() |
289 | { |
290 | qCDebug(qLcSampleCache) << "QSample: cleanup" ; |
291 | if (m_waveDecoder) { |
292 | m_waveDecoder->disconnect(receiver: this); |
293 | m_waveDecoder->deleteLater(); |
294 | } |
295 | if (m_stream) { |
296 | m_stream->disconnect(receiver: this); |
297 | m_stream->deleteLater(); |
298 | } |
299 | |
300 | m_waveDecoder = nullptr; |
301 | m_stream = nullptr; |
302 | } |
303 | |
304 | // Called in application thread |
305 | void QSample::addRef() |
306 | { |
307 | m_ref++; |
308 | } |
309 | |
310 | // Called in loading thread |
311 | void QSample::readSample() |
312 | { |
313 | #if QT_CONFIG(thread) |
314 | Q_ASSERT(QThread::currentThread()->objectName() == QLatin1String("QSampleCache::LoadingThread" )); |
315 | #endif |
316 | QMutexLocker m(&m_mutex); |
317 | qint64 read = m_waveDecoder->read(data: m_soundData.data() + m_sampleReadLength, |
318 | maxlen: qMin(a: m_waveDecoder->bytesAvailable(), |
319 | b: qint64(m_waveDecoder->size() - m_sampleReadLength))); |
320 | qCDebug(qLcSampleCache) << "QSample: readSample" << read; |
321 | if (read > 0) |
322 | m_sampleReadLength += read; |
323 | if (m_sampleReadLength < m_waveDecoder->size()) |
324 | return; |
325 | Q_ASSERT(m_sampleReadLength == qint64(m_soundData.size())); |
326 | onReady(); |
327 | } |
328 | |
329 | // Called in loading thread |
330 | void QSample::decoderReady() |
331 | { |
332 | #if QT_CONFIG(thread) |
333 | Q_ASSERT(QThread::currentThread()->objectName() == QLatin1String("QSampleCache::LoadingThread" )); |
334 | #endif |
335 | QMutexLocker m(&m_mutex); |
336 | qCDebug(qLcSampleCache) << "QSample: decoder ready" ; |
337 | m_parent->refresh(usageChange: m_waveDecoder->size()); |
338 | |
339 | m_soundData.resize(size: m_waveDecoder->size()); |
340 | m_sampleReadLength = 0; |
341 | qint64 read = m_waveDecoder->read(data: m_soundData.data(), maxlen: m_waveDecoder->size()); |
342 | qCDebug(qLcSampleCache) << " bytes read" << read; |
343 | if (read > 0) |
344 | m_sampleReadLength += read; |
345 | if (m_sampleReadLength >= m_waveDecoder->size()) |
346 | onReady(); |
347 | } |
348 | |
349 | // Called in all threads |
350 | QSample::State QSample::state() const |
351 | { |
352 | QMutexLocker m(&m_mutex); |
353 | return m_state; |
354 | } |
355 | |
356 | // Called in loading thread |
357 | // Essentially a second ctor, doesn't need locks (?) |
358 | void QSample::load() |
359 | { |
360 | #if QT_CONFIG(thread) |
361 | Q_ASSERT(QThread::currentThread()->objectName() == QLatin1String("QSampleCache::LoadingThread" )); |
362 | #endif |
363 | qCDebug(qLcSampleCache) << "QSample: load [" << m_url << "]" ; |
364 | |
365 | if (m_url.scheme().isEmpty()) { |
366 | // exit early, to avoid QNetworkAccessManager trying to construct a default ssl |
367 | // configuration, which tends to cause timeouts on CI on macos. |
368 | // catch this case and exit early. |
369 | emit loadingError(QNetworkReply::NetworkError::ProtocolUnknownError); |
370 | return; |
371 | } |
372 | |
373 | QNetworkReply *reply = m_parent->networkAccessManager().get(request: QNetworkRequest(m_url)); |
374 | m_stream = reply; |
375 | connect(sender: reply, signal: &QNetworkReply::errorOccurred, context: this, slot: &QSample::loadingError); |
376 | m_waveDecoder = new QWaveDecoder(m_stream); |
377 | connect(sender: m_waveDecoder, signal: &QWaveDecoder::formatKnown, context: this, slot: &QSample::decoderReady); |
378 | connect(sender: m_waveDecoder, signal: &QWaveDecoder::parsingError, context: this, slot: &QSample::decoderError); |
379 | connect(sender: m_waveDecoder, signal: &QIODevice::readyRead, context: this, slot: &QSample::readSample); |
380 | |
381 | m_waveDecoder->open(mode: QIODevice::ReadOnly); |
382 | } |
383 | |
384 | void QSample::loadingError(QNetworkReply::NetworkError errorCode) |
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: loading error" << errorCode; |
391 | cleanup(); |
392 | m_state = QSample::Error; |
393 | m_parent->loadingRelease(); |
394 | emit error(self: this); |
395 | } |
396 | |
397 | // Called in loading thread |
398 | void QSample::decoderError() |
399 | { |
400 | #if QT_CONFIG(thread) |
401 | Q_ASSERT(QThread::currentThread()->objectName() == QLatin1String("QSampleCache::LoadingThread" )); |
402 | #endif |
403 | QMutexLocker m(&m_mutex); |
404 | qCDebug(qLcSampleCache) << "QSample: decoder error" ; |
405 | cleanup(); |
406 | m_state = QSample::Error; |
407 | m_parent->loadingRelease(); |
408 | emit error(self: this); |
409 | } |
410 | |
411 | // Called in loading thread from decoder when sample is done. Locked already. |
412 | void QSample::onReady() |
413 | { |
414 | #if QT_CONFIG(thread) |
415 | Q_ASSERT(QThread::currentThread()->objectName() == QLatin1String("QSampleCache::LoadingThread" )); |
416 | #endif |
417 | m_audioFormat = m_waveDecoder->audioFormat(); |
418 | qCDebug(qLcSampleCache) << "QSample: load ready format:" << m_audioFormat; |
419 | cleanup(); |
420 | m_state = QSample::Ready; |
421 | m_parent->loadingRelease(); |
422 | emit ready(self: this); |
423 | } |
424 | |
425 | // Called in application thread, then moved to loader thread |
426 | QSample::QSample(const QUrl& url, QSampleCache *parent) |
427 | : m_parent(parent) |
428 | , m_stream(nullptr) |
429 | , m_waveDecoder(nullptr) |
430 | , m_url(url) |
431 | , m_sampleReadLength(0) |
432 | , m_state(Creating) |
433 | , m_ref(0) |
434 | { |
435 | } |
436 | |
437 | QT_END_NAMESPACE |
438 | |
439 | #include "moc_qsamplecache_p.cpp" |
440 | |