UNPKG

@rtco/peer

Version:
184 lines (179 loc) 4.98 kB
// Generated by dts-bundle-generator v9.5.1 import { EventEmitter } from 'eventemitter3'; declare enum LogLevel { /** * Prints no logs. */ Disabled = 0, /** * Prints only errors. */ Errors = 1, /** * Prints errors and warnings. */ Warnings = 2, /** * Prints info, errors and warnings. */ Info = 3, /** * Prints all logs (Info + Debug). */ All = 4 } export type Signal = { type: "candidate"; data: RTCIceCandidate; } | { type: "sdp"; data: RTCSessionDescription; }; export type PeerData = string | ArrayBuffer | Blob | ArrayBufferView; export interface PeerOptions { /** * The log level (0: none, 1: errors, 2: warnings, 3: info, 4: debug). * @defaultValue 1 */ debug: LogLevel; /** * Whether this peer is the initiator. * @defaultValue false */ initiator?: boolean; /** * Optional RTCConfiguration for the peer connection. * @defaultValue { iceServers: [ { urls: "stun:stun.l.google.com:19302" }, { urls: "stun:stun1.l.google.com:19302" } ] } * @see https://developer.mozilla.org/en-US/docs/Web/API/RTCConfiguration */ config?: RTCConfiguration; /** * Optional RTCDataChannelInit for the data channel. * @see https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannelInit * @defaultValue {} */ channelConfig?: RTCDataChannelInit; /** * Optional name for the data channel. * @defaultValue "dc_${randomToken()}" */ channelName?: string; } export interface PeerEvents { /** * Emitted when the peer connection is closed. */ close: () => void; /** * Emitted when the data channel is connected. */ connect: () => void; /** * Emitted when data is received from the peer. * @param data - The data received from the peer. */ data: (data: PeerData) => void; /** * Emitted when an error occurs. * @param err - The error that occurred. */ error: (err: Error) => void; /** * Emitted when a stream is removed from the peer connection. * @param stream - The stream that was removed. */ removestream: (stream: MediaStream) => void; /** * Emitted when a track is removed from the peer connection. * @param track - The track that was removed. * @param stream - The stream that the track belonged to. */ removetrack: (track: MediaStreamTrack, stream: MediaStream) => void; /** * Emitted when a track is replaced in the peer connection. * @param oldTrack - The track that was replaced. * @param newTrack - The new track that was used. */ replacetrack: (oldTrack: MediaStreamTrack, newTrack: MediaStreamTrack) => void; /** * Emitted when a signal is received from the peer. * @param data - The signal received from the peer. */ signal: (data: Signal) => void; /** * Emitted when a stream is added to the peer connection. * @param stream - The stream that was added. */ stream: (stream: MediaStream) => void; /** * Emitted when a track is added to the peer connection. * @param track - The track that was added. * @param stream - The stream that the track belongs to. */ track: (track: MediaStreamTrack, stream: MediaStream) => void; } export interface IPeer { /** * Destroys the peer connection. * This will close the data channel and the peer connection. * Emits a "close" event. */ destroy(): void; /** * Deliver a remote signal to the local peer. * @param data - The signal to deliver. */ signal(data: Signal): Promise<void>; /** * Send a message to the remote peer. * @param data - The message to send. */ send(data: string): void; /** * Add a stream to the peer connection. * @param stream - The stream to add. */ addStream(stream: MediaStream): void; /** * Remove a stream from the peer connection. * @param stream - The stream to remove. */ removeStream(stream: MediaStream): void; /** * Add a track to the peer connection. * @param track - The track to add. * @param stream - The stream that the track belongs to. */ addTrack(track: MediaStreamTrack, stream: MediaStream): void; /** * Remove a stream from the peer connection. * @param stream - The stream to remove. */ removeTrack(track: MediaStreamTrack): void; /** * Replace a track in the peer connection. * @param oldTrack - The track to replace. * @param newTrack - The new track to use. */ replaceTrack(oldTrack: MediaStreamTrack, newTrack: MediaStreamTrack): Promise<void>; } /** * RTCPeerConnection abstraction. */ declare class Peer extends EventEmitter<PeerEvents> implements IPeer { #private; constructor(opts?: Partial<PeerOptions>); get ready(): boolean; destroy: () => void; signal: (signal: Signal) => Promise<void>; send(data: string): void; addStream: (stream: MediaStream) => void; removeStream: (stream: MediaStream) => void; addTrack: (track: MediaStreamTrack, stream: MediaStream) => void; removeTrack: (track: MediaStreamTrack) => void; replaceTrack: (oldTrack: MediaStreamTrack, newTrack: MediaStreamTrack) => Promise<void>; } export { Peer as default, }; export {};