InitConfig

TCGSDK InitConfig 相关配置,对应TCGSDK 中的 init 方法的。

Members

appid :number

用户的腾讯云 APPID,可选参数

Type:
  • number

mount :string

页面挂载点的 HTML 元素 ID

Type:
  • string

mic :boolean|MicProfileConstraints

Default Value:
  • false

是否开启本地麦克风

Type:
  • boolean | MicProfileConstraints

camera :boolean|CameraProfileConstraints|CameraProfileType

Default Value:
  • false

是否开启本地摄像头

Type:

tabletMode :boolean

Default Value:
  • false

true 为使用平板滑动鼠标模式,false 为绝对映射模式。该参数只针对移动端,PC 端忽略该参数。该 mode 下鼠标产生相对位移。

Type:
  • boolean

mobileGame :boolean

Default Value:
  • false

true 为使用接入手游,false 为适用端游

Type:
  • boolean

mobileVpx :boolean

Default Value:
  • false

手游启用VPX 编码

Type:
  • boolean

cursorMode :number

Default Value:
  • 0

鼠标模式

mode=0: 页面渲染的固定鼠标图片,如为设置默认鼠标图片,会采用系统鼠标

mode=1: 采用云端应用内的鼠标图片,图片会下发到前端,由前端在浏览器页面绘制

mode=2: (不建议使用)云端画面内渲染鼠标图片,此时会隐藏本地渲染的鼠标,兼容性最好,但是有延时

Type:
  • number

clickToFullscreen :boolean

Default Value:
  • false

是否启动点击全屏操作,true 为启用,false为禁用。

Type:
  • boolean

clickBodyToPlay :boolean

Default Value:
  • true

点击body 任意地方尝试播放video,true 为启用,false为禁用。

Type:
  • boolean

idleThreshold :number

Default Value:
  • 300

用户操作空闲时间阈值,单位为秒,默认值:300s 空闲超过这个时间将触发 onEvent 事件,消息为 {type: 'idle', data: {times: 1}}

可在回调中调用 TCGSDK.destroy() 释放并发

Type:
  • number

keepLastFrame :boolean

Default Value:
  • false

断开的时候是否保留最后一帧画面,如果需要保留最后一帧画面并重连,不能再次调用 init 函数,而是先调用 destroy() 接口,再调用 start() 接口。

mac safari/ios webview 无法生效

Type:
  • boolean

reconnect :boolean

Default Value:
  • true

是否自动重连,会在弱网,或帧率持续掉 0所导致的断连时,主动重连

重连策略:每6秒尝试一次,最多重连10次

Type:
  • boolean

showLoading :boolean

Default Value:
  • true

是否显示加载中画面

Type:
  • boolean

loadingText :string

Default Value:
  • '正在启动云渲染'

加载中的文字提示内容

Type:
  • string

restartText :string

Default Value:
  • '重新连接'

重新连接的文字提示内容

Type:
  • string

autoRotateContainer :boolean

Default Value:
  • false

当横竖屏切换时,是否自动旋转html节点适配,该参数会旋转整个html

Type:
  • boolean

autoRotateMountPoint :boolean

Default Value:
  • false

当横竖屏切换时,是否自动旋转挂载节点(mount)适配,该参数会旋转挂载节点(mount)

Type:
  • boolean

fullVideoToScreen :boolean

Default Value:
  • true

当 mount挂载节点宽高大于云端推流分辨率时候

true 拉伸video 尺寸并采用短边适配

false 不拉伸video,保持原有云端分辨率

Type:
  • boolean

debugSetting :DebugSettingParams

debugSetting 会在控制台打印出对应的日志 有如下配置

Type:

webDraftLevel :number

Default Value:
  • 0

0:关闭鼠标高频采样, 1:打开,但是打包发送, 2:拆开发送, 3: 限制包长度,多的丢掉

Type:
  • number

forceShowCursor :boolean

Default Value:
  • false

强制显示鼠标

Type:
  • boolean

bgImgUrl :string

设置初始化背景图,web端container 的背景,非云端背景

Type:
  • string

defaultCursorImgUrl :string

Default Value:
  • null

默认不展示

鼠标图片格式 https/http 地址

移动端传 'dot' 展示一个宽高 3px 的圆形小点。

