Skip to content

Commit

Permalink
feat: Method named getNumActiveSimulcastLayers & event to LocalTrack …
Browse files Browse the repository at this point in the history
…for constraints changed (#35)

* fix: add a method named getNumActiveSimulcastLayers & event to LocalTrack for constraints changed

* fix: add related ut cases

* fix: minor errors in unit test

---------

Co-authored-by: Brown Zhang <[email protected]>
  • Loading branch information
x-epoch and Brown Zhang authored Mar 4, 2023
1 parent 081b8b2 commit 9298169
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/media/local-track.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,24 @@ describe('LocalTrack', () => {
expect(constraints).toStrictEqual({ autoGainControl: true });
});
});

describe('LocalTrack getNumActiveSimulcastLayers', () => {
it('activeSimulcastLayers returns 3 if video height greater than 360', () => {
expect.hasAssertions();
const mockStream = createMockedStream(720);
const localTrack = new TestLocalTrack(mockStream);
expect(localTrack.getNumActiveSimulcastLayers()).toBe(3);
});
it('activeSimulcastLayers returns 2 if video height greater than 180 but less(equals) than 360', () => {
expect.hasAssertions();
const mockStream = createMockedStream(360);
const localTrack = new TestLocalTrack(mockStream);
expect(localTrack.getNumActiveSimulcastLayers()).toBe(2);
});
it('activeSimulcastLayers returns 1 if video height less(equals) than 180', () => {
expect.hasAssertions();
const mockStream = createMockedStream(180);
const localTrack = new TestLocalTrack(mockStream);
expect(localTrack.getNumActiveSimulcastLayers()).toBe(1);
});
});
31 changes: 30 additions & 1 deletion src/media/local-track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export enum LocalTrackEvents {
* Fires when there has been a change in the underlying track.
*/
UnderlyingTrackChange = 'underlying-track-change',
/**
* Fires when the applyConstraints() has been called for the track.
*/
TrackConstraintsChange = 'track-constraints-change',
}

export interface TrackState {
Expand Down Expand Up @@ -44,6 +48,7 @@ export interface TrackEvents extends EventMap {
[LocalTrackEvents.Muted]: (event: TrackMuteEvent) => void;
[LocalTrackEvents.PublishedStateUpdate]: (event: TrackPublishEvent) => void;
[LocalTrackEvents.UnderlyingTrackChange]: () => void;
[LocalTrackEvents.TrackConstraintsChange]: () => void;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -276,7 +281,10 @@ export abstract class LocalTrack extends EventEmitter<TrackEvents> {
*/
async applyConstraints(constraints?: MediaTrackConstraints): Promise<void> {
logger.log(`Applying constraints to local track:`, constraints);
return this.underlyingTrack.applyConstraints(constraints);
const ret = this.underlyingTrack.applyConstraints(constraints).then(() => {
this.emit(LocalTrackEvents.TrackConstraintsChange);
});
return ret;
}

/**
Expand All @@ -296,4 +304,25 @@ export abstract class LocalTrack extends EventEmitter<TrackEvents> {
getSettings(): MediaTrackSettings {
return this.underlyingTrack.getSettings();
}

/**
* Check the resolution and then return how many layers will be active.
*
* @returns The active layers count.
*/
getNumActiveSimulcastLayers(): number {
let activeSimulcastLayersNumber = 0;
if (this.trackState.type === 'audio') {
return activeSimulcastLayersNumber;
}
const videoHeight = this.underlyingTrack.getSettings().height;
if ((videoHeight as number) <= 180) {
activeSimulcastLayersNumber = 1;
} else if ((videoHeight as number) <= 360) {
activeSimulcastLayersNumber = 2;
} else {
activeSimulcastLayersNumber = 3;
}
return activeSimulcastLayersNumber;
}
}
4 changes: 3 additions & 1 deletion src/mocks/media-stream-track-stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class MediaStreamTrackStub {
*/
stop(): void {}

applyConstraints(constraints?: MediaTrackConstraints): void {}
applyConstraints(constraints?: MediaTrackConstraints): Promise<void> {
return Promise.resolve();
}

getConstraints(): MediaTrackConstraints {
return {} as MediaTrackConstraints;
Expand Down
6 changes: 4 additions & 2 deletions src/util/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ jest.mock('../mocks/media-stream-track-stub');
/**
* Create a mocked stream with a mocked MediaStreamTrack.
*
* @param videoHeight - Video height.
* @returns A Mocked MediaStreamStub type coerced to a MediaStream.
*/
export const createMockedStream = (): MediaStream => {
export const createMockedStream = (videoHeight = 360): MediaStream => {
// eslint-disable-next-line no-use-before-define
return createMockedStreamWithSize(640, 480);
return createMockedStreamWithSize((videoHeight * 16) / 9, videoHeight);
};

/**
Expand All @@ -32,6 +33,7 @@ export const createMockedStreamWithSize = (width: number, height: number): Media
});
track.applyConstraints.mockImplementation((constraints?: MediaTrackConstraints) => {
track.constraints = constraints || {};
return Promise.resolve();
});
track.getConstraints.mockImplementation(() => {
return track.constraints;
Expand Down

0 comments on commit 9298169

Please sign in to comment.