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 ofRoomclass, 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
onStreamEnabledevent ofParticipantEventListener.
disableCam()
- By using
disableCam()function ofRoomclass, the host can stop publishing their video to other hosts and audience members.
isCamEnabled
- The
isCamEnabledproperty ofRoomclass tells you whether your camera is on. Before you join, it tells you whether the camera will be turned on at join.
enableCam()anddisableCam()areasync throws. Call them withtry awaitinside aTask.- When a call is refused, it throws a
VideoSDKError:ERROR_ACTION_NOT_SUPPORTED_IN_MODE(3066) fromenableCam()when the participant is inRECV_ONLYmode, as audience members are, or inSIGNALLING_ONLYmode. Only hosts inSEND_AND_RECVmode 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 toonError(error:)ofRoomEventListener. For example, you getERROR_CAMERA_ACCESS_DENIED_OR_DISMISSED(3017) when the user turns down the permission prompt, andERROR_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.
- 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 theParticipantwith theRTCStream.
Events associated with disableCam
- Every Participant will receive a callback on
onStreamDisabled()of theParticipantwith theRTCStream.
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 thegetVideoPermissionStatus()method of theVideoSDKclass to check whether permission is granted. It only returns the status. To ask the user, usegetVideoPermission()as shown below.
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.
- Swift
let videoPermission = VideoSDK.getVideoPermissionStatus() // video permission status
Request Permissions (if necessary)
-
If permission status is
notDetermined, use thegetVideoPermission()method of theVideoSDKclass 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.
- Swift
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

