Bug 1605769 - part0 : extend PlaybackState and move it out from MediaEventSource. r=MeFisto94
authoralwu <alwu@mozilla.com>
Fri, 10 Jan 2020 12:23:04 +0000
changeset 509767 6868755789c3a93ec277953166a738c32b471d92
parent 509766 ec620113838689ad5fece3978fbece96a19042a5
child 509768 914bdf2c92e24eb1a7eaf8952bfd44e189e9ef18
push id37004
push usershindli@mozilla.com
push dateSat, 11 Jan 2020 09:48:29 +0000
treeherdermozilla-central@fb64636dad2c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMeFisto94
bugs1605769
milestone74.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1605769 - part0 : extend PlaybackState and move it out from MediaEventSource. r=MeFisto94 Since we would like to use `PlaybackState` in MediaController, we move it out from `MediaEventSource` and put it in `MediaControl.h`, which makes more sense because it actually represents media controller's playback state. In addition, modify `ePlayed` to `ePlaying` and add one new state `eStopped`. Differential Revision: https://phabricator.services.mozilla.com/D59100
dom/media/mediacontrol/MediaControlKeysEvent.cpp
dom/media/mediacontrol/MediaControlKeysEvent.h
dom/media/mediacontrol/MediaControlKeysManager.cpp
dom/media/mediacontrol/MediaControlUtils.h
dom/media/mediacontrol/MediaController.h
dom/media/mediacontrol/tests/gtest/TestMediaKeysEvent.cpp
--- a/dom/media/mediacontrol/MediaControlKeysEvent.cpp
+++ b/dom/media/mediacontrol/MediaControlKeysEvent.cpp
@@ -1,25 +1,24 @@
 /* -*- 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 "MediaControlKeysEvent.h"
 
+#include "MediaController.h"
 #include "MediaControlUtils.h"
 #include "MediaControlService.h"
 #include "mozilla/Logging.h"
 
 namespace mozilla {
 namespace dom {
 
-using PlaybackState = MediaControlKeysEventSource::PlaybackState;
-
 // avoid redefined macro in unified build
 #undef LOG_SOURCE
 #define LOG_SOURCE(msg, ...)                 \
   MOZ_LOG(gMediaControlLog, LogLevel::Debug, \
           ("MediaControlKeysEventSource=%p, " msg, this, ##__VA_ARGS__))
 
 #undef LOG_KEY
 #define LOG_KEY(msg, key, ...)                       \
@@ -66,16 +65,19 @@ void MediaControlKeysHandler::OnKeyPress
       controller->Stop();
       return;
     default:
       MOZ_ASSERT_UNREACHABLE("Error : undefined event!");
       return;
   }
 }
 
+MediaControlKeysEventSource::MediaControlKeysEventSource()
+    : mPlaybackState(PlaybackState::eStopped) {}
+
 void MediaControlKeysEventSource::AddListener(
     MediaControlKeysEventListener* aListener) {
   MOZ_ASSERT(aListener);
   LOG_SOURCE("Add listener %p", aListener);
   mListeners.AppendElement(aListener);
 }
 
 void MediaControlKeysEventSource::RemoveListener(
--- a/dom/media/mediacontrol/MediaControlKeysEvent.h
+++ b/dom/media/mediacontrol/MediaControlKeysEvent.h
@@ -55,33 +55,27 @@ class MediaControlKeysHandler final : pu
  public:
   NS_INLINE_DECL_REFCOUNTING(MediaControlKeysHandler, override)
   void OnKeyPressed(MediaControlKeysEvent aKeyEvent) override;
 
  private:
   virtual ~MediaControlKeysHandler() = default;
 };
 
+enum class PlaybackState : uint8_t;
+
 /**
  * MediaControlKeysEventSource is an abstract class which is used to implement
  * transporting media control keys event to all its listeners when media keys
  * event happens.
  */
 class MediaControlKeysEventSource {
  public:
   NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
-  MediaControlKeysEventSource() = default;
-
-  // This is used to indicate current media playback state. For those platforms
-  // which have virtual control interface, we have to update the playback state
-  // correctly in order to show the correct control icon on the interface.
-  enum class PlaybackState : uint8_t {
-    ePaused,
-    ePlayed,
-  };
+  MediaControlKeysEventSource();
 
   virtual void AddListener(MediaControlKeysEventListener* aListener);
   virtual void RemoveListener(MediaControlKeysEventListener* aListener);
   size_t GetListenersNum() const;
 
   // Return true if the initialization of the source succeeds, and inherited
   // sources should implement this method to handle the initialization fails.
   virtual bool Open() = 0;
@@ -89,15 +83,15 @@ class MediaControlKeysEventSource {
   virtual bool IsOpened() const = 0;
 
   virtual void SetPlaybackState(PlaybackState aState);
   virtual PlaybackState GetPlaybackState() const;
 
  protected:
   virtual ~MediaControlKeysEventSource() = default;
   nsTArray<RefPtr<MediaControlKeysEventListener>> mListeners;
-  PlaybackState mPlaybackState = PlaybackState::ePaused;
+  PlaybackState mPlaybackState;
 };
 
 }  // namespace dom
 }  // namespace mozilla
 
 #endif
