feat(connection): 增加远程视频录制、分辨率/帧率切换及高刷适配
- 新增 VideoRecorder 基于 flutter_webrtc MediaRecorder 录制远程视频为 MP4 - 新增 ControlMessageDecoder 解析被控端上报的 REPORT_* protobuf 消息 - 连接控制器支持分辨率/帧率切换、录制状态管理与远程上报处理 - 远程控制页新增更多菜单(静音/分辨率/帧率/录制),并统一退出时断开连接 - WebRTC 编排器在 DataChannel 打开及媒体就绪后主动请求默认分辨率,解决黑屏问题 - 新增 DisplayModeUtil 在 Android 上启用最高刷新率 - 登出时失效设备相关 Provider,避免切换账号后读到旧缓存
This commit is contained in:
1 parent
41a514e9cc
commit
bfb106f1fb
13 files changed
+1267
-514
No files matched your search
@@ -3,30 +3,84 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_webrtc/flutter_webrtc.dart';
|
||||
|
||||
import '../data/connection_providers.dart';
|
||||
import '../data/protobuf_codec.dart';
|
||||
import '../data/video_recorder.dart';
|
||||
import '../data/webrtc_orchestrator.dart';
|
||||
import '../domain/connection_models.dart';
|
||||
|
||||
/// 分辨率预设(与被控端 SET_RESOLUTION 语义一致:width<=0 表示原始)。
|
||||
const List<Map<String, Object>> kResolutionOptions = [
|
||||
{'label': '原始', 'width': 0, 'height': 0, 'fps': 0},
|
||||
{'label': '1080P', 'width': 1920, 'height': 0, 'fps': 0},
|
||||
{'label': '720P', 'width': 1280, 'height': 0, 'fps': 0},
|
||||
{'label': '480P', 'width': 854, 'height': 0, 'fps': 0},
|
||||
];
|
||||
|
||||
/// 默认帧率档位(收到被控端上报的 supported_fps 后以上报列表为准)。
|
||||
const List<int> kDefaultFpsOptions = [15, 24, 30, 60];
|
||||
|
||||
/// 远程控制会话状态(供 UI 渲染)。
|
||||
class ConnectionSessionState {
|
||||
final ConnectionState state;
|
||||
final String? errorMessage;
|
||||
final bool hasVideo;
|
||||
|
||||
/// 是否正在录制远程视频。
|
||||
final bool recording;
|
||||
/// 录制状态提示(已保存路径等)。
|
||||
final String recordStatus;
|
||||
|
||||
/// 当前选中的分辨率预设下标。
|
||||
final int selectedResolution;
|
||||
/// 分辨率预设列表。
|
||||
final List<Map<String, Object>> resolutionOptions;
|
||||
/// 帧率档位(收到被控端上报后以上报列表为准)。
|
||||
final List<int> fpsOptions;
|
||||
/// 被控端当前采集帧率(0 表示尚未收到上报)。
|
||||
final int currentFps;
|
||||
/// 被控端最近上报的实际采集尺寸(切帧率时保持分辨率不变)。
|
||||
final int lastReportedWidth;
|
||||
final int lastReportedHeight;
|
||||
|
||||
const ConnectionSessionState({
|
||||
this.state = ConnectionState.idle,
|
||||
this.errorMessage,
|
||||
this.hasVideo = false,
|
||||
this.recording = false,
|
||||
this.recordStatus = '',
|
||||
this.selectedResolution = 1,
|
||||
this.resolutionOptions = kResolutionOptions,
|
||||
this.fpsOptions = kDefaultFpsOptions,
|
||||
this.currentFps = 0,
|
||||
this.lastReportedWidth = 0,
|
||||
this.lastReportedHeight = 0,
|
||||
});
|
||||
|
||||
ConnectionSessionState copyWith({
|
||||
ConnectionState? state,
|
||||
String? errorMessage,
|
||||
bool? hasVideo,
|
||||
bool? recording,
|
||||
String? recordStatus,
|
||||
int? selectedResolution,
|
||||
List<Map<String, Object>>? resolutionOptions,
|
||||
List<int>? fpsOptions,
|
||||
int? currentFps,
|
||||
int? lastReportedWidth,
|
||||
int? lastReportedHeight,
|
||||
}) {
|
||||
return ConnectionSessionState(
|
||||
state: state ?? this.state,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
hasVideo: hasVideo ?? this.hasVideo,
|
||||
recording: recording ?? this.recording,
|
||||
recordStatus: recordStatus ?? this.recordStatus,
|
||||
selectedResolution: selectedResolution ?? this.selectedResolution,
|
||||
resolutionOptions: resolutionOptions ?? this.resolutionOptions,
|
||||
fpsOptions: fpsOptions ?? this.fpsOptions,
|
||||
currentFps: currentFps ?? this.currentFps,
|
||||
lastReportedWidth: lastReportedWidth ?? this.lastReportedWidth,
|
||||
lastReportedHeight: lastReportedHeight ?? this.lastReportedHeight,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -34,6 +88,7 @@ class ConnectionSessionState {
|
||||
/// 远程控制控制器:管理连接状态机(idle → connecting → connected → ended)。
|
||||
class ConnectionController extends Notifier<ConnectionSessionState> {
|
||||
WebRtcOrchestrator? _orchestrator;
|
||||
final VideoRecorder _videoRecorder = VideoRecorder();
|
||||
|
||||
@override
|
||||
ConnectionSessionState build() {
|
||||
@@ -71,6 +126,9 @@ class ConnectionController extends Notifier<ConnectionSessionState> {
|
||||
_orchestrator!.onConnectionState = (connected) {
|
||||
_onRtcState(connected);
|
||||
};
|
||||
_orchestrator!.onReport = (report) {
|
||||
_onReport(report);
|
||||
};
|
||||
|
||||
signaling.onMessage = (message) {
|
||||
// 先透传给 WebRTC 编排器处理(ANSWER/ICE 等),确保握手能继续
|
||||
@@ -104,12 +162,33 @@ class ConnectionController extends Notifier<ConnectionSessionState> {
|
||||
|
||||
void _onRtcState(bool connected) {
|
||||
if (connected) {
|
||||
state = const ConnectionSessionState(
|
||||
state = state.copyWith(
|
||||
state: ConnectionState.connected,
|
||||
hasVideo: true,
|
||||
);
|
||||
} else if (state.state == ConnectionState.connected) {
|
||||
state = const ConnectionSessionState(state: ConnectionState.ended);
|
||||
// 连接成功后按默认分辨率(1080P)下发一次,确保默认生效。
|
||||
selectResolution(state.selectedResolution);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
state: ConnectionState.ended,
|
||||
hasVideo: false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 处理被控端经 DataChannel 上报的 REPORT_RESOLUTION / REPORT_STREAM_MODE。
|
||||
void _onReport(ReportInfo report) {
|
||||
if (report.width > 0 && report.height > 0) {
|
||||
state = state.copyWith(
|
||||
lastReportedWidth: report.width,
|
||||
lastReportedHeight: report.height,
|
||||
);
|
||||
}
|
||||
if (report.fps > 0) {
|
||||
state = state.copyWith(currentFps: report.fps);
|
||||
}
|
||||
if (report.supportedFps.isNotEmpty) {
|
||||
state = state.copyWith(fpsOptions: report.supportedFps);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,8 +222,68 @@ class ConnectionController extends Notifier<ConnectionSessionState> {
|
||||
_orchestrator?.sendSetResolution(width: width, height: height, fps: fps);
|
||||
}
|
||||
|
||||
/// 切换分辨率:写入状态并下发指令。
|
||||
void selectResolution(int index) {
|
||||
final options = state.resolutionOptions;
|
||||
if (index < 0 || index >= options.length) return;
|
||||
state = state.copyWith(selectedResolution: index);
|
||||
final o = options[index];
|
||||
_orchestrator?.sendSetResolution(
|
||||
width: o['width'] as int,
|
||||
height: o['height'] as int,
|
||||
fps: o['fps'] as int,
|
||||
);
|
||||
}
|
||||
|
||||
/// 切换帧率:仅切帧率,分辨率保持不变。
|
||||
void selectFps(int fps) {
|
||||
if (fps <= 0 || fps == state.currentFps) return;
|
||||
_orchestrator?.sendSetResolution(
|
||||
width: state.lastReportedWidth,
|
||||
height: state.lastReportedHeight,
|
||||
fps: fps,
|
||||
);
|
||||
}
|
||||
|
||||
/// 切换远程视频录制:开始 / 停止。
|
||||
Future<void> toggleRecord() async {
|
||||
if (state.recording) {
|
||||
await _stopRecording();
|
||||
return;
|
||||
}
|
||||
await _startRecording();
|
||||
}
|
||||
|
||||
Future<void> _startRecording() async {
|
||||
if (state.recording) return;
|
||||
final renderer = _orchestrator?.remoteRenderer;
|
||||
final stream = renderer?.srcObject;
|
||||
if (stream == null) {
|
||||
state = state.copyWith(recordStatus: '尚未接收到视频画面,无法录制');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final ok = await _videoRecorder.start(stream);
|
||||
if (ok) {
|
||||
state = state.copyWith(recording: true, recordStatus: '录制中...');
|
||||
}
|
||||
} catch (e) {
|
||||
state = state.copyWith(recordStatus: '');
|
||||
debugPrint('[record] 开始录制失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _stopRecording() async {
|
||||
final path = await _videoRecorder.stop();
|
||||
state = state.copyWith(
|
||||
recording: false,
|
||||
recordStatus: path != null ? '已保存:$path' : '录制已停止',
|
||||
);
|
||||
}
|
||||
|
||||
/// 关闭并清理。
|
||||
Future<void> disconnect() async {
|
||||
await _videoRecorder.dispose();
|
||||
await _orchestrator?.dispose();
|
||||
_orchestrator = null;
|
||||
ref.read(signalingClientProvider).dispose();
|
||||
|
||||
Reference in new issue
Block a user