summaryrefslogtreecommitdiff
path: root/src/plugins/multimedia/android/audio/qandroidaudiodecoder_p.h
blob: 39446e9e498e15ccaa580a2d60250fe17cb7ad9c (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// 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 QANDROIDAUDIODECODER_P_H
#define QANDROIDAUDIODECODER_P_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 "private/qplatformaudiodecoder_p.h"

#include <QtCore/qurl.h>
#include <QThread>

#include "media/NdkMediaCodec.h"
#include "media/NdkMediaExtractor.h"
#include "media/NdkMediaFormat.h"
#include "media/NdkMediaError.h"


QT_USE_NAMESPACE

class Decoder : public QObject
{
    Q_OBJECT
public:
    Decoder();
    ~Decoder();

public slots:
    void setSource(const QUrl &source);
    void doDecode();
    void stop();

signals:
    void positionChanged(const QAudioBuffer &buffer, qint64 position);
    void durationChanged(const qint64 duration);
    void error(const QAudioDecoder::Error error, const QString &errorString);
    void finished();
    void decodingChanged(bool decoding);

private:
    void createDecoder();

    AMediaCodec *m_codec = nullptr;
    AMediaExtractor *m_extractor = nullptr;
    AMediaFormat *m_format = nullptr;

    QAudioFormat m_outputFormat;
    QString m_formatError;
    bool m_inputEOS;
};


class QAndroidAudioDecoder : public QPlatformAudioDecoder
{
    Q_OBJECT
public:
    QAndroidAudioDecoder(QAudioDecoder *parent);
        virtual ~QAndroidAudioDecoder();

    QUrl source() const override { return m_source; }
    void setSource(const QUrl &fileName) override;

    QIODevice *sourceDevice() const override { return m_device; }
    void setSourceDevice(QIODevice *device) override;

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

    QAudioFormat audioFormat() const override { return {}; }
    void setAudioFormat(const QAudioFormat &/*format*/) override {}

    QAudioBuffer read() override;
    bool bufferAvailable() const override;

    qint64 position() const override;
    qint64 duration() const override;

signals:
    void setSourceUrl(const QUrl &source);

private slots:
    void positionChanged(QAudioBuffer audioBuffer, qint64 position);
    void durationChanged(qint64 duration);
    void error(const QAudioDecoder::Error error, const QString &errorString);
    void readDevice();
    void finished();

private:
    bool requestPermissions();
    bool createTempFile();
    void decode();

    QIODevice *m_device = nullptr;
    Decoder *m_decoder;

    QList<QPair<QAudioBuffer, int>> m_audioBuffer;
    QUrl m_source;

    qint64 m_position = -1;
    qint64 m_duration = -1;
    long long m_presentationTimeUs = 0;

    QByteArray m_deviceBuffer;

    QThread *m_threadDecoder = nullptr;
};

QT_END_NAMESPACE

#endif // QANDROIDAUDIODECODER_P_H