UNPKG

@4players/odin

Version:

A cross-platform SDK enabling developers to integrate real-time VoIP chat technology into their projects

119 lines (118 loc) 4.99 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _RemotePeer_volume, _RemotePeer_data; Object.defineProperty(exports, "__esModule", { value: true }); exports.RemotePeer = void 0; const index_1 = require("./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. */ class RemotePeer extends index_1.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]); } } exports.RemotePeer = RemotePeer; _RemotePeer_volume = new WeakMap(), _RemotePeer_data = new WeakMap();