timingsrc

A library to synchronize a MediaElement with a TimingObject.

Usage no npm install needed!

<script type="module">
  import timingsrc from 'https://cdn.skypack.dev/timingsrc';
</script>

README

timingsrc

A library to synchronize a MediaElement with a TimingObject.

dependencies version

This libary is meant to synchronize a MediaElement (aka an <audio/> or <video/> element) with a TimingObject.

The Timing Object specification defines an extension of the MediaElement which would add timingsrc property. But so far this is not supported in any browser. Therefore this package can be used as a replacement as it provides the same functionality. But instead of patching the native protoypes this libary provides a function which can be used without modifying any built-in browser objects.

The code of this library is heavily inspired by the mediaSync() function that came with v1 of the timingsrc library. However all the code has been completely rewritten in TypeScript with the goal to make it more testable and easier to reason about.

The actual synchronization is handled by two different algorithms. By default the userAgent string gets parsed to determine which algorithm to use. Since Safari is not capable of processing changes of the currentTime or the plackbackRate of a MediaElement in a timely manner the position in Safari is set abbruptly whenever it changes. Firefox and Chromium based browsers happily handle frequent changes of both properties which is why they are adjusted gradually to keep a MediaElement in sync with a TimingObject in these browsers. The technique for doing so has been taken from Tom Jenkinon's media-element-syncer.

Usage

The timingsrc package is published on npm and can be installed as usual.

npm install timingsrc

The main function exported by this package is the setTimingsrc() function. It can be thought of as the replacement of the timingsrc property on a MediaElement. It can for example be used with a TimingObject created with the timing-object package.

import { TimingObject } from 'timing-object';
import { setTimingsrc } from 'timingsrc';

const mediaElement = document.getElementsByTagName('video')[0];
const timingObject = new TimingObject();

const deleteTimingsrc = setTimingsrc(mediaElement, timingObject);

// The synchronization can be stopped again at any point in time.
deleteTimingsrc();

It is also possible to configure a custom version of the setTimingsrc() function. The following would build a setTimingSrc() function which does not include the fallback for stepwise updates which is necessary in Safari.

import { createSetTimingsrc, createUpdateGradually, setTimingsrcWithCustomUpdateFunction } from 'timingsrc';

const customSetTimingsrc = createSetTimingsrc(setTimingsrcWithCustomUpdateFunction, createUpdateGradually(0.5, 1, 0.025));

Please take a look at the video-synchronization-demo for a more comprehensive example.