Skip to content

Commit

Permalink
Remove internal calls to setCodecPreferences on sender transceivers (#…
Browse files Browse the repository at this point in the history
…1114)

* Remove internal calls to setCodecPreferences on senders

* Create slimy-flowers-travel.md

* remove unneeded ts-ignore
  • Loading branch information
lukasIO authored Apr 24, 2024
1 parent b99ee7d commit 4318954
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 90 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-flowers-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Remove internal calls to setCodecPreferences on senders
2 changes: 0 additions & 2 deletions src/room/PCTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,6 @@ export default class PCTransport extends EventEmitter {
await this.pc.setLocalDescription(sd);
}
} catch (e) {
// this error cannot always be caught.
// If the local description has a setCodecPreferences error, this error will be uncaught
let msg = 'unknown error';
if (e instanceof Error) {
msg = e.message;
Expand Down
68 changes: 7 additions & 61 deletions src/room/RTCEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,14 @@ import { EngineEvent } from './events';
import CriticalTimers from './timers';
import type LocalTrack from './track/LocalTrack';
import type LocalTrackPublication from './track/LocalTrackPublication';
import type LocalVideoTrack from './track/LocalVideoTrack';
import LocalVideoTrack from './track/LocalVideoTrack';
import type { SimulcastTrackInfo } from './track/LocalVideoTrack';
import type RemoteTrackPublication from './track/RemoteTrackPublication';
import { Track } from './track/Track';
import type { Track } from './track/Track';
import type { TrackPublishOptions, VideoCodec } from './track/options';
import { getTrackPublicationInfo } from './track/utils';
import type { LoggerOptions } from './types';
import {
Mutex,
isVideoCodec,
isWeb,
sleep,
supportsAddTrack,
supportsSetCodecPreferences,
supportsTransceiver,
} from './utils';
import { Mutex, isVideoCodec, isWeb, sleep, supportsAddTrack, supportsTransceiver } from './utils';

const lossyDataChannel = '_lossy';
const reliableDataChannel = '_reliable';
Expand Down Expand Up @@ -671,51 +663,6 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
this.updateAndEmitDCBufferStatus(channelKind);
};

private setPreferredCodec(
transceiver: RTCRtpTransceiver,
kind: Track.Kind,
videoCodec: VideoCodec,
) {
if (!('getCapabilities' in RTCRtpReceiver)) {
return;
}
// when setting codec preferences, the capabilites need to be read from the RTCRtpReceiver
const cap = RTCRtpReceiver.getCapabilities(kind);
if (!cap) return;
this.log.debug('get receiver capabilities', { ...this.logContext, cap });
const matched: RTCRtpCodecCapability[] = [];
const partialMatched: RTCRtpCodecCapability[] = [];
const unmatched: RTCRtpCodecCapability[] = [];
cap.codecs.forEach((c) => {
const codec = c.mimeType.toLowerCase();
if (codec === 'audio/opus') {
matched.push(c);
return;
}
const matchesVideoCodec = codec === `video/${videoCodec}`;
if (!matchesVideoCodec) {
unmatched.push(c);
return;
}
// for h264 codecs that have sdpFmtpLine available, use only if the
// profile-level-id is 42e01f for cross-browser compatibility
if (videoCodec === 'h264') {
if (c.sdpFmtpLine && c.sdpFmtpLine.includes('profile-level-id=42e01f')) {
matched.push(c);
} else {
partialMatched.push(c);
}
return;
}

matched.push(c);
});

if (supportsSetCodecPreferences(transceiver)) {
transceiver.setCodecPreferences(matched.concat(partialMatched, unmatched));
}
}

async createSender(
track: LocalTrack,
opts: TrackPublishOptions,
Expand Down Expand Up @@ -766,6 +713,10 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
streams.push(track.mediaStream);
}

if (track instanceof LocalVideoTrack) {
track.codec = opts.videoCodec;
}

const transceiverInit: RTCRtpTransceiverInit = { direction: 'sendonly', streams };
if (encodings) {
transceiverInit.sendEncodings = encodings;
Expand All @@ -776,10 +727,6 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
transceiverInit,
);

if (track.kind === Track.Kind.Video && opts.videoCodec) {
this.setPreferredCodec(transceiver, track.kind, opts.videoCodec);
track.codec = opts.videoCodec;
}
return transceiver.sender;
}

Expand All @@ -804,7 +751,6 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
if (!opts.videoCodec) {
return;
}
this.setPreferredCodec(transceiver, track.kind, opts.videoCodec);
track.setSimulcastTrackSender(opts.videoCodec, transceiver.sender);
return transceiver.sender;
}
Expand Down
1 change: 0 additions & 1 deletion src/room/participant/LocalParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,6 @@ export default class LocalParticipant extends Participant {
...getLogContextFromTrack(track),
codec: updatedCodec,
});
/* @ts-ignore */
opts.videoCodec = updatedCodec;

// recompute encodings since bitrates/etc could have changed
Expand Down
26 changes: 0 additions & 26 deletions src/room/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ClientInfo, ClientInfo_SDK } from '@livekit/protocol';
import type { DetectableBrowser } from '../utils/browserParser';
import { getBrowser } from '../utils/browserParser';
import { protocolVersion, version } from '../version';
import CriticalTimers from './timers';
Expand Down Expand Up @@ -107,31 +106,6 @@ export function supportsSetSinkId(elm?: HTMLMediaElement): boolean {
return 'setSinkId' in elm;
}

const setCodecPreferencesVersions: Record<DetectableBrowser, string> = {
Chrome: '100',
Safari: '15',
Firefox: '100',
};

export function supportsSetCodecPreferences(transceiver: RTCRtpTransceiver): boolean {
if (!isWeb()) {
return false;
}
if (!('setCodecPreferences' in transceiver)) {
return false;
}
const browser = getBrowser();
if (!browser?.name || !browser.version) {
// version is required
return false;
}
const v = setCodecPreferencesVersions[browser.name];
if (v) {
return compareVersions(browser.version, v) >= 0;
}
return false;
}

export function isBrowserSupported() {
if (typeof RTCPeerConnection === 'undefined') {
return false;
Expand Down

0 comments on commit 4318954

Please sign in to comment.