Skip to content

Commit

Permalink
Keep dd extension id in the session (#1297)
Browse files Browse the repository at this point in the history
* Keep dd extension id in the session

Safari could generate different rtp extensions
for video tracks(camera and screenshare), need
to use same extension id for dependency descriptor
in the offer.

* changeset and prettier

* parse entire offer for max extension id
  • Loading branch information
cnderrauber authored Oct 24, 2024
1 parent 42ecb8b commit 24bb7a9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-steaks-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Keep dd extension id in the session
71 changes: 42 additions & 29 deletions src/room/PCTransport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter } from 'events';
import type { MediaDescription } from 'sdp-transform';
import type { MediaDescription, SessionDescription } from 'sdp-transform';
import { parse, write } from 'sdp-transform';
import { debounce } from 'ts-debounce';
import log, { LoggerNames, getLogger } from '../logger';
Expand Down Expand Up @@ -48,6 +48,8 @@ export default class PCTransport extends EventEmitter {

private loggerOptions: LoggerOptions;

private ddExtID = 0;

pendingCandidates: RTCIceCandidateInit[] = [];

restartingIce: boolean = false;
Expand Down Expand Up @@ -288,7 +290,7 @@ export default class PCTransport extends EventEmitter {
}

if (isSVCCodec(trackbr.codec)) {
ensureVideoDDExtensionForSVC(media);
this.ensureVideoDDExtensionForSVC(media, sdpParsed);
}

// TODO: av1 slow starting issue already fixed in chrome 124, clean this after some versions
Expand Down Expand Up @@ -503,6 +505,44 @@ export default class PCTransport extends EventEmitter {
throw new NegotiationError(msg);
}
}

private ensureVideoDDExtensionForSVC(
media: {
type: string;
port: number;
protocol: string;
payloads?: string | undefined;
} & MediaDescription,
sdp: SessionDescription,
) {
const ddFound = media.ext?.some((ext): boolean => {
if (ext.uri === ddExtensionURI) {
return true;
}
return false;
});

if (!ddFound) {
if (this.ddExtID === 0) {
let maxID = 0;
sdp.media.forEach((m) => {
if (m.type !== 'video') {
return;
}
m.ext?.forEach((ext) => {
if (ext.value > maxID) {
maxID = ext.value;
}
});
});
this.ddExtID = maxID + 1;
}
media.ext?.push({
value: this.ddExtID,
uri: ddExtensionURI,
});
}
}
}

function ensureAudioNackAndStereo(
Expand Down Expand Up @@ -555,33 +595,6 @@ function ensureAudioNackAndStereo(
}
}

function ensureVideoDDExtensionForSVC(
media: {
type: string;
port: number;
protocol: string;
payloads?: string | undefined;
} & MediaDescription,
) {
let maxID = 0;
const ddFound = media.ext?.some((ext): boolean => {
if (ext.uri === ddExtensionURI) {
return true;
}
if (ext.value > maxID) {
maxID = ext.value;
}
return false;
});

if (!ddFound) {
media.ext?.push({
value: maxID + 1,
uri: ddExtensionURI,
});
}
}

function extractStereoAndNackAudioFromOffer(offer: RTCSessionDescriptionInit): {
stereoMids: string[];
nackMids: string[];
Expand Down

0 comments on commit 24bb7a9

Please sign in to comment.