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

ref(TPC) Move all the utility functions to TPCUtils. #2599

Merged
merged 6 commits into from
Nov 25, 2024
Merged

Conversation

jallamsetty1
Copy link
Member

@jallamsetty1 jallamsetty1 commented Nov 18, 2024

Move all utility functions to TPCUtils that include

  1. munging the SDP.
  2. getting current codecs
  3. getting expected transceiver direction.
  4. getting expected settings for video encodings.

Move all functions that call RTCPeerConnection APIs directly back to TPC.

@jallamsetty1 jallamsetty1 marked this pull request as ready for review November 19, 2024 15:41
Copy link
Member

@saghul saghul left a comment

Choose a reason for hiding this comment

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

Left some comments!

modules/RTC/TPCUtils.js Show resolved Hide resolved
*/
export class TPCUtils {
/**
* Creates a new instance for a given TraceablePeerConnection
*
* @param peerconnection - the tpc instance for which we have utility functions.
*/
constructor(peerconnection) {
constructor(peerconnection, options) {
Copy link
Member

Choose a reason for hiding this comment

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

Make options default to {}, it will help you later.

Copy link
Member

Choose a reason for hiding this comment

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

Also document it above.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, makes sense! Will modify it.

}

const sdp = this.pc.remoteDescription?.sdp;
const defaultCodec = CodecMimeType.VP8;
Copy link
Member

Choose a reason for hiding this comment

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

Constant outside of here?

if (this.pc.usesCodecSelectionAPI() && rtpSender) {
const { codecs } = rtpSender.getParameters();

return codecs[0].mimeType.split('/')[1].toLowerCase();
Copy link
Member

Choose a reason for hiding this comment

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

Is there a chance the codec list is empty?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it shouldn't be unless there is a bug in the browser. Will add a check for that.

Copy link
Member

Choose a reason for hiding this comment

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

👍

modules/RTC/TPCUtils.js Show resolved Hide resolved
}
});

return new RTCSessionDescription({
Copy link
Member

Choose a reason for hiding this comment

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

Ditto.

@@ -35,7 +35,7 @@ describe('TPCUtils', () => {

it('sort ssrcs associated with all FID ssrc-groups', () => {
const pc = new MockPeerConnection();
const tpcUtils = new TPCUtils(pc);
const tpcUtils = new TPCUtils(pc, { });
Copy link
Member

Choose a reason for hiding this comment

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

If you use a default, we don't need to pass the weird empty object.

Copy link
Member

Choose a reason for hiding this comment

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

You can remove this one now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like I missed this one, I removed it from all other places.


try {
transceiver = this.peerconnection.addTransceiver(mediaStreamTrack, transceiverInit);
} catch (error) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: make this function async so you don't need to do this, execptions will reject the promsie already.

Copy link
Member Author

Choose a reason for hiding this comment

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

I just moved the code around and tried not to make any additional changes. This one however is simple enough that I don't mind doing it.

// peerconnection on chrome in unified-plan. It is ok to ignore and not report the error here since the
// action that triggers 'addTrack' (like unmute) will also configure the encodings and set bitrates after that.
if (!parameters?.encodings?.length) {
return Promise.resolve();
Copy link
Member

Choose a reason for hiding this comment

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

Ditto, why not make it async?

let mungedDescription = description;

this.trace('RTCSessionDescription::preTransform', dumpSDP(description));
mungedDescription = this.tpcUtils.mungeOpus(description);
Copy link
Member

Choose a reason for hiding this comment

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

Each of these operations re-parses the SDP. Since we are refactoring, doesn't it make sense to parse it in this functiom, and pass the transform object along, so each of the tpcUtils functions can apply transformations, and then after all of them, re-generate the SDP?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep makes sense.
I attempted all transformations in one utils function and that made it look rather unwieldy. Also some of the transormations will be gone soon so I thought its easier to just remove indivdual transformations then.
I see the benefit in passing just the transform object along though, will update it.

@jallamsetty1 jallamsetty1 requested a review from saghul November 25, 2024 03:28
* @param desc A session description object (with 'type' and 'sdp' fields)
* @return A session description object with its sdp field modified to contain an inject ssrc-group for simulcast.
*/
injectSsrcGroupForUnifiedSimulcast(desc) {
Copy link
Member

Choose a reason for hiding this comment

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

Why does this have "unified" in the name now?

Copy link
Member Author

Choose a reason for hiding this comment

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

Forgot to rename the method, will do. Thanks for pointing it out.

@@ -574,6 +575,57 @@ export class TPCUtils {
} ];
}

/**
* Injects a 'SIM' ssrc-group line for simulcast into the given session description object to make Jicofo happy.
* This is needed only for Firefox since it does not generate it when simulcast is enabled.
Copy link
Member

Choose a reason for hiding this comment

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

We call this regardless of the browser, is it really only required for FF?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this is only needed for Firefox now since it doesn't produce a SIM group but we also need to verify it on other browsers just in case as this would break the functionality otherwise. This code adds the SIM group only if its missing from the SDP.

* @returns {Array}
*/
getConfiguredVideoCodecs(description) {
const currentSdp = description?.sdp ?? this.pc.localDescription?.sdp;
getConfiguredVideoCodecs(sdp) {
Copy link
Member

Choose a reason for hiding this comment

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

I suggest you break this function into 2:

getConfiguredVideoCodecs(sdp) {
}

_getConfiguredVideoCodecsImpl(parsedSdp) {
}

With the former calling the latter, after parsing the SDP.

This will allow you to call the 2nd one in the munge code and doing it all in 1 pass!

@@ -35,7 +35,7 @@ describe('TPCUtils', () => {

it('sort ssrcs associated with all FID ssrc-groups', () => {
const pc = new MockPeerConnection();
const tpcUtils = new TPCUtils(pc);
const tpcUtils = new TPCUtils(pc, { });
Copy link
Member

Choose a reason for hiding this comment

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

You can remove this one now.

*/
TraceablePeerConnection.prototype._mungeDescription = function(description) {
this.trace('RTCSessionDescription::preTransform', dumpSDP(description));
let mungedSdp = transform.parse(description.sdp);
Copy link
Member

Choose a reason for hiding this comment

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

Why are we not using SdpTransformWrap here?

Copy link
Member Author

Choose a reason for hiding this comment

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

There is no added benefit of using SdpTransformWrap here since util functions operate on the parsedSDP directly. The helper functions that SdpTransformWrap provides are all related to SSRCs and only used by RTXModifier class.

Copy link
Member

@saghul saghul left a comment

Choose a reason for hiding this comment

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

🚀

@jallamsetty1 jallamsetty1 merged commit 39c4422 into master Nov 25, 2024
2 checks passed
@jallamsetty1 jallamsetty1 deleted the ref-tpc branch November 25, 2024 16:39
@saghul
Copy link
Member

saghul commented Nov 28, 2024

Pl next time use squash-merge, you can keep the individual commit messages if you want, but this was 1 logical change.

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.

2 participants