Skip to content

Commit

Permalink
mung sdp for av1 bitrate setting (#314)
Browse files Browse the repository at this point in the history
* mung sdp for av1 bitrate setting

* code clean

* Update src/room/participant/LocalParticipant.ts

Co-authored-by: lukasIO <[email protected]>

* Update src/room/participant/LocalParticipant.ts

Co-authored-by: lukasIO <[email protected]>

Co-authored-by: lukasIO <[email protected]>
  • Loading branch information
cnderrauber and lukasIO authored Jul 12, 2022
1 parent c519a2a commit d25b86c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lemon-ads-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

apply av1 bitrate setting
39 changes: 39 additions & 0 deletions src/room/PCTransport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { debounce } from 'ts-debounce';
import log from '../logger';

/** @internal */
interface TrackBitrateInfo {
sid: string;
codec: string;
maxbr: number;
}

/** @internal */
export default class PCTransport {
pc: RTCPeerConnection;
Expand All @@ -11,6 +18,8 @@ export default class PCTransport {

renegotiate: boolean = false;

trackBitrates: TrackBitrateInfo[] = [];

onOffer?: (offer: RTCSessionDescriptionInit) => void;

constructor(config?: RTCConfiguration) {
Expand Down Expand Up @@ -78,10 +87,40 @@ export default class PCTransport {
// actually negotiate
log.debug('starting to negotiate');
const offer = await this.pc.createOffer(options);

// mung sdp for codec bitrate setting that can't apply by sendEncoding
this.trackBitrates.forEach((trackbr) => {
let sdp = offer.sdp ?? '';
const sidIndex = sdp.search(new RegExp(`msid.* ${trackbr.sid}`));
if (sidIndex < 0) {
return;
}

const mlineStart = sdp.substring(0, sidIndex).lastIndexOf('m=');
const mlineEnd = sdp.indexOf('m=', sidIndex);
const mediaSection = sdp.substring(mlineStart, mlineEnd);

const mungedMediaSection = mediaSection.replace(
new RegExp(`a=rtpmap:(\\d+) ${trackbr.codec}/\\d+`, 'i'),
`$&\r\na=fmtp:$1 x-google-max-bitrate=${trackbr.maxbr}`,
);
sdp = sdp.substring(0, mlineStart) + mungedMediaSection + sdp.substring(mlineEnd);
offer.sdp = sdp;
});
this.trackBitrates = [];

await this.pc.setLocalDescription(offer);
this.onOffer(offer);
}

setTrackCodecBitrate(sid: string, codec: string, maxbr: number) {
this.trackBitrates.push({
sid,
codec,
maxbr,
});
}

close() {
this.pc.close();
}
Expand Down
15 changes: 15 additions & 0 deletions src/room/participant/LocalParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,14 @@ export default class LocalParticipant extends Participant {
track.codec = opts.videoCodec;
}

if (track.codec === 'av1' && encodings && encodings[0]?.maxBitrate) {
this.engine.publisher.setTrackCodecBitrate(
req.cid,
track.codec,
encodings[0].maxBitrate / 1000,
);
}

this.engine.negotiate();

// store RTPSender
Expand Down Expand Up @@ -642,6 +650,13 @@ export default class LocalParticipant extends Participant {
this.setPreferredCodec(transceiver, track.kind, opts.videoCodec);
track.setSimulcastTrackSender(opts.videoCodec, transceiver.sender);

if (videoCodec === 'av1' && encodings[0]?.maxBitrate) {
this.engine.publisher.setTrackCodecBitrate(
req.cid,
videoCodec,
encodings[0].maxBitrate / 1000,
);
}
this.engine.negotiate();
log.debug(`published ${opts.videoCodec} for track ${track.sid}`, { encodings, trackInfo: ti });
}
Expand Down
2 changes: 1 addition & 1 deletion src/room/participant/publishUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function computeVideoEncodings(
encodings.push({
rid: videoRids[2 - i],
scaleResolutionDownBy: 2 ** i,
maxBitrate: videoEncoding ? videoEncoding.maxBitrate / 2 ** i : 0,
maxBitrate: videoEncoding ? videoEncoding.maxBitrate / 3 ** i : 0,
/* @ts-ignore */
maxFramerate: original.encoding.maxFramerate,
/* @ts-ignore */
Expand Down

0 comments on commit d25b86c

Please sign in to comment.