fix:更新组包缓冲区的key

This commit is contained in:
liyi 2024-12-24 15:38:36 +08:00
parent 1108d4c665
commit a029c4b4e0
11 changed files with 51 additions and 47 deletions

View File

@ -25,12 +25,6 @@ class StarChartLogic extends BaseGetXController {
@override
void onReady() {
super.onReady();
// 44100 Hz
FlutterPcmSound.setup(sampleRate: 44100, channelCount: 1, );
// 使
int feedThreshold = 44100 * 1; // 44100
FlutterPcmSound.setFeedThreshold(feedThreshold);
_getTalkStatusRefreshUIAction();
_startListenTalkData();
}
@ -75,8 +69,16 @@ class StarChartLogic extends BaseGetXController {
}
///
Future<void> _playG711Data(List<int> audioData) async {
final PcmArrayInt16 fromList = PcmArrayInt16.fromList(audioData);
Future<void> _playG711Data(List<int> pcmData) async {
// PCM PcmArrayInt16
final PcmArrayInt16 fromList = PcmArrayInt16.fromList(pcmData);
// PCM
await FlutterPcmSound.setup(sampleRate: 8000, channelCount: 1);
//
FlutterPcmSound.setFeedThreshold(8000 ~/ 2); //
// final PcmArrayInt16 fromList = PcmArrayInt16.fromList(audioData);
await FlutterPcmSound.feed(fromList);
FlutterPcmSound.play();

View File

@ -316,7 +316,7 @@ class _StarChartPageState extends State<StarChartPage> {
void dispose() {
super.dispose();
logic.stopProcessing();
state.getTVDataRefreshUIEvent!.cancel();
// state.getTVDataRefreshUIEvent!.cancel();
}
Widget _buildTalkView({required bool isMpeg4}) {

View File

@ -14,7 +14,7 @@ class StarChartState {
RxBool isOpenVoice = false.obs;
int udpSendDataFrameNumber = 0; //
// var isSenderAudioData = false.obs;//
StreamSubscription? getTVDataRefreshUIEvent; //
// StreamSubscription? getTVDataRefreshUIEvent; //
RxBool shouldUpdateUI = false.obs; //UI
Future<String?> userMobileIP = NetworkInfo().getWifiIP();

View File

@ -4,6 +4,7 @@ import 'dart:typed_data';
import 'package:star_lock/talk/startChart/constant/message_type_constant.dart';
import 'package:star_lock/talk/startChart/constant/payload_type_constant.dart';
import 'package:star_lock/talk/startChart/constant/protocol_flag_constant.dart';
import 'package:star_lock/talk/startChart/constant/udp_constant.dart';
import 'package:star_lock/talk/startChart/entity/scp_message.dart';
import 'package:star_lock/talk/startChart/proto/gateway_reset.pb.dart';
import 'package:star_lock/talk/startChart/proto/generic.pb.dart';
@ -17,6 +18,7 @@ import 'package:star_lock/talk/startChart/proto/talk_request.pb.dart';
class MessageCommand {
// ToPeerId messageId
// messageId需要递增
static Map<String, int> _messageIdMap = {};
static int _maxIntValue = 9223372036854775807; // Dart int
@ -322,8 +324,8 @@ class MessageCommand {
int? MessageId,
}) {
final genericResp = GenericResp();
genericResp.message = 'ok';
genericResp.code = 0;
genericResp.message = UdpConstant.genericRespSuccessMsg;
genericResp.code = UdpConstant.genericRespSuccessCode;
final payload = genericResp.writeToBuffer();
ScpMessage message = ScpMessage(
ProtocolFlag: ProtocolFlagConstant.scp01,
@ -348,10 +350,11 @@ class MessageCommand {
required String ToPeerId,
required int PayloadType,
int? MessageId,
String? errorMessageText,
}) {
final genericResp = GenericResp();
genericResp.message = 'error';
genericResp.code = -1;
genericResp.message = errorMessageText ?? UdpConstant.genericRespErrorMsg;
genericResp.code = UdpConstant.genericRespErrorCode;
final payload = genericResp.writeToBuffer();
ScpMessage message = ScpMessage(
ProtocolFlag: ProtocolFlagConstant.scp01,

View File

@ -2,4 +2,7 @@ class UdpConstant{
// generic成功响应
static const int genericRespSuccessCode = 0;
static const String genericRespSuccessMsg = 'ok';
// generic失败响应
static const int genericRespErrorCode = -1;
static const String genericRespErrorMsg = 'error';
}

View File

@ -31,12 +31,12 @@ class UdpTalkAcceptHandler extends ScpMessageBaseHandle
//
final GenericResp genericResp = scpMessage.Payload;
if (checkGenericRespSuccess(genericResp)) {
//
//
_handleStartSendTalkExpectDataRequest();
Future.delayed(Duration(seconds: 1), () {
print('启动定时器判断');
//
_handleStartTalkPing();
//
_handleStartSendTalkExpectDataRequest();
//
_handleCheckTalkDataTimer();
});
@ -78,7 +78,7 @@ class UdpTalkAcceptHandler extends ScpMessageBaseHandle
}
}
//
// x秒内是否收到通话保持
void _handleStartTalkPing() {
//
startChartManage.startTalkPingMessageTimer();

View File

@ -85,11 +85,7 @@ class UdpTalkDataHandler extends ScpMessageBaseHandle
int? spTotal,
int? spIndex,
int? messageId}) {
if (messageType == MessageTypeConstant.Resp) {
final GenericResp genericResp = GenericResp();
genericResp.mergeFromBuffer(byte);
return genericResp;
} else if (messageType == MessageTypeConstant.RealTimeData) {
if (messageType == MessageTypeConstant.RealTimeData) {
//
if (spTotal != null &&
spTotal > 1 &&
@ -109,9 +105,6 @@ class UdpTalkDataHandler extends ScpMessageBaseHandle
talkData.mergeFromBuffer(byte);
return talkData;
}
} else {
String payload = utf8.decode(byte);
return payload;
}
}
}

View File

@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
@ -65,6 +66,8 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle
Routers.starChartPage,
arguments: <String, String>{'lockId': '111'},
);
//
HapticFeedback.lightImpact();
}
//

View File

@ -12,9 +12,6 @@ class OverTimeTimerManager {
// final
final int timeoutInSeconds;
//
static const int defaultTimeoutInSeconds = 5;
//
TimeoutCallback? _onTimeout;
@ -24,7 +21,7 @@ class OverTimeTimerManager {
_timer = Timer(Duration(seconds: timeoutInSeconds), () {
//
_onTimeout?.call();
//
//
_cancelTimer();
});
}

View File

@ -22,8 +22,8 @@ class ScpMessageBaseHandle {
///
// messageId
static Map<int, List<List<int>>> _packetBuffer = {};
final Map<int, Timer> _packetTimers = {};
static Map<String, List<List<int>>> _packetBuffer = {};
final Map<String, Timer> _packetTimers = {};
final Duration _timeoutDuration = Duration(seconds: 10); //
//
@ -85,28 +85,30 @@ class ScpMessageBaseHandle {
required int payloadType,
}) {
//
String key = '$messageId-$payloadType';
if (!_packetBuffer.containsKey(messageId)) {
_packetBuffer[messageId] = List.filled(spTotal, []);
_startTimer(messageId);
_packetBuffer[key] = List.filled(spTotal, []);
_startTimer(key);
}
//
if (spIndex < 1 || spIndex > spTotal) {
print('Invalid spIndex: $spIndex for messageId: $messageId');
print(
'Invalid spTotal: $spTotal spIndex: $spIndex for messageId: $messageId');
return null;
}
//
_packetBuffer[messageId]![spIndex - 1] = byte;
_packetBuffer[key]![spIndex - 1] = byte;
//
if (_packetBuffer[messageId]!.every((packet) => packet.isNotEmpty)) {
if (_packetBuffer[key]!.every((packet) => packet.isNotEmpty)) {
//
List<int> completePayload =
_packetBuffer[messageId]!.expand((packet) => packet).toList();
_packetBuffer[key]!.expand((packet) => packet).toList();
//
_clearPacketData(messageId);
//
_clearPacketData(key);
// payload
if (payloadType == PayloadTypeConstant.talkData) {
@ -126,16 +128,16 @@ class ScpMessageBaseHandle {
}
//
void _startTimer(int messageId) {
_packetTimers[messageId]?.cancel();
_packetTimers[messageId] = Timer(_timeoutDuration, () {
_clearPacketData(messageId);
void _startTimer(String key) {
_packetTimers[key]?.cancel();
_packetTimers[key] = Timer(_timeoutDuration, () {
_clearPacketData(key);
});
}
//
void _clearPacketData(int messageId) {
_packetBuffer.remove(messageId);
_packetTimers.remove(messageId)?.cancel();
void _clearPacketData(String key) {
_packetBuffer.remove(key);
_packetTimers.remove(key)?.cancel();
}
}

View File

@ -296,6 +296,7 @@ class StartChartManage {
//
List<int> packet = payload.sublist(start, end);
// messageID
final messageId =
MessageCommand.getNextMessageId(toPeerId, increment: false);
//