This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
299 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { selectDominantSpeaker } from "@100mslive/hms-video-store"; | ||
|
||
class PeersSorter { | ||
listeners = new Set(); | ||
storeUnsubscribe; | ||
|
||
constructor(store) { | ||
this.store = store; | ||
this.peers = new Map(); | ||
this.lruPeers = new Set(); | ||
this.speaker = undefined; | ||
} | ||
|
||
setPeersAndTilesPerPage = ({ peers, tilesPerPage }) => { | ||
this.tilesPerPage = tilesPerPage; | ||
const peerIds = new Set(peers.map(peer => peer.id)); | ||
// remove existing peers which are no longer provided | ||
this.peers.forEach((_, key) => { | ||
if (!peerIds.has(key)) { | ||
this.peers.delete(key); | ||
} | ||
}); | ||
this.lruPeers = new Set( | ||
[...this.lruPeers].filter(peerId => peerIds.has(peerId)) | ||
); | ||
peers.forEach(peer => { | ||
this.peers.set(peer.id, peer); | ||
if (this.lruPeers.size < tilesPerPage) { | ||
this.lruPeers.add(peer.id); | ||
} | ||
}); | ||
if (!this.storeUnsubscribe) { | ||
this.storeUnsubscribe = this.store.subscribe( | ||
this.onDominantSpeakerChange, | ||
selectDominantSpeaker | ||
); | ||
} | ||
this.moveSpeakerToFront(this.speaker); | ||
}; | ||
|
||
onUpdate = cb => { | ||
this.listeners.add(cb); | ||
}; | ||
|
||
stop = () => { | ||
this.updateListeners(); | ||
this.listeners.clear(); | ||
this.storeUnsubscribe?.(); | ||
}; | ||
|
||
moveSpeakerToFront = speaker => { | ||
if (!speaker) { | ||
this.updateListeners(); | ||
return; | ||
} | ||
if ( | ||
this.lruPeers.has(speaker.id) && | ||
this.lruPeers.size <= this.tilesPerPage | ||
) { | ||
this.updateListeners(); | ||
return; | ||
} | ||
// delete to insert at beginning | ||
this.lruPeers.delete(speaker.id); | ||
let lruPeerArray = Array.from(this.lruPeers); | ||
while (lruPeerArray.length >= this.tilesPerPage) { | ||
lruPeerArray.pop(); | ||
} | ||
this.lruPeers = new Set([speaker.id, ...lruPeerArray]); | ||
this.updateListeners(); | ||
}; | ||
|
||
onDominantSpeakerChange = speaker => { | ||
if (speaker && speaker.id !== this?.speaker?.id) { | ||
this.speaker = speaker; | ||
this.moveSpeakerToFront(speaker); | ||
} | ||
}; | ||
|
||
updateListeners = () => { | ||
const orderedPeers = []; | ||
this.lruPeers.forEach(key => { | ||
const peer = this.peers.get(key); | ||
if (peer) { | ||
orderedPeers.push(peer); | ||
} | ||
}); | ||
this.peers.forEach(peer => { | ||
if (!this.lruPeers.has(peer.id) && peer) { | ||
orderedPeers.push(peer); | ||
} | ||
}); | ||
this.listeners.forEach(listener => listener?.(orderedPeers)); | ||
}; | ||
} | ||
|
||
export default PeersSorter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
import { useHMSVanillaStore } from "@100mslive/react-sdk"; | ||
import PeersSorter from "./PeersSorter"; | ||
import { useActiveSpeakerSorting } from "../components/AppData/useUISettings"; | ||
|
||
function useSortedPeers({ peers, maxTileCount = 9 }) { | ||
const [sortedPeers, setSortedPeers] = useState([]); | ||
const store = useHMSVanillaStore(); | ||
const activeSpeakerSorting = useActiveSpeakerSorting(); | ||
const peerSortedRef = useRef(new PeersSorter(store)); | ||
peerSortedRef.current.onUpdate(setSortedPeers); | ||
|
||
useEffect(() => { | ||
const peersSorter = peerSortedRef.current; | ||
if (peers?.length > 0 && maxTileCount && activeSpeakerSorting) { | ||
peersSorter.setPeersAndTilesPerPage({ | ||
peers, | ||
tilesPerPage: maxTileCount, | ||
}); | ||
} else if (!activeSpeakerSorting) { | ||
peersSorter.stop(); | ||
} | ||
}, [maxTileCount, peers, activeSpeakerSorting]); | ||
|
||
return activeSpeakerSorting ? sortedPeers : peers; | ||
} | ||
|
||
export default useSortedPeers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
b654ef4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
100ms-web – ./
100ms-web-git-main-akash1234.vercel.app
100ms-web-akash1234.vercel.app
100ms-web-taupe.vercel.app
b654ef4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
100ms-web-2-for-wiki – ./
100ms-web-2-for-wiki-mycoolteam.vercel.app
100ms-web-2-for-wiki.vercel.app
100ms-web-2-for-wiki-git-main-mycoolteam.vercel.app
b654ef4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
100ms-web-v2 – ./
100ms-web-v2-100mslive.vercel.app
100ms-web-v2.vercel.app
dev2-infra.100ms.live
qa2.100ms.live
prod2.100ms.live
100ms-web-v2-git-main-100mslive.vercel.app