Browse Source

feat(classroom): 优化RTC设备控制逻辑

- 增加未加入课堂时的错误提示
- 重构摄像头和屏幕分享切换逻辑,减少频道操作
- 使用底层API直接控制视频流
- 添加5秒延迟推流机制
yourname 7 months ago
parent
commit
94b73d4c85
1 changed files with 49 additions and 27 deletions
  1. 49 27
      client/mobile/pages_classroom.tsx

+ 49 - 27
client/mobile/pages_classroom.tsx

@@ -780,24 +780,37 @@ export const ClassroomPage = () => {
 
   // 切换摄像头状态
   const toggleCamera = async () => {
+    if(!aliRtcEngine.current?.isInCall){
+      showToast('error', '先加入课堂');
+      return;
+    }
+
     try {
       if (isCameraOn) {
-        await leaveRtcChannel();
-        await joinRtcChannel(classId, {
-          publishVideo: false,
-          publishAudio: isAudioOn,
-          publishScreen: isScreenSharing
-        });
+        // await leaveRtcChannel();
+        // await joinRtcChannel(classId, {
+        //   publishVideo: false,
+        //   publishAudio: isAudioOn,
+        //   publishScreen: isScreenSharing
+        // });
         await aliRtcEngine.current?.stopPreview();
+        await aliRtcEngine.current?.enableLocalVideo(false)
+        await aliRtcEngine.current?.publishLocalVideoStream(false)
       } else {
-        await leaveRtcChannel();
-        await joinRtcChannel(classId, {
-          publishVideo: true,
-          publishAudio: isAudioOn,
-          publishScreen: isScreenSharing
-        });
+        // await leaveRtcChannel();
+        // await joinRtcChannel(classId, {
+        //   publishVideo: true,
+        //   publishAudio: isAudioOn,
+        //   publishScreen: isScreenSharing
+        // });
         await aliRtcEngine.current?.setLocalViewConfig('localPreviewer', AliRtcVideoTrack.AliRtcVideoTrackCamera);
+        await aliRtcEngine.current?.enableLocalVideo(true)
         await aliRtcEngine.current?.startPreview();
+        setTimeout(()=>{
+          showToast('info', '开始推流');
+          aliRtcEngine.current?.publishLocalVideoStream(true)
+          aliRtcEngine.current?.startAndPublishDefaultDevices()
+        },1000 * 5)
       }
       setIsCameraOn(!isCameraOn);
     } catch (err) {
@@ -824,23 +837,31 @@ export const ClassroomPage = () => {
 
   // 切换屏幕分享状态
   const toggleScreenShare = async () => {
+    if(!aliRtcEngine.current?.isInCall){
+      showToast('error', '先加入课堂');
+      return;
+    }
+
     try {
       if (isScreenSharing) {
-        await leaveRtcChannel();
-        await joinRtcChannel(classId, {
-          publishVideo: isCameraOn,
-          publishAudio: isAudioOn,
-          publishScreen: false
-        });
-        await aliRtcEngine.current?.stopPreviewScreen();
+        // await leaveRtcChannel();
+        // await joinRtcChannel(classId, {
+        //   publishVideo: isCameraOn,
+        //   publishAudio: isAudioOn,
+        //   publishScreen: false
+        // });
+        // await aliRtcEngine.current?.stopPreviewScreen();
+        await aliRtcEngine.current?.publishLocalScreenShareStream(false)
+        await aliRtcEngine.current?.stopScreenShare()
       } else {
-        await leaveRtcChannel();
-        // 设置屏幕分享预览视图
-        await joinRtcChannel(classId, {
-          publishVideo: isCameraOn,
-          publishAudio: isAudioOn,
-          publishScreen: true
-        });
+        // await leaveRtcChannel();
+        // // 设置屏幕分享预览视图
+        // await joinRtcChannel(classId, {
+        //   publishVideo: isCameraOn,
+        //   publishAudio: isAudioOn,
+        //   publishScreen: true
+        // });
+        await aliRtcEngine.current?.publishLocalScreenShareStream(true)
         await aliRtcEngine.current?.setLocalViewConfig(
           'screenPreviewer',
           AliRtcVideoTrack.AliRtcVideoTrackScreen
@@ -850,7 +871,8 @@ export const ClassroomPage = () => {
         //   videoTrack: undefined,  // 使用默认视频轨道
         //   audioTrack: undefined   // 使用默认音频轨道
         // });
-        await aliRtcEngine.current?.startPreviewScreen()
+        // await aliRtcEngine.current?.startPreviewScreen()
+        // await aliRtcEngine.current?.startScreenShare()
       }
       setIsScreenSharing(!isScreenSharing);
     } catch (err) {