Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 1605769 - part1 : use PlaybackState in media controller and add…
Browse files Browse the repository at this point in the history
… a method to monitor its change r=MeFisto94

We use `PlaybackState` to replace `boolean` which can clearly indicate what controller's current state is and introduce a new method `PlayStateChangedEvent()` which can be used to monitor the play state change of the media controller.

Differential Revision: https://phabricator.services.mozilla.com/D58174
  • Loading branch information
alastor0325 committed Jan 9, 2020
1 parent 81eb029 commit a267025
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
8 changes: 5 additions & 3 deletions dom/media/mediacontrol/MediaControlKeysEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@ void MediaControlKeysHandler::OnKeyPressed(MediaControlKeysEvent aKeyEvent) {
return;
}

const bool isControllerPlaying =
controller->GetState() == PlaybackState::ePlaying;
switch (aKeyEvent) {
case MediaControlKeysEvent::ePlay:
if (!controller->IsPlaying()) {
if (!isControllerPlaying) {
controller->Play();
}
return;
case MediaControlKeysEvent::ePause:
if (controller->IsPlaying()) {
if (isControllerPlaying) {
controller->Pause();
}
return;
case MediaControlKeysEvent::ePlayPause: {
if (controller->IsPlaying()) {
if (isControllerPlaying) {
controller->Pause();
} else {
controller->Play();
Expand Down
29 changes: 20 additions & 9 deletions dom/media/mediacontrol/MediaController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ MediaController::~MediaController() {

void MediaController::Play() {
LOG("Play");
mIsPlaying = true;
SetPlayState(PlaybackState::ePlaying);
UpdateMediaControlKeysEventToContentMediaIfNeeded(
MediaControlKeysEvent::ePlay);
}

void MediaController::Pause() {
LOG("Pause");
mIsPlaying = false;
SetPlayState(PlaybackState::ePaused);
UpdateMediaControlKeysEventToContentMediaIfNeeded(
MediaControlKeysEvent::ePause);
}

void MediaController::Stop() {
LOG("Stop");
mIsPlaying = false;
SetPlayState(PlaybackState::eStopped);
UpdateMediaControlKeysEventToContentMediaIfNeeded(
MediaControlKeysEvent::eStop);
}
Expand All @@ -68,7 +68,7 @@ void MediaController::UpdateMediaControlKeysEventToContentMediaIfNeeded(
}

void MediaController::Shutdown() {
mIsPlaying = false;
SetPlayState(PlaybackState::eStopped);
mControlledMediaNum = 0;
RefPtr<MediaControlService> service = MediaControlService::GetService();
MOZ_ASSERT(service);
Expand Down Expand Up @@ -123,7 +123,7 @@ void MediaController::IncreasePlayingControlledMediaNum() {
"The number of playing media should not exceed the number of "
"controlled media!");
if (mPlayingControlledMediaNum == 1) {
mIsPlaying = true;
SetPlayState(PlaybackState::ePlaying);
}
}

Expand All @@ -133,7 +133,7 @@ void MediaController::DecreasePlayingControlledMediaNum() {
mPlayingControlledMediaNum);
MOZ_ASSERT(mPlayingControlledMediaNum >= 0);
if (mPlayingControlledMediaNum == 0) {
mIsPlaying = false;
SetPlayState(PlaybackState::ePaused);
}
}

Expand All @@ -151,11 +151,22 @@ void MediaController::Deactivate() {
service->GetAudioFocusManager().RevokeAudioFocus(Id());
}

uint64_t MediaController::Id() const { return mBrowsingContextId; }
void MediaController::SetPlayState(PlaybackState aState) {
if (mState == aState) {
return;
}
LOG("SetPlayState : '%s'", ToPlaybackStateEventStr(aState));
mState = aState;
mPlaybackStateChangedEvent.Notify(mState);
}

bool MediaController::IsPlaying() const { return mIsPlaying; }
PlaybackState MediaController::GetState() const { return mState; }

bool MediaController::IsAudible() const { return IsPlaying() && mAudible; }
uint64_t MediaController::Id() const { return mBrowsingContextId; }

bool MediaController::IsAudible() const {
return mState == PlaybackState::ePlaying && mAudible;
}

uint64_t MediaController::ControlledMediaNum() const {
return mControlledMediaNum;
Expand Down
13 changes: 11 additions & 2 deletions dom/media/mediacontrol/MediaController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define DOM_MEDIA_MEDIACONTROL_MEDIACONTROLLER_H_

#include "ContentMediaController.h"
#include "MediaEventSource.h"
#include "nsDataHashtable.h"
#include "nsISupportsImpl.h"

Expand Down Expand Up @@ -63,9 +64,13 @@ class MediaController final {
void Shutdown();

uint64_t Id() const;
bool IsPlaying() const;
bool IsAudible() const;
uint64_t ControlledMediaNum() const;
PlaybackState GetState() const;

MediaEventSource<PlaybackState>& PlaybackStateChangedEvent() {
return mPlaybackStateChangedEvent;
}

// These methods are only being used to notify the state changes of controlled
// media in ContentParent or MediaControlUtils.
Expand All @@ -85,11 +90,15 @@ class MediaController final {
void Activate();
void Deactivate();

void SetPlayState(PlaybackState aState);

uint64_t mBrowsingContextId;
bool mIsPlaying = false;
bool mAudible = false;
int64_t mControlledMediaNum = 0;
int64_t mPlayingControlledMediaNum = 0;

PlaybackState mState = PlaybackState::eStopped;
MediaEventProducer<PlaybackState> mPlaybackStateChangedEvent;
};

} // namespace dom
Expand Down
38 changes: 18 additions & 20 deletions dom/media/mediacontrol/tests/gtest/TestMediaController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ TEST(MediaController, DefaultValueCheck)
RefPtr<MediaController> controller = new MediaController(CONTROLLER_ID);
ASSERT_TRUE(controller->ControlledMediaNum() == 0);
ASSERT_TRUE(controller->Id() == CONTROLLER_ID);
ASSERT_TRUE(!controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::eStopped);
ASSERT_TRUE(!controller->IsAudible());
}

Expand Down Expand Up @@ -89,19 +89,19 @@ TEST(MediaController, AlwaysInaudibleIfControllerIsNotPlaying)
TEST(MediaController, ChangePlayingStateViaPlayPauseStop)
{
RefPtr<MediaController> controller = new MediaController(CONTROLLER_ID);
ASSERT_TRUE(!controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::eStopped);

controller->Play();
ASSERT_TRUE(controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::ePlaying);

controller->Pause();
ASSERT_TRUE(!controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::ePaused);

controller->Play();
ASSERT_TRUE(controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::ePlaying);

controller->Stop();
ASSERT_TRUE(!controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::eStopped);
}

class FakeControlledMedia final {
Expand Down Expand Up @@ -136,55 +136,53 @@ class FakeControlledMedia final {
TEST(MediaController, PlayingStateChangeViaControlledMedia)
{
RefPtr<MediaController> controller = new MediaController(CONTROLLER_ID);
ASSERT_TRUE(!controller->IsPlaying());

// In order to check playing state after FakeControlledMedia destroyed.
{
FakeControlledMedia foo(controller);
ASSERT_TRUE(!controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::eStopped);

foo.SetPlaying(true);
ASSERT_TRUE(controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::ePlaying);

foo.SetPlaying(false);
ASSERT_TRUE(!controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::ePaused);

foo.SetPlaying(true);
ASSERT_TRUE(controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::ePlaying);
}

// FakeControlledMedia has been destroyed, no playing media exists.
ASSERT_TRUE(!controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::ePaused);
}

TEST(MediaController, ControllerShouldRemainPlayingIfAnyPlayingMediaExists)
{
RefPtr<MediaController> controller = new MediaController(CONTROLLER_ID);
ASSERT_TRUE(!controller->IsPlaying());

{
FakeControlledMedia foo(controller);
ASSERT_TRUE(!controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::eStopped);

foo.SetPlaying(true);
ASSERT_TRUE(controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::ePlaying);

// foo is playing, so controller is in `playing` state.
FakeControlledMedia bar(controller);
ASSERT_TRUE(controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::ePlaying);

bar.SetPlaying(true);
ASSERT_TRUE(controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::ePlaying);

// Although we paused bar, but foo is still playing, so the controller would
// still be in `playing`.
bar.SetPlaying(false);
ASSERT_TRUE(controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::ePlaying);

foo.SetPlaying(false);
ASSERT_TRUE(!controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::ePaused);
}

// both foo and bar have been destroyed, no playing media exists.
ASSERT_TRUE(!controller->IsPlaying());
ASSERT_TRUE(controller->GetState() == PlaybackState::ePaused);
}

0 comments on commit a267025

Please sign in to comment.