Skip to content

Commit c06a3f2

Browse files
authored
[camera_avfoundation] Replace deprecated high-resolution capture APIs (flutter#12372)
Replaces `isHighResolutionCaptureEnabled`/`isHighResolutionPhotoEnabled` (`API_DEPRECATED`, iOS 16) with `maxPhotoDimensions`, falling back to the deprecated APIs below iOS 16. Part of flutter#160687 Also fixes the resolution setting being silently dropped for HEIF captures at `ResolutionPreset.max`, found while making this change: the HEIF branch of `captureToFile` replaces `settings` with a new instance after the flag was set on the discarded one. Regression tests are included for both the HEIF and JPEG-quality branches. Deprecation source (Xcode 26.2 SDK headers, `API_DEPRECATED("Use maxPhotoDimensions instead.", ios(10.0, 16.0))`): - https://developer.apple.com/documentation/avfoundation/avcapturephotooutput/ishighresolutioncaptureenabled - https://developer.apple.com/documentation/avfoundation/avcapturephotosettings/ishighresolutionphotoenabled ## Pre-Review Checklist
1 parent e362bb9 commit c06a3f2

10 files changed

Lines changed: 242 additions & 10 deletions

File tree

‎packages/camera/camera_avfoundation/CHANGELOG.md‎

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

3+
* Replaces the deprecated `isHighResolutionCaptureEnabled`/`isHighResolutionPhotoEnabled` with
4+
`maxPhotoDimensions` on iOS 16 and later, falling back to the deprecated APIs on older versions.
5+
* Fixes the maximum resolution setting being ignored for HEIF captures when using
6+
`ResolutionPreset.max`.
37
* Updates minimum supported SDK version to Flutter 3.41/Dart 3.11.
48

59
## 0.10.3
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2013 The Flutter Authors
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 AVFoundation
6+
import XCTest
7+
8+
@testable import camera_avfoundation
9+
10+
/// Includes test cases related to the `start` method of the Camera class.
11+
final class CameraStartTests: XCTestCase {
12+
func testStart_setsMaxPhotoDimensionsToTheLargestSupportedByTheActiveFormat() throws {
13+
guard #available(iOS 16.0, *) else {
14+
throw XCTSkip("maxPhotoDimensions requires iOS 16.")
15+
}
16+
17+
let activeFormatMock = MockCaptureDeviceFormat()
18+
activeFormatMock.supportedMaxPhotoDimensions = [
19+
CMVideoDimensions(width: 1920, height: 1080),
20+
CMVideoDimensions(width: 4032, height: 3024),
21+
CMVideoDimensions(width: 3264, height: 2448),
22+
]
23+
let captureDeviceMock = MockCaptureDevice()
24+
captureDeviceMock.activeFormatStub = { activeFormatMock }
25+
26+
let configuration = CameraTestUtils.createTestCameraConfiguration()
27+
configuration.videoCaptureDeviceFactory = { _ in captureDeviceMock }
28+
let cam = CameraTestUtils.createTestCamera(configuration)
29+
30+
let mockOutput = MockCapturePhotoOutput()
31+
cam.capturePhotoOutput = mockOutput
32+
33+
cam.start()
34+
35+
XCTAssertEqual(mockOutput.maxPhotoDimensions.width, 4032)
36+
XCTAssertEqual(mockOutput.maxPhotoDimensions.height, 3024)
37+
}
38+
}

