Constructor
new TRROSDK(params)
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params | Object | Properties
|
// 公有云模式,初始化最小配置
new TRROSDK({
cloudMode: 'public',
projectId: 'xxxxxxxx',
remoteDeviceId: 'xxxxxxxx',
password: '********',
});
// 私有云模式,初始化最小配置
new TRROSDK({
serverIp: '1.1.1.1',
projectId: 1111,
remoteDeviceId: 'xxxxxxxx',
password: '',
});Methods
(async) init()
初始化 MQTT 连接
返回 Promise 对象。
| Name | Type | Description |
|---|---|---|
| code | number | 0 success, 21 mqtt client connnect error, 26 mqtt client alreay connected, 100 request error |
| message | string | 描述信息,error 信息 |
init()setSessionPermissionToken(fieldDeviceId, token) → {void}
设置指定现场设备的临时会话密钥。
只有在公有云模式下,通过使用项目共享密钥生成的远端设备 ID 和密码,实现远端设备的自动注册和登录时,才需要调用该方法。
| Name | Type | Description |
|---|---|---|
fieldDeviceId | string | |
token | string |
- Type:
- void
setSessionPermissionToken('<FIELD-DEVICE-ID>', '<TOKEN>')(async) connect(fieldDeviceId)
连接指定现场设备
| Name | Type | Description |
|---|---|---|
fieldDeviceId | string | 现场设备 ID |
返回 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 连接失败 |
connect('<FIELD-DEVICE-ID>')(async) subscribe(params)
订阅媒体流(拉流)
| Name | Type | Description | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params | Object | Properties
|
返回 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 信息 |
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'
})(async) unsubscribe(params)
取消订阅 track (拉流)
| Name | Type | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params | Object | Properties
|
返回 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 信息 |
unsubscribe({ fieldDeviceId: 'xxxxxxxx', streamIds: [0] })(async) publish(params)
发布音频流(推流)
只有在成功订阅了视频流的情况下,才能发布音频流,若取消订阅了所有视频流,则 SDK 会自动取消发布音频流,并通过 onEvent 回调通知
一个 SDK 实例只允许发布一个音频流
若使用 server 模式,需先调用 requestPermission 申请 master 权限,否则无法推流
| Name | Type | Description | ||||||
|---|---|---|---|---|---|---|---|---|
params | Object | Properties
|
返回 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 信息 |
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 () {}(async) unpublish()
取消发布音频流(取消推流)
返回 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 信息 |
unpublish();(async) muteAudio(params)
静音或者取消静音指定音频流
| Name | Type | Description | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
params | Object | Properties
|
返回 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 信息 |
muteAudio({ deviceId: 'xxxxxxxx', muted: true })(async) disconnect(fieldDeviceId)
断开与指定现场设备的连接
| Name | Type | Description |
|---|---|---|
fieldDeviceId | string | 现场设备 |
返回 Promise 对象。
| Name | Type | Description |
|---|---|---|
| code | number | 0 success, 1 unjoined, 3 failed, 100 request error |
| message | string | 描述信息,error 信息 |
disconnect('xxxxxxxx')(async) disconnectAll()
断开与所有现场设备的连接
返回 Promise 对象。
| Name | Type | Description |
|---|---|---|
| code | number | 0 success, 1 unjoined, 3 failed, 100 request error |
| message | string | 描述信息,error 信息 |
disconnectAll()(async) destroy()
销毁所有连接
返回 Promise 对象。
| Name | Type | Description |
|---|---|---|
| code | number | 0 success, 1 unjoined, 3 failed, 100 request error |
| message | string | 描述信息,error 信息 |
destroy()changeFieldDeviceEncodeConfig(fieldDeviceId, params)
调整现场设备编码配置。
调用此接口将临时更新现场设备的编码配置,现场设备重启后仍会恢复到原配置。
配置更新只涉及传入的字段,未传入的配置项将保持当前状态。
| Name | Type | Description | |||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fieldDeviceId | string | 现场设备 ID | |||||||||||||||||||||||||||||||||||||||||||||
params | Array.<Object> | Properties
|
| Name | Type | Description |
|---|---|---|
| code | number | 0 success, 1 unjoined, 2 joining, 20 mqtt client not found, 21 mqtt client not connected |
| message | string | 描述信息,error 信息 |
changeFieldDeviceEncodeConfig('xxxxxxxx', [{ streamId: 0, encodeConfig: { minBps: 1000, forceMin: true } }])(async) requestPermission(fieldDeviceId, params)
网关权限申请
| Name | Type | Description | ||||||
|---|---|---|---|---|---|---|---|---|
fieldDeviceId | string | 现场设备 ID | ||||||
params | Array.<Object> | Properties
|
| 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 信息 |
requestPermission({ fieldDeviceId: 'xxx', permission: 'master' })(async) pullGatewayList()
拉取网关设备列表
| 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 信息 |
pullGatewayList()(async) getGatewayInfo(gatewayId)
获取指定网关设备信息
| Name | Type | Description |
|---|---|---|
gatewayId | string | 网关设备 ID |
| 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 | 网关设备信息 |
getGatewayInfo('gateway_id_123')(async) adjustVideoRate(fieldDeviceId, params)
调整视频码率
| Name | Type | Description | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
fieldDeviceId | string | 现场设备 ID | |||||||||
params | Array.<Object> | Properties
|
返回 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 信息 |
adjustVideoRate('xxxxxxxx', [{ streamId: 0, videoRate: 3000 }, { streamId: 1, videoRate: 1000 }])sendControlData(fieldDeviceId, params)
通过 DataChannel 或 MQTT 发送自定义消息。需先调用 requestPermission 申请 master 权限,否则返回失败。
| Name | Type | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fieldDeviceId | string | 现场设备 ID | ||||||||||||||||||||
params | Object | Properties
|
返回 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 信息 |
// 通过 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' })(async) requestDiagnosisReport(fieldDeviceId) → {Promise.<DiagnosisReport>}
获取诊断报告。通常需要等待 10 秒后才能获取到诊断报告,最大等待时间为 15 秒。
| Name | Type | Description |
|---|---|---|
fieldDeviceId | string | 现场设备 ID |
返回诊断报告。
- Type:
- Promise.<DiagnosisReport>
requestDiagnosisReport('<FIELD-DEVICE-ID>')setJitterBufferTarget(params) → {Object}
设置 jitterBufferTarget(抖动缓冲区目标值)。若网络环境较差,视频卡顿比较明显,可以尝试手动调用该方法调大 jitterBufferTarget。注意:调大 jitterBufferTarget 会增加视频延迟;Chrome 浏览器推荐优先调整 playoutDelayHint 而不是 jitterBufferTarget。
| Name | Type | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params | Object | 参数对象 Properties
|
返回状态对象,包含 code(0 成功, -1 失败)和 message(描述信息)
- Type:
- Object
setJitterBufferTarget({ fieldDeviceId: '<FIELD-DEVICE-ID>', streamId: 0, jitterBufferTarget: 100 })
setJitterBufferTarget({ fieldDeviceId: '<FIELD-DEVICE-ID>', streamId: 0, jitterBufferTarget: null }) // 还原默认值setPlayoutDelayHint(params) → {Object}
设置 playoutDelayHint(播放延迟提示值)。若网络环境较差,视频卡顿比较明显,可以尝试手动调用该方法调大 playoutDelayHint。注意:调大 playoutDelayHint 会增加视频延迟;Chrome 浏览器推荐优先调整 playoutDelayHint 而不是 jitterBufferTarget。
| Name | Type | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params | Object | 参数对象 Properties
|
返回状态对象,包含 code(0 成功, -1 失败)和 message(描述信息)
- Type:
- Object
setPlayoutDelayHint({ fieldDeviceId: '<FIELD-DEVICE-ID>', streamId: 0, playoutDelayHint: 100 })
setPlayoutDelayHint({ fieldDeviceId: '<FIELD-DEVICE-ID>', streamId: 0, playoutDelayHint: null }) // 还原默认值getCameraViewNavigationLine(params) → {Array.<NavigationLinePoint>}
获取车辆行驶方向的导航线坐标(实验性功能)。基于针孔相机视角模型构建,需要根据真实参数设置,可接近图中真实轨迹。
| Name | Type | Description | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params | CameraViewNavigationLineParams | 导航线计算参数 Properties
|
导航线坐标数组,共 size 个采样点。坐标原点为画面中心点,X/Y 坐标范围均为 (-1, 1)。
- Type:
- Array.<NavigationLinePoint>
const points = sdk.getCameraViewNavigationLine({
navDist: 20,
blindDist: 2,
carWidth: 2.5,
carLength: 3.5,
cameraHeight: 2.0,
cameraPitchDegree: 75,
cameraAspectRatio: 16 / 9,
carAngle: 15,
size: 20,
});
// 使用坐标在 Canvas 上绘制导航线
points.filter(p => p.valid).forEach(p => {
const canvasX = (p.leftX + 1) / 2 * canvasWidth;
const canvasY = (1 - (p.leftY + 1) / 2) * canvasHeight;
// ...绑定绘制逻辑
});