fix:增加talkData的组包逻辑

This commit is contained in:
liyi 2024-12-23 15:21:48 +08:00
parent 75985e0600
commit 6147b86e8b
5 changed files with 82 additions and 51 deletions

View File

@ -102,23 +102,23 @@ class StarChartLogic extends BaseGetXController {
}
Future<void> startProcessing() async {
state.isButtonDisabled.value = true;
state.voiceProcessor?.addFrameListener(_onFrame);
state.voiceProcessor?.addErrorListener(_onError);
try {
if (await state.voiceProcessor?.hasRecordAudioPermission() ?? false) {
await state.voiceProcessor?.start(state.frameLength, state.sampleRate);
state.isProcessing.value =
await state.voiceProcessor?.isRecording() ?? false;
} else {
state.errorMessage.value = 'Recording permission not granted';
}
} on PlatformException catch (ex) {
state.errorMessage.value = 'Failed to start recorder: $ex';
} finally {
state.isButtonDisabled.value = false;
}
// state.isButtonDisabled.value = true;
//
// state.voiceProcessor?.addFrameListener(_onFrame);
// state.voiceProcessor?.addErrorListener(_onError);
// try {
// if (await state.voiceProcessor?.hasRecordAudioPermission() ?? false) {
// await state.voiceProcessor?.start(state.frameLength, state.sampleRate);
// state.isProcessing.value =
// await state.voiceProcessor?.isRecording() ?? false;
// } else {
// state.errorMessage.value = 'Recording permission not granted';
// }
// } on PlatformException catch (ex) {
// state.errorMessage.value = 'Failed to start recorder: $ex';
// } finally {
// state.isButtonDisabled.value = false;
// }
}
void _onError(VoiceProcessorException error) {
@ -127,23 +127,23 @@ class StarChartLogic extends BaseGetXController {
Future<void> stopProcessing() async {
// voiceProcessor
if (state.voiceProcessor == null) {
state.errorMessage.value = 'Voice processor is not initialized.';
return;
}
state.isButtonDisabled.value = true;
try {
await state.voiceProcessor?.stop();
state.voiceProcessor?.removeFrameListener(_onFrame);
state.udpSendDataFrameNumber = 0;
} on PlatformException catch (ex) {
state.errorMessage.value = 'Failed to stop recorder: $ex';
} finally {
state.isProcessing.value =
await state.voiceProcessor?.isRecording() ?? false;
state.isButtonDisabled.value = false;
}
// if (state.voiceProcessor == null) {
// state.errorMessage.value = 'Voice processor is not initialized.';
// return;
// }
//
// state.isButtonDisabled.value = true;
// try {
// await state.voiceProcessor?.stop();
// state.voiceProcessor?.removeFrameListener(_onFrame);
// state.udpSendDataFrameNumber = 0;
// } on PlatformException catch (ex) {
// state.errorMessage.value = 'Failed to stop recorder: $ex';
// } finally {
// state.isProcessing.value =
// await state.voiceProcessor?.isRecording() ?? false;
// state.isButtonDisabled.value = false;
// }
}
int linearToULaw(int pcmVal) {

View File

@ -170,9 +170,12 @@ class ScpMessage {
static ScpMessage deserialize(List<int> bytes) {
final message = ScpMessage();
int offset = 0;
// _log(
// text:
// '原始字节数组: ${bytes.sublist(0, 20).map((b) => b.toRadixString(16)).join(" ")}');
// Convert byte array to hex string with zero padding and without spaces
// String hexString =
// bytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join();
//
// // Log the hex string
// _log(text: '原始字节数组: $hexString');
// ProtocolFlag (4 bytes)
if (bytes.length - offset >= 4) {
message.ProtocolFlag = utf8.decode(bytes.sublist(offset, offset + 4));
@ -257,6 +260,7 @@ class ScpMessage {
} else {
throw FormatException("Invalid PayloadLength length");
}
// print("message result: $message");
// Payload
if (message.PayloadLength != null &&
@ -317,6 +321,7 @@ class ScpMessage {
spTotal: spTotal,
spIndex: spIndex,
byte: byte,
payloadType: payloadType,
);
} else {
// spTotal 1 byte
@ -447,9 +452,24 @@ class ScpMessage {
final GenericResp genericResp = GenericResp.fromBuffer(byte);
return genericResp;
} else if (messageType == MessageTypeConstant.RealTimeData) {
//
final TalkData talkData = TalkData.fromBuffer(byte);
return talkData;
//
if (spTotal != null &&
spTotal > 1 &&
messageId != null &&
spIndex != null) {
//
return _handleFragmentedPayload(
messageId: messageId,
spTotal: spTotal,
spIndex: spIndex,
byte: byte,
payloadType: payloadType,
);
} else {
//
final TalkData talkData = TalkData.fromBuffer(byte);
return talkData;
}
} else {
String payload = utf8.decode(byte);
return payload;
@ -470,10 +490,11 @@ class ScpMessage {
String payload = utf8.decode(byte);
return payload;
}
} catch (e) {
e.printError();
_log(text: '❌反序列化udp数据时遇到错误----》$e,message:$messageType byte:${byte.take(20)}');
//
} catch (e, stackTrace) {
//
_log(text: '❌反序列化udp数据时遇到错误----》$e');
//
_log(text: '堆栈跟踪:\n$stackTrace');
return '';
}
}
@ -503,9 +524,10 @@ class ScpMessage {
required int spTotal,
required int spIndex,
required List<int> byte,
required int payloadType,
}) {
print(
'_handleFragmentedPayload spTotal:$spTotal spindex:$spIndex} byte:${byte.take(20)}');
// print(
// '_handleFragmentedPayload spTotal:$spTotal spindex:$spIndex} byte:${byte.take(20)}');
//
if (!_packetBuffer.containsKey(messageId)) {
_packetBuffer[messageId] = List.filled(spTotal, []);
@ -523,6 +545,12 @@ class ScpMessage {
//
_packetBuffer.remove(messageId);
if (payloadType == PayloadTypeConstant.talkData) {
// payload
final TalkData talkData = TalkData.fromBuffer(completePayload);
return talkData;
}
return completePayload;
// payload

View File

@ -27,7 +27,7 @@ class UdpTalkAcceptHandler extends ScpMessageBaseHandle
final GenericResp genericResp = scpMessage.Payload;
if (checkGenericRespSuccess(genericResp)) {
// 2
Future.delayed(Duration(seconds: 3), () {
Future.delayed(Duration(seconds: 4), () {
//
_handleStartTalkPing();
//

View File

@ -27,6 +27,7 @@ class UdpTalkDataHandler extends ScpMessageBaseHandle
final TalkData talkData = scpMessage.Payload;
//
_handleTalkData(talkData: talkData);
print('talkData$talkData');
//
talkDataOverTimeTimerManager.receiveMessage();
}

View File

@ -50,12 +50,16 @@ class StartChartManage {
final String _productName = F.navTitle;
RawDatagramSocket? _udpSocket;
final Map<String, Completer<void>> _completers = {}; //
final Uuid _uuid = Uuid(); // id
int _messageMaxTimeout = 5; // s
late String remoteHost = ''; // ()
late int remotePort = 0; // ()
final int localPort = 62289; //
String localPublicHost = ''; // ip地址
int heartbeatIntervalTime = 1; // s
Timer? _heartBeatTimer; //
bool _heartBeatTimerRunning = false; //
@ -308,9 +312,6 @@ class StartChartManage {
);
//
await _sendMessage(message: message);
// _log(
// text:
// '发送回声测试分包消息=====messageId:$messageId,SpTotal:$totalPackets,SpIndex:${i + 1},packet:${packet.length}');
}
// id
MessageCommand.getNextMessageId(toPeerId);
@ -765,8 +766,9 @@ class StartChartManage {
// _log(text: 'Udp收到结构体数据---》$deserialize');
}
}
} catch (e) {
} catch (e, stackTrace) {
_log(text: '❌ Udp result data error ----> $e');
_log(text: '堆栈跟踪:\n$stackTrace');
}
}
});