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
|
/* -*- 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/. */
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/ContentMediaController.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/MediaSession.h"
#include "mozilla/dom/MediaControlUtils.h"
#include "mozilla/dom/WindowContext.h"
#include "mozilla/EnumeratedArrayCycleCollection.h"
// avoid redefined macro in unified build
#undef LOG
#define LOG(msg, ...) \
MOZ_LOG(gMediaControlLog, LogLevel::Debug, \
("MediaSession=%p, " msg, this, ##__VA_ARGS__))
namespace mozilla::dom {
// We don't use NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE because we need to
// unregister MediaSession from document's activity listeners.
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(MediaSession)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(MediaSession)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMediaMetadata)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mActionHandlers)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDoc)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(MediaSession)
tmp->Shutdown();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mMediaMetadata)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mActionHandlers)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDoc)
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaSession)
NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaSession)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaSession)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_ENTRY(nsIDocumentActivity)
NS_INTERFACE_MAP_END
MediaSession::MediaSession(nsPIDOMWindowInner* aParent)
: mParent(aParent), mDoc(mParent->GetExtantDoc()) {
MOZ_ASSERT(mParent);
MOZ_ASSERT(mDoc);
mDoc->RegisterActivityObserver(this);
if (mDoc->IsCurrentActiveDocument()) {
SetMediaSessionDocStatus(SessionDocStatus::eActive);
}
}
void MediaSession::Shutdown() {
if (mDoc) {
mDoc->UnregisterActivityObserver(this);
}
if (mParent) {
SetMediaSessionDocStatus(SessionDocStatus::eInactive);
}
}
void MediaSession::NotifyOwnerDocumentActivityChanged() {
const bool isDocActive = mDoc->IsCurrentActiveDocument();
LOG("Document activity changed, isActive=%d", isDocActive);
if (isDocActive) {
SetMediaSessionDocStatus(SessionDocStatus::eActive);
} else {
SetMediaSessionDocStatus(SessionDocStatus::eInactive);
}
}
void MediaSession::SetMediaSessionDocStatus(SessionDocStatus aState) {
if (mSessionDocState == aState) {
return;
}
mSessionDocState = aState;
NotifyMediaSessionDocStatus(mSessionDocState);
}
nsPIDOMWindowInner* MediaSession::GetParentObject() const { return mParent; }
JSObject* MediaSession::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return MediaSession_Binding::Wrap(aCx, this, aGivenProto);
}
MediaMetadata* MediaSession::GetMetadata() const { return mMediaMetadata; }
void MediaSession::SetMetadata(MediaMetadata* aMetadata) {
mMediaMetadata = aMetadata;
NotifyMetadataUpdated();
}
void MediaSession::SetPlaybackState(
const MediaSessionPlaybackState& aPlaybackState) {
if (mDeclaredPlaybackState == aPlaybackState) {
return;
}
mDeclaredPlaybackState = aPlaybackState;
NotifyPlaybackStateUpdated();
}
MediaSessionPlaybackState MediaSession::PlaybackState() const {
return mDeclaredPlaybackState;
}
void MediaSession::SetActionHandler(MediaSessionAction aAction,
MediaSessionActionHandler* aHandler) {
MOZ_ASSERT(size_t(aAction) < ArrayLength(mActionHandlers));
// If the media session changes its supported action, then we would propagate
// this information to the chrome process in order to run the media session
// actions update algorithm.
// https://w3c.github.io/mediasession/#supported-media-session-actions
RefPtr<MediaSessionActionHandler>& hanlder = mActionHandlers[aAction];
if (!hanlder && aHandler) {
NotifyEnableSupportedAction(aAction);
} else if (hanlder && !aHandler) {
NotifyDisableSupportedAction(aAction);
}
mActionHandlers[aAction] = aHandler;
}
MediaSessionActionHandler* MediaSession::GetActionHandler(
MediaSessionAction aAction) const {
MOZ_ASSERT(size_t(aAction) < ArrayLength(mActionHandlers));
return mActionHandlers[aAction];
}
void MediaSession::SetPositionState(const MediaPositionState& aState,
ErrorResult& aRv) {
// https://w3c.github.io/mediasession/#dom-mediasession-setpositionstate
// If the state is an empty dictionary then clear the position state.
if (!aState.IsAnyMemberPresent()) {
mPositionState.reset();
return;
}
// If the duration is not present, throw a TypeError.
if (!aState.mDuration.WasPassed()) {
return aRv.ThrowTypeError("Duration is not present");
}
// If the duration is negative, throw a TypeError.
if (aState.mDuration.WasPassed() && aState.mDuration.Value() < 0.0) {
return aRv.ThrowTypeError(nsPrintfCString(
"Invalid duration %f, it can't be negative", aState.mDuration.Value()));
}
// If the position is negative or greater than duration, throw a TypeError.
if (aState.mPosition.WasPassed() &&
(aState.mPosition.Value() < 0.0 ||
aState.mPosition.Value() > aState.mDuration.Value())) {
return aRv.ThrowTypeError(nsPrintfCString(
"Invalid position %f, it can't be negative or greater than duration",
aState.mPosition.Value()));
}
// If the playbackRate is zero, throw a TypeError.
if (aState.mPlaybackRate.WasPassed() && aState.mPlaybackRate.Value() == 0.0) {
return aRv.ThrowTypeError("The playbackRate is zero");
}
// If the position is not present, set it to zero.
double position = aState.mPosition.WasPassed() ? aState.mPosition.Value() : 0;
// If the playbackRate is not present, set it to 1.0.
double playbackRate =
aState.mPlaybackRate.WasPassed() ? aState.mPlaybackRate.Value() : 1.0;
// Update the position state and last position updated time.
MOZ_ASSERT(aState.mDuration.WasPassed());
mPositionState =
Some(PositionState(aState.mDuration.Value(), playbackRate, position));
NotifyPositionStateChanged();
}
void MediaSession::NotifyHandler(const MediaSessionActionDetails& aDetails) {
DispatchNotifyHandler(aDetails);
}
void MediaSession::DispatchNotifyHandler(
const MediaSessionActionDetails& aDetails) {
class Runnable final : public mozilla::Runnable {
public:
Runnable(const MediaSession* aSession,
const MediaSessionActionDetails& aDetails)
: mozilla::Runnable("MediaSession::DispatchNotifyHandler"),
mSession(aSession),
mDetails(aDetails) {}
MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHOD Run() override {
if (RefPtr<MediaSessionActionHandler> handler =
mSession->GetActionHandler(mDetails.mAction)) {
handler->Call(mDetails);
}
return NS_OK;
}
private:
RefPtr<const MediaSession> mSession;
MediaSessionActionDetails mDetails;
};
RefPtr<nsIRunnable> runnable = new Runnable(this, aDetails);
NS_DispatchToMainThread(runnable);
}
bool MediaSession::IsSupportedAction(MediaSessionAction aAction) const {
MOZ_ASSERT(size_t(aAction) < ArrayLength(mActionHandlers));
return mActionHandlers[aAction] != nullptr;
}
bool MediaSession::IsActive() const {
RefPtr<BrowsingContext> currentBC = GetParentObject()->GetBrowsingContext();
MOZ_ASSERT(currentBC);
RefPtr<WindowContext> wc = currentBC->GetTopWindowContext();
if (!wc) {
return false;
}
Maybe<uint64_t> activeSessionContextId = wc->GetActiveMediaSessionContextId();
if (!activeSessionContextId) {
return false;
}
LOG("session context Id=%" PRIu64 ", active session context Id=%" PRIu64,
currentBC->Id(), *activeSessionContextId);
return *activeSessionContextId == currentBC->Id();
}
void MediaSession::NotifyMediaSessionDocStatus(SessionDocStatus aState) {
RefPtr<BrowsingContext> currentBC = GetParentObject()->GetBrowsingContext();
MOZ_ASSERT(currentBC, "Update session status after context destroyed!");
RefPtr<IMediaInfoUpdater> updater = ContentMediaAgent::Get(currentBC);
if (!updater) {
return;
}
if (aState == SessionDocStatus::eActive) {
updater->NotifySessionCreated(currentBC->Id());
// If media session set its attributes before its document becomes active,
// then we would notify those attributes which hasn't been notified as well
// because attributes update would only happen if its document is already
// active.
NotifyMediaSessionAttributes();
} else {
updater->NotifySessionDestroyed(currentBC->Id());
}
}
void MediaSession::NotifyMediaSessionAttributes() {
MOZ_ASSERT(mSessionDocState == SessionDocStatus::eActive);
if (mDeclaredPlaybackState != MediaSessionPlaybackState::None) {
NotifyPlaybackStateUpdated();
}
if (mMediaMetadata) {
NotifyMetadataUpdated();
}
for (size_t idx = 0; idx < ArrayLength(mActionHandlers); idx++) {
MediaSessionAction action = static_cast<MediaSessionAction>(idx);
if (mActionHandlers[action]) {
NotifyEnableSupportedAction(action);
}
}
if (mPositionState) {
NotifyPositionStateChanged();
}
}
void MediaSession::NotifyPlaybackStateUpdated() {
if (mSessionDocState != SessionDocStatus::eActive) {
return;
}
RefPtr<BrowsingContext> currentBC = GetParentObject()->GetBrowsingContext();
MOZ_ASSERT(currentBC,
"Update session playback state after context destroyed!");
if (RefPtr<IMediaInfoUpdater> updater = ContentMediaAgent::Get(currentBC)) {
updater->SetDeclaredPlaybackState(currentBC->Id(), mDeclaredPlaybackState);
}
}
void MediaSession::NotifyMetadataUpdated() {
if (mSessionDocState != SessionDocStatus::eActive) {
return;
}
RefPtr<BrowsingContext> currentBC = GetParentObject()->GetBrowsingContext();
MOZ_ASSERT(currentBC, "Update session metadata after context destroyed!");
Maybe<MediaMetadataBase> metadata;
if (GetMetadata()) {
metadata.emplace(*(GetMetadata()->AsMetadataBase()));
}
if (RefPtr<IMediaInfoUpdater> updater = ContentMediaAgent::Get(currentBC)) {
updater->UpdateMetadata(currentBC->Id(), metadata);
}
}
void MediaSession::NotifyEnableSupportedAction(MediaSessionAction aAction) {
if (mSessionDocState != SessionDocStatus::eActive) {
return;
}
RefPtr<BrowsingContext> currentBC = GetParentObject()->GetBrowsingContext();
MOZ_ASSERT(currentBC, "Update action after context destroyed!");
if (RefPtr<IMediaInfoUpdater> updater = ContentMediaAgent::Get(currentBC)) {
updater->EnableAction(currentBC->Id(), aAction);
}
}
void MediaSession::NotifyDisableSupportedAction(MediaSessionAction aAction) {
if (mSessionDocState != SessionDocStatus::eActive) {
return;
}
RefPtr<BrowsingContext> currentBC = GetParentObject()->GetBrowsingContext();
MOZ_ASSERT(currentBC, "Update action after context destroyed!");
if (RefPtr<IMediaInfoUpdater> updater = ContentMediaAgent::Get(currentBC)) {
updater->DisableAction(currentBC->Id(), aAction);
}
}
void MediaSession::NotifyPositionStateChanged() {
if (mSessionDocState != SessionDocStatus::eActive) {
return;
}
RefPtr<BrowsingContext> currentBC = GetParentObject()->GetBrowsingContext();
MOZ_ASSERT(currentBC, "Update action after context destroyed!");
if (RefPtr<IMediaInfoUpdater> updater = ContentMediaAgent::Get(currentBC)) {
updater->UpdatePositionState(currentBC->Id(), *mPositionState);
}
}
} // namespace mozilla::dom
|