content/media/webaudio/AudioParam.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "AudioParam.h"
michael@0 8 #include "mozilla/dom/AudioParamBinding.h"
michael@0 9 #include "AudioNodeEngine.h"
michael@0 10 #include "AudioNodeStream.h"
michael@0 11 #include "AudioContext.h"
michael@0 12
michael@0 13 namespace mozilla {
michael@0 14 namespace dom {
michael@0 15
michael@0 16 NS_IMPL_CYCLE_COLLECTION_CLASS(AudioParam)
michael@0 17
michael@0 18 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AudioParam)
michael@0 19 tmp->DisconnectFromGraphAndDestroyStream();
michael@0 20 NS_IMPL_CYCLE_COLLECTION_UNLINK(mNode)
michael@0 21 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
michael@0 22 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
michael@0 23 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AudioParam)
michael@0 24 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNode)
michael@0 25 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
michael@0 26 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
michael@0 27
michael@0 28 NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(AudioParam)
michael@0 29
michael@0 30 NS_IMPL_CYCLE_COLLECTING_NATIVE_ADDREF(AudioParam)
michael@0 31
michael@0 32 NS_IMETHODIMP_(MozExternalRefCountType)
michael@0 33 AudioParam::Release()
michael@0 34 {
michael@0 35 if (mRefCnt.get() == 1) {
michael@0 36 // We are about to be deleted, disconnect the object from the graph before
michael@0 37 // the derived type is destroyed.
michael@0 38 DisconnectFromGraphAndDestroyStream();
michael@0 39 }
michael@0 40 NS_IMPL_CC_NATIVE_RELEASE_BODY(AudioParam)
michael@0 41 }
michael@0 42
michael@0 43 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(AudioParam, AddRef)
michael@0 44 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(AudioParam, Release)
michael@0 45
michael@0 46 AudioParam::AudioParam(AudioNode* aNode,
michael@0 47 AudioParam::CallbackType aCallback,
michael@0 48 float aDefaultValue)
michael@0 49 : AudioParamTimeline(aDefaultValue)
michael@0 50 , mNode(aNode)
michael@0 51 , mCallback(aCallback)
michael@0 52 , mDefaultValue(aDefaultValue)
michael@0 53 {
michael@0 54 SetIsDOMBinding();
michael@0 55 }
michael@0 56
michael@0 57 AudioParam::~AudioParam()
michael@0 58 {
michael@0 59 MOZ_ASSERT(mInputNodes.IsEmpty());
michael@0 60 }
michael@0 61
michael@0 62 JSObject*
michael@0 63 AudioParam::WrapObject(JSContext* aCx)
michael@0 64 {
michael@0 65 return AudioParamBinding::Wrap(aCx, this);
michael@0 66 }
michael@0 67
michael@0 68 void
michael@0 69 AudioParam::DisconnectFromGraphAndDestroyStream()
michael@0 70 {
michael@0 71 // Addref this temporarily so the refcount bumping below doesn't destroy us
michael@0 72 // prematurely
michael@0 73 nsRefPtr<AudioParam> kungFuDeathGrip = this;
michael@0 74
michael@0 75 while (!mInputNodes.IsEmpty()) {
michael@0 76 uint32_t i = mInputNodes.Length() - 1;
michael@0 77 nsRefPtr<AudioNode> input = mInputNodes[i].mInputNode;
michael@0 78 mInputNodes.RemoveElementAt(i);
michael@0 79 input->RemoveOutputParam(this);
michael@0 80 }
michael@0 81
michael@0 82 if (mNodeStreamPort) {
michael@0 83 mNodeStreamPort->Destroy();
michael@0 84 mNodeStreamPort = nullptr;
michael@0 85 }
michael@0 86
michael@0 87 if (mStream) {
michael@0 88 mStream->Destroy();
michael@0 89 mStream = nullptr;
michael@0 90 }
michael@0 91 }
michael@0 92
michael@0 93 MediaStream*
michael@0 94 AudioParam::Stream()
michael@0 95 {
michael@0 96 if (mStream) {
michael@0 97 return mStream;
michael@0 98 }
michael@0 99
michael@0 100 AudioNodeEngine* engine = new AudioNodeEngine(nullptr);
michael@0 101 nsRefPtr<AudioNodeStream> stream =
michael@0 102 mNode->Context()->Graph()->CreateAudioNodeStream(engine,
michael@0 103 MediaStreamGraph::INTERNAL_STREAM,
michael@0 104 Node()->Context()->SampleRate());
michael@0 105
michael@0 106 // Force the input to have only one channel, and make it down-mix using
michael@0 107 // the speaker rules if needed.
michael@0 108 stream->SetChannelMixingParametersImpl(1, ChannelCountMode::Explicit, ChannelInterpretation::Speakers);
michael@0 109 // Mark as an AudioParam helper stream
michael@0 110 stream->SetAudioParamHelperStream();
michael@0 111
michael@0 112 mStream = stream.forget();
michael@0 113
michael@0 114 // Setup the AudioParam's stream as an input to the owner AudioNode's stream
michael@0 115 MediaStream* nodeStream = mNode->Stream();
michael@0 116 MOZ_ASSERT(nodeStream->AsProcessedStream());
michael@0 117 ProcessedMediaStream* ps = static_cast<ProcessedMediaStream*>(nodeStream);
michael@0 118 mNodeStreamPort = ps->AllocateInputPort(mStream, MediaInputPort::FLAG_BLOCK_INPUT);
michael@0 119
michael@0 120 // Let the MSG's copy of AudioParamTimeline know about the change in the stream
michael@0 121 mCallback(mNode);
michael@0 122
michael@0 123 return mStream;
michael@0 124 }
michael@0 125
michael@0 126 float
michael@0 127 AudioParamTimeline::AudioNodeInputValue(size_t aCounter) const
michael@0 128 {
michael@0 129 MOZ_ASSERT(mStream);
michael@0 130
michael@0 131 // If we have a chunk produced by the AudioNode inputs to the AudioParam,
michael@0 132 // get its value now. We use aCounter to tell us which frame of the last
michael@0 133 // AudioChunk to look at.
michael@0 134 float audioNodeInputValue = 0.0f;
michael@0 135 const AudioChunk& lastAudioNodeChunk =
michael@0 136 static_cast<AudioNodeStream*>(mStream.get())->LastChunks()[0];
michael@0 137 if (!lastAudioNodeChunk.IsNull()) {
michael@0 138 MOZ_ASSERT(lastAudioNodeChunk.GetDuration() == WEBAUDIO_BLOCK_SIZE);
michael@0 139 audioNodeInputValue =
michael@0 140 static_cast<const float*>(lastAudioNodeChunk.mChannelData[0])[aCounter];
michael@0 141 audioNodeInputValue *= lastAudioNodeChunk.mVolume;
michael@0 142 }
michael@0 143
michael@0 144 return audioNodeInputValue;
michael@0 145 }
michael@0 146
michael@0 147 }
michael@0 148 }
michael@0 149

mercurial