forked from w3c/mediasession
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.bs
1424 lines (1253 loc) · 50.5 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 Standard
Repository: w3c/mediasession
Status: ED
ED: https://w3c.github.io/mediasession/
TR: https://www.w3.org/TR/mediasession/
Shortname: mediasession
Level: 1
Editor: Mounir Lamouri, w3cid 45389, Google Inc., [email protected]
Editor: Becca Hughes, w3cid 103353, Google Inc., [email protected]
Former Editor: Zhiqiang Zhang, Google Inc., [email protected]
Former Editor: Rich Tibbett, Opera, [email protected]
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.
!Participate: <a href="https://github.com/w3c/mediasession/">We are on GitHub</a>
!Participate: <a href="https://github.com/w3c/mediasession/issues/new">File an issue</a>
!Participate: <a href="https://github.com/w3c/mediasession/issues?state=open">Open issues</a>
!Version History: <a href="https://github.com/w3c/mediasession/commits">https://github.com/w3c/mediasession/commits</a>
Ignored Vars: context, media, session
Boilerplate: omit conformance, omit feedback-header
</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="anchors">
urlPrefix: https://html.spec.whatwg.org/multipage/; spec: HTML
type: dfn
urlPrefix: common-microsyntaxes.html
text: unordered set of unique space-separated tokens; url: #unordered-set-of-unique-space-separated-tokens
urlPrefix: media.html
text: media element
text: muted; url: #concept-media-muted
text: potentially playing
urlPrefix: browsers.html
text: browsing context
urlPrefix: webappapis.html
text: entry settings object
urlPrefix: semantics.html
text: link; for: HTMLLinkElement; url:#the-link-element
urlPrefix: interaction.html
text: activation notification
urlPrefix: https://fetch.spec.whatwg.org/; spec: FETCH
type: dfn; urlPrefix: #concept-
text: fetch
text: internal response
text: response
text: response type
type: dfn;
text: force Origin header flag
urlPrefix: https://www.w3.org/TR/appmanifest/#dom-; spec: appmanifest
type: dfn
text: ImageResource
urlPrefix: https://tc39.es/ecma262/#sec-object.; spec: WEBIDL;
type: dfn
text: freeze
</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 notification,
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 which may come from platform UI or
media keys, thereby improving the user experience.
<h2 id="conformance">Conformance</h2>
All diagrams, examples, and notes in this specification are non-normative, as
are all sections explicitly marked non-normative. Everything else in this
specification is normative.
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
"SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this
document are to be interpreted as described in RFC 2119. For readability, these
words do not appear in all uppercase letters in this specification. [[!RFC2119]]
Requirements phrased in the imperative as part of algorithms (such as "strip any
leading space characters" or "return false and terminate these steps") are to be
interpreted with the meaning of the key word ("must", "should", "may", etc) used
in introducing the algorithm.
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.)
User agents may impose implementation-specific limits on otherwise unconstrained
inputs, e.g. to prevent denial of service attacks, to guard against running out
of memory, or to work around platform-specific limitations.
When a method or an attribute is said to call another method or attribute, the
user agent must invoke its internal API for that attribute or method so that
e.g. the author can't change the behavior by overriding attributes or methods
with custom properties or functions in JavaScript.
Unless otherwise stated, string comparisons are done in a <a>case-sensitive</a>
manner.
<h2 id="dependencies">Dependencies</h2>
The IDL fragments in this specification must be interpreted as required for
conforming IDL fragments, as described in the Web IDL specification. [[!WEBIDL]]
<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 <a enum-value for="MediaSessionAction">play</a> and
<a enum-value for="MediaSessionAction">pause</a> actions work properly,
the user agent SHOULD be able to determine if a <a>browsing context</a> 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 browsing context is the <a>browsing context</a>. The
<a>browsing context</a>'s <a>guessed playback state</a> is <a enum-value
for="MediaSessionPlaybackState">playing</a> if any of them is
<a>potentially playing</a> and not <a>muted</a>, and is <a enum-value
for="MediaSessionPlaybackState">paused</a> otherwise. Other information
SHOULD also be considered, such as WebAudio and plugins.
</p>
<p>
The <a attribute for="MediaSession">playbackState</a> attribute specifies
the <a>declared playback state</a> from the <a>browsing context</a>. 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
<a enum-value for="MediaSessionAction">play</a> and
<a enum-value for="MediaSessionAction">pause</a> 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 <a enum-value
for="MediaSessionPlaybackState">playing</a>, return <a enum-value
for="MediaSessionPlaybackState">playing</a>.
</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 <a enum-value
for="MediaSessionAction">pause</a> 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
<a>top-level browsing context</a> and multiple <a>nested browsing
contexts</a>, and each <a>browsing context</a> 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 <a>browsing context</a>
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 <a attribute
for="MediaSession"><code>metadata</code></a> 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 <a attribute for="MediaSession"><code>metadata</code></a> 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 <a
attribute for="MediaSession"><code>metadata</code></a> for the
<a>active media session</a>.
</li>
<li>
If the user agent wants to display an <a>artwork image</a>, 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 <a attribute
for="MediaMetadata"><code>artwork</code></a> 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 <a
attribute for="MediaMetadata"><code>artwork</code></a> of the <a>active
media session</a>.
</li>
<li>
<a title="fetch">Fetch</a> the <a>preferred artwork image</a>'s
{{MediaImage/src}}.
Then, <a>in parallel</a>:
<ol>
<li>
Wait for the <a>response</a>.
</li>
<li>
If the <a>response</a>'s <a>internal response</a>'s <a lt="response
type">type</a> is <i>default</i>, 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 title='media session action source'>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</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>
</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 <code>null</code>, 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>
that a
<a>media session action</a> named <var>action</var> has been triggered,
the user agent MUST run the <dfn>handle media session action</dfn> steps
as follows:
<ol>
<li>
If the <a>active media session</a> is <code>null</code>, abort these
steps.
</li>
<li>
Let <var>actions</var> be the <a>active media session</a>'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
<a>browsing context</a> associated with the
<a>active media session</a>.
</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
run the following steps:
<ol>
<li>
If the <a>active media session</a> is <code>null</code>, 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 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'>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
<a>browsing context</a>.
</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"
};
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 = {});
};
</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 <code>null</code>.
</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 <a for=MediaSession>metadata</a>. On getting,
it MUST return the {{MediaSession}}'s <a for=MediaSession>metadata</a>. On
setting, it MUST run the following steps with <var>value</var> being the new
value being set:
<ol>
<li>
If the {{MediaSession}}'s <a for=MediaSession>metadata</a> is not
<code>null</code>, set its <a for=MediaMetadata>media session</a> to
<code>null</code>.
</li>
<li>
Set the {{MediaSession}}'s <a for=MediaSession>metadata</a> to
<var>value</var>.
</li>
<li>
If the {{MediaSession}}'s <a for=MediaSession>metadata</a> is not
<code>null</code>, set its <a for=MediaMetadata>media session</a> 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 <a>browsing context</a>
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 <a>browsing
context</a> 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
<a>browsing context</a> is playing media or not, the values are described as
follows:
<ul>
<li>
<dfn enum-value for="MediaSessionPlaybackState">none</dfn> means the
<a>browsing context</a>
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
<a>browsing context</a> is currently playing media and it can be paused.
</li>
<li>
<dfn enum-value for="MediaSessionPlaybackState">paused</dfn> means the
<a>browsing context</a> 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 the <var>state</var> is an empty dictionary then clear the <a>position
state</a>.
</li>
<li>
If the <a dict-member for="MediaPositionState">duration</a> is not present
or its value is null, throw a <a exception>TypeError</a>.
</li>
<li>
If the <a dict-member for="MediaPositionState">duration</a> is negative,
throw a <a exception>TypeError</a>.
</li>
<li>
If the <a dict-member for="MediaPositionState">position</a> is not present
or its value is null, set it to zero.
</li>
<li>
If the <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 the <a dict-member for="MediaPositionState">playbackRate</a> is not
present or its value is null, set it to 1.0.
</li>
<li>
If the <a dict-member for="MediaPositionState">playbackRate</a> 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>
<h2 id="the-mediametadata-interface">The {{MediaMetadata}} interface</h2>
<pre class="idl">
[Exposed=Window]
interface MediaMetadata {
constructor(optional MediaMetadataInit init = {});
attribute DOMString title;
attribute DOMString artist;
attribute DOMString album;
attribute FrozenArray<MediaImage> artwork;
};
dictionary MediaMetadataInit {
DOMString title = "";
DOMString artist = "";
DOMString album = "";
sequence<MediaImage> artwork = [];
};
</pre>
<p>
A {{MediaMetadata}} object is a representation of the metadata associated with
a {{MediaSession}} that can be used by user agents to provide customized user
interface.
</p>
<p>
A {{MediaMetadata}} can have an associated <dfn for="MediaMetadata">media
session</dfn>.
</p>
<p>
A {{MediaMetadata}} has an associated <dfn for="MediaMetadata">title</dfn>,
<dfn for="MediaMetadata">artist</dfn> and <dfn for="MediaMetadata">album</dfn>
which are DOMString.
</p>
<p>
A {{MediaMetadata}} has an associated list of <dfn for="MediaMetadata">artwork
images</dfn>.
</p>
<p>
A {{MediaMetadata}} is said to be an <dfn>empty metadata</dfn> if it is equal
to <code>null</code> or all the following conditions are true:
<ul>
<li>Its <a for=MediaMetadata>title</a> is the empty string.</li>
<li>Its <a for=MediaMetadata>artist</a> is the empty string.</li>
<li>Its <a for=MediaMetadata>album</a> is the empty string.</li>
<li>Its <a for=MediaMetadata title='artwork image'>artwork images</a> length
is <code>0</code>.</li>
</ul>
</p>
<p>
The <dfn constructor
for="MediaMetadata"><code>MediaMetadata(<var>init</var>)</code></dfn>
constructor, when invoked, MUST run the following steps:
<ol>
<li>
Let <var>metadata</var> be a new {{MediaMetadata}} object.
</li>
<li>
Set <var>metadata</var>'s {{MediaMetadata/title}} to <var>init</var>'s
{{MediaMetadataInit/title}}.
</li>
<li>
Set <var>metadata</var>'s {{MediaMetadata/artist}} to <var>init</var>'s
{{MediaMetadataInit/artist}}.
</li>
<li>
Set <var>metadata</var>'s {{MediaMetadata/album}} to
<var>init</var>'s {{MediaMetadataInit/album}}.
</li>
<li>
Run the <a>convert artwork algorithm</a> with <var>init</var>'s
{{MediaMetadataInit/artwork}} as <var>input</var> and set
<var>metadata</var>'s <a for="MediaMetadata">artwork images</a> as the
result if it succeeded.
</li>
<li>
Return <var>metadata</var>.
</li>
</ol>
</p>
When the <dfn>convert artwork algorithm</dfn> with <var>input</var> parameter is
invoked, the user agent MUST run the following steps:
<ol>
<li>
Let <var>output</var> be an empty list of type {{MediaImage}}.
</li>
<li>
For each <var>entry</var> in <var>input</var>'s
{{MediaMetadataInit/artwork}}, perform the following steps:
<ol>
<li>
Let <var>image</var> be a new {{MediaImage}}.
</li>
<li>Let <var>baseURL</var> be the API base URL specified by the <a>entry
settings object</a>. </li>
<li>
<a lt="url parser">Parse</a> <var>entry</var>'s {{MediaImage/src}} using
<var>baseURL</var>. If it does not return failure, set
<var>image</var>'s {{MediaImage/src}} to the return value. Otherwise,
throw a <a exception>TypeError</a> and abort these steps.
</li>
<li>
Set <var>image</var>'s {{MediaImage/sizes}} to <var>entry</var>'s
{{MediaImage/sizes}}.
</li>
<li>
Set <var>image</var>'s {{MediaImage/type}} to <var>entry</var>'s
{{MediaImage/type}}.
</li>
<li>
Append <var>image</var> to the <var>output</var>.
</li>
</ol>
</li>
<li>
Return <var>output</var> as result.
</li>
</ol>
<p>
The <dfn attribute for="MediaMetadata"><code>title</code></dfn> attribute
reflects the {{MediaMetadata}}'s <a for=MediaMetadata>title</a>. On getting,
it MUST return the {{MediaMetadata}}'s <a for=MediaMetadata>title</a>. On
setting, it MUST set the {{MediaMetadata}}'s <a for=MediaMetadata>title</a> to
the given value.
</p>
<p>
The <dfn attribute for="MediaMetadata"><code>artist</code></dfn> attribute
reflects the {{MediaMetadata}}'s <a for=MediaMetadata>artist</a>. On getting,
it MUST return the {{MediaMetadata}}'s <a for=MediaMetadata>artist</a>. On
setting, it MUST set the {{MediaMetadata}}'s <a for=MediaMetadata>artist</a>
to the given value.
</p>