330 lines
12 KiB
TypeScript
330 lines
12 KiB
TypeScript
import {
|
|
createAgoraRtcEngine,
|
|
ClientRoleType,
|
|
VideoSourceType,
|
|
VideoViewSetupMode,
|
|
ScreenCaptureSourceType,
|
|
RenderModeType,
|
|
ChannelProfileType
|
|
} from "agora-electron-sdk";
|
|
import { GetRoomRtcToken } from "@/api/Home/Index";
|
|
import { storage } from '@/utils';
|
|
const option: any = {
|
|
appId: 'dcfc466a6ecb4a1f972630065dfb1e75',
|
|
token: '',
|
|
channelId: '',
|
|
uid: ''
|
|
}
|
|
let rtcEngine: any = '';
|
|
|
|
const agora = {
|
|
// 初始化
|
|
init: async (bool: boolean = false) => {
|
|
rtcEngine = createAgoraRtcEngine();
|
|
await rtcEngine.initialize({
|
|
appId: option.appId,
|
|
});
|
|
if (bool) {
|
|
const setting = JSON.parse(storage.getItem('setting') as string)
|
|
if (setting.videoDeviceId) {
|
|
agora.getVideoDeviceManager().then(async (res) => {
|
|
let item = res.list.find((item: any) => item.deviceId === setting.videoDeviceId);
|
|
if (item) {
|
|
agora.setVideoDeviceManager(setting.videoDeviceId) //通过设备 ID 指定视频采集设备。
|
|
} else {
|
|
agora.setVideoDeviceManager(rtcEngine.getVideoDeviceManager().getDevice())
|
|
}
|
|
})
|
|
} else {
|
|
agora.setVideoDeviceManager(rtcEngine.getVideoDeviceManager().getDevice())
|
|
}
|
|
if (setting.playBackDeviceId) agora.setPlaybackDevice(setting.playBackDeviceId) //指定播放设备
|
|
if (setting.playBackVolume) agora.setPlaybackDeviceVolume(setting.playBackVolume) // 设置播放设备音量
|
|
if (setting.ecordingDeviceId) agora.setRecordingDevice(setting.ecordingDeviceId) // 设置音频采集设备
|
|
if (setting.ecordingVolume) agora.setRecordingDeviceVolume(setting.ecordingVolume) // 设置音频设备音量
|
|
}
|
|
},
|
|
// 事件回调
|
|
registerEventHandler: ({ onJoinChannelSuccess, onUserJoined, onUserOffline, onAudioVolumeIndication }: any) => {
|
|
rtcEngine.registerEventHandler({
|
|
// 监听本地用户加入频道事件
|
|
onJoinChannelSuccess: async (info: any, elapsed: any) => {
|
|
await onJoinChannelSuccess(info, elapsed)
|
|
},
|
|
// 监听远端用户加入频道事件
|
|
onUserJoined: async (info: any, remoteUid: any, elapsed: any) => {
|
|
await onUserJoined(info, remoteUid, elapsed)
|
|
},
|
|
// 监听用户离开频道事件
|
|
onUserOffline: async (info: any, remoteUid: any, reason: any) => {
|
|
await onUserOffline(info, remoteUid, reason)
|
|
},
|
|
// // 视频发布状态改变回调
|
|
// onVideoPublishStateChanged: (source: any, channel: any, oldState: any, newState: any, elapseSinceLastState: any) => {
|
|
// if (newState === 1) {
|
|
|
|
// }
|
|
// },
|
|
// // 音频发布状态改变回调
|
|
// onAudioPublishStateChanged: (channel: any, oldState: any, newState: any, elapseSinceLastState: any) => {
|
|
// if (newState === 1) {
|
|
|
|
// }
|
|
// },
|
|
// // 用户音量提示回调。
|
|
onAudioVolumeIndication: async (_connection: any, _speakers: any, _speakerNumber: any, totalVolume: any,) => {
|
|
const percentage = (totalVolume / 255) * 100
|
|
await onAudioVolumeIndication(percentage)
|
|
}
|
|
});
|
|
},
|
|
// 获取视图模式
|
|
getRrenderMode: (uid: number) => {
|
|
if (String(uid).length === 9) {
|
|
return RenderModeType.RenderModeFit
|
|
} else {
|
|
return RenderModeType.RenderModeHidden
|
|
}
|
|
},
|
|
// 本地加入
|
|
setupLocalVideo: async (item: any) => {
|
|
if (item.view?.childNodes.length === 1) {
|
|
await rtcEngine.setupLocalVideo({
|
|
renderMode: agora.getRrenderMode(item.uid),
|
|
sourceType: item.sourceType,
|
|
uid: item.uid,
|
|
view: item.view,
|
|
setupMode: VideoViewSetupMode.VideoViewSetupAdd,
|
|
});
|
|
}
|
|
},
|
|
// 远端加入
|
|
setupRemoteVideoJoin: async (item: any) => {
|
|
if (item.view?.childNodes.length === 1) {
|
|
await rtcEngine.setupRemoteVideo(
|
|
{
|
|
renderMode: agora.getRrenderMode(item.uid),
|
|
sourceType: VideoSourceType.VideoSourceRemote,
|
|
uid: item.uid,
|
|
view: item.view,
|
|
setupMode: VideoViewSetupMode.VideoViewSetupAdd,
|
|
},
|
|
{ channelId: item.channelId },
|
|
);
|
|
}
|
|
},
|
|
// 退出
|
|
setupRemoteVideo: async (item: any) => {
|
|
await rtcEngine.setupRemoteVideo(
|
|
{
|
|
renderMode: agora.getRrenderMode(item.uid),
|
|
sourceType: VideoSourceType.VideoSourceRemote,
|
|
uid: item.uid,
|
|
view: item.view,
|
|
setupMode: VideoViewSetupMode.VideoViewSetupRemove,
|
|
},
|
|
);
|
|
},
|
|
// 销毁
|
|
release: () => {
|
|
rtcEngine.release()
|
|
},
|
|
// 离开频道
|
|
leaveChannel: async () => {
|
|
await rtcEngine.leaveChannel({
|
|
stopAudioMixing: true,
|
|
stopAllEffect: true,
|
|
stopMicrophoneRecording: true,
|
|
})
|
|
agora.stopScreenCapture()
|
|
agora.release()
|
|
},
|
|
// 加入频道
|
|
joinChannel: () => {
|
|
rtcEngine.joinChannel(option.token, option.channelId, option.uid, {
|
|
autoSubscribeAudio: true,//设置是否自动订阅所有音频流
|
|
autoSubscribeVideo: true,//设置是否自动订阅所有视频流
|
|
publishMicrophoneTrack: true,//设置是否发布麦克风采集到的音频
|
|
publishCameraTrack: true,//设置是否发布摄像头采集的视频
|
|
clientRoleType: ClientRoleType.ClientRoleBroadcaster, //用户角色 ClientRoleBroadcaster 主播 ClientRoleAudience 观众
|
|
publishScreenTrack: false,//设置是否发布屏幕采集的视频
|
|
});
|
|
},
|
|
// 共享屏幕单独用户
|
|
joinChannelEx: async (uid: any) => {
|
|
await agora.leaveChannelEx(uid)
|
|
await rtcEngine.joinChannelEx(
|
|
option.token,
|
|
{ channelId: option.channelId, localUid: Number(uid) },
|
|
{
|
|
autoSubscribeAudio: false,//设置是否自动订阅所有音频流
|
|
autoSubscribeVideo: false,//设置是否自动订阅所有视频流
|
|
publishMicrophoneTrack: false,//设置是否发布麦克风采集到的音频
|
|
publishCameraTrack: false,//设置是否发布摄像头采集的视频
|
|
clientRoleType: ClientRoleType.ClientRoleBroadcaster,//用户角色 ClientRoleBroadcaster 主播 ClientRoleAudience 观众
|
|
publishScreenTrack: true,//设置是否发布屏幕采集的视频
|
|
}
|
|
);
|
|
},
|
|
// 离开共享屏幕频道
|
|
leaveChannelEx: async (uid: any) => {
|
|
await rtcEngine.leaveChannelEx({ channelId: option.channelId, localUid: Number(uid) })
|
|
},
|
|
// 停止共享屏幕
|
|
stopScreenCapture: () => {
|
|
rtcEngine.stopScreenCapture();
|
|
rtcEngine.enableLoopbackRecording(false)
|
|
},
|
|
// 取消或恢复发布本地音频流
|
|
muteLocalAudioStream: (mute: any) => {
|
|
rtcEngine.muteLocalAudioStream(mute)
|
|
},
|
|
// 取消或恢复发布本地视频流
|
|
muteLocalVideoStream: (mute: any) => {
|
|
rtcEngine.muteLocalVideoStream(mute)
|
|
},
|
|
// 摄像头采集
|
|
startCameraCapture: async () => {
|
|
await rtcEngine.startCameraCapture(VideoSourceType.VideoSourceCamera, {})
|
|
},
|
|
// 停止采集摄像头
|
|
stopCameraCapture: async () => {
|
|
await rtcEngine.stopCameraCapture()
|
|
},
|
|
// 加入频道
|
|
setJoinChannel: async (data: any) => {
|
|
option.token = data.token;
|
|
option.channelId = data.channelId;
|
|
option.uid = Number(data.uid);
|
|
agora.joinChannel()
|
|
},
|
|
// 桌面捕获音频和视频的媒体源的信息
|
|
getDesktopCapturerVideo: async () => {
|
|
return rtcEngine.getScreenCaptureSources({ width: 300, height: 300 }, { width: 300, height: 300 }, true);
|
|
},
|
|
// 共享屏幕采集
|
|
setDesktopCapturerVideo: async (targetSource: any, isComputerAudio: boolean, isFluencyPriority: boolean) => {
|
|
const user = JSON.parse(storage.getItem('user') as string)
|
|
agora.stopScreenCapture();
|
|
if (isComputerAudio) {
|
|
rtcEngine.enableLoopbackRecording(true)
|
|
}
|
|
let data = {};
|
|
if (isFluencyPriority) {
|
|
data = {
|
|
frameRate: 15,
|
|
dimensions: {
|
|
window: 3000,
|
|
height: 3000,
|
|
}
|
|
}
|
|
}
|
|
if (
|
|
targetSource.type ===
|
|
ScreenCaptureSourceType.ScreencapturesourcetypeScreen
|
|
) {
|
|
rtcEngine.startScreenCaptureByDisplayId(
|
|
targetSource.sourceId,
|
|
{},
|
|
{
|
|
windowFocus: true,
|
|
enableHighLight: true,
|
|
highLightColor: 0xFF99CC00,
|
|
...data
|
|
}
|
|
);
|
|
} else {
|
|
rtcEngine.startScreenCaptureByWindowId(
|
|
targetSource.sourceId,
|
|
{},
|
|
{
|
|
windowFocus: true,
|
|
enableHighLight: true,
|
|
highLightColor: 0xFF99CC00,
|
|
...data
|
|
}
|
|
);
|
|
}
|
|
await agora.joinChannelEx(user.screenShareId)
|
|
},
|
|
// 获取系统中所有的视频设备列表。
|
|
getVideoDeviceManager: async (): Promise<any> => {
|
|
return {
|
|
list: rtcEngine.getVideoDeviceManager().enumerateVideoDevices(),
|
|
item: rtcEngine.getVideoDeviceManager().getDevice()
|
|
}
|
|
},
|
|
// 通过设备 ID 指定视频采集设备。
|
|
setVideoDeviceManager: async (deviceIdUTF8: string) => {
|
|
await rtcEngine.getVideoDeviceManager().setDevice(deviceIdUTF8)
|
|
},
|
|
// 开启本地视频预览
|
|
startPreview: async (id: string, uid: number): Promise<void> => {
|
|
rtcEngine.enableVideo();
|
|
rtcEngine.startPreview();
|
|
await GetRoomRtcToken(`${+new Date()}`).then(async (res) => {
|
|
await rtcEngine.joinChannelEx(res.data, {
|
|
channelId: `${+new Date()}`,
|
|
localUid: uid,
|
|
}, {
|
|
channelProfile: ChannelProfileType.ChannelProfileLiveBroadcasting,
|
|
clientRoleType: ClientRoleType.ClientRoleBroadcaster,
|
|
publishMicrophoneTrack: true,
|
|
publishCameraTrack: true,
|
|
autoSubscribeAudio: true,
|
|
autoSubscribeVideo: true,
|
|
});
|
|
rtcEngine.setupLocalVideo({
|
|
sourceType: VideoSourceType.VideoSourceCameraPrimary,
|
|
uid,
|
|
view: document.getElementById(id),
|
|
setupMode: VideoViewSetupMode.VideoViewSetupAdd,
|
|
});
|
|
})
|
|
},
|
|
// 获取输入输出设备列表
|
|
getAudioMediaList: async () => {
|
|
return {
|
|
playBackList: rtcEngine.getAudioDeviceManager().enumeratePlaybackDevices(),
|
|
ecordingList: rtcEngine.getAudioDeviceManager().enumerateRecordingDevices(),
|
|
playBackItem: rtcEngine.getAudioDeviceManager().getPlaybackDefaultDevice(),
|
|
ecordingItem: rtcEngine.getAudioDeviceManager().getRecordingDefaultDevice(),
|
|
ecordingVolume: rtcEngine.getAudioDeviceManager().getRecordingDeviceVolume(),
|
|
}
|
|
},
|
|
// 启动音频播放设备测试。
|
|
startPlaybackDeviceTest: async () => {
|
|
await rtcEngine.getAudioDeviceManager().startPlaybackDeviceTest('https://wgshare.oss-cn-chengdu.aliyuncs.com/TestAudio.mp3')
|
|
},
|
|
// 停止音频播放设备测试。
|
|
stopPlaybackDeviceTest: async () => {
|
|
await rtcEngine.getAudioDeviceManager().stopPlaybackDeviceTest()
|
|
},
|
|
// 设置播放设备音量
|
|
setPlaybackDeviceVolume: async (volume: number) => {
|
|
await rtcEngine.getAudioDeviceManager().setPlaybackDeviceVolume(volume)
|
|
},
|
|
// 指定播放设备
|
|
setPlaybackDevice: async (deviceId: string) => {
|
|
await rtcEngine.getAudioDeviceManager().setPlaybackDevice(deviceId)
|
|
},
|
|
// 启动音频采集设备测试
|
|
startRecordingDeviceTest: async (indicationInterval: number) => {
|
|
await rtcEngine.getAudioDeviceManager().startRecordingDeviceTest(indicationInterval)
|
|
},
|
|
// 设置音频设备音量
|
|
setRecordingDeviceVolume: async (volume: number) => {
|
|
await rtcEngine.getAudioDeviceManager().setRecordingDeviceVolume(volume)
|
|
},
|
|
// 设置音频采集设备
|
|
setRecordingDevice: async (deviceId: string) => {
|
|
await rtcEngine.getAudioDeviceManager().setRecordingDevice(deviceId)
|
|
},
|
|
// 停止音频采集设备测试
|
|
stopRecordingDeviceTest: async () => {
|
|
await rtcEngine.getAudioDeviceManager().stopRecordingDeviceTest()
|
|
},
|
|
|
|
}
|
|
|
|
export default agora; |