-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanswer.js
127 lines (126 loc) · 3.81 KB
/
answer.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
//accessing video elms
const localVideo = document.querySelectorAll("video")[0]
const remoteVideo = document.querySelectorAll("video")[1]
//senders
var senderVideo, senderMic;
//creating streams var
var localStream = "", remoteStream = "";
//listening for window to load
window.addEventListener("load", () => {
//creating streams
localStream = new MediaStream(), remoteStream = new MediaStream();
//setting streams to video elms
localVideo.srcObject = localStream
remoteVideo.srcObject = remoteStream
})
//creating easeRTC obj
const RTC = new EaseRTC()
//listening for ice
RTC.onIce = (e) => {
localStorage.setItem("answerIce", e)
}
//listening to recieve remote tracks
RTC.onTrack = (e) => {
//removing old useless Tracks
const muted = remoteStream.getTracks();
muted.forEach(track => {
if (track.muted) {
remoteStream.removeTrack(track)
}
});
//adding current track
remoteStream.addTrack(e.track);
remoteVideo.play();
}
//listening for SDP and ICE
setInterval(() => {
const SDP = localStorage.getItem("offerSDP");
if (SDP) {
//setting offer and creating answer
RTC.createAnswer(SDP).then(e => {
localStorage.setItem("answerSDP", e)
});
localStorage.removeItem("offerSDP");
}
const ICE = localStorage.getItem("offerIce");
if (ICE) {
//setting ICE
RTC.addIce(ICE)
localStorage.removeItem("offerIce");
}
}, 1000);
//mic on/off and cam on/off part
const mic = document.querySelectorAll("span")[0]
const micOff = document.querySelectorAll("span")[1]
const cam = document.querySelectorAll("span")[2]
const camOff = document.querySelectorAll("span")[3]
micOff.addEventListener("click", () => {
micOff.style.display = "none"
mic.style.display = "block"
RTC.getMedia(true, false).then(str => {
if (senderMic) {
RTC.pc.removeTrack(senderMic)
}
RTC.addTrack(str.getTracks()[0]).then(e => {
senderMic = e;
});
RTC.reconnectSession()
const muted = localStream.getTracks();
muted.forEach(track => {
if (track.readyState == "ended") {
localStream.removeTrack(track)
}
});
localStream.addTrack(str.getTracks()[0])
localVideo.play()
})
})
mic.addEventListener("click", () => {
micOff.style.display = "block"
mic.style.display = "none"
const muted = localStream.getTracks();
muted.forEach(track => {
if (track.muted) {
localStream.removeTrack(track)
}
});
localStream.getAudioTracks().forEach(track => {
RTC.pc.removeTrack(senderMic)
track.stop()
});
})
camOff.addEventListener("click", () => {
camOff.style.display = "none"
cam.style.display ="block"
RTC.getMedia(false, true).then(str => {
if (senderVideo) {
RTC.pc.removeTrack(senderVideo)
}
RTC.addTrack(str.getTracks()[0]).then(e => {
senderVideo = e;
});;
RTC.reconnectSession()
const muted = localStream.getTracks();
muted.forEach(track => {
if (track.readyState == "ended") {
localStream.removeTrack(track)
}
});
localStream.addTrack(str.getTracks()[0])
localVideo.play()
})
})
cam.addEventListener("click", () => {
camOff.style.display = "block"
cam.style.display = "none"
const muted = localStream.getTracks();
muted.forEach(track => {
if (track.muted) {
localStream.removeTrack(track)
}
});
localStream.getVideoTracks().forEach(track => {
RTC.pc.removeTrack(senderVideo)
track.stop()
});
})