import 'dart:typed_data'; import 'package:flutter/foundation.dart'; import 'package:flutter_pcm_sound/flutter_pcm_sound.dart'; import 'package:star_lock/talk/call/g711.dart'; import 'package:star_lock/talk/call/iFrameInfo.dart'; import '../../app_settings/app_settings.dart'; import '../../tools/eventBusEventManage.dart'; class CallTalk { static CallTalk? _manager; static int POS_iframe_index = 63; static int POS_alen = 65; static int POS_blen = 73; static int POS_bag_index = 71; static int POS_bag_num = 69; static int POS_data = 77; static int ABUF_NUM = 100; static int FIRSTINDEX = 1; IframeInfo? iframe; // 假设有这个成员变量 List allDataBytes = []; //音频数据 CallTalk._init() { iframe = IframeInfo(); FlutterPcmSound.setLogLevel(LogLevel.error); FlutterPcmSound.setup(sampleRate: 8000, channelCount: 1); FlutterPcmSound.setFeedThreshold(8000 ~/ 2); } static CallTalk _share() { _manager ??= CallTalk._init(); return _manager!; } factory CallTalk() => _share(); CallTalk get manager => _share(); Future getAVData(Uint8List bb, int len) async { // 音频数据 if (bb[61] == 1) { // AppLog.log('音频数据来了'); final Uint8List g711Data = bb.sublist(77, bb.length); List pcmBytes; try { // 将 ALaw 转为 Linear pcmBytes = G711().convertList(g711Data); allDataBytes.addAll(pcmBytes); // String filePath = 'assets/s10-g711.bin'; // // List audioData = await G711().readAssetFile(filePath); // pcmBytes = G711().convertList(audioData); // allDataBytes = pcmBytes.sublist(0, 640); _initializeAudioPlayer(pcmBytes); } catch (e) { AppLog.log('Error decoding G.711 to PCM: $e'); } } // 视频数据 else { // AppLog.log('********视频数据来了'); // 音视频数据开始下标 final int bagLen = bb[POS_blen + 2] + bb[POS_blen + 3] * 256; // AppLog.log('音视频数据开始下标 bagLen:$bagLen'); // 获取帧序号 63 final int getIframeIndex = bb[POS_iframe_index] + bb[POS_iframe_index + 1] * 256; // AppLog.log('获取帧序号 getIframeIndex:$getIframeIndex'); // 获取帧长度 65 final int alen = bb[POS_alen] + bb[POS_alen + 1] * 256; // AppLog.log('获取帧长度 alen:$alen'); // 当前包号 71 final int getBagIndex = bb[POS_bag_index] & 0xff; // AppLog.log('当前包号 getBagIndex:$getBagIndex'); // 总包数 69 final int getBagNum = bb[POS_bag_num] & 0xff; // AppLog.log('总包数 getBagNum:$getBagNum'); // 数据长度 73 final int blen = bb[POS_blen] + bb[POS_blen + 1] * 256; // AppLog.log('数据长度 blen:$blen'); // 这里判断是否是同一帧,如果不是同一帧就重新创建一个 IframeInfo if (getIframeIndex != iframe!.iframeIndex) { iframe = IframeInfo(); iframe!.iframeIndex = getIframeIndex; iframe!.bagNum = getBagNum; iframe!.cur_len = alen; iframe!.bb = []; // growableList = iframe!.bb!.toList(growable: true); } iframe!.bagReceive++; // 如果是同一帧就添加起来 if (getIframeIndex == iframe!.iframeIndex) { final Uint8List getList = bb.sublist(POS_data, bb.length); iframe!.bb!.addAll(getList); } // AppLog.log( // 'iframe.bagNum: ${iframe!.bagNum} iframe.bagReceive: ${iframe!.bagReceive}'); // 如果收到的包数等于总包数,说明这一帧数据已经接收完毕 if (iframe!.bagNum == iframe!.bagReceive) { // AppLog.log( // '播放第${iframe!.iframeIndex}帧 一帧图片的hexStringData: ${Uint8List.fromList(growableList)}'); // AppLog.log('得到的一张图片的数据长度为${iframe!.bb!.length}'); // DateTime now = DateTime.now(); // String formattedTime = "${now.hour}:${now.minute}:${now.second}"; // AppLog.log('$formattedTime得到了一张图片共${iframe!.bagReceive}个数据包'); eventBus.fire(GetTVDataRefreshUI(iframe!.bb!)); } else { // AppLog.log('接收到的包数不等于总包数'); } } } //音频相关处理 Future _initializeAudioPlayer(List audioData) async { // AppLog.log('_initializeAudioPlayer audioData:$audioData'); final PcmArrayInt16 fromList = PcmArrayInt16.fromList(audioData); await FlutterPcmSound.feed(fromList); FlutterPcmSound.play(); } void startPlaySound() { FlutterPcmSound.play(); } void stopPlaySound() { FlutterPcmSound.pause(); FlutterPcmSound.clear(); FlutterPcmSound.stop(); } //停止接收音视频数据 void finishAVData() { // FlutterPcmSound.setup(sampleRate: 8000, channelCount: 1); FlutterPcmSound.pause(); FlutterPcmSound.clear(); FlutterPcmSound.stop(); // AppLog.log('已停止播放声音'); iframe = IframeInfo(); iframe!.iframeIndex = 0; iframe!.bagNum = 0; iframe!.bagReceive = 0; iframe!.bb = []; } }