Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlaz committed Dec 9, 2024
1 parent 627e16c commit c622f53
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/client/src/rtc/Publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
TrackInfo,
TrackType,
} from '../gen/video/sfu/models/models';
import { VideoSender } from '../gen/video/sfu/event/events';
import {
findOptimalVideoLayers,
OptimalVideoLayer,
Expand All @@ -21,7 +22,6 @@ import {
trackTypeToParticipantStreamKey,
} from './helpers/tracks';
import { extractMid } from './helpers/sdp';
import { VideoSender } from '../gen/video/sfu/event/events';
import { withoutConcurrency } from '../helpers/concurrency';

export type PublisherConstructorOpts = BasePeerConnectionOpts & {
Expand Down Expand Up @@ -161,7 +161,7 @@ export class Publisher extends BasePeerConnection {
const trackToPublish = track.clone();

const transceiver = this.transceiverCache.get(publishOption);
if (!transceiver || !transceiver.sender.track) {
if (!transceiver) {
this.addTransceiver(trackToPublish, publishOption);
} else {
await this.updateTransceiver(transceiver, trackToPublish);
Expand Down
92 changes: 91 additions & 1 deletion packages/client/src/rtc/__tests__/Publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { Publisher } from '../Publisher';
import { CallState } from '../../store';
import { StreamSfuClient } from '../../StreamSfuClient';
import { DispatchableMessage, Dispatcher } from '../Dispatcher';
import { PeerType, TrackType } from '../../gen/video/sfu/models/models';
import {
PeerType,
PublishOption,
TrackType,
} from '../../gen/video/sfu/models/models';
import { SfuEvent } from '../../gen/video/sfu/event/events';
import { IceTrickleBuffer } from '../IceTrickleBuffer';
import { StreamClient } from '../../coordinator/connection/client';
Expand Down Expand Up @@ -528,4 +532,90 @@ describe('Publisher', () => {
]);
});
});

describe('changePublishOptions', () => {
it('adds missing transceivers', async () => {
const transceiver = new RTCRtpTransceiver();
const track = new MediaStreamTrack();
vi.spyOn(transceiver.sender, 'track', 'get').mockReturnValue(track);
vi.spyOn(track, 'getSettings').mockReturnValue({
width: 640,
height: 480,
});
vi.spyOn(track, 'clone').mockReturnValue(track);
// @ts-expect-error private method
vi.spyOn(publisher, 'addTransceiver');

publisher['publishOptions'] = [
// @ts-expect-error incomplete data
{ trackType: TrackType.VIDEO, id: 0, codec: { name: 'vp8' } },
// @ts-expect-error incomplete data
{ trackType: TrackType.VIDEO, id: 1, codec: { name: 'av1' } },
// @ts-expect-error incomplete data
{ trackType: TrackType.VIDEO, id: 2, codec: { name: 'vp9' } },
];

publisher['transceiverCache'].add(
publisher['publishOptions'][0],
transceiver,
);

vi.spyOn(publisher, 'isPublishing').mockReturnValue(true);

// enable av1 and vp9
await publisher['syncPublishOptions']();

expect(publisher['transceiverCache'].items().length).toBe(3);
expect(publisher['addTransceiver']).toHaveBeenCalledTimes(2);
expect(publisher['addTransceiver']).toHaveBeenCalledWith(
track,
expect.objectContaining({
trackType: TrackType.VIDEO,
id: 1,
codec: { name: 'av1' },
}),
);
expect(publisher['addTransceiver']).toHaveBeenCalledWith(
track,
expect.objectContaining({
trackType: TrackType.VIDEO,
id: 2,
codec: { name: 'vp9' },
}),
);
});

it('disables extra transceivers', async () => {
const publishOptions: PublishOption[] = [
// @ts-expect-error incomplete data
{ trackType: TrackType.VIDEO, id: 0, codec: { name: 'vp8' } },
// @ts-expect-error incomplete data
{ trackType: TrackType.VIDEO, id: 1, codec: { name: 'av1' } },
// @ts-expect-error incomplete data
{ trackType: TrackType.VIDEO, id: 2, codec: { name: 'vp9' } },
];

const track = new MediaStreamTrack();
const transceiver = new RTCRtpTransceiver();
// @ts-ignore test setup
transceiver.sender.track = track;

publisher['transceiverCache'].add(publishOptions[0], transceiver);
publisher['transceiverCache'].add(publishOptions[1], transceiver);
publisher['transceiverCache'].add(publishOptions[2], transceiver);

vi.spyOn(publisher, 'isPublishing').mockReturnValue(true);
// disable av1
publisher['publishOptions'] = publishOptions.filter(
(o) => o.codec?.name !== 'av1',
);

await publisher['syncPublishOptions']();

expect(publisher['transceiverCache'].items().length).toBe(3);
expect(track.stop).toHaveBeenCalledOnce();
expect(transceiver.sender.replaceTrack).toHaveBeenCalledOnce();
expect(transceiver.sender.replaceTrack).toHaveBeenCalledWith(null);
});
});
});

0 comments on commit c622f53

Please sign in to comment.