From d33188e9a6a9f5af82ece70398fa51cf70086ee4 Mon Sep 17 00:00:00 2001 From: Thanh Dodeur Date: Fri, 13 May 2022 11:20:18 +0000 Subject: [PATCH] [REF] mail: move session stream update to the right model part of task-2692836 Part-of: odoo/odoo#91296 --- addons/mail/static/src/models/rtc.js | 28 +---- addons/mail/static/src/models/rtc_session.js | 121 +++++++++++-------- 2 files changed, 73 insertions(+), 76 deletions(-) diff --git a/addons/mail/static/src/models/rtc.js b/addons/mail/static/src/models/rtc.js index 75d14ca898a..e92a18643e1 100644 --- a/addons/mail/static/src/models/rtc.js +++ b/addons/mail/static/src/models/rtc.js @@ -448,7 +448,7 @@ registerModel({ }; peerConnection.ontrack = ({ transceiver, track }) => { this._addLogEntry(rtcSession.id, `received ${track.kind} track`); - this._updateExternalSessionTrack(track, rtcSession); + rtcSession.updateStream(track); }; const dataChannel = peerConnection.createDataChannel("notifications", { negotiated: true, id: 1 }); dataChannel.onmessage = (event) => { @@ -910,31 +910,7 @@ registerModel({ if (!this.videoTrack) { this.currentRtcSession.removeVideo(); } else { - this._updateExternalSessionTrack(this.videoTrack, this.currentRtcSession); - } - }, - /** - * Updates the RtcSession with a new track. - * - * @private - * @param {Track} [track] - * @param {RtcSession} rtcSession - */ - _updateExternalSessionTrack(track, rtcSession) { - const stream = new window.MediaStream(); - stream.addTrack(track); - - if (track.kind === 'audio') { - rtcSession.setAudio({ - audioStream: stream, - isSelfMuted: false, - isTalking: false, - }); - } - if (track.kind === 'video') { - rtcSession.update({ - videoStream: stream, - }); + this.currentRtcSession.updateStream(this.videoTrack); } }, /** diff --git a/addons/mail/static/src/models/rtc_session.js b/addons/mail/static/src/models/rtc_session.js index 2d6d8b20ec4..0e1543f81ed 100644 --- a/addons/mail/static/src/models/rtc_session.js +++ b/addons/mail/static/src/models/rtc_session.js @@ -60,56 +60,6 @@ registerModel({ videoStream: clear(), }); }, - /** - * @param {Object} param0 - * @param {MediaStream} param0.audioStream - * @param {boolean} param0.isSelfMuted - * @param {boolean} param0.isTalking - */ - async setAudio({ audioStream, isSelfMuted, isTalking }) { - const audioElement = this.audioElement || new window.Audio(); - try { - audioElement.srcObject = audioStream; - } catch (error) { - this.update({ isAudioInError: true }); - console.error(error); - } - audioElement.load(); - if (this.partner && this.partner.volumeSetting) { - audioElement.volume = this.partner.volumeSetting.volume; - } else if (this.guest && this.guest.volumeSetting) { - audioElement.volume = this.guest.volumeSetting.volume; - } else { - audioElement.volume = this.volume; - } - audioElement.muted = this.messaging.rtc.currentRtcSession.isDeaf; - // Using both autoplay and play() as safari may prevent play() outside of user interactions - // while some browsers may not support or block autoplay. - audioElement.autoplay = true; - this.update({ - audioElement, - audioStream, - isSelfMuted, - isTalking, - }); - try { - await audioElement.play(); - if (!this.exists()) { - return; - } - this.update({ isAudioInError: false }); - } catch (error) { - if (typeof error === 'object' && error.name === 'NotAllowedError') { - // Ignored as some browsers may reject play() calls that do not - // originate from a user input. - return; - } - if (this.exists()) { - this.update({ isAudioInError: true }); - } - console.error(error); - } - }, /** * @param {number} volume */ @@ -196,6 +146,26 @@ registerModel({ remoteCandidateType: clear(), }); }, + /** + * Updates the RtcSession with a new track. + * + * @param {Track} [track] + */ + updateStream(track) { + const stream = new window.MediaStream(); + stream.addTrack(track); + + if (track.kind === 'audio') { + this._setAudio({ + audioStream: stream, + isSelfMuted: false, + isTalking: false, + }); + } + if (track.kind === 'video') { + this.update({ videoStream: stream }); + } + }, /** * @private * @returns {string} @@ -323,6 +293,57 @@ registerModel({ isAudioInError: false, }); }, + /** + * @private + * @param {Object} param0 + * @param {MediaStream} param0.audioStream + * @param {boolean} param0.isSelfMuted + * @param {boolean} param0.isTalking + */ + async _setAudio({ audioStream, isSelfMuted, isTalking }) { + const audioElement = this.audioElement || new window.Audio(); + try { + audioElement.srcObject = audioStream; + } catch (error) { + this.update({ isAudioInError: true }); + console.error(error); + } + audioElement.load(); + if (this.partner && this.partner.volumeSetting) { + audioElement.volume = this.partner.volumeSetting.volume; + } else if (this.guest && this.guest.volumeSetting) { + audioElement.volume = this.guest.volumeSetting.volume; + } else { + audioElement.volume = this.volume; + } + audioElement.muted = this.messaging.rtc.currentRtcSession.isDeaf; + // Using both autoplay and play() as safari may prevent play() outside of user interactions + // while some browsers may not support or block autoplay. + audioElement.autoplay = true; + this.update({ + audioElement, + audioStream, + isSelfMuted, + isTalking, + }); + try { + await audioElement.play(); + if (!this.exists()) { + return; + } + this.update({ isAudioInError: false }); + } catch (error) { + if (typeof error === 'object' && error.name === 'NotAllowedError') { + // Ignored as some browsers may reject play() calls that do not + // originate from a user input. + return; + } + if (this.exists()) { + this.update({ isAudioInError: true }); + } + console.error(error); + } + }, }, fields: { /**