refactor(mediabunny): [PKG-MB-04] checkpoint typed shim and event cleanup

This commit is contained in:
Harvey Zhao committed 2026-09-12 18:27:33 +08:00
1 parent adba8a3d12
commit 9af253a5a9
24 files changed
+1478 -250

No files matched your search

@@ -0,0 +1,61 @@
import type { EnginePort } from './engine-types'
interface FrameOwner {
engine: Pick<EnginePort, 'currentTime' | 'videoWidth' | 'videoHeight'>
}
interface FrameState {
closed: boolean
frames: Set<number>
}
// Keep resource bookkeeping off the observable shim own-property/canvas surface.
const states = new WeakMap<FrameOwner, FrameState>()
function state(owner: FrameOwner): FrameState {
let value = states.get(owner)
if (!value) {
value = { closed: false, frames: new Set() }
states.set(owner, value)
}
return value
}
export function requestFrame(owner: FrameOwner, callback: VideoFrameRequestCallback): number {
const resource = state(owner)
if (resource.closed)
return 0
const id = requestAnimationFrame((time) => {
resource.frames.delete(id)
if (resource.closed)
return
callback(time, {
presentationTime: owner.engine.currentTime,
expectedDisplayTime: time + 16.6,
width: owner.engine.videoWidth,
height: owner.engine.videoHeight,
mediaTime: owner.engine.currentTime,
presentedFrames: 0,
processingDuration: 0,
captureTime: time,
receiveTime: time,
rtpTimestamp: 0,
})
})
resource.frames.add(id)
return id
}
export function cancelFrame(owner: FrameOwner, id: number): void {
state(owner).frames.delete(id)
cancelAnimationFrame(id)
}
export function closeFrames(owner: FrameOwner): boolean {
const resource = state(owner)
if (resource.closed)
return false
resource.closed = true
for (const id of resource.frames) cancelAnimationFrame(id)
resource.frames.clear()
return true
}