368 lines
11 KiB
Dart
368 lines
11 KiB
Dart
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 = const <DeviceInfo>[];
|
|
DeviceInfo? _selected;
|
|
ConnectionPhase _phase = ConnectionPhase.idle;
|
|
AppSettings _settings = AppSettings.defaults;
|
|
String _effectiveSignalingUrl = AppSettings.defaults.signalingUrl;
|
|
bool _localSignalingRunning = false;
|
|
bool _refreshing = false;
|
|
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();
|
|
_demoStatusTimer = Timer.periodic(
|
|
const Duration(seconds: 1),
|
|
(_) => _refreshDemoStatus(),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_demoStatusTimer?.cancel();
|
|
_bridge.stopDemoServer();
|
|
_bridge.stopLocalSignaling();
|
|
_nativeCore?.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _refreshDemoStatus() {
|
|
if (_nativeCore == null || !mounted) return;
|
|
try {
|
|
final status = _bridge.demoStatus();
|
|
setState(() {
|
|
_demoStatus = status;
|
|
final running = status['running'] == true;
|
|
final connected = status['peerConnected'] == true;
|
|
final keyEstablished = status['keyEstablished'] == true;
|
|
if (running && connected && keyEstablished) {
|
|
_phase = ConnectionPhase.direct;
|
|
} else if (running && _phase != ConnectionPhase.idle) {
|
|
_phase = ConnectionPhase.probing;
|
|
}
|
|
});
|
|
} on Object catch (_) {
|
|
// 动态库不可用时忽略轮询错误。
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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;
|
|
if (uri == null) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('当前平台不支持扫码配对')),
|
|
);
|
|
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 _buildConnectionStatusCard() {
|
|
final status = _demoStatus;
|
|
final connected = status['peerConnected'] == true;
|
|
final keyEstablished = status['keyEstablished'] == true;
|
|
final peerIp = (status['peerIp'] as String? ?? '').trim();
|
|
|
|
return Card(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(12),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.link, size: 18),
|
|
const SizedBox(width: 8),
|
|
const Text('连接状态', style: TextStyle(fontWeight: FontWeight.bold)),
|
|
],
|
|
),
|
|
const SizedBox(height: 8),
|
|
if (_nativeError != null)
|
|
Text(
|
|
_nativeError!,
|
|
style: TextStyle(color: Theme.of(context).colorScheme.error, fontSize: 12),
|
|
)
|
|
else
|
|
Text(
|
|
'被控端 ${connected ? '已连接' : '未连接'} · '
|
|
'${keyEstablished ? '会话密钥已建立' : '等待 ECDH 协商'}' +
|
|
(peerIp.isEmpty ? '' : ' · 对端 $peerIp'),
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildStage() {
|
|
return Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
VideoView(
|
|
textureId: null,
|
|
connected: _phase.isConnected,
|
|
resolution: _selected?.resolution ?? '',
|
|
),
|
|
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),
|
|
_buildConnectionStatusCard(),
|
|
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,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|