‎packages/camera/camera_avfoundation/darwin/Tests/Mocks/MockCaptureDeviceFormat.swift‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ final class MockCaptureDeviceFormat: NSObject, CaptureDeviceFormat {
2525
/// The array of frame rate ranges supported by the video format.
2626
var flutterVideoSupportedFrameRateRanges: [FrameRateRange] = []
2727

28+
/// The photo dimensions supported by the video format.
29+
var supportedMaxPhotoDimensions: [CMVideoDimensions] = []
30+
2831
override init() {
2932
super.init()
3033

‎packages/camera/camera_avfoundation/darwin/Tests/Mocks/MockCapturePhotoOutput.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ final class MockCapturePhotoOutput: NSObject, CapturePhotoOutput {
1212
var avOutput = AVCapturePhotoOutput()
1313
var availablePhotoCodecTypes: [AVVideoCodecType] = []
1414
var isHighResolutionCaptureEnabled = false
15+
var maxPhotoDimensions = CMVideoDimensions()
1516
var supportedFlashModes: [AVCaptureDevice.FlashMode] = []
1617

1718
// Stub that is called when the corresponding public method is called.

‎packages/camera/camera_avfoundation/darwin/Tests/PhotoCaptureTests.swift‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,157 @@ final class PhotoCaptureTests: XCTestCase {
245245

246246
waitForExpectations(timeout: 30, handler: nil)
247247
}
248+
249+
func testCaptureToFile_setsMaxPhotoDimensionsWhenResolutionPresetIsMax() throws {
250+
guard #available(iOS 16.0, *) else {
251+
throw XCTSkip("maxPhotoDimensions requires iOS 16.")
252+
}
253+
254+
let settingsExpectation = expectation(
255+
description: "Must set maxPhotoDimensions to the output's configured value.")
256+
let captureSessionQueue = DispatchQueue(label: "capture_session_queue")
257+
captureSessionQueue.setSpecific(
258+
key: captureSessionQueueSpecificKey, value: captureSessionQueueSpecificValue)
259+
260+
let configuration = CameraTestUtils.createTestCameraConfiguration()
261+
configuration.captureSessionQueue = captureSessionQueue
262+
configuration.mediaSettings = CameraTestUtils.createDefaultMediaSettings(
263+
resolutionPreset: PlatformResolutionPreset.max)
264+
let cam = CameraTestUtils.createTestCamera(configuration)
265+
266+
let expectedDimensions = CMVideoDimensions(width: 4032, height: 3024)
267+
let mockOutput = MockCapturePhotoOutput()
268+
mockOutput.maxPhotoDimensions = expectedDimensions
269+
mockOutput.capturePhotoWithSettingsStub = { settings, photoDelegate in
270+
XCTAssertEqual(settings.maxPhotoDimensions.width, expectedDimensions.width)
271+
XCTAssertEqual(settings.maxPhotoDimensions.height, expectedDimensions.height)
272+
let delegate = cam.inProgressSavePhotoDelegates[settings.uniqueID]
273+
let ioQueue = DispatchQueue(label: "io_queue")
274+
ioQueue.async {
275+
delegate?.completionHandler(delegate?.filePath, nil)
276+
}
277+
settingsExpectation.fulfill()
278+
}
279+
cam.capturePhotoOutput = mockOutput
280+
281+
captureSessionQueue.async {
282+
cam.captureToFile { _ in }
283+
}
284+
285+
waitForExpectations(timeout: 30, handler: nil)
286+
}
287+
288+
func testCaptureToFile_doesNotSetMaxPhotoDimensionsWhenResolutionPresetIsNotMax() throws {
289+
guard #available(iOS 16.0, *) else {
290+
throw XCTSkip("maxPhotoDimensions requires iOS 16.")
291+
}
292+
293+
let settingsExpectation = expectation(
294+
description: "Must leave maxPhotoDimensions unset for non-max presets.")
295+
let captureSessionQueue = DispatchQueue(label: "capture_session_queue")
296+
captureSessionQueue.setSpecific(
297+
key: captureSessionQueueSpecificKey, value: captureSessionQueueSpecificValue)
298+
let cam = createCam(with: captureSessionQueue)
299+
300+
let mockOutput = MockCapturePhotoOutput()
301+
mockOutput.maxPhotoDimensions = CMVideoDimensions(width: 4032, height: 3024)
302+
mockOutput.capturePhotoWithSettingsStub = { settings, photoDelegate in
303+
XCTAssertEqual(settings.maxPhotoDimensions.width, 0)
304+
XCTAssertEqual(settings.maxPhotoDimensions.height, 0)
305+
let delegate = cam.inProgressSavePhotoDelegates[settings.uniqueID]
306+
let ioQueue = DispatchQueue(label: "io_queue")
307+
ioQueue.async {
308+
delegate?.completionHandler(delegate?.filePath, nil)
309+
}
310+
settingsExpectation.fulfill()
311+
}
312+
cam.capturePhotoOutput = mockOutput
313+
314+
captureSessionQueue.async {
315+
cam.captureToFile { _ in }
316+
}
317+
318+
waitForExpectations(timeout: 30, handler: nil)
319+
}
320+
321+
func testCaptureToFile_setsMaxPhotoDimensionsWhenFileFormatIsHeif() throws {
322+
guard #available(iOS 16.0, *) else {
323+
throw XCTSkip("maxPhotoDimensions requires iOS 16.")
324+
}
325+
326+
let settingsExpectation = expectation(
327+
description: "Must set maxPhotoDimensions on the HEIF settings instance.")
328+
let captureSessionQueue = DispatchQueue(label: "capture_session_queue")
329+
captureSessionQueue.setSpecific(
330+
key: captureSessionQueueSpecificKey, value: captureSessionQueueSpecificValue)
331+
332+
let configuration = CameraTestUtils.createTestCameraConfiguration()
333+
configuration.captureSessionQueue = captureSessionQueue
334+
configuration.mediaSettings = CameraTestUtils.createDefaultMediaSettings(
335+
resolutionPreset: PlatformResolutionPreset.max)
336+
let cam = CameraTestUtils.createTestCamera(configuration)
337+
cam.setImageFileFormat(PlatformImageFileFormat.heif)
338+
339+
let expectedDimensions = CMVideoDimensions(width: 4032, height: 3024)
340+
let mockOutput = MockCapturePhotoOutput()
341+
mockOutput.availablePhotoCodecTypes = [AVVideoCodecType.hevc]
342+
mockOutput.maxPhotoDimensions = expectedDimensions
343+
mockOutput.capturePhotoWithSettingsStub = { settings, photoDelegate in
344+
XCTAssertEqual(settings.maxPhotoDimensions.width, expectedDimensions.width)
345+
XCTAssertEqual(settings.maxPhotoDimensions.height, expectedDimensions.height)
346+
let delegate = cam.inProgressSavePhotoDelegates[settings.uniqueID]
347+
let ioQueue = DispatchQueue(label: "io_queue")
348+
ioQueue.async {
349+
delegate?.completionHandler(delegate?.filePath, nil)
350+
}
351+
settingsExpectation.fulfill()
352+
}
353+
cam.capturePhotoOutput = mockOutput
354+
355+
captureSessionQueue.async {
356+
cam.captureToFile { _ in }
357+
}
358+
359+
waitForExpectations(timeout: 30, handler: nil)
360+
}
361+
362+
func testCaptureToFile_setsMaxPhotoDimensionsWhenJpegQualityIsBelow100() throws {
363+
guard #available(iOS 16.0, *) else {
364+
throw XCTSkip("maxPhotoDimensions requires iOS 16.")
365+
}
366+
367+
let settingsExpectation = expectation(
368+
description: "Must set maxPhotoDimensions on the reduced-quality JPEG settings instance.")
369+
let captureSessionQueue = DispatchQueue(label: "capture_session_queue")
370+
captureSessionQueue.setSpecific(
371+
key: captureSessionQueueSpecificKey, value: captureSessionQueueSpecificValue)
372+
373+
let configuration = CameraTestUtils.createTestCameraConfiguration()
374+
configuration.captureSessionQueue = captureSessionQueue
375+
configuration.mediaSettings = CameraTestUtils.createDefaultMediaSettings(
376+
resolutionPreset: PlatformResolutionPreset.max)
377+
let cam = CameraTestUtils.createTestCamera(configuration)
378+
cam.setJpegImageQuality(80)
379+
380+
let expectedDimensions = CMVideoDimensions(width: 4032, height: 3024)
381+
let mockOutput = MockCapturePhotoOutput()
382+
mockOutput.maxPhotoDimensions = expectedDimensions
383+
mockOutput.capturePhotoWithSettingsStub = { settings, photoDelegate in
384+
XCTAssertEqual(settings.maxPhotoDimensions.width, expectedDimensions.width)
385+
XCTAssertEqual(settings.maxPhotoDimensions.height, expectedDimensions.height)
386+
let delegate = cam.inProgressSavePhotoDelegates[settings.uniqueID]
387+
let ioQueue = DispatchQueue(label: "io_queue")
388+
ioQueue.async {
389+
delegate?.completionHandler(delegate?.filePath, nil)
390+
}
391+
settingsExpectation.fulfill()
392+
}
393+
cam.capturePhotoOutput = mockOutput
394+
395+
captureSessionQueue.async {
396+
cam.captureToFile { _ in }
397+
}
398+
399+
waitForExpectations(timeout: 30, handler: nil)
400+
}
248401
}

