- Rust: 新增 media::stream 访问单元队列与分片重组,FFI 增加 ttstd_video_next/pending/stats(ABI 2) - Windows: runner 内嵌 Media Foundation 解码纹理插件(NV12/RGB32→BGRA),开启 CODECAPI_AVLowLatencyMode 消除约 36 帧积压,按显示区域裁掉 16 对齐补齐行 - Android: 大帧按 1200 字节分片发送;csd(SPS/PPS) 随每个关键帧重发,修复先投屏后连接时只收到 P 帧导致的黑屏 - Flutter: 独立 ttstd/video 通道获取 textureId 与统计,VideoView 渲染 Texture 并显示分辨率/PTS/失败原因 - 诊断: stats 增加 format/auBytes/keyframes/submitHr;缺插件、缺 SPS/PPS 显式提示;TTSTD_DUMP_VIDEO 可抓码流配合 ffprobe 定位 - 发现与服务检查: mDNS 发现客户端、服务地址探测、设置页与连接状态卡片(启动本机监听/停止) - 工具与文档: video_feed 推流示例、一键打包脚本、DEMO 出画面排查表与实测截图
264 lines
7.5 KiB
Dart
264 lines
7.5 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;
|
|
|
|
final MethodChannel _method;
|
|
final EventChannel _events;
|
|
final NativeCore? _native;
|
|
final LocalSignalingProcess _localSignaling = LocalSignalingProcess();
|
|
|
|
/// 原生视频解码纹理通道(Windows runner 内嵌插件)。
|
|
static const MethodChannel _video = MethodChannel('ttstd/video');
|
|
|
|
NativeCore? get nativeCore => _native;
|
|
|
|
bool get isLocalSignalingRunning => _localSignaling.isRunning;
|
|
|
|
Stream<ConnectionPhase> watchPhase() {
|
|
return _events
|
|
.receiveBroadcastStream()
|
|
.handleError((Object _) => null)
|
|
.map<ConnectionPhase>(ConnectionPhaseX.fromWire)
|
|
.distinct();
|
|
}
|
|
|
|
Future<List<DeviceInfo>> discover({String? signalingUrl}) async {
|
|
if (signalingUrl != null && signalingUrl.trim().isNotEmpty) {
|
|
final discovered = await DiscoveryClient.listDevices(signalingUrl);
|
|
if (discovered.isNotEmpty) return discovered;
|
|
}
|
|
if (_native != null) {
|
|
return const <DeviceInfo>[];
|
|
}
|
|
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) {
|
|
native.startDemoServer();
|
|
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;
|
|
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,
|
|
];
|
|
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>{};
|
|
}
|
|
}
|
|
|
|
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 {
|
|
// 当前平台没有原生实现,忽略该调用。
|
|
}
|
|
}
|
|
}
|