From a0d47e2e64813516dd2939223f7c1c60de3f228b Mon Sep 17 00:00:00 2001 From: Mirko Date: Tue, 5 Jan 2021 19:06:14 +0100 Subject: [PATCH] Camera: Fix AVFoundation provider to release the camera, start it async, and check if started before stopping it (#7303) * Camera: Fix AVCamera to release the camera, start it async, and check if started before stopping it * Camera: Additional check on storage.camera pre/post deinit --- kivy/core/camera/camera_avfoundation.pyx | 9 +++++++++ kivy/core/camera/camera_avfoundation_implem.m | 8 ++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/kivy/core/camera/camera_avfoundation.pyx b/kivy/core/camera/camera_avfoundation.pyx index f25e01742..1f8b76dc6 100644 --- a/kivy/core/camera/camera_avfoundation.pyx +++ b/kivy/core/camera/camera_avfoundation.pyx @@ -49,11 +49,20 @@ class CameraAVFoundation(CameraBase): self._framerate = 30 super(CameraAVFoundation, self).__init__(**kwargs) + def __del__(self): + self._release_camera() + def init_camera(self): cdef _AVStorage storage = <_AVStorage>self._storage storage.camera = avf_camera_init( self._index, self.resolution[0], self.resolution[1]) + def _release_camera(self): + cdef _AVStorage storage = <_AVStorage>self._storage + if storage.camera != NULL: + avf_camera_deinit(storage.camera) + storage.camera = NULL + @property def _scheduled_rate(self): # We're going 4 times faster the framerate to avoid frame skipping. diff --git a/kivy/core/camera/camera_avfoundation_implem.m b/kivy/core/camera/camera_avfoundation_implem.m index 46a2166e8..67e40ae18 100644 --- a/kivy/core/camera/camera_avfoundation_implem.m +++ b/kivy/core/camera/camera_avfoundation_implem.m @@ -214,7 +214,9 @@ Camera::Camera(int _cameraNum, int _width, int _height) { } Camera::~Camera() { - stopCaptureDevice(); + if(started){ + stopCaptureDevice(); + } } bool Camera::grabFrame(double timeOut) { @@ -411,7 +413,9 @@ int Camera::startCaptureDevice() { #endif [conn setVideoOrientation:default_orientation]; - [mCaptureSession startRunning]; + dispatch_async(dispatch_get_main_queue(), ^{ + [mCaptureSession startRunning]; + }); [localpool drain]; started = 1;