本地视频延迟优化+视频的获取链路播放
This commit is contained in:
@@ -0,0 +1,479 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:video_player/video_player.dart';
|
||||
|
||||
import '../../domain/entities/onvif_camera.dart';
|
||||
import '../cubit/local_camera_cubit.dart';
|
||||
import '../cubit/local_camera_state.dart';
|
||||
|
||||
/// 本地视频页:ONVIF 自动发现 + RTSP 播放(搜索与播放合一)。
|
||||
/// 上方为播放器 / 搜索状态 / 摄像头列表,下方为链路状态"链条"实时展示。
|
||||
class LocalCameraPage extends StatelessWidget {
|
||||
const LocalCameraPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (_) => LocalCameraCubit()..start(),
|
||||
child: const _LocalCameraView(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _LocalCameraView extends StatefulWidget {
|
||||
const _LocalCameraView();
|
||||
|
||||
@override
|
||||
State<_LocalCameraView> createState() => _LocalCameraViewState();
|
||||
}
|
||||
|
||||
class _LocalCameraViewState extends State<_LocalCameraView> {
|
||||
bool _noNetworkDialogShown = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cubit = context.read<LocalCameraCubit>();
|
||||
|
||||
return BlocConsumer<LocalCameraCubit, LocalCameraState>(
|
||||
listenWhen: (prev, cur) => prev.phase != cur.phase,
|
||||
listener: (context, state) {
|
||||
if (state.phase == LocalCameraPhase.noNetwork) {
|
||||
if (!_noNetworkDialogShown) {
|
||||
_noNetworkDialogShown = true;
|
||||
_showNoNetworkDialog(context, state.message ?? LocalCameraCubit.msgNoNetwork);
|
||||
}
|
||||
} else {
|
||||
_noNetworkDialogShown = false;
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
return PopScope(
|
||||
canPop: state.phase != LocalCameraPhase.playing &&
|
||||
state.phase != LocalCameraPhase.connecting,
|
||||
onPopInvokedWithResult: (didPop, result) {
|
||||
if (!didPop) {
|
||||
// 播放/连接中返回:先停流再返回到列表
|
||||
cubit.backToList();
|
||||
}
|
||||
},
|
||||
child: Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
appBar: AppBar(
|
||||
backgroundColor: const Color(0xFF165DFF),
|
||||
elevation: 0,
|
||||
leading: state.showVideo
|
||||
? IconButton(
|
||||
icon: const Icon(Icons.arrow_back, color: Colors.white),
|
||||
onPressed: () => cubit.backToList(),
|
||||
)
|
||||
: IconButton(
|
||||
icon: const Icon(Icons.close, color: Colors.white),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
),
|
||||
title: const Text(
|
||||
'本地视频',
|
||||
style: TextStyle(color: Colors.white, fontWeight: FontWeight.w600),
|
||||
),
|
||||
centerTitle: true,
|
||||
actions: [
|
||||
if (state.phase == LocalCameraPhase.found ||
|
||||
state.phase == LocalCameraPhase.notFound)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh, color: Colors.white),
|
||||
onPressed: () => cubit.runDiscovery(),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SafeArea(
|
||||
child: Column(
|
||||
children: [
|
||||
Expanded(child: _buildContent(context, cubit, state)),
|
||||
_buildChain(context, state),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContent(
|
||||
BuildContext context,
|
||||
LocalCameraCubit cubit,
|
||||
LocalCameraState state,
|
||||
) {
|
||||
if (state.showVideo) {
|
||||
return _buildVideoArea(context, cubit, state);
|
||||
}
|
||||
switch (state.phase) {
|
||||
case LocalCameraPhase.searching:
|
||||
return _buildCenterState(
|
||||
icon: Icons.wifi_tethering,
|
||||
spinning: true,
|
||||
message: state.message ?? LocalCameraCubit.msgSearching,
|
||||
);
|
||||
case LocalCameraPhase.notFound:
|
||||
case LocalCameraPhase.noNetwork:
|
||||
return _buildCenterState(
|
||||
icon: Icons.videocam_off,
|
||||
message: state.message ?? LocalCameraCubit.msgNotFound,
|
||||
actionLabel: '重新搜索',
|
||||
onAction: () => cubit.runDiscovery(),
|
||||
);
|
||||
case LocalCameraPhase.found:
|
||||
return _buildCameraList(context, cubit, state);
|
||||
default:
|
||||
return _buildCenterState(
|
||||
icon: Icons.videocam_off,
|
||||
message: state.message ?? '准备就绪',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 播放器区域 =====
|
||||
Widget _buildVideoArea(
|
||||
BuildContext context,
|
||||
LocalCameraCubit cubit,
|
||||
LocalCameraState state,
|
||||
) {
|
||||
return Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: _buildPlayer(cubit),
|
||||
),
|
||||
// 连接中遮罩
|
||||
if (state.phase == LocalCameraPhase.connecting)
|
||||
Positioned.fill(
|
||||
child: _buildCenterState(
|
||||
icon: Icons.link,
|
||||
spinning: true,
|
||||
message: '正在连接摄像头...',
|
||||
dark: true,
|
||||
),
|
||||
),
|
||||
// 断流 / 播放失败遮罩 + 重新连接按钮
|
||||
if (state.phase == LocalCameraPhase.disconnected ||
|
||||
state.phase == LocalCameraPhase.playFailed)
|
||||
Positioned.fill(
|
||||
child: _buildCenterState(
|
||||
icon: Icons.error_outline,
|
||||
message: state.message ?? '播放失败',
|
||||
dark: true,
|
||||
actionLabel: '重新连接',
|
||||
onAction: () => cubit.reconnect(),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
// ===== 视频渲染(video_player / ExoPlayer)=====
|
||||
Widget _buildPlayer(LocalCameraCubit cubit) {
|
||||
final controller = cubit.videoController;
|
||||
// 控制器尚未创建 / 尚未初始化完成:先置黑底,由上层“连接中”遮罩提示
|
||||
if (controller == null || !controller.value.isInitialized) {
|
||||
return const ColoredBox(color: Colors.black);
|
||||
}
|
||||
return Center(
|
||||
child: AspectRatio(
|
||||
aspectRatio: controller.value.aspectRatio,
|
||||
child: VideoPlayer(controller),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ===== 摄像头列表 =====
|
||||
Widget _buildCameraList(
|
||||
BuildContext context,
|
||||
LocalCameraCubit cubit,
|
||||
LocalCameraState state,
|
||||
) {
|
||||
if (state.cameras.isEmpty) {
|
||||
return _buildCenterState(
|
||||
icon: Icons.videocam_off,
|
||||
message: LocalCameraCubit.msgNotFound,
|
||||
);
|
||||
}
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 8),
|
||||
child: Text(
|
||||
'发现 ${state.cameras.length} 个摄像头,点击查看实时画面',
|
||||
style: const TextStyle(color: Colors.white70, fontSize: 14),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: ListView.separated(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12),
|
||||
itemCount: state.cameras.length,
|
||||
separatorBuilder: (_, _) => const SizedBox(height: 8),
|
||||
itemBuilder: (context, i) {
|
||||
final cam = state.cameras[i];
|
||||
return _CameraTile(
|
||||
camera: cam,
|
||||
onTap: () => cubit.selectCamera(cam),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCenterState({
|
||||
required IconData icon,
|
||||
required String message,
|
||||
bool spinning = false,
|
||||
bool dark = false,
|
||||
String? actionLabel,
|
||||
VoidCallback? onAction,
|
||||
}) {
|
||||
return Container(
|
||||
color: dark ? Colors.black.withValues(alpha: 0.65) : Colors.black,
|
||||
alignment: Alignment.center,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (spinning)
|
||||
const SizedBox(
|
||||
width: 42,
|
||||
height: 42,
|
||||
child: CircularProgressIndicator(
|
||||
color: Color(0xFF165DFF),
|
||||
strokeWidth: 3,
|
||||
),
|
||||
)
|
||||
else
|
||||
Icon(icon, color: Colors.white54, size: 52),
|
||||
const SizedBox(height: 18),
|
||||
Text(
|
||||
message,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 15,
|
||||
height: 1.5,
|
||||
),
|
||||
),
|
||||
if (actionLabel != null && onAction != null) ...[
|
||||
const SizedBox(height: 22),
|
||||
ElevatedButton.icon(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFF165DFF),
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
onPressed: onAction,
|
||||
icon: const Icon(Icons.refresh, size: 18),
|
||||
label: Text(actionLabel),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ===== 链条:网络检查 → ONVIF发现 → 取流 → 拉流播放 =====
|
||||
Widget _buildChain(BuildContext context, LocalCameraState state) {
|
||||
final steps = <_ChainStep>[
|
||||
_ChainStep('网络检查', state.networkStep),
|
||||
_ChainStep('ONVIF发现', state.discoveryStep),
|
||||
_ChainStep('获取RTSP', state.streamStep),
|
||||
_ChainStep('拉流播放', state.playStep),
|
||||
];
|
||||
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
color: const Color(0xFF111111),
|
||||
padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 8),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: [
|
||||
for (int i = 0; i < steps.length; i++) ...[
|
||||
_ChainNode(step: steps[i]),
|
||||
if (i != steps.length - 1)
|
||||
_ChainConnector(active: steps[i].status == ChainStatus.success),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showNoNetworkDialog(BuildContext context, String message) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
title: const Text('网络未就绪'),
|
||||
content: Text(message),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.of(ctx).pop();
|
||||
Navigator.of(context).pop(); // 关闭本地视频页
|
||||
},
|
||||
child: const Text('我知道了'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
Navigator.of(ctx).pop();
|
||||
context.read<LocalCameraCubit>().runDiscovery();
|
||||
},
|
||||
child: const Text('重试'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ===================== 链条子组件 =====================
|
||||
|
||||
class _ChainStep {
|
||||
final String label;
|
||||
final ChainStatus status;
|
||||
_ChainStep(this.label, this.status);
|
||||
}
|
||||
|
||||
class _ChainNode extends StatelessWidget {
|
||||
final _ChainStep step;
|
||||
const _ChainNode({required this.step});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Color color;
|
||||
IconData? icon;
|
||||
bool loading = false;
|
||||
switch (step.status) {
|
||||
case ChainStatus.running:
|
||||
color = const Color(0xFF165DFF);
|
||||
loading = true;
|
||||
break;
|
||||
case ChainStatus.success:
|
||||
color = const Color(0xFF00B42A);
|
||||
icon = Icons.check;
|
||||
break;
|
||||
case ChainStatus.failed:
|
||||
color = const Color(0xFFF53F3F);
|
||||
icon = Icons.close;
|
||||
break;
|
||||
case ChainStatus.idle:
|
||||
color = Colors.white24;
|
||||
break;
|
||||
}
|
||||
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 34,
|
||||
height: 34,
|
||||
child: loading
|
||||
? Padding(
|
||||
padding: const EdgeInsets.all(3),
|
||||
child: CircularProgressIndicator(color: color, strokeWidth: 3),
|
||||
)
|
||||
: CircleAvatar(
|
||||
backgroundColor: color,
|
||||
radius: 17,
|
||||
child: icon != null
|
||||
? Icon(icon, color: Colors.white, size: 18)
|
||||
: const SizedBox.shrink(),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
step.label,
|
||||
style: TextStyle(
|
||||
color: step.status == ChainStatus.idle ? Colors.white38 : Colors.white,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ChainConnector extends StatelessWidget {
|
||||
final bool active;
|
||||
const _ChainConnector({required this.active});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
width: 22,
|
||||
height: 2,
|
||||
margin: const EdgeInsets.only(bottom: 20),
|
||||
color: active ? const Color(0xFF00B42A) : Colors.white24,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ===================== 摄像头列表项 =====================
|
||||
|
||||
class _CameraTile extends StatelessWidget {
|
||||
final OnvifCamera camera;
|
||||
final VoidCallback onTap;
|
||||
const _CameraTile({required this.camera, required this.onTap});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Material(
|
||||
color: const Color(0xFF1C1C1E),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
onTap: onTap,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF165DFF).withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.videocam,
|
||||
color: Color(0xFF165DFF),
|
||||
size: 22,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
camera.ip,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
camera.model.isNotEmpty
|
||||
? camera.model
|
||||
: (camera.manufacturer.isNotEmpty
|
||||
? camera.manufacturer
|
||||
: '未知型号'),
|
||||
style: const TextStyle(color: Colors.white54, fontSize: 13),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Icon(Icons.chevron_right, color: Colors.white38),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user