2019-11-09 00:17:22 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
#include "ContentPlaybackController.h"
|
2019-11-09 00:17:22 +03:00
|
|
|
|
2019-11-26 12:25:33 +03:00
|
|
|
#include "MediaControlUtils.h"
|
2020-10-28 19:31:26 +03:00
|
|
|
#include "mozilla/dom/ContentMediaController.h"
|
2020-02-11 23:55:51 +03:00
|
|
|
#include "mozilla/dom/MediaSession.h"
|
|
|
|
#include "mozilla/dom/Navigator.h"
|
2020-10-28 19:31:26 +03:00
|
|
|
#include "mozilla/dom/WindowContext.h"
|
2020-10-14 23:04:31 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2020-04-23 22:32:34 +03:00
|
|
|
#include "nsFocusManager.h"
|
2019-11-09 00:17:22 +03:00
|
|
|
|
|
|
|
// avoid redefined macro in unified build
|
|
|
|
#undef LOG
|
|
|
|
#define LOG(msg, ...) \
|
|
|
|
MOZ_LOG(gMediaControlLog, LogLevel::Debug, \
|
2020-05-07 08:49:03 +03:00
|
|
|
("ContentPlaybackController=%p, " msg, this, ##__VA_ARGS__))
|
2019-11-09 00:17:22 +03:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2019-11-09 00:17:22 +03:00
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
ContentPlaybackController::ContentPlaybackController(
|
|
|
|
BrowsingContext* aContext) {
|
2019-11-09 00:17:22 +03:00
|
|
|
MOZ_ASSERT(aContext);
|
2020-01-03 04:27:24 +03:00
|
|
|
mBC = aContext;
|
2019-11-09 00:17:22 +03:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
MediaSession* ContentPlaybackController::GetMediaSession() const {
|
2020-02-11 23:55:51 +03:00
|
|
|
RefPtr<nsPIDOMWindowOuter> window = mBC->GetDOMWindow();
|
|
|
|
if (!window) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Navigator> navigator = window->GetNavigator();
|
2020-09-14 19:23:55 +03:00
|
|
|
if (!navigator) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:55:51 +03:00
|
|
|
return navigator->HasCreatedMediaSession() ? navigator->MediaSession()
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-09 05:59:57 +03:00
|
|
|
void ContentPlaybackController::NotifyContentMediaControlKeyReceiver(
|
|
|
|
MediaControlKey aKey) {
|
|
|
|
if (RefPtr<ContentMediaControlKeyReceiver> receiver =
|
|
|
|
ContentMediaControlKeyReceiver::Get(mBC)) {
|
2020-08-27 07:10:45 +03:00
|
|
|
LOG("Handle '%s' in default behavior for BC %" PRIu64,
|
|
|
|
ToMediaControlKeyStr(aKey), mBC->Id());
|
2020-06-09 05:59:57 +03:00
|
|
|
receiver->HandleMediaKey(aKey);
|
2020-05-04 23:05:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
void ContentPlaybackController::NotifyMediaSession(MediaSessionAction aAction) {
|
2020-07-16 03:16:33 +03:00
|
|
|
MediaSessionActionDetails details;
|
|
|
|
details.mAction = aAction;
|
|
|
|
NotifyMediaSession(details);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContentPlaybackController::NotifyMediaSession(
|
|
|
|
const MediaSessionActionDetails& aDetails) {
|
2020-05-04 23:05:08 +03:00
|
|
|
if (RefPtr<MediaSession> session = GetMediaSession()) {
|
2020-08-27 07:10:45 +03:00
|
|
|
LOG("Handle '%s' in media session behavior for BC %" PRIu64,
|
|
|
|
ToMediaSessionActionStr(aDetails.mAction), mBC->Id());
|
2020-08-25 09:22:45 +03:00
|
|
|
MOZ_ASSERT(session->IsActive(), "Notify inactive media session!");
|
2020-07-16 03:16:33 +03:00
|
|
|
session->NotifyHandler(aDetails);
|
2020-05-04 23:05:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
void ContentPlaybackController::NotifyMediaSessionWhenActionIsSupported(
|
2020-05-04 23:05:08 +03:00
|
|
|
MediaSessionAction aAction) {
|
|
|
|
if (IsMediaSessionActionSupported(aAction)) {
|
|
|
|
NotifyMediaSession(aAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
bool ContentPlaybackController::IsMediaSessionActionSupported(
|
2020-05-04 23:05:08 +03:00
|
|
|
MediaSessionAction aAction) const {
|
|
|
|
RefPtr<MediaSession> session = GetMediaSession();
|
2020-08-25 09:22:45 +03:00
|
|
|
return session ? session->IsActive() && session->IsSupportedAction(aAction)
|
|
|
|
: false;
|
2020-05-04 23:05:08 +03:00
|
|
|
}
|
|
|
|
|
2020-08-26 07:23:31 +03:00
|
|
|
Maybe<uint64_t> ContentPlaybackController::GetActiveMediaSessionId() const {
|
|
|
|
RefPtr<WindowContext> wc = mBC->GetTopWindowContext();
|
|
|
|
return wc ? wc->GetActiveMediaSessionContextId() : Nothing();
|
|
|
|
}
|
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
void ContentPlaybackController::Focus() {
|
2020-04-23 22:32:34 +03:00
|
|
|
// Focus is not part of the MediaSession standard, so always use the
|
|
|
|
// default behavior and focus the window currently playing media.
|
|
|
|
if (RefPtr<nsPIDOMWindowOuter> win = mBC->GetDOMWindow()) {
|
|
|
|
nsFocusManager::FocusWindow(win, CallerType::System);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
void ContentPlaybackController::Play() {
|
2020-02-11 23:55:51 +03:00
|
|
|
const MediaSessionAction action = MediaSessionAction::Play;
|
2020-08-26 07:23:31 +03:00
|
|
|
RefPtr<MediaSession> session = GetMediaSession();
|
2020-05-04 23:05:08 +03:00
|
|
|
if (IsMediaSessionActionSupported(action)) {
|
|
|
|
NotifyMediaSession(action);
|
2020-08-26 07:23:31 +03:00
|
|
|
}
|
|
|
|
// We don't want to arbitrarily call play default handler, because we want to
|
|
|
|
// resume the frame which a user really gets interest in, not all media in the
|
|
|
|
// same page. Therefore, we would only call default handler for `play` when
|
|
|
|
// (1) We don't have an active media session (If we have one, the play action
|
|
|
|
// handler should only be triggered on that session)
|
|
|
|
// (2) Active media session without setting action handler for `play`
|
|
|
|
else if (!GetActiveMediaSessionId() || (session && session->IsActive())) {
|
2020-06-09 05:59:57 +03:00
|
|
|
NotifyContentMediaControlKeyReceiver(MediaControlKey::Play);
|
2019-11-09 00:17:22 +03:00
|
|
|
}
|
2020-05-04 23:05:08 +03:00
|
|
|
}
|
2019-11-09 00:17:22 +03:00
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
void ContentPlaybackController::Pause() {
|
2020-02-11 23:55:51 +03:00
|
|
|
const MediaSessionAction action = MediaSessionAction::Pause;
|
2020-05-04 23:05:08 +03:00
|
|
|
if (IsMediaSessionActionSupported(action)) {
|
|
|
|
NotifyMediaSession(action);
|
2020-02-11 23:55:51 +03:00
|
|
|
} else {
|
2020-06-09 05:59:57 +03:00
|
|
|
NotifyContentMediaControlKeyReceiver(MediaControlKey::Pause);
|
2019-11-09 00:17:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
void ContentPlaybackController::SeekBackward() {
|
2020-05-04 23:05:08 +03:00
|
|
|
NotifyMediaSessionWhenActionIsSupported(MediaSessionAction::Seekbackward);
|
2019-11-09 00:17:22 +03:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
void ContentPlaybackController::SeekForward() {
|
2020-05-04 23:05:08 +03:00
|
|
|
NotifyMediaSessionWhenActionIsSupported(MediaSessionAction::Seekforward);
|
2019-11-09 00:17:22 +03:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
void ContentPlaybackController::PreviousTrack() {
|
2020-05-04 23:05:08 +03:00
|
|
|
NotifyMediaSessionWhenActionIsSupported(MediaSessionAction::Previoustrack);
|
2019-11-09 00:17:22 +03:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
void ContentPlaybackController::NextTrack() {
|
2020-05-04 23:05:08 +03:00
|
|
|
NotifyMediaSessionWhenActionIsSupported(MediaSessionAction::Nexttrack);
|
2019-11-09 00:17:22 +03:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
void ContentPlaybackController::SkipAd() {
|
2020-07-10 21:59:31 +03:00
|
|
|
NotifyMediaSessionWhenActionIsSupported(MediaSessionAction::Skipad);
|
2019-11-09 00:17:22 +03:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:49:03 +03:00
|
|
|
void ContentPlaybackController::Stop() {
|
2020-02-11 23:55:51 +03:00
|
|
|
const MediaSessionAction action = MediaSessionAction::Stop;
|
2020-05-04 23:05:08 +03:00
|
|
|
if (IsMediaSessionActionSupported(action)) {
|
|
|
|
NotifyMediaSession(action);
|
2020-02-11 23:55:51 +03:00
|
|
|
} else {
|
2020-06-09 05:59:57 +03:00
|
|
|
NotifyContentMediaControlKeyReceiver(MediaControlKey::Stop);
|
2019-11-09 00:17:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 03:16:33 +03:00
|
|
|
void ContentPlaybackController::SeekTo(double aSeekTime, bool aFastSeek) {
|
|
|
|
MediaSessionActionDetails details;
|
|
|
|
details.mAction = MediaSessionAction::Seekto;
|
|
|
|
details.mSeekTime.Construct(aSeekTime);
|
|
|
|
if (aFastSeek) {
|
|
|
|
details.mFastSeek.Construct(aFastSeek);
|
|
|
|
}
|
|
|
|
if (IsMediaSessionActionSupported(details.mAction)) {
|
|
|
|
NotifyMediaSession(details);
|
|
|
|
}
|
2019-11-09 00:17:22 +03:00
|
|
|
}
|
|
|
|
|
2020-07-16 03:16:33 +03:00
|
|
|
void ContentMediaControlKeyHandler::HandleMediaControlAction(
|
|
|
|
BrowsingContext* aContext, const MediaControlAction& aAction) {
|
2020-08-27 07:10:45 +03:00
|
|
|
MOZ_ASSERT(aContext);
|
|
|
|
// The web content doesn't exist in this browsing context.
|
|
|
|
if (!aContext->GetDocShell()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-07 08:49:03 +03:00
|
|
|
ContentPlaybackController controller(aContext);
|
2020-07-16 03:16:33 +03:00
|
|
|
switch (aAction.mKey) {
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Focus:
|
2020-04-23 22:32:34 +03:00
|
|
|
controller.Focus();
|
|
|
|
return;
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Play:
|
2019-11-09 00:17:22 +03:00
|
|
|
controller.Play();
|
2020-01-23 00:21:16 +03:00
|
|
|
return;
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Pause:
|
2019-11-09 00:17:22 +03:00
|
|
|
controller.Pause();
|
2020-01-23 00:21:16 +03:00
|
|
|
return;
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Stop:
|
2019-11-09 00:17:22 +03:00
|
|
|
controller.Stop();
|
2020-01-23 00:21:16 +03:00
|
|
|
return;
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Previoustrack:
|
2020-01-23 00:21:16 +03:00
|
|
|
controller.PreviousTrack();
|
|
|
|
return;
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Nexttrack:
|
2020-01-23 00:21:16 +03:00
|
|
|
controller.NextTrack();
|
|
|
|
return;
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Seekbackward:
|
2020-01-23 00:21:16 +03:00
|
|
|
controller.SeekBackward();
|
|
|
|
return;
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Seekforward:
|
2020-01-23 00:21:16 +03:00
|
|
|
controller.SeekForward();
|
|
|
|
return;
|
2020-07-10 21:59:31 +03:00
|
|
|
case MediaControlKey::Skipad:
|
|
|
|
controller.SkipAd();
|
|
|
|
return;
|
2020-07-16 03:16:33 +03:00
|
|
|
case MediaControlKey::Seekto: {
|
|
|
|
const SeekDetails& details = *aAction.mDetails;
|
|
|
|
controller.SeekTo(details.mSeekTime, details.mFastSeek);
|
|
|
|
return;
|
|
|
|
}
|
2019-11-09 00:17:22 +03:00
|
|
|
default:
|
2020-08-27 07:10:45 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Invalid media control key.");
|
2019-11-09 00:17:22 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|