播放器修改,功能完善
This commit is contained in:
@@ -1,20 +1,24 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:maibu_satabot_v2/core/logging/i_logger_service.dart';
|
||||
import 'package:maibu_satabot_v2/core/protocol/packet_entity.dart';
|
||||
import 'package:maibu_satabot_v2/features/remote_control/domain/entities/running_status_entity.dart';
|
||||
import 'package:maibu_satabot_v2/features/remote_control/presentation/bloc/remote_control_state.dart';
|
||||
import '../../../../core/di/injection.dart';
|
||||
import '../../../connectivity/presentation/bloc/connectivity_cubit.dart';
|
||||
import '../../domain/entities/machine_control_entity.dart';
|
||||
import '../../domain/repositories/remote_control_repository.dart';
|
||||
|
||||
class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
final RemoteControlRepository _repository;
|
||||
final ConnectivityCubit connectivityCubit;
|
||||
|
||||
// 定时器与流订阅管理
|
||||
Timer? _timer;
|
||||
StreamSubscription<RunningStatusEntity>? _statusSubscription;
|
||||
StreamSubscription<PacketEntity>? _responseSubscription;
|
||||
|
||||
RemoteControlCubit(this._repository)
|
||||
RemoteControlCubit(this._repository,this.connectivityCubit)
|
||||
: super(
|
||||
RemoteControlState(
|
||||
controlEntity: MachineControlEntity(),
|
||||
@@ -153,6 +157,32 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
void toggleLeftPip() => emit(state.copyWith(showLeftPip: !state.showLeftPip));
|
||||
void toggleRightPip() => emit(state.copyWith(showRightPip: !state.showRightPip));
|
||||
|
||||
void toggleTopLeftExpand() {
|
||||
emit(state.copyWith(topRightIsExpanded: !state.topRightIsExpanded));
|
||||
}
|
||||
|
||||
void toggleObstacleRecognition() {
|
||||
emit(state.copyWith(obstacleRecognitionFlag: !state.obstacleRecognitionFlag));
|
||||
}
|
||||
|
||||
void initVideoUrls(String main, String ai) {
|
||||
emit(state.copyWith(
|
||||
mainStreamUrl: main,
|
||||
aiStreamUrl: ai,
|
||||
isVideoEnabled: true,
|
||||
));
|
||||
}
|
||||
|
||||
void disposeVideo() {
|
||||
emit(state.copyWith(
|
||||
mainStreamUrl: "",
|
||||
aiStreamUrl: "",
|
||||
isVideoEnabled: false,
|
||||
obstacleRecognitionFlag: false,
|
||||
));
|
||||
sl<ILoggerService>().log("Cubit: 视频地址已置空,准备断开连接");
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// 4. 销毁与清理 (Cleanup)
|
||||
// ==========================================
|
||||
@@ -164,4 +194,5 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
_responseSubscription?.cancel();
|
||||
return super.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,7 +19,7 @@ class RemoteControlState extends Equatable {
|
||||
// --- 安全与锁定 ---
|
||||
final bool isEmergency; // 紧急停止状态
|
||||
final bool isLocked; // 控制锁定状态
|
||||
final String obstacleFlag; // 障碍物标志位文字 (对应 Entity 里的 0/1)
|
||||
final String obstacleFlag; // 障碍物标志位文字(这是机器传过来的) (对应 Entity 里的 0/1)
|
||||
|
||||
// --- 网络与连接 ---
|
||||
final int ping; // 延迟/心跳
|
||||
@@ -28,6 +28,12 @@ class RemoteControlState extends Equatable {
|
||||
// --- UI 视图配置 ---
|
||||
final bool showLeftPip; // 左侧画中画显示
|
||||
final bool showRightPip; // 右侧画中画显示
|
||||
final bool topRightIsExpanded; // 顶部右侧按钮展开状态
|
||||
final bool obstacleRecognitionFlag; // 障碍物识别标志位(这是UI显示的)
|
||||
|
||||
final String mainStreamUrl;
|
||||
final String aiStreamUrl;
|
||||
final bool isVideoEnabled;
|
||||
|
||||
const RemoteControlState({
|
||||
this.status = RemoteControlStatus.initial,
|
||||
@@ -44,10 +50,15 @@ class RemoteControlState extends Equatable {
|
||||
this.errorMessage,
|
||||
this.showLeftPip = true,
|
||||
this.showRightPip = true,
|
||||
this.topRightIsExpanded = false,
|
||||
this.obstacleRecognitionFlag = false,
|
||||
this.mainStreamUrl = '',
|
||||
this.aiStreamUrl = '',
|
||||
this.isVideoEnabled = true
|
||||
});
|
||||
|
||||
// 辅助属性:判断当前是否具备物理驾驶条件
|
||||
bool get canDrive => hasPermission && !isEmergency && !isLocked;
|
||||
bool get canDrive => hasPermission && !isLocked;
|
||||
|
||||
RemoteControlState copyWith({
|
||||
RemoteControlStatus? status,
|
||||
@@ -64,6 +75,11 @@ class RemoteControlState extends Equatable {
|
||||
String? errorMessage,
|
||||
bool? showLeftPip,
|
||||
bool? showRightPip,
|
||||
bool? topRightIsExpanded,
|
||||
bool? obstacleRecognitionFlag,
|
||||
bool? isVideoEnabled,
|
||||
String? mainStreamUrl,
|
||||
String? aiStreamUrl
|
||||
}) {
|
||||
return RemoteControlState(
|
||||
status: status ?? this.status,
|
||||
@@ -80,6 +96,11 @@ class RemoteControlState extends Equatable {
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
showLeftPip: showLeftPip ?? this.showLeftPip,
|
||||
showRightPip: showRightPip ?? this.showRightPip,
|
||||
topRightIsExpanded: topRightIsExpanded ?? this.topRightIsExpanded,
|
||||
obstacleRecognitionFlag: obstacleRecognitionFlag ?? this.obstacleRecognitionFlag,
|
||||
isVideoEnabled: isVideoEnabled ?? this.isVideoEnabled,
|
||||
mainStreamUrl: mainStreamUrl ?? this.mainStreamUrl,
|
||||
aiStreamUrl: aiStreamUrl ?? this.aiStreamUrl
|
||||
);
|
||||
}
|
||||
|
||||
@@ -99,5 +120,10 @@ class RemoteControlState extends Equatable {
|
||||
errorMessage,
|
||||
showLeftPip,
|
||||
showRightPip,
|
||||
topRightIsExpanded,
|
||||
obstacleRecognitionFlag,
|
||||
isVideoEnabled,
|
||||
mainStreamUrl,
|
||||
aiStreamUrl
|
||||
];
|
||||
}
|
||||
@@ -49,125 +49,153 @@ class _RemoteControlPageState extends State<RemoteControlPage> {
|
||||
final deviceState = context.watch<DevicesCubit>().state;
|
||||
final currentDevice = deviceState.selectedDevice;
|
||||
|
||||
if (currentDevice == null) {
|
||||
return _buildOfflineScaffold();
|
||||
if (currentDevice != null &&
|
||||
context.read<RemoteControlCubit>().state.mainStreamUrl.isEmpty) {
|
||||
// 拼接 URL
|
||||
// final String mainUrl =
|
||||
// "webrtc://${TCPConsts.TCP_IP}/live/livestream/${currentDevice.deviceName}?token=${userState.user!.token}";
|
||||
// final String aiUrl =
|
||||
// "webrtc://${TCPConsts.TCP_IP}/live/livestream/${currentDevice.deviceName}/detect?token=${userState.user!.token}";
|
||||
|
||||
final String mainUrl =
|
||||
"webrtc://${TCPConsts.TCP_IP}/live/livestream/111?token=${userState.user!.token}";
|
||||
final String aiUrl =
|
||||
"webrtc://${TCPConsts.TCP_IP}/live/livestream/222?token=${userState.user!.token}";
|
||||
|
||||
// 初始化给 Cubit
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
context.read<RemoteControlCubit>().initVideoUrls(mainUrl, aiUrl);
|
||||
});
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: Stack(
|
||||
children: [
|
||||
// 在 Stack 的最底层替换:
|
||||
Positioned.fill(
|
||||
child: BlocBuilder<RemoteControlCubit, RemoteControlState>(
|
||||
// 只有当显示隐藏状态改变时才重构,内部的拖拽由组件自身 State 处理,不影响这里
|
||||
buildWhen: (p, c) =>
|
||||
p.showLeftPip != c.showLeftPip ||
|
||||
p.showRightPip != c.showRightPip,
|
||||
return PopScope(
|
||||
onPopInvokedWithResult: (didPop, result) {
|
||||
if (didPop) {
|
||||
// 无论怎么退出的,都强制清理视频和控制循环
|
||||
context.read<RemoteControlCubit>().stopControlLoop();
|
||||
context.read<RemoteControlCubit>().disposeVideo();
|
||||
}
|
||||
},
|
||||
child: Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: Stack(
|
||||
children: [
|
||||
// 在 Stack 的最底层替换:
|
||||
Positioned.fill(
|
||||
child: BlocBuilder<RemoteControlCubit, RemoteControlState>(
|
||||
buildWhen: (p, c) =>
|
||||
p.showLeftPip != c.showLeftPip ||
|
||||
p.showRightPip != c.showRightPip ||
|
||||
p.obstacleRecognitionFlag != c.obstacleRecognitionFlag ||
|
||||
p.mainStreamUrl != c.mainStreamUrl, // 必须监听地址变化
|
||||
builder: (context, state) {
|
||||
return WebRTCLocalPlayer(
|
||||
// 关键:使用 state 里的地址,这样 disposeVideo 将其置空时,Player 才会断开
|
||||
streamUrl: state.mainStreamUrl,
|
||||
secondStreamUrl: state.aiStreamUrl,
|
||||
showLeftPip: state.showLeftPip,
|
||||
showRightPip: state.showRightPip,
|
||||
obstacleRecognitionFlag: state.obstacleRecognitionFlag,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
BlocBuilder<RemoteControlCubit, RemoteControlState>(
|
||||
buildWhen: (p, c) => p.isEmergency != c.isEmergency,
|
||||
builder: (context, state) {
|
||||
return WebRTCLocalPlayer(
|
||||
// 这里的 URL 拼接根据你的后端规则
|
||||
// streamUrl: "webrtc://${TCPConsts.TCP_IP}/live/livestream/${currentDevice.deviceName}?token=${userState.user!.token}",
|
||||
streamUrl:
|
||||
"webrtc://${TCPConsts.TCP_IP}/live/livestream/111?token=${userState.user!.token}",
|
||||
showLeftPip: state.showLeftPip, // 从 Cubit 状态中读取
|
||||
showRightPip: state.showRightPip, // 从 Cubit 状态中读取
|
||||
);
|
||||
return state.isEmergency
|
||||
? const Positioned.fill(child: EmergencyOverlay())
|
||||
: const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
BlocBuilder<RemoteControlCubit, RemoteControlState>(
|
||||
buildWhen: (p, c) => p.isEmergency != c.isEmergency,
|
||||
builder: (context, state) {
|
||||
return state.isEmergency
|
||||
? const Positioned.fill(child: EmergencyOverlay())
|
||||
: const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
SafeArea(
|
||||
child: Column(
|
||||
children: [
|
||||
const TopStatusBar(),
|
||||
Expanded(
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final double sideAreaWidth =
|
||||
constraints.maxWidth * 0.25;
|
||||
final double centerAreaWidth =
|
||||
constraints.maxWidth * 0.5;
|
||||
|
||||
SafeArea(
|
||||
child: Column(
|
||||
children: [
|
||||
const TopStatusBar(),
|
||||
Expanded(
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final double sideAreaWidth = constraints.maxWidth * 0.25;
|
||||
final double centerAreaWidth = constraints.maxWidth * 0.5;
|
||||
|
||||
// 关键:使用 BlocBuilder 局部刷新摇杆,不要 watch 整个 Page
|
||||
return BlocBuilder<
|
||||
RemoteControlCubit,
|
||||
RemoteControlState
|
||||
>(
|
||||
// 只有 isLocked 改变时才重构摇杆区域,摇杆坐标改变由内部处理
|
||||
buildWhen: (p, c) => p.isLocked != c.isLocked,
|
||||
builder: (context, remoteState) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 10),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
// 左摇杆
|
||||
SizedBox(
|
||||
width: sideAreaWidth,
|
||||
child: Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: LeftJoystickArea(
|
||||
isLocked: remoteState.isLocked,
|
||||
width: sideAreaWidth * 0.5 * 0.7,
|
||||
// 关键:使用 BlocBuilder 局部刷新摇杆,不要 watch 整个 Page
|
||||
return BlocBuilder<
|
||||
RemoteControlCubit,
|
||||
RemoteControlState
|
||||
>(
|
||||
// 只有 isLocked 改变时才重构摇杆区域,摇杆坐标改变由内部处理
|
||||
buildWhen: (p, c) => p.isLocked != c.isLocked,
|
||||
builder: (context, remoteState) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 10),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
// 左摇杆
|
||||
SizedBox(
|
||||
width: sideAreaWidth,
|
||||
child: Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: LeftJoystickArea(
|
||||
isLocked: remoteState.isLocked,
|
||||
width: sideAreaWidth * 0.5 * 0.7,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// 中间区
|
||||
SizedBox(
|
||||
width: centerAreaWidth,
|
||||
child: Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: CenterControlArea(
|
||||
totalWidth: centerAreaWidth,
|
||||
// 中间区
|
||||
SizedBox(
|
||||
width: centerAreaWidth,
|
||||
child: Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: CenterControlArea(
|
||||
totalWidth: centerAreaWidth,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// 右摇杆
|
||||
SizedBox(
|
||||
width: sideAreaWidth,
|
||||
child: Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: RightJoystickArea(
|
||||
isLocked: remoteState.isLocked,
|
||||
width: sideAreaWidth * 0.5 * 0.7,
|
||||
// 右摇杆
|
||||
SizedBox(
|
||||
width: sideAreaWidth,
|
||||
child: Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: RightJoystickArea(
|
||||
isLocked: remoteState.isLocked,
|
||||
width: sideAreaWidth * 0.5 * 0.7,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// 权限弹窗
|
||||
BlocBuilder<RemoteControlCubit, RemoteControlState>(
|
||||
buildWhen: (p, c) =>
|
||||
p.showPermissionRequestDialog != c.showPermissionRequestDialog,
|
||||
builder: (context, state) {
|
||||
if (state.showPermissionRequestDialog) {
|
||||
return _buildPermissionDialog(context, state);
|
||||
}
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
],
|
||||
// 权限弹窗
|
||||
BlocBuilder<RemoteControlCubit, RemoteControlState>(
|
||||
buildWhen: (p, c) =>
|
||||
p.showPermissionRequestDialog !=
|
||||
c.showPermissionRequestDialog,
|
||||
builder: (context, state) {
|
||||
if (state.showPermissionRequestDialog) {
|
||||
return _buildPermissionDialog(context, state);
|
||||
}
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -20,12 +20,18 @@ class TopStatusBar extends StatelessWidget {
|
||||
// 监听局部遥控状态
|
||||
final remoteState = context.watch<RemoteControlCubit>().state;
|
||||
|
||||
var _remoteControlCubit = context.read<RemoteControlCubit>();
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: Row(
|
||||
children: [
|
||||
// 1. 返回按钮 (对应 SimpleSmallFunctionButton)
|
||||
_buildIconButton("assets/svgs/remote_back.svg", () => context.pop()),
|
||||
_buildIconButton("assets/svgs/remote_back.svg", () async {
|
||||
_remoteControlCubit.stopControlLoop();
|
||||
_remoteControlCubit.disposeVideo();
|
||||
if (context.mounted) context.pop();
|
||||
}),
|
||||
const SizedBox(width: 16),
|
||||
|
||||
// 2. 控制状态 (对应 StatusChipLeft)
|
||||
@@ -39,7 +45,7 @@ class TopStatusBar extends StatelessWidget {
|
||||
onTap: () {
|
||||
if (!remoteState.hasPermission) {
|
||||
// 弹出请求权限对话框逻辑
|
||||
context.read<RemoteControlCubit>().togglePermissionDialog(true);
|
||||
_remoteControlCubit.togglePermissionDialog(true);
|
||||
}
|
||||
},
|
||||
),
|
||||
@@ -50,7 +56,7 @@ class TopStatusBar extends StatelessWidget {
|
||||
remoteState.isLocked
|
||||
? "assets/svgs/remote_lock.svg"
|
||||
: "assets/svgs/remote_lock_open.svg",
|
||||
() => context.read<RemoteControlCubit>().toggleLock(),
|
||||
() => _remoteControlCubit.toggleLock(),
|
||||
isSelected: remoteState.isLocked,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
@@ -60,16 +66,65 @@ class TopStatusBar extends StatelessWidget {
|
||||
|
||||
const Spacer(),
|
||||
|
||||
_buildControlModeChip(remoteState.runningStatusEntity.controlMode),
|
||||
_buildExpandIconButton(
|
||||
iconPath: "assets/svgs/remote_expand.svg",
|
||||
selectedIconPath: "assets/svgs/remote_unexpand.svg",
|
||||
isSelected: remoteState.topRightIsExpanded,
|
||||
onTap: (){
|
||||
_remoteControlCubit.toggleTopLeftExpand();
|
||||
},
|
||||
),
|
||||
|
||||
const SizedBox(width: 8),
|
||||
const SizedBox(width: 5),
|
||||
|
||||
_buildVoltageChip(remoteState.runningStatusEntity.voltage),
|
||||
AnimatedSize(
|
||||
duration: const Duration(milliseconds: 100),
|
||||
curve: Curves.easeInOut,
|
||||
child: Row(
|
||||
children: !remoteState.topRightIsExpanded
|
||||
? [
|
||||
const SizedBox(width: 8),
|
||||
_buildSwitchIconButton(
|
||||
iconPath: "assets/svgs/remote_recognition_off.svg",
|
||||
selectedIconPath: "assets/svgs/remote_recognition.svg",
|
||||
isSelected: remoteState.obstacleRecognitionFlag,
|
||||
onTap: (){
|
||||
_remoteControlCubit.toggleObstacleRecognition();
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
_buildSwitchIconButton(
|
||||
iconPath: "assets/svgs/remote_video_left_off.svg",
|
||||
selectedIconPath: "assets/svgs/remote_video_left.svg",
|
||||
isSelected: remoteState.showLeftPip,
|
||||
onTap: (){
|
||||
_remoteControlCubit.toggleLeftPip();
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
_buildSwitchIconButton(
|
||||
iconPath: "assets/svgs/remote_video_right_off.svg",
|
||||
selectedIconPath: "assets/svgs/remote_video_right.svg",
|
||||
isSelected: remoteState.showRightPip,
|
||||
onTap: (){
|
||||
_remoteControlCubit.toggleRightPip();
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
_buildControlModeChip(remoteState.runningStatusEntity.controlMode),
|
||||
const SizedBox(width: 8),
|
||||
_buildVoltageChip(remoteState.runningStatusEntity.voltage),
|
||||
const SizedBox(width: 8),
|
||||
_buildPingChip(remoteState.ping),
|
||||
|
||||
]
|
||||
: [], // 折叠时数组为空
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(width: 8),
|
||||
|
||||
// 5. 信号延迟 (对应 pingStatusChip)
|
||||
_buildPingChip(remoteState.ping),
|
||||
// _buildPingChip(remoteState.ping),
|
||||
const SizedBox(width: 8),
|
||||
|
||||
// 6. 电量 (对应 StatusChipRight)
|
||||
@@ -80,7 +135,8 @@ class TopStatusBar extends StatelessWidget {
|
||||
}
|
||||
|
||||
Widget _buildIconButton(
|
||||
String svgAsset, // 改为 String 路径,例如 'assets/icons/stop.svg'
|
||||
String svgAsset,
|
||||
// 改为 String 路径,例如 'assets/icons/stop.svg'
|
||||
VoidCallback onTap, {
|
||||
bool isSelected = false,
|
||||
}) {
|
||||
@@ -107,6 +163,90 @@ class TopStatusBar extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSwitchIconButton({
|
||||
required String iconPath, // 默认图标路径
|
||||
required String selectedIconPath, // 选中后的图标路径
|
||||
required VoidCallback onTap, // 点击事件
|
||||
bool isSelected = false, // 是否选中状态
|
||||
double size = 32, // 按钮尺寸
|
||||
}) {
|
||||
return InkWell(
|
||||
onTap: onTap,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 200),
|
||||
// 增加颜色切换动画
|
||||
width: size,
|
||||
height: size,
|
||||
decoration: BoxDecoration(
|
||||
// 选中时为深蓝色,未选中时为半透明灰色
|
||||
color: isSelected
|
||||
? const Color(0xFF0078D4)
|
||||
: Colors.grey.withOpacity(0.4),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(
|
||||
color: isSelected
|
||||
? Colors.transparent
|
||||
: Colors.white.withOpacity(0.3),
|
||||
width: 0.5,
|
||||
),
|
||||
boxShadow: isSelected
|
||||
? [
|
||||
BoxShadow(
|
||||
color: const Color(0xFF0078D4).withOpacity(0.4),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
]
|
||||
: null,
|
||||
),
|
||||
padding: const EdgeInsets.all(7),
|
||||
child: SvgPicture.asset(
|
||||
// 根据状态切换图片
|
||||
isSelected ? selectedIconPath : iconPath,
|
||||
colorFilter: const ColorFilter.mode(Colors.white, BlendMode.srcIn),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Widget _buildExpandIconButton({
|
||||
required String iconPath, // 默认图标路径
|
||||
required String selectedIconPath, // 选中后的图标路径
|
||||
required VoidCallback onTap, // 点击事件
|
||||
bool isSelected = false, // 是否选中状态
|
||||
double size = 28, // 按钮尺寸
|
||||
}) {
|
||||
return InkWell(
|
||||
onTap: onTap,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 200),
|
||||
// 增加颜色切换动画
|
||||
width: size,
|
||||
height: size,
|
||||
decoration: BoxDecoration(
|
||||
// 选中时为深蓝色,未选中时为半透明灰色
|
||||
color: Colors.grey.withOpacity(0.4),
|
||||
borderRadius: BorderRadius.circular(15),
|
||||
border: Border.all(
|
||||
color: Colors.white.withOpacity(0.3),
|
||||
width: 0.5,
|
||||
),
|
||||
boxShadow: null,
|
||||
),
|
||||
padding: const EdgeInsets.all(7),
|
||||
child: SvgPicture.asset(
|
||||
// 根据状态切换图片
|
||||
isSelected ? selectedIconPath : iconPath,
|
||||
colorFilter: const ColorFilter.mode(Colors.white, BlendMode.srcIn),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPingChip(int ping) {
|
||||
Color color = ping < 100
|
||||
? Colors.green
|
||||
@@ -132,12 +272,12 @@ class TopStatusBar extends StatelessWidget {
|
||||
displayText = 'TCP模式';
|
||||
break;
|
||||
case ControlMode.NONE:
|
||||
displayText = '无模式';
|
||||
displayText = '无';
|
||||
break;
|
||||
case ControlMode.OTHER:
|
||||
displayText = '其他模式';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return StatusChip(
|
||||
text: displayText,
|
||||
|
||||
@@ -5,15 +5,19 @@ import 'package:flutter_webrtc/flutter_webrtc.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class WebRTCLocalPlayer extends StatefulWidget {
|
||||
final String streamUrl;
|
||||
final String streamUrl; // 第一个流(四分屏)
|
||||
final String? secondStreamUrl; // 第二条完整流,可选
|
||||
final bool showLeftPip;
|
||||
final bool showRightPip;
|
||||
final bool obstacleRecognitionFlag;
|
||||
|
||||
const WebRTCLocalPlayer({
|
||||
super.key,
|
||||
required this.streamUrl,
|
||||
this.secondStreamUrl,
|
||||
this.showLeftPip = true,
|
||||
this.showRightPip = true,
|
||||
this.obstacleRecognitionFlag = false,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -21,19 +25,17 @@ class WebRTCLocalPlayer extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
final RTCVideoRenderer _renderer = RTCVideoRenderer();
|
||||
final RTCVideoRenderer _renderer = RTCVideoRenderer(); // 第一个流(四分屏)
|
||||
RTCVideoRenderer? _secondRenderer; // 第二条完整流
|
||||
|
||||
RTCPeerConnection? _peerConnection;
|
||||
RTCPeerConnection? _secondPeerConnection;
|
||||
|
||||
bool _isInitialized = false;
|
||||
bool _isFrontMain = true;
|
||||
bool _isFrontMain = true; // true=前,false=后
|
||||
|
||||
// 使用 ValueNotifier 配合局部刷新,提升拖拽性能
|
||||
final ValueNotifier<Offset> _leftPosNotifier = ValueNotifier(
|
||||
const Offset(20, 80),
|
||||
);
|
||||
final ValueNotifier<Offset> _rightPosNotifier = ValueNotifier(
|
||||
const Offset(200, 80),
|
||||
);
|
||||
final ValueNotifier<Offset> _leftPosNotifier = ValueNotifier(const Offset(20, 80));
|
||||
final ValueNotifier<Offset> _rightPosNotifier = ValueNotifier(const Offset(200, 80));
|
||||
bool _isPosInitialized = false;
|
||||
|
||||
@override
|
||||
@@ -45,42 +47,44 @@ class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
Future<void> _prepareAndConnect() async {
|
||||
try {
|
||||
await _renderer.initialize();
|
||||
if (!mounted) return;
|
||||
await _initWebRTCConnection();
|
||||
await _initWebRTCConnection(widget.streamUrl, _renderer, true);
|
||||
|
||||
if (widget.obstacleRecognitionFlag && widget.secondStreamUrl != null) {
|
||||
await _startSecondStream(widget.secondStreamUrl!);
|
||||
}
|
||||
|
||||
setState(() => _isInitialized = true);
|
||||
} catch (e) {
|
||||
debugPrint("初始化失败: $e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _initWebRTCConnection() async {
|
||||
_peerConnection = await createPeerConnection({
|
||||
Future<void> _initWebRTCConnection(
|
||||
String url, RTCVideoRenderer renderer, bool isMain) async {
|
||||
final pc = await createPeerConnection({
|
||||
"sdpSemantics": "unified-plan",
|
||||
"iceServers": [
|
||||
{"urls": "stun:stun.l.google.com:19302"},
|
||||
],
|
||||
});
|
||||
|
||||
await _peerConnection!.addTransceiver(
|
||||
// 只接收视频
|
||||
await pc.addTransceiver(
|
||||
kind: RTCRtpMediaType.RTCRtpMediaTypeVideo,
|
||||
init: RTCRtpTransceiverInit(direction: TransceiverDirection.RecvOnly),
|
||||
);
|
||||
|
||||
_peerConnection!.onTrack = (RTCTrackEvent event) {
|
||||
if (event.track.kind == 'video' && event.streams.isNotEmpty) {
|
||||
if (mounted) setState(() => _renderer.srcObject = event.streams[0]);
|
||||
pc.onTrack = (RTCTrackEvent event) {
|
||||
if (event.streams.isNotEmpty && event.track.kind == 'video') {
|
||||
if (mounted) setState(() => renderer.srcObject = event.streams[0]);
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
RTCSessionDescription offer = await _peerConnection!.createOffer({
|
||||
'offerToReceiveVideo': 1,
|
||||
});
|
||||
await _peerConnection!.setLocalDescription(offer);
|
||||
RTCSessionDescription offer = await pc.createOffer({'offerToReceiveVideo': 1});
|
||||
await pc.setLocalDescription(offer);
|
||||
|
||||
Uri uri = Uri.parse(
|
||||
widget.streamUrl.replaceFirst('webrtc://', 'http://'),
|
||||
);
|
||||
Uri uri = Uri.parse(url.replaceFirst('webrtc://', 'http://'));
|
||||
String apiUrl = "http://${uri.host}:1985/rtc/v1/play/";
|
||||
|
||||
final response = await http.post(
|
||||
@@ -88,7 +92,7 @@ class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({
|
||||
"api": apiUrl,
|
||||
"streamurl": widget.streamUrl,
|
||||
"streamurl": url,
|
||||
"clientip": null,
|
||||
"sdp": offer.sdp,
|
||||
}),
|
||||
@@ -96,179 +100,155 @@ class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(response.body);
|
||||
await _peerConnection!.setRemoteDescription(
|
||||
await pc.setRemoteDescription(
|
||||
RTCSessionDescription(data['sdp'], 'answer'),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("信令错误: $e");
|
||||
}
|
||||
|
||||
if (isMain) _peerConnection = pc;
|
||||
else _secondPeerConnection = pc;
|
||||
}
|
||||
|
||||
Future<void> _startSecondStream(String url) async {
|
||||
_secondRenderer = RTCVideoRenderer();
|
||||
await _secondRenderer!.initialize();
|
||||
await _initWebRTCConnection(url, _secondRenderer!, false);
|
||||
}
|
||||
|
||||
Future<void> _stopSecondStream() async {
|
||||
if (_secondPeerConnection != null) {
|
||||
await _secondPeerConnection!.close();
|
||||
await _secondPeerConnection!.dispose();
|
||||
_secondPeerConnection = null;
|
||||
}
|
||||
if (_secondRenderer != null) {
|
||||
_secondRenderer!.srcObject = null;
|
||||
await _secondRenderer!.dispose();
|
||||
_secondRenderer = null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant WebRTCLocalPlayer oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.obstacleRecognitionFlag != oldWidget.obstacleRecognitionFlag) {
|
||||
if (widget.obstacleRecognitionFlag) {
|
||||
if (widget.secondStreamUrl != null) _startSecondStream(widget.secondStreamUrl!);
|
||||
} else {
|
||||
_stopSecondStream();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_peerConnection?.close();
|
||||
_peerConnection?.dispose();
|
||||
_secondPeerConnection?.close();
|
||||
_secondPeerConnection?.dispose();
|
||||
|
||||
_renderer.srcObject = null;
|
||||
_renderer.dispose();
|
||||
_secondRenderer?.srcObject = null;
|
||||
_secondRenderer?.dispose();
|
||||
|
||||
_leftPosNotifier.dispose();
|
||||
_rightPosNotifier.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// 四分屏裁剪视图
|
||||
Widget _buildQuadrantView({required Alignment alignment}) {
|
||||
if (!_isInitialized || _renderer.srcObject == null)
|
||||
return Container(color: Colors.black);
|
||||
// -------------------- 渲染逻辑 --------------------
|
||||
|
||||
return RepaintBoundary(
|
||||
child: ClipRect(
|
||||
child: FractionallySizedBox(
|
||||
widthFactor: 2.0,
|
||||
heightFactor: 2.0,
|
||||
alignment: alignment,
|
||||
child: RTCVideoView(
|
||||
_renderer,
|
||||
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover,
|
||||
mirror: false,
|
||||
RTCVideoRenderer getFrontRenderer() {
|
||||
if (widget.obstacleRecognitionFlag && _secondRenderer != null) {
|
||||
return _secondRenderer!;
|
||||
}
|
||||
return _renderer;
|
||||
}
|
||||
|
||||
RTCVideoRenderer getBackRenderer() => _renderer;
|
||||
RTCVideoRenderer getLeftRenderer() => _renderer;
|
||||
RTCVideoRenderer getRightRenderer() => _renderer;
|
||||
|
||||
Widget _buildQuadrantView({required Alignment alignment, required RTCVideoRenderer renderer}) {
|
||||
if (renderer.srcObject == null) return Container(color: Colors.black);
|
||||
|
||||
if (renderer == _secondRenderer) {
|
||||
// 第二条流完整显示
|
||||
return RTCVideoView(renderer,
|
||||
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover, mirror: false);
|
||||
}
|
||||
|
||||
// 第一个流四分屏
|
||||
return ClipRect(
|
||||
child: FractionallySizedBox(
|
||||
widthFactor: 2.0,
|
||||
heightFactor: 2.0,
|
||||
alignment: alignment,
|
||||
child: RTCVideoView(renderer,
|
||||
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover, mirror: false),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMainViewWithFeathering() {
|
||||
final renderer = _isFrontMain ? getFrontRenderer() : getBackRenderer();
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
// 背景层(虚化 + 半透明黑色)
|
||||
Positioned.fill(
|
||||
child: _buildQuadrantView(
|
||||
alignment: _isFrontMain ? Alignment.topLeft : Alignment.topRight,
|
||||
renderer: renderer,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 主视角的矩形边缘羽化效果
|
||||
Widget _buildMainViewWithFeathering() {
|
||||
return ShaderMask(
|
||||
shaderCallback: (Rect bounds) {
|
||||
return const LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [
|
||||
Colors.transparent,
|
||||
Colors.white,
|
||||
Colors.white,
|
||||
Colors.transparent,
|
||||
],
|
||||
stops: [0.0, 0.15, 0.85, 1.0],
|
||||
).createShader(bounds);
|
||||
},
|
||||
blendMode: BlendMode.dstIn,
|
||||
child: ShaderMask(
|
||||
shaderCallback: (Rect bounds) {
|
||||
return const LinearGradient(
|
||||
begin: Alignment.centerLeft,
|
||||
end: Alignment.centerRight,
|
||||
colors: [
|
||||
Colors.transparent,
|
||||
Colors.white,
|
||||
Colors.white,
|
||||
Colors.transparent,
|
||||
],
|
||||
stops: [0.0, 0.05, 0.95, 1.0],
|
||||
).createShader(bounds);
|
||||
},
|
||||
blendMode: BlendMode.dstIn,
|
||||
child: _buildQuadrantView(
|
||||
alignment: _isFrontMain ? Alignment.topLeft : Alignment.topRight,
|
||||
Positioned.fill(
|
||||
child: BackdropFilter(
|
||||
filter: ImageFilter.blur(sigmaX: 45, sigmaY: 45),
|
||||
child: Container(color: Colors.black.withOpacity(0.55)),
|
||||
),
|
||||
),
|
||||
),
|
||||
// 中心主视角 + 羽化
|
||||
Center(
|
||||
child: ShaderMask(
|
||||
shaderCallback: (Rect bounds) {
|
||||
return const LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [Colors.transparent, Colors.white, Colors.white, Colors.transparent],
|
||||
stops: [0.0, 0.15, 0.85, 1.0],
|
||||
).createShader(bounds);
|
||||
},
|
||||
blendMode: BlendMode.dstIn,
|
||||
child: ShaderMask(
|
||||
shaderCallback: (Rect bounds) {
|
||||
return const LinearGradient(
|
||||
begin: Alignment.centerLeft,
|
||||
end: Alignment.centerRight,
|
||||
colors: [Colors.transparent, Colors.white, Colors.white, Colors.transparent],
|
||||
stops: [0.0, 0.05, 0.95, 1.0],
|
||||
).createShader(bounds);
|
||||
},
|
||||
blendMode: BlendMode.dstIn,
|
||||
child: AspectRatio(
|
||||
aspectRatio: 16 / 9,
|
||||
child: _buildQuadrantView(
|
||||
alignment: _isFrontMain ? Alignment.topLeft : Alignment.topRight,
|
||||
renderer: renderer,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!_isInitialized)
|
||||
return const Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final double pipW = constraints.maxWidth / 5;
|
||||
final double pipH = pipW * 9 / 16;
|
||||
|
||||
// 核心:处理 Windows 窗口大小变化时的坐标修正
|
||||
Future.microtask(() {
|
||||
if (!mounted) return;
|
||||
_correctPosition(_leftPosNotifier, constraints, pipW, pipH);
|
||||
_correctPosition(_rightPosNotifier, constraints, pipW, pipH);
|
||||
});
|
||||
|
||||
if (!_isPosInitialized) {
|
||||
_leftPosNotifier.value = const Offset(20, 80);
|
||||
_rightPosNotifier.value = Offset(
|
||||
constraints.maxWidth - pipW - 20,
|
||||
80,
|
||||
);
|
||||
_isPosInitialized = true;
|
||||
}
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
// ① 背景虚化层(背景两侧模糊,模拟视觉聚焦)
|
||||
Positioned.fill(
|
||||
child: Stack(
|
||||
children: [
|
||||
_buildQuadrantView(
|
||||
alignment: _isFrontMain
|
||||
? Alignment.topLeft
|
||||
: Alignment.topRight,
|
||||
),
|
||||
Positioned.fill(
|
||||
child: BackdropFilter(
|
||||
filter: ImageFilter.blur(sigmaX: 45, sigmaY: 45),
|
||||
child: Container(color: Colors.black.withOpacity(0.55)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// ② 主视频层
|
||||
Center(
|
||||
child: GestureDetector(
|
||||
onDoubleTap: () =>
|
||||
setState(() => _isFrontMain = !_isFrontMain),
|
||||
child: AspectRatio(
|
||||
aspectRatio: 16 / 9,
|
||||
child: _buildMainViewWithFeathering(),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// ③ 悬浮小窗 - 解决阻尼感的 1:1 稳定拖拽
|
||||
if (widget.showLeftPip)
|
||||
_buildFastPip(
|
||||
_leftPosNotifier,
|
||||
Alignment.bottomLeft,
|
||||
constraints,
|
||||
pipW,
|
||||
pipH,
|
||||
),
|
||||
if (widget.showRightPip)
|
||||
_buildFastPip(
|
||||
_rightPosNotifier,
|
||||
Alignment.bottomRight,
|
||||
constraints,
|
||||
pipW,
|
||||
pipH,
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _correctPosition(
|
||||
ValueNotifier<Offset> notifier,
|
||||
BoxConstraints constraints,
|
||||
double w,
|
||||
double h,
|
||||
) {
|
||||
void _correctPosition(ValueNotifier<Offset> notifier, BoxConstraints constraints, double w, double h) {
|
||||
double maxX = (constraints.maxWidth - w).clamp(0.0, double.infinity);
|
||||
double maxY = (constraints.maxHeight - h).clamp(0.0, double.infinity);
|
||||
double newX = notifier.value.dx.clamp(0.0, maxX);
|
||||
@@ -278,14 +258,7 @@ class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
}
|
||||
}
|
||||
|
||||
// 极致丝滑的局部刷新组件
|
||||
Widget _buildFastPip(
|
||||
ValueNotifier<Offset> notifier,
|
||||
Alignment align,
|
||||
BoxConstraints constraints,
|
||||
double w,
|
||||
double h,
|
||||
) {
|
||||
Widget _buildFastPip(ValueNotifier<Offset> notifier, Alignment align, BoxConstraints constraints, double w, double h) {
|
||||
return ValueListenableBuilder<Offset>(
|
||||
valueListenable: notifier,
|
||||
builder: (context, pos, child) {
|
||||
@@ -295,7 +268,6 @@ class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onPanUpdate: (details) {
|
||||
// 关键修正:使用 notifier.value.dx 替换 pos.dx,消除位移丢失导致的阻尼感
|
||||
final double nextX = (notifier.value.dx + details.delta.dx).clamp(
|
||||
0.0,
|
||||
(constraints.maxWidth - w).clamp(0.0, double.infinity),
|
||||
@@ -315,16 +287,57 @@ class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
height: h,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.5),
|
||||
blurRadius: 20,
|
||||
offset: const Offset(0, 8),
|
||||
),
|
||||
],
|
||||
boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.5), blurRadius: 20, offset: const Offset(0, 8))],
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: _buildQuadrantView(alignment: align),
|
||||
child: _buildQuadrantView(alignment: align, renderer: _renderer),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!_isInitialized) {
|
||||
return const Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final double pipW = constraints.maxWidth / 5;
|
||||
final double pipH = pipW * 9 / 16;
|
||||
|
||||
Future.microtask(() {
|
||||
if (!mounted) return;
|
||||
_correctPosition(_leftPosNotifier, constraints, pipW, pipH);
|
||||
_correctPosition(_rightPosNotifier, constraints, pipW, pipH);
|
||||
});
|
||||
|
||||
if (!_isPosInitialized) {
|
||||
_leftPosNotifier.value = const Offset(20, 80);
|
||||
_rightPosNotifier.value = Offset(constraints.maxWidth - pipW - 20, 80);
|
||||
_isPosInitialized = true;
|
||||
}
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
Center(
|
||||
child: GestureDetector(
|
||||
onDoubleTap: () => setState(() => _isFrontMain = !_isFrontMain),
|
||||
child: _buildMainViewWithFeathering(),
|
||||
),
|
||||
),
|
||||
if (widget.showLeftPip)
|
||||
_buildFastPip(_leftPosNotifier, Alignment.bottomLeft, constraints, pipW, pipH),
|
||||
if (widget.showRightPip)
|
||||
_buildFastPip(_rightPosNotifier, Alignment.bottomRight, constraints, pipW, pipH),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user