[REF] mail: move session stream update to the right model

part of task-2692836

Part-of: odoo/odoo#91296
This commit is contained in:
Thanh Dodeur
2022-05-16 13:56:58 +02:00
parent 5799880ad0
commit d33188e9a6
2 changed files with 73 additions and 76 deletions
+2 -26
View File
@@ -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);
}
},
/**
+71 -50
View File
@@ -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: {
/**