Skip to content

bug: iOS crash when starting preview with cameraMode: true #367

@CoderMag

Description

@CoderMag

Bug Report

Capacitor Version

💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 8.4.0
  @capacitor/core: 8.4.0
  @capacitor/android: 8.4.0
  @capacitor/ios: 8.4.0

Installed Dependencies:

  @capacitor/android: not installed
  @capacitor/cli: 8.4.0
  @capacitor/core: 8.4.0
  @capacitor/ios: 8.4.0

Plugin Version

  @capgo/camera-preview: 8.4.7 

context(s)

ManualModel: false
AutoMode: false
CapgoCloud: false
OnPremise: false

Platform(s)

iOS

Current Behavior

Starting the camera preview with the iOS cameraMode: true option crashes the app in Xcode.

Crash:

Thread 3: "*** -[AVCaptureConnection setVideoOrientation:] Not supported - use -isVideoOrientationSupported"

The crash happens in CameraController.swift in this block:

// Add video output if in camera mode
if cameraMode, let fileVideoOutput = self.fileVideoOutput, captureSession.canAddOutput(fileVideoOutput) {
    captureSession.addOutput(fileVideoOutput)
    // Set orientation immediately
    fileVideoOutput.connections.forEach { $0.videoOrientation = videoOrientation }
}

It looks like videoOrientation is being set on every AVCaptureConnection, even though not all connections necessarily support video orientation.

Expected Behavior

The plugin should not crash when video output is attached during preview startup.

Before setting videoOrientation, the iOS implementation should check whether the connection supports it:

fileVideoOutput.connections.forEach { connection in
    if connection.isVideoOrientationSupported {
        connection.videoOrientation = videoOrientation
    }
}

Alternatively, it should only set orientation on the actual video connection.

Code Reproduction

Minimal Capacitor usage:

import { CameraPreview } from '@capgo/camera-preview';

await CameraPreview.start({
  aspectRatio: '16:9',
  positioning: 'center',
  toBack: true,
  disableAudio: false,
  cameraMode: true,
});

On iOS, this reaches the native cameraMode path and crashes while setting videoOrientation on fileVideoOutput.connections.

Additional Context

The public TypeScript definitions document enableVideoMode?: boolean, but the iOS native implementation appears to read cameraMode:

let cameraMode = call.getBool("cameraMode") ?? false

and then uses it to decide whether to attach AVCaptureMovieFileOutput during preview startup.

Without cameraMode: true, recording can still work because captureVideo() has fallback logic that adds the video output later. However, using cameraMode: true currently crashes because this line does not guard isVideoOrientationSupported:

fileVideoOutput.connections.forEach { $0.videoOrientation = videoOrientation }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions