From 64e84dab2673020e4384ae36d9d1e9e4f0d8052d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Str=C3=B8mme?= Date: Mon, 12 Dec 2016 18:06:54 +0100 Subject: Android: Run-time permission checks for camera The camera should ask for permission before being used. Normally this should be handle by the user, which can provide proper reasons for why the camera is used. To verify that we are allowed to open the camera, and as a good default, we'll check the permission and request access if needed. [ChangeLog][Android] Added run-time permission checks for accessing the camera. Task-number: QTBUG-55992 Change-Id: Ifb4e176728aab3076fdba292e17e17d33b1260a2 Reviewed-by: Eskil Abrahamsen Blomfeldt --- .../android/src/mediacapture/qandroidcamerasession.cpp | 2 +- src/plugins/android/src/wrappers/jni/androidcamera.cpp | 14 ++++++++++++++ src/plugins/android/src/wrappers/jni/androidcamera.h | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) (limited to 'src/plugins/android') diff --git a/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp b/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp index b0dd7d900..a7f0254ee 100644 --- a/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp +++ b/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp @@ -176,7 +176,7 @@ bool QAndroidCameraSession::open() m_status = QCamera::LoadingStatus; emit statusChanged(m_status); - m_camera = AndroidCamera::open(m_selectedCamera); + m_camera = AndroidCamera::requestCameraPermission() ? AndroidCamera::open(m_selectedCamera) : nullptr; if (m_camera) { connect(m_camera, SIGNAL(pictureExposed()), this, SLOT(onCameraPictureExposed())); diff --git a/src/plugins/android/src/wrappers/jni/androidcamera.cpp b/src/plugins/android/src/wrappers/jni/androidcamera.cpp index 0f2a43531..e3afddd59 100644 --- a/src/plugins/android/src/wrappers/jni/androidcamera.cpp +++ b/src/plugins/android/src/wrappers/jni/androidcamera.cpp @@ -42,6 +42,7 @@ #include "androidsurfacetexture.h" #include "androidsurfaceview.h" #include "qandroidmultimediautils.h" +#include "qandroidglobal.h" #include #include @@ -55,6 +56,11 @@ QT_BEGIN_NAMESPACE static const char QtCameraListenerClassName[] = "org/qtproject/qt5/android/multimedia/QtCameraListener"; +static QString cameraPermissionKey() +{ + return QStringLiteral("android.permission.CAMERA"); +} + typedef QHash CameraMap; Q_GLOBAL_STATIC(CameraMap, cameras) Q_GLOBAL_STATIC(QReadWriteLock, rwLock) @@ -756,6 +762,9 @@ QJNIObjectPrivate AndroidCamera::getCameraObject() int AndroidCamera::getNumberOfCameras() { + if (!requestCameraPermission()) + return 0; + return QJNIObjectPrivate::callStaticMethod("android/hardware/Camera", "getNumberOfCameras"); } @@ -790,6 +799,11 @@ void AndroidCamera::getCameraInfo(int id, AndroidCameraInfo *info) } } +bool AndroidCamera::requestCameraPermission() +{ + return qt_androidRequestPermission(cameraPermissionKey()); +} + void AndroidCamera::startPreview() { Q_D(AndroidCamera); diff --git a/src/plugins/android/src/wrappers/jni/androidcamera.h b/src/plugins/android/src/wrappers/jni/androidcamera.h index e58a81f8e..5ae141f01 100644 --- a/src/plugins/android/src/wrappers/jni/androidcamera.h +++ b/src/plugins/android/src/wrappers/jni/androidcamera.h @@ -201,6 +201,7 @@ public: static int getNumberOfCameras(); static void getCameraInfo(int id, AndroidCameraInfo *info); + static bool requestCameraPermission(); static bool initJNI(JNIEnv *env); -- cgit v1.2.3 From f7e5f12149b73b60c594be626eeac8c2aa34beda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Str=C3=B8mme?= Date: Thu, 20 Apr 2017 17:23:11 +0200 Subject: Android: Fix crash caused by stale video-frame objects Make sure that the frame being processed is still valid, that is, the size is valid and correct, and that the frame buffer is accessible. Task-number: QTBUG-60115 Change-Id: Ic64b6f7d45d92b7923d97d6ecdc630da31abee17 Reviewed-by: Michael Dippold Reviewed-by: Eirik Aavitsland --- .../android/src/common/qandroidvideooutput.cpp | 52 +++++++++++++++------- .../android/src/common/qandroidvideooutput.h | 2 +- 2 files changed, 36 insertions(+), 18 deletions(-) (limited to 'src/plugins/android') diff --git a/src/plugins/android/src/common/qandroidvideooutput.cpp b/src/plugins/android/src/common/qandroidvideooutput.cpp index 1d0df27f2..b425b9d89 100644 --- a/src/plugins/android/src/common/qandroidvideooutput.cpp +++ b/src/plugins/android/src/common/qandroidvideooutput.cpp @@ -86,11 +86,12 @@ void OpenGLResourcesDeleter::deleteShaderProgramHelper(void *prog) class AndroidTextureVideoBuffer : public QAbstractVideoBuffer { public: - AndroidTextureVideoBuffer(QAndroidTextureVideoOutput *output) + AndroidTextureVideoBuffer(QAndroidTextureVideoOutput *output, const QSize &size) : QAbstractVideoBuffer(GLTextureHandle) + , m_mapMode(NotMapped) , m_output(output) + , m_size(size) , m_textureUpdated(false) - , m_mapMode(NotMapped) { } @@ -100,8 +101,7 @@ public: uchar *map(MapMode mode, int *numBytes, int *bytesPerLine) { - if (m_mapMode == NotMapped && mode == ReadOnly) { - updateFrame(); + if (m_mapMode == NotMapped && mode == ReadOnly && updateFrame()) { m_mapMode = mode; m_image = m_output->m_fbo->toImage(); @@ -126,24 +126,41 @@ public: QVariant handle() const { AndroidTextureVideoBuffer *that = const_cast(this); - that->updateFrame(); + if (!that->updateFrame()) + return QVariant(); + return m_output->m_fbo->texture(); } private: - void updateFrame() + bool updateFrame() { - if (!m_textureUpdated) { - // update the video texture (called from the render thread) - m_output->renderFrameToFbo(); - m_textureUpdated = true; - } + // Even though the texture was updated in a previous call, we need to re-check + // that this has not become a stale buffer, e.g., if the output size changed or + // has since became invalid. + if (!m_output->m_nativeSize.isValid()) + return false; + + // Size changed + if (m_output->m_nativeSize != m_size) + return false; + + // In the unlikely event that we don't have a valid fbo, but have a valid size, + // force an update. + const bool forceUpdate = !m_output->m_fbo; + + if (m_textureUpdated && !forceUpdate) + return true; + + // update the video texture (called from the render thread) + return (m_textureUpdated = m_output->renderFrameToFbo()); } - QAndroidTextureVideoOutput *m_output; - bool m_textureUpdated; MapMode m_mapMode; + QAndroidTextureVideoOutput *m_output; QImage m_image; + QSize m_size; + bool m_textureUpdated; }; QAndroidTextureVideoOutput::QAndroidTextureVideoOutput(QObject *parent) @@ -267,7 +284,6 @@ AndroidSurfaceTexture *QAndroidTextureVideoOutput::surfaceTexture() void QAndroidTextureVideoOutput::setVideoSize(const QSize &size) { QMutexLocker locker(&m_mutex); - if (m_nativeSize == size) return; @@ -297,7 +313,7 @@ void QAndroidTextureVideoOutput::onFrameAvailable() if (!m_nativeSize.isValid() || !m_surface) return; - QAbstractVideoBuffer *buffer = new AndroidTextureVideoBuffer(this); + QAbstractVideoBuffer *buffer = new AndroidTextureVideoBuffer(this, m_nativeSize); QVideoFrame frame(buffer, m_nativeSize, QVideoFrame::Format_BGR32); if (m_surface->isActive() && (m_surface->surfaceFormat().pixelFormat() != frame.pixelFormat() @@ -316,12 +332,12 @@ void QAndroidTextureVideoOutput::onFrameAvailable() m_surface->present(frame); } -void QAndroidTextureVideoOutput::renderFrameToFbo() +bool QAndroidTextureVideoOutput::renderFrameToFbo() { QMutexLocker locker(&m_mutex); if (!m_nativeSize.isValid() || !m_surfaceTexture) - return; + return false; createGLResources(); @@ -376,6 +392,8 @@ void QAndroidTextureVideoOutput::renderFrameToFbo() glEnable(GL_SCISSOR_TEST); if (blendEnabled) glEnable(GL_BLEND); + + return true; } void QAndroidTextureVideoOutput::createGLResources() diff --git a/src/plugins/android/src/common/qandroidvideooutput.h b/src/plugins/android/src/common/qandroidvideooutput.h index a12db75a2..936e4c40b 100644 --- a/src/plugins/android/src/common/qandroidvideooutput.h +++ b/src/plugins/android/src/common/qandroidvideooutput.h @@ -112,7 +112,7 @@ private Q_SLOTS: private: bool initSurfaceTexture(); - void renderFrameToFbo(); + bool renderFrameToFbo(); void createGLResources(); QMutex m_mutex; -- cgit v1.2.3