|
@@ -3,8 +3,9 @@ import AgoraRTC, { IAgoraRTCClient, ILocalAudioTrack, ILocalVideoTrack } from 'a
|
|
import { useRtcStore } from '/@/store/modules/rtc';
|
|
import { useRtcStore } from '/@/store/modules/rtc';
|
|
export type AgoraClient = IAgoraRTCClient;
|
|
export type AgoraClient = IAgoraRTCClient;
|
|
export { ILocalAudioTrack, ILocalVideoTrack } from 'agora-rtc-sdk-ng';
|
|
export { ILocalAudioTrack, ILocalVideoTrack } from 'agora-rtc-sdk-ng';
|
|
-import { useRtcSdk } from './useTRTC';
|
|
|
|
|
|
+// import { useRtcSdk } from './useTRTC';
|
|
import { ref } from 'vue';
|
|
import { ref } from 'vue';
|
|
|
|
+import { muteVideoLeader } from './useTRTC';
|
|
|
|
|
|
export let localAudioTrack: ILocalAudioTrack;
|
|
export let localAudioTrack: ILocalAudioTrack;
|
|
export let localVideoTrack: ILocalVideoTrack;
|
|
export let localVideoTrack: ILocalVideoTrack;
|
|
@@ -21,13 +22,18 @@ export const createAgoraClient = async (
|
|
userId: string,
|
|
userId: string,
|
|
): Promise<IAgoraRTCClient> => {
|
|
): Promise<IAgoraRTCClient> => {
|
|
const rtcStore = useRtcStore();
|
|
const rtcStore = useRtcStore();
|
|
- const { muteVideoLeader } = useRtcSdk();
|
|
|
|
|
|
+ // const { muteVideoLeader } = useRtcSdk();
|
|
|
|
|
|
const isOK = AgoraRTC.checkSystemRequirements();
|
|
const isOK = AgoraRTC.checkSystemRequirements();
|
|
if (!isOK) {
|
|
if (!isOK) {
|
|
console.log('当前不支持');
|
|
console.log('当前不支持');
|
|
}
|
|
}
|
|
- const client: IAgoraRTCClient = AgoraRTC.createClient({ mode: 'rtc', codec: 'vp9' });
|
|
|
|
|
|
+
|
|
|
|
+ AgoraRTC.setLogLevel(4);
|
|
|
|
+
|
|
|
|
+ const role = rtcStore.role === 'leader' ? 'host' : 'audience';
|
|
|
|
+ const client: IAgoraRTCClient = AgoraRTC.createClient({ mode: 'rtc', codec: 'vp9', role: role });
|
|
|
|
+
|
|
localClient = client;
|
|
localClient = client;
|
|
const uid = await client.join(String(sdkAppId), roomId, sign, userId);
|
|
const uid = await client.join(String(sdkAppId), roomId, sign, userId);
|
|
userID.value = userId;
|
|
userID.value = userId;
|
|
@@ -39,7 +45,7 @@ export const createAgoraClient = async (
|
|
});
|
|
});
|
|
});
|
|
});
|
|
client.on('exception', (error) => {
|
|
client.on('exception', (error) => {
|
|
- console.error(error);
|
|
|
|
|
|
+ console.error('sdk error', error);
|
|
});
|
|
});
|
|
client.on('user-joined', (user) => {
|
|
client.on('user-joined', (user) => {
|
|
console.log('userJoin', user);
|
|
console.log('userJoin', user);
|
|
@@ -47,62 +53,95 @@ export const createAgoraClient = async (
|
|
client.on('user-info-updated', (user) => {
|
|
client.on('user-info-updated', (user) => {
|
|
console.log('userJoin', user);
|
|
console.log('userJoin', user);
|
|
});
|
|
});
|
|
|
|
+ //取消订阅
|
|
client.on('user-unpublished', async (user, mediaType) => {
|
|
client.on('user-unpublished', async (user, mediaType) => {
|
|
|
|
+ // await client.unsubscribe(user, mediaType);
|
|
if (mediaType === 'audio') {
|
|
if (mediaType === 'audio') {
|
|
- console.error('unpublished', user);
|
|
|
|
|
|
+ console.error('unpublished-audio', user);
|
|
}
|
|
}
|
|
if (mediaType === 'video') {
|
|
if (mediaType === 'video') {
|
|
- // console.error('unpublished', user);
|
|
|
|
//暂时只有leader有video
|
|
//暂时只有leader有video
|
|
- muteVideoLeader.value = true;
|
|
|
|
|
|
+ const remoteVideoEl = document.getElementById(`cameraRemoteBox`);
|
|
|
|
+ if (remoteVideoEl) {
|
|
|
|
+ remoteVideoEl.style.display = 'none';
|
|
|
|
+ }
|
|
|
|
+ const leader = rtcStore.getRoomLeader();
|
|
|
|
+ console.error('unpublished-video', leader, user.uid === leader?.Id);
|
|
|
|
+ if (user.uid === leader?.Id) {
|
|
|
|
+ }
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
+ //发布订阅
|
|
client.on('user-published', async (user, mediaType) => {
|
|
client.on('user-published', async (user, mediaType) => {
|
|
|
|
+ console.warn('远程音视频-published', user, mediaType);
|
|
await client.subscribe(user, mediaType);
|
|
await client.subscribe(user, mediaType);
|
|
if (mediaType === 'video') {
|
|
if (mediaType === 'video') {
|
|
console.error('subscribe video success', user);
|
|
console.error('subscribe video success', user);
|
|
const remoteVideoEl = document.getElementById(`cameraRemoteBox`);
|
|
const remoteVideoEl = document.getElementById(`cameraRemoteBox`);
|
|
- if (user.videoTrack && remoteVideoEl) {
|
|
|
|
|
|
+ if (user.videoTrack && user.hasVideo && remoteVideoEl) {
|
|
// remoteVideoEl.style.display = 'block';
|
|
// remoteVideoEl.style.display = 'block';
|
|
- console.warn('video play');
|
|
|
|
|
|
+ remoteVideoEl.style.display = 'block';
|
|
|
|
+ console.warn(`收到远程视频-->当前用ID${user.uid}`);
|
|
user.videoTrack.play(remoteVideoEl);
|
|
user.videoTrack.play(remoteVideoEl);
|
|
- muteVideoLeader.value = false;
|
|
|
|
|
|
+ // muteVideoLeader.value = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (mediaType === 'audio') {
|
|
if (mediaType === 'audio') {
|
|
|
|
+ console.warn(`收到远程音频-->当前用ID${user.uid}`);
|
|
user.audioTrack?.play();
|
|
user.audioTrack?.play();
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
+
|
|
try {
|
|
try {
|
|
isHasCamera.value = (await AgoraRTC.getCameras()).length > 0;
|
|
isHasCamera.value = (await AgoraRTC.getCameras()).length > 0;
|
|
isHasMicrophone.value = (await AgoraRTC.getMicrophones()).length > 0;
|
|
isHasMicrophone.value = (await AgoraRTC.getMicrophones()).length > 0;
|
|
|
|
|
|
- isHasCamera.value && (localVideoTrack = await AgoraRTC.createCameraVideoTrack());
|
|
|
|
|
|
+ if (isHasCamera.value) {
|
|
|
|
+ localVideoTrack = await AgoraRTC.createCameraVideoTrack();
|
|
|
|
+ localVideoTrack.setEncoderConfiguration('120p');
|
|
|
|
+ }
|
|
|
|
+
|
|
isHasMicrophone.value && (localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack());
|
|
isHasMicrophone.value && (localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack());
|
|
} catch (error) {}
|
|
} catch (error) {}
|
|
|
|
|
|
const localVideoEl = document.getElementById(`camera_box_${userId}`);
|
|
const localVideoEl = document.getElementById(`camera_box_${userId}`);
|
|
console.warn('isHasCamera', isHasCamera.value);
|
|
console.warn('isHasCamera', isHasCamera.value);
|
|
console.warn('isHasMicrophone', isHasMicrophone.value);
|
|
console.warn('isHasMicrophone', isHasMicrophone.value);
|
|
|
|
+
|
|
if (rtcStore.role === 'leader') {
|
|
if (rtcStore.role === 'leader') {
|
|
if (isHasCamera.value) {
|
|
if (isHasCamera.value) {
|
|
rtcStore.setHasCamera();
|
|
rtcStore.setHasCamera();
|
|
if (localVideoEl) {
|
|
if (localVideoEl) {
|
|
- localVideoTrack.play(localVideoEl);
|
|
|
|
|
|
+ try {
|
|
|
|
+ console.warn('播放本地rtc-localVideoTrack');
|
|
|
|
+ localVideoTrack.play(localVideoEl);
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error(error);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
|
|
- isHasCamera.value && (await client.publish(localVideoTrack));
|
|
|
|
- isHasMicrophone.value && (await client.publish(localAudioTrack));
|
|
|
|
- } else {
|
|
|
|
- isHasMicrophone.value && (await client.publish([localAudioTrack]));
|
|
|
|
|
|
+ //主持人有相机的
|
|
|
|
+ console.warn('主持人看到::');
|
|
|
|
+ console.warn('有VID leader发布流');
|
|
|
|
+ await client.publish(localVideoTrack);
|
|
|
|
+ localVideoTrack.setMuted(true);
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- isHasMicrophone.value && (await client.publish([localAudioTrack]));
|
|
|
|
}
|
|
}
|
|
- isHasMicrophone.value && localAudioTrack.setMuted(true);
|
|
|
|
|
|
+
|
|
|
|
+ if (isHasMicrophone.value) {
|
|
|
|
+ console.warn('所有有MIC静音和发布');
|
|
|
|
+ await client.publish(localAudioTrack);
|
|
|
|
+ // 先play
|
|
|
|
+ localAudioTrack.setVolume(120);
|
|
|
|
+ localAudioTrack.play();
|
|
|
|
+ localAudioTrack.setMuted(true);
|
|
|
|
+ }
|
|
|
|
+ //默认头闭摄像头
|
|
|
|
+ muteVideoLeader.value = true;
|
|
rtcStore.setIsRTCJoined(true);
|
|
rtcStore.setIsRTCJoined(true);
|
|
|
|
+
|
|
return client;
|
|
return client;
|
|
};
|
|
};
|
|
|
|
+
|
|
export const muteVideo = () => {
|
|
export const muteVideo = () => {
|
|
//通用开音方法
|
|
//通用开音方法
|
|
localVideoTrack.setMuted(true);
|
|
localVideoTrack.setMuted(true);
|
|
@@ -116,7 +155,6 @@ export const mutedAudio = () => {
|
|
console.error('mutedAudio');
|
|
console.error('mutedAudio');
|
|
if (isHasMicrophone.value) {
|
|
if (isHasMicrophone.value) {
|
|
localAudioTrack.setMuted(true);
|
|
localAudioTrack.setMuted(true);
|
|
- // localAudioTrack.stop();
|
|
|
|
}
|
|
}
|
|
};
|
|
};
|
|
export const unMutedAudio = () => {
|
|
export const unMutedAudio = () => {
|
|
@@ -124,7 +162,6 @@ export const unMutedAudio = () => {
|
|
console.error('unMutedAudio');
|
|
console.error('unMutedAudio');
|
|
if (isHasMicrophone.value) {
|
|
if (isHasMicrophone.value) {
|
|
localAudioTrack.setMuted(false);
|
|
localAudioTrack.setMuted(false);
|
|
- // localAudioTrack.play();
|
|
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
@@ -142,7 +179,6 @@ export const switchToFrontCam = async () => {
|
|
};
|
|
};
|
|
|
|
|
|
export const switchToBackCam = async () => {
|
|
export const switchToBackCam = async () => {
|
|
- // localStream.switchDevice('video', 'environment');
|
|
|
|
localClient.unpublish(localVideoTrack);
|
|
localClient.unpublish(localVideoTrack);
|
|
localVideoTrack = await AgoraRTC.createCameraVideoTrack({
|
|
localVideoTrack = await AgoraRTC.createCameraVideoTrack({
|
|
facingMode: 'environment',
|
|
facingMode: 'environment',
|
|
@@ -153,3 +189,6 @@ export const switchToBackCam = async () => {
|
|
}
|
|
}
|
|
await localClient.publish(localVideoTrack);
|
|
await localClient.publish(localVideoTrack);
|
|
};
|
|
};
|
|
|
|
+export const cleanAll = () => {
|
|
|
|
+ AgoraRTC.removeAllListeners();
|
|
|
|
+};
|