TRROSDK

TRROSDK

TRRO 类

Constructor

new TRROSDK(params)

Example
// 公有云模式,初始化最小配置
new TRROSDK({
 cloudMode: 'public',
 projectId: 'xxxxxxxx',
 remoteDeviceId: 'xxxxxxxx',
 password: '********',
});

// 私有云模式,初始化最小配置
new TRROSDK({
 serverIp: '1.1.1.1',
 projectId: 1111,
 remoteDeviceId: 'xxxxxxxx',
 password: '',
});
Parameters:
Name Type Description
params Object
Properties
Name Type Attributes Default Description
cloudMode CloudMode <optional>
"private"

云模式

取值:

  • public 公有云
  • public_intl 公有云国际站
  • private 私有云

serverIp string | Array.<string> <optional>

信令调度服务器的域名或 IP 地址,可传入多个信令调度服务器地址

  • 云模式 cloudMode 设置为 publicpublic_intl 时,该字段无效;
  • 云模式 cloudMode 设置为 private 时,必须提供该字段,否则将抛出错误。

port number <optional>

信令调度服务端口。
公有云模式下,该字段无效,私有云模式下默认为 3000/443(启用 https)。

https boolean <optional>
false

是否启用 https 协议,可选。
公有云模式下强制使用 https,该字段无效,私有云模式下默认为 false。

mqttOptions Object <optional>

MQTT 配置项,可选。

Properties
Name Type Attributes Default Description
url string | Array.<string> <optional>

MQTT 服务器的域名或 IP,可选,可传入多个 MQTT 服务器地址。
公有云模式下,一般无需配置该字段;私有云模式下默认与 serverIp 一致。

port number <optional>

MQTT 服务器端口,可选。
公有云模式下,一般无需配置该字段;私有云模式下默认为 2833。

forceLogin boolean <optional>
false

强制登录

  • 若配置为 true,则登录时会无视是否已有同名用户登录,并踢掉已登录的同名用户

projectId string | number

项目 ID

remoteDeviceId string

远端设备 ID

password string

远端设备密码

observer TRROMediaObserver <optional>

事件回调函数

Properties
Name Type Attributes Description
onMqttConnectionState function <optional>

返回值 { state: MQTTConnectionState; message?: string; }

onSignalingState function <optional>

返回值 { roomId: string; state: string; data?: Record<string, any>; }

onPeerConnectionState function <optional>

返回值 { srcUserId: string; code: number; state: ConnectionState; message?: string; data?: Record<string, any>; }

onConnected function <optional>

返回值 { deviceId: string }

onDisconnected function <optional>

返回值 { deviceId: string }

onTrackPublishState function <optional>

返回值 { state: 'publish' | 'unpublish'; deviceId: string; streamId: number; mediaType: MediaType }

onControlData function <optional>

返回值 { deviceId: string; data: string | ArrayBuffer; ordered: boolean }

onKick function <optional>

返回值 { code: number; reason: string }

onEvent function <optional>

返回 Event 类型 autoplay | webrtcStats | gatewayStats

loggerOption Object <optional>

SDK 日志配置

Properties
Name Type Attributes Default Description
showLog boolean <optional>
true

是否打印 SDK 日志

reportLog boolean <optional>
false

是否上报 SDK 日志

Methods

(async) init()

Description:
  • 初始化 MQTT 连接

Example
init()
Returns:

返回 Promise 对象。

Name Type Description
code number 0 success, 21 mqtt client connnect error, 26 mqtt client alreay connected, 100 request error
message string 描述信息,error 信息

setSessionPermissionToken(fieldDeviceId, token) → {void}

Description:
  • 设置指定现场设备的临时会话密钥。
    只有在公有云模式下,通过使用项目共享密钥生成的远端设备 ID 和密码,实现远端设备的自动注册和登录时,才需要调用该方法。

Example
setSessionPermissionToken('<FIELD-DEVICE-ID>', '<TOKEN>')
Parameters:
Name Type Description
fieldDeviceId string
token string
Returns:
Type
void

(async) connect(fieldDeviceId)

Description:
  • 连接指定现场设备

Example
connect('<FIELD-DEVICE-ID>')
Parameters:
Name Type Description
fieldDeviceId string

现场设备 ID

Returns:

返回 Promise 对象。

字段 类型 描述
code number 错误码
message string 描述信息

其中,code 和 message 字段的含义如下:

