Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't store streams that are only used once #2157

Merged
merged 1 commit into from
Feb 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/webrtc/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ export class MatrixCall extends EventEmitter {
const upgradeVideo = video && !this.hasLocalUserMediaVideoTrack;
logger.debug(`Upgrading call: audio?=${upgradeAudio} video?=${upgradeVideo}`);

const stream = await this.client.getMediaHandler().getUserMediaStream(upgradeAudio, upgradeVideo);
const stream = await this.client.getMediaHandler().getUserMediaStream(upgradeAudio, upgradeVideo, false);
if (upgradeAudio && upgradeVideo) {
if (this.hasLocalUserMediaAudioTrack) return;
if (this.hasLocalUserMediaVideoTrack) return;
Expand Down Expand Up @@ -1978,14 +1978,15 @@ export class MatrixCall extends EventEmitter {
}

private stopAllMedia(): void {
logger.debug(`stopAllMedia (stream=${this.localUsermediaStream})`);
logger.debug("Stopping all media for call", this.callId);

for (const feed of this.feeds) {
if (feed.isLocal() && feed.purpose === SDPStreamMetadataPurpose.Usermedia) {
this.client.getMediaHandler().stopUserMediaStream(feed.stream);
} else if (feed.isLocal() && feed.purpose === SDPStreamMetadataPurpose.Screenshare) {
this.client.getMediaHandler().stopScreensharingStream(feed.stream);
} else {
logger.debug("Stopping remote stream", feed.stream.id);
for (const track of feed.stream.getTracks()) {
track.stop();
}
Expand Down
17 changes: 13 additions & 4 deletions src/webrtc/mediaHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ export class MediaHandler {
}

/**
* @param audio should have an audio track
* @param video should have a video track
* @param reusable is allowed to be reused by the MediaHandler
* @returns {MediaStream} based on passed parameters
*/
public async getUserMediaStream(audio: boolean, video: boolean): Promise<MediaStream> {
public async getUserMediaStream(audio: boolean, video: boolean, reusable = true): Promise<MediaStream> {
const shouldRequestAudio = audio && await this.hasAudioDevice();
const shouldRequestVideo = video && await this.hasVideoDevice();

Expand All @@ -78,7 +81,9 @@ export class MediaHandler {
stream = await navigator.mediaDevices.getUserMedia(constraints);
}

this.userMediaStreams.push(stream);
if (reusable) {
this.userMediaStreams.push(stream);
}

return stream;
}
Expand All @@ -101,9 +106,11 @@ export class MediaHandler {
}

/**
* @param desktopCapturerSourceId sourceId for Electron DesktopCapturer
* @param reusable is allowed to be reused by the MediaHandler
* @returns {MediaStream} based on passed parameters
*/
public async getScreensharingStream(desktopCapturerSourceId: string): Promise<MediaStream | null> {
public async getScreensharingStream(desktopCapturerSourceId: string, reusable = true): Promise<MediaStream | null> {
let stream: MediaStream;

if (this.screensharingStreams.length === 0) {
Expand All @@ -125,7 +132,9 @@ export class MediaHandler {
stream = matchingStream.clone();
}

this.screensharingStreams.push(stream);
if (reusable) {
this.screensharingStreams.push(stream);
}

return stream;
}
Expand Down