Initial Author of this Specification was Ian Hickson, Google Inc., with the following copyright statement:
 © Copyright 2004-2011 Apple Computer, Inc., Mozilla Foundation, and Opera Software ASA. You are granted a license to use, reproduce and create derivative works of this document.
All subsequent changes since 26 July 2011 done by the W3C WebRTC Working Group and the Device APIs Working Group are under the following Copyright:
© 2011-2012 W3C® (MIT, ERCIM, Keio), All Rights Reserved. Document use  rules apply.
For the entire publication on the W3C site the liability and trademark rules apply.
This document defines a set of JavaScript APIs that allow local media, including audio and video, to be requested from a platform.
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 feedback is 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.
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.
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.
        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].
The MediaStreamMediaStream
Each MediaStream
Each track in a MediaStream object has a corresponding
      MediaStreamTrack
A MediaStreamTrack
A channel is the smallest unit considered in this API specification.
A MediaStreamLocalMediaStreamgetUserMedia() 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
A MediaStream
The output of a MediaStream
A new MediaStreamMediaStreamTrackMediaStream() constructor.
      The constructor takes two lists of MediaStreamTrackMediaStreamTrackMediaStream

The ability to duplicate a MediaStreamMediaStreamMediaStream
The LocalMediaStream
When a LocalMediaStreamMediaStream 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.
The  MediaStream()
      constructor takes zero or one argument. If the argument is supplied, it
      must either be of type MediaStreamMediaStreamTrack
Let stream be a newly constructed 
           object.MediaStream
Initialize stream’s label attribute to a newly generated value.
If the constructor’s argument is present and not null, run the sub steps that corresponds to the argument type.
Array of MediaStreamTrack
Run the following sub steps for each 
               in the array:MediaStreamTrack
Add track: Let track be the 
                    about to be processed.MediaStreamTrack
If track has ended or if there is already
                  a MediaStreamTrack
Create a new MediaStreamTrackkind and
                  label
                  attributes. Append the new MediaStreamTrack
                  audioTracks
                   or 
                  videoTracks)
                  in stream according to kind.
Run the sub steps labeled Add track (above) for every
              MediaStreamTrackaudioTracks
               and 
              videoTracks).
              
Return stream.
A MediaStreamMediaStream
The tracks of a MediaStreamMediaStreamTrackListMediaStreamTrack
An object that reads data from the output of a
      MediaStreamMediaStreamMediaStreamPeerConnection API specified in
      [WEBRTC10].
MediaStream
A MediaStreamstop() method being
      invoked, the user agent must queue a task that runs the following
      steps:
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.)
Set the object’s ended attribute to true.
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;MediaStreamTrack type.[Constructor (),
        Constructor (MediaStream? stream),
        Constructor (MediaStreamTrackArray tracks)]
interface MediaStream : EventTarget {
    readonly attribute DOMString            label;
    readonly attribute MediaStreamTrackList audioTracks;
    readonly attribute MediaStreamTrackList videoTracks;
             attribute boolean              ended;
             attribute EventHandler         onended;
};audioTracks of type MediaStreamTrackList, readonlyReturns a MediaStreamTrackList
The audioTracks
          attribute must return an array host
          object for objects of type
          MediaStreamTrack
ended of type booleanThe MediaStream.ended
          attribute must return true if the MediaStream
When a MediaStreamended attribute
          must be set to false, unless it is being created using the
          MediaStream() constructor
          whose arguments are lists of MediaStreamTrackMediaStreamended attribute set
          to true.
label of type DOMString, readonlyWhen a LocalMediaStreamlabel 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.
When a MediaStreamMediaStream()
          constructor, the label attribute is initialized to
          a newly generated value.
The label
          attribute must return the value to which it was initialized when the
          object was created.
onended of type EventHandlerended, must be supported by all
        objects implementing the MediaStreamvideoTracks of type MediaStreamTrackList, readonlyReturns a MediaStreamTrackList
