forked from w3c/mediacapture-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getusermedia.html
5068 lines (4013 loc) · 198 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>
<!--
To publish this document, see instructions in README
-->
<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">
// <!-- keep this comment --> //
</script>
<script class="remove" src="getusermedia.js" type="text/javascript">
//
<!-- 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]], 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><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 are <a href="#track-detached">detached</a> from a track when
the track is ended for any reason.</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>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>
</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 href=
"#dom-mediastream">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>Run the steps for <code><a href=
"#widl-MediaStream-addTrack-void-MediaStreamTrack-track">addTrack</a></code>
on <var>stream</var> for each <code><a>MediaStreamTrack</a></code> in
<var>tracks</var>.</p>
</li>
<li>
<p>If <var>stream</var>'s <a href="#track-set">track set</a> is empty
or only contains <a href="#track-ended">ended</a> tracks, set
<var>stream</var>'s <code><a href="#stream-active">active</a></code>
attribute to <code>false</code>, otherwise set it to
<code>true</code>.</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>When a <code><a>MediaStream</a></code> goes from being active to
inactive, the User Agent MUST queue a task that sets the object's
<code><a href="#stream-active">active</a></code> attribute to
<code>false</code> and fire a simple event named <code><a href=
"#event-mediastream-inactive">inactive</a></code> at the object. When a
<code><a>MediaStream</a></code> goes from being inactive to active, the
User Agent MUST queue a task that sets the object's <code><a href=
"#stream-active">active</a></code> attribute to <code>true</code> and
fire a simple event named <code><a href=
"#event-mediastream-active">active</a></code> at the object.</p>
<p>If the stream's activity status changed due to a user request, the
<a href="http://www.w3.org/TR/html5/webappapis.html#task-source">task
source</a> [[!HTML5]] for this <span title="concept-task">task</span> is
the <a href=
"http://www.w3.org/TR/html5/webappapis.html#user-interaction-task-source">
user interaction task source</a> [[!HTML5]]. Otherwise the task source
for this <span title="concept-task">task</span> is the <a href=
"http://www.w3.org/TR/html5/webappapis.html#networking-task-source">networking
task source</a> [[!HTML5]].</p>
<dl class="idl" title="interface MediaStream : EventTarget">
<dt>Constructor()</dt>
<dd>
<p>See the <a href="#mediastream-constructor">MediaStream constructor
algorithm</a></p>
</dd>
<dt>Constructor(MediaStream stream)</dt>
<dd>
<p>See the <a href="#mediastream-constructor">MediaStream constructor
algorithm</a></p>
</dd>
<dt>Constructor(sequence<MediaStreamTrack> tracks)</dt>
<dd>
<p>See the <a href="#mediastream-constructor">MediaStream constructor
algorithm</a></p>
</dd>
<dt>readonly attribute DOMString id</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.</p>
<p>The <dfn 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>sequence<MediaStreamTrack> getAudioTracks()</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>
</dd>
<dt>sequence<MediaStreamTrack> getVideoTracks()</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>
</dd>
<dt>sequence<MediaStreamTrack> getTracks()</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>
</dd>
<dt>MediaStreamTrack? getTrackById(DOMString trackId)</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>
</dd>
<dt>void addTrack(MediaStreamTrack track)</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 run the following steps:</p>
<ol>
<li>
<p>Let <var>track</var> be the
<code><a>MediaStreamTrack</a></code> argument and
<var>stream</var> this <code><a>MediaStream</a></code>
object.</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>
</dd>
<dt>void removeTrack(MediaStreamTrack track)</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 remove the
<code><a>MediaStreamTrack</a></code> object, indicated by the
method's argument, from the stream's <a href="#track-set">track
set</a>, if present.</p>
</dd>
<dt>MediaStream clone()</dt>
<dd>
<p>Clones the given <code><a>MediaStream</a></code> and all its
tracks.</p>
<p>When the <dfn id=
"dom-mediastream-clone"><code>MediaStream.clone()</code></dfn> 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>Let <var>clonedTracks</var> be a list that contains the result
of running <code><a href=
"#dom-mediastreamtrack-clone">MediaStreamTrack.clone()</a></code>
on all the tracks in the stream on which this method was
called.</p>
</li>
<li>
<p>Let <var>clonedTracks</var> be <var>streamClone</var>'s
<a href="#track-set">track set</a>.</p>
</li>
<li>Return <var>streamClone</var>.</li>
</ol>
</dd>
<dt>readonly attribute boolean active</dt>
<dd>
<p>This attribute is true if the <code><a>MediaStream</a></code> is
<a href="#stream-active">active</a> and false otherwise.</p>
</dd>
<dt>attribute EventHandler onactive</dt>
<dd>
<p>This event handler, of type <code><a href=
"#event-mediastream-active">active</a></code>, is executed when the
MediaStream becomes active.</p>
</dd>
<dt>attribute EventHandler oninactive</dt>
<dd>
<p>This event handler, of type <code><a href=
"#event-mediastream-inactive">inactive</a></code>, is executed when
the MediaStream becomes inactive.</p>
</dd>
<dt>attribute EventHandler onaddtrack</dt>
<dd>
<p>This event handler, of type <code><a href=
"#event-mediastream-addtrack">addtrack</a></code>, is executed when a
<code><a>MediaStreamTrack</a></code> is added to the MediaStream.</p>
</dd>
<dt>attribute EventHandler onremovetrack</dt>
<dd>
<p>This event handler, of type <code><a href=
"#event-mediastream-removetrack">removetrack</a></code>, is executed
when a <code><a>MediaStreamTrack</a></code> is removed from the
MediaStream.</p>
</dd>
</dl>
</section>
<section>
<h2>MediaStreamTrack</h2>
<p>A <code><a>MediaStreamTrack</a></code> object represents a media
source in the User Agent. 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 track no longer needs its source with the
<code><a href=
"#dom-mediastreamtrack-stop">MediaStreamTrack.stop()</a></code> method.
When all tracks using a source have been stopped, the given permission
for that source is revoked and the source is <dfn id=
"source-stopped">stopped</dfn>. 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>If there is no stored permission to use that source, the User Agent
SHOULD also remove the "permission granted" indicator for the source.</p>
<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>A <code><a>MediaStreamTrack</a></code> can be <dfn id=
"track-detached">detached</dfn> from its source. It means that the
track is no longer dependent on the source for media data. If no other
<code><a>MediaStreamTrack</a></code> is using the same source, the
source will be <a href="#source-stopped">stopped</a>.
<code><a>MediaStreamTrack</a></code> attributes such as <code><a href=
"#dom-mediastreamtrack-kind">kind</a></code> and <code><a href=
"#dom-mediastreamtrack-label">label</a></code> MUST NOT change values
when the source is detached.</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><a href="#track-detached">Detach</a> <var>track's</var>
source.</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 concepts related to the media flow for a
<code>live</code> <code><a>MediaStreamTrack</a></code> : muted / not
muted, and enabled / disabled.</p>
<p><dfn 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><dfn id="track-enabled">Enabled/disabled</dfn> on the other hand is
available to 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 meaning 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>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>Each track maintains an internal version of the
<code><a>Constraints</a></code> 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 exposed to the application by the
<code><a>constraints</a></code> attribute, and may be modified by the
<code><a>applyConstraints()</a></code> method.</p>
<p>When <code><a>applyConstraints()</a></code> is called, a User Agent
MUST queue a task to evaluate those changes when the task queue is next
serviced.</p>
<p>If the <code><a>MediaStreamError</a></code> event named
<code><a>overconstrained</a></code> 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>
<dl class="idl" title="interface MediaStreamTrack : EventTarget">
<dt>readonly attribute DOMString kind</dt>
<dd>
<p>The <dfn id=
"dom-mediastreamtrack-kind"><code>MediaStreamTrack.kind</code></dfn>
attribute MUST return the string "<code>audio</code>" if the object
represents an audio track or "<code>video</code>" if object
represents a video track.</p>
</dd>
<dt>readonly attribute DOMString id</dt>
<dd>
<p>Unless a <code><a>MediaStreamTrack</a></code> object is created
as a part of a special purpose algorithm that specifies how the
track id must be initialized, the User Agent MUST generate an
identifier string and initialize the object's <code><a href=
"#dom-mediastreamtrack-id">id</a></code> attribute to that string.
See <code><a href="#dom-mediastream-id">MediaStream.id</a></code>
for guidelines on how to generate such an identifier.</p>
<p>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 <code><a>MediaStreamTrack</a></code> object.
[[WEBRTC10]]</p>
<p><dfn id=
"dom-mediastreamtrack-id"><code>MediaStreamTrack.id</code></dfn>
attribute MUST return the value to which it was initialized when
the object was created.</p>
</dd>
<dt>readonly attribute DOMString label</dt>
<dd>
<p>User Agents MAY label audio and video sources (e.g., "Internal
microphone" or "External USB Webcam"). The <dfn id=
"dom-mediastreamtrack-label"><code>MediaStreamTrack.label</code></dfn>
attribute MUST return the label of the object's corresponding
source, if any. If the corresponding source has or had no label,
the attribute MUST instead return the empty string.</p>
</dd>
<dt>attribute boolean enabled</dt>
<dd>
<p>The <dfn id=
"dom-mediastreamtrack-enabled"><code>MediaStreamTrack.enabled</code></dfn>
attribute controls the <code><a href=
"#track-enabled">enabled</a></code> state for the object.</p>
<p>On getting, the attribute MUST return the value to which it was
last set. On setting, it MUST be set to the new value, regardless
of whether the <code><a>MediaStreamTrack</a></code> object has been
<a href="#track-detached">detached</a> from its source or not.</p>
<p class="note">Thus, after a <code><a>MediaStreamTrack</a></code>
is detached from its source, its <code><a href=
"#dom-mediastreamtrack-enabled">enabled</a></code> attribute still
changes value when set; it just doesn't do anything with that new
value.</p>
</dd>
<dt>readonly attribute boolean muted</dt>
<dd>
<p>The <dfn id=
"dom-mediastreamtrack-muted"><code>MediaStreamTrack.muted</code></dfn>
attribute MUST return <code>true</code> if the track is <a href=
"#track-muted">muted</a>, and <code>false</code> otherwise.</p>
</dd>
<dt>attribute EventHandler onmute</dt>
<dd>
<p>This event handler, of type <code><a href=
"#event-mediastreamtrack-mute">mute</a></code>, is executed when
the <code>MediaStreamTrack</code> source is temporarily unable to
provide data.</p>
</dd>
<dt>attribute EventHandler onunmute</dt>
<dd>
<p>This event handler, of type <code><a href=
"#event-mediastreamtrack-unmute">unmute</a></code>, is executed
when the <code>MediaStreamTrack</code> source is live again after
having been temporarily unable to provide data.</p>
</dd>
<dt>readonly attribute boolean _readonly</dt>
<dd>
<p>If the track (audio or video) source is a local microphone or
camera that is shared so that constraints applied to the track
cannot modify the source's settings, the <dfn id=
"dom-mediastreamtrack-readonly"><code>readonly</code></dfn>
attribute MUST return the value <code>true</code>. Otherwise, it
must return the value <code>false</code>.</p>
</dd>
<dt>readonly attribute boolean remote</dt>
<dd>
<p>If the track is sourced by a non-local source, the <dfn id=
"dom-mediastreamtrack-remote"><code>remote</code></dfn> attribute
MUST return the value <code>true</code>. Otherwise, it must return
the value <code>false</code>.</p>
</dd>
<dt>readonly attribute MediaStreamTrackState readyState</dt>
<dd>
<p>The <dfn id=
"dom-mediastreamtrack-readystate"><code>readyState</code></dfn>
attribute represents the state of the track. It MUST return the
value as most recently set by the User Agent.</p>
</dd>
<dt>attribute EventHandler onended</dt>
<dd>
<p>This event handler, of type <code><a href=
"#event-mediastreamtrack-ended">ended</a></code>, is executed when
the <code>MediaStreamTrack</code> source will no longer provide any
data, either due to a user action (revoked permission, removal of
capture device) or due to an error.</p>
</dd>
<dt>MediaStreamTrack clone()</dt>
<dd>
<p>Clones the given <code><a>MediaStreamTrack</a></code>.</p>
<p>When the <dfn id=
"dom-mediastreamtrack-clone"><code>MediaStreamTrack.clone()</code></dfn>
method is invoked, the User Agent MUST run the following steps:</p>
<ol>
<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>Let <var>trackClone</var> inherit this track's underlying
source, <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,
as well as its currently active constraints.</p>
</li>