HTMLMediaElement: srcObject property

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The srcObject property of the HTMLMediaElement interface sets or returns the object which serves as the source of the media associated with the HTMLMediaElement, or null if not assigned.

The object can be a MediaStream, a MediaSource, a Blob, or a File (which inherits from Blob).

Note: As of March 2020, only Safari has full support for srcObject, i.e., using MediaSource, MediaStream, Blob, and File objects as values. Other browsers support MediaStream objects; until they catch up, consider falling back to creating a URL with URL.createObjectURL() and assigning it to HTMLMediaElement.src (see below for an example). In addition, as of version 108 Chromium supports attaching a dedicated worker MediaSource object by assigning that object's MediaSourceHandle instance (transferred from the worker) to srcObject.

Value

A MediaStream, MediaSource, Blob, or File object (though see the compatibility table for what is actually supported), or null if not assigned.

Usage notes

Older versions of the Media Source specification required using URL.createObjectURL() to create an object URL then setting src to that URL. Now you can just set srcObject to the MediaStream directly.

Examples

Basic example

In this example, a MediaStream from a camera is assigned to a newly-created <video> element.

js
const mediaStream = await navigator.mediaDevices.getUserMedia({ video: true });
const video = document.createElement("video");
video.srcObject = mediaStream;

In this example, a new MediaSource is assigned to a newly-created <video> element.

js
const mediaSource = new MediaSource();
const video = document.createElement("video");
video.srcObject = mediaSource;

Supporting fallback to the src property

The examples below support older browser versions that require you to create an object URL and assign it to src if srcObject isn't supported.

First, a MediaStream from a camera is assigned to a newly-created <video> element, with fallback for older browsers.

js
const mediaStream = await navigator.mediaDevices.getUserMedia({ video: true });
const video = document.createElement("video");
if ("srcObject" in video) {
  video.srcObject = mediaStream;
} else {
  // Avoid using this in new browsers, as it is going away.
  video.src = URL.createObjectURL(mediaStream);
}

Second, a new MediaSource is assigned to a newly-created <video> element, with fallback for older browsers and browsers that don't yet support assignment of MediaSource directly.

js
const mediaSource = new MediaSource();
const video = document.createElement("video");
// Older browsers may not have srcObject
if ("srcObject" in video) {
  try {
    video.srcObject = mediaSource;
  } catch (err) {
    if (err.name !== "TypeError") {
      throw err;
    }
    // Even if they do, they may only support MediaStream
    video.src = URL.createObjectURL(mediaSource);
  }
} else {
  video.src = URL.createObjectURL(mediaSource);
}

Constructing a MediaSource in a worker and passing it to the main thread to play

The MediaSource.handle property can be accessed inside a dedicated worker and the resulting MediaSourceHandle object is then transferred over to the thread that created the worker (in this case the main thread) via a postMessage() call:

js
// Inside dedicated worker
let mediaSource = new MediaSource();
let handle = mediaSource.handle;
// Transfer the handle to the context that created the worker
postMessage({ arg: handle }, [handle]);

mediaSource.addEventListener("sourceopen", () => {
  // Await sourceopen on MediaSource before creating SourceBuffers
  // and populating them with fetched media — MediaSource won't
  // accept creation of SourceBuffers until it is attached to the
  // HTMLMediaElement and its readyState is "open"
});

Over in the main thread, we receive the handle via a message event handler, attach it to a <video> via its HTMLMediaElement.srcObject property, and play the video:

js
worker.addEventListener("message", (msg) => {
  let mediaSourceHandle = msg.data.arg;
  video.srcObject = mediaSourceHandle;
  video.play();
});

Note: MediaSourceHandles cannot be successfully transferred into or via a shared worker or service worker.

Specifications

