171 lines
4.4 KiB
JavaScript
171 lines
4.4 KiB
JavaScript
import AgoraRTC from 'agora-rtc-sdk-ng';
|
|
|
|
class AgoraService {
|
|
constructor() {
|
|
this.client = null;
|
|
this.localAudioTrack = null;
|
|
this.uid = null;
|
|
this.isJoined = false;
|
|
this.remoteUsers = {};
|
|
this.volumeIndicator = null;
|
|
this.vadEnabled = true;
|
|
this.appid = '01a1debc964a4c6a8df1de2a6ce7aa4d';
|
|
this.token = '007eJxTYHi+XWtqBkPmn71LGvdmPds7sfiQfomBxpt3XMfOC53fcjVegcHAMNEwJTUp2dLMJNEk2SzRIiUNyDdKNEtONU9MNEkpibmZ3hDIyPDYXo2JkQECQXwRhuT8vLLETCBZnJ+TGm9oYmluYMDAAACcPigI';
|
|
this.channel = 'convaiconsole_149700';
|
|
this.vadParams = {
|
|
interruptDurationMs: 160,
|
|
prefixPaddingMs: 300,
|
|
silenceDurationMs: 480,
|
|
threshold: 0.5
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Initialize the Agora RTC client
|
|
*/
|
|
init() {
|
|
this.client = AgoraRTC.createClient({ mode: 'rtc', codec: 'vp8' });
|
|
this.setupEventListeners();
|
|
return this.client;
|
|
}
|
|
|
|
/**
|
|
* Set up event listeners for the Agora client
|
|
*/
|
|
setupEventListeners() {
|
|
this.client.on('user-published', async (user, mediaType) => {
|
|
await this.client.subscribe(user, mediaType);
|
|
if (mediaType === 'audio') {
|
|
user.audioTrack.play();
|
|
this.remoteUsers[user.uid] = user;
|
|
}
|
|
});
|
|
|
|
this.client.on('user-unpublished', (user) => {
|
|
if (user.audioTrack) {
|
|
user.audioTrack.stop();
|
|
}
|
|
delete this.remoteUsers[user.uid];
|
|
});
|
|
|
|
this.client.on('user-left', (user) => {
|
|
delete this.remoteUsers[user.uid];
|
|
});
|
|
|
|
this.client.on('volume-indicator', (volumes) => {
|
|
volumes.forEach((volume) => {
|
|
// Handle volume indicator
|
|
if (volume.uid === this.uid) {
|
|
// Local user's volume
|
|
const event = new CustomEvent('local-volume', {
|
|
detail: { level: volume.level }
|
|
});
|
|
window.dispatchEvent(event);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Join a channel with the given token and channel name
|
|
* @param {string} token - The token for authentication
|
|
* @param {string} channel - The channel name to join
|
|
* @param {string} uid - The user ID (optional)
|
|
*/
|
|
async join(agent_id, create_ts, status, uid = null) {
|
|
try {
|
|
// Join the channel
|
|
this.uid = await this.client.join(this.appid, this.channel, this.token, uid);
|
|
|
|
console.log("successful! this.uid is ", this.uid);
|
|
|
|
this.isJoined = true;
|
|
|
|
// Create and publish local audio track
|
|
this.localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack({
|
|
AEC: true,
|
|
AGC: true,
|
|
ANS: true
|
|
});
|
|
|
|
// Enable VAD (Voice Activity Detection)
|
|
if (this.vadEnabled && this.localAudioTrack.setVADMode) {
|
|
this.localAudioTrack.setVADMode(true, this.vadParams);
|
|
}
|
|
|
|
// Publish local audio track
|
|
await this.client.publish([this.localAudioTrack]);
|
|
|
|
// Enable volume indicator
|
|
this.client.enableAudioVolumeIndicator();
|
|
|
|
console.log("status:", this.status);
|
|
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error joining channel:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Leave the channel and release resources
|
|
*/
|
|
async leave() {
|
|
if (this.localAudioTrack) {
|
|
this.localAudioTrack.close();
|
|
this.localAudioTrack = null;
|
|
}
|
|
|
|
await this.client.leave();
|
|
this.isJoined = false;
|
|
this.remoteUsers = {};
|
|
}
|
|
|
|
/**
|
|
* Mute or unmute the local audio
|
|
* @param {boolean} mute - Whether to mute the audio
|
|
*/
|
|
muteAudio(mute) {
|
|
if (this.localAudioTrack) {
|
|
if (mute) {
|
|
this.localAudioTrack.setEnabled(false);
|
|
} else {
|
|
this.localAudioTrack.setEnabled(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if the local audio is muted
|
|
* @returns {boolean} - Whether the audio is muted
|
|
*/
|
|
isAudioMuted() {
|
|
return this.localAudioTrack ? !this.localAudioTrack.enabled : true;
|
|
}
|
|
|
|
/**
|
|
* Set the VAD parameters
|
|
* @param {Object} params - The VAD parameters
|
|
*/
|
|
setVADParams(params) {
|
|
this.vadParams = { ...this.vadParams, ...params };
|
|
if (this.localAudioTrack && this.localAudioTrack.setVADMode) {
|
|
this.localAudioTrack.setVADMode(this.vadEnabled, this.vadParams);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Enable or disable VAD
|
|
* @param {boolean} enabled - Whether to enable VAD
|
|
*/
|
|
enableVAD(enabled) {
|
|
this.vadEnabled = enabled;
|
|
if (this.localAudioTrack && this.localAudioTrack.setVADMode) {
|
|
this.localAudioTrack.setVADMode(enabled, this.vadParams);
|
|
}
|
|
}
|
|
}
|
|
|
|
export default new AgoraService();
|