‎packages/camera/camera_avfoundation/darwin/camera_avfoundation/Sources/camera_avfoundation/CaptureDeviceFormat.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ protocol CaptureDeviceFormat: NSObjectProtocol {
2020

2121
var formatDescription: CMFormatDescription { get }
2222
var flutterVideoSupportedFrameRateRanges: [FrameRateRange] { get }
23+
24+
/// Corresponds to the `supportedMaxPhotoDimensions` property of `AVCaptureDevice.Format`.
25+
@available(iOS 16.0, *)
26+
var supportedMaxPhotoDimensions: [CMVideoDimensions] { get }
2327
}
2428

2529
extension AVFrameRateRange: FrameRateRange {}

‎packages/camera/camera_avfoundation/darwin/camera_avfoundation/Sources/camera_avfoundation/CaptureOutput.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ protocol CapturePhotoOutput: CaptureOutput {
5656
/// Corresponds to the `isHighResolutionCaptureEnabled` property of `AVCapturePhotoOutput`
5757
var isHighResolutionCaptureEnabled: Bool { get set }
5858

59+
/// Corresponds to the `maxPhotoDimensions` property of `AVCapturePhotoOutput`
60+
@available(iOS 16.0, *)
61+
var maxPhotoDimensions: CMVideoDimensions { get set }
62+
5963
/// Corresponds to the `supportedFlashModes` property of `AVCapturePhotoOutput`
6064
var supportedFlashModes: [AVCaptureDevice.FlashMode] { get }
6165

‎packages/camera/camera_avfoundation/darwin/camera_avfoundation/Sources/camera_avfoundation/DefaultCamera.swift‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ final class DefaultCamera: NSObject, Camera {
179179
flashMode = captureDevice.hasFlash ? .auto : .off
180180

181181
capturePhotoOutput = AVCapturePhotoOutput()
182-
capturePhotoOutput.isHighResolutionCaptureEnabled = true
183182

184183
videoCaptureSession.automaticallyConfiguresApplicationAudioSession = false
185184
audioCaptureSession.automaticallyConfiguresApplicationAudioSession = false
@@ -495,10 +494,29 @@ final class DefaultCamera: NSObject, Camera {
495494
}
496495

497496
func start() {
497+
configureMaxPhotoDimensions()
498+
498499
videoCaptureSession.startRunning()
499500
audioCaptureSession.startRunning()
500501
}
501502

503+
/// Configures `capturePhotoOutput` to allow capturing at the active format's highest supported
504+
/// resolution. Must be called whenever `captureDevice` changes, since the active format's
505+
/// supported dimensions can change with it.
506+
private func configureMaxPhotoDimensions() {
507+
if #available(iOS 16.0, *) {
508+
// If the active format reports no supported dimensions, `capturePhotoOutput` keeps
509+
// AVFoundation's own default rather than being left unconfigured.
510+
if let maxSupportedDimensions = captureDevice.flutterActiveFormat.supportedMaxPhotoDimensions
511+
.max(by: { Int64($0.width) * Int64($0.height) < Int64($1.width) * Int64($1.height) })
512+
{
513+
capturePhotoOutput.maxPhotoDimensions = maxSupportedDimensions
514+
}
515+
} else {
516+
capturePhotoOutput.isHighResolutionCaptureEnabled = true
517+
}
518+
}
519+
502520
func stop() {
503521
videoCaptureSession.stopRunning()
504522
audioCaptureSession.stopRunning()
@@ -706,10 +724,6 @@ final class DefaultCamera: NSObject, Camera {
706724
func captureToFile(completion: @escaping (Result<String, any Error>) -> Void) {
707725
var settings = AVCapturePhotoSettings()
708726

709-
if mediaSettings.resolutionPreset == .max {
710-
settings.isHighResolutionPhotoEnabled = true
711-
}
712-
713727
let fileExtension: String
714728

715729
let isHEVCCodecAvailable = capturePhotoOutput.availablePhotoCodecTypes.contains(
@@ -727,9 +741,14 @@ final class DefaultCamera: NSObject, Camera {
727741
AVVideoQualityKey: CGFloat(imageQuality) / 100.0
728742
],
729743
])
730-
if mediaSettings.resolutionPreset == .max {
731-
settings.isHighResolutionPhotoEnabled = true
732-
}
744+
}
745+
}
746+
747+
if mediaSettings.resolutionPreset == .max {
748+
if #available(iOS 16.0, *) {
749+
settings.maxPhotoDimensions = capturePhotoOutput.maxPhotoDimensions
750+
} else {
751+
settings.isHighResolutionPhotoEnabled = true
733752
}
734753
}
735754