Type:
  • string

clientInteractMode :'cursor'|'touch'

Default Value:
  • 'cursor'

云上应用交互模式,支持鼠标 或者 触摸

该参数建议移动端使用

Type:
  • 'cursor' | 'touch'

enablePaste :boolean

Default Value:
  • true

劫持键盘 Ctrl+v/Cmd+v,当用户使用粘贴功能时候,直接将本地剪切板内容发送给云端

该接口适用PC 端,通常在云端 input 框 focus 时候使用

Type:
  • boolean

enableMousemoveV2 :boolean

Default Value:
  • true

启用云端计算鼠标

可能会更平滑

Type:
  • boolean

remoteDesktopResolution :object

Default Value:
  • null
Properties:
Name Type Description
width number
height number

初始化云端桌面分辨率

可通过 TCGSDK.getPageSize() 获取页面 width,height 来实现自适应分辨率(移动端可将 width * 2,height * 2 保证画面清晰)

Type:
  • object

enableEventIntercept :boolean

Default Value:
  • true

是否开启事件拦截

拦截鼠标/键盘等事件,只针对云渲染节点操作时才向云端发送指令。

Type:
  • boolean

Methods

onInitSuccess(response) → {function}

初始化完毕的回调,触发此回调之后才能调用后面的 API

CreateSession 需要在该回调成功后进行;

Parameters:
Name Type Description
response Object

OnInitSuccess 回调函数的 response

Properties
Name Type Description
code number

code=-2 peerConnection 未断开,需要先调用 TCGSDK.destroy() code=-1 localOffer 无法获取H264 编码 code=0 Success

msg string

message

description any

相关描述

Returns:
Type
function

onConnectSuccess(response) → {function}

连接成功回调,调用 start 接口成功后才会触发

建议TCGSDK 相关接口都在该回调成功后调用,例如:

  1. 创建数据通道 createCustomDataChannel
  2. 创建摇杆(Plugin/Joystick)
  3. setRemoteDesktopResolution 等
Parameters:
Name Type Description
response Object

onConnectSuccess 回调函数的 response

Properties
Name Type Description
code number

code=0 Success

seat_index number

座位号,多人云游场景可能会用到

role string

角色,多人云游场景可能会用到

Returns:
Type
function

onConnectFail(response) → {function}

连接失败回调,连接断开,重连中会回调该接口

通常重连时间超过两分钟(例如连接断开/移动端切后台,两分钟后触发重连) 系统会自动回收实例,表现为返回 code > 0,建议该情况下 重新init + createSession

Parameters:
Name Type Description
response Object

onConnectFail 回调函数的 response

Properties
Name Type Description
code number

code 详情如下:

code Description
-3 超出重连次数
-2 自动重连中
-1 连接失败,触发了限频操作 5s,可稍后再连接
大于0(code > 0) Proxy 返回的重连错误,通常连不上,需重新init + createSession
msg string

message

Returns:
Type
function

onWebrtcStatusChange(response) → {function}

webrtc 状态回调,调用 start 接口成功后才会触发。

Parameters:
Name Type Description
response Object

onWebrtcStatusChange 回调函数的 response

Properties
Name Type Description
code number

code 详情如下:

code Description
-2 获取H264 编码失败
-1 setRemoteDescription 失败
0 成功
1 系统繁忙
2 票据不合法
3 用户带宽不足
4 资源不足,没有可用机器
5 session 失效,需要重新登录
6 媒体描述信息错误
7 游戏拉起失败
100 proxy 错误
255 设备不支持webrtc
msg string

message

Returns:
Type
function

onDisconnect(response) → {function}

断开/被踢触发此回调,调用 start 接口成功后才会触发

当出现 -1 时候,如果设置了 init 参数 reconnect: true(默认值 true) 不用任何操作,SDK 会主动重连,未设置需要调用 TCGSDK.reconnect()

如果持续链接不上,可以看回调 onConnectFail 相关错误码,在里面完善相关逻辑

Parameters:
Name Type Description
response Object

onDisconnect 回调函数的 response

Properties
Name Type Description
code number

code 详情如下:

