940 lines
30 KiB
Dart
940 lines
30 KiB
Dart
import 'dart:async';
|
||
import 'dart:io';
|
||
import 'dart:ui' as ui;
|
||
import 'dart:math'; // Import the math package to use sqrt
|
||
|
||
import 'package:flutter/foundation.dart';
|
||
import 'package:flutter/rendering.dart';
|
||
import 'package:flutter/services.dart';
|
||
import 'package:flutter_pcm_sound/flutter_pcm_sound.dart';
|
||
import 'package:flutter_screen_recording/flutter_screen_recording.dart';
|
||
import 'package:flutter_voice_processor/flutter_voice_processor.dart';
|
||
import 'package:gallery_saver/gallery_saver.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:image_gallery_saver/image_gallery_saver.dart';
|
||
import 'package:path_provider/path_provider.dart';
|
||
import 'package:permission_handler/permission_handler.dart';
|
||
import 'package:star_lock/app_settings/app_settings.dart';
|
||
import 'package:star_lock/login/login/entity/LoginEntity.dart';
|
||
import 'package:star_lock/main/lockDetail/lockDetail/lockDetail_logic.dart';
|
||
import 'package:star_lock/main/lockDetail/lockDetail/lockDetail_state.dart';
|
||
import 'package:star_lock/main/lockDetail/lockDetail/lockNetToken_entity.dart';
|
||
import 'package:star_lock/main/lockDetail/lockSet/lockSet/lockSetInfo_entity.dart';
|
||
import 'package:star_lock/main/lockMian/entity/lockListInfo_entity.dart';
|
||
import 'package:star_lock/network/api_repository.dart';
|
||
import 'package:star_lock/talk/call/g711.dart';
|
||
import 'package:star_lock/talk/starChart/constant/talk_status.dart';
|
||
import 'package:star_lock/talk/starChart/proto/talk_data.pb.dart';
|
||
import 'package:star_lock/talk/starChart/proto/talk_expect.pb.dart';
|
||
import 'package:star_lock/talk/starChart/star_chart_manage.dart';
|
||
import 'package:star_lock/talk/starChart/views/talkView/talk_view_state.dart';
|
||
import 'package:star_lock/tools/bugly/bugly_tool.dart';
|
||
|
||
import '../../../../tools/baseGetXController.dart';
|
||
|
||
class TalkViewLogic extends BaseGetXController {
|
||
final TalkViewState state = TalkViewState();
|
||
|
||
final LockDetailState lockDetailState = Get.put(LockDetailLogic()).state;
|
||
Timer? _syncTimer; // 音视频播放刷新率定时器
|
||
Timer? _audioTimer; // 音视频播放刷新率定时器
|
||
Timer? _networkQualityTimer; // 网络质量监测定时器
|
||
|
||
int _startTime = 0; // 开始播放时间戳,用于判断帧数据中的时间戳位置
|
||
int bufferSize = 40; // 缓冲区大小(以帧为单位)
|
||
int audioBufferSize = 500; // 缓冲区大小(以帧为单位)
|
||
// 帧率监控相关
|
||
final List<double> _lastFewFps = <double>[]; // 存储最近的帧率数据
|
||
|
||
int frameIntervalMs = 83; // 初始帧间隔设置为83毫秒(12FPS)
|
||
int audioFrameIntervalMs = 20; // 初始帧间隔设置为45毫秒(约22FPS)
|
||
int minFrameIntervalMs = 83; // 最小帧间隔(12 FPS)
|
||
int maxFrameIntervalMs = 166; // 最大帧间隔(约6 FPS)
|
||
// 定义音频帧缓冲和发送函数
|
||
final List<int> _bufferedAudioFrames = <int>[];
|
||
|
||
// 在类的开始处添加缓存相关变量
|
||
final int maxImageCacheCount = 40; // 最大图片缓存数量
|
||
final Map<String, ui.Image> _imageCache = {};
|
||
|
||
/// 初始化音频播放器
|
||
void _initFlutterPcmSound() {
|
||
const int sampleRate = 8000;
|
||
FlutterPcmSound.setLogLevel(LogLevel.none);
|
||
FlutterPcmSound.setup(sampleRate: sampleRate, channelCount: 1);
|
||
// 设置 feed 阈值
|
||
if (Platform.isAndroid) {
|
||
FlutterPcmSound.setFeedThreshold(1024); // Android 平台的特殊处理
|
||
} else {
|
||
FlutterPcmSound.setFeedThreshold(2000); // 非 Android 平台的处理
|
||
}
|
||
}
|
||
|
||
/// 挂断
|
||
void udpHangUpAction() async {
|
||
if (state.talkStatus.value == TalkStatus.answeredSuccessfully) {
|
||
// 如果是通话中就挂断
|
||
StartChartManage().startTalkHangupMessageTimer();
|
||
} else {
|
||
// 拒绝
|
||
StartChartManage().startTalkRejectMessageTimer();
|
||
}
|
||
Get.back();
|
||
}
|
||
|
||
// 发起接听命令
|
||
void initiateAnswerCommand() {
|
||
StartChartManage().startTalkAcceptTimer();
|
||
}
|
||
|
||
// 监听音视频数据流
|
||
void _startListenTalkData() {
|
||
state.talkDataRepository.talkDataStream.listen((TalkData talkData) async {
|
||
final contentType = talkData.contentType;
|
||
// 判断数据类型,进行分发处理
|
||
switch (contentType) {
|
||
case TalkData_ContentTypeE.G711:
|
||
if (state.audioBuffer.length >= audioBufferSize) {
|
||
state.audioBuffer.removeAt(0); // 丢弃最旧的数据
|
||
}
|
||
state.audioBuffer.add(talkData); // 添加新数据
|
||
break;
|
||
case TalkData_ContentTypeE.Image:
|
||
if (state.videoBuffer.length >= bufferSize) {
|
||
state.videoBuffer.removeAt(0); // 丢弃最旧的数据
|
||
}
|
||
state.videoBuffer.add(talkData); // 添加新数据
|
||
break;
|
||
}
|
||
});
|
||
}
|
||
|
||
/// 监听对讲状态
|
||
void _startListenTalkStatus() {
|
||
state.startChartTalkStatus.statusStream.listen((talkStatus) {
|
||
state.talkStatus.value = talkStatus;
|
||
switch (talkStatus) {
|
||
case TalkStatus.rejected:
|
||
case TalkStatus.hangingUpDuring:
|
||
case TalkStatus.notTalkData:
|
||
case TalkStatus.notTalkPing:
|
||
case TalkStatus.end:
|
||
_handleInvalidTalkStatus();
|
||
break;
|
||
case TalkStatus.answeredSuccessfully:
|
||
state.oneMinuteTimeTimer?.cancel(); // 取消旧定时器
|
||
state.oneMinuteTimeTimer ??=
|
||
Timer.periodic(const Duration(seconds: 1), (Timer t) {
|
||
if (state.listData.value.length > 0) {
|
||
state.oneMinuteTime.value++;
|
||
if (state.oneMinuteTime.value >= 60) {
|
||
t.cancel(); // 取消定时器
|
||
state.oneMinuteTime.value = 0;
|
||
}
|
||
}
|
||
});
|
||
break;
|
||
default:
|
||
// 其他状态的处理
|
||
break;
|
||
}
|
||
});
|
||
}
|
||
|
||
/// 播放音频数据
|
||
void _playAudioData(TalkData talkData) async {
|
||
if (state.isOpenVoice.value) {
|
||
final list =
|
||
G711().decodeAndDenoise(talkData.content, true, 8000, 300, 150);
|
||
// // 将 PCM 数据转换为 PcmArrayInt16
|
||
final PcmArrayInt16 fromList = PcmArrayInt16.fromList(list);
|
||
FlutterPcmSound.feed(fromList);
|
||
if (!state.isPlaying.value) {
|
||
FlutterPcmSound.play();
|
||
state.isPlaying.value = true;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 播放视频数据
|
||
void _playVideoData(TalkData talkData) async {
|
||
try {
|
||
// 计算当前帧的哈希值作为缓存key
|
||
String cacheKey = talkData.content.hashCode.toString();
|
||
|
||
// 检查缓存
|
||
if (_imageCache.containsKey(cacheKey)) {
|
||
// 使用缓存的解码图片
|
||
state.currentImage.value = _imageCache[cacheKey];
|
||
} else {
|
||
// 将 List<int> 转换为 Uint8List
|
||
final Uint8List uint8Data = Uint8List.fromList(talkData.content);
|
||
// 在后台线程解码图片
|
||
ui.Image? image = await decodeImageFromList(uint8Data);
|
||
|
||
// 缓存管理:如果缓存太大则移除最早的项
|
||
if (_imageCache.length >= maxImageCacheCount) {
|
||
_imageCache.remove(_imageCache.keys.first);
|
||
}
|
||
|
||
// 添加到缓存
|
||
_imageCache[cacheKey] = image;
|
||
state.currentImage.value = image;
|
||
}
|
||
|
||
// 更新显示数据
|
||
state.listData.value = Uint8List.fromList(talkData.content);
|
||
} catch (e) {
|
||
print('视频帧解码错误: $e');
|
||
}
|
||
// state.listData.value = Uint8List.fromList(talkData.content);
|
||
}
|
||
|
||
/// 启动播放
|
||
void _startPlayback() {
|
||
Future.delayed(Duration(milliseconds: 800), () {
|
||
// 添加网络质量监测
|
||
_networkQualityTimer ??=
|
||
Timer.periodic(const Duration(seconds: 5), _checkNetworkQuality);
|
||
_startTime = DateTime.now().millisecondsSinceEpoch;
|
||
_syncTimer ??=
|
||
Timer.periodic(Duration(milliseconds: frameIntervalMs), (timer) {
|
||
// 动态调整帧间隔
|
||
_adjustFrameInterval();
|
||
// 监控帧率稳定性
|
||
_monitorFrameStability();
|
||
});
|
||
});
|
||
}
|
||
|
||
/// 动态调整帧间隔
|
||
void _adjustFrameInterval() {
|
||
// 计算目标帧间隔
|
||
int targetInterval = _calculateTargetInterval();
|
||
|
||
// 平滑过渡到目标帧率,避免突变
|
||
if (frameIntervalMs != targetInterval) {
|
||
// 每次最多调整2ms,使变化更平滑
|
||
frameIntervalMs += (targetInterval > frameIntervalMs) ? 2 : -2;
|
||
|
||
// 确保在合理范围内
|
||
frameIntervalMs =
|
||
frameIntervalMs.clamp(minFrameIntervalMs, maxFrameIntervalMs);
|
||
|
||
// 只在帧间隔变化超过阈值时才重建定时器
|
||
if ((frameIntervalMs - targetInterval).abs() >= 5) {
|
||
_rebuildTimers();
|
||
}
|
||
}
|
||
// int newFrameIntervalMs = frameIntervalMs;
|
||
// if (state.videoBuffer.length < 10 && frameIntervalMs < maxFrameIntervalMs) {
|
||
// // 如果缓冲区较小且帧间隔小于最大值,则增加帧间隔
|
||
// frameIntervalMs += 5;
|
||
// } else if (state.videoBuffer.length > 20 &&
|
||
// frameIntervalMs > minFrameIntervalMs) {
|
||
// // 如果缓冲区较大且帧间隔大于最小值,则减少帧间隔
|
||
// frameIntervalMs -= 5;
|
||
// }
|
||
// // 只有在帧间隔发生变化时才重建定时器
|
||
// if (newFrameIntervalMs != frameIntervalMs) {
|
||
// frameIntervalMs = newFrameIntervalMs;
|
||
// // 取消旧的定时器
|
||
// _syncTimer?.cancel();
|
||
// _syncTimer =
|
||
// Timer.periodic(Duration(milliseconds: frameIntervalMs), (timer) {
|
||
// // 播放视频帧
|
||
// _playVideoFrames();
|
||
// });
|
||
//
|
||
// _audioTimer?.cancel();
|
||
// _audioTimer =
|
||
// Timer.periodic(Duration(milliseconds: audioFrameIntervalMs), (timer) {
|
||
// final currentTime = DateTime.now().millisecondsSinceEpoch;
|
||
// final elapsedTime = currentTime - _startTime;
|
||
//
|
||
// // 播放合适的音频帧
|
||
// if (state.audioBuffer.isNotEmpty &&
|
||
// state.audioBuffer.first.durationMs <= elapsedTime) {
|
||
// // 判断音频开关是否打开
|
||
// if (state.isOpenVoice.value) {
|
||
// _playAudioData(state.audioBuffer.removeAt(0));
|
||
// } else {
|
||
// // 如果不播放音频,只从缓冲区中读取数据,但不移除
|
||
// // 你可以根据需要调整此处逻辑,例如保留缓冲区的最大长度,防止无限增长
|
||
// // 仅移除缓冲区数据但不播放音频,确保音频也是实时更新的
|
||
// state.audioBuffer.removeAt(0);
|
||
// }
|
||
// }
|
||
// });
|
||
// }
|
||
}
|
||
|
||
/// 监控帧率稳定性
|
||
void _monitorFrameStability() {
|
||
const stabilityThreshold = 5; // 帧率波动阈值
|
||
final currentFps = 1000 / frameIntervalMs;
|
||
|
||
if (_lastFewFps.length >= 10) {
|
||
_lastFewFps.removeAt(0);
|
||
}
|
||
_lastFewFps.add(currentFps);
|
||
|
||
// 计算帧率标准差
|
||
if (_lastFewFps.length >= 5) {
|
||
double mean = _lastFewFps.reduce((a, b) => a + b) / _lastFewFps.length;
|
||
double variance =
|
||
_lastFewFps.map((fps) => pow(fps - mean, 2)).reduce((a, b) => a + b) /
|
||
_lastFewFps.length;
|
||
double stdDev = sqrt(variance);
|
||
|
||
// 如果帧率波动过大,采取平滑措施
|
||
if (stdDev > stabilityThreshold) {
|
||
_smoothFrameRate(mean);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 检查网络质量
|
||
void _checkNetworkQuality(Timer timer) {
|
||
final bufferHealth = state.videoBuffer.length / bufferSize;
|
||
|
||
if (bufferHealth < 0.3) {
|
||
// 缓冲区不足30%
|
||
// 降低帧率以适应网络状况
|
||
frameIntervalMs = min(frameIntervalMs + 10, maxFrameIntervalMs);
|
||
_rebuildTimers();
|
||
} else if (bufferHealth > 0.7) {
|
||
// 缓冲区超过70%
|
||
// 提高帧率以提供更好体验
|
||
frameIntervalMs = max(frameIntervalMs - 5, minFrameIntervalMs);
|
||
_rebuildTimers();
|
||
}
|
||
}
|
||
|
||
/// 计算目标帧间隔
|
||
int _calculateTargetInterval() {
|
||
const int optimalBufferSize = 15; // 理想的缓冲区大小
|
||
const int bufferTolerance = 5; // 缓冲区容差
|
||
|
||
if (state.videoBuffer.length < optimalBufferSize - bufferTolerance) {
|
||
// 缓冲区过小,降低帧率
|
||
return (frameIntervalMs * 1.2).round();
|
||
} else if (state.videoBuffer.length > optimalBufferSize + bufferTolerance) {
|
||
// 缓冲区过大,提高帧率
|
||
return (frameIntervalMs * 0.8).round();
|
||
}
|
||
return frameIntervalMs;
|
||
}
|
||
|
||
/// 重建定时器
|
||
void _rebuildTimers() {
|
||
// 取消现有定时器
|
||
_syncTimer?.cancel();
|
||
_audioTimer?.cancel();
|
||
|
||
// 创建新的视频定时器
|
||
_syncTimer =
|
||
Timer.periodic(Duration(milliseconds: frameIntervalMs), (timer) {
|
||
_playVideoFrames();
|
||
});
|
||
|
||
// 创建新的音频定时器,使用固定间隔
|
||
_audioTimer =
|
||
Timer.periodic(Duration(milliseconds: audioFrameIntervalMs), (timer) {
|
||
_processAudioFrame();
|
||
});
|
||
}
|
||
|
||
/// 处理音频帧
|
||
void _processAudioFrame() {
|
||
final currentTime = DateTime.now().millisecondsSinceEpoch;
|
||
final elapsedTime = currentTime - _startTime;
|
||
|
||
while (state.audioBuffer.isNotEmpty &&
|
||
state.audioBuffer.first.durationMs <= elapsedTime) {
|
||
if (state.isOpenVoice.value) {
|
||
_playAudioData(state.audioBuffer.removeAt(0));
|
||
} else {
|
||
state.audioBuffer.removeAt(0);
|
||
}
|
||
}
|
||
}
|
||
|
||
void _playVideoFrames() {
|
||
final currentTime = DateTime.now().millisecondsSinceEpoch;
|
||
final elapsedTime = currentTime - _startTime;
|
||
|
||
// 播放合适的视频帧
|
||
// 跳帧策略:如果缓冲区中有多个帧,且它们的时间戳都在当前时间之前,则播放最新的帧
|
||
int maxFramesToProcess = 5; // 每次最多处理 5 帧
|
||
int processedFrames = 0;
|
||
|
||
while (state.videoBuffer.isNotEmpty &&
|
||
state.videoBuffer.first.durationMs <= elapsedTime &&
|
||
processedFrames < maxFramesToProcess) {
|
||
if (state.videoBuffer.length > 1) {
|
||
state.videoBuffer.removeAt(0);
|
||
} else {
|
||
_playVideoData(state.videoBuffer.removeAt(0));
|
||
}
|
||
processedFrames++;
|
||
}
|
||
}
|
||
|
||
/// 停止播放音频
|
||
void _stopPlayG711Data() async {
|
||
await FlutterPcmSound.pause();
|
||
await FlutterPcmSound.stop();
|
||
await FlutterPcmSound.clear();
|
||
}
|
||
|
||
/// 开门
|
||
// udpOpenDoorAction() async {
|
||
// final List<String>? privateKey =
|
||
// await Storage.getStringList(saveBluePrivateKey);
|
||
// final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
||
//
|
||
// final List<String>? signKey = await Storage.getStringList(saveBlueSignKey);
|
||
// final List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
||
//
|
||
// final List<String>? token = await Storage.getStringList(saveBlueToken);
|
||
// final List<int> getTokenList = changeStringListToIntList(token!);
|
||
//
|
||
// await _getLockNetToken();
|
||
//
|
||
// final OpenLockCommand openLockCommand = OpenLockCommand(
|
||
// lockID: BlueManage().connectDeviceName,
|
||
// userID: await Storage.getUid(),
|
||
// openMode: lockDetailState.openDoorModel,
|
||
// openTime: _getUTCNetTime(),
|
||
// onlineToken: lockDetailState.lockNetToken,
|
||
// token: getTokenList,
|
||
// needAuthor: 1,
|
||
// signKey: signKeyDataList,
|
||
// privateKey: getPrivateKeyList,
|
||
// );
|
||
// final messageDetail = openLockCommand.packageData();
|
||
// // 将 List<int> 转换为十六进制字符串
|
||
// String hexString = messageDetail
|
||
// .map((byte) => byte.toRadixString(16).padLeft(2, '0'))
|
||
// .join(' ');
|
||
//
|
||
// AppLog.log('open lock hexString: $hexString');
|
||
// // 发送远程开门消息
|
||
// StartChartManage().sendRemoteUnLockMessage(
|
||
// bluetoothDeviceName: BlueManage().connectDeviceName,
|
||
// openLockCommand: messageDetail,
|
||
// );
|
||
// showToast('正在开锁中...'.tr);
|
||
// }
|
||
|
||
int _getUTCNetTime() {
|
||
if (lockDetailState.isHaveNetwork) {
|
||
return DateTime.now().millisecondsSinceEpoch ~/ 1000 +
|
||
lockDetailState.differentialTime;
|
||
} else {
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
// 获取手机联网token,根据锁设置里面获取的开锁时是否联网来判断是否调用这个接口
|
||
Future<void> _getLockNetToken() async {
|
||
final LockNetTokenEntity entity = await ApiRepository.to.getLockNetToken(
|
||
lockId: lockDetailState.keyInfos.value.lockId.toString());
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
lockDetailState.lockNetToken = entity.data!.token!.toString();
|
||
AppLog.log('从服务器获取联网token:${lockDetailState.lockNetToken}');
|
||
} else {
|
||
BuglyTool.uploadException(
|
||
message: '点击了需要联网开锁', detail: '点击了需要联网开锁 获取连网token失败', upload: true);
|
||
showToast('网络访问失败,请检查网络是否正常'.tr, something: () {});
|
||
}
|
||
}
|
||
|
||
/// 获取权限状态
|
||
Future<bool> getPermissionStatus() async {
|
||
final Permission permission = Permission.microphone;
|
||
//granted 通过,denied 被拒绝,permanentlyDenied 拒绝且不在提示
|
||
final PermissionStatus status = await permission.status;
|
||
if (status.isGranted) {
|
||
return true;
|
||
} else if (status.isDenied) {
|
||
requestPermission(permission);
|
||
} else if (status.isPermanentlyDenied) {
|
||
openAppSettings();
|
||
} else if (status.isRestricted) {
|
||
requestPermission(permission);
|
||
} else {}
|
||
return false;
|
||
}
|
||
|
||
///申请权限
|
||
void requestPermission(Permission permission) async {
|
||
final PermissionStatus status = await permission.request();
|
||
if (status.isPermanentlyDenied) {
|
||
openAppSettings();
|
||
}
|
||
}
|
||
|
||
Future<void> requestPermissions() async {
|
||
// 申请存储权限
|
||
var storageStatus = await Permission.storage.request();
|
||
// 申请录音权限
|
||
var microphoneStatus = await Permission.microphone.request();
|
||
|
||
if (storageStatus.isGranted && microphoneStatus.isGranted) {
|
||
print("Permissions granted");
|
||
} else {
|
||
print("Permissions denied");
|
||
// 如果权限被拒绝,可以提示用户或跳转到设置页面
|
||
if (await Permission.storage.isPermanentlyDenied) {
|
||
openAppSettings(); // 跳转到应用设置页面
|
||
}
|
||
}
|
||
}
|
||
|
||
Future<void> startRecording() async {
|
||
requestPermissions();
|
||
if (state.isRecordingScreen.value) {
|
||
showToast('录屏已开始,请勿重复点击');
|
||
}
|
||
bool start = await FlutterScreenRecording.startRecordScreen(
|
||
"Screen Recording", // 视频文件名
|
||
titleNotification: "Recording in progress", // 通知栏标题
|
||
messageNotification: "Tap to stop recording", // 通知栏内容
|
||
);
|
||
|
||
if (start) {
|
||
state.isRecordingScreen.value = true;
|
||
}
|
||
}
|
||
|
||
Future<void> stopRecording() async {
|
||
String path = await FlutterScreenRecording.stopRecordScreen;
|
||
print("Recording saved to: $path");
|
||
|
||
// 将视频保存到系统相册
|
||
bool? success = await GallerySaver.saveVideo(path);
|
||
if (success == true) {
|
||
print("Video saved to gallery");
|
||
} else {
|
||
print("Failed to save video to gallery");
|
||
}
|
||
|
||
showToast('录屏结束,已保存到系统相册');
|
||
state.isRecordingScreen.value = false;
|
||
}
|
||
|
||
@override
|
||
void onReady() {
|
||
super.onReady();
|
||
}
|
||
|
||
@override
|
||
void onInit() {
|
||
super.onInit();
|
||
|
||
// 启动监听音视频数据流
|
||
_startListenTalkData();
|
||
// 启动监听对讲状态
|
||
_startListenTalkStatus();
|
||
// 在没有监听成功之前赋值一遍状态
|
||
// *** 由于页面会在状态变化之后才会初始化,导致识别不到最新的状态,在这里手动赋值 ***
|
||
state.talkStatus.value = state.startChartTalkStatus.status;
|
||
|
||
// 初始化音频播放器
|
||
_initFlutterPcmSound();
|
||
|
||
// 启动播放定时器
|
||
_startPlayback();
|
||
|
||
// 初始化录音控制器
|
||
_initAudioRecorder();
|
||
|
||
requestPermissions();
|
||
}
|
||
|
||
/// 平滑帧率
|
||
void _smoothFrameRate(double targetFps) {
|
||
// 计算目标帧间隔
|
||
int targetInterval = (1000 / targetFps).round();
|
||
|
||
// 使用加权平均来平滑过渡
|
||
double weight = 0.3; // 权重因子,可以根据需要调整
|
||
frameIntervalMs =
|
||
(frameIntervalMs * (1 - weight) + targetInterval * weight).round();
|
||
|
||
// 确保帧间隔在合理范围内
|
||
frameIntervalMs =
|
||
frameIntervalMs.clamp(minFrameIntervalMs, maxFrameIntervalMs);
|
||
|
||
// 重建定时器
|
||
_rebuildTimers();
|
||
}
|
||
|
||
@override
|
||
void onClose() {
|
||
_stopPlayG711Data(); // 停止播放音频
|
||
state.listData.value = Uint8List(0); // 清空视频数据
|
||
state.audioBuffer.clear(); // 清空音频缓冲区
|
||
state.videoBuffer.clear(); // 清空视频缓冲区
|
||
_syncTimer?.cancel(); // 取消定时器
|
||
_syncTimer = null; // 释放定时器引用
|
||
_audioTimer?.cancel();
|
||
_audioTimer = null; // 释放定时器引用
|
||
state.oneMinuteTimeTimer?.cancel();
|
||
state.oneMinuteTimeTimer = null;
|
||
// 添加新的清理代码
|
||
_networkQualityTimer?.cancel();
|
||
_lastFewFps.clear();
|
||
stopProcessingAudio();
|
||
// 清理图片缓存
|
||
_imageCache.clear();
|
||
|
||
super.onClose();
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
stopProcessingAudio();
|
||
// 重置期望数据
|
||
StartChartManage().reSetDefaultTalkExpect();
|
||
super.dispose();
|
||
}
|
||
|
||
/// 处理无效通话状态
|
||
void _handleInvalidTalkStatus() {
|
||
state.listData.value = Uint8List(0);
|
||
// 停止播放音频
|
||
_stopPlayG711Data();
|
||
stopProcessingAudio();
|
||
}
|
||
|
||
/// 更新发送预期数据
|
||
void updateTalkExpect() {
|
||
TalkExpectReq talkExpectReq = TalkExpectReq();
|
||
state.isOpenVoice.value = !state.isOpenVoice.value;
|
||
if (!state.isOpenVoice.value) {
|
||
talkExpectReq = TalkExpectReq(
|
||
videoType: [VideoTypeE.IMAGE],
|
||
audioType: [],
|
||
);
|
||
showToast('已静音'.tr);
|
||
} else {
|
||
talkExpectReq = TalkExpectReq(
|
||
videoType: [VideoTypeE.IMAGE],
|
||
audioType: [AudioTypeE.G711],
|
||
);
|
||
}
|
||
|
||
/// 修改发送预期数据
|
||
StartChartManage().changeTalkExpectDataTypeAndReStartTalkExpectMessageTimer(
|
||
talkExpect: talkExpectReq);
|
||
}
|
||
|
||
/// 截图并保存到相册
|
||
Future<void> captureAndSavePng() async {
|
||
try {
|
||
if (state.globalKey.currentContext == null) {
|
||
AppLog.log('截图失败: 未找到当前上下文');
|
||
return;
|
||
}
|
||
final RenderRepaintBoundary boundary = state.globalKey.currentContext!
|
||
.findRenderObject()! as RenderRepaintBoundary;
|
||
final ui.Image image = await boundary.toImage();
|
||
final ByteData? byteData =
|
||
await image.toByteData(format: ui.ImageByteFormat.png);
|
||
|
||
if (byteData == null) {
|
||
AppLog.log('截图失败: 图像数据为空');
|
||
return;
|
||
}
|
||
final Uint8List pngBytes = byteData.buffer.asUint8List();
|
||
|
||
// 获取应用程序的文档目录
|
||
final Directory directory = await getApplicationDocumentsDirectory();
|
||
final String imagePath = '${directory.path}/screenshot.png';
|
||
|
||
// 将截图保存为文件
|
||
final File imgFile = File(imagePath);
|
||
await imgFile.writeAsBytes(pngBytes);
|
||
|
||
// 将截图保存到相册
|
||
await ImageGallerySaver.saveFile(imagePath);
|
||
|
||
AppLog.log('截图保存路径: $imagePath');
|
||
showToast('截图已保存到相册'.tr);
|
||
} catch (e) {
|
||
AppLog.log('截图失败: $e');
|
||
}
|
||
}
|
||
|
||
// 远程开锁
|
||
Future<void> remoteOpenLock() async {
|
||
final lockPeerId = StartChartManage().lockPeerId;
|
||
final lockListPeerId = StartChartManage().lockListPeerId;
|
||
int lockId = lockDetailState.keyInfos.value.lockId ?? 0;
|
||
|
||
// 如果锁列表获取到peerId,代表有多个锁,使用锁列表的peerId
|
||
// 从列表中遍历出对应的peerId
|
||
lockListPeerId.forEach((element) {
|
||
if (element.network?.peerId == lockPeerId) {
|
||
lockId = element.lockId ?? 0;
|
||
}
|
||
});
|
||
|
||
final LockSetInfoEntity lockSetInfoEntity =
|
||
await ApiRepository.to.getLockSettingInfoData(
|
||
lockId: lockId.toString(),
|
||
);
|
||
if (lockSetInfoEntity.errorCode!.codeIsSuccessful) {
|
||
if (lockSetInfoEntity.data?.lockFeature?.remoteUnlock == 1 &&
|
||
lockSetInfoEntity.data?.lockSettingInfo?.remoteUnlock == 1) {
|
||
final LoginEntity entity = await ApiRepository.to
|
||
.remoteOpenLock(lockId: lockId.toString(), timeOut: 60);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
showToast('已开锁'.tr);
|
||
StartChartManage().lockListPeerId = [];
|
||
}
|
||
} else {
|
||
showToast('该锁的远程开锁功能未启用'.tr);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 初始化音频录制器
|
||
void _initAudioRecorder() {
|
||
state.voiceProcessor = VoiceProcessor.instance;
|
||
}
|
||
|
||
//开始录音
|
||
Future<void> startProcessingAudio() async {
|
||
try {
|
||
if (await state.voiceProcessor?.hasRecordAudioPermission() ?? false) {
|
||
await state.voiceProcessor?.start(state.frameLength, state.sampleRate);
|
||
final bool? isRecording = await state.voiceProcessor?.isRecording();
|
||
state.isRecordingAudio.value = isRecording!;
|
||
state.startRecordingAudioTime.value = DateTime.now();
|
||
|
||
// 增加录音帧监听器和错误监听器
|
||
state.voiceProcessor
|
||
?.addFrameListeners(<VoiceProcessorFrameListener>[_onFrame]);
|
||
state.voiceProcessor?.addErrorListener(_onError);
|
||
} else {
|
||
// state.errorMessage.value = 'Recording permission not granted';
|
||
}
|
||
} on PlatformException catch (ex) {
|
||
// state.errorMessage.value = 'Failed to start recorder: $ex';
|
||
}
|
||
state.isOpenVoice.value = false;
|
||
}
|
||
|
||
/// 停止录音
|
||
Future<void> stopProcessingAudio() async {
|
||
try {
|
||
await state.voiceProcessor?.stop();
|
||
state.voiceProcessor?.removeFrameListener(_onFrame);
|
||
state.udpSendDataFrameNumber = 0;
|
||
// 记录结束时间
|
||
state.endRecordingAudioTime.value = DateTime.now();
|
||
|
||
// 计算录音的持续时间
|
||
final Duration duration = state.endRecordingAudioTime.value
|
||
.difference(state.startRecordingAudioTime.value);
|
||
|
||
state.recordingAudioTime.value = duration.inSeconds;
|
||
} on PlatformException catch (ex) {
|
||
// state.errorMessage.value = 'Failed to stop recorder: $ex';
|
||
} finally {
|
||
final bool? isRecording = await state.voiceProcessor?.isRecording();
|
||
state.isRecordingAudio.value = isRecording!;
|
||
state.isOpenVoice.value = true;
|
||
}
|
||
}
|
||
|
||
// 音频帧处理
|
||
Future<void> _onFrame(List<int> frame) async {
|
||
final List<int> processedFrame = preprocessAudio(frame);
|
||
final List<int> list = listLinearToALaw(processedFrame);
|
||
_bufferedAudioFrames.addAll(list);
|
||
|
||
final int ms = DateTime.now().millisecondsSinceEpoch -
|
||
state.startRecordingAudioTime.value.millisecondsSinceEpoch;
|
||
|
||
int getFrameLength = state.frameLength;
|
||
if (Platform.isIOS) {
|
||
getFrameLength = state.frameLength * 2;
|
||
}
|
||
|
||
if (_bufferedAudioFrames.length >= getFrameLength) {
|
||
// 发送音频数据到UDP
|
||
await StartChartManage()
|
||
.sendTalkDataMessage(
|
||
talkData: TalkData(
|
||
content: _bufferedAudioFrames,
|
||
contentType: TalkData_ContentTypeE.G711,
|
||
durationMs: ms,
|
||
),
|
||
)
|
||
.then((value) {
|
||
_bufferedAudioFrames.clear();
|
||
});
|
||
}
|
||
}
|
||
|
||
// 错误监听
|
||
void _onError(VoiceProcessorException error) {
|
||
AppLog.log(error.message!);
|
||
}
|
||
|
||
List<int> preprocessAudio(List<int> pcmList) {
|
||
// 简单的降噪处理
|
||
final List<int> processedList = [];
|
||
for (int pcmVal in pcmList) {
|
||
// 简单的降噪示例:将小于阈值的信号置为0
|
||
if (pcmVal.abs() < 200) {
|
||
pcmVal = 0;
|
||
}
|
||
processedList.add(pcmVal);
|
||
}
|
||
return processedList;
|
||
}
|
||
|
||
//test测试降噪算法
|
||
// List<int> preprocessAudio(List<int> pcmList) {
|
||
// final List<int> processedList = [];
|
||
// final int windowSize = 5;
|
||
// final int thresholdFactor = 2; // 动态阈值的倍数
|
||
|
||
// for (int i = 0; i < pcmList.length; i++) {
|
||
// int pcmVal = pcmList[i];
|
||
|
||
// // 计算当前窗口内的标准差
|
||
// int sum = 0;
|
||
// int count = 0;
|
||
// for (int j = i; j < i + windowSize && j < pcmList.length; j++) {
|
||
// sum += pcmList[j];
|
||
// count++;
|
||
// }
|
||
// int mean = sum ~/ count;
|
||
|
||
// // 计算标准差
|
||
// int varianceSum = 0;
|
||
// for (int j = i; j < i + windowSize && j < pcmList.length; j++) {
|
||
// varianceSum += (pcmList[j] - mean) * (pcmList[j] - mean);
|
||
// }
|
||
// double standardDeviation =
|
||
// sqrt(varianceSum / count); // Use sqrt from dart:math
|
||
|
||
// // 动态阈值
|
||
// int dynamicThreshold = (standardDeviation * thresholdFactor).toInt();
|
||
|
||
// // 动态降噪:如果信号小于动态阈值,则设为0
|
||
// if (pcmVal.abs() < dynamicThreshold) {
|
||
// pcmVal = 0;
|
||
// }
|
||
|
||
// // 移动平均滤波器
|
||
// int sumFilter = 0;
|
||
// int countFilter = 0;
|
||
// for (int j = i; j < i + windowSize && j < pcmList.length; j++) {
|
||
// sumFilter += pcmList[j];
|
||
// countFilter++;
|
||
// }
|
||
// int average = sumFilter ~/ countFilter;
|
||
|
||
// processedList.add(average);
|
||
// }
|
||
|
||
// return processedList;
|
||
// }
|
||
|
||
List<int> adjustVolume(List<int> pcmList, double volume) {
|
||
final List<int> adjustedPcmList = <int>[];
|
||
for (final int pcmVal in pcmList) {
|
||
// 调整音量
|
||
int adjustedPcmVal = (pcmVal * volume).round();
|
||
|
||
// 裁剪到 16-bit PCM 范围
|
||
if (adjustedPcmVal > 32767) {
|
||
adjustedPcmVal = 32767;
|
||
} else if (adjustedPcmVal < -32768) {
|
||
adjustedPcmVal = -32768;
|
||
}
|
||
|
||
adjustedPcmList.add(adjustedPcmVal);
|
||
}
|
||
return adjustedPcmList;
|
||
}
|
||
|
||
List<int> listLinearToALaw(List<int> pcmList) {
|
||
// 先调节音量
|
||
final List<int> adjustedPcmList = adjustVolume(pcmList, 5.0);
|
||
|
||
// 再进行 A-law 编码
|
||
final List<int> aLawList = <int>[];
|
||
for (final int pcmVal in adjustedPcmList) {
|
||
final int aLawVal = linearToALaw(pcmVal);
|
||
aLawList.add(aLawVal);
|
||
}
|
||
return aLawList;
|
||
}
|
||
|
||
int linearToALaw(int pcmVal) {
|
||
const int alawMax = 0x7FFF; // 32767
|
||
const int alawBias = 0x84; // 132
|
||
|
||
int mask;
|
||
int seg;
|
||
int aLawVal;
|
||
|
||
// Handle sign
|
||
if (pcmVal < 0) {
|
||
pcmVal = -pcmVal;
|
||
mask = 0x7F; // 127 (sign bit is 1)
|
||
} else {
|
||
mask = 0xFF; // 255 (sign bit is 0)
|
||
}
|
||
|
||
// Add bias and clamp to ALAW_MAX
|
||
pcmVal += alawBias;
|
||
if (pcmVal > alawMax) {
|
||
pcmVal = alawMax;
|
||
}
|
||
|
||
// Determine segment
|
||
seg = search(pcmVal);
|
||
|
||
// Calculate A-law value
|
||
if (seg >= 8) {
|
||
aLawVal = 0x7F ^ mask; // Clamp to maximum value
|
||
} else {
|
||
final int quantized = (pcmVal >> (seg + 3)) & 0xF;
|
||
aLawVal = (seg << 4) | quantized;
|
||
aLawVal ^= 0xD5; // XOR with 0xD5 to match standard A-law table
|
||
}
|
||
|
||
return aLawVal;
|
||
}
|
||
|
||
int search(int val) {
|
||
final List<int> table = <int>[
|
||
0xFF, // Segment 0
|
||
0x1FF, // Segment 1
|
||
0x3FF, // Segment 2
|
||
0x7FF, // Segment 3
|
||
0xFFF, // Segment 4
|
||
0x1FFF, // Segment 5
|
||
0x3FFF, // Segment 6
|
||
0x7FFF // Segment 7
|
||
];
|
||
const int size = 8;
|
||
for (int i = 0; i < size; i++) {
|
||
if (val <= table[i]) {
|
||
return i;
|
||
}
|
||
}
|
||
return size;
|
||
}
|
||
}
|