Update StreamProvider.tsx

This commit is contained in:
talksik
2022-05-19 09:27:37 -05:00
parent bf93768a50
commit e14e9c70a8
@@ -46,7 +46,7 @@ export function StreamProvider({ children }: { children: React.ReactChild }) {
const peerForMeAndNewbie = new Peer({ const peerForMeAndNewbie = new Peer({
initiator: false, initiator: false,
trickle: false, // prevents the multiple tries on different ice servers and signal from getting called a bunch of times trickle: true, // prevents the multiple tries on different ice servers and signal from getting called a bunch of times
stream: userLocalStream, stream: userLocalStream,
config: { config: {
iceServers: [ iceServers: [
@@ -63,13 +63,7 @@ export function StreamProvider({ children }: { children: React.ReactChild }) {
peerForMeAndNewbie.signal(res.simplePeerSignal); peerForMeAndNewbie.signal(res.simplePeerSignal);
updatePeerMap((draft) => { handleAddPeer(res.lineId, res.userWhoCalled, peerForMeAndNewbie, userLocalStream);
if (draft[res.lineId]) {
draft[res.lineId].push({ userId: res.userWhoCalled, peer: peerForMeAndNewbie });
} else {
draft[res.lineId] = [{ userId: res.userWhoCalled, peer: peerForMeAndNewbie }];
}
});
peerForMeAndNewbie.on('signal', (signal) => { peerForMeAndNewbie.on('signal', (signal) => {
console.log('sending an answer to the slave', res); console.log('sending an answer to the slave', res);
@@ -102,7 +96,7 @@ export function StreamProvider({ children }: { children: React.ReactChild }) {
(currPeerRelationship) => currPeerRelationship.userId === res.masterUserId, (currPeerRelationship) => currPeerRelationship.userId === res.masterUserId,
); );
localPeerForMasterAndMe.peer.signal(res.simplePeerSignal); if (localPeerForMasterAndMe) localPeerForMasterAndMe.peer.signal(res.simplePeerSignal);
}); });
}); });
@@ -139,6 +133,14 @@ export function StreamProvider({ children }: { children: React.ReactChild }) {
const handleAddPeer = useCallback( const handleAddPeer = useCallback(
(lineId: string, userId: string, peerObj: Peer, mediaStream?: MediaStream) => { (lineId: string, userId: string, peerObj: Peer, mediaStream?: MediaStream) => {
updatePeerMap((draft) => { updatePeerMap((draft) => {
// for trickling, if we already have a peer for this line and user, then just replace
const existingUserLinePeerRelation = draft[lineId]?.find(
(currPeerRelation) => currPeerRelation.userId === userId,
);
if (existingUserLinePeerRelation) {
return draft;
}
if (draft[lineId]) { if (draft[lineId]) {
draft[lineId].push({ userId, peer: peerObj, mediaStream }); draft[lineId].push({ userId, peer: peerObj, mediaStream });
} else { } else {
@@ -223,7 +225,7 @@ function LineConnector({
const localPeerConnection = new Peer({ const localPeerConnection = new Peer({
initiator: true, initiator: true,
stream: localMediaStream, stream: localMediaStream,
trickle: false, // prevents the multiple tries on different ice servers and signal from getting called a bunch of times, trickle: true, // prevents the multiple tries on different ice servers and signal from getting called a bunch of times,
config: { config: {
iceServers: [ iceServers: [
{ urls: 'stun:stun.l.google.com:19302' }, { urls: 'stun:stun.l.google.com:19302' },
@@ -238,6 +240,8 @@ function LineConnector({
}); });
localPeerConnection.on('signal', (signal) => { localPeerConnection.on('signal', (signal) => {
console.log('have a signal to make call to someone ');
$ws.emit( $ws.emit(
ServerRequestChannels.RTC_CALL_SOMEONE_FOR_LINE, ServerRequestChannels.RTC_CALL_SOMEONE_FOR_LINE,
new RtcCallRequest(memberId, lineId, signal), new RtcCallRequest(memberId, lineId, signal),