code Description
-2 创建local offer 失败,需要重新init + createSession
-1 需要重连,通常出现在码率掉0,收不到推流,连接超时,ice 断开,SDK 会自动重连
0 主动关闭
1 用户重复连接(该消息不可靠)
2 用户心跳超时,webrtc服务端主动断开,这个消息有可能丢失 init + createSession
msg string

message

Returns:
Type
function

onNetworkChange(response) → {function}

网络状态变化/用户idle,都会调用此回调

Parameters:
Name Type Description
response Object

onNetworkChange 回调函数的 response

Properties
Name Type Description
status 'idle' | 'noflow' | 'noflowcenter' | 'stats' | 'jitter' | 'openurl' | 'latency'
times number
stats WebrtcStats
data Object
Properties
Name Type Description
code number
value number | string
message string
begin number
finish number
Returns:
Type
function

onTouchEvent(response) → {function}

移动端适用

移动端触摸事件回调,调用 start 接口成功后才会触发

返回 OnTouchEventResponse[],对应当前屏幕触控点数,可根据 array 长度判断多指操作

Example
onTouchEvent: async (res) => {
  // console.log('onTouchEvent', res);
  // 针对单指触控操作
  if (res.length === 1) {
    const [{ id, type, pageX, pageY }] = res;
    // console.log('onTouchEvent', id, type, pageX, pageY);
    TCGSDK.mouseMove(id, type, pageX, pageY);
    if (type === 'touchstart') {
      TCGSDK.sendMouseEvent({ type: 'mouseleft', down: true });
    }
    if (type === 'touchend' || type === 'touchcancel') {
      TCGSDK.sendMouseEvent({ type: 'mouseleft', down: false });
    }
  }
  // 针对双指缩放操作,这里双指模拟了PC 的鼠标滚轮事件
  if (res.length === 2) {
    const [{ pageX: oneX, pageY: oneY, type: oneType }, { pageX: twoX, pageY: twoY, type: twoType }] = res;

    const currentX = Math.ceil(Math.abs(oneX - twoX));
    const currentY = Math.ceil(Math.abs(oneY - twoY));
    // lastX,lastY 为上一次的位置,可定义在全局 如 let lastX = null, lastY = null
    lastX || (lastX = currentX);
    lastY || (lastY = currentY);

    if (lastX && currentX - lastX < 0 && lastY && currentY - lastY < 0) {
      TCGSDK.sendMouseEvent({ type: 'mousescroll', delta: 1 });
      lastX = currentX;
      lastY = currentY;
    }

    if (lastX && currentX - lastX > 0 && lastY && currentY - lastY > 0) {
      TCGSDK.sendMouseEvent({ type: 'mousescroll', delta: -1 });
      lastX = currentX;
      lastY = currentY;
    }

    // 双指离开后,发送一次鼠标抬起操作
    if (
      oneType === 'touchend' ||
      oneType === 'touchcancel' ||
      twoType === 'touchend' ||
      twoType === 'touchcancel'
    ) {
      TCGSDK.sendMouseEvent({ type: 'mouseleft', down: false });
    }
  }
}
Parameters:
Name Type Description
response Array.<Object>

onTouchEvent 回调函数的 response

Properties
Name Type Description
id number

触控事件的 ID

type TouchType

事件类型,可选择 'touchstart','touchmove','touchend','touchcancel' 四种之一

x number

触控点在视频区域内的 x 坐标 (hook 模式未减去 screen_left)

y number

触控点在视频区域内的 y 坐标 (hook 模式未减去 screen_top)

pageX number

触控点在当前网页内的 x 坐标

pageY number

触控点在当前网页内的 y 坐标

movementX number

触控点相对上次坐标的 x 偏移值

movementY number

触控点相对上次坐标的 y 偏移值

Returns:
Type
function

onDoubleTap() → {function}

只针对移动端,双击屏幕回调(同onTouchEvent)

Returns:
Type
function

onGameStartComplete(response) → {function}

游戏启动成功后的通知

Parameters:
Name Type Description
response Object

onGameStartComplete 回调函数的 response

Properties
Name Type Description
request_id string

请求唯一标示

user_id string

user_id

game_id string

game_id

status number

0 启动游戏成功; 1 启动游戏失败

Returns:
Type
function

onGameStop(response) → {function}

云端进程终止的通知

Parameters:
Name Type Description
response Object

