Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Feature: added min spatial and min temporal for limit #43

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/lib/consumer-pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,27 @@ export class StreamConsumerPair extends TypedEventEmitter<IConsumerCallbacks> {
this.emit('quality', quality);
};

limit(key: string, priority: number = 50, maxSpatial: number = 2, maxTemporal: number = 2) {
this._videoConsumer.limit(key, priority, maxSpatial, maxTemporal);
limit(
key: string,
priority: number = 50,
minSpatial: number = 0,
maxSpatial: number = 2,
minTemporal: number = 0,
maxTemporal: number = 2,
) {
this._videoConsumer.limit(key, priority, minSpatial, maxSpatial, minTemporal, maxTemporal);
}

view(key: string, priority: number = 50, maxSpatial: number = 2, maxTemporal: number = 2): MediaStream {
view(
key: string,
priority: number = 50,
minSpatial: number = 0,
maxSpatial: number = 2,
minTemporal: number = 0,
maxTemporal: number = 2,
): MediaStream {
const audioStream = this._audioConsumer.view(key);
const videoStream = this._videoConsumer.view(key, priority, maxSpatial, maxTemporal);
const videoStream = this._videoConsumer.view(key, priority, minSpatial, maxSpatial, minTemporal, maxTemporal);
this._combinedStream.getTracks().forEach((track) => {
this._combinedStream.removeTrack(track);
});
Expand Down
42 changes: 35 additions & 7 deletions src/lib/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,21 @@ export class StreamConsumer extends TypedEventEmitter<IConsumerCallbacks> {
* Sets up a view for a specific viewer key.
* @param key - The key of the viewer.
* @param priority - The priority of the view (default: 50).
* @param maxSpatial - The maximum spatial layer for the view (default: 2).
* @param maxTemporal - The maximum temporal layer for the view (default: 2).
* @param minSpatial - The minimum spatial layer for the view (default: 0).
* @param maxSpatial - The maximum spatial limit (default: 2).
* @param minTemporal - The minimum temporal layer for the view (default: 0).
* @param maxTemporal - The maximum temporal limit (default: 2).
* @returns The MediaStream of the view.
*/
public view(key: string, priority: number = 50, maxSpatial: number = 2, maxTemporal: number = 2): MediaStream {
this.keys.set(key, { priority, maxSpatial, maxTemporal });
public view(
key: string,
priority: number = 50,
minSpatial: number = 0,
maxSpatial: number = 2,
minTemporal: number = 0,
maxTemporal: number = 2,
): MediaStream {
this.keys.set(key, { priority, maxSpatial, maxTemporal, minSpatial, minTemporal });
if (!this.receiver) {
this.receiver = this._session.takeReceiver(this._remote.kind);
this.receiver.on('state', this.onReceiverStateChanged);
Expand All @@ -54,11 +63,20 @@ export class StreamConsumer extends TypedEventEmitter<IConsumerCallbacks> {
* Sets the limit for a specific view by key.
* @param key - The key of the view to set the limit for.
* @param priority - The priority of the view (default: 50).
* @param minSpatial - The minimum spatial layer for the view (default: 0).
* @param maxSpatial - The maximum spatial limit (default: 2).
* @param minTemporal - The minimum temporal layer for the view (default: 0).
* @param maxTemporal - The maximum temporal limit (default: 2).
*/
public limit(key: string, priority: number = 50, maxSpatial: number = 2, maxTemporal: number = 2) {
this.keys.set(key, { priority, maxSpatial, maxTemporal });
public limit(
key: string,
priority: number = 50,
minSpatial: number = 0,
maxSpatial: number = 2,
minTemporal: number = 0,
maxTemporal: number = 2,
) {
this.keys.set(key, { priority, maxSpatial, maxTemporal, minSpatial, minTemporal });
this.configLayer();
}

Expand Down Expand Up @@ -93,12 +111,22 @@ export class StreamConsumer extends TypedEventEmitter<IConsumerCallbacks> {
let selectedPriority = 0;
let selectedMaxSpartial = 0;
let selectedMaxTemporal = 0;
let selectedMinSpartial = 0;
let selectedMinTemporal = 0;
Array.from(this.keys.values()).map((viewer) => {
selectedPriority = Math.max(selectedPriority, viewer.priority);
selectedMaxSpartial = Math.max(selectedMaxSpartial, viewer.maxSpatial);
selectedMaxTemporal = Math.max(selectedMaxTemporal, viewer.maxTemporal);
selectedMinSpartial = Math.max(selectedMinSpartial, viewer.minSpatial);
selectedMinTemporal = Math.max(selectedMinTemporal, viewer.minTemporal);
});
this.receiver?.limit(selectedPriority, selectedMaxSpartial, selectedMaxTemporal);
this.receiver?.limit(
selectedPriority,
selectedMinSpartial,
selectedMaxSpartial,
selectedMinTemporal,
selectedMaxTemporal,
);
}

private onReceiverAudioLevelChanged = (level: number) => {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/interfaces/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import type { StreamReceiverState } from './receiver';

export interface ViewInfo {
priority: number;
minSpatial: number;
maxSpatial: number;
minTemporal: number;
maxTemporal: number;
}

Expand Down
12 changes: 9 additions & 3 deletions src/lib/interfaces/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ export interface IStreamReceiver extends TypedEventEmitter<IStreamReceiverCallba
* `temporal` is a value indicating the smoothness, or frame rate of the stream.
*
* @param priority The priority of the stream to limit.
* @param max_spatial The maximum spatial value.
* @param max_temporal The maximum temporal value.
* @param maxSpatial The maximum spatial value.
* @param maxTemporal The maximum temporal value.
* @returns A promise that resolves to a boolean indicating whether the limit was successful.
*/
limit(priority: number, max_spatial: number, max_temporal: number): Promise<boolean>;
limit(
priority: number,
minSpatial: number,
maxSpatial: number,
minTemporal: number,
maxTemporal: number,
): Promise<boolean>;

/**
* Stops the stream.
Expand Down
14 changes: 11 additions & 3 deletions src/lib/interfaces/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,17 @@ export interface ISessionConfig {
*
*/
senders?: SenderConfig[];
receivers: {
audio: number;
video: number;

/**
* The number of receivers the session will be pre-allocated at connect time.
* This is so that the media server can pre-allocate the resources for the session.
* You can also dynamically add receivers later.
* @example { audio: 1, video: 1 }
*
*/
receivers?: {
audio?: number;
video?: number;
};

/**
Expand Down
21 changes: 13 additions & 8 deletions src/lib/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,21 @@ export class Session extends TypedEventEmitter<ISessionCallbacks> {
}
}
});
for (let i = 0; i < this._cfg.receivers.video; i++) {
const recvrTrack = this._socket.createReceiverTrack(`video_${i}`, StreamKinds.VIDEO);
const receiver = new StreamReceiver(this._rpc, recvrTrack, this._streams);
this._videoReceivers.push(receiver);

if (this._cfg.receivers?.video) {
for (let i = 0; i < this._cfg.receivers.video; i++) {
const recvrTrack = this._socket.createReceiverTrack(`video_${i}`, StreamKinds.VIDEO);
const receiver = new StreamReceiver(this._rpc, recvrTrack, this._streams);
this._videoReceivers.push(receiver);
}
}

for (let i = 0; i < this._cfg.receivers.audio; i++) {
const recvrTrack = this._socket.createReceiverTrack(`audio_${i}`, StreamKinds.AUDIO);
const receiver = new StreamReceiver(this._rpc, recvrTrack, this._streams);
this._audioReceivers.push(receiver);
if (this._cfg.receivers?.audio) {
for (let i = 0; i < this._cfg.receivers.audio; i++) {
const recvrTrack = this._socket.createReceiverTrack(`audio_${i}`, StreamKinds.AUDIO);
const receiver = new StreamReceiver(this._rpc, recvrTrack, this._streams);
this._audioReceivers.push(receiver);
}
}
}

Expand Down
Loading