summaryrefslogtreecommitdiffstats
path: root/src/plugins/multimedia/android/qandroidformatsinfo.cpp
blob: 584c7a122e95d5f37d878f0a1447ce9342057547 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qandroidformatsinfo_p.h"

#include <QtCore/qjnienvironment.h>
#include <QtCore/qjniobject.h>
#include <qcoreapplication.h>

static const char encoderFilter[] = ".encoder";
static const char decoderFilter[] = ".decoder";

QT_BEGIN_NAMESPACE

QAndroidFormatInfo::QAndroidFormatInfo()
{
    // Audio/Video/Image formats with their decoder/encoder information is documented at
    // https://developer.android.com/guide/topics/media/media-formats

    const QJniObject codecsArrayObject = QJniObject::callStaticObjectMethod(
                "org/qtproject/qt/android/multimedia/QtMultimediaUtils",
                "getMediaCodecs",
                "()[Ljava/lang/String;");
    QStringList codecs;
    QJniEnvironment env;
    const jobjectArray devsArray = codecsArrayObject.object<jobjectArray>();
    for (int i = 0; i < env->GetArrayLength(devsArray); ++i) {
        const QString codec = QJniObject(env->GetObjectArrayElement(devsArray, i)).toString();
        if (codec.contains(QStringLiteral("encoder")))
            m_supportedEncoders.append(codec);
        else
            m_supportedDecoders.append(codec);
    }

    auto removeUnspecifiedValues = [](QList<CodecMap> &map) {
        for (CodecMap &codec : map) {
            codec.audio.removeAll(QMediaFormat::AudioCodec::Unspecified);
            codec.video.removeAll(QMediaFormat::VideoCodec::Unspecified);
        }
        erase_if(map, [](const CodecMap &codec) {
            return codec.audio.isEmpty() && codec.video.isEmpty();
        });
    };

    {
        const QMediaFormat::AudioCodec aac = hasDecoder(QMediaFormat::AudioCodec::AAC);
        const QMediaFormat::AudioCodec mp3 = hasDecoder(QMediaFormat::AudioCodec::MP3);
        const QMediaFormat::AudioCodec flac = hasDecoder(QMediaFormat::AudioCodec::FLAC);
        const QMediaFormat::AudioCodec opus = hasDecoder(QMediaFormat::AudioCodec::Opus);
        const QMediaFormat::AudioCodec vorbis = hasDecoder(QMediaFormat::AudioCodec::Vorbis);

        const QMediaFormat::VideoCodec vp8 = hasDecoder(QMediaFormat::VideoCodec::VP8);
        const QMediaFormat::VideoCodec vp9 = hasDecoder(QMediaFormat::VideoCodec::VP9);
        const QMediaFormat::VideoCodec h264 = hasDecoder(QMediaFormat::VideoCodec::H264);
        const QMediaFormat::VideoCodec h265 = hasDecoder(QMediaFormat::VideoCodec::H265);
        const QMediaFormat::VideoCodec av1 = hasDecoder(QMediaFormat::VideoCodec::AV1);

        decoders = {
            { QMediaFormat::AAC, {aac}, {} },
            { QMediaFormat::MP3, {mp3}, {} },
            { QMediaFormat::FLAC, {flac}, {} },
            { QMediaFormat::Mpeg4Audio, {mp3, aac, flac, vorbis}, {} },
            { QMediaFormat::MPEG4, {mp3, aac, flac, vorbis}, {h264, h265, av1} },
            { QMediaFormat::Ogg, {opus, vorbis, flac}, {} },
            { QMediaFormat::Matroska, {mp3, opus, vorbis}, {vp8, vp9, h264, h265, av1} },
            { QMediaFormat::WebM, {opus, vorbis}, {vp8, vp9} }
        };

        removeUnspecifiedValues(decoders);
    }

    {
        const QMediaFormat::AudioCodec aac = hasEncoder(QMediaFormat::AudioCodec::AAC);
        const QMediaFormat::AudioCodec mp3 = hasEncoder(QMediaFormat::AudioCodec::MP3);
        const QMediaFormat::AudioCodec flac = hasEncoder(QMediaFormat::AudioCodec::FLAC);
        const QMediaFormat::AudioCodec opus = hasEncoder(QMediaFormat::AudioCodec::Opus);
        const QMediaFormat::AudioCodec vorbis = hasEncoder(QMediaFormat::AudioCodec::Vorbis);

        const QMediaFormat::VideoCodec vp8 = hasEncoder(QMediaFormat::VideoCodec::VP8);
        const QMediaFormat::VideoCodec vp9 = hasEncoder(QMediaFormat::VideoCodec::VP9);
        const QMediaFormat::VideoCodec h264 = hasEncoder(QMediaFormat::VideoCodec::H264);
        const QMediaFormat::VideoCodec h265 = hasEncoder(QMediaFormat::VideoCodec::H265);
        const QMediaFormat::VideoCodec av1 = hasEncoder(QMediaFormat::VideoCodec::AV1);

        // MP3 and Vorbis encoders are not supported by the default Android SDK
        // Opus encoder available only for Android 10+
        encoders = {
            { QMediaFormat::AAC, {aac}, {} },
            { QMediaFormat::MP3, {mp3}, {} },
            { QMediaFormat::FLAC, {flac}, {} },
            { QMediaFormat::Mpeg4Audio, {mp3, aac, flac, vorbis}, {} },
            { QMediaFormat::MPEG4, {mp3, aac, flac, vorbis}, {h264, h265, av1} },
            { QMediaFormat::Ogg, {opus, vorbis, flac}, {} },
            { QMediaFormat::Matroska, {mp3, opus, flac}, {vp8, vp9, h264, h265, av1} },
            // NOTE: WebM seems to be documented to supported with VP8 encoder,
            // but the Camera API doesn't work with it, keep it commented for now.
            // { QMediaFormat::WebM, {vorbis, opus}, {vp8, vp9} }
        };

        removeUnspecifiedValues(encoders);
    }

    imageFormats << QImageCapture::JPEG;
    // NOTE: Add later if needed, the Camera API doens't seem to work with it.
    // imageFormats << QImageCapture::PNG << QImageCapture::WebP;
}

