DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
/* 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/. */
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef DOM_MEDIA_MEDIACONTROL_MEDIACONTROLSERVICE_H_
#define DOM_MEDIA_MEDIACONTROL_MEDIACONTROLSERVICE_H_

#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/AlreadyAddRefed.h"

#include "AudioFocusManager.h"
#include "MediaController.h"
#include "MediaControlKeyManager.h"
#include "MediaEventSource.h"
#include "mozilla/dom/MediaControllerBinding.h"
#include "nsDataHashtable.h"
#include "nsIObserver.h"
#include "nsTArray.h"

namespace mozilla {
namespace mozilla {
namespace dom {

/**
 * MediaControlService is an interface to access controllers by providing
 * controller Id. Everytime when controller becomes active, which means there is
 * 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
 * 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:
 public:
  NS_DECL_ISUPPORTS
  NS_DECL_NSIOBSERVER

  static RefPtr<MediaControlService> GetService();

  AudioFocusManager& GetAudioFocusManager() { return mAudioFocusManager; }
  MediaControlKeySource* GetMediaControlKeySource() {
  MediaControlKeysEventSource* GetMediaControlKeysEventSource() {
    return mMediaControlKeyManager;
  }

  // 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);
  bool UnregisterActiveMediaController(MediaController* aController);
  uint64_t GetActiveControllersNum() const;

  // This method would be called when the controller changes its playback state.
  void NotifyControllerPlaybackStateChanged(MediaController* aController);


  // This method would be called when the controller starts to being used in the
  // picture-in-picture mode.
  void NotifyControllerBeingUsedInPictureInPictureMode(
      MediaController* aController);


  // 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;

  // This event is used to generate a media event indicating media controller
  // This event is used to generate a media event indicating media controller
  // amount changed.
  MediaEventSource<uint64_t>& MediaControllerAmountChangedEvent() {
    return mMediaControllerAmountChangedEvent;
  }
  }


  /**
   * 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.
   */
   */
  void GenerateTestMediaControlKey(MediaControlKey aKey);
  MediaMetadataBase GetMainControllerMediaMetadata() const;
  MediaSessionPlaybackState GetMainControllerPlaybackState() const;

 private:
 private:
  MediaControlService();
  ~MediaControlService();

  /**
   * When there are multiple media controllers existing, we would only choose
   * 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
   * 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
   * 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 {
  class ControllerManager final {
   public:
   public:
    explicit ControllerManager(MediaControlService* aService);
    ~ControllerManager() = default;

    using MediaKeysArray = nsTArray<MediaControlKey>;
    bool RemoveController(MediaController* aController);
    using LinkedListControllerPtr = LinkedListElement<RefPtr<MediaController>>*;
    using ConstLinkedListControllerPtr =
        const LinkedListElement<RefPtr<MediaController>>*;

    bool AddController(MediaController* aController);
    MediaController* GetMainController() const;
    bool RemoveController(MediaController* aController);
    void UpdateMainControllerIfNeeded(MediaController* aController);

    void Shutdown();

    // These functions are used for monitoring main controller's status change.
    MediaController* GetMainController() const;
    MediaController* GetControllerById(uint64_t aId) const;
    bool Contains(MediaController* aController) const;
    void MainControllerMetadataChanged(const MediaMetadataBase& aMetadata);
    uint64_t GetControllersNum() const;

    // These functions are used for monitoring main controller's status change.
    void MainControllerPlaybackStateChanged(MediaSessionPlaybackState aState);
    void MainControllerMetadataChanged(const MediaMetadataBase& aMetadata);
    // When applying `eInsertBeforeTail`, list would become [A, C, B, D].

   private:
    // Assume that we have a list [A, B, C, D], and we want to reorder B.
    // When applying `eInsertToTail`, list would become [A, C, D, B].
    };
    // When applying `eInsertBeforeTail`, list would become [A, C, B, D].
    enum class InsertOptions {
      eInsertToTail,
      eInsertBeforeTail,
    };


    // Adjust the given controller's order by the insert option.
    void ReorderGivenController(MediaController* aController,
                                InsertOptions aOption);

    LinkedList<RefPtr<MediaController>> mControllers;
    void UpdateMainControllerInternal(MediaController* aController);
    void ConnectMainControllerEvents();
    void DisconnectMainControllerEvents();

    RefPtr<MediaControlKeysEventSource> mSource;
    LinkedList<RefPtr<MediaController>> mControllers;
    RefPtr<MediaController> mMainController;

    // These member are use to listen main controller's play state changes and
    // update the playback state to the event source.
    RefPtr<MediaControlKeySource> mSource;
    MediaEventListener mMetadataChangedListener;
    MediaEventListener mSupportedKeysChangedListener;
  };

  RefPtr<MediaControlKeysManager> mMediaControlKeysManager;
  void Init();
  void Shutdown();

  AudioFocusManager mAudioFocusManager;
  RefPtr<MediaControlKeyManager> mMediaControlKeyManager;

  RefPtr<MediaControlKeyListener> mMediaKeysHandler;
  MediaEventProducer<uint64_t> mMediaControllerAmountChangedEvent;
  UniquePtr<ControllerManager> mControllerManager;
};

}  // namespace dom
}  // namespace mozilla

#endif