-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsender.js
More file actions
109 lines (91 loc) · 2.45 KB
/
sender.js
File metadata and controls
109 lines (91 loc) · 2.45 KB
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
// JavaScript source code
const webSocket = new WebSocket("ws://192.168.43.145:3000")
webSocket.onmessage = (event) => {
handleSignallingData(JSON.parse(event.data))
}
function handleSignallingData(data) {
switch (data.type) {
case "answer":
peerConn.setRemoteDescription(data.answer)
break
case "candidate":
peerConn.addIceCandidate(data.candidate)
}
}
let username
function sendUsername() {
username = document.getElementById("username-input").value
sendData({
type: "store_user"
})
}
function sendData(data) {
data.username = username
webSocket.send(JSON.stringify(data))
}
let localStream
let peerConn
function startCall() {
document.getElementById("video-call-div")
.style.display = "inline"
navigator.getUserMedia({
video: {
frameRate: 24,
width: {
min: 480, ideal: 720, max: 1280
},
aspectRatio: 1.33333
},
audio: true
}, (stream) => {
localStream = stream
document.getElementById("local-video").srcObject = localStream
let configuration = {
iceServers: [
{
"urls": ["stun:stun.l.google.com:19302",
"stun:stun1.l.google.com:19302",
"stun:stun2.l.google.com:19302"]
}
]
}
peerConn = new RTCPeerConnection(configuration)
peerConn.addStream(localStream)
peerConn.onaddstream = (e) => {
document.getElementById("remote-video")
.srcObject = e.stream
}
peerConn.onicecandidate = ((e) => {
if (e.candidate == null)
return
sendData({
type: "store_candidate",
candidate: e.candidate
})
})
createAndSendOffer()
}, (error) => {
console.log(error)
})
}
function createAndSendOffer() {
peerConn.createOffer((offer) => {
sendData({
type: "store_offer",
offer: offer
})
peerConn.setLocalDescription(offer)
}, (error) => {
console.log(error)
})
}
let isAudio = true
function muteAudio() {
isAudio = !isAudio
localStream.getAudioTracks()[0].enabled = isAudio
}
let isVideo = true
function muteVideo() {
isVideo = !isVideo
localStream.getVideoTracks()[0].enabled = isVideo
}