fix:修复对讲的高清、标清切换功能

This commit is contained in:
liyi 2025-06-18 15:12:51 +08:00
parent bf4c2b4750
commit 51ca6e1f23

View File

@ -25,6 +25,7 @@ import 'package:star_lock/network/api_repository.dart';
import 'package:star_lock/talk/call/callTalk.dart'; import 'package:star_lock/talk/call/callTalk.dart';
import 'package:star_lock/talk/call/g711.dart'; import 'package:star_lock/talk/call/g711.dart';
import 'package:star_lock/talk/starChart/constant/talk_status.dart'; import 'package:star_lock/talk/starChart/constant/talk_status.dart';
import 'package:star_lock/talk/starChart/entity/scp_message.dart';
import 'package:star_lock/talk/starChart/handle/other/packet_loss_statistics.dart'; import 'package:star_lock/talk/starChart/handle/other/packet_loss_statistics.dart';
import 'package:star_lock/talk/starChart/handle/other/talk_data_model.dart'; import 'package:star_lock/talk/starChart/handle/other/talk_data_model.dart';
import 'package:star_lock/talk/starChart/proto/talk_data.pb.dart'; import 'package:star_lock/talk/starChart/proto/talk_data.pb.dart';
@ -87,15 +88,17 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
// I帧状态 // I帧状态
bool _waitingForIFrame = false; bool _waitingForIFrame = false;
int? lastDecodedIFrameSeq;
// //
Future<void> _initVideoDecoder() async { Future<void> _initVideoDecoder() async {
try { try {
state.isLoading.value = true; state.isLoading.value = true;
// //
final config = VideoDecoderConfig( final config = VideoDecoderConfig(
width: 864, width: StartChartManage().videoWidth,
// //
height: 480, height: StartChartManage().videoHeight,
codecType: 'h264', codecType: 'h264',
); );
// textureId // textureId
@ -157,6 +160,7 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
int pts, int pts,
int frameSeq, int frameSeq,
int frameSeqI, int frameSeqI,
ScpMessage scpMessage,
) { ) {
// frameSeq回绕I帧 // frameSeq回绕I帧
if (!_pendingStreamReset && if (!_pendingStreamReset &&
@ -212,6 +216,7 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
'frameSeq': frameSeq, 'frameSeq': frameSeq,
'frameSeqI': frameSeqI, 'frameSeqI': frameSeqI,
'pts': pts, 'pts': pts,
'scpMessage': scpMessage,
}; };
// P/B帧 // P/B帧
@ -257,14 +262,25 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
return; return;
} }
// // I帧frameSeq最小的I帧消费
state.isProcessingFrame = true; final iFrames = state.h264FrameBuffer
.where((f) => f['frameType'] == TalkDataH264Frame_FrameTypeE.I)
.toList();
iFrames
.sort((a, b) => (a['frameSeq'] as int).compareTo(b['frameSeq'] as int));
try { if (iFrames.isNotEmpty) {
// // I帧I帧frameSeq
final Map<String, dynamic>? frameMap = state.h264FrameBuffer.isNotEmpty final minIFrame = iFrames.first;
? state.h264FrameBuffer.removeAt(0) final minIFrameSeq = minIFrame['frameSeq'];
: null; final targetIndex = state.h264FrameBuffer.indexWhere(
(f) =>
f['frameType'] == TalkDataH264Frame_FrameTypeE.I &&
f['frameSeq'] == minIFrameSeq,
);
state.isProcessingFrame = true;
final Map<String, dynamic>? frameMap =
state.h264FrameBuffer.removeAt(targetIndex);
if (frameMap == null) { if (frameMap == null) {
state.isProcessingFrame = false; state.isProcessingFrame = false;
return; return;
@ -274,6 +290,7 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
final int? frameSeq = frameMap['frameSeq']; final int? frameSeq = frameMap['frameSeq'];
final int? frameSeqI = frameMap['frameSeqI']; final int? frameSeqI = frameMap['frameSeqI'];
final int? pts = frameMap['pts']; final int? pts = frameMap['pts'];
final ScpMessage? scpMessage = frameMap['scpMessage'];
if (frameData == null || if (frameData == null ||
frameType == null || frameType == null ||
frameSeq == null || frameSeq == null ||
@ -282,25 +299,82 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
state.isProcessingFrame = false; state.isProcessingFrame = false;
return; return;
} }
// textureId为null时跳过
if (state.textureId.value == null) { if (state.textureId.value == null) {
state.isProcessingFrame = false; state.isProcessingFrame = false;
return; return;
} }
lastDecodedIFrameSeq = minIFrameSeq;
// AppLog.log('送入解码器的P帧数据frameSeq:${frameSeq},frameSeqI:${frameSeqI},'
// 'frameType:${frameType},messageId:${scpMessage!.MessageId}');
await VideoDecodePlugin.sendFrame( await VideoDecodePlugin.sendFrame(
frameData: frameData, frameData: frameData,
frameType: frameType == TalkDataH264Frame_FrameTypeE.I ? 0 : 1, frameType: 0,
frameSeq: frameSeq, frameSeq: frameSeq,
timestamp: pts, timestamp: pts,
splitNalFromIFrame: true, splitNalFromIFrame: true,
refIFrameSeq: frameSeqI, refIFrameSeq: frameSeqI,
); );
} catch (e) {
AppLog.log('处理缓冲帧失败: $e');
} finally {
//
state.isProcessingFrame = false; state.isProcessingFrame = false;
return;
} }
// I帧时refIFrameSeq等于lastDecodedIFrameSeq的P帧
if (lastDecodedIFrameSeq != null) {
final validPFrames = state.h264FrameBuffer
.where((f) =>
f['frameType'] == TalkDataH264Frame_FrameTypeE.P &&
f['frameSeqI'] == lastDecodedIFrameSeq)
.toList();
if (validPFrames.isNotEmpty) {
validPFrames.sort(
(a, b) => (a['frameSeq'] as int).compareTo(b['frameSeq'] as int));
final minPFrame = validPFrames.first;
final targetIndex = state.h264FrameBuffer.indexWhere(
(f) =>
f['frameType'] == TalkDataH264Frame_FrameTypeE.P &&
f['frameSeq'] == minPFrame['frameSeq'] &&
f['frameSeqI'] == lastDecodedIFrameSeq,
);
state.isProcessingFrame = true;
final Map<String, dynamic>? frameMap =
state.h264FrameBuffer.removeAt(targetIndex);
if (frameMap == null) {
state.isProcessingFrame = false;
return;
}
final List<int>? frameData = frameMap['frameData'];
final TalkDataH264Frame_FrameTypeE? frameType = frameMap['frameType'];
final int? frameSeq = frameMap['frameSeq'];
final int? frameSeqI = frameMap['frameSeqI'];
final int? pts = frameMap['pts'];
final ScpMessage? scpMessage = frameMap['scpMessage'];
if (frameData == null ||
frameType == null ||
frameSeq == null ||
frameSeqI == null ||
pts == null) {
state.isProcessingFrame = false;
return;
}
if (state.textureId.value == null) {
state.isProcessingFrame = false;
return;
}
// AppLog.log('送入解码器的P帧数据frameSeq:${frameSeq},frameSeqI:${frameSeqI},'
// 'frameType:${frameType},messageId:${scpMessage!.MessageId}');
await VideoDecodePlugin.sendFrame(
frameData: frameData,
frameType: 1,
frameSeq: frameSeq,
timestamp: pts,
splitNalFromIFrame: true,
refIFrameSeq: frameSeqI,
);
state.isProcessingFrame = false;
return;
}
}
// I帧到来
} }
/// ///
@ -331,6 +405,7 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
.listen((TalkDataModel talkDataModel) async { .listen((TalkDataModel talkDataModel) async {
final talkData = talkDataModel.talkData; final talkData = talkDataModel.talkData;
final talkDataH264Frame = talkDataModel.talkDataH264Frame; final talkDataH264Frame = talkDataModel.talkDataH264Frame;
final scpMessage = talkDataModel.scpMessage;
final contentType = talkData!.contentType; final contentType = talkData!.contentType;
// //
@ -345,7 +420,7 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
break; break;
case TalkData_ContentTypeE.H264: case TalkData_ContentTypeE.H264:
// H264帧 // H264帧
if (state.textureId.value != null) { if (state.textureId.value != null || true) {
if (talkDataH264Frame != null) { if (talkDataH264Frame != null) {
_addFrameToBuffer( _addFrameToBuffer(
talkData.content, talkData.content,
@ -353,6 +428,7 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
talkData.durationMs, talkData.durationMs,
talkDataH264Frame.frameSeq, talkDataH264Frame.frameSeq,
talkDataH264Frame.frameSeqI, talkDataH264Frame.frameSeqI,
scpMessage!,
); );
} }
} else { } else {
@ -585,7 +661,8 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
TalkExpectReq talkExpectReq = TalkExpectReq(); TalkExpectReq talkExpectReq = TalkExpectReq();
state.isOpenVoice.value = !state.isOpenVoice.value; state.isOpenVoice.value = !state.isOpenVoice.value;
// videoType // videoType
VideoTypeE currentVideoType = qualityToVideoType[state.currentQuality.value] ?? VideoTypeE.H264; VideoTypeE currentVideoType =
qualityToVideoType[state.currentQuality.value] ?? VideoTypeE.H264;
if (!state.isOpenVoice.value) { if (!state.isOpenVoice.value) {
talkExpectReq = TalkExpectReq( talkExpectReq = TalkExpectReq(
videoType: [currentVideoType], videoType: [currentVideoType],
@ -1104,144 +1181,144 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
} }
// I帧处理方法 // I帧处理方法
void _handleIFrameWithSpsPpsAndIdr( // void _handleIFrameWithSpsPpsAndIdr(
List<int> frameData, int durationMs, int frameSeq, int frameSeqI) { // List<int> frameData, int durationMs, int frameSeq, int frameSeqI) {
// I帧前所有未处理帧SPS/PPS/I帧 // // I帧前所有未处理帧SPS/PPS/I帧
state.h264FrameBuffer.clear(); // state.h264FrameBuffer.clear();
_extractAndBufferSpsPpsForBuffer( // _extractAndBufferSpsPpsForBuffer(
frameData, durationMs, frameSeq, frameSeqI); // frameData, durationMs, frameSeq, frameSeqI);
// SPS/PPS就先写入I帧本体IDR // // SPS/PPS就先写入I帧本体IDR
if (spsCache == null || ppsCache == null) { // if (spsCache == null || ppsCache == null) {
// SPS/PPS缓存I帧 // // SPS/PPS缓存I帧
return; // return;
} // }
// SPS/PPS // // SPS/PPS
_addFrameToBuffer(spsCache!, TalkDataH264Frame_FrameTypeE.I, durationMs, // _addFrameToBuffer(spsCache!, TalkDataH264Frame_FrameTypeE.I, durationMs,
frameSeq, frameSeqI); // frameSeq, frameSeqI);
_addFrameToBuffer(ppsCache!, TalkDataH264Frame_FrameTypeE.I, durationMs, // _addFrameToBuffer(ppsCache!, TalkDataH264Frame_FrameTypeE.I, durationMs,
frameSeq, frameSeqI); // frameSeq, frameSeqI);
// I帧包IDRtype 5 // // I帧包IDRtype 5
List<List<int>> nalus = []; // List<List<int>> nalus = [];
int i = 0; // int i = 0;
List<int> data = frameData; // List<int> data = frameData;
while (i < data.length - 3) { // while (i < data.length - 3) {
int start = -1; // int start = -1;
int next = -1; // int next = -1;
if (data[i] == 0x00 && data[i + 1] == 0x00) { // if (data[i] == 0x00 && data[i + 1] == 0x00) {
if (data[i + 2] == 0x01) { // if (data[i + 2] == 0x01) {
start = i; // start = i;
i += 3; // i += 3;
} else if (i + 3 < data.length && // } else if (i + 3 < data.length &&
data[i + 2] == 0x00 && // data[i + 2] == 0x00 &&
data[i + 3] == 0x01) { // data[i + 3] == 0x01) {
start = i; // start = i;
i += 4; // i += 4;
} else { // } else {
i++; // i++;
continue; // continue;
} // }
next = i; // next = i;
while (next < data.length - 3) { // while (next < data.length - 3) {
if (data[next] == 0x00 && // if (data[next] == 0x00 &&
data[next + 1] == 0x00 && // data[next + 1] == 0x00 &&
((data[next + 2] == 0x01) || // ((data[next + 2] == 0x01) ||
(data[next + 2] == 0x00 && data[next + 3] == 0x01))) { // (data[next + 2] == 0x00 && data[next + 3] == 0x01))) {
break; // break;
} // }
next++; // next++;
} // }
nalus.add(data.sublist(start, next)); // nalus.add(data.sublist(start, next));
i = next; // i = next;
} else { // } else {
i++; // i++;
} // }
} // }
int nalusTotalLen = // int nalusTotalLen =
nalus.isNotEmpty ? nalus.fold(0, (p, n) => p + n.length) : 0; // nalus.isNotEmpty ? nalus.fold(0, (p, n) => p + n.length) : 0;
if (nalus.isEmpty && data.isNotEmpty) { // if (nalus.isEmpty && data.isNotEmpty) {
nalus.add(data); // nalus.add(data);
} else if (nalus.isNotEmpty && nalusTotalLen < data.length) { // } else if (nalus.isNotEmpty && nalusTotalLen < data.length) {
nalus.add(data.sublist(nalusTotalLen)); // nalus.add(data.sublist(nalusTotalLen));
} // }
for (final nalu in nalus) { // for (final nalu in nalus) {
int offset = 0; // int offset = 0;
if (nalu.length >= 4 && nalu[0] == 0x00 && nalu[1] == 0x00) { // if (nalu.length >= 4 && nalu[0] == 0x00 && nalu[1] == 0x00) {
if (nalu[2] == 0x01) // if (nalu[2] == 0x01)
offset = 3; // offset = 3;
else if (nalu[2] == 0x00 && nalu[3] == 0x01) offset = 4; // else if (nalu[2] == 0x00 && nalu[3] == 0x01) offset = 4;
} // }
if (nalu.length > offset) { // if (nalu.length > offset) {
int naluType = nalu[offset] & 0x1F; // int naluType = nalu[offset] & 0x1F;
if (naluType == 5) { // if (naluType == 5) {
_addFrameToBuffer(nalu, TalkDataH264Frame_FrameTypeE.I, durationMs, // _addFrameToBuffer(nalu, TalkDataH264Frame_FrameTypeE.I, durationMs,
frameSeq, frameSeqI); // frameSeq, frameSeqI);
} // }
} // }
} // }
} // }
// P帧处理方法 // P帧处理方法
void _handlePFrame( // void _handlePFrame(
List<int> frameData, int durationMs, int frameSeq, int frameSeqI) { // List<int> frameData, int durationMs, int frameSeq, int frameSeqI) {
// P帧type 1 // // P帧type 1
List<List<int>> nalus = []; // List<List<int>> nalus = [];
int i = 0; // int i = 0;
List<int> data = frameData; // List<int> data = frameData;
while (i < data.length - 3) { // while (i < data.length - 3) {
int start = -1; // int start = -1;
int next = -1; // int next = -1;
if (data[i] == 0x00 && data[i + 1] == 0x00) { // if (data[i] == 0x00 && data[i + 1] == 0x00) {
if (data[i + 2] == 0x01) { // if (data[i + 2] == 0x01) {
start = i; // start = i;
i += 3; // i += 3;
} else if (i + 3 < data.length && // } else if (i + 3 < data.length &&
data[i + 2] == 0x00 && // data[i + 2] == 0x00 &&
data[i + 3] == 0x01) { // data[i + 3] == 0x01) {
start = i; // start = i;
i += 4; // i += 4;
} else { // } else {
i++; // i++;
continue; // continue;
} // }
next = i; // next = i;
while (next < data.length - 3) { // while (next < data.length - 3) {
if (data[next] == 0x00 && // if (data[next] == 0x00 &&
data[next + 1] == 0x00 && // data[next + 1] == 0x00 &&
((data[next + 2] == 0x01) || // ((data[next + 2] == 0x01) ||
(data[next + 2] == 0x00 && data[next + 3] == 0x01))) { // (data[next + 2] == 0x00 && data[next + 3] == 0x01))) {
break; // break;
} // }
next++; // next++;
} // }
nalus.add(data.sublist(start, next)); // nalus.add(data.sublist(start, next));
i = next; // i = next;
} else { // } else {
i++; // i++;
} // }
} // }
int nalusTotalLen = // int nalusTotalLen =
nalus.isNotEmpty ? nalus.fold(0, (p, n) => p + n.length) : 0; // nalus.isNotEmpty ? nalus.fold(0, (p, n) => p + n.length) : 0;
if (nalus.isEmpty && data.isNotEmpty) { // if (nalus.isEmpty && data.isNotEmpty) {
nalus.add(data); // nalus.add(data);
} else if (nalus.isNotEmpty && nalusTotalLen < data.length) { // } else if (nalus.isNotEmpty && nalusTotalLen < data.length) {
nalus.add(data.sublist(nalusTotalLen)); // nalus.add(data.sublist(nalusTotalLen));
} // }
for (final nalu in nalus) { // for (final nalu in nalus) {
int offset = 0; // int offset = 0;
if (nalu.length >= 4 && nalu[0] == 0x00 && nalu[1] == 0x00) { // if (nalu.length >= 4 && nalu[0] == 0x00 && nalu[1] == 0x00) {
if (nalu[2] == 0x01) // if (nalu[2] == 0x01)
offset = 3; // offset = 3;
else if (nalu[2] == 0x00 && nalu[3] == 0x01) offset = 4; // else if (nalu[2] == 0x00 && nalu[3] == 0x01) offset = 4;
} // }
if (nalu.length > offset) { // if (nalu.length > offset) {
int naluType = nalu[offset] & 0x1F; // int naluType = nalu[offset] & 0x1F;
if (naluType == 1) { // if (naluType == 1) {
_addFrameToBuffer(nalu, TalkDataH264Frame_FrameTypeE.P, durationMs, // _addFrameToBuffer(nalu, TalkDataH264Frame_FrameTypeE.P, durationMs,
frameSeq, frameSeqI); // frameSeq, frameSeqI);
} // }
} // }
} // }
} // }
// //
void onQualityChanged(String quality) async { void onQualityChanged(String quality) async {
@ -1293,30 +1370,56 @@ class TalkViewNativeDecodeLogic extends BaseGetXController {
// //
Future<void> _resetDecoderForNewStream(int width, int height) async { Future<void> _resetDecoderForNewStream(int width, int height) async {
try { try {
//
_stopFrameProcessTimer();
//
if (state.textureId.value != null) { if (state.textureId.value != null) {
await VideoDecodePlugin.releaseDecoder(); await VideoDecodePlugin.releaseDecoder();
Future.microtask(() => state.textureId.value = null); state.textureId.value = null;
} }
//
await Future.delayed(Duration(milliseconds: 100));
//
final config = VideoDecoderConfig( final config = VideoDecoderConfig(
width: width, width: width,
height: height, height: height,
codecType: 'h264', codecType: 'h264',
); );
//
final textureId = await VideoDecodePlugin.initDecoder(config); final textureId = await VideoDecodePlugin.initDecoder(config);
if (textureId != null) { if (textureId != null) {
Future.microtask(() => state.textureId.value = textureId); state.textureId.value = textureId;
AppLog.log('frameSeq回绕后解码器初始化成功textureId=$textureId'); AppLog.log('frameSeq回绕后解码器初始化成功textureId=$textureId');
//
VideoDecodePlugin.setOnFrameRenderedListener((textureId) { VideoDecodePlugin.setOnFrameRenderedListener((textureId) {
AppLog.log('已经开始渲染======='); AppLog.log('已经开始渲染=======');
// loading // loading
Future.microtask(() => state.isLoading.value = false); state.isLoading.value = false;
}); });
//
_startFrameProcessTimer();
//
_decodedIFrames.clear();
state.h264FrameBuffer.clear();
state.isProcessingFrame = false;
hasSps = false;
hasPps = false;
spsCache = null;
ppsCache = null;
} else { } else {
AppLog.log('frameSeq回绕后解码器初始化失败'); AppLog.log('frameSeq回绕后解码器初始化失败');
state.isLoading.value = false;
} }
_startFrameProcessTimer();
} catch (e) { } catch (e) {
AppLog.log('frameSeq回绕时解码器初始化错误: $e'); AppLog.log('frameSeq回绕时解码器初始化错误: $e');
state.isLoading.value = false;
} }
} }
} }