Skip to content

Commit

Permalink
fix: account for multiple layers
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlaz committed Dec 23, 2024
1 parent 9098cb4 commit b5cd7b7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
33 changes: 32 additions & 1 deletion packages/client/src/rtc/__tests__/videoLayers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('videoLayers', () => {
});
});

it('should map OptimalVideoLayer to SVC encodings', () => {
it('should map OptimalVideoLayer to SVC encodings (three layers)', () => {
const layers: Array<Partial<OptimalVideoLayer>> = [
{ rid: 'f', width: 1920, height: 1080, maxBitrate: 3000000 },
{ rid: 'h', width: 960, height: 540, maxBitrate: 750000 },
Expand All @@ -210,6 +210,37 @@ describe('videoLayers', () => {
});
});

it('should map OptimalVideoLayer to SVC encodings (two layers)', () => {
const layers: Array<Partial<OptimalVideoLayer>> = [
{ rid: 'h', width: 960, height: 540, maxBitrate: 750000 },
{ rid: 'q', width: 480, height: 270, maxBitrate: 187500 },
];

const svcLayers = toSvcEncodings(layers as OptimalVideoLayer[]);
expect(svcLayers.length).toBe(1);
expect(svcLayers[0]).toEqual({
rid: 'q',
width: 960,
height: 540,
maxBitrate: 750000,
});
});

it('should map OptimalVideoLayer to SVC encodings (two layers)', () => {
const layers: Array<Partial<OptimalVideoLayer>> = [
{ rid: 'q', width: 480, height: 270, maxBitrate: 187500 },
];

const svcLayers = toSvcEncodings(layers as OptimalVideoLayer[]);
expect(svcLayers.length).toBe(1);
expect(svcLayers[0]).toEqual({
rid: 'q',
width: 480,
height: 270,
maxBitrate: 187500,
});
});

describe('getComputedMaxBitrate', () => {
it('should scale target bitrate down if resolution is smaller than target resolution', () => {
const targetResolution = { width: 1920, height: 1080, bitrate: 3000000 };
Expand Down
10 changes: 8 additions & 2 deletions packages/client/src/rtc/videoLayers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ const defaultBitratePerRid: Record<string, number> = {
export const toSvcEncodings = (
layers: OptimalVideoLayer[] | undefined,
): RTCRtpEncodingParameters[] | undefined => {
// we take the `f` layer, and we rename it to `q`.
return layers?.filter((l) => l.rid === 'f').map((l) => ({ ...l, rid: 'q' }));
if (!layers) return undefined;
// we take the highest quality layer, and we assign it to `q` encoder.
const withRid = (rid: string) => (l: OptimalVideoLayer) => l.rid === rid;
const highestLayer =
layers.find(withRid('f')) ||
layers.find(withRid('h')) ||
layers.find(withRid('q'));
return [{ ...highestLayer, rid: 'q' }];
};

/**
Expand Down

0 comments on commit b5cd7b7

Please sign in to comment.