-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
183 lines (131 loc) · 4.69 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
171
172
173
174
175
176
177
178
179
180
181
182
183
let APP_ID = "d7994b9b37e44c7daa9171457a12dc0e"
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.l.google.com:19302', 'stun:stun2.l.google.com:19302']
}
]
}
let constraints = {
video:{
width:{min:640, ideal:1920, max:1920},
height:{min:480, ideal:1080, max:1080},
},
audio:true
}
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) => {
document.getElementById('user-2').style.display = 'none'
document.getElementById('user-1').classList.remove('smallFrame')
}
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 handleUserJoined = async (MemberId) => {
console.log('A new user joined the channel:', MemberId)
createOffer(MemberId)
}
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({video:true, audio:false})
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)'
}
}
window.addEventListener('beforeunload', leaveChannel)
document.getElementById('camera-btn').addEventListener('click', toggleCamera)
document.getElementById('mic-btn').addEventListener('click', toggleMic)
init()