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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_MediaSession_h
#define mozilla_dom_MediaSession_h
#include "js/TypeDecls.h"
#include "mozilla/Attributes.h"
#include "mozilla/dom/MediaSessionBinding.h"
#include "mozilla/EnumeratedArray.h"
#include "mozilla/TimeStamp.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIDocumentActivity.h"
#include "nsWrapperCache.h"
class nsPIDOMWindowInner;
namespace mozilla {
class ErrorResult;
namespace dom {
class Document;
class MediaMetadata;
// https://w3c.github.io/mediasession/#position-state
struct PositionState {
PositionState() = default;
PositionState(double aDuration, double aPlaybackRate,
double aLastReportedTime, TimeStamp aPositionUpdatedTime)
: mDuration(aDuration),
mPlaybackRate(aPlaybackRate),
mLastReportedPlaybackPosition(aLastReportedTime),
mPositionUpdatedTime(aPositionUpdatedTime) {}
double mDuration = 0.0;
double mPlaybackRate = 0.0;
double mLastReportedPlaybackPosition = 0.0;
TimeStamp mPositionUpdatedTime;
// Returns the playback position in seconds (from 0 to mDuration)
// at the current time (aNow).
// https://w3c.github.io/mediasession/#current-playback-position
double CurrentPlaybackPosition(TimeStamp aNow = TimeStamp::Now()) const;
};
class MediaSession final : public nsIDocumentActivity, public nsWrapperCache {
public:
// Ref counting and cycle collection
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(MediaSession)
NS_DECL_NSIDOCUMENTACTIVITY
explicit MediaSession(nsPIDOMWindowInner* aParent);
// WebIDL methods
nsPIDOMWindowInner* GetParentObject() const;
JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override;
MediaMetadata* GetMetadata() const;
void SetMetadata(MediaMetadata* aMetadata);
void SetPlaybackState(const MediaSessionPlaybackState& aPlaybackState);
MediaSessionPlaybackState PlaybackState() const;
void SetActionHandler(MediaSessionAction aAction,
MediaSessionActionHandler* aHandler);
void SetPositionState(const MediaPositionState& aState, ErrorResult& aRv);
bool IsSupportedAction(MediaSessionAction aAction) const;
// Use this method to trigger media session action handler asynchronously.
void NotifyHandler(const MediaSessionActionDetails& aDetails);
void Shutdown();
// `MediaStatusManager` would determine which media session is an active media
// session and update it from the chrome process. This active session is not
// 100% equal to the active media session in the spec, which is a globally
// active media session *among all tabs*. The active session here is *among
// different windows but in same tab*, so each tab can have at most one
// active media session.
bool IsActive() const;
private:
// When the document which media session belongs to is going to be destroyed,
// or is in the bfcache, then the session would be inactive. Otherwise, it's
// active all the time.
enum class SessionDocStatus : bool {
eInactive = false,
eActive = true,
};
void SetMediaSessionDocStatus(SessionDocStatus aState);
// These methods are used to propagate media session's status to the chrome
// process.
void NotifyMediaSessionDocStatus(SessionDocStatus aState);
void NotifyMediaSessionAttributes();
void NotifyPlaybackStateUpdated();
void NotifyMetadataUpdated();
void NotifyEnableSupportedAction(MediaSessionAction aAction);
void NotifyDisableSupportedAction(MediaSessionAction aAction);
void NotifyPositionStateChanged();
void DispatchNotifyHandler(const MediaSessionActionDetails& aDetails);
MediaSessionActionHandler* GetActionHandler(MediaSessionAction aAction) const;
~MediaSession() = default;
nsCOMPtr<nsPIDOMWindowInner> mParent;
RefPtr<MediaMetadata> mMediaMetadata;
EnumeratedArray<MediaSessionAction, RefPtr<MediaSessionActionHandler>>
mActionHandlers;
// This is used as is a hint for the user agent to determine whether the
// browsing context is playing or paused.
// https://w3c.github.io/mediasession/#declared-playback-state
MediaSessionPlaybackState mDeclaredPlaybackState =
MediaSessionPlaybackState::None;
Maybe<PositionState> mPositionState;
RefPtr<Document> mDoc;
SessionDocStatus mSessionDocState = SessionDocStatus::eInactive;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_MediaSession_h
|