@4players/odin
Version:
A cross-platform SDK enabling developers to integrate real-time VoIP chat technology into their projects
208 lines (207 loc) • 7.2 kB
TypeScript
import { IOdinAudioSettings, OdinAudioContextConfig } from './types';
import { OdinRoom } from './room';
import { OdinPeer } from './peer';
import { OdinMedia } from './media';
/**
* Class responsible for handling audio encoding/decoding operations and emitting events related
* to voice activity status.
*/
export declare class OdinAudioService {
private _worker;
private _audioDataChannel;
private _audioContexts;
/**
* Singleton instance of the audio service.
*/
private static _instance?;
/**
* Audio element for playback.
*/
private _audioElement?;
/**
* The audio source node from the media stream.
*/
private _audioSource?;
/**
* Node responsible for encoding the audio.
*/
private _encoderNode?;
/**
* Node responsible for decoding the audio.
*/
private _decoderNode?;
/**
* Room associated with this audio service.
*/
private _room;
/**
* Array to hold OdinMedia instances.
*/
private _medias;
/**
* Percent of outgoing media packets to artificially drop.
*/
private _artificialPacketLoss;
/**
* Whether or not RNN VAD statistics are enabled.
*/
private _voiceProcessingStatsEnabled;
/**
* Default settings for audio processing.
*/
private _audioSettings;
/**
* Initializes and returns a singleton instance of the audio service. If the instance already exists, returns the existing instance.
*
* @param _worker The worker instance responsible for audio encoding/decoding
* @param _audioDataChannel The RTC data channel used for transmitting audio data
* @param _audioContexts The web audio context used for input/output audio processing
*/
private constructor();
/**
* Retrieves the `OdinPeer` instance that matches a specified media ID.
*
* @param id The identifier of the peer's associated media
* @return The peer object matching the specified media ID or `undefined` if none found
*/
getPeerByMediaId(id: number): OdinPeer | undefined;
/**
* Initializes and returns a singleton instance of the audio service.
*
* @param worker The worker instance handing all the encoding/decoding
* @param audioChannel The RTC data channel used to transfer audio data
* @param audioContexts The web audio contexts for processing capture/playback data
*/
static setInstance(worker: Worker, audioChannel: RTCDataChannel, audioContexts: OdinAudioContextConfig): OdinAudioService;
/**
* Returns a singleton instance of the audio service.
*/
static getInstance(): OdinAudioService | undefined;
/**
* Returns the underlying audio worker instance.
*/
get audioWorker(): Worker;
/**
* Returns the underlying room instance.
*/
get room(): OdinRoom;
/**
* Sets the room this audio service is dealing with.
*
* @param room A room instance
*/
set room(room: OdinRoom);
/**
* Returns the artificial packet loss for outgoing media packets.
*/
get artificialPacketLossPercentage(): number;
/**
* Sets the artificial packet loss for outgoing media packets.
*
* @param room Packet loss in percent (0-100)
*/
set artificialPacketLossPercentage(value: number);
/**
* Returns the sample rate of the input audio context.
*/
get inputSampleRate(): number;
/**
* Returns the sample rate of the output audio context.
*/
get outputSampleRate(): number;
/**
* Returns the `OdinMedia` instance matching the specified ID if known.
*
* @param id The media ID to search for
*/
getMedia(id: number): OdinMedia | undefined;
/**
* Returns true if the audio service knows a media with the specified ID.
*
* @param id The media ID to search for
*/
hasMedia(id: number): boolean;
/**
* Updates the activity status of a specific media. This method is responsible for setting the activity state of a media and
* triggering the necessary events when the media activity status changes.
*
* @param id The ID of the media whose activity status needs updating
* @param isActive The updated activity status of the media
*/
updateMediaActivity(id: number, isActive: boolean): void;
/**
* Registers a media to handle its talk status.
*
* @param media The media that gets registered
*/
registerMedia(media: OdinMedia): void;
/**
* Unregister a media to stop the talk status handling.
*
* @param media The media that gets unregistered
*/
unregisterMedia(media: OdinMedia): void;
/**
* Starts an internal encoder for the specified media.
*
* @param media The media to start an encoder for
*/
startEncoder(media: OdinMedia): void;
/**
* Stops an internal encoder for the specified media.
*
* @param media The media to stop an encoder for
*/
stopEncoder(media: OdinMedia): void;
/**
* Starts an internal decoder for the specified media.
*
* @param media The media to start a decoder for
*/
startDecoder(media: OdinMedia): void;
/**
* Stops an internal decoder for the specified media.
*
* @param media The media to stop a decoder for
*/
stopDecoder(media: OdinMedia): void;
/**
* Set up the audio device, encoder and decoder.
*/
setupAudio(): Promise<void>;
/**
* Initiates the audio input recording. In case of using a Blink-based browser, it handles echo cancellation as well.
*
* @param mediaStream The media stream object that contains the audio track to be processed
* @param audioSettings Settings related to audio processing
*/
updateInputStream(mediaStream: MediaStream): Promise<void>;
/**
* Updates settings for voice activity detection and volume gate. These settings control how voice activity is detected and how
* the volume gate is operated.
*
* @param settings The new configurations and thresholds for voice activity detection and volume gate
*/
setVoiceProcessingConfig(settings: IOdinAudioSettings): void;
/**
* Returns settings for voice activity detection and volume gate.
*/
getVoiceProcessingConfig(): IOdinAudioSettings;
/**
* Enables/disables emitting of voice activity detection stats.
*/
setVoiceProcessingStatsEnabled(enabled: boolean): void;
/**
* Stops all audio encoding/decoding and closes the related connections. This includes disconnecting the encoder and decoder nodes,
* closing the audio data channel, and stopping any audio currently playing.
*/
stopAllAudio(): Promise<void>;
/**
* Helper functions to allow echo cancellation on Chromium based browsers by piping all incoming audio through WebRTC. This is a
* workaround to a known issue in Chromium.
*
* See https://bugs.chromium.org/p/chromium/issues/detail?id=687574
*/
private webrtcDummy;
private webrtcLoopback;
}