Specification
HTML
# dom-media-srcobject-dev
Media Source Extensions™
# htmlmediaelement-extensions-srcobject

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
srcObject
Chrome – Partial support
Chrome 52 – 107 (Release date: 2016-07-20)
footnote Partial support
footnote Support added for MediaStream objects (see bug 41186131).
Chrome – Partial support
Chrome 108 (Release date: 2022-11-29)
footnote Partial support
footnote Support added for MediaSourceHandle objects transferred from dedicated workers where they were obtained from MediaSource.handle (see bug 40591101).
Edge – Partial support
Edge 79 – 107 (Release date: 2020-01-15)
footnote Partial support
footnote Support added for MediaStream objects (see bug 41186131).
Edge – Partial support
Edge 108 (Release date: 2022-12-05)
footnote Partial support
footnote Support added for MediaSourceHandle objects transferred from dedicated workers where they were obtained from MediaSource.handle (see bug 40591101).
Firefox – No support
Firefox 18 – 57 (Release date: 2013-01-08)
prefix
footnote Removed in 58 and later
prefix Implemented with the vendor prefix: moz
Firefox – Partial support
Firefox 42 (Release date: 2015-11-03)
footnote Partial support
footnote Only supports MediaStream objects (see bug 886194).
Opera – Partial support
Opera 39 – 93 (Release date: 2016-08-02)
footnote Partial support
footnote Support added for MediaStream objects (see bug 41186131).
Opera – Partial support
Opera 94 (Release date: 2022-12-15)
footnote Partial support
footnote Support added for MediaSourceHandle objects transferred from dedicated workers where they were obtained from MediaSource.handle (see bug 40591101).
Safari – Full support
Safari 11 (Release date: 2017-09-19)
footnote Full support
Chrome Android – Partial support
Chrome Android 52 – 107 (Release date: 2016-07-27)
footnote Partial support
footnote Support added for MediaStream objects (see bug 41186131).
Chrome Android – Partial support
Chrome Android 108 (Release date: 2022-11-29)
footnote Partial support
footnote Support added for MediaSourceHandle objects transferred from dedicated workers where they were obtained from MediaSource.handle (see bug 40591101).
Firefox for Android – No support
Firefox for Android 18 – 57 (Release date: 2013-01-08)
prefix
footnote Removed in 58 and later
prefix Implemented with the vendor prefix: moz
Firefox for Android – Partial support
Firefox for Android 42 (Release date: 2015-11-03)
footnote Partial support
footnote Only supports MediaStream objects (see bug 886194).
Opera Android – Partial support
Opera Android 41 – 72 (Release date: 2016-10-25)
footnote Partial support
footnote Support added for MediaStream objects (see bug 41186131).
Opera Android – Partial support
Opera Android 73 (Release date: 2023-01-17)
footnote Partial support
footnote Support added for MediaSourceHandle objects transferred from dedicated workers where they were obtained from MediaSource.handle (see bug 40591101).
Safari on iOS – Full support
Safari on iOS 11 (Release date: 2017-09-19)
footnote Full support
Samsung Internet – Partial support
Samsung Internet 6 – 20 (Release date: 2017-08-23)
footnote Partial support
footnote Support added for MediaStream objects (see bug 41186131).
Samsung Internet – Partial support
Samsung Internet 21 (Release date: 2023-05-19)
footnote Partial support
footnote Support added for MediaSourceHandle objects transferred from dedicated workers where they were obtained from MediaSource.handle (see bug 40591101).
WebView Android – Partial support
WebView Android 52 – 107 (Release date: 2016-07-27)
footnote Partial support
footnote Support added for MediaStream objects (see bug 41186131).
WebView Android – Partial support
WebView Android 108 (Release date: 2022-11-29)
footnote Partial support
footnote Support added for MediaSourceHandle objects transferred from dedicated workers where they were obtained from MediaSource.handle (see bug 40591101).
WebView on iOS – Full support
WebView on iOS 11 (Release date: 2017-09-19)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Requires a vendor prefix or different name for use.
Has more compatibility info.