forked from w3c/mediacapture-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getusermedia.html
5632 lines (5619 loc) · 271 KB
/
getusermedia.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en-us">
<head>
<link href="getusermedia.css" rel="stylesheet" type="text/css">
<title>Media Capture and Streams</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<script class="remove" src=
"https://www.w3.org/Tools/respec/respec-w3c-common" type="text/javascript">
//<![CDATA[
<!-- keep this comment -->
//]]>
</script>
<script class="remove" src="getusermedia.js" type="text/javascript">
//<![CDATA[
<!-- keep this comment -->
//]]>
</script>
</head>
<body>
<section id="abstract">
<p>This document defines a set of JavaScript APIs that allow local media,
including audio and video, to be requested from a platform.</p>
</section>
<section id="sotd">
<p>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.</p>
</section>
<section class="informative" id="intro">
<h2>Introduction</h2>
<p>This document defines APIs for requesting access to local multimedia
devices, such as microphones or video cameras.</p>
<p>This document also defines the MediaStream API, which provides the means
to control where multimedia stream data is consumed, and provides some
control over the devices that produce the media. It also exposes
information about devices able to capture and render media.</p>
</section>
<section id="conformance">
<p>This specification defines conformance criteria that apply to a single
product: the <dfn>User Agent</dfn> that implements the interfaces that it
contains.</p>
<p>Conformance requirements phrased as algorithms or specific steps may be
implemented in any manner, so long as the end result is equivalent. (In
particular, the algorithms defined in this specification are intended to be
easy to follow, and not intended to be performant.)</p>
<p>Implementations that use ECMAScript [[ECMA-262]] 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-1]], as this specification uses that specification and
terminology.</p>
</section>
<section>
<h2>Terminology</h2>
<dl>
<dt><i>HTML Terms:</i></dt>
<dd>
<p>The <code><a href=
"http://dev.w3.org/html5/spec/webappapis.html#eventhandler">EventHandler</a></code>
interface represents a callback used for event handlers as defined in
[[!HTML5]].</p>
<p>The concepts <dfn><a href=
"http://dev.w3.org/html5/spec/webappapis.html#queue-a-task">queue a
task</a></dfn> and <dfn><a href=
"http://dev.w3.org/html5/spec/webappapis.html#fire-a-simple-event">fires
a simple event</a></dfn> are defined in [[!HTML5]].</p>
<p>The terms <dfn><a href=
"http://dev.w3.org/html5/spec/webappapis.html#event-handlers">event
handlers</a></dfn> and <dfn><a href=
"http://dev.w3.org/html5/spec/webappapis.html#event-handler-event-type">
event handler event types</a></dfn> are defined in [[!HTML5]].</p>
</dd>
<dt><code><dfn>DOMException</dfn></code></dt>
<dd>
The term <code>DOMException</code> is defined in <a href=
"https://www.w3.org/TR/WebIDL-1/#idl-DOMException">WebIDL</a>
[[!WEBIDL-1]]
</dd>
<dt><dfn>source</dfn></dt>
<dd>
<p>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. Note that this
document describes the use of microphone and camera type sources only,
the use of other source types is described in other documents.</p>
<p>An application that has no prior authorization regarding sources is
only given the number of available sources, their type and any
relationship to other devices. Additional information about sources can
become available when applications are authorized to use a source (see
<a href="#access-control-model"></a>).</p>
<p>Sources <strong>do not</strong> have constraints — tracks have
constraints. When a source is connected to a track, it must produce
media that conforms to the constraints present on that track. Multiple
tracks can be attached to the same source. User Agent processing, such
as downsampling, MAY be used to ensure that all tracks have appropriate
media.</p>
<p>Sources have constrainable properties which have
<code><a>capabilities</a></code> and <code><a>settings</a></code>. The
constrainable properties 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 track objects bound to the same source ask for the same
capability or setting information, they will get back the same
answer).</p>
</dd>
<dt>Setting (Source Setting)</dt>
<dd>
<p>A <a href="#settings">setting</a> refers to the immediate, current
value of the source's constrainable properties. Settings are always
read-only.</p>
<p>A source's settings can change dynamically over time due to
environmental conditions, sink configurations, or constraint changes. A
source's settings must always conform to the current set of mandatory
constraints on all attached tracks. A source that cannot conform to
mandatory constraints causes affected tracks to become
<a>overconstrained</a> and therefore <a href="#track-muted">muted</a>.
A <a>User Agent</a> attempts to ensure that sources adhere to optional
constraints as closely as possible, see <a href=
"#constrainable-interface"></a>.</p>
<p>Although settings are a property of the source, they are only
exposed to the application through the tracks attached to the source.
This is exposed via the <a>ConstrainablePattern</a> interface.</p>
</dd>
<dt>Capabilities</dt>
<dd>
<p>For each constrainable property, there is a capability that
describes whether it is supported by the source and if so, the range of
supported values. As with settings, capabilities are exposed to the
application via the <a>ConstrainablePattern</a> interface.</p>
<p>The values of the supported capabilities must be normalized to the
ranges and enumerated types defined in this specification.</p>
<p>A <a>getCapabilities()</a> call on a track returns the same
underlying per-source capabilities for all tracks connected to the
source.</p>
<p>Source capabilities are effectively constant. Applications should be
able to depend on a specific source having the same capabilities for
any browsing session.</p>
<p>This API is intentionally simplified. Capabilities are not capable
of describing interactions between different values. For instance, it
is not possible to accurately describe the capabilities of a camera
that can produce a high resolution video stream at a low frame rate and
lower resolutions at a higher frame rate. Capabilities describe the
complete range of each value. Interactions between constraints are
exposed by attempting to apply constraints.</p>
</dd>
<dt><dfn>Constraints</dfn></dt>
<dd>
<p>Constraints provide a general control surface that allows
applications to both select an appropriate source for a track and, once
selected, to influence how a source operates.</p>
<p>Constraints limit the range of operating modes that a source can use
when providing media for a track. Without provided track constraints,
implementations are free to select a source's settings from the full
ranges of its supported capabilities. Implementations may also adjust
source settings at any time within the bounds imposed by all applied
constraints.</p>
<p><a>getUserMedia()</a> uses constraints to help select an appropriate
source for a track and configure it. Additionally, the
<a>ConstrainablePattern</a> interface on tracks includes an API for
dynamically changing the track's constraints at any later time.</p>
<p>A track will not be connected to a source using
<a>getUserMedia()</a> if its initial constraints cannot be satisfied.
However, the ability to meet the constraints on a track can change over
time, and constraints can be changed. If circumstances change such that
constraints cannot be met, the <a>ConstrainablePattern</a> interface
defines an appropriate error to inform the application. <a href=
"#the-model-sources-sinks-constraints-and-settings"></a> explains how
constraints interact in more detail.</p>
<p>In general, User Agents will have more flexibility to optimize the
media streaming experience the fewer constraints are applied, so
application authors are strongly encouraged to use mandatory
constraints sparingly.</p>
<p>For each constrainable property, a constraint exists whose name
corresponds with the relevant source setting name and capability
name.</p>
</dd>
<dt><code>RTCPeerConnection</code></dt>
<dd><dfn><code>RTCPeerConnection</code></dfn> is defined in
[[WEBRTC10]].</dd>
<dt>Permissions</dt>
<dd>
<p>The terms <dfn>permission</dfn>, <dfn>retrieve the permission
state</dfn>, <dfn id="request-permission">request permission</dfn> and
<dfn id="create-permission">create a permission storage entry</dfn> are
defined in [[!permissions]].</p>
<p class="note">"request permission" isn't defined yet. Filed as
permissions <a href=
"https://github.com/w3c/permissions/issues/62">issue 62</a>.</p>
</dd>
</dl>
</section>
<section id="stream-api">
<h2>MediaStream API</h2>
<section>
<h2>Introduction</h2>
<p>The two main components in the MediaStream API are the
<code><a>MediaStreamTrack</a></code> and <code><a>MediaStream</a></code>
interfaces. The <code><a>MediaStreamTrack</a></code> object represents
media of a single type that originates from one media source in the User
Agent, e.g. video produced by a web camera. A
<code><a>MediaStream</a></code> is used to group several
<code><a>MediaStreamTrack</a></code> objects into one unit that can be
recorded or rendered in a media element.</p>
<p>Each <code><a>MediaStream</a></code> can contain zero or more
<code><a>MediaStreamTrack</a></code> objects. All tracks in a
<code><a>MediaStream</a></code> are intended to be synchronized when
rendered. This is not a hard requirement, since it might not be possible
to synchronize tracks from sources that have different clocks. Different
<code><a>MediaStream</a></code> objects do not need to be
synchronized.</p>
<p class="note">While the intent is to synchronize tracks, it could be
better in some circumstances to permit tracks to lose synchronization. In
particular, when tracks are remotely sourced and real-time [[WEBRTC10]],
it can be better to allow loss of synchronization than to accumulate
delays or risk glitches and other artifacts. Implementations are expected
to understand the implications of choices regarding synchronization of
playback and the effect that these have on user perception.</p>
<p>A single <code><a>MediaStreamTrack</a></code> can represent
multi-channel content, such as stereo or 5.1 audio or stereoscopic video,
where the channels have a well defined relationship to each other.
Information about channels might be exposed through other APIs, such as
[[WEBAUDIO]], but this specification provides no direct access to
channels.</p>
<p>A <code><a>MediaStream</a></code> object has an input and an output
that represent the combined input and output of all the object's tracks.
The output of the <code><a>MediaStream</a></code> controls how the object
is rendered, e.g., what is saved if the object is recorded to a file or
what is displayed if the object is used in a <code>video</code> element.
A single <code><a>MediaStream</a></code> object can be attached to
multiple different outputs at the same time.</p>
<p>A new <code><a>MediaStream</a></code> object can be created from
existing media streams or tracks using the
<code><a>MediaStream()</a></code> constructor. The constructor argument
can either be an existing <code><a>MediaStream</a></code> object, in
which case all the tracks of the given stream are added to the new
<code><a>MediaStream</a></code> object, or an array of
<code><a>MediaStreamTrack</a></code> objects. The latter form makes it
possible to compose a stream from different source streams.</p>
<p>Both <code><a>MediaStream</a></code> and
<code><a>MediaStreamTrack</a></code> objects can be cloned. A cloned
<code><a>MediaStream</a></code> contains clones of all member tracks from
the original stream. A cloned <code><a>MediaStreamTrack</a></code> has a
<a href="#constrainable-interface">set of constraints</a> that is
independent of the instance it is cloned from, which allows media from
the same source to have different constraints applied for different
<a>consumer</a>s. The <code>MediaStream</code> object is also used in
contexts outside <code>getUserMedia</code>, such as [[WEBRTC10]].</p>
</section>
<section>
<h2>MediaStream</h2>
<p>The <dfn id="dom-mediastream"><code>MediaStream()</code></dfn>
constructor composes a new stream out of existing tracks. It takes an
optional argument of type <code><a>MediaStream</a></code> or an array of
<code><a>MediaStreamTrack</a></code> objects. <dfn id=
"mediastream-constructor">When the constructor is invoked</dfn>, the User
Agent must run the following steps:</p>
<ol>
<li>
<p>Let <var>stream</var> be a newly constructed
<code><a>MediaStream</a></code> object.</p>
</li>
<li>
<p>Initialize <var>stream's</var> <code><a href=
"#dom-mediastream-id">id</a></code> attribute to a newly generated
value.</p>
</li>
<li>
<p>If the constructor's argument is present, construct a set of
tracks, <var>tracks</var> based on the type of argument:</p>
<ul>
<li>
<p>A <code><a>MediaStream</a></code> object:</p>
<p>Let <var>tracks</var> be a set containing all the
<code><a>MediaStreamTrack</a></code> objects in the
<code><a>MediaStream</a></code> <a href="#track-set">track
set</a>.</p>
</li>
<li>
<p>A sequence of <code><a>MediaStreamTrack</a></code>
objects:</p>
<p>Let <var>tracks</var> be a set containing all the
<code><a>MediaStreamTrack</a></code> objects in the provided
sequence.</p>
</li>
</ul>
</li>
<li>
<p><a href="#stream-add-track">Add each track</a> in
<var>tracks</var> to <var>stream</var>.</p>
</li>
<li>
<p>Return <var>stream</var>.</p>
</li>
</ol>
<p>The tracks of a <code><a>MediaStream</a></code> are stored in a
<dfn id="track-set">track set</dfn>. The track set MUST contain the
<code><a>MediaStreamTrack</a></code> 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 <code><a>MediaStreamTrack</a></code>
object in the set is to look it up by its <code><a href=
"#dom-mediastreamtrack-id">id</a></code>.</p>
<p>An object that reads data from the output of a
<code><a>MediaStream</a></code> is referred to as a
<code><a>MediaStream</a></code> <dfn>consumer</dfn>. The list of
<code><a>MediaStream</a></code> consumers currently include media
elements (such as <code><video></code> and
<code><audio></code>) [[HTML5]], Web Real-Time Communications
(WebRTC; <code>RTCPeerConnection</code>) [[WEBRTC10]], media recording
(<code>MediaRecorder</code>) [[mediastream-recording]], image capture
(<code>ImageCapture</code>) [[image-capture]], and web audio
(<code>MediaStreamAudioSourceNode</code>) [[WEBAUDIO]].</p>
<p class="note"><code><a>MediaStream</a></code> consumers must be able to
handle tracks being added and removed. This behavior is specified per
consumer.</p>
<p>A <code><a>MediaStream</a></code> object is said to be <dfn id=
"stream-active">active</dfn> when it has at least one
<code><a>MediaStreamTrack</a></code> that has not <a href=
"#track-ended">ended</a>. A <code><a>MediaStream</a></code> that does not
have any tracks or only has tracks that are <a href=
"#track-ended">ended</a> is <dfn id="stream-inactive">inactive</dfn>.</p>
<p>To <dfn id="stream-add-track">add a track</dfn> to a
<code><a>MediaStream</a></code>, the User Agent MUST run the following
steps:</p>
<ol>
<li>
<p>Let <var>track</var> be the <code><a>MediaStreamTrack</a></code>
in question and <var>stream</var> the <code><a>MediaStream</a></code>
object to which <var>track</var> is to be added.</p>
</li>
<li>
<p>If <var>track</var> is already in <var>stream's</var> <a href=
"#track-set">track set</a>, then abort these steps.</p>
</li>
<li>
<p>Add <var>track</var> to <var>stream</var>'s <a href=
"#track-set">track set</a>.</p>
</li>
</ol>
<p>To <dfn id="stream-remove-track">remove a track</dfn> from a
<code><a>MediaStream</a></code>, the User Agent MUST run the following
steps:</p>
<ol>
<li>
<p>Let <var>track</var> be the <code><a>MediaStreamTrack</a></code>
in question and <var>stream</var> the <code><a>MediaStream</a></code>
object from which <var>track</var> is to be removed.</p>
</li>
<li>
<p>If <var>track</var> is not in <var>stream's</var> <a href=
"#track-set">track set</a>, then abort these steps.</p>
</li>
<li>
<p>Remove <var>track</var> from <var>stream</var>'s <a href=
"#track-set">track set</a>.</p>
</li>
</ol>
<p>The User Agent may update a <code><a>MediaStream</a></code>'s <a href=
"#track-set">track set</a> in response to, for example, an external
event. This specification does not specify any such cases, but other
specifications using the MediaStream API may. One such example is the
WebRTC 1.0 [[WEBRTC10]] specification where the <a href=
"#track-set">track set</a> of a <code><a>MediaStream</a></code>, received
from another peer, can be updated as a result of changes to the media
session.</p>
<p>When the User Agent initiates adding a track to a
<code><a>MediaStream</a></code>, with the exception of initializing a
newly created <code><a>MediaStream</a></code> with tracks, the User Agent
MUST queue a task that runs the following steps:</p>
<ol>
<li>
<p>Let <var>track</var> be the <code><a>MediaStreamTrack</a></code>
in question and <var>stream</var> the <code><a>MediaStream</a></code>
object to which <var>track</var> is to be added.</p>
</li>
<li>
<p><a href="#stream-add-track">Add</a> <var>track</var> to
<var>stream</var>.</p>
</li>
<li>
<p>If the operation in the previous step was aborted prematurely,
then abort these steps.</p>
</li>
<li>
<p>Fire a track event named <code><a href=
"#event-mediastream-addtrack">addtrack</a></code> with
<var>track</var> at <var>stream</var>.</p>
</li>
</ol>
<p>When the User Agent initiates removing a track from a
<code><a>MediaStream</a></code>, the User Agent MUST queue a task that
runs the following steps:</p>
<ol>
<li>
<p>Let <var>track</var> be the <code><a>MediaStreamTrack</a></code>
in question and <var>stream</var> the <code><a>MediaStream</a></code>
object to which <var>track</var> is to be added.</p>
</li>
<li>
<p><a href="#stream-remove-track">Remove</a> <var>track</var> from
<var>stream</var>.</p>
</li>
<li>
<p>If the operation in the previous step was aborted prematurely,
then abort these steps.</p>
</li>
<li>
<p>Fire a track event named <code><a href=
"#event-mediastream-removetrack">removetrack</a></code> with
<var>track</var> at <var>stream</var>.</p>
</li>
</ol>
<div>
<pre class="idl">[Exposed=Window,
Constructor,
Constructor (MediaStream stream),
Constructor (sequence<MediaStreamTrack> tracks)]
interface MediaStream : EventTarget {
readonly attribute DOMString id;
sequence<MediaStreamTrack> getAudioTracks ();
sequence<MediaStreamTrack> getVideoTracks ();
sequence<MediaStreamTrack> getTracks ();
MediaStreamTrack? getTrackById (DOMString trackId);
void addTrack (MediaStreamTrack track);
void removeTrack (MediaStreamTrack track);
MediaStream clone ();
readonly attribute boolean active;
attribute EventHandler onaddtrack;
attribute EventHandler onremovetrack;
};</pre>
<section>
<h2>Constructors</h2>
<dl data-link-for="MediaStream" data-dfn-for="MediaStream" class=
"constructors">
<dt><code>MediaStream</code></dt>
<dd>
<p>See the <a href="#mediastream-constructor">MediaStream
constructor algorithm</a></p>
<div>
<em>No parameters.</em>
</div>
</dd>
<dt><code>MediaStream</code></dt>
<dd>
<p>See the <a href="#mediastream-constructor">MediaStream
constructor algorithm</a></p>
<table class="parameters">
<tbody>
<tr>
<th>Parameter</th>
<th>Type</th>
<th>Nullable</th>
<th>Optional</th>
<th>Description</th>
</tr>
<tr>
<td class="prmName">stream</td>
<td class="prmType"><code>MediaStream</code></td>
<td class="prmNullFalse"><span role="img" aria-label=
"False">✘</span></td>
<td class="prmOptFalse"><span role="img" aria-label=
"False">✘</span></td>
<td class="prmDesc"></td>
</tr>
</tbody>
</table>
</dd>
<dt><code>MediaStream</code></dt>
<dd>
<p>See the <a href="#mediastream-constructor">MediaStream
constructor algorithm</a></p>
<table class="parameters">
<tbody>
<tr>
<th>Parameter</th>
<th>Type</th>
<th>Nullable</th>
<th>Optional</th>
<th>Description</th>
</tr>
<tr>
<td class="prmName">tracks</td>
<td class="prmType">
<code>sequence<MediaStreamTrack></code></td>
<td class="prmNullFalse"><span role="img" aria-label=
"False">✘</span></td>
<td class="prmOptFalse"><span role="img" aria-label=
"False">✘</span></td>
<td class="prmDesc"></td>
</tr>
</tbody>
</table>
</dd>
</dl>
</section>
<section>
<h2>Attributes</h2>
<dl data-link-for="MediaStream" data-dfn-for="MediaStream" class=
"attributes">
<dt><code>id</code> of type <span class=
"idlAttrType"><a>DOMString</a></span>, readonly</dt>
<dd>
<p>When a <code><a>MediaStream</a></code> object is created, the
User Agent MUST generate an identifier string, and MUST
initialize the object's <code><a href=
"#dom-mediastream-id">id</a></code> attribute to that string. A
good practice is to use a UUID [[rfc4122]], which is 36
characters long in its canonical form. To avoid fingerprinting,
implementations SHOULD use the forms in section 4.4 or 4.5 of RFC
4122 when generating UUIDs.</p>
<p>The <dfn data-lt-nodefault="" id=
"dom-mediastream-id"><code>id</code></dfn> attribute MUST return
the value to which it was initialized when the object was
created.</p>
</dd>
<dt><dfn><code>active</code></dfn> of type <span class=
"idlAttrType"><a>boolean</a></span>, readonly</dt>
<dd>
<p>The <code>active</code> attribute MUST return
<code>true</code> if this <code><a>MediaStream</a></code> is
<a href="#stream-active">active</a> and <code>false</code>
otherwise.</p>
</dd>
<dt><dfn><code>onaddtrack</code></dfn> of type <span class=
"idlAttrType"><a>EventHandler</a></span></dt>
<dd>
<p>The event type of this event handler is <code><a href=
"#event-mediastream-addtrack">addtrack</a></code>.</p>
</dd>
<dt><dfn><code>onremovetrack</code></dfn> of type <span class=
"idlAttrType"><a>EventHandler</a></span></dt>
<dd>
<p>The event type of this event handler is <code><a href=
"#event-mediastream-removetrack">removetrack</a></code>.</p>
</dd>
</dl>
</section>
<section>
<h2>Methods</h2>
<dl data-link-for="MediaStream" data-dfn-for="MediaStream" class=
"methods">
<dt><code>getAudioTracks</code></dt>
<dd>
<p>Returns a sequence of <code><a>MediaStreamTrack</a></code>
objects representing the audio tracks in this stream.</p>
<p>The <dfn id=
"dom-mediastream-getaudiotracks"><code>getAudioTracks()</code></dfn>
method MUST return a sequence that represents a snapshot of all
the <code><a>MediaStreamTrack</a></code> objects in this stream's
<a href="#track-set">track set</a> whose <code><a href=
"#dom-mediastreamtrack-kind">kind</a></code> is equal to
"<code>audio</code>". The conversion from the <a href=
"#track-set">track set</a> to the sequence is User Agent defined
and the order does not have to be stable between calls.</p>
<div>
<em>No parameters.</em>
</div>
<div>
<em>Return type:</em>
<code>sequence<MediaStreamTrack></code>
</div>
</dd>
<dt><code>getVideoTracks</code></dt>
<dd>
<p>Returns a sequence of <code><a>MediaStreamTrack</a></code>
objects representing the video tracks in this stream.</p>
<p>The <dfn id=
"dom-mediastream-getvideotracks"><code>getVideoTracks()</code></dfn>
method MUST return a sequence that represents a snapshot of all
the <code><a>MediaStreamTrack</a></code> objects in this stream's
<a href="#track-set">track set</a> whose <code><a href=
"#dom-mediastreamtrack-kind">kind</a></code> is equal to
"<code>video</code>". The conversion from the <a href=
"#track-set">track set</a> to the sequence is User Agent defined
and the order does not have to be stable between calls.</p>
<div>
<em>No parameters.</em>
</div>
<div>
<em>Return type:</em>
<code>sequence<MediaStreamTrack></code>
</div>
</dd>
<dt><code>getTracks</code></dt>
<dd>
<p>Returns a sequence of <code><a>MediaStreamTrack</a></code>
objects representing all the tracks in this stream.</p>
<p>The <dfn id=
"dom-mediastream-gettracks"><code>getTracks()</code></dfn> method
MUST return a sequence that represents a snapshot of all the
<code><a>MediaStreamTrack</a></code> objects in this stream's
<a href="#track-set">track set</a>, regardless of <code><a href=
"#dom-mediastreamtrack-kind">kind</a></code>. The conversion from
the <a href="#track-set">track set</a> to the sequence is User
Agent defined and the order does not have to be stable between
calls.</p>
<div>
<em>No parameters.</em>
</div>
<div>
<em>Return type:</em>
<code>sequence<MediaStreamTrack></code>
</div>
</dd>
<dt><code>getTrackById</code></dt>
<dd>
<p>The <dfn id=
"dom-mediastream-gettrackbyid"><code>getTrackById()</code></dfn>
method MUST return either a <code><a>MediaStreamTrack</a></code>
object from this stream's <a href="#track-set">track set</a>
whose <code><a href="#dom-mediastreamtrack-id">id</a></code> is
equal to <var>trackId</var>, or null, if no such track
exists.</p>
<table class="parameters">
<tbody>
<tr>
<th>Parameter</th>
<th>Type</th>
<th>Nullable</th>
<th>Optional</th>
<th>Description</th>
</tr>
<tr>
<td class="prmName">trackId</td>
<td class="prmType"><code>DOMString</code></td>
<td class="prmNullFalse"><span role="img" aria-label=
"False">✘</span></td>
<td class="prmOptFalse"><span role="img" aria-label=
"False">✘</span></td>
<td class="prmDesc"></td>
</tr>
</tbody>
</table>
<div>
<em>Return type:</em> <code>MediaStreamTrack</code>, nullable
</div>
</dd>
<dt><code>addTrack</code></dt>
<dd>
<p>Adds the given <code><a>MediaStreamTrack</a></code> to this
<code><a>MediaStream</a></code>.</p>
<p>When the <dfn id=
"dom-mediastream-addtrack"><code>addTrack()</code></dfn> method
is invoked, the User Agent MUST <a href="#stream-add-track">add
the track</a>, specified by the method's first argument, to this
<code><a>MediaStream</a></code>.</p>
<table class="parameters">
<tbody>
<tr>
<th>Parameter</th>
<th>Type</th>
<th>Nullable</th>
<th>Optional</th>
<th>Description</th>
</tr>
<tr>
<td class="prmName">track</td>
<td class="prmType"><code>MediaStreamTrack</code></td>
<td class="prmNullFalse"><span role="img" aria-label=
"False">✘</span></td>
<td class="prmOptFalse"><span role="img" aria-label=
"False">✘</span></td>
<td class="prmDesc"></td>
</tr>
</tbody>
</table>
<div>
<em>Return type:</em> <code>void</code>
</div>
</dd>
<dt><code>removeTrack</code></dt>
<dd>
<p>Removes the given <code><a>MediaStreamTrack</a></code> object
from this <code><a>MediaStream</a></code>.</p>
<p>When the <dfn id=
"dom-mediastream-removetrack"><code>removeTrack()</code></dfn>
method is invoked, the User Agent MUST <a href=
"#stream-remove-track">remove the track</a>, specified by the
method's first argument, from this
<code><a>MediaStream</a></code>.</p>
<table class="parameters">
<tbody>
<tr>
<th>Parameter</th>
<th>Type</th>
<th>Nullable</th>
<th>Optional</th>
<th>Description</th>
</tr>
<tr>
<td class="prmName">track</td>
<td class="prmType"><code>MediaStreamTrack</code></td>
<td class="prmNullFalse"><span role="img" aria-label=
"False">✘</span></td>
<td class="prmOptFalse"><span role="img" aria-label=
"False">✘</span></td>
<td class="prmDesc"></td>
</tr>
</tbody>
</table>
<div>
<em>Return type:</em> <code>void</code>
</div>
</dd>
<dt><dfn><code>clone</code></dfn></dt>
<dd>
<p>Clones the given <code><a>MediaStream</a></code> and all its
tracks.</p>
<p>When the <code>clone()</code> method is invoked, the User
Agent MUST run the following steps:</p>
<ol>
<li>
<p>Let <var>streamClone</var> be a newly constructed
<code><a>MediaStream</a></code> object.</p>
</li>
<li>
<p>Initialize <var>streamClone</var>'s <code><a href=
"#dom-mediastream-id">id</a></code> attribute to a newly
generated value.</p>
</li>
<li>
<p><a href="#track-clone">Clone each track</a> in this
<code><a>MediaStream</a></code> object and add the result to
<var>streamClone</var>'s <a href="#track-set">track
set</a>.</p>
</li>
<li>Return <var>streamClone</var>.</li>
</ol>
<div>
<em>No parameters.</em>
</div>
<div>
<em>Return type:</em> <code>MediaStream</code>
</div>
</dd>
</dl>
</section>
</div>
</section>
<section>
<h2>MediaStreamTrack</h2>
<p>A <code><a>MediaStreamTrack</a></code> object represents a media
source in the User Agent. An example source is a device connected to the
User Agent. Other specifications may define sources for
<code><a>MediaStreamTrack</a></code> that override the behavior specified
here. Several <code><a>MediaStreamTrack</a></code> 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 <code><a>getUserMedia()</a></code>
.</p>
<p>The data from a <code><a>MediaStreamTrack</a></code> 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 in whatever fashion is most suitable on
the user's platform.</p>
<p>A script can indicate that a <code><a>MediaStreamTrack</a></code>
object no longer needs its source with the <code><a href=
"#dom-mediastreamtrack-stop">stop()</a></code> method. When all tracks
using a source have been stopped or ended by some other means, the source
is <dfn id="source-stopped">stopped</dfn>. If there is no stored
permission for the source, the User Agent SHOULD also remove the
"permission granted" indicator for the 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.
An implementation may use a per-source reference count to keep track of
source usage, but the specifics are out of scope for this
specification.</p>
<p>To <dfn id="track-clone">clone a track</dfn> the User Agent MUST run
the following steps:</p>
<ol>
<li>
<p>Let <var>track</var> be the <code><a>MediaStreamTrack</a></code>
object to be cloned.</p>
</li>
<li>
<p>Let <var>trackClone</var> be a newly constructed
<code><a>MediaStreamTrack</a></code> object.</p>
</li>
<li>
<p>Initialize <var>trackClone</var>'s <code><a href=
"#dom-mediastreamtrack-id">id</a></code> attribute to a newly
generated value.</p>
</li>
<li>
<p>Initialize <var>trackClone</var>'s <code><a href=
"#dom-mediastreamtrack-kind">kind</a></code>, <code><a href=
"#dom-mediastreamtrack-label">label</a></code>, <code><a href=
"#dom-mediastreamtrack-readystate">readyState</a></code>, and
<code><a href="#dom-mediastreamtrack-enabled">enabled</a></code>
attributes by copying the corresponding values from
<var>track</var>.</p>
</li>
<li>
<p>Let <var>trackClone</var>'s underlying source be the source of
<var>track</var>.</p>
</li>
<li>
<p>Set <var>trackClone</var>'s constraints to the active constrains
of <var>track</var>.</p>
</li>
<li>
<p>Return <var>trackClone</var>.</p>
</li>
</ol>
<section>
<h3>Life-cycle and Media Flow</h3>
<h4>Life-cycle</h4>
<p>A <code><a>MediaStreamTrack</a></code> has two states in its
life-cycle: <code>live</code> and <code>ended</code>. A newly created
<code><a>MediaStreamTrack</a></code> can be in either state depending
on how it was created. For example, cloning an ended track results in a
new ended track. The current state is reflected by the object's
<code><a href="#dom-mediastreamtrack-readystate">readyState</a></code>
attribute.</p>
<p>In the <code>live</code> state, the track is active and media is
available for use by consumers (but may be replaced by
zero-information-content if the <code><a>MediaStreamTrack</a></code> is
<a href="#track-muted">muted</a> or <a href=
"#track-enabled">disabled</a>, see below).</p>
<p>A muted or disabled <code><a>MediaStreamTrack</a></code> renders
either silence (audio), black frames (video), or a
zero-information-content equivalent. For example, a video element
sourced by a muted or disabled <code><a>MediaStreamTrack</a></code>
(contained within a <code><a>MediaStream</a></code> ), is playing but
the rendered content is the muted output. When all tracks connected to
a source are muted or disabled, the "on-air" or "recording" indicator
for that source can be turned off; when the track is no longer muted or
disabled, it MUST be turned back on.</p>
<p>The muted/unmuted state of a track reflects whether the source
provides any media at this moment. The enabled/disabled state is under
application control and determines whether the track outputs media (to
its consumers). Hence, media from the source only flows when a
<code><a>MediaStreamTrack</a></code> object is both unmuted and
enabled.</p>
<p>A <code><a>MediaStreamTrack</a></code> is <a href=
"#track-muted">muted</a> when the source is temporarily unable to
provide the track with data. A track can be muted by a user. Often this
action is outside the control of the application. This could be as a
result of the user hitting a hardware switch or toggling a control in
the operating system / browser chrome. A track can also be muted by the
User Agent.</p>
<p>Applications are able to <a href="#track-enabled">enable</a> or
disable a <code><a>MediaStreamTrack</a></code> to prevent it from
rendering media from the source. A muted track will however, regardless
of the enabled state, render silence and blackness. A disabled track is
logically equivalent to a muted track, from a consumer point of
view.</p>
<p>For a newly created <code><a>MediaStreamTrack</a></code> object, the
following applies. The track is always enabled unless stated otherwise
(for example when cloned) and the muted state reflects the state of the
source at the time the track is created.</p>
<p>A <code><a>MediaStreamTrack</a></code> object is said to
<em>end</em> when the source of the track is disconnected or
exhausted.</p>
<p>If all <code><a>MediaStreamTrack</a></code>s that are using the same
source are <a href="#track-ended">ended</a>, the source will be
<a href="#source-stopped">stopped</a>.</p>
<p>When a <code><a>MediaStreamTrack</a></code> object ends for any
reason (e.g., because the user rescinds the permission for the page to
use the local camera, or because the application invoked the
<code><a href="#dom-mediastreamtrack-stop">stop()</a></code> method on
the <code><a>MediaStreamTrack</a></code> object, or because the User
Agent has instructed the track to end for any reason) it is said to be
<dfn id="track-ended">ended</dfn>.</p>
<p>When a <code><a>MediaStreamTrack</a></code> <var>track</var> ends
for any reason other than the <code><a href=
"#dom-mediastreamtrack-stop">stop()</a></code> method being invoked,
the User Agent MUST queue a task that runs the following steps:</p>
<ol>
<li>
<p>If the <var>track's</var> <code><a href=
"#dom-mediastreamtrack-readystate">readyState</a></code> attribute
has the value <code>ended</code> already, then abort these
steps.</p>
</li>
<li>
<p>Set <var>track's</var> <code><a href=
"#dom-mediastreamtrack-readystate">readyState</a></code> attribute
to <code>ended</code>.</p>
</li>
<li>
<p>Notify <var>track</var>'s source that <var>track</var> is
<a href="#track-ended">ended</a> so that the source may be <a href=
"#source-stopped">stopped</a>, unless other
<code><a>MediaStreamTrack</a></code> objects depend on it.</p>
</li>
<li>
<p>Fire a simple event named <code><a href=
"#event-mediastreamtrack-ended">ended</a></code> at the object.</p>
</li>
</ol>
<p>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.</p>
<h4>Media Flow</h4>
<p>There are two dimensions related to the media flow for a
<code>live</code> <code><a>MediaStreamTrack</a></code> : muted / not
muted, and enabled / disabled.</p>
<p><dfn data-lt-nodefault="" data-lt="track-muted" id=
"track-muted">Muted</dfn> refers to the input to the
<code><a>MediaStreamTrack</a></code>. If live samples are not made
available to the <code><a>MediaStreamTrack</a></code> it is muted.</p>
<p>Muted is out of control for the application, but can be observed by
the application by reading the <code><a href=
"#dom-mediastreamtrack-muted">muted</a></code> attribute and listening
to the associated events <code><a href=
"#event-mediastreamtrack-mute">mute</a></code> and <code><a href=
"#event-mediastreamtrack-unmute">unmute</a></code>. There can be
several reasons for a <code><a>MediaStreamTrack</a></code> to be muted:
the user pushing a physical mute button on the microphone, the user
toggling a control in the operating system, the user clicking a mute
button in the browser chrome, the User Agent (on behalf of the user)
mutes, etc.</p>
<p>To <dfn id="update-track-muted">update a track's muted state</dfn>
to <var>newState</var>, the User Agent MUST queue a task to run the
following steps:</p>
<ol>
<li>
<p>Let <var>track</var> be the <code>MediaStreamTrack</code> in
question.</p>
</li>
<li>
<p>Set <var>track</var>'s <code>muted</code> attribute to
<var>newState</var>.</p>
</li>
<li>
<p>If <var>newState</var> is <code>true</code> let
<var>eventName</var> be <code>mute</code>, otherwise
<code>unmute</code>.</p>
</li>
<li>
<p>Fire a simple event named <var>eventName</var> on
<var>track</var>.</p>
</li>
</ol>
<p><dfn id="track-enabled">Enabled/disabled</dfn> on the other hand is
available to the application to control (and observe) via the
<code><a href="#dom-mediastreamtrack-enabled">enabled</a></code>
attribute.</p>
<p>The result for the consumer is the same in the sense that whenever
<code><a>MediaStreamTrack</a></code> is muted or disabled (or both) the
consumer gets zero-information-content, which means silence for audio
and black frames for video. In other words, media from the source only
flows when a <code><a>MediaStreamTrack</a></code> object is both
unmuted and enabled. For example, a video element sourced by a muted or
disabled <code><a>MediaStreamTrack</a></code> (contained in a
<code><a>MediaStream</a></code> ), is playing but rendering
blackness.</p>
<p>For a newly created <code><a>MediaStreamTrack</a></code> object, the
following applies: the track is always enabled unless stated otherwise
(for example when cloned) and the muted state reflects the state of the
source at the time the track is created.</p>
</section>
<section>
<h3>Tracks and Constraints</h3>
<p><code><a>MediaStreamTrack</a></code> is a <a>constrainable
object</a> as defined in the <a href=
"#constrainable-interface">Constrainable Pattern</a> section.
Constraints are set on tracks and may affect sources.</p>
<p>Whether <code><a>Constraints</a></code> were provided at track
initialization time or need to be established later at runtime, the
APIs defined in the <a>ConstrainablePattern</a> Interface allow the
retrieval and manipulation of the constraints currently established on
a track.</p>
<p>If the <code><a>overconstrained</a></code> event is thrown, the
track MUST be muted until either new satisfiable constraints are
applied or the existing constraints become satisfiable.</p>
</section>
<section id="media-stream-track-interface-definition">
<h3>Interface Definition</h3>
<div>
<pre class="idl">[Exposed=Window]
interface MediaStreamTrack : EventTarget {
readonly attribute DOMString kind;
readonly attribute DOMString id;
readonly attribute DOMString label;
attribute boolean enabled;
readonly attribute boolean muted;
attribute EventHandler onmute;
attribute EventHandler onunmute;
readonly attribute MediaStreamTrackState readyState;
attribute EventHandler onended;
MediaStreamTrack clone ();
void stop ();
MediaTrackCapabilities getCapabilities ();
MediaTrackConstraints getConstraints ();
MediaTrackSettings getSettings ();
Promise<void> applyConstraints (optional MediaTrackConstraints constraints);
attribute EventHandler onoverconstrained;
};</pre>
<section>
<h2>Attributes</h2>
<dl data-link-for="MediaStreamTrack" data-dfn-for=
"MediaStreamTrack" class="attributes">
<dt><code>kind</code> of type <span class=
"idlAttrType"><a>DOMString</a></span>, readonly</dt>
<dd>
<p>The <dfn id=
"dom-mediastreamtrack-kind"><code>kind</code></dfn> attribute
MUST return the string "<code>audio</code>" if this object
represents an audio track or "<code>video</code>" if this
object represents a video track.</p>