UNPKG

media-stream-library

Version:

Media stream library for Node & the Web.

81 lines (80 loc) 2.69 kB
export interface Clock { readonly currentTime: number; readonly play: () => void; readonly pause: () => void; } /** * A scheduler that can decide when to execute a certain * timestamped callback so that it happens in sync with a video * element. * * To use it: * * (1) Initialize a new Scheduler with a clock (to synchronize * against) and a callback (to be called when a message is in * sync with the video). The clock can be a HTMLVideoElement, * or anything that has a `currentTime` property which gives * the current presentation time in seconds, and a `pause` and * `play` method to control playback. * * (2) Call the `run` method every time a new message arrives * that you want to schedule (it needs to have an ntpTimestamp). * As soon at the presentation time is known, call the `init` * method and pass in that time, so that the scheduler can * start to schedule the callbacks. From then on, whenever * a message in the queue has a timestamp that matches the * current presentation time of the video, your callback will * fire. */ export declare class Scheduler<T extends { readonly ntpTimestamp?: number; }> { private readonly _clock; private readonly _handler; private readonly _tolerance; private _nextRun; private _nextPlay; private _fifo; private _ntpPresentationTime; private _suspended; /** * Creates an instance of Scheduler. * @param clock - The clock to use (so we can control playback) * @param handler - The callback to invoke when a message is in sync * @param tolerance - The milliseconds defining "in sync" (default = 10) */ constructor(clock: Clock, handler: (msg: T) => void, tolerance?: number); /** * Bring the scheduler back to it's initial state. */ reset(): void; /** * Initialize the scheduler. * * @param ntpPresentationTime - The offset representing the start of the presentation */ init(ntpPresentationTime: number): void; /** * Suspend the scheduler. * * This releases control of the clock and stops any scheduling activity. * Note that this doesn't mean the clock will be in a particular state * (could be started or stopped), just that the scheduler will no longer * control it. */ suspend(): void; /** * Resume the scheduler. * * This gives back control of the clock and the ability * to schedule messages. The scheduler will immediately * try to do that on resume. */ resume(): void; /** * Run the scheduler. * * @param newMessage - New message to schedule. */ run(newMessage?: T): void; }