Fake W3C MediaStreamTrack implementation. Suitable for for Node.js or testing.
This library is intended for Node.js applications or libraries that depend on the MediaStreamTrack
class. The exposed FakeMediaStreamTrack
class does not internally manage any audio/video source.
This library provides TypeScript definitions and the FakeMediaStreamTrack
extendes the MediaStreamTrack
interface, so it can be safely used in any code requiring a MediaStreamTrack
object.
$ npm install fake-mediastreamtrack
const { FakeMediaStreamTrack } = require('fake-mediastreamtrack');
// or
import { FakeMediaStreamTrack } from 'fake-mediastreamtrack';
const track = new FakeMediaStreamTrack({ kind: 'audio' });
track.enabled = false;
console.log(
'track.readyState: %s, track.enabled: %s', track.readyState, track.enabled
);
// => 'track.readyState: live, track.enabled: false'
const clonedTrack = track.clone();
track.stop();
console.log('track.readyState: %s', track.readyState);
// => 'track.readyState: ended'
console.log('clonedTrack.readyState: %s', clonedTrack.readyState);
// => 'clonedTrack.readyState: live'
clonedTrack.applyConstraints({ frameRate: { max: 30, ideal: 20 } });
console.log('clonedTrack.getConstraints(): %o', clonedTrack.getConstraints());
// => clonedTrack.getConstraints(): { frameRate: { max: 30, ideal: 20 } }
The FakeMediaStreamTrack
class constructor accepts an object with the following fields.
const track = new FakeMediaStreamTrack({ kind, id, label, isolated, muted, data })
kind
(string, mandatory): "audio" or "video".id
(string, optional): Track unique identificator. If not given, a random one is generated.label
(string, optional): Track label. Defaults to empty string.isolated
(boolean, optional): See the spec. Defaults tofalse
.muted
(boolean, optional): Whether this track belongs to a muted source. Defaults tofalse
.data
(object, options): An object with custom application data.
track.data
getter returns custom applicationdata
object. The app can write into it at any time.track.enabled = flag
setter fires a proprietary "@enabledchange" event if theenabled
property changed.
track.stop()
will fire a proprietary "@stop" event if not already stopped.track.remoteStop()
emulates a stop generated remotely. It will fired a proprietary "@stop" event and "ended" event if not already stopped.track.remoteMute()
emulates a mute generated remotely. It will fired "mute" event if not already muted.track.remoteUnmute()
emulates a unmute generated remotely. It will fired "unmute" event if not already unmuted.
const { FakeMediaStreamTrack } = require('fake-mediastreamtrack');
const track = new FakeMediaStreamTrack({ kind: 'video' });
track.onended = () => console.log('track ended (1)');
track.addEventListener('ended', () => console.log('track ended (2)'));
track.addEventListener('@enabledchange', () => {
console.log('track enabled changed:', track.enabled);
});
track.enabled = false;
track.enabled = false;
track.enabled = true;
track.remoteStop();
// => track enabled changed: false
// => track enabled changed: true
// => track ended (1)
// => track ended (2)
Some W3C MediaStreamTrack properties and methods are not implemented:
track.getCapabilities()
track.getSettings()
track.onisolationchange
track.onoverconstrained
ISC