2019-08-07 04:46:10 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "MediaController.h"
|
|
|
|
|
|
|
|
#include "MediaControlService.h"
|
2019-11-26 12:25:33 +03:00
|
|
|
#include "MediaControlUtils.h"
|
2020-07-16 03:16:33 +03:00
|
|
|
#include "MediaControlKeySource.h"
|
2020-06-08 21:51:12 +03:00
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2019-08-07 04:46:10 +03:00
|
|
|
#include "mozilla/dom/BrowsingContext.h"
|
2019-08-07 04:46:03 +03:00
|
|
|
#include "mozilla/dom/CanonicalBrowsingContext.h"
|
2020-07-02 22:41:22 +03:00
|
|
|
#include "mozilla/dom/MediaSession.h"
|
|
|
|
#include "mozilla/dom/PositionStateEvent.h"
|
2019-08-07 04:46:10 +03:00
|
|
|
|
|
|
|
// avoid redefined macro in unified build
|
|
|
|
#undef LOG
|
Bug 1602336 - merge MediaController and TabMediaController. r=chunmin
The intial reason of creating MediaController as a basic class and extend this class to TabMediaController is that we would like to have a class MediaSessionController in the future, which is used to handle media session operations.
However, I found that being a media session controller or not is actually a dynamic change, so if we implement that as a different controller, we have to create and destroy controllers and switch between them a lots of times.
Ex. the tab can be controlled by TabController in the beginning, but after we create a media session, we have to use MediaSessionController. However, if we cancel the action handlers of the media session, then we have to switch back to TabController again...
Therefore, we should merge them into one class, and handle the media session part by different methods.
Differential Revision: https://phabricator.services.mozilla.com/D56315
--HG--
extra : moz-landing-system : lando
2019-12-09 23:57:42 +03:00
|
|
|
#define LOG(msg, ...) \
|
|
|
|
MOZ_LOG(gMediaControlLog, LogLevel::Debug, \
|
|
|
|
("MediaController=%p, Id=%" PRId64 ", " msg, this, this->Id(), \
|
2019-08-07 04:46:10 +03:00
|
|
|
##__VA_ARGS__))
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2020-06-08 21:51:12 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(MediaController, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(MediaController,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(MediaController,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
nsISupports* MediaController::GetParentObject() const {
|
|
|
|
RefPtr<BrowsingContext> bc = BrowsingContext::Get(Id());
|
|
|
|
return bc;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* MediaController::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
|
|
|
return MediaController_Binding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaController::GetSupportedKeys(
|
|
|
|
nsTArray<MediaControlKey>& aRetVal) const {
|
|
|
|
aRetVal.Clear();
|
|
|
|
for (const auto& key : mSupportedKeys) {
|
2020-06-09 05:59:57 +03:00
|
|
|
aRetVal.AppendElement(key);
|
2020-06-08 21:51:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 05:59:57 +03:00
|
|
|
static const MediaControlKey sDefaultSupportedKeys[] = {
|
|
|
|
MediaControlKey::Focus, MediaControlKey::Play, MediaControlKey::Pause,
|
|
|
|
MediaControlKey::Playpause, MediaControlKey::Stop,
|
2020-06-09 05:23:35 +03:00
|
|
|
};
|
|
|
|
|
2020-06-09 05:59:57 +03:00
|
|
|
static void GetDefaultSupportedKeys(nsTArray<MediaControlKey>& aKeys) {
|
2020-06-09 05:23:35 +03:00
|
|
|
for (const auto& key : sDefaultSupportedKeys) {
|
|
|
|
aKeys.AppendElement(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 01:08:40 +03:00
|
|
|
MediaController::MediaController(uint64_t aBrowsingContextId)
|
2020-05-14 01:09:23 +03:00
|
|
|
: MediaStatusManager(aBrowsingContextId) {
|
2019-08-07 04:46:10 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess(),
|
|
|
|
"MediaController only runs on Chrome process!");
|
|
|
|
LOG("Create controller %" PRId64, Id());
|
2020-06-09 05:23:35 +03:00
|
|
|
GetDefaultSupportedKeys(mSupportedKeys);
|
|
|
|
mSupportedActionsChangedListener = SupportedActionsChangedEvent().Connect(
|
|
|
|
AbstractThread::MainThread(), this,
|
|
|
|
&MediaController::HandleSupportedMediaSessionActionsChanged);
|
2020-07-02 22:41:22 +03:00
|
|
|
mPositionStateChangedListener = PositionChangedEvent().Connect(
|
|
|
|
AbstractThread::MainThread(), this,
|
|
|
|
&MediaController::HandlePositionStateChanged);
|
2019-08-07 04:46:10 +03:00
|
|
|
}
|
|
|
|
|
Bug 1602336 - merge MediaController and TabMediaController. r=chunmin
The intial reason of creating MediaController as a basic class and extend this class to TabMediaController is that we would like to have a class MediaSessionController in the future, which is used to handle media session operations.
However, I found that being a media session controller or not is actually a dynamic change, so if we implement that as a different controller, we have to create and destroy controllers and switch between them a lots of times.
Ex. the tab can be controlled by TabController in the beginning, but after we create a media session, we have to use MediaSessionController. However, if we cancel the action handlers of the media session, then we have to switch back to TabController again...
Therefore, we should merge them into one class, and handle the media session part by different methods.
Differential Revision: https://phabricator.services.mozilla.com/D56315
--HG--
extra : moz-landing-system : lando
2019-12-09 23:57:42 +03:00
|
|
|
MediaController::~MediaController() {
|
2019-08-07 04:46:10 +03:00
|
|
|
LOG("Destroy controller %" PRId64, Id());
|
2020-01-22 04:47:59 +03:00
|
|
|
if (!mShutdown) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
2019-08-07 04:46:10 +03:00
|
|
|
};
|
|
|
|
|
2020-04-23 22:32:34 +03:00
|
|
|
void MediaController::Focus() {
|
|
|
|
LOG("Focus");
|
2020-07-16 03:16:33 +03:00
|
|
|
UpdateMediaControlActionToContentMediaIfNeeded(
|
|
|
|
MediaControlAction(MediaControlKey::Focus));
|
2020-04-23 22:32:34 +03:00
|
|
|
}
|
|
|
|
|
Bug 1602336 - merge MediaController and TabMediaController. r=chunmin
The intial reason of creating MediaController as a basic class and extend this class to TabMediaController is that we would like to have a class MediaSessionController in the future, which is used to handle media session operations.
However, I found that being a media session controller or not is actually a dynamic change, so if we implement that as a different controller, we have to create and destroy controllers and switch between them a lots of times.
Ex. the tab can be controlled by TabController in the beginning, but after we create a media session, we have to use MediaSessionController. However, if we cancel the action handlers of the media session, then we have to switch back to TabController again...
Therefore, we should merge them into one class, and handle the media session part by different methods.
Differential Revision: https://phabricator.services.mozilla.com/D56315
--HG--
extra : moz-landing-system : lando
2019-12-09 23:57:42 +03:00
|
|
|
void MediaController::Play() {
|
2019-08-07 04:46:10 +03:00
|
|
|
LOG("Play");
|
2020-07-16 03:16:33 +03:00
|
|
|
UpdateMediaControlActionToContentMediaIfNeeded(
|
|
|
|
MediaControlAction(MediaControlKey::Play));
|
2019-08-07 04:46:10 +03:00
|
|
|
}
|
|
|
|
|
Bug 1602336 - merge MediaController and TabMediaController. r=chunmin
The intial reason of creating MediaController as a basic class and extend this class to TabMediaController is that we would like to have a class MediaSessionController in the future, which is used to handle media session operations.
However, I found that being a media session controller or not is actually a dynamic change, so if we implement that as a different controller, we have to create and destroy controllers and switch between them a lots of times.
Ex. the tab can be controlled by TabController in the beginning, but after we create a media session, we have to use MediaSessionController. However, if we cancel the action handlers of the media session, then we have to switch back to TabController again...
Therefore, we should merge them into one class, and handle the media session part by different methods.
Differential Revision: https://phabricator.services.mozilla.com/D56315
--HG--
extra : moz-landing-system : lando
2019-12-09 23:57:42 +03:00
|
|
|
void MediaController::Pause() {
|
2019-08-07 04:46:10 +03:00
|
|
|
LOG("Pause");
|
2020-07-16 03:16:33 +03:00
|
|
|
UpdateMediaControlActionToContentMediaIfNeeded(
|
|
|
|
MediaControlAction(MediaControlKey::Pause));
|
2019-08-07 04:46:10 +03:00
|
|
|
}
|
|
|
|
|
2020-01-23 00:21:16 +03:00
|
|
|
void MediaController::PrevTrack() {
|
|
|
|
LOG("Prev Track");
|
2020-07-16 03:16:33 +03:00
|
|
|
UpdateMediaControlActionToContentMediaIfNeeded(
|
|
|
|
MediaControlAction(MediaControlKey::Previoustrack));
|
2020-01-23 00:21:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaController::NextTrack() {
|
|
|
|
LOG("Next Track");
|
2020-07-16 03:16:33 +03:00
|
|
|
UpdateMediaControlActionToContentMediaIfNeeded(
|
|
|
|
MediaControlAction(MediaControlKey::Nexttrack));
|
2020-01-23 00:21:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaController::SeekBackward() {
|
|
|
|
LOG("Seek Backward");
|
2020-07-16 03:16:33 +03:00
|
|
|
UpdateMediaControlActionToContentMediaIfNeeded(
|
|
|
|
MediaControlAction(MediaControlKey::Seekbackward));
|
2020-01-23 00:21:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaController::SeekForward() {
|
|
|
|
LOG("Seek Forward");
|
2020-07-16 03:16:33 +03:00
|
|
|
UpdateMediaControlActionToContentMediaIfNeeded(
|
|
|
|
MediaControlAction(MediaControlKey::Seekforward));
|
2020-01-23 00:21:16 +03:00
|
|
|
}
|
|
|
|
|
2020-07-10 21:59:31 +03:00
|
|
|
void MediaController::SkipAd() {
|
|
|
|
LOG("Skip Ad");
|
2020-07-16 03:16:33 +03:00
|
|
|
UpdateMediaControlActionToContentMediaIfNeeded(
|
|
|
|
MediaControlAction(MediaControlKey::Skipad));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaController::SeekTo(double aSeekTime, bool aFastSeek) {
|
|
|
|
LOG("Seek To");
|
|
|
|
UpdateMediaControlActionToContentMediaIfNeeded(MediaControlAction(
|
|
|
|
MediaControlKey::Seekto, SeekDetails(aSeekTime, aFastSeek)));
|
2020-07-10 21:59:31 +03:00
|
|
|
}
|
|
|
|
|
Bug 1602336 - merge MediaController and TabMediaController. r=chunmin
The intial reason of creating MediaController as a basic class and extend this class to TabMediaController is that we would like to have a class MediaSessionController in the future, which is used to handle media session operations.
However, I found that being a media session controller or not is actually a dynamic change, so if we implement that as a different controller, we have to create and destroy controllers and switch between them a lots of times.
Ex. the tab can be controlled by TabController in the beginning, but after we create a media session, we have to use MediaSessionController. However, if we cancel the action handlers of the media session, then we have to switch back to TabController again...
Therefore, we should merge them into one class, and handle the media session part by different methods.
Differential Revision: https://phabricator.services.mozilla.com/D56315
--HG--
extra : moz-landing-system : lando
2019-12-09 23:57:42 +03:00
|
|
|
void MediaController::Stop() {
|
2019-08-07 04:46:10 +03:00
|
|
|
LOG("Stop");
|
2020-07-16 03:16:33 +03:00
|
|
|
UpdateMediaControlActionToContentMediaIfNeeded(
|
|
|
|
MediaControlAction(MediaControlKey::Stop));
|
2019-12-18 19:32:18 +03:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:08:40 +03:00
|
|
|
uint64_t MediaController::Id() const { return mTopLevelBrowsingContextId; }
|
2020-05-14 01:08:01 +03:00
|
|
|
|
|
|
|
bool MediaController::IsAudible() const { return IsMediaAudible(); }
|
|
|
|
|
|
|
|
bool MediaController::IsPlaying() const { return IsMediaPlaying(); }
|
|
|
|
|
2020-07-16 03:16:33 +03:00
|
|
|
void MediaController::UpdateMediaControlActionToContentMediaIfNeeded(
|
|
|
|
const MediaControlAction& aAction) {
|
2020-06-24 08:52:40 +03:00
|
|
|
// If the controller isn't active or it has been shutdown, we don't need to
|
|
|
|
// update media action to the content process.
|
|
|
|
if (!mIsActive || mShutdown) {
|
2019-12-18 19:32:18 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-02-11 23:55:51 +03:00
|
|
|
// If we have an active media session, then we should directly notify the
|
|
|
|
// browsing context where active media session exists in order to let the
|
|
|
|
// session handle media control key events. Otherwises, we would notify the
|
|
|
|
// top-level browsing context to let it handle events.
|
|
|
|
RefPtr<BrowsingContext> context =
|
|
|
|
mActiveMediaSessionContextId
|
|
|
|
? BrowsingContext::Get(*mActiveMediaSessionContextId)
|
2020-05-14 01:08:01 +03:00
|
|
|
: BrowsingContext::Get(Id());
|
2020-02-11 23:55:51 +03:00
|
|
|
if (context && !context->IsDiscarded()) {
|
2020-07-16 03:16:33 +03:00
|
|
|
context->Canonical()->UpdateMediaControlAction(aAction);
|
2019-08-07 04:46:03 +03:00
|
|
|
}
|
2019-08-07 04:46:10 +03:00
|
|
|
}
|
|
|
|
|
Bug 1602336 - merge MediaController and TabMediaController. r=chunmin
The intial reason of creating MediaController as a basic class and extend this class to TabMediaController is that we would like to have a class MediaSessionController in the future, which is used to handle media session operations.
However, I found that being a media session controller or not is actually a dynamic change, so if we implement that as a different controller, we have to create and destroy controllers and switch between them a lots of times.
Ex. the tab can be controlled by TabController in the beginning, but after we create a media session, we have to use MediaSessionController. However, if we cancel the action handlers of the media session, then we have to switch back to TabController again...
Therefore, we should merge them into one class, and handle the media session part by different methods.
Differential Revision: https://phabricator.services.mozilla.com/D56315
--HG--
extra : moz-landing-system : lando
2019-12-09 23:57:42 +03:00
|
|
|
void MediaController::Shutdown() {
|
2020-01-22 04:47:59 +03:00
|
|
|
MOZ_ASSERT(!mShutdown, "Do not call shutdown twice!");
|
2020-01-16 21:05:14 +03:00
|
|
|
// The media controller would be removed from the service when we receive a
|
|
|
|
// notification from the content process about all controlled media has been
|
|
|
|
// stoppped. However, if controlled media is stopped after detaching
|
|
|
|
// browsing context, then sending the notification from the content process
|
|
|
|
// would fail so that we are not able to notify the chrome process to remove
|
|
|
|
// the corresponding controller. Therefore, we should manually remove the
|
|
|
|
// controller from the service.
|
|
|
|
Deactivate();
|
2020-01-22 04:47:59 +03:00
|
|
|
mShutdown = true;
|
2020-06-09 05:23:35 +03:00
|
|
|
mSupportedActionsChangedListener.DisconnectIfExists();
|
2020-07-02 22:41:22 +03:00
|
|
|
mPositionStateChangedListener.DisconnectIfExists();
|
2019-08-07 04:46:10 +03:00
|
|
|
}
|
|
|
|
|
2020-05-13 23:57:23 +03:00
|
|
|
void MediaController::NotifyMediaPlaybackChanged(uint64_t aBrowsingContextId,
|
|
|
|
MediaPlaybackState aState) {
|
2020-01-22 04:47:59 +03:00
|
|
|
if (mShutdown) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-14 01:09:23 +03:00
|
|
|
MediaStatusManager::NotifyMediaPlaybackChanged(aBrowsingContextId, aState);
|
2020-06-24 08:52:30 +03:00
|
|
|
UpdateDeactivationTimerIfNeeded();
|
2020-05-16 00:50:01 +03:00
|
|
|
UpdateActivatedStateIfNeeded();
|
2019-08-07 04:46:10 +03:00
|
|
|
}
|
|
|
|
|
2020-06-24 08:52:30 +03:00
|
|
|
void MediaController::UpdateDeactivationTimerIfNeeded() {
|
2020-06-24 08:53:36 +03:00
|
|
|
bool shouldBeAlwaysActive =
|
|
|
|
IsPlaying() || IsMediaBeingUsedInPIPModeOrFullScreen();
|
2020-06-24 08:52:30 +03:00
|
|
|
if (shouldBeAlwaysActive && mDeactivationTimer) {
|
|
|
|
LOG("Cancel deactivation timer");
|
|
|
|
mDeactivationTimer->Cancel();
|
|
|
|
mDeactivationTimer = nullptr;
|
|
|
|
} else if (!shouldBeAlwaysActive && !mDeactivationTimer) {
|
|
|
|
nsresult rv = NS_NewTimerWithCallback(
|
|
|
|
getter_AddRefs(mDeactivationTimer), this,
|
|
|
|
StaticPrefs::media_mediacontrol_stopcontrol_timer_ms(),
|
|
|
|
nsITimer::TYPE_ONE_SHOT, AbstractThread::MainThread());
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
LOG("Create a deactivation timer");
|
|
|
|
} else {
|
|
|
|
LOG("Failed to create a deactivation timer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 08:53:36 +03:00
|
|
|
bool MediaController::IsMediaBeingUsedInPIPModeOrFullScreen() const {
|
|
|
|
return mIsInPictureInPictureMode || mIsInFullScreenMode;
|
|
|
|
}
|
|
|
|
|
2020-06-24 08:52:30 +03:00
|
|
|
NS_IMETHODIMP MediaController::Notify(nsITimer* aTimer) {
|
|
|
|
mDeactivationTimer = nullptr;
|
|
|
|
if (mShutdown) {
|
|
|
|
LOG("Cancel deactivation timer because controller has been shutdown");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-06-24 08:53:36 +03:00
|
|
|
// As the media being used in the PIP mode or fullscreen would always display
|
|
|
|
// on the screen, users would have high chance to interact with it again, so
|
|
|
|
// we don't want to stop media control.
|
|
|
|
if (IsMediaBeingUsedInPIPModeOrFullScreen()) {
|
2020-06-24 08:52:30 +03:00
|
|
|
LOG("Cancel deactivation timer because controller is in PIP mode");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsPlaying()) {
|
|
|
|
LOG("Cancel deactivation timer because controller is still playing");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-06-24 08:52:40 +03:00
|
|
|
if (!mIsActive) {
|
2020-06-24 08:52:30 +03:00
|
|
|
LOG("Cancel deactivation timer because controller has been deactivated");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
Deactivate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-05-13 23:57:23 +03:00
|
|
|
void MediaController::NotifyMediaAudibleChanged(uint64_t aBrowsingContextId,
|
|
|
|
MediaAudibleState aState) {
|
2020-01-22 04:47:59 +03:00
|
|
|
if (mShutdown) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-13 20:05:31 +03:00
|
|
|
|
|
|
|
bool oldAudible = IsAudible();
|
2020-05-14 01:09:23 +03:00
|
|
|
MediaStatusManager::NotifyMediaAudibleChanged(aBrowsingContextId, aState);
|
2020-05-13 20:05:31 +03:00
|
|
|
if (IsAudible() == oldAudible) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-16 00:50:01 +03:00
|
|
|
UpdateActivatedStateIfNeeded();
|
2020-05-13 20:05:31 +03:00
|
|
|
|
|
|
|
// Request the audio focus amongs different controllers that could cause
|
|
|
|
// pausing other audible controllers if we enable the audio focus management.
|
2020-01-16 02:26:29 +03:00
|
|
|
RefPtr<MediaControlService> service = MediaControlService::GetService();
|
|
|
|
MOZ_ASSERT(service);
|
2020-05-13 20:05:31 +03:00
|
|
|
if (IsAudible()) {
|
2020-01-16 02:26:29 +03:00
|
|
|
service->GetAudioFocusManager().RequestAudioFocus(this);
|
|
|
|
} else {
|
|
|
|
service->GetAudioFocusManager().RevokeAudioFocus(this);
|
2019-08-07 04:46:08 +03:00
|
|
|
}
|
2019-08-07 04:46:10 +03:00
|
|
|
}
|
|
|
|
|
2020-05-13 20:05:31 +03:00
|
|
|
bool MediaController::ShouldActivateController() const {
|
2020-01-22 04:47:59 +03:00
|
|
|
MOZ_ASSERT(!mShutdown);
|
2020-06-24 08:53:36 +03:00
|
|
|
// After media is successfully loaded and match our critiera, such as its
|
|
|
|
// duration is longer enough, which is used to exclude the notification-ish
|
|
|
|
// sound, then it would be able to be controlled once the controll gets
|
|
|
|
// activated.
|
|
|
|
//
|
|
|
|
// Activating a controller means that we would start to interfere the media
|
|
|
|
// keys on the platform and show the virtual control interface (if needed).
|
|
|
|
// The controller would be activated when (1) the controller becomes audible
|
|
|
|
// or (2) enters fullscreen or PIP mode.
|
|
|
|
//
|
|
|
|
// The reason of activating controller after it beomes audible is, if there is
|
|
|
|
// another application playing audio at the same time, it doesn't make sense
|
|
|
|
// to interfere it if we're playing an inaudible media. In addtion, it can
|
|
|
|
// preven showing control interface for those inaudible media which are used
|
|
|
|
// for GIF-like image or background image.
|
|
|
|
//
|
|
|
|
// When a media enters fullscreen or Picture-in-Picture mode, we can regard it
|
|
|
|
// as a sign of that a user is going to start that media soon. Therefore, it
|
|
|
|
// makes sense to activate the controller in order to start controlling media.
|
|
|
|
return IsAnyMediaBeingControlled() &&
|
|
|
|
(IsAudible() || IsMediaBeingUsedInPIPModeOrFullScreen()) && !mIsActive;
|
2019-08-07 04:46:10 +03:00
|
|
|
}
|
|
|
|
|
2020-05-13 20:05:31 +03:00
|
|
|
bool MediaController::ShouldDeactivateController() const {
|
2020-01-22 04:47:59 +03:00
|
|
|
MOZ_ASSERT(!mShutdown);
|
2020-06-24 08:52:40 +03:00
|
|
|
return !IsAnyMediaBeingControlled() && mIsActive;
|
2020-01-03 04:39:33 +03:00
|
|
|
}
|
|
|
|
|
Bug 1602336 - merge MediaController and TabMediaController. r=chunmin
The intial reason of creating MediaController as a basic class and extend this class to TabMediaController is that we would like to have a class MediaSessionController in the future, which is used to handle media session operations.
However, I found that being a media session controller or not is actually a dynamic change, so if we implement that as a different controller, we have to create and destroy controllers and switch between them a lots of times.
Ex. the tab can be controlled by TabController in the beginning, but after we create a media session, we have to use MediaSessionController. However, if we cancel the action handlers of the media session, then we have to switch back to TabController again...
Therefore, we should merge them into one class, and handle the media session part by different methods.
Differential Revision: https://phabricator.services.mozilla.com/D56315
--HG--
extra : moz-landing-system : lando
2019-12-09 23:57:42 +03:00
|
|
|
void MediaController::Activate() {
|
2020-01-22 04:47:59 +03:00
|
|
|
MOZ_ASSERT(!mShutdown);
|
2019-08-07 04:46:10 +03:00
|
|
|
RefPtr<MediaControlService> service = MediaControlService::GetService();
|
2020-06-24 08:52:40 +03:00
|
|
|
if (service && !mIsActive) {
|
|
|
|
LOG("Activate");
|
|
|
|
mIsActive = service->RegisterActiveMediaController(this);
|
|
|
|
MOZ_ASSERT(mIsActive, "Fail to register controller!");
|
2020-01-16 03:28:17 +03:00
|
|
|
}
|
2019-08-07 04:46:10 +03:00
|
|
|
}
|
|
|
|
|
Bug 1602336 - merge MediaController and TabMediaController. r=chunmin
The intial reason of creating MediaController as a basic class and extend this class to TabMediaController is that we would like to have a class MediaSessionController in the future, which is used to handle media session operations.
However, I found that being a media session controller or not is actually a dynamic change, so if we implement that as a different controller, we have to create and destroy controllers and switch between them a lots of times.
Ex. the tab can be controlled by TabController in the beginning, but after we create a media session, we have to use MediaSessionController. However, if we cancel the action handlers of the media session, then we have to switch back to TabController again...
Therefore, we should merge them into one class, and handle the media session part by different methods.
Differential Revision: https://phabricator.services.mozilla.com/D56315
--HG--
extra : moz-landing-system : lando
2019-12-09 23:57:42 +03:00
|
|
|
void MediaController::Deactivate() {
|
2020-01-22 04:47:59 +03:00
|
|
|
MOZ_ASSERT(!mShutdown);
|
2019-08-07 04:46:10 +03:00
|
|
|
RefPtr<MediaControlService> service = MediaControlService::GetService();
|
2020-01-16 03:28:17 +03:00
|
|
|
if (service) {
|
2020-01-16 02:26:29 +03:00
|
|
|
service->GetAudioFocusManager().RevokeAudioFocus(this);
|
2020-06-24 08:52:40 +03:00
|
|
|
if (mIsActive) {
|
|
|
|
LOG("Deactivate");
|
|
|
|
mIsActive = !service->UnregisterActiveMediaController(this);
|
|
|
|
MOZ_ASSERT(!mIsActive, "Fail to unregister controller!");
|
2020-01-16 03:28:17 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 04:46:10 +03:00
|
|
|
}
|
|
|
|
|
2020-04-02 02:04:13 +03:00
|
|
|
void MediaController::SetIsInPictureInPictureMode(
|
2020-06-09 01:08:26 +03:00
|
|
|
uint64_t aBrowsingContextId, bool aIsInPictureInPictureMode) {
|
2020-04-02 02:04:13 +03:00
|
|
|
if (mIsInPictureInPictureMode == aIsInPictureInPictureMode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG("Set IsInPictureInPictureMode to %s",
|
|
|
|
aIsInPictureInPictureMode ? "true" : "false");
|
|
|
|
mIsInPictureInPictureMode = aIsInPictureInPictureMode;
|
2020-06-24 08:53:36 +03:00
|
|
|
UpdateActivatedStateIfNeeded();
|
Bug 1623486 - part2 : update main controller when controller starts being used in Picture-in-Picture mode. r=chunmin
We would like to ensure that the controller being used in Picture-in-Picture mode can always be the main controller, which means even if there are other controllers starting later than PIP video, they are not able to become the main controller, but we would still maintain a correct order of controllers that can ensure that the main controller can be selected correctly after current main controller get destroyed.
For example, we have a list `[A, B, C]` and `C` is the main controller. And `D` is new coming controller, then the list would become `[A, B, D, C]` and `C` is still the main controller. But if `C` gets destroyed, the list would become `[A, B, D]` and `D` is the main controller.
Differential Revision: https://phabricator.services.mozilla.com/D67710
--HG--
extra : moz-landing-system : lando
2020-03-27 02:35:50 +03:00
|
|
|
if (RefPtr<MediaControlService> service = MediaControlService::GetService();
|
|
|
|
service && mIsInPictureInPictureMode) {
|
|
|
|
service->NotifyControllerBeingUsedInPictureInPictureMode(this);
|
|
|
|
}
|
2020-06-24 08:52:30 +03:00
|
|
|
UpdateDeactivationTimerIfNeeded();
|
2020-06-24 08:54:03 +03:00
|
|
|
mPictureInPictureModeChangedEvent.Notify(mIsInPictureInPictureMode);
|
2020-04-02 02:04:13 +03:00
|
|
|
}
|
|
|
|
|
2020-06-24 08:53:08 +03:00
|
|
|
void MediaController::NotifyMediaFullScreenState(uint64_t aBrowsingContextId,
|
|
|
|
bool aIsInFullScreen) {
|
|
|
|
if (mIsInFullScreenMode == aIsInFullScreen) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG("%s fullscreen", aIsInFullScreen ? "Entered" : "Left");
|
|
|
|
mIsInFullScreenMode = aIsInFullScreen;
|
2020-06-24 08:53:36 +03:00
|
|
|
UpdateActivatedStateIfNeeded();
|
2020-06-24 08:54:03 +03:00
|
|
|
mFullScreenChangedEvent.Notify(mIsInFullScreenMode);
|
2020-06-24 08:53:08 +03:00
|
|
|
}
|
|
|
|
|
2020-05-14 00:18:06 +03:00
|
|
|
void MediaController::HandleActualPlaybackStateChanged() {
|
|
|
|
// Media control service would like to know all controllers' playback state
|
|
|
|
// in order to decide which controller should be the main controller that is
|
|
|
|
// usually the last tab which plays media.
|
|
|
|
if (RefPtr<MediaControlService> service = MediaControlService::GetService()) {
|
|
|
|
service->NotifyControllerPlaybackStateChanged(this);
|
|
|
|
}
|
2020-04-02 02:04:13 +03:00
|
|
|
}
|
|
|
|
|
2020-05-14 00:18:06 +03:00
|
|
|
bool MediaController::IsInPictureInPictureMode() const {
|
|
|
|
return mIsInPictureInPictureMode;
|
2020-03-14 04:18:31 +03:00
|
|
|
}
|
Bug 1602336 - merge MediaController and TabMediaController. r=chunmin
The intial reason of creating MediaController as a basic class and extend this class to TabMediaController is that we would like to have a class MediaSessionController in the future, which is used to handle media session operations.
However, I found that being a media session controller or not is actually a dynamic change, so if we implement that as a different controller, we have to create and destroy controllers and switch between them a lots of times.
Ex. the tab can be controlled by TabController in the beginning, but after we create a media session, we have to use MediaSessionController. However, if we cancel the action handlers of the media session, then we have to switch back to TabController again...
Therefore, we should merge them into one class, and handle the media session part by different methods.
Differential Revision: https://phabricator.services.mozilla.com/D56315
--HG--
extra : moz-landing-system : lando
2019-12-09 23:57:42 +03:00
|
|
|
|
2020-05-16 00:50:01 +03:00
|
|
|
void MediaController::UpdateActivatedStateIfNeeded() {
|
|
|
|
if (ShouldActivateController()) {
|
|
|
|
Activate();
|
|
|
|
} else if (ShouldDeactivateController()) {
|
|
|
|
Deactivate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 05:23:35 +03:00
|
|
|
void MediaController::HandleSupportedMediaSessionActionsChanged(
|
|
|
|
const nsTArray<MediaSessionAction>& aSupportedAction) {
|
|
|
|
// Convert actions to keys, some of them have been included in the supported
|
|
|
|
// keys, such as "play", "pause" and "stop".
|
2020-06-09 05:59:57 +03:00
|
|
|
nsTArray<MediaControlKey> newSupportedKeys;
|
2020-06-09 05:23:35 +03:00
|
|
|
GetDefaultSupportedKeys(newSupportedKeys);
|
|
|
|
for (const auto& action : aSupportedAction) {
|
2020-06-09 05:59:57 +03:00
|
|
|
MediaControlKey key = ConvertMediaSessionActionToControlKey(action);
|
2020-06-09 05:23:35 +03:00
|
|
|
if (!newSupportedKeys.Contains(key)) {
|
|
|
|
newSupportedKeys.AppendElement(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// As the supported key event should only be notified when supported keys
|
|
|
|
// change, so abort following steps if they don't change.
|
|
|
|
if (newSupportedKeys == mSupportedKeys) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG("Supported keys changes");
|
|
|
|
mSupportedKeys = newSupportedKeys;
|
|
|
|
mSupportedKeysChangedEvent.Notify(mSupportedKeys);
|
2020-06-08 21:51:12 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher = new AsyncEventDispatcher(
|
2020-07-01 11:29:29 +03:00
|
|
|
this, u"supportedkeyschange"_ns, CanBubble::eYes);
|
2020-06-08 21:51:12 +03:00
|
|
|
asyncDispatcher->PostDOMEvent();
|
|
|
|
MediaController_Binding::ClearCachedSupportedKeysValue(this);
|
2020-06-09 05:23:35 +03:00
|
|
|
}
|
|
|
|
|
2020-07-02 22:41:22 +03:00
|
|
|
void MediaController::HandlePositionStateChanged(const PositionState& aState) {
|
|
|
|
PositionStateEventInit init;
|
|
|
|
init.mDuration = aState.mDuration;
|
|
|
|
init.mPlaybackRate = aState.mPlaybackRate;
|
|
|
|
init.mPosition = aState.mLastReportedPlaybackPosition;
|
2020-07-04 12:38:43 +03:00
|
|
|
RefPtr<PositionStateEvent> event =
|
|
|
|
PositionStateEvent::Constructor(this, u"positionstatechange"_ns, init);
|
2020-07-02 22:41:22 +03:00
|
|
|
DispatchAsyncEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaController::DispatchAsyncEvent(const nsAString& aName) {
|
|
|
|
LOG("Dispatch event %s", NS_ConvertUTF16toUTF8(aName).get());
|
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
|
|
|
new AsyncEventDispatcher(this, aName, CanBubble::eYes);
|
|
|
|
asyncDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaController::DispatchAsyncEvent(Event* aEvent) {
|
|
|
|
MOZ_ASSERT(aEvent);
|
|
|
|
nsAutoString eventType;
|
|
|
|
aEvent->GetType(eventType);
|
|
|
|
LOG("Dispatch event %s", NS_ConvertUTF16toUTF8(eventType).get());
|
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
|
|
|
new AsyncEventDispatcher(this, aEvent);
|
|
|
|
asyncDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
|
2020-06-09 05:59:57 +03:00
|
|
|
CopyableTArray<MediaControlKey> MediaController::GetSupportedMediaKeys() const {
|
2020-06-09 05:23:35 +03:00
|
|
|
return mSupportedKeys;
|
|
|
|
}
|
|
|
|
|
2019-08-07 04:46:10 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|