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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix quality issues with SVC screenshares. #1077

Merged
merged 4 commits into from
Mar 15, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wet-kids-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Fix quality issues with SVC screenshares
6 changes: 5 additions & 1 deletion example/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,11 @@ const appActions = {
const enabled = currentRoom.localParticipant.isScreenShareEnabled;
appendLog(`${enabled ? 'stopping' : 'starting'} screen share`);
setButtonDisabled('share-screen-button', true);
await currentRoom.localParticipant.setScreenShareEnabled(!enabled, { audio: true });
try {
await currentRoom.localParticipant.setScreenShareEnabled(!enabled, { audio: true });
} catch (e) {
appendLog('error sharing screen', e);
}
setButtonDisabled('share-screen-button', false);
updateButtonsForPublishState();
},
Expand Down
24 changes: 6 additions & 18 deletions src/room/PCTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,31 +286,19 @@ export default class PCTransport extends EventEmitter {
return true;
}

let fmtpFound = false;
const startBitrate = Math.round(trackbr.maxbr * startBitrateForSVC);

for (const fmtp of media.fmtp) {
if (fmtp.payload === codecPayload) {
// if another track's fmtp already is set, we cannot override the bitrate
// this has the unfortunate consequence of being forced to use the
// initial track's bitrate for all tracks
if (!fmtp.config.includes('x-google-start-bitrate')) {
fmtp.config += `;x-google-start-bitrate=${Math.round(
trackbr.maxbr * startBitrateForSVC,
)}`;
}
if (!fmtp.config.includes('x-google-max-bitrate')) {
fmtp.config += `;x-google-max-bitrate=${trackbr.maxbr}`;
fmtp.config += `;x-google-start-bitrate=${startBitrate}`;
}
fmtpFound = true;
break;
}
}

if (!fmtpFound) {
media.fmtp.push({
payload: codecPayload,
config: `x-google-start-bitrate=${Math.round(
trackbr.maxbr * startBitrateForSVC,
)};x-google-max-bitrate=${trackbr.maxbr}`,
});
}

return true;
});
}
Expand Down
13 changes: 7 additions & 6 deletions src/room/participant/LocalParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,21 +732,21 @@ export default class LocalParticipant extends Participant {
// for svc codecs, disable simulcast and use vp8 for backup codec
if (track instanceof LocalVideoTrack) {
if (isSVCCodec(videoCodec)) {
// vp9 svc with screenshare has problem to encode, always use L1T3 here
if (track.source === Track.Source.ScreenShare && videoCodec === 'vp9') {
if (track.source === Track.Source.ScreenShare) {
// vp9 svc with screenshare cannot encode multiple spatial layers
// doing so reduces publish resolution to minimal resolution
opts.scalabilityMode = 'L1T3';
// Chrome does not allow more than 5 fps with L1T3, and it has encoding bugs with L3T3
// It has a different path for screenshare handling and it seems to be untested/buggy
// As a workaround, we are setting contentHint to force it to go through the same
// path as regular camera video. While this is not optimal, it delivers the performance
// that we need
if ('contentHint' in track.mediaStreamTrack) {
track.mediaStreamTrack.contentHint = 'motion';
this.log.info('forcing contentHint to motion for screenshare with VP9', {
this.log.info('forcing contentHint to motion for screenshare with SVC codecs', {
...this.logContext,
...getLogContextFromTrack(track),
});
} else {
opts.scalabilityMode = 'L1T3';
}
}
// set scalabilityMode to 'L3T3_KEY' by default
Expand Down Expand Up @@ -881,7 +881,8 @@ export default class LocalParticipant extends Participant {
maxbr: encodings[0]?.maxBitrate ? encodings[0].maxBitrate / 1000 : 0,
});
}
} else if (track.codec && isSVCCodec(track.codec) && encodings[0]?.maxBitrate) {
} else if (track.codec && track.codec == 'av1' && encodings[0]?.maxBitrate) {
// AV1 requires setting x-start-bitrate in SDP
this.engine.pcManager.publisher.setTrackCodecBitrate({
cid: req.cid,
codec: track.codec,
Expand Down
21 changes: 8 additions & 13 deletions src/room/participant/publishUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,21 @@ export function computeVideoEncodings(
);

if (scalabilityMode && isSVCCodec(videoCodec)) {
log.debug(`using svc with scalabilityMode ${scalabilityMode}`);

const sm = new ScalabilityMode(scalabilityMode);

const encodings: RTCRtpEncodingParameters[] = [];

if (sm.spatial > 3) {
throw new Error(`unsupported scalabilityMode: ${scalabilityMode}`);
}
for (let i = 0; i < sm.spatial; i += 1) {
encodings.push({
rid: videoRids[2 - i],
maxBitrate: videoEncoding.maxBitrate / 3 ** i,
/* @ts-ignore */
maxFramerate: original.encoding.maxFramerate,
});
}
/* @ts-ignore */
encodings[0].scalabilityMode = scalabilityMode;
log.debug('encodings', encodings);
encodings.push({
maxBitrate: videoEncoding.maxBitrate,
/* @ts-ignore */
maxFramerate: original.encoding.maxFramerate,
/* @ts-ignore */
scalabilityMode: scalabilityMode,
});
log.debug(`using svc encoding`, encodings[0]);
return encodings;
}

Expand Down
Loading