Skip to content

[camera_avfoundation] handle interruptions and use single offset #8982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/camera/camera/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.11.1+1

* Fixes delivering errors from onCameraError.

## 0.11.1

* Adds API support query for image streaming.
Expand Down
11 changes: 10 additions & 1 deletion packages/camera/camera/lib/src/camera_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class CameraValue {
}) {
return CameraValue(
isInitialized: isInitialized ?? this.isInitialized,
errorDescription: errorDescription,
errorDescription: errorDescription ?? this.errorDescription,
previewSize: previewSize ?? this.previewSize,
isRecordingVideo: isRecordingVideo ?? this.isRecordingVideo,
isTakingPicture: isTakingPicture ?? this.isTakingPicture,
Expand Down Expand Up @@ -353,6 +353,15 @@ class CameraController extends ValueNotifier<CameraValue> {
initializeCompleter.complete(event);
}));

_unawaited(CameraPlatform.instance
.onCameraError(_cameraId)
.first
.then((CameraErrorEvent event) {
value = value.copyWith(
errorDescription: event.description,
);
}));

await CameraPlatform.instance.initializeCamera(
_cameraId,
imageFormatGroup: imageFormatGroup ?? ImageFormatGroup.unknown,
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A Flutter plugin for controlling the camera. Supports previewing
Dart.
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.11.1
version: 0.11.1+1

environment:
sdk: ^3.6.0
Expand Down
14 changes: 14 additions & 0 deletions packages/camera/camera/test/camera_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,20 @@ void main() {
'This is a test error message',
)));
});

test('error from onCameraError is received', () async {
final CameraController cameraController = CameraController(
const CameraDescription(
name: 'cam',
lensDirection: CameraLensDirection.back,
sensorOrientation: 90),
ResolutionPreset.max);
await cameraController.initialize();

expect(cameraController.value.hasError, isTrue);
expect(cameraController.value.errorDescription,
mockOnCameraErrorEvent.description);
});
});
}

Expand Down
5 changes: 5 additions & 0 deletions packages/camera/camera_avfoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.9.18+14

* Handle video and audio interruptions and errors.
* Use a single time offset for both video and audio.

## 0.9.18+13

