Skip to content

Commit

Permalink
[Backport staging] Fix screen sharing in recent Chrome (#4243)
Browse files Browse the repository at this point in the history
Co-authored-by: David Baker <[email protected]>
  • Loading branch information
RiotRobot and dbkr authored Jun 13, 2024
1 parent 395c3cf commit 2ee43ca
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/webrtc/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2381,22 +2381,40 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
// RTCRtpReceiver.getCapabilities and RTCRtpSender.getCapabilities don't seem to be supported on FF before v113
if (!RTCRtpReceiver.getCapabilities || !RTCRtpSender.getCapabilities) return;

const screenshareVideoTransceiver = this.transceivers.get(
getTransceiverKey(SDPStreamMetadataPurpose.Screenshare, "video"),
);

// setCodecPreferences isn't supported on FF (as of v113)
if (!screenshareVideoTransceiver || !screenshareVideoTransceiver.setCodecPreferences) return;

const recvCodecs = RTCRtpReceiver.getCapabilities("video")!.codecs;
const sendCodecs = RTCRtpSender.getCapabilities("video")!.codecs;
const codecs = [...sendCodecs, ...recvCodecs];
const codecs = [];

for (const codec of codecs) {
if (codec.mimeType === "video/rtx") {
const rtxCodecIndex = codecs.indexOf(codec);
codecs.splice(rtxCodecIndex, 1);
for (const codec of [...recvCodecs, ...sendCodecs]) {
if (codec.mimeType !== "video/rtx") {
codecs.push(codec);
try {
screenshareVideoTransceiver.setCodecPreferences(codecs);
} catch (e) {
// Specifically, Chrome around version 125 and Electron 30 (which is Chromium 124) return an H.264 codec in
// the sender's capabilities but throw when you try to set it. Hence... this mess.
// Specifically, that codec is:
// {
// clockRate: 90000,
// mimeType: "video/H264",
// sdpFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=640034",
// }
logger.info(
"Working around buggy WebRTC impl: claimed to support codec but threw when setting codec preferences",
codec,
e,
);
codecs.pop();
}
}
}

const screenshareVideoTransceiver = this.transceivers.get(
getTransceiverKey(SDPStreamMetadataPurpose.Screenshare, "video"),
);
// setCodecPreferences isn't supported on FF (as of v113)
screenshareVideoTransceiver?.setCodecPreferences?.(codecs);
}

private onNegotiationNeeded = async (): Promise<void> => {
Expand Down

0 comments on commit 2ee43ca

Please sign in to comment.