QAndroidFormatInfo::~QAndroidFormatInfo()
{
}

static QString getVideoCodecName(QMediaFormat::VideoCodec codec)
{
    QString codecString = QMediaFormat::videoCodecName(codec);
    if (codec == QMediaFormat::VideoCodec::H265)
        codecString = QLatin1String("HEVC");
    return codecString;
}

QMediaFormat::AudioCodec QAndroidFormatInfo::hasEncoder(QMediaFormat::AudioCodec codec) const
{
    const QString codecString = QMediaFormat::audioCodecName(codec);
    for (auto str : m_supportedEncoders) {
        if (str.contains(codecString + QLatin1String(encoderFilter), Qt::CaseInsensitive))
            return codec;
    }
    return QMediaFormat::AudioCodec::Unspecified;
}

QMediaFormat::VideoCodec QAndroidFormatInfo::hasEncoder(QMediaFormat::VideoCodec codec) const
{
    const QString codecString = getVideoCodecName(codec);
    for (auto str : m_supportedEncoders) {
        if (str.contains(codecString + QLatin1String(encoderFilter), Qt::CaseInsensitive))
            return codec;
    }
    return QMediaFormat::VideoCodec::Unspecified;
}

QMediaFormat::AudioCodec QAndroidFormatInfo::hasDecoder(QMediaFormat::AudioCodec codec) const
{
    const QString codecString = QMediaFormat::audioCodecName(codec);
    for (auto str : m_supportedDecoders) {
        if (str.contains(codecString + QLatin1String(decoderFilter), Qt::CaseInsensitive))
            return codec;
    }
    return QMediaFormat::AudioCodec::Unspecified;
}

QMediaFormat::VideoCodec QAndroidFormatInfo::hasDecoder(QMediaFormat::VideoCodec codec) const
{
    const QString codecString = getVideoCodecName(codec);
    for (auto str : m_supportedDecoders) {
        if (str.contains(codecString + QLatin1String(decoderFilter), Qt::CaseInsensitive))
            return codec;
    }
    return QMediaFormat::VideoCodec::Unspecified;
}

QT_END_NAMESPACE