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 theRoomclass, 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
onStreamEnabledevent ofParticipantEventListener.
disableCam()
-
By using
disableCam()function of theRoomclass, local participant can stop publishing video to other participants. -
You can call this method when the local participant is broadcasting any video to others.
enableCam()anddisableCam()areasync throws. Call them withtry awaitinside aTask.- 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) fromenableCam()when the room is inRECV_ONLYorSIGNALLING_ONLYmode.
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 asERROR_CAMERA_ACCESS_DENIED_OR_DISMISSED, only toonError(error:).
You can use the hardware-accelerated codec (H.264) with VideoSDK.createCameraVideoTrack().
Read room.isCamEnabled to know whether your camera is on.
- Swift
@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
- Every Participant will receive a callback on
onStreamEnabled()of theParticipantwithStreamobject.
Events associated with disableCam
- Every Participant will receive a callback on
onStreamDisabled()of theParticipantwithStreamobject.
- Swift
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: .backtoVideoSDK.createRoom(...).defaultCameratakes aFacingMode(.frontor.back). -
A custom track passed as
customCameraTrackkeeps its ownfacingMode. -
To switch cameras while in the room, use
changeCam(). See Change Input/Output Device.
- Swift
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 theRoomclass, 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
onStreamDisabledand thenonStreamEnabled. 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.
- Swift
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

