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 length:len]];
143 submitted += len;
144 }
145
146 // Finish loading even if not all bytes submitted.
147 [loadingRequest finishLoading];
148 }
149
150 return YES;
151}
152
153- (void)clearDecoder
154{
155 std::lock_guard locker(m_mutex);
156 m_decoder = nullptr;
157}
158
159@end
160
161namespace {
162
163NSDictionary *av_audio_settings_for_format(const QAudioFormat &format)
164{
165 float sampleRate = format.sampleRate();
166 int nChannels = format.channelCount();
167 int sampleSize = format.bytesPerSample() * 8;
168 BOOL isFloat = format.sampleFormat() == QAudioFormat::Float;
169
170 NSDictionary *audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
171 [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
172 [NSNumber numberWithFloat:sampleRate], AVSampleRateKey,
173 [NSNumber numberWithInt:nChannels], AVNumberOfChannelsKey,
174 [NSNumber numberWithInt:sampleSize], AVLinearPCMBitDepthKey,
175 [NSNumber numberWithBool:isFloat], AVLinearPCMIsFloatKey,
176 [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
177 [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
178 nil];
179
180 return audioSettings;
181}
182
183QAudioFormat qt_format_for_audio_track(AVAssetTrack *track)
184{
185 CMFormatDescriptionRef desc = (__bridge CMFormatDescriptionRef)track.formatDescriptions[0];
186 const AudioStreamBasicDescription* const asbd =
187 CMAudioFormatDescriptionGetStreamBasicDescription(desc);
188 return QCoreAudioUtils::toPreferredQAudioFormat(*asbd);
189}
190
191} // namespace
192
193struct AVFAudioDecoder::DecodingContext
194{
197
199 {
200 if (m_reader) {
201 [m_reader cancelReading];
202 [m_reader release];
203 }
204
205 [m_readerOutput release];
206 }
207};
208
209AVFAudioDecoder::AVFAudioDecoder(QAudioDecoder *parent)
210 : QPlatformAudioDecoder(parent)
211{
212 m_readingQueue = dispatch_queue_create("reader_queue", DISPATCH_QUEUE_SERIAL);
213 m_decodingQueue = dispatch_queue_create("decoder_queue", DISPATCH_QUEUE_SERIAL);
214
215 m_readerDelegate = [[AVFResourceReaderDelegate alloc] initWithDecoder:this];
216 Q_ASSERT(m_readerDelegate);
217}
218
219AVFAudioDecoder::~AVFAudioDecoder()
220{
221 stop();
222
223 [m_readerDelegate clearDecoder];
224 [m_readerDelegate release];
225
226 [m_asset release];
227
228 dispatch_release(m_readingQueue);
229 dispatch_release(m_decodingQueue);
230}
231
232QUrl AVFAudioDecoder::source() const
233{
234 return m_source;
235}
236
237void AVFAudioDecoder::setSource(const QUrl &fileName)
238{
239 if (!m_device && m_source == fileName)
240 return;
241
242 stop();
243 m_device = nullptr;
244 [m_asset release];
245 m_asset = nil;
246
247 m_source = fileName;
248
249 if (!m_source.isEmpty()) {
250 NSURL *nsURL = m_source.toNSURL();
251 m_asset = [[AVURLAsset alloc] initWithURL:nsURL options:nil];
252 }
253
254 sourceChanged();
255}
256
257QIODevice *AVFAudioDecoder::sourceDevice() const
258{
259 return m_device;
260}
261
262void AVFAudioDecoder::setSourceDevice(QIODevice *device)
263{
264 if (m_device == device && m_source.isEmpty())
265 return;
266
267 stop();
268 m_source.clear();
269 [m_asset release];
270 m_asset = nil;
271
272 m_device = device;
273
274 if (m_device) {
275 const QString ext = QMimeDatabase().mimeTypeForData(m_device).preferredSuffix();
276 const QString url = u"iodevice:///iodevice."_s + ext;
277 NSString *_Nonnull urlString = url.toNSString();
278 NSURL *nsURL = [NSURL URLWithString:urlString];
279
280 if (nsURL == nil) {
281 processInvalidMedia(QAudioDecoder::FormatError,
282 tr("Failed to create URL for the device"));
283 return;
284 }
285 m_asset = [[AVURLAsset alloc] initWithURL:nsURL options:nil];
286
287 // use decoding queue instead of reading queue in order to fix random stucks.
288 // Anyway, decoding queue is empty in the moment.
289 [m_asset.resourceLoader setDelegate:m_readerDelegate queue:m_decodingQueue];
290 }
291
292 sourceChanged();
293}
294
295void AVFAudioDecoder::start()
296{
297 if (m_decodingContext) {
298 qCDebug(qLcAVFAudioDecoder()) << "AVFAudioDecoder has been already started";
299 return;
300 }
301
302 positionChanged(-1);
303
304 if (m_device && (!m_device->isOpen() || !m_device->isReadable())) {
305 processInvalidMedia(QAudioDecoder::ResourceError, tr("Unable to read from specified device"));
306 return;
307 }
308
309 m_decodingContext = std::make_shared<DecodingContext>();
310 std::weak_ptr<DecodingContext> weakContext(m_decodingContext);
311
312 auto handleLoadingResult = [=, this]() {
313 NSError *error = nil;
314 AVKeyValueStatus status = [m_asset statusOfValueForKey:@"tracks" error:&error];
315
316 if (status == AVKeyValueStatusFailed) {
317 if (error.domain == NSURLErrorDomain)
318 processInvalidMedia(QAudioDecoder::ResourceError,
319 QString::fromNSString(error.localizedDescription));
320 else
321 processInvalidMedia(QAudioDecoder::FormatError,
322 tr("Could not load media source's tracks"));
323 } else if (status != AVKeyValueStatusLoaded) {
324 qWarning() << "Unexpected AVKeyValueStatus:" << status;
325 stop();
326 }
327 else {
328 initAssetReader();
329 }
330 };
331
332 [m_asset loadValuesAsynchronouslyForKeys:@[ @"tracks" ]
333 completionHandler:[=, this]() {
334 invokeWithDecodingContext(weakContext, handleLoadingResult);
335 }];
336}
337
338void AVFAudioDecoder::decBuffersCounter(uint val)
339{
340 if (val) {
341 QMutexLocker locker(&m_buffersCounterMutex);
342 m_buffersCounter -= val;
343 }
344
345 Q_ASSERT(m_buffersCounter >= 0);
346
347 m_buffersCounterCondition.wakeAll();
348}
349
350void AVFAudioDecoder::stop()
351{
352 qCDebug(qLcAVFAudioDecoder()) << "stop decoding";
353
354 m_decodingContext.reset();
355 decBuffersCounter(m_cachedBuffers.size());
356 m_cachedBuffers.clear();
357
358 bufferAvailableChanged(false);
359 positionChanged(-1);
360 durationChanged(-1);
361
362 onFinished();
363}
364
365QAudioFormat AVFAudioDecoder::audioFormat() const
366{
367 return m_format;
368}
369
370void AVFAudioDecoder::setAudioFormat(const QAudioFormat &format)
371{
372 if (m_format != format) {
373 m_format = format;
374 formatChanged(m_format);
375 }
376}
377
378QAudioBuffer AVFAudioDecoder::read()
379{
380 if (m_cachedBuffers.empty())
381 return QAudioBuffer();
382
383 Q_ASSERT(m_cachedBuffers.size() > 0);
384 QAudioBuffer buffer = m_cachedBuffers.dequeue();
385 decBuffersCounter(1);
386
387 positionChanged(buffer.startTime() / 1000);
388 bufferAvailableChanged(!m_cachedBuffers.empty());
389 return buffer;
390}
391
392void AVFAudioDecoder::processInvalidMedia(QAudioDecoder::Error errorCode,
393 const QString &errorString)
394{
395 qCDebug(qLcAVFAudioDecoder()) << "Invalid media. Error code:" << errorCode
396 << "Description:" << errorString;
397
398 Q_ASSERT(QThread::currentThread() == thread());
399
400 error(int(errorCode), errorString);
401
402 // TODO: may be check if decodingCondext was changed by
403 // user's action (restart) from the emitted error.
404 // We should handle it somehow (don't run stop, print warning or etc...)
405
406 stop();
407}
408
409void AVFAudioDecoder::onFinished()
410{
411 m_decodingContext.reset();
412
413 if (isDecoding())
414 finished();
415}
416
417void AVFAudioDecoder::initAssetReaderImpl(AVAssetTrack *track, NSError *error)
418{
419 Q_ASSERT(track != nullptr);
420
421 if (error) {
422 processInvalidMedia(QAudioDecoder::ResourceError, QString::fromNSString(error.localizedDescription));
423 return;
424 }
425
426 QAudioFormat format = m_format.isValid() ? m_format : qt_format_for_audio_track(track);
427 if (!format.isValid()) {
428 processInvalidMedia(QAudioDecoder::FormatError, tr("Unsupported source format"));
429 return;
430 }
431
432 durationChanged(CMTimeGetSeconds(track.timeRange.duration) * 1000);
433
434 NSDictionary *audioSettings = av_audio_settings_for_format(format);
435
436 AVAssetReaderTrackOutput *readerOutput =
437 [[AVAssetReaderTrackOutput alloc] initWithTrack:track outputSettings:audioSettings];
438 AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:m_asset error:&error];
439 auto cleanup = qScopeGuard([&] {
440 [readerOutput release];
441 [reader release];
442 });
443
444 if (error) {
445 processInvalidMedia(QAudioDecoder::ResourceError, QString::fromNSString(error.localizedDescription));
446 return;
447 }
448 if (![reader canAddOutput:readerOutput]) {
449 processInvalidMedia(QAudioDecoder::ResourceError, tr("Failed to add asset reader output"));
450 return;
451 }
452
453 [reader addOutput:readerOutput];
454
455 Q_ASSERT(m_decodingContext);
456 cleanup.dismiss();
457
458 m_decodingContext->m_reader = reader;
459 m_decodingContext->m_readerOutput = readerOutput;
460
462}
463
464void AVFAudioDecoder::initAssetReader()
465{
466 qCDebug(qLcAVFAudioDecoder()) << "Init asset reader";
467
468 Q_ASSERT(m_asset);
469 Q_ASSERT(QThread::currentThread() == thread());
470
471#if defined(Q_OS_VISIONOS)
472 [m_asset loadTracksWithMediaType:AVMediaTypeAudio completionHandler:[=](NSArray<AVAssetTrack *> *tracks, NSError *error) {
473 if (tracks && tracks.count > 0) {
474 if (AVAssetTrack *track = [tracks objectAtIndex:0])
475 QMetaObject::invokeMethod(this, &AVFAudioDecoder::initAssetReaderImpl, Qt::QueuedConnection, track, error);
476 }
477 }];
478#else
479 NSArray<AVAssetTrack *> *tracks = [m_asset tracksWithMediaType:AVMediaTypeAudio];
480 if (tracks && tracks.count > 0) {
481 if (AVAssetTrack *track = [tracks objectAtIndex:0])
482 initAssetReaderImpl(track, nullptr /*error*/);
483 }
484#endif
485
486}
487
488void AVFAudioDecoder::startReading(QAudioFormat format)
489{
490 Q_ASSERT(m_decodingContext);
491 Q_ASSERT(m_decodingContext->m_reader);
492 Q_ASSERT(QThread::currentThread() == thread());
493
494 // Prepares the receiver for obtaining sample buffers from the asset.
495 if (![m_decodingContext->m_reader startReading]) {
496 processInvalidMedia(QAudioDecoder::ResourceError, tr("Could not start reading"));
497 return;
498 }
499
500 setIsDecoding(true);
501
502 std::weak_ptr<DecodingContext> weakContext = m_decodingContext;
503
504 // Since copyNextSampleBuffer is synchronous, submit it to an async dispatch queue
505 // to run in a separate thread. Call the handleNextSampleBuffer "callback" on another
506 // thread when new audio sample is read.
507 auto copyNextSampleBuffer = [=, this]() {
508 auto decodingContext = weakContext.lock();
509 if (!decodingContext)
510 return false;
511
512 QCFType<CMSampleBufferRef> sampleBuffer{
513 [decodingContext->m_readerOutput copyNextSampleBuffer],
514 };
515 if (!sampleBuffer)
516 return false;
517
518 dispatch_async(m_decodingQueue, [=, this]() {
519 if (!weakContext.expired() && CMSampleBufferDataIsReady(sampleBuffer)) {
520 auto audioBuffer = handleNextSampleBuffer(format, sampleBuffer);
521
522 if (audioBuffer.isValid())
523 invokeWithDecodingContext(weakContext, [=, this]() {
524 handleNewAudioBuffer(audioBuffer);
525 });
526 }
527 });
528
529 return true;
530 };
531
532 dispatch_async(m_readingQueue, [=, this]() {
533 qCDebug(qLcAVFAudioDecoder()) << "start reading thread";
534
535 do {
536 // Note, waiting here doesn't ensure strong contol of the counter.
537 // However, it doesn't affect the logic: the reading flow works fine
538 // even if the counter is time-to-time more than max value
539 waitUntilBuffersCounterLessMax();
540 } while (copyNextSampleBuffer());
541
542 // TODO: check m_reader.status == AVAssetReaderStatusFailed
543 invokeWithDecodingContext(weakContext, [this]() { onFinished(); });
544 });
545}
546
547void AVFAudioDecoder::waitUntilBuffersCounterLessMax()
548{
549 if (m_buffersCounter >= MAX_BUFFERS_IN_QUEUE) {
550 // the check avoids extra mutex lock.
551
552 QMutexLocker locker(&m_buffersCounterMutex);
553
554 while (m_buffersCounter >= MAX_BUFFERS_IN_QUEUE)
555 m_buffersCounterCondition.wait(&m_buffersCounterMutex);
556 }
557}
558
559void AVFAudioDecoder::handleNewAudioBuffer(QAudioBuffer buffer)
560{
561 m_cachedBuffers.enqueue(std::move(buffer));
562 ++m_buffersCounter;
563
564 Q_ASSERT(m_cachedBuffers.size() == m_buffersCounter);
565
566 bufferAvailableChanged(true);
567 bufferReady();
568}
569
570/*
571 * The method calls the passed functor in the thread of AVFAudioDecoder and guarantees that
572 * the passed decoding context is not expired. In other words, it helps avoiding all callbacks
573 * after stopping of the decoder.
574 */
575template<typename F>
576void AVFAudioDecoder::invokeWithDecodingContext(std::weak_ptr<DecodingContext> weakContext, F &&f)
577{
578 if (!weakContext.expired())
579 QMetaObject::invokeMethod(
580 this, [this, f = std::forward<F>(f), weakContext = std::move(weakContext)]() {
581 // strong check: compare with actual decoding context.
582 // Otherwise, the context can be temporary locked by one of dispatch queues.
583 if (auto context = weakContext.lock(); context && context == m_decodingContext)
584 f();
585 });
586}
587
588#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