code message 描述
0 Already joined. 已连接
2 Joining 当前传入的现场设备正在连接中
20 Connect failed for public cloud mode, MQTT client not found. 公有云模式,MQTT 未初始化
21 Connect failed for public cloud mode: xxxxxx 公有云模式,MQTT 连接失败

(async) subscribe(params)

Description:
  • 订阅媒体流(拉流)

Example
subscribe({
  fieldDeviceId: 'xxxxxxxx',
  containers: [{
    streamId: 0,
    mount: '#test-div-0',
    callback: ({latency, metadata}) => {
     console.log(latency, metadata);
    }
  },{
    streamId: 1,
    mount: '#test-div-1',
    callback: ({latency, metadata}) => {
     console.log(latency, metadata);
    }
  }],
 audioContainer: '#test-div-audio'
})
Parameters:
Name Type Description
params Object
Properties
Name Type Attributes Description
fieldDeviceId string

现场设备 ID

containers Array.<Object>
Properties
Name Type Description
streamId number

视频流流号

mount string

视频流挂载节点,以 ID 形式,#test-div-0,SDK 会在该节点下自动创建媒体元素并播放流。

callback function

latency 为端到端延迟,metadata 为 requestVideoFrameCallback 回调的 metadata

audioContainer string <optional>

音频流挂载节点,以 ID 形式,#test-div-0。SDK 会在该节点下自动创建媒体元素并播放流。若不传该字段,则不拉取音频流。若不传 containers 字段,则该字段无效,订阅接口返回错误码。

incremental boolean <optional>

是否增量订阅,默认为 false,即每次调用订阅接口传入的参数,会全量覆盖现有的订阅状态。若为 true,则会增量订阅,即保持现有流的订阅状态并新增订阅的传入的流,或者更新已订阅的流的相关参数(如挂载节点)

Returns:

返回 Promise 对象。

Name Type Description
code number 0 success, 1 unjoined, 2 joining, 3 failed, 4 streams not published, 5 empty containers, 20 mqtt client not found, 21 mqtt client not connected, 100 request error
message string 描述信息,error 信息

(async) unsubscribe(params)

Description:
  • 取消订阅 track (拉流)

Example
unsubscribe({ fieldDeviceId: 'xxxxxxxx', streamIds: [0] })
Parameters:
Name Type Description
params Object
Properties
Name Type Attributes Description
fieldDeviceId string

现场设备 ID

streamIds Array.<number>

需要取消订阅的视频流流号

audioStream boolean <optional>

是否取消订阅音频流,默认为 false

Returns:

返回 Promise 对象。

Name Type Description
code number 0 success, 1 unjoined, 2 joining, 5 empty streamIds,20 mqtt client not found, 21 mqtt client not connected, 100 request error
message string 描述信息,error 信息

(async) publish(params)

Description:
  • 发布音频流(推流)
    只有在成功订阅了视频流的情况下,才能发布音频流,若取消订阅了所有视频流,则 SDK 会自动取消发布音频流,并通过 onEvent 回调通知
    一个 SDK 实例只允许发布一个音频流
    若使用 server 模式,需先调用 requestPermission 申请 master 权限,否则无法推流

Example
try {
  // 初始化 SDK 实例
  const client = new TRROSDK({ ... });
  // 连接指定现场设备
  await client.connect('xxxxxxxx');
  // 订阅视频流
  await client.subscribe({ fieldDeviceId: 'xxxxxxxx', containers: [{ streamId: 0, mount: '#test-div-0' }] });
  // 获取本地音频流
  const stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });
  [audioTrack] = stream.getAudioTracks()
  // 发布音频流
  await client.publish({ audioTrack });
} catch () {}
Parameters:
Name Type Description
params Object
Properties
Name Type Description
audioTrack MediaStreamTrack

音频流。需要业务层自行通过 navigator.mediaDevices.getUserMedia() 等手段获取 MediaStreamTrack 对象。·

Returns:

返回 Promise 对象。

Name Type Description
code number 0 success, 1 unjoined, 2 joining, 3 failed, 4 streams not published, 5 empty containers, 20 mqtt client not found, 21 mqtt client not connected, 100 request error
message string 描述信息,error 信息

(async) unpublish()

Description:
  • 取消发布音频流(取消推流)

Example
unpublish();
Returns:

返回 Promise 对象。

Name Type Description
code number 0 success, 1 unjoined, 2 joining, 3 failed, 4 streams not published, 5 empty containers, 20 mqtt client not found, 21 mqtt client not connected, 100 request error
message string 描述信息,error 信息

(async) muteAudio(params)

