@4players/odin
Version:
A cross-platform SDK enabling developers to integrate real-time VoIP chat technology into their projects
203 lines (202 loc) • 6.23 kB
JavaScript
import { __awaiter } from "tslib";
import { OdinAudioService } from './audio';
/**
* Class describing a single media stream inside an `OdinRoom`.
*/
export class OdinMedia {
/**
* Creates a new `OdinMedia` instance.
*
* @param _id The ID of the new media
* @param _peerId The ID of the peer that owns the new media
* @param _remote Wether or not the new media belongs to a remote peer
*
* @ignore
*/
constructor(_id, _peerId, _remote) {
this._id = _id;
this._peerId = _peerId;
this._remote = _remote;
/**
* An instance of `EventTarget` for handling events related to this media.
*/
this._eventTarget = new EventTarget();
/**
* A boolean that indicates if the media is sending/receiving data or not.
*/
this._active = false;
/**
* A boolean that indicates if the media is paused or not.
*/
this._paused = false;
/**
* Represents the volume of the media stream, default value is 1.
*/
this._volume = 1;
this._audioService = OdinAudioService.getInstance();
}
/**
* The ID of the media.
*/
get id() {
return this._id;
}
/**
* The ID of the peer that owns the media.
*/
get peerId() {
return this._peerId;
}
/**
* Indicates wether or not the media belongs to a remote peer.
*/
get remote() {
return this._remote;
}
/**
* Set the paused flag of the media.
*
* NOTE: Important do not adjust this manually!
*/
set paused(paused) {
this._paused = paused;
}
/**
* Indicates wether or not the media is paused.
*/
get paused() {
return this._paused;
}
/**
* Set the activity status of the media.
*
* NOTE: Important do not adjust this manually!
*/
set active(active) {
this._active = active;
}
/**
* Indicates wether or not the media is currently sending/receiving data.
*/
get active() {
return this._active;
}
/**
* Indicates whether or not the media is registered in the audio service instance (e.g. started).
*/
get started() {
var _a, _b;
return (_b = (_a = this._audioService) === null || _a === void 0 ? void 0 : _a.hasMedia(this._id)) !== null && _b !== void 0 ? _b : false;
}
/**
* An event target handler for the peer.
*
* @ignore
*/
get eventTarget() {
return this._eventTarget;
}
/**
* The individual playback volume of the media stream.
*/
get volume() {
return this._volume;
}
/**
* Starts the media stream by initiating the encoder/decoder and adding it to the room if it belongs to the local peer.
*
* @returns A promise which yields when the request is resolved
*/
start() {
return __awaiter(this, void 0, void 0, function* () {
if (this.started)
return;
if (!this._audioService) {
throw new Error('Unable to start media; AudioService is not available');
}
if (this._remote) {
this._audioService.startDecoder(this);
}
else {
yield this._audioService.room.addMedia(this);
this._audioService.startEncoder(this);
}
this._audioService.registerMedia(this);
});
}
/**
* Stops the media stream by terminating the encoder/decoder and removing it from the room if it belongs to the local peer.
*
* @returns A promise which yields when the request is resolved
*/
stop() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.started)
return;
if (!this._audioService) {
throw new Error('Unable to stop media; AudioService is not available');
}
if (this._remote) {
this._audioService.stopDecoder(this);
}
else {
yield this._audioService.room.removeMedia(this);
this._audioService.stopEncoder(this);
}
this._active = false;
this._audioService.unregisterMedia(this);
});
}
/**
* Paused the media stream on the server to stop receiving data on it.
*
* @returns A promise which yields when the request is resolved
*/
pause() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if (!this._audioService) {
throw new Error('Unable to pause media; AudioService is not available');
}
yield ((_a = this._audioService) === null || _a === void 0 ? void 0 : _a.room.pauseMedia(this));
});
}
/**
* Paused the media stream on the server to start receiving data on it.
*
* @returns A promise which yields when the request is resolved
*/
resume() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if (!this._audioService) {
throw new Error('Unable to resume media; AudioService is not available');
}
yield ((_a = this._audioService) === null || _a === void 0 ? void 0 : _a.room.resumeMedia(this));
});
}
/**
* Changes the playback volume of the media.
*
* @param volume The new volume (Default is 1)
*/
changeVolume(volume) {
if (!this._audioService) {
throw new Error('Unable to change media volume; AudioService is not available');
}
this._audioService.audioWorker.postMessage({
type: 'set_playback_volume',
media_id: this._id,
value: volume,
});
}
/**
* Registers to media events from `IOdinMediaEvents`.
*
* @param eventName The name of the event to listen to
* @param handler The callback to handle the event
*/
addEventListener(eventName, handler) {
this._eventTarget.addEventListener(eventName, handler);
}
}