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 * Creates an SDP offer from the local RTCPeerConnection to send to the other peer.
* Only if the negotiation was initiated by this peer * Only if the negotiation was initiated by this peer.
*/ */
createOffer(): Promise<RTCSessionDescriptionInit> { async createOffer(): Promise<RTCSessionDescriptionInit> {
return new Promise(async (resolve, reject) => {
// TODO: Delete this conditional when all supported browsers are // TODO: Delete this conditional when all supported browsers are
// modern enough to implement the Transceiver methods. // 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"); logger.debug("[createOffer] Method RTCPeerConnection.addTransceiver() is available; using it");
}
// Spec doc: https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtransceiver // Spec doc: https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtransceiver
if (this.configuration.mode !== "recvonly") { if (this.configuration.mode !== "recvonly") {
// To send media, assume that all desired media tracks // To send media, assume that all desired media tracks have been
// have been already added by higher level code to our // already added by higher level code to our MediaStream.
// MediaStream.
if (!this.configuration.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()) { 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] Video track Content Hint set: '${track.contentHint}'`);
} }
logger.info(`[createOffer] this.configuration.simulcast: ${this.configuration.simulcast}`);
if (this.configuration.simulcast) { if (this.configuration.simulcast) {
// Check if the requested size is enough to ask for 3 layers. // Check if the requested size is enough to ask for 3 layers.
const trackSettings = track.getSettings(); const trackSettings = track.getSettings();
@ -202,7 +228,7 @@ export class WebRtcPeer {
const encoding: RTCRtpEncodingParameters = { const encoding: RTCRtpEncodingParameters = {
rid: "rDiv" + layerDiv.toString(), rid: "rDiv" + layerDiv.toString(),
// @ts-ignore: Property missing from DOM types. // @ts-ignore -- Property missing from DOM types.
scalabilityMode: "L1T1", scalabilityMode: "L1T1",
}; };
@ -210,7 +236,7 @@ export class WebRtcPeer {
// Prioritize best resolution, for maximum picture detail. // Prioritize best resolution, for maximum picture detail.
encoding.scaleResolutionDownBy = 1.0; 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 = Math.floor(30 / layerDiv);
// encoding.maxFramerate = (l === 2) ? 30 : Math.floor(30 / (2 * layerDiv)); // TESTING // encoding.maxFramerate = (l === 2) ? 30 : Math.floor(30 / (2 * layerDiv)); // TESTING
} else { } else {
@ -229,9 +255,7 @@ export class WebRtcPeer {
let needSetParams = false; let needSetParams = false;
if (!("degradationPreference" in sendParams)) { if (!("degradationPreference" in sendParams)) {
logger.debug( logger.debug(`[createOffer] RTCRtpSendParameters.degradationPreference attribute not present`);
`[createOffer] RTCRtpSendParameters.degradationPreference attribute not present`
);
// Asked about why this might happen. Check it: // Asked about why this might happen. Check it:
// https://groups.google.com/g/discuss-webrtc/c/R8Xug-irfRY // https://groups.google.com/g/discuss-webrtc/c/R8Xug-irfRY
@ -311,40 +335,7 @@ export class WebRtcPeer {
} }
} }
this.pc return this.pc.createOffer();
.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));
}
});
} }
deprecatedPeerConnectionTrackApi() { deprecatedPeerConnectionTrackApi() {