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

View File

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

View File

@ -14,7 +14,7 @@ class StarChartState {
RxBool isOpenVoice = false.obs; RxBool isOpenVoice = false.obs;
int udpSendDataFrameNumber = 0; // int udpSendDataFrameNumber = 0; //
// var isSenderAudioData = false.obs;// // var isSenderAudioData = false.obs;//
StreamSubscription? getTVDataRefreshUIEvent; // // StreamSubscription? getTVDataRefreshUIEvent; //
RxBool shouldUpdateUI = false.obs; //UI RxBool shouldUpdateUI = false.obs; //UI
Future<String?> userMobileIP = NetworkInfo().getWifiIP(); 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/message_type_constant.dart';
import 'package:star_lock/talk/startChart/constant/payload_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/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/entity/scp_message.dart';
import 'package:star_lock/talk/startChart/proto/gateway_reset.pb.dart'; import 'package:star_lock/talk/startChart/proto/gateway_reset.pb.dart';
import 'package:star_lock/talk/startChart/proto/generic.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 { class MessageCommand {
// ToPeerId messageId // ToPeerId messageId
// messageId需要递增
static Map<String, int> _messageIdMap = {}; static Map<String, int> _messageIdMap = {};
static int _maxIntValue = 9223372036854775807; // Dart int static int _maxIntValue = 9223372036854775807; // Dart int
@ -322,8 +324,8 @@ class MessageCommand {
int? MessageId, int? MessageId,
}) { }) {
final genericResp = GenericResp(); final genericResp = GenericResp();
genericResp.message = 'ok'; genericResp.message = UdpConstant.genericRespSuccessMsg;
genericResp.code = 0; genericResp.code = UdpConstant.genericRespSuccessCode;
final payload = genericResp.writeToBuffer(); final payload = genericResp.writeToBuffer();
ScpMessage message = ScpMessage( ScpMessage message = ScpMessage(
ProtocolFlag: ProtocolFlagConstant.scp01, ProtocolFlag: ProtocolFlagConstant.scp01,
@ -348,10 +350,11 @@ class MessageCommand {
required String ToPeerId, required String ToPeerId,
required int PayloadType, required int PayloadType,
int? MessageId, int? MessageId,
String? errorMessageText,
}) { }) {
final genericResp = GenericResp(); final genericResp = GenericResp();
genericResp.message = 'error'; genericResp.message = errorMessageText ?? UdpConstant.genericRespErrorMsg;
genericResp.code = -1; genericResp.code = UdpConstant.genericRespErrorCode;
final payload = genericResp.writeToBuffer(); final payload = genericResp.writeToBuffer();
ScpMessage message = ScpMessage( ScpMessage message = ScpMessage(
ProtocolFlag: ProtocolFlagConstant.scp01, ProtocolFlag: ProtocolFlagConstant.scp01,

View File

@ -2,4 +2,7 @@ class UdpConstant{
// generic成功响应 // generic成功响应
static const int genericRespSuccessCode = 0; static const int genericRespSuccessCode = 0;
static const String genericRespSuccessMsg = 'ok'; 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; final GenericResp genericResp = scpMessage.Payload;
if (checkGenericRespSuccess(genericResp)) { if (checkGenericRespSuccess(genericResp)) {
// //
_handleStartSendTalkExpectDataRequest();
Future.delayed(Duration(seconds: 1), () { Future.delayed(Duration(seconds: 1), () {
print('启动定时器判断');
// //
_handleStartTalkPing(); _handleStartTalkPing();
//
_handleStartSendTalkExpectDataRequest();
// //
_handleCheckTalkDataTimer(); _handleCheckTalkDataTimer();
}); });
@ -78,7 +78,7 @@ class UdpTalkAcceptHandler extends ScpMessageBaseHandle
} }
} }
// // x秒内是否收到通话保持
void _handleStartTalkPing() { void _handleStartTalkPing() {
// //
startChartManage.startTalkPingMessageTimer(); startChartManage.startTalkPingMessageTimer();

View File

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

View File

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

View File

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

View File

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

View File

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