-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathview.php
More file actions
555 lines (476 loc) · 19.6 KB
/
view.php
File metadata and controls
555 lines (476 loc) · 19.6 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<?php
// <!-- https://html5.tutorials24x7.com/blog/how-to-capture-image-from-camera -->
// include_once '../login/serviceServer.php';
// auth check - commented out for public release
/*
$user = new authMaintainer();
if (!($user->statusCheck())) {
header("Location: https://" . join(DIRECTORY_SEPARATOR, array($config['SSOdomain'], $config['SSOrootPath'], $config['SSOerrPath'])));
die();
}
*/
// render the requested image
if ((isset($_GET['act'])) && ($_GET['act'] == "stream")) {
if (!isset($_GET['f'])) {
header("HTTP/1.0 404 Not Found");
die();
}
header("Access-Control-Allow-Origin: *");
$file = getcwd() . '/' . $_GET['f'];
$pos = strpos($file, 'php');
if ($pos !== false) {
header("HTTP/1.0 404 Not Found");
die();
}
$pos = strpos($file, '..');
if ($pos !== false) {
header("HTTP/1.0 404 Not Found");
die();
}
$file = preg_replace('/[^A-Za-z0-9.\]\[(\/)% _\-]/', '-', $file);
if (is_dir($file)) {
header("HTTP/1.0 404 Not Found");
die();
}
if (file_exists($file)) {
$file_extension = strtolower(substr(strrchr($file, "."), 1));
$ctype = "text/css";
switch ($file_extension) {
case "gif":
$ctype = "image/gif";
break;
case "png":
$ctype = "image/png";
break;
case "jpeg":
$ctype = "image/jpeg";
break;
case "jpg":
$ctype = "image/jpeg";
break;
case "mp4":
$ctype = "video/mp4";
break;
case "webm":
$ctype = "video/webm";
break;
case "js":
$ctype = "application/javascript";
break;
case "svg":
$ctype = "image/svg+xml";
break;
default:
//$ctype="invalid";
}
//if ($ctype=="invalid"){header("HTTP/1.0 404 Not Found");die();}
header("Content-Type: " . $ctype);
readfile($file);
} else {
header("HTTP/1.0 404 Not Found");
}
}
// get the file name of the most up to date image
if ((isset($_GET['act'])) && ($_GET['act'] == "view")) {
if ((!(isset($_GET['f']))) || (!(is_numeric($_GET['f'])))) { // if no current user file name provided
$json = json_encode(array(
'name' => '',
'status' => 'Failed',
));
echo $json;
die();
}
$datapath = 'data/';
$file_ext = ".jpg";
$fileValidPeriod = 30 * 1000; // delete file after $fileValidPeriod seconds
$requestTimeOut = 1500; // milliseconds, request wait for file to come up for this much time
$loopSleepTime = 5; // milliseconds, wait before next round of research, not to overload server
//$timestamp = round(microtime(true) * 1000);
$millitimestamp = round(microtime(true) * 1000);
while ((round(microtime(true) * 1000) - $millitimestamp) < $requestTimeOut) { // request wait for file to come up
$allfiles = array_diff(scandir($datapath), array('.', '..', '.htaccess', '.ipynb_checkpoints'));
$streamFilename = '';
$allfiles = array_map(function ($value) use ($file_ext) {
$filetimestamp = basename($value, $file_ext);
if (is_numeric($filetimestamp)) {
return $filetimestamp;
}
}, $allfiles);
$allfiles = array_filter($allfiles);
rsort($allfiles); // put the latest first
foreach ($allfiles as $value) {
# if is the latest and if not empty file
if (($value > $millitimestamp - $fileValidPeriod) // if within valid period
&& ($value > intval($_GET['f'])) // if file is more up-to-date than user's current one
&& (file_exists($datapath . $value . $file_ext))
&& (filesize($datapath . $value . $file_ext) > 1)) { // file not empty (not currently being written to)
$streamFilename = $value . $file_ext;
break; // break from loop when find one file (in an already reverse sorted array)
}
}
if ($streamFilename != '') { // if at this moment, the newest file is found, then break while loop
break;
}
usleep($loopSleepTime * 1000);
}
if ($streamFilename == '') { // if no updated file found
if ($millitimestamp - intval($_GET['f']) < $fileValidPeriod) { // if there is stream, but just not updated file
$status = 'No update';
} else { // no stream
$status = 'Failed';
}
} else {
$status = 'Success';
}
$json = json_encode(array(
'name' => $datapath . $streamFilename,
'status' => $status,
'size' => filesize($datapath . $streamFilename),
));
echo $json;
die();
}
?>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Simple Web Video Streaming - Viewer</title>
<link rel="shortcut icon" href="assets/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, minimum-scale=1.0">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="assets/custom.js"></script>
<link rel="stylesheet" href="assets/custom.css">
<style>
body {
margin: 0;
padding: 0;
background-color: #fafafa;
font-family: "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
color: #333;
}
.button-group,
.play-area {
margin: 2em auto;
max-width: 800px;
background-color: #ededed;
border: 1px solid #ccc;
border-radius: 8px;
padding: 1em;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}
.button,
.btn {
display: inline-block;
margin-right: 1em;
padding: 0.65em 1.4em;
font-size: 1em;
font-weight: 500;
border: 1px solid #ccc;
border-radius: 6px;
background-color: #fff;
color: #333;
cursor: pointer;
text-decoration: none;
transition: box-shadow 0.2s ease, transform 0.2s ease;
}
.button:hover,
.btn:hover {
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.12);
transform: translateY(-2px);
}
.button:active,
.btn:active {
transform: translateY(0);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.button:focus,
.btn:focus {
outline: none;
box-shadow: 0 0 0 3px rgba(170, 170, 170, 0.3);
}
h1, h2, h3, h4, h5, h6 {
margin: 0;
padding: 0;
font-weight: 600;
color: #333;
}
.play-area-sub {
display: inline-block;
vertical-align: top;
padding: 0.5em;
}
#capture {
display: none;
}
#snapshot {
display: inline-block;
margin-top: 0.5em;
}
#snapshot img {
max-width: 100%;
height: auto;
border-radius: 4px;
border: 1px solid #ccc;
}
#timestampIndicator {
margin-top: 0.75em;
font-size: 0.9em;
color: #666;
font-style: italic;
}
table {
margin: 1em auto 0 auto;
border-collapse: collapse;
text-align: center;
font-size: 1.05em;
color: #555;
}
table td {
padding: 0.3em 1em;
}
#metricFrameRate,
#metricBitRate {
font-weight: 700;
color: #11998e;
font-size: 1.15em;
}
</style>
</head>
<body>
<!-- The buttons to control the stream -->
<div class="button-group">
<button id="btn-start" type="button" class="btn btn-primary">Start View</button>
<button id="btn-stop" type="button" class="btn btn-primary">Stop View</button>
<button id="btn-streaming" type="button" class="btn btn-primary">Streaming Page</button>
</div>
<!-- Video Element & Canvas -->
<div class="play-area">
<div class="play-area-sub">
<h3>View Stream</h3>
<!-- size setting here has no effect, depending on image size -->
<!-- <canvas id="capture" width="480" height="360"></canvas> -->
<div id="snapshot">
<img id="pic" src="">
</div>
<br>
<div id="timestampIndicator"></div>
<br>
<!-- <div id="metricFrameRate"></div>-->
<div>
<table style="border: 0px; margin-left: auto; margin-right: auto;">
<tr>
<td>Frame Rate: </td>
<td id="metricFrameRate">0.0</td>
<td> FPS</td>
</tr>
<tr>
<td>Transfer Rate: </td>
<td id="metricBitRate">0.0</td>
<td> kB/s</td>
</tr>
</table>
</div>
</div>
</div>
<script>
// Config object
const CONFIG = {
requestInterval: 5000, // polling rate when no streaming available
xhrTimeout: 6000, // millisecond
logLength: 6,
emptyTransferSize: 9, // kB, the extra http image request size (transfer size on top of image size)
borderWidth: 40, // border between image feed and window edge
capture_width: 480,
capture_height: 360
};
// DOM elements
const DOM = {
btn: {
start: document.getElementById("btn-start"),
stop: document.getElementById("btn-stop"),
streaming: document.getElementById("btn-streaming")
},
snapshot: document.getElementById("snapshot"),
pic: document.getElementById("pic"),
timestampIndicator: document.getElementById("timestampIndicator"),
metrics: {
frameRate: document.getElementById("metricFrameRate"),
bitRate: document.getElementById("metricBitRate")
}
};
let display_width, display_height;
// Stream state
let currentFileTimestamp = 0;
let logTimestamp = []; // log of timestamp for frame rate analysis with the array length of logLength
let logFileSize = []; // log of image file size for bit rate analysis with the array length of logLength
// Initialize browser detection
const detectedBrowser = fnBrowserDetect();
// Initial setup when document is ready
$(document).ready(function() {
initializeUI();
attachEventListeners();
});
// Initialize the UI
function initializeUI() {
DOM.btn.stop.style.display = "none"; // hide stop button
}
// Attach event listeners
function attachEventListeners() {
DOM.btn.start.addEventListener("click", startStream);
DOM.btn.stop.addEventListener("click", stopStreaming);
DOM.btn.streaming.addEventListener("click", redirectStreamingPage);
}
// Detect user browser type
function fnBrowserDetect() {
let userAgent = navigator.userAgent;
let browserName;
if (userAgent.match(/chrome|chromium|crios/i)) {
browserName = "chrome";
} else if (userAgent.match(/firefox|fxios/i)) {
browserName = "firefox";
} else if (userAgent.match(/safari/i)) {
browserName = "safari";
} else if (userAgent.match(/opr\//i)) {
browserName = "opera";
} else if (userAgent.match(/edg/i)) {
browserName = "edge";
} else {
browserName = "No match";
}
return browserName;
}
// Update metrics with new data
function updateMetrics(timestamp, fileSize, displayRounding) {
logTimestamp.push(timestamp);
if (logTimestamp.length > CONFIG.logLength) {
logTimestamp.shift(); // Remove an item from the beginning of an array
}
let logTimeSpan = (logTimestamp[logTimestamp.length - 1] - logTimestamp[0]) / 1000;
if (logTimeSpan > 0) {
DOM.metrics.frameRate.innerHTML = (logTimestamp.length / logTimeSpan).toFixed(displayRounding);
}
logFileSize.push(fileSize);
if (logFileSize.length > CONFIG.logLength) {
logFileSize.shift(); // Remove an item from the beginning of an array
}
let sum = 0;
for (let i = 0; i < logFileSize.length; i++) {
sum += parseInt(logFileSize[i], 10); //don't forget to add the base
}
if (logTimeSpan > 0) {
DOM.metrics.bitRate.innerHTML = (sum / logTimeSpan).toFixed(displayRounding);
}
}
// Start Streaming
function startStream() {
// show stop button, hide stop button
DOM.btn.start.style.display = "none";
DOM.btn.stop.style.display = "inline";
// set a predefined image snapshot size
setImageSize();
// start multiple function "threads" in polling newest image
setTimeout("viewStream()", 0);
setTimeout("viewStream()", 500);
setTimeout("viewStream()", 1000);
// setTimeout("viewStream()",1500);
}
// Set the snapshot image size based on window width
function setImageSize() {
if (window.innerWidth < CONFIG.capture_width + CONFIG.borderWidth) {
display_width = window.innerWidth - CONFIG.borderWidth;
display_height = (window.innerWidth - CONFIG.borderWidth) / CONFIG.capture_width * CONFIG.capture_height;
DOM.pic.width = display_width;
DOM.pic.height = display_height;
} else {
display_width = CONFIG.capture_width;
display_height = CONFIG.capture_height;
DOM.pic.width = display_width;
DOM.pic.height = display_height;
}
}
// Stop streaming by redirecting to the same page
function stopStreaming() {
window.location.replace("./view.php");
}
// Redirect to video streaming host page
function redirectStreamingPage() {
window.location.replace("./");
}
// format time string
function timeBreakout(inputTime) {
let dateObj = new Date(inputTime);
let months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
let year = dateObj.getFullYear();
let month = months[dateObj.getMonth()];
let date = dateObj.getDate();
let hour = dateObj.getHours();
let min = dateObj.getMinutes();
let sec = dateObj.getSeconds();
let formattedTime = year + '-' + month + '-' + date + ' ' + hour + ':' + min + ':' + sec;
return formattedTime;
}
// load an image in background before displaying
function preloadImage(img, resp, anImageLoadedCallback) { // download image from server before displaying
// fix for Firefox flickering issue:
// https://stackoverflow.com/questions/14704796/image-reload-causes-flicker-only-in-firefox
img.onload = anImageLoadedCallback;
// set the source of the new image to trigger the load
img.src = 'view.php?act=stream&f=' + resp.name;
}
// Display image based on browser type
function displayImage(img, respTimestamp) {
// this is required due to firefox image flickering issue
if (detectedBrowser == "firefox") { // this will be slow for Chrome but not Firefox
document.images["pic"].src = img.src; // replace the existing image once the new image has loaded
} else { // this method does not introduce slow down for Chrome
DOM.snapshot.innerHTML = '';
img.style.width = display_width; // need to resize image again since it was cleared
img.style.height = display_height;
DOM.snapshot.appendChild(img); // display image
}
DOM.timestampIndicator.innerHTML = timeBreakout(respTimestamp);
}
// view stream
function viewStream() {
let displayRounding = 1;
let request = new XMLHttpRequest();
request.open("GET", "view.php?act=view&f=" + String(currentFileTimestamp), async = true);
request.timeout = CONFIG.xhrTimeout; // time in milliseconds
request.send();
request.onload = function() {
if (request.status != 200) {
// analyze HTTP status of the response
setTimeout("viewStream()", 0);
} else {
let resp = JSON.parse(request.response);
let dispTime = resp.name.substring(resp.name.indexOf('/') + 1, resp.name.indexOf('.'));
let respTimestamp = parseInt(dispTime, 10);
if (resp.status == 'Success') {
if (respTimestamp <= currentFileTimestamp) { // if this file timestamp has already been returned
setTimeout("viewStream()", 0);
} else {
currentFileTimestamp = respTimestamp; // going to load this image, update timestamp
var img = new Image();
preloadImage(img, resp, function() { // don't write this as a separate callback function
displayImage(img, respTimestamp);
updateMetrics(respTimestamp, resp.size / 1000 + CONFIG.emptyTransferSize, displayRounding);
setTimeout("viewStream()", 0);
});
}
} else if (resp.status == 'No update') {
setTimeout("viewStream()", 0);
} else {
DOM.snapshot.innerHTML = 'No Streaming available.';
setTimeout("viewStream()", CONFIG.requestInterval); // reduce polling rate when no streaming available
}
}
};
request.ontimeout = function() {
// XMLHttpRequest timed out.
setTimeout("viewStream()", 0);
};
}
</script>
</body>
</html>