Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

add main html that is just a copy atm

roryclear4d793c455f14
diff --git a/clearcam/FileServer.m b/clearcam/FileServer.m
index d5407e4..665388e 100644
--- a/clearcam/FileServer.m
+++ b/clearcam/FileServer.m
@@ -104,7 +104,7 @@
}
}
}
-
+
if (indexesParam) {
NSMutableArray<NSNumber *> *newIndexes = [NSMutableArray array];
NSArray *indexesArray = [indexesParam componentsSeparatedByString:@","];
@@ -135,6 +135,26 @@
send(clientSocket, jsonData.bytes, jsonData.length, 0);
return;
}
+ if ([filePath hasPrefix:@"main"]) {
+ NSString *playerFilePath = [[NSBundle mainBundle] pathForResource:@"main" ofType:@"html"];
+ if (playerFilePath && [[NSFileManager defaultManager] fileExistsAtPath:playerFilePath]) {
+ FILE *file = fopen([playerFilePath UTF8String], "rb");
+ if (file) {
+ fseek(file, 0, SEEK_END);
+ NSUInteger fileSize = ftell(file);
+ fseek(file, 0, SEEK_SET);
+ dprintf(clientSocket, "HTTP/1.1 200 OK\r\n");
+ dprintf(clientSocket, "Content-Type: text/html\r\n");
+ dprintf(clientSocket, "Content-Length: %lu\r\n", fileSize);
+ dprintf(clientSocket, "\r\n");
+ [self sendFileData:file toSocket:clientSocket withContentLength:fileSize];
+ fclose(file);
+ return;
+ }
+ }
+ dprintf(clientSocket, "HTTP/1.1 500 Internal Server Error\r\n\r\n");
+ return;
+ }
if ([filePath hasPrefix:@"get-segments"]) {
NSString *startParam = nil;
NSString *dateParam = nil;
@@ -153,7 +173,7 @@
}
}
}
-
+
if(!dateParam){
NSString *httpHeader = @"HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\n\r\n";
NSString *errorMessage = @"{\"error\": \"Missing or invalid date parameter\"}";
@@ -170,7 +190,7 @@
if ([[NSFileManager defaultManager] fileExistsAtPath:segmentsFilePath]) {
NSData *data = [NSData dataWithContentsOfFile:segmentsFilePath];
NSArray *fileSegments = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
-
+
if (fileSegments) {
self.segmentsDict[dateParam] = [fileSegments mutableCopy];
NSLog(@"found segments %lu",(unsigned long)fileSegments.count);
@@ -183,7 +203,7 @@
}
}
}
-
+
NSArray *segmentsForDate = self.segmentsDict[dateParam];
if (start >= segmentsForDate.count) {
NSString *httpHeader = @"HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\n\r\n";
@@ -199,20 +219,20 @@
send(clientSocket, slicedJsonData.bytes, slicedJsonData.length, 0);
return;
}
-
+
NSString *fullPath = [basePath stringByAppendingPathComponent:filePath];
NSRange queryRange = [fullPath rangeOfString:@"?"];
if (queryRange.location != NSNotFound) {
fullPath = [fullPath substringToIndex:queryRange.location];
}
-
+
BOOL isDirectory = NO;
if (![[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory]) {
dprintf(clientSocket, "HTTP/1.1 404 Not Found\r\n\r\n");
return;
}
-
-
+
+
if (isDirectory) { //todo, change url maybe
NSString *playerFilePath = [[NSBundle mainBundle] pathForResource:@"player" ofType:@"html"];
if (playerFilePath && [[NSFileManager defaultManager] fileExistsAtPath:playerFilePath]) {
@@ -233,7 +253,7 @@
dprintf(clientSocket, "HTTP/1.1 500 Internal Server Error\r\n\r\n");
return;
}
-
+
FILE *file = fopen([fullPath UTF8String], "rb");
if (!file) {
dprintf(clientSocket, "HTTP/1.1 500 Internal Server Error\r\n\r\n");
@@ -242,8 +262,15 @@
fseek(file, 0, SEEK_END);
NSUInteger fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
-
+
NSString *fileExtension = [[fullPath pathExtension] lowercaseString];
+
+ // Check if the file exists in the app bundle
+ NSString *bundlePath = [[NSBundle mainBundle] pathForResource:[fullPath stringByDeletingPathExtension] ofType:fileExtension];
+ if (bundlePath) {
+ fullPath = bundlePath;
+ }
+
if ([fileExtension isEqualToString:@"mp4"]) {
range = [request rangeOfString:@"Range: bytes="];
if (range.location != NSNotFound) {
@@ -253,7 +280,7 @@
NSArray<NSString *> *rangeParts = [byteRange componentsSeparatedByString:@"-"];
NSUInteger start = [rangeParts[0] integerValue];
NSUInteger end = (rangeParts.count > 1 && rangeParts[1].length > 0) ? [rangeParts[1] integerValue] : fileSize - 1;
-
+
if (start >= fileSize || end >= fileSize || start > end) {
dprintf(clientSocket, "HTTP/1.1 416 Requested Range Not Satisfiable\r\n");
dprintf(clientSocket, "Content-Range: bytes */%lu\r\n", fileSize);
@@ -270,7 +297,13 @@
[self sendFileData:file toSocket:clientSocket withContentLength:contentLength];
}
}
- } else { //.txt
+ } else if ([fileExtension isEqualToString:@"html"]) {
+ dprintf(clientSocket, "HTTP/1.1 200 OK\r\n");
+ dprintf(clientSocket, "Content-Type: text/html\r\n");
+ dprintf(clientSocket, "Content-Length: %lu\r\n", fileSize);
+ dprintf(clientSocket, "\r\n");
+ [self sendFileData:file toSocket:clientSocket withContentLength:fileSize];
+ } else if ([fileExtension isEqualToString:@"txt"]) {
dprintf(clientSocket, "HTTP/1.1 200 OK\r\n");
dprintf(clientSocket, "Content-Type: text/plain\r\n");
dprintf(clientSocket, "Content-Length: %lu\r\n", fileSize);
diff --git a/clearcam/main.html b/clearcam/main.html
new file mode 100644
index 0000000..9ad1d86
--- /dev/null
+++ b/clearcam/main.html
@@ -0,0 +1,1033 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <style>
+ #videoContainer {
+ position: relative;
+ width: 100%;
+ max-width: 640px; /* Default max-width for landscape */
+ background: black;
+ aspect-ratio: 16 / 9; /* Default aspect ratio for landscape */
+ overflow: hidden; /* Prevent overflow */
+ transition: max-width 0.3s ease-in-out, aspect-ratio 0.3s ease-in-out; /* Smooth transitions */
+ }
+
+ .videoPlayer {
+ width: 100%;
+ height: 100%;
+ object-fit: contain; /* Ensure video fits within the container */
+ }
+ .video-wrapper {
+ position: relative;
+ width: 100%;
+ height: 100%;
+ }
+ video {
+ position: absolute;
+ width: 100%;
+ height: 100%;
+ object-fit: contain;
+ }
+ #controls {
+ position: absolute;
+ bottom: 0;
+ width: 100%;
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ background: rgba(0, 0, 0, 0.5);
+ padding: 10px;
+ opacity: 0;
+ transition: opacity 0.3s;
+ box-sizing: border-box;
+ }
+ #controls.show {
+ opacity: 1;
+ }
+ .top-controls {
+ display: flex;
+ align-items: center;
+ width: 100%;
+ justify-content: space-between;
+ margin-bottom: 10px;
+ box-sizing: border-box;
+ }
+ #progress {
+ width: 100%;
+ height: 10px;
+ background-color: #ccc;
+ cursor: pointer;
+ position: relative;
+ border-radius: 5px;
+ box-sizing: border-box;
+ overflow: hidden;
+ }
+
+ #progress-bar {
+ height: 100%;
+ background-color: #007bff;
+ border-radius: 5px;
+ position: absolute;
+ top: 0;
+ left: 0;
+ width: 0;
+ }
+
+ .gap-indicator {
+ position: absolute;
+ top: 0;
+ height: 100%;
+ background: repeating-linear-gradient(
+ -45deg,
+ black,
+ black 3px,
+ grey 3px,
+ grey 6px
+ );
+ pointer-events: none;
+ }
+
+ button {
+ background: none;
+ border: none;
+ padding: 0;
+ margin: 0;
+ }
+ #timeDisplay {
+ color: white;
+ margin-left: 10px;
+ }
+ #fullscreenBtn {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ width: 40px;
+ height: 40px;
+ padding: 8px;
+ cursor: pointer;
+ background: transparent;
+ border: none;
+ outline: none;
+ }
+
+ #controls {
+ position: absolute;
+ bottom: 0;
+ width: 100%;
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ background: rgba(0, 0, 0, 0.5);
+ padding: 10px;
+ opacity: 0;
+ visibility: hidden;
+ transition: opacity 0.3s ease-in-out, visibility 0s linear 0.3s;
+ box-sizing: border-box;
+ }
+
+ #controls.show {
+ opacity: 1;
+ visibility: visible;
+ transition: opacity 0.3s ease-in-out;
+ }
+
+ #playPauseBtn {
+ position: absolute;
+ top: 50%;
+ left: 50%;
+ transform: translate(-50%, -50%);
+ z-index: 10;
+ background: rgba(0, 0, 0, 0.5);
+ border-radius: 50%;
+ width: 60px;
+ height: 60px;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ cursor: pointer;
+ transition: opacity 0.3s ease-in-out, visibility 0s linear 0.3s, background 0.3s;
+ opacity: 0; /* Start hidden */
+ visibility: hidden; /* Start hidden */
+ }
+
+ #playPauseBtn.show {
+ opacity: 1; /* Show when .show is added */
+ visibility: visible; /* Show when .show is added */
+ transition: opacity 0.3s ease-in-out;
+ }
+
+ #playPauseBtn:hover {
+ background: rgba(0, 0, 0, 0.7);
+ }
+
+ #playPauseBtn svg {
+ width: 30px;
+ height: 30px;
+ }
+
+ /* Adjust the top controls to align time and fullscreen button to the right */
+ .top-controls {
+ justify-content: flex-end;
+ }
+
+ .top-controls #timeDisplay {
+ margin-right: 10px;
+ }
+ /* Existing styles remain the same until .top-controls */
+
+
+
+ #classSelector {
+ margin-right: 0; /* Remove margin for the last element */
+ }
+
+ /* Adjust the bottom controls to remove the date picker and select dropdown */
+ #controls {
+ position: absolute;
+ bottom: 0;
+ width: 100%;
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ background: rgba(0, 0, 0, 0.5);
+ padding: 10px;
+ opacity: 0;
+ visibility: hidden;
+ transition: opacity 0.3s ease-in-out, visibility 0s linear 0.3s;
+ box-sizing: border-box;
+ }
+
+ #controls.show {
+ opacity: 1;
+ visibility: visible;
+ transition: opacity 0.3s ease-in-out;
+ }
+
+ /* Adjust the time display and fullscreen button container */
+ .top-controls {
+ display: flex;
+ align-items: center;
+ width: 100%;
+ justify-content: flex-end; /* Align to the right */
+ gap: 10px; /* Add gap between elements */
+ }
+
+ #timeDisplay {
+ font-size: 14px; /* Smaller font size for compactness */
+ white-space: nowrap; /* Prevent wrapping */
+ color: white; /* White text */
+ }
+
+ #fullscreenBtn {
+ width: 30px; /* Smaller button for compactness */
+ height: 30px;
+ padding: 5px;
+ background: transparent;
+ border: none;
+ cursor: pointer;
+ }
+
+ #topControls {
+ position: absolute;
+ top: 0;
+ width: 100%;
+ display: flex;
+ align-items: center;
+ justify-content: flex-end; /* Align items to the right */
+ background: rgba(0, 0, 0, 0.5);
+ padding: 10px;
+ box-sizing: border-box;
+ opacity: 0;
+ visibility: hidden;
+ transition: opacity 0.3s ease-in-out, visibility 0s linear 0.3s;
+ z-index: 5; /* Ensure it's above the video */
+ gap: 10px; /* Add gap between elements */
+ }
+
+ #topControls.show {
+ opacity: 1;
+ visibility: visible;
+ transition: opacity 0.3s ease-in-out;
+ }
+
+ #datePicker, #classSelector {
+ flex: 0 1 auto; /* Don't grow, but allow shrinking */
+ min-width: 120px; /* Set a minimum width */
+ max-width: 200px; /* Set a maximum width */
+ font-size: 14px; /* Smaller font size for compactness */
+ padding: 5px; /* Smaller padding for compactness */
+ background: rgba(255, 255, 255, 0.1); /* Slightly transparent background */
+ border: 1px solid rgba(255, 255, 255, 0.3); /* Light border */
+ color: white; /* White text */
+ border-radius: 4px; /* Rounded corners */
+ }
+
+ /* Media query for smaller screens */
+ @media (max-width: 600px) {
+ #datePicker, #classSelector {
+ min-width: 100px; /* Smaller minimum width for mobile */
+ max-width: 150px; /* Smaller maximum width for mobile */
+ font-size: 12px; /* Even smaller font size for mobile */
+ padding: 4px; /* Even smaller padding for mobile */
+ }
+
+ #timeDisplay {
+ font-size: 12px; /* Smaller font size for mobile */
+ }
+
+ #fullscreenBtn {
+ width: 24px; /* Smaller button for mobile */
+ height: 24px;
+ padding: 4px;
+ }
+ }
+
+ </style>
+</head>
+<body>
+ <div id="videoContainer">
+ <div class="video-wrapper">
+ <!-- Video elements remain the same -->
+ <video class="videoPlayer" playsinline webkit-playsinline muted></video>
+ <video class="videoPlayer" playsinline webkit-playsinline muted style="display: none;"></video>
+ <video class="videoPlayer" playsinline webkit-playsinline muted style="display: none;"></video>
+ <video class="videoPlayer" playsinline webkit-playsinline muted style="display: none;"></video>
+ <video class="videoPlayer" playsinline webkit-playsinline muted style="display: none;"></video>
+ <video class="videoPlayer" playsinline webkit-playsinline muted style="display: none;"></video>
+
+ <!-- New top controls bar -->
+ <div id="topControls">
+ <input type="date" id="datePicker" name="datePicker">
+ <select id="classSelector" name="classSelector" onchange="changeClasses()">
+ <option value="all">Detect All</option>
+ <option value="vehiclesPeople">People+Vehicles</option>
+ </select>
+ </div>
+ <!-- Bottom controls (progress bar, time display, fullscreen button) -->
+ <div id="controls">
+ <div class="top-controls">
+ <div id="timeDisplay">0:00 / 0:00</div>
+ <button id="fullscreenBtn">
+ <svg id="fullscreenIcon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="white" width="24px" height="24px">
+ <path d="M3 3h6v2H5v4H3zm12 0h6v6h-2V5h-4zm6 12v6h-6v-2h4v-4zm-12 6H3v-6h2v4h4z"/>
+ </svg>
+ </button>
+ </div>
+ <div id="progress">
+ <div id="progress-bar"></div>
+ <div id="gaps-container"></div>
+ </div>
+ </div>
+
+ <!-- Play/Pause button -->
+ <button id="playPauseBtn">
+ <svg id="playIcon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="white" width="24px" height="24px">
+ <path d="M8 5v14l11-7z"/>
+ </svg>
+ <svg id="pauseIcon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="white" width="24px" height="24px" style="display: none;">
+ <path d="M6 19h4V5H6zm8-14v14h4V5h-4z"/>
+ </svg>
+ </button>
+ </div>
+ </div>
+ <script>
+ const yolo_classes = [
+ ["person", "red"], ["bicycle", "green"], ["car", "blue"],
+ ["motorcycle", "cyan"], ["airplane", "magenta"], ["bus", "yellow"],
+ ["train", "orange"], ["truck", "purple"], ["boat", "brown"],
+ ["traffic light", "rgb(128, 179, 51)"], ["fire hydrant", "rgb(204, 26, 26)"],
+ ["stop sign", "rgb(77, 77, 204)"], ["parking meter", "rgb(179, 128, 77)"],
+ ["bench", "rgb(102, 102, 51)"], ["bird", "rgb(26, 128, 230)"],
+ ["cat", "rgb(204, 51, 153)"], ["dog", "rgb(230, 77, 77)"],
+ ["horse", "rgb(51, 153, 179)"], ["sheep", "rgb(179, 77, 128)"],
+ ["cow", "rgb(102, 204, 102)"], ["elephant", "rgb(77, 102, 230)"],
+ ["bear", "rgb(153, 51, 204)"], ["zebra", "rgb(204, 128, 51)"],
+ ["giraffe", "rgb(128, 230, 26)"], ["backpack", "rgb(77, 179, 102)"],
+ ["umbrella", "rgb(102, 153, 230)"], ["handbag", "rgb(230, 51, 128)"],
+ ["tie", "rgb(128, 77, 179)"], ["suitcase", "rgb(153, 179, 51)"],
+ ["frisbee", "rgb(179, 51, 102)"], ["skis", "rgb(77, 230, 77)"],
+ ["snowboard", "rgb(204, 26, 153)"], ["sports ball", "rgb(102, 77, 204)"],
+ ["kite", "rgb(51, 128, 179)"], ["baseball bat", "rgb(153, 102, 51)"],
+ ["baseball glove", "rgb(179, 26, 102)"], ["skateboard", "rgb(128, 204, 128)"],
+ ["surfboard", "rgb(204, 77, 153)"], ["tennis racket", "rgb(51, 179, 230)"],
+ ["bottle", "rgb(230, 51, 77)"], ["wine glass", "rgb(153, 153, 77)"],
+ ["cup", "rgb(77, 102, 230)"], ["fork", "rgb(102, 179, 51)"],
+ ["knife", "rgb(204, 51, 128)"], ["spoon", "rgb(153, 77, 179)"],
+ ["bowl", "rgb(51, 204, 102)"], ["banana", "rgb(179, 179, 26)"],
+ ["apple", "rgb(230, 26, 102)"], ["sandwich", "rgb(102, 128, 204)"],
+ ["orange", "rgb(204, 153, 51)"], ["broccoli", "rgb(77, 204, 77)"],
+ ["carrot", "rgb(179, 51, 153)"], ["hot dog", "rgb(230, 77, 128)"],
+ ["pizza", "rgb(128, 77, 204)"], ["donut", "rgb(204, 26, 102)"],
+ ["cake", "rgb(179, 128, 26)"], ["chair", "rgb(153, 51, 102)"],
+ ["couch", "rgb(102, 153, 51)"], ["potted plant", "rgb(204, 102, 128)"],
+ ["bed", "rgb(77, 179, 179)"], ["dining table", "rgb(128, 204, 77)"],
+ ["toilet", "rgb(179, 102, 153)"], ["tv", "rgb(230, 128, 51)"],
+ ["laptop", "rgb(153, 77, 179)"], ["mouse", "rgb(51, 230, 128)"],
+ ["remote", "rgb(204, 102, 77)"], ["keyboard", "rgb(77, 153, 204)"],
+ ["cell phone", "rgb(179, 77, 230)"], ["microwave", "rgb(102, 230, 102)"],
+ ["oven", "rgb(128, 179, 51)"], ["toaster", "rgb(230, 51, 77)"],
+ ["sink", "rgb(153, 204, 77)"], ["refrigerator", "rgb(204, 102, 179)"],
+ ["book", "rgb(77, 128, 230)"], ["clock", "rgb(179, 179, 51)"],
+ ["vase", "rgb(230, 102, 128)"], ["scissors", "rgb(51, 179, 204)"],
+ ["teddy bear", "rgb(153, 77, 230)"], ["hair drier", "rgb(204, 51, 77)"],
+ ["toothbrush", "rgb(102, 179, 153)"]
+ ];
+
+ function changeClasses() {
+ const selector = document.getElementById('classSelector');
+ const selectedValue = selector.value;
+ let url = '';
+
+ if (selectedValue === 'all') {
+ url = 'change-classes?indexes=0,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';
+ } else if (selectedValue === 'vehiclesPeople') {
+ url = 'change-classes?indexes=0,1,2,3,5,7';
+ }
+
+ fetch(url)
+ .then(response => {
+ if (!response.ok) {
+ throw new Error('Network response was not ok');
+ }
+ console.log('Class change request successful:', selectedValue);
+ })
+ .catch(error => {
+ console.error('There was a problem with the fetch operation:', error);
+ });
+ }
+
+ function fetchCurrentClasses() {
+ fetch('get-classes')
+ .then(response => response.json())
+ .then(data => {
+ if (Array.isArray(data)) {
+ updateDropdown(data);
+ }
+ })
+ .catch(error => {
+ console.error('Error fetching current classes:', error);
+ });
+ }
+
+ function updateDropdown(currentClasses) {
+ const selector = document.getElementById('classSelector');
+ if (currentClasses.length === 80) { //todo, will need to change with more presets + custom !
+ selector.value = 'all';
+ } else if (JSON.stringify(currentClasses) === JSON.stringify([0, 1, 2, 3, 5, 7])) {
+ selector.value = 'vehiclesPeople';
+ }
+ }
+
+ // Call fetchCurrentClasses on page load and every minute
+ window.addEventListener('load', fetchCurrentClasses);
+ setInterval(fetchCurrentClasses, 60000); // Every minute
+
+
+ let currentDate = "";
+ const dateParam = getQueryParam("date");
+ if (!dateParam) {
+ const fetchStartTime = new Date();
+ currentDate = formatDate(fetchStartTime);
+ } else{
+ currentDate = dateParam;
+ }
+
+ let segments = [];
+ let gaps = [];
+ let totalDuration = 0;
+ let retryTimeout = 2000;
+
+
+ const videoPlayers = document.querySelectorAll(".videoPlayer");
+ let is_prepared = new Array(videoPlayers.length).fill(false); //todo, store in one obj
+ const playPauseBtn = document.getElementById('playPauseBtn');
+ const playIcon = document.getElementById('playIcon');
+ const pauseIcon = document.getElementById('pauseIcon');
+ const progressBar = document.getElementById('progress-bar');
+ const progress = document.getElementById('progress');
+ const timeDisplay = document.getElementById('timeDisplay');
+
+ let video_index = 0;
+ let currentSegment = 0;
+ let isPlaying = false;
+ let isScrubbing = false;
+ let isWaitingForNextSegment = false;
+ let wasPlayingBeforeScrub = false;
+ let shape_idx = -1;
+
+ let hideControlsTimeout;
+ const controls = document.getElementById('controls');
+ const topControls = document.getElementById('topControls');
+ const videoContainer = document.getElementById('videoContainer');
+
+ //todo
+ topControls.addEventListener('focusin', showControls);
+ topControls.addEventListener('focusout', hideControls);
+ controls.addEventListener('focusin', showControls);
+ controls.addEventListener('focusout', hideControls);
+
+ function showControls() {
+ controls.classList.add('show');
+ topControls.classList.add('show');
+ playPauseBtn.classList.add('show'); // Show the play/pause button
+ clearTimeout(hideControlsTimeout);
+ hideControlsTimeout = setTimeout(() => {
+ topControls.classList.remove('show');
+ controls.classList.remove('show');
+ playPauseBtn.classList.remove('show'); // Hide the play/pause button
+ controls.style.transform = 'translateY(0.0001px)'; // Tiny nudge to force repaint
+ topControls.style.transform = 'translateY(0.0001px)'; // Tiny nudge to force repaint
+ }, 4000);
+ }
+
+ function hideControls() {
+ if (!document.activeElement.closest('#topControls') && !document.activeElement.closest('#controls')) {
+ topControls.classList.remove('show');
+ controls.classList.remove('show');
+ playPauseBtn.classList.remove('show'); // Hide the play/pause button
+ topControls.style.transform = 'translateY(0.0001px)'; // Safari-specific hack
+ controls.style.transform = 'translateY(0.0001px)'; // Safari-specific hack
+ }
+ }
+
+ function toggleControls() {
+ if (controls.classList.contains('show')) {
+ hideControls();
+ } else {
+ showControls();
+ }
+ }
+
+ // Event listeners to show/hide controls
+ videoContainer.addEventListener('mousemove', showControls);
+ videoContainer.addEventListener('mouseleave', hideControls);
+ videoContainer.addEventListener('touchend', toggleControls);
+ videoContainer.addEventListener('click', toggleControls);
+
+ function retrySegment(videoElement) {
+ console.warn(`Retrying segment: ${segments[currentSegment].url}`);
+ setTimeout(() => {
+ videoElement.src = segments[currentSegment].url;
+ videoElement.load();
+ videoElement.play();
+ }, retryTimeout);
+ }
+
+ function updateTimeline() {
+ const startTimestamp = segments[0].timeStamp;
+ let adjustedStartTimestamp;
+
+ if (startTimestamp < 0) {
+ adjustedStartTimestamp = (24 * 3600) + startTimestamp; //todo hack?
+ } else {
+ adjustedStartTimestamp = startTimestamp;
+ }
+
+ const currentTime = adjustedStartTimestamp + videoPlayers[video_index].currentTime + segments[currentSegment].cumulativeStart;
+
+ let frames = segments[currentSegment].frames;
+ let show_new = false;
+ while((shape_idx+1 < frames.length) && (currentTime > frames[shape_idx+1].frame_timeStamp)) {
+ shape_idx += 1;
+ show_new = true;
+ }
+
+ if(show_new == true){
+ let n_boxes = document.querySelectorAll(".bounding-box").length;
+ let aspect_ratio = frames[shape_idx].aspect_ratio;
+ let res = frames[shape_idx].res;
+ for(let i = 0; i < frames[shape_idx].squares.length; i++){
+ if(portrait){ //todo, can just send the correct aspect ratio instead?
+ let originX = (frames[shape_idx].squares[i].originX / (res / aspect_ratio));
+ let originY = (frames[shape_idx].squares[i].originY / res);
+ let length = ((frames[shape_idx].squares[i].bottomRightX - frames[shape_idx].squares[i].originX) / (res / aspect_ratio));
+ let height = ((frames[shape_idx].squares[i].bottomRightY - frames[shape_idx].squares[i].originY) / res);
+ drawBoundingBox(originX, originY, length, height,yolo_classes[frames[shape_idx].squares[i].classIndex][1],yolo_classes[frames[shape_idx].squares[i].classIndex][0]);
+ } else {
+ let originX = (frames[shape_idx].squares[i].originX / res);
+ let originY = (frames[shape_idx].squares[i].originY / (res / aspect_ratio));
+ let length = ((frames[shape_idx].squares[i].bottomRightX - frames[shape_idx].squares[i].originX) / res);
+ let height = ((frames[shape_idx].squares[i].bottomRightY - frames[shape_idx].squares[i].originY) / (res / aspect_ratio));
+ drawBoundingBox(originX, originY, length, height,yolo_classes[frames[shape_idx].squares[i].classIndex][1],yolo_classes[frames[shape_idx].squares[i].classIndex][0]);
+ }
+ }
+ let boundingBoxes = document.querySelectorAll(".bounding-box");
+ for (let i = 0; i < n_boxes; i++) {
+ boundingBoxes[i].remove();
+ }
+ }
+
+ progressBar.style.width = `${(currentTime - adjustedStartTimestamp) / totalDuration * 100}%`;
+ timeDisplay.innerText = `${formatTime(currentTime)} / ${formatTime(adjustedStartTimestamp + totalDuration)}`;
+ updateGaps();
+ }
+
+
+ function updateGaps() {
+ const progressBar = document.getElementById("progress");
+ const gapsContainer = document.getElementById("gaps-container");
+ gapsContainer.innerHTML = ''; // Clear existing gaps
+
+ gaps.forEach(([start, duration]) => {
+ const gapElement = document.createElement("div");
+ gapElement.classList.add("gap-indicator");
+
+ const gapStartPercent = (start / totalDuration) * 100;
+ const gapWidthPercent = (duration / totalDuration) * 100;
+
+ gapElement.style.left = `${gapStartPercent}%`;
+ gapElement.style.width = `${gapWidthPercent}%`;
+
+ gapsContainer.appendChild(gapElement);
+ });
+ }
+
+ function togglePlayPause() {
+ if (isPlaying) {
+ videoPlayers[video_index].pause();
+ playIcon.style.display = 'block';
+ pauseIcon.style.display = 'none';
+ isPlaying = false;
+ } else {
+ videoPlayers[video_index].play();
+ playIcon.style.display = 'none';
+ pauseIcon.style.display = 'block';
+ isPlaying = true;
+ }
+ updateTimeline();
+ }
+
+ playPauseBtn.addEventListener('click', togglePlayPause);
+
+
+ videoContainer.addEventListener('dblclick', (e) => {
+ const containerWidth = videoContainer.clientWidth;
+ const x = e.clientX;
+ const currentTime = videoPlayers[video_index].currentTime + segments[currentSegment].cumulativeStart;
+
+ if (x > containerWidth * 0.6) {
+ // Double-tap/click on the right side to skip forward 10s
+ let newTime = Math.min(currentTime + 10, totalDuration);
+ seekToTime(newTime);
+ } else if (x < containerWidth * 0.4) {
+ // Double-tap/click on the left side to rewind 10s
+ let newTime = Math.max(currentTime - 10, 0);
+ seekToTime(newTime);
+ }
+
+ updateTimeline();
+ });
+
+
+ function seekToTime(time) {
+ clearBoundingBoxes();
+ is_prepared = new Array(videoPlayers.length).fill(false);
+ let targetSegment = segments.findIndex(segment => time < segment.cumulativeStart + segment.duration);
+ targetSegment = targetSegment === -1 ? segments.length - 1 : targetSegment;
+ currentSegment = targetSegment;
+ videoPlayers[video_index].src = segments[currentSegment].url;
+ videoPlayers[video_index].currentTime = time - segments[currentSegment].cumulativeStart;
+
+ shape_idx = 0;
+ if (isPlaying) {
+ videoPlayers[video_index].play();
+ }
+ }
+
+ function getQueryParam(name) {
+ const urlParams = new URLSearchParams(window.location.search);
+ return urlParams.get(name);
+ }
+
+ function formatDate(date) {
+ const year = date.getFullYear();
+ const month = String(date.getMonth() + 1).padStart(2, '0');
+ const day = String(date.getDate()).padStart(2, '0');
+ return `${year}-${month}-${day}`;
+ }
+
+ document.addEventListener('DOMContentLoaded', function() {
+ const datePicker = document.getElementById('datePicker');
+ datePicker.value = currentDate;
+ datePicker.addEventListener('change', function() {
+ const selectedDate = new Date(this.value);
+ const formattedDate = formatDate(selectedDate);
+ window.location.href = `${window.location.pathname}?date=${formattedDate}`;
+ });
+ });
+
+ let isFetchingSegments = false;
+ async function fetchSegments() {
+ if (isFetchingSegments) return;
+ isFetchingSegments = true;
+
+ try {
+ const startParam = segments.length;
+ const response = await fetch(`/get-segments?date=${currentDate}&start=${startParam}`);
+
+ if (response.ok) {
+ const newSegments = await response.json();
+ if (Array.isArray(newSegments)) {
+ const oldTotalDuration = totalDuration;
+
+ let cumulativeStart = totalDuration;
+ for (let i = 0; i < newSegments.length - 1; i++) {
+ const segment = newSegments[i];
+ const timeDifference = newSegments[i+1].timeStamp - newSegments[i].timeStamp;
+ const lostFrames = timeDifference - segment.duration;
+ const currentCumulativeStart = cumulativeStart;
+ cumulativeStart += timeDifference;
+ segments.push({
+ ...segment,
+ cumulativeStart: currentCumulativeStart,
+ lostFrames: lostFrames > 0 ? lostFrames : 0,
+ });
+ if(lostFrames > 60){ //todo why 60
+ gaps.push([currentCumulativeStart + segment.duration,lostFrames]);
+ }
+ }
+ totalDuration = segments[segments.length-1].duration + (segments[segments.length - 1].timeStamp - segments[0].timeStamp);
+ if(gaps.length > 0){
+ console.log("gaps!");
+ console.log(gaps);
+ }
+
+ if (totalDuration !== oldTotalDuration) {
+ updateTimeline();
+ preloadNextSegments();
+
+ if (videoPlayers[video_index].currentTime + segments[currentSegment].cumulativeStart === 0 ||
+ (isPlaying && isWaitingForNextSegment && segments.length < currentSegment + 1)) {
+ playSegment();
+ }
+ if (isWaitingForNextSegment && segments.length > currentSegment + 1) {
+ isWaitingForNextSegment = false;
+ switchToNextSegment();
+ }
+ }
+ }
+ }
+ } catch (error) {
+ console.error('Failed to fetch segments:', error);
+ } finally {
+ isFetchingSegments = false;
+ }
+ }
+
+
+ function formatTime(seconds) {
+ const hours = Math.floor(seconds / 3600).toString().padStart(2, '0');
+ const minutes = Math.floor((seconds % 3600) / 60).toString().padStart(2, '0');
+ const secs = Math.floor(seconds % 60).toString().padStart(2, '0');
+ return `${hours}:${minutes}:${secs}`;
+ }
+
+ function scrub(e) {
+ const scrubTime = Math.min(((e.offsetX || e.touches[0].clientX - progress.getBoundingClientRect().left) / progress.offsetWidth) * totalDuration, totalDuration - 1);
+
+ let targetSegment = segments.findIndex(segment => scrubTime < segment.cumulativeStart + segment.duration);
+ targetSegment = targetSegment === -1 ? segments.length - 1 : targetSegment;
+
+ const targetTime = scrubTime - segments[targetSegment].cumulativeStart;
+
+ if (targetSegment >= 0 && targetSegment < segments.length) {
+ currentSegment = targetSegment;
+ videoPlayers[video_index].src = segments[currentSegment].url;
+ videoPlayers[video_index].currentTime = targetTime;
+ if (wasPlayingBeforeScrub) {
+ videoPlayers[video_index].play();
+ }
+ }
+
+ updateTimeline();
+ preloadNextSegments();
+ }
+
+ function startScrubbing(e) {
+ wasPlayingBeforeScrub = !videoPlayers[video_index].paused;
+ isScrubbing = true;
+ scrub(e);
+ document.addEventListener("mousemove", scrubWhileDragging);
+ document.addEventListener("mouseup", stopScrubbing);
+ document.addEventListener("touchmove", scrubWhileDragging);
+ document.addEventListener("touchend", stopScrubbing);
+ }
+
+ function scrubWhileDragging(e) {
+ if (isScrubbing) {
+ scrub(e);
+ }
+ }
+
+ function stopScrubbing() {
+ clearBoundingBoxes();
+ isScrubbing = false;
+ if (!wasPlayingBeforeScrub) {
+ videoPlayers[video_index].pause();
+ }
+ document.removeEventListener("mousemove", scrubWhileDragging);
+ document.removeEventListener("mouseup", stopScrubbing);
+ document.removeEventListener("touchmove", scrubWhileDragging);
+ document.removeEventListener("touchend", stopScrubbing);
+ }
+
+ function preloadNextSegments() {
+ console.log("preload start");
+ let x = 1;
+ while(x < videoPlayers.length){
+ let current_x = x;
+ if (currentSegment + x < segments.length) {
+ console.log(x);
+ if(!videoPlayers[(video_index + current_x) % videoPlayers.length].src.endsWith(segments[currentSegment + current_x].url)){
+ console.log(videoPlayers[(video_index + current_x) % videoPlayers.length].src);
+ console.log(segments[currentSegment + current_x].url);
+ videoPlayers[(video_index + current_x) % videoPlayers.length].src = segments[currentSegment + current_x].url;
+ videoPlayers[(video_index + current_x) % videoPlayers.length].load();
+ videoPlayers[(video_index + current_x) % videoPlayers.length].preload = 'auto';
+ } else{
+ console.log("dont need to load");
+ }
+ }
+ x++;
+ }
+ console.log("preload DONE");
+ }
+ let portrait = false;
+ function adjustContainerForOrientation(videoElement) {
+ const videoContainer = document.getElementById('videoContainer');
+
+ // Get the video's dimensions
+ const videoWidth = videoElement.videoWidth;
+ const videoHeight = videoElement.videoHeight;
+
+ // Determine if the video is in portrait mode
+ const isPortrait = videoHeight > videoWidth;
+
+ // Adjust the container's dimensions based on the video's orientation
+ if (isPortrait) {
+ portrait = true;
+ // Portrait mode: set container to 480x640
+ videoContainer.style.maxWidth = '480px';
+ videoContainer.style.aspectRatio = '9 / 16'; // 480:640 simplifies to 3:4
+ } else {
+ portrait = false;
+ // Landscape mode: set container to 640x480
+ videoContainer.style.maxWidth = '640px';
+ videoContainer.style.aspectRatio = '16 / 9'; // 640:480 simplifies to 4:3
+ }
+ }
+
+ function playSegment() {
+ if (segments.length > 0) {
+ const currentVideo = videoPlayers[video_index];
+ currentVideo.src = segments[currentSegment].url;
+ currentVideo.currentTime = 0;
+ currentVideo.style.display = "block";
+
+ // Hide other video players
+ for (let i = 1; i < videoPlayers.length; i++) {
+ videoPlayers[(video_index + i) % videoPlayers.length].style.display = "none";
+ }
+
+ // Play the video
+ currentVideo.play();
+ isPlaying = true;
+ playIcon.style.display = "none";
+ pauseIcon.style.display = "block";
+
+ // Adjust container dimensions based on video orientation
+ currentVideo.addEventListener('loadedmetadata', () => {
+ adjustContainerForOrientation(currentVideo);
+ });
+
+ preloadNextSegments();
+ }
+ }
+
+ function switchToNextSegment() {
+ if (currentSegment + 1 < segments.length) {
+ currentSegment++;
+ is_prepared[video_index] = false;
+ video_index = (video_index + 1) % videoPlayers.length;
+
+ // Hide other video players
+ for (let i = 1; i < videoPlayers.length; i++) {
+ videoPlayers[(video_index + i) % videoPlayers.length].style.display = "none";
+ }
+
+ const currentVideo = videoPlayers[video_index];
+ currentVideo.style.display = "block";
+
+ // Play the video
+ currentVideo.play().catch(() => {
+ retrySegment(currentVideo);
+ });
+
+ // Adjust container dimensions based on video orientation
+ currentVideo.addEventListener('loadedmetadata', () => {
+ adjustContainerForOrientation(currentVideo);
+ });
+
+ // Preload the next segments
+ preloadNextSegments();
+ } else {
+ isWaitingForNextSegment = true;
+ }
+ shape_idx = -1;
+ }
+
+ setInterval(updateTimeline, 50);
+
+ for(let i = 0; i < videoPlayers.length; i++) {
+ videoPlayers[i].addEventListener("timeupdate", updateTimeline);
+ videoPlayers[i].addEventListener("ended", switchToNextSegment);
+ videoPlayers[i].addEventListener("error", () => retrySegment(videoPlayers[i]));
+ }
+
+ progress.addEventListener("mousedown", startScrubbing);
+ progress.addEventListener("touchstart", startScrubbing);
+
+ setInterval(fetchSegments, 500);
+
+ const fullscreenBtn = document.getElementById('fullscreenBtn');
+ fullscreenBtn.addEventListener('click', () => {
+ clearBoundingBoxes(); //todo, redraw instead?
+ if (!document.fullscreenElement) {
+ if (videoContainer.requestFullscreen) {
+ videoContainer.requestFullscreen();
+ } else if (videoContainer.mozRequestFullScreen) { /* Firefox */
+ videoContainer.mozRequestFullScreen();
+ } else if (videoContainer.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
+ videoContainer.webkitRequestFullscreen();
+ } else if (videoContainer.msRequestFullscreen) { /* IE/Edge */
+ videoContainer.msRequestFullscreen();
+ }
+ } else {
+ if (document.exitFullscreen) {
+ document.exitFullscreen();
+ } else if (document.mozCancelFullScreen) { /* Firefox */
+ document.mozCancelFullScreen();
+ } else if (document.webkitExitFullscreen) { /* Chrome, Safari and Opera */
+ document.webkitExitFullscreen();
+ } else if (document.msExitFullscreen) { /* IE/Edge */
+ document.msExitFullscreen();
+ }
+ }
+ });
+
+ function isMobile() {
+ return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
+ }
+
+ if (isMobile()) {
+ document.getElementById('fullscreenBtn').style.display = 'none';
+ }
+
+
+ async function fetchSegmentsAndHandleVisibilityAndLoad() {
+ await fetchSegments();
+ handleVisibilityAndLoad();
+ }
+
+ function handleVisibilityAndLoad() { //todo, clean
+ let currentDate = "";
+ const dateParam = getQueryParam("date");
+ const fetchStartTime = new Date();
+ currentDate = formatDate(fetchStartTime);
+ if(currentDate == dateParam || !dateParam) {
+ if (isPlaying && segments.length > 0) {
+ currentSegment = segments.length - 1;
+ videoPlayers[video_index].src = segments[currentSegment].url;
+ videoPlayers[video_index].currentTime = segments[currentSegment].duration;
+ videoPlayers[video_index].play().catch(() => {});
+ }
+ }
+ }
+
+ // Handle visibility change
+ document.addEventListener("visibilitychange", async () => {
+ if (!document.hidden) {
+ await fetchSegmentsAndHandleVisibilityAndLoad();
+ }
+ });
+
+ // Handle page load/refresh
+ window.addEventListener("load", async () => {
+ await fetchSegmentsAndHandleVisibilityAndLoad();
+ });
+
+ function drawBoundingBox(relativeX, relativeY, relativeWidth, relativeHeight, color, label) {
+ const container = document.getElementById("videoContainer");
+ const video = container.querySelector("video");
+
+ const containerAspectRatio = container.clientWidth / container.clientHeight;
+ const videoAspectRatio = video.videoWidth / video.videoHeight;
+
+ let videoWidth, videoHeight, offsetX, offsetY;
+
+ if (containerAspectRatio > videoAspectRatio) {
+ // Video is pillarboxed
+ videoHeight = container.clientHeight;
+ videoWidth = videoHeight * videoAspectRatio;
+ offsetX = (container.clientWidth - videoWidth) / 2;
+ offsetY = 0;
+ } else {
+ // Video is letterboxed
+ videoWidth = container.clientWidth;
+ videoHeight = videoWidth / videoAspectRatio;
+ offsetX = 0;
+ offsetY = (container.clientHeight - videoHeight) / 2;
+ }
+
+ let boxWrapper = document.createElement("div");
+ boxWrapper.classList.add("bounding-box");
+
+ // Create the bounding box
+ let box = document.createElement("div");
+ box.style.position = "absolute";
+ box.style.left = `${offsetX + relativeX * videoWidth}px`;
+ box.style.top = `${offsetY + relativeY * videoHeight}px`;
+ box.style.width = `${relativeWidth * videoWidth}px`;
+ box.style.height = `${relativeHeight * videoHeight}px`;
+ box.style.border = `2px solid ${color}`;
+ box.style.boxSizing = "border-box";
+ box.style.pointerEvents = "none";
+
+ // Create the label (interactive)
+ let labelDiv = document.createElement("span");
+ labelDiv.textContent = label;
+ labelDiv.style.position = "absolute";
+ labelDiv.style.top = "-20px";
+ labelDiv.style.left = "0";
+ labelDiv.style.background = color;
+ labelDiv.style.color = "white";
+ labelDiv.style.padding = "2px 5px";
+ labelDiv.style.fontSize = "12px";
+ labelDiv.style.fontFamily = "Arial, sans-serif";
+ labelDiv.style.whiteSpace = "nowrap";
+ labelDiv.style.pointerEvents = "auto"; //Keeps label clickable if needed
+ box.style.zIndex = "1000";
+ labelDiv.style.zIndex = "1001";
+
+ box.appendChild(labelDiv);
+ boxWrapper.appendChild(box);
+ container.appendChild(boxWrapper);
+ }
+
+ function clearBoundingBoxes() {
+ document.querySelectorAll(".bounding-box").forEach(box => box.remove());
+ }
+
+ function hideBoundingBoxes() {
+ document.querySelectorAll(".bounding-box").forEach(box => box.style.display = "none");
+ }
+ function showBoundingBoxes() {
+ document.querySelectorAll(".bounding-box").forEach(box => box.style.display = "");
+ }
+ </script>
+</body>
+</html>
+
+