TRROSDK

TRROSDK

TRRO 类,Join 前需初始化实例

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 公有云,private 私有云。
默认为 private。

serverIp string | Array.<string> <optional>

信令调度服务器的域名或 IP,可传入多个信令调度服务器地址。
公有云模式下,该字段无效;私有云模式下,不传该字段将抛出错误。

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

MQTT 强制登录,可选。默认 false。

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

logger Object <optional>

日志配置

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

是否打印 SDK 日志

reportLog boolean <optional>
true

是否上报 SDK 日志

log function <optional>

日志打印方法

Methods

(async) init()

初始化 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}

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

Example
setSessionPermissionToken(‘xxxxxxxx’, ‘********’)
Parameters:
Name Type Description
fieldDeviceId string
token string
Returns:
Type
void

(async) connect(fieldDeviceId)

连接指定现场设备

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

现场设备 ID

Returns:

返回 Promise 对象。

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

(async) subscribe(params)

订阅 track (拉流)

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)

取消订阅 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)

发布音频流(推流)
只有在成功订阅了视频流的情况下,才能发布音频流,若取消订阅了所有视频流,则 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()

取消发布音频流(取消推流)

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)

静音或者取消静音指定音频流

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)

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

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()

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

Example
disconnectAll()
Returns:

返回 Promise 对象。

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

(async) destroy()

销毁所有连接

Example
destroy()
Returns:

返回 Promise 对象。

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

changeFieldDeviceEncodeConfig(fieldDeviceId, params)

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

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)

网关权限申请

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()

拉取网关设备列表

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) adjustVideoRate(fieldDeviceId, params)

调整视频码率

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)

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

Example
sendControlData('xxxxxxxx', { data: 'xxx' })
Parameters:
Name Type Description
fieldDeviceId string

现场设备 ID

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

自定义消息

ordered boolean <optional>
true

是否通过可靠通道发送

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, 42 no permission, 45 no subscribed streams, 100 request error
message string 描述信息,error 信息