W3C

Media Capture and Streams

W3C Editor's Draft 20 March 2013

This version:
http://dev.w3.org/2011/webrtc/editor/archives/20130320/getusermedia.html
Latest published version:
http://www.w3.org/TR/mediacapture-streams/
Latest editor's draft:
http://dev.w3.org/2011/webrtc/editor/getusermedia.html
Previous editor's draft:
http://dev.w3.org/2011/webrtc/editor/archives/20121212/getusermedia.html
Editors:
Daniel C. Burnett, Voxeo
Adam Bergkvist, Ericsson
Cullen Jennings, Cisco
Anant Narayanan, Mozilla (until November 2012)

Abstract

This document defines a set of JavaScript APIs that allow local media, including audio and video, to be requested from a platform.

Status of This Document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

This document is not complete. It is subject to major changes and, while early experimentations are encouraged, it is therefore not intended for implementation. The API is based on preliminary work done in the WHATWG. The Media Capture Task Force expects this specification to evolve significantly based on:

This document was published by the Web Real-Time Communication Working Group and Device APIs Working Group as an Editor's Draft. If you wish to make comments regarding this document, please send them to public-media-capture@w3.org (subscribe, archives). All comments are welcome.

Publication as an Editor's Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures (Web Real-Time Communication Working Group, Device APIs Working Group) made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

Table of Contents

1. Introduction

This section is non-normative.

Access to multimedia streams (video, audio, or both) from local devices (video cameras, microphones, Web cams) can have a number of uses, such as real-time communication, recording, and surveillance.

This document defines the APIs used to get access to local devices that can generate multimedia stream data. This document also defines the stream API by which JavaScript is able to manipulate the stream data or otherwise process it.

2. Conformance

As well as sections marked as non-normative, all authoring guidelines, diagrams, examples, and notes in this specification are non-normative. Everything else in this specification is normative.

The key words MUST, MUST NOT, REQUIRED, SHOULD, SHOULD NOT, RECOMMENDED, MAY, and OPTIONAL in this specification are to be interpreted as described in [RFC2119].

This specification defines conformance criteria that apply to a single product: the user agent that implements the interfaces that it contains.

Implementations that use ECMAScript to implement the APIs defined in this specification must implement them in a manner consistent with the ECMAScript Bindings defined in the Web IDL specification [WEBIDL], as this specification uses that specification and terminology.

3. Terminology

The EventHandler interface represents a callback used for event handlers as defined in [HTML5].

The concepts queue a task and fires a simple event are defined in [HTML5].

The terms event handlers and event handler event types are defined in [HTML5].

Note

The following paragraphs need to be spread out to make them easier to read. Also, descriptions for "source states" and "capabilities" need to be added.

A source is the "thing" providing the source of a media stream track. The source is the broadcaster of the media itself. A source can be a physical webcam, microphone, local video or audio file from the user's hard drive, network resource, or static image. Some sources have an identifier which must be unique to the application (un-guessable by another application) and persistent between application sessions (e.g., the identifier for a given source device/application must stay the same, but not be guessable by another application). Sources that must have an identifier are camera and microphone sources; local file sources are not required to have an identifier. Source identifiers let the application save, identify the availability of, and directly request specific sources. Other than the identifier, other bits of source identify are never directly available to the application until the user agent connects a source to a track. Once a source has been "released" to the application (either via a permissions UI, pre-configured allow-list, or some other release mechanism) the application will be able discover additional source-specific capabilities. When a source is connected to a track, it must conform to the constraints present on that track (or set of tracks). Sources will be released (un-attached) from a track when the track is ended for any reason. On the MediaStreamTrack object, sources are represented by a sourceType attribute. The behavior of APIs associated with the source's capabilities and state change depending on the source type. [[Sources have capabilities and state. The capabilities and state are "owned" by the source and are common to any [multiple] tracks that happen to be using the same source (e.g., if two different tracks objects bound to the same source ask for the same capability or state information, they will get back the same answer).]]

Constraints are an optional feature for restricting the range of allowed variability on a source. Without provided constraints, implementations are free to select a source's state from the full range of its supported capabilities, and to adjust that state at any time for any reason. Constraints may be optional or mandatory. Optional constraints are represented by an ordered list, mandatory constraints are an unordered set. The order of the optional constraints is from most important (at the head of the list) to least important (at the tail of the list). Constraints are stored on the track object, not the source. Each track can be optionally initialized with constraints, or constraints can be added afterward through the constraint APIs defined in this spec. Applying track level constraints to a source is conditional based on the type of source. For example, read-only sources will ignore any specified constraints on the track. It is possible for two tracks that share a unique source to apply contradictory constraints. Under such contradictions, the implementation will mute both tracks and notify them that they are over-constrained. Events are available that allow the application to know when constraints cannot be met by the user agent. These typically occur when the application applies constraints beyond the capability of a source, contradictory constraints, or in some cases when a source cannot sustain itself in over-constrained scenarios (overheating, etc.). Constraints that are intended for video sources will be ignored by audio sources and vice-versa. Similarly, constraints that are not recognized will be preserved in the constraint structure, but ignored by the application. This will allow future constraints to be defined in a backward compatible manner. A correspondingly-named constraint exists for each corresponding source state name and capability name. In general, user agents will have more flexibility to optimize the media streaming experience the fewer constraints are applied.

4. Stream API

4.1 Introduction

The MediaStream interface is used to represent streams of media data, typically (but not necessarily) of audio and/or video content, e.g. from a local camera. The data from a MediaStream object does not necessarily have a canonical binary form; for example, it could just be "the video currently coming from the user’s video camera". This allows user agents to manipulate media streams in whatever fashion is most suitable on the user’s platform.

