MediaStreamTrackGenerator

The MediaStreamTrackGenerator interface of the Insertable Streams for MediaStreamTrack API creates a WritableStream that acts as a MediaStreamTrack source. The object consumes a stream of media frames as input, which can be audio or video frames.

Constructor

MediaStreamTrackGenerator()

Creates a new MediaStreamTrackGenerator object which accepts either VideoFrame or AudioFrame objects.

Properties

This interface also inherits properties from MediaStreamTrack.

MediaStreamTrackGenerator.writable

A WritableStream.

Methods

This interface doesn't implement any specific methods, but inherits methods from MediaStreamTrack.

Examples

The following example is from the article Insertable streams for MediaStreamTrack, and demonstrates a barcode scanner application, which process barcodes and highlights them before writing the transformed frames to the writablestream of MediaStreamTrackGenerator.writable.

const stream = await getUserMedia({ video: true });
const videoTrack = stream.getVideoTracks()[0];

const trackProcessor = new MediaStreamTrackProcessor({ track: videoTrack });
const trackGenerator = new MediaStreamTrackGenerator({ kind: 'video' });

const transformer = new TransformStream({
  async transform(videoFrame, controller) {
    const barcodes = await detectBarcodes(videoFrame);
    const newFrame = highlightBarcodes(videoFrame, barcodes);
    videoFrame.close();
    controller.enqueue(newFrame);
  },
});

trackProcessor.readable.pipeThrough(transformer).pipeTo(trackGenerator.writable);

Specifications

No specification found

No specification data found for api.MediaStreamTrackGenerator.
Check for problems with this page or contribute a missing spec_url to mdn/browser-compat-data. Also make sure the specification is included in w3c/browser-specs.

Browser compatibility

BCD tables only load in the browser