Skip to content

Commit

Permalink
fix: simplify track removal from store
Browse files Browse the repository at this point in the history
  • Loading branch information
raviteja83 committed Aug 1, 2024
1 parent c58345b commit 750b68c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
8 changes: 8 additions & 0 deletions packages/hms-video-store/src/sdk/store/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ class Store {
this.simulcastEnabled = enabled;
}

removeRemoteTracks() {
this.tracks.forEach(track => {
if (track instanceof HMSRemoteAudioTrack || track instanceof HMSRemoteVideoTrack) {
this.removeTrack(track);
}
});
}

getEnv() {
return this.env;
}
Expand Down
51 changes: 19 additions & 32 deletions packages/hms-video-store/src/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,36 +431,26 @@ export default class HMSTransport {
// eslint-disable-next-line complexity
async handleSFUMigration() {
HMSLogger.time('sfu migration');
this.clearPeerConnections();
const peers = this.store.getPeerMap();
this.store.removeRemoteTracks();
for (const peerId in peers) {
const peer = peers[peerId];
if (peer.isLocal) {
continue;
}
if (peer.audioTrack) {
this.store.removeTrack(peer.audioTrack);
peer.audioTrack = undefined;
}
if (peer.videoTrack) {
this.store.removeTrack(peer.videoTrack);
peer.videoTrack = undefined;
}
while (peer.auxiliaryTracks.length > 0) {
const track = peer.auxiliaryTracks.shift();
if (track) {
this.store.removeTrack(track);
}
}
peer.audioTrack = undefined;
peer.videoTrack = undefined;
peer.auxiliaryTracks = [];
}
this.clearPeerConnections();
this.createPeerConnections();
await this.negotiateOnFirstPublish();

const localPeer = this.store.getLocalPeer();
if (!localPeer) {
return;
}

let tracksToPublish = [];
console.log('creating peer connections');
this.createPeerConnections();
await this.negotiateOnFirstPublish();
const streamMap = new Map<string, HMSLocalStream>();
if (localPeer.audioTrack) {
const stream = localPeer.audioTrack.stream as HMSLocalStream;
Expand All @@ -469,7 +459,8 @@ export default class HMSTransport {
}
const newTrack = localPeer.audioTrack.clone(streamMap.get(stream.id));
this.store.removeTrack(localPeer.audioTrack);
tracksToPublish.push(newTrack);
localPeer.audioTrack.cleanup();
await this.publishTrack(newTrack);
localPeer.audioTrack = newTrack;
}

Expand All @@ -480,7 +471,8 @@ export default class HMSTransport {
}
this.store.removeTrack(localPeer.videoTrack);
const newTrack = localPeer.videoTrack.clone(streamMap.get(stream.id));
tracksToPublish.push(newTrack);
localPeer.videoTrack.cleanup();
await this.publishTrack(newTrack);
localPeer.videoTrack = newTrack;
}

Expand All @@ -497,16 +489,13 @@ export default class HMSTransport {
if (newTrack.type === 'video' && newTrack.source === 'screen') {
newTrack.nativeTrack.addEventListener('ended', this.onScreenshareStop);
}
track.cleanup();
await this.publishTrack(newTrack);
auxTracks.push(newTrack);
}
}
localPeer.auxiliaryTracks = auxTracks;
tracksToPublish = tracksToPublish.concat(auxTracks);
streamMap.clear();
for (const track of tracksToPublish) {
await this.publishTrack(track);
// this.listener?.onTrackUpdate(HMSTrackUpdate.TRACK_ADDED, track, localPeer);
}
this.listener?.onSFUMigration?.();
HMSLogger.timeEnd('sfu migration');
}
Expand Down Expand Up @@ -573,7 +562,7 @@ export default class HMSTransport {
HMSLogger.d(TAG, `✅ publishTrack: trackId=${track.trackId}`, `${track}`, this.callbacks);
}

private async unpublishTrack(track: HMSLocalTrack, sfuMigration = false): Promise<void> {
private async unpublishTrack(track: HMSLocalTrack): Promise<void> {
HMSLogger.d(TAG, `⏳ unpublishTrack: trackId=${track.trackId}`, `${track}`);
if (track.publishedTrackId && this.trackStates.has(track.publishedTrackId)) {
this.trackStates.delete(track.publishedTrackId);
Expand All @@ -599,11 +588,9 @@ export default class HMSTransport {
const stream = track.stream as HMSLocalStream;
stream.removeSender(track);
await p;
if (!sfuMigration) {
await track.cleanup();
// remove track from store on unpublish
this.store.removeTrack(track);
}
await track.cleanup();
// remove track from store on unpublish
this.store.removeTrack(track);
HMSLogger.d(TAG, `✅ unpublishTrack: trackId=${track.trackId}`, this.callbacks);
}

Expand Down

0 comments on commit 750b68c

Please sign in to comment.