Bug 923247 - patch 3 - audio-active/inactive notifications per window, r=ehsan, r=roc

--HG--
rename : content/html/content/test/wakelock.ogg => dom/base/test/audio.ogg
This commit is contained in:
Andrea Marchesini 2014-03-11 10:47:25 +00:00
parent 7c2d706f91
commit 05bde413ca
5 changed files with 143 additions and 0 deletions

View File

@ -113,6 +113,18 @@ AudioChannelService::RegisterAudioChannelAgent(AudioChannelAgent* aAgent,
aWithVideo);
mAgents.Put(aAgent, data);
RegisterType(aType, CONTENT_PROCESS_ID_MAIN, aWithVideo);
// If this is the first agent for this window, we must notify the observers.
uint32_t count = CountWindow(aAgent->Window());
if (count == 1) {
nsCOMPtr<nsIObserverService> observerService =
services::GetObserverService();
if (observerService) {
observerService->NotifyObservers(ToSupports(aAgent->Window()),
"media-playback",
NS_LITERAL_STRING("active").get());
}
}
}
void
@ -181,6 +193,18 @@ AudioChannelService::UnregisterAudioChannelAgent(AudioChannelAgent* aAgent)
mSpeakerManager[i]->SetAudioChannelActive(active);
}
#endif
// If this is the last agent for this window, we must notify the observers.
uint32_t count = CountWindow(aAgent->Window());
if (count == 0) {
nsCOMPtr<nsIObserverService> observerService =
services::GetObserverService();
if (observerService) {
observerService->NotifyObservers(ToSupports(aAgent->Window()),
"media-playback",
NS_LITERAL_STRING("inactive").get());
}
}
}
void
@ -822,3 +846,37 @@ AudioChannelService::RefreshAgentsVolume(nsPIDOMWindow* aWindow)
data.mAgents[i]->WindowVolumeChanged();
}
}
struct CountWindowData
{
CountWindowData(nsIDOMWindow* aWindow)
: mWindow(aWindow)
, mCount(0)
{}
nsIDOMWindow* mWindow;
uint32_t mCount;
};
PLDHashOperator
AudioChannelService::CountWindowEnumerator(AudioChannelAgent* aAgent,
AudioChannelAgentData* aUnused,
void* aPtr)
{
CountWindowData* data = static_cast<CountWindowData*>(aPtr);
MOZ_ASSERT(aAgent);
if (aAgent->Window() == data->mWindow) {
++data->mCount;
}
return PL_DHASH_NEXT;
}
uint32_t
AudioChannelService::CountWindow(nsIDOMWindow* aWindow)
{
CountWindowData data(aWindow);
mAgents.EnumerateRead(CountWindowEnumerator, &data);
return data.mCount;
}

View File

@ -188,6 +188,14 @@ protected:
AudioChannelAgentData* aUnused,
void *aPtr);
static PLDHashOperator
CountWindowEnumerator(AudioChannelAgent* aAgent,
AudioChannelAgentData* aUnused,
void *aPtr);
// This returns the number of agents from this aWindow.
uint32_t CountWindow(nsIDOMWindow* aWindow);
nsClassHashtable< nsPtrHashKey<AudioChannelAgent>, AudioChannelAgentData > mAgents;
#ifdef MOZ_WIDGET_GONK
nsTArray<SpeakerManagerService*> mSpeakerManager;

BIN
dom/base/test/audio.ogg Normal file

Binary file not shown.

View File

@ -1,5 +1,6 @@
[DEFAULT]
support-files =
audio.ogg
iframe_messageChannel_cloning.html
iframe_messageChannel_pingpong.html
iframe_messageChannel_post.html
@ -9,6 +10,7 @@ support-files =
[test_Image_constructor.html]
[test_appname_override.html]
[test_audioWindowUtils.html]
[test_audioNotification.html]
[test_bug913761.html]
[test_bug978522.html]
[test_bug979109.html]

View File

@ -0,0 +1,75 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for audio controller in windows</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<pre id="test">
</pre>
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
var expectedNotification = null;
var observer = {
observe: function(subject, topic, data) {
is(topic, "media-playback", "media-playback received");
is(data, expectedNotification, "This is the right notification");
runTest();
}
};
var observerService = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
.getService(SpecialPowers.Ci.nsIObserverService);
var audio = new Audio();
audio.src = "audio.ogg";
var tests = [
function() {
SpecialPowers.pushPrefEnv({"set": [["media.useAudioChannelService", true]]}, runTest);
},
function() {
observerService.addObserver(observer, "media-playback", false);
ok(true, "Observer set");
runTest();
},
function() {
expectedNotification = 'active';
audio.play();
},
function() {
expectedNotification = 'inactive';
audio.pause();
},
function() {
observerService.removeObserver(observer, "media-playback");
ok(true, "Observer removed");
runTest();
}
];
function runTest() {
if (!tests.length) {
SimpleTest.finish();
return;
}
var test = tests.shift();
test();
}
runTest();
</script>
</body>
</html>