Skip to content

Commit

Permalink
feat: add disableTwcc helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
bxu2 committed Dec 18, 2023
1 parent cf5061c commit e0e960f
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 5 deletions.
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"trackingid",
"transcoding",
"transpiled",
"twcc",
"typedoc",
"ufrag",
"ulpfec",
Expand Down
74 changes: 73 additions & 1 deletion src/munge.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as fs from 'fs';
import { AvMediaDescription, CodecInfo, Sdp } from './model';
import { removeCodec, retainCandidates, retainCodecs } from './munge';
import {
removeCodec,
retainCandidates,
retainCodecs,
disableRtcpFbValue,
disableTwcc,
} from './munge';
import { parse } from './parser';

/**
Expand Down Expand Up @@ -31,6 +37,29 @@ const validateOfferCodecs = (offer: Sdp): boolean => {
return true;
};

/**
* Check that the sdp offer contains rtcpFbValue or not.
*
* @param offer - The {@link Sdp} or {@link AvMediaDescription} to validate.
* @param rtcpFbValue - The rtcp-fb value to check.
* @returns True if the offer contains rtcp-fb value.
*/
const checkOfferContainsRtcpFeedbacks = (
offer: Sdp | AvMediaDescription,
rtcpFbValue: string
): boolean => {
let bContains = false;
const mediaDescriptions = offer instanceof Sdp ? offer.avMedia : [offer];
mediaDescriptions.forEach((av: AvMediaDescription) => {
av.codecs.forEach((ci: CodecInfo) => {
if (ci.feedback.includes(rtcpFbValue)) {
bContains = true;
}
});
});
return bContains;
};

describe('munging', () => {
describe('removeCodec', () => {
it('should remove codecs correctly when passing in an SDP', () => {
Expand Down Expand Up @@ -119,4 +148,47 @@ describe('munging', () => {
});
});
});

describe('disableRtcpFbValue', () => {
it('should remove rtcp feedback correctly when passing in an SDP', () => {
expect.hasAssertions();
const offer = fs.readFileSync('./src/sdp-corpus/offer_with_rtcp_feedback.sdp', 'utf-8');
const parsed = parse(offer);

disableRtcpFbValue(parsed, 'transport-cc');
expect(checkOfferContainsRtcpFeedbacks(parsed, 'transport-cc')).toBe(false);
expect(checkOfferContainsRtcpFeedbacks(parsed, 'goog-remb')).toBe(true);
});
it('should remove rtcp feedback correctly when passing in an AvMediaDescription', () => {
expect.hasAssertions();
const offer = fs.readFileSync('./src/sdp-corpus/offer_with_rtcp_feedback.sdp', 'utf-8');
const parsed = parse(offer);

parsed.avMedia
.filter((av) => av.type === 'audio')
.forEach((av) => {
disableRtcpFbValue(av, 'transport-cc');
expect(checkOfferContainsRtcpFeedbacks(av, 'transport-cc')).toBe(false);
});
expect(checkOfferContainsRtcpFeedbacks(parsed, 'transport-cc')).toBe(true);
expect(checkOfferContainsRtcpFeedbacks(parsed, 'goog-remb')).toBe(true);
});
});

describe('disableTwcc', () => {
it('should remove rtcp feedback correctly when passing in an AvMediaDescription', () => {
expect.hasAssertions();
const offer = fs.readFileSync('./src/sdp-corpus/offer_with_rtcp_feedback.sdp', 'utf-8');
const parsed = parse(offer);

parsed.avMedia
.filter((av) => av.type === 'audio')
.forEach((av) => {
disableTwcc(av);
expect(checkOfferContainsRtcpFeedbacks(av, 'transport-cc')).toBe(false);
});
expect(checkOfferContainsRtcpFeedbacks(parsed, 'transport-cc')).toBe(true);
expect(checkOfferContainsRtcpFeedbacks(parsed, 'goog-remb')).toBe(true);
});
});
});
18 changes: 14 additions & 4 deletions src/munge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import { AvMediaDescription, CodecInfo, MediaDescription, Sdp } from './model';

/**
* Disable an rtcp-fb value from all media blocks in the given SDP.
* Disable an rtcp-fb value from the media blocks in the given SDP or audio/video media description.
*
* @param sdp - The SDP from which to filter an rtcp-fb value.
* @param sdpOrAv - The {@link Sdp} or {@link AvMediaDescription} from which to filter an rtcp-fb value.
* @param rtcpFbValue - The rtcp-fb value to filter.
*/
export function disableRtcpFbValue(sdp: Sdp, rtcpFbValue: string) {
sdp.avMedia.forEach((media: AvMediaDescription) => {
export function disableRtcpFbValue(sdpOrAv: Sdp | AvMediaDescription, rtcpFbValue: string) {
const mediaDescriptions = sdpOrAv instanceof Sdp ? sdpOrAv.avMedia : [sdpOrAv];
mediaDescriptions.forEach((media: AvMediaDescription) => {
media.codecs.forEach((codec: CodecInfo) => {
// eslint-disable-next-line no-param-reassign
codec.feedback = codec.feedback.filter((fb) => fb !== rtcpFbValue);
Expand All @@ -40,6 +41,15 @@ export function disableRemb(sdp: Sdp) {
disableRtcpFbValue(sdp, 'goog-remb');
}

/**
* Disable REMB from all media blocks in the given SDP.
*
* @param sdpOrAv - The {@link Sdp} or {@link AvMediaDescription} from which to filter TWCC.
*/
export function disableTwcc(sdpOrAv: Sdp | AvMediaDescription) {
disableRtcpFbValue(sdpOrAv, 'transport-cc');
}

/**
* Remove the codec with the given name (as well as any secondary codecs associated with
* it) from the media blocks in the given SDP or audio/video media description.
Expand Down
Loading

0 comments on commit e0e960f

Please sign in to comment.