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-08-18 09:19:23 +03:00
|
|
|
void MediaController::GetMetadata(MediaMetadataInit& aMetadata,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!IsActive() || mShutdown) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MediaMetadataBase metadata = GetCurrentMediaMetadata();
|
|
|
|
aMetadata.mTitle = metadata.mTitle;
|
|
|
|
aMetadata.mArtist = metadata.mArtist;
|
|
|
|
aMetadata.mAlbum = metadata.mAlbum;
|
|
|
|
for (const auto& artwork : metadata.mArtwork) {
|
|
|
|
if (MediaImage* image = aMetadata.mArtwork.AppendElement(fallible)) {
|
|
|
|
image->mSrc = artwork.mSrc;
|
|
|
|
image->mSizes = artwork.mSizes;
|
|
|
|
image->mType = artwork.mType;
|
|
|
|
} else {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2020-08-18 09:55:47 +03:00
|
|
|
mMetadataChangedListener =
|
|
|
|
MetadataChangedEvent().Connect(AbstractThread::MainThread(), this,
|
|
|
|
&MediaController::HandleMetadataChanged);
|
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-31 20:26:38 +03:00
|
|
|
bool MediaController::IsActive() const { return mIsActive; };
|
|
|
|
|
2020-08-27 07:10:45 +03:00
|
|
|
bool MediaController::ShouldPropagateActionToAllContexts(
|
|
|
|
const MediaControlAction& aAction) const {
|
|
|
|
// These three actions have default action handler for each frame, so we
|
|
|
|
// need to propagate to all contexts. We would handle default handlers in
|
|
|
|
// `ContentMediaController::HandleMediaKey`.
|
|
|
|
return aAction.mKey == MediaControlKey::Play ||
|
|
|
|
aAction.mKey == MediaControlKey::Pause ||
|
|
|
|
aAction.mKey == MediaControlKey::Stop;
|
|
|
|
}
|
|
|
|
|
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-08-27 07:10:45 +03:00
|
|
|
|
|
|
|
// For some actions which have default action handler, we want to propagate
|
|
|
|
// them on all contexts in order to trigger the default handler on each
|
|
|
|
// context separately. Otherwise, other action should only be propagated to
|
|
|
|
// the context where active media session exists.
|
|
|
|
const bool propateToAll = ShouldPropagateActionToAllContexts(aAction);
|
|
|
|
const uint64_t targetContextId = propateToAll || !mActiveMediaSessionContextId
|
|
|
|
? Id()
|
|
|
|
: *mActiveMediaSessionContextId;
|
|
|
|
RefPtr<BrowsingContext> context = BrowsingContext::Get(targetContextId);
|
|
|
|
if (!context || context->IsDiscarded()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (propateToAll) {
|
|
|
|
context->PreOrderWalk([&](BrowsingContext* bc) {
|
|
|
|
bc->Canonical()->UpdateMediaControlAction(aAction);
|
|
|
|
});
|
|
|
|
} else {
|
2020-07-16 03:16:33 +03:00
|
|
|
context->Canonical()->UpdateMediaControlAction(aAction);
|
2019-08-07 04:46:03 +03:00
|
|
|
}
|
2020-10-14 23:04:34 +03:00
|
|
|
RefPtr<MediaControlService> service = MediaControlService::GetService();
|
|
|
|
MOZ_ASSERT(service);
|
|
|
|
service->NotifyMediaControlHasEverBeenUsed();
|
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();
|
2020-08-18 09:55:47 +03:00
|
|
|
mMetadataChangedListener.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-08-06 02:09:21 +03:00
|
|
|
if (!StaticPrefs::media_mediacontrol_stopcontrol_timer()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-14 01:13:53 +03:00
|
|
|
bool shouldBeAlwaysActive = IsPlaying() || IsBeingUsedInPIPModeOrFullscreen();
|
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-08-14 01:13:53 +03:00
|
|
|
bool MediaController::IsBeingUsedInPIPModeOrFullscreen() const {
|
2020-06-24 08:53:36 +03:00
|
|
|
return mIsInPictureInPictureMode || mIsInFullScreenMode;
|
|
|
|
}
|
|
|
|
|
2020-06-24 08:52:30 +03:00
|
|
|
NS_IMETHODIMP MediaController::Notify(nsITimer* aTimer) {
|
|
|
|
mDeactivationTimer = nullptr;
|
2020-08-06 02:09:21 +03:00
|
|
|
if (!StaticPrefs::media_mediacontrol_stopcontrol_timer()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-06-24 08:52:30 +03:00
|
|
|
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.
|
2020-08-14 01:13:53 +03:00
|
|
|
if (IsBeingUsedInPIPModeOrFullscreen()) {
|
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() &&
|
2020-08-14 01:13:53 +03:00
|
|
|
(IsAudible() || IsBeingUsedInPIPModeOrFullscreen()) && !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);
|
Bug 1665527 - part1 : deactivate a controller only when it doesn't have controlled media and active media session. r=chunmin
Currently, we only keep controller active when it has controlled media. That strategy works well for non-media session situation because only controlled media need to listen to media keys.
However, when having media session, thing goes slightly different. When we don't have any controlled media, active media session may still listen to media keys and do the corresponding operation. Therefore, we should keep the active media session being able to receive media keys even if the controlled media has gone and deactivate a controller when it doesn't have controlled media and no active media session.
Example, play a audible media first, then press `next track`, if media session is going to play an inaudible media (which is not a controllable media, so no controlled media is existing), we still want media session to receive and handle media keys for users.
Differential Revision: https://phabricator.services.mozilla.com/D90771
2020-09-23 15:12:08 +03:00
|
|
|
// If we don't have an active media session and no controlled media exists,
|
|
|
|
// then we don't need to keep controller active, because there is nothing to
|
|
|
|
// control. However, if we still have an active media session, then we should
|
|
|
|
// keep controller active in order to receive media keys even if we don't have
|
|
|
|
// any controlled media existing, because a website might start other media
|
|
|
|
// when media session receives media keys.
|
|
|
|
return !IsAnyMediaBeingControlled() && mIsActive &&
|
|
|
|
!mActiveMediaSessionContextId;
|
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-07-31 20:26:38 +03:00
|
|
|
DispatchAsyncEvent(u"activated"_ns);
|
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-07-31 20:26:38 +03:00
|
|
|
DispatchAsyncEvent(u"deactivated"_ns);
|
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-08-07 00:37:02 +03:00
|
|
|
ForceToBecomeMainControllerIfNeeded();
|
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-08-07 00:37:02 +03:00
|
|
|
ForceToBecomeMainControllerIfNeeded();
|
|
|
|
mFullScreenChangedEvent.Notify(mIsInFullScreenMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MediaController::IsMainController() const {
|
|
|
|
RefPtr<MediaControlService> service = MediaControlService::GetService();
|
|
|
|
return service ? service->GetMainController() == this : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MediaController::ShouldRequestForMainController() const {
|
|
|
|
// This controller is already the main controller.
|
|
|
|
if (IsMainController()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// We would only force controller to become main controller if it's in the
|
|
|
|
// PIP mode or fullscreen, otherwise it should follow the general rule.
|
|
|
|
// In addition, do nothing if the controller has been shutdowned.
|
|
|
|
return IsBeingUsedInPIPModeOrFullscreen() && !mShutdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaController::ForceToBecomeMainControllerIfNeeded() {
|
|
|
|
if (!ShouldRequestForMainController()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RefPtr<MediaControlService> service = MediaControlService::GetService();
|
|
|
|
MOZ_ASSERT(service, "service was shutdown before shutting down controller?");
|
|
|
|
// If the controller hasn't been activated and it's ready to be activated,
|
|
|
|
// then activating it should also make it become a main controller. If it's
|
|
|
|
// already activated but isn't a main controller yet, then explicitly request
|
|
|
|
// it.
|
|
|
|
if (!IsActive() && ShouldActivateController()) {
|
|
|
|
Activate();
|
|
|
|
} else if (IsActive()) {
|
2020-08-07 00:37:00 +03:00
|
|
|
service->RequestUpdateMainController(this);
|
|
|
|
}
|
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-08-18 09:32:59 +03:00
|
|
|
DispatchAsyncEvent(u"playbackstatechange"_ns);
|
2020-04-02 02:04:13 +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);
|
|
|
|
}
|
|
|
|
|
2020-08-18 09:55:47 +03:00
|
|
|
void MediaController::HandleMetadataChanged(
|
|
|
|
const MediaMetadataBase& aMetadata) {
|
|
|
|
// The reason we don't append metadata with `metadatachange` event is that
|
|
|
|
// allocating artwork might fail if the memory is not enough, but for the
|
|
|
|
// event we are not able to throw an error. Therefore, we want to the listener
|
|
|
|
// to use `getMetadata()` to get metadata, because it would throw an error if
|
|
|
|
// we fail to allocate artwork.
|
|
|
|
DispatchAsyncEvent(u"metadatachange"_ns);
|
Bug 1665527 - part1 : deactivate a controller only when it doesn't have controlled media and active media session. r=chunmin
Currently, we only keep controller active when it has controlled media. That strategy works well for non-media session situation because only controlled media need to listen to media keys.
However, when having media session, thing goes slightly different. When we don't have any controlled media, active media session may still listen to media keys and do the corresponding operation. Therefore, we should keep the active media session being able to receive media keys even if the controlled media has gone and deactivate a controller when it doesn't have controlled media and no active media session.
Example, play a audible media first, then press `next track`, if media session is going to play an inaudible media (which is not a controllable media, so no controlled media is existing), we still want media session to receive and handle media keys for users.
Differential Revision: https://phabricator.services.mozilla.com/D90771
2020-09-23 15:12:08 +03:00
|
|
|
// If metadata change is because of resetting active media session, then we
|
|
|
|
// should check if controller needs to be deactivated.
|
|
|
|
if (ShouldDeactivateController()) {
|
|
|
|
Deactivate();
|
|
|
|
}
|
2020-08-18 09:55:47 +03:00
|
|
|
}
|
|
|
|
|
2020-07-02 22:41:22 +03:00
|
|
|
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
|