From 30625f41ee823ad37155e6860a697cf84e15c428 Mon Sep 17 00:00:00 2001 From: yj <1336058017@qq.com> Date: Wed, 17 Jul 2024 11:19:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E4=BA=AB=E5=B1=8F=E5=B9=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/package/agora.ts | 60 +++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/src/utils/package/agora.ts b/src/utils/package/agora.ts index 13ddb04..82e7e45 100644 --- a/src/utils/package/agora.ts +++ b/src/utils/package/agora.ts @@ -17,6 +17,7 @@ const option: any = { token: '', channelId: '', userid: '', + sourceType: VideoSourceType.VideoSourceCameraPrimary, } let iMediaRecorder: any = ''; let rtcEngine: any = ''; @@ -77,8 +78,7 @@ const agora = { setupLocalVideo: (item: any) => { rtcEngine.setupLocalVideo({ renderMode: RenderModeType.RenderModeFit, - sourceType: VideoSourceType.VideoSourceCameraPrimary, - // sourceType: option.isCamera ? VideoSourceType.VideoSourceCameraPrimary : VideoSourceType.VideoSourceScreen, + sourceType: option.sourceType, uid: item.account, view: item.view, setupMode: VideoViewSetupMode.VideoViewSetupAdd, @@ -116,29 +116,16 @@ const agora = { storage.setItem('isJoin', false) }, // 加入频道 - joinChannel: (bool: boolean) => { - // false 摄像头 true 共享屏幕 - if (bool) { - rtcEngine.joinChannel(option.token, option.channelId, option.userid, { - channelProfile: ChannelProfileType.ChannelProfileLiveBroadcasting, //设置频道场景为直播场景 - clientRoleType: ClientRoleType.ClientRoleBroadcaster, //用户角色 1主播 2观众 - publishMicrophoneTrack: true, //设置是否发布麦克风采集到的音频 - publishCameraTrack: false, //设置是否发布摄像头采集的视频 - publishScreenTrack: true, //设置是否发布屏幕采集的视频 - autoSubscribeAudio: true, //设置是否自动订阅所有音频流 - autoSubscribeVideo: true, //设置是否自动订阅所有视频流 - }); - } else { - rtcEngine.joinChannel(option.token, option.channelId, option.userid, { - channelProfile: ChannelProfileType.ChannelProfileLiveBroadcasting, //设置频道场景为直播场景 - clientRoleType: ClientRoleType.ClientRoleBroadcaster, //设置用户角色为主播;如果要将用户角色设置为观众,保持默认值即可 - publishMicrophoneTrack: true, //发布麦克风采集的音频 - publishCameraTrack: true, //发布摄像头采集的视频 - publishScreenTrack: false, //设置是否发布屏幕采集的视频 - autoSubscribeAudio: true, //自动订阅所有音频流 - autoSubscribeVideo: true, //自动订阅所有视频流 - }); - } + joinChannel: () => { + rtcEngine.joinChannel(option.token, option.channelId, option.userid, { + channelProfile: ChannelProfileType.ChannelProfileLiveBroadcasting, //设置频道场景为直播场景 + clientRoleType: ClientRoleType.ClientRoleBroadcaster, //用户角色 1主播 2观众 + publishMicrophoneTrack: true, //设置是否发布麦克风采集到的音频 + publishCameraTrack: true, //设置是否发布摄像头采集的视频 + publishScreenTrack: true, //设置是否发布屏幕采集的视频 + autoSubscribeAudio: true, //设置是否自动订阅所有音频流 + autoSubscribeVideo: true, //设置是否自动订阅所有视频流 + }); }, // 停止共享屏幕 stopScreenCapture: () => { @@ -154,6 +141,16 @@ const agora = { }, // 摄像头采集 setCameraCapture: (sourceType: number) => { + agora.stopScreenCapture() + if (sourceType !== option.sourceType) { + rtcEngine.destroyRendererByConfig(option.sourceType, option.channelId, option.account) + option.sourceType = sourceType + agora.setupLocalVideo({ + account: Number(option.userid), + view: document.getElementById(`video-${option.userid}`), + channelId: option.channelId, + }) + } rtcEngine.startCameraCapture(VideoSourceType.VideoSourceCamera, {}) }, // 加入频道 @@ -161,7 +158,7 @@ const agora = { option.token = data.token; option.channelId = data.channelId; option.userid = Number(data.userid); - agora.joinChannel(false) + agora.joinChannel() }, // 桌面捕获音频和视频的媒体源的信息 getDesktopCapturerVideo: async () => { @@ -169,7 +166,16 @@ const agora = { }, // 共享屏幕采集 setDesktopCapturerVideo: (targetSource: any, sourceType: number) => { - // rtcEngine.stopCameraCapture(VideoSourceType.VideoSourceCamera); + if (sourceType !== option.sourceType) { + rtcEngine.stopCameraCapture(option.sourceType) + rtcEngine.destroyRendererByConfig(option.sourceType, option.channelId, option.account) + option.sourceType = sourceType + agora.setupLocalVideo({ + account: Number(option.userid), + view: document.getElementById(`video-${option.userid}`), + channelId: option.channelId, + }) + } agora.stopScreenCapture(); if ( targetSource.type ===