* Migrates test utils and mocks to Swift.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ enum CameraTestUtils {

/// Creates a test sample buffer.
/// @return a test sample buffer.
static func createTestSampleBuffer() -> CMSampleBuffer {
static func createTestSampleBuffer(
timestamp: CMTime = CMTime.zero, duration: CMTime = CMTimeMake(value: 1, timescale: 44100)
) -> CMSampleBuffer {
var pixelBuffer: CVPixelBuffer?
CVPixelBufferCreate(kCFAllocatorDefault, 100, 100, kCVPixelFormatType_32BGRA, nil, &pixelBuffer)

Expand All @@ -91,9 +93,9 @@ enum CameraTestUtils {
formatDescriptionOut: &formatDescription)

var timingInfo = CMSampleTimingInfo(
duration: CMTimeMake(value: 1, timescale: 44100),
presentationTimeStamp: CMTime.zero,
decodeTimeStamp: CMTime.invalid)
duration: duration,
presentationTimeStamp: timestamp,
decodeTimeStamp: .invalid)

var sampleBuffer: CMSampleBuffer?
CMSampleBufferCreateReadyWithImageBuffer(
Expand All @@ -108,24 +110,24 @@ enum CameraTestUtils {

/// Creates a test audio sample buffer.
/// @return a test audio sample buffer.
static func createTestAudioSampleBuffer() -> CMSampleBuffer? {
static func createTestAudioSampleBuffer(
timestamp: CMTime = .zero, duration: CMTime = CMTimeMake(value: 1, timescale: 44100)
) -> CMSampleBuffer {
var blockBuffer: CMBlockBuffer?
CMBlockBufferCreateWithMemoryBlock(
allocator: kCFAllocatorDefault,
memoryBlock: nil,
blockLength: 100,
blockLength: Int(duration.value),
blockAllocator: kCFAllocatorDefault,
customBlockSource: nil,
offsetToData: 0,
dataLength: 100,
dataLength: Int(duration.value),
flags: kCMBlockBufferAssureMemoryNowFlag,
blockBufferOut: &blockBuffer)

guard let blockBuffer = blockBuffer else { return nil }

var formatDescription: CMFormatDescription?
var basicDescription = AudioStreamBasicDescription(
mSampleRate: 44100,
mSampleRate: Float64(duration.timescale),
mFormatID: kAudioFormatLinearPCM,
mFormatFlags: 0,
mBytesPerPacket: 1,
Expand All @@ -148,13 +150,13 @@ enum CameraTestUtils {
var sampleBuffer: CMSampleBuffer?
CMAudioSampleBufferCreateReadyWithPacketDescriptions(
allocator: kCFAllocatorDefault,
dataBuffer: blockBuffer,
dataBuffer: blockBuffer!,
formatDescription: formatDescription!,
sampleCount: 1,
presentationTimeStamp: .zero,
sampleCount: CMItemCount(duration.value),
presentationTimeStamp: timestamp,
packetDescriptions: nil,
sampleBufferOut: &sampleBuffer)

return sampleBuffer
return sampleBuffer!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ final class MockCaptureSession: NSObject, FLTCaptureSession {
var stopRunningStub: (() -> Void)?
var canSetSessionPresetStub: ((AVCaptureSession.Preset) -> Bool)?

var captureSession = AVCaptureSession()
var _sessionPreset = AVCaptureSession.Preset.high
var inputs = [AVCaptureInput]()
var outputs = [AVCaptureOutput]()
var automaticallyConfiguresApplicationAudioSession = false
var running = true

var sessionPreset: AVCaptureSession.Preset {
get {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,146 @@ final class CameraSampleBufferTests: XCTestCase {
AVAudioSession.sharedInstance().category == .playAndRecord,
"Category should be PlayAndRecord.")
}

func testDidOutputSampleBufferMustUseSingleOffsetForVideoAndAudio() {
let (camera, writerMock, adaptorMock, inputMock, connectionMock) = createCamera()

var status = AVAssetWriter.Status.unknown
writerMock.startWritingStub = {
status = .writing
return true
}
writerMock.statusStub = {
return status
}

var appendedTime = CMTime.invalid

adaptorMock.appendStub = { buffer, time in
appendedTime = time
return true
}

inputMock.readyForMoreMediaData = true
inputMock.appendStub = { buffer in
appendedTime = CMSampleBufferGetPresentationTimeStamp(buffer)
return true
}

camera.startVideoRecording(completion: { error in }, messengerForStreaming: nil)

let appendVideoSample = { (time: Int64) in
camera.captureOutput(
camera.captureVideoOutput.avOutput,
didOutputSampleBuffer: CameraTestUtils.createTestSampleBuffer(
timestamp: CMTimeMake(value: time, timescale: 1),
duration: .invalid),
from: connectionMock)
}

let appendAudioSample = { (time: Int64, duration: Int64) in
camera.captureOutput(
nil,
didOutputSampleBuffer: CameraTestUtils.createTestAudioSampleBuffer(
timestamp: CMTimeMake(value: time, timescale: 1),
duration: CMTimeMake(value: duration, timescale: 1)),
from: connectionMock)
}

appendedTime = .invalid
camera.pauseVideoRecording()
camera.resumeVideoRecording()
appendVideoSample(1)
XCTAssertEqual(appendedTime, CMTimeMake(value: 1, timescale: 1))

appendedTime = .invalid
camera.pauseVideoRecording()
camera.resumeVideoRecording()
appendVideoSample(11)
XCTAssertEqual(appendedTime, .invalid)
appendVideoSample(12)
XCTAssertEqual(appendedTime, CMTimeMake(value: 2, timescale: 1))

appendedTime = .invalid
camera.pauseVideoRecording()
camera.resumeVideoRecording()
appendAudioSample(20, 2)
XCTAssertEqual(appendedTime, .invalid)
appendVideoSample(23)
XCTAssertEqual(appendedTime, CMTimeMake(value: 3, timescale: 1))

appendedTime = .invalid
camera.pauseVideoRecording()
camera.resumeVideoRecording()
appendVideoSample(28)
XCTAssertEqual(appendedTime, .invalid)
appendAudioSample(30, 2)
XCTAssertEqual(appendedTime, .invalid)
appendVideoSample(33)
XCTAssertEqual(appendedTime, .invalid)
appendAudioSample(32, 2)
XCTAssertEqual(appendedTime, CMTimeMake(value: 2, timescale: 1))
}

func testDidOutputSampleBufferMustConnectVideoAfterSessionInterruption() {
let (camera, writerMock, adaptorMock, inputMock, connectionMock) = createCamera()

var status = AVAssetWriter.Status.unknown
writerMock.startWritingStub = {
status = .writing
return true
}
writerMock.statusStub = {
return status
}

var appendedTime = CMTime.invalid

adaptorMock.appendStub = { buffer, time in
appendedTime = time
return true
}

inputMock.readyForMoreMediaData = true
inputMock.appendStub = { buffer in
appendedTime = CMSampleBufferGetPresentationTimeStamp(buffer)
return true
}

camera.startVideoRecording(completion: { error in }, messengerForStreaming: nil)

let appendVideoSample = { (time: Int64) in
camera.captureOutput(
camera.captureVideoOutput.avOutput,
didOutputSampleBuffer: CameraTestUtils.createTestSampleBuffer(
timestamp: CMTimeMake(value: time, timescale: 1),
duration: .invalid),
from: connectionMock)
}

let appendAudioSample = { (time: Int64, duration: Int64) in
camera.captureOutput(
nil,
didOutputSampleBuffer: CameraTestUtils.createTestAudioSampleBuffer(
timestamp: CMTimeMake(value: time, timescale: 1),
duration: CMTimeMake(value: duration, timescale: 1)),
from: connectionMock)
}

appendVideoSample(1)
appendAudioSample(1, 1)

NotificationCenter.default.post(
name: AVCaptureSession.wasInterruptedNotification,
object: camera.audioCaptureSession.captureSession)

appendedTime = .invalid
appendAudioSample(11, 1)
XCTAssertEqual(appendedTime, .invalid)
appendVideoSample(12)
XCTAssertEqual(appendedTime, CMTimeMake(value: 2, timescale: 1))
appendedTime = .invalid
appendAudioSample(12, 1)
XCTAssertEqual(appendedTime, CMTimeMake(value: 2, timescale: 1))
}
}
Loading