2023-12-11 10:43:41 +08:00
|
|
|
|
import 'dart:typed_data';
|
2023-12-29 14:31:25 +08:00
|
|
|
|
import 'package:flutter/foundation.dart';
|
2023-12-29 18:40:02 +08:00
|
|
|
|
import 'package:get/get.dart';
|
2023-12-22 14:22:34 +08:00
|
|
|
|
import 'package:star_lock/talk/call/g711.dart';
|
2023-12-11 10:43:41 +08:00
|
|
|
|
import 'package:star_lock/talk/call/iFrameInfo.dart';
|
2023-12-18 16:03:05 +08:00
|
|
|
|
import '../../tools/eventBusEventManage.dart';
|
2023-12-29 14:31:25 +08:00
|
|
|
|
import 'package:flutter_pcm_sound/flutter_pcm_sound.dart';
|
2023-12-18 16:03:05 +08:00
|
|
|
|
|
2023-12-11 10:43:41 +08:00
|
|
|
|
class CallTalk {
|
2023-12-19 18:33:02 +08:00
|
|
|
|
static CallTalk? _manager;
|
2023-12-11 10:43:41 +08:00
|
|
|
|
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; // 假设有这个成员变量
|
2023-12-19 18:33:02 +08:00
|
|
|
|
IframeInfo? iframe; // 假设有这个成员变量
|
2023-12-20 16:52:18 +08:00
|
|
|
|
bool getFirstFrame = false; //是否得到了第一帧
|
2023-12-29 18:40:02 +08:00
|
|
|
|
List<int> allDataBytes = <int>[]; //音频数据
|
2023-12-11 10:43:41 +08:00
|
|
|
|
|
2023-12-19 18:33:02 +08:00
|
|
|
|
CallTalk._init() {
|
|
|
|
|
|
iframe = IframeInfo();
|
2023-12-29 18:10:22 +08:00
|
|
|
|
FlutterPcmSound.setLogLevel(LogLevel.error);
|
|
|
|
|
|
FlutterPcmSound.setup(sampleRate: 8000, channelCount: 1);
|
|
|
|
|
|
FlutterPcmSound.setFeedThreshold(8000 ~/ 2);
|
2023-12-19 18:33:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static CallTalk _share() {
|
|
|
|
|
|
_manager ??= CallTalk._init();
|
|
|
|
|
|
return _manager!;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
factory CallTalk() => _share();
|
|
|
|
|
|
CallTalk get manager => _share();
|
|
|
|
|
|
|
2023-12-11 10:43:41 +08:00
|
|
|
|
Future<void> getAVData(Uint8List bb, int len) async {
|
|
|
|
|
|
// 音频数据
|
|
|
|
|
|
if (bb[61] == 1) {
|
2024-01-08 11:25:36 +08:00
|
|
|
|
// print('音频数据来了');
|
2023-12-21 16:48:07 +08:00
|
|
|
|
Uint8List g711Data = bb.sublist(77, bb.length);
|
2023-12-22 14:30:33 +08:00
|
|
|
|
|
2023-12-27 13:59:11 +08:00
|
|
|
|
List<int> pcmBytes;
|
2023-12-26 16:30:06 +08:00
|
|
|
|
try {
|
|
|
|
|
|
// 将 ALaw 转为 Linear
|
|
|
|
|
|
pcmBytes = G711().convertList(g711Data);
|
2023-12-29 18:40:02 +08:00
|
|
|
|
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);
|
|
|
|
|
|
|
2023-12-29 14:31:25 +08:00
|
|
|
|
_initializeAudioPlayer(pcmBytes);
|
2023-12-26 16:30:06 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
print('Error decoding G.711 to PCM: $e');
|
|
|
|
|
|
}
|
2023-12-11 10:43:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 视频数据
|
|
|
|
|
|
else {
|
2024-01-08 11:25:36 +08:00
|
|
|
|
// print('********视频数据来了');
|
2023-12-19 18:33:02 +08:00
|
|
|
|
// 音视频数据开始下标
|
|
|
|
|
|
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;
|
2024-01-08 17:56:12 +08:00
|
|
|
|
// print('获取帧序号 getIframeIndex:$getIframeIndex');
|
2023-12-21 13:56:48 +08:00
|
|
|
|
|
2023-12-19 18:33:02 +08:00
|
|
|
|
// 获取帧长度 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
|
2023-12-11 10:43:41 +08:00
|
|
|
|
int getBagIndex = bb[POS_bag_index] & 0xff;
|
2024-01-08 17:56:12 +08:00
|
|
|
|
// print('当前包号 getBagIndex:$getBagIndex');
|
2023-12-19 18:33:02 +08:00
|
|
|
|
// 总包数 69
|
2023-12-11 10:43:41 +08:00
|
|
|
|
int getBagNum = bb[POS_bag_num] & 0xff;
|
2024-01-08 17:56:12 +08:00
|
|
|
|
// print('总包数 getBagNum:$getBagNum');
|
2023-12-19 18:33:02 +08:00
|
|
|
|
// 数据长度 73
|
|
|
|
|
|
int blen = bb[POS_blen] + bb[POS_blen + 1] * 256;
|
2023-12-21 13:56:48 +08:00
|
|
|
|
// print('数据长度 blen:$blen');
|
2023-12-11 10:43:41 +08:00
|
|
|
|
|
2023-12-19 18:33:02 +08:00
|
|
|
|
// 这里判断是否是同一帧,如果不是同一帧就重新创建一个 IframeInfo
|
|
|
|
|
|
if (getIframeIndex != iframe!.iframeIndex) {
|
2023-12-11 10:43:41 +08:00
|
|
|
|
iframe = IframeInfo();
|
2023-12-19 18:33:02 +08:00
|
|
|
|
iframe!.iframeIndex = getIframeIndex;
|
|
|
|
|
|
iframe!.bagNum = getBagNum;
|
|
|
|
|
|
// iframe!.cur_len = alen;
|
2024-01-08 17:56:12 +08:00
|
|
|
|
iframe!.bb = [];
|
|
|
|
|
|
// growableList = iframe!.bb!.toList(growable: true);
|
2023-12-11 10:43:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-19 18:33:02 +08:00
|
|
|
|
iframe!.bagReceive++;
|
|
|
|
|
|
|
|
|
|
|
|
// 如果是同一帧就添加起来
|
|
|
|
|
|
if (getIframeIndex == iframe!.iframeIndex) {
|
|
|
|
|
|
var getList = bb.sublist(77, bb.length);
|
2024-01-08 17:56:12 +08:00
|
|
|
|
iframe!.bb!.addAll(getList);
|
2023-12-11 10:43:41 +08:00
|
|
|
|
}
|
2023-12-21 13:56:48 +08:00
|
|
|
|
// print('iframe.bagNum: ${iframe!.bagNum} iframe.bagReceive: ${iframe!.bagReceive}');
|
2023-12-19 18:33:02 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果收到的包数等于总包数,说明这一帧数据已经接收完毕
|
|
|
|
|
|
if (iframe!.bagNum == iframe!.bagReceive) {
|
2023-12-21 13:56:48 +08:00
|
|
|
|
// print('播放第${iframe!.iframeIndex}帧 一帧图片的hexStringData: ${Uint8List.fromList(growableList)}');
|
2024-01-08 17:56:12 +08:00
|
|
|
|
eventBus.fire(GetTVDataRefreshUI(iframe!.bb!));
|
2023-12-18 16:03:05 +08:00
|
|
|
|
}
|
2023-12-11 10:43:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-20 13:46:05 +08:00
|
|
|
|
//音频相关处理
|
2023-12-27 13:59:11 +08:00
|
|
|
|
Future<void> _initializeAudioPlayer(List<int> audioData) async {
|
2023-12-29 18:40:02 +08:00
|
|
|
|
Get.log('_initializeAudioPlayer audioData:$audioData');
|
|
|
|
|
|
|
2023-12-29 14:31:25 +08:00
|
|
|
|
PcmArrayInt16 fromList = PcmArrayInt16.fromList(audioData);
|
2023-12-29 18:40:02 +08:00
|
|
|
|
// FlutterPcmSound.setFeedCallback(onFeed);
|
2023-12-29 14:31:25 +08:00
|
|
|
|
await FlutterPcmSound.feed(fromList);
|
|
|
|
|
|
FlutterPcmSound.play();
|
2023-12-11 10:43:41 +08:00
|
|
|
|
}
|
2023-12-29 18:40:02 +08:00
|
|
|
|
|
|
|
|
|
|
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.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-01-03 15:34:08 +08:00
|
|
|
|
|
|
|
|
|
|
//停止接收音频数据
|
|
|
|
|
|
void stopPcmSound() {
|
2024-01-08 11:32:35 +08:00
|
|
|
|
// FlutterPcmSound.setup(sampleRate: 8000, channelCount: 1);
|
2024-01-06 14:49:44 +08:00
|
|
|
|
FlutterPcmSound.pause();
|
|
|
|
|
|
FlutterPcmSound.clear();
|
2024-01-03 15:34:08 +08:00
|
|
|
|
FlutterPcmSound.stop();
|
2024-01-08 11:25:36 +08:00
|
|
|
|
|
|
|
|
|
|
iframe = IframeInfo();
|
|
|
|
|
|
iframe!.iframeIndex = 0;
|
|
|
|
|
|
iframe!.bagNum = 0;
|
|
|
|
|
|
iframe!.bagReceive = 0;
|
2024-01-08 17:56:12 +08:00
|
|
|
|
iframe!.bb = [];
|
2023-12-29 18:40:02 +08:00
|
|
|
|
}
|
2024-01-03 15:34:08 +08:00
|
|
|
|
}
|