@@ -1208,6 +1227,8 @@ final class DefaultCamera: NSObject, Camera {
12081227
videoCaptureSession.addConnection(newConnection)
12091228
videoCaptureSession.commitConfiguration()
12101229

1230+
configureMaxPhotoDimensions()
1231+
12111232
completion(.success(()))
12121233
}
12131234

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
335A7B032F6B061D005902FE /* CameraZoomTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 335A7AE82F6B061D005902FE /* CameraZoomTests.swift */; };
1212
335A7B042F6B061D005902FE /* AvailableCamerasTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 335A7AD72F6B061D005902FE /* AvailableCamerasTests.swift */; };
1313
335A7B052F6B061D005902FE /* PhotoCaptureTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 335A7AFE2F6B061D005902FE /* PhotoCaptureTests.swift */; };
14+
3097B748B18C43AFBCA006FF /* CameraStartTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F0FB15D14F347278D8F9FB4 /* CameraStartTests.swift */; };
1415
335A7B062F6B061D005902FE /* SavePhotoDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 335A7B012F6B061D005902FE /* SavePhotoDelegateTests.swift */; };
1516
335A7B072F6B061D005902FE /* MockCameraDeviceDiscoverer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 335A7AED2F6B061D005902FE /* MockCameraDeviceDiscoverer.swift */; };
1617
335A7B082F6B061D005902FE /* MockFlutterTextureRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 335A7AF92F6B061D005902FE /* MockFlutterTextureRegistry.swift */; };
@@ -126,6 +127,7 @@
126127
335A7AFB2F6B061D005902FE /* MockGlobalEventApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockGlobalEventApi.swift; sourceTree = "<group>"; };
127128
335A7AFC2F6B061D005902FE /* MockWritableData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockWritableData.swift; sourceTree = "<group>"; };
128129
335A7AFE2F6B061D005902FE /* PhotoCaptureTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhotoCaptureTests.swift; sourceTree = "<group>"; };
130+
4F0FB15D14F347278D8F9FB4 /* CameraStartTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraStartTests.swift; sourceTree = "<group>"; };
129131
335A7AFF2F6B061D005902FE /* QueueUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueUtilsTests.swift; sourceTree = "<group>"; };
130132
335A7B002F6B061D005902FE /* SampleBufferTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SampleBufferTests.swift; sourceTree = "<group>"; };
131133
335A7B012F6B061D005902FE /* SavePhotoDelegateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SavePhotoDelegateTests.swift; sourceTree = "<group>"; };
@@ -178,6 +180,7 @@
178180
335A7AE22F6B061D005902FE /* CameraSettingsTests.swift */,
179181
335A7AE62F6B061D005902FE /* CameraSetDeviceOrientationTests.swift */,
180182
335A7AE72F6B061D005902FE /* CameraSetFlashModeTests.swift */,
183+
4F0FB15D14F347278D8F9FB4 /* CameraStartTests.swift */,
181184
335A7AE32F6B061D005902FE /* CameraTestUtils.swift */,
182185
335A7AE82F6B061D005902FE /* CameraZoomTests.swift */,
183186
335A7AFE2F6B061D005902FE /* PhotoCaptureTests.swift */,
@@ -415,6 +418,7 @@
415418
335A7B032F6B061D005902FE /* CameraZoomTests.swift in Sources */,
416419
335A7B042F6B061D005902FE /* AvailableCamerasTests.swift in Sources */,
417420
335A7B052F6B061D005902FE /* PhotoCaptureTests.swift in Sources */,
421+
3097B748B18C43AFBCA006FF /* CameraStartTests.swift in Sources */,
418422
335A7B062F6B061D005902FE /* SavePhotoDelegateTests.swift in Sources */,
419423
335A7B072F6B061D005902FE /* MockCameraDeviceDiscoverer.swift in Sources */,
420424
335A7B082F6B061D005902FE /* MockFlutterTextureRegistry.swift in Sources */,

‎packages/camera/camera_avfoundation/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera_avfoundation
22
description: iOS implementation of the camera plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_avfoundation
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
5-
version: 0.10.3
5+
version: 0.10.3+1
66

77
environment:
88
sdk: ^3.11.0

0 commit comments

Comments
 (0)