summaryrefslogtreecommitdiffstats
path: root/dom/media/MediaPlaybackDelayPolicy.cpp
blob: 1ac326db7c21c575298c678251c2d1484294171d (plain)
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
/* 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 "MediaPlaybackDelayPolicy.h"

#include "nsPIDOMWindow.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "mozilla/StaticPrefs_media.h"

namespace mozilla::dom {

using AudibleState = AudioChannelService::AudibleState;

static AudibleState DetermineMediaAudibleState(const HTMLMediaElement* aElement,
                                               bool aIsAudible) {
  MOZ_ASSERT(aElement);
  if (!aElement->HasAudio()) {
    return AudibleState::eNotAudible;
  }
  // `eMaybeAudible` is used to distinguish if the media has audio track or not,
  // because we would only show the delayed media playback icon for media with
  // an audio track.
  return aIsAudible ? AudibleState::eAudible : AudibleState::eMaybeAudible;
}

ResumeDelayedPlaybackAgent::~ResumeDelayedPlaybackAgent() {
  if (mDelegate) {
    mDelegate->Clear();
    mDelegate = nullptr;
  }
}

bool ResumeDelayedPlaybackAgent::InitDelegate(const HTMLMediaElement* aElement,
                                              bool aIsAudible) {
  MOZ_ASSERT(!mDelegate, "Delegate has been initialized!");
  mDelegate = new ResumePlayDelegate();
  if (!mDelegate->Init(aElement, aIsAudible)) {
    mDelegate->Clear();
    mDelegate = nullptr;
    return false;
  }
  return true;
}

RefPtr<ResumeDelayedPlaybackAgent::ResumePromise>
ResumeDelayedPlaybackAgent::GetResumePromise() {
  MOZ_ASSERT(mDelegate);
  return mDelegate->GetResumePromise();
}

void ResumeDelayedPlaybackAgent::UpdateAudibleState(
    const HTMLMediaElement* aElement, bool aIsAudible) {
  MOZ_ASSERT(aElement);
  MOZ_ASSERT(mDelegate);
  mDelegate->UpdateAudibleState(aElement, aIsAudible);
}

NS_IMPL_ISUPPORTS(ResumeDelayedPlaybackAgent::ResumePlayDelegate,
                  nsIAudioChannelAgentCallback)

ResumeDelayedPlaybackAgent::ResumePlayDelegate::~ResumePlayDelegate() {
  MOZ_ASSERT(!mAudioChannelAgent);
}

bool ResumeDelayedPlaybackAgent::ResumePlayDelegate::Init(
    const HTMLMediaElement* aElement, bool aIsAudible) {
  MOZ_ASSERT(aElement);
  if (!aElement->OwnerDoc()->GetInnerWindow()) {
    return false;
  }

  MOZ_ASSERT(!mAudioChannelAgent);
  mAudioChannelAgent = new AudioChannelAgent();
  nsresult rv =
      mAudioChannelAgent->Init(aElement->OwnerDoc()->GetInnerWindow(), this);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    Clear();
    return false;
  }

  // Start AudioChannelAgent in order to wait the suspended state change when we
  // are able to resume delayed playback.
  AudibleState audibleState = DetermineMediaAudibleState(aElement, aIsAudible);
  rv = mAudioChannelAgent->NotifyStartedPlaying(audibleState);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    Clear();
    return false;
  }

  return true;
}

void ResumeDelayedPlaybackAgent::ResumePlayDelegate::Clear() {
  if (mAudioChannelAgent) {
    mAudioChannelAgent->NotifyStoppedPlaying();
    mAudioChannelAgent = nullptr;
  }
  mPromise.RejectIfExists(true, __func__);
}

RefPtr<ResumeDelayedPlaybackAgent::ResumePromise>
ResumeDelayedPlaybackAgent::ResumePlayDelegate::GetResumePromise() {
  return mPromise.Ensure(__func__);
}

void ResumeDelayedPlaybackAgent::ResumePlayDelegate::UpdateAudibleState(
    const HTMLMediaElement* aElement, bool aIsAudible) {
  MOZ_ASSERT(aElement);
  // It's possible for the owner of `ResumeDelayedPlaybackAgent` to call
  // `UpdateAudibleState()` after we resolve the promise and clean resource.
  if (!mAudioChannelAgent) {
    return;
  }
  AudibleState audibleState = DetermineMediaAudibleState(aElement, aIsAudible);
  mAudioChannelAgent->NotifyStartedAudible(
      audibleState,
      AudioChannelService::AudibleChangedReasons::eDataAudibleChanged);
}

NS_IMETHODIMP
ResumeDelayedPlaybackAgent::ResumePlayDelegate::WindowVolumeChanged(
    float aVolume, bool aMuted) {
  return NS_OK;
}

NS_IMETHODIMP
ResumeDelayedPlaybackAgent::ResumePlayDelegate::WindowAudioCaptureChanged(
    bool aCapture) {
  return NS_OK;
}

NS_IMETHODIMP
ResumeDelayedPlaybackAgent::ResumePlayDelegate::WindowSuspendChanged(
    SuspendTypes aSuspend) {
  if (aSuspend == nsISuspendedTypes::NONE_SUSPENDED) {
    mPromise.ResolveIfExists(true, __func__);
    Clear();
  }
  return NS_OK;
}

bool MediaPlaybackDelayPolicy::ShouldDelayPlayback(
    const HTMLMediaElement* aElement) {
  MOZ_ASSERT(aElement);
  if (!StaticPrefs::media_block_autoplay_until_in_foreground()) {
    return false;
  }

  const Document* doc = aElement->OwnerDoc();
  nsPIDOMWindowInner* inner = nsPIDOMWindowInner::From(doc->GetInnerWindow());
  nsPIDOMWindowOuter* outer = nsPIDOMWindowOuter::GetFromCurrentInner(inner);
  return outer && outer->ShouldDelayMediaFromStart();
}

RefPtr<ResumeDelayedPlaybackAgent>
MediaPlaybackDelayPolicy::CreateResumeDelayedPlaybackAgent(
    const HTMLMediaElement* aElement, bool aIsAudible) {
  MOZ_ASSERT(aElement);
  RefPtr<ResumeDelayedPlaybackAgent> agent = new ResumeDelayedPlaybackAgent();
  return agent->InitDelegate(aElement, aIsAudible) ? agent : nullptr;
}

}  // namespace mozilla::dom