diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index cb35fe2..0ca48ca 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -6,13 +6,16 @@ on: pull_request: branches: [main, master] schedule: - # Weekly, so advisories published after the last push still get reported. + # Daily, for the Trivy job, so advisories published after the last push still get reported. + - cron: '23 5 * * *' + # Weekly, for the secrets and code jobs. Each job's if: condition picks the entry that runs it. - cron: '23 5 * * 1' workflow_dispatch: {} jobs: secrets: name: Secrets (gitleaks) + if: github.event_name != 'schedule' || github.event.schedule == '23 5 * * 1' runs-on: ubuntu-latest permissions: contents: read @@ -35,6 +38,7 @@ jobs: code: name: Code (Semgrep) + if: github.event_name != 'schedule' || github.event.schedule == '23 5 * * 1' runs-on: ubuntu-latest permissions: contents: read @@ -55,6 +59,7 @@ jobs: dependencies: name: Dependencies (Trivy) + if: github.event_name != 'schedule' || github.event.schedule == '23 5 * * *' runs-on: ubuntu-latest permissions: contents: read diff --git a/README.md b/README.md index c708ace..52a3b4d 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ These are the complete Svelte component exports from `packages/web-ui/src/lib/in | Component | Includes | |---|---| -| `MediaPlayer` | Audio/video player with playback, seeking, volume, quality, fullscreen, keyboard controls, and custom end controls. | +| `MediaPlayer` | Audio/video player with playback, seeking, volume, speed, quality, fullscreen, keyboard controls, custom end controls, and optional resume from the saved position (`storageKey`, per app). With `controls={false}` it is just the element, returned through `element`, for apps that drive playback themselves and set `src` on it. | #### Accounts and access diff --git a/packages/web-ui/src/lib/components/MediaPlayer.svelte b/packages/web-ui/src/lib/components/MediaPlayer.svelte index 1a8bc76..7dc0ee9 100644 --- a/packages/web-ui/src/lib/components/MediaPlayer.svelte +++ b/packages/web-ui/src/lib/components/MediaPlayer.svelte @@ -9,7 +9,7 @@ let { kind = 'video', - src = '', + src, poster = '', title = '', autoplay = false, @@ -35,6 +35,12 @@ onSkip, onQualityChange, onToggleGraph, + storageKey = '', + showSpeed = true, + controls = true, + element = $bindable(null), + preload = 'auto', + crossorigin, endControls, }: { kind?: MediaPlayerKind @@ -66,6 +72,24 @@ onSkip?: (seconds: number) => void onQualityChange?: (value: string) => void onToggleGraph?: () => void + /** + * Turns on resume: the position of each source is saved in localStorage under this prefix and + * restored when the source loads. Give each app its own prefix. Empty (the default) keeps + * playback position out of browser storage. + */ + storageKey?: string + /** Offers the playback speed in the settings menu. */ + showSpeed?: boolean + /** + * False renders the element with no player controls, surface clicks, keyboard handling or + * fullscreen tracking. The caller then drives playback itself, through `element`, and may set + * the element's `src` directly: this component leaves `src` alone when none is passed. + */ + controls?: boolean + /** The media element, once mounted. Cleared when it goes. */ + element?: HTMLMediaElement | null + preload?: 'none' | 'metadata' | 'auto' + crossorigin?: 'anonymous' | 'use-credentials' endControls?: Snippet } = $props() @@ -81,12 +105,19 @@ let orientationLocked = $state(false) let controlsHideTimeout: ReturnType | null = null let orientationLockRequest = 0 + let playbackRate = $state(1) + let lastProgressSaveAt = 0 + const PROGRESS_SAVE_INTERVAL_MS = 1500 const currentTime = $derived(externalCurrentTime ?? internalTime) const duration = $derived(externalDuration ?? internalDuration) const audio = $derived(kind === 'audio') const controlsHideDelay = 2500 + $effect(() => { + element = mediaElement + }) + function clearControlsHideTimer() { if (controlsHideTimeout === null) return clearTimeout(controlsHideTimeout) @@ -195,6 +226,65 @@ skip(event.clientX - bounds.left < bounds.width / 2 ? -10 : 10) } + function progressKey() { + return `${storageKey}:${src}` + } + + function restorePlaybackProgress() { + if (!storageKey || !mediaElement) return + try { + const savedProgress = Number(window.localStorage.getItem(progressKey())) + const length = mediaElement.duration + if ( + Number.isFinite(savedProgress) && + savedProgress > 0 && + Number.isFinite(length) && + length > 0 && + savedProgress < length - 1 + ) { + mediaElement.currentTime = savedProgress + } + } catch { + // Playback remains available when browser storage is disabled or full. + } + } + + function savePlaybackProgress(force = false) { + if (!storageKey || !mediaElement || mediaElement.ended) return + const position = mediaElement.currentTime + if (!Number.isFinite(position)) return + try { + if (position <= 0) { + if (force) window.localStorage.removeItem(progressKey()) + return + } + if (!force && Date.now() - lastProgressSaveAt < PROGRESS_SAVE_INTERVAL_MS) return + lastProgressSaveAt = Date.now() + window.localStorage.setItem(progressKey(), String(position)) + } catch { + // Keep playing when browser storage is disabled or full. + } + } + + function clearPlaybackProgress() { + if (!storageKey) return + try { + window.localStorage.removeItem(progressKey()) + } catch { + // Keep playback completion independent from browser storage. + } + } + + function changeSpeed(value: number) { + if (!Number.isFinite(value) || value <= 0) return + playbackRate = value + if (mediaElement) mediaElement.playbackRate = value + } + + function handleSeeked() { + savePlaybackProgress(true) + } + function handlePlay(event: Event) { playing = true onPlay?.(event) @@ -203,11 +293,13 @@ function handlePause(event: Event) { playing = false controlsVisible = true + savePlaybackProgress(true) onPause?.(event) } function handleTimeUpdate(event: Event) { if (mediaElement) internalTime = mediaElement.currentTime || 0 + savePlaybackProgress() onTimeUpdate?.(event) } @@ -216,13 +308,17 @@ internalDuration = Number.isFinite(mediaElement.duration) ? mediaElement.duration : 0 volume = mediaElement.volume muted = mediaElement.muted || mediaElement.volume === 0 - void syncScreenOrientation(fullscreen) + // Loading a source resets the rate, so the chosen speed is applied again. + mediaElement.playbackRate = playbackRate + restorePlaybackProgress() + if (controls) void syncScreenOrientation(fullscreen) onLoadedMetadata?.(event) } function handleEnded(event: Event) { playing = false controlsVisible = true + clearPlaybackProgress() onEnded?.(event) } @@ -287,6 +383,7 @@ } function handleFullscreenChange() { + if (!controls) return fullscreen = Boolean(document.fullscreenElement) void syncScreenOrientation(fullscreen) } @@ -311,7 +408,7 @@ }) onMount(() => { - if (!playerFrame) return + if (!controls || !playerFrame) return const activityEvents = ['mousemove', 'click', 'keydown', 'focusin', 'touchstart'] as const for (const eventName of activityEvents) playerFrame.addEventListener(eventName, handlePlayerActivity) @@ -329,15 +426,15 @@ -
+
{#if audio}
{#if title}{title}{/if} @@ -346,7 +443,7 @@ bind:this={mediaElement} {src} {autoplay} - preload="auto" + {preload} tabindex="-1" aria-hidden="true" onplay={handlePlay} @@ -355,6 +452,7 @@ onloadedmetadata={handleLoadedMetadata} onended={handleEnded} onvolumechange={handleVolumeChange} + onseeked={handleSeeked} onerror={handleError} > {:else} @@ -366,25 +464,28 @@ {poster} {autoplay} muted={autoplay && autoplayMuted} - preload="auto" + {preload} + {crossorigin} playsinline - tabindex="0" - class={`media-player-video ${mediaClass}`} + tabindex={controls ? 0 : undefined} + class={[controls && 'media-player-video', mediaClass].filter(Boolean).join(' ')} aria-label={title ? `Video player for ${title}` : 'Video player'} - onclick={handleSurfaceClick} - ondblclick={handleVideoDoubleTap} - onkeydown={handleKeydown} + onclick={controls ? handleSurfaceClick : undefined} + ondblclick={controls ? handleVideoDoubleTap : undefined} + onkeydown={controls ? handleKeydown : undefined} onplay={handlePlay} onpause={handlePause} ontimeupdate={handleTimeUpdate} onloadedmetadata={handleLoadedMetadata} onended={handleEnded} onvolumechange={handleVolumeChange} + onseeked={handleSeeked} onerror={handleError} > {/if} + {#if controls} + {/if}