onGameStop 回调函数的 response

Properties
Name Type Description
user_id string

user_id

game_name string

game_name

timestamp number

timestamp

message string

message

Returns:
Type
function

onLoadGameArchive(response) → {function}

游戏存档加载回调,会不断回调size

Parameters:
Name Type Description
response Object

onLoadGameArchive 回调函数的 response

Properties
Name Type Description
user_id string

user_id

game_id string

game_id

name string

存档最终文件名

url string

存档下载地址

status number

0: 加载存档成功 ; 1: 下载存档失败 ; 2: 校验存档失败 ; 3: 解压存档失败 ; 4: 其他错误 ; 5: 下载中

save_type 'Auto' | 'Normal'

'Auto' or 'Normal'

category_id string

分类标识

archive_size number

archive_size

loaded_size number

表示下载的字节数

Returns:
Type
function

onSaveGameArchive(response) → {function}

游戏保存存档回调

Parameters:
Name Type Description
response Object

onLoadGameArchive 回调函数的 response

Properties
Name Type Description
user_id string

user_id

game_id string

game_id

name string

存档最终文件名

md5 string

md5

status number

0: 存档保存成功 1: 存档保存失败 2: 存档压缩失败失败 3: 其他错误 4: 上传中

save_type 'Auto' | 'Normal'

'Auto' or 'Normal'

category_id string

分类标识

archive_size number

archive_size

saved_size number

表示上传的字节数

Returns:
Type
function

onInputStatusChange(response) → {function}

云端输入状态改变,有点击事件的时候都会触发,可根据状态判断 input 框是否聚焦

该回调可能会触发两次,分别对应鼠标左右按下/抬起

Parameters:
Name Type Description
response Object

onInputStatusChange 回调函数的 response

Properties
Name Type Description
field_type 'normal_input' | 'unfocused'

unfocused 脱焦,normal_input 常规input 输入

status 'disabled' | 'enabled'

输入可用状态

Returns:
Type
function

onGamepadConnectChange(response) → {function}

手柄连接/断开事件回调

需要 Web 运行环境支持 Gamepad API

Parameters:
Name Type Description
response Object

onGamepadConnectChange 回调函数的 response

Properties
Name Type Description
status 'gamepadconnect' | 'gamepaddisconnect'

链接状态

index number

手柄索引

gamepad Gamepad

Gamepad API interface

Returns:
Type
function

onCursorShowStatChange(response) → {function}

云端鼠标显示/隐藏,只在变化的时候回调,包含新旧状态描述

Parameters:
Name Type Description
response Object

onCursorShowStatChange 回调函数的 response

Properties
Name Type Description
oldStatus boolean

老状态

newStatus boolean

新状态

Returns:
Type
function

onOrientationChange(response) → {function}

屏幕方向变化事件回调,type 包含 portrait 以及 landscape

Parameters:
Name Type Description
response Object

onOrientationChange 回调函数的 response

Properties
Name Type Description
type 'portrait' | 'landscape'

当前横竖屏状态

Returns:
Type
function

onVisibilityChange(response) → {function}

页面显示/隐藏发生变化,status 包含 visible 以及 hidden

Parameters:
Name Type Description
response Object

onVisibilityChange 回调函数的 response

Properties
Name Type Description
status 'visible' | 'hidden'

当前显示/隐藏状态

Returns:
Type
function

onConfigurationChange(response) → {function}

云端config 发生变化时候回调,包含屏幕相关 screen_config,具体如下:

Parameters:
Name Type Description
response Object

onConfigurationChange 回调函数的 response

Properties
Name Type Description
width number

height number

orientation 'landscape' | 'portrait'

屏幕方向

Returns:
Type
function

onRemoteScreenResolutionChange(response) → {function}

云端屏幕分辨率发生变化,具体如下:

Parameters:
Name Type Description
response Object

onRemoteScreenResolutionChange 回调函数的 response

Properties
Name Type Description
width number
height number
top number
left number
Returns:
Type
function

onVideoStreamConfigChange(response) → {function}

推流分辨率发生变化

与 onRemoteScreenResolutionChange 区别在于,前者是云端屏幕/应用的分辨率,后者是推流分辨率,可能存在设置的屏幕/应用分辨率为 1280720,但是推流的分辨率为 19201080的情况。

