Skip to content

RtcPlayer ​

拉流播放器核心类。

构造函数 ​

typescript
new RtcPlayer(options: RtcPlayerOptions)

配置项(RtcPlayerOptions) ​

属性 ​

属性类型必填说明
urlstring是WebRTC 播放地址
apistring是信令服务器地址
targetHTMLVideoElement | HTMLAudioElement | HTMLCanvasElement否渲染目标元素(video/audio/canvas)
mutedboolean否目标元素是否静音,默认 true
media'audio' | 'video' | 'all'否媒体类型,默认 all
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

方法 ​

play() 异步 ​

开始拉流。

typescript
await player.play();

switchStream(url) 异步 ​

切换拉流地址。

typescript
await player.switchStream('webrtc://localhost/live/newstream');

use(plugin) 同步 ​

注册并安装插件,返回实例自身,支持链式调用。

typescript
import { createPlayerLoggerPlugin } from '@webrtc-engine/plugin-logger';

player.use(createPlayerLoggerPlugin());

详细用法请参考插件系统。

unuse(name) 同步 ​

卸载指定名称的插件。

typescript
player.unuse('player-logger');

destroy() ​

销毁实例。

typescript
player.destroy();

事件 ​

事件说明
track收到远端流
mediaready媒体进入可播放状态
stateRTC 状态变化(connecting/connected/...)
error运行时错误
icecandidate本地 ICE 候选产生
iceconnectionstateICE 连接状态更新
icegatheringstateICE 收集状态更新
reconnecting自动重连尝试开始
reconnectfailed自动重连耗尽(达到最大重试次数)
reconnected自动重连成功
signalingerror信令请求异常

使用示例 ​

typescript
import { RtcPlayer } from '@webrtc-engine/core';

const player = new RtcPlayer({
  url: 'webrtc://localhost/live/livestream',
  api: 'http://localhost:1985/rtc/v1/play/',
  target: videoElement,
});

player.on('track', ({ stream }) => (video.srcObject = stream));
player.on('state', (state) => console.log(state));
player.play();

基于 MIT 许可证发布