--- a/dom/media/mediacontrol/MediaControlKeysManager.cpp
+++ b/dom/media/mediacontrol/MediaControlKeysManager.cpp
@@ -15,18 +15,16 @@
 #undef LOG
 #define LOG(msg, ...)                        \
   MOZ_LOG(gMediaControlLog, LogLevel::Debug, \
           ("MediaControlKeysManager=%p, " msg, this, ##__VA_ARGS__))
 
 namespace mozilla {
 namespace dom {
 
-using PlaybackState = MediaControlKeysEventSource::PlaybackState;
-
 bool MediaControlKeysManager::IsOpened() const {
   // As MediaControlKeysManager represents a platform-indenpendent event source,
   // which we can use to add other listeners to moniter media key events, we
   // would always return true even if we fail to open the real media key event
   // source, because we still have chances to open the source again when there
   // are other new controllers being added.
   return true;
 }
--- a/dom/media/mediacontrol/MediaControlUtils.h
+++ b/dom/media/mediacontrol/MediaControlUtils.h
@@ -60,23 +60,24 @@ ConvertMediaControlKeysTestEventToMediaC
     case MediaControlKeysTestEvent::SeekForward:
       return MediaControlKeysEvent::eSeekForward;
     default:
       MOZ_ASSERT(aEvent == MediaControlKeysTestEvent::Stop);
       return MediaControlKeysEvent::eStop;
   }
 }
 
-inline const char* ToPlaybackStateEventStr(
-    MediaControlKeysEventSource::PlaybackState aState) {
+inline const char* ToPlaybackStateEventStr(PlaybackState aState) {
   switch (aState) {
-    case MediaControlKeysEventSource::PlaybackState::ePlayed:
-      return "Played";
-    case MediaControlKeysEventSource::PlaybackState::ePaused:
+    case PlaybackState::ePlaying:
+      return "Playing";
+    case PlaybackState::ePaused:
       return "Paused";
+    case PlaybackState::eStopped:
+      return "Stopped";
     default:
       MOZ_ASSERT_UNREACHABLE("Invalid playback state.");
       return "Unknown";
   }
 }
 
 inline const char* ToControlledMediaStateStr(ControlledMediaState aState) {
   switch (aState) {
--- a/dom/media/mediacontrol/MediaController.h
+++ b/dom/media/mediacontrol/MediaController.h
@@ -3,24 +3,34 @@
 /* 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/. */
 
 #ifndef DOM_MEDIA_MEDIACONTROL_MEDIACONTROLLER_H_
 #define DOM_MEDIA_MEDIACONTROL_MEDIACONTROLLER_H_
 
 #include "ContentMediaController.h"
-#include "MediaControlKeysEvent.h"
 #include "nsDataHashtable.h"
 #include "nsISupportsImpl.h"
 
 namespace mozilla {
 namespace dom {
 
 class BrowsingContext;
+enum class MediaControlKeysEvent : uint32_t;
+
+// This is used to indicate current media playback state for media controller.
+// For those platforms which have virtual control interface, we have to update
+// the playback state correctly in order to show the correct control icon on the
+// interface.
+enum class PlaybackState : uint8_t {
+  ePlaying,
+  ePaused,
+  eStopped,
+};
 
 /**
  * MediaController is a class, which is used to control all media within a tab.
  * It can only be used in Chrome process and the controlled media are usually
  * in the content process (unless we disable e10s).
  *
  * Each tab would have only one media controller, they are 1-1 corresponding
  * relationship, we use tab's top-level browsing context ID to initialize the
--- a/dom/media/mediacontrol/tests/gtest/TestMediaKeysEvent.cpp
+++ b/dom/media/mediacontrol/tests/gtest/TestMediaKeysEvent.cpp
@@ -1,17 +1,17 @@
 /* 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 "gtest/gtest.h"
+#include "MediaController.h"
 #include "MediaControlKeysEvent.h"
 
 using namespace mozilla::dom;
-using PlaybackState = MediaControlKeysEventSource::PlaybackState;
 
 class MediaControlKeysEventSourceTestImpl : public MediaControlKeysEventSource {
  public:
   NS_INLINE_DECL_REFCOUNTING(MediaControlKeysEventSourceTestImpl, override)
   bool Open() override { return true; }
   bool IsOpened() const override { return true; }
 
  private:
@@ -33,16 +33,19 @@ TEST(MediaControlKeysEvent, TestAddOrRem
   source->RemoveListener(listener);
   ASSERT_TRUE(source->GetListenersNum() == 0);
 }
 
 TEST(MediaControlKeysEvent, SetSourcePlaybackState)
 {
   RefPtr<MediaControlKeysEventSource> source =
       new MediaControlKeysEventSourceTestImpl();
-  ASSERT_TRUE(source->GetPlaybackState() == PlaybackState::ePaused);
+  ASSERT_TRUE(source->GetPlaybackState() == PlaybackState::eStopped);
 
-  source->SetPlaybackState(PlaybackState::ePlayed);
-  ASSERT_TRUE(source->GetPlaybackState() == PlaybackState::ePlayed);
+  source->SetPlaybackState(PlaybackState::ePlaying);
+  ASSERT_TRUE(source->GetPlaybackState() == PlaybackState::ePlaying);
 
   source->SetPlaybackState(PlaybackState::ePaused);
   ASSERT_TRUE(source->GetPlaybackState() == PlaybackState::ePaused);
+
+  source->SetPlaybackState(PlaybackState::eStopped);
+  ASSERT_TRUE(source->GetPlaybackState() == PlaybackState::eStopped);
 }