Skip to content

Commit 72dff1b

Browse files
authored
[camera] Sample buffer handling on session queue (flutter#4709)
1 parent 60982c3 commit 72dff1b

File tree

7 files changed

+59
-4
lines changed

7 files changed

+59
-4
lines changed

packages/camera/camera/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 0.9.4+9
22

3+
* iOS performance improvement by moving sample buffer handling from the main queue to a background session queue.
34
* Minor iOS internal code cleanup related to camera class and its delegate.
45
* Minor iOS internal code cleanup related to resolution preset, video format, focus mode, exposure mode and device orientation.
56
* Minor iOS internal code cleanup related to flash mode.

packages/camera/camera/example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
E0C6E2012770F01A00EA6AA3 /* ThreadSafeTextureRegistryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E0C6E1FE2770F01A00EA6AA3 /* ThreadSafeTextureRegistryTests.m */; };
2727
E0C6E2022770F01A00EA6AA3 /* ThreadSafeEventChannelTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E0C6E1FF2770F01A00EA6AA3 /* ThreadSafeEventChannelTests.m */; };
2828
E0F95E3D27A32AB900699390 /* CameraPropertiesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E0F95E3C27A32AB900699390 /* CameraPropertiesTests.m */; };
29+
E0F95E4427A36B9200699390 /* SampleBufferQueueTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E0F95E4327A36B9200699390 /* SampleBufferQueueTests.m */; };
2930
E487C86026D686A10034AC92 /* CameraPreviewPauseTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E487C85F26D686A10034AC92 /* CameraPreviewPauseTests.m */; };
3031
F6EE622F2710A6FC00905E4A /* MockFLTThreadSafeFlutterResult.m in Sources */ = {isa = PBXBuildFile; fileRef = F6EE622E2710A6FC00905E4A /* MockFLTThreadSafeFlutterResult.m */; };
3132
/* End PBXBuildFile section */
@@ -86,6 +87,7 @@
8687
E0C6E1FE2770F01A00EA6AA3 /* ThreadSafeTextureRegistryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ThreadSafeTextureRegistryTests.m; sourceTree = "<group>"; };
8788
E0C6E1FF2770F01A00EA6AA3 /* ThreadSafeEventChannelTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ThreadSafeEventChannelTests.m; sourceTree = "<group>"; };
8889
E0F95E3C27A32AB900699390 /* CameraPropertiesTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CameraPropertiesTests.m; sourceTree = "<group>"; };
90+
E0F95E4327A36B9200699390 /* SampleBufferQueueTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SampleBufferQueueTests.m; sourceTree = "<group>"; };
8991
E487C85F26D686A10034AC92 /* CameraPreviewPauseTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CameraPreviewPauseTests.m; sourceTree = "<group>"; };
9092
F63F9EED27143B19002479BF /* MockFLTThreadSafeFlutterResult.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MockFLTThreadSafeFlutterResult.h; sourceTree = "<group>"; };
9193
F6EE622E2710A6FC00905E4A /* MockFLTThreadSafeFlutterResult.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MockFLTThreadSafeFlutterResult.m; sourceTree = "<group>"; };
@@ -122,6 +124,7 @@
122124
E0C6E1FF2770F01A00EA6AA3 /* ThreadSafeEventChannelTests.m */,
123125
E0C6E1FD2770F01A00EA6AA3 /* ThreadSafeMethodChannelTests.m */,
124126
E0C6E1FE2770F01A00EA6AA3 /* ThreadSafeTextureRegistryTests.m */,
127+
E0F95E4327A36B9200699390 /* SampleBufferQueueTests.m */,
125128
E01EE4A72799F3A5008C1950 /* QueueHelperTests.m */,
126129
E487C85F26D686A10034AC92 /* CameraPreviewPauseTests.m */,
127130
F6EE622E2710A6FC00905E4A /* MockFLTThreadSafeFlutterResult.m */,
@@ -390,6 +393,7 @@
390393
isa = PBXSourcesBuildPhase;
391394
buildActionMask = 2147483647;
392395
files = (
396+
E0F95E4427A36B9200699390 /* SampleBufferQueueTests.m in Sources */,
393397
03F6F8B226CBB4670024B8D3 /* ThreadSafeFlutterResultTests.m in Sources */,
394398
033B94BE269C40A200B4DF97 /* CameraMethodChannelTests.m in Sources */,
395399
E0F95E3D27A32AB900699390 /* CameraPropertiesTests.m in Sources */,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
@import camera;
6+
@import camera.Test;
7+
@import AVFoundation;
8+
@import XCTest;
9+
#import <OCMock/OCMock.h>
10+
11+
@interface SampleBufferQueueTests : XCTestCase
12+
13+
@end
14+
15+
@implementation SampleBufferQueueTests
16+
17+
- (void)testSampleBufferCallbackQueueMustBeCaptureSessionQueue {
18+
id inputMock = OCMClassMock([AVCaptureDeviceInput class]);
19+
OCMStub([inputMock deviceInputWithDevice:[OCMArg any] error:[OCMArg setTo:nil]])
20+
.andReturn(inputMock);
21+
22+
id sessionMock = OCMClassMock([AVCaptureSession class]);
23+
OCMStub([sessionMock alloc]).andReturn(sessionMock);
24+
OCMStub([sessionMock addInputWithNoConnections:[OCMArg any]]); // no-op
25+
OCMStub([sessionMock canSetSessionPreset:[OCMArg any]]).andReturn(YES);
26+
27+
dispatch_queue_t captureSessionQueue = dispatch_queue_create("testing", NULL);
28+
FLTCam *cam = [[FLTCam alloc] initWithCameraName:@"camera"
29+
resolutionPreset:@"medium"
30+
enableAudio:true
31+
orientation:UIDeviceOrientationPortrait
32+
captureSessionQueue:captureSessionQueue
33+
error:nil];
34+
XCTAssertEqual(captureSessionQueue, cam.captureVideoOutput.sampleBufferCallbackQueue);
35+
}
36+
37+
@end

packages/camera/camera/ios/Classes/CameraPlugin.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ framework module camera {
88
header "CameraPlugin_Test.h"
99
header "CameraProperties.h"
1010
header "FLTCam.h"
11+
header "FLTCam_Test.h"
1112
}
1213
}

