feat(sender): 轮询被控端上行事件并下发会话配置
- Windows runner 没有 ttstd/sender/events 实现,改为 250ms 轮询 FFI 队列消费 mirror 应答与剪贴板 - 被控端 granted/denied/stopped/error 均落到界面:拒绝或出错时关掉串流窗口并提示,不再干等画面 - 上行剪贴板写入本机剪贴板(clipboardSync 关闭时忽略) - 新增 sessionConfig 控制帧(msgType=9),请求投屏前与保存设置后各下发一次,让编码格式/码率/音频/剪贴板开关生效 - 剪贴板与投屏说明文本改用 UTF-8 编码,中文不再截断
This commit is contained in:
1 parent
e43e51d9c3
commit
4c0eba9717
3 files changed
+142
-3
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
@@ -36,6 +37,11 @@ class SenderBridge {
|
||||
/// x/y 是**画面像素坐标**(编码分辨率),由被控端换算成真实屏幕坐标。
|
||||
void Function(int action, int x, int y)? onVideoPointer;
|
||||
|
||||
/// 取一条被控端上行的控制事件(投屏应答 / 剪贴板),没有则返回 null。
|
||||
///
|
||||
/// 事件形如 `{"type":8,"action":2,"text":""}`,`type` 是 msgType。
|
||||
Map<String, dynamic>? nextInboundEvent() => _native?.pollEvent();
|
||||
|
||||
Future<void> _handleVideoCall(MethodCall call) async {
|
||||
switch (call.method) {
|
||||
case 'windowClosed':
|
||||
@@ -188,6 +194,9 @@ class SenderBridge {
|
||||
case 'requestMirror':
|
||||
case 'stopMirror':
|
||||
return 8;
|
||||
// 会话配置:编码器偏好、码率、音频、剪贴板同步。
|
||||
case 'sessionConfig':
|
||||
return 9;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
@@ -222,7 +231,8 @@ class SenderBridge {
|
||||
case 'wheel':
|
||||
return [...le16(byte('deltaX')), ...le16(byte('deltaY')), byte('buttons')];
|
||||
case 'clipboard':
|
||||
return (payload['text'] as String? ?? '').codeUnits;
|
||||
// 按 UTF-8 编码,中文才能原样传到被控端。
|
||||
return utf8.encode(payload['text'] as String? ?? '');
|
||||
case 'ping':
|
||||
final ts = byte('timestamp');
|
||||
return [
|
||||
@@ -234,10 +244,13 @@ class SenderBridge {
|
||||
];
|
||||
case 'requestMirror':
|
||||
// action=0 请求投屏;可选的 UTF-8 说明跟在后面。
|
||||
return [0, ...(payload['message'] as String? ?? '').codeUnits];
|
||||
return [0, ...utf8.encode(payload['message'] as String? ?? '')];
|
||||
case 'stopMirror':
|
||||
// action=1 请求停止投屏。
|
||||
return [1, ...(payload['message'] as String? ?? '').codeUnits];
|
||||
return [1, ...utf8.encode(payload['message'] as String? ?? '')];
|
||||
case 'sessionConfig':
|
||||
// msgType=9:payload 是 UTF-8 JSON,被控端按字段应用。
|
||||
return utf8.encode(jsonEncode(payload));
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -97,6 +97,8 @@ class NativeCore {
|
||||
late final DartDemoSendControlFn _demoSendControl = _library
|
||||
.lookup<NativeFunction<NativeDemoSendControlFn>>('ttstd_demo_send_control')
|
||||
.asFunction();
|
||||
/// 老版本 Core 没有这个导出,缺失时降级成「没有上行事件」。
|
||||
late final DartEventNextFn? _eventNext = _resolveEventNext();
|
||||
late final DartDiscoveryStartFn _discoveryStart = _library
|
||||
.lookup<NativeFunction<NativeDiscoveryStartFn>>('ttstd_discovery_start')
|
||||
.asFunction();
|
||||
@@ -218,6 +220,30 @@ class NativeCore {
|
||||
}
|
||||
}
|
||||
|
||||
/// 取出一条被控端上行的控制事件,没有则返回 null。
|
||||
///
|
||||
/// 事件形如 `{"type":8,"action":2,"text":""}`:
|
||||
/// `type` 是 msgType(4=剪贴板,8=投屏应答),`action` 见 `MirrorAction`。
|
||||
Map<String, dynamic>? pollEvent() {
|
||||
final next = _eventNext;
|
||||
if (next == null) return null;
|
||||
final out = calloc<Uint8>(65536);
|
||||
try {
|
||||
final written = next(out, 65536);
|
||||
if (written <= 0) return null;
|
||||
return jsonDecode(utf8.decode(_copy(out, written))) as Map<String, dynamic>;
|
||||
} finally {
|
||||
calloc.free(out);
|
||||
}
|
||||
}
|
||||
|
||||
DartEventNextFn? _resolveEventNext() {
|
||||
if (!_library.providesSymbol('ttstd_event_next')) return null;
|
||||
return _library
|
||||
.lookup<NativeFunction<NativeEventNextFn>>('ttstd_event_next')
|
||||
.asFunction();
|
||||
}
|
||||
|
||||
/// 启动同网段自动发现(UDP 41003 信标),幂等。
|
||||
bool startDiscovery({
|
||||
int controlPort = 41001,
|
||||
@@ -429,6 +455,8 @@ typedef NativeDemoStopFn = Void Function();
|
||||
typedef DartDemoStopFn = void Function();
|
||||
typedef NativeDemoStatusFn = Int32 Function(Pointer<Uint8>, UintPtr);
|
||||
typedef DartDemoStatusFn = int Function(Pointer<Uint8>, int);
|
||||
typedef NativeEventNextFn = Int32 Function(Pointer<Uint8>, UintPtr);
|
||||
typedef DartEventNextFn = int Function(Pointer<Uint8>, int);
|
||||
typedef NativeDemoSendControlFn = Int32 Function(
|
||||
Uint16, Uint8, Pointer<Uint8>, UintPtr,
|
||||
);
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'core/bridge.dart';
|
||||
import 'core/diagnostic_log.dart';
|
||||
@@ -50,6 +51,7 @@ class _HomePageState extends State<HomePage> {
|
||||
Timer? _connectWatchdog;
|
||||
Timer? _discoveryTimer;
|
||||
Timer? _videoStatsTimer;
|
||||
Timer? _eventTimer;
|
||||
|
||||
List<DeviceInfo> _devices = const <DeviceInfo>[];
|
||||
DeviceInfo? _selected;
|
||||
@@ -112,6 +114,12 @@ class _HomePageState extends State<HomePage> {
|
||||
const Duration(milliseconds: 500),
|
||||
(_) => _refreshVideoStats(),
|
||||
);
|
||||
// 被控端上行事件(投屏应答 / 剪贴板 / pong):Windows runner 没有 EventChannel
|
||||
// 实现,只能主动轮询 FFI 队列。
|
||||
_eventTimer = Timer.periodic(
|
||||
const Duration(milliseconds: 250),
|
||||
(_) => _drainInboundEvents(),
|
||||
);
|
||||
_refreshVideoTextureId();
|
||||
// 自测/演示用开关:TTSTD_DEMO_AUTOSTART=1 时启动即开启本机监听。
|
||||
if (Platform.environment['TTSTD_DEMO_AUTOSTART'] == '1') {
|
||||
@@ -127,6 +135,7 @@ class _HomePageState extends State<HomePage> {
|
||||
_connectWatchdog?.cancel();
|
||||
_discoveryTimer?.cancel();
|
||||
_videoStatsTimer?.cancel();
|
||||
_eventTimer?.cancel();
|
||||
_diagnosticLog.dispose();
|
||||
_bridge.stopDemoServer();
|
||||
_bridge.stopLocalSignaling();
|
||||
@@ -157,6 +166,91 @@ class _HomePageState extends State<HomePage> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 取空原生上行事件队列,逐条处理。
|
||||
///
|
||||
/// 单次最多取 32 条,避免某次积压后一帧 setState 处理太久。
|
||||
void _drainInboundEvents() {
|
||||
if (_nativeCore == null || !mounted) return;
|
||||
for (var i = 0; i < 32; i++) {
|
||||
final event = _bridge.nextInboundEvent();
|
||||
if (event == null) break;
|
||||
_handleInboundEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
void _handleInboundEvent(Map<String, dynamic> event) {
|
||||
final type = (event['type'] as num?)?.toInt() ?? -1;
|
||||
final action = (event['action'] as num?)?.toInt() ?? -1;
|
||||
final text = (event['text'] as String? ?? '').trim();
|
||||
switch (type) {
|
||||
case 8:
|
||||
_handleMirrorEvent(action, text);
|
||||
case 4:
|
||||
unawaited(_applyRemoteClipboard(text));
|
||||
case 6:
|
||||
_diagnosticLog.write('pong received');
|
||||
default:
|
||||
_diagnosticLog.write('inbound event type=$type action=$action text=$text');
|
||||
}
|
||||
}
|
||||
|
||||
/// 被控端的投屏应答。
|
||||
///
|
||||
/// action 与协议 MirrorAction 一致:0=请求 1=停止 2=已允许 3=已拒绝
|
||||
/// 4=已停止 5=出错。以前没有这条通道,被控端拒绝后主控端只能干等画面。
|
||||
void _handleMirrorEvent(int action, String text) {
|
||||
_diagnosticLog.write('mirror event action=$action text=$text');
|
||||
switch (action) {
|
||||
case 2:
|
||||
_snack('被控端已允许投屏');
|
||||
case 3:
|
||||
unawaited(_abortMirror(text.isEmpty ? '被控端拒绝了投屏请求' : '被控端拒绝了投屏请求:$text'));
|
||||
case 4:
|
||||
unawaited(_abortMirror('被控端已停止投屏'));
|
||||
case 5:
|
||||
unawaited(_abortMirror(text.isEmpty ? '被控端投屏失败' : '被控端投屏失败:$text'));
|
||||
}
|
||||
}
|
||||
|
||||
/// 投屏没建立起来或对端已停流:关掉独立窗口,避免一直停在「等待画面」。
|
||||
///
|
||||
/// [_stopSentForWindowClose] 置位是必须的——窗口是我们自己关的,
|
||||
/// 不能再给对端补发一条 stopMirror。
|
||||
Future<void> _abortMirror(String message) async {
|
||||
_stopSentForWindowClose = true;
|
||||
if (_videoWindowOpen) {
|
||||
await _bridge.closeVideoWindow();
|
||||
}
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_videoWindowOpen = false;
|
||||
_lastFrameAt = null;
|
||||
});
|
||||
_snack(message);
|
||||
}
|
||||
|
||||
/// 被控端上行的剪贴板内容 → 写入本机剪贴板。
|
||||
Future<void> _applyRemoteClipboard(String text) async {
|
||||
if (text.isEmpty || !_settings.clipboardSync) return;
|
||||
await Clipboard.setData(ClipboardData(text: text));
|
||||
_diagnosticLog.write('clipboard from peer applied (${text.length} chars)');
|
||||
}
|
||||
|
||||
/// 把本机会话偏好下发给被控端(msgType=9 会话配置帧)。
|
||||
///
|
||||
/// 必须在请求投屏之前下发:被控端按该配置决定编码格式、码率、音频与剪贴板同步。
|
||||
void _applySessionConfig() {
|
||||
if (!_phase.isConnected) return;
|
||||
final payload = <String, dynamic>{
|
||||
'preferH265': _settings.preferH265,
|
||||
'bitrateMbps': _settings.bitrateMbps,
|
||||
'audioEnabled': _settings.audioEnabled,
|
||||
'clipboardSync': _settings.clipboardSync,
|
||||
};
|
||||
_bridge.sendControl('sessionConfig', payload);
|
||||
_diagnosticLog.write('sessionConfig sent: $payload');
|
||||
}
|
||||
|
||||
/// 首帧前先取一次纹理 ID,避免等待轮询。
|
||||
Future<void> _refreshVideoTextureId() async {
|
||||
final id = await _bridge.videoTextureId();
|
||||
@@ -229,6 +323,8 @@ class _HomePageState extends State<HomePage> {
|
||||
}
|
||||
setState(() => _videoWindowOpen = true);
|
||||
}
|
||||
// 先下发会话配置,被控端据此决定编码器参数后再开流。
|
||||
_applySessionConfig();
|
||||
_sendControl('requestMirror', const {});
|
||||
_diagnosticLog.write('mirror request sent');
|
||||
} finally {
|
||||
@@ -483,6 +579,8 @@ class _HomePageState extends State<HomePage> {
|
||||
setState(() => _settings = updated);
|
||||
await _bridge.saveSettings(updated);
|
||||
await _applySignalingSettings();
|
||||
// 已连接时立刻把新配置推给被控端,投屏中改码率/音频也能生效。
|
||||
_applySessionConfig();
|
||||
}
|
||||
|
||||
Future<void> _showPairing() async {
|
||||
|
||||
Reference in new issue
Block a user