openvidu-browser: Fixed StreamPropertyChangedEvent when replaced tracks

pull/739/head
csantosm 2022-06-24 14:34:59 +02:00
parent ea3f16778e
commit 03a15d1077
2 changed files with 48 additions and 9 deletions

View File

@ -627,8 +627,8 @@ export class OpenVidu {
*/ */
sendNewVideoDimensionsIfRequired(publisher: Publisher, reason: string, WAIT_INTERVAL: number, MAX_ATTEMPTS: number) { sendNewVideoDimensionsIfRequired(publisher: Publisher, reason: string, WAIT_INTERVAL: number, MAX_ATTEMPTS: number) {
let attempts = 0; let attempts = 0;
const oldWidth = publisher.stream.videoDimensions.width; const oldWidth = publisher?.stream?.videoDimensions?.width || 0;
const oldHeight = publisher.stream.videoDimensions.height; const oldHeight = publisher?.stream?.videoDimensions?.height || 0;
const repeatUntilChangeOrMaxAttempts: NodeJS.Timeout = setInterval(() => { const repeatUntilChangeOrMaxAttempts: NodeJS.Timeout = setInterval(() => {
attempts++; attempts++;
@ -671,6 +671,36 @@ export class OpenVidu {
}); });
}; };
/**
* @hidden
*/
sendTrackChangedEvent(publisher: Publisher, reason: string, oldLabel: string, newLabel: string, propertyType: string) {
const oldValue = {label: oldLabel};
const newValue = {label: newLabel};
if(publisher.stream.isLocalStreamPublished){
this.sendRequest(
'streamPropertyChanged',
{
streamId: publisher.stream.streamId,
property: propertyType,
newValue: JSON.stringify({newLabel}),
reason
},
(error, response) => {
if (error) {
logger.error("Error sending 'streamPropertyChanged' event", error);
} else {
this.session.emitEvent('streamPropertyChanged', [new StreamPropertyChangedEvent(this.session, publisher.stream, propertyType, newValue, oldValue, reason)]);
publisher.emitEvent('streamPropertyChanged', [new StreamPropertyChangedEvent(publisher, publisher.stream, propertyType, newValue, oldValue, reason)]);
}
});
} else {
this.session.emitEvent('streamPropertyChanged', [new StreamPropertyChangedEvent(this.session, publisher.stream, propertyType, newValue, oldValue, reason)]);
publisher.emitEvent('streamPropertyChanged', [new StreamPropertyChangedEvent(publisher, publisher.stream, propertyType, newValue, oldValue, reason)]);
}
}
/** /**
* @hidden * @hidden
*/ */
@ -826,7 +856,7 @@ export class OpenVidu {
params = {}; params = {};
} }
logger.debug('Sending request: {method:"' + method + '", params: ' + JSON.stringify(params) + '}'); logger.debug('Sending request: {method:"' + method + '", params: ' + JSON.stringify(params) + '}');
this.jsonRpcClient.send(method, params, callback); this.jsonRpcClient?.send(method, params, callback);
} }
/** /**

View File

@ -651,11 +651,11 @@ export class Publisher extends StreamManager {
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
await this.replaceTrackInMediaStream(track, updateLastConstraints); this.replaceTrackInMediaStream(track, updateLastConstraints);
return await this.replaceTrackInRtcRtpSender(track); return await this.replaceTrackInRtcRtpSender(track);
} else { } else {
// Publisher not published. Simply replace the track on the local MediaStream // Publisher not published. Simply replace the track on the local MediaStream
return await this.replaceTrackInMediaStream(track, updateLastConstraints); return this.replaceTrackInMediaStream(track, updateLastConstraints);
} }
} catch (error) { } catch (error) {
track.enabled = trackOriginalEnabledValue; track.enabled = trackOriginalEnabledValue;
@ -773,10 +773,19 @@ export class Publisher extends StreamManager {
removedTrack.stop(); removedTrack.stop();
mediaStream.removeTrack(removedTrack); mediaStream.removeTrack(removedTrack);
mediaStream.addTrack(track); mediaStream.addTrack(track);
if (track.kind === 'video' && this.stream.isLocalStreamPublished && updateLastConstraints) { const trackInfo = {
oldLabel: removedTrack?.label || '',
newLabel: track?.label || ''
};
if (track.kind === 'video' && updateLastConstraints) {
this.openvidu.sendNewVideoDimensionsIfRequired(this, 'trackReplaced', 50, 30); this.openvidu.sendNewVideoDimensionsIfRequired(this, 'trackReplaced', 50, 30);
this.openvidu.sendTrackChangedEvent(this,'trackReplaced', trackInfo.oldLabel, trackInfo.newLabel, 'videoActive');
if(this.stream.isLocalStreamPublished) {
this.session.sendVideoData(this.stream.streamManager, 5, true, 5); this.session.sendVideoData(this.stream.streamManager, 5, true, 5);
} }
} else if(track.kind === 'audio' && updateLastConstraints) {
this.openvidu.sendTrackChangedEvent(this,'trackReplaced', trackInfo.oldLabel, trackInfo.newLabel, 'audioActive');
}
} }
/* Private methods */ /* Private methods */