Bug 855568 - Implement MediaElementAudioSourceNode

X-Git-Commit-ID: 52f1fcdd561e8214e6820a3f2478d9e8b9623430

--HG--
extra : rebase_source : c40cd0c7070dec9e0273093499228f13906f8fc5
This commit is contained in:
Ehsan Akhgari 2013-07-25 15:01:49 +12:00
parent 306005fb0f
commit d6eeb2dccd
10 changed files with 174 additions and 2 deletions

View File

@ -8,14 +8,16 @@
#include "nsContentUtils.h"
#include "nsPIDOMWindow.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/AnalyserNode.h"
#include "mozilla/dom/AudioContextBinding.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "mozilla/dom/OfflineAudioContextBinding.h"
#include "MediaStreamGraph.h"
#include "mozilla/dom/AnalyserNode.h"
#include "AudioDestinationNode.h"
#include "AudioBufferSourceNode.h"
#include "AudioBuffer.h"
#include "GainNode.h"
#include "MediaElementAudioSourceNode.h"
#include "MediaStreamAudioSourceNode.h"
#include "DelayNode.h"
#include "PannerNode.h"
@ -254,6 +256,23 @@ AudioContext::CreateAnalyser()
return analyserNode.forget();
}
already_AddRefed<MediaElementAudioSourceNode>
AudioContext::CreateMediaElementSource(HTMLMediaElement& aMediaElement,
ErrorResult& aRv)
{
if (mIsOffline) {
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
return nullptr;
}
nsRefPtr<DOMMediaStream> stream = aMediaElement.MozCaptureStream(aRv);
if (aRv.Failed()) {
return nullptr;
}
nsRefPtr<MediaElementAudioSourceNode> mediaElementAudioSourceNode =
new MediaElementAudioSourceNode(this, stream);
return mediaElementAudioSourceNode.forget();
}
already_AddRefed<MediaStreamAudioSourceNode>
AudioContext::CreateMediaStreamSource(DOMMediaStream& aMediaStream,
ErrorResult& aRv)
@ -262,7 +281,8 @@ AudioContext::CreateMediaStreamSource(DOMMediaStream& aMediaStream,
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
return nullptr;
}
nsRefPtr<MediaStreamAudioSourceNode> mediaStreamAudioSourceNode = new MediaStreamAudioSourceNode(this, &aMediaStream);
nsRefPtr<MediaStreamAudioSourceNode> mediaStreamAudioSourceNode =
new MediaStreamAudioSourceNode(this, &aMediaStream);
return mediaStreamAudioSourceNode.forget();
}

View File

@ -52,6 +52,8 @@ class DelayNode;
class DynamicsCompressorNode;
class GainNode;
class GlobalObject;
class HTMLMediaElement;
class MediaElementAudioSourceNode;
class MediaStreamAudioDestinationNode;
class MediaStreamAudioSourceNode;
class OfflineRenderSuccessCallback;
@ -162,6 +164,8 @@ public:
return CreateGain();
}
already_AddRefed<MediaElementAudioSourceNode>
CreateMediaElementSource(HTMLMediaElement& aMediaElement, ErrorResult& aRv);
already_AddRefed<MediaStreamAudioSourceNode>
CreateMediaStreamSource(DOMMediaStream& aMediaStream, ErrorResult& aRv);

View File

@ -0,0 +1,27 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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 "MediaElementAudioSourceNode.h"
#include "mozilla/dom/MediaElementAudioSourceNodeBinding.h"
#include "mozilla/dom/HTMLMediaElement.h"
namespace mozilla {
namespace dom {
MediaElementAudioSourceNode::MediaElementAudioSourceNode(AudioContext* aContext,
DOMMediaStream* aStream)
: MediaStreamAudioSourceNode(aContext, aStream)
{
}
JSObject*
MediaElementAudioSourceNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
{
return MediaElementAudioSourceNodeBinding::Wrap(aCx, aScope, this);
}
}
}

View File

@ -0,0 +1,29 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/. */
#ifndef MediaElementAudioSourceNode_h_
#define MediaElementAudioSourceNode_h_
#include "MediaStreamAudioSourceNode.h"
namespace mozilla {
namespace dom {
class HTMLMediaElement;
class MediaElementAudioSourceNode : public MediaStreamAudioSourceNode
{
public:
MediaElementAudioSourceNode(AudioContext* aContext,
DOMMediaStream* aStream);
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
};
}
}
#endif

View File

@ -39,6 +39,7 @@ EXPORTS.mozilla.dom += [
'DynamicsCompressorNode.h',
'EnableWebAudioCheck.h',
'GainNode.h',
'MediaElementAudioSourceNode.h',
'MediaStreamAudioDestinationNode.h',
'MediaStreamAudioSourceNode.h',
'OfflineAudioCompletionEvent.h',
@ -67,6 +68,7 @@ CPP_SOURCES += [
'EnableWebAudioCheck.cpp',
'GainNode.cpp',
'MediaBufferDecoder.cpp',
'MediaElementAudioSourceNode.cpp',
'MediaStreamAudioDestinationNode.cpp',
'MediaStreamAudioSourceNode.cpp',
'OfflineAudioCompletionEvent.cpp',

View File

@ -67,6 +67,7 @@ MOCHITEST_FILES := \
test_gainNodeInLoop.html \
test_maxChannelCount.html \
test_mediaDecoding.html \
test_mediaElementAudioSourceNode.html \
test_mediaStreamAudioDestinationNode.html \
test_mediaStreamAudioSourceNode.html \
test_mediaStreamAudioSourceNodeCrossOrigin.html \

View File

@ -0,0 +1,69 @@
<!DOCTYPE HTML>
<html>
<meta charset="utf-8">
<head>
<title>Test MediaElementAudioSourceNode</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
var audio = new Audio("small-shot.ogg");
var context = new AudioContext();
var node = context.createMediaElementSource(audio);
var sp = context.createScriptProcessor(2048, 1);
node.connect(sp);
var expectedMinNonzeroSampleCount;
var expectedMaxNonzeroSampleCount;
var nonzeroSampleCount = 0;
var complete = false;
var iterationCount = 0;
// This test ensures we receive at least expectedSampleCount nonzero samples
function processSamples(e) {
if (complete) {
return;
}
if (iterationCount == 0) {
// Don't start playing the audio until the AudioContext stuff is connected
// and running.
audio.play();
}
++iterationCount;
var buf = e.inputBuffer.getChannelData(0);
var nonzeroSamplesThisBuffer = 0;
for (var i = 0; i < buf.length; ++i) {
if (buf[i] != 0) {
++nonzeroSamplesThisBuffer;
}
}
nonzeroSampleCount += nonzeroSamplesThisBuffer;
is(e.inputBuffer.numberOfChannels, 1,
"Checking data channel count (nonzeroSamplesThisBuffer=" +
nonzeroSamplesThisBuffer + ")");
ok(nonzeroSampleCount <= expectedMaxNonzeroSampleCount,
"Too many nonzero samples (got " + nonzeroSampleCount + ", expected max " + expectedMaxNonzeroSampleCount + ")");
if (nonzeroSampleCount >= expectedMinNonzeroSampleCount &&
nonzeroSamplesThisBuffer == 0) {
ok(true,
"Check received enough nonzero samples (got " + nonzeroSampleCount + ", expected min " + expectedMinNonzeroSampleCount + ")");
SimpleTest.finish();
complete = true;
}
}
audio.oncanplaythrough = function() {
// Use a fuzz factor of 100 to account for samples that just happen to be zero
expectedMinNonzeroSampleCount = Math.floor(audio.duration*context.sampleRate) - 100;
expectedMaxNonzeroSampleCount = Math.floor(audio.duration*context.sampleRate) + 500;
sp.onaudioprocess = processSamples;
};
</script>
</pre>
</body>
</html>

View File

@ -43,6 +43,8 @@ interface AudioContext : EventTarget {
[Creator]
AnalyserNode createAnalyser();
[Creator, Throws]
MediaElementAudioSourceNode createMediaElementSource(HTMLMediaElement mediaElement);
[Creator, Throws]
MediaStreamAudioSourceNode createMediaStreamSource(MediaStream mediaStream);
[Creator]
GainNode createGain();

View File

@ -0,0 +1,17 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/.
*
* The origin of this IDL file is
* https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html
*
* Copyright © 2012 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C
* liability, trademark and document use rules apply.
*/
[PrefControlled]
interface MediaElementAudioSourceNode : AudioNode {
};

View File

@ -183,6 +183,7 @@ webidl_files = \
LinkStyle.webidl \
LocalMediaStream.webidl \
Location.webidl \
MediaElementAudioSourceNode.webidl \
MediaError.webidl \
MediaRecorder.webidl \
MediaSource.webidl \