USB 摄像头+远程遥控+中英的适配+topic的集成和状态的使用到相应页面
This commit is contained in:
@@ -6,6 +6,8 @@ import 'package:crypto/crypto.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:xml/xml.dart';
|
||||
|
||||
import 'rtsp_playback_tuning.dart';
|
||||
|
||||
/// 轻量 ONVIF SOAP 客户端。默认账号 admin、密码为空(构造函数可覆盖)。
|
||||
/// 提供 GetDeviceInformation / GetCapabilities(Media XAddr) / GetProfiles / GetStreamUri。
|
||||
/// 认证采用 WS-Security UsernameToken + PasswordDigest。
|
||||
@@ -170,27 +172,17 @@ class OnvifClient {
|
||||
return _firstText(doc, 'XAddr');
|
||||
}
|
||||
|
||||
/// 获取第一个视频 Profile 的 token。
|
||||
Future<String> getFirstProfileToken(String mediaXAddr) async {
|
||||
final body = _envelope(
|
||||
serviceNs: 'http://www.onvif.org/ver10/media/wsdl',
|
||||
actionTail: 'GetProfiles',
|
||||
bodyInner:
|
||||
'<trt:GetProfiles xmlns:trt="http://www.onvif.org/ver10/media/wsdl"/>',
|
||||
);
|
||||
final resp = await _post(mediaXAddr, body);
|
||||
final doc = XmlDocument.parse(resp);
|
||||
// Profiles 元素的 token 属性即 ProfileToken
|
||||
for (final p in doc.findAllElements('Profiles')) {
|
||||
final token = p.getAttribute('token');
|
||||
if (token != null && token.isNotEmpty) return token;
|
||||
}
|
||||
throw OnvifException('未获取到视频 Profile');
|
||||
}
|
||||
|
||||
/// 选择“分辨率最小”的视频 Profile(即子码流)以降低延迟与码率;
|
||||
/// 摄像头只报一个 Profile 或无分辨率信息时退回第一个。
|
||||
Future<String> getLowLatencyProfileToken(String mediaXAddr) async {
|
||||
/// 按 [select] 挑一个视频 Profile,返回 `(token, 声明码率kbps)`。
|
||||
///
|
||||
/// 码率一并带出来是给【链路探针】当分母用的:声明码率是“应该收到多少”,
|
||||
/// 实测进给率是“实际到了多少”,两者一比就能把“图传不够”和“内容坏了”分开。
|
||||
/// 设备不报 Bitrate(杂牌机常见)时给 null,探针退成只看比例。
|
||||
///
|
||||
/// 只报一个 Profile、或压根没报分辨率时,退回第一个(因“选不出”而放弃拉流)。
|
||||
Future<(String, int?)> getProfileToken(
|
||||
String mediaXAddr,
|
||||
RtspProfileSelect select,
|
||||
) async {
|
||||
final body = _envelope(
|
||||
serviceNs: 'http://www.onvif.org/ver10/media/wsdl',
|
||||
actionTail: 'GetProfiles',
|
||||
@@ -200,28 +192,54 @@ class OnvifClient {
|
||||
final resp = await _post(mediaXAddr, body);
|
||||
final doc = XmlDocument.parse(resp);
|
||||
String? firstToken;
|
||||
int? firstKbps;
|
||||
String? bestToken;
|
||||
int? bestKbps;
|
||||
int bestArea = -1;
|
||||
final listed = <String>[];
|
||||
for (final p in doc.findAllElements('Profiles')) {
|
||||
final token = p.getAttribute('token');
|
||||
if (token == null || token.isEmpty) continue;
|
||||
firstToken ??= token;
|
||||
final w = _firstIntIn(p, 'Width');
|
||||
final h = _firstIntIn(p, 'Height');
|
||||
if (w != null && h != null && w > 0 && h > 0) {
|
||||
final area = w * h;
|
||||
if (bestArea < 0 || area < bestArea) {
|
||||
bestArea = area;
|
||||
bestToken = token;
|
||||
}
|
||||
final bitrateKbps = _firstIntIn(p, 'Bitrate');
|
||||
if (firstToken == token) firstKbps = bitrateKbps;
|
||||
listed.add(
|
||||
'$token ${w ?? '?'}x${h ?? '?'}'
|
||||
'${bitrateKbps != null ? ' ${bitrateKbps}kbps' : ''}',
|
||||
);
|
||||
if (w == null || h == null || w <= 0 || h <= 0) continue;
|
||||
if (select == RtspProfileSelect.first) continue;
|
||||
final area = w * h;
|
||||
final better = select == RtspProfileSelect.smallest
|
||||
? (bestArea < 0 || area < bestArea)
|
||||
: (bestArea < 0 || area > bestArea);
|
||||
if (better) {
|
||||
bestArea = area;
|
||||
bestToken = token;
|
||||
bestKbps = bitrateKbps;
|
||||
}
|
||||
}
|
||||
if (bestToken != null) {
|
||||
debugPrint('🎚 [子码流] 选用最小分辨率 Profile(面积≈$bestArea)token=$bestToken');
|
||||
return bestToken;
|
||||
if (listed.isNotEmpty) {
|
||||
debugPrint('🎚 [码流] ${select.label};候选:${listed.join(' | ')}');
|
||||
}
|
||||
if (firstToken != null) return firstToken;
|
||||
throw OnvifException('未获取到视频 Profile');
|
||||
if (firstToken == null) throw OnvifException('未获取到视频 Profile');
|
||||
// 选不出合适的那一路(设备只报一个 / 没报分辨率 / 选了“设备首个”)→ 退回第一个。
|
||||
final String picked;
|
||||
final int? kbps;
|
||||
if (select == RtspProfileSelect.first || bestToken == null) {
|
||||
picked = firstToken;
|
||||
kbps = firstKbps;
|
||||
} else {
|
||||
picked = bestToken;
|
||||
kbps = bestKbps;
|
||||
}
|
||||
debugPrint(
|
||||
'🎚 [码流] 最终使用 token=$picked(面积≈$bestArea'
|
||||
'${kbps != null ? ', 声明 ${kbps}kbps' : ', 设备未报码率'})',
|
||||
);
|
||||
return (picked, kbps);
|
||||
}
|
||||
|
||||
/// 在 [parent] 及其后代中找第一个可解析为 int 的 [name] 元素值。
|
||||
@@ -252,11 +270,16 @@ class OnvifClient {
|
||||
return _injectCredentials(uri);
|
||||
}
|
||||
|
||||
/// 一步到位:由 device xAddr 得到带凭证的 RTSP 地址。
|
||||
Future<String> resolveRtsp(String deviceXAddr) async {
|
||||
/// 一步到位:由 device xAddr 得到带凭证的 RTSP 地址与声明码率。
|
||||
/// [profileSelect] 由调参面板控制:取子码流还是主码流。
|
||||
Future<(String, int?)> resolveRtsp(
|
||||
String deviceXAddr, {
|
||||
RtspProfileSelect profileSelect = RtspProfileSelect.smallest,
|
||||
}) async {
|
||||
final mediaXAddr = await getMediaXAddr(deviceXAddr);
|
||||
final token = await getLowLatencyProfileToken(mediaXAddr);
|
||||
return getStreamUri(mediaXAddr, token);
|
||||
final (token, bitrateKbps) = await getProfileToken(mediaXAddr, profileSelect);
|
||||
final uri = await getStreamUri(mediaXAddr, token);
|
||||
return (uri, bitrateKbps);
|
||||
}
|
||||
|
||||
/// rtsp://ip/path -> rtsp://admin:pass@ip/path。
|
||||
|
||||
Reference in New Issue
Block a user