优化ios视频bug

This commit is contained in:
sky_min 2025-11-10 17:53:20 +08:00
parent 6b6a754cf9
commit a6d53858f6

View File

@ -61,21 +61,35 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
final actualRatio = _actualFps / targetFps;
//
if (actualRatio < 0.4) {
//
state.targetFps = (targetFps * 0.6).round().clamp(15, 60);
_startFrameProcessTimer();
} else if (actualRatio < 0.6) {
//
state.targetFps = (targetFps * 0.8).round().clamp(15, 60);
_startFrameProcessTimer();
} else if (actualRatio > 1.8 && targetFps < 25) {
//
state.targetFps = (targetFps * 1.15).round().clamp(15, 30);
_startFrameProcessTimer();
// iOS平台使用更保守的调整策略
if (Platform.isIOS) {
if (actualRatio < 0.25) {
// iOS处理能力严重不足
state.targetFps = (targetFps * 0.6).round().clamp(15, 60);
_startFrameProcessTimer();
} else if (actualRatio < 0.45) {
// iOS处理能力不足
state.targetFps = (targetFps * 0.8).round().clamp(15, 60);
_startFrameProcessTimer();
} else if (actualRatio > 2.5 && targetFps < 25) {
// iOS处理能力充足
state.targetFps = (targetFps * 1.05).round().clamp(15, 30);
_startFrameProcessTimer();
}
} else {
// Android平台原有逻辑
if (actualRatio < 0.4) {
state.targetFps = (targetFps * 0.6).round().clamp(15, 60);
_startFrameProcessTimer();
} else if (actualRatio < 0.6) {
state.targetFps = (targetFps * 0.8).round().clamp(15, 60);
_startFrameProcessTimer();
} else if (actualRatio > 1.8 && targetFps < 25) {
state.targetFps = (targetFps * 1.15).round().clamp(15, 30);
_startFrameProcessTimer();
}
}
//
}
}//
}
// frameSeq较小时阈值也小
@ -139,7 +153,9 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
);
// 使
final timeoutFuture = Future.delayed(const Duration(seconds: 3), () => null);
// iOS平台使用更短的超时时间
final timeoutSeconds = Platform.isIOS ? 2 : 3;
final timeoutFuture = Future.delayed(Duration(seconds: timeoutSeconds), () => null);
final decoderFuture = VideoDecodePlugin.initDecoder(config);
// textureId
@ -163,9 +179,11 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
AppLog.log('初始化视频解码器错误: $e');
state.isLoading.value = false;
//
await Future.delayed(const Duration(seconds: 1));
// iOS平台使用更短的重试延迟
final delaySeconds = Platform.isIOS ? 0.5 : 1;
await Future.delayed(Duration(milliseconds: (delaySeconds * 1000).toInt()));
if (!Get.isRegistered<TalkViewNativeDecodeLogic>()) {
return; //
return;
}
_initVideoDecoder(); //
}
@ -292,7 +310,10 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
_stopFrameProcessTimer();
//
final int intervalMs = max(16, min(40, (1000 / state.targetFps).round()));
// iOS平台使用更精确的定时器间隔
final int intervalMs = Platform.isIOS
? max(16, min(33, (1000 / state.targetFps).round())) // iOS使用更严格的范围
: max(16, min(40, (1000 / state.targetFps).round()));
//
state.frameProcessTimer = Timer.periodic(Duration(milliseconds: intervalMs), (timer) {
@ -368,12 +389,22 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
final bufferLength = state.h264FrameBuffer.length;
//
if (bufferLength > 50) {
//
_temporarilyIncreaseProcessFrequency();
} else if (bufferLength < 5) {
//
_adjustFrameProcessFrequency((state.targetFps * 0.9).toDouble());
// iOS平台优化
if (Platform.isIOS) {
if (bufferLength > 40) {
// iOS缓冲区过长
_temporarilyIncreaseProcessFrequency();
} else if (bufferLength < 3) {
// iOS缓冲区过短
_adjustFrameProcessFrequency((state.targetFps * 0.95).toDouble());
}
} else {
// Android平台原有逻辑
if (bufferLength > 50) {
_temporarilyIncreaseProcessFrequency();
} else if (bufferLength < 5) {
_adjustFrameProcessFrequency((state.targetFps * 0.9).toDouble());
}
}
//
@ -438,6 +469,8 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
//
try {
// iOS平台使用更短的超时时间
final timeoutMs = Platform.isIOS ? 15 : 20;
await VideoDecodePlugin.sendFrame(
frameData: frameData,
frameType: pluginFrameType,
@ -445,7 +478,7 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
timestamp: pts,
splitNalFromIFrame: true,
refIFrameSeq: frameSeqI,
).timeout(const Duration(milliseconds: 20)); //
).timeout(Duration(milliseconds: timeoutMs)); //
// I帧序号
if (frameType == TalkDataH264Frame_FrameTypeE.I) {
@ -462,7 +495,9 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
final durationMs = (endTime - startTime) / 1000.0;
// -
if (durationMs > 30) {
// iOS平台使用更严格的性能监控
final thresholdMs = Platform.isIOS ? 25 : 30;
if (durationMs > thresholdMs) {
AppLog.log('帧处理耗时过长: ${durationMs.toStringAsFixed(2)} ms, 缓冲区长度: ${state.h264FrameBuffer.length}');
}
}
@ -482,7 +517,7 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
_adjustFrameProcessFrequency(tempFps.toDouble());
// 3
// 2
_tempAdjustTimer = Timer(const Duration(seconds: 2), () {
_adjustFrameProcessFrequency(originalFps.toDouble());
_tempAdjustTimer = null;
@ -1012,7 +1047,9 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
if (state.textureId.value != null) {
try {
//
await VideoDecodePlugin.releaseDecoder().timeout(const Duration(milliseconds: 300));
// iOS平台使用更短的超时时间
final timeoutMs = Platform.isIOS ? 200 : 300;
await VideoDecodePlugin.releaseDecoder().timeout(Duration(milliseconds: timeoutMs));
state.textureId.value = null;
} catch (e) {
AppLog.log('释放解码器超时或失败: $e');
@ -1022,7 +1059,9 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
}
//
await Future.delayed(Duration(milliseconds: 2));
// iOS平台使用更短的等待时间
final delayMs = Platform.isIOS ? 0 : 1;
await Future.delayed(Duration(milliseconds: delayMs));
//
final config = VideoDecoderConfig(
@ -1034,8 +1073,10 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
//
try {
// 使
// iOS平台使用更短的超时时间
final timeoutMs = Platform.isIOS ? 1000 : 1500;
final textureId = await VideoDecodePlugin.initDecoder(config)
.timeout(const Duration(milliseconds: 1500));
.timeout(Duration(milliseconds: timeoutMs));
if (textureId != null) {
state.textureId.value = textureId;