Skip to main content
Version: 4.x.x

On / Off Camera - iOS

Any participant can turn their camera on or off in the room using below methods.

enableCam()​

  • By using enableCam() function of the Room class, local participant can publish video to other participants.

  • You can call this method when the local participant is not broadcasting any video to others.

  • You can pass customised video track in enableCam(customCameraTrack:) 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 the Room class, local participant can stop publishing video to other participants.

  • You can call this method when the local participant is broadcasting any video to others.

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_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 room, or after you leave it.
    • ERROR_ROOM_RECONNECTING (3065) while the room is reconnecting. The camera stays as it was. Call the method again after the room reconnects.
    • ERROR_NO_WEBCAM_DEVICE_FOUND (3051) when the device has no camera.
    • ERROR_ACTION_NOT_SUPPORTED_IN_MODE (3066) from enableCam() when the room is in RECV_ONLY or SIGNALLING_ONLY mode.
  • 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, such as ERROR_CAMERA_ACCESS_DENIED_OR_DISMISSED, only to onError(error:).
info

You can use the hardware-accelerated codec (H.264) with VideoSDK.createCameraVideoTrack().

Read room.isCamEnabled to know whether your camera is on.

@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​

extension RoomViewController: ParticipantEventListener {
//Callback for when the participant starts a stream
func onStreamEnabled(_ stream: RTCStream, forParticipant participant: Participant) {
if stream.kind == .video {
print("new video stream enabled")
}
}
//Callback for when the participant stops a stream
func onStreamDisabled(_ stream: RTCStream, forParticipant participant: Participant) {
if stream.kind == .video {
print("new video stream disabled")
}
}
}

Start with the back camera​

  • The camera opens on the front camera by default. To open it on the back camera, pass defaultCamera: .back to VideoSDK.createRoom(...). defaultCamera takes a FacingMode (.front or .back).

  • A custom track passed as customCameraTrack keeps its own facingMode.

  • To switch cameras while in the room, use changeCam(). See Change Input/Output Device.

do {
room = try VideoSDK.createRoom(
roomId: "<Id-of-room>",
token: "<Your-Token>",
participantName: "John Doe",
micEnabled: true,
camEnabled: true,
defaultCamera: .back
)
} catch {
print("Could not create the room: \(error)")
}

changeCam(customCameraTrack:)​

  • By using changeCam(customCameraTrack:) function of the Room class, local participant can replace the camera track with a new custom video track while in the room, for example to change its resolution.

  • If the camera is on, it is turned off and on again with the new track, so you get onStreamDisabled and then onStreamEnabled. If the camera is off, it is turned on with the new track.

  • The SDK keeps the track for later calls to enableCam(). If the call fails, the SDK stops the track.

Task {
do {
let track = try await VideoSDK.createCameraVideoTrack(encoderConfig: .h540p_w960p, facingMode: .front)
try await self.room?.changeCam(customCameraTrack: track)
} catch {
print("Could not change the camera track: \(error)")
}
}

API Reference​

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

Got a Question? Ask us on discord