优化iphone手机第一次点击监控无法接通问题
This commit is contained in:
parent
a6843f858b
commit
780ae8ccd2
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user