Skip to content

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

clean a bit

roryclear719293585905
diff --git a/clearcam/player.html b/clearcam/player.html
index 4f5c8d9..e5220dd 100644
--- a/clearcam/player.html
+++ b/clearcam/player.html
@@ -771,90 +771,6 @@
}
}
-
- async function fetchSegmentsInit() {
- if (isFetchingSegments) return;
- isFetchingSegments = true;
-
- try {
- const response = await fetch(`/get-segments?date=${currentDate}&start=${n_segments}`);
- if (response.ok) {
- const newSegments = await response.json();
- if (Array.isArray(newSegments)) {
- const oldTotalDuration = totalDuration;
-
- let cumulativeStart = totalDuration;
- let i = 0;
- let next_i = 1;
- while(next_i < newSegments.length) {
- if(newSegments[i].url.length == 0){
- i=next_i;
- next_i+=1;
- }
- while(newSegments[next_i].url.length == 0 && next_i < newSegments.length){
- next_i += 1;
- }
- if(next_i >= newSegments.length){
- break;
- }
- const segment = newSegments[i];
- const timeDifference = newSegments[next_i].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 > 1){ //todo why 60
- gaps.push([currentCumulativeStart + segment.duration,lostFrames]);
- }
- i=next_i;
- next_i+=1;
- }
- n_segments += i;
- totalDuration = segments[segments.length-1].duration + (segments[segments.length - 1].timeStamp - segments[0].timeStamp);
- if (totalDuration !== oldTotalDuration) {
- updateTimeline();
- //await preloadNextSegments();
-
- if (videoPlayers[video_index].currentTime + segments[currentSegment].cumulativeStart === 0 ||
- (isPlaying && isWaitingForNextSegment && segments.length < currentSegment + 1)) {
- 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";
- }
-
- currentVideo.play();
- isPlaying = true;
- playIcon.style.display = "none";
- pauseIcon.style.display = "block";
-
- // Adjust container dimensions based on video orientation
- currentVideo.addEventListener('loadedmetadata', () => {
- adjustContainerForOrientation(currentVideo);
- });
- }
- 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');
@@ -1095,6 +1011,86 @@
}
async function handleVisibilityAndLoad() { //todo, clean
+
+ if (isFetchingSegments) return;
+ isFetchingSegments = true;
+
+ try {
+ const response = await fetch(`/get-segments?date=${currentDate}&start=${n_segments}`);
+ if (response.ok) {
+ const newSegments = await response.json();
+ if (Array.isArray(newSegments)) {
+ const oldTotalDuration = totalDuration;
+
+ let cumulativeStart = totalDuration;
+ let i = 0;
+ let next_i = 1;
+ while(next_i < newSegments.length) {
+ if(newSegments[i].url.length == 0){
+ i=next_i;
+ next_i+=1;
+ }
+ while(newSegments[next_i].url.length == 0 && next_i < newSegments.length){
+ next_i += 1;
+ }
+ if(next_i >= newSegments.length){
+ break;
+ }
+ const segment = newSegments[i];
+ const timeDifference = newSegments[next_i].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 > 1){ //todo why 60
+ gaps.push([currentCumulativeStart + segment.duration,lostFrames]);
+ }
+ i=next_i;
+ next_i+=1;
+ }
+ n_segments += i;
+ totalDuration = segments[segments.length-1].duration + (segments[segments.length - 1].timeStamp - segments[0].timeStamp);
+ if (totalDuration !== oldTotalDuration) {
+ updateTimeline();
+ //await preloadNextSegments();
+
+ if (videoPlayers[video_index].currentTime + segments[currentSegment].cumulativeStart === 0 ||
+ (isPlaying && isWaitingForNextSegment && segments.length < currentSegment + 1)) {
+ 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";
+ }
+
+ currentVideo.play();
+ isPlaying = true;
+ playIcon.style.display = "none";
+ pauseIcon.style.display = "block";
+
+ // Adjust container dimensions based on video orientation
+ currentVideo.addEventListener('loadedmetadata', () => {
+ adjustContainerForOrientation(currentVideo);
+ });
+ }
+ if (isWaitingForNextSegment && segments.length > currentSegment + 1) {
+ isWaitingForNextSegment = false;
+ switchToNextSegment();
+ }
+ }
+ }
+ }
+ } catch (error) {
+ console.error('Failed to fetch segments:', error);
+ } finally {
+ isFetchingSegments = false;
let currentDate = "";
const dateParam = getQueryParam("date");
const fetchStartTime = new Date();
@@ -1147,18 +1143,17 @@
window.history.replaceState({}, document.title, window.location.pathname); //remove params
//alert(segments.length);
}
+ }
// Handle visibility change
document.addEventListener("visibilitychange", async () => {
if (!document.hidden) {
- await fetchSegmentsInit();
await handleVisibilityAndLoad();
}
});
// Handle page load/refresh
window.addEventListener("load", async () => {
- await fetchSegmentsInit();
await handleVisibilityAndLoad();
});