import 'dart:async'; import 'package:flutter/services.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(); NativeCore? get nativeCore => _native; bool get isLocalSignalingRunning => _localSignaling.isRunning; static const mockDevices = [ DeviceInfo( id: 'demo-pixel', name: '客厅 Pixel', ip: '192.168.1.10', mediaPort: 41000, controlPort: 41001, fingerprint: 'sha256:ab12cd34ef567890', capabilities: ['h264', 'opus', 'uibc', 'clipboard', 'file'], model: 'Pixel8', resolution: '1920x1080', ), DeviceInfo( id: 'demo-tablet', name: '书房平板', ip: '192.168.1.23', mediaPort: 42000, controlPort: 42001, fingerprint: 'sha256:9988776655443322', capabilities: ['h265', 'opus', 'uibc'], model: 'PadPro', resolution: '2560x1600', ), ]; Stream watchPhase() { return _events .receiveBroadcastStream() .handleError((Object _) => null) .map(ConnectionPhaseX.fromWire) .distinct(); } Future> discover() async { try { final raw = await _method.invokeListMethod('discover'); if (raw == null || raw.isEmpty) return mockDevices; return DeviceInfo.listFromJson(raw); } on MissingPluginException { return mockDevices; } on PlatformException { return mockDevices; } } Future connect(DeviceInfo device) async { await _invokeIgnoringMissing('connect', device.toJson()); } Future disconnect() async { await _invokeIgnoringMissing('disconnect'); } Future sendControl(String type, Map payload) async { await _invokeIgnoringMissing('control', {'type': type, ...payload}); } Future saveSettings(AppSettings settings) async { await _invokeIgnoringMissing('settings', settings.toJson()); } Future startLocalSignaling(int port) => _localSignaling.start(port: port); Future stopLocalSignaling() => _localSignaling.stop(); String localSignalingUrl(int port) => _localSignaling.url(port: port); Future resolveSignalingUrl(AppSettings settings) async { if (settings.useLocalSignaling) { return _localSignaling.url(port: settings.localSignalingPort); } return settings.signalingUrl; } /// 通过 Rust Core 跑一次真实 ECDH + HKDF + AES-GCM + 媒体 MAC 自检。 Map runCryptoSelfTest() { final native = _native; if (native == null) { return {'available': false, 'reason': 'Rust Core 未加载'}; } final alice = native.generateKeyPair(); final bob = native.generateKeyPair(); try { final aliceSecret = alice.agree(bob.publicBytes); final bobSecret = bob.agree(alice.publicBytes); final controlKey = native.deriveSubkey(aliceSecret, 'lancast/control/v1'); final videoKey = native.deriveSubkey(aliceSecret, 'lancast/video/v1'); final channel = native.createSecureChannel(controlKey); final frame = channel.encodePing(1, DateTime.now().millisecondsSinceEpoch); final decoded = channel.decode(frame); channel.dispose(); final media = native.mediaEncode( 1234, 1, videoKey, const [1, 2, 3, 4, 5], ); final mediaDecoded = native.mediaDecode(media, videoKey); return { 'available': true, 'ecdhMatch': aliceSecret.join() == bobSecret.join(), 'controlFrameBytes': frame.length, 'controlKind': decoded['kind'], 'mediaPacketBytes': media.length, 'mediaPts': mediaDecoded['pts'], 'controlKey': _hexPrefix(controlKey), 'videoKey': _hexPrefix(videoKey), }; } finally { alice.free(); bob.free(); } } /// 启动 Rust Core 的本机 demo 服务。 bool startDemoServer() => _native?.startDemoServer() ?? false; void stopDemoServer() => _native?.stopDemoServer(); Map demoStatus() => _native?.demoStatus() ?? {'running': false}; static String _hexPrefix(List bytes) => bytes .take(4) .map((byte) => byte.toRadixString(16).padLeft(2, '0')) .join(); Future pairingUri() async { try { final value = await _method.invokeMethod('pairingUri'); if (value != null && value.isNotEmpty) return value; } on MissingPluginException { // 桌面/测试环境没有原生桥时给出可扫描的示例 URI。 } on PlatformException { // 同上。 } return 'lancast://pair?ip=192.168.1.10&port=41001&fp=sha256:ab12cd34ef567890'; } Future _invokeIgnoringMissing(String method, [Object? argument]) async { try { await _method.invokeMethod(method, argument); } on MissingPluginException { // 测试或尚未接入 Rust Core 时使用 UI 演示数据。 } } }