自习室优化ok

This commit is contained in:
zhutao
2025-11-28 13:31:23 +08:00
parent 4ecb0c35d6
commit 57305c5804
57 changed files with 2500 additions and 597 deletions

View File

@@ -2,24 +2,23 @@ import 'dart:async';
import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:app/config/config.dart';
import 'package:app/providers/user_store.dart';
import 'package:app/data/models/meeting_room_dto.dart';
import 'package:app/request/dto/room/room_list_item_dto.dart';
import 'package:app/request/dto/room/room_info_dto.dart';
import 'package:app/request/dto/room/room_type_dto.dart';
import 'package:app/request/dto/room/room_user_dto.dart';
import 'package:app/request/dto/room/rtc_token_dto.dart';
import 'package:app/request/websocket/room_protocol.dart';
import 'package:app/request/websocket/room_websocket.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:logger/logger.dart';
import 'package:wakelock_plus/wakelock_plus.dart';
Logger log = Logger();
class StuRoomVM extends ChangeNotifier {
///房间信息
final RoomInfoDto roomInfo;
///房间开启状态,0没开始1进行中2已结束
int roomStatus = 0;
///房间信息状态0没开始1进行中2已结束
late MeetingRoomDto roomInfo;
///其他学生列表,老师信息,自己信息
int uid;
@@ -27,12 +26,18 @@ class StuRoomVM extends ChangeNotifier {
RoomUserDto? teacherInfo;
RoomUserDto? selfInfo;
///本人的摄像头麦克风、扬声器状态是否打开了
bool get cameraOpen => selfInfo?.cameraStatus == 1;
// ///老师是否发送请求过来了0关闭1摄像头2麦克风
// bool cameraReq = false;
// bool micReq = false;
bool get micOpen => selfInfo?.microphoneStatus == 1;
///本人的摄像头、麦克风、扬声器、举手状态是否关闭了
bool get cameraClose => selfInfo?.cameraStatus == 0;
bool get speakerOpen => selfInfo?.speekerStatus == 1;
bool get micClose => selfInfo?.microphoneStatus == 0;
bool get speakerClose => selfInfo?.speekerStatus == 0;
bool get handClose => selfInfo?.handup == 0;
///ws管理
final RoomWebSocket _ws = RoomWebSocket();
@@ -43,12 +48,14 @@ class StuRoomVM extends ChangeNotifier {
RtcEngine? get engine => _engine;
StuRoomVM({required this.roomInfo, required this.uid}) {
StuRoomVM({required RoomListItemDto info, required this.uid}) {
roomInfo = MeetingRoomDto.fromRoomListItem(info);
_startRoom();
}
///初始化声网
Future<void> _initRtc() async {
if (_engine != null) return;
_engine = createAgoraRtcEngine();
//初始化 RtcEngine设置频道场景为 channelProfileLiveBroadcasting直播场景
await _engine!.initialize(
@@ -57,27 +64,34 @@ class StuRoomVM extends ChangeNotifier {
channelProfile: ChannelProfileType.channelProfileCommunication,
),
);
//启动视频模块
_engine!.getUserInfoByUid(1);
// 启用视频模块
await _engine!.enableVideo();
//加入频道
await _engine!.joinChannel(
token: _ws.rtcToken!.token,
channelId: _ws.rtcToken!.channel,
uid: uid,
// uid: _ws.rtcToken!.uid,
options: ChannelMediaOptions(
// 自动订阅所有视频流
autoSubscribeVideo: true,
// 自动订阅所有音频流
autoSubscribeAudio: true,
// 发布摄像头采集的视频
publishCameraTrack: true,
// 发布麦克风采集的音频
publishMicrophoneTrack: true,
// 设置用户角色为 clientRoleBroadcaster主播或 clientRoleAudience观众
clientRoleType: ClientRoleType.clientRoleBroadcaster,
),
);
// 开启本地预览
await _engine!.startPreview();
WakelockPlus.enable();
final status = await _engine!.getConnectionState();
if (status == ConnectionStateType.connectionStateDisconnected) {
//加入频道
await _engine!.joinChannel(
token: _ws.rtcToken!.token,
channelId: _ws.rtcToken!.channel,
uid: _ws.rtcToken!.uid,
options: ChannelMediaOptions(
// 自动订阅所有视频流
autoSubscribeVideo: true,
// 自动订阅所有音频流
autoSubscribeAudio: true,
// 发布摄像头采集的视频
publishCameraTrack: true,
// 发布麦克风采集的音频
publishMicrophoneTrack: true,
// 设置用户角色为 clientRoleBroadcaster主播或 clientRoleAudience观众
clientRoleType: ClientRoleType.clientRoleBroadcaster,
),
);
}
}
///开始链接房间
@@ -94,7 +108,30 @@ class StuRoomVM extends ChangeNotifier {
if (msg.event == RoomEvent.changeUser) {
final list = RoomUserDto.listFromJson(msg.data['user_list']);
onStudentChange(list);
onRoomStartStatus(RoomTypeDto.fromJson(msg.data['room_info']));
onRoomStartStatus(RoomInfoDto.fromJson(msg.data['room_info']));
} else if (msg.event == RoomEvent.closeStudentCamera) {
changeCameraSwitch(fromServer: false, value: false);
} else if (msg.event == RoomEvent.closeStudentMic) {
changeMicSwitch(fromServer: false, value: false);
} else if ([
RoomEvent.closeStudentSpeaker,
RoomEvent.openStudentSpeaker,
].contains(msg.event)) {
//控制扬声器
changeSpeakerSwitch(
value: msg.event == RoomEvent.openStudentSpeaker,
fromServer: false,
);
} else if (msg.event == RoomEvent.openStudentMic) {
EasyLoading.showToast("老师请求打开麦克风");
// 打开麦克风
} else if (msg.event == RoomEvent.openStudentCamera) {
EasyLoading.showToast("老师请求打开摄像头");
// 打开摄像头
} else if (msg.event == RoomEvent.clearHandUp) {
changeHandSwitch();
} else if (msg.event == RoomEvent.closeRoom) {
_closeRoom();
}
});
}
@@ -112,6 +149,10 @@ class StuRoomVM extends ChangeNotifier {
newList.add(t);
} else {
selfInfo = t;
//同步声网的状态
changeCameraSwitch(value: selfInfo!.cameraStatus == 1, fromServer: false);
changeMicSwitch(value: selfInfo!.microphoneStatus == 1, fromServer: false);
changeSpeakerSwitch(value: selfInfo!.speekerStatus == 1, fromServer: false);
}
}
}
@@ -120,52 +161,111 @@ class StuRoomVM extends ChangeNotifier {
}
///设置房间开启状态
void onRoomStartStatus(RoomTypeDto roomInfo) {
roomStatus = roomInfo.roomStatus;
void onRoomStartStatus(RoomInfoDto info) {
roomInfo = roomInfo.copyWith(
roomStatus: info.roomStatus,
actualStartTime: info.roomStartTime,
boardUuid: info.boardUuid,
);
//开启摄像头
if (roomInfo.roomStatus == 1) {
_initRtc();
}
notifyListeners();
}
///控制摄像头开关
void changeCameraSwitch() {
bool isOpen = selfInfo!.cameraStatus == 1;
selfInfo!.cameraStatus = isOpen ? 0 : 1;
//发送指令
_ws.send(RoomCommand.studentActon, {
"mute_type": "camera",
"is_mute": isOpen ? 1 : 0,
});
/// - [value] 摄像头状态true为开启false为关闭
/// - [fromServer] 发送指令给服务器默认true
void changeCameraSwitch({
required bool value,
bool fromServer = true,
}) {
//改变后的操作状态,true表示开false关
selfInfo!.cameraStatus = value ? 1 : 0;
// //发送指令
if (fromServer) {
_ws.send(RoomCommand.studentActon, {
"mute_type": "camera",
"is_mute": value ? 0 : 1,
});
}
_engine?.enableLocalVideo(value);
notifyListeners();
}
///控制麦克风开关
void changeMicSwitch() {
bool isOpen = selfInfo!.microphoneStatus == 1;
selfInfo!.microphoneStatus = isOpen ? 0 : 1;
print(selfInfo!.microphoneStatus);
/// - [value] 麦克风状态true为开启false为关闭
/// - [fromServer] 默认为true发送指令给服务器
void changeMicSwitch({required bool value, bool fromServer = true}) {
selfInfo!.microphoneStatus = value ? 1 : 0;
//发送指令
_ws.send(RoomCommand.studentActon, {
"mute_type": "microphone",
"is_mute": isOpen ? 1 : 0,
});
if (fromServer) {
_ws.send(RoomCommand.studentActon, {
"mute_type": "microphone",
"is_mute": value ? 0 : 1,
});
}
_engine?.enableLocalAudio(value);
notifyListeners();
}
/// 控制扬声器开关
void changeSpeakerSwitch() {
bool isOpen = selfInfo!.speekerStatus == 1;
selfInfo!.speekerStatus = isOpen ? 0 : 1;
/// - [value] 扬声器状态true为开启false为关闭
/// - [fromServer] 默认为true发送指令给服务器
void changeSpeakerSwitch({required bool value, bool fromServer = true}) {
//操作后是否是开启状态
selfInfo!.speekerStatus = value ? 1 : 0;
//发送指令
_ws.send(RoomCommand.studentActon, {
"mute_type": "speeker",
"is_mute": isOpen ? 1 : 0,
});
if (fromServer) {
_ws.send(RoomCommand.studentActon, {
"mute_type": "speeker",
"is_mute": value ? 0 : 1,
});
}
_engine?.muteAllRemoteAudioStreams(!value);
notifyListeners();
}
///控制举手
void changeHandSwitch({bool fromServer = true}) {
bool nextOpen = handClose;
selfInfo!.handup = nextOpen ? 1 : 0;
if (fromServer) {
_ws.send(RoomCommand.handUp, {
'is_handup': nextOpen ? 1 : 0,
});
}
notifyListeners();
}
///上传文件
void uploadFile(List<String> files) {
selfInfo?.filesList.addAll(files);
_ws.send(RoomCommand.uploadFile, {
"files": selfInfo!.filesList,
});
}
///自习室关闭
void _closeRoom() {
roomInfo.roomStatus = 2;
_dispose();
notifyListeners();
}
///销毁
void _dispose() {
_engine?.leaveChannel();
_engine?.release();
_sub?.cancel();
_ws.dispose();
WakelockPlus.disable();
}
@override
void dispose() {
super.dispose();
_sub?.cancel();
_ws.dispose();
_dispose();
}
}