- video_window 增加 InputHandler 回调,WM_LBUTTONDOWN/MOUSEMOVE/LBUTTONUP/CAPTURECHANGED 映射为 0/2/1/3,移动事件按 60Hz 限流 - Paint 记录帧尺寸与实际绘制矩形,ClientToFrame 换算到画面像素并过滤黑边 - video_texture 通过方法通道转发 pointer 事件到 Dart,析构时清空回调 - bridge.dart 新增 onVideoPointer,main.dart 以 msg_type=0 触摸帧下发(压力 1000/0)
350 lines
11 KiB
Dart
350 lines
11 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'discovery_client.dart';
|
|
import 'local_signaling.dart';
|
|
import 'models.dart';
|
|
import 'native_core.dart';
|
|
|
|
class SenderBridge {
|
|
SenderBridge({
|
|
MethodChannel? method,
|
|
EventChannel? events,
|
|
NativeCore? nativeCore,
|
|
}) : _method = method ?? const MethodChannel('ttstd/sender'),
|
|
_events = events ?? const EventChannel('ttstd/sender/events'),
|
|
_native = nativeCore {
|
|
_video.setMethodCallHandler(_handleVideoCall);
|
|
}
|
|
|
|
final MethodChannel _method;
|
|
final EventChannel _events;
|
|
final NativeCore? _native;
|
|
final LocalSignalingProcess _localSignaling = LocalSignalingProcess();
|
|
|
|
/// 原生视频解码纹理通道(Windows runner 内嵌插件)。
|
|
static const MethodChannel _video = MethodChannel('ttstd/video');
|
|
|
|
/// 用户关掉独立串流窗口(点关闭按钮 / 按 ESC)时触发,用于停止投屏。
|
|
void Function()? onVideoWindowClosed;
|
|
|
|
/// 用户在串流窗口里点击/拖拽画面时触发:(action, x, y)。
|
|
///
|
|
/// action 与协议 TouchEvent.action 一致(0=按下 1=抬起 2=移动 3=取消),
|
|
/// x/y 是**画面像素坐标**(编码分辨率),由被控端换算成真实屏幕坐标。
|
|
void Function(int action, int x, int y)? onVideoPointer;
|
|
|
|
Future<void> _handleVideoCall(MethodCall call) async {
|
|
switch (call.method) {
|
|
case 'windowClosed':
|
|
onVideoWindowClosed?.call();
|
|
case 'pointer':
|
|
final args = call.arguments;
|
|
if (args is! Map) return;
|
|
final action = (args['action'] as num?)?.toInt();
|
|
final x = (args['x'] as num?)?.toInt();
|
|
final y = (args['y'] as num?)?.toInt();
|
|
if (action == null || x == null || y == null) return;
|
|
onVideoPointer?.call(action, x, y);
|
|
}
|
|
}
|
|
|
|
NativeCore? get nativeCore => _native;
|
|
|
|
bool get isLocalSignalingRunning => _localSignaling.isRunning;
|
|
|
|
Stream<ConnectionPhase> watchPhase() {
|
|
return _events
|
|
.receiveBroadcastStream()
|
|
.handleError((Object _) => null)
|
|
.map<ConnectionPhase>(ConnectionPhaseX.fromWire)
|
|
.distinct();
|
|
}
|
|
|
|
/// 发现设备:优先用同网段 UDP 信标(被控端广播、主控端被动收集,无需填 IP),
|
|
/// 信令服务器只作为跨网段兜底。
|
|
Future<List<DeviceInfo>> discover({String? signalingUrl}) async {
|
|
final devices = <DeviceInfo>[];
|
|
final native = _native;
|
|
if (native != null) {
|
|
try {
|
|
native.startDiscovery();
|
|
devices.addAll(DeviceInfo.listFromJson(native.discoveryDevices()));
|
|
} on Object {
|
|
// 信标不可用(端口被占用等)时退化为信令发现。
|
|
}
|
|
}
|
|
if (signalingUrl != null && signalingUrl.trim().isNotEmpty) {
|
|
for (final device in await DiscoveryClient.listDevices(signalingUrl)) {
|
|
final duplicated = devices.any(
|
|
(item) => item.ip == device.ip && item.controlPort == device.controlPort,
|
|
);
|
|
if (!duplicated) devices.add(device);
|
|
}
|
|
}
|
|
if (devices.isNotEmpty || native != null) return devices;
|
|
try {
|
|
final raw = await _method.invokeListMethod<dynamic>('discover');
|
|
if (raw == null || raw.isEmpty) return const <DeviceInfo>[];
|
|
return DeviceInfo.listFromJson(raw);
|
|
} on MissingPluginException {
|
|
return const <DeviceInfo>[];
|
|
} on PlatformException {
|
|
return const <DeviceInfo>[];
|
|
}
|
|
}
|
|
|
|
Future<DeviceInfo?> localListener() async {
|
|
final localIp = await _localIp();
|
|
if (localIp == null) return null;
|
|
return DeviceInfo(
|
|
id: 'local-listener',
|
|
name: '本机监听端点',
|
|
ip: localIp,
|
|
mediaPort: 41000,
|
|
controlPort: 41001,
|
|
fingerprint: '',
|
|
capabilities: const <String>[],
|
|
model: '',
|
|
resolution: '',
|
|
);
|
|
}
|
|
|
|
static Future<String?> _localIp() async {
|
|
try {
|
|
final interfaces = await NetworkInterface.list(
|
|
includeLoopback: false,
|
|
type: InternetAddressType.IPv4,
|
|
);
|
|
for (final interface in interfaces) {
|
|
for (final address in interface.addresses) {
|
|
if (!address.isLoopback) return address.address;
|
|
}
|
|
}
|
|
} on Object {
|
|
// 忽略网络接口枚举失败。
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<void> connect(DeviceInfo device) async {
|
|
final native = _native;
|
|
if (native != null) {
|
|
// 先确保本机控制/媒体端口在监听,再让被控端立刻拨号(不必等它下次广播)。
|
|
// 监听失败必须显式报错:否则被控端拨号会没人接,界面只能笼统地等到超时。
|
|
if (!native.startDemoServer()) {
|
|
throw Exception('本机监听启动失败(TCP 41001 / UDP 41000、41002 可能被占用)');
|
|
}
|
|
if (device.ip.isNotEmpty) {
|
|
native.discoveryConnect(device.ip);
|
|
}
|
|
return;
|
|
}
|
|
await _invokeIgnoringMissing('connect', device.toJson());
|
|
}
|
|
|
|
Future<void> disconnect() async {
|
|
final native = _native;
|
|
if (native != null) {
|
|
native.stopDemoServer();
|
|
return;
|
|
}
|
|
await _invokeIgnoringMissing('disconnect');
|
|
}
|
|
|
|
Future<void> sendControl(String type, Map<String, dynamic> payload) async {
|
|
final native = _native;
|
|
if (native != null) {
|
|
final sequence = _controlSequence++;
|
|
final msgType = _msgTypeFor(type);
|
|
if (msgType == null) return;
|
|
final bytes = _controlPayload(type, msgType, payload);
|
|
if (bytes == null) return;
|
|
native.sendDemoControl(sequence, msgType, bytes);
|
|
return;
|
|
}
|
|
await _invokeIgnoringMissing('control', {'type': type, ...payload});
|
|
}
|
|
|
|
int _controlSequence = 1;
|
|
|
|
static int? _msgTypeFor(String type) {
|
|
switch (type) {
|
|
case 'touch':
|
|
return 0;
|
|
case 'mouse':
|
|
return 1;
|
|
case 'key':
|
|
return 2;
|
|
case 'wheel':
|
|
return 3;
|
|
case 'clipboard':
|
|
return 4;
|
|
case 'ping':
|
|
return 5;
|
|
// 投屏请求/停止:被控端会弹窗确认后再开流。
|
|
case 'requestMirror':
|
|
case 'stopMirror':
|
|
return 8;
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static List<int>? _controlPayload(String type, int msgType, Map<String, dynamic> payload) {
|
|
int byte(String key, [int fallback = 0]) {
|
|
final value = payload[key];
|
|
return value is num ? value.toInt() : fallback;
|
|
}
|
|
|
|
List<int> le16(int value) => [value & 0xff, (value >> 8) & 0xff];
|
|
|
|
switch (type) {
|
|
case 'touch':
|
|
return [
|
|
byte('action'),
|
|
byte('pointerId'),
|
|
...le16(byte('x')),
|
|
...le16(byte('y')),
|
|
...le16(byte('pressure')),
|
|
];
|
|
case 'mouse':
|
|
return [
|
|
byte('action'),
|
|
...le16(byte('x')),
|
|
...le16(byte('y')),
|
|
byte('buttons'),
|
|
];
|
|
case 'key':
|
|
return [byte('action'), ...le16(byte('keycode')), ...le16(byte('modifiers'))];
|
|
case 'wheel':
|
|
return [...le16(byte('deltaX')), ...le16(byte('deltaY')), byte('buttons')];
|
|
case 'clipboard':
|
|
return (payload['text'] as String? ?? '').codeUnits;
|
|
case 'ping':
|
|
final ts = byte('timestamp');
|
|
return [
|
|
ts & 0xff,
|
|
(ts >> 8) & 0xff,
|
|
(ts >> 16) & 0xff,
|
|
(ts >> 24) & 0xff,
|
|
0, 0, 0, 0,
|
|
];
|
|
case 'requestMirror':
|
|
// action=0 请求投屏;可选的 UTF-8 说明跟在后面。
|
|
return [0, ...(payload['message'] as String? ?? '').codeUnits];
|
|
case 'stopMirror':
|
|
// action=1 请求停止投屏。
|
|
return [1, ...(payload['message'] as String? ?? '').codeUnits];
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<void> saveSettings(AppSettings settings) async {
|
|
await _invokeIgnoringMissing('settings', settings.toJson());
|
|
}
|
|
|
|
Future<bool> startLocalSignaling(int port) => _localSignaling.start(port: port);
|
|
|
|
Future<void> stopLocalSignaling() => _localSignaling.stop();
|
|
|
|
String localSignalingUrl(int port) => _localSignaling.url(port: port);
|
|
|
|
Future<String> resolveSignalingUrl(AppSettings settings) async {
|
|
if (settings.useLocalSignaling) {
|
|
return _localSignaling.url(port: settings.localSignalingPort);
|
|
}
|
|
return settings.signalingUrl;
|
|
}
|
|
|
|
/// 启动 Rust Core 的本机 demo 服务。
|
|
bool startDemoServer() =>
|
|
_native?.startDemoServer() ?? false;
|
|
|
|
void stopDemoServer() => _native?.stopDemoServer();
|
|
|
|
Map<String, dynamic> demoStatus() =>
|
|
_native?.demoStatus() ?? <String, dynamic>{'running': false};
|
|
|
|
/// 原生视频纹理 ID;无原生实现或纹理尚未注册时返回 null。
|
|
Future<int?> videoTextureId() async {
|
|
try {
|
|
final id = await _video.invokeMethod<int>('textureId');
|
|
if (id == null || id <= 0) return null;
|
|
return id;
|
|
} on MissingPluginException {
|
|
return null;
|
|
} on PlatformException {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// 原生视频解码统计:textureId/decoded/pending/received/width/height/pts/error。
|
|
Future<Map<String, dynamic>> videoStats() async {
|
|
try {
|
|
final stats = await _video.invokeMapMethod<String, dynamic>('stats');
|
|
return stats ?? const <String, dynamic>{};
|
|
} on MissingPluginException {
|
|
// 老构建产物(runner 里没有视频纹理插件)会走到这里,必须显式提示,
|
|
// 否则界面只会停在「视频通道已建立」,看不出是二进制过期。
|
|
return const <String, dynamic>{
|
|
'error': '当前程序缺少原生视频插件(旧版构建),请重新构建/重新解压发布包',
|
|
};
|
|
} on PlatformException {
|
|
return const <String, dynamic>{};
|
|
}
|
|
}
|
|
|
|
/// 把串流画面弹出为独立原生窗口(Windows);返回是否成功。
|
|
///
|
|
/// [width] / [height] 是被控端屏幕尺寸提示:传了就按该比例开局,
|
|
/// 不再先弹出一个 16:9 的大窗口等首帧再缩回去。
|
|
Future<bool> openVideoWindow({int? width, int? height}) async {
|
|
try {
|
|
final arguments = (width != null && height != null && width > 0 && height > 0)
|
|
? <String, dynamic>{'width': width, 'height': height}
|
|
: null;
|
|
final opened = await _video.invokeMethod<bool>('openWindow', arguments);
|
|
return opened ?? false;
|
|
} on MissingPluginException {
|
|
return false;
|
|
} on PlatformException {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// 关闭独立串流窗口,把画面收回主窗口。
|
|
Future<void> closeVideoWindow() async {
|
|
try {
|
|
await _video.invokeMethod<bool>('closeWindow');
|
|
} on MissingPluginException {
|
|
// 老构建没有独立窗口能力。
|
|
} on PlatformException {
|
|
// 关闭失败无需打扰用户。
|
|
}
|
|
}
|
|
|
|
Future<String?> pairingUri() async {
|
|
try {
|
|
final value = await _method.invokeMethod<String>('pairingUri');
|
|
if (value != null && value.isNotEmpty) return value;
|
|
} on MissingPluginException {
|
|
// 桌面端无原生配对桥。
|
|
} on PlatformException {
|
|
// 原生桥返回错误。
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<void> _invokeIgnoringMissing(String method, [Object? argument]) async {
|
|
try {
|
|
await _method.invokeMethod<void>(method, argument);
|
|
} on MissingPluginException {
|
|
// 当前平台没有原生实现,忽略该调用。
|
|
}
|
|
}
|
|
}
|