Skip to content

Commit

Permalink
Merge pull request #6988 from misl6/fix/avfoundation-camera-fixes
Browse files Browse the repository at this point in the history
Stage 1: Improve avfoundation camera implementation on iOS
  • Loading branch information
Zen-CODE authored Dec 6, 2020
2 parents b714003 + 3f4c2d3 commit e16794a
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 17 deletions.
24 changes: 23 additions & 1 deletion kivy/core/camera/camera_avfoundation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ cdef extern from "camera_avfoundation_implem.h":
void avf_camera_start(camera_t camera)
void avf_camera_stop(camera_t camera)
void avf_camera_get_image(camera_t camera, int *width, int *height, int *rowsize, char **data)
bint avf_camera_attempt_framerate_selection(camera_t camera, int fps)
bint avf_camera_attempt_capture_preset(camera_t camera, char* preset)
bint avf_camera_attempt_start_metadata_analysis(camera_t camera)
void avf_camera_get_metadata(camera_t camera, char **metatype, char **data)
bint avf_camera_have_new_metadata(camera_t camera);


from kivy.logger import Logger
Expand All @@ -38,6 +43,7 @@ class CameraAVFoundation(CameraBase):
def __init__(self, **kwargs):
self._storage = _AVStorage()
self._update_ev = None
self._metadata_callback = None
super(CameraAVFoundation, self).__init__(**kwargs)

def init_camera(self):
Expand All @@ -49,6 +55,8 @@ class CameraAVFoundation(CameraBase):
cdef _AVStorage storage = <_AVStorage>self._storage
cdef int width, height, rowsize
cdef char *data
cdef char *metadata_type
cdef char *metadata_data

if self.stopped:
return
Expand All @@ -61,7 +69,7 @@ class CameraAVFoundation(CameraBase):
return

self._resolution = (width, height)

if self._texture is None or self._texture.size != self._resolution:
if platform == 'ios':
self._texture = Texture.create(self._resolution, colorfmt='bgra')
Expand All @@ -73,6 +81,10 @@ class CameraAVFoundation(CameraBase):
self._buffer = <bytes>data[:rowsize * height]
self._format = 'bgra'
self._copy_to_gpu()
if self._metadata_callback:
if avf_camera_have_new_metadata(storage.camera):
avf_camera_get_metadata(storage.camera, &metadata_type, &metadata_data)
self._metadata_callback(metadata_type, metadata_data)

def start(self):
cdef _AVStorage storage = <_AVStorage>self._storage
Expand All @@ -90,5 +102,15 @@ class CameraAVFoundation(CameraBase):
self._update_ev = None
avf_camera_stop(storage.camera)

def set_framerate(self, framerate):
cdef _AVStorage storage = <_AVStorage>self._storage
avf_camera_attempt_framerate_selection(storage.camera, framerate)

def set_preset(self, preset):
cdef _AVStorage storage = <_AVStorage>self._storage
avf_camera_attempt_capture_preset(storage.camera, preset)

def start_metadata_analysis(self, callback=None):
cdef _AVStorage storage = <_AVStorage>self._storage
self._metadata_callback = callback
avf_camera_attempt_start_metadata_analysis(storage.camera)
5 changes: 5 additions & 0 deletions kivy/core/camera/camera_avfoundation_implem.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ void avf_camera_update(camera_t camera);
void avf_camera_start(camera_t camera);
void avf_camera_stop(camera_t camera);
void avf_camera_get_image(camera_t camera, int *width, int *height, int *rowsize, char **data);
bool avf_camera_attempt_framerate_selection(camera_t camera, int fps);
bool avf_camera_attempt_capture_preset(camera_t camera, char* preset);
bool avf_camera_attempt_start_metadata_analysis(camera_t camera);
void avf_camera_get_metadata(camera_t camera, char **metatype, char **data);
bool avf_camera_have_new_metadata(camera_t camera);
Loading

0 comments on commit e16794a

Please sign in to comment.