feat: 增加分辨率修改

This commit is contained in:
2026-07-21 11:05:40 +08:00
parent 9e5ac91e46
commit 341dce249f
25 changed files with 646 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
import { encodeControlMessage } from '../proto/controlMessage';
import { encodeControlMessage, decodeControlMessage, Action } from '../proto/controlMessage';
// 对应 Android 端 WebRtcClient / Flutter webrtc_controller.dart。
// 作为 OFFER 方仅接收远端视频recvonly+ 创建控制用 DataChannel
@@ -6,7 +6,7 @@ import { encodeControlMessage } from '../proto/controlMessage';
const DATA_CHANNEL_LABEL = 'control_channel';
export class WebRtcController {
constructor({ iceServers, deviceId, targetDeviceId, signaling, onIceState, onDataChannelState, onStream, onStats, onError }) {
constructor({ iceServers, deviceId, targetDeviceId, signaling, onIceState, onDataChannelState, onStream, onStats, onError, onResolutionReport }) {
this.iceServers = iceServers;
this.deviceId = deviceId;
this.targetDeviceId = targetDeviceId;
@@ -16,6 +16,7 @@ export class WebRtcController {
this.onStream = onStream;
this.onStats = onStats;
this.onError = onError;
this.onResolutionReport = onResolutionReport;
this.pc = null;
this.dataChannel = null;
@@ -96,7 +97,22 @@ export class WebRtcController {
this.dataChannel = dc;
dc.onopen = () => this.onDataChannelState && this.onDataChannelState(true);
dc.onclose = () => this.onDataChannelState && this.onDataChannelState(false);
dc.onmessage = (e) => console.debug('[DataChannel] 收到消息', e.data);
dc.onmessage = (e) => {
try {
// 被控端发来的是二进制 protobuf目前仅 REPORT_RESOLUTION 上报)。
const bytes = e.data instanceof ArrayBuffer ? new Uint8Array(e.data) : e.data;
const msg = decodeControlMessage(bytes);
if (msg && msg.action === Action.REPORT_RESOLUTION) {
this.onResolutionReport && this.onResolutionReport({
width: msg.width | 0,
height: msg.height | 0,
fps: msg.fps | 0,
});
}
} catch (err) {
console.debug('[DataChannel] 解码消息失败(非分辨率上报或格式异常)', err);
}
};
}
async handleAnswer(sdp) {
@@ -147,6 +163,7 @@ export class WebRtcController {
sendKey(keyCode) { return this.sendControlMessage({ action: 3, keyCode, keyAction: 0 }); } // KEY
sendLongPress(x, y) { return this.sendControlMessage({ action: 4, x, y }); } // LONG_PRESS
sendMotionEvent(action, x, y) { return this.sendControlMessage({ action: 5, motionAction: action, x, y }); } // MOTION_EVENT
sendResolutionChange(width, height, fps) { return this.sendControlMessage({ action: 6, width, height, fps }); } // SET_RESOLUTION
_startStats() {
this._statsTimer = setInterval(async () => {