fix:调整视频帧缓冲区逻辑计算逻辑,增加缓冲区最大大小至20帧

This commit is contained in:
liyi 2025-04-08 16:43:11 +08:00
parent 5864b29055
commit 47b0a18ef3

View File

@ -56,6 +56,9 @@ class TalkViewLogic extends BaseGetXController {
final Map<String, ui.Image> _imageCache = {}; final Map<String, ui.Image> _imageCache = {};
//
int _lastFrameTimestamp = 0; // 0
// //
int _frameCount = 0; int _frameCount = 0;
int _lastFpsUpdateTime = 0; int _lastFpsUpdateTime = 0;
@ -100,11 +103,11 @@ class TalkViewLogic extends BaseGetXController {
// //
switch (contentType) { switch (contentType) {
case TalkData_ContentTypeE.G711: case TalkData_ContentTypeE.G711:
// // //
if (_isFirstAudioFrame) { // if (_isFirstAudioFrame) {
_startAudioTime = currentTime; // _startAudioTime = currentTime;
_isFirstAudioFrame = false; // _isFirstAudioFrame = false;
} // }
// //
final expectedTime = _startAudioTime + talkData.durationMs; final expectedTime = _startAudioTime + talkData.durationMs;
@ -130,15 +133,16 @@ class TalkViewLogic extends BaseGetXController {
if (_isFirstFrame) { if (_isFirstFrame) {
_startTime = currentTime; _startTime = currentTime;
_isFirstFrame = false; _isFirstFrame = false;
// AppLog.log('记录第一帧的时间戳${currentTime},${talkData.durationMs}'); AppLog.log('第一帧帧的时间戳:${talkData.durationMs}');
} }
// AppLog.log('其他帧的时间戳:${talkData.durationMs}');
//
if (_lastFrameTimestamp != 0) {
final int frameInterval = talkData.durationMs - _lastFrameTimestamp;
_adjustBufferSize(frameInterval); //
}
_lastFrameTimestamp = talkData.durationMs; //
// -
final expectedTime = _startTime + talkData.durationMs;
final videoDelay = currentTime - expectedTime; //
//
_adjustBufferSize(videoDelay);
// //
if (state.videoBuffer.length >= bufferSize) { if (state.videoBuffer.length >= bufferSize) {
state.videoBuffer.removeAt(0); state.videoBuffer.removeAt(0);
@ -257,19 +261,21 @@ class TalkViewLogic extends BaseGetXController {
} }
// //
void _adjustBufferSize(int delay) { void _adjustBufferSize(int frameInterval) {
const int delayThresholdHigh = 250; // 3 const int frameDuration = 83; // 83ms12fps
const int delayThresholdLow = 166; // 2 const int delayThresholdHigh = frameDuration * 2; // 2
const int delayThresholdLow = frameDuration; // 1
const int adjustInterval = 1; // 1 const int adjustInterval = 1; // 1
if (delay > delayThresholdHigh && bufferSize < maxBufferSize) { if (frameInterval > delayThresholdHigh && bufferSize < maxBufferSize) {
// //
bufferSize = min(bufferSize + adjustInterval, maxBufferSize); bufferSize = min(bufferSize + adjustInterval, maxBufferSize);
// AppLog.log('📈 增加缓冲区 - 当前大小: $bufferSize, 延迟: ${delay}ms'); AppLog.log('📈 增加缓冲区 - 当前大小: $bufferSize, 帧间间隔: ${frameInterval}ms');
} else if (delay < delayThresholdLow && bufferSize > minBufferSize) { } else if (frameInterval < delayThresholdLow &&
// bufferSize > minBufferSize) {
//
bufferSize = max(bufferSize - adjustInterval, minBufferSize); bufferSize = max(bufferSize - adjustInterval, minBufferSize);
// AppLog.log('📉 减少缓冲区 - 当前大小: $bufferSize, 延迟: ${delay}ms'); AppLog.log('📉 减少缓冲区 - 当前大小: $bufferSize, 帧间间隔: ${frameInterval}ms');
} }
} }