summaryrefslogtreecommitdiffstats
path: root/src/multimedia/platform/android/common/qandroidmultimediautils.cpp
blob: 552c8e9039e2b5bdd9f7fe49145894dfb0bd25f8 (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
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** 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.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
******************************************************************************/

#include "qandroidmultimediautils_p.h"
#include "qandroidglobal_p.h"

#include <qlist.h>
#include <QtCore/qcoreapplication.h>
#include <QtCore/private/qandroidextras_p.h>

QT_BEGIN_NAMESPACE

int qt_findClosestValue(const QList<int> &list, int value)
{
    if (list.size() < 2)
        return 0;

    int begin = 0;
    int end = list.size() - 1;
    int pivot = begin + (end - begin) / 2;
    int v = list.at(pivot);

    while (end - begin > 1) {
        if (value == v)
            return pivot;

        if (value > v)
            begin = pivot;
        else
            end = pivot;

        pivot = begin + (end - begin) / 2;
        v = list.at(pivot);
    }

    return value - v >= list.at(pivot + 1) - value ? pivot + 1 : pivot;
}

bool qt_sizeLessThan(const QSize &s1, const QSize &s2)
{
    return s1.width() * s1.height() < s2.width() * s2.height();
}

QVideoFrameFormat::PixelFormat qt_pixelFormatFromAndroidImageFormat(AndroidCamera::ImageFormat f)
{
    switch (f) {
    case AndroidCamera::NV21:
        return QVideoFrameFormat::Format_NV21;
    case AndroidCamera::YV12:
        return QVideoFrameFormat::Format_YV12;
    case AndroidCamera::YUY2:
        return QVideoFrameFormat::Format_YUYV;
    case AndroidCamera::JPEG:
        return QVideoFrameFormat::Format_Jpeg;
    default:
        return QVideoFrameFormat::Format_Invalid;
    }
}

AndroidCamera::ImageFormat qt_androidImageFormatFromPixelFormat(QVideoFrameFormat::PixelFormat f)
{
    switch (f) {
    case QVideoFrameFormat::Format_NV21:
        return AndroidCamera::NV21;
    case QVideoFrameFormat::Format_YV12:
        return AndroidCamera::YV12;
    case QVideoFrameFormat::Format_YUYV:
        return AndroidCamera::YUY2;
    case QVideoFrameFormat::Format_Jpeg:
        return AndroidCamera::JPEG;
    default:
        return AndroidCamera::UnknownImageFormat;
    }
}

static bool androidRequestPermission(QtAndroidPrivate::PermissionType key)
{
    if (QNativeInterface::QAndroidApplication::sdkVersion() < 23)
        return true;

    // Permission already granted?
    if (QtAndroidPrivate::checkPermission(key).result() == QtAndroidPrivate::Authorized)
        return true;

    if (QtAndroidPrivate::requestPermission(key).result() != QtAndroidPrivate::Authorized)
        return false;

    return true;
}

static bool androidCheckPermission(QtAndroidPrivate::PermissionType key)
{
    if (QNativeInterface::QAndroidApplication::sdkVersion() < 23)
        return true;

    // Permission already granted?
    return (QtAndroidPrivate::checkPermission(key).result() == QtAndroidPrivate::Authorized);
}

bool qt_androidCheckCameraPermission()
{
    return androidCheckPermission(QtAndroidPrivate::Camera);
}

bool qt_androidCheckMicrophonePermission()
{
    return androidCheckPermission(QtAndroidPrivate::Microphone);
}

bool qt_androidRequestCameraPermission()
{
    if (!androidRequestPermission(QtAndroidPrivate::Camera)) {
        qCDebug(qtAndroidMediaPlugin, "Camera permission denied by user!");
        return false;
    }

    return true;
}

bool qt_androidRequestRecordingPermission()
{
    if (!androidRequestPermission(QtAndroidPrivate::Microphone)) {
        qCDebug(qtAndroidMediaPlugin, "Microphone permission denied by user!");
        return false;
    }

    return true;
}

bool qt_androidRequestWriteStoragePermission()
{
    if (!androidRequestPermission(QtAndroidPrivate::Storage)) {
        qCDebug(qtAndroidMediaPlugin, "Storage permission denied by user!");
        return false;
    }

    return true;
}

QT_END_NAMESPACE