SRTCEngine is the main entry point of the Swift SDK. It’s responsible for joining channels, leaving channels, creating local tracks, and configuring logging and audio processors.
Initialization
init()
Creates an SDK instance and completes the underlying WebRTC initialization.
Properties
logLevel
Sets the SDK log level.
Example:
language
SDK language, of type String? (a BCP 47 language tag such as "en" or "zh-CN"). Defaults to nil, which follows the system language. Available since 1.5.0.
It only affects the Accept-Language header on requests to the backend, that is, the language of backend business error messages (codes 1000–99999). The SDK’s own errors (SRTCError.message) are always in English and don’t change with it. Like logLevel, it’s a process-wide global setting; set it before joinChannel / enableIm.
currentLanguage
The language actually in effect, of type String, read-only: language if it’s set, otherwise the system’s preferred language, or "zh" if that can’t be determined. Available since 1.5.0.
For error codes and how to handle them, see Error codes.
audioCaptureProcessor
Audio processor applied after microphone capture (AudioProcessor?), usable for voice changing, noise suppression, and similar processing. There is one globally; the SDK’s own local level metering runs after it, and the two don’t interfere. Since 1.6.0 you no longer need it to compute your own volume—see activeSpeakerOptions.
The SDK holds the processor weakly, so your code must keep the instance; otherwise it is released immediately after being set. If the processor also conforms to AudioProcessingDeclaring (since 1.8.0), the SDK adjusts WebRTC’s built-in 3A according to its declared AudioProcessingConstraints when it’s set, and restores the defaults when it’s replaced or cleared. For the official AI noise suppression plugin, see AI noise suppression.
activeSpeakerOptions
ActiveSpeakerOptions, parameters for your own entry in the didChangeActiveSpeakers snapshot, available since 1.6.0: includeLocal (default true), interval (local sampling interval, default 0.5 s, read when joining a channel), and silenceThreshold (default 0.01). See Call quality and active speakers.
audioRenderProcessor
Processor applied to remote audio before playback, usable for playback-side audio enhancement.
channels
The list of channels that have been joined and are still live ([Channel]), ordered by join time. An empty array means you’re not in any channel.
defaultChannel
The default channel (Channel?): the earliest-joined one that’s still live; once you leave it, the next one takes its place. The no-argument
leaveChannel() applies to it. See Multi-channel for details.
Methods
joinChannel(token:options:)
Joins a channel and returns a Channel instance. You can call it multiple times to join several channels at once; each channel’s publishing, subscriptions, users, and
events are independent of each other. It throws alreadyJoined only when the same channel name has already been joined (or a join is in progress).
Returns:
Channel
Throws:
SRTCError.alreadyJoinedSRTCError.tokenExpiredSRTCError.tokenInvalid- Other network, signaling, and media streaming connection errors
leaveChannel()
Leaves the default channel (the earliest-joined one that’s still live).
leaveChannel(_:)
Leaves the specified channel, equivalent to channel.leave(); other channels aren’t affected. With multiple channels we recommend this version,
to avoid ambiguity from “the default channel moving to the next one”.
createLocalMicTrack(preset:)
Creates a local microphone track.
Returns:
LocalMicTrack
createLocalCameraTrack(preset:)
Creates a local camera track.
Returns:
LocalCameraTrack
createLocalScreenTrack(preset:audioPreset:mode:)
Creates a local screen-sharing track.
Returns:
LocalScreenTrack
With iOS full-screen capture, a successful
startCapture() only means the SDK has started listening; video is transmitted only after the user starts the broadcast from the system UI.
The actual start / end is notified through TrackDelegate.screenBroadcastDidStart /
screenBroadcastDidFinish(_:reason:).createLocalScreenTrack(source:preset:audioPreset:excludedWindowIds:excludesCurrentApplication:)
Overload for macOS 12.3+ only, used to specify a display or window source.
Returns:
LocalScreenTrack
createLocalCustomVideoTrack(desc:)
Creates a custom video track, for scenarios where frames are pushed from outside.
Returns:
LocalVideoTrack
After calling it, you can inject CVPixelBuffer values through pushFrame(...).
Creating a track with a specified track ID (1.5.5+)
createLocalCustomVideoTrack(desc:trackId:), createLocalScreenTrack(preset:audioPreset:mode:trackId:), and, on macOS, createLocalScreenTrack(source:preset:audioPreset:excludedWindowIds:excludesCurrentApplication:trackId:) are the same as the methods of the same name, except for an extra trackId: String parameter that is used as the track ID instead of a randomly generated one.
Use them when the same feed switches capture sources: several track objects capture separately and are published one at a time, but always appear under the same track ID. With the Wangsu CDN, the stream name is derived from the track ID, and cloud recording only pulls the stream that existed when recording started; if the ID changes, recording treats the stream as interrupted. Unpublishing and then republishing under the same ID isn’t affected.
Only one object with a given ID can be published at a time: calling
publishLocalTrack while another one is still published throws invalidState. Unpublishing only affects the object that is currently published; calling unpublishLocalTrack on an old object that has been replaced returns immediately and doesn’t affect the new track.
createLocalCustomAudioTrack(desc:)
Creates a custom audio track, for scenarios with external PCM input.
Returns:
LocalAudioTrack
After calling it, you can inject AVAudioPCMBuffer values through pushAudioBuffer(...).
Video preprocessing
defaultVideoProcessors
The default video processor pipeline. Configure it once here, and every camera track created afterward picks it up automatically.
A camera track is created anew every time the camera is turned on (turning the camera off destroys it), so if you set processors only on the track, the effect is gone the next time the camera is turned on—this property exists for exactly that reason. It affects only tracks created afterward and doesn’t apply retroactively to existing tracks; custom video tracks aren’t affected (frames on that path are pushed by you via
pushFrame, so you decide whether they go through processors).
Virtual background
iOS only: starting with1.7.0, these members don’t exist on macOS; wrap code shared by both platforms in #if os(iOS). The following methods forward to SRTCVirtualBackground.shared, with a single process-wide state. Once enabled, it automatically applies to all camera tracks, and you don’t need to add anything to videoProcessors. For usage and tuning, see Virtual background.
Throws:
SRTCError.virtualBackgroundAlreadyInstalled, .virtualBackgroundNotInstalled, .virtualBackgroundModelNotFound(String), .virtualBackgroundSessionFailed(String)
Background blur and background replacement are mutually exclusive, and the later call wins; both are also remembered if called before installing, and take effect automatically once installation completes.
Pre-call network check
networkCheck(appId:serverUrl:timeoutMs:onItem:)
Pre-call network check, available since 1.10.0. Runs the checks in a fixed order, permission (network permission) → connectivity (network connection) → mtu → dns (DNS resolution) → server (API / signaling / media servers), calls onItem (on the main thread) as each one completes, and returns the result when all are done. Call it before joining: it doesn’t require joining a channel or token authentication. When everything is normal, it takes about 1–5 seconds.
Returns:
NetworkCheckResult (async throws)
May throw: SRTCError.invalidArgument (empty parameter), .alreadyJoined (a check is already running; only one can run at a time), .cancelled (cancelled by cancelNetworkCheck() or by cancelling the caller’s task)
The API check in
server requires server-side support for the pre-call network check. If the server hasn’t been upgraded, that check is fail (api_unreachable), and mtu and the signaling and media checks are skipped; other items are unaffected.networkCheck(token:timeoutMs:onItem:)
A convenience form for when you already have a join or IM token: it takes app_id and client_api from the token; everything else is the same as above. The token is only read for the addresses, not used for authentication, so an expired token works too. Throws SRTCError.invalidArgument if the token can’t be decoded. Available since 1.10.0.
cancelNetworkCheck()
Cancels the running network check; the pending networkCheck then throws SRTCError.cancelled. Does nothing if no check is running. Available since 1.10.0.