app-starlock/lib/talk/call/callTalk.dart

159 lines
5.1 KiB
Dart
Raw Normal View History

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';
import 'package:flutter_pcm_sound/flutter_pcm_sound.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';
2024-04-26 15:38:59 +08:00
import '../../app_settings/app_settings.dart';
2023-12-18 16:03:05 +08:00
import '../../tools/eventBusEventManage.dart';
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;
2023-12-19 18:33:02 +08:00
IframeInfo? iframe; // 假设有这个成员变量
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-04-26 15:38:59 +08:00
// AppLog.log('音频数据来了');
final Uint8List g711Data = bb.sublist(77, bb.length);
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) {
2024-04-26 15:38:59 +08:00
AppLog.log('Error decoding G.711 to PCM: $e');
2023-12-26 16:30:06 +08:00
}
2023-12-11 10:43:41 +08:00
}
// 视频数据
else {
2024-04-26 15:38:59 +08:00
// AppLog.log('********视频数据来了');
2023-12-19 18:33:02 +08:00
// 音视频数据开始下标
final int bagLen = bb[POS_blen + 2] + bb[POS_blen + 3] * 256;
2024-04-26 15:38:59 +08:00
// AppLog.log('音视频数据开始下标 bagLen$bagLen');
2023-12-19 18:33:02 +08:00
// 获取帧序号 63
final int getIframeIndex =
2023-12-19 18:33:02 +08:00
bb[POS_iframe_index] + bb[POS_iframe_index + 1] * 256;
2024-04-26 15:38:59 +08:00
// AppLog.log('获取帧序号 getIframeIndex$getIframeIndex');
2023-12-21 13:56:48 +08:00
2023-12-19 18:33:02 +08:00
// 获取帧长度 65
final int alen = bb[POS_alen] + bb[POS_alen + 1] * 256;
2024-04-26 15:38:59 +08:00
// AppLog.log('获取帧长度 alen$alen');
2023-12-19 18:33:02 +08:00
// 当前包号 71
final int getBagIndex = bb[POS_bag_index] & 0xff;
2024-04-26 15:38:59 +08:00
// AppLog.log('当前包号 getBagIndex$getBagIndex');
2023-12-19 18:33:02 +08:00
// 总包数 69
final int getBagNum = bb[POS_bag_num] & 0xff;
2024-04-26 15:38:59 +08:00
// AppLog.log('总包数 getBagNum$getBagNum');
2023-12-19 18:33:02 +08:00
// 数据长度 73
final int blen = bb[POS_blen] + bb[POS_blen + 1] * 256;
2024-04-26 15:38:59 +08:00
// AppLog.log('数据长度 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;
iframe!.bb = <int>[];
// 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) {
final Uint8List getList = bb.sublist(POS_data, bb.length);
iframe!.bb!.addAll(getList);
2023-12-11 10:43:41 +08:00
}
2024-04-26 15:38:59 +08:00
// AppLog.log(
2024-04-09 09:49:30 +08:00
// 'iframe.bagNum: ${iframe!.bagNum} iframe.bagReceive: ${iframe!.bagReceive}');
2023-12-19 18:33:02 +08:00
// 如果收到的包数等于总包数,说明这一帧数据已经接收完毕
2024-03-27 11:51:49 +08:00
if (iframe!.bagNum == iframe!.bagReceive) {
2024-04-26 15:38:59 +08:00
// AppLog.log(
// '播放第${iframe!.iframeIndex}帧 一帧图片的hexStringData: ${Uint8List.fromList(growableList)}');
2024-04-26 15:38:59 +08:00
// AppLog.log('得到的一张图片的数据长度为${iframe!.bb!.length}');
// DateTime now = DateTime.now();
// String formattedTime = "${now.hour}:${now.minute}:${now.second}";
2024-04-26 15:38:59 +08:00
// AppLog.log('$formattedTime得到了一张图片共${iframe!.bagReceive}个数据包');
eventBus.fire(GetTVDataRefreshUI(iframe!.bb!));
} else {
2024-04-26 15:38:59 +08:00
// AppLog.log('接收到的包数不等于总包数');
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 {
2024-04-26 15:38:59 +08:00
// AppLog.log('_initializeAudioPlayer audioData:$audioData');
2023-12-29 18:40:02 +08:00
final PcmArrayInt16 fromList = PcmArrayInt16.fromList(audioData);
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 startPlaySound() {
FlutterPcmSound.play();
2023-12-29 18:40:02 +08:00
}
void stopPlaySound() {
FlutterPcmSound.pause();
FlutterPcmSound.clear();
FlutterPcmSound.stop();
}
//停止接收音视频数据
void finishAVData() {
// FlutterPcmSound.setup(sampleRate: 8000, channelCount: 1);
2024-01-06 14:49:44 +08:00
FlutterPcmSound.pause();
FlutterPcmSound.clear();
FlutterPcmSound.stop();
2024-04-26 15:38:59 +08:00
// AppLog.log('已停止播放声音');
iframe = IframeInfo();
iframe!.iframeIndex = 0;
iframe!.bagNum = 0;
iframe!.bagReceive = 0;
iframe!.bb = <int>[];
2023-12-29 18:40:02 +08:00
}
}