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.

210 lines (206 loc) 7.84 kB
'use strict'; var chunkO5HDCTOE_cjs = require('./chunk-O5HDCTOE.cjs'); var chunkUIH7JJYU_cjs = require('./chunk-UIH7JJYU.cjs'); // src/Consumer.ts var logger = chunkO5HDCTOE_cjs.mainLogger.createSubLogger("Consumer"); var Consumer = class _Consumer extends chunkUIH7JJYU_cjs.EnhancedEventEmitter { /** * ProducerId of the Consumer, this is the id of the Media Entity which is responsible for producing the media in the room. */ producerId; /** * PeerId of the Producer, this is the peerId of the Peer which is responsible for producing the media in the room. */ producerPeerId; /** * Label of the Consumer, this is the label of the Media Entity which is responsible for producing the media in the room. */ label; /** * Flag to check if the Producer which is being consumed by the Consumer is paused. * @remarks If the Producer is paused, the Consumer will not be able to consume the media. * @default false */ __producerPaused = false; /** * Flag to check if the Consumer is consuming a media, if `true` then the Consumer is consuming a media. * Consuming Means the incoming media stream is being consumed by the Remote Peer if the Remote Peer is Producing any media. * @default false */ __consuming = false; /** * Flag to check if the Consumer is consuming a media, if `true` then the Consumer is consuming a media. * * @default false * * @remarks * There are two ways a Peer can Consume any media produced in the room. * - Automatically consume the media streams of the remote peers by setting the `autoConsume` flag to `true` in the `Room`. * - Using the `consume` function inside the `LocalPeer` instance. `localPeer.consume({ peerId, label: "video", appData: {} });` */ get consuming() { return this.__consuming; } /** * Flag to check if the Producer which is being consumed by the Consumer is paused. * @remarks If the Producer is paused, the Consumer will not be able to consume the media. * @default false */ get producerPaused() { return this.__producerPaused; } /** * mediasoupConsumer instance, this is the instance of the mediasoupConsumer which is responsible for consuming the media in the room. * @remarks This is a private property and should not be accessed directly. * * Every Consumer is created without a mediasoupConsumer, when the peer starts to consume the media, the mediasoupConsumer is set. */ #mediasoupConsumer = null; /** * Getter for the id for the mediaSoupConsumer, which is also the id of the Consumer for the RemotePeer. */ get id() { return this.#mediasoupConsumer?.id; } /** * * @param consumer Sets the mediasoupConsumer for the Consumer */ setMediaSoupConsumer(consumer) { if (this.consuming) { throw new Error("Consumer is already consuming"); } if (this.#mediasoupConsumer) { throw new Error("Consumer already has a mediasoupConsumer"); } this.#mediasoupConsumer = consumer; } /** * Getter for the mediasoupConsumer id, which is also the id of the Consumer for the RemotePeer. it is only available when the Consumer is consuming a media. * * @remarks * There are two ways a Peer can Consume any media produced in the room. * - Automatically consume the media streams of the remote peers by setting the `autoConsume` flag to `true` in the `Room`. * - Using the `consume` function inside the `LocalPeer` instance. `localPeer.consume({ peerId, label: "video", appData: {} });` */ get consumerId() { return this.#mediasoupConsumer?.id; } /** * Get the Track of the Consumer, it is only available when the Consumer is consuming a media. * * @remarks * There are two ways a Peer can Consume any media produced in the room. * - Automatically consume the media streams of the remote peers by setting the `autoConsume` flag to `true` in the `Room`. * - Using the `consume` function inside the `LocalPeer` instance. `localPeer.consume({ peerId, label: "video", appData: {} });` */ get track() { return this.#mediasoupConsumer?.track; } /** * Get the kind of the Consumer, it is only available when the Consumer is consuming a media. * * @remarks * There are two ways a Peer can Consume any media produced in the room. * - Automatically consume the media streams of the remote peers by setting the `autoConsume` flag to `true` in the `Room`. * - Using the `consume` function inside the `LocalPeer` instance. `localPeer.consume({ peerId, label: "video", appData: {} });` */ get kind() { return this.#mediasoupConsumer?.kind; } /** * If the Consumer is paused, it is only available when the Consumer is consuming a media. * * if paused the user is not consuming any media for the given producerId. */ get paused() { return this.#mediasoupConsumer?.paused; } /** * AppData of the Consumer, it is only available when the Consumer is consuming a media. */ get appData() { return this.#mediasoupConsumer?.appData; } /** * State of a Consumer is defined by the following: * - `playable` - The Consumer is ready to play the media. * - `unavailable` - The Consumer is not available to play the media. This can happen when the Consumer is closed or paused. * - `paused` - The Consumer is paused and is not playing the media. * - `available` - The Consumer is available to play the media. Peer can consume the media by using `localPeer.consume({ peerId, label: "video", appData: {} });` after which the state will change to `playable`. */ get state() { if (this?.consuming) return "playable"; if (this?.paused) return "paused"; return "available"; } /** * Get the stats of the Consumer, it is only available when the Consumer is consuming a media. * It generates the stats for the Consumer using the `getStats` method of the mediasoupConsumer. * @returns - RTCStatsReport | null */ getStats = async () => { const stats = await this.#mediasoupConsumer?.getStats(); return stats; }; /** * Resume the consumer, if the state of the consumer is `paused`. * @returns - Returns `true` if the consumer is resumed, `false` if the consumer is not resumed. */ resume = () => { this.#mediasoupConsumer?.resume(); this.__consuming = true; return true; }; /** * Pause the consumer, if the state of the consumer is `playable`. * * @returns - Returns `true` if the consumer is paused, `false` if the consumer is not paused. */ pause = () => { this.#mediasoupConsumer?.pause(); this.__consuming = false; return true; }; /** * Removes all the eventListeners attached to the Consumer. */ removeListeners = () => { this.removeAllListeners(); }; /** * Creates a Consumer instance. This is a static method and should be called using `Consumer.create({ producerPeerId, producerId, label })`. */ static create = (data) => { try { const consumer = new _Consumer(data); return consumer; } catch (error) { logger.error(error); throw new Error("\u274C Error creating Consumer"); } }; close = () => { try { this.#mediasoupConsumer?.close(); this.__consuming = false; this.emit("closed"); this.removeAllListeners(); } catch (error) { logger.error("\u274C Error Closing Consumer"); logger.error({ consumerId: this.consumerId, producerId: this.producerId }); } }; constructor(data) { super(); this.producerPeerId = data.producerPeerId; this.producerId = data.producerId; this.label = data.label; this.__producerPaused = data.producerPaused ?? false; } }; var Consumer_default = Consumer; exports.Consumer_default = Consumer_default;