Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | #ifndef AudioParam_h_ |
michael@0 | 8 | #define AudioParam_h_ |
michael@0 | 9 | |
michael@0 | 10 | #include "AudioParamTimeline.h" |
michael@0 | 11 | #include "nsWrapperCache.h" |
michael@0 | 12 | #include "nsCycleCollectionParticipant.h" |
michael@0 | 13 | #include "nsAutoPtr.h" |
michael@0 | 14 | #include "AudioNode.h" |
michael@0 | 15 | #include "mozilla/dom/TypedArray.h" |
michael@0 | 16 | #include "WebAudioUtils.h" |
michael@0 | 17 | #include "js/TypeDecls.h" |
michael@0 | 18 | |
michael@0 | 19 | namespace mozilla { |
michael@0 | 20 | |
michael@0 | 21 | namespace dom { |
michael@0 | 22 | |
michael@0 | 23 | class AudioParam MOZ_FINAL : public nsWrapperCache, |
michael@0 | 24 | public AudioParamTimeline |
michael@0 | 25 | { |
michael@0 | 26 | public: |
michael@0 | 27 | typedef void (*CallbackType)(AudioNode*); |
michael@0 | 28 | |
michael@0 | 29 | AudioParam(AudioNode* aNode, |
michael@0 | 30 | CallbackType aCallback, |
michael@0 | 31 | float aDefaultValue); |
michael@0 | 32 | virtual ~AudioParam(); |
michael@0 | 33 | |
michael@0 | 34 | NS_IMETHOD_(MozExternalRefCountType) AddRef(void); |
michael@0 | 35 | NS_IMETHOD_(MozExternalRefCountType) Release(void); |
michael@0 | 36 | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(AudioParam) |
michael@0 | 37 | |
michael@0 | 38 | AudioContext* GetParentObject() const |
michael@0 | 39 | { |
michael@0 | 40 | return mNode->Context(); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | double DOMTimeToStreamTime(double aTime) const |
michael@0 | 44 | { |
michael@0 | 45 | return mNode->Context()->DOMTimeToStreamTime(aTime); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
michael@0 | 49 | |
michael@0 | 50 | // We override SetValueCurveAtTime to convert the Float32Array to the wrapper |
michael@0 | 51 | // object. |
michael@0 | 52 | void SetValueCurveAtTime(const Float32Array& aValues, double aStartTime, double aDuration, ErrorResult& aRv) |
michael@0 | 53 | { |
michael@0 | 54 | if (!WebAudioUtils::IsTimeValid(aStartTime)) { |
michael@0 | 55 | aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); |
michael@0 | 56 | return; |
michael@0 | 57 | } |
michael@0 | 58 | aValues.ComputeLengthAndData(); |
michael@0 | 59 | AudioParamTimeline::SetValueCurveAtTime(aValues.Data(), aValues.Length(), |
michael@0 | 60 | DOMTimeToStreamTime(aStartTime), aDuration, aRv); |
michael@0 | 61 | mCallback(mNode); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | // We override the rest of the mutating AudioParamTimeline methods in order to make |
michael@0 | 65 | // sure that the callback is called every time that this object gets mutated. |
michael@0 | 66 | void SetValue(float aValue) |
michael@0 | 67 | { |
michael@0 | 68 | // Optimize away setting the same value on an AudioParam |
michael@0 | 69 | if (HasSimpleValue() && |
michael@0 | 70 | WebAudioUtils::FuzzyEqual(GetValue(), aValue)) { |
michael@0 | 71 | return; |
michael@0 | 72 | } |
michael@0 | 73 | AudioParamTimeline::SetValue(aValue); |
michael@0 | 74 | mCallback(mNode); |
michael@0 | 75 | } |
michael@0 | 76 | void SetValueAtTime(float aValue, double aStartTime, ErrorResult& aRv) |
michael@0 | 77 | { |
michael@0 | 78 | if (!WebAudioUtils::IsTimeValid(aStartTime)) { |
michael@0 | 79 | aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); |
michael@0 | 80 | return; |
michael@0 | 81 | } |
michael@0 | 82 | AudioParamTimeline::SetValueAtTime(aValue, DOMTimeToStreamTime(aStartTime), aRv); |
michael@0 | 83 | mCallback(mNode); |
michael@0 | 84 | } |
michael@0 | 85 | void LinearRampToValueAtTime(float aValue, double aEndTime, ErrorResult& aRv) |
michael@0 | 86 | { |
michael@0 | 87 | if (!WebAudioUtils::IsTimeValid(aEndTime)) { |
michael@0 | 88 | aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); |
michael@0 | 89 | return; |
michael@0 | 90 | } |
michael@0 | 91 | AudioParamTimeline::LinearRampToValueAtTime(aValue, DOMTimeToStreamTime(aEndTime), aRv); |
michael@0 | 92 | mCallback(mNode); |
michael@0 | 93 | } |
michael@0 | 94 | void ExponentialRampToValueAtTime(float aValue, double aEndTime, ErrorResult& aRv) |
michael@0 | 95 | { |
michael@0 | 96 | if (!WebAudioUtils::IsTimeValid(aEndTime)) { |
michael@0 | 97 | aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); |
michael@0 | 98 | return; |
michael@0 | 99 | } |
michael@0 | 100 | AudioParamTimeline::ExponentialRampToValueAtTime(aValue, DOMTimeToStreamTime(aEndTime), aRv); |
michael@0 | 101 | mCallback(mNode); |
michael@0 | 102 | } |
michael@0 | 103 | void SetTargetAtTime(float aTarget, double aStartTime, double aTimeConstant, ErrorResult& aRv) |
michael@0 | 104 | { |
michael@0 | 105 | if (!WebAudioUtils::IsTimeValid(aStartTime) || |
michael@0 | 106 | !WebAudioUtils::IsTimeValid(aTimeConstant)) { |
michael@0 | 107 | aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); |
michael@0 | 108 | return; |
michael@0 | 109 | } |
michael@0 | 110 | AudioParamTimeline::SetTargetAtTime(aTarget, DOMTimeToStreamTime(aStartTime), aTimeConstant, aRv); |
michael@0 | 111 | mCallback(mNode); |
michael@0 | 112 | } |
michael@0 | 113 | void CancelScheduledValues(double aStartTime, ErrorResult& aRv) |
michael@0 | 114 | { |
michael@0 | 115 | if (!WebAudioUtils::IsTimeValid(aStartTime)) { |
michael@0 | 116 | aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); |
michael@0 | 117 | return; |
michael@0 | 118 | } |
michael@0 | 119 | AudioParamTimeline::CancelScheduledValues(DOMTimeToStreamTime(aStartTime)); |
michael@0 | 120 | mCallback(mNode); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | float DefaultValue() const |
michael@0 | 124 | { |
michael@0 | 125 | return mDefaultValue; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | AudioNode* Node() const |
michael@0 | 129 | { |
michael@0 | 130 | return mNode; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | const nsTArray<AudioNode::InputNode>& InputNodes() const |
michael@0 | 134 | { |
michael@0 | 135 | return mInputNodes; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | void RemoveInputNode(uint32_t aIndex) |
michael@0 | 139 | { |
michael@0 | 140 | mInputNodes.RemoveElementAt(aIndex); |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | AudioNode::InputNode* AppendInputNode() |
michael@0 | 144 | { |
michael@0 | 145 | return mInputNodes.AppendElement(); |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | void DisconnectFromGraphAndDestroyStream(); |
michael@0 | 149 | |
michael@0 | 150 | // May create the stream if it doesn't exist |
michael@0 | 151 | MediaStream* Stream(); |
michael@0 | 152 | |
michael@0 | 153 | virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE |
michael@0 | 154 | { |
michael@0 | 155 | size_t amount = AudioParamTimeline::SizeOfExcludingThis(aMallocSizeOf); |
michael@0 | 156 | // Not owned: |
michael@0 | 157 | // - mNode |
michael@0 | 158 | |
michael@0 | 159 | // Just count the array, actual nodes are counted in mNode. |
michael@0 | 160 | amount += mInputNodes.SizeOfExcludingThis(aMallocSizeOf); |
michael@0 | 161 | |
michael@0 | 162 | if (mNodeStreamPort) { |
michael@0 | 163 | amount += mNodeStreamPort->SizeOfIncludingThis(aMallocSizeOf); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | return amount; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE |
michael@0 | 170 | { |
michael@0 | 171 | return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | protected: |
michael@0 | 175 | nsCycleCollectingAutoRefCnt mRefCnt; |
michael@0 | 176 | NS_DECL_OWNINGTHREAD |
michael@0 | 177 | |
michael@0 | 178 | private: |
michael@0 | 179 | nsRefPtr<AudioNode> mNode; |
michael@0 | 180 | // For every InputNode, there is a corresponding entry in mOutputParams of the |
michael@0 | 181 | // InputNode's mInputNode. |
michael@0 | 182 | nsTArray<AudioNode::InputNode> mInputNodes; |
michael@0 | 183 | CallbackType mCallback; |
michael@0 | 184 | const float mDefaultValue; |
michael@0 | 185 | // The input port used to connect the AudioParam's stream to its node's stream |
michael@0 | 186 | nsRefPtr<MediaInputPort> mNodeStreamPort; |
michael@0 | 187 | }; |
michael@0 | 188 | |
michael@0 | 189 | } |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | #endif |
michael@0 | 193 |