Skip to content

RtcPublisher

Used to publish local media streams to a streaming server.

Constructor

typescript
new RtcPublisher(options: RtcPublisherOptions)

Options (RtcPublisherOptions)

Properties

PropertyTypeRequiredDescription
urlstringYesWebRTC publishing URL
apistringYesSignaling server URL
sourceMediaSourceYesMedia source config
targetHTMLVideoElement | HTMLAudioElement | HTMLCanvasElementNoPreview render target element (video/audio/canvas)
mutedbooleanNoWhether target element is muted, default true
configRTCConfigurationNoWebRTC connection config
reconnectReconnectOptionsNoAuto reconnect behavior
iceIceOptionsNoICE behavior options (wait strategy and timeout)

ReconnectOptions

PropertyTypeRequiredDescription
enabledbooleanNoEnable auto reconnect, default false
maxRetriesnumberNoMaximum retry attempts, default 5
intervalnumberNoRetry interval in ms; initial interval when exponential backoff is enabled, default 2000
exponentialbooleanNoEnable exponential backoff, default false
maxIntervalnumberNoMaximum retry interval in ms
jitterRationumberNoRandom jitter ratio (0~1)
disconnectedTimeoutnumberNoFallback reconnect delay after entering disconnected (ms), default 5000

IceOptions

PropertyTypeRequiredDescription
waitForCompletebooleanNoWhether to wait for ICE gathering to complete before SDP exchange, default true
gatheringTimeoutnumberNoICE gathering timeout in ms, default 3000

Methods

start() async

Start publishing.

typescript
await publisher.start();

stop()

Stop publishing.

typescript
publisher.stop();

switchSource(source) async

Switch media source without rebuilding the connection.

typescript
await publisher.switchSource({ type: 'screen', audio: true });

getStream()

Get local MediaStream.

typescript
const stream = publisher.getStream();

destroy()

Destroy instance and release resources.

typescript
publisher.destroy();

Events

EventDescription
stateRTC state changed (connecting/connected/...)
streamstartPublishing started
streamstopPublishing stopped
streamingstatechangePublishing state changed (idle/connecting/streaming)
sourcechangeSource changed
permissiondeniedMedia permission denied
trackRemote stream received (echo/talkback scenarios)
trackendedLocal track ended
trackmutechangedLocal track mute state changed
errorRuntime error
icecandidateLocal ICE candidate generated
iceconnectionstateICE connection state updated
icegatheringstateICE gathering state updated
reconnectingAuto-reconnect attempt started
reconnectfailedAuto-reconnect exhausted (max retries reached)
reconnectedAuto-reconnect succeeded
signalingerrorSignaling request failed

See Publishing Guide for details.

基于 MIT 许可证发布