-
Notifications
You must be signed in to change notification settings - Fork 2
/
Video.js
103 lines (81 loc) · 3.38 KB
/
Video.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
const APP_ID = "3024a26123dc405b9622dac34df6d37f"
const TOKEN = "007eJxTYEh7fSC6wD5Rttz+x+7Et2dvuy3VPBb8zGHvhDaTHmm9sLsKDMYGRiaJRmaGRsYpySYGpkmWZkZGKYnJxiYpaWYpxuZpBosnJjcEMjIsFKhmZGSAQBCfhSE3MTOPgQEAbh4fYA=="
const CHANNEL = "main"
const client = AgoraRTC.createClient({ mode: 'rtc', codec: 'vp8' })
let localTracks = []
let remoteUsers = {}
let joinAndDisplayLocalStream = async () => {
client.on('user-published', handleUserJoined)
client.on('user-left', handleUserLeft)
let UID = await client.join(APP_ID, CHANNEL, TOKEN, null)
localTracks = await AgoraRTC.createMicrophoneAndCameraTracks()
let player = `<div class="video-container" id="user-container-${UID}">
<div class="video-player" id="user-${UID}"></div>
</div>`
document.getElementById('video-streams').insertAdjacentHTML('beforeend', player)
localTracks[1].play(`user-${UID}`)
await client.publish([localTracks[0], localTracks[1]])
}
let joinStream = async () => {
await joinAndDisplayLocalStream()
document.getElementById('join-btn').style.display = 'none'
document.getElementById('stream-controls').style.display = 'flex'
}
let handleUserJoined = async (user, mediaType) => {
remoteUsers[user.uid] = user
await client.subscribe(user, mediaType)
if (mediaType === 'video') {
let player = document.getElementById(`user-container-${user.uid}`)
if (player != null) {
player.remove()
}
player = `<div class="video-container" id="user-container-${user.uid}">
<div class="video-player" id="user-${user.uid}"></div>
</div>`
document.getElementById('video-streams').insertAdjacentHTML('beforeend', player)
user.videoTrack.play(`user-${user.uid}`)
}
if (mediaType === 'audio') {
user.audioTrack.play()
}
}
let handleUserLeft = async (user) => {
delete remoteUsers[user.uid]
document.getElementById(`user-container-${user.uid}`).remove()
}
let leaveAndRemoveLocalStream = async () => {
for (let i = 0; localTracks.length > i; i++) {
localTracks[i].stop()
localTracks[i].close()
}
await client.leave()
document.getElementById('join-btn').style.display = 'block'
document.getElementById('stream-controls').style.display = 'none'
document.getElementById('video-streams').innerHTML = ''
}
let toggleMic = async (e) => {
if (localTracks[0].muted) {
await localTracks[0].setMuted(false)
e.target.innerText = 'Mic on'
e.target.style.backgroundColor = 'cadetblue'
} else {
await localTracks[0].setMuted(true)
e.target.innerText = 'Mic off'
e.target.style.backgroundColor = '#EE4B2B'
}
}
let toggleCamera = async (e) => {
if (localTracks[1].muted) {
await localTracks[1].setMuted(false)
e.target.innerText = 'Camera on'
e.target.style.backgroundColor = 'cadetblue'
} else {
await localTracks[1].setMuted(true)
e.target.innerText = 'Camera off'
e.target.style.backgroundColor = '#EE4B2B'
}
}
document.getElementById('join-btn').addEventListener('click', joinStream)
document.getElementById('leave-btn').addEventListener('click', leaveAndRemoveLocalStream)
document.getElementById('mic-btn').addEventListener('click', toggleMic)
document.getElementById('camera-btn').addEventListener('click', toggleCamera)