openvidu-browser: re-apply VB filter automatically after unmute

pull/721/head
pabloFuente 2022-05-05 14:01:31 +02:00
parent 99cd4cdfd7
commit f735819ae2
2 changed files with 75 additions and 51 deletions

View File

@ -168,7 +168,9 @@ export class Publisher extends StreamManager {
* useful if the Publisher was unpublished freeing the hardware resource, and openvidu-browser is not able to successfully re-create the video track as it was before unpublishing. In this way previous track settings will be ignored and this MediaStreamTrack * useful if the Publisher was unpublished freeing the hardware resource, and openvidu-browser is not able to successfully re-create the video track as it was before unpublishing. In this way previous track settings will be ignored and this MediaStreamTrack
* will be used instead. * will be used instead.
*/ */
publishVideo<T extends boolean>(enabled: T, resource?: T extends false ? boolean : MediaStreamTrack): void { publishVideo<T extends boolean>(enabled: T, resource?: T extends false ? boolean : MediaStreamTrack): Promise<void> {
return new Promise(async (resolve, reject) => {
if (this.stream.videoActive !== enabled) { if (this.stream.videoActive !== enabled) {
@ -184,27 +186,43 @@ export class Publisher extends StreamManager {
} }
}); });
// There is a Virtual Background filter applied that must be removed in case the hardware must be freed
if (!enabled && resource === true && !!this.stream.filter && this.stream.filter.type.startsWith('VB:')) {
this.stream.lastVBFilter = this.stream.filter; // Save the filter to be re-applied in case of unmute
await this.stream.removeFilterAux(true);
}
if (mustRestartMediaStream) { if (mustRestartMediaStream) {
const oldVideoTrack = affectedMediaStream.getVideoTracks()[0]; const oldVideoTrack = affectedMediaStream.getVideoTracks()[0];
affectedMediaStream.removeTrack(oldVideoTrack); affectedMediaStream.removeTrack(oldVideoTrack);
const replaceVideoTrack = (tr: MediaStreamTrack) => { const replaceVideoTrack = async (tr: MediaStreamTrack) => {
affectedMediaStream.addTrack(tr); affectedMediaStream.addTrack(tr);
if (this.stream.isLocalStreamPublished) { if (this.stream.isLocalStreamPublished) {
this.replaceTrackInRtcRtpSender(tr); await this.replaceTrackInRtcRtpSender(tr);
}
if (!!this.stream.lastVBFilter) {
setTimeout(async () => {
let options = this.stream.lastVBFilter!.options;
const lastExecMethod = this.stream.lastVBFilter!.lastExecMethod;
if (!!lastExecMethod && lastExecMethod.method === 'update') {
options = Object.assign({}, options, lastExecMethod.params);
}
await this.stream.applyFilter(this.stream.lastVBFilter!.type, options);
delete this.stream.lastVBFilter;
}, 1);
} }
} }
if (!!resource && resource instanceof MediaStreamTrack) { if (!!resource && resource instanceof MediaStreamTrack) {
replaceVideoTrack(resource); await replaceVideoTrack(resource);
} else { } else {
navigator.mediaDevices.getUserMedia({ audio: false, video: this.stream.lastVideoTrackConstraints }) try {
.then(mediaStream => { const mediaStream = await navigator.mediaDevices.getUserMedia({ audio: false, video: this.stream.lastVideoTrackConstraints });
replaceVideoTrack(mediaStream.getVideoTracks()[0]); await replaceVideoTrack(mediaStream.getVideoTracks()[0]);
}) } catch (error) {
.catch(error => { return reject(error);
console.error(error); }
});
} }
} }
@ -229,7 +247,9 @@ export class Publisher extends StreamManager {
} }
this.stream.videoActive = enabled; this.stream.videoActive = enabled;
logger.info("'Publisher' has " + (enabled ? 'published' : 'unpublished') + ' its video stream'); logger.info("'Publisher' has " + (enabled ? 'published' : 'unpublished') + ' its video stream');
return resolve();
} }
});
} }

View File

@ -232,6 +232,10 @@ export class Stream {
* @hidden * @hidden
*/ */
lastVideoTrackConstraints: MediaTrackConstraints | boolean | undefined; lastVideoTrackConstraints: MediaTrackConstraints | boolean | undefined;
/**
* @hidden
*/
lastVBFilter?: Filter;
/** /**