From b04ea8e173f918d1d8c425f6041eec38b13ac89d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Mon, 21 Feb 2022 14:34:41 +0000 Subject: [PATCH] [IMP] mail: turn "RtcSession/rtcDataChannel" into field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Task-2734101 closes odoo/odoo#85024 Signed-off-by: Sébastien Theys (seb) --- addons/mail/static/src/models/rtc.js | 36 +++++++++---------- .../static/src/models/rtc_data_channel.js | 25 +++++++++++++ addons/mail/static/src/models/rtc_session.js | 7 ++++ 3 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 addons/mail/static/src/models/rtc_data_channel.js diff --git a/addons/mail/static/src/models/rtc.js b/addons/mail/static/src/models/rtc.js index 4315c0d9f0b..498dc0ebaba 100644 --- a/addons/mail/static/src/models/rtc.js +++ b/addons/mail/static/src/models/rtc.js @@ -4,7 +4,7 @@ import { browser } from "@web/core/browser/browser"; import { registerModel } from '@mail/model/model_core'; import { attr, many, one } from '@mail/model/model_field'; -import { clear, insert, unlink } from '@mail/model/model_field_command'; +import { clear, insert, replace, unlink } from '@mail/model/model_field_command'; import { monitorAudio } from '@mail/utils/media_monitoring'; /** @@ -26,14 +26,6 @@ registerModel({ identifyingFields: ['messaging'], lifecycleHooks: { _created() { - // technical fields that are not exposed - // Especially important for _peerConnections, as garbage collection of peerConnections is important for - // peerConnection.close(). - /** - * Object { token: dataChannel } - * Contains the RTCDataChannels with the other rtc sessions. - */ - this._dataChannels = {}; /** /** * Object { token: peerConnection } @@ -206,7 +198,6 @@ registerModel({ this.audioTrack && this.audioTrack.stop(); this.videoTrack && this.videoTrack.stop(); - this._dataChannels = {}; this._peerConnections = {}; for (const rtcSession of this.messaging.models['RtcSession'].all()) { @@ -214,6 +205,7 @@ registerModel({ rtcSession.update({ connectionRecoveryTimeout: clear(), isConnected: clear(), + rtcDataChannel: clear(), }); } this.update({ @@ -507,7 +499,10 @@ registerModel({ } }; this._peerConnections[rtcSession.id] = peerConnection; - this._dataChannels[rtcSession.id] = dataChannel; + this.messaging.models['RtcDataChannel'].insert({ + dataChannel, + rtcSession: replace(rtcSession), + }); return peerConnection; }, /** @@ -667,11 +662,15 @@ registerModel({ } if (type === 'peerToPeer') { for (const token of targetTokens) { - const dataChannel = this._dataChannels[token]; - if (!dataChannel || dataChannel.readyState !== 'open') { + const rtcSession = this.messaging.models['RtcSession'].findFromIdentifyingData({ id: token }); + if (!rtcSession) { continue; } - dataChannel.send(JSON.stringify({ + const rtcDataChannel = rtcSession.rtcDataChannel; + if (!rtcDataChannel || rtcDataChannel.dataChannel.readyState !== 'open') { + continue; + } + rtcDataChannel.dataChannel.send(JSON.stringify({ event, channelId: this.channel.id, payload, @@ -750,12 +749,11 @@ registerModel({ const rtcSession = this.messaging.models['RtcSession'].findFromIdentifyingData({ id: sessionId }); if (rtcSession) { rtcSession.reset(); + const rtcDataChannel = rtcSession.rtcDataChannel; + if (rtcDataChannel) { + rtcDataChannel.delete(); + } } - const dataChannel = this._dataChannels[sessionId]; - if (dataChannel) { - dataChannel.close(); - } - delete this._dataChannels[sessionId]; const peerConnection = this._peerConnections[sessionId]; if (peerConnection) { this._removeRemoteTracks(peerConnection); diff --git a/addons/mail/static/src/models/rtc_data_channel.js b/addons/mail/static/src/models/rtc_data_channel.js new file mode 100644 index 00000000000..e7e7347fa07 --- /dev/null +++ b/addons/mail/static/src/models/rtc_data_channel.js @@ -0,0 +1,25 @@ +/** @odoo-module **/ + +import { registerModel } from '@mail/model/model_core'; +import { attr, one } from '@mail/model/model_field'; + +registerModel({ + name: 'RtcDataChannel', + identifyingFields: ['rtcSession'], + lifecycleHooks: { + _willDelete() { + this.dataChannel.close(); + }, + }, + fields: { + dataChannel: attr({ + required: true, + readonly: true, + }), + rtcSession: one('RtcSession', { + inverse: 'rtcDataChannel', + readonly: true, + required: true, + }), + }, +}); diff --git a/addons/mail/static/src/models/rtc_session.js b/addons/mail/static/src/models/rtc_session.js index 365969e9705..a9c123faf30 100644 --- a/addons/mail/static/src/models/rtc_session.js +++ b/addons/mail/static/src/models/rtc_session.js @@ -411,6 +411,13 @@ registerModel({ rtc: one('Rtc', { inverse: 'currentRtcSession', }), + /** + * Contains the RTCDataChannel of the rtc session. + */ + rtcDataChannel: one('RtcDataChannel', { + inverse: 'rtcSession', + isCausal: true, + }), /** * MediaStream of the user's video. *