feat:优化帧缓冲区逻辑

This commit is contained in:
liyi 2025-03-13 15:07:13 +08:00
parent 0fa0e1f340
commit face4dfd45

View File

@ -47,7 +47,9 @@ class TalkViewLogic extends BaseGetXController {
// //
int _startTime = 0; // int _startTime = 0; //
int _startAudioTime = 0; //
bool _isFirstFrame = true; // bool _isFirstFrame = true; //
bool _isFirstAudioFrame = true; //
// //
final List<int> _bufferedAudioFrames = <int>[]; final List<int> _bufferedAudioFrames = <int>[];
@ -90,16 +92,28 @@ class TalkViewLogic extends BaseGetXController {
final contentType = talkData.contentType; final contentType = talkData.contentType;
final currentTime = DateTime.now().millisecondsSinceEpoch; final currentTime = DateTime.now().millisecondsSinceEpoch;
//
if (_isFirstFrame) {
_startTime = currentTime;
_isFirstFrame = false;
}
// //
switch (contentType) { switch (contentType) {
case TalkData_ContentTypeE.G711: case TalkData_ContentTypeE.G711:
if (state.audioBuffer.length >= bufferSize) { //
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();
}
return;
}
if (state.audioBuffer.length >= audioBufferSize) {
state.audioBuffer.removeAt(0); // state.audioBuffer.removeAt(0); //
} }
state.audioBuffer.add(talkData); // state.audioBuffer.add(talkData); //
@ -107,9 +121,17 @@ class TalkViewLogic extends BaseGetXController {
_playAudioFrames(); _playAudioFrames();
break; break;
case TalkData_ContentTypeE.Image: case TalkData_ContentTypeE.Image:
// ( - ) - //
if (_isFirstFrame) {
_startTime = currentTime;
_isFirstFrame = false;
AppLog.log('记录第一帧的时间戳${currentTime},${talkData.durationMs}');
}
// -
final expectedTime = _startTime + talkData.durationMs; final expectedTime = _startTime + talkData.durationMs;
final videoDelay = currentTime - expectedTime; final videoDelay = currentTime - expectedTime; //
// //
_adjustBufferSize(videoDelay); _adjustBufferSize(videoDelay);
// //