优化iphone手机第一次点击监控无法接通问题

This commit is contained in:
sky.min 2026-01-06 11:13:26 +08:00
parent a6843f858b
commit 780ae8ccd2

View File

@ -40,6 +40,9 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
int bufferSize = 25; //
int audioBufferSize = 20; // 2
// videoWidth和videoHeight的变化
Timer? _videoDimensionCheckTimer;
// frameSeq较小时阈值也小
int _getFrameSeqRolloverThreshold(int lastSeq) {
@ -93,6 +96,10 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
Future<void> _initVideoDecoder() async {
try {
state.isLoading.value = true;
// videoWidth和videoHeight有效0
await _waitForValidVideoDimensions();
//
final config = VideoDecoderConfig(
width: StartChartManage().videoWidth,
@ -125,6 +132,86 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
_initVideoDecoder(); //
}
}
//
Future<void> _waitForValidVideoDimensions() async {
int attempts = 0;
const maxAttempts = 20; // 10 (20 * 500ms)
while (attempts < maxAttempts) {
// videoWidth和videoHeight是否有效0
if (StartChartManage().videoWidth != 0 && StartChartManage().videoHeight != 0) {
AppLog.log('获取到有效的视频尺寸: ${StartChartManage().videoWidth}x${StartChartManage().videoHeight}');
return;
}
attempts++;
await Future.delayed(const Duration(milliseconds: 500));
AppLog.log('等待有效的视频尺寸... 第 $attempts 次, 当前尺寸: ${StartChartManage().videoWidth}x${StartChartManage().videoHeight}');
}
// 使
AppLog.log('等待视频尺寸超时,使用默认尺寸: 864x480');
StartChartManage().videoWidth = 864;
StartChartManage().videoHeight = 480;
}
// iOS平台专用的视频解码器初始化方法
Future<int?> _attemptVideoDecoderInitialization(VideoDecoderConfig config, {int maxAttempts = 3}) async {
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
try {
AppLog.log('视频解码器初始化第 $attempt 次尝试, 尺寸: ${config.width}x${config.height}');
final textureId = await VideoDecodePlugin.initDecoder(config);
if (textureId != null) {
AppLog.log('视频解码器初始化第 $attempt 次尝试成功');
return textureId;
}
//
if (attempt < maxAttempts) {
await Future.delayed(Duration(milliseconds: 500 * attempt));
}
} catch (e) {
AppLog.log('视频解码器初始化第 $attempt 次尝试出错: $e');
if (attempt < maxAttempts) {
await Future.delayed(Duration(milliseconds: 500 * attempt));
}
}
}
// 使使
AppLog.log('使用实际尺寸初始化失败,尝试使用默认尺寸重新初始化');
final defaultConfig = VideoDecoderConfig(
width: 864,
height: 480,
codecType: 'h264',
);
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
try {
AppLog.log('使用默认尺寸进行视频解码器初始化第 $attempt 次尝试');
final textureId = await VideoDecodePlugin.initDecoder(defaultConfig);
if (textureId != null) {
AppLog.log('使用默认尺寸视频解码器初始化第 $attempt 次尝试成功');
// StartChartManage中的尺寸值
StartChartManage().videoWidth = 864;
StartChartManage().videoHeight = 480;
return textureId;
}
if (attempt < maxAttempts) {
await Future.delayed(Duration(milliseconds: 500 * attempt));
}
} catch (e) {
AppLog.log('使用默认尺寸视频解码器初始化第 $attempt 次尝试出错: $e');
if (attempt < maxAttempts) {
await Future.delayed(Duration(milliseconds: 500 * attempt));
}
}
}
return null;
}
///
void _initFlutterPcmSound() {
@ -555,6 +642,9 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
requestPermissions();
//
_startVideoDimensionListener();
//
_initVideoDecoder();
@ -612,6 +702,11 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
_startProcessingAudioTimer?.cancel();
_startProcessingAudioTimer = null;
_bufferedAudioFrames.clear();
//
_videoDimensionCheckTimer?.cancel();
_videoDimensionCheckTimer = null;
super.onClose();
}
@ -922,6 +1017,21 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
}
}
//
void _startVideoDimensionListener() {
_videoDimensionCheckTimer = Timer.periodic(const Duration(milliseconds: 500), (timer) {
// videoWidth和videoHeight
if (state.textureId.value == null &&
StartChartManage().videoWidth != 0 &&
StartChartManage().videoHeight != 0 ) {
//
if (state.textureId.value == null) {
_initVideoDecoder();
}
}
});
}
void _processFrame(TalkDataModel talkDataModel) {
final talkData = talkDataModel.talkData;
final talkDataH264Frame = talkDataModel.talkDataH264Frame;