UNPKG

@huddle01/web-core

Version:

The Huddle01 Javascript SDK offers a comprehensive suite of methods and event listeners that allow for seamless real-time audio and video communication with minimal coding required.

266 lines (263 loc) 10.4 kB
import * as mediasoup from 'mediasoup-client'; import { EnhancedEventEmitter } from './common-js/EnhancedEventEmitter.cjs'; import { TTransportType, TSDPInfo, TConsumeResponseData, TConsumeDataResponseData } from 'types/dist/common.types'; import { Producer, AppData, DataConsumer } from 'mediasoup-client/lib/types'; import Socket from './Socket.cjs'; import Consumer from './Consumer.cjs'; import { EnhancedMap } from './common-js/EnhancedMap.cjs'; import { SupportedCodecs } from './types/common.types.cjs'; import 'types/dist/clientToSushiEvents'; import 'types/dist/sushiToClientEvents'; import 'types/dist/socket.types'; import 'mediasoup-client/lib/RtpParameters'; import './RemotePeer.cjs'; type TransportEvents = { connectTransportResponse: []; }; type CreateDataProducerType = { label: string; ordered?: boolean; maxRetransmits?: number; maxPacketLifeTime?: number; }; declare class Transport extends EnhancedEventEmitter<TransportEvents> { /** * Peer Id, which handles the peer id. */ readonly peerId: string; /** * MediaSoup Device Instance, which handles the browsers or mobile device init. */ private __device; /** * MediaSoup Transport Instance, which handles the media transport. */ private __mediasoupTransport; /** * Socket Instance, which handles the socket connection. */ private readonly __socket; /** * Map of Producers, which handles the producers. ( Sending out Media Streams ) * * `Mapped with {producerId => Producer}` */ private readonly __producers; /** * Map of DataProducers, which handles the dataProducers. ( Sending out Data ) * * `Mapped with {label => DataProducer}` */ private readonly __dataProducers; /** * Map of DataConsumers, which handles the dataConsumers. ( Receiving Media Streams ) * * `Mapped with {label:label => DataConsumer}` */ private readonly __dataConsumers; /** * Map of Consumers, which handles the consumers. ( Receiving Media Streams ) * * `Mapped with {label:RemotePeerId => Consumer}` */ private readonly __consumers; /** * Map of Identifiers to Producer Ids, which handles the mapping of identifiers to producer ids. * * `identifiers` are the unique identifiers for the stream, which is used to identify the stream. */ readonly labelToProducerId: Map<string, string>; /** * Transport Type, which handles the transport type. ( `send | recv` ) */ readonly transportType: TTransportType; /** * Pending Producer Tasks, which handles the pending producer tasks. * callback function is necessary to be called when the producer is created * on the server as well as on the client side. */ private __pendingProducerTasks; /** * Debounce to handle concurrent request to restart Ice. Waits for some time before sending * more requests to restart ice. */ private __iceRestartDebounce; /** * WebRTC Device whose APIs for Connection Creation is used. */ get device(): mediasoup.types.Device; /** * WebRTC Connection, Which handles the producing and consuming of media. */ get mediasoupTransport(): mediasoup.types.Transport<mediasoup.types.AppData>; /** * WebRTC Connection State. */ get connectionState(): mediasoup.types.ConnectionState; /** * Returns the Map of Producers, which handles the producers. ( Sending out Media Streams ) */ get producers(): Map<string, mediasoup.types.Producer<mediasoup.types.AppData>>; /** * Returns the ids of the producers, which handles the producers. ( Sending out Media Streams ) */ get producerIds(): string[]; /** * Type of data which is supposed to be produced in the room. ( ArrayBuffer ) */ get dataProducers(): Map<string, mediasoup.types.DataProducer<mediasoup.types.AppData>>; /** * Type of data which is supposed to be consumed from the room. ( ArrayBuffer ) */ get dataConsumers(): Map<string, mediasoup.types.DataConsumer<mediasoup.types.AppData>>; /** * Entity which is supposed to consume the media stream. ( MediaStreamTrack ) */ get consumers(): EnhancedMap<Consumer>; /** * Get a producer by its id ( ProducerId is generated by the Media Node ) * @param producerId - Producer Id of the producer to be fetched. * @returns - Producer * @throws - Throws an error if the producer is not found. */ getProducerById(producerId: string): Producer; /** * Get the consumer by label and peerId * @param data * @returns Consumer | null; Returns null if consumer is not found */ getConsumer: (data: { label: string; peerId: string; }) => Consumer | null; /** * Getter for the transport instance. */ get transport(): mediasoup.types.Transport; /** * Every time a producer is created, it is added to the pending producer tasks. * Upon ack from the Media Node the producer is resolved and removed from the pending producer tasks. ( Using resolvePendingProducerTask ) * This is done to handle concurrency in the producer creation. * @param data - { label?: string, peerId: string, callback: ({ id }) => void } * @returns - void */ addPendingProducerTask: (data: { label?: string; peerId: string; callback: ({ id, }: { id: string; }) => void; }) => void; /** * Every time a producer is created, it is added to the pending producer tasks. * Upon ack from the Media Node the producer is resolved and removed from the pending producer tasks. * This is done to handle concurrency in the producer creation. * @param data - { label: string, peerId: string, id: string } * @returns - void */ resolvePendingProducerTask: (data: { label: string; peerId: string; id: string; }) => void; /** * Creates a new Transport Instance, Transports are used to send and receive media streams or data. * Transport for the SDK mean a WebRTC Connection. * @param data * @returns */ static create: (data: { peerId: string; sdpInfo: TSDPInfo; device: mediasoup.Device; iceServers: RTCIceServer[]; transportType: TTransportType; socket: Socket; }) => Transport; private constructor(); /** * Function to handle the event when transport is supposed to be connected. ( WebRTC ) */ private __listenTransportConnect; /** * Function to handle the event when media is supposed produced over a Transport ( WebRTC ) */ private __listenTransportProduce; /** * Function to handle the event when data is supposed produced over a Transport ( WebRTC DataChannel ) */ private __listenTransportDataProduce; /** * Function to handle the producing of a media stream to the room. * @param data - { stream: MediaStream, label: string, stopTrackOnClose: boolean, appData?: AppData } * @returns - Producer * NOTE: PrefferedCodec is optional, if not provided, H264 is used as the default codec. * is device does not support the passed codec, an error is thrown. */ produce: (data: { stream: MediaStream; label: string; stopTrackOnClose: boolean; appData?: AppData; prefferedCodec?: SupportedCodecs; }) => Promise<mediasoup.types.Producer<{ producerPeerId: string; }>>; /** * Function to handle the producing of a data stream to the room. * @param data - { label: string, ordered?: boolean, maxRetransmits?: number, maxPacketLifeTime?: number } * - `label` is the unique identifier for the data stream, Cannot use `bot` which is reserved. * - `ordered` is the boolean value to determine if the data should be ordered or not, or reliably. * - `maxRetransmits` When ordered is false indicates the maximum number of times a packet will be retransmitted. * - `maxPacketLifeTime` When ordered is false indicates the time (in milliseconds) after which a SCTP packet will stop being retransmitted. * @returns - DataProducer */ produceData: (options: CreateDataProducerType) => Promise<mediasoup.types.DataProducer<mediasoup.types.AppData>>; /** * @description - Close the data producer by label * @param label - Label of the data producer to be closed */ closeDataProducer: (label: string) => void; /** * Consume means to receive the media stream from the room. ( MediaStreamTrack ) * Using WebRTC Connection to receive the media stream. * @param data - { producerPeerId: string, kind: string, rtpParameters: any, appData?: AppData } * @returns - { consumer: Consumer, mediaSoupConsumer: mediasoup.types.Consumer } */ consume: (data: TConsumeResponseData & { producerPaused?: boolean; }) => Promise<{ consumer: Consumer; mediaSoupConsumer: mediasoup.types.Consumer<AppData>; }>; /** * Consume means to receive the data stream from the room. ( ArrayBuffer ) * Using WebRTC Connection to receive the data stream. * @param data - { label: string, appData?: AppData, dataProducerId: string, protocol: string, id: string, peerId: string, sctpStreamParameters: any } * @returns - DataConsumer */ consumeData: (data: TConsumeDataResponseData) => Promise<DataConsumer>; /** * Close the consumer by label and peerId * @param data - { label: string, peerId: string } */ closeConsumer: (data: { label: string; peerId: string; }) => void; /** * Close the underlying transport * @param data - { retries: number } * @returns - void */ close: (data: { retries: number; }) => Promise<void>; /** * WebRTC Connection is prone to connection state changes, as its a peer to peer connection. * This function handles the connection state changes. and re establishes the connection if it is lost. * @param state - ConnectionState */ private __connectionStateChangeHandler; } export { type CreateDataProducerType, type TransportEvents, Transport as default };