feat: 接入端到端 demo 与签名变体
- Flutter 桌面端通过 dart:ffi 调用 Rust Core cdylib - 实现 ECDH P-256 密钥协商、HKDF 子密钥派生、AES-256-GCM 控制帧 - Android 被控端用派生密钥做控制帧加密与媒体包 MAC - 新增 libopus JNI 骨架,默认回退 MediaCodec - 新增 aosp/zhanxun/neutral 签名变体与系统 sharedUserId - 本机独立信令服务与设置页开关 - 新增 .gitignore 排除所有编译产物与打包目录
This commit is contained in:
commit
31b9a52eb4
154 files changed
+13287
No files matched your search
@@ -0,0 +1,409 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'core/bridge.dart';
|
||||
import 'core/native_core.dart';
|
||||
import 'core/models.dart';
|
||||
import 'features/control/control_toolbar.dart';
|
||||
import 'features/discovery/device_list.dart';
|
||||
import 'features/media/video_view.dart';
|
||||
import 'features/session/connect_bar.dart';
|
||||
import 'features/settings/settings_page.dart';
|
||||
|
||||
void main() => runApp(const TTSTDConnectApp());
|
||||
|
||||
class TTSTDConnectApp extends StatelessWidget {
|
||||
const TTSTDConnectApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'TTSTD Connect',
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: ThemeData(
|
||||
useMaterial3: true,
|
||||
colorSchemeSeed: const Color(0xFF5468FF),
|
||||
brightness: Brightness.dark,
|
||||
),
|
||||
home: const HomePage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class HomePage extends StatefulWidget {
|
||||
const HomePage({super.key});
|
||||
|
||||
@override
|
||||
State<HomePage> createState() => _HomePageState();
|
||||
}
|
||||
|
||||
class _HomePageState extends State<HomePage> {
|
||||
NativeCore? _nativeCore;
|
||||
late final SenderBridge _bridge;
|
||||
Timer? _demoStatusTimer;
|
||||
|
||||
List<DeviceInfo> _devices = SenderBridge.mockDevices;
|
||||
DeviceInfo? _selected = SenderBridge.mockDevices.first;
|
||||
ConnectionPhase _phase = ConnectionPhase.idle;
|
||||
AppSettings _settings = AppSettings.defaults;
|
||||
String _effectiveSignalingUrl = AppSettings.defaults.signalingUrl;
|
||||
bool _localSignalingRunning = false;
|
||||
bool _refreshing = false;
|
||||
Map<String, dynamic>? _selfTest;
|
||||
Map<String, dynamic> _demoStatus = const <String, dynamic>{'running': false};
|
||||
String? _nativeError;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
try {
|
||||
_nativeCore = NativeCore();
|
||||
} on Object catch (error) {
|
||||
_nativeError = error.toString();
|
||||
}
|
||||
_bridge = SenderBridge(nativeCore: _nativeCore);
|
||||
_bridge.watchPhase().listen((phase) {
|
||||
if (mounted) setState(() => _phase = phase);
|
||||
});
|
||||
_applySignalingSettings();
|
||||
_refreshDevices();
|
||||
_runSelfTest();
|
||||
_demoStatusTimer = Timer.periodic(
|
||||
const Duration(seconds: 1),
|
||||
(_) => _refreshDemoStatus(),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_demoStatusTimer?.cancel();
|
||||
_bridge.stopDemoServer();
|
||||
_bridge.stopLocalSignaling();
|
||||
_nativeCore?.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _runSelfTest() {
|
||||
if (_nativeCore == null) return;
|
||||
try {
|
||||
final result = _bridge.runCryptoSelfTest();
|
||||
if (mounted) setState(() => _selfTest = result);
|
||||
} on Object catch (error) {
|
||||
if (mounted) setState(() => _nativeError = error.toString());
|
||||
}
|
||||
}
|
||||
|
||||
void _refreshDemoStatus() {
|
||||
if (_nativeCore == null || !mounted) return;
|
||||
try {
|
||||
final status = _bridge.demoStatus();
|
||||
setState(() => _demoStatus = status);
|
||||
} on Object catch (_) {
|
||||
// 动态库不可用时忽略轮询错误。
|
||||
}
|
||||
}
|
||||
|
||||
void _toggleDemoServer() {
|
||||
if (_nativeCore == null) return;
|
||||
try {
|
||||
final running = _demoStatus['running'] == true;
|
||||
if (running) {
|
||||
_bridge.stopDemoServer();
|
||||
} else {
|
||||
_bridge.startDemoServer();
|
||||
}
|
||||
_refreshDemoStatus();
|
||||
} on Object catch (error) {
|
||||
if (mounted) setState(() => _nativeError = error.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _applySignalingSettings() async {
|
||||
if (_settings.localSignalingEnabled) {
|
||||
final started = await _bridge.startLocalSignaling(_settings.localSignalingPort);
|
||||
if (!mounted) return;
|
||||
setState(() => _localSignalingRunning = started);
|
||||
} else {
|
||||
await _bridge.stopLocalSignaling();
|
||||
if (!mounted) return;
|
||||
setState(() => _localSignalingRunning = false);
|
||||
}
|
||||
final url = await _bridge.resolveSignalingUrl(_settings);
|
||||
if (!mounted) return;
|
||||
setState(() => _effectiveSignalingUrl = url);
|
||||
}
|
||||
|
||||
Future<void> _refreshDevices() async {
|
||||
if (_refreshing) return;
|
||||
setState(() => _refreshing = true);
|
||||
final devices = await _bridge.discover();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_devices = devices;
|
||||
if (_selected == null || !devices.any((device) => device.id == _selected!.id)) {
|
||||
_selected = devices.isEmpty ? null : devices.first;
|
||||
}
|
||||
_refreshing = false;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _connect() async {
|
||||
final device = _selected;
|
||||
if (device == null) return;
|
||||
setState(() => _phase = ConnectionPhase.probing);
|
||||
await _bridge.connect(device);
|
||||
if (!mounted) return;
|
||||
setState(() => _phase = ConnectionPhase.direct);
|
||||
}
|
||||
|
||||
Future<void> _disconnect() async {
|
||||
await _bridge.disconnect();
|
||||
if (!mounted) return;
|
||||
setState(() => _phase = ConnectionPhase.idle);
|
||||
}
|
||||
|
||||
Future<void> _openSettings() async {
|
||||
final updated = await Navigator.of(context).push<AppSettings>(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => SettingsPage(settings: _settings, bridge: _bridge),
|
||||
),
|
||||
);
|
||||
if (updated == null) return;
|
||||
setState(() => _settings = updated);
|
||||
await _bridge.saveSettings(updated);
|
||||
await _applySignalingSettings();
|
||||
}
|
||||
|
||||
Future<void> _showPairing() async {
|
||||
final uri = await _bridge.pairingUri();
|
||||
if (!mounted) return;
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('扫码配对'),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Container(
|
||||
width: 180,
|
||||
height: 180,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: const Icon(Icons.qr_code_2, size: 140, color: Colors.black87),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
SelectableText(uri, textAlign: TextAlign.center),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
'首次连接需在被控端确认指纹;配对时可选择“仅查看 / 查看+控制 / 全权限”。',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text('关闭'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _sendControl(String type, Map<String, dynamic> payload) {
|
||||
_bridge.sendControl(type, payload);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('已发送 $type 控制事件'), duration: const Duration(milliseconds: 900)),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('TTSTD Connect'),
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: '扫码配对',
|
||||
onPressed: _showPairing,
|
||||
icon: const Icon(Icons.qr_code_scanner),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: '设置',
|
||||
onPressed: _openSettings,
|
||||
icon: const Icon(Icons.settings),
|
||||
),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: _showPairing,
|
||||
tooltip: '扫码配对',
|
||||
child: const Icon(Icons.add_link),
|
||||
),
|
||||
body: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
if (constraints.maxWidth >= 1000) {
|
||||
return Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 360,
|
||||
child: Card(
|
||||
margin: const EdgeInsets.all(12),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: DeviceList(
|
||||
devices: _devices,
|
||||
selectedId: _selected?.id,
|
||||
onSelected: (device) => setState(() => _selected = device),
|
||||
onRefresh: _refreshDevices,
|
||||
refreshing: _refreshing,
|
||||
),
|
||||
),
|
||||
),
|
||||
const VerticalDivider(width: 1),
|
||||
Expanded(child: SingleChildScrollView(child: _buildStage())),
|
||||
],
|
||||
);
|
||||
}
|
||||
return SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
SizedBox(
|
||||
height: 360,
|
||||
child: Card(
|
||||
margin: const EdgeInsets.fromLTRB(12, 12, 12, 0),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: DeviceList(
|
||||
devices: _devices,
|
||||
selectedId: _selected?.id,
|
||||
onSelected: (device) => setState(() => _selected = device),
|
||||
onRefresh: _refreshDevices,
|
||||
refreshing: _refreshing,
|
||||
),
|
||||
),
|
||||
),
|
||||
_buildStage(),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildNativeDemoCard() {
|
||||
final native = _nativeCore;
|
||||
final test = _selfTest;
|
||||
final status = _demoStatus;
|
||||
final running = status['running'] == true;
|
||||
final connected = status['peerConnected'] == true;
|
||||
final keyEstablished = status['keyEstablished'] == true;
|
||||
final events = (status['events'] as List<dynamic>? ?? const <dynamic>[])
|
||||
.map((item) => item.toString())
|
||||
.toList();
|
||||
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.bolt_outlined, size: 18),
|
||||
const SizedBox(width: 8),
|
||||
const Text('Rust Core 协议自检', style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
const Spacer(),
|
||||
if (native != null)
|
||||
TextButton(
|
||||
onPressed: _toggleDemoServer,
|
||||
child: Text(running ? '停止 demo 服务' : '启动 demo 服务'),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (_nativeError != null)
|
||||
Text(
|
||||
_nativeError!,
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.error, fontSize: 12),
|
||||
)
|
||||
else if (native == null)
|
||||
const Text('未加载 Rust Core 动态库(桌面端编译后自动可用)。')
|
||||
else
|
||||
Text(
|
||||
'版本 ${native.versionString} · ABI ${native.abiRevision()} · '
|
||||
'ECDH ${test?['ecdhMatch'] == true ? '一致' : '未验证'} · '
|
||||
'控制帧 ${test?['controlFrameBytes'] ?? '-'} B · '
|
||||
'媒体包 ${test?['mediaPacketBytes'] ?? '-'} B',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'demo 服务:${running ? '运行中' : '已停止'} · '
|
||||
'被控端 ${connected ? '已连接' : '未连接'} · '
|
||||
'${keyEstablished ? '会话密钥已建立' : '等待 ECDH'} · '
|
||||
'密钥 ${status['controlKey'] ?? '----'}',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
if (events.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(
|
||||
events.take(5).join('\n'),
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(fontSize: 11),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStage() {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
VideoView(
|
||||
textureId: null,
|
||||
connected: _phase.isConnected,
|
||||
resolution: _selected?.resolution ?? '1920x1080',
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ConnectBar(
|
||||
phase: _phase,
|
||||
device: _selected,
|
||||
onConnect: _connect,
|
||||
onDisconnect: _disconnect,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ControlToolbar(
|
||||
enabled: _phase.isConnected,
|
||||
onEvent: _sendControl,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_buildNativeDemoCard(),
|
||||
const SizedBox(height: 12),
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Text(
|
||||
_settings.useLocalSignaling
|
||||
? 'mDNS 同网段优先;本机信令${_localSignalingRunning ? '运行中' : '未运行'} · $_effectiveSignalingUrl,探测失败自动降级到信令中转。'
|
||||
: 'mDNS 同网段优先;外部信令 · $_effectiveSignalingUrl,探测失败自动降级到信令中转。',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user