summaryrefslogtreecommitdiffstats
path: root/src/plugins/multimedia/darwin/avfaudiodecoder_p.h
blob: 81ef3f49e24b639f22c313cca29bc46d105c274e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef AVFAUDIODECODER_H
#define AVFAUDIODECODER_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtMultimedia/private/qtmultimediaglobal_p.h>
#include <QObject>
#include <QtCore/qurl.h>
#include <QWaitCondition>
#include <QMutex>
#include <QQueue>

#include "private/qplatformaudiodecoder_p.h"
#include "qaudiodecoder.h"

#include <dispatch/dispatch.h>

Q_FORWARD_DECLARE_OBJC_CLASS(AVURLAsset);
Q_FORWARD_DECLARE_OBJC_CLASS(AVAssetReader);
Q_FORWARD_DECLARE_OBJC_CLASS(AVAssetReaderTrackOutput);
Q_FORWARD_DECLARE_OBJC_CLASS(AVFResourceReaderDelegate);

QT_BEGIN_NAMESPACE

class AVFAudioDecoder : public QPlatformAudioDecoder
{
    Q_OBJECT

    struct DecodingContext;

public:
    AVFAudioDecoder(QAudioDecoder *parent);
    virtual ~AVFAudioDecoder();

    QUrl source() const override;
    void setSource(const QUrl &fileName) override;

    QIODevice *sourceDevice() const override;
    void setSourceDevice(QIODevice *device) override;

    void start() override;
    void stop() override;

    QAudioFormat audioFormat() const override;
    void setAudioFormat(const QAudioFormat &format) override;

    QAudioBuffer read() override;

private:
    void handleNewAudioBuffer(QAudioBuffer);
    void startReading();

    void processInvalidMedia(QAudioDecoder::Error errorCode, const QString& errorString);
    void initAssetReader();
    void onFinished();

    void waitUntilBuffersCounterLessMax();

    void decBuffersCounter(uint val);

    template<typename F>
    void invokeWithDecodingContext(std::weak_ptr<DecodingContext> weakContext, F &&f);

private:
    QUrl m_source;
    QIODevice *m_device = nullptr;
    QAudioFormat m_format;

    // Use a separate counter instead of buffers queue size in order to
    // ensure atomic access and also make mutex locking shorter
    std::atomic<int> m_buffersCounter = 0;
    QQueue<QAudioBuffer> m_cachedBuffers;

    AVURLAsset *m_asset = nullptr;

    AVFResourceReaderDelegate *m_readerDelegate = nullptr;
    dispatch_queue_t m_readingQueue;
    dispatch_queue_t m_decodingQueue;

    std::shared_ptr<DecodingContext> m_decodingContext;
    QMutex m_buffersCounterMutex;
    QWaitCondition m_buffersCounterCondition;
};

QT_END_NAMESPACE

#endif // AVFAUDIODECODER_H