2019-08-07 04:46:10 +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/. */
|
|
|
|
|
2019-10-30 02:08:05 +03:00
|
|
|
#ifndef DOM_MEDIA_MEDIACONTROL_MEDIACONTROLSERVICE_H_
|
|
|
|
#define DOM_MEDIA_MEDIACONTROL_MEDIACONTROLSERVICE_H_
|
2019-08-07 04:46:10 +03:00
|
|
|
|
|
|
|
#include "mozilla/AlreadyAddRefed.h"
|
|
|
|
|
2019-08-07 04:46:08 +03:00
|
|
|
#include "AudioFocusManager.h"
|
2019-08-07 04:46:10 +03:00
|
|
|
#include "MediaController.h"
|
2020-06-09 05:59:57 +03:00
|
|
|
#include "MediaControlKeyManager.h"
|
|
|
|
#include "mozilla/dom/MediaControllerBinding.h"
|
2019-08-07 04:46:10 +03:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MediaControlService is an interface to access controllers by providing
|
|
|
|
* controller Id. Everytime when controller becomes active, which means there is
|
|
|
|
* one or more media started in the corresponding browsing context, so now the
|
|
|
|
* controller is actually controlling something in the content process, so it
|
|
|
|
* would be added into the list of the MediaControlService. The controller would
|
|
|
|
* be removed from the list of the MediaControlService when it becomes inactive,
|
|
|
|
* which means no media is playing in the corresponding browsing context. Note
|
|
|
|
* that, a controller can't be added to or remove from the list twice. It should
|
|
|
|
* should have a responsibility to add and remove itself in the proper time.
|
|
|
|
*/
|
|
|
|
class MediaControlService final : public nsIObserver {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
static RefPtr<MediaControlService> GetService();
|
|
|
|
|
2020-08-14 04:32:10 +03:00
|
|
|
// Currently these following static methods are only being used in testing.
|
|
|
|
static void GenerateMediaControlKey(const GlobalObject& global,
|
|
|
|
MediaControlKey aKey);
|
|
|
|
static void GetCurrentActiveMediaMetadata(const GlobalObject& aGlobal,
|
|
|
|
MediaMetadataInit& aMetadata);
|
|
|
|
static MediaSessionPlaybackState GetCurrentMediaSessionPlaybackState(
|
|
|
|
GlobalObject& aGlobal);
|
|
|
|
|
2019-08-07 04:46:08 +03:00
|
|
|
AudioFocusManager& GetAudioFocusManager() { return mAudioFocusManager; }
|
2020-06-09 05:59:57 +03:00
|
|
|
MediaControlKeySource* GetMediaControlKeySource() {
|
|
|
|
return mMediaControlKeyManager;
|
2019-10-30 23:07:14 +03:00
|
|
|
}
|
2019-08-07 04:46:10 +03:00
|
|
|
|
2020-01-16 03:28:17 +03:00
|
|
|
// Use these functions to register/unresgister controller to/from the active
|
|
|
|
// controller list in the service. Return true if the controller is registered
|
|
|
|
// or unregistered sucessfully.
|
|
|
|
bool RegisterActiveMediaController(MediaController* aController);
|
|
|
|
bool UnregisterActiveMediaController(MediaController* aController);
|
2020-01-16 02:26:07 +03:00
|
|
|
uint64_t GetActiveControllersNum() const;
|
2019-08-07 04:46:10 +03:00
|
|
|
|
2020-03-25 17:42:53 +03:00
|
|
|
// This method would be called when the controller changes its playback state.
|
|
|
|
void NotifyControllerPlaybackStateChanged(MediaController* aController);
|
|
|
|
|
2020-08-07 00:37:00 +03:00
|
|
|
// This method is used to help a media controller become a main controller, if
|
|
|
|
// it fits the requirement.
|
|
|
|
void RequestUpdateMainController(MediaController* aController);
|
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
|
|
|
|
2020-01-10 15:24:13 +03:00
|
|
|
// The main controller is the controller which can receive the media control
|
|
|
|
// key events and would show its metadata to virtual controller interface.
|
|
|
|
MediaController* GetMainController() const;
|
2019-09-18 23:55:57 +03:00
|
|
|
|
2020-03-17 04:01:31 +03:00
|
|
|
/**
|
|
|
|
* These following functions are used for testing only. We use them to
|
|
|
|
* generate fake media control key events, get the media metadata and playback
|
|
|
|
* state from the main controller.
|
|
|
|
*/
|
2020-06-09 05:59:57 +03:00
|
|
|
void GenerateTestMediaControlKey(MediaControlKey aKey);
|
2020-03-05 22:43:50 +03:00
|
|
|
MediaMetadataBase GetMainControllerMediaMetadata() const;
|
2020-03-17 04:01:31 +03:00
|
|
|
MediaSessionPlaybackState GetMainControllerPlaybackState() const;
|
2020-03-05 22:43:50 +03:00
|
|
|
|
2020-10-02 13:02:00 +03:00
|
|
|
// Media title that should be used as a fallback. This commonly used
|
|
|
|
// when playing media in private browsing mode and we are trying to avoid
|
|
|
|
// exposing potentially sensitive titles.
|
|
|
|
nsString GetFallbackTitle() const;
|
|
|
|
|
2020-10-14 23:04:34 +03:00
|
|
|
// These functions are used to update the variable which would be used for
|
|
|
|
// telemetry probe.
|
|
|
|
void NotifyMediaControlHasEverBeenUsed() { mHasEverUsedMediaControl = true; }
|
|
|
|
void NotifyMediaControlHasEverBeenEnabled() {
|
|
|
|
mHasEverEnabledMediaControl = true;
|
|
|
|
}
|
|
|
|
|
2019-08-07 04:46:10 +03:00
|
|
|
private:
|
|
|
|
MediaControlService();
|
|
|
|
~MediaControlService();
|
|
|
|
|
2020-01-10 15:24:13 +03:00
|
|
|
/**
|
|
|
|
* When there are multiple media controllers existing, we would only choose
|
|
|
|
* one media controller as the main controller which can be controlled by
|
|
|
|
* media control keys event. The latest controller which is added into the
|
|
|
|
* service would become the main controller.
|
|
|
|
*
|
|
|
|
* However, as the main controller would be changed from time to time, so we
|
|
|
|
* create this wrapper to hold a real main controller if it exists. This class
|
|
|
|
* would also observe the playback state of controller in order to update the
|
|
|
|
* playback state of the event source.
|
|
|
|
*
|
|
|
|
* In addition, after finishing bug1592037, we would get the media metadata
|
|
|
|
* from the main controller, and update them to the event source in order to
|
|
|
|
* show those information on the virtual media controller interface on each
|
|
|
|
* platform.
|
|
|
|
*/
|
|
|
|
class ControllerManager final {
|
|
|
|
public:
|
2020-01-10 07:13:32 +03:00
|
|
|
explicit ControllerManager(MediaControlService* aService);
|
2020-01-10 15:24:13 +03:00
|
|
|
~ControllerManager() = default;
|
|
|
|
|
2020-06-09 05:59:57 +03:00
|
|
|
using MediaKeysArray = nsTArray<MediaControlKey>;
|
2020-06-08 21:51:12 +03:00
|
|
|
using LinkedListControllerPtr = LinkedListElement<RefPtr<MediaController>>*;
|
|
|
|
using ConstLinkedListControllerPtr =
|
|
|
|
const LinkedListElement<RefPtr<MediaController>>*;
|
2020-06-09 05:23:35 +03:00
|
|
|
|
2020-01-16 03:28:17 +03:00
|
|
|
bool AddController(MediaController* aController);
|
|
|
|
bool RemoveController(MediaController* aController);
|
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
|
|
|
void UpdateMainControllerIfNeeded(MediaController* aController);
|
2020-01-10 15:24:13 +03:00
|
|
|
|
|
|
|
void Shutdown();
|
|
|
|
|
|
|
|
MediaController* GetMainController() const;
|
2020-03-25 17:42:53 +03:00
|
|
|
bool Contains(MediaController* aController) const;
|
2020-01-10 15:24:13 +03:00
|
|
|
uint64_t GetControllersNum() const;
|
|
|
|
|
2020-03-21 04:04:27 +03:00
|
|
|
// These functions are used for monitoring main controller's status change.
|
|
|
|
void MainControllerPlaybackStateChanged(MediaSessionPlaybackState aState);
|
|
|
|
void MainControllerMetadataChanged(const MediaMetadataBase& aMetadata);
|
2020-01-10 07:13:32 +03:00
|
|
|
|
2020-01-10 15:24:13 +03:00
|
|
|
private:
|
2020-08-14 01:13:53 +03:00
|
|
|
// When applying `eInsertAsMainController`, we would always insert the
|
|
|
|
// element to the tail of the list. Eg. Insert C , [A, B] -> [A, B, C]
|
|
|
|
// When applying `eInsertAsNormalController`, we would insert the element
|
|
|
|
// prior to the element with a higher priority controller. Eg. Insert E and
|
|
|
|
// C and D have higher priority. [A, B, C, D] -> [A, B, E, C, D]
|
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
|
|
|
enum class InsertOptions {
|
2020-08-14 01:13:53 +03:00
|
|
|
eInsertAsMainController,
|
|
|
|
eInsertAsNormalController,
|
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
|
|
|
};
|
|
|
|
|
|
|
|
// Adjust the given controller's order by the insert option.
|
|
|
|
void ReorderGivenController(MediaController* aController,
|
|
|
|
InsertOptions aOption);
|
|
|
|
|
2020-03-25 17:42:53 +03:00
|
|
|
void UpdateMainControllerInternal(MediaController* aController);
|
2020-06-09 05:23:35 +03:00
|
|
|
void ConnectMainControllerEvents();
|
2020-03-05 00:34:45 +03:00
|
|
|
void DisconnectMainControllerEvents();
|
2020-01-10 15:24:13 +03:00
|
|
|
|
Bug 1620113 - part2 : replace nsTArray with LinkedList. r=chunmin
When we remove a controller from the list of controllers, which is a `nsTarray`, the array needs to be reconstructed if the removed controller is not the first or last element of the array.
Eg. If we have a list of controllers, [A, B, C, ....., Z] and in some situation we would like to reoroder it to [A, C, D, ......, Z, B]. If we use nsTArray, then doing this kinds of reorder is expensive because it can't arbitrarily move the element back and forth, but the linked list can.
So we replace the `nsTArray` with `LinkedList`, then this operation would become `O(1)` which could improve the performance
Differential Revision: https://phabricator.services.mozilla.com/D66855
--HG--
extra : moz-landing-system : lando
2020-03-26 22:02:25 +03:00
|
|
|
LinkedList<RefPtr<MediaController>> mControllers;
|
2020-01-10 15:24:13 +03:00
|
|
|
RefPtr<MediaController> mMainController;
|
2020-01-10 07:13:32 +03:00
|
|
|
|
|
|
|
// These member are use to listen main controller's play state changes and
|
|
|
|
// update the playback state to the event source.
|
2020-06-09 05:59:57 +03:00
|
|
|
RefPtr<MediaControlKeySource> mSource;
|
2020-03-05 00:34:45 +03:00
|
|
|
MediaEventListener mMetadataChangedListener;
|
2020-06-09 05:23:35 +03:00
|
|
|
MediaEventListener mSupportedKeysChangedListener;
|
2020-06-24 08:54:03 +03:00
|
|
|
MediaEventListener mFullScreenChangedListener;
|
|
|
|
MediaEventListener mPictureInPictureModeChangedListener;
|
2020-07-02 22:18:48 +03:00
|
|
|
MediaEventListener mPositionChangedListener;
|
2020-01-10 15:24:13 +03:00
|
|
|
};
|
|
|
|
|
2019-11-12 20:44:32 +03:00
|
|
|
void Init();
|
2019-08-07 04:46:08 +03:00
|
|
|
void Shutdown();
|
|
|
|
|
|
|
|
AudioFocusManager mAudioFocusManager;
|
2020-06-09 05:59:57 +03:00
|
|
|
RefPtr<MediaControlKeyManager> mMediaControlKeyManager;
|
|
|
|
RefPtr<MediaControlKeyListener> mMediaKeysHandler;
|
2019-11-09 01:09:23 +03:00
|
|
|
MediaEventProducer<uint64_t> mMediaControllerAmountChangedEvent;
|
2020-01-10 15:24:13 +03:00
|
|
|
UniquePtr<ControllerManager> mControllerManager;
|
2020-10-02 13:02:00 +03:00
|
|
|
nsString mFallbackTitle;
|
2020-10-14 23:04:34 +03:00
|
|
|
|
|
|
|
// Used for telemetry probe.
|
|
|
|
void UpdateTelemetryUsageProbe();
|
|
|
|
bool mHasEverUsedMediaControl = false;
|
|
|
|
bool mHasEverEnabledMediaControl = false;
|
2019-08-07 04:46:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|