fix:调整无音频问题

This commit is contained in:
liyi 2025-04-27 09:53:05 +08:00
parent 3e2009069a
commit 25fb142765
5 changed files with 127 additions and 57 deletions

View File

@ -78,6 +78,7 @@ class UdpTalkAcceptHandler extends ScpMessageBaseHandle
}
}
///
void _handleSendExpect() {
final LockListInfoItemEntity currentKeyInfo =
CommonDataManage().currentKeyInfo;

View File

@ -1,20 +1,15 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import 'package:star_lock/appRouters.dart';
import 'package:star_lock/app_settings/app_settings.dart';
import 'package:star_lock/main/lockMian/entity/lockListInfo_entity.dart';
import 'package:star_lock/talk/starChart/constant/message_type_constant.dart';
import 'package:star_lock/talk/starChart/constant/talk_constant.dart';
import 'package:star_lock/talk/starChart/constant/talk_status.dart';
import 'package:star_lock/talk/starChart/entity/scp_message.dart';
import 'package:star_lock/talk/starChart/handle/scp_message_base_handle.dart';
import 'package:star_lock/talk/starChart/handle/scp_message_handle.dart';
import 'package:star_lock/talk/starChart/proto/gateway_reset.pb.dart';
import 'package:star_lock/talk/starChart/proto/generic.pb.dart';
import 'package:star_lock/talk/starChart/proto/talk_expect.pb.dart';
import 'package:star_lock/talk/starChart/proto/talk_request.pb.dart';
@ -28,26 +23,10 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle
RxString currentLanguage =
CurrentLocaleTool.getCurrentLocaleString().obs; //
//
int _lastRequestTime = 0;
@override
void handleReq(ScpMessage scpMessage) async {
final currentTime = DateTime.now().millisecondsSinceEpoch;
// 1
if (currentTime - _lastRequestTime < 1000) {
// 1
replyErrorMessage(scpMessage);
AppLog.log('对讲请求过于频繁,已拒绝');
return;
}
//
_lastRequestTime = currentTime;
//
final loginData = await Storage.getLoginData();
//
if (loginData != null &&
(talkStatus.status != TalkStatus.passiveCallWaitingAnswer ||
@ -77,6 +56,8 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle
//
startChartManage.FromPeerId = scpMessage.ToPeerId!;
startChartManage.ToPeerId = scpMessage.FromPeerId!;
//
_handleResponseSendExpect();
//
startChartManage.startTalkExpectTimer();
//
@ -99,7 +80,7 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle
//
void _talkRequestEvent({required String talkObjectName}) {
//
_handleSendExpect();
_handleRequestSendExpect();
//
//test:使
playRingtone();
@ -188,7 +169,8 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle
}
}
void _handleSendExpect() {
/// app收到的对讲请求后
void _handleRequestSendExpect() {
final LockListInfoItemEntity currentKeyInfo =
CommonDataManage().currentKeyInfo;
final isH264 = currentKeyInfo.lockFeature?.isH264 == 1;
@ -209,4 +191,27 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle
print('锁不支持H264和MJPEG默认发送图像视频格式期望数据');
}
}
/// app主动发请求
void _handleResponseSendExpect() {
final LockListInfoItemEntity currentKeyInfo =
CommonDataManage().currentKeyInfo;
final isH264 = currentKeyInfo.lockFeature?.isH264 == 1;
final isMJpeg = currentKeyInfo.lockFeature?.isMJpeg == 1;
// 使H264MJPEG
if (isH264) {
// H264H264视频和G711音频期望
startChartManage.sendH264VideoAndG711AudioTalkExpectData();
print('锁支持H264发送H264视频格式期望数据');
} else if (isMJpeg) {
// MJPEGG711音频期望
startChartManage.sendImageVideoAndG711AudioTalkExpectData();
print('锁不支持H264支持MJPEG发送MJPEG视频格式期望数据');
} else {
// 使
startChartManage.sendImageVideoAndG711AudioTalkExpectData();
print('锁不支持H264和MJPEG默认发送图像视频格式期望数据');
}
}
}

View File

@ -51,7 +51,6 @@ class TalkViewLogic extends BaseGetXController {
int _startAudioTime = 0; //
bool _isFirstFrame = true; //
//
final List<int> _bufferedAudioFrames = <int>[];
@ -65,6 +64,10 @@ class TalkViewLogic extends BaseGetXController {
int _lastFpsUpdateTime = 0;
Timer? _fpsTimer;
//
bool _isListening = false;
StreamSubscription? _streamSubscription;
///
void _initFlutterPcmSound() {
const int sampleRate = 8000;
@ -97,7 +100,15 @@ class TalkViewLogic extends BaseGetXController {
//
void _startListenTalkData() {
state.talkDataRepository.talkDataStream
//
if (_isListening) {
AppLog.log("已经存在数据流监听,避免重复监听");
return;
}
AppLog.log("==== 启动新的数据流监听 ====");
_isListening = true;
_streamSubscription = state.talkDataRepository.talkDataStream
.listen((TalkDataModel talkDataModel) async {
final talkData = talkDataModel.talkData;
final contentType = talkData!.contentType;
@ -106,13 +117,13 @@ class TalkViewLogic extends BaseGetXController {
//
switch (contentType) {
case TalkData_ContentTypeE.G711:
// //
if (_isFirstAudioFrame) {
_startAudioTime = currentTime;
_isFirstAudioFrame = false;
}
// //
if (_isFirstAudioFrame) {
_startAudioTime = currentTime;
_isFirstAudioFrame = false;
}
//
//
final expectedTime = _startAudioTime + talkData.durationMs;
final audioDelay = currentTime - expectedTime;
@ -384,7 +395,6 @@ class TalkViewLogic extends BaseGetXController {
}
}
///
Future<bool> getPermissionStatus() async {
final Permission permission = Permission.microphone;
@ -504,6 +514,9 @@ class TalkViewLogic extends BaseGetXController {
state.oneMinuteTimeTimer?.cancel(); //
state.oneMinuteTimeTimer = null; //
state.oneMinuteTime.value = 0;
//
_streamSubscription?.cancel();
_isListening = false;
super.onClose();
}

View File

@ -54,6 +54,10 @@ class H264WebViewLogic extends BaseGetXController {
final Queue<List<int>> _frameBuffer = Queue<List<int>>();
static const int FRAME_BUFFER_SIZE = 25;
//
bool _isListening = false;
StreamSubscription? _streamSubscription;
@override
void onInit() {
// WebView
@ -122,7 +126,15 @@ class H264WebViewLogic extends BaseGetXController {
}
void _createFramesStreamListen() async {
state.talkDataRepository.talkDataStream
//
if (_isListening) {
AppLog.log("已经存在数据流监听,避免重复监听");
return;
}
AppLog.log("==== 启动新的数据流监听 ====");
_isListening = true;
_streamSubscription = state.talkDataRepository.talkDataStream
.listen((TalkDataModel talkDataModel) async {
final talkData = talkDataModel.talkData;
final contentType = talkData!.contentType;
@ -131,30 +143,33 @@ class H264WebViewLogic extends BaseGetXController {
//
switch (contentType) {
case TalkData_ContentTypeE.G711:
// //
if (_isFirstAudioFrame) {
_startAudioTime = currentTime;
_isFirstAudioFrame = false;
}
//
final expectedTime = _startAudioTime + talkData.durationMs;
final audioDelay = currentTime - expectedTime;
//
if (audioDelay > 500) {
state.audioBuffer.clear();
if (state.isOpenVoice.value) {
_playAudioFrames();
if (state.isShowLoading.isFalse) {
// //
if (_isFirstAudioFrame) {
_startAudioTime = currentTime;
_isFirstAudioFrame = false;
}
return;
//
final expectedTime = _startAudioTime + talkData.durationMs;
final audioDelay = currentTime - expectedTime;
//
if (audioDelay > 500) {
state.audioBuffer.clear();
if (state.isOpenVoice.value) {
_playAudioFrames();
}
return;
}
if (state.audioBuffer.length >= audioBufferSize) {
state.audioBuffer.removeAt(0); //
}
state.audioBuffer.add(talkData); //
//
_playAudioFrames();
}
if (state.audioBuffer.length >= audioBufferSize) {
state.audioBuffer.removeAt(0); //
}
state.audioBuffer.add(talkData); //
//
_playAudioFrames();
break;
case TalkData_ContentTypeE.H264:
// //
@ -537,6 +552,39 @@ class H264WebViewLogic extends BaseGetXController {
}
}
///
void _stopPlayG711Data() async {
await FlutterPcmSound.pause();
await FlutterPcmSound.stop();
await FlutterPcmSound.clear();
}
@override
void onClose() {
_stopPlayG711Data(); //
state.audioBuffer.clear(); //
state.oneMinuteTimeTimer?.cancel();
state.oneMinuteTimeTimer = null;
//
stopProcessingAudio();
state.oneMinuteTimeTimer?.cancel(); //
state.oneMinuteTimeTimer = null; //
state.oneMinuteTime.value = 0;
//
_streamSubscription?.cancel();
_isListening = false;
//
StartChartManage().reSetDefaultTalkExpect();
super.onClose();
}
@override
void dispose() {
// _mockDataTimer?.cancel();

View File

@ -7,6 +7,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:star_lock/app_settings/app_colors.dart';
import 'package:star_lock/app_settings/app_settings.dart';
import 'package:star_lock/talk/call/callTalk.dart';
import 'package:star_lock/talk/starChart/constant/talk_status.dart';
import 'package:star_lock/talk/starChart/handle/other/talk_data_repository.dart';
import 'package:star_lock/talk/starChart/proto/talk_data.pbserver.dart';
@ -413,8 +414,10 @@ class _H264WebViewState extends State<H264WebView>
}
@override
void dispose() {
state.animationController.dispose(); //
super.dispose();
state.animationController.dispose();
CallTalk().finishAVData();
// UdpTalkDataHandler().resetDataRates();
super.dispose();
}
}