setting up back and forth calling as before
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
import {
|
import {
|
||||||
ConnectToLineRequest,
|
ConnectToLineRequest,
|
||||||
RtcReceiveSignalResponse,
|
RtcAnswerSomeoneRequest,
|
||||||
RtcSendSignalRequest,
|
RtcCallRequest,
|
||||||
|
RtcNewUserJoinedResponse,
|
||||||
|
RtcReceiveAnswerResponse,
|
||||||
ServerRequestChannels,
|
ServerRequestChannels,
|
||||||
ServerResponseChannels,
|
ServerResponseChannels,
|
||||||
SomeoneConnectedResponse,
|
SomeoneConnectedResponse,
|
||||||
@@ -151,16 +153,27 @@ export default function InitializeWs(io: any) {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// tell the proper other user to create a local peer object for the one on one mesh connection
|
socket.on(ServerRequestChannels.RTC_CALL_SOMEONE_FOR_LINE, (req: RtcCallRequest) => {
|
||||||
socket.on(ServerRequestChannels.RTC_SEND_SIGNAL, (req: RtcSendSignalRequest) => {
|
|
||||||
const userSocketId = userIdsToSocketIds[req.userToCall];
|
const userSocketId = userIdsToSocketIds[req.userToCall];
|
||||||
|
|
||||||
io.to(userSocketId).emit(
|
io.to(userSocketId).emit(
|
||||||
ServerResponseChannels.RTC_RECEIVING_SIGNAL,
|
ServerResponseChannels.RTC_NEW_USER_JOINED,
|
||||||
new RtcReceiveSignalResponse(userInfo.userId, req.simplePeerSignal),
|
new RtcNewUserJoinedResponse(userInfo.userId, req.lineId, req.simplePeerSignal),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on(
|
||||||
|
ServerRequestChannels.RTC_ANSWER_SOMEONE_FOR_LINE,
|
||||||
|
(req: RtcAnswerSomeoneRequest) => {
|
||||||
|
const userSocketId = userIdsToSocketIds[req.newbieUserId];
|
||||||
|
|
||||||
|
io.to(userSocketId).emit(
|
||||||
|
ServerResponseChannels.RTC_NEW_USER_JOINED,
|
||||||
|
new RtcReceiveAnswerResponse(userInfo.userId, req.lineId, req.simplePeerSignal),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// tell everyone in the channel to
|
// tell everyone in the channel to
|
||||||
// socket.on(ServerRequestChannels.CREATED_CHANNEL, (req: CreatedLineRequest) => {
|
// socket.on(ServerRequestChannels.CREATED_CHANNEL, (req: CreatedLineRequest) => {
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,8 @@ export enum ServerRequestChannels {
|
|||||||
BROADCAST_TO_LINE = 'BROADCAST_TO_LINE',
|
BROADCAST_TO_LINE = 'BROADCAST_TO_LINE',
|
||||||
STOP_BROADCAST_TO_LINE = 'STOP_BROADCAST_TO_LINE',
|
STOP_BROADCAST_TO_LINE = 'STOP_BROADCAST_TO_LINE',
|
||||||
|
|
||||||
RTC_SEND_SIGNAL = 'RTC_SEND_SIGNAL',
|
RTC_CALL_SOMEONE_FOR_LINE = 'RTC_CALL_SOMEONE_FOR_LINE',
|
||||||
|
RTC_ANSWER_SOMEONE_FOR_LINE = 'RTC_ANSWER_SOMEONE_FOR_LINE',
|
||||||
|
|
||||||
GOING_INTO_FLOW_STATE = 'GOING_INTO_FLOW_STATE',
|
GOING_INTO_FLOW_STATE = 'GOING_INTO_FLOW_STATE',
|
||||||
}
|
}
|
||||||
@@ -57,8 +58,8 @@ export enum ServerResponseChannels {
|
|||||||
SOMEONE_STARTED_BROADCASTING = 'SOMEONE_STARTED_BROADCASTING', //show their stream tracks
|
SOMEONE_STARTED_BROADCASTING = 'SOMEONE_STARTED_BROADCASTING', //show their stream tracks
|
||||||
SOMEONE_STOPPED_BROADCASTING = 'SOMEONE_STOPPED_BROADCASTING', // stop showing their stream tracks
|
SOMEONE_STOPPED_BROADCASTING = 'SOMEONE_STOPPED_BROADCASTING', // stop showing their stream tracks
|
||||||
|
|
||||||
// sending to the correct room of tunedin folks AND also making sure it's the right event handler in the right handler for this component
|
RTC_NEW_USER_JOINED = 'RTC_NEW_USER_JOINED',
|
||||||
RTC_RECEIVING_SIGNAL = 'RTC_RECEIVING_SIGNAL',
|
RTC_RECEIVING_MASTER_ANSWER = 'RTC_RECEIVING_MASTER_ANSWER',
|
||||||
|
|
||||||
SOMEONE_GOING_INTO_FLOW_STATE = 'SOMEONE_GOING_INTO_FLOW_STATE',
|
SOMEONE_GOING_INTO_FLOW_STATE = 'SOMEONE_GOING_INTO_FLOW_STATE',
|
||||||
}
|
}
|
||||||
@@ -107,12 +108,20 @@ export class SocketEmitter<T> {
|
|||||||
constructor(public channel: SocketChannels, data: T) {}
|
constructor(public channel: SocketChannels, data: T) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class RtcSendSignalRequest {
|
export class RtcCallRequest {
|
||||||
constructor(public userToCall: string, public simplePeerSignal: any) {}
|
constructor(public userToCall: string, public lineId: string, public simplePeerSignal: any) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class RtcReceiveSignalResponse {
|
export class RtcNewUserJoinedResponse {
|
||||||
constructor(public senderUserId: string, public simplePeerSignal: any) {}
|
constructor(public userWhoCalled: string, public lineId: string, public simplePeerSignal: any) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class RtcAnswerSomeoneRequest {
|
||||||
|
constructor(public newbieUserId: string, public lineId: string, public simplePeerSignal: any) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class RtcReceiveAnswerResponse {
|
||||||
|
constructor(public masterUserId: string, public lineId: string, public simplePeerSignal: any) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FlowStateRequest {
|
export class FlowStateRequest {
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ import useAuth from './AuthProvider';
|
|||||||
import { useImmer } from 'use-immer';
|
import { useImmer } from 'use-immer';
|
||||||
import useSockets from './SocketProvider';
|
import useSockets from './SocketProvider';
|
||||||
import {
|
import {
|
||||||
RtcReceiveSignalResponse,
|
RtcAnswerSomeoneRequest,
|
||||||
RtcSendSignalRequest,
|
RtcCallRequest,
|
||||||
|
RtcNewUserJoinedResponse,
|
||||||
|
RtcReceiveAnswerResponse,
|
||||||
ServerRequestChannels,
|
ServerRequestChannels,
|
||||||
ServerResponseChannels,
|
ServerResponseChannels,
|
||||||
} from '@nirvana/core/sockets/channels';
|
} from '@nirvana/core/sockets/channels';
|
||||||
@@ -15,7 +17,7 @@ import MasterLineData from '@nirvana/core/models/masterLineData.model';
|
|||||||
import { useEffectOnce } from 'react-use';
|
import { useEffectOnce } from 'react-use';
|
||||||
|
|
||||||
type LinePeerMap = {
|
type LinePeerMap = {
|
||||||
[lineId: string]: { userId: string; peer: Peer }[];
|
[lineId: string]: { userId: string; peer: Peer; mediaStream?: MediaStream }[];
|
||||||
};
|
};
|
||||||
interface IStreamProvider {
|
interface IStreamProvider {
|
||||||
peerMap: LinePeerMap;
|
peerMap: LinePeerMap;
|
||||||
@@ -36,6 +38,60 @@ export function StreamProvider({ children }: { children: React.ReactChild }) {
|
|||||||
|
|
||||||
const [userLocalStream, setUserLocalStream] = useState<MediaStream>();
|
const [userLocalStream, setUserLocalStream] = useState<MediaStream>();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
$ws.on(ServerResponseChannels.RTC_NEW_USER_JOINED, (res: RtcNewUserJoinedResponse) => {
|
||||||
|
toast.success('NEWBIE JOINED!!!');
|
||||||
|
|
||||||
|
const peerForMeAndNewbie = new Peer({
|
||||||
|
initiator: false,
|
||||||
|
trickle: false, // prevents the multiple tries on different ice servers and signal from getting called a bunch of times
|
||||||
|
config: {
|
||||||
|
iceServers: [
|
||||||
|
{ urls: 'stun:stun.l.google.com:19302' },
|
||||||
|
{ urls: 'stun:global.stun.twilio.com:3478?transport=udp' },
|
||||||
|
{
|
||||||
|
url: 'turn:numb.viagenie.ca',
|
||||||
|
credential: 'muazkh',
|
||||||
|
username: '[email protected]',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
peerForMeAndNewbie.signal(res.simplePeerSignal);
|
||||||
|
|
||||||
|
updatePeerMap((draft) => {
|
||||||
|
draft[res.lineId].push({ userId: res.userWhoCalled, peer: peerForMeAndNewbie });
|
||||||
|
});
|
||||||
|
|
||||||
|
peerForMeAndNewbie.on('signal', (signal) => {
|
||||||
|
$ws.emit(
|
||||||
|
ServerRequestChannels.RTC_ANSWER_SOMEONE_FOR_LINE,
|
||||||
|
new RtcAnswerSomeoneRequest(res.userWhoCalled, res.lineId, signal),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$ws.on(ServerResponseChannels.RTC_RECEIVING_MASTER_ANSWER, (res: RtcReceiveAnswerResponse) => {
|
||||||
|
toast.success('MASTER gave me an answer!!!');
|
||||||
|
// find this person in peer map
|
||||||
|
updatePeerMap((draft) => {
|
||||||
|
const localPeerForMasterAndMe = draft[res.lineId].find(
|
||||||
|
(currPeerRelationship) => currPeerRelationship.userId === res.masterUserId,
|
||||||
|
);
|
||||||
|
|
||||||
|
localPeerForMasterAndMe.peer.signal(res.simplePeerSignal);
|
||||||
|
|
||||||
|
return;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
$ws.removeAllListeners(ServerResponseChannels.RTC_NEW_USER_JOINED);
|
||||||
|
$ws.removeAllListeners(ServerResponseChannels.RTC_RECEIVING_MASTER_ANSWER);
|
||||||
|
};
|
||||||
|
}, [updatePeerMap]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
navigator.mediaDevices.enumerateDevices().then((devices) => {
|
navigator.mediaDevices.enumerateDevices().then((devices) => {
|
||||||
const uniqueDevices = [];
|
const uniqueDevices = [];
|
||||||
@@ -61,9 +117,13 @@ export function StreamProvider({ children }: { children: React.ReactChild }) {
|
|||||||
console.log(`peer map: `, peerMap);
|
console.log(`peer map: `, peerMap);
|
||||||
|
|
||||||
const handleAddPeer = useCallback(
|
const handleAddPeer = useCallback(
|
||||||
(userId: string, peerObj: Peer) => {
|
(lineId: string, userId: string, peerObj: Peer, mediaStream?: MediaStream) => {
|
||||||
updatePeerMap((draft) => {
|
updatePeerMap((draft) => {
|
||||||
draft[userId] = peerObj;
|
if (draft[lineId]) {
|
||||||
|
draft[lineId].push({ userId, peer: peerObj, mediaStream });
|
||||||
|
} else {
|
||||||
|
draft[lineId] = [{ userId, peer: peerObj, mediaStream }];
|
||||||
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[updatePeerMap],
|
[updatePeerMap],
|
||||||
@@ -77,6 +137,7 @@ export function StreamProvider({ children }: { children: React.ReactChild }) {
|
|||||||
return (
|
return (
|
||||||
<MemoLineConnector
|
<MemoLineConnector
|
||||||
key={`streamConnector-${line.lineDetails._id.toString()}`}
|
key={`streamConnector-${line.lineDetails._id.toString()}`}
|
||||||
|
lineId={line.lineDetails._id.toString()}
|
||||||
handleAddPeer={handleAddPeer}
|
handleAddPeer={handleAddPeer}
|
||||||
membersToCall={line.tunedInMemberIds.filter(
|
membersToCall={line.tunedInMemberIds.filter(
|
||||||
(currMemberId) => currMemberId !== user._id.toString(),
|
(currMemberId) => currMemberId !== user._id.toString(),
|
||||||
@@ -96,13 +157,18 @@ export default function useStreams() {
|
|||||||
|
|
||||||
const MemoLineConnector = React.memo(LineConnector);
|
const MemoLineConnector = React.memo(LineConnector);
|
||||||
|
|
||||||
|
// handle managing stream connections for one line
|
||||||
function LineConnector({
|
function LineConnector({
|
||||||
|
lineId,
|
||||||
membersToCall,
|
membersToCall,
|
||||||
handleAddPeer,
|
handleAddPeer,
|
||||||
}: {
|
}: {
|
||||||
|
lineId: string;
|
||||||
membersToCall: string[];
|
membersToCall: string[];
|
||||||
handleAddPeer: (userId: string, peerObj: Peer) => void;
|
handleAddPeer: (lineId: string, userId: string, peerObj: Peer, mediaStream?: MediaStream) => void;
|
||||||
}) {
|
}) {
|
||||||
|
const { $ws } = useSockets();
|
||||||
|
|
||||||
console.log('rendering this piece of shit');
|
console.log('rendering this piece of shit');
|
||||||
|
|
||||||
useEffectOnce(() => {
|
useEffectOnce(() => {
|
||||||
@@ -114,49 +180,51 @@ function LineConnector({
|
|||||||
// also so that I can signal for the peer object relationship between me and this other person for this particular channel
|
// also so that I can signal for the peer object relationship between me and this other person for this particular channel
|
||||||
|
|
||||||
// then add in the stream to this local peer relationship object
|
// then add in the stream to this local peer relationship object
|
||||||
});
|
|
||||||
|
|
||||||
return <></>;
|
|
||||||
}
|
|
||||||
|
|
||||||
function StreamConnector({
|
|
||||||
peerUserId,
|
|
||||||
handleAddPeer,
|
|
||||||
}: {
|
|
||||||
peerUserId: string;
|
|
||||||
handleAddPeer: (userId: string, peerObj: Peer) => void;
|
|
||||||
}) {
|
|
||||||
const { $ws } = useSockets();
|
|
||||||
|
|
||||||
// call all of the people on the initial load of this
|
|
||||||
useEffect(() => {
|
|
||||||
console.log('calling all of the initials until this component unmounts');
|
|
||||||
|
|
||||||
|
// todo get the user media selections
|
||||||
navigator.mediaDevices
|
navigator.mediaDevices
|
||||||
.getUserMedia({ video: false, audio: true })
|
.getUserMedia({ video: true, audio: true })
|
||||||
.then((localMediaStream: MediaStream) => {
|
.then((localMediaStream: MediaStream) => {
|
||||||
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: false, // prevents the multiple tries on different ice servers and signal from getting called a bunch of times,
|
||||||
|
config: {
|
||||||
|
iceServers: [
|
||||||
|
{ urls: 'stun:stun.l.google.com:19302' },
|
||||||
|
{ urls: 'stun:global.stun.twilio.com:3478?transport=udp' },
|
||||||
|
{
|
||||||
|
url: 'turn:numb.viagenie.ca',
|
||||||
|
credential: 'muazkh',
|
||||||
|
username: '[email protected]',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
localPeerConnection.on('signal', (signal) => {
|
// todo check if already in peer map? keeping it simple for now
|
||||||
console.log('going to call someone');
|
// a peer relationship between me and someone for this particular channel so that I can just enable or disable this particular stream
|
||||||
$ws.emit(
|
// object instead of managing different ones
|
||||||
ServerRequestChannels.RTC_SEND_SIGNAL,
|
|
||||||
new RtcSendSignalRequest(peerUserId, signal),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
// sending back the connection to the parent for everyone
|
// bandwidth wise, would be uploading stream to one room at a time but downloading a x b streams but someone can't
|
||||||
handleAddPeer(peerUserId, localPeerConnection);
|
// stream in two at same time anyway
|
||||||
|
|
||||||
|
toast.success('CALLING bunch of people!!!');
|
||||||
|
|
||||||
|
membersToCall.map((memberId) => {
|
||||||
|
localPeerConnection.on('signal', (signal) => {
|
||||||
|
$ws.emit(
|
||||||
|
ServerRequestChannels.RTC_CALL_SOMEONE_FOR_LINE,
|
||||||
|
new RtcCallRequest(memberId, lineId, signal),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// sending back the connection to the parent
|
||||||
|
// so that we can accept the answer later on
|
||||||
|
handleAddPeer(lineId, memberId, localPeerConnection, localMediaStream);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
() => {
|
|
||||||
// destroy this peer and remove from the parent controller? or happens when someone else leaves the tuned list?
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return <></>;
|
return <></>;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user