接入声网四路视频
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../../../core/consts/http_api_consts.dart';
|
||||
import '../../domain/video/pv_agora_credentials.dart';
|
||||
|
||||
class PvAgoraRemoteDataSource {
|
||||
const PvAgoraRemoteDataSource(this.dio);
|
||||
|
||||
final Dio dio;
|
||||
|
||||
Future<void> startVideoStream(String deviceSn) async {
|
||||
final response = await _get(
|
||||
HttpApiConsts.hostStartVideoStream,
|
||||
deviceSn,
|
||||
'startVideoStream',
|
||||
);
|
||||
final body = response.data;
|
||||
if (response.statusCode != 200 ||
|
||||
body is! Map ||
|
||||
'${body['code']}' != '200' ||
|
||||
body['data'] == false) {
|
||||
final message = body is Map ? body['msg'] : null;
|
||||
throw FormatException(
|
||||
message is String && message.trim().isNotEmpty
|
||||
? message.trim()
|
||||
: '服务端未确认推流已开启',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<PvAgoraCredentials> getCredentials(String deviceSn) async {
|
||||
final response = await _get(
|
||||
HttpApiConsts.hostAgoraToken,
|
||||
deviceSn,
|
||||
'getAgoraToken',
|
||||
);
|
||||
final body = response.data;
|
||||
if (body is! Map) throw const FormatException('声网配置响应格式错误');
|
||||
if (body.containsKey('code') && '${body['code']}' != '200') {
|
||||
throw const FormatException('服务端未能签发声网配置,请稍后重试');
|
||||
}
|
||||
final data = body.containsKey('data') ? body['data'] : body;
|
||||
if (data is! Map) throw const FormatException('声网配置响应格式错误');
|
||||
|
||||
final appId = data['appId'];
|
||||
final token = data['token'];
|
||||
final uid = int.tryParse('${data['uid']}'.trim());
|
||||
if (appId is! String || token is! String || uid == null) {
|
||||
throw const FormatException('声网配置缺少有效的 App ID、Token 或 UID');
|
||||
}
|
||||
// 加入频道只需要已签发的 Token,App Certificate 由服务端使用。
|
||||
return PvAgoraCredentials(
|
||||
appId: appId.trim(),
|
||||
token: token.trim(),
|
||||
viewerUid: uid,
|
||||
);
|
||||
}
|
||||
|
||||
Future<Response<Object?>> _get(
|
||||
String url,
|
||||
String deviceSn,
|
||||
String endpoint,
|
||||
) async {
|
||||
try {
|
||||
final response = await dio.get<Object?>(
|
||||
url,
|
||||
queryParameters: {'deviceId': deviceSn},
|
||||
);
|
||||
_logResponse(endpoint, deviceSn, response);
|
||||
return response;
|
||||
} on DioException catch (error) {
|
||||
_logResponse(endpoint, deviceSn, error.response, errorType: error.type);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
void _logResponse(
|
||||
String endpoint,
|
||||
String deviceSn,
|
||||
Response<Object?>? response, {
|
||||
DioExceptionType? errorType,
|
||||
}) {
|
||||
final body = response?.data;
|
||||
final fields = <String, Object?>{
|
||||
'deviceId': deviceSn,
|
||||
'httpStatus': response?.statusCode,
|
||||
'bodyType': body.runtimeType.toString(),
|
||||
if (errorType != null) 'errorType': errorType.name,
|
||||
'body': _redact(_decodeForLog(body)),
|
||||
};
|
||||
debugPrint('[声网配置][$endpoint] ${jsonEncode(fields)}');
|
||||
}
|
||||
|
||||
Object? _decodeForLog(Object? body) {
|
||||
if (body is! String) return body;
|
||||
try {
|
||||
final decoded = jsonDecode(body);
|
||||
if (decoded is Map || decoded is List) return decoded;
|
||||
} on FormatException {
|
||||
// 纯文本可能包含凭据,只记录长度。
|
||||
}
|
||||
return '<文本长度 ${body.length}>';
|
||||
}
|
||||
|
||||
Object? _redact(Object? value) {
|
||||
if (value is Map) {
|
||||
return value.map((key, field) {
|
||||
final name = key.toString();
|
||||
final normalized = name.toLowerCase();
|
||||
if (normalized.contains('token') ||
|
||||
normalized.contains('certificate') ||
|
||||
normalized == 'authorization') {
|
||||
final masked = field is String && field.isNotEmpty
|
||||
? '<已脱敏,长度 ${field.length}>'
|
||||
: field == null || field == ''
|
||||
? field
|
||||
: '<已脱敏>';
|
||||
return MapEntry(name, masked);
|
||||
}
|
||||
return MapEntry(name, _redact(field));
|
||||
});
|
||||
}
|
||||
if (value is List) return value.map(_redact).toList();
|
||||
if (value == null ||
|
||||
value is String ||
|
||||
value is bool ||
|
||||
(value is num && value.isFinite)) {
|
||||
return value;
|
||||
}
|
||||
return '<${value.runtimeType}>';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user