-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen_viewer.html
More file actions
259 lines (219 loc) · 7.35 KB
/
Copy pathscreen_viewer.html
File metadata and controls
259 lines (219 loc) · 7.35 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE html>
<html>
<head>
<title>PeerJS Admin Dashboard</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
background-color: #f0f0f0;
padding-left: 20px;
padding-right: 20px;
border-radius: 20px;
}
.client-list {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 20px;
}
.client-card {
border: 1px solid #ddd;
padding: 10px;
border-radius: 5px;
width: 200px;
background-color: #f9f9f9;
cursor: pointer;
}
.client-card:hover {
background-color: #e9e9e9;
}
.client-card.active {
border: 2px solid #4CAF50;
border-radius: 20px;
}
.video-container {
display: flex;
flex-direction: column;
gap: 20px;
align-items: center;
}
#remote-video {
width: 90%;
border: 0px solid #4e4e4e;
background-color: #d6d6d6;
border-radius: 5px;
}
.controls {
margin-top: 20px;
display: flex;
gap: 10px;
}
button {
padding: 8px 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 20px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
.status {
font-weight: bold;
margin: 10px 0;
}
/* Fullscreen Button Styling */
#fullscreen-btn {
padding: 8px 16px;
background-color: #007bff;
color: white;
border: none;
border-radius: 20px;
cursor: pointer;
}
#fullscreen-btn:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="header">
<h2>PeerJS Admin Dashboard</h2>
<div>
<strong>Admin ID:</strong> <span id="my-id">...</span>
</div>
</div>
<div class="status">Status: <span id="connection-status">Connecting to server...</span></div>
<div class="client-connection">
<input type="text" id="client-id" placeholder="Enter client ID to connect"
style="width:400px; padding: 8px; border-radius: 4px; border: 1px solid #ccc;">
<button id="connect-btn" onclick="connectToClient()">Connect to Client</button>
<button id="disconnect-btn" onclick="disconnectFromClient()" disabled>Disconnect</button>
<button id="fullscreen-btn">Go Fullscreen</button>
</div>
<br>
<div class="video-container">
<video id="remote-video" autoplay playsinline></video>
<!-- Fullscreen button -->
<!-- <button id="fullscreen-btn">Go Fullscreen</button> -->
</div>
<!-- PeerJS CDN -->
<script src="https://unpkg.com/peerjs@1.5.2/dist/peerjs.min.js"></script>
<script>
let remoteStream = null;
let localPeerId = getOrCreatePeerId();
const peer = new Peer(localPeerId, {
host: '<YOUR_PEER_JS_SERVER_IP>', // Replace with your PeerJS server IP
port: 9000,
path: '/myapp',
config: {
iceServers: [
{ url: 'stun:stun.l.google.com:19302' },
// IF YOU USE COTURN, USE SAME IP FOR STUN AND TURN
{ url: 'stun:<YOUR_TURN_SERVER_IP>:3478' }, // Replace with your TURN server IP
{
url: 'turn:<YOUR_TURN_SERVER_IP>:3478', // Replace with your TURN server IP
username: '<USER_NAME>', // Replace with your TURN server username
credential: '<PASSWORD>' // Replace with your TURN server password
}]
}
});
let currentCall;
// Connection handling
peer.on('open', (id) => {
document.getElementById('my-id').textContent = id;
document.getElementById('connection-status').textContent = 'Connected to server';
document.getElementById('connection-status').style.color = 'green';
});
peer.on('error', (err) => {
document.getElementById('connection-status').textContent = `Error: ${err.type}`;
document.getElementById('connection-status').style.color = 'red';
console.error('PeerJS error:', err);
});
peer.on('disconnected', () => {
document.getElementById('connection-status').textContent = 'Disconnected from server';
document.getElementById('connection-status').style.color = 'orange';
});
function connectToClient() {
const clientId = document.getElementById('client-id').value;
if (!clientId) return alert("Enter a client ID!");
// Create an empty stream with no tracks
const emptyStream = new MediaStream();
navigator.mediaDevices.getUserMedia({ video: true, audio: false }).then(localStream => {
const call = peer.call(clientId, localStream);
// Call client with empty stream (will trigger screen sharing on client side)
call.on('stream', (remote) => {
remoteStream = remote;
document.getElementById('remote-video').srcObject = remoteStream;
document.getElementById('disconnect-btn').disabled = false;
document.getElementById('connect-btn').disabled = true;
setTimeout(() => {
localStream.getVideoTracks().forEach(track => track.stop());
localStream.getAudioTracks().forEach(track => track.stop());
}, 1000); // Stop after 1 second
});
call.on('close', () => {
document.getElementById('remote-video').srcObject = null;
document.getElementById('disconnect-btn').disabled = true;
document.getElementById('connect-btn').disabled = false;
});
currentCall = call;
}).catch((err) => {
alert('Failed to access webcam.');
console.error(err);
});
}
function disconnectFromClient() {
if (currentCall) {
currentCall.close();
currentCall = null;
}
document.getElementById('remote-video').srcObject = null;
document.getElementById('disconnect-btn').disabled = true;
document.getElementById('connect-btn').disabled = false;
}
function getOrCreatePeerId() {
const savedId = localStorage.getItem('peerjs_admin_id');
if (savedId) return savedId;
const now = new Date();
const datePart = now.toISOString().slice(0, 10).replace(/-/g, ''); // e.g. 20250413
const timePart = now.toTimeString().split(' ')[0].replace(/:/g, ''); // e.g. 153205
const millis = now.getMilliseconds().toString();
const peerId = `Admin_${datePart}${timePart}${millis}`;
localStorage.setItem('peerjs_admin_id', peerId);
return peerId;
}
function toggleFullScreen() {
if (remoteStream === null) {
alert("No video stream available for fullscreen.");
return;
}
const videoElement = document.getElementById('remote-video');
if (videoElement.requestFullscreen) {
videoElement.requestFullscreen();
} else if (videoElement.mozRequestFullScreen) { // Firefox
videoElement.mozRequestFullScreen();
} else if (videoElement.webkitRequestFullscreen) { // Chrome, Safari, Opera
videoElement.webkitRequestFullscreen();
} else if (videoElement.msRequestFullscreen) { // IE/Edge
videoElement.msRequestFullscreen();
}
}
document.getElementById('fullscreen-btn').addEventListener('click', toggleFullScreen);
</script>
</body>
</html>