-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
166 lines (135 loc) · 4.64 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
let localStream;
let remoteStream;
const localVideo = document.getElementById('local-video');
const remoteVideo = document.getElementById('remote-video');
let peerConnection;
const servers = {
iceServers: [
{
urls: [
'stun:stun.l.google.com:19302',
'stun:stun1.l.google.com:19302',
'stun:stun2.l.google.com:19302',
'stun:stun3.l.google.com:19302',
'stun:stun4.l.google.com:19302',
],
},
],
};
let socket;
const room = 999;
const uid = 'user1';
let init = async () => {
// Get the local stream and set it as the video source
localStream = await navigator.mediaDevices.getDisplayMedia();
localVideo.srcObject = localStream;
// initialize socket.io
socket = io('https://모도코.com/socket/room/');
socket.on('connect', () => {
console.log('[SOCKET] socket connected');
// join random room
socket.emit('joinRoom', { room, uid });
});
// on joinRoom event
socket.on('joinedRoom', (room) => {
console.log('[SOCKET] join room', room, 'success');
// on newUser joinedRoom event
socket.on('newUser', onNewUserJoinedRoom);
// listen to call-user event's offer
// which is call-made event
socket.on('call-made', onCallMade); // end of call-made event listener
// listen to answer-made event
socket.on('answer-made', onAnswerMade);
// listen to ice-candidate event
// which is ice-candidate of other user
socket.on('ice-candidate', onIceCandidateRecieved);
});
};
// create peer connection
// used both for offer and answer
let createPeerConnection = async (sid) => {
peerConnection = new RTCPeerConnection(servers);
// setup remote video
remoteStream = new MediaStream();
remoteVideo.srcObject = remoteStream;
// setup local video if not ready
if (!localStream) {
console.log('[RTC] local stream not ready, wait for it...');
localStream = await navigator.mediaDevices.getDisplayMedia();
localVideo.srcObject = localStream;
}
// setup local track to send
localStream.getTracks().forEach((track) => {
peerConnection.addTrack(track, localStream);
});
// on remote track added to peer connection
// = anytime a remote track is added to the peer connection
// add it to the remote video track
peerConnection.ontrack = (event) => {
event.streams[0].getTracks().forEach((track) => {
remoteStream.addTrack(track);
});
};
// trickle ice candidates
// onicecandidate event is fired whenever a candidate is found
peerConnection.onicecandidate = async (event) => {
if (event.candidate) {
console.log('[RTC] New ice candiate created :', event.candidate);
console.log('[SOCKET] Trickle ice (send ice candidate)');
// send ice candidate to other user
socket.emit('ice-candidate', {
sid: sid,
candidate: event.candidate,
});
}
};
};
let createOffer = async (sid) => {
await createPeerConnection(sid);
// create offer
const offer = await peerConnection.createOffer();
await peerConnection.setLocalDescription(offer); // triggers ice gathering
// send offer to new user
console.log(`[SOCKET] call user(${sid}) with offer`);
socket.emit('call-user', { sid: sid, offer });
};
// create answer to offer
let createAnswer = async (sid, offer) => {
await createPeerConnection(sid);
await peerConnection.setRemoteDescription(offer);
// create answer
const answer = await peerConnection.createAnswer();
await peerConnection.setLocalDescription(answer); // peer 2 case
// send answer to other user
console.log(`[SOCKET] answer to user(${sid})`);
socket.emit('make-answer', { sid: sid, answer });
};
let onNewUserJoinedRoom = async (user) => {
console.log(`[SOCKET:on"newUser"] new user(${user.sid}) joined room`);
await createOffer(user.sid);
};
// handle offer from new user
let onCallMade = async (data) => {
console.log(`[SOCKET:on"call-made"] received offer from other user(${data.sid})`);
// create answer and send it to other user
await createAnswer(data.sid, data.offer);
};
// handle answer made from other user
let onAnswerMade = async (data) => {
console.log(`[SOCKET:on"answer-made"] received answer from other user(${data.sid})`);
if (!peerConnection.currentRemoteDescription) {
peerConnection.setRemoteDescription(data.answer);
}
};
// handle ice-candidate from other user
let onIceCandidateRecieved = (data) => {
console.log(
`[SOCKET:on"ice-candidate"] received ice-candidate from other user(${data.sid}), candidate: ${data.candidate}`
);
// add ice candidate to peer connection
if (peerConnection) {
console.log('Debug', peerConnection);
peerConnection.addIceCandidate(data.candidate);
}
};
init();