Parameters:
Name Type Description
response Object

onVideoStreamConfigChange 回调函数的 response

Properties
Name Type Description
width number
height number
Returns:
Type
function

onDeviceChange() → {function}

连接设备发生变化,需要通过 navigator.mediaDevices.enumerateDevices() 拿到可用设备

Returns:
Type
function

onGetUserMediaStatusChange(response) → {function}

获取 mic/camera 回调

Parameters:
Name Type Description
response Object

onGetUserMediaStatusChange 回调函数的 response

Properties
Name Type Description
code number

0 Success; 1 NotFoundError; 2 NotAllowedError; 3 OverconstrainedError

msg string

'NotFoundError' | 'NotAllowedError' | string;

userMedia MediaStream

userMedia

Returns:
Type
function

onEvent(response) → {function}

事件回调

目前主要用于自动播放是否成功回调

Parameters:
Name Type Description
response Object

onEvent 回调函数的 response

Properties
Name Type Attributes Description
type string

对应类型 'autoplay'| 'video_state' | 'idle' | 'noflow' | 'noflowcenter' | 'openurl' | 'pointerlockerror' | 'readclipboarderror' | 'webrtc_stats' | 'latency'

data Object <optional>

不同 type可能 data 不同,参考下表

对照表

type data
autoplay Object<{code: number; message: string;}>
code0 success -1 failed
messagestring
video_state Object<{code: number; message: string;}>
code0 playing 1 pause 2 ended
messagestring
idle Object<{times: number;}>
timesnumber
noflow -
noflowcenter -
openurl Object<{value: string;}>
valuestring
pointerlockerror -
readclipboarderror Object<{message?: string;}>
messagestring
webrtc_stats Object<>
bit_ratenumber客户端接收的码率,单位:Mbps
cpunumber云端CPU占用率,单位:百分比
gpustring云端GPU占用率,单位:百分比
delaynumber客户端收到图像帧到解码显示的延时,单位:ms,iOS可能收不到
fpsnumber客户端显示帧率
load_cost_timenumber云端加载时长,单位:ms
nacknumber客户端重传次数
packet_lostnumber客户端丢包次数
packet_receivednumber客户端收到的包总数
rttnumber客户端到云端,网络端数据包往返耗时
timestampnumber此数据回调的时间戳,单位:ms
latency Object<{value: number; message: string;}>
valuevalue=0 NETWORK_NORMAL
value=1 NETWORK_CONGESTION
value=2 NACK_RISING
value=3 HIGH_DELAY
value=4 NETWORK_JITTER
messagestring
ice_state Object<{value: string;}>
valueconnected / disconnected
Returns:
Type
function

onStreamPushStateChange(response) → {function}

Parameters:
Name Type Description
response Object

onStreamPushStateChange 回调函数的 response

Properties
Name Type Description
stream_push_state string

推流状态 'NoStreamPushing' | 'PushConnecting' | 'Pushing' | 'PushPaused' | 'PushReConnecting';

Returns:
Type
function

onMultiPlayerChange(response) → {function}

多人云游场景的回调函数,包括 user_state,seats_info

Parameters:
Name Type Description
response Object

onMultiPlayerChange 回调函数的 response

Properties
Name Type Description
user_state Object
Properties
Name Type Description
state 'offline' | 'online'

用户状态

user_id string

用户 id

seats_info Object

座位信息

Properties
Name Type Description
players Array.<Object>
Properties
Name Type Description
name string

用户名称

seat_index number

坐席位置

mic_status number

麦克风状态 0 管理员禁麦 1 闭麦(自己主动)2 开麦

viewers Array.<Object>
Properties
Name Type Description
name string

用户名称

seat_index number

坐席位置

mic_status number

麦克风状态 0 管理员禁麦 1 闭麦(自己主动)2 开麦

submit_seat_change Object

只有主机玩家才能收到该消息

Properties
Name Type Description
user_id string

用户 id

to_role 'viewer' | 'player'

需要切换至的角色

seat_index number

需要切换至的坐席

Returns:
Type
function

onLog(response) → {function}

日志回调函数,用于外部获取日志,作用与 setLogHandler 接口一致

Parameters:
Name Type Description
response string
Returns:
Type
function