Skip to content

Commit

Permalink
Add TypeScript interfaces for all exported classes
Browse files Browse the repository at this point in the history
## Details

- `mediasoup.types` namespace no longer exports `Worker`, `Router`, `Producer`, etc classes in `types` but instead it exports `WorkerInterface`, `RouterInterface`, `ProducerInterface`, etc.
  - NOTE: This is a breaking change somehow, but just at TS level.
- Use the new interfaces everywhere.
- This makes it easier for applications to create mediasoup mocks.

## TODO

- How to export as interface/types the functions and consts exposed by `index.ts` such as `createWorker()`, `setLogEventListeners()`, `getSupportedRtpCapabilities()`, `observer`, etc?
- In the future we may want to move those interfaces/types to another separate package so a Node application that only depends on mediasoup types doesn't need to install mediaosup. Even if we don't do that, we should change things so `types.ts` doesn't export **anything** that depends on compiled FBS types.
  • Loading branch information
ibc committed Nov 8, 2024
1 parent e062161 commit 52eeb36
Show file tree
Hide file tree
Showing 47 changed files with 3,308 additions and 2,232 deletions.
58 changes: 17 additions & 41 deletions node/src/ActiveSpeakerObserver.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,33 @@
import { Logger } from './Logger';
import { EnhancedEventEmitter } from './enhancedEvents';
import {
RtpObserver,
RtpObserverEvents,
RtpObserverObserverEvents,
RtpObserverConstructorOptions,
} from './RtpObserver';
import { Producer } from './Producer';
ActiveSpeakerObserverInterface,
ActiveSpeakerObserverDominantSpeaker,
ActiveSpeakerObserverEvents,
ActiveSpeakerObserverObserver,
ActiveSpeakerObserverObserverEvents,
} from './ActiveSpeakerObserverInterface';
import { RtpObserverInterface } from './RtpObserverInterface';
import { RtpObserver, RtpObserverConstructorOptions } from './RtpObserver';
import { AppData } from './types';
import { Event, Notification } from './fbs/notification';
import * as FbsActiveSpeakerObserver from './fbs/active-speaker-observer';

export type ActiveSpeakerObserverOptions<
ActiveSpeakerObserverAppData extends AppData = AppData,
> = {
interval?: number;

/**
* Custom application data.
*/
appData?: ActiveSpeakerObserverAppData;
};

export type ActiveSpeakerObserverDominantSpeaker = {
/**
* The audio Producer instance.
*/
producer: Producer;
};

export type ActiveSpeakerObserverEvents = RtpObserverEvents & {
dominantspeaker: [ActiveSpeakerObserverDominantSpeaker];
};

export type ActiveSpeakerObserverObserver =
EnhancedEventEmitter<ActiveSpeakerObserverObserverEvents>;

export type ActiveSpeakerObserverObserverEvents = RtpObserverObserverEvents & {
dominantspeaker: [ActiveSpeakerObserverDominantSpeaker];
};

type RtpObserverObserverConstructorOptions<ActiveSpeakerObserverAppData> =
RtpObserverConstructorOptions<ActiveSpeakerObserverAppData>;

const logger = new Logger('ActiveSpeakerObserver');

