-
Notifications
You must be signed in to change notification settings - Fork 1
/
livestream.html
185 lines (160 loc) · 5.58 KB
/
livestream.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
181
182
183
184
185
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LiveStream</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<style>
body {
background: #1d2024;
}
#screen {
border: 4px solid #353535;
background-position: center;
background-repeat: no-repeat;
background-size:contain;
position: fixed;
height: var(--size);
}
#viewcount, #resolution {
margin-right: 50px;
}
.nav-item {
margin-left: 50px;
}
.form-range {
width: 250px;
}
.card {
border: none;
}
h1 {
margin-top: 15px;
font-size: 30px;
color: white;
}
input[type="range"] {
color: green;
}
</style>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="navbar-collapse collapsed" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-item nav-link">
<!-- This value will be updated for everyone watching -->
<label>Quality (For everyone synced)</label><br>
<input type="range" class="form-range" id="compression" min="0" max="100" oninput="this.nextElementSibling.value = this.value + '%'">
<output id="rangevalue">50%</output>
</a>
<a class="nav-item nav-link">
<!-- Select the size of the screen -->
<label>Size: </label><br>
<input type="range" class="form-range" id="size" min="0" max="100" oninput="document.documentElement.style.setProperty('--size', this.value + '%');">
</a>
</div>
<!-- <div class="dropdown">
<button id="resolutiontext" class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
Select resolution
</button>
<ul id="resolution" class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a class="dropdown-item">1080p</a></li>
<li><a class="dropdown-item">720p</a></li>
<li><a class="dropdown-item">480p</a></li>
<li><a class="dropdown-item">360p</a></li>
<li><a class="dropdown-item">240p</a></li>
<li><a class="dropdown-item">144p</a></li>
</ul>
</div> -->
</div>
<ul class="navbar-nav ml-auto">
<!-- <h1 id="network">Network usage at title</h1> -->
<h1 id="resolution"></h1>
<h1 id="viewcount"></h1>
</ul>
</nav>
<div class="card text-center align-items-center col-6 mx-auto">
<img onclick="FullScreen()" id="screen" class="img-fluid">
</div>
</body>
<script>
function getColor(value) {
var hue = ((1 - value) * 120).toString(10);
return ["hsl(", hue, ",100%,50%)"].join("");
}
//Set color of slider on load
document.getElementById('rangevalue').style.color = getColor($("#compression").val() / 100)
//Set the screenshare to fullscreen when clicked
function FullScreen() {
var video = document.getElementById('screen')
if (!document.fullscreenElement) {
video.requestFullscreen();
}
else
{
document.exitFullscreen();
}
}
//Setting up WebSocket
var ip = '127.0.0.1'
var port = '6969'
var socket = new WebSocket(`ws://${ip}:${port}`)
console.log('WebSocket: Waiting for connection')
//Set default image screen to 91% onload
document.documentElement.style.setProperty('--size', '91%')
$("#size").val('91');
//Default image
document.getElementById('screen').src = `./offline.png`;
//If we receive connection to websocket print in console
socket.onopen = function(){
console.log(`WebSocket: Listening on ${ip}:${port}`)
}
//If we lose connection reset the image screen and let the user know
socket.onclose = function(){
document.getElementById('screen').src = `./offline.png`;
console.error('WebSocket: Lost connection')
alert('WebSocket: Lost connection')
}
//If compression updates, send new compression value to server
$("#compression").click(function() {
socket.send(JSON.stringify({compression: parseInt($("#compression").val())}))
});
$("#compression").mousemove(function() {
document.getElementById('rangevalue').style.color = getColor($("#compression").val() / 100)
});
//Convert bytes to correct format
function formatBytes(a,b=2,k=1024){with(Math){let d=floor(log(a)/log(k));return 0==a?"0 Bytes":parseFloat((a/pow(k,d)).toFixed(max(0,b)))+" "+["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"][d]}}
var bytes = 0
socket.onmessage = function (event) {
var json = JSON.parse(event.data)
//If its not a image but settings/viewcount etc
if (json.image == undefined){
//Check if viewcount is not undefined then update viewcount
if (json.viewcount != undefined){
document.getElementById('viewcount').innerHTML = `Viewcount: ${json.viewcount}`
}
else
{
//Set the current compression value
document.getElementById('rangevalue').innerHTML = `${json.compression}%`
$("#compression").val(json.compression);
}
}
else
{
//Add data from length of image to total bytes used
bytes += json.image.length
//Set title to total bytes used
document.title = "Network: " + formatBytes(bytes)
document.getElementById('resolution').innerHTML = `Resolution: ${json.width}x${json.height}`;
//Set the screenshared image
document.getElementById('screen').src = json.image
}
}
</script>
</html>