openvidu-browser: small refactoring of Publisher.replaceTrack method

pull/621/head
pabloFuente 2021-03-25 16:29:38 +01:00
parent f92869cff5
commit 087ef665c2
1 changed files with 16 additions and 26 deletions

View File

@ -304,7 +304,7 @@ export class Publisher extends StreamManager {
* *
* @returns A Promise (to which you can optionally subscribe to) that is resolved if the track was successfully replaced and rejected with an Error object in other case * @returns A Promise (to which you can optionally subscribe to) that is resolved if the track was successfully replaced and rejected with an Error object in other case
*/ */
replaceTrack(track: MediaStreamTrack): Promise<void> { async replaceTrack(track: MediaStreamTrack): Promise<void> {
const replaceTrackInMediaStream = (): Promise<void> => { const replaceTrackInMediaStream = (): Promise<void> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -354,7 +354,6 @@ export class Publisher extends StreamManager {
}); });
} }
return new Promise(async (resolve, reject) => {
// Set field "enabled" of the new track to the previous value // Set field "enabled" of the new track to the previous value
const trackOriginalEnabledValue: boolean = track.enabled; const trackOriginalEnabledValue: boolean = track.enabled;
if (track.kind === 'video') { if (track.kind === 'video') {
@ -362,30 +361,21 @@ export class Publisher extends StreamManager {
} else if (track.kind === 'audio') { } else if (track.kind === 'audio') {
track.enabled = this.stream.audioActive; track.enabled = this.stream.audioActive;
} }
try {
if (this.stream.isLocalStreamPublished) { if (this.stream.isLocalStreamPublished) {
// Only if the Publisher has been published is necessary to call native Web API RTCRtpSender.replaceTrack // Only if the Publisher has been published is necessary to call native Web API RTCRtpSender.replaceTrack
// If it has not been published yet, replacing it on the MediaStream object is enough // If it has not been published yet, replacing it on the MediaStream object is enough
try {
await replaceTrackInRtcRtpSender(); await replaceTrackInRtcRtpSender();
await replaceTrackInMediaStream(); return await replaceTrackInMediaStream();
resolve();
} catch (error) {
track.enabled = trackOriginalEnabledValue;
reject(error);
}
} else { } else {
// Publisher not published. Simply replace the track on the local MediaStream // Publisher not published. Simply replace the track on the local MediaStream
try { return await replaceTrackInMediaStream();
await replaceTrackInMediaStream(); }
resolve();
} catch (error) { } catch (error) {
track.enabled = trackOriginalEnabledValue; track.enabled = trackOriginalEnabledValue;
reject(error); throw error;
} }
} }
});
}
/* Hidden methods */ /* Hidden methods */