Skip to content

Commit

Permalink
ICC Patches
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry2013 committed Apr 1, 2024
1 parent 311766e commit d16b7be
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions modules/RTC/ScreenObtainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ const ScreenObtainer = {
// working properly.
if (streamType === 'screen') {
audioConstraints.mandatory = {
echoCancellation: true, // Ref: https://github.com/electron/electron/issues/27337
chromeMediaSource: 'desktop'
};
}
Expand Down
73 changes: 70 additions & 3 deletions modules/RTC/TraceablePeerConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,44 @@ export default function TraceablePeerConnection(
*/
this._localTrackTransceiverMids = new Map();

/**
* ICC
* - iosrtc stream sequence is out-of-sync with ICE state
* - safari 15 ontrack sequence is out-of-sync with ICE state
*
* @type {Array<[string, string]>}
*/
this._pendingStreams = [];
this._addPendingStreams = () => {
if (this.signalingState !== 'stable' || this.iceConnectionState !== 'connected') {
return;
}

// ICC: add because ICE is ready
for (const [ streamId, transceiverMid ] of this._pendingStreams) {
this.trace('onsignalingstatechange', streamId);
if (transceiverMid) {
const transceiver = this.peerconnection.getTransceivers().find(
ts => ts.mid === transceiverMid
);
const stream = this.peerconnection.getRemoteStreams().find(
s => s.id === streamId
);

if (stream && transceiver) {
this._remoteTrackAdded(stream, transceiver.receiver.track, transceiver);
}
} else {
const stream = this.peerconnection.remoteStreams[streamId];

if (stream) {
this._remoteStreamAdded(stream);
}
}
}
this._pendingStreams = [];
};

// override as desired
this.trace = (what, info) => {
logger.trace(what, info);
Expand All @@ -342,12 +380,31 @@ export default function TraceablePeerConnection(
}
};

/** @param {RTCTrackEvent} evt */
this.onTrack = evt => {
const stream = evt.streams[0];

this._remoteTrackAdded(stream, evt.track, evt.transceiver);
this.trace('ontrack',
`${this.iceConnectionState}, ${this.signalingState}: ${stream.id}`);
if (this.signalingState === 'have-remote-offer') {
// ICC: safari 15 triggers ontrack before remoteDescription is set.
// We delay adding the track.
this._pendingStreams.push([ stream.id, evt.transceiver.mid ]);
} else {
this._remoteTrackAdded(stream, evt.track, evt.transceiver);
}
stream.addEventListener('removetrack', e => {
this._remoteTrackRemoved(stream, e.track);
this.trace('onremovetrack',
`${this.iceConnectionState}, ${this.signalingState}: ${stream.id}`);

// ICC: clean up if not yet added.
const idx = this._pendingStreams.findIndex(s => s[0] === stream.id);

if (idx === -1) {
this._remoteTrackRemoved(stream, e.track);
} else {
this._pendingStreams.splice(idx, 1);
}
});
};
this.peerconnection.addEventListener('track', this.onTrack);
Expand All @@ -358,13 +415,18 @@ export default function TraceablePeerConnection(
if (this.onsignalingstatechange !== null) {
this.onsignalingstatechange(event);
}
this._addPendingStreams();
};
this.oniceconnectionstatechange = null;
this.peerconnection.oniceconnectionstatechange = event => {
this.trace('oniceconnectionstatechange', this.iceConnectionState);
if (this.oniceconnectionstatechange !== null) {
this.oniceconnectionstatechange(event);
}
this._addPendingStreams();
if (this.iceConnectionState === 'closed') {
this._pendingStreams = [];
}
};
this.onnegotiationneeded = null;
this.peerconnection.onnegotiationneeded = event => {
Expand Down Expand Up @@ -894,7 +956,12 @@ TraceablePeerConnection.prototype._remoteTrackAdded = function(stream, track, tr

let ssrcLines = SDPUtil.findLines(mediaLine, 'a=ssrc:');

ssrcLines = ssrcLines.filter(line => line.indexOf(`msid:${streamId}`) !== -1);
// ICC: iosrtc's streamId has extra uuid but msid line does not.
const _streamId = 'remoteStreams' in this.peerconnection
? streamId.slice(0, streamId.lastIndexOf('_'))
: streamId;

ssrcLines = ssrcLines.filter(line => line.indexOf(`msid:${_streamId}`) !== -1);
if (!ssrcLines.length) {
logger.error(`No SSRC lines found in remote SDP for remote stream[msid=${streamId},type=${mediaType}]`
+ 'track creation failed!');
Expand Down

0 comments on commit d16b7be

Please sign in to comment.