Skip to content

Commit

Permalink
fix: Use RTCRtpSender.replaceTrack() for applying presenter effect on FF
Browse files Browse the repository at this point in the history
  • Loading branch information
jallamsetty1 committed Dec 10, 2019
1 parent 4e7034e commit d5d8e0a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
26 changes: 26 additions & 0 deletions JitsiConference.js
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,32 @@ JitsiConference.prototype.replaceTrack = function(oldTrack, newTrack) {
}, error => Promise.reject(new Error(error)));
};

/**
* Replaces the track at the lower level by going through the Jingle session
* and WebRTC peer connection. The track is replaced without the need for an
* offer/answer cycle.
* @param {JitsiLocalTrack} localTrack - the local track whose media stream has
* been updated.
*/
JitsiConference.prototype.replaceTrackWithoutOfferAnswer = function(localTrack) {
const replaceTrackPromises = [];

if (this.jvbJingleSession) {
replaceTrackPromises.push(
this.jvbJingleSession.replaceTrackWithoutOfferAnswer(localTrack));
} else {
logger.info('replaceTrackWithoutOfferAnswer - no JVB JingleSession');
}
if (this.p2pJingleSession) {
replaceTrackPromises.push(
this.p2pJingleSession.replaceTrackWithoutOfferAnswer(localTrack));
} else {
logger.info('_doReplaceTrack - no P2P JingleSession');
}

return Promise.all(replaceTrackPromises);
};

/**
* Replaces the tracks at the lower level by going through the Jingle session
* and WebRTC peer connection. The method will resolve immediately if there is
Expand Down
17 changes: 16 additions & 1 deletion modules/RTC/JitsiLocalTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ export default class JitsiLocalTrack extends JitsiTrack {
if (this._streamEffect) {
this._streamEffect.stopEffect();
this._setStream(this._originalStream);
this._originalStream = undefined;
}
}

Expand Down Expand Up @@ -392,6 +391,22 @@ export default class JitsiLocalTrack extends JitsiTrack {

this._setEffectInProgress = true;

// For firefox/safari, replace the stream without doing a offer answer with the remote peer.
if (browser.supportsRtpSender()) {
this._switchStreamEffect(effect);

return conference.replaceTrackWithoutOfferAnswer(this)
.then(() => {
this._setEffectInProgress = false;
})
.catch(error => {
this._setEffectInProgress = false;
this._switchStreamEffect();
logger.error('Failed to switch to the new stream!', error);
throw error;
});
}

// TODO: Create new JingleSessionPC method for replacing a stream in JitsiLocalTrack without offer answer.
return conference.removeTrack(this)
.then(() => {
Expand Down
38 changes: 38 additions & 0 deletions modules/RTC/TraceablePeerConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import transform from 'sdp-transform';

import * as GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
import JitsiRemoteTrack from './JitsiRemoteTrack';
import * as JitsiTrackEvents from '../../JitsiTrackEvents';
import * as MediaType from '../../service/RTC/MediaType';
import LocalSdpMunger from './LocalSdpMunger';
import RTC from './RTC';
Expand Down Expand Up @@ -1546,6 +1547,16 @@ TraceablePeerConnection.prototype.removeTrack = function(localTrack) {
}
};

/**
* Returns the sender corresponding to the given media type.
* @param {MEDIA_TYPE} mediaType - The media type 'audio' or 'video' to be used for the search.
* @returns {RTPSender|undefined} - The found sender or undefined if no sender
* was found.
*/
TraceablePeerConnection.prototype.findSenderByKind = function(mediaType) {
return this.peerconnection.getSenders().find(s => s.track && s.track.kind === mediaType);
};

/**
* Returns the sender corresponding to the given MediaStream.
*
Expand Down Expand Up @@ -1633,6 +1644,33 @@ TraceablePeerConnection.prototype.replaceTrack = function(oldTrack, newTrack) {
return Promise.resolve(true);
};

/**
* Replaces the existing media stream from the underlying peerconnection with the new
* mediastream that has been added to the JitsiLocalTrack. Renegotiation with the remote
* peer is not needed in this case.
* @param {JitsiLocalTrack} localTrack - the localtrack whose mediastream has been updated.
* @return {Promise} - Promise resolved with undefined if the track is replaced,
* or rejected with <tt>InvalidModificationError<tt> if the track cannot be replaced.
*/
TraceablePeerConnection.prototype.replaceTrackWithoutOfferAnswer = function(localTrack) {
const newTrack = localTrack.stream.getTracks()[0];
const sender = this.findSenderByKind(newTrack.kind);

if (!sender) {
return Promise.reject(new Error(`Could not find RTCRtpSender for ${newTrack.kind}`));
}

return sender.replaceTrack(newTrack)
.then(() => {
this._addedStreams = this._addedStreams.filter(s => s !== localTrack._originalStream);
this._addedStreams.push(localTrack.stream);
localTrack.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED, localTrack);
})
.catch(err => {
logger.error(`replaceTrackWithoutOfferAnswer - replaceTrack failed for ${newTrack.kind}`, err);
});
};

/**
* Removes local track as part of the mute operation.
* @param {JitsiLocalTrack} localTrack the local track to be remove as part of
Expand Down
13 changes: 13 additions & 0 deletions modules/xmpp/JingleSessionPC.js
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,19 @@ export default class JingleSessionPC extends JingleSession {
});
}

/**
* Replaces the existing mediaStream on the underlying peerconnection with the newly
* added stream on the same JitsiLocalTrack wihtout the need to perform a offer/answer
* cycle.
* @param {JitsiLocalTrack} track - the current track in use whose media stream has been
* updated.
* @returns {Promise} which resolves once the replacement is complete or reject with an
* error {string}.
*/
replaceTrackWithoutOfferAnswer(track) {
return this.peerconnection.replaceTrackWithoutOfferAnswer(track);
}

/**
* Replaces <tt>oldTrack</tt> with <tt>newTrack</tt> and performs a single
* offer/answer cycle after both operations are done. Either
Expand Down

0 comments on commit d5d8e0a

Please sign in to comment.