feat(video): 打通桌面端真实视频画面链路

- 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 出画面排查表与实测截图
This commit is contained in:
tongtong committed 2026-11-27 04:29:40 +08:00
1 parent adaa23f895
commit df2affa9e2
30 files changed
+2814 -134

No files matched your search

+196 -7
View File
@@ -1,8 +1,10 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'core/bridge.dart';
import 'core/diagnostic_log.dart';
import 'core/native_core.dart';
import 'core/models.dart';
import 'features/control/control_toolbar.dart';
@@ -41,7 +43,12 @@ class HomePage extends StatefulWidget {
class _HomePageState extends State<HomePage> {
NativeCore? _nativeCore;
late final SenderBridge _bridge;
final DiagnosticLog _diagnosticLog = DiagnosticLog();
Timer? _demoStatusTimer;
Timer? _diagnosticTimer;
Timer? _connectWatchdog;
Timer? _discoveryTimer;
Timer? _videoStatsTimer;
List<DeviceInfo> _devices = const <DeviceInfo>[];
DeviceInfo? _selected;
@@ -51,6 +58,8 @@ class _HomePageState extends State<HomePage> {
bool _localSignalingRunning = false;
bool _refreshing = false;
Map<String, dynamic> _demoStatus = const <String, dynamic>{'running': false};
Map<String, dynamic> _videoStats = const <String, dynamic>{};
int? _textureId;
String? _nativeError;
@override
@@ -62,7 +71,10 @@ class _HomePageState extends State<HomePage> {
_nativeError = error.toString();
}
_bridge = SenderBridge(nativeCore: _nativeCore);
_diagnosticLog.start();
_diagnosticLog.write('app initialized; nativeError=${_nativeError ?? '-'}');
_bridge.watchPhase().listen((phase) {
_diagnosticLog.write('phase event -> ${phase.wireName} (${phase.label})');
if (mounted) setState(() => _phase = phase);
});
_applySignalingSettings();
@@ -71,11 +83,34 @@ class _HomePageState extends State<HomePage> {
const Duration(seconds: 1),
(_) => _refreshDemoStatus(),
);
_diagnosticTimer = Timer.periodic(
const Duration(seconds: 1),
(_) => _writeDiagnosticLog(),
);
_discoveryTimer = Timer.periodic(
const Duration(seconds: 5),
(_) => _refreshDevices(),
);
_videoStatsTimer = Timer.periodic(
const Duration(milliseconds: 500),
(_) => _refreshVideoStats(),
);
_refreshVideoTextureId();
// 自测/演示用开关:TTSTD_DEMO_AUTOSTART=1 时启动即开启本机监听。
if (Platform.environment['TTSTD_DEMO_AUTOSTART'] == '1') {
_startDemoListener();
}
_writeDiagnosticLog();
}
@override
void dispose() {
_demoStatusTimer?.cancel();
_diagnosticTimer?.cancel();
_connectWatchdog?.cancel();
_discoveryTimer?.cancel();
_videoStatsTimer?.cancel();
_diagnosticLog.dispose();
_bridge.stopDemoServer();
_bridge.stopLocalSignaling();
_nativeCore?.dispose();
@@ -92,16 +127,65 @@ class _HomePageState extends State<HomePage> {
final connected = status['peerConnected'] == true;
final keyEstablished = status['keyEstablished'] == true;
if (running && connected && keyEstablished) {
_connectWatchdog?.cancel();
_phase = ConnectionPhase.direct;
} else if (running && _phase != ConnectionPhase.idle) {
} else if (running &&
_phase != ConnectionPhase.idle &&
_phase != ConnectionPhase.failed) {
_phase = ConnectionPhase.probing;
}
});
} on Object catch (_) {
// 动态库不可用时忽略轮询错误。
} on Object catch (error) {
_diagnosticLog.write('demoStatus refresh failed: $error');
}
}
/// 首帧前先取一次纹理 ID,避免等待轮询。
Future<void> _refreshVideoTextureId() async {
final id = await _bridge.videoTextureId();
if (!mounted || id == null || id == _textureId) return;
setState(() => _textureId = id);
}
/// 轮询原生解码统计:纹理 ID、分辨率、PTS 与失败原因。
Future<void> _refreshVideoStats() async {
if (!mounted) return;
final stats = await _bridge.videoStats();
if (!mounted || stats.isEmpty) return;
final id = stats['textureId'];
setState(() {
_videoStats = stats;
if (_textureId == null && id is int && id > 0) {
_textureId = id;
}
});
}
void _writeDiagnosticLog() {
Map<String, dynamic> status;
if (_nativeCore == null) {
status = <String, dynamic>{'available': false, 'error': _nativeError ?? ''};
} else {
try {
status = _bridge.demoStatus();
} on Object catch (error) {
status = <String, dynamic>{'available': true, 'error': error.toString()};
}
}
final selected = _selected;
final target = selected == null
? '-'
: '${selected.id} ${selected.ip}:${selected.mediaPort}/${selected.controlPort}';
_diagnosticLog.write(
'phase=${_phase.wireName} label=${_phase.label}; '
'devices=${_devices.length} target=$target refreshing=$_refreshing; '
'signaling local=${_settings.useLocalSignaling} '
'running=$_localSignalingRunning url=$_effectiveSignalingUrl; '
'nativeError=${_nativeError ?? '-'}; textureId=${_textureId ?? '-'}; '
'video=$_videoStats; demoStatus=$status',
);
}
Future<void> _applySignalingSettings() async {
if (_settings.localSignalingEnabled) {
final started = await _bridge.startLocalSignaling(_settings.localSignalingPort);
@@ -115,12 +199,13 @@ class _HomePageState extends State<HomePage> {
final url = await _bridge.resolveSignalingUrl(_settings);
if (!mounted) return;
setState(() => _effectiveSignalingUrl = url);
unawaited(_refreshDevices());
}
Future<void> _refreshDevices() async {
if (_refreshing) return;
setState(() => _refreshing = true);
final devices = await _bridge.discover();
final devices = await _bridge.discover(signalingUrl: _effectiveSignalingUrl);
if (!mounted) return;
setState(() {
_devices = devices;
@@ -129,16 +214,42 @@ class _HomePageState extends State<HomePage> {
}
_refreshing = false;
});
_diagnosticLog.write('discover result: ${devices.length} devices');
}
Future<void> _connect() async {
final device = _selected;
if (device == null) return;
_diagnosticLog.write(
'connect requested: ${device.id} ${device.ip}:${device.mediaPort}/${device.controlPort}',
);
_connectWatchdog?.cancel();
setState(() => _phase = ConnectionPhase.probing);
await _bridge.connect(device);
_connectWatchdog = Timer(const Duration(seconds: 10), () {
if (!mounted || _phase != ConnectionPhase.probing) return;
_diagnosticLog.write('connect timeout after 10s');
_writeDiagnosticLog();
setState(() => _phase = ConnectionPhase.failed);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('连接超时:被控端未接入,请检查 IP、端口和网络')),
);
});
try {
await _bridge.connect(device);
} on Object catch (error) {
_connectWatchdog?.cancel();
_diagnosticLog.write('connect failed: $error');
if (!mounted) return;
setState(() => _phase = ConnectionPhase.failed);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('连接失败:$error')),
);
}
}
Future<void> _disconnect() async {
_diagnosticLog.write('disconnect requested');
_connectWatchdog?.cancel();
await _bridge.disconnect();
if (!mounted) return;
setState(() => _phase = ConnectionPhase.idle);
@@ -317,12 +428,88 @@ class _HomePageState extends State<HomePage> {
(peerIp.isEmpty ? '' : ' · 对端 $peerIp'),
style: Theme.of(context).textTheme.bodySmall,
),
const SizedBox(height: 6),
Text(
"诊断日志:${_diagnosticLog.path.isEmpty ? '不可写' : _diagnosticLog.path}",
style: Theme.of(context).textTheme.bodySmall,
),
const SizedBox(height: 10),
Row(
children: [
FilledButton.tonalIcon(
onPressed: _nativeError != null || _demoRunning
? null
: _startDemoListener,
icon: const Icon(Icons.dns_outlined, size: 18),
label: const Text('启动本机监听'),
),
const SizedBox(width: 8),
OutlinedButton.icon(
onPressed: _demoRunning ? _stopDemoListener : null,
icon: const Icon(Icons.stop_circle_outlined, size: 18),
label: const Text('停止'),
),
const SizedBox(width: 12),
Expanded(
child: Text(
_demoRunning
? 'TCP 41001 / UDP 41000 已监听,等待被控端接入'
: '监听未开启:被控端无法反连本机',
style: Theme.of(context).textTheme.bodySmall,
overflow: TextOverflow.ellipsis,
),
),
],
),
],
),
),
);
}
bool get _demoRunning => _demoStatus['running'] == true;
void _startDemoListener() {
final started = _bridge.startDemoServer();
_diagnosticLog.write('demo listener start -> $started');
if (!mounted) return;
setState(() {
if (started) {
_phase = ConnectionPhase.probing;
}
});
if (!started) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('启动本机监听失败:端口可能被占用')),
);
}
}
void _stopDemoListener() {
_bridge.stopDemoServer();
_diagnosticLog.write('demo listener stopped');
if (!mounted) return;
setState(() => _phase = ConnectionPhase.idle);
}
String get _videoResolution {
final width = _videoStats['width'];
final height = _videoStats['height'];
if (width is int && height is int && width > 0 && height > 0) {
return '${width}x$height';
}
return _selected?.resolution ?? '';
}
int? get _videoPts {
final decoded = _videoStats['decoded'];
final pts = _videoStats['pts'];
if (decoded is int && decoded > 0 && pts is int) return pts;
return null;
}
String get _videoError => (_videoStats['error'] as String? ?? '').trim();
Widget _buildStage() {
return Padding(
padding: const EdgeInsets.all(16),
@@ -330,9 +517,11 @@ class _HomePageState extends State<HomePage> {
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
VideoView(
textureId: null,
textureId: _textureId,
connected: _phase.isConnected,
resolution: _selected?.resolution ?? '',
resolution: _videoResolution,
pts: _videoPts,
error: _videoError,
),
const SizedBox(height: 16),
ConnectBar(