Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update retain helpers #21

Merged
merged 3 commits into from
Jul 30, 2024
Merged

feat: update retain helpers #21

merged 3 commits into from
Jul 30, 2024

Conversation

brycetham
Copy link
Collaborator

@brycetham brycetham commented Jul 29, 2024

This PR updates the following utility functions:

  • The current retainCodecs function has been renamed to retainCodecsByCodecName. It now also returns a Boolean to indicate if any codecs were filtered out, similar to the current retainCandidates. In addition, the sdpOrAv parameter has been updated to only accept an AvMediaDescription (instead of also accepting an Sdp) as suggested by @bbaldino.
  • A new retainCodecs function has been created. This function takes a predicate as a parameter that is allows the user to define their own conditions for which codecs will be retained.
  • The current retainCandidates function has been renamed to retainCandidatesByTransportType.
  • A new retainCandidates function has been created. Similar to the new retainCodecs, this function takes a predicate as a parameter.

The purpose of these changes is to allow users to filter out codecs and candidates using whatever conditions they want (such as only retaining H.264 codecs with a certain profile, or only retaining TLS candidates), while keeping the old utility functions for convenience.

src/munge.ts Outdated
*
* @param sdpOrAv - The {@link Sdp} or {@link AvMediaDescription} from which to filter codecs.
* @param allowedCodecNames - The names of the codecs that should remain in the SDP.
* @param predicate - A function used to determine which codecs should be retained.
* @returns A boolean that indicates if some codecs have been filtered out.
*/
export function retainCodecs(
sdpOrAv: Sdp | AvMediaDescription,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, do we ever use this with an entire SDP? Retaining codecs across media types doesn't make much sense, not sure why I supported that (or if it was even me, I guess)

I guess the predicate version it makes a bit more sense since then it can detect audio vs video.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. I took a look at the history for this file. It seems like removeCodec was the original helper function which took in an SDP (and it's still in this file, though I don't see it being used anywhere). Then we added the current retainCodecs and retainCandidates functions... and I'm not sure why I had it accept both? Probably because it made sense to take in a media description for those, but removeCodec took in an SDP, so I compromised and just had all of them take both.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, yeah I think removeCodec made sense with an SDP because it would remove all instances of those codecs from all mlines, so it was easy to throw both video and audio codecs in there. With 'retain' though, for codecs, I'm not sure it makes sense (transport types still accepting an entire sdp does, though)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bbaldino Will it be logical to keep the API consistent between these functions and more flexible? Or is your vision always to retain codecs only from AvMediaDescription when working with SDP?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's true that the APIs would differ, but I think in this situation it's accurate: the transport information is consistent across all mlines, while codecs differ between audio and video.

describe('retainCodecs', () => {
it('should retain codecs correctly when passing in an SDP', () => {
expect.hasAssertions();
const offer = fs.readFileSync('./src/sdp-corpus/offer_with_extra_codecs.sdp', 'utf-8');
const parsed = parse(offer);

retainCodecs(parsed, ['h264', 'opus']);
// eslint-disable-next-line jsdoc/require-jsdoc
const predicate = (codecInfo: CodecInfo) =>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this predicate is the same for multiple tests, consider putting it in a describe to avoid duplicating code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only being used for half of the tests though. To me, it would make sense to do so if most or all of the tests use it (and in the future, if we add more cases to this describe block, that means most of the test cases will not use it).

src/munge.ts Outdated
.filter((codecName) => !allowedLowerCase.includes(codecName.toLowerCase()))
.forEach((unwantedCodec) => removeCodec(sdpOrAv, unwantedCodec));
return retainCodecs(sdpOrAv, (codecInfo) =>
allowedLowerCase.includes(codecInfo.name?.toLowerCase() as string)
Copy link

@antsukanova antsukanova Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can you ensure that this function will always have codecInfo.name defined to cast type to string and not have some callback value like "" for example?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, it was like this before but I probably should be checking to see if it exists first before checking if it's in the list of allowed codec names.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bbaldino, do you remember why CodecInfo.name is optional? Is it just for convenience when building the CodecInfo, so you don't have to define a name until CodecInfo.addLine() is called?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline, but I think your memory is right: it's optional because we fill in that information later. Bryce and I discussed some investigation of using an intermediary type while parsing that could then be finalized into another type which probably models required fields vs optional ones, so we can look into that.

@brycetham brycetham merged commit 49b5566 into main Jul 30, 2024
1 check passed
@brycetham brycetham deleted the btham/generic_retain_helpers branch July 30, 2024 18:34
bbaldino pushed a commit that referenced this pull request Jul 30, 2024
# [1.7.0](v1.6.0...v1.7.0) (2024-07-30)

### Features

* update retain helpers ([#21](#21)) ([49b5566](49b5566))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants