-
Notifications
You must be signed in to change notification settings - Fork 54
/
PlaybackController.cpp
152 lines (134 loc) · 4.62 KB
/
PlaybackController.cpp
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
/* 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 "PlaybackController.h"
#include "MediaControlUtils.h"
#include "mozilla/dom/MediaSession.h"
#include "mozilla/dom/Navigator.h"
// avoid redefined macro in unified build
#undef LOG
#define LOG(msg, ...) \
MOZ_LOG(gMediaControlLog, LogLevel::Debug, \
("PlaybackController=%p, " msg, this, ##__VA_ARGS__))
namespace mozilla {
namespace dom {
PlaybackController::PlaybackController(BrowsingContext* aContext) {
MOZ_ASSERT(aContext);
mBC = aContext;
}
MediaSession* PlaybackController::GetMediaSession() {
RefPtr<nsPIDOMWindowOuter> window = mBC->GetDOMWindow();
if (!window) {
return nullptr;
}
RefPtr<Navigator> navigator = window->GetNavigator();
return navigator->HasCreatedMediaSession() ? navigator->MediaSession()
: nullptr;
}
void PlaybackController::Play() {
const MediaSessionAction action = MediaSessionAction::Play;
RefPtr<MediaSession> session = GetMediaSession();
if (!session || !session->IsSupportedAction(action)) {
// Our default behavior is to play all media elements within same browsing
// context tree.
LOG("Handle 'play' in default behavior");
if (RefPtr<ContentControlKeyEventReceiver> receiver =
ContentControlKeyEventReceiver::Get(mBC)) {
receiver->OnKeyPressed(MediaControlKeysEvent::ePlay);
}
} else {
session->NotifyHandler(action);
}
};
void PlaybackController::Pause() {
const MediaSessionAction action = MediaSessionAction::Pause;
RefPtr<MediaSession> session = GetMediaSession();
if (!session || !session->IsSupportedAction(action)) {
// Our default behavior is to pause all media elements within same browsing
// context tree.
LOG("Handle 'pause' in default behavior");
if (RefPtr<ContentControlKeyEventReceiver> receiver =
ContentControlKeyEventReceiver::Get(mBC)) {
receiver->OnKeyPressed(MediaControlKeysEvent::ePause);
}
} else {
session->NotifyHandler(action);
}
}
void PlaybackController::SeekBackward() {
// TODO : use media session's action handler if it exists. MediaSessionAction
// doesn't support `seekbackward` yet.
return;
}
void PlaybackController::SeekForward() {
// TODO : use media session's action handler if it exists. MediaSessionAction
// doesn't support `seekforward` yet.
return;
}
void PlaybackController::PreviousTrack() {
if (RefPtr<MediaSession> session = GetMediaSession()) {
session->NotifyHandler(MediaSessionAction::Previoustrack);
}
}
void PlaybackController::NextTrack() {
if (RefPtr<MediaSession> session = GetMediaSession()) {
session->NotifyHandler(MediaSessionAction::Nexttrack);
}
}
void PlaybackController::SkipAd() {
// TODO : use media session's action handler if it exists. MediaSessionAction
// doesn't support `skipad` yet.
return;
}
void PlaybackController::Stop() {
const MediaSessionAction action = MediaSessionAction::Stop;
RefPtr<MediaSession> session = GetMediaSession();
if (!session || !session->IsSupportedAction(action)) {
// Our default behavior is to stop all media elements within same browsing
// context tree.
LOG("Handle 'stop' in default behavior");
RefPtr<ContentControlKeyEventReceiver> receiver =
ContentControlKeyEventReceiver::Get(mBC);
if (receiver) {
receiver->OnKeyPressed(MediaControlKeysEvent::eStop);
}
} else {
session->NotifyHandler(action);
}
}
void PlaybackController::SeekTo() {
// TODO : use media session's action handler if it exists. MediaSessionAction
// doesn't support `seekto` yet.
return;
}
void MediaActionHandler::HandleMediaControlKeysEvent(
BrowsingContext* aContext, MediaControlKeysEvent aEvent) {
PlaybackController controller(aContext);
switch (aEvent) {
case MediaControlKeysEvent::ePlay:
controller.Play();
return;
case MediaControlKeysEvent::ePause:
controller.Pause();
return;
case MediaControlKeysEvent::eStop:
controller.Stop();
return;
case MediaControlKeysEvent::ePrevTrack:
controller.PreviousTrack();
return;
case MediaControlKeysEvent::eNextTrack:
controller.NextTrack();
return;
case MediaControlKeysEvent::eSeekBackward:
controller.SeekBackward();
return;
case MediaControlKeysEvent::eSeekForward:
controller.SeekForward();
return;
default:
MOZ_ASSERT_UNREACHABLE("Invalid event.");
};
}
} // namespace dom
} // namespace mozilla