Skip to content

Commit

Permalink
ref(TPC): Convert to async functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jallamsetty1 committed Nov 25, 2024
1 parent ad5559a commit 76c66eb
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions modules/RTC/TraceablePeerConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1331,11 +1331,11 @@ TraceablePeerConnection.prototype._isSharingScreen = function() {
* @param {boolean} isInitiator indicates if the endpoint is the offerer.
* @returns {Promise<void>} - resolved when done.
*/
TraceablePeerConnection.prototype.addTrack = function(track, isInitiator = false) {
TraceablePeerConnection.prototype.addTrack = async function(track, isInitiator = false) {
const rtcId = track.rtcId;

if (this.localTracks.has(rtcId)) {
return Promise.reject(new Error(`${track} is already in ${this}`));
throw new Error(`${track} is already in ${this}`);
}

logger.info(`${this} adding ${track}`);
Expand All @@ -1360,11 +1360,7 @@ TraceablePeerConnection.prototype.addTrack = function(track, isInitiator = false
transceiverInit.sendEncodings = this.tpcUtils.getStreamEncodings(track);
}

try {
transceiver = this.peerconnection.addTransceiver(mediaStreamTrack, transceiverInit);
} catch (error) {
return Promise.reject(new Error(`${this} failed to create transceiver for ${track} - ${error}`));
}
transceiver = this.peerconnection.addTransceiver(mediaStreamTrack, transceiverInit);
} else {
// Use pc.addTrack() for responder case so that we can re-use the m-lines that were created
// when setRemoteDescription was called. pc.addTrack() automatically attaches to any existing
Expand All @@ -1388,14 +1384,10 @@ TraceablePeerConnection.prototype.addTrack = function(track, isInitiator = false
}
}

let promiseChain = Promise.resolve();

// On Firefox, the encodings have to be configured on the sender only after the transceiver is created.
if (browser.isFirefox()) {
promiseChain = promiseChain.then(() => webrtcStream && this._setEncodings(track));
if (browser.isFirefox() && webrtcStream) {
await this._setEncodings(track);
}

return promiseChain;
};

/**
Expand Down Expand Up @@ -1923,7 +1915,7 @@ TraceablePeerConnection.prototype.configureAudioSenderEncodings = function(local
* @param {JitsiLocalTracj} localTrack - The local track whose outbound stream needs to be configured.
* @returns {Promise} - A promise that resolves when the operation is successful, rejected otherwise.
*/
TraceablePeerConnection.prototype._configureSenderEncodings = function(localTrack) {
TraceablePeerConnection.prototype._configureSenderEncodings = async function(localTrack) {
const mediaType = localTrack.getType();
const transceiver = localTrack?.track && localTrack.getOriginalStream()
? this.peerconnection.getTransceivers().find(t => t.sender?.track?.id === localTrack.getTrackId())
Expand All @@ -1934,12 +1926,11 @@ TraceablePeerConnection.prototype._configureSenderEncodings = function(localTrac
// 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();
return;
}

parameters.encodings = this.tpcUtils.getStreamEncodings(localTrack);

return transceiver.sender.setParameters(parameters);
await transceiver.sender.setParameters(parameters);
};

/**
Expand All @@ -1949,7 +1940,7 @@ TraceablePeerConnection.prototype._configureSenderEncodings = function(localTrac
* @param {boolean} enable - whether the streams needs to be enabled or disabled.
* @returns {Promise} - A promise that resolves when the operation is successful, rejected otherwise.
*/
TraceablePeerConnection.prototype._enableSenderEncodings = function(sender, enable) {
TraceablePeerConnection.prototype._enableSenderEncodings = async function(sender, enable) {
const parameters = sender.getParameters();

if (parameters?.encodings?.length) {
Expand All @@ -1958,7 +1949,7 @@ TraceablePeerConnection.prototype._enableSenderEncodings = function(sender, enab
}
}

return sender.setParameters(parameters);
await sender.setParameters(parameters);
};

/**
Expand Down

0 comments on commit 76c66eb

Please sign in to comment.