本地视频延迟优化+视频的获取链路播放
This commit is contained in:
@@ -0,0 +1,559 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:video_player/video_player.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../data/onvif_client.dart';
|
||||
import '../../data/onvif_discovery_service.dart';
|
||||
import '../../data/onvif_subnet_scanner.dart';
|
||||
import '../../data/rtsp_url_guesser.dart';
|
||||
import '../../domain/entities/onvif_camera.dart';
|
||||
import 'local_camera_state.dart';
|
||||
|
||||
/// 本地摄像头(ONVIF 发现 + RTSP 播放)核心 Cubit。
|
||||
/// 负责:网络检查 → WS-Discovery 搜索 → ONVIF 取 RTSP → video_player(ExoPlayer) 拉流播放 → 断流/失败处理。
|
||||
class LocalCameraCubit extends Cubit<LocalCameraState> {
|
||||
LocalCameraCubit({OnvifClient? client})
|
||||
: _client = client ?? OnvifClient(username: 'admin', password: ''),
|
||||
super(const LocalCameraState());
|
||||
|
||||
final OnvifDiscoveryService _discovery = OnvifDiscoveryService();
|
||||
final OnvifClient _client;
|
||||
|
||||
/// 网段扫描兜底(组播失败时不依赖组播,直接 TCP 探设 ONVIF 设备)。
|
||||
late final OnvifSubnetScanner _scanner = OnvifSubnetScanner(client: _client);
|
||||
|
||||
// ===== 播放器(video_player / Android=ExoPlayer)=====
|
||||
// ExoPlayer 原生支持 RTSP(RtspMediaSource),默认走 TCP 交错、缓冲小,天然低延迟,
|
||||
// 不需要 mpv 那套 cache/demuxer 手动调参。
|
||||
VideoPlayerController? _videoController;
|
||||
VoidCallback? _controllerListener;
|
||||
|
||||
/// 供页面渲染视频用;未就绪时为 null。
|
||||
VideoPlayerController? get videoController => _videoController;
|
||||
|
||||
static const String _prefsKeyLastList = 'local_camera_last_list';
|
||||
|
||||
// ===== 提示语常量(严格按需求原文)=====
|
||||
static const String msgNoNetworkEntry = '请打开手机以太网网络共享和连上设备的以太网线,才能搜索摄像头';
|
||||
static const String msgNoNetwork = '请打开手机以太网网络共享(或连上网络),才能搜索摄像头';
|
||||
static const String msgSearching = '正在搜索局域网内摄像头,请稍候...';
|
||||
static const String msgNotFound =
|
||||
'未发现摄像头。请检查:①网络已连接 ②摄像头开启ONVIF ③同一网段';
|
||||
static const String msgDisconnected = '共享网络断开,请去检查';
|
||||
|
||||
StreamSubscription? _connectivitySub;
|
||||
Timer? _openWatchdog;
|
||||
Timer? _stallTimer;
|
||||
bool _userStopped = false;
|
||||
|
||||
/// 页面进入时调用:检查网络并自动搜索。
|
||||
Future<void> start() async {
|
||||
_listenConnectivity();
|
||||
await _checkNetworkAndDiscover();
|
||||
}
|
||||
|
||||
/// 链条:①网络检查 ②ONVIF发现 ③取流(GetStreamUri) ④拉流播放
|
||||
Future<void> _checkNetworkAndDiscover() async {
|
||||
emit(state.copyWith(networkStep: ChainStatus.running));
|
||||
final ok = await _isEthernetConnected();
|
||||
if (!ok) {
|
||||
final detail = await _describeNetworks();
|
||||
emit(
|
||||
state.copyWith(
|
||||
phase: LocalCameraPhase.noNetwork,
|
||||
networkStep: ChainStatus.failed,
|
||||
discoveryStep: ChainStatus.idle,
|
||||
message: '$msgNoNetworkEntry\n\n当前网卡:$detail',
|
||||
clearSelected: true,
|
||||
clearRtsp: true,
|
||||
showReconnect: false,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
emit(state.copyWith(networkStep: ChainStatus.success));
|
||||
await runDiscovery();
|
||||
}
|
||||
|
||||
/// 链条②:WS-Discovery 搜索摄像头。
|
||||
Future<void> runDiscovery() async {
|
||||
emit(
|
||||
state.copyWith(
|
||||
phase: LocalCameraPhase.searching,
|
||||
discoveryStep: ChainStatus.running,
|
||||
streamStep: ChainStatus.idle,
|
||||
playStep: ChainStatus.idle,
|
||||
clearSelected: true,
|
||||
clearRtsp: true,
|
||||
message: msgSearching,
|
||||
showReconnect: false,
|
||||
),
|
||||
);
|
||||
|
||||
try {
|
||||
var cams = await _discovery.discover();
|
||||
// 组播对杂牌摄像头通常无应答 → 先快速校验上次缓存的 IP,命中即秒开,
|
||||
// 避免每次都跑满 254×8 的网段扫描("有时扫不到 + 耗时长"的主因)。
|
||||
if (cams.isEmpty) {
|
||||
cams = await _validateCached(await loadCachedCameras());
|
||||
}
|
||||
if (cams.isEmpty) {
|
||||
// 组播未发现 → 网段扫描兜底(对齐 IP Cam Viewer 的 IP Scanner,不吃组播/锁)
|
||||
debugPrint('🔎 [发现] 组播未发现,尝试网段扫描兜底');
|
||||
cams = await _scanner.scan();
|
||||
}
|
||||
if (cams.isEmpty) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
phase: LocalCameraPhase.notFound,
|
||||
discoveryStep: ChainStatus.failed,
|
||||
cameras: const [],
|
||||
message: msgNotFound,
|
||||
showReconnect: false,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// 尽量补全型号(best-effort,失败不影响列表展示)
|
||||
final enriched = await Future.wait(
|
||||
cams.map((c) async {
|
||||
if (c.model.isNotEmpty) return c;
|
||||
try {
|
||||
final info = await _client.getDeviceInformation(c.xAddr);
|
||||
return c.copyWith(manufacturer: info.manufacturer, model: info.model);
|
||||
} catch (_) {
|
||||
return c;
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
await _cacheCameras(enriched);
|
||||
emit(
|
||||
state.copyWith(
|
||||
phase: LocalCameraPhase.found,
|
||||
discoveryStep: ChainStatus.success,
|
||||
cameras: enriched,
|
||||
message: null,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
phase: LocalCameraPhase.notFound,
|
||||
discoveryStep: ChainStatus.failed,
|
||||
message: msgNotFound,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 链条③④:选择摄像头 → 依次试候选 RTSP URL。
|
||||
/// 候选顺序:① ONVIF 正规 GetStreamUri 结果 → ② RtspUrlGuesser 常见品牌模板。
|
||||
Future<void> selectCamera(OnvifCamera cam) async {
|
||||
_userStopped = false;
|
||||
emit(
|
||||
state.copyWith(
|
||||
phase: LocalCameraPhase.connecting,
|
||||
selected: cam,
|
||||
streamStep: ChainStatus.running,
|
||||
playStep: ChainStatus.idle,
|
||||
clearRtsp: true,
|
||||
clearMessage: true,
|
||||
showReconnect: false,
|
||||
),
|
||||
);
|
||||
|
||||
await _disposeController();
|
||||
_armOpenWatchdog();
|
||||
|
||||
// 1) 先按 ONVIF 正道取流(能拿到就放到候选第一位)
|
||||
final urls = <String>[];
|
||||
String? onvifUrl;
|
||||
try {
|
||||
onvifUrl = await _client.resolveRtsp(cam.xAddr);
|
||||
urls.add(onvifUrl);
|
||||
debugPrint('🎬 [拉流] ONVIF 解析成功:$onvifUrl');
|
||||
} catch (e) {
|
||||
debugPrint('🎬 [拉流] ONVIF 未给出 RTSP:$e');
|
||||
}
|
||||
|
||||
// 2) 猜测表兜底(杂牌摄像头大概率命中这里)
|
||||
urls.addAll(
|
||||
RtspUrlGuesser.candidates(
|
||||
ip: cam.ip,
|
||||
user: _client.username,
|
||||
password: _client.password,
|
||||
),
|
||||
);
|
||||
final dedup = urls.toSet().toList();
|
||||
if (dedup.isEmpty) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
phase: LocalCameraPhase.playFailed,
|
||||
streamStep: ChainStatus.failed,
|
||||
playStep: ChainStatus.failed,
|
||||
message: '无可用 RTSP 地址',
|
||||
showReconnect: true,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// 3) 逐个试:任一 URL 在 5s 内 playing=true 即成功
|
||||
for (var i = 0; i < dedup.length; i++) {
|
||||
final url = dedup[i];
|
||||
debugPrint('🎬 [拉流] 尝试 #${i + 1}/${dedup.length}: $url');
|
||||
final ok = await _tryPlay(url, timeout: const Duration(seconds: 5));
|
||||
if (ok) {
|
||||
_cancelWatchdog();
|
||||
debugPrint('🎬 [拉流] 成功:$url');
|
||||
emit(
|
||||
state.copyWith(
|
||||
phase: LocalCameraPhase.playing,
|
||||
rtspUrl: url,
|
||||
streamStep: ChainStatus.success,
|
||||
playStep: ChainStatus.success,
|
||||
clearMessage: true,
|
||||
showReconnect: false,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 全部失败
|
||||
_cancelWatchdog();
|
||||
emit(
|
||||
state.copyWith(
|
||||
phase: LocalCameraPhase.playFailed,
|
||||
streamStep: onvifUrl != null
|
||||
? ChainStatus.success
|
||||
: ChainStatus.failed,
|
||||
playStep: ChainStatus.failed,
|
||||
message: '无法拉流:已尝试 ONVIF 与 ${dedup.length} 个常见 RTSP 路径',
|
||||
showReconnect: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 打开单个候选 URL,[timeout] 内完成 initialize() 且首帧就绪即视为成功。
|
||||
/// ExoPlayer 对 RTSP 直播默认走 TCP 交错、缓冲小;失败/超时则 dispose 后返回 false。
|
||||
Future<bool> _tryPlay(String url, {required Duration timeout}) async {
|
||||
VideoPlayerController? c;
|
||||
try {
|
||||
c = VideoPlayerController.networkUrl(Uri.parse(url));
|
||||
await c.initialize().timeout(timeout);
|
||||
// 直播流 initialize 完成即已连上并拿到分辨率;立即起播
|
||||
await c.play();
|
||||
// 短暂观察是否立刻报错(错误路径 / 鉴权失败会在这里暴露)
|
||||
await Future<void>.delayed(const Duration(milliseconds: 400));
|
||||
if (c.value.hasError) {
|
||||
debugPrint(
|
||||
'🎬 [拉流] 起播即报错:${c.value.errorDescription}',
|
||||
);
|
||||
await c.dispose();
|
||||
return false;
|
||||
}
|
||||
_videoController = c;
|
||||
_attachControllerListener(c);
|
||||
return true;
|
||||
} on TimeoutException {
|
||||
debugPrint('🎬 [拉流] 超时:$url');
|
||||
await c?.dispose();
|
||||
return false;
|
||||
} catch (e) {
|
||||
debugPrint('🎬 [拉流] open 抛错:$e');
|
||||
await c?.dispose();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 监听正在播放的控制器:断流/报错/长时间缓冲 → 判定断开。
|
||||
void _attachControllerListener(VideoPlayerController c) {
|
||||
void listener() {
|
||||
if (_userStopped || isClosed) return;
|
||||
if (c.value.hasError) {
|
||||
if (state.phase == LocalCameraPhase.playing) _handleDisconnected();
|
||||
return;
|
||||
}
|
||||
if (state.phase == LocalCameraPhase.playing) {
|
||||
if (c.value.isBuffering) {
|
||||
_stallTimer?.cancel();
|
||||
_stallTimer = Timer(const Duration(seconds: 12), _handleDisconnected);
|
||||
} else {
|
||||
_stallTimer?.cancel();
|
||||
_stallTimer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
_controllerListener = listener;
|
||||
c.addListener(listener);
|
||||
}
|
||||
|
||||
/// 释放当前控制器并摘掉监听。
|
||||
Future<void> _disposeController() async {
|
||||
_stallTimer?.cancel();
|
||||
_stallTimer = null;
|
||||
final c = _videoController;
|
||||
_videoController = null;
|
||||
if (c != null) {
|
||||
if (_controllerListener != null) c.removeListener(_controllerListener!);
|
||||
_controllerListener = null;
|
||||
try {
|
||||
await c.dispose();
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
void _cancelWatchdog() {
|
||||
_openWatchdog?.cancel();
|
||||
_openWatchdog = null;
|
||||
}
|
||||
|
||||
void _handleDisconnected() {
|
||||
if (_userStopped || isClosed) return;
|
||||
emit(
|
||||
state.copyWith(
|
||||
phase: LocalCameraPhase.disconnected,
|
||||
playStep: ChainStatus.failed,
|
||||
message: msgDisconnected,
|
||||
showReconnect: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 点击"重新连接":有选中摄像头则重拉流,否则重新搜索。
|
||||
Future<void> reconnect() async {
|
||||
final cam = state.selected;
|
||||
if (cam == null) {
|
||||
await _checkNetworkAndDiscover();
|
||||
return;
|
||||
}
|
||||
await selectCamera(cam);
|
||||
}
|
||||
|
||||
/// 返回摄像头列表(重新搜索)。
|
||||
Future<void> backToList() async {
|
||||
_userStopped = true;
|
||||
_cancelWatchdog();
|
||||
_stallTimer?.cancel();
|
||||
await _disposeController();
|
||||
emit(
|
||||
state.copyWith(
|
||||
phase: state.cameras.isNotEmpty
|
||||
? LocalCameraPhase.found
|
||||
: LocalCameraPhase.idle,
|
||||
clearSelected: true,
|
||||
clearRtsp: true,
|
||||
streamStep: ChainStatus.idle,
|
||||
playStep: ChainStatus.idle,
|
||||
clearMessage: true,
|
||||
showReconnect: false,
|
||||
),
|
||||
);
|
||||
if (state.cameras.isEmpty) {
|
||||
await runDiscovery();
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 共享网络(以太网)检查 =====
|
||||
/// 本功能运行环境:手机需开启“以太网共享”并接上 USB‑C 转以太网,
|
||||
/// 才能跟局域网摄像头通信。这里用"双信号取或"保证不漏检:
|
||||
/// 1) `connectivity_plus` 直接报 `ConnectivityResult.ethernet`;
|
||||
/// 2) 本机网卡里存在“名字像以太网 或 非 WiFi/移动”的 IPv4 接口。
|
||||
Future<bool> _isEthernetConnected() async {
|
||||
// 信号 1
|
||||
try {
|
||||
final results = await Connectivity().checkConnectivity();
|
||||
if (results.contains(ConnectivityResult.ethernet)) {
|
||||
debugPrint('🌐 [网络检查] connectivity_plus 报以太网 ✔');
|
||||
return true;
|
||||
}
|
||||
debugPrint('🌐 [网络检查] connectivity_plus 结果:$results');
|
||||
} catch (e) {
|
||||
debugPrint('🌐 [网络检查] connectivity_plus 失败:$e');
|
||||
}
|
||||
// 信号 2
|
||||
try {
|
||||
final ifs = await NetworkInterface.list(
|
||||
type: InternetAddressType.IPv4,
|
||||
includeLoopback: false,
|
||||
);
|
||||
for (final i in ifs) {
|
||||
if (_looksEthernet(i.name)) {
|
||||
debugPrint('🌐 [网络检查] 网卡 ${i.name} 明确为以太网 ✔');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (final i in ifs) {
|
||||
if (!_looksWifiOrCellular(i.name) && i.addresses.isNotEmpty) {
|
||||
debugPrint('🌐 [网络检查] 网卡 ${i.name} 非 WiFi/移动,视为以太网共享 ✔');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
debugPrint('🌐 [网络检查] 未发现以太网:${_fmtIfs(ifs)}');
|
||||
return false;
|
||||
} catch (e) {
|
||||
debugPrint('🌐 [网络检查] 枚举网卡失败:$e(保守放行,交给后续探测)');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// 把当前网卡列成人读摘要(失败提示拼上,用户一眼看到“到底有没有插”)。
|
||||
Future<String> _describeNetworks() async {
|
||||
try {
|
||||
final ifs = await NetworkInterface.list(
|
||||
type: InternetAddressType.IPv4,
|
||||
includeLoopback: false,
|
||||
);
|
||||
if (ifs.isEmpty) return '无任何非回环 IPv4 网卡(以太网线可能未接好或共享未开)';
|
||||
return _fmtIfs(ifs);
|
||||
} catch (e) {
|
||||
return '网卡枚举失败:$e';
|
||||
}
|
||||
}
|
||||
|
||||
static bool _looksEthernet(String raw) {
|
||||
final n = raw.toLowerCase();
|
||||
return n.startsWith('eth') ||
|
||||
n.startsWith('en') ||
|
||||
n.startsWith('usb') ||
|
||||
n.contains('rndis') ||
|
||||
n.contains('asix') ||
|
||||
n.contains('ax88') ||
|
||||
n.contains('mtk_eth');
|
||||
}
|
||||
|
||||
static bool _looksWifiOrCellular(String raw) {
|
||||
final n = raw.toLowerCase();
|
||||
const prefixes = [
|
||||
'wlan', 'ap', 'rmnet', 'ccmni', 'gbr', 'qrtr', 'mobile',
|
||||
'vnet', 'dummy', 'lo', 'tun', 'tap', 'ppp', 'p2p', 'wpan',
|
||||
'bt', 'swlan', 'utun', 'ipsec',
|
||||
];
|
||||
for (final p in prefixes) {
|
||||
if (n.startsWith(p)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static String _fmtIfs(List<NetworkInterface> ifs) {
|
||||
if (ifs.isEmpty) return '无任何非回环 IPv4 网卡';
|
||||
return ifs
|
||||
.map(
|
||||
(i) =>
|
||||
'${i.name}(${i.addresses.map((a) => a.address.toString()).join(",")})',
|
||||
)
|
||||
.join(' | ');
|
||||
}
|
||||
|
||||
void _listenConnectivity() {
|
||||
_connectivitySub?.cancel();
|
||||
_connectivitySub = Connectivity().onConnectivityChanged.listen(
|
||||
(List<ConnectivityResult> results) {
|
||||
// 盯以太网掉线(拔掉以太网线 / 共享网络断开),而非任意网络
|
||||
final ethernetLost = !results.contains(ConnectivityResult.ethernet);
|
||||
if (ethernetLost &&
|
||||
(state.isPlaying || state.phase == LocalCameraPhase.connecting)) {
|
||||
_handleDisconnected();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// 快速校验上次缓存的候选:短超时逐个探活,任一 SOAP 有回包即保留。
|
||||
/// 全部不可达则返回空,交由上层做满段扫描兜底。
|
||||
Future<List<OnvifCamera>> _validateCached(List<OnvifCamera> cached) async {
|
||||
if (cached.isEmpty) return const [];
|
||||
final results = await Future.wait(
|
||||
cached.map((c) async {
|
||||
try {
|
||||
await _client
|
||||
.getDeviceInformation(c.xAddr)
|
||||
.timeout(const Duration(milliseconds: 1200));
|
||||
debugPrint('缓存校验命中 ${c.xAddr},跳过满段扫描');
|
||||
return c;
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}),
|
||||
);
|
||||
return results.whereType<OnvifCamera>().toList();
|
||||
}
|
||||
|
||||
void _armOpenWatchdog() {
|
||||
_openWatchdog?.cancel();
|
||||
_openWatchdog = Timer(const Duration(seconds: 15), () {
|
||||
// 15s 仍未成功出画面
|
||||
if (state.phase == LocalCameraPhase.connecting ||
|
||||
state.playStep == ChainStatus.running) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
phase: LocalCameraPhase.playFailed,
|
||||
playStep: ChainStatus.failed,
|
||||
message: '连接超时,拉取视频流失败',
|
||||
showReconnect: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ===== 本地缓存(shared_preferences)=====
|
||||
Future<void> _cacheCameras(List<OnvifCamera> cams) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final list = cams
|
||||
.map(
|
||||
(c) => {
|
||||
'ip': c.ip,
|
||||
'port': c.port,
|
||||
'xAddr': c.xAddr,
|
||||
'manufacturer': c.manufacturer,
|
||||
'model': c.model,
|
||||
},
|
||||
)
|
||||
.toList();
|
||||
await prefs.setString(_prefsKeyLastList, jsonEncode(list));
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<List<OnvifCamera>> loadCachedCameras() async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final raw = prefs.getString(_prefsKeyLastList);
|
||||
if (raw == null || raw.isEmpty) return const [];
|
||||
final list = (jsonDecode(raw) as List).cast<Map<String, dynamic>>();
|
||||
return list
|
||||
.map(
|
||||
(m) => OnvifCamera(
|
||||
ip: m['ip'] as String,
|
||||
port: m['port'] as int,
|
||||
xAddr: m['xAddr'] as String,
|
||||
manufacturer: (m['manufacturer'] ?? '') as String,
|
||||
model: (m['model'] ?? '') as String,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
} catch (_) {
|
||||
return const [];
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_userStopped = true;
|
||||
_openWatchdog?.cancel();
|
||||
_stallTimer?.cancel();
|
||||
_connectivitySub?.cancel();
|
||||
await _disposeController();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
import '../../domain/entities/onvif_camera.dart';
|
||||
|
||||
/// 链条每一步的状态
|
||||
enum ChainStatus { idle, running, success, failed }
|
||||
|
||||
/// 页面整体阶段
|
||||
enum LocalCameraPhase {
|
||||
idle, // 未开始
|
||||
searching, // 正在搜索
|
||||
found, // 已发现,等待选择
|
||||
notFound, // 未发现摄像头
|
||||
noNetwork, // 未联网
|
||||
connecting, // 正在取流/拉流
|
||||
playing, // 播放中
|
||||
disconnected, // 播放中断/断流
|
||||
playFailed, // 播放失败
|
||||
}
|
||||
|
||||
class LocalCameraState extends Equatable {
|
||||
final LocalCameraPhase phase;
|
||||
final ChainStatus networkStep;
|
||||
final ChainStatus discoveryStep;
|
||||
final ChainStatus streamStep; // ONVIF 取 RTSP 地址
|
||||
final ChainStatus playStep; // media_kit 拉流
|
||||
final List<OnvifCamera> cameras;
|
||||
final OnvifCamera? selected;
|
||||
final String? rtspUrl;
|
||||
final String? message; // 提示语
|
||||
final bool showReconnect; // 是否显示"重新连接"按钮
|
||||
|
||||
const LocalCameraState({
|
||||
this.phase = LocalCameraPhase.idle,
|
||||
this.networkStep = ChainStatus.idle,
|
||||
this.discoveryStep = ChainStatus.idle,
|
||||
this.streamStep = ChainStatus.idle,
|
||||
this.playStep = ChainStatus.idle,
|
||||
this.cameras = const [],
|
||||
this.selected,
|
||||
this.rtspUrl,
|
||||
this.message,
|
||||
this.showReconnect = false,
|
||||
});
|
||||
|
||||
bool get isPlaying => phase == LocalCameraPhase.playing;
|
||||
bool get showVideo =>
|
||||
rtspUrl != null &&
|
||||
(phase == LocalCameraPhase.connecting ||
|
||||
phase == LocalCameraPhase.playing ||
|
||||
phase == LocalCameraPhase.disconnected ||
|
||||
phase == LocalCameraPhase.playFailed);
|
||||
|
||||
LocalCameraState copyWith({
|
||||
LocalCameraPhase? phase,
|
||||
ChainStatus? networkStep,
|
||||
ChainStatus? discoveryStep,
|
||||
ChainStatus? streamStep,
|
||||
ChainStatus? playStep,
|
||||
List<OnvifCamera>? cameras,
|
||||
OnvifCamera? selected,
|
||||
bool clearSelected = false,
|
||||
String? rtspUrl,
|
||||
bool clearRtsp = false,
|
||||
String? message,
|
||||
bool clearMessage = false,
|
||||
bool? showReconnect,
|
||||
}) {
|
||||
return LocalCameraState(
|
||||
phase: phase ?? this.phase,
|
||||
networkStep: networkStep ?? this.networkStep,
|
||||
discoveryStep: discoveryStep ?? this.discoveryStep,
|
||||
streamStep: streamStep ?? this.streamStep,
|
||||
playStep: playStep ?? this.playStep,
|
||||
cameras: cameras ?? this.cameras,
|
||||
selected: clearSelected ? null : (selected ?? this.selected),
|
||||
rtspUrl: clearRtsp ? null : (rtspUrl ?? this.rtspUrl),
|
||||
message: clearMessage ? null : (message ?? this.message),
|
||||
showReconnect: showReconnect ?? this.showReconnect,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
phase,
|
||||
networkStep,
|
||||
discoveryStep,
|
||||
streamStep,
|
||||
playStep,
|
||||
cameras,
|
||||
selected,
|
||||
rtspUrl,
|
||||
message,
|
||||
showReconnect,
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user