import { OdinMedia } from "./odin.media"; import { OdinCipher } from "./odin.cipher"; /** * Defines available Odin events */ declare interface OdinEvents { /** * Fired when the local user connected to the room (successfully joined). */ Joined: OdinJoinedEvent; /** * Fired when the local user disconnected from the room (left). */ Left: OdinLeftEvent; /** * Fired when a remote peer joined the room. This event is fired for all existing peers in the room before the Joined event is fired. */ PeerJoined: OdinPeerJoinedEvent; /** * Fired when a remote peer left the room. */ PeerLeft: OdinPeerLeftEvent; /** * Fired when a remote peer sent a message to the room. */ MessageReceived: OdinMessageReceivedEvent; /** * Fired when the user data of a remote peer changed. */ PeerUserDataChanged: OdinPeerUserDataChangedEvent; /** * Fired when the user data of the room changed. */ RoomUserDataChanged: OdinRoomUserDataChangedEvent; /** * Fired when the connection state of the room changed. */ ConnectionStateChanged: OdinConnectionStateChangedEvent; /** * Fired when a media stream was added to a peer. This happens for example if a peer adds a microphone to the room */ MediaAdded: OdinMediaAddedEvent; /** * Fired when a media stream was removed from a peer. This happens for example if a peer removes a microphone from the room */ MediaRemoved: OdinMediaRemovedEvent; /** * Fired when a media stream was activated or deactivated. The active flag is true if the user started to talk and false if the user stopped talking. */ MediaActivity: OdinMediaActivityEvent; /** * Fired when audio data is available for media streams. Audio comes in 16 bit and 32 bit float samples and are available individually for each peer. * You can use the samples to record audio or send them to an AI for transcription. */ AudioDataReceived: OdinAudioDataReceivedEvent; /** * Fired when the tags of a remote peer changed. */ PeerTagsChanged: OdinPeerTagsChangedEvent; } export type OdinConnectionStateChangedEvent = (event: OdinConnectionStateChangedEventPayload) => void; export type OdinJoinedEvent = (event: OdinJoinedEventPayload) => void; export type OdinLeftEvent = (event: OdinLeftEventPayload) => void; export type OdinPeerJoinedEvent = (event: OdinPeerJoinedEventPayload) => void; export type OdinPeerLeftEvent = (event: OdinPeerLeftEventPayload) => void; export type OdinMessageReceivedEvent = (event: OdinMessageReceivedEventPayload) => void; export type OdinPeerUserDataChangedEvent = (event: OdinPeerUserDataChangedEventPayload) => void; export type OdinRoomUserDataChangedEvent = (event: OdinRoomUserDataChangedEventPayload) => void; export type OdinMediaAddedEvent = (event: OdinMediaAddedEventPayload) => void; export type OdinMediaRemovedEvent = (event: OdinMediaRemovedEventPayload) => void; export type OdinMediaActivityEvent = (event: OdinMediaActivityEventPayload) => void; export type OdinAudioDataReceivedEvent = (event: OdinAudioDataReceivedEventPayload) => void; export type OdinPeerTagsChangedEvent = (event: OdinPeerTagsChangedEventPayload) => void; /** * The base event payload that is passed to all events. */ declare interface OdinEventPayload { /** * The name of the event that was fired (see keys of OdinEvents for possible values). */ event: keyof OdinEvents; /** * An internal integer representing the type of the event. This is mainly used internally for testing purposes. */ tag: number; } /** * Represents a media stream in the ODIN room. * Media streams are used for audio transmission between peers. */ export declare interface OdinMediaInfo { /** * The unique identifier of the media stream. */ id: number; /** * Custom properties associated with this media stream. */ properties: Record; /** * Whether the media stream is currently paused. */ paused: boolean; } /** * Represents a peer in the ODIN room. * A peer is a participant in the room that can send and receive audio. */ export declare interface OdinPeerInfo { /** * The unique identifier of the peer within the room. */ id: number; /** * The user ID associated with this peer (from the token). */ user_id: string; /** * Custom user data associated with this peer. */ user_data: Uint8Array | undefined; /** * List of media streams owned by this peer. */ medias: OdinMediaInfo[]; /** * Tags associated with this peer for filtering/grouping. */ tags: string[]; } /** * Represents the room state returned when joining. * Contains information about the room and all current participants. */ export declare interface OdinRoomInfo { /** * The unique identifier of the room. */ id: string; /** * The customer ID owning this room. */ customer: string; /** * Custom user data associated with the room. */ user_data: Uint8Array | undefined; /** * List of all peers currently in the room. */ peers: OdinPeerInfo[]; } /** * The payload for the Joined event. * This event is fired when the local user successfully joins a room. */ export declare interface OdinJoinedEventPayload extends OdinEventPayload { /** * The ID of the room that was joined. It's the same as the room id that was passed to join. */ roomId: string; /** * The ID of the local peer within the room. */ ownPeerId: number; /** * The full room state including all current peers and their media streams. */ room: OdinRoomInfo; /** * List of media IDs owned by the local peer. * These are media streams that were previously created and are still active. */ mediaIds: number[]; } /** * The payload for the Left event. * This event is fired when the local user leaves a room, either due to a server-initiated * disconnect or when `room.close()` is called programmatically. */ export declare interface OdinLeftEventPayload extends OdinEventPayload { /** * The ID of the room that was left. */ roomId: string; /** * The reason for leaving the room. * - 'ClientDisconnect': The client called `room.close()` programmatically * - Other values: Server-initiated disconnects (e.g., 'kicked', 'timeout', etc.) */ reason: string; } /** * The payload for the PeerJoined event. */ export declare interface OdinPeerJoinedEventPayload extends OdinEventPayload { /** * The ID of the peer that joined. */ peerId: number; /** * The ID of the user that joined. */ userId: string; /** * The user data of the peer that joined. */ userData: Uint8Array | undefined; } /** * The payload for the PeerLeft event. */ export declare interface OdinPeerLeftEventPayload extends OdinEventPayload { /** * The ID of the peer that left. */ peerId: number; } /** * The payload for the PeerUserDataChanged event. */ export declare interface OdinPeerUserDataChangedEventPayload extends OdinEventPayload { /** * The ID of the peer that changed the user data. */ peerId: number; /** * The new user data of the peer. */ userData: Uint8Array | undefined; } /** * The payload for the RoomUserDataChanged event. */ export declare interface OdinRoomUserDataChangedEventPayload extends OdinEventPayload { /** * The new user data of the room. */ userData: Uint8Array | undefined; } /** * Connection states of the room */ export declare type OdinConnectionState = 'connecting' | 'connected' | 'disconnecting' | 'disconnected' | 'unknown'; /** * Change reasons for the connection state */ export declare type OdinConnectionStateChangeReason = 'client_request' | 'server_request' | 'timeout' | 'unknown'; /** * The payload for the ConnectionStateChanged event. */ export declare interface OdinConnectionStateChangedEventPayload extends OdinEventPayload { /** * The new connection state of the room. */ state: OdinConnectionState; /** * The error code if the connection state is Error. */ reason: OdinConnectionStateChangeReason; } /** * The payload for the MessageReceived event. */ export declare interface OdinMessageReceivedEventPayload extends OdinEventPayload { /** * The ID of the peer that sent the message. */ peerId: number; /** * The message data received from the peer. */ messageData: Uint8Array; } /** * The payload for the MediaAdded event. */ export declare interface OdinMediaAddedEventPayload extends OdinEventPayload { /** * The ID of the peer that added the media. */ peerId: number; /** * The ID of the media that was added. */ mediaId: number; } /** * The payload for the MediaRemoved event. */ export declare interface OdinMediaRemovedEventPayload extends OdinEventPayload { /** * The ID of the peer that removed the media. */ peerId: number; /** * The ID of the media that was removed. */ mediaId: number; } /** * The payload for the MediaActivity event. */ export declare interface OdinMediaActivityEventPayload extends OdinEventPayload { /** * The ID of the peer that changed the media activity. */ peerId: number; /** * The ID of the media that changed the activity. */ mediaId: number; /** * Indicates if the media is currently active or not (i.e. user talking or not). */ state: boolean; } /** * The payload for the AudioDataReceived event. */ export declare interface OdinAudioDataReceivedEventPayload { /** * The ID of the peer that sent the audio data. */ peerId: number; /** * The ID of the media that sent the audio data. */ mediaId: number; /** * The audio data received from the peer as 16-bit PCM samples ranging from -32768 to 32767 as a byte array. * Use `const samplesArray = new Int16Array(samples16.buffer)` to get an actual array */ samples16: Uint8Array; /** * The audio data received from the peer as 32-bit PCM samples ranging from -1 to 1. * Use `const floats = new Float32Array(samples32.buffer)` to get an actual array */ samples32: Uint8Array; } /** * The payload for the PeerTagsChanged event. */ export declare interface OdinPeerTagsChangedEventPayload extends OdinEventPayload { /** * The ID of the peer whose tags changed. */ peerId: number; /** * The new tags of the peer. */ tags: string[]; } /** * Noise suppression levels */ export declare enum OdinNoiseSuppressionLevel { /** * Noise suppression is disabled */ None = 0, /** * Use low suppression (6 dB) */ Low = 1, /** * Use moderate suppression (12 dB) */ Moderate = 2, /** * Use high suppression (18 dB) */ High = 3, /** * Use very high suppression (21 dB) */ VeryHigh = 4 } export declare interface OdinAPMSettings { /** * Enables or disables voice activity detection (VAD) */ voiceActivityDetection: boolean, /** * Voice probability value when the VAD should engage */ voiceActivityDetectionAttackProbability: number, /** * Voice probability value when the VAD should disengage */ voiceActivityDetectionReleaseProbability: number, /** * Enables or disables the input volume gate */ volumeGate: boolean, /** * Root-mean-square power (dBFS) when the volume gate should engage */ volumeGateAttackLoudness: number, /** * Root-mean-square power (dBFS) when the volume gate should disengage */ volumeGateReleaseLoudness: number, /** * Enable or disable echo cancellation */ echoCanceller: boolean, /** * Enable or disable high pass filtering */ highPassFilter: boolean, /** * Enable or disable the pre amplifier */ preAmplifier: boolean, /** * Set the aggressiveness of the suppression */ noiseSuppressionLevel: OdinNoiseSuppressionLevel, /** * Enable or disable the transient suppressor */ transientSuppressor: boolean, /** * Enable or disable the gain controller */ gainController: boolean } /** * The OdinRoom class. Use createRoom from OdinClient instance to create a room. The OdinRoom requires a token to connect * to a room. OdinClients handles token creation for you, so its easier to just use OdinClient for room creation. * Typical procedure is to create an OdinClient instance with an access key and call its `createRoom` function to create * a room with a name and for a user id. Then add event listeners to the room and call `join` to connect to the room. */ export declare class OdinRoom { /** * Creates a new instance of a room with a token. Use OdinClient if you don't want to manage tokens yourself. * Important: Don't use this function directly, use OdinClient instead. * @param token - The token to use for this room. */ constructor(token: string); /** * Joins the room with the given gateway URL and optional user data. * @param gatewayUrlOrServer - The gateway URL to connect to or the URL to the ODIN Server instance. Use gateway.odin.4players.io if you are unsure. * @param userData - The user data to send to the room. This can be used to identify the user. */ join(gatewayOrServerUrl: string, userData?: Uint8Array): void; /** * Sends a message to the room. * @param message - The message to send as a byte array. * @param peerIdList - The list of peer IDs to send the message to. If this is undefined, the message will be sent to all peers. If the list is defined and empty an error will be thrown. */ sendMessage(message: Uint8Array, peerIdList?: number[]): void; /** * Adds an event listener to the room for specific events. * @param eventName - The event to listen for (see keys of OdinEvents for possible values) * @param handler - The callback to call when the event is fired. */ addEventListener(eventName: Event, handler: OdinEvents[Event]): void; /** * Removes an event listener from the room for specific events. * @param eventName - The event to remove the listener from (see keys of OdinEvents for possible values) */ removeEventListener(eventName: Event): void; /** * Sets a global event listener that received all events, this can be helpful for debugging. Please use addEventListener instead for production code. * @param callback - The callback to call when the event is fired. */ setEventListener(callback: (data: OdinEventPayload) => void): void; /** * Updates the peer user data of the local peer * @param userData - The new user data to set. */ updateOwnUserData(userData: Uint8Array): void; /** * Updates the three-dimensional position of the current peer in this room. * The server utilizes the provided coordinates to perform automatic culling among peers in the same * room, based on unit circles with a radius of `1.0`. This feature is particularly beneficial in * scenarios involving a large number of peers within the same room, enabling peers to interact or * 'see' each other only when in close proximity. To modify the distance sensitivity for position * updates, use `setPositionScale`. * * Note: Use this before calling `join` to set the initial peer position upon connect. * * @param x - The new x position of the peer. * @param y - The new y position of the peer. * @param z - The new z position of the peer. */ updatePosition(x: number, y: number, z: number): void; /** * Sets the scaling factor for coordinates supplied to `updatePosition`, facilitating * adaptation to your game's unique coordinate system requirements. Peers are visible to each other * only within a unit circle of radius `1.0`. When altering a peer's position, ensure the position * is scaled such that the maximum distance remains one or less. This scaling can be performed * manually or by specifying the multiplicative scale here. * * Note: It's crucial to maintain consistent scaling across all client applications. * @param scale - The new scaling factor to use. */ setPositionScale(scale: number): void; /** * Sets the cipher for end-to-end encryption. * @param cipher - The cipher instance. */ setCipher(cipher: OdinCipher): void; /** * Closes the room and disconnects from the server. * * This method emits the 'Left' event before closing the native connection, * ensuring that cleanup callbacks registered via `onLeft()` are triggered. * The event payload will contain `{ reason: 'ClientDisconnect' }` to * distinguish it from server-initiated disconnects. * * This provides a single, consistent cleanup path for session state management, * as the `onLeft` handler will fire for both: * - Client-initiated disconnects (calling `close()`) * - Server-initiated disconnects (kicked, timeout, etc.) */ close(): void; /** * Gets the ID of the room. */ get id(): string; /** * Gets the ID of the local peer joined to the room. */ get ownPeerId(): number; /** * Creates a local audio stream and adds it to the room. An OdinMedia object will be returned that allows you to send * audio data. * @param sampleRate - The sample rate of the audio stream. Can be between 8000 and 48000. * @param channels - The number of channels of the audio stream. Can be 1 or 2. * @returns The OdinMedia object that allows you to send audio data. */ createAudioStream(sampleRate: number, channels: number, apmSettings?: OdinAPMSettings): OdinMedia; /** * Gets whether the room is currently connected. */ get connected(): boolean; // ========================================= // Convenience event handler methods // ========================================= /** * Set handler for when room is successfully joined. * @param handler - Handler receiving `{ roomId, ownPeerId, room }` */ onJoined(handler: OdinJoinedEvent): void; /** * Set handler for when room is left. * @param handler - Handler receiving `{ reason }` */ onLeft(handler: OdinLeftEvent): void; /** * Set handler for peer joined events. * @param handler - Handler receiving `{ peerId, peer, userId, userData }` */ onPeerJoined(handler: OdinPeerJoinedEvent): void; /** * Set handler for peer left events. * @param handler - Handler receiving `{ peerId }` */ onPeerLeft(handler: OdinPeerLeftEvent): void; /** * Set handler for media started events (when a peer starts streaming audio). * @param handler - Handler receiving `{ peerId, media }` */ onMediaStarted(handler: OdinMediaAddedEvent): void; /** * Set handler for media stopped events. * @param handler - Handler receiving `{ peerId, mediaId }` */ onMediaStopped(handler: OdinMediaRemovedEvent): void; /** * Set handler for message received events. * @param handler - Handler receiving `{ senderPeerId, message }` */ onMessageReceived(handler: OdinMessageReceivedEvent): void; /** * Set handler for connection state changes. * @param handler - Handler receiving `{ state, message }` */ onConnectionStateChanged(handler: OdinConnectionStateChangedEvent): void; /** * Set handler for audio data events (for recording/processing). * @param handler - Handler receiving `{ peerId, mediaId, samples16, samples32 }` */ onAudioDataReceived(handler: OdinAudioDataReceivedEvent): void; /** * Set handler for peer user data changed events. * @param handler - Handler receiving `{ peerId, userData }` */ onPeerUserDataChanged(handler: OdinPeerUserDataChangedEvent): void; /** * Set handler for room user data changed events. * @param handler - Handler receiving `{ userData }` */ onRoomUserDataChanged(handler: OdinRoomUserDataChangedEvent): void; /** * Set handler for media activity events (voice activity detection). * @param handler - Handler receiving `{ peerId, mediaId, state }` */ onMediaActivity(handler: OdinMediaActivityEvent): void; /** * Set handler for peer tags changed events. * @param handler - Handler receiving `{ peerId, tags }` */ onPeerTagsChanged(handler: OdinPeerTagsChangedEvent): void; // ========================================= // Diagnostics and Statistics // ========================================= /** * Retrieves detailed connection statistics for the room. * Useful for monitoring network quality and debugging connectivity issues. * @returns Connection statistics object, or null if not connected. */ getConnectionStats(): OdinConnectionStats | null; /** * Retrieves the underlying connection identifier for the room. * @returns The connection ID, or 0 if not connected. */ getConnectionId(): number; /** * Retrieves jitter buffer statistics for a specific media stream. * Useful for debugging audio quality issues. * @param mediaId - The media ID to get jitter stats for. * @returns Jitter statistics object, or null if decoder not found. */ getJitterStats(mediaId: number): OdinJitterStats | null; } /** * Connection statistics returned by getConnectionStats(). * Provides detailed metrics about the underlying UDP connection. */ export declare interface OdinConnectionStats { /** * The number of outgoing UDP datagrams sent. */ udpTxDatagrams: number; /** * The total number of bytes sent in outgoing UDP datagrams. */ udpTxBytes: number; /** * The packet loss percentage for outgoing UDP datagrams (0.0 to 1.0). */ udpTxLoss: number; /** * The number of incoming UDP datagrams received. */ udpRxDatagrams: number; /** * The total number of bytes received in incoming UDP datagrams. */ udpRxBytes: number; /** * The packet loss percentage for incoming UDP datagrams (0.0 to 1.0). */ udpRxLoss: number; /** * The current congestion window size in bytes. */ cwnd: number; /** * The number of congestion events that have occurred. */ congestionEvents: number; /** * The current round-trip time (RTT) in milliseconds. */ rtt: number; } /** * Jitter buffer statistics returned by getJitterStats(). * Provides detailed metrics about audio packet reception and processing. */ export declare interface OdinJitterStats { /** * The total number of packets seen by the jitter buffer. */ packetsTotal: number; /** * The number of packets currently buffered. */ packetsBuffered: number; /** * The number of packets successfully processed. */ packetsProcessed: number; /** * The number of packets dropped because they arrived too early. */ packetsArrivedTooEarly: number; /** * The number of packets dropped because they arrived too late. */ packetsArrivedTooLate: number; /** * The number of packets dropped due to jitter buffer reset. */ packetsDropped: number; /** * The number of packets marked as invalid. */ packetsInvalid: number; /** * The number of duplicate packets received. */ packetsRepeated: number; /** * The number of packets lost during transmission. */ packetsLost: number; }