-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
170 lines (130 loc) · 4.82 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
167
168
169
170
let APP_ID="cb4017d3746b4b3bb39c20793b351be6";
let token = null;
let uid = String(Math.floor(Math.random()*10000));
let client;
let channel;
let queryString = window.location.search
let urlParams = new URLSearchParams(queryString)
let roomID = urlParams.get('room')
if (!roomID){
window.location = 'lobby.html'
}
let localStream;
let remoteStream;
let peerConnection;
const servers = {
iceServers: [{
urls: ['stun:stun1.1.google.com:19302', 'stun:stun2.1.google.com:19302']
}]
}
let constraints = {
video:{
width:{min:640, ideal:1920, max:1920},
height:{min:480, ideal:1080, max:1080}
},
audio:false
}
let init = async () => {
client = await AgoraRTM.createInstance(APP_ID)
await client.login({uid, token})
channel = client.createChannel(roomID)
await channel.join()
channel.on('MemberJoined', handleUserJoined)
channel.on('MemberLeft', handleUserLeft)
client.on('MessageFromPeer', handleMessageFromPeer)
localStream = await navigator.mediaDevices.getUserMedia(constraints)
document.getElementById('user-1').srcObject = localStream
}
let handleUserLeft = (MemberId)=>{
console.log("Userrrr leffttt", MemberId)
document.getElementById('user-2').style.display='none'
document.getElementById('user-1').classList.remove('smallFrame')
}
let handleUserJoined = async (MemberId) =>{
console.log('New user joined : ', MemberId)
createOffer(MemberId)
}
let handleMessageFromPeer = async (message, MemberId)=>{
message = JSON.parse(message.text)
if(message.type ==='offer'){
createAnswer(MemberId, message.offer)
}
if(message.type ==='answer'){
addAnswer(message.answer)
}
if(message.type ==='candidate'){
if (peerConnection){
peerConnection.addIceCandidate(message.candidate)
}
}
}
let createPeerConnection = async (MemberId)=>{
peerConnection = new RTCPeerConnection(servers)
remoteStream = new MediaStream()
document.getElementById('user-2').srcObject = remoteStream
document.getElementById('user-2').style.display = 'block'
document.getElementById('user-1').classList.add('smallFrame')
if(!localStream){
localStream = await navigator.mediaDevices.getUserMedia(constraints)
document.getElementById('user-1').srcObject = localStream
}
localStream.getTracks().forEach((track) => {
peerConnection.addTrack(track, localStream)
})
peerConnection.ontrack = (event) => {
event.streams[0].getTracks().forEach((track) => {
remoteStream.addTrack(track)
})
}
peerConnection.onicecandidate = async (event) => {
if (event.candidate) {
client.sendMessageToPeer({text:JSON.stringify({'type':'candidate', 'candidate':event.candidate})}, MemberId)
}
}
}
let createOffer = async (MemberId) => {
await createPeerConnection(MemberId)
let offer = await peerConnection.createOffer()
await peerConnection.setLocalDescription(offer)
client.sendMessageToPeer({text:JSON.stringify({'type':'offer', 'offer':offer})}, MemberId)
}
let createAnswer = async (MemberId, offer)=>{
await createPeerConnection(MemberId)
await peerConnection.setRemoteDescription(offer)
let answer = await peerConnection.createAnswer()
await peerConnection.setLocalDescription(answer)
client.sendMessageToPeer({text:JSON.stringify({'type':'answer', 'answer':answer})}, MemberId)
}
let addAnswer = async (answer)=>{
if(!peerConnection.currentRemoteDescription){
peerConnection.setRemoteDescription(answer)
}
}
let leaveChannel = async () =>{
await channel.leave()
await client.logout()
}
let toggleCamera = async ()=>{
let videoTrack = localStream.getTracks().find(track => track.kind ==='video')
if(videoTrack.enabled){
videoTrack.enabled = false;
document.getElementById('camera-btn').style.backgroundColor = 'rgb(255,80,80)'
}else{
videoTrack.enabled = true;
document.getElementById('camera-btn').style.backgroundColor = 'rgb(179,102,249,.9)'
}
}
let toggleMic = async ()=>{
let audioTrack = localStream.getTracks().find(track => track.kind ==='audio')
if(audioTrack.enabled){
audioTrack.enabled = false;
document.getElementById('mic-btn').style.backgroundColor = 'rgb(255,80,80)'
}else{
audioTrack.enabled = true;
document.getElementById('mic-btn').style.backgroundColor = 'rgb(179,102,249,.9)'
}
}
document.getElementById('camera-btn').addEventListener('click', toggleCamera)
document.getElementById('mic-btn').addEventListener('click', toggleMic)
window.addEventListener('beforeunload',leaveChannel)
init()