@@ -129,8 +129,7 @@ - (bool)prepareStartCapture
129129 AVCaptureDevice *device = nil ;
130130 if ( ! mDeviceUniqueId ) {
131131 device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
132- }
133- else {
132+ } else {
134133 device = [AVCaptureDevice deviceWithUniqueID: mDeviceUniqueId ];
135134 }
136135
@@ -153,8 +152,19 @@ - (bool)prepareStartCapture
153152 [mSession beginConfiguration ];
154153 if ( cinder::ivec2 ( mWidth , mHeight ) == cinder::ivec2 ( 640 , 480 ) )
155154 mSession .sessionPreset = AVCaptureSessionPreset640x480;
156- else if ( cinder::ivec2 ( mWidth , mHeight ) == cinder::ivec2 ( 1280 , 720 ) )
155+ /* else if( cinder::ivec2( mWidth, mHeight ) == cinder::ivec2( 960, 540 ) )
156+ mSession.sessionPreset = AVCaptureSessionPreset960x540;
157+ */ else if ( cinder::ivec2 ( mWidth , mHeight ) == cinder::ivec2 ( 1280 , 720 ) )
157158 mSession .sessionPreset = AVCaptureSessionPreset1280x720;
159+ else if ( cinder::ivec2 ( mWidth , mHeight ) == cinder::ivec2 ( 1920 , 1080 ) )
160+ if (@available (macOS 10.15 , *)) {
161+ mSession .sessionPreset = AVCaptureSessionPreset1920x1080;
162+ } else {
163+ // Fallback on earlier versions
164+ mSession .sessionPreset = AVCaptureSessionPreset1280x720;
165+ }
166+ else if ( glm::max (mWidth , mHeight ) > 900 )
167+ mSession .sessionPreset = AVCaptureSessionPresetHigh;
158168 else
159169 mSession .sessionPreset = AVCaptureSessionPresetMedium;
160170 [mSession commitConfiguration ];
@@ -199,23 +209,54 @@ - (void)startCapture
199209 if ( mIsCapturing )
200210 return ;
201211
202- @synchronized ( self ) {
212+ dispatch_async (dispatch_get_global_queue ( QOS_CLASS_DEFAULT, 0 ), ^(void ){
213+ if ( [self prepareStartCapture ] ) {
214+ // @synchronized( self ) {
215+ mWorkingPixelBuffer = 0 ;
216+ mHasNewFrame = false ;
217+
218+ [mSession startRunning ];
219+ mIsCapturing = true ;
220+ // }
221+ }
222+ });
223+
224+ /* @synchronized( self ) {
203225 if( [self prepareStartCapture] ) {
204226 mWorkingPixelBuffer = 0;
205227 mHasNewFrame = false;
206228
207229 mIsCapturing = true;
208230 [mSession startRunning];
209231 }
210- }
232+ }*/
211233}
212234
213235- (void )stopCapture
214236{
215237 if ( ! mIsCapturing )
216238 return ;
217239
218- @synchronized ( self ) {
240+ dispatch_async (dispatch_get_global_queue ( QOS_CLASS_DEFAULT, 0 ), ^(void ){
241+ @synchronized ( self ) {
242+ [mSession stopRunning ];
243+
244+ if ( mWorkingPixelBuffer ) {
245+ ::CVBufferRelease ( mWorkingPixelBuffer );
246+ mWorkingPixelBuffer = nullptr ;
247+ }
248+
249+ [mSession release ];
250+ mSession = nil ;
251+
252+ mIsCapturing = false ;
253+ mHasNewFrame = false ;
254+
255+ mCurrentFrame .reset ();
256+ }
257+ });
258+
259+ /* @synchronized( self ) {
219260 [mSession stopRunning];
220261
221262 if( mWorkingPixelBuffer ) {
@@ -230,14 +271,45 @@ - (void)stopCapture
230271 mHasNewFrame = false;
231272
232273 mCurrentFrame.reset();
233- }
274+ }*/
234275}
235276
236277- (bool )isCapturing
237278{
238279 return mIsCapturing ;
239280}
240281
282+ - (void )requirePermission : (void (^)(bool ))handler
283+ {
284+ #if defined( CINDER_COCOA_TOUCH )
285+ #else
286+ if (@available (macOS 10.14 , *))
287+ #endif
288+ {
289+ AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType: AVMediaTypeVideo];
290+ _completionHandler = [handler copy ];
291+
292+ if (status == AVAuthorizationStatusNotDetermined){ // not determined
293+ [AVCaptureDevice requestAccessForMediaType: AVMediaTypeVideo completionHandler: ^(BOOL granted) {
294+ _completionHandler (granted);
295+ [_completionHandler release ];
296+ _completionHandler = nil ;
297+ }];
298+ } else {
299+ if (status == AVAuthorizationStatusAuthorized) { // authorized
300+ _completionHandler (true );
301+ } else if (status == AVAuthorizationStatusDenied){ // denied
302+ _completionHandler (false );
303+ } else if (status == AVAuthorizationStatusRestricted){ // restricted
304+ _completionHandler (false );
305+ }
306+
307+ [_completionHandler release ];
308+ _completionHandler = nil ;
309+ }
310+ }
311+ }
312+
241313// Called initially when the camera is instantiated and then again (hypothetically) if the resolution ever changes
242314- (void )avCaptureInputPortFormatDescriptionDidChange : (NSNotification *)notification
243315{
@@ -338,4 +410,4 @@ - (int32_t)getCurrentFrameHeight
338410 return mExposedFrameHeight ;
339411}
340412
341- @end
413+ @end
0 commit comments