Home Information - Your home, your data, organized.
Python
Add replay-from-start affordance for video recordings (#331)
Tony C2517928531bd
diff --git a/src/hi/apps/alert/templates/alert/modals/alert_details.html b/src/hi/apps/alert/templates/alert/modals/alert_details.htmlindex a639be58..de796a51 100644--- a/src/hi/apps/alert/templates/alert/modals/alert_details.html+++ b/src/hi/apps/alert/templates/alert/modals/alert_details.html@@ -59,7 +59,10 @@</h6></div><div class="card-body p-2">- <img class="img-fluid" data-video-stream src="{{ video_stream.source_url }}" alt="{{ alert_visual_content.alarm.title }}">+ <div class="hi-video-recording">+ <img class="img-fluid" data-video-recording src="{{ video_stream.source_url }}" alt="{{ alert_visual_content.alarm.title }}">+ {% include "console/panes/video_recording_replay_button.html" %}+ </div>{% if not alert_visual_content.is_from_latest %}<small class="text-muted d-block mt-1">From: {{ alert_visual_content.alarm.timestamp|localtime|date:"M j, Y H:i" }}</small>{% endif %}@@ -113,7 +116,10 @@<small class="text-muted">Source {{ video_source.index }} - {{ video_source.alarm.timestamp|localtime|date:"M j H:i" }}</small></div><div class="card-body p-2">- <img class="img-fluid" data-video-stream src="{{ video_stream.source_url }}" alt="Source {{ video_source.index }}">+ <div class="hi-video-recording">+ <img class="img-fluid" data-video-recording src="{{ video_stream.source_url }}" alt="Source {{ video_source.index }}">+ {% include "console/panes/video_recording_replay_button.html" %}+ </div></div></div>{% endif %}diff --git a/src/hi/apps/alert/templates/alert/panes/alarm_sensor_response.html b/src/hi/apps/alert/templates/alert/panes/alarm_sensor_response.htmlindex 0131300c..770d4009 100644--- a/src/hi/apps/alert/templates/alert/panes/alarm_sensor_response.html+++ b/src/hi/apps/alert/templates/alert/panes/alarm_sensor_response.html@@ -5,7 +5,10 @@{% sensor_response_video_stream sensor_response as video_stream %}{% if video_stream and video_stream.source_url %}<div class="mb-2">- <img class="img-fluid" data-video-stream src="{{ video_stream.source_url }}" alt="{{ alarm.title }}">+ <div class="hi-video-recording">+ <img class="img-fluid" data-video-recording src="{{ video_stream.source_url }}" alt="{{ alarm.title }}">+ {% include "console/panes/video_recording_replay_button.html" %}+ </div></div>{% elif sensor_response.source_image_url %}<!-- Fallback to image when video stream is not available -->diff --git a/src/hi/apps/console/templates/console/panes/entity_video_sensor_history.html b/src/hi/apps/console/templates/console/panes/entity_video_sensor_history.htmlindex 3fdc64aa..38eb3842 100644--- a/src/hi/apps/console/templates/console/panes/entity_video_sensor_history.html+++ b/src/hi/apps/console/templates/console/panes/entity_video_sensor_history.html@@ -115,7 +115,10 @@<div class="video-container mb-3" role="img" aria-label="Event video playback">{% sensor_response_video_stream sensor_history_data.current_sensor_response as video_stream %}{% if video_stream and video_stream.source_url %}- <img class="img-fluid" data-video-stream src="{{ video_stream.source_url }}" alt="Video recording from {{ sensor_history_data.current_sensor_response.timestamp|localtime|date:'F j, Y g:i A' }}" data-event-id="{{ sensor_history_data.current_sensor_response.sensor_history_id }}" onerror="this.style.display='none'; this.outerHTML='<div class=\'alert alert-warning text-center d-flex align-items-center justify-content-center\' style=\'min-height: 240px;\'><div><div class=\'mb-2\'>[VIDEO]</div><h6>Video No Longer Available</h6></div></div>'">+ <div class="hi-video-recording">+ <img class="img-fluid" data-video-recording src="{{ video_stream.source_url }}" alt="Video recording from {{ sensor_history_data.current_sensor_response.timestamp|localtime|date:'F j, Y g:i A' }}" data-event-id="{{ sensor_history_data.current_sensor_response.sensor_history_id }}" onerror="this.style.display='none'; this.outerHTML='<div class=\'alert alert-warning text-center d-flex align-items-center justify-content-center\' style=\'min-height: 240px;\'><div><div class=\'mb-2\'>[VIDEO]</div><h6>Video No Longer Available</h6></div></div>'">+ {% include "console/panes/video_recording_replay_button.html" %}+ </div>{% elif sensor_history_data.current_sensor_response.source_image_url %}<!-- Fallback to image when video stream is not available --><img class="img-fluid" src="{{ sensor_history_data.current_sensor_response.source_image_url }}" alt="Image from {{ sensor_history_data.current_sensor_response.timestamp|localtime|date:'F j, Y g:i A' }}" data-event-id="{{ sensor_history_data.current_sensor_response.sensor_history_id }}">diff --git a/src/hi/apps/console/templates/console/panes/video_recording_replay_button.html b/src/hi/apps/console/templates/console/panes/video_recording_replay_button.htmlnew file mode 100644index 00000000..0103c35c--- /dev/null+++ b/src/hi/apps/console/templates/console/panes/video_recording_replay_button.html@@ -0,0 +1,11 @@+{% load icons %}+{# Replay-from-start affordance for a ``<img data-video-recording>`` #}+{# element. Expected to sit inside a ``.hi-video-recording`` wrapper #}+{# alongside the img; the wrapper provides ``position: relative`` so #}+{# this button anchors to the top-right of the video frame. The #}+{# click handler (delegated in video-timeline.js) re-fetches the #}+{# img's URL to play the recording from the start. #}+<button type="button" class="hi-video-recording-replay"+ title="Replay from start" aria-label="Replay from start">+ {% icon "rotate" size="sm" %}+</button>diff --git a/src/hi/apps/sense/templates/sense/modals/sensor_history_details.html b/src/hi/apps/sense/templates/sense/modals/sensor_history_details.htmlindex 5ab2a56c..7a5591ce 100644--- a/src/hi/apps/sense/templates/sense/modals/sensor_history_details.html+++ b/src/hi/apps/sense/templates/sense/modals/sensor_history_details.html@@ -13,7 +13,10 @@ Sensor Response Details{% sensor_response_video_stream sensor_response as video_stream %}{% if video_stream and video_stream.source_url %}<div>- <img class="img-fluid" data-video-stream src="{{ video_stream.source_url }}" alt="Sensor History">+ <div class="hi-video-recording">+ <img class="img-fluid" data-video-recording src="{{ video_stream.source_url }}" alt="Sensor History">+ {% include "console/panes/video_recording_replay_button.html" %}+ </div></div>{% elif sensor_response.source_image_url %}<!-- Fallback to image when video stream is not available -->diff --git a/src/hi/static/css/main.css b/src/hi/static/css/main.cssindex 42d8a0eb..cf03e065 100644--- a/src/hi/static/css/main.css+++ b/src/hi/static/css/main.css@@ -2241,3 +2241,70 @@ g[hover] {max-height: 200px;overflow-y: auto;}++/* Wrapper around a finite-recording <img> + its replay-from-start+ overlay button. ``position: relative`` anchors the button to the+ image's top-right; ``display: block`` and ``line-height: 0`` let+ the wrapper shrink to the image's actual rendered dimensions so+ the overlay sits on the image, not on padding around it. */+.hi-video-recording {+ position: relative;+ display: block;+ line-height: 0;+}++/* Replay-from-start overlay button. Always visible (touch screens+ don't reliably support hover-reveal), semi-transparent backdrop+ so it reads against any video content underneath. */+.hi-video-recording-replay {+ position: absolute;+ top: 8px;+ right: 8px;+ width: 32px;+ height: 32px;+ padding: 0;+ border: 0;+ border-radius: 50%;+ background: rgba(0, 0, 0, 0.55);+ color: #ffffff;+ display: inline-flex;+ align-items: center;+ justify-content: center;+ cursor: pointer;+ opacity: 0.85;+ transition: opacity 0.15s ease, background-color 0.15s ease,+ transform 0.05s ease;+ line-height: 1;+ z-index: 1;++ /* Touch-friendly behavior on tablets / phones. */+ touch-action: manipulation; /* no 300ms tap delay */+ -webkit-tap-highlight-color: transparent; /* suppress iOS gray flash */+ -webkit-touch-callout: none; /* no long-press callout */+ user-select: none;+}++/* Extend the hit area outward to ~48x48 without changing the visual+ size of the button — meets the WCAG 2.5.5 / Apple HIG / Material+ touch-target guidance while keeping the icon compact on the video+ frame. Pseudo-element shares the button's pointer-events, so taps+ in the extended area dispatch the click handler. */+.hi-video-recording-replay::before {+ content: '';+ position: absolute;+ top: -8px;+ left: -8px;+ right: -8px;+ bottom: -8px;+}++.hi-video-recording-replay:hover,+.hi-video-recording-replay:focus {+ opacity: 1;+ background: rgba(0, 0, 0, 0.75);+ outline: none;+}+.hi-video-recording-replay:active {+ background: rgba(0, 0, 0, 0.9);+ transform: scale(0.92);+}diff --git a/src/hi/static/js/video-timeline.js b/src/hi/static/js/video-timeline.jsindex ec18204f..361822f4 100644--- a/src/hi/static/js/video-timeline.js+++ b/src/hi/static/js/video-timeline.js@@ -8,12 +8,19 @@const TRANSPARENT_GIF_SRC ='data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';+ // The connection manager treats both markers as the same kind of+ // long-lived MJPEG fetch — both need explicit cleanup on DOM+ // removal so browser per-host connection slots are released. The+ // markers themselves are disjoint by content type:+ // - ``data-video-stream`` : continuous live MJPEG (camera).+ // - ``data-video-recording`` : finite recorded MJPEG (event playback).+ // The replay click handler at the bottom of this file applies+ // only to the recording case.+ const LONG_LIVED_VIDEO_SELECTOR =+ 'img[data-video-stream], img[data-video-recording]';+// Tracks every long-lived video connection currently attached to- // the document. An <img> opts in by carrying ``data-video-stream``;- // both live MJPEG streams and event-video playback elements need- // this because surveillance users typically browse between events- // faster than playback completes, leaving recorded-video fetches- // holding browser per-host connection slots if not closed.+ // the document.//// Responsibilities split with antinode:// - Antinode fires ``beforeAsyncRender($target)`` immediately@@ -59,7 +66,7 @@closeWithin: function( $scope ) {if ( ! $scope || ! $scope.find ) return;const manager = this;- $scope.find( 'img[data-video-stream]' ).each(function() {+ $scope.find( LONG_LIVED_VIDEO_SELECTOR ).each(function() {manager.forceClose( this );});},@@ -72,7 +79,7 @@this.streams.delete( element );}}- document.querySelectorAll( 'img[data-video-stream]' ).forEach(+ document.querySelectorAll( LONG_LIVED_VIDEO_SELECTOR ).forEach((el) => this.register( el ));},@@ -179,7 +186,7 @@// Tag the current video element with its event id for// debug visibility in console logs / DOM inspection. The// connection manager handles registration itself via the- // ``data-video-stream`` marker.+ // ``data-video-stream`` / ``data-video-recording`` markers.this.tagCurrentVideoWithEventId();},@@ -246,6 +253,43 @@VideoConnectionManager.cleanup();});+ // Replay-from-start for finite recordings. Templates wrap each+ // ``[data-video-recording]`` <img> in a ``.hi-video-recording``+ // container that also holds a ``.hi-video-recording-replay``+ // button. Delegated on body so async-loaded fragments work+ // without an init pass.+ //+ // Mechanism: append a fresh ``_replay`` query parameter to the+ // cached original URL on each click. The browser sees a new URL,+ // abandons the previous fetch, and starts a new one. ZoneMinder+ // serves the recording from the start on each request+ // (``replay=single``). Avoids ever blanking the ``src`` —+ // some templates carry inline ``onerror`` handlers that fire+ // on empty src and replace the <img> with an error message,+ // which the empty-src + reset technique would trigger.+ function videoRecordingReplayBuster( baseUrl ) {+ const sep = baseUrl.includes('?') ? '&' : '?';+ return baseUrl + sep + '_replay=' + Date.now();+ }+ jQuery(function($) {+ $( 'body' ).on( 'click', '.hi-video-recording-replay', function( ev ) {+ ev.preventDefault();+ ev.stopPropagation();+ const wrapper = this.closest( '.hi-video-recording' );+ const img = wrapper && wrapper.querySelector( 'img[data-video-recording]' );+ if ( ! img ) return;+ // Cache the original URL on first click. Subsequent+ // clicks always rebuild from this cached base so the+ // ``_replay`` parameter doesn't stack.+ if ( ! img.dataset.videoRecordingSrc ) {+ img.dataset.videoRecordingSrc = img.src;+ }+ const baseUrl = img.dataset.videoRecordingSrc;+ if ( ! baseUrl || baseUrl.startsWith('data:') ) return;+ img.src = videoRecordingReplayBuster( baseUrl );+ });+ });+// Expose for potential external use and debuggingwindow.VideoTimelineScrollManager = VideoTimelineScrollManager;diff --git a/src/hi/templates/service-worker.js b/src/hi/templates/service-worker.jsindex 3a507371..1b78c3b4 100644--- a/src/hi/templates/service-worker.js+++ b/src/hi/templates/service-worker.js@@ -1,37 +1,24 @@-// Static-asset cache + pass-through for everything else.+// Pass-through service worker.//-// The service worker intercepts only requests whose path begins with-// Django's STATIC_URL. Cache-first with on-demand population on miss.-// Everything else — dynamic pages, the polling endpoint, MJPEG video-// streams (multipart/x-mixed-replace) — passes through to the-// browser's native fetch path. Firefox in particular mishandles-// multiple concurrent multipart streams routed through a service-// worker's respondWith, so this scoping is deliberate.--var CACHE_VERSION = 2;-var STATIC_CACHE_NAME = 'static-cache-' + CACHE_VERSION;-var STATIC_URL_PREFIX = '{{ STATIC_URL }}';+// Registered solely to satisfy the installable-PWA criteria+// (manifest + active SW with a fetch handler). No caching: the+// browser's HTTP cache handles repeat fetches for /static/ assets,+// and adding a SW cache layer on top introduced stale-content bugs+// every time a static file changed (cache key is the URL, which+// doesn't change between deploys).self.addEventListener('install', function(event) {- // Take over immediately rather than waiting for all existing- // controlled pages to close. Combined with ``clients.claim()`` on- // activate, this ensures the new SW handles fetches as soon as it- // is installed.event.waitUntil(self.skipWaiting());});self.addEventListener('activate', function(event) {- // Sweep any caches that don't match the current version. Bumping- // CACHE_VERSION invalidates everything previously cached.+ // Sweep every cache this SW has ever created so users upgrading+ // from a caching version don't keep serving stale content.event.waitUntil(caches.keys().then(function(cacheNames) {- return Promise.all(- cacheNames.map(function(name) {- if (name !== STATIC_CACHE_NAME) {- return caches.delete(name);- }- })- );+ return Promise.all(cacheNames.map(function(name) {+ return caches.delete(name);+ }));}).then(function() {return self.clients.claim();})@@ -39,30 +26,7 @@ self.addEventListener('activate', function(event) {});self.addEventListener('fetch', function(event) {- if (event.request.method !== 'GET') {- return;- }- if (!event.request.url.startsWith('http')) {- return;- }- var pathname = new URL(event.request.url).pathname;- if (!pathname.startsWith(STATIC_URL_PREFIX)) {- return;- }- event.respondWith(- caches.match(event.request).then(function(cached) {- if (cached) {- return cached;- }- return fetch(event.request).then(function(response) {- if (response && response.ok) {- var clone = response.clone();- caches.open(STATIC_CACHE_NAME).then(function(cache) {- cache.put(event.request, clone);- });- }- return response;- });- })- );+ // Intentional no-op: let the browser handle every request+ // natively. Present only to satisfy PWA installability checks+ // that look for a fetch handler on the active SW.});