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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
Returns:
返回状态对象,包含 code(0 成功, -1 失败)和 message(描述信息)
- Type
- Object