content/media/webaudio/AudioBuffer.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 #include "AudioBuffer.h"
michael@0 8 #include "mozilla/dom/AudioBufferBinding.h"
michael@0 9 #include "jsfriendapi.h"
michael@0 10 #include "mozilla/ErrorResult.h"
michael@0 11 #include "AudioSegment.h"
michael@0 12 #include "AudioChannelFormat.h"
michael@0 13 #include "mozilla/PodOperations.h"
michael@0 14 #include "mozilla/CheckedInt.h"
michael@0 15 #include "AudioNodeEngine.h"
michael@0 16
michael@0 17 namespace mozilla {
michael@0 18 namespace dom {
michael@0 19
michael@0 20 NS_IMPL_CYCLE_COLLECTION_CLASS(AudioBuffer)
michael@0 21
michael@0 22 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AudioBuffer)
michael@0 23 NS_IMPL_CYCLE_COLLECTION_UNLINK(mContext)
michael@0 24 NS_IMPL_CYCLE_COLLECTION_UNLINK(mJSChannels)
michael@0 25 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
michael@0 26 tmp->ClearJSChannels();
michael@0 27 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
michael@0 28
michael@0 29 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AudioBuffer)
michael@0 30 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContext)
michael@0 31 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
michael@0 32 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
michael@0 33
michael@0 34 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(AudioBuffer)
michael@0 35 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
michael@0 36 for (uint32_t i = 0; i < tmp->mJSChannels.Length(); ++i) {
michael@0 37 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mJSChannels[i])
michael@0 38 }
michael@0 39 NS_IMPL_CYCLE_COLLECTION_TRACE_END
michael@0 40
michael@0 41 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(AudioBuffer, AddRef)
michael@0 42 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(AudioBuffer, Release)
michael@0 43
michael@0 44 AudioBuffer::AudioBuffer(AudioContext* aContext, uint32_t aNumberOfChannels,
michael@0 45 uint32_t aLength, float aSampleRate)
michael@0 46 : mContext(aContext),
michael@0 47 mLength(aLength),
michael@0 48 mSampleRate(aSampleRate)
michael@0 49 {
michael@0 50 mJSChannels.SetCapacity(aNumberOfChannels);
michael@0 51 SetIsDOMBinding();
michael@0 52 mozilla::HoldJSObjects(this);
michael@0 53 }
michael@0 54
michael@0 55 AudioBuffer::~AudioBuffer()
michael@0 56 {
michael@0 57 ClearJSChannels();
michael@0 58 }
michael@0 59
michael@0 60 void
michael@0 61 AudioBuffer::ClearJSChannels()
michael@0 62 {
michael@0 63 mJSChannels.Clear();
michael@0 64 mozilla::DropJSObjects(this);
michael@0 65 }
michael@0 66
michael@0 67 /* static */ already_AddRefed<AudioBuffer>
michael@0 68 AudioBuffer::Create(AudioContext* aContext, uint32_t aNumberOfChannels,
michael@0 69 uint32_t aLength, float aSampleRate,
michael@0 70 JSContext* aJSContext, ErrorResult& aRv)
michael@0 71 {
michael@0 72 // Note that a buffer with zero channels is permitted here for the sake of
michael@0 73 // AudioProcessingEvent, where channel counts must match parameters passed
michael@0 74 // to createScriptProcessor(), one of which may be zero.
michael@0 75 if (aSampleRate < WebAudioUtils::MinSampleRate ||
michael@0 76 aSampleRate > WebAudioUtils::MaxSampleRate ||
michael@0 77 aNumberOfChannels > WebAudioUtils::MaxChannelCount ||
michael@0 78 !aLength || aLength > INT32_MAX) {
michael@0 79 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
michael@0 80 return nullptr;
michael@0 81 }
michael@0 82
michael@0 83 nsRefPtr<AudioBuffer> buffer =
michael@0 84 new AudioBuffer(aContext, aNumberOfChannels, aLength, aSampleRate);
michael@0 85
michael@0 86 for (uint32_t i = 0; i < aNumberOfChannels; ++i) {
michael@0 87 JS::Rooted<JSObject*> array(aJSContext,
michael@0 88 JS_NewFloat32Array(aJSContext, aLength));
michael@0 89 if (!array) {
michael@0 90 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
michael@0 91 return nullptr;
michael@0 92 }
michael@0 93 buffer->mJSChannels.AppendElement(array.get());
michael@0 94 }
michael@0 95
michael@0 96 return buffer.forget();
michael@0 97 }
michael@0 98
michael@0 99 JSObject*
michael@0 100 AudioBuffer::WrapObject(JSContext* aCx)
michael@0 101 {
michael@0 102 return AudioBufferBinding::Wrap(aCx, this);
michael@0 103 }
michael@0 104
michael@0 105 bool
michael@0 106 AudioBuffer::RestoreJSChannelData(JSContext* aJSContext)
michael@0 107 {
michael@0 108 if (mSharedChannels) {
michael@0 109 for (uint32_t i = 0; i < mJSChannels.Length(); ++i) {
michael@0 110 const float* data = mSharedChannels->GetData(i);
michael@0 111 // The following code first zeroes the array and then copies our data
michael@0 112 // into it. We could avoid this with additional JS APIs to construct
michael@0 113 // an array (or ArrayBuffer) containing initial data.
michael@0 114 JS::Rooted<JSObject*> array(aJSContext,
michael@0 115 JS_NewFloat32Array(aJSContext, mLength));
michael@0 116 if (!array) {
michael@0 117 return false;
michael@0 118 }
michael@0 119 memcpy(JS_GetFloat32ArrayData(array), data, sizeof(float)*mLength);
michael@0 120 mJSChannels[i] = array;
michael@0 121 }
michael@0 122
michael@0 123 mSharedChannels = nullptr;
michael@0 124 }
michael@0 125
michael@0 126 return true;
michael@0 127 }
michael@0 128
michael@0 129 void
michael@0 130 AudioBuffer::CopyFromChannel(const Float32Array& aDestination, uint32_t aChannelNumber,
michael@0 131 uint32_t aStartInChannel, ErrorResult& aRv)
michael@0 132 {
michael@0 133 aDestination.ComputeLengthAndData();
michael@0 134
michael@0 135 uint32_t length = aDestination.Length();
michael@0 136 CheckedInt<uint32_t> end = aStartInChannel;
michael@0 137 end += length;
michael@0 138 if (aChannelNumber >= NumberOfChannels() ||
michael@0 139 !end.isValid() || end.value() > mLength) {
michael@0 140 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
michael@0 141 return;
michael@0 142 }
michael@0 143
michael@0 144 if (!mSharedChannels && JS_GetTypedArrayLength(mJSChannels[aChannelNumber]) != mLength) {
michael@0 145 // The array was probably neutered
michael@0 146 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
michael@0 147 return;
michael@0 148 }
michael@0 149
michael@0 150 const float* sourceData = mSharedChannels ?
michael@0 151 mSharedChannels->GetData(aChannelNumber) :
michael@0 152 JS_GetFloat32ArrayData(mJSChannels[aChannelNumber]);
michael@0 153 PodMove(aDestination.Data(), sourceData + aStartInChannel, length);
michael@0 154 }
michael@0 155
michael@0 156 void
michael@0 157 AudioBuffer::CopyToChannel(JSContext* aJSContext, const Float32Array& aSource,
michael@0 158 uint32_t aChannelNumber, uint32_t aStartInChannel,
michael@0 159 ErrorResult& aRv)
michael@0 160 {
michael@0 161 aSource.ComputeLengthAndData();
michael@0 162
michael@0 163 uint32_t length = aSource.Length();
michael@0 164 CheckedInt<uint32_t> end = aStartInChannel;
michael@0 165 end += length;
michael@0 166 if (aChannelNumber >= NumberOfChannels() ||
michael@0 167 !end.isValid() || end.value() > mLength) {
michael@0 168 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
michael@0 169 return;
michael@0 170 }
michael@0 171
michael@0 172 if (!mSharedChannels && JS_GetTypedArrayLength(mJSChannels[aChannelNumber]) != mLength) {
michael@0 173 // The array was probably neutered
michael@0 174 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
michael@0 175 return;
michael@0 176 }
michael@0 177
michael@0 178 if (!RestoreJSChannelData(aJSContext)) {
michael@0 179 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
michael@0 180 return;
michael@0 181 }
michael@0 182
michael@0 183 PodMove(JS_GetFloat32ArrayData(mJSChannels[aChannelNumber]) + aStartInChannel,
michael@0 184 aSource.Data(), length);
michael@0 185 }
michael@0 186
michael@0 187 void
michael@0 188 AudioBuffer::SetRawChannelContents(JSContext* aJSContext, uint32_t aChannel,
michael@0 189 float* aContents)
michael@0 190 {
michael@0 191 PodCopy(JS_GetFloat32ArrayData(mJSChannels[aChannel]), aContents, mLength);
michael@0 192 }
michael@0 193
michael@0 194 void
michael@0 195 AudioBuffer::GetChannelData(JSContext* aJSContext, uint32_t aChannel,
michael@0 196 JS::MutableHandle<JSObject*> aRetval,
michael@0 197 ErrorResult& aRv)
michael@0 198 {
michael@0 199 if (aChannel >= NumberOfChannels()) {
michael@0 200 aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
michael@0 201 return;
michael@0 202 }
michael@0 203
michael@0 204 if (!RestoreJSChannelData(aJSContext)) {
michael@0 205 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
michael@0 206 return;
michael@0 207 }
michael@0 208
michael@0 209 if (mJSChannels[aChannel]) {
michael@0 210 JS::ExposeObjectToActiveJS(mJSChannels[aChannel]);
michael@0 211 }
michael@0 212 aRetval.set(mJSChannels[aChannel]);
michael@0 213 }
michael@0 214
michael@0 215 static already_AddRefed<ThreadSharedFloatArrayBufferList>
michael@0 216 StealJSArrayDataIntoThreadSharedFloatArrayBufferList(JSContext* aJSContext,
michael@0 217 const nsTArray<JSObject*>& aJSArrays)
michael@0 218 {
michael@0 219 nsRefPtr<ThreadSharedFloatArrayBufferList> result =
michael@0 220 new ThreadSharedFloatArrayBufferList(aJSArrays.Length());
michael@0 221 for (uint32_t i = 0; i < aJSArrays.Length(); ++i) {
michael@0 222 JS::Rooted<JSObject*> arrayBuffer(aJSContext,
michael@0 223 JS_GetArrayBufferViewBuffer(aJSContext, aJSArrays[i]));
michael@0 224 uint8_t* stolenData = arrayBuffer
michael@0 225 ? (uint8_t*) JS_StealArrayBufferContents(aJSContext, arrayBuffer)
michael@0 226 : nullptr;
michael@0 227 if (stolenData) {
michael@0 228 result->SetData(i, stolenData, reinterpret_cast<float*>(stolenData));
michael@0 229 } else {
michael@0 230 return nullptr;
michael@0 231 }
michael@0 232 }
michael@0 233 return result.forget();
michael@0 234 }
michael@0 235
michael@0 236 ThreadSharedFloatArrayBufferList*
michael@0 237 AudioBuffer::GetThreadSharedChannelsForRate(JSContext* aJSContext)
michael@0 238 {
michael@0 239 if (!mSharedChannels) {
michael@0 240 for (uint32_t i = 0; i < mJSChannels.Length(); ++i) {
michael@0 241 if (mLength != JS_GetTypedArrayLength(mJSChannels[i])) {
michael@0 242 // Probably one of the arrays was neutered
michael@0 243 return nullptr;
michael@0 244 }
michael@0 245 }
michael@0 246
michael@0 247 mSharedChannels =
michael@0 248 StealJSArrayDataIntoThreadSharedFloatArrayBufferList(aJSContext, mJSChannels);
michael@0 249 }
michael@0 250
michael@0 251 return mSharedChannels;
michael@0 252 }
michael@0 253
michael@0 254 size_t
michael@0 255 AudioBuffer::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
michael@0 256 {
michael@0 257 size_t amount = aMallocSizeOf(this);
michael@0 258 amount += mJSChannels.SizeOfExcludingThis(aMallocSizeOf);
michael@0 259 if (mSharedChannels) {
michael@0 260 amount += mSharedChannels->SizeOfExcludingThis(aMallocSizeOf);
michael@0 261 }
michael@0 262 return amount;
michael@0 263 }
michael@0 264
michael@0 265 }
michael@0 266 }

mercurial