Skip to content

Commit

Permalink
Add support for choosing preferred video codec profile (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamerns authored Mar 26, 2021
1 parent 10f3c5a commit bc7ec5d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions decs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ interface RTCDtlsTransport {
// https://www.w3.org/TR/webrtc/#idl-def-rtcrtpcodeccapability
interface RTCRtpCodecCapability {
mimeType: string;
sdpFmtpLine?: string;
}

// https://www.w3.org/TR/webrtc/#idl-def-rtcrtpheaderextensioncapability
Expand Down
30 changes: 25 additions & 5 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export interface Constraints extends MediaStreamConstraints {
codec: string;
simulcast?: boolean;
sendEmptyOnMute?: boolean;
preferredCodecProfile?: string;
}

const defaults = {
Expand Down Expand Up @@ -253,11 +254,30 @@ export class LocalStream extends MediaStream {
if ('setCodecPreferences' in transceiver) {
const cap = RTCRtpSender.getCapabilities(kind);
if (!cap) return;
const selCodec = cap.codecs.find(
(c) =>
c.mimeType.toLowerCase() === `video/${this.constraints.codec.toLowerCase()}` ||
c.mimeType.toLowerCase() === `audio/opus`,
);

let selCodec: RTCRtpCodecCapability | undefined;
if (this.constraints.preferredCodecProfile && kind === 'video') {
const allCodecProfiles = cap.codecs.filter((c) => c.mimeType.toLowerCase() === `video/${this.constraints.codec.toLowerCase()}`);
if (!allCodecProfiles) {
return;
}
selCodec = allCodecProfiles.find(
(c) =>
c.sdpFmtpLine &&
c.sdpFmtpLine?.indexOf(`profile-level-id=${this.constraints.preferredCodecProfile}`) >= 0
);
if (!selCodec) {
// get first one
selCodec = allCodecProfiles[0];

}
} else {
selCodec = cap.codecs.find(
(c) =>
c.mimeType.toLowerCase() === `video/${this.constraints.codec.toLowerCase()}` ||
c.mimeType.toLowerCase() === `audio/opus`
);
}
if (selCodec) {
transceiver.setCodecPreferences([selCodec]);
}
Expand Down

0 comments on commit bc7ec5d

Please sign in to comment.