Skip to content

RtcPublisher ​

用于将本地媒体流推送到流媒体服务器。

构造函数 ​

typescript
new RtcPublisher(options: RtcPublisherOptions)

配置项(RtcPublisherOptions) ​

属性 ​

属性类型必填说明
urlstring是WebRTC 推流地址
apistring是信令服务器地址
sourceMediaSource是媒体源配置
targetHTMLVideoElement | HTMLAudioElement | HTMLCanvasElement否本地预览渲染目标元素(video/audio/canvas)
mutedboolean否目标元素是否静音,默认 true
configRTCConfiguration否WebRTC 连接配置
reconnectReconnectOptions否自动重连配置
iceIceOptions否ICE 行为配置(是否等待收集完成、收集超时等)

ReconnectOptions ​

属性类型必填说明
enabledboolean否是否开启自动重连,默认 false
maxRetriesnumber否最大重试次数,默认 5
intervalnumber否重试间隔(ms),指数退避时为初始间隔,默认 2000
exponentialboolean否是否启用指数退避,默认 false
maxIntervalnumber否重试最大间隔(ms)
jitterRationumber否随机抖动比例(0~1)
disconnectedTimeoutnumber否进入 disconnected 后的兜底重连延迟(ms),默认 5000

IceOptions ​

属性类型必填说明
waitForCompleteboolean否是否等待 ICE 收集完成后再交换 SDP,默认 true
gatheringTimeoutnumber否ICE 收集超时时间(ms),默认 3000

方法 ​

start() 异步 ​

开始推流。

typescript
await publisher.start();

stop() ​

停止推流。

typescript
publisher.stop();

switchSource(source) 异步 ​

切换输入源,无需重建连接。

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

getStream() ​

获取本地 MediaStream。

typescript
const stream = publisher.getStream();

destroy() ​

销毁实例,释放资源。

typescript
publisher.destroy();

事件 ​

事件说明
stateRTC 状态变化(connecting/connected/...)
streamstart推流开始
streamstop推流停止
streamingstatechange推流状态变化(idle/connecting/streaming)
sourcechange输入源切换
permissiondenied媒体权限被拒绝
track收到远端流(回声/对讲场景)
trackended本地轨道结束
trackmutechanged本地轨道静音状态变化
error运行时错误
icecandidate本地 ICE 候选产生
iceconnectionstateICE 连接状态更新
icegatheringstateICE 收集状态更新
reconnecting自动重连尝试开始
reconnectfailed自动重连耗尽(达到最大重试次数)
reconnected自动重连成功
signalingerror信令请求异常

详见 推流指南。

基于 MIT 许可证发布