-
Notifications
You must be signed in to change notification settings - Fork 140
Add a buffer visualization #897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
257b9f2
Add buffer visualization and improve audio/video handling
kixelated 9f0332c
Fix some bugs n stuff.
kixelated 2fc6c7e
PR changes
kixelated 419385a
Fix some more incorrect .set usages.
kixelated 27092dc
Fix the MSE seeking.
kixelated File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import { Moq } from "@moq/hang"; | ||
| import type { BufferedRange } from "@moq/hang/watch"; | ||
| import { createMemo, createSignal, For, onCleanup, Show } from "solid-js"; | ||
| import useWatchUIContext from "../hooks/use-watch-ui"; | ||
|
|
||
| const MIN_RANGE = 0 as Moq.Time.Milli; | ||
| const RANGE_STEP = 100 as Moq.Time.Milli; | ||
|
|
||
| type BufferControlProps = { | ||
| /** Maximum buffer range in milliseconds (default: 5000ms = 5s) */ | ||
| max?: Moq.Time.Milli; | ||
| }; | ||
|
|
||
| export default function BufferControl(props: BufferControlProps) { | ||
| const context = useWatchUIContext(); | ||
| const maxRange = (): Moq.Time.Milli => props.max ?? (5000 as Moq.Time.Milli); | ||
| const [isDragging, setIsDragging] = createSignal(false); | ||
|
|
||
| // Compute range style and overflow info relative to current timestamp | ||
| const computeRange = (range: BufferedRange, timestamp: Moq.Time.Milli, color: string) => { | ||
| const startMs = (range.start - timestamp) as Moq.Time.Milli; | ||
| const endMs = (range.end - timestamp) as Moq.Time.Milli; | ||
| const visibleStartMs = Math.max(0, startMs) as Moq.Time.Milli; | ||
| const visibleEndMs = Math.min(endMs, maxRange()) as Moq.Time.Milli; | ||
| const leftPct = (visibleStartMs / maxRange()) * 100; | ||
| const widthPct = Math.max(0.5, ((visibleEndMs - visibleStartMs) / maxRange()) * 100); | ||
| const isOverflow = endMs > maxRange(); | ||
| const overflowSec = isOverflow | ||
| ? Moq.Time.Milli.toSecond((endMs - visibleStartMs) as Moq.Time.Milli).toFixed(1) | ||
| : null; | ||
| return { | ||
| style: `left: ${leftPct}%; width: ${widthPct}%; background: ${color};`, | ||
| isOverflow, | ||
| overflowSec, | ||
| }; | ||
| }; | ||
|
|
||
| // Determine color based on gap detection and buffering state | ||
| const rangeColor = (index: number, isBuffering: boolean) => { | ||
| if (isBuffering) return "#f87171"; // red | ||
| if (index > 0) return "#facc15"; // yellow | ||
| return "#4ade80"; // green | ||
| }; | ||
|
|
||
| const bufferTargetPct = createMemo(() => (context.jitter() / maxRange()) * 100); | ||
|
|
||
| // Handle mouse interaction to set buffer via clicking/dragging on the visualization | ||
| let containerRef: HTMLDivElement | undefined; | ||
|
|
||
| const LABEL_WIDTH = 48; // px reserved for track labels | ||
|
|
||
| const updateBufferFromMouseX = (clientX: number) => { | ||
| if (!containerRef) return; | ||
| const rect = containerRef.getBoundingClientRect(); | ||
| const trackWidth = rect.width - LABEL_WIDTH; | ||
| const x = Math.max(0, Math.min(clientX - rect.left - LABEL_WIDTH, trackWidth)); | ||
| const ms = (x / trackWidth) * maxRange(); | ||
| const snapped = (Math.round(ms / RANGE_STEP) * RANGE_STEP) as Moq.Time.Milli; | ||
| const clamped = Math.max(MIN_RANGE, Math.min(maxRange(), snapped)) as Moq.Time.Milli; | ||
| context.setJitter(clamped); | ||
| }; | ||
|
|
||
| const onMouseDown = (e: MouseEvent) => { | ||
| setIsDragging(true); | ||
| updateBufferFromMouseX(e.clientX); | ||
| document.addEventListener("mousemove", onMouseMove); | ||
| document.addEventListener("mouseup", onMouseUp); | ||
| }; | ||
|
|
||
| const onMouseMove = (e: MouseEvent) => { | ||
| if (isDragging()) { | ||
| updateBufferFromMouseX(e.clientX); | ||
| } | ||
| }; | ||
|
|
||
| const onMouseUp = () => { | ||
| setIsDragging(false); | ||
| document.removeEventListener("mousemove", onMouseMove); | ||
| document.removeEventListener("mouseup", onMouseUp); | ||
| }; | ||
|
|
||
| // Cleanup listeners on unmount | ||
| onCleanup(() => { | ||
| document.removeEventListener("mousemove", onMouseMove); | ||
| document.removeEventListener("mouseup", onMouseUp); | ||
| }); | ||
|
|
||
| return ( | ||
| <div class="buffer__container"> | ||
| {/* Buffer Visualization - interactive, click/drag to set buffer */} | ||
| <div | ||
| class={`buffer__visualization ${isDragging() ? "buffer__visualization--dragging" : ""}`} | ||
| ref={containerRef} | ||
| onMouseDown={onMouseDown} | ||
| role="slider" | ||
| tabIndex={0} | ||
| aria-valuenow={context.jitter()} | ||
| aria-valuemin={MIN_RANGE} | ||
| aria-valuemax={maxRange()} | ||
| aria-label="Buffer jitter" | ||
| > | ||
|
Comment on lines
88
to
101
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing keyboard support for slider accessibility. The element has ♿ Proposed fix for keyboard accessibility+ const onKeyDown = (e: KeyboardEvent) => {
+ const current = context.jitter();
+ let newValue = current;
+ if (e.key === "ArrowRight" || e.key === "ArrowUp") {
+ newValue = Math.min(maxRange(), current + RANGE_STEP) as Moq.Time.Milli;
+ } else if (e.key === "ArrowLeft" || e.key === "ArrowDown") {
+ newValue = Math.max(MIN_RANGE, current - RANGE_STEP) as Moq.Time.Milli;
+ } else {
+ return;
+ }
+ e.preventDefault();
+ context.setJitter(newValue);
+ };
return (
<div class="bufferControlContainer">
<div
class={`bufferVisualization ${isDragging() ? "dragging" : ""}`}
ref={containerRef}
onMouseDown={onMouseDown}
+ onKeyDown={onKeyDown}
role="slider" |
||
| {/* Playhead (left edge = current time) */} | ||
| <div class="buffer__playhead" /> | ||
|
|
||
| {/* Video buffer track */} | ||
| <div class="buffer__track buffer__track--video"> | ||
| <span class="buffer__track-label">Video</span> | ||
| <For each={context.videoBuffered()}> | ||
| {(range, i) => { | ||
| const info = () => { | ||
| const timestamp = context.timestamp(); | ||
| if (timestamp === undefined) return null; | ||
| return computeRange(range, timestamp, rangeColor(i(), context.buffering())); | ||
| }; | ||
| return ( | ||
| <Show when={info()}> | ||
| {(rangeInfo) => ( | ||
| <div class="buffer__range" style={rangeInfo().style}> | ||
| <Show when={rangeInfo().isOverflow}> | ||
| <span class="buffer__overflow-label">{rangeInfo().overflowSec}s</span> | ||
| </Show> | ||
| </div> | ||
| )} | ||
| </Show> | ||
| ); | ||
| }} | ||
| </For> | ||
| </div> | ||
|
|
||
| {/* Audio buffer track */} | ||
| <div class="buffer__track buffer__track--audio"> | ||
| <span class="buffer__track-label">Audio</span> | ||
| <For each={context.audioBuffered()}> | ||
| {(range, i) => { | ||
| const info = () => { | ||
| const timestamp = context.timestamp(); | ||
| if (timestamp === undefined) return null; | ||
| return computeRange(range, timestamp, rangeColor(i(), context.buffering())); | ||
| }; | ||
| return ( | ||
| <Show when={info()}> | ||
| {(rangeInfo) => ( | ||
| <div class="buffer__range" style={rangeInfo().style}> | ||
| <Show when={rangeInfo().isOverflow}> | ||
| <span class="buffer__overflow-label">{rangeInfo().overflowSec}s</span> | ||
| </Show> | ||
| </div> | ||
| )} | ||
| </Show> | ||
| ); | ||
| }} | ||
| </For> | ||
| </div> | ||
|
|
||
| {/* Buffer target line (draggable) - wrapped in track-area container */} | ||
| <div class="buffer__target-area"> | ||
| <div class="buffer__target-line" style={{ left: `${bufferTargetPct()}%` }}> | ||
| <span class="buffer__target-label">{`${Math.round(context.jitter())}ms`}</span> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing cleanup for document event listeners on component unmount.
If the component unmounts while the user is dragging (e.g., navigation or conditional rendering), the
mousemoveandmouseuplisteners will remain attached to the document. Consider using SolidJS'sonCleanupto remove listeners when the component unmounts.🛠️ Proposed fix
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jdreetz, great! Adding the event listener cleanup will prevent memory leaks and other issues if the component unmounts during a drag.Would you like me to help implement the change? I can prepare the code to add
onCleanupthat removes the document event listeners when the component unmounts.