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_p.h
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
4#ifndef AVFAUDIODECODER_H
5#define AVFAUDIODECODER_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 <QtMultimedia/private/qtmultimediaglobal_p.h>
19#include <QObject>
20#include <QtCore/qurl.h>
21#include <QWaitCondition>
22#include <QMutex>
23#include <QQueue>
24
25#include "private/qplatformaudiodecoder_p.h"
26#include "qaudiodecoder.h"
27
28#import <dispatch/dispatch.h>
29#import <AVFoundation/AVFoundation.h>
30
31@class AVFResourceReaderDelegate;
32
33QT_BEGIN_NAMESPACE
34
35class AVFAudioDecoder final : public QPlatformAudioDecoder
36{
37 Q_OBJECT
38
39 struct DecodingContext;
40
41public:
42 AVFAudioDecoder(QAudioDecoder *parent);
43 ~AVFAudioDecoder() override;
44
45 QUrl source() const override;
46 void setSource(const QUrl &fileName) override;
47
48 QIODevice *sourceDevice() const override;
49 void setSourceDevice(QIODevice *device) override;
50
51 void start() override;
52 void stop() override;
53
54 QAudioFormat audioFormat() const override;
55 void setAudioFormat(const QAudioFormat &format) override;
56
57 QAudioBuffer read() override;
58
59private:
60 void handleNewAudioBuffer(QAudioBuffer);
61 void startReading(QAudioFormat);
62
63 void processInvalidMedia(QAudioDecoder::Error errorCode, const QString& errorString);
64 void initAssetReader();
65 void onFinished();
66
67 void waitUntilBuffersCounterLessMax();
68
69 void decBuffersCounter(uint val);
70
71 template<typename F>
72 void invokeWithDecodingContext(std::weak_ptr<DecodingContext> weakContext, F &&f);
73 Q_INVOKABLE void initAssetReaderImpl(AVAssetTrack *track, NSError *error);
74
75private:
76 QUrl m_source;
77 QIODevice *m_device = nullptr;
78 QAudioFormat m_format;
79
80 // Use a separate counter instead of buffers queue size in order to
81 // ensure atomic access and also make mutex locking shorter
82 std::atomic<int> m_buffersCounter = 0;
83 QQueue<QAudioBuffer> m_cachedBuffers;
84
85 AVURLAsset *m_asset = nullptr;
86
87 AVFResourceReaderDelegate *m_readerDelegate = nullptr;
88 dispatch_queue_t m_readingQueue;
89 dispatch_queue_t m_decodingQueue;
90
91 std::shared_ptr<DecodingContext> m_decodingContext;
92 QMutex m_buffersCounterMutex;
93 QWaitCondition m_buffersCounterCondition;
94};
95
96QT_END_NAMESPACE
97
98#endif // AVFAUDIODECODER_H
static constexpr int MAX_BUFFERS_IN_QUEUE
static QAudioBuffer handleNextSampleBuffer(QAudioFormat qtFormat, CMSampleBufferRef sampleBuffer)
AVAssetReaderTrackOutput * m_readerOutput
QImage::Format format
Definition qimage_p.h:52