packages/camera/camera/ios/Classes/FLTCam.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
#import "FLTCam.h"
6+
#import "FLTCam_Test.h"
67

78
@import CoreMotion;
89
#import <libkern/OSAtomic.h>
@@ -114,7 +115,6 @@ @interface FLTCam () <AVCaptureVideoDataOutputSampleBufferDelegate,
114115
@property(readonly, nonatomic) AVCaptureSession *captureSession;
115116

116117
@property(readonly, nonatomic) AVCapturePhotoOutput *capturePhotoOutput API_AVAILABLE(ios(10));
117-
@property(readonly, nonatomic) AVCaptureVideoDataOutput *captureVideoOutput;
118118
@property(readonly, nonatomic) AVCaptureInput *captureVideoInput;
119119
@property(readonly) CVPixelBufferRef volatile latestPixelBuffer;
120120
@property(readonly, nonatomic) CGSize captureSize;
@@ -184,7 +184,7 @@ - (instancetype)initWithCameraName:(NSString *)cameraName
184184
_captureVideoOutput.videoSettings =
185185
@{(NSString *)kCVPixelBufferPixelFormatTypeKey : @(_videoFormat)};
186186
[_captureVideoOutput setAlwaysDiscardsLateVideoFrames:YES];
187-
[_captureVideoOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
187+
[_captureVideoOutput setSampleBufferDelegate:self queue:captureSessionQueue];
188188

189189
AVCaptureConnection *connection =
190190
[AVCaptureConnection connectionWithInputPorts:_captureVideoInput.ports
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import "FLTCam.h"
6+
7+
// APIs exposed for unit testing.
8+
@interface FLTCam ()
9+
10+
@property(readonly, nonatomic) AVCaptureVideoDataOutput *captureVideoOutput;
11+
12+
@end

packages/camera/camera/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: A Flutter plugin for controlling the camera. Supports previewing
44
Dart.
55
repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
7-
version: 0.9.4+8
7+
version: 0.9.4+9
88

99
environment:
1010
sdk: ">=2.14.0 <3.0.0"

0 commit comments

Comments
 (0)