Each MediaStream object can contain zero or more tracks, in particular audio and video tracks. All tracks in a MediaStream are intended to be synchronized when rendered. Different MediaStreams do not need to be synchronized.

Each track in a MediaStream object has a corresponding MediaStreamTrack object.

A MediaStreamTrack represents content comprising one or more channels, where the channels have a defined well known relationship to each other (such as a stereo or 5.1 audio signal).

A channel is the smallest unit considered in this API specification.

A MediaStream object has an input and an output. The input depends on how the object was created: a MediaStream object generated by a getUserMedia() call (which is described later in this document), for instance, might take its input from the user’s local camera. The output of the object controls how the object is used, e.g., what is saved if the object is written to a file or what is displayed if the object is used in a video element.

Each track in a MediaStream object can be disabled, meaning that it is muted in the object’s output. All tracks are initially enabled.

A MediaStream can be finished, indicating that its inputs have forever stopped providing data.

The output of a MediaStream object MUST correspond to the tracks in its input. Muted audio tracks MUST be replaced with silence. Muted video tracks MUST be replaced with blackness.

A new MediaStream object can be created from accessible media sources (that does not require any additional permissions) using the MediaStream() constructor. The constructor argument can either be an existing MediaStream object, in which case the given stream is cloned, or an array of MediaStreamTrack objects. The latter from makes it possible to compose a stream from different source streams.

A MediaStream

The ability to duplicate a MediaStream, specifically creating a new MediaStream object using another stream as input, allows for greater control since separate MediaStream instances can be manipulated and consumed individually.

When a MediaStream object is being generated from a local file (as opposed to a live audio/video source), the user agent SHOULD stream the data from the file in real time, not all at once. The MediaStream object is also used in contexts outside getUserMedia, such as [WEBRTC10]. In both cases, ensuring a realtime stream reduces the ease with which pages can distinguish live video from pre-recorded video, which can help protect the user’s privacy.

4.2 MediaStream

The MediaStream() constructor takes zero or one argument. If the argument is supplied, it MUST either be of type MediaStream or an array of MediaStreamTrack objects. When the constructor is invoked, the UA must run the following steps:

  1. Let stream be a newly constructed MediaStream object.

  2. Initialize stream’s id attribute to a newly generated value.

  3. If the constructor’s argument is present, run the sub steps that corresponds to the argument type.

  4. Return stream.

A MediaStream can have multiple audio and video sources (e.g. because the user has multiple microphones, or because the real source of the stream is a media resource with many media tracks). The stream represented by a MediaStream thus has zero or more tracks.

The tracks of a MediaStream are stored in a track set. The track set MUST contain the MediaStreamTrack objects that correspond to the tracks of the stream. The relative order of the tracks in the set is user agent defined and the API will never put any requirements on the order. The proper way to find a specific MediaStreamTrack object in the set is to look it up by its id.

An object that reads data from the output of a MediaStream is referred to as a MediaStream consumer. The list of MediaStream consumers currently includes the media elements and the PeerConnection API specified in [WEBRTC10].

Note

MediaStream consumers must be able to handle tracks being added and removed. This behavior is specified per consumer.

A MediaStream object is said to be finished when all tracks belonging to the stream have ended. When this happens for any reason other than the stop() method being invoked, the user agent MUST queue a task that runs the following steps:

  1. If the object’s ended attribute has the value true already, then abort these steps. (The stop() method was probably called just before the stream stopped for other reasons, e.g. the user clicked an in-page stop button and then the user agent provided stop button.)

  2. Set the object’s ended attribute to true.

  3. Fire a simple event named ended at the object.

If the end of the stream was reached due to a user request, the task source for this task is the user interaction task source. Otherwise the task source for this task is the networking task source.

typedef MediaStreamTrack[] MediaStreamTrackArray;
Throughout this specification, the identifier MediaStreamTrackArray is used to refer to the array of MediaStreamTrack type.
[Constructor (),
        Constructor (MediaStream stream),
        Constructor (MediaStreamTrackArray tracks)]
interface MediaStream : EventTarget {
    readonly    attribute DOMString    id;
    sequence<MediaStreamTrack> getAudioTracks ();
    sequence<MediaStreamTrack> getVideoTracks ();
    MediaStreamTrack?          getTrackById (DOMString trackId);
    void                       addTrack (MediaStreamTrack track);
    void                       removeTrack (MediaStreamTrack track);
                attribute boolean      ended;
                attribute EventHandler onended;
                attribute EventHandler onaddtrack;
                attribute EventHandler onremovetrack;
};

4.2.1 Attributes

ended of type boolean,

The MediaStream.ended attribute MUST return true if the MediaStream has finished, and false otherwise.

When a MediaStream object is created, its ended attribute MUST be set to false, unless it is being created using the MediaStream() constructor whose arguments are lists of MediaStreamTrack objects that are all ended, in which case the MediaStream object MUST be created with its ended attribute set to true.

id of type DOMString, readonly

When a MediaStream object is created, the user agent MUST generate a globally unique identifier string, and MUST initialize the object’s id attribute to that string. Such strings MUST only use characters in the ranges U+0021, U+0023 to U+0027, U+002A to U+002B, U+002D to U+002E, U+0030 to U+0039, U+0041 to U+005A, U+005E to U+007E, and MUST be 36 characters long.

The id attribute MUST return the value to which it was initialized when the object was created.

onaddtrack of type EventHandler,
This event handler, of type addtrack, MUST be supported by all objects implementing the MediaStream interface.
onended of type EventHandler,
This event handler, of type ended, MUST be supported by all objects implementing the MediaStream interface.
onremovetrack of type EventHandler,
This event handler, of type removetrack, MUST be supported by all objects implementing the MediaStream interface.

4.2.2 Methods

addTrack

Adds the given MediaStreamTrack to this MediaStream.

When the addTrack() method is invoked, the user agent MUST run the following steps:

  1. Let track be the MediaStreamTrack argument and stream this MediaStream object.

  2. If stream is finished, throw an INVALID_STATE_ERR exception.

  3. If track is already in stream’s track set, then abort these steps.

  4. Create a new MediaStreamTrack object and let it inherit track’s underlying source, kind and label attributes. Add the new MediaStreamTrack to stream’s track set.

ParameterTypeNullableOptionalDescription
trackMediaStreamTrack
Return type: void
getAudioTracks

Returns a sequence of MediaStreamTrack objects representing the audio tracks in this stream.

The getAudioTracks() method MUST return a sequence that represents a snapshot of all the MediaStreamTrack objects in this stream’s track set whose kind is equal to "audio". The conversion from the track set to the sequence is user agent defined and the order does not have to stable between calls.

No parameters.
Return type: sequence<MediaStreamTrack>
getTrackById

The getTrackById() method MUST return the first MediaStreamTrack object in this stream’s track set whose id is equal to trackId. The method MUST return null if no track matches the trackId argument.

ParameterTypeNullableOptionalDescription
trackIdDOMString
Return type: MediaStreamTrack, nullable
getVideoTracks

Returns a sequence of MediaStreamTrack objects representing the video tracks in this stream.

The getVideoTracks() method MUST return a sequence that represents a snapshot of all the MediaStreamTrack objects in this stream’s track set whose kind is equal to "video". The conversion from the track set to the sequence is user agent defined and the order does not have to stable between calls.

No parameters.
Return type: sequence<MediaStreamTrack>
removeTrack

Removes the given MediaStreamTrack from this MediaStream.

When the removeTrack() method is invoked, the user agent MUST run the following steps:

  1. Let track be the MediaStreamTrack argument and stream this MediaStream object.

  2. If stream is finished, throw an INVALID_STATE_ERR exception.

  3. If track is not in stream’s track set, abort these steps.

  4. Remove track from stream’s track set.

ParameterTypeNullableOptionalDescription
trackMediaStreamTrack
Return type: void

4.3 MediaStreamTrack

A MediaStreamTrack object represents a media source in the user agent. Several MediaStreamTrack objects can represent the same media source, e.g., when the user chooses the same camera in the UI shown by two consecutive calls to getUserMedia() .

Note that a web application can revoke all given permissions with MediaStreamTrack.stop().

A MediaStreamTrack object is said to end when the user agent learns that no more data will ever be forthcoming for this track.

When a MediaStreamTrack object ends for any reason (e.g., because the user rescinds the permission for the page to use the local camera, or because the data comes from a finite file and the file’s end has been reached and the user has not requested that it be looped, or because the UA has instructed the track to end for any reason, or because the reference count of the track’s underlying media source has reached zero, it is said to be ended. When track instance track ends for any reason other than the stop() method being invoked on the MediaStreamTrack object that represents track, the user agent MUST queue a task that runs the following steps:

  1. If the track’s readyState attribute has the value ended already, then abort these steps.

  2. Set track’s readyState attribute to ended.

  3. Fire a simple event named ended at the object.

If the end of the stream was reached due to a user request, the event source for this event is the user interaction event source.

Constraints are independent of sources. **** Add note about potential effects of readonly and remote flags ****

Whether MediaTrackConstraints were provided at track initialization time or need to be established later at runtime, the APIs defined below allow the retrieval and manipulation of the constraints currently established on a track.

Each track maintains an internal version of the MediaTrackConstraints structure, namely a mandatory set of constraints (no duplicates), and an optional ordered list of individual constraint objects (may contain duplicates). The internal stored constraint structure is only exposed to the application using the existing MediaTrackConstraints, MediaTrackConstraintSet, MediaTrackConstraint, and similarly-derived-type dictionary objects.

When track constraints change, a user agent must queue a task to evaluate those changes when the task queue is next serviced. Similarly, if the sourceType changes, then the user agent should perform the same actions to re-evaluate the constraints of each track affected by that source change.

interface MediaStreamTrack {
    readonly    attribute DOMString             kind;
    readonly    attribute DOMString             id;
    readonly    attribute DOMString             label;
                attribute boolean               enabled;
    readonly    attribute MediaStreamTrackState readyState;
    readonly    attribute SourceTypeEnum        sourceType;
    readonly    attribute DOMString             sourceId;
                attribute EventHandler          onstarted;
                attribute EventHandler          onmute;
                attribute EventHandler          onunmute;
                attribute EventHandler          onended;
    any                    getConstraint (DOMString constraintName, optional boolean mandatory = false);
    void                   setConstraint (DOMString constraintName, any constraintValue, optional boolean mandatory = false);
    MediaTrackConstraints? constraints ();
    void                   applyConstraints (MediaTrackConstraints constraints);
    void                   prependConstraint (DOMString constraintName, any constraintValue);
    void                   appendConstraint (DOMString constraintName, any constraintValue);
                attribute EventHandler          onoverconstrained;
    void                   stop ();
};

4.3.1 Attributes

enabled of type boolean,

The MediaStreamTrack.enabled attribute, on getting, MUST return the last value to which it was set. On setting, it MUST be set to the new value, and then, if the MediaStreamTrack object is still associated with a track, MUST enable the track if the new value is true, and disable it otherwise.

Note

Thus, after a MediaStreamTrack is disassociated from its track, its enabled attribute still changes value when set; it just doesn’t do anything with that new value.

id of type DOMString, readonly

Unless a MediaStreamTrack object is created as a part a of special purpose algorithm that specifies how the track id must be initialized, the user agent MUST generate a globally unique identifier string and initialize the object’s id attribute to that string.

An example of an algorithm that specifies how the track id must be initialized is the algorithm to represent an incoming network component with a MediaStreamTrack object. [WEBRTC10]

MediaStreamTrack.id attribute MUST return the value to which it was initialized when the object was created.

kind of type DOMString, readonly

The MediaStreamTrack.kind attribute MUST return the string "audio" if the object represents an audio track or "video" if object represents a video track.

label of type DOMString, readonly

User agents MAY label audio and video sources (e.g., "Internal microphone" or "External USB Webcam"). The MediaStreamTrack.label attribute MUST return the label of the object’s corresponding track, if any. If the corresponding track has or had no label, the attribute MUST instead return the empty string.

Note

Thus the kind and label attributes do not change value, even if the MediaStreamTrack object is disassociated from its corresponding track.

onended of type EventHandler,
This event handler, of type ended, MUST be supported by all objects implementing the MediaStreamTrack interface.
onmute of type EventHandler,
This event handler, of type muted, MUST be supported by all objects implementing the MediaStreamTrack interface.
onoverconstrained of type EventHandler,
This event handler, of type overconstrained, MUST be supported by all objects implementing the MediaStreamTrack interface.
onstarted of type EventHandler,
This event handler, of type started, MUST be supported by all objects implementing the MediaStreamTrack interface.
onunmute of type EventHandler,
This event handler, of type unmuted, MUST be supported by all objects implementing the MediaStreamTrack interface.
readyState of type MediaStreamTrackState, readonly

The readyState attribute represents the state of the track. It MUST return the value to which the user agent last set it.

When a MediaStreamTrack object is created via getUserMedia(), its readyState is either live or muted, depending on the state of the track's underlying media source. For example, a track in a MediaStream created with getUserMedia() MUST initially have its readyState attribute set to live.

sourceId of type DOMString, readonly
The application-unique identifier for this source. The same identifier must be valid between sessions of this application, but must also be different for other applications. Some sort of GUID is recommended for the identifier.
sourceType of type SourceTypeEnum, readonly
Returns the type information associated with the currently attached source (if any).

4.3.2 Methods

appendConstraint

Appends (at the end of the list) the provided constraint name and value. This method does not consider whether a same-named constraint already exists in the optional constraints list.

This method applies exclusively to optional constraints; it does not modify mandatory constraints.

This method is a convenience API for programmatically building constraint structures.

ParameterTypeNullableOptionalDescription
constraintNameDOMStringThe name of the constraint to append to the list of optional constraints.
constraintValueanyEither a primitive value (float/DOMString/etc), or a MinMaxConstraint dictionary.
Return type: void
applyConstraints

This API will replace all existing constraints with the provided constraints (if existing constraints exist). Otherwise, it will apply the newly provided constraints to the track.

ParameterTypeNullableOptionalDescription
constraintsMediaTrackConstraintsA new constraint structure to apply to this track.
Return type: void
constraints
Returns the complete constraints object associated with the track. If no mandatory constraints have been defined, the mandatory field will not be present (it will be undefined). If no optional constraints have been defined, the optional field will not be present (it will be undefined). If neither optional, nor mandatory constraints have been created, the value null is returned.
No parameters.
Return type: MediaTrackConstraints, nullable
getConstraint

Retrieves a specific named constraint value from the track. The named constraints are the same names used for the capabilities API, and also are the same names used for the source's state attributes.

Returns one of the following types:

null
If no constraint matching the provided constraintName exists in the respective optional or mandatory set on this track.
sequence<MediaTrackConstraint>

If the mandatory flag is false and there is at least one optional matching constraint name defined on this track.

Each MediaTrackConstraint result in the list will contain a key which matches the requested constraintName parameter, and whose value will either be a primitive value, or a MinMaxConstraint object.

The returned list will be ordered from most important-to-satisfy at index 0, to the least-important-to-satisfy optional constraint.

Note

Example: Given a track with an internal constraint structure: { mandatory: { width: { min: 640 }, height: { min: 480 } }, optional: [ { width: 650 }, { width: { min: 650 }}, { frameRate: 60 }, { width: { max: 800 }}, { fillLightMode: "off" }, { facingMode: "user" } ] } and a request for getConstraint("width"), the following list would be returned: [ { width: 650 }, { width: { min: 650 }} { width: { max: 800 }} ]

MinMaxConstraint
If the mandatory flag is true, and the requested constraint is defined in the mandatory MediaTrackConstraintSet associated with this track, and the value of the constraint is a min/max range object.
primitive_value
If the mandatory flag is true, and the requested constraint is defined in the mandatory MediaTrackConstraintSet associated with this track, and the value of the constraint is a primitive value (DOMString, unsigned long, float, etc.).
ParameterTypeNullableOptionalDescription
constraintNameDOMStringThe name of the setting for which the current value of that setting should be returned
mandatoryboolean = falsetrue to indicate that the constraint should be looked up in the mandatory set of constraints, otherwise, the constraintName should be retrieved from the optional list of constraints.
Return type: any
prependConstraint

Prepends (inserts before the start of the list) the provided constraint name and value. This method does not consider whether a same-named constraint already exists in the optional constraints list.

This method applies exclusively to optional constraints; it does not modify mandatory constraints.

This method is a convenience API for programmatically building constraint structures.

ParameterTypeNullableOptionalDescription
constraintNameDOMStringThe name of the constraint to prepend to the list of optional constraints.
constraintValueanyEither a primitive value (float/DOMString/etc), or a MinMaxConstraint dictionary.
Return type: void
setConstraint

This method updates the value of a same-named existing constraint (if found) in either the mandatory or optional list, and otherwise sets the new constraint.

This method searches the list of optional constraints from index 0 (highest priority) to the end of the list (lowest priority) looking for matching constraints. Therefore, for multiple same-named optional constraints, this method will only update the value of the highest-priority matching constraint.

If the mandatory flag is false and the constraint is not found in the list of optional constraints, then a new optional constraint is created and appended to the end of the list (thus having lowest priority).

Note

Note: This behavior allows applications to iteratively call setConstraint and have their constraints added in the order specified in the source.

ParameterTypeNullableOptionalDescription
constraintNameDOMStringThe name of the constraint to set.
constraintValueanyEither a primitive value (float/DOMString/etc), or a MinMaxConstraint dictionary.
mandatoryboolean = falseA flag indicating whether this constraint should be applied to the optional or mandatory constraints.
Return type: void
stop

When a MediaStreamTrack object’s stop() method is invoked, if no source is attached (e.g., sourceType is "none"), then this call returns immediately (e.g., is a no-op). Otherwise, the user agent MUST queue a task that runs the following steps:

  1. Let track be the current MediaStreamTrack object.

  2. End track. The track starts outputting only silence and/or blackness, as appropriate.

  3. Permanently stop the generation of data for track's source. If the data is being generated from a live source (e.g., a microphone or camera), then the user agent SHOULD remove any active "on-air" indicator for that source. If the data is being generated from a prerecorded source (e.g. a video file), any remaining content in the file is ignored.

The task source for the tasks queued for the stop() method is the DOM manipulation task source.

No parameters.
Return type: void
enum MediaStreamTrackState {
    "new",
    "live",
    "muted",
    "ended"
};
Enumeration description
newThe track type is new and has not been initialized (connected to a source of any kind). This state implies that the track's label will be the empty string.
live

The track is active (the track’s underlying media source is making a best-effort attempt to provide data in real time).

The output of a track in the live state can be switched on and off with the enabled attribute.

muted

The track is muted (the track’s underlying media source is temporarily unable to provide data).

A MediaStreamTrack in a MediaStream may be muted if the user temporarily revokes the web application's permission to use a media input device.

Also, the "muted" state can be entered when a track becomes over-constrained.

ended

The track has ended (the track's underlying media source is no longer providing data, and will never provide more data for this track). Once a track enters this state, it never exits it.

For example, a video track in a MediaStream ends if the user unplugs the USB web camera that acts as the track's media source.

4.3.3 Track Source Types

enum SourceTypeEnum {
    "none",
    "camera",
    "microphone",
    "photo-camera"
};
Enumeration description
noneThis track has no source. This is the case when the track is in the "new" or "ended" readyState.
cameraA valid source type only for VideoStreamTracks. The source is a local video-producing camera source (without special photo-mode support).
microphoneA valid source type only for AudioStreamTracks. The source is a local audio-producing microphone source.
photo-cameraA valid source type only for VideoStreamTracks. The source is a local video-producing camera source which supports high-resolution photo-mode and its related state attributes.

4.4 Video and Audio Tracks

The MediaStreamTrack object cannot be instantiated directly. To create an instance of a MediaStreamTrack, one of its derived track types may be instantiated. These derived types are defined in this section.

Note that the camera's green light doesn't come on when a new track is created; nor does the user get prompted to enable the camera/microphone. Those actions only happen after the developer has requested that a media stream containing "new" tracks be bound to a source via getUserMedia(). Until that point tracks are inert.

4.4.1 VideoStreamTrack interface

Video tracks may be instantiated with optional media track constraints. These constraints can be later modified on the track as needed by the application, or created after-the-fact if the initial constraints are unknown to the application.

Note

Example: VideoStreamTrack objects are instantiated in JavaScript using the new operator:
new VideoStreamTrack();
or
new VideoStreamTrack( { optional: [ { sourceId: "20983-20o198-109283-098-09812" }, { width: { min: 800, max: 1200 }}, { height: { min: 600 }}] });

[Constructor(optional MediaTrackConstraints videoConstraints)]
interface VideoStreamTrack : MediaStreamTrack {
    static sequence<DOMString> getSourceIds ();
    void                       takePhoto ();
                attribute EventHandler onphoto;
                attribute EventHandler onphotoerror;
};
4.4.1.1 Attributes
onphoto of type EventHandler,
Register/unregister for "photo" events. The handler should expect to get a BlobEvent object as its first parameter.
Note

The BlobEvent returns a photo (as a Blob) in a compressed format (for example: PNG/JPEG) rather than a raw ImageData object due to the expected large, uncompressed size of the resulting photos.

onphotoerror of type EventHandler,
In the event of an error taking the photo, a "photoerror" event will be dispatched instead of a "photo" event. The "photoerror" is a simple event of type Event.
4.4.1.2 Methods
getSourceIds, static

Returns an array of application-unique source identifiers. This list will be populated only with local sources whose sourceType is "camera" or "photo-camera" [and if allowed by the user-agent, "readonly" variants of the former two types]. The video source ids returned in the list constitute those sources that the user agent can identify at the time the API is called (the list can grow/shrink over time as sources may be added or removed). As a static method, getSourceIds can be queried without instantiating any VideoStreamTrack objects or without calling getUserMedia().

Issue 1

Issue: This information deliberately adds to the fingerprinting surface of the UA. However, this information will not be identifiable outside the scope of this application and could also be obtained via other round-about techniques using getUserMedia().

No parameters.
Return type: sequence<DOMString>
takePhoto

If the sourceType's value is anything other than "photo-camera", this method returns immediately and does nothing. If the sourceType is "photo-camera", then this method temporarily (asynchronously) switches the source into "high resolution photo mode", applies the configured photoWidth, photoHeight, exposureMode, and isoMode state to the stream, and records/encodes an image (using a user-agent determined format) into a Blob object. Finally, a task is queued to fire a "photo" event with the resulting recorded/encoded data. In case of a failure for any reason, a "photoerror" event is queued instead and no "photo" event is dispatched.

Issue 2

Issue: We could consider providing a hint or setting for the desired photo format? There could be some alignment opportunity with the Recoding proposal...

No parameters.
Return type: void

4.4.3 AudioStreamTrack

Note

Example: AudioStreamTrack objects are instantiated in JavaScript using the new operator:
new AudioStreamTrack();
or
new AudioStreamTrack( { optional: [ { sourceId: "64815-wi3c89-1839dk-x82-392aa" }, { gain: 0.5 }] });

[Constructor(optional MediaTrackConstraints audioConstraints)]
interface AudioStreamTrack : MediaStreamTrack {
    static sequence<DOMString> getSourceIds ();
};
4.4.3.1 Methods
getSourceIds, static
See definition of getSourceIds on the VideoStreamTrack object. Note that the list of source ids for AudioStreamTrack will be populated only with local sources whose sourceType is "microphone" [and if allowed by the user-agent, "readonly" microphone variants].
No parameters.
Return type: sequence<DOMString>

5. URL

partial interface URL {
    static DOMString createObjectURL (MediaStream stream);
};

5.1 Methods

createObjectURL, static

Mints a Blob URL to refer to the given MediaStream.

When the createObjectURL() method is called with a MediaStream argument, the user agent MUST return a unique Blob URL for the given MediaStream. [FILE-API]

For audio and video streams, the data exposed on that stream MUST be in a format supported by the user agent for use in audio and video elements.

A Blob URL is the same as what the File API specification calls a Blob URI, except that anything in the definition of that feature that refers to File and Blob objects is hereby extended to also apply to MediaStream objects.

ParameterTypeNullableOptionalDescription
streamMediaStream
Return type: DOMString

6. MediaStreams as Media Elements

A MediaStream may be assigned to media elements as defined in HTML5 [HTML5] by calling createObjectURL to obtain a URL for the MediaStream and then setting the media elements src attribute to that URL. A MediaStream is not preloadable or seekable and represents a simple, potentially infinite, linear media timeline. The timeline starts at 0 and increments linearly in real time as long as the MediaStream is playing. The timeline does not increment when the MediaStream is paused.

Issue 3

Do we also need to support direct assignment and access of the underlying stream?

The nature of the MediaStream places certain restrictions on the behavior and attribute values of the associated media element and on the operations that can be performed on it, as shown below:

Attribute Name Attribute Type Valid Values Additional considerations
src DOMString a local URI referencing a MediaStream N.B. Revocation of the URI does not count as a change to this field.
currentSrc DOMString a local URI referencing a MediaStream -
preload DOMString none A MediaStream cannot be preloaded.
buffered TimeRanges buffered.length MUST return 1.
buffered.start(0) MUST return 0.
buffered.end(0) MUST return 0.
A MediaStream cannot be preloaded. Therefore, the amount buffered is always an empty TimeRange.
currentTime double Any positive integer. The initial value is 0 and the values increments linearly in real time whenever the stream is playing. The value is the current stream position, in seconds. The UA MUST ignore attempts to set this attribute.
duration double Infinity A MediaStream does not have a pre-defined duration.

If the underlying MediaStream is destroyed, the UA MUST set this property to the value of the last known currentTime.

seeking boolean false A MediaStream is not seekable. Therefore, this attribute MUST always have the value false.
defaultPlaybackRate double 1.0 A MediaStream is not seekable. Therefore, this attribute MUST always have the value 1.0 and any attempt to alter it MUST fail.
playbackRate double 1.0 A MediaStream is not seekable. Therefore, this attribute MUST always have the value 1.0 and any attempt to alter it MUST fail.
played TimeRanges played.length MUST return 1.
played.start(0) MUST return 0.
played.end(0) MUST return the last known currentTime.
A MediaStream's timeline always consists of a single range, starting at 0 and extending up to the currentTime.
seekable TimeRanges seekable.length MUST return 0.
seekable.start() MUST return currentTime.
seekable.end() MUST return currentTime.
A MediaStream is not seekable.
startOffsetTime Date Not-a-Number (NaN) A MediaStream does not specify a timeline offset.
loop boolean false A MediaStream has no defined end and therefore cannot be looped.

7. Event summary

This section is non-normative.

The following event fires on MediaStream objects:

Event name Interface Fired when...
ended Event The MediaStream finished as a result of all tracks in the MediaStream ending.
addtrack MediaStreamTrackEvent A new MediaStreamTrack has been added to this stream.
removetrack MediaStreamTrackEvent A MediaStreamTrack has been removed from this stream.

The following event fires on MediaStreamTrack objects:

Event name Interface Fired when...
started Event The MediaStreamTrack object has just transitioned from the "new" readyState to another state. This event fires before any other corresponding events such as "ended" or "statechanged".
muted Event The MediaStreamTrack object's source is temporarily unable to provide data.
unmuted Event The MediaStreamTrack object's source is live again after having been temporarily unable to provide data.
overconstrained Event

This event fires asynchronously for each affected track (when multiple tracks share the same source) after the user agent has evaluated the current constraints against a given sourceId and is not able to configure the source within the limitations established by the union of imposed constraints.

This event may also fire when takePhoto is called and the source cannot record/encode an image due to over-constrained or conflicting constraints of those uniquely related to sourceTypes of type "photo-camera".

Due to being over-constrained, the user agent must mute each affected track.

The affected track(s) will remain un-usable (in the "muted" readyState) until the application adjusts the constraints to accommodate the source's capabilities.

The "overconstrained" event is a simple event of type Event; it carries no information about which constraints caused the source to be over-constrained (the application has all the necessary APIs to figure it out).

ended Event The MediaStreamTrack object's source will no longer provide any data, either because the user revoked the permissions, or because the source device has been ejected, or because the remote peer stopped sending data, or because the stop() method was invoked.

8. Obtaining local multimedia content

8.2 MediaStreamConstraints

dictionary MediaStreamConstraints {
    (boolean or MediaTrackConstraints) video = false;
    (boolean or MediaTrackConstraints) audio = false;
};

8.2.1 Dictionary MediaStreamConstraints Members

audio of type booleanMediaTrackConstraints, defaulting to false
Issue 7

Provide definition of audio constraints here.

video of type booleanMediaTrackConstraints, defaulting to false
Issue 6

Provide definition of video constraints here.

dictionary MediaTrackConstraints {
    MediaTrackConstraintSet? mandatory = null;
    MediaTrackConstraint[]?  optional = null;
};

8.2.2 Dictionary MediaTrackConstraints Members

mandatory of type MediaTrackConstraintSet, nullable, defaulting to null
Issue 8

Provide definition of mandatory constraints here.

optional of type array of MediaTrackConstraint, nullable, defaulting to null
Issue 9

Provide definition of optional constraints here.

A MediaTrackConstraintSet is a dictionary containing one or more key-value pairs, where each key MUST be a valid registered constraint name in the IANA-hosted RTCWeb Media Constraints registry [RTCWEB-CONSTRAINTS] and its value SHOULD be as defined in the associated reference[s] given in the registry.

A MediaTrackConstraint is a dictionary containing exactly one key-value pair, where the key MUST be a valid registered constraint name in the IANA-hosted RTCWeb Media Constraints registry [RTCWEB-CONSTRAINTS] and the value SHOULD be as defined in the associated reference[s] given in the registry.

8.5 Implementation Suggestions

This section is non-normative.

Best Practice 1: Resource reservation

The user agent is encouraged to reserve resources when it has determined that a given call to getUserMedia() will succeed. It is preferable to reserve the resource prior to invoking the success callback provided by the web page. Subsequent calls to getUserMedia() (in this page or any other) should treat the resource that was previously allocated, as well as resources held by other applications, as busy. Resources marked as busy should not be provided as sources to the current web page, unless specified by the user. Optionally, the user agent may choose to provide a stream sourced from a busy source but only to a page whose origin matches the owner of the original stream that is keeping the source busy.

This document recommends that in the permission grant dialog or device selection interace (if one is present), the user be allowed to select any available hardware as a source for the stream requested by the page (provided the resource is able to fulfill mandatory constraints, if any were specified), in addition to the ability to substitute a video or audio source with local files and other media. A file picker may be used to provide this functionality to the user.

This document also recommends that the user be shown all resources that are currently busy as a result of prior calls to getUserMedia() (in this page or any other page that is still alive) and be allowed to terminate that stream and utilize the resource for the current page instead. If possible in the current operating environment, it is also suggested that resources currently held by other applications be presented and treated in the same manner. If the user chooses this option, the track corresponding to the resource that was provided to the page whose stream was affected must be removed. Additionally, if removing a track in this manner causes the stream to contain no more tracks, the onended event must be raised on it.

Best Practice 2: Handling multiple devices

A MediaStream may contain more than one video and audio track. This makes it possible to include video from two or more webcams in a single stream object, for example. However, the current API does not allow a page to express a need for multiple video streams from independent sources.

It is recommended for multiple calls to getUserMedia() from the same page be allowed as a way for pages to request multiple, discrete, video or audio streams.

A single call to getUserMedia() will always return a stream with either zero or one audio tracks, and either zero or one video tracks. If a script calls getUserMedia() multiple times before reaching a stable state, this document advises the UI designer that the permission dialogs should be merged, so that the user can give permission for the use of multiple cameras and/or media sources in one dialog interaction. The constraints on each getUserMedia call can be used to decide which stream gets which media sources.

9. Examples

This sample code exposes a button. When clicked, the button is disabled and the user is prompted to offer a stream. The user can cause the button to be re-enabled by providing a stream (e.g., giving the page access to the local camera) and then disabling the stream (e.g., revoking that access).

Example 1
<input type="button" value="Start" onclick="start()" id="startBtn">
<script>
 var startBtn = document.getElementById('startBtn');
 function start() {
   navigator.getUserMedia({audio:true, video:true}, gotStream);
   startBtn.disabled = true;
 }
 function gotStream(stream) {
   stream.onended = function () {
     startBtn.disabled = false;
   };
 }
</script> 

This example allows people to take photos of themselves from the local video camera.

Example 2
<article>
 <style scoped>
  video { transform: scaleX(-1); }
  p { text-align: center; }
 </style>
 <h1>Snapshot Kiosk</h1>
 <section id="splash">
  <p id="errorMessage">Loading...</p>
 </section>
 <section id="app" hidden>
  <p><video id="monitor" autoplay></video> <canvas id="photo"></canvas>
  <p><input type=button value="&#x1F4F7;" onclick="snapshot()">
 </section>
 <script>
  navigator.getUserMedia({video:true}, gotStream, noStream);
  var video = document.getElementById('monitor');
  var canvas = document.getElementById('photo');
  function gotStream(stream) {
    video.src = URL.createObjectURL(stream);
    video.onerror = function () {
      stream.stop();
    };
    stream.onended = noStream;
    video.onloadedmetadata = function () {
      canvas.width = video.videoWidth;
      canvas.height = video.videoHeight;
      document.getElementById('splash').hidden = true;
      document.getElementById('app').hidden = false;
    };
  }
  function noStream() {
    document.getElementById('errorMessage').textContent = 'No camera available.';
  }
  function snapshot() {
    canvas.getContext('2d').drawImage(video, 0, 0);
  }
 </script>
</article>

10. IANA Registrations

10.1 Constraint Registrations

IANA is requested to register the following constraints as specified in [RTCWEB-CONSTRAINTS]:

Note

Note: The following specific list(s) of constraints DOES NOT REFLECT CONSENSUS. Many constraints beyond these have been proposed, and the ones listed do not have universal support. The ones below are included **** ONLY **** to provide a starting point so we can see concrete examples of what real constraints might look like. Along those lines, there was an attempt to include constraints of a variety of types so sample type definitions could be included as well (e.g., DOMString, MinMaxConstraint).

The following constraint names are defined to apply to both VideoStreamTrack and AudioStreamTrack objects:

Constraint Name Values Notes
sourceType SourceTypeEnum Constrain the video or audio source to an exact value from the set of enumerated-type values of the SourceTypeEnum.
sourceId DOMString Constrain the video or audio source to an exact source identifier value.

The following constraint names are defined to apply only to VideoStreamTrack objects:

Constraint Name Values Notes
width unsigned long or MinMaxConstraint Constrain the video source to the exact desired width or width range.
height unsigned long or MinMaxConstraint Constrain the video source to the exact desired height or height range.
frameRate float or MinMaxConstraint Constrain the video source to the exact desired frame rate (fps) or frameRate range.
facingMode VideoFacingModeEnum Constrain the video source to an exact value from the set of enumerated-type values of the VideoFacingModeEnum.

The following constraint names are defined to apply only to AudioStreamTrack objects:

Constraint Name Values Notes
volume unsigned long or MinMaxConstraint Constrain the audio source to the exact desired volume or volume range.

For constraints that accept ranges, the MinMaxConstraint dictionary is also defined. Note that the type of the value associated with min and max must be the same for both. The specific types associated with min and max are defined differently for each constraint name.

dictionary MinMaxConstraint {
    any max;
    any min;
};

10.1.1 Dictionary MinMaxConstraint Members

max of type any
The related constraint's maximum allowed value.
min of type any
The related constraint's minimum allowed value.

11. Change Log

This section will be removed before publication.

Changes since December 12, 2012

  1. Changed error code to be string instead of number.
  2. Added core of settings proposal allowing for constraint changes after stream/track creation.

November 15 2012

  1. Introduced new representation of tracks in a stream (removed MediaStreamTrackList).
  2. Updated MediaStreamTrack.readyState to use an enum type (instad of unsigned short constants).
  3. Renamed MediaStream.label to MediaStream.id (the definition needs some more work).

October 1 2012

  1. Limited the track kind values to "audio" and "video" only (could previously be user defined as well).
  2. Made MediaStream extend EventTarget.
  3. Simplified the MediaStream constructor.

June 23 2012

  1. Rename title to "Media Capture and Streams".
  2. Update document to comply with HTML5.
  3. Update image describing a MediaStream.
  4. Add known issues and various other editorial changes.

June 22 2012

  1. Update wording for constraints algorithm.

June 19 2012

  1. Added "Media Streams as Media Elements section".

June 12 2012

  1. Switch to respec v3.

June 5 2012

  1. Added non-normative section "Implementation Suggestions".
  2. Removed stray whitespace.

June 1 2012

  1. Added media constraint algorithm.

Apr 23 2012

  1. Remove MediaStreamRecorder.

Apr 20 2012

  1. Add definitions of MediaStreams and related objects.

Dec 21 2011

  1. Changed to make wanted media opt in (rather than opt out). Minor edits.

Nov 29 2011

  1. Changed examples to use MediaStreamOptions objects rather than strings. Minor edits.

Nov 15 2011

  1. Removed MediaStream stuff. Refers to webrtc 1.0 spec for that part instead.

Nov 9 2011

  1. Created first version by copying the webrtc spec and ripping out stuff. Put it on github.

A. Acknowledgements

B. References

B.1 Normative references

[FILE-API]
Arun Ranganathan; Jonas Sicking. File API. 25 October 2012. W3C Working Draft. URL: http://www.w3.org/TR/2012/WD-FileAPI-20121025
[HTML5]
Robin Berjon et al. HTML5. 17 December 2012. W3C Candidate Recommendation. URL: http://www.w3.org/TR/html5/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Internet RFC 2119. URL: http://www.ietf.org/rfc/rfc2119.txt
[RTCWEB-CONSTRAINTS]
D. Burnett. IANA Registry for RTCWeb Media Constraints. URL: http://datatracker.ietf.org/doc/draft-burnett-rtcweb-constraints-registry/
[WEBIDL]
Cameron McCormack. Web IDL. 27 September 2011. W3C Working Draft. URL: http://www.w3.org/TR/2011/WD-WebIDL-20110927/
[WEBRTC10]
A Bergkvist; D Burnett; C Jennings; A Narayanan. WebRTC 1.0. 27 October 2011. W3C Working Draft. URL: http://www.w3.org/TR/2011/WD-webrtc-20111027/