initialize method
Configure the native session and begin streaming preview frames.
Throws if camera or microphone permission is denied, or if another instance already holds the requested device or the audio session.
Implementation
Future<void> initialize() async {
_throwIfDisposed('initialize');
try {
final result = await XCameraBackend.instance.create(
cameraId: description.id,
enableAudio: value.enableAudio,
preset: resolutionPreset,
);
_handle = result.handle;
_textureId = result.textureId;
_eventsSub = XCameraBackend.instance
.events(result.handle)
.listen(
_onEvent,
onError: (Object error, StackTrace? stack) {
value = value.copyWith(errorDescription: error.toString());
},
);
await XCameraBackend.instance.initialize(result.handle);
final audioGranted = await XCameraBackend.instance
.audioPermissionGranted();
value = value.copyWith(
isInitialized: true,
previewSize: result.previewSize,
previewRotationQuarterTurns: result.previewRotationQuarterTurns,
audioPermissionGranted: audioGranted,
);
} catch (_) {
// initialize failed mid-way. The native side may have allocated
// a handle + claimed the camera/audio. Tear down what we have
// so the next attempt isn't blocked by a leaked device claim.
// [_handle] / [_eventsSub] are cleaned by [dispose] which
// tolerates the partial state.
await dispose();
rethrow;
}
}