Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
avfaudiodecoder.mm
Go to the documentation of this file.
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
5
6#include <QtCore/qiodevice.h>
7#include <QtCore/qloggingcategory.h>
8#include <QtCore/qmimedatabase.h>
9#include <QtCore/qmutex.h>
10#include <QtCore/qthread.h>
11#include <QtCore/private/qcore_mac_p.h>
12
13#include "private/qcoreaudioutils_p.h"
14
15#import <AVFoundation/AVFoundation.h>
16
17QT_USE_NAMESPACE
18
19Q_STATIC_LOGGING_CATEGORY(qLcAVFAudioDecoder, "qt.multimedia.darwin.AVFAudioDecoder");
20constexpr static int MAX_BUFFERS_IN_QUEUE = 5;
21using namespace Qt::Literals;
22
23static QAudioBuffer handleNextSampleBuffer(QAudioFormat qtFormat,
24 const QCFType<CMSampleBufferRef> &sampleBuffer)
25{
26 if (!sampleBuffer)
27 return {};
28
29 // Check format
30 auto validateFormat = [&] {
31 CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);
32 if (!formatDescription)
33 return false;
34 const AudioStreamBasicDescription *const asbd =
35 CMAudioFormatDescriptionGetStreamBasicDescription(formatDescription);
36 if (!asbd)
37 return false;
38 return qtFormat == QCoreAudioUtils::toPreferredQAudioFormat(*asbd);
39 };
40
41 Q_ASSERT(validateFormat());
42
43 // Get the required size to allocate to audioBufferList
44 size_t audioBufferListSize = 0;
45 OSStatus err = CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer,
46 &audioBufferListSize,
47 NULL,
48 0,
49 NULL,
50 NULL,
51 kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment,
52 NULL);
53 if (err != noErr)
54 return {};
55
56 QCFType<CMBlockBufferRef> blockBuffer;
57 AudioBufferList* audioBufferList = (AudioBufferList*) malloc(audioBufferListSize);
58 // This ensures the buffers placed in audioBufferList are contiguous
59 err = CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer,
60 NULL,
61 audioBufferList,
62 audioBufferListSize,
63 NULL,
64 NULL,
65 kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment,
66 &blockBuffer);
67 if (err != noErr) {
68 free(audioBufferList);
69 return {};
70 }
71
72 QByteArray abuf;
73 for (UInt32 i = 0; i < audioBufferList->mNumberBuffers; i++)
74 {
75 AudioBuffer audioBuffer = audioBufferList->mBuffers[i];
76 abuf.push_back(QByteArray((const char*)audioBuffer.mData, audioBuffer.mDataByteSize));
77 }
78
79 free(audioBufferList);
80
81 CMTime sampleStartTime = (CMSampleBufferGetPresentationTimeStamp(sampleBuffer));
82 float sampleStartTimeSecs = CMTimeGetSeconds(sampleStartTime);
83
84 return QAudioBuffer(abuf, qtFormat, qint64(sampleStartTimeSecs * 1000000));
85}
86
87@interface AVFResourceReaderDelegate : NSObject <AVAssetResourceLoaderDelegate> {
88 AVFAudioDecoder *m_decoder;
89 QMutex m_mutex;
90}
91
92- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader
93 shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest;
94
95- (void)clearDecoder;
96
97@end
98
99@implementation AVFResourceReaderDelegate
100
101- (id)initWithDecoder:(AVFAudioDecoder *)decoder
102{
103 if (!(self = [super init]))
104 return nil;
105
106 m_decoder = decoder;
107
108 return self;
109}
110
111-(BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader
112 shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
113{
114 Q_UNUSED(resourceLoader);
115
116 if (![loadingRequest.request.URL.scheme isEqualToString:@"iodevice"])
117 return NO;
118
119 std::lock_guard locker(m_mutex);
120
121 QIODevice *device = m_decoder ? m_decoder->sourceDevice() : nullptr;
122 if (!device)
123 return NO;
124
125 device->seek(loadingRequest.dataRequest.requestedOffset);
126 if (loadingRequest.contentInformationRequest) {
127 loadingRequest.contentInformationRequest.contentLength = device->size();
128 loadingRequest.contentInformationRequest.byteRangeAccessSupported = YES;
129 }
130
131 if (loadingRequest.dataRequest) {
132 NSInteger requestedLength = loadingRequest.dataRequest.requestedLength;
133 int maxBytes = qMin(32 * 1024, int(requestedLength));
134 QByteArray buffer;
135 buffer.resize(maxBytes);
136 NSInteger submitted = 0;
137 while (submitted < requestedLength) {
138 qint64 len = device->read(buffer.data(), maxBytes);
139 if (len < 1)
140 break;
141
142 [loadingRequest.dataRequest respondWithData:[NSData dataWithBytes:buffer.constData()
143 length:len]];
144 submitted += len;
145 }
146
147 // Finish loading even if not all bytes submitted.
148 [loadingRequest finishLoading];
149 }
150
151 return YES;
152}
153
154- (void)clearDecoder
155{
156 std::lock_guard locker(m_mutex);
157 m_decoder = nullptr;
158}
159
160@end
161
162namespace {
163
164NSDictionary *av_audio_settings_for_format(const QAudioFormat &format)
165{
166 float sampleRate = format.sampleRate();
167 int nChannels = format.channelCount();
168 int sampleSize = format.bytesPerSample() * 8;
169 BOOL isFloat = format.sampleFormat() == QAudioFormat::Float;
170
171 NSDictionary *audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
172 [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
173 [NSNumber numberWithFloat:sampleRate], AVSampleRateKey,
174 [NSNumber numberWithInt:nChannels], AVNumberOfChannelsKey,
175 [NSNumber numberWithInt:sampleSize], AVLinearPCMBitDepthKey,
176 [NSNumber numberWithBool:isFloat], AVLinearPCMIsFloatKey,
177 [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
178 [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
179 nil];
180
181 return audioSettings;
182}
183
184QAudioFormat qt_format_for_audio_track(AVAssetTrack *track)
185{
186 CMFormatDescriptionRef desc = (__bridge CMFormatDescriptionRef)track.formatDescriptions[0];
187 const AudioStreamBasicDescription* const asbd =
188 CMAudioFormatDescriptionGetStreamBasicDescription(desc);
189 return QCoreAudioUtils::toPreferredQAudioFormat(*asbd);
190}
191
192} // namespace
193
194struct AVFAudioDecoder::DecodingContext
195{
198
200 {
201 if (m_reader) {
202 [m_reader cancelReading];
203 [m_reader release];
204 }
205
206 [m_readerOutput release];
207 }
208};
209
210AVFAudioDecoder::AVFAudioDecoder(QAudioDecoder *parent)
211 : QPlatformAudioDecoder(parent)
212{
213 m_readingQueue = dispatch_queue_create("reader_queue", DISPATCH_QUEUE_SERIAL);
214 m_decodingQueue = dispatch_queue_create("decoder_queue", DISPATCH_QUEUE_SERIAL);
215
216 m_readerDelegate = [[AVFResourceReaderDelegate alloc] initWithDecoder:this];
217 Q_ASSERT(m_readerDelegate);
218}
219
220AVFAudioDecoder::~AVFAudioDecoder()
221{
222 stop();
223
224 [m_readerDelegate clearDecoder];
225 [m_readerDelegate release];
226
227 [m_asset release];
228
229 dispatch_release(m_readingQueue);
230 dispatch_release(m_decodingQueue);
231}
232
233QUrl AVFAudioDecoder::source() const
234{
235 return m_source;
236}
237
238void AVFAudioDecoder::setSource(const QUrl &fileName)
239{
240 if (!m_device && m_source == fileName)
241 return;
242
243 stop();
244 m_device = nullptr;
245 [m_asset release];
246 m_asset = nil;
247
248 m_source = fileName;
249
250 if (!m_source.isEmpty()) {
251 NSURL *nsURL = m_source.toNSURL();
252 m_asset = [[AVURLAsset alloc] initWithURL:nsURL options:nil];
253 }
254
255 sourceChanged();
256}
257
258QIODevice *AVFAudioDecoder::sourceDevice() const
259{
260 return m_device;
261}
262
263void AVFAudioDecoder::setSourceDevice(QIODevice *device)
264{
265 if (m_device == device && m_source.isEmpty())
266 return;
267
268 stop();
269 m_source.clear();
270 [m_asset release];
271 m_asset = nil;
272
273 m_device = device;
274
275 if (m_device) {
276 const QString ext = QMimeDatabase().mimeTypeForData(m_device).preferredSuffix();
277 const QString url = u"iodevice:///iodevice."_s + ext;
278 NSString *_Nonnull urlString = url.toNSString();
279 NSURL *nsURL = [NSURL URLWithString:urlString];
280
281 if (nsURL == nil) {
282 processInvalidMedia(QAudioDecoder::FormatError,
283 tr("Failed to create URL for the device"));
284 return;
285 }
286 m_asset = [[AVURLAsset alloc] initWithURL:nsURL options:nil];
287
288 // use decoding queue instead of reading queue in order to fix random stucks.
289 // Anyway, decoding queue is empty in the moment.
290 [m_asset.resourceLoader setDelegate:m_readerDelegate queue:m_decodingQueue];
291 }
292
293 sourceChanged();
294}
295
296void AVFAudioDecoder::start()
297{
298 if (m_decodingContext) {
299 qCDebug(qLcAVFAudioDecoder()) << "AVFAudioDecoder has been already started";
300 return;
301 }
302
303 positionChanged(-1);
304
305 if (m_device && (!m_device->isOpen() || !m_device->isReadable())) {
306 processInvalidMedia(QAudioDecoder::ResourceError, tr("Unable to read from specified device"));
307 return;
308 }
309
310 m_decodingContext = std::make_shared<DecodingContext>();
311 std::weak_ptr<DecodingContext> weakContext(m_decodingContext);
312
313 auto handleLoadingResult = [=, this]() {
314 NSError *error = nil;
315 AVKeyValueStatus status = [m_asset statusOfValueForKey:@"tracks" error:&error];
316
317 if (status == AVKeyValueStatusFailed) {
318 if (error.domain == NSURLErrorDomain)
319 processInvalidMedia(QAudioDecoder::ResourceError,
320 QString::fromNSString(error.localizedDescription));
321 else
322 processInvalidMedia(QAudioDecoder::FormatError,
323 tr("Could not load media source's tracks"));
324 } else if (status != AVKeyValueStatusLoaded) {
325 qWarning() << "Unexpected AVKeyValueStatus:" << status;
326 stop();
327 }
328 else {
329 initAssetReader();
330 }
331 };
332
333 [m_asset loadValuesAsynchronouslyForKeys:@[ @"tracks" ]
334 completionHandler:[=, this]() {
335 invokeWithDecodingContext(weakContext, handleLoadingResult);
336 }];
337}
338
339void AVFAudioDecoder::decBuffersCounter(uint val)
340{
341 if (val) {
342 QMutexLocker locker(&m_buffersCounterMutex);
343 m_buffersCounter -= val;
344 }
345
346 Q_ASSERT(m_buffersCounter >= 0);
347
348 m_buffersCounterCondition.wakeAll();
349}
350
351void AVFAudioDecoder::stop()
352{
353 qCDebug(qLcAVFAudioDecoder()) << "stop decoding";
354
355 m_decodingContext.reset();
356 decBuffersCounter(m_cachedBuffers.size());
357 m_cachedBuffers.clear();
358
359 bufferAvailableChanged(false);
360 positionChanged(-1);
361 durationChanged(-1);
362
363 onFinished();
364}
365
366QAudioFormat AVFAudioDecoder::audioFormat() const
367{
368 return m_format;
369}
370
371void AVFAudioDecoder::setAudioFormat(const QAudioFormat &format)
372{
373 if (m_format != format) {
374 m_format = format;
375 formatChanged(m_format);
376 }
377}
378
379QAudioBuffer AVFAudioDecoder::read()
380{
381 if (m_cachedBuffers.empty())
382 return QAudioBuffer();
383
384 Q_ASSERT(m_cachedBuffers.size() > 0);
385 QAudioBuffer buffer = m_cachedBuffers.dequeue();
386 decBuffersCounter(1);
387
388 positionChanged(buffer.startTime() / 1000);
389 bufferAvailableChanged(!m_cachedBuffers.empty());
390 return buffer;
391}
392
393void AVFAudioDecoder::processInvalidMedia(QAudioDecoder::Error errorCode,
394 const QString &errorString)
395{
396 qCDebug(qLcAVFAudioDecoder()) << "Invalid media. Error code:" << errorCode
397 << "Description:" << errorString;
398
399 Q_ASSERT(QThread::currentThread() == thread());
400
401 error(int(errorCode), errorString);
402
403 // TODO: may be check if decodingCondext was changed by
404 // user's action (restart) from the emitted error.
405 // We should handle it somehow (don't run stop, print warning or etc...)
406
407 stop();
408}
409
410void AVFAudioDecoder::onFinished()
411{
412 m_decodingContext.reset();
413
414 if (isDecoding())
415 finished();
416}
417
418void AVFAudioDecoder::initAssetReaderImpl(AVAssetTrack *track, NSError *error)
419{
420 Q_ASSERT(track != nullptr);
421
422 if (error) {
423 processInvalidMedia(QAudioDecoder::ResourceError, QString::fromNSString(error.localizedDescription));
424 return;
425 }
426
427 QAudioFormat format = m_format.isValid() ? m_format : qt_format_for_audio_track(track);
428 if (!format.isValid()) {
429 processInvalidMedia(QAudioDecoder::FormatError, tr("Unsupported source format"));
430 return;
431 }
432
433 durationChanged(CMTimeGetSeconds(track.timeRange.duration) * 1000);
434
435 NSDictionary *audioSettings = av_audio_settings_for_format(format);
436
437 AVAssetReaderTrackOutput *readerOutput =
438 [[AVAssetReaderTrackOutput alloc] initWithTrack:track outputSettings:audioSettings];
439 AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:m_asset error:&error];
440 auto cleanup = qScopeGuard([&] {
441 [readerOutput release];
442 [reader release];
443 });
444
445 if (error) {
446 processInvalidMedia(QAudioDecoder::ResourceError, QString::fromNSString(error.localizedDescription));
447 return;
448 }
449 if (![reader canAddOutput:readerOutput]) {
450 processInvalidMedia(QAudioDecoder::ResourceError, tr("Failed to add asset reader output"));
451 return;
452 }
453
454 [reader addOutput:readerOutput];
455
456 Q_ASSERT(m_decodingContext);
457 cleanup.dismiss();
458
459 m_decodingContext->m_reader = reader;
460 m_decodingContext->m_readerOutput = readerOutput;
461
463}
464
465void AVFAudioDecoder::initAssetReader()
466{
467 qCDebug(qLcAVFAudioDecoder()) << "Init asset reader";
468
469 Q_ASSERT(m_asset);
470 Q_ASSERT(QThread::currentThread() == thread());
471
472#if defined(Q_OS_VISIONOS)
473 [m_asset loadTracksWithMediaType:AVMediaTypeAudio completionHandler:[=](NSArray<AVAssetTrack *> *tracks, NSError *error) {
474 if (tracks && tracks.count > 0) {
475 if (AVAssetTrack *track = [tracks objectAtIndex:0])
476 QMetaObject::invokeMethod(this, &AVFAudioDecoder::initAssetReaderImpl, Qt::QueuedConnection, track, error);
477 }
478 }];
479#else
480 NSArray<AVAssetTrack *> *tracks = [m_asset tracksWithMediaType:AVMediaTypeAudio];
481 if (tracks && tracks.count > 0) {
482 if (AVAssetTrack *track = [tracks objectAtIndex:0])
483 initAssetReaderImpl(track, nullptr /*error*/);
484 }
485#endif
486
487}
488
489void AVFAudioDecoder::startReading(QAudioFormat format)
490{
491 Q_ASSERT(m_decodingContext);
492 Q_ASSERT(m_decodingContext->m_reader);
493 Q_ASSERT(QThread::currentThread() == thread());
494
495 // Prepares the receiver for obtaining sample buffers from the asset.
496 if (![m_decodingContext->m_reader startReading]) {
497 processInvalidMedia(QAudioDecoder::ResourceError, tr("Could not start reading"));
498 return;
499 }
500
501 setIsDecoding(true);
502
503 std::weak_ptr<DecodingContext> weakContext = m_decodingContext;
504
505 // Since copyNextSampleBuffer is synchronous, submit it to an async dispatch queue
506 // to run in a separate thread. Call the handleNextSampleBuffer "callback" on another
507 // thread when new audio sample is read.
508 auto copyNextSampleBuffer = [=, this]() {
509 auto decodingContext = weakContext.lock();
510 if (!decodingContext)
511 return false;
512
513 QCFType<CMSampleBufferRef> sampleBuffer{
514 [decodingContext->m_readerOutput copyNextSampleBuffer],
515 };
516 if (!sampleBuffer)
517 return false;
518
519 dispatch_async(m_decodingQueue, [=, this]() {
520 if (!weakContext.expired() && CMSampleBufferDataIsReady(sampleBuffer)) {
521 auto audioBuffer = handleNextSampleBuffer(format, sampleBuffer);
522
523 if (audioBuffer.isValid())
524 invokeWithDecodingContext(weakContext, [=, this]() {
525 handleNewAudioBuffer(audioBuffer);
526 });
527 }
528 });
529
530 return true;
531 };
532
533 dispatch_async(m_readingQueue, [=, this]() {
534 qCDebug(qLcAVFAudioDecoder()) << "start reading thread";
535
536 do {
537 // Note, waiting here doesn't ensure strong contol of the counter.
538 // However, it doesn't affect the logic: the reading flow works fine
539 // even if the counter is time-to-time more than max value
540 waitUntilBuffersCounterLessMax();
541 } while (copyNextSampleBuffer());
542
543 // TODO: check m_reader.status == AVAssetReaderStatusFailed
544 invokeWithDecodingContext(weakContext, [this]() { onFinished(); });
545 });
546}
547
548void AVFAudioDecoder::waitUntilBuffersCounterLessMax()
549{
550 if (m_buffersCounter >= MAX_BUFFERS_IN_QUEUE) {
551 // the check avoids extra mutex lock.
552
553 QMutexLocker locker(&m_buffersCounterMutex);
554
555 while (m_buffersCounter >= MAX_BUFFERS_IN_QUEUE)
556 m_buffersCounterCondition.wait(&m_buffersCounterMutex);
557 }
558}
559
560void AVFAudioDecoder::handleNewAudioBuffer(QAudioBuffer buffer)
561{
562 m_cachedBuffers.enqueue(std::move(buffer));
563 ++m_buffersCounter;
564
565 Q_ASSERT(m_cachedBuffers.size() == m_buffersCounter);
566
567 bufferAvailableChanged(true);
568 bufferReady();
569}
570
571/*
572 * The method calls the passed functor in the thread of AVFAudioDecoder and guarantees that
573 * the passed decoding context is not expired. In other words, it helps avoiding all callbacks
574 * after stopping of the decoder.
575 */
576template<typename F>
577void AVFAudioDecoder::invokeWithDecodingContext(std::weak_ptr<DecodingContext> weakContext, F &&f)
578{
579 if (!weakContext.expired())
580 QMetaObject::invokeMethod(
581 this, [this, f = std::forward<F>(f), weakContext = std::move(weakContext)]() {
582 // strong check: compare with actual decoding context.
583 // Otherwise, the context can be temporary locked by one of dispatch queues.
584 if (auto context = weakContext.lock(); context && context == m_decodingContext)
585 f();
586 });
587}
588
589#include "moc_avfaudiodecoder_p.cpp"
static constexpr int MAX_BUFFERS_IN_QUEUE
static QAudioBuffer handleNextSampleBuffer(QAudioFormat qtFormat, const QCFType< CMSampleBufferRef > &sampleBuffer)
AVAssetReaderTrackOutput * m_readerOutput
QImage::Format format
Definition qimage_p.h:52