diff --git a/dom/media/webaudio/AudioContext.cpp b/dom/media/webaudio/AudioContext.cpp index 1e5c402e175f..4dd0654935a9 100644 --- a/dom/media/webaudio/AudioContext.cpp +++ b/dom/media/webaudio/AudioContext.cpp @@ -36,6 +36,7 @@ #include "mozilla/dom/OscillatorNodeBinding.h" #include "mozilla/dom/PannerNodeBinding.h" #include "mozilla/dom/PeriodicWaveBinding.h" +#include "mozilla/dom/Performance.h" #include "mozilla/dom/Promise.h" #include "mozilla/dom/StereoPannerNodeBinding.h" #include "mozilla/dom/WaveShaperNodeBinding.h" @@ -525,6 +526,28 @@ AudioListener* AudioContext::Listener() { double AudioContext::OutputLatency() { return Graph()->AudioOutputLatency(); } +void AudioContext::GetOutputTimestamp(AudioTimestamp& aTimeStamp) { + if (!Destination()) { + aTimeStamp.mContextTime.Construct(0.0); + aTimeStamp.mPerformanceTime.Construct(0.0); + return; + } + + // The currentTime currently being output is the currentTime minus the audio + // output latency. + aTimeStamp.mContextTime.Construct( + std::max(0.0, CurrentTime() - OutputLatency())); + nsPIDOMWindowInner* parent = GetParentObject(); + Performance* perf = parent ? parent->GetPerformance() : nullptr; + if (perf) { + // Convert to milliseconds. + aTimeStamp.mPerformanceTime.Construct( + std::max(0., perf->Now() - (OutputLatency() * 1000.))); + } else { + aTimeStamp.mPerformanceTime.Construct(0.0); + } +} + Worklet* AudioContext::GetAudioWorklet(ErrorResult& aRv) { if (!mWorklet) { mWorklet = AudioWorkletImpl::CreateWorklet(this, aRv); diff --git a/dom/media/webaudio/AudioContext.h b/dom/media/webaudio/AudioContext.h index 82d98a7fef57..a4858a74841b 100644 --- a/dom/media/webaudio/AudioContext.h +++ b/dom/media/webaudio/AudioContext.h @@ -9,6 +9,7 @@ #include "AudioParamDescriptorMap.h" #include "mozilla/dom/OfflineAudioContextBinding.h" +#include "mozilla/dom/AudioContextBinding.h" #include "MediaBufferDecoder.h" #include "mozilla/Attributes.h" #include "mozilla/DOMEventTargetHelper.h" @@ -200,6 +201,8 @@ class AudioContext final : public DOMEventTargetHelper, double OutputLatency(); + void GetOutputTimestamp(AudioTimestamp& aTimeStamp); + Worklet* GetAudioWorklet(ErrorResult& aRv); bool IsRunning() const; diff --git a/dom/webidl/AudioContext.webidl b/dom/webidl/AudioContext.webidl index 566237a0ac76..08e7984f2ecc 100644 --- a/dom/webidl/AudioContext.webidl +++ b/dom/webidl/AudioContext.webidl @@ -14,13 +14,18 @@ dictionary AudioContextOptions { float sampleRate = 0; }; +dictionary AudioTimestamp { + double contextTime; + DOMHighResTimeStamp performanceTime; +}; + [Pref="dom.webaudio.enabled", Constructor(optional AudioContextOptions contextOptions = {})] interface AudioContext : BaseAudioContext { - // Bug 1324545: AudioTimestamp getOutputTimestamp (); readonly attribute double baseLatency; readonly attribute double outputLatency; + AudioTimestamp getOutputTimestamp(); [Throws] Promise suspend(); diff --git a/testing/web-platform/meta/webaudio/idlharness.https.window.js.ini b/testing/web-platform/meta/webaudio/idlharness.https.window.js.ini index 1b936d7caf6f..d55f475baf1b 100644 --- a/testing/web-platform/meta/webaudio/idlharness.https.window.js.ini +++ b/testing/web-platform/meta/webaudio/idlharness.https.window.js.ini @@ -20,9 +20,6 @@ [AudioParam interface: calling cancelAndHoldAtTime(double) on new AudioBufferSourceNode(context).playbackRate with too few arguments must throw TypeError] expected: FAIL - [AudioContext interface: context must inherit property "getOutputTimestamp()" with the proper type] - expected: FAIL - [AudioListener interface: context.listener must inherit property "forwardX" with the proper type] expected: FAIL @@ -44,9 +41,6 @@ [MediaElementAudioSourceNode interface: new MediaElementAudioSourceNode(context, {mediaElement: new Audio}) must inherit property "mediaElement" with the proper type] expected: FAIL - [AudioContext interface: operation getOutputTimestamp()] - expected: FAIL - [Stringification of new AudioProcessingEvent('', {\n playbackTime: 0, inputBuffer: buffer, outputBuffer: buffer\n })] expected: FAIL diff --git a/testing/web-platform/meta/webaudio/the-audio-api/the-audiocontext-interface/audiocontext-getoutputtimestamp.html.ini b/testing/web-platform/meta/webaudio/the-audio-api/the-audiocontext-interface/audiocontext-getoutputtimestamp.html.ini deleted file mode 100644 index abdb861f9365..000000000000 --- a/testing/web-platform/meta/webaudio/the-audio-api/the-audiocontext-interface/audiocontext-getoutputtimestamp.html.ini +++ /dev/null @@ -1,2 +0,0 @@ -[audiocontext-getoutputtimestamp.html] - expected: ERROR