export class ActiveSpeakerObserver<
ActiveSpeakerObserverAppData extends AppData = AppData,
> extends RtpObserver<
ActiveSpeakerObserverAppData,
ActiveSpeakerObserverEvents,
ActiveSpeakerObserverObserver
> {
ActiveSpeakerObserverAppData extends AppData = AppData,
>
extends RtpObserver<
ActiveSpeakerObserverAppData,
ActiveSpeakerObserverEvents,
ActiveSpeakerObserverObserver
>
implements RtpObserverInterface, ActiveSpeakerObserverInterface
{
/**
* @private
*/
Expand Down
47 changes: 47 additions & 0 deletions node/src/ActiveSpeakerObserverInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { EnhancedEventEmitter } from './enhancedEvents';
import {
RtpObserverInterface,
RtpObserverEvents,
RtpObserverObserverEvents,
} from './RtpObserverInterface';
import { ProducerInterface } from './ProducerInterface';
import { AppData } from './types';

export type ActiveSpeakerObserverOptions<
ActiveSpeakerObserverAppData extends AppData = AppData,
> = {
interval?: number;

/**
* Custom application data.
*/
appData?: ActiveSpeakerObserverAppData;
};

export type ActiveSpeakerObserverDominantSpeaker = {
/**
* The audio Producer instance.
*/
producer: ProducerInterface;
};

export type ActiveSpeakerObserverEvents = RtpObserverEvents & {
dominantspeaker: [ActiveSpeakerObserverDominantSpeaker];
};

export type ActiveSpeakerObserverObserver =
EnhancedEventEmitter<ActiveSpeakerObserverObserverEvents>;

export type ActiveSpeakerObserverObserverEvents = RtpObserverObserverEvents & {
dominantspeaker: [ActiveSpeakerObserverDominantSpeaker];
};

export interface ActiveSpeakerObserverInterface<
ActiveSpeakerObserverAppData extends AppData = AppData,
> extends RtpObserverInterface<
ActiveSpeakerObserverAppData,
ActiveSpeakerObserverEvents,
ActiveSpeakerObserverObserver
> {
get observer(): ActiveSpeakerObserverObserver;
}
85 changes: 21 additions & 64 deletions node/src/AudioLevelObserver.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,35 @@
import { Logger } from './Logger';
import { EnhancedEventEmitter } from './enhancedEvents';
import {
RtpObserver,
RtpObserverEvents,
RtpObserverObserverEvents,
RtpObserverConstructorOptions,
} from './RtpObserver';
import { Producer } from './Producer';
AudioLevelObserverInterface,
AudioLevelObserverVolume,
AudioLevelObserverEvents,
AudioLevelObserverObserver,
AudioLevelObserverObserverEvents,
} from './AudioLevelObserverInterface';
import { RtpObserverInterface } from './RtpObserverInterface';
import { RtpObserver, RtpObserverConstructorOptions } from './RtpObserver';
import { ProducerInterface } from './ProducerInterface';
import { AppData } from './types';
import * as utils from './utils';
import { Event, Notification } from './fbs/notification';
import * as FbsAudioLevelObserver from './fbs/audio-level-observer';

export type AudioLevelObserverOptions<
AudioLevelObserverAppData extends AppData = AppData,
> = {
/**
* Maximum number of entries in the 'volumes”' event. Default 1.
*/
maxEntries?: number;

/**
* Minimum average volume (in dBvo from -127 to 0) for entries in the
* 'volumes' event. Default -80.
*/
threshold?: number;

/**
* Interval in ms for checking audio volumes. Default 1000.
*/
interval?: number;

/**
* Custom application data.
*/
appData?: AudioLevelObserverAppData;
};

export type AudioLevelObserverVolume = {
/**
* The audio Producer instance.
*/
producer: Producer;

/**
* The average volume (in dBvo from -127 to 0) of the audio Producer in the
* last interval.
*/
volume: number;
};

export type AudioLevelObserverEvents = RtpObserverEvents & {
volumes: [AudioLevelObserverVolume[]];
silence: [];
};

export type AudioLevelObserverObserver =
EnhancedEventEmitter<AudioLevelObserverObserverEvents>;

export type AudioLevelObserverObserverEvents = RtpObserverObserverEvents & {
volumes: [AudioLevelObserverVolume[]];
silence: [];
};

type AudioLevelObserverConstructorOptions<AudioLevelObserverAppData> =
RtpObserverConstructorOptions<AudioLevelObserverAppData>;

const logger = new Logger('AudioLevelObserver');

export class AudioLevelObserver<
AudioLevelObserverAppData extends AppData = AppData,
> extends RtpObserver<
AudioLevelObserverAppData,
AudioLevelObserverEvents,
AudioLevelObserverObserver
> {
AudioLevelObserverAppData extends AppData = AppData,
>
extends RtpObserver<
AudioLevelObserverAppData,
AudioLevelObserverEvents,
AudioLevelObserverObserver
>
implements RtpObserverInterface, AudioLevelObserverInterface
{
/**
* @private
*/
Expand Down Expand Up @@ -125,7 +80,9 @@ export class AudioLevelObserver<
volume,
})
)
.filter(({ producer }: { producer: Producer }) => producer);
.filter(
({ producer }: { producer: ProducerInterface }) => producer
);

if (volumes.length > 0) {
this.safeEmit('volumes', volumes);
Expand Down
69 changes: 69 additions & 0 deletions node/src/AudioLevelObserverInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { EnhancedEventEmitter } from './enhancedEvents';
import {
RtpObserverInterface,
RtpObserverEvents,
RtpObserverObserverEvents,
} from './RtpObserverInterface';
import { ProducerInterface } from './ProducerInterface';
import { AppData } from './types';

export type AudioLevelObserverOptions<
AudioLevelObserverAppData extends AppData = AppData,
> = {
/**
* Maximum number of entries in the 'volumes”' event. Default 1.
*/
maxEntries?: number;

/**
* Minimum average volume (in dBvo from -127 to 0) for entries in the
* 'volumes' event. Default -80.
*/
threshold?: number;

/**
* Interval in ms for checking audio volumes. Default 1000.
*/
interval?: number;

/**
* Custom application data.
*/
appData?: AudioLevelObserverAppData;
};

export type AudioLevelObserverVolume = {
/**
* The audio Producer instance.
*/
producer: ProducerInterface;

/**
* The average volume (in dBvo from -127 to 0) of the audio Producer in the
* last interval.
*/
volume: number;
};

export type AudioLevelObserverEvents = RtpObserverEvents & {
volumes: [AudioLevelObserverVolume[]];
silence: [];
};

export type AudioLevelObserverObserver =
EnhancedEventEmitter<AudioLevelObserverObserverEvents>;

export type AudioLevelObserverObserverEvents = RtpObserverObserverEvents & {
volumes: [AudioLevelObserverVolume[]];
silence: [];
};

export interface AudioLevelObserverInterface<
AudioLevelObserverAppData extends AppData = AppData,
> extends RtpObserverInterface<
AudioLevelObserverAppData,
AudioLevelObserverEvents,
AudioLevelObserverObserver
> {
get observer(): AudioLevelObserverObserver;
}
Loading

0 comments on commit 52eeb36

Please sign in to comment.