openvidu-browser: Reduce 2 nesting levels in WebRtcPeer.createOffer

pull/699/head
Juan Navarro 2022-02-03 16:28:14 +01:00
parent af818b66f6
commit f10649b40c
1 changed files with 206 additions and 215 deletions

View File

@ -120,26 +120,53 @@ export class WebRtcPeer {
}
}
// DEPRECATED LEGACY METHOD: Old WebRTC versions don't implement
// Transceivers, and instead depend on the deprecated
// "offerToReceiveAudio" and "offerToReceiveVideo".
private createOfferLegacy(): Promise<RTCSessionDescriptionInit> {
if (!!this.configuration.mediaStream) {
this.deprecatedPeerConnectionTrackApi();
}
const hasAudio = this.configuration.mediaConstraints.audio;
const hasVideo = this.configuration.mediaConstraints.video;
const options: RTCOfferOptions = {
offerToReceiveAudio: this.configuration.mode !== "sendonly" && hasAudio,
offerToReceiveVideo: this.configuration.mode !== "sendonly" && hasVideo,
};
logger.debug("[createOfferLegacy] RTCPeerConnection.createOffer() options:", JSON.stringify(options));
return this.pc.createOffer(options);
}
/**
* Creates an SDP offer from the local RTCPeerConnection to send to the other peer
* Only if the negotiation was initiated by this peer
* Creates an SDP offer from the local RTCPeerConnection to send to the other peer.
* Only if the negotiation was initiated by this peer.
*/
createOffer(): Promise<RTCSessionDescriptionInit> {
return new Promise(async (resolve, reject) => {
async createOffer(): Promise<RTCSessionDescriptionInit> {
// TODO: Delete this conditional when all supported browsers are
// modern enough to implement the Transceiver methods.
if ("addTransceiver" in this.pc) {
if (!("addTransceiver" in this.pc)) {
logger.warn(
"[createOffer] Method RTCPeerConnection.addTransceiver() is NOT available; using LEGACY offerToReceive{Audio,Video}"
);
return this.createOfferLegacy();
} else {
logger.debug("[createOffer] Method RTCPeerConnection.addTransceiver() is available; using it");
}
// Spec doc: https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtransceiver
if (this.configuration.mode !== "recvonly") {
// To send media, assume that all desired media tracks
// have been already added by higher level code to our
// MediaStream.
// To send media, assume that all desired media tracks have been
// already added by higher level code to our MediaStream.
if (!this.configuration.mediaStream) {
return reject(new Error(`${this.configuration.mode} direction requested, but no stream was configured to be sent`));
throw new Error(
`[WebRtcPeer.createOffer] Direction is '${this.configuration.mode}', but no stream was configured to be sent`
);
}
for (const track of this.configuration.mediaStream.getTracks()) {
@ -167,7 +194,6 @@ export class WebRtcPeer {
logger.info(`[createOffer] Video track Content Hint set: '${track.contentHint}'`);
}
logger.info(`[createOffer] this.configuration.simulcast: ${this.configuration.simulcast}`);
if (this.configuration.simulcast) {
// Check if the requested size is enough to ask for 3 layers.
const trackSettings = track.getSettings();
@ -202,7 +228,7 @@ export class WebRtcPeer {
const encoding: RTCRtpEncodingParameters = {
rid: "rDiv" + layerDiv.toString(),
// @ts-ignore: Property missing from DOM types.
// @ts-ignore -- Property missing from DOM types.
scalabilityMode: "L1T1",
};
@ -210,7 +236,7 @@ export class WebRtcPeer {
// Prioritize best resolution, for maximum picture detail.
encoding.scaleResolutionDownBy = 1.0;
// @ts-ignore: Property missing from DOM types.
// @ts-ignore -- Property missing from DOM types.
encoding.maxFramerate = Math.floor(30 / layerDiv);
// encoding.maxFramerate = (l === 2) ? 30 : Math.floor(30 / (2 * layerDiv)); // TESTING
} else {
@ -229,9 +255,7 @@ export class WebRtcPeer {
let needSetParams = false;
if (!("degradationPreference" in sendParams)) {
logger.debug(
`[createOffer] RTCRtpSendParameters.degradationPreference attribute not present`
);
logger.debug(`[createOffer] RTCRtpSendParameters.degradationPreference attribute not present`);
// Asked about why this might happen. Check it:
// https://groups.google.com/g/discuss-webrtc/c/R8Xug-irfRY
@ -311,40 +335,7 @@ export class WebRtcPeer {
}
}
this.pc
.createOffer()
.then((sdpOffer) => resolve(sdpOffer))
.catch((error) => reject(error));
} else {
logger.warn("[createOffer] Method RTCPeerConnection.addTransceiver() is NOT available; using LEGACY offerToReceive{Audio,Video}");
// DEPRECATED LEGACY METHOD: Old WebRTC versions don't implement
// Transceivers, and instead depend on the deprecated
// "offerToReceiveAudio" and "offerToReceiveVideo".
if (!!this.configuration.mediaStream) {
this.deprecatedPeerConnectionTrackApi();
}
const hasAudio = this.configuration.mediaConstraints.audio;
const hasVideo = this.configuration.mediaConstraints.video;
const options: RTCOfferOptions = {
offerToReceiveAudio:
this.configuration.mode !== "sendonly" && hasAudio,
offerToReceiveVideo:
this.configuration.mode !== "sendonly" && hasVideo,
};
logger.debug("RTCPeerConnection.createOffer() options:", JSON.stringify(options));
this.pc
// @ts-ignore - Compiler is too clever and thinks this branch will never execute.
.createOffer(options)
.then((sdpOffer) => resolve(sdpOffer))
.catch((error) => reject(error));
}
});
return this.pc.createOffer();
}
deprecatedPeerConnectionTrackApi() {