Skip to content

1:1 screenshare tests #2617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 9 additions & 2 deletions spec/test-utils/webrtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export const DUMMY_SDP = (
"a=ssrc:3619738545 cname:2RWtmqhXLdoF4sOi\r\n"
);

export const USERMEDIA_STREAM_ID = "mock_stream_from_media_handler";
export const SCREENSHARE_STREAM_ID = "mock_screen_stream_from_media_handler";

class MockMediaStreamAudioSourceNode {
connect() {}
}
Expand Down Expand Up @@ -128,6 +131,10 @@ export class MockRTCPeerConnection {
return new MockRTCRtpSender(track);
}

removeTrack() {
this.needsNegotiation = true;
}

doNegotiation() {
if (this.needsNegotiation && this.negotiationNeededListener) {
this.needsNegotiation = false;
Expand Down Expand Up @@ -222,7 +229,7 @@ export class MockMediaHandler {
if (audio) tracks.push(new MockMediaStreamTrack("audio_track", "audio"));
if (video) tracks.push(new MockMediaStreamTrack("video_track", "video"));

const stream = new MockMediaStream("mock_stream_from_media_handler", tracks);
const stream = new MockMediaStream(USERMEDIA_STREAM_ID, tracks);
this.userMediaStreams.push(stream);
return stream;
}
Expand All @@ -233,7 +240,7 @@ export class MockMediaHandler {
const tracks = [new MockMediaStreamTrack("video_track", "video")];
if (opts?.audio) tracks.push(new MockMediaStreamTrack("audio_track", "audio"));

const stream = new MockMediaStream("mock_screen_stream_from_media_handler", tracks);
const stream = new MockMediaStream(SCREENSHARE_STREAM_ID, tracks);
this.screensharingStreams.push(stream);
return stream;
}
Expand Down
99 changes: 88 additions & 11 deletions spec/unit/webrtc/call.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
MockMediaStream,
MockMediaStreamTrack,
installWebRTCMocks,
MockRTCPeerConnection,
SCREENSHARE_STREAM_ID,
} from "../../test-utils/webrtc";
import { CallFeed } from "../../../src/webrtc/callFeed";
import { EventType, MatrixEvent } from "../../../src";
Expand Down Expand Up @@ -117,6 +119,9 @@ describe('Call', function() {
});

afterEach(function() {
// Hangup to stop timers
call.hangup(CallErrorCode.UserHangup, true);

client.stop();
global.navigator = prevNavigator;
global.window = prevWindow;
Expand Down Expand Up @@ -178,9 +183,6 @@ describe('Call', function() {
getSender: () => "@test:foo",
});
expect(call.peerConn.addIceCandidate.mock.calls.length).toBe(1);

// Hangup to stop timers
call.hangup(CallErrorCode.UserHangup, true);
});

it('should add candidates received before answer if party ID is correct', async function() {
Expand Down Expand Up @@ -283,9 +285,6 @@ describe('Call', function() {
const ident = call.getRemoteAssertedIdentity();
expect(ident.id).toEqual("@steve:example.com");
expect(ident.displayName).toEqual("Steve Gibbons");

// Hangup to stop timers
call.hangup(CallErrorCode.UserHangup, true);
});

it("should map SDPStreamMetadata to feeds", async () => {
Expand Down Expand Up @@ -734,18 +733,16 @@ describe('Call', function() {

describe("ignoring streams with ids for which we already have a feed", () => {
const STREAM_ID = "stream_id";
const FEEDS_CHANGED_CALLBACK = jest.fn();
let FEEDS_CHANGED_CALLBACK;

beforeEach(async () => {
FEEDS_CHANGED_CALLBACK = jest.fn();

await startVoiceCall(client, call);
call.on(CallEvent.FeedsChanged, FEEDS_CHANGED_CALLBACK);
jest.spyOn(call, "pushLocalFeed");
});

afterEach(() => {
FEEDS_CHANGED_CALLBACK.mockReset();
});

it("should ignore stream passed to pushRemoteFeed()", async () => {
await call.onAnswerReceived({
getContent: () => {
Expand Down Expand Up @@ -941,4 +938,84 @@ describe('Call', function() {

expect(call.state).toEqual(CallState.Ended);
});

describe("Screen sharing", () => {
beforeEach(async () => {
await startVoiceCall(client, call);

await call.onAnswerReceived({
getContent: () => {
return {
"version": 1,
"call_id": call.callId,
"party_id": 'party_id',
"answer": {
sdp: DUMMY_SDP,
},
"org.matrix.msc3077.sdp_stream_metadata": {
"foo": {
"purpose": "m.usermedia",
"audio_muted": false,
"video_muted": false,
},
},
};
},
getSender: () => "@test:foo",
});
});

afterEach(() => {
// Hangup to stop timers
call.hangup(CallErrorCode.UserHangup, true);
});

it("enables screensharing", async () => {
await call.setScreensharingEnabled(true);

expect(call.feeds.filter(f => f.purpose == SDPStreamMetadataPurpose.Screenshare).length).toEqual(1);

client.client.sendEvent.mockReset();
const sendNegotiatePromise = new Promise<void>(resolve => {
client.client.sendEvent.mockImplementation(() => {
resolve();
});
});

MockRTCPeerConnection.triggerAllNegotiations();
await sendNegotiatePromise;

expect(client.client.sendEvent).toHaveBeenCalledWith(
FAKE_ROOM_ID,
EventType.CallNegotiate,
expect.objectContaining({
"version": "1",
"call_id": call.callId,
"org.matrix.msc3077.sdp_stream_metadata": expect.objectContaining({
[SCREENSHARE_STREAM_ID]: expect.objectContaining({
purpose: SDPStreamMetadataPurpose.Screenshare,
}),
}),
}),
);
});

it("disables screensharing", async () => {
await call.setScreensharingEnabled(true);

client.client.sendEvent.mockReset();
const sendNegotiatePromise = new Promise<void>(resolve => {
client.client.sendEvent.mockImplementation(() => {
resolve();
});
});

MockRTCPeerConnection.triggerAllNegotiations();
await sendNegotiatePromise;

await call.setScreensharingEnabled(false);

expect(call.feeds.filter(f => f.purpose == SDPStreamMetadataPurpose.Screenshare).length).toEqual(0);
});
});
});
2 changes: 1 addition & 1 deletion src/webrtc/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2240,7 +2240,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
eventType,
roomId: this.roomId,
content: realContent,
userId: this.invitee || this.getOpponentMember().userId,
userId: this.invitee || this.getOpponentMember()?.userId,
});

await this.client.sendEvent(this.roomId, eventType, realContent);
Expand Down