CameraEngine icon indicating copy to clipboard operation
CameraEngine copied to clipboard

[WIP] Swift 4 and some new api is available here

Open freemansion opened this issue 7 years ago • 6 comments

If anyone needs Swift 4.1 version of Camera Engine it is available here

This branch also contains some code refactoring per specific project requirements, which you might find helpful for your projects as well. It allows you specify requested device outputs, such as camera and microphone and provides callback which gives you detailed information about current device access request state and an ability to send back an action to be handled.

Device access request states:

public enum DeviceAccessState {
        public enum UnableToAdd {
            case camera, microphone
        }    
        case authorized // can proceed safely
        case denied // settings changes required
        case restricted // probably restricted at parental control
        case notDetermined // choice by the user wasn't made yet
        case unableToAdd(UnableToAdd)
        case other(String)
    }

A list of actions can be applied after retrieving device access state:

public enum DeviceAccessAction {
        case settingsChangeRequired // access was denied  or restricted at parental control
        case canPerformFirstTimeDeviceAccess // access not determined, can present in-app device access description (optional) before requesting on system level
        case canProceedAccessGranted // access granted, can proceed safely
        case unexpectedError(String)
    }

Usage example:

cameraEngine.startSession(devicePermissionRequests: [.camera, .microphone],
                                  deviceAccessPermissionHandler: { result, onFinishHandlingDeviceAccessState in
            switch result {
            case .camera(.denied),
                 .camera(.restricted),
                 .camera(.unableToAdd(.camera)),
                 .camera(.unableToAdd(.microphone)),
                 .microphone(.denied),
                 .microphone(.restricted),
                 .microphone(.unableToAdd(.microphone)),
                 .microphone(.unableToAdd(.camera)):
                // print("sad :(")
                // present in-app alert that OS settings change required
                onFinishHandlingDeviceAccessState?(.settingsChangeRequired)
            case .camera(.notDetermined), .microphone(.notDetermined):
                //
                //print("ok, preparing ui for the showing permission request first time")
                //
                onFinishHandlingDeviceAccessState?(.canPerformFirstTimeDeviceAccess)
            case .camera(.authorized), .microphone(.authorized):
                onFinishHandlingDeviceAccessState?(.canProceedAccessGranted)
            case let .camera(.other(errorMessage)):
                onFinishHandlingDeviceAccessState?(.unexpectedError(errorMessage))
            case let .microphone(.other(errorMessage)):
                onFinishHandlingDeviceAccessState?(.unexpectedError(errorMessage))
            case .camera(.runningOnSimulator),
                 .microphone(.runningOnSimulator):
                print("can't use camera device when running on simulator")
            }
        })

CameraEngineSessionPreset was updated to match with the native Apple's naming: before:

public enum CameraEngineSessionPreset {
    case photo
    case high
    case medium
    case low
    case res352x288
    case res640x480
    case res1280x720
    case res1920x1080
    case res3840x2160
    case frame960x540
    case frame1280x720
    case inputPriority

After

public enum CameraEngineSessionPreset {
    case photo
    case high
    case medium
    case low
    case cif352x288
    case vga640x480
    case hd1280x720
    case hd1920x1080
    case hd4k3840x2160
    case iFrame960x540
    case iFrame1280x720
    case inputPriority

Though some things are still needs to be finished. Such as getting rid of redundancy like:

.microphone(.unableToAdd(.microphone)),
.microphone(.unableToAdd(.camera)):

I'm a bit busy at the moment with main work at another project, will try to finish this and make PR asap, when I get a chance. Feel free to message me here I you found something wrong or you want to add or change. And many thanks to @remirobert for writing and sharing this library.

freemansion avatar Sep 17 '18 07:09 freemansion

The link to your Swift 4.1 version of Camera Engine is no longer working. Can you update it please?

JohnFlint avatar Mar 22 '19 13:03 JohnFlint

@JohnFlint sorry for the delayed response. Here is the link to the repo I made these changes - https://github.com/oozou/CameraEngine

Or alternatively, you can install fork with cocoapods pointing to our repo and specific commit using following command:

pod 'CameraEngine', :git => 'https://github.com/oozou/CameraEngine.git', :commit => 'f494fcdc1dab424c951106aad31a7fd4145718aa'

It's a bit hacky, but better than nothing. Hope I will find a time to clean it up and make a PR to origin repo.

freemansion avatar Mar 25 '19 05:03 freemansion

@freemansion thank you for the update! Your implementation works for me up to the point of terminating with "[AVAssetWriterInput markAsFinished] Cannot call method when status is 0'", at pressing the "stop recording" button. In my own Swift 4 implementation of CameraEngine I have the same issue even though I have (as you) implemented the suggestion from issue #55. Do you think this is due to an api update since you converted the CameraEngine?

JohnFlint avatar Mar 25 '19 11:03 JohnFlint

@JohnFlint does this issue reproducible at the repo example app?

freemansion avatar Mar 26 '19 07:03 freemansion

For me the error is reproduced in the example app from https://github.com/oozou/CameraEngine. Using Xcode Version 10.2 (10E125) and after only changing developer team, each time I run the app, select mode Video, switch camera twice (as the view does not show camera input before the first switch), press start recording, and press stop recording it terminates as described.

I fetched https://github.com/bartleby/CameraEngine and converted from 4.0 to 4.2 and was actually able to run the example as described and save video, and here without the need for switching camera. However this app did not allow to take more than one photo and at the second terminated with "[AVCapturePhotoOutput capturePhotoWithSettings:delegate:] Settings may not be re-used". As video is my main use of this framework I am at the moment limping on with this version.

JohnFlint avatar Mar 27 '19 14:03 JohnFlint

@JohnFlint a change as at this commit probably is the fix for error [AVCapturePhotoOutput capturePhotoWithSettings:delegate:] Settings may not be re-used". I remember I had the same issue.

freemansion avatar Mar 27 '19 14:03 freemansion