162 lines
5.2 KiB
Dart
162 lines
5.2 KiB
Dart
import 'dart:typed_data';
|
||
import 'package:flutter/foundation.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:star_lock/talk/call/g711.dart';
|
||
import 'package:star_lock/talk/call/iFrameInfo.dart';
|
||
import '../../tools/eventBusEventManage.dart';
|
||
import 'package:flutter_pcm_sound/flutter_pcm_sound.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;
|
||
|
||
int status = 0; // 假设有这个成员变量
|
||
IframeInfo? iframe; // 假设有这个成员变量
|
||
bool getFirstFrame = false; //是否得到了第一帧
|
||
List<int> allDataBytes = <int>[]; //音频数据
|
||
|
||
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<void> getAVData(Uint8List bb, int len) async {
|
||
// 音频数据
|
||
if (bb[61] == 1) {
|
||
// print('音频数据来了');
|
||
Uint8List g711Data = bb.sublist(77, bb.length);
|
||
|
||
List<int> pcmBytes;
|
||
try {
|
||
// 将 ALaw 转为 Linear
|
||
pcmBytes = G711().convertList(g711Data);
|
||
allDataBytes.addAll(pcmBytes);
|
||
|
||
// String filePath = 'assets/s10-g711.bin';
|
||
//
|
||
// List<int> audioData = await G711().readAssetFile(filePath);
|
||
// pcmBytes = G711().convertList(audioData);
|
||
// allDataBytes = pcmBytes.sublist(0, 640);
|
||
|
||
_initializeAudioPlayer(pcmBytes);
|
||
} catch (e) {
|
||
print('Error decoding G.711 to PCM: $e');
|
||
}
|
||
}
|
||
// 视频数据
|
||
else {
|
||
// print('********视频数据来了');
|
||
// 音视频数据开始下标
|
||
var bagLen = bb[POS_blen + 2] + bb[POS_blen + 3] * 256;
|
||
// print('音视频数据开始下标 bagLen:$bagLen');
|
||
|
||
// 获取帧序号 63
|
||
int getIframeIndex =
|
||
bb[POS_iframe_index] + bb[POS_iframe_index + 1] * 256;
|
||
// print('获取帧序号 getIframeIndex:$getIframeIndex');
|
||
|
||
// 获取帧长度 65
|
||
// int alen = bb[POS_alen] & 0xff;
|
||
// var alenList = bb.sublist(POS_alen, POS_alen + 4);
|
||
// int alen = ((0xff & alenList[(0)]) << 24 |
|
||
// (0xff & alenList[1]) << 16 |
|
||
// (0xff & alenList[2]) << 8 |
|
||
// (0xFF & alenList[3]));
|
||
// print('获取帧长度 alen:$alen');
|
||
|
||
// 当前包号 71
|
||
int getBagIndex = bb[POS_bag_index] & 0xff;
|
||
// print('当前包号 getBagIndex:$getBagIndex');
|
||
// 总包数 69
|
||
int getBagNum = bb[POS_bag_num] & 0xff;
|
||
// print('总包数 getBagNum:$getBagNum');
|
||
// 数据长度 73
|
||
int blen = bb[POS_blen] + bb[POS_blen + 1] * 256;
|
||
// print('数据长度 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) {
|
||
var getList = bb.sublist(77, bb.length);
|
||
iframe!.bb!.addAll(getList);
|
||
}
|
||
// print('iframe.bagNum: ${iframe!.bagNum} iframe.bagReceive: ${iframe!.bagReceive}');
|
||
|
||
// 如果收到的包数等于总包数,说明这一帧数据已经接收完毕
|
||
if (iframe!.bagNum == iframe!.bagReceive) {
|
||
// print('播放第${iframe!.iframeIndex}帧 一帧图片的hexStringData: ${Uint8List.fromList(growableList)}');
|
||
eventBus.fire(GetTVDataRefreshUI(iframe!.bb!));
|
||
}
|
||
}
|
||
}
|
||
|
||
//音频相关处理
|
||
Future<void> _initializeAudioPlayer(List<int> audioData) async {
|
||
Get.log('_initializeAudioPlayer audioData:$audioData');
|
||
|
||
PcmArrayInt16 fromList = PcmArrayInt16.fromList(audioData);
|
||
// FlutterPcmSound.setFeedCallback(onFeed);
|
||
await FlutterPcmSound.feed(fromList);
|
||
FlutterPcmSound.play();
|
||
}
|
||
|
||
void onFeed(int remainingFrames) async {
|
||
int framesToFeed = 320;
|
||
|
||
if (allDataBytes.length >= framesToFeed) {
|
||
List<int> frames = allDataBytes.sublist(0, framesToFeed);
|
||
allDataBytes.removeRange(0, framesToFeed);
|
||
|
||
// 将数据传递给 FlutterPcmSound
|
||
PcmArrayInt16 fromList = PcmArrayInt16.fromList(frames);
|
||
await FlutterPcmSound.feed(fromList);
|
||
FlutterPcmSound.play();
|
||
} else {
|
||
// 处理长度不足的情况,可以等待更多数据或者采取其他措施
|
||
print("Not enough data in allPcmData.");
|
||
}
|
||
}
|
||
|
||
//停止接收音频数据
|
||
void stopPcmSound() {
|
||
// FlutterPcmSound.setup(sampleRate: 8000, channelCount: 1);
|
||
FlutterPcmSound.pause();
|
||
FlutterPcmSound.clear();
|
||
FlutterPcmSound.stop();
|
||
|
||
iframe = IframeInfo();
|
||
iframe!.iframeIndex = 0;
|
||
iframe!.bagNum = 0;
|
||
iframe!.bagReceive = 0;
|
||
iframe!.bb = [];
|
||
}
|
||
}
|