2012-12-06 18:29:20 +04: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/. */
|
|
|
|
|
|
|
|
#include "AudioChannelAgent.h"
|
|
|
|
#include "AudioChannelService.h"
|
2015-12-11 19:17:33 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2016-04-14 16:19:41 +03:00
|
|
|
#include "nsContentUtils.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2014-03-11 14:46:55 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2012-12-06 18:29:20 +04:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2015-07-17 14:08:00 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(AudioChannelAgent)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AudioChannelAgent)
|
|
|
|
tmp->Shutdown();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AudioChannelAgent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2013-04-23 19:28:00 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AudioChannelAgent)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIAudioChannelAgent)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(AudioChannelAgent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(AudioChannelAgent)
|
2012-12-06 18:29:20 +04:00
|
|
|
|
|
|
|
AudioChannelAgent::AudioChannelAgent()
|
|
|
|
: mInnerWindowID(0), mIsRegToService(false) {
|
2017-02-03 05:48:38 +03:00
|
|
|
// Init service in the begining, it can help us to know whether there is any
|
|
|
|
// created media component via AudioChannelService::IsServiceStarted().
|
|
|
|
RefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
|
2012-12-06 18:29:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelAgent::~AudioChannelAgent() { Shutdown(); }
|
2015-07-17 14:08:00 +03:00
|
|
|
|
|
|
|
void AudioChannelAgent::Shutdown() {
|
2013-01-15 18:31:19 +04:00
|
|
|
if (mIsRegToService) {
|
2015-10-07 22:06:47 +03:00
|
|
|
NotifyStoppedPlaying();
|
2013-01-15 18:31:19 +04:00
|
|
|
}
|
2012-12-06 18:29:20 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 14:46:04 +04:00
|
|
|
NS_IMETHODIMP
|
2017-08-10 04:00:09 +03:00
|
|
|
AudioChannelAgent::Init(mozIDOMWindow* aWindow,
|
2014-03-11 14:46:04 +04:00
|
|
|
nsIAudioChannelAgentCallback* aCallback) {
|
2017-08-10 04:00:09 +03:00
|
|
|
return InitInternal(nsPIDOMWindowInner::From(aWindow), aCallback,
|
2016-01-30 20:05:36 +03:00
|
|
|
/* useWeakRef = */ false);
|
2013-04-04 00:35:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-01-30 20:05:36 +03:00
|
|
|
AudioChannelAgent::InitWithWeakCallback(
|
2014-03-11 14:46:04 +04:00
|
|
|
mozIDOMWindow* aWindow, nsIAudioChannelAgentCallback* aCallback) {
|
2017-08-10 04:00:09 +03:00
|
|
|
return InitInternal(nsPIDOMWindowInner::From(aWindow), aCallback,
|
2016-01-30 20:05:36 +03:00
|
|
|
/* useWeakRef = */ true);
|
2013-04-04 00:35:05 +04:00
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsresult AudioChannelAgent::FindCorrectWindow(nsPIDOMWindowInner* aWindow) {
|
2019-07-26 19:48:31 +03:00
|
|
|
mWindow = aWindow->GetInProcessScriptableTop();
|
2015-12-11 19:17:33 +03:00
|
|
|
if (NS_WARN_IF(!mWindow)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// From here we do an hack for nested iframes.
|
|
|
|
// The system app doesn't have access to the nested iframe objects so it
|
|
|
|
// cannot control the volume of the agents running in nested apps. What we do
|
|
|
|
// here is to assign those Agents to the top scriptable window of the parent
|
|
|
|
// iframe (what is controlled by the system app).
|
|
|
|
// For doing this we go recursively back into the chain of windows until we
|
|
|
|
// find apps that are not the system one.
|
2019-07-26 19:48:31 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> outerParent = mWindow->GetInProcessParent();
|
2016-01-30 20:05:36 +03:00
|
|
|
if (!outerParent || outerParent == mWindow) {
|
2015-12-11 19:17:33 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> parent = outerParent->GetCurrentInnerWindow();
|
|
|
|
if (!parent) {
|
2015-12-11 19:17:33 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = parent->GetExtantDoc();
|
2015-12-11 19:17:33 +03:00
|
|
|
if (!doc) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-14 16:19:41 +03:00
|
|
|
if (nsContentUtils::IsChromeDoc(doc)) {
|
2015-12-11 19:17:33 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
return FindCorrectWindow(parent);
|
2015-12-11 19:17:33 +03:00
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsresult AudioChannelAgent::InitInternal(
|
|
|
|
nsPIDOMWindowInner* aWindow, nsIAudioChannelAgentCallback* aCallback,
|
2015-07-10 19:38:44 +03:00
|
|
|
bool aUseWeakRef) {
|
2015-09-01 19:05:59 +03:00
|
|
|
if (NS_WARN_IF(!aWindow)) {
|
2016-11-08 06:06:04 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-09-01 19:05:59 +03:00
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
mInnerWindowID = aWindow->WindowID();
|
2015-09-01 19:05:59 +03:00
|
|
|
|
2015-12-11 19:17:33 +03:00
|
|
|
nsresult rv = FindCorrectWindow(aWindow);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2015-04-08 11:24:04 +03:00
|
|
|
}
|
|
|
|
|
2013-04-04 00:35:05 +04:00
|
|
|
if (aUseWeakRef) {
|
|
|
|
mWeakCallback = do_GetWeakReference(aCallback);
|
|
|
|
} else {
|
|
|
|
mCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
2015-11-10 13:32:00 +03:00
|
|
|
MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
|
2017-08-10 04:00:09 +03:00
|
|
|
("AudioChannelAgent, InitInternal, this = %p, "
|
|
|
|
"owner = %p, hasCallback = %d\n",
|
2015-11-10 13:32:00 +03:00
|
|
|
this, mWindow.get(), (!!mCallback || !!mWeakCallback)));
|
|
|
|
|
2012-12-06 18:29:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-09-25 00:17:59 +03:00
|
|
|
void AudioChannelAgent::PullInitialUpdate() {
|
|
|
|
RefPtr<AudioChannelService> service = AudioChannelService::Get();
|
|
|
|
MOZ_ASSERT(service);
|
|
|
|
MOZ_ASSERT(mIsRegToService);
|
|
|
|
|
|
|
|
AudioPlaybackConfig config = service->GetMediaConfig(mWindow);
|
2019-09-25 00:18:07 +03:00
|
|
|
MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
|
|
|
|
("AudioChannelAgent, PullInitialUpdate, this=%p, "
|
|
|
|
"mute=%s, volume=%f, suspend=%s, audioCapturing=%s\n",
|
|
|
|
this, config.mMuted ? "true" : "false", config.mVolume,
|
|
|
|
SuspendTypeToStr(config.mSuspend),
|
|
|
|
config.mCapturedAudio ? "true" : "false"));
|
2019-09-24 09:55:35 +03:00
|
|
|
WindowVolumeChanged(config.mVolume, config.mMuted);
|
2019-09-25 00:17:59 +03:00
|
|
|
WindowSuspendChanged(config.mSuspend);
|
|
|
|
WindowAudioCaptureChanged(InnerWindowID(), config.mCapturedAudio);
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:52:44 +03:00
|
|
|
NS_IMETHODIMP
|
2019-09-25 00:18:07 +03:00
|
|
|
AudioChannelAgent::NotifyStartedPlaying(uint8_t aAudible) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
|
2017-08-10 04:00:09 +03:00
|
|
|
if (service == nullptr || mIsRegToService) {
|
2012-12-06 18:29:20 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-12-13 17:47:13 +03:00
|
|
|
MOZ_ASSERT(AudioChannelService::AudibleState::eNotAudible == 0 &&
|
|
|
|
AudioChannelService::AudibleState::eMaybeAudible == 1 &&
|
|
|
|
AudioChannelService::AudibleState::eAudible == 2);
|
2016-05-03 12:59:27 +03:00
|
|
|
service->RegisterAudioChannelAgent(
|
|
|
|
this, static_cast<AudioChannelService::AudibleState>(aAudible));
|
2015-07-10 19:38:44 +03:00
|
|
|
|
2015-11-10 13:32:00 +03:00
|
|
|
MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
|
2019-09-25 00:18:07 +03:00
|
|
|
("AudioChannelAgent, NotifyStartedPlaying, this = %p, audible = %s\n",
|
2017-03-24 09:44:18 +03:00
|
|
|
this,
|
|
|
|
AudibleStateToStr(
|
2019-09-25 00:18:07 +03:00
|
|
|
static_cast<AudioChannelService::AudibleState>(aAudible))));
|
2019-09-24 09:55:43 +03:00
|
|
|
|
2012-12-06 18:29:20 +04:00
|
|
|
mIsRegToService = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:52:44 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioChannelAgent::NotifyStoppedPlaying() {
|
2017-08-10 04:00:09 +03:00
|
|
|
if (!mIsRegToService) {
|
2012-12-06 18:29:20 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-11-10 13:32:00 +03:00
|
|
|
MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
|
|
|
|
("AudioChannelAgent, NotifyStoppedPlaying, this = %p\n", this));
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
|
2015-11-16 20:34:52 +03:00
|
|
|
if (service) {
|
2015-12-24 12:17:30 +03:00
|
|
|
service->UnregisterAudioChannelAgent(this);
|
2015-11-16 20:34:52 +03:00
|
|
|
}
|
|
|
|
|
2012-12-06 18:29:20 +04:00
|
|
|
mIsRegToService = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:03:02 +03:00
|
|
|
NS_IMETHODIMP
|
2016-12-13 17:47:13 +03:00
|
|
|
AudioChannelAgent::NotifyStartedAudible(uint8_t aAudible, uint32_t aReason) {
|
2016-05-03 05:03:02 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
|
|
|
|
("AudioChannelAgent, NotifyStartedAudible, this = %p, "
|
2017-03-24 09:44:18 +03:00
|
|
|
"audible = %s, reason = %s\n",
|
|
|
|
this,
|
|
|
|
AudibleStateToStr(
|
|
|
|
static_cast<AudioChannelService::AudibleState>(aAudible)),
|
|
|
|
AudibleChangedReasonToStr(
|
|
|
|
static_cast<AudioChannelService::AudibleChangedReasons>(aReason))));
|
2016-05-03 05:03:02 +03:00
|
|
|
|
|
|
|
RefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
service->AudioAudibleChanged(
|
2016-06-01 05:26:04 +03:00
|
|
|
this, static_cast<AudioChannelService::AudibleState>(aAudible),
|
|
|
|
static_cast<AudioChannelService::AudibleChangedReasons>(aReason));
|
2016-05-03 05:03:02 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-04 00:35:05 +04:00
|
|
|
already_AddRefed<nsIAudioChannelAgentCallback>
|
|
|
|
AudioChannelAgent::GetCallback() {
|
|
|
|
nsCOMPtr<nsIAudioChannelAgentCallback> callback = mCallback;
|
|
|
|
if (!callback) {
|
|
|
|
callback = do_QueryReferent(mWeakCallback);
|
|
|
|
}
|
|
|
|
return callback.forget();
|
|
|
|
}
|
2014-03-11 14:46:55 +04:00
|
|
|
|
2019-09-24 09:55:35 +03:00
|
|
|
void AudioChannelAgent::WindowVolumeChanged(float aVolume, bool aMuted) {
|
2014-03-11 14:46:55 +04:00
|
|
|
nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
|
|
|
|
if (!callback) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:52:38 +03:00
|
|
|
MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
|
2017-03-24 09:44:18 +03:00
|
|
|
("AudioChannelAgent, WindowVolumeChanged, this = %p, mute = %s, "
|
|
|
|
"volume = %f\n",
|
2019-09-24 09:55:35 +03:00
|
|
|
this, aMuted ? "true" : "false", aVolume));
|
|
|
|
callback->WindowVolumeChanged(aVolume, aMuted);
|
2016-05-03 04:52:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioChannelAgent::WindowSuspendChanged(nsSuspendedTypes aSuspend) {
|
|
|
|
nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
|
|
|
|
if (!callback) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-28 14:59:04 +03:00
|
|
|
MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
|
2016-05-03 04:52:38 +03:00
|
|
|
("AudioChannelAgent, WindowSuspendChanged, this = %p, "
|
2017-03-24 09:44:18 +03:00
|
|
|
"suspended = %s\n",
|
|
|
|
this, SuspendTypeToStr(aSuspend)));
|
2016-05-03 04:52:38 +03:00
|
|
|
callback->WindowSuspendChanged(aSuspend);
|
|
|
|
}
|
|
|
|
|
2019-09-24 09:55:39 +03:00
|
|
|
AudioPlaybackConfig AudioChannelAgent::GetMediaConfig() const {
|
2016-05-03 04:52:38 +03:00
|
|
|
RefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
|
|
|
|
AudioPlaybackConfig config(1.0, false, nsISuspendedTypes::NONE_SUSPENDED);
|
|
|
|
if (service) {
|
2017-08-10 04:00:09 +03:00
|
|
|
config = service->GetMediaConfig(mWindow);
|
2016-05-03 04:52:38 +03:00
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
2016-04-28 14:59:04 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
uint64_t AudioChannelAgent::WindowID() const {
|
|
|
|
return mWindow ? mWindow->WindowID() : 0;
|
2014-03-11 14:46:55 +04:00
|
|
|
}
|
2015-07-09 17:40:08 +03:00
|
|
|
|
2015-12-24 12:28:45 +03:00
|
|
|
uint64_t AudioChannelAgent::InnerWindowID() const { return mInnerWindowID; }
|
|
|
|
|
|
|
|
void AudioChannelAgent::WindowAudioCaptureChanged(uint64_t aInnerWindowID,
|
|
|
|
bool aCapture) {
|
2015-07-09 17:40:08 +03:00
|
|
|
if (aInnerWindowID != mInnerWindowID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
|
|
|
|
if (!callback) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-24 12:28:45 +03:00
|
|
|
MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
|
|
|
|
("AudioChannelAgent, WindowAudioCaptureChanged, this = %p, "
|
|
|
|
"capture = %d\n",
|
|
|
|
this, aCapture));
|
|
|
|
|
|
|
|
callback->WindowAudioCaptureChanged(aCapture);
|
2015-07-09 17:40:08 +03:00
|
|
|
}
|
2016-11-01 12:46:09 +03:00
|
|
|
|
2019-09-24 09:55:39 +03:00
|
|
|
bool AudioChannelAgent::IsWindowAudioCapturingEnabled() const {
|
|
|
|
return GetMediaConfig().mCapturedAudio;
|
|
|
|
}
|
|
|
|
|
2016-11-01 12:46:09 +03:00
|
|
|
bool AudioChannelAgent::IsPlayingStarted() const { return mIsRegToService; }
|