dom / latest / mediastream.html /

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.

EventTarget MediaStream

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.

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.

removetrack

Fired when a MediaStreamTrack object has been removed.

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
79
12
15
No
42
15
11
55
≤37
55
25
15
42
14
11
6.0
1.5
MediaStream
55
21
79
12
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
12
No
No
32
No
45
45
No
No
No
5.0
addTrack
26
12
44
No
No
11
37
26
No
No
11
1.5
addtrack_event
26
12
50
No
No
11
≤37
26
50
No
11
1.5
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
15
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
34
No
32
11
45
45
34
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
15
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
12
No
No
32
No
45
45
No
No
No
5.0
removeTrack
26
12
44
No
15
11
37
26
44
No
11
1.5
removetrack_event
26
12
No
No
No
11
≤37
26
No
No
11
1.5
stop
29-47
13-79
No
No
No
No
≤37-47
29-47
No
No
No
2.0-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