/
videotest.html
180 lines (171 loc) · 4.43 KB
/
videotest.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Let's do video baby!</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="shortcut icon" href="./favicon.ico" type="image/x-icon">
<link rel="icon" href="./favicon.ico" type="image/x-icon">
<script type="text/javascript" src="./node_modules/peerjs/dist/peerjs.js"></script>
<style>
* {
font-family: "Open Sans";
}
#myID.connected:before {
content: 'Your ID: ';
}
#container {
display: flex;
flex-direction: row;
}
.video-container {
width: 50%;
}
label {
display: block;
}
video {
width: 100%;
}
.myVideo {
position: absolute;
max-width: 275px;
width: 40%;
bottom: 5px;
right: 5px;
font-size: 8pt;
}
@media only screen and (max-width: 800px) {
#container {
flex-direction: column;
}
.video-container {
width: 100%;
height: 50%;
}
}
</style>
<script>
const peerOptions = {
config: {
iceServers: [
{ url: 'stun:stun.l.google.com:19302' },
{
url: 'turn:numb.viagenie.ca',
credential: 'muazkh',
username: 'webrtc@live.com'
}
]
}
};
if (localStorage.videoID == undefined) {
mypeerID = prompt("What is your ID?");
localStorage.videoID = mypeerID;
} else {
mypeerID = localStorage.videoID;
}
// console.log(mypeerID);
console.log("-- Connecting Peer with id ", mypeerID);
const peer = new Peer(mypeerID);
peer.on('open', (id) => {
console.log({on: "open"});
console.log({PeerID: id});
let myID = document.querySelector("#myID");
myID.classList.add("connected");
myID.innerText = id;
});
// peer.on('stream', stream => {
// console.log({on: "stream, inside peer"});
// document.querySelector("#yourCam2").srcObject = stream;
// });
const constraints = {
video: { facingMode: "user" },
audio: true
};
const addVideoStream = (peerID, stream, muted) => {
let video = document.querySelector("#" + peerID);
video.srcObject = stream;
if (muted == "mute") { video.muted = true; }
video.addEventListener('loadedmetadata', () => {
video.play();
})
}
const loadLocalstream = async(myID) => {
if (navigator.mediaDevices.getUserMedia) {
// console.log('mycam');
const localStream = await mediaStream(constraints);
addVideoStream(myID, localStream, "mute");
let myVideo = document.querySelector("#" + myID + "-container");
myVideo.classList.add('myVideo');
myVideo.classList.remove('video-container');
peer.on('call', call => {
console.log({on: "call"});
call.answer(localStream);
call.on('stream', stream => {
console.log({on: "stream, inside call"});
addVideoStream(call.peer, stream);
});
});
window.localStream = localStream;
checkVideo();
}
}
function checkVideo() {
let occupied = 0;
document.querySelectorAll("video").forEach(function(e) {
if (e.srcObject == null) {
connectToUser(e.id);
occupied++;
console.log(e.id);
}
});
if (occupied > 0) {
var t = setTimeout(() => {
checkVideo();
}, 7000);
} else {
clearTimeout(t);
}
}
function reconnect() {
peerIDs.forEach(peerID => {
console.log("calling ", peerID);
if (localStorage.videoID !== peerID) {
connectToUser(peerID);
}
});
}
const mediaStream = async (constraints) => {
try {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
return stream;
} catch (error) {
console.log({mediaStreamError: error});
return false;
}
}
const connectToUser = (peerID) => {
const thisCall = peer.call(peerID, window.localStream);
thisCall.on('stream', stream => {
addVideoStream(peerID, stream);
});
thisCall.on('close', () => {
document.querySelector("#" + peerID).srcObject = null;
});
}
const peerIDs = ["firefox", "pratik", "mobile"];
loadLocalstream(localStorage.videoID);
</script>
</head>
<body>
<!-- <video id="myCam" autoplay="true" muted></video> -->
<!-- <button id="reconnect">Reconnect</button>
<button id="check">Check</button> -->
<p id="myID">Connecting...</p>
<div id="container">
<div class="video-container" id="firefox-container"><label>Amit</label><video id="firefox" autoplay="true"></video></div>
<div class="video-container" id="pratik-container"><label>Pratik</label><video id="pratik" autoplay="true"></video></div>
<div class="video-container" id="mobile-container"><label>Amit's Mobile</label><video id="mobile" autoplay="true"></video></div>
</div>
</body>
</html>