@4players/odin
Version:
A cross-platform SDK enabling developers to integrate real-time VoIP chat technology into their projects
90 lines (89 loc) • 2.99 kB
JavaScript
var _LocalPeer_volume;
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
import { Peer } from './index';
/**
* Represents the local peer connected to a room. This class extends the base Peer class and includes
* functionality specific to the local peer, such as adjusting audio input volumes and managing user data.
*/
export class LocalPeer extends Peer {
/**
* Gets the current volume of the LocalPeer.
*
* @return {CaptureVolume} The current volume.
*/
get volume() {
return __classPrivateFieldGet(this, _LocalPeer_volume, "f");
}
/**
* Determines if any of the audio inputs are currently active.
*
* @return {boolean} True if at least one audio input is active, otherwise false.
*/
get isActive() {
return this.audioInputs.some((input) => input.isActive);
}
/**
* Retrieves the highest power level among the audio inputs.
*
* @return {number} The highest power level detected across all audio inputs.
*/
get powerLevel() {
let highestPowerLevel = 0;
for (const audioInput of this.audioInputs) {
if (audioInput.powerLevel > highestPowerLevel) {
highestPowerLevel = audioInput.powerLevel;
}
}
return highestPowerLevel;
}
/**
* Retrieves the list of AudioInput devices associated with this Peer.
*
* @return {AudioInput[]} An array of AudioInput objects. Returns an empty array if the instance is remote.
*/
get audioInputs() {
return this.room.audioInputs;
}
/**
* Set updated user data for the peer.
*/
set data(data) {
this.room.userData = data;
}
/**
* The arbitrary user data of the peer.
*
* @return {Uint8Array} The appropriate Uint8Array data based on the context.
*/
get data() {
return this.room.userData;
}
constructor(_peerData, room) {
super(_peerData, room);
this._peerData = _peerData;
this.room = room;
this.isRemote = false;
_LocalPeer_volume.set(this, 1);
}
/**
* Sets the volume for all associated Medias of this Peer.
*
* @param {CaptureVolume} value - The new volume level to set.
* @return {Promise<void>} A promise that resolves when the volume is set for all medias.
*/
async setVolume(value) {
__classPrivateFieldSet(this, _LocalPeer_volume, value, "f");
for (const media of this.room.audioInputs) {
await media.setVolume(value);
}
}
/**
* Updates the current user's data by flushing it to the associated room.
*
* @return {Promise<void>} Resolves when the user data has been successfully updated, or rejects with an error if the user is a remote peer or if the operation encounters an issue.
*/
async update() {
await this.room.flushUserData();
}
}
_LocalPeer_volume = new WeakMap();