@4players/odin
Version:
A cross-platform SDK enabling developers to integrate real-time VoIP chat technology into their projects
105 lines (104 loc) • 3.83 kB
JavaScript
var _RemotePeer_volume, _RemotePeer_data;
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
import { Peer } from './index';
/**
* Represents a RemotePeer, which extends functionality from the Peer class. A RemotePeer
* is associated with audio outputs as well as data and messages exchanged in the room.
* It provides utilities to manage playback volume, user data, and message communication with the remote peer.
*/
export class RemotePeer extends Peer {
/**
* Set updated user data for the peer.
*/
set data(data) {
__classPrivateFieldSet(this, _RemotePeer_data, data, "f");
}
/**
* The arbitrary user data of the peer.
*
* @return {Uint8Array} The appropriate Uint8Array data based on the context.
*/
get data() {
return __classPrivateFieldGet(this, _RemotePeer_data, "f");
}
/**
* Determines if any of the audio outputs related to this peer are currently active.
*
* @return {boolean} True if at least one of the related audio outputs is active, otherwise false.
*/
get isActive() {
return this.room.audioOutputs.some((input) => {
return input.peer === this && input.isActive;
});
}
/**
* Retrieves the highest power level from the related audio outputs.
*
* @return {number} The highest power level among all audio outputs.
*/
get powerLevel() {
let highestPowerLevel = 0;
for (const audioOutput of this.audioOutputs) {
if (audioOutput.powerLevel > highestPowerLevel) {
highestPowerLevel = audioOutput.powerLevel;
}
}
return highestPowerLevel;
}
/**
* Gets the current volume of the LocalPeer.
*
* @return {PlaybackVolume} The current volume.
*/
get volume() {
return __classPrivateFieldGet(this, _RemotePeer_volume, "f");
}
/**
* Retrieves the list of audio output devices associated with this Peer.
*
* @return {AudioOutput[]} An array of AudioOutput objects related to this Peer.
*/
get audioOutputs() {
return this.room.audioOutputs.filter((playback) => playback.peer === this);
}
/**
* Retrieves the list of VideoOutput objects associated with this Peer.
*
* @return {VideoOutput[]} An array of VideoOutput objects. Returns an empty array if no video outputs are associated.
*/
get videoOutputs() {
return this.room.videoOutputs.filter((playback) => playback.peer === this);
}
constructor(_peerData, room) {
super(_peerData, room);
this._peerData = _peerData;
this.room = room;
this.isRemote = true;
_RemotePeer_volume.set(this, [1, 1]);
_RemotePeer_data.set(this, new Uint8Array());
__classPrivateFieldSet(this, _RemotePeer_data, _peerData.user_data, "f");
}
/**
* Sets the volume for the playback.
*
* @param {PlaybackVolume|number} value - The desired volume level. It can be a number or left and right channel [number, number].
*/
setVolume(value) {
__classPrivateFieldSet(this, _RemotePeer_volume, typeof value === 'number' ? [value, value] : value, "f");
for (const media of this.audioOutputs) {
media.setVolume(value).catch(console.error);
}
}
/**
* Sends a message with arbitrary data to this peer.
*
* @param {Uint8Array} message - The message to be sent as a byte array.
* @return {Promise<void>} A promise that resolves when the message is sent successfully.
*/
async sendMessage(message) {
if (!message)
return;
await this.room.sendMessage(message, [this.id]);
}
}
_RemotePeer_volume = new WeakMap(), _RemotePeer_data = new WeakMap();