-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.bs
1869 lines (1672 loc) · 66 KB
/
index.bs
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
<pre class="metadata">
Title: Media Session
Repository: w3c/mediasession
Status: ED
ED: https://w3c.github.io/mediasession/
TR: https://www.w3.org/TR/mediasession/
Shortname: mediasession
Level: None
Editor: Tommy Steimel, w3cid 135774, Google Inc., [email protected]
Editor: Youenn Fablet, w3cid 96458, Apple Inc., [email protected]
Former Editor: Mounir Lamouri, w3cid 45389, Google Inc., [email protected]
Former Editor: Becca Hughes, w3cid 103353, Google Inc., [email protected]
Former Editor: Zhiqiang Zhang, Google Inc., [email protected]
Former Editor: Rich Tibbett, Opera, [email protected]
Markup Shorthands: markdown yes
Group: mediawg
Logo: https://resources.whatwg.org/logo-mediasession.svg
Abstract: This specification enables web developers to show customized media
Abstract: metadata on platform UI, customize available platform media
Abstract: controls, and access platform media keys such as hardware keys found
Abstract: on keyboards, headsets, remote controls, and software keys found in
Abstract: notification areas and on lock screens of mobile devices.
!Version History: <a href="https://github.com/w3c/mediasession/commits">https://github.com/w3c/mediasession/commits</a>
Ignored Vars: context, media, session
</pre>
<style>
/* https://github.com/tabatkins/bikeshed/issues/485 */
.example .self-link { display: none; }
</style>
<style>
table {
border-collapse: collapse;
border-left-style: hidden;
border-right-style: hidden;
text-align: left;
}
table caption {
font-weight: bold;
padding: 3px;
text-align: left;
}
table td, table th {
border: 1px solid black;
padding: 3px;
}
</style>
<pre class="link-defaults">
spec:html; type:element; text:link
spec:webidl; type:interface; text:object
</pre>
<pre class="anchors">
urlPrefix: https://html.spec.whatwg.org/multipage/; spec: HTML
type: dfn
urlPrefix: webappapis.html
text: entry settings object
urlPrefix: interaction.html
text: activation notification
</pre>
<h2 id="introduction">Introduction</h2>
<em>This section is non-normative.</em>
Media is used extensively today, and the Web is one of the primary means of
consuming media content. Many platforms can display media metadata, such as
title, artist, album and album art on various UI elements such as notifications,
media control center, device lockscreen, and wearable devices. This
specification aims to enable web pages to specify the media metadata to be
displayed in platform UI, and respond to media controls that may come from
platform UI or media keys, thereby improving the user experience.
<section>
<h2 id='security-privacy-considerations'>Security and Privacy
Considerations</h2>
<em>This section is non-normative.</em>
<p>
The API introduced in this specification has very low impact with regards to
security and privacy. Part of the API allows a website to expose metadata
that can be used by the user agent. The user agent obviously needs to use
this data with care. Another part of the API allows a website to receive
commands from the user via buttons or other form of controls which might
sometimes introduce a new input layer between the user and the website.
</p>
<section>
<h3 id='user-interface-guidelines'>User interface guidelines</h3>
<p>
The {{MediaMetadata}} introduced in this specification allows a website to
offer more information with regards to what is being played. The user
agent is meant to use this information in any UI related to media
playback, either internal to the user agent or within the platform.
</p>
<p>
The {{MediaMetadata}} are expected to be used in the context of media
playback, making spoofing harder but because the {{MediaMetadata}} has
text fields and image fields, a malicious website could try to spoof
another website's identity. It is recommended that the user agent offers a
way to find the origin or clearly expose the origin of the website which
the metadata are coming from.
</p>
<p>
If a user agent offers a mechanism to go back to a website from a UI
element created based on the {{MediaMetadata}}, it is recommended that the
action should not be noticeable by the website, thus reducing the chances
of spoofing.
</p>
<p>
In general, all security and privacy considerations related to the display
of notifications from a website should apply here. It is worth noting that
the {{MediaMetadata}} offer less customization than regular web
notifications, thus would be harder to spoof.
</p>
</section>
<section>
<h3 id='incognito-mode-privacy'>Incognito mode</h3>
<p>
For privacy purposes, when in incognito mode, the user agent should be
careful when sharing the information from {{MediaMetadata}} with the
system and make sure they will not be used in a way that would harm the
user. Displaying this information in a way that is very visible would be
against the user's intent of browsing in incognito mode. When available,
the UI elements should be advertized as private to the platform.
</p>
</section>
<section>
<h3 id='media-session-actions-privacy'>Media Session Actions</h3>
<p>
<a>Media session actions</a> expose a new input layer to the web platform.
User agents should make sure users are aware that their actions might be
routed to the website with the <a>active media session</a>. Especially,
when the actions are coming from remote devices such as a headset or other
remote device. It is recommended for the user agent to follow the platform
conventions when listening to these inputs in order to facilitate the user
understanding.
</p>
</section>
</section>
<section>
<h2 id='model'>Model</h2>
<section>
<h3 id='playback-state-model'>Playback State</h3>
<p>
In order to make {{MediaSessionAction/play}} and
{{MediaSessionAction/pause}} actions work properly, the user agent SHOULD
be able to determine if a [=/browsing context=] of the <a>active media
session</a> is playing media or not, which is called the <dfn>guessed
playback state</dfn>. The RECOMMENDED way for determining the <a>guessed
playback state</a> is to monitor the media elements whose node document's
[=Document/browsing context=] is the [=/browsing context=]. The
[=/browsing context=]'s <a>guessed playback state</a> is
{{MediaSessionPlaybackState/"playing"}} if any of them is [=media
element/potentially playing=] and not [=media element/muted=], and is
{{MediaSessionPlaybackState/"paused"}} otherwise. Other information SHOULD
also be considered, such as WebAudio and plugins.
</p>
<p>
The {{MediaSession/playbackState}} attribute specifies the <a>declared
playback state</a> from the [=/browsing context=]. The state is combined
with the <a>guessed playback state</a> to compute the
<dfn>actual playback state</dfn>, which is a finalized state and will be
used for {{MediaSessionAction/play}} and {{MediaSessionAction/pause}}
actions.
</p>
<p>
The <a>actual playback state</a> is computed in the following way:
<ul>
<li>
If the <a>declared playback state</a> is
{{MediaSessionPlaybackState/playing}}, return
{{MediaSessionPlaybackState/playing}}.
</li>
<li>
Otherwise, return the <a>guessed playback state</a>.
</li>
</ul>
</p>
<p class=note>
The {{MediaSession/playbackState}} attribute could be useful when the page
wants to do some preparation steps when the media is paused but it allows
the preparation steps to be interrupted by {{MediaSessionAction/pause}}
action. See <a href="#example-set-playbackState">Setting playbackState</a>
for example.
</p>
<p>
When the <a>actual playback state</a> of the <a>active media session</a>
changes, the user agent MUST run the <a>media session actions update
algorithm</a>.
</p>
</section>
<section>
<h3 id="media-session-routing">Routing</h3>
There could be multiple {{MediaSession}} objects existing at the same time
since the user agent could have multiple tabs, each tab could contain a
[=browsing context/top-level traversable=] and descendant [=/navigables=],
and each [=/navigable=] could have a {{MediaSession}} object.
The user agent MUST select at most one of the {{MediaSession}} objects to
present to the user, which is called the <dfn>active media session</dfn>.
The <a>active media session</a> may be null. The selection is up to the user
agent and SHOULD be based on preferred user experience. Note that the
{{MediaSession/playbackState}} attribute MUST not affect media session
routing. It only takes effect for the <a>active media session</a>.
It is RECOMMENDED that the user agent selects the <a>active media
session</a> by managing <a>audio focus</a>. A tab or [=Window/browsing
context=] is said to have <dfn>audio focus</dfn> if it is currently playing
audio or the user expects to control the media in it. The AudioFocus API
targets this area and could be used once it's finished.
Whenever the <a>active media session</a> is changed, the user agent MUST run
the <a>media session actions update algorithm</a> and the <a>update metadata
algorithm</a>.
</section>
<section>
<h3 id='metadata'>Metadata</h3>
The media metadata for the <a>active media session</a> MAY be displayed in
the platform UI depending on platform conventions. Whenever the <a>active
media session</a> changes or setting {{MediaSession/metadata}} of the
<a>active media session</a>, the user agent MUST run the <dfn>update
metadata algorithm</dfn>. The steps are as follows:
<ol>
<li>
If the <a>active media session</a> is null, unset the media metadata
presented to the platform, and terminate these steps.
</li>
<li>
If the {{MediaSession/metadata}} of the
<a>active media session</a> is an <a>empty metadata</a>, unset the media
metadata presented to the platform, and terminate these steps.
</li>
<li>
Update the media metadata presented to the platform to match the
{{MediaSession/metadata}} for the
<a>active media session</a>.
</li>
<li>
If the user agent wants to display an [=MediaMetadata/artwork image=],
it is RECOMMENDED to run the <a>fetch image algorithm</a>.
</li>
</ol>
The RECOMMENDED <dfn>fetch image algorithm</dfn> is as follows:
<ol>
<!-- XXX https://www.w3.org/Bugs/Public/show_bug.cgi?id=24055 -->
<li>
If there are other <a>fetch image algorithms</a> running, cancel
existing algorithm execution instances.
</li>
<li>
If <var>metadata</var>'s {{MediaMetadata/artwork}} of the <a>active
media session</a> is empty, then terminate these steps.
</li>
<li>
If the platform supports displaying media artwork, select a
<dfn>preferred artwork image</dfn> from <var>metadata</var>'s
{{MediaMetadata/artwork}} of the <a>active media session</a>.
</li>
<li>
[=Fetch=] the <a>preferred artwork image</a>'s {{MediaImage/src}}.
Then, <a>in parallel</a>:
<ol>
<li>
Wait for the [=/response=].
</li>
<li>
If the [=/response=]'s [=response/type=] is
{{ResponseType/"default"}}, attempt to decode the resource as an
image.
</li>
<li>
If the image format is supported, use the image as the artwork for
display in platform UI. Otherwise the <a>fetch image algorithm</a>
fails and terminates.
</li>
</ol>
</li>
</ol>
If no images are fetched in the <a>fetch image algorithm</a>, the user agent
MAY have fallback behavior such as displaying a default image as artwork.
</section>
<section>
<h3 id="actions-model">Actions</h3>
<p>
A <dfn>media session action</dfn> is an action that the page can handle in
order for the user to interact with the {{MediaSession}}. For example, a
page can handle some actions that will then be triggered when the user
presses buttons from a headset or other remote device.
</p>
<p>
A <dfn>media session action source</dfn> is a source that might produce a
<a>media session action</a>. Such a source can be the platform or the UI
surfaces created by the user agent.
</p>
<p>
A <a>media session action source</a> has an optional
<dfn for="media session action source">target</dfn> which should be the
recipient of any <a>media session action</a> created by the
<a>media session action source</a>. If a <a>media session action
source</a>'s
<a for="media session action source">target</a> is `null`, the <a>active
media session</a> is the recipient of all
<a>media session action source</a>'s actions.
</p>
<p>
A <a>media session action</a> is represented by a {{MediaSessionAction}}
which can have one of the following value:
<ul>
<li>
<dfn enum-value for=MediaSessionAction>play</dfn>: the action's intent
is to resume the playback.
</li>
<li>
<dfn enum-value for=MediaSessionAction>pause</dfn>: the action's
intent is to pause the currently active playback.
</li>
<li>
<dfn enum-value for=MediaSessionAction>seekbackward</dfn>: the
action's intent is to move the playback time backward by a short
period (eg. a few seconds).
</li>
<li>
<dfn enum-value for=MediaSessionAction>seekforward</dfn>: the action's
intent is to move the playback time forward by a short period (eg. a
few seconds).
</li>
<li>
<dfn enum-value for=MediaSessionAction>previoustrack</dfn>: the
action's intent is to either start the current playback from the
beginning if the playback has a notion of beginning, or move to the
previous item in the playlist if the playback has a notion of
playlist.
</li>
<li>
<dfn enum-value for=MediaSessionAction>nexttrack</dfn>: the action's
intent is to move to the playback to the next item in the playlist if
the playback has a notion of playlist.
</li>
<li>
<dfn enum-value for=MediaSessionAction>skipad</dfn>: the action's
intent is to skip the advertisement that is currently playing.
</li>
<li>
<dfn enum-value for=MediaSessionAction>stop</dfn>: the action's intent
is to stop the playback and clear the state if appropriate.
</li>
<li>
<dfn enum-value for=MediaSessionAction>seekto</dfn>: the action's
intent is to move the playback time to a specific time.
</li>
<li>
<dfn enum-value for=MediaSessionAction>togglemicrophone</dfn>: the
action's intent is to mute or unmute the user's microphone.
</li>
<li>
<dfn enum-value for=MediaSessionAction>togglecamera</dfn>: the
action's intent is to turn the user's active camera on or off.
</li>
<li>
<dfn enum-value for=MediaSessionAction>togglescreenshare</dfn>: the
action's intent is to turn the user's active screenshare on or off.
</li>
<li>
<dfn enum-value for=MediaSessionAction>hangup</dfn>: the action's
intent is to end a call.
</li>
<li>
<dfn enum-value for=MediaSessionAction>previousslide</dfn>: the
action's intent is to go back to the previous slide when presenting
slides.
</li>
<li>
<dfn enum-value for=MediaSessionAction>nextslide</dfn>: the action's
intent is to go to the next slide when presenting slides.
</li>
<li>
<dfn enum-value for=MediaSessionAction>enterpictureinpicture</dfn>:
the action's intent is to open the media session in a
picture-in-picture window.
</li>
<li>
<dfn enum-value for=MediaSessionAction>voiceactivity</dfn>: the
action's intent is to notify the web page that voice activity has been
detected by the microphone.
</li>
</ul>
</p>
<p>
All {{MediaSession}}s have a map of <dfn>supported media session
actions</dfn> with, as a key, a <a>media session action</a> and as a value
a {{MediaSessionActionHandler}}.
</p>
<p>
When the <dfn>update action handler algorithm</dfn> on a given
{{MediaSession}} with <var>action</var> and <var>handler</var> parameters
is invoked, the user agent MUST run the following steps:
<ol>
<li>
If <var>handler</var> is `null`, remove <var>action</var>
from the <a>supported media session actions</a> for {{MediaSession}}
and abort these steps.
</li>
<li>
Add <var>action</var> to the <a>supported media session actions</a>
for {{MediaSession}} and associate to it the <var>handler</var>.
</li>
</ol>
</p>
<p>
When the <a>supported media session actions</a> are changed, the user
agent SHOULD run the <a>media session actions update algorithm</a>. The
user agent MAY <a>queue a task</a> in order to run the <a>media session
actions update algorithm</a> in order to avoid UI flickering when multiple
actions are modified in the same event loop.
</p>
<p>
When the user agent is notified by a <a>media session action source</a>
named <var>source</var> that a
<a>media session action</a> named <var>action</var> has been triggered,
the user agent MUST <a>queue a task</a>, using the [=user interaction task
source=], to run the following
<dfn>handle media session action</dfn> steps:
<ol>
<li>
Let <var>session</var> be <var>source</var>'s <a for="media session
action source">target</a>.
</li>
<li>
If <var>session</var> is `null`, set <var>session</var> to the
<a>active media session</a>.
</li>
<li>
If <var>session</var> is `null`, abort these steps.
</li>
<li>
Let <var>actions</var> be <var>session</var>'s
<a>supported media session actions</a>.
</li>
<li>
If <var>actions</var> does not contain the key <var>action</var>,
abort these steps.
</li>
<li>
Let <var>handler</var> be the {{MediaSessionActionHandler}} associated
with the key <var>action</var> in <var>actions</var>.
</li>
<li>
Run <var>handler</var> with the <var>details</var> parameter set to:
{{MediaSessionActionDetails}}.
</li>
<li>
Run the <a>activation notification</a> steps in the [=/browsing
context=] associated with <var>session</var>.
</li>
</ol>
</p>
<p>
When the user agent receives a joint command for <a enum-value
for=MediaSessionAction>play</a> and <a enum-value
for=MediaSessionAction>pause</a>, such as a headset button click, it MUST
<a>queue a task</a>, using the [=user interaction task source=], to run
the following steps:
<ol>
<li>
If the <a>active media session</a> is `null`, abort these steps.
</li>
<li>
Let <var>action</var> be a <a>media session action</a>.
</li>
<li>
If the <a>actual playback state</a> of the <a>active media session</a>
is <a enum-value for="MediaSessionPlaybackState">playing</a>, set
<var>action</var> to <a enum-value for=MediaSessionAction>pause</a>.
</li>
<li>
Otherwise, set <var>action</var> to <a enum-value
for=MediaSessionAction>play</a>.
</li>
<li>
Run the <a>handle media session action</a> steps with
<var>action</var>.
</li>
</ol>
</p>
<p>
It is RECOMMENDED for user agents to implement a default handler for the
<a enum-value for=MediaSessionAction>play</a> and <a enum-value
for=MediaSessionAction>pause</a> <a>media session actions</a> if none was
provided for the <a>active media session</a>.
</p>
<p>
A user agent MAY implement a default handler for the <a enum-value
for=MediaSessionAction>togglemicrophone</a>, <a enum-value
for=MediaSessionAction>togglecamera</a>, or <a enum-value
for=MediaSessionAction>togglescreenshare</a>, or <a enum-value
for=MediaSessionAction>hangup</a> <a>media session actions</a> if none was
provided for the <a>active media session</a>.
</p>
<p>
A user agent MAY expose microphone, camera, and screenshare state to web
pages via {{MediaStreamTrack}}'s {{MediaStreamTrack/muted}} attribute in
addition to {{MediaSessionAction/togglemicrophone}},
{{MediaSessionAction/togglecamera}} or
{{MediaSessionAction/togglescreenshare}} [=media session action=]. In that
case, the user agent MUST execute the corresponding
{{MediaSessionActionHandler}} before running, as different tasks, the
steps defined to [$set a track's muted state$].
</p>
<p>
The {{MediaSessionAction/voiceactivity}} action source MUST always have a
target whose document MUST always have {{MediaStreamTrackState/live}}
microphone {{MediaStreamTrack}}s. A user agent MUST invoke the
{{MediaSessionActionHandler}} for {{MediaSessionAction/voiceactivity}}
only when voice activity is detected from a microphone with one or more
{{MediaStreamTrackState/live}} {{MediaStreamTrack}}s. A user agent MAY
ignore voice activity if the microphone is not muted and all
{{MediaStreamTrack}}s associated with the microphone are
{{MediaStreamTrack/enabled}}. It is RECOMMENDED for user agents to set a
minimal interval between invocations of the {{MediaSessionActionHandler}}
for {{MediaSessionAction/voiceactivity}} based on privacy and power
efficiency policies.
</p>
<p class=note>
{{MediaSessionAction/voiceactivity}} only indicates the start of voice
activity. Applications may display a notification if the user is speaking
while the {{MediaStreamTrack}} is muted, or start an {{AudioWorklet}} for
audio processing. No action is defined for the end of voice activity.
Unlike other actions which are explicitly triggered by the user,
{{MediaSessionAction/voiceactivity}} also depends on the voice activity
detection algorithm of the user agent or the system. For privacy and power
efficiency concerns, the web page may not be notified if voice activity
ends and restarts soon after the last {{MediaSessionAction/voiceactivity}}
action.
</p>
<p class=note>
A page should only register a {{MediaSessionActionHandler}} for a <a>media
session action</a> when it can handle the action given that the user agent
will list this as a <a>supported media session action</a> and update the
<a>media session action sources</a>.
</p>
<p>
When the <dfn>media session actions update algorithm</dfn> is invoked, the
user agent MUST run the following steps:
<ol>
<li>
Let <var>available actions</var> be an array of <a>media session
actions</a>.
</li>
<li>
If the <a>active media session</a> is null, set <var>available
actions</var> to the empty array.
</li>
<li>
Otherwise, set the <var>available actions</var> to the list of keys
available in the <a>active media session</a>'s <a>supported media
session actions</a>.
</li>
<li>
For each <a>media session action source</a> <var>source</var>, run the
following substeps:
<ol>
<li>
Optionally, if the <a>active media session</a> is not null:
<ol>
<li>
If the <a>active media session</a>'s <a>actual playback
state</a> is <a enum-value
for="MediaSessionPlaybackState">playing</a>, remove <a
enum-value for=MediaSessionAction>play</a> from <var>available
actions</var>.
</li>
<li>
Otherwise, remove <a enum-value
for=MediaSessionAction>pause</a> from <var>available
actions</var>.
</li>
</ol>
</li>
<li>
If the <var>source</var> is a UI element created by the user
agent, it MAY remove some elements from <var>available
actions</var> if there are too many of them compared to the
available space.
</li>
<li>
Notify the <var>source</var> with the updated list of
<var>available actions</var>.
</li>
</ol>
</li>
</ol>
</p>
</section>
<section>
<h3 id='position-state-sec'>Position State</h3>
<p>
A user agent MAY display the <a>current playback position</a> and
<a>duration</a>
of a media session in the platform UI depending on platform conventions.
The
<dfn>position state</dfn> is the combination of the following:
<ul>
<li>
The <dfn>duration</dfn> of the media in seconds.
</li>
<li>
The <dfn>playback rate</dfn> of the media. It is a coefficient.
</li>
<li>
The <dfn>last reported playback position</dfn> of the media. This is
the playback position of the media in seconds when the <a>position
state</a>
was created.
</li>
</ul>
</p>
<p>
The <a>position state</a> is represented by a {{MediaPositionState}} which
MUST always be stored with the <dfn>last position updated time</dfn>. This
is the time the <a>position state</a> was last updated in seconds.
</p>
<p>
The RECOMMENDED way to determine the <a>position state</a> is to monitor
the media elements whose node document's browsing context is the
[=/browsing context=].
</p>
<p>
The <dfn>actual playback rate</dfn> is a coefficient computed in the
following way:
<ul>
<li>
If the <a>actual playback state</a> is <a enum-value
for="MediaSessionPlaybackState">paused</a>, then return zero.
</li>
<li>
Return <a>playback rate</a>.
</li>
</ul>
</p>
<p>
The <dfn>current playback position</dfn> in seconds is computed in the
following way:
<ul>
<li>
Set <var>time elapsed</var> to the system time in seconds minus the
<a>last position updated time</a>.
</li>
<li>
Mutliply <var>time elapsed</var> with <a>actual playback rate</a>.
</li>
<li>
Set <var>position</var> to <var>time elapsed</var> added to
<a>last reported playback position</a>.
</li>
<li>
If <var>position</var> is less than zero, return zero.
</li>
<li>
If <var>position</var> is greater than <a>duration</a>, return
<a>duration</a>.
</li>
<li>
Return <var>position</var>.
</li>
</ul>
</p>
</section>
</section>
<h2 id="the-mediasession-interface">The {{MediaSession}} interface</h2>
<pre class="idl">
[Exposed=Window]
partial interface Navigator {
[SameObject] readonly attribute MediaSession mediaSession;
};
enum MediaSessionPlaybackState {
"none",
"paused",
"playing"
};
enum MediaSessionAction {
"play",
"pause",
"seekbackward",
"seekforward",
"previoustrack",
"nexttrack",
"skipad",
"stop",
"seekto",
"togglemicrophone",
"togglecamera",
"togglescreenshare",
"hangup",
"previousslide",
"nextslide",
"enterpictureinpicture",
"voiceactivity"
};
callback MediaSessionActionHandler = undefined(MediaSessionActionDetails details);
[Exposed=Window]
interface MediaSession {
attribute MediaMetadata? metadata;
attribute MediaSessionPlaybackState playbackState;
undefined setActionHandler(MediaSessionAction action, MediaSessionActionHandler? handler);
undefined setPositionState(optional MediaPositionState state = {});
Promise<undefined> setMicrophoneActive(boolean active);
Promise<undefined> setCameraActive(boolean active);
Promise<undefined> setScreenshareActive(boolean active);
};
</pre>
<p>
A {{MediaSession}} object represents a media session for a given document and
allows a document to communicate to the user agent some information about the
playback and how to handle it.
</p>
<p>
A {{MediaSession}} has an associated <dfn for="MediaSession">metadata</dfn>
object represented by a {{MediaMetadata}}. It is initially `null`.
</p>
<p>
The <dfn attribute for="Navigator"><code>mediaSession</code></dfn> attribute
MUST return the {{MediaSession}} instance associated with the {{Navigator}}
object.
</p>
<p>
The <dfn attribute for="MediaSession"><code>metadata</code></dfn> attribute
reflects the {{MediaSession}}'s {{MediaSession/metadata}}. On getting, it MUST
return the {{MediaSession}}'s {{MediaSession/metadata}}. On setting, it MUST
run the following steps with <var>value</var> being the new value being set:
<ol>
<li>
If the {{MediaSession}}'s {{MediaSession/metadata}} is not `null`, set its
[=MediaMetadata/media session=] to `null`.
</li>
<li>
Set the {{MediaSession}}'s {{MediaSession/metadata}} to
<var>value</var>.
</li>
<li>
If the {{MediaSession}}'s {{MediaSession/metadata}} is not `null`, set its
[=MediaMetadata/media session=] to the current {{MediaSession}}.
</li>
<li>
<a>In parallel</a>, run the <a>update metadata algorithm</a>.
</li>
</ol>
</p>
<p>
The <dfn attribute for="MediaSession"><code>playbackState</code></dfn>
attribute represents the <dfn>declared playback state</dfn> of the <a>media
session</a>, by which the session declares whether its [=/browsing context=]
is playing media or not. The initial value is <a enum-value
for="MediaSessionPlaybackState">none</a>. On setting, the user agent MUST set
the IDL attribute to the new value if it is a valid
{{MediaSessionPlaybackState}} value. On getting, the user agent MUST return
the last valid value that was set. The {{MediaSession/playbackState}}
attribute is a hint for the user agent to determine whether the [=/browsing
context=] is playing or paused.
</p>
<p class=note>
Setting {{MediaSession/playbackState}} may cause the <a>actual playback
state</a> to change and run the <a>media session actions update algorithm</a>.
</p>
<p>
The {{MediaSessionPlaybackState}} enum is used to indicate whether a
[=/browsing context=] is playing media or not, the values are described as
follows:
<ul>
<li>
<dfn enum-value for="MediaSessionPlaybackState">none</dfn> means the
[=/browsing context=] does not specify whether it's playing or paused, it
can only be used in the {{MediaSession/playbackState}} attribute.
</li>
<li>
<dfn enum-value for="MediaSessionPlaybackState">playing</dfn> means the
[=/browsing context=] is currently playing media and it can be paused.
</li>
<li>
<dfn enum-value for="MediaSessionPlaybackState">paused</dfn> means the
[=/browsing context=] has paused media and it can be resumed.
</li>
</ul>
</p>
<p>
The <dfn method for=MediaSession>setActionHandler(action, handler)</dfn>
method, when invoked, MUST run the <a>update action handler algorithm</a> with
<var>action</var> and <var>handler</var> on the {{MediaSession}}.
</p>
<p>
The <dfn method for=MediaSession>setPositionState(|state|)</dfn> method, when
invoked MUST perform the following steps:
<ul>
<li>
If <var>state</var> is an empty dictionary, clear the <a>position
state</a>
and abort these steps.
</li>
<li>
If <var>state</var>'s <a dict-member for="MediaPositionState">duration</a>
is not present, throw a <a exception>TypeError</a>.
</li>
<li>
If <var>state</var>'s {{MediaPositionState/duration}} is negative or
<code>NaN</code>, throw a <a exception>TypeError</a>.
</li>
<li>
If <var>state</var>'s {{MediaPositionState/position}} is not present, set
it to zero.
</li>
<li>
If <var>state</var>'s <a dict-member for="MediaPositionState">position</a>
is negative or greater than <a dict-member
for="MediaPositionState">duration</a>, throw a
<a exception>TypeError</a>.
</li>
<li>
If <var>state</var>'s <a dict-member
for="MediaPositionState">playbackRate</a> is not present, set it to 1.0.
</li>
<li>
If <var>state</var>'s {{MediaPositionState/playbackRate}} is zero, throw a
<a exception>TypeError</a>.
</li>
<li>
Update the <a>position state</a> and <a>last position updated time</a>.
</li>
</ul>
</p>
<p>
The <dfn method for=MediaSession>setMicrophoneActive(active)</dfn> method
indicates to the user agent the microphone capture state desired by the page
(e.g. if the microphone is considered "inactive" by the page since it is no
longer sending audio through a call, the page can invoke
<code>setMicrophoneActive(false)</code>). When invoked, it MUST perform the
following steps:
<ol>
<li>
Let <var>document</var> be [=this=]'s [=relevant global object=]'s
[=associated Document=].
</li>
<li>
Let <var>captureKind</var> be "microphone".
</li>
<li>
Return the result of running the [=update capture state algorithm=] with
<var>document</var>, <var>active</var> and <var>captureKind</var>.
</li>
</ol>
</p>
<p>
Similarly, the <dfn method for=MediaSession>setCameraActive(active)</dfn>
method indicates to the user agent the camera capture state desired by the
page. When invoked, it MUST perform the following steps:
<ol>
<li>
Let <var>document</var> be [=this=]'s [=relevant global object=]'s
[=associated Document=].
</li>
<li>
Let <var>captureKind</var> be "camera".
</li>
<li>
Return the result of running the [=update capture state algorithm=] with
<var>document</var>, <var>active</var> and <var>captureKind</var>.
</li>
</ol>
</p>
<p>
Similarly, the <dfn method for=MediaSession>setScreenshareActive(active)</dfn>
method indicates to the user agent the screenshare capture state desired by
the page. When invoked, it MUST perform the following steps:
<ol>
<li>
Let <var>document</var> be [=this=]'s [=relevant global object=]'s
[=associated Document=].
</li>
<li>
Let <var>captureKind</var> be "screenshare".
</li>
<li>
Return the result of running the [=update capture state algorithm=] with
<var>document</var>, <var>active</var> and <var>captureKind</var>.
</li>
</ol>
</p>
<p>
The <dfn>update capture state algorithm</dfn>, when invoked with
<var>document</var>, <var>active</var> and <var>captureKind</var>, MUST
perform the following steps:
<ol>
<li>
If <var>document</var> is not [=fully active=], return [=a promise
rejected with=] <a exception>InvalidStateError</a>.
</li>
<li>
If <var>active</var> is <code>true</code> and <var>document</var>'s
[=Document/visibility state=] is not "visible", the user agent MAY return
[=a promise rejected with=] <a exception>InvalidStateError</a>.
</li>
<li>
Let <var>p</var> be a new promise.
</li>
<li>
<a>In parallel</a>, run the following steps:
<ol>
<li>
Let <var>applyPausePolicy</var> be <code>true</code> if the user agent
implements a policy of <dfn>pausing all input sources</dfn> of type
<var>captureKind</var> in response to UI and <code>false</code>
otherwise.
</li>
<li>
If <var>applyPausePolicy</var> is <code>true</code>, run the following
substeps:
<ol>
<li>
Let <var>currentlyActive</var> be <code>false</code> if the user
agent is currently [=pausing all input sources=] of type
<var>captureKind</var>
and <code>true</code> otherwise.
</li>
<li>
If <var>active</var> is <var>currentlyActive</var>, resolve
<var>p</var> with <code>undefined</code> and abort these steps.