Description:
  • 静音或者取消静音指定音频流

Example
muteAudio({ deviceId: 'xxxxxxxx', muted: true })
Parameters:
Name Type Description
params Object
Properties
Name Type Description
deviceId string

现场设备 ID 或远端设备 ID(即 SDK 初始化时传入的 remoteDeviceId

muted boolean

true 表示静音,false 表示取消静音

Returns:

返回 Promise 对象。

Name Type Description
code number 0 success, 1 unjoined, 2 joining, 5 empty streamIds,20 mqtt client not found, 21 mqtt client not connected, 100 request error
message string 描述信息,error 信息

(async) disconnect(fieldDeviceId)

Description:
  • 断开与指定现场设备的连接

Example
disconnect('xxxxxxxx')
Parameters:
Name Type Description
fieldDeviceId string

现场设备

Returns:

返回 Promise 对象。

Name Type Description
code number 0 success, 1 unjoined, 3 failed, 100 request error
message string 描述信息,error 信息

(async) disconnectAll()

Description:
  • 断开与所有现场设备的连接

Example
disconnectAll()
Returns:

返回 Promise 对象。

Name Type Description
code number 0 success, 1 unjoined, 3 failed, 100 request error
message string 描述信息,error 信息

(async) destroy()

Description:
  • 销毁所有连接

Example
destroy()
Returns:

返回 Promise 对象。

Name Type Description
code number 0 success, 1 unjoined, 3 failed, 100 request error
message string 描述信息,error 信息

changeFieldDeviceEncodeConfig(fieldDeviceId, params)

Description:
  • 调整现场设备编码配置。
    调用此接口将临时更新现场设备的编码配置,现场设备重启后仍会恢复到原配置。
    配置更新只涉及传入的字段,未传入的配置项将保持当前状态。

Example
changeFieldDeviceEncodeConfig('xxxxxxxx', [{ streamId: 0, encodeConfig: { minBps: 1000, forceMin: true } }])
Parameters:
Name Type Description
fieldDeviceId string

现场设备 ID

params Array.<Object>
Properties
Name Type Description
streamId number

视频流流号

encodeConfig Object

视频流编码配置

Properties
Name Type Attributes Description
encodeWidth number <optional>

编码分辨率宽度

encodeHeight number <optional>

编码分辨率高度

minWidth number <optional>

最低分辨率宽度

fps number <optional>

视频期望帧率

minFps number <optional>

视频最低帧率

bps number <optional>

视频期望码率,单位 kbps

minBps number <optional>

视频最低码率,单位 kbps

forceMinBps boolean <optional>

是否强制保证视频分配码率不低于最低码率

Returns:
Name Type Description
code number 0 success, 1 unjoined, 2 joining, 20 mqtt client not found, 21 mqtt client not connected
message string 描述信息,error 信息

(async) requestPermission(fieldDeviceId, params)

Description:
  • 网关权限申请

Example
requestPermission({ fieldDeviceId: 'xxx', permission: 'master' })
Parameters:
Name Type Description
fieldDeviceId string

现场设备 ID

params Array.<Object>
Properties
Name Type Description
permission PermissionState

master 控制权限及观看权限 guest 仅观看权限

Returns:
Name Type Description
code number 0 success, 1 unjoined, 2 joining, 20 mqtt client not found, 21 mqtt client not connected, 22 mqtt message publish failed, 23 request pending 24 request timeout
message string 描述信息,error 信息

(async) pullGatewayList()

Description:
  • 拉取网关设备列表

Example
pullGatewayList()
Returns:
Name Type Description
code number 0 success, 20 mqtt client not found, 21 mqtt client not connected, 22 request publish failed, 23 request pending 24 request timeout
message string 描述信息,error 信息

(async) getGatewayInfo(gatewayId)

Description:
  • 获取指定网关设备信息

Example
getGatewayInfo('gateway_id_123')
Parameters:
Name Type Description
gatewayId string

网关设备 ID

Returns:
Name Type Description
code number 0 success, 20 mqtt client not found, 21 mqtt client not connected, 22 request publish failed, 23 request pending 24 request timeout
message string 描述信息,error 信息
data Gateway 网关设备信息

(async) adjustVideoRate(fieldDeviceId, params)

Description:
  • 调整视频码率

Example
adjustVideoRate('xxxxxxxx', [{ streamId: 0, videoRate: 3000 }, { streamId: 1, videoRate: 1000 }])
Parameters:
Name Type Description
fieldDeviceId string

现场设备 ID

params Array.<Object>
Properties
Name Type Description
streamId number

视频流流号

videoRate number

码率,单位 kbps

Returns:

返回 Promise 对象。

Name Type Description
code number 0 success, 1 unjoined, 2 joining, 3 failed, 20 mqtt client not found, 21 mqtt client not connected, 100 request error
message string 描述信息,error 信息

sendControlData(fieldDeviceId, params)

Description:
  • 通过 DataChannel 或 MQTT 发送自定义消息。需先调用 requestPermission 申请 master 权限,否则返回失败。

Example
// 通过 SCTP DataChannel 发送(需要先 connect 和 subscribe)
sendControlData('xxxxxxxx', { data: 'xxx', ordered: true })

// 通过 MQTT 发送(只需登录,无需 connect 和 subscribe)
await sdk.init(); // 登录成功即可
await sdk.requestPermission('fieldDeviceId', { permission: 'master' });
sendControlData('xxxxxxxx', { data: 'xxx', channelType: 'mqtt' })
Parameters:
Name Type Description
fieldDeviceId string

现场设备 ID

params Object
Properties
Name Type Attributes Default Description
data string | ArrayBuffer

自定义消息

ordered boolean <optional>
true

是否通过可靠通道发送(仅 SCTP 通道有效,与 channelType='mqtt' 互斥)

channelType 'sctp' | 'mqtt' <optional>
'sctp'

消息通道类型,'sctp' 表示 DataChannel,'mqtt' 表示 MQTT

Returns:

返回 Promise 对象。

Name Type Description
code number 0 success, 1 unjoined (仅SCTP), 2 joining (仅SCTP), 3 failed, 20 mqtt client not found, 21 mqtt client not connected, 42 no permission, 45 no subscribed streams (仅SCTP), 100 request error
message string 描述信息,error 信息

(async) requestDiagnosisReport(fieldDeviceId) → {Promise.<DiagnosisReport>}

Description:
  • 获取诊断报告。通常需要等待 10 秒后才能获取到诊断报告,最大等待时间为 15 秒。

Example
requestDiagnosisReport('<FIELD-DEVICE-ID>')
Parameters:
Name Type Description
fieldDeviceId string

现场设备 ID

Returns:

返回诊断报告。

Type
Promise.<DiagnosisReport>

setJitterBufferTarget(params) → {Object}

Description:
  • 设置 jitterBufferTarget(抖动缓冲区目标值)。若网络环境较差,视频卡顿比较明显,可以尝试手动调用该方法调大 jitterBufferTarget。注意:调大 jitterBufferTarget 会增加视频延迟;Chrome 浏览器推荐优先调整 playoutDelayHint 而不是 jitterBufferTarget。

Example
setJitterBufferTarget({ fieldDeviceId: '<FIELD-DEVICE-ID>', streamId: 0, jitterBufferTarget: 100 })
setJitterBufferTarget({ fieldDeviceId: '<FIELD-DEVICE-ID>', streamId: 0, jitterBufferTarget: null }) // 还原默认值
Parameters:
Name Type Description
params Object

参数对象

Properties
Name Type Description
fieldDeviceId string

现场设备 ID

streamId number

流 ID

jitterBufferTarget number | null

jitterBufferTarget 值,单位毫秒,传 null 还原为默认值

Returns:

返回状态对象,包含 code(0 成功, -1 失败)和 message(描述信息)

Type
Object

setPlayoutDelayHint(params) → {Object}

Description:
  • 设置 playoutDelayHint(播放延迟提示值)。若网络环境较差,视频卡顿比较明显,可以尝试手动调用该方法调大 playoutDelayHint。注意:调大 playoutDelayHint 会增加视频延迟;Chrome 浏览器推荐优先调整 playoutDelayHint 而不是 jitterBufferTarget。

Example
setPlayoutDelayHint({ fieldDeviceId: '<FIELD-DEVICE-ID>', streamId: 0, playoutDelayHint: 100 })
setPlayoutDelayHint({ fieldDeviceId: '<FIELD-DEVICE-ID>', streamId: 0, playoutDelayHint: null }) // 还原默认值
Parameters:
Name Type Description
params Object

参数对象

Properties
Name Type Description
fieldDeviceId string

现场设备 ID

streamId number

流 ID

playoutDelayHint number | null

playoutDelayHint 值,单位毫秒,传 null 还原为默认值

Returns:

返回状态对象,包含 code(0 成功, -1 失败)和 message(描述信息)

Type
Object