The videoTracks
          attribute must return an array host
          object for objects of type
          MediaStreamTrack
Before the web application can access the user's media input devices
      it must let getUserMedia() create a
      LocalMediaStreamstop() on the
      LocalMediaStream
A web application may, once it has access to a
      LocalMediaStreamMediaStream() constructor to construct
      additional MediaStreamMediaStream
interface LocalMediaStream : MediaStream {
    void stop ();
};stopWhen a LocalMediaStreamstop() method is invoked,
          the user agent must queue a task that runs the following steps on
          every track:
Let track be the current
              MediaStreamTrack
End track. The track starts outputting only silence and/or blackness, as appropriate.
Dereference track’s underlying media source.
If the reference count of track’s underlying media source is greater than zero, then abort these steps.
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.
voidA MediaStreamTrackMediaStreamTrackgetUserMedia()
      .
A MediaStreamTrackMediaStreamLocalMediaStreamMediaStream() constructor, has a weak
      reference to a local media source, while a track in a
      LocalMediaStreamMediaStreamLocalMediaStreamLocalMediaStream
The concept with strong and weak references to media
      sources allows the web application to derive new
      MediaStreamLocalMediaStreamgetUserMedia()) and still be
      able to revoke all given permissions with LocalMediaStream.stop().
A MediaStreamTrack
When a MediaStreamTrackstop() method being invoked on the
      LocalMediaStream
If the track’s readyState attribute
          has the value ENDED (2) already, then
          abort these steps.
Set track’s readyState attribute to
          ENDED
          (2).
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.
interface MediaStreamTrack {
    readonly attribute DOMString      kind;
    readonly attribute DOMString      label;
             attribute boolean        enabled;
    const unsigned short LIVE = 0;
    const unsigned short MUTED = 1;
    const unsigned short ENDED = 2;
    readonly attribute unsigned short readyState;
             attribute EventHandler   onmute;
             attribute EventHandler   onunmute;
             attribute EventHandler   onended;
};enabled of type booleanThe 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
Thus, after a MediaStreamTrackenabled attribute still
          changes value when set; it just doesn’t do anything with that new
          value.
kind of type DOMString, readonlyThe 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, readonlyUser 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.
Thus the kind and label attributes do not
          change value, even if the MediaStreamTrack
onended of type EventHandlerended, must be supported by
        all objects implementing the MediaStreamTrackonmute of type EventHandlermuted, must be supported by
        all objects implementing the MediaStreamTrackonunmute of type EventHandlerunmuted, must be supported
        by all objects implementing the MediaStreamTrackreadyState of type unsigned short, readonlyThe readyState
          attribute represents the state of the track. It must return the value
          to which the user agent last set it (as defined below). It can have
          the following values: LIVE, MUTED or
          ENDED.
When a MediaStreamTrackreadyState is either
          LIVE (0) or
          MUTED (1),
          depending on the state of the track’s underlying media source. For
          example, a track in a LocalMediaStreamgetUserMedia(), must
          initially have its readyState attribute
          set to LIVE
          (1).
ENDED of type unsigned shortThe track has ended (the track’s underlying media source is no longer providing data, and will never provide more data for this track).
For example, a video track in a
          LocalMediaStream
LIVE of type unsigned shortThe 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 of type unsigned shortThe track is muted (the track’s underlying media source is temporarily unable to provide data).
A MediaStreamTrackLocalMediaStream
partial interface URL {
    static DOMString createObjectURL (MediaStream stream);
};createObjectURL, staticMints a Blob URL to refer to the given
          MediaStream
When the createObjectURL() method
          is called with a MediaStreamMediaStream
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 MediaStreamLocalMediaStream
| Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| stream |  | ✘ | ✘ | 
DOMStringA MediaStreamTrackListMediaStreamMediaStreamMediaStreamTrackList
interface MediaStreamTrackList {
    readonly attribute unsigned long length;
    MediaStreamTrack item (unsigned long index);
    void             add (MediaStreamTrack track);
    void             remove (MediaStreamTrack track);
             attribute EventHandler  onaddtrack;
             attribute EventHandler  onremovetrack;
};length of type unsigned long, readonlyonaddtrack of type EventHandleraddtrack, must be
        supported by all objects implementing the
        MediaStreamTrackListonremovetrack of type EventHandlerremovetrack, must
        be supported by all objects implementing the
        MediaStreamTrackListaddAdds the given MediaStreamTrackMediaStreamTrackList
When the add() method is
          invoked, the user agent must run the following steps:
Let track be the
              MediaStreamTrack
Let stream be the
              MediaStreamTrackListMediaStream
If stream is finished, throw an
              INVALID_STATE_ERR exception.
If track is already in the
              MediaStreamTrackList
Add track to the end of the
              MediaStreamTrackList
| Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| track |  | ✘ | ✘ | 
voiditemMediaStreamTrack| Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| index | unsigned long | ✘ | ✘ | 
MediaStreamTrackremoveRemoves the given MediaStreamTrackMediaStreamTrackList
When the remove() method
          is invoked, the user agent must run the following steps:
Let track be the
              MediaStreamTrack
Let stream be the
              MediaStreamTrackListMediaStream
If stream is finished, throw an
              INVALID_STATE_ERR exception.
If track is not in the
              MediaStreamTrackList
Remove track from the
              MediaStreamTrackList
| Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| track |  | ✘ | ✘ | 
voidA 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.
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:
media.readystate to HAVE_ENOUGH_DATA.
        | 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.lengthmust return1.buffered.start(0)must return0.buffered.end(0)must return0. | 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  | 
| 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.0and 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.0and any attempt to alter it must fail. | 
| played | TimeRanges | played.lengthmust return1.played.start(0)must return0.played.end(0)must return the last knowncurrentTime. | A MediaStream's timeline always consists of a single range, starting at 0 and extending up to the currentTime. | 
| seekable | TimeRanges | seekable.lengthmust return0.seekable.start()must returncurrentTime.seekable.end()must returncurrentTime. | 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. | 
This section is non-normative.
The following event fires on 
           objects:MediaStream
        
| Event name | Interface | Fired when... | 
|---|---|---|
| ended | Event | The 
                finished as a result of all tracks in the
                ending. | 
The following event fires on 
           objects:MediaStreamTrack
        
| Event name | Interface | Fired when... | 
|---|---|---|
| muted | Event | The 
                object's source is temporarily unable to provide
            data. | 
| unmuted | Event | The 
                object's source is live again after having been
            temporarily unable to provide data. | 
| ended | Event | The 
                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. | 
The following event fires on 
           objects:MediaStreamTrackList
        
| Event name | Interface | Fired when... | 
|---|---|---|
| addtrack | 
                MediaStreamTrackEvent
               | A new 
                has been added to this list. | 
| removetrack | 
                MediaStreamTrackEvent
               | A 
                has been removed from this list. | 
dictionary MediaStreamConstraints {
    (boolean or MediaTrackConstraints) video = false;;
    (boolean or MediaTrackConstraints) audio = false;;
};MediaStreamConstraints Membersaudio of type booleanMediaTrackConstraints, defaulting to false;Provide definition of audio constraints here.
video of type booleanMediaTrackConstraints, defaulting to false;Provide definition of video constraints here.
dictionary MediaTrackConstraints {
    MediaTrackConstraintSet? mandatory = null;;
    MediaTrackConstraint[]?  optional = null;;
};MediaTrackConstraints Membersmandatory of type MediaTrackConstraintSet, nullable, defaulting to null;Provide definition of mandatory constraints here.
optional of type array of MediaTrackConstraint, nullable, defaulting to null;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.
This section is non-normative.
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.
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.
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).
<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.
<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="📷" 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>
IANA is requested to register the following constraints as specified in [RTCWEB-CONSTRAINTS]:
This section will be removed before publication.