Bug 1200208 - Send the audio-playback notification when the page calls HTMLMediaElement::Play() before the metadata has been fully loaded; r=baku

This commit is contained in:
Ehsan Akhgari 2015-08-31 12:50:20 -04:00
Родитель d3f5baeb34
Коммит 1fd49e2d20
4 изменённых файлов: 143 добавлений и 17 удалений

Просмотреть файл

@ -267,6 +267,8 @@ support-files =
skip-if = buildapp == 'mulet' skip-if = buildapp == 'mulet'
[test_audioNotificationStopOnNavigation.html] [test_audioNotificationStopOnNavigation.html]
skip-if = buildapp == 'mulet' skip-if = buildapp == 'mulet'
[test_audioNotificationWithEarlyPlay.html]
skip-if = buildapp == 'mulet'
[test_bug1091883.html] [test_bug1091883.html]
[test_bug116083.html] [test_bug116083.html]
[test_bug793311.html] [test_bug793311.html]

Просмотреть файл

@ -0,0 +1,73 @@
<!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, "audio-playback", "audio-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.loop = true;
audio.preload = "metadata";
var tests = [
function() {
observerService.addObserver(observer, "audio-playback", false);
ok(true, "Observer set");
runTest();
},
function() {
expectedNotification = 'active';
audio.src = "audio.ogg";
audio.play();
},
function() {
expectedNotification = 'inactive';
audio.pause();
},
function() {
observerService.removeObserver(observer, "audio-playback");
ok(true, "Observer removed");
runTest();
}
];
function runTest() {
if (!tests.length) {
SimpleTest.finish();
return;
}
var test = tests.shift();
test();
}
runTest();
</script>
</body>
</html>

Просмотреть файл

@ -3184,11 +3184,47 @@ void HTMLMediaElement::ProcessMediaFragmentURI()
} }
} }
class MOZ_STACK_CLASS AutoNotifyAudioChannelAgent
{
nsRefPtr<HTMLMediaElement> mElement;
bool mShouldNotify;
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER;
public:
AutoNotifyAudioChannelAgent(HTMLMediaElement* aElement,
bool aNotify
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: mElement(aElement)
, mShouldNotify(aNotify)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
if (mShouldNotify) {
mElement->NotifyAudioChannelAgent(false);
}
}
~AutoNotifyAudioChannelAgent()
{
if (mShouldNotify) {
// The audio channel agent is destroyed at this point.
if (mElement->MaybeCreateAudioChannelAgent()) {
mElement->NotifyAudioChannelAgent(true);
}
}
}
};
void HTMLMediaElement::MetadataLoaded(const MediaInfo* aInfo, void HTMLMediaElement::MetadataLoaded(const MediaInfo* aInfo,
nsAutoPtr<const MetadataTags> aTags) nsAutoPtr<const MetadataTags> aTags)
{ {
MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(NS_IsMainThread());
// If the element is gaining or losing an audio track, we need to notify
// the audio channel agent so that the correct audio-playback events will
// get dispatched.
bool audioTrackChanging = mMediaInfo.HasAudio() != aInfo->HasAudio();
AutoNotifyAudioChannelAgent autoNotify(this,
audioTrackChanging &&
mPlayingThroughTheAudioChannel);
mMediaInfo = *aInfo; mMediaInfo = *aInfo;
mIsEncrypted = aInfo->IsEncrypted() mIsEncrypted = aInfo->IsEncrypted()
#ifdef MOZ_EME #ifdef MOZ_EME
@ -4488,7 +4524,25 @@ nsresult HTMLMediaElement::UpdateChannelMuteState(float aVolume, bool aMuted)
return NS_OK; return NS_OK;
} }
void HTMLMediaElement::UpdateAudioChannelPlayingState() bool
HTMLMediaElement::MaybeCreateAudioChannelAgent()
{
if (!mAudioChannelAgent) {
nsresult rv;
mAudioChannelAgent = do_CreateInstance("@mozilla.org/audiochannelagent;1", &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return false;
}
MOZ_ASSERT(mAudioChannelAgent);
mAudioChannelAgent->InitWithWeakCallback(OwnerDoc()->GetInnerWindow(),
static_cast<int32_t>(mAudioChannel),
this);
}
return true;
}
void
HTMLMediaElement::UpdateAudioChannelPlayingState()
{ {
bool playingThroughTheAudioChannel = bool playingThroughTheAudioChannel =
(!mPaused && (!mPaused &&
@ -4506,18 +4560,9 @@ void HTMLMediaElement::UpdateAudioChannelPlayingState()
return; return;
} }
if (!mAudioChannelAgent) { if (MaybeCreateAudioChannelAgent()) {
nsresult rv; NotifyAudioChannelAgent(mPlayingThroughTheAudioChannel);
mAudioChannelAgent = do_CreateInstance("@mozilla.org/audiochannelagent;1", &rv);
if (!mAudioChannelAgent) {
return;
}
mAudioChannelAgent->InitWithWeakCallback(OwnerDoc()->GetInnerWindow(),
static_cast<int32_t>(mAudioChannel),
this);
} }
NotifyAudioChannelAgent(mPlayingThroughTheAudioChannel);
} }
} }

Просмотреть файл

@ -35,10 +35,6 @@
// Define to output information on decoding and painting framerate // Define to output information on decoding and painting framerate
/* #define DEBUG_FRAME_RATE 1 */ /* #define DEBUG_FRAME_RATE 1 */
class nsIChannel;
class nsIHttpChannel;
class nsILoadGroup;
typedef uint16_t nsMediaNetworkState; typedef uint16_t nsMediaNetworkState;
typedef uint16_t nsMediaReadyState; typedef uint16_t nsMediaReadyState;
@ -56,9 +52,13 @@ class MediaTrack;
} // namespace dom } // namespace dom
} // namespace mozilla } // namespace mozilla
class AutoNotifyAudioChannelAgent;
class nsIChannel;
class nsIHttpChannel;
class nsILoadGroup;
class nsIRunnable;
class nsITimer; class nsITimer;
class nsRange; class nsRange;
class nsIRunnable;
namespace mozilla { namespace mozilla {
namespace dom { namespace dom {
@ -78,6 +78,8 @@ class HTMLMediaElement : public nsGenericHTMLElement,
public MediaDecoderOwner, public MediaDecoderOwner,
public nsIAudioChannelAgentCallback public nsIAudioChannelAgentCallback
{ {
friend class AutoNotifyAudioChannelAgent;
public: public:
typedef mozilla::TimeStamp TimeStamp; typedef mozilla::TimeStamp TimeStamp;
typedef mozilla::layers::ImageContainer ImageContainer; typedef mozilla::layers::ImageContainer ImageContainer;
@ -1050,6 +1052,10 @@ protected:
// Notifies the audio channel agent when the element starts or stops playing. // Notifies the audio channel agent when the element starts or stops playing.
void NotifyAudioChannelAgent(bool aPlaying); void NotifyAudioChannelAgent(bool aPlaying);
// Creates the audio channel agent if needed. Returns true if the audio
// channel agent is ready to be used.
bool MaybeCreateAudioChannelAgent();
class nsAsyncEventRunner; class nsAsyncEventRunner;
using nsGenericHTMLElement::DispatchEvent; using nsGenericHTMLElement::DispatchEvent;
// For nsAsyncEventRunner. // For nsAsyncEventRunner.