-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
189 lines (139 loc) · 5.77 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
184
185
186
187
188
189
let App_ID = "useYourID"
let token = null;
let uid = String(Math.floor(Math.random() * 10000)) //User Id
let client; // Login and access to all funtion
let channel; // allows to send the messages
let queryString = window.location.search
let urlParams = new URLSearchParams(queryString)
let roomID = urlParams.get('room')
if(!roomID){
window.location = 'lobby.html'
}
let localStream; // local camera video and audio feed
let remoteStream;
let peerConnection // Our another camera window
// Creating stun server Generally not need locally but in Deployement
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})
// index.html?room=23456
// channel = client.createChannel(roomID)
channel = client.createChannel(roomID)
await channel.join() // log in with the client
channel.on('MemberJoined', handleUserJoined)
channel.on('MemberLeft', handleUserLeft)
client.on('MessageFromPeer', handleMessageFromPeer)
localStream = await navigator.mediaDevices.getUserMedia(constraints) // This will request the camera access
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)
// console.log('Message:', message)
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) //A WebRTC connection between the local computer and a remote peer. It provides methods to connect to a remote peer, maintain and monitor the connection, and close the connection once it's no longer needed.
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:true}) // This will request the camera access
document.getElementById('user-1').srcObject = localStream;
}
localStream.getTracks().forEach((track)=>{
peerConnection.addTrack(track, localStream)
})
// When Our Remote Peer Adds the track
peerConnection.ontrack = (event)=>{
event.streams[0].getTracks().forEach((track)=>{
remoteStream.addTrack(track)
})
}
peerConnection.onicecandidate = async(event)=>{ // From Created offer "setLocalDescription" Triggers the onicecandidate
if(event.candidate){
client.sendMessageToPeer({text: JSON.stringify({'type': 'candidate', 'candidate': event.candidate})}, MemberId)
}
}
}
// Creating an Offer
let createOffer = async(MemberId)=>{
await createPeerConnection(MemberId)
// Creating an Offer
let offer = await peerConnection.createOffer()
await peerConnection.setLocalDescription(offer)
console.log('Offer:', 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()