MediaStream

The MediaStream interface represents a stream of media content. A stream consists of several tracks, such as video or audio tracks. Each track is specified as an instance of MediaStreamTrack.

You can obtain a MediaStream object either by using the constructor or by calling functions such as MediaDevices.getUserMedia(), MediaDevices.getDisplayMedia(), or HTMLCanvasElement.captureStream().

Some user agents subclass this interface to provide more precise information or functionality, like in CanvasCaptureMediaStreamTrack.

Constructor

MediaStream()

Creates and returns a new MediaStream object. You can create an empty stream, a stream which is based upon an existing stream, or a stream that contains a specified list of tracks (specified as an array of MediaStreamTrack objects).

Properties

This interface inherits properties from its parent, EventTarget.

MediaStream.active Read only

A Boolean value that returns true if the MediaStream is active, or false otherwise.

MediaStream.id Read only

A DOMString containing 36 characters denoting a universally unique identifier (UUID) for the object.

Event handlers

MediaStream.onaddtrack

An event handler containing the action to perform when an addtrack event is fired when a new MediaStreamTrack object is added.

MediaStream.onremovetrack

An event handler containing the action to perform when a removetrack event is fired when a MediaStreamTrack object is removed from it.

Methods

This interface inherits methods from its parent, EventTarget.

MediaStream.addTrack()

Stores a copy of the MediaStreamTrack given as argument. If the track has already been added to the MediaStream object, nothing happens.

MediaStream.clone()

Returns a clone of the MediaStream object. The clone will, however, have a unique value for id.

MediaStream.getAudioTracks()

Returns a list of the MediaStreamTrack objects stored in the MediaStream object that have their kind attribute set to audio. The order is not defined, and may not only vary from one browser to another, but also from one call to another.

MediaStream.getTrackById()

Returns the track whose ID corresponds to the one given in parameters, trackid. If no parameter is given, or if no track with that ID does exist, it returns null. If several tracks have the same ID, it returns the first one.

MediaStream.getTracks()

Returns a list of all MediaStreamTrack objects stored in the MediaStream object, regardless of the value of the kind attribute. The order is not defined, and may not only vary from one browser to another, but also from one call to another.

MediaStream.getVideoTracks()

Returns a list of the MediaStreamTrack objects stored in the MediaStream object that have their kind attribute set to "video". The order is not defined, and may not only vary from one browser to another, but also from one call to another.

MediaStream.removeTrack()

Removes the MediaStreamTrack given as argument. If the track is not part of the MediaStream object, nothing happens.

Events

addtrack

Fired when a new MediaStreamTrack object is added. Also available via the onaddtrack property.

removetrack

Fired when a MediaStreamTrack object has been removed. Also available via the onremovetrack property.

active

Fired when the MediaStream is activated.

inactive

Fired when the MediaStream is inactivated.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
MediaStream
55
21
12
79
15
No
42
15
11
55
≤37
55
25
15
42
14
11
6.0
1.5
MediaStream
55
21
12
79
44
No
42
15
11
55
≤37
55
25
42
42
14
11
6.0
1.5
active
45
12
52
No
No
11
45
45
52
No
11
5.0
active_event
45
≤79
?
No
?
No
45
45
?
No
No
5.0
addTrack
26
12
44
No
No
11
37
26
No
No
11
1.5
addtrack_event
Yes
12
50
No
No
Yes
Yes
Yes
50
No
Yes
Yes
clone
45
12
48
No
No
11
45
45
48
No
11
5.0
getAudioTracks
26
12
22
Before Firefox 64, this method returned an array of AudioStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
No
Yes
11
37
26
22
Before Firefox 64, this method returned an array of AudioStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
No
11
1.5
getTrackById
26
12
49
No
No
11
37
26
49
No
11
1.5
getTracks
45
12
Yes
No
Yes
11
45
45
Yes
No
11
5.0
getVideoTracks
26
12
22
Before Firefox 64, this method returned an array of VideoStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
No
Yes
11
37
26
22
Before Firefox 64, this method returned an array of VideoStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
No
11
1.5
id
26
12
41
No
15
11
≤37
26
41
14
11
1.5
inactive_event
45
≤79
?
No
?
No
45
45
?
No
No
5.0
onactive
45
12
?
No
?
No
45
45
?
No
No
5.0
onaddtrack
26
12
50
No
No
11
37
26
50
No
11
1.5
oninactive
45
12
?
No
?
No
45
45
?
No
No
5.0
onremovetrack
26
12
No
No
No
11
37
26
No
No
11
1.5
removeTrack
26
12
Yes
No
Yes
11
37
26
Yes
No
11
1.5
removetrack_event
Yes
12
No
No
No
Yes
Yes
Yes
No
No
Yes
Yes
stop
Yes-47
13-79
?
No
No
No
Yes-47
Yes-47
?
No
No
Yes-5.0

See also

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MediaStream