Skip to main content
Version: 4.x.x

On / Off Camera - iOS

This feature enables hosts to turn their cameras on or off to share their video stream with other hosts and audience members in the room. Only hosts (in SEND_AND_RECV mode) can broadcast their camera feed, while audience members (in RECV_ONLY mode) can view it in real time.

enableCam()​

  • By using enableCam() function of Room class, the host can publish their video to other hosts and audience members.

  • You can also pass a customised video track in enableCam() by using Custom Video Track.

  • Video stream of the participant can be accessed from the onStreamEnabled event of ParticipantEventListener.

disableCam()​

  • By using disableCam() function of Room class, the host can stop publishing their video to other hosts and audience members.

isCamEnabled​

  • The isCamEnabled property of Room class tells you whether your camera is on. Before you join, it tells you whether the camera will be turned on at join.
note
  • enableCam() and disableCam() are async throws. Call them with try await inside a Task.
  • When a call is refused, it throws a VideoSDKError:
    • ERROR_ACTION_NOT_SUPPORTED_IN_MODE (3066) from enableCam() when the participant is in RECV_ONLY mode, as audience members are, or in SIGNALLING_ONLY mode. Only hosts in SEND_AND_RECV mode can turn the camera on.
    • ERROR_ALREADY_IN_REQUESTED_STATE (3054) when the camera is already on, or already off.
    • ERROR_OPERATION_IN_PROGRESS (3055) while an earlier camera call is still running.
    • ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED (3035) before you join the live stream, or after you leave it.
    • ERROR_ROOM_RECONNECTING (3065) while the live stream is reconnecting. The camera stays as it was. Call the method again after it reconnects.
    • ERROR_NO_WEBCAM_DEVICE_FOUND (3051) when the device has no camera.
  • enableCam() returns before the camera opens. If the camera then fails to open, for example because the user denied camera access or another app is using the camera, the SDK turns the camera off and sends the error only to onError(error:) of RoomEventListener. For example, you get ERROR_CAMERA_ACCESS_DENIED_OR_DISMISSED (3017) when the user turns down the permission prompt, and ERROR_CAMERA_PERMISSION_DENIED_BY_OS (3019) when camera access is off in Settings.

Example​

Read isCamEnabled to decide whether to turn the camera on or off.

@IBAction func videoButtonTapped(_ sender: Any) {
Task {
do {
guard let room = self.room else { return }
if !room.isCamEnabled {
// enable camera
try await room.enableCam()
} else {
// disable camera
try await room.disableCam()
}
} catch let error as VideoSDKError where error == .ERROR_OPERATION_IN_PROGRESS {
// an earlier tap is still running
} catch {
print("Could not change the camera: \(error)")
}
}
}

Events associated with enableCam​

Events associated with disableCam​

Video Permissions​

Before a host can turn on their camera in the live stream, the app must have camera access.

  • Step 1: Begin by ensuring that your application has video permission to access user's camera device. Utilize the getVideoPermissionStatus() method of the VideoSDK class to check whether permission is granted. It only returns the status. To ask the user, use getVideoPermission() as shown below.
note

In case permissions are blocked by the user, the app's permission request dialogue cannot be re-rendered programmatically. In such cases, consider providing guidance to users on manually adjusting their application settings.

let videoPermission = VideoSDK.getVideoPermissionStatus() // video permission status

Request Permissions (if necessary)​

  • If permission status is notDetermined, use the getVideoPermission() method of the VideoSDK class to prompt users to grant access to their devices.

  • Calling the above method will automatically handle the notDetermined state and prompt the user to grant permission.

class LiveStreamViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// UI Setup
setupUI()

// prompt user to grant permission when the app starts
VideoSDK.getVideoPermission()

}

func setupUI() {
// your UI code
}
}

API Reference​

The API references for all the methods and events utilized in this guide are provided below.

Got a Question? Ask us on discord