Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@observertc/sfu-monitor-js

observertc219Apache-2.02.0.3TypeScript support: included

ObserveRTC SFU Integration Core Library

webrtc, getStats, observertc, sfu, integration

readme

Javascript library to monitor Selective Forwarding Units (SFU)

@observertc/sfu-monitor-js is a JavaScript library to monitor your SFU and integrate with ObserveRTC.

Table of Contents:

Quick Start

Install the library by using NPM:

npm i @observertc/sfu-monitor-js

Once the library is installed, you need to integrate it. The first step is to set up a monitor in your application.

import * as ObserveRTC from '@observertc/sfu-monitor-js';

// Quick start to create a monitor and log the collected stats
const monitor = ObserveRTC.createSfuMonitor({
  collectingPeriodInMs: 2000,
  samplingPeriodInMs: 4000,
});

monitor.on('sample-created', ({ sfuSample }) => {
  console.log('The created sample', sfuSample);
});

A monitor does the following:

  1. Collect Stats
  2. Make sample based on the collected stats

Integrate Mediasoup

To integrate mediasoup:3^ you can use the built-in MediasoupCollector.

import * as mediasoup from 'mediasoup';

const sfuMonitor = createSfuMonitor({
  collectingPeriodInMs: 5000,
});

const mediasoupCollector = sfuMonitor.createMediasoupCollector({
  mediasoup,
});

With this configuration, the monitor automatically adds and removes mediasoup objects.

monitor.on('stats-collected', () => {
    const storage = monitor.storage
    console.log(`Mediasoup SFU has ${storage.getNumberOfInboundRtpPads()} incoming RTP stream`);
    console.log(`Mediasoup SFU has ${storage.getNumberOfOutboundRtpPads()} outgoing RTP stream`);
    console.log(`Mediasoup SFU has ${storage.getNumberOfTransports()} transports`);
});

By default, it does not call getStats as it turned out to be performance intensive in some cases. If you want the collector to call the getStats on objects, you can change the configuration like:

const mediasoupCollector = sfuMonitor.createMediasoupCollector({
    mediasoup,
    mediasoupCollector: {
        pollWebRtcTransportStats: (transportId) => true,
        pollPlainRtpTransportStats: (transportId) => true,
        pollPipeTransportStats: (transportId) => true,
        pollDirectTransportStats: (transportId) => true,
        pollProducerStats: (producerId) => true,
        pollConsumerStats: (consumerId) => true,
        pollDataProducerStats: (dataProducerId) => true,
        pollDataConsumerStats: (dataProducerId) => true,
    }
});

Integrate other type of SFUs

To have a custom integration, you could use AuxCollector as follows:


const collector = sfuMonitor.createAuxCollector();

collector.addTransportStatsSupplier("myUniqueGeneratedTransportId", async () => {
    const stats: SfuTransport = {

    };
    return stats;
});
// when you want to remove it:
collector.removeTransportStatsSupplier(transportId);

// similarly:
collector.addInboundRtpPadStatsSupplier("padId", ...);
collector.removeInboundRtpPadStatsSupplier("padId");

collector.addOutboundRtpPadStatsSupplier("padId", ...);
collector.removeOutboundRtpPadStatsSupplier("padId");

collector.addSctpStreamStatsSupplier("channelId", ...);
collector.removeSctpStreamSupplier("channelId");

Configurations

const config = {
    /**
  * The identifier of the SFU.
  *
  * DEFAULT: a generated unique value
  */
  sfuId: 'my-sfu-id',

  /**
  * Sets the default logging level for sfu-monitor-js
  * 
  * DEFAULT: warn
  */
  logLevel: LogLevel,

  /**
  * Sets the maximum number of listeners for event emitters
  */
  maxListeners: 1000,

  /**
  * Set the ticking time of the timer invokes processes for collecting, sampling, and sending.
  * 
  * DEFAULT: 1000
  */
  tickingTimeInMs: 1000,

  /**
  * By setting it, the observer calls the added statsCollectors periodically
  * and pulls the stats.
  *
  * DEFAULT: undefined
  */
  collectingPeriodInMs: 1000,
  /**
  * By setting it, the observer make samples periodically.
  *
  * DEFAULT: undefined
  */
  samplingPeriodInMs: 1000,

  /**
  * By setting it, the observer sends the samples periodically.
  *
  * DEFAULT: undefined
  */
  sendingPeriodInMs: 1000,

  /**
  * Limits the number of stats polled at once from the collectors. 
  * 
  * DEFAULT: 50
  */
  pollingBatchSize: 50,

  /**
  * Pacing time between polling batches of stats
  * 
  * DEFAULT: undefined
  */
  pollingBatchPaceTimeInMs: 50,

  /**
  * Flag indicating if the monitor creates sfu events.
  * If true, events happening on the collected sources create sfu events such as SFU_TRANSPORT_OPENED, SFU_TRANSPORT_CLOSED.
  * 
  * If this flag is false, the application is responsible for adding sfu events by calling the appropriate SfuMonitor method for the corresponding event.
  * 
  * DEFAULT: false
  */
  createSfuEvents: true,

  /**
  * Configuration for the samples accumulator to balance the transfer the size of the Samples 
  * prepared to be sent to the server
  * 
  */
  accumulator: {
    /**
     * Sets the maximum number of client sample allowed to be in one Sample
     * 
     * DEFAULT: 100
     */
    maxClientSamples: 100,

    /**
     * Sets the maximum number of Samples the accumulator can hold
     * 
     * DEFAULT: 10
     */
    maxSamples: 10,

    /**
     * Forward a Sample to the server even if it is empty
     * 
     * DEFAULT: false
     */
    forwardIfEmpty: false
  }
};

API docs

https://observertc.org/docs/api/sfu-monitor-js-v2/

NPM package

https://www.npmjs.com/package/@observertc/sfu-monitor-js

Schemas

https://github.com/observertc/schemas

Getting Involved

Sfu-monitor is designed to provide an open-source monitoring solution for WebRTC developers. We develop new features and maintain the current product with the help of the community. If you are interested in getting involved, please read our contribution guidelines.

Generate docs:

typedoc --out docs ./src

License

Apache-2.0

changelog

2.0.2

  • Keep polled stats in the storage even if a consecutive poll indicate noReport until sampled

2.0.1

  • expose (forgotten) event handler methods for SfuMonitor: on, once, off

2.0.0

Conceptual changes

  • The SfuMonitor is no longer responsible for WebSocket connections, signaling, and transports.
  • The SfuMonitor has become responsible for the following event emissions:
    • SFU_TRANSPORT_OPENED, SFU_TRANSPORT_CLOSED
    • SFU_RTP_STREAM_ADDED, SFU_RTP_STREAM_REMOVED

Major Code changes

  • Removed Sender component and corresponding configuration from SfuMonitor.
  • Removed Transport component, as sending and transporting no longer fall under the responsibility of the SfuMonitor.
  • Removed events field from SfuMonitor, as events have become part of the SfuMonitor itself, and SfuMonitor now provides on, off, once interfaces for events.

Functionality changes

  • Storage entries are removed based on visited ids from collectors. If a stat is no longer present in the collect() extracted result, it is removed from the Storage.

Configuration changes

  • Sampler configuration is reduced.
  • Sender configuration is removed.
  • statsExpirationTimeInMs is removed.
  • createSfuEvents is added.

1.1.1

  • Fix problem of setting statswriter twice
  • Add MonitorMetrics for self checking purposes
  • Set default statsExpirationTimeTo 60s
  • Set package version to 1.1.1
  • Fix schema version to 2.2.0

1.1.0

  • use schema 2.2.0
  • Add MediasoupMonitor, as a specialized SfuMonitor
  • Change Collectors interface, from now it is accessable through monitor.collectors
  • Export CustomSfuEvent from schema 2.2.0, and create method addCustomSfuEvent to the monitor
  • bugfix for PromiseFetcher if the min and max pacing time is equal
  • Make sample method sync instead of async
  • Fix bug of Timer stays live even there is no listener
  • Add tests for mediasoup monitor and collectors
  • Make mediasoup integration one line and everything is watched
  • Send keepalive samples for in-, and outbound rtp pad when polling stats and no bytes are flowing on them

1.0.0

Init