2012-09-21 15:42:14 -07:00
|
|
|
/* -*- 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 "AudioBuffer.h"
|
|
|
|
#include "mozilla/dom/AudioBufferBinding.h"
|
|
|
|
#include "jsfriendapi.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
2013-02-04 15:07:25 -08:00
|
|
|
#include "AudioSegment.h"
|
2013-05-05 08:15:58 -07:00
|
|
|
#include "AudioChannelFormat.h"
|
|
|
|
#include "mozilla/PodOperations.h"
|
2013-09-27 17:11:26 -07:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2013-08-15 12:44:14 -07:00
|
|
|
#include "AudioNodeEngine.h"
|
2012-09-21 15:42:14 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(AudioBuffer)
|
|
|
|
|
2012-09-21 15:42:14 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AudioBuffer)
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mContext)
|
2013-02-04 15:07:25 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mJSChannels)
|
2012-09-21 15:42:14 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2013-01-25 13:21:22 -08:00
|
|
|
tmp->ClearJSChannels();
|
2012-09-21 15:42:14 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AudioBuffer)
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContext)
|
2012-09-21 15:42:14 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(AudioBuffer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
2013-02-04 15:07:25 -08:00
|
|
|
for (uint32_t i = 0; i < tmp->mJSChannels.Length(); ++i) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mJSChannels[i])
|
2012-09-21 15:42:14 -07:00
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2013-07-05 13:21:52 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(AudioBuffer, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(AudioBuffer, Release)
|
2012-09-21 15:42:14 -07:00
|
|
|
|
|
|
|
AudioBuffer::AudioBuffer(AudioContext* aContext, uint32_t aLength,
|
2013-02-04 15:07:25 -08:00
|
|
|
float aSampleRate)
|
2012-09-21 15:42:14 -07:00
|
|
|
: mContext(aContext),
|
|
|
|
mLength(aLength),
|
|
|
|
mSampleRate(aSampleRate)
|
|
|
|
{
|
|
|
|
SetIsDOMBinding();
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::HoldJSObjects(this);
|
2012-09-21 15:42:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioBuffer::~AudioBuffer()
|
2013-01-25 13:21:22 -08:00
|
|
|
{
|
|
|
|
ClearJSChannels();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioBuffer::ClearJSChannels()
|
2012-09-21 15:42:14 -07:00
|
|
|
{
|
2013-02-04 15:07:25 -08:00
|
|
|
mJSChannels.Clear();
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::DropJSObjects(this);
|
2012-09-21 15:42:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AudioBuffer::InitializeBuffers(uint32_t aNumberOfChannels, JSContext* aJSContext)
|
|
|
|
{
|
2013-02-04 15:07:25 -08:00
|
|
|
if (!mJSChannels.SetCapacity(aNumberOfChannels)) {
|
2012-09-21 15:42:14 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < aNumberOfChannels; ++i) {
|
2013-11-11 00:04:41 -08:00
|
|
|
JS::Rooted<JSObject*> array(aJSContext,
|
|
|
|
JS_NewFloat32Array(aJSContext, mLength));
|
2012-09-21 15:42:14 -07:00
|
|
|
if (!array) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-18 03:00:38 -07:00
|
|
|
mJSChannels.AppendElement(array.get());
|
2012-09-21 15:42:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
AudioBuffer::WrapObject(JSContext* aCx)
|
2012-09-21 15:42:14 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return AudioBufferBinding::Wrap(aCx, this);
|
2012-09-21 15:42:14 -07:00
|
|
|
}
|
|
|
|
|
2013-05-04 14:40:20 -07:00
|
|
|
bool
|
2013-02-04 15:07:25 -08:00
|
|
|
AudioBuffer::RestoreJSChannelData(JSContext* aJSContext)
|
|
|
|
{
|
|
|
|
if (mSharedChannels) {
|
|
|
|
for (uint32_t i = 0; i < mJSChannels.Length(); ++i) {
|
|
|
|
const float* data = mSharedChannels->GetData(i);
|
|
|
|
// The following code first zeroes the array and then copies our data
|
|
|
|
// into it. We could avoid this with additional JS APIs to construct
|
|
|
|
// an array (or ArrayBuffer) containing initial data.
|
2013-11-11 00:04:41 -08:00
|
|
|
JS::Rooted<JSObject*> array(aJSContext,
|
|
|
|
JS_NewFloat32Array(aJSContext, mLength));
|
2013-05-04 14:40:20 -07:00
|
|
|
if (!array) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-04 15:07:25 -08:00
|
|
|
memcpy(JS_GetFloat32ArrayData(array), data, sizeof(float)*mLength);
|
|
|
|
mJSChannels[i] = array;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSharedChannels = nullptr;
|
|
|
|
}
|
2013-05-04 14:40:20 -07:00
|
|
|
|
|
|
|
return true;
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
|
|
|
|
2013-09-17 04:30:32 -07:00
|
|
|
void
|
|
|
|
AudioBuffer::CopyFromChannel(const Float32Array& aDestination, uint32_t aChannelNumber,
|
|
|
|
uint32_t aStartInChannel, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
uint32_t length = aDestination.Length();
|
2013-09-27 17:11:26 -07:00
|
|
|
CheckedInt<uint32_t> end = aStartInChannel;
|
|
|
|
end += length;
|
2013-09-17 04:30:32 -07:00
|
|
|
if (aChannelNumber >= NumberOfChannels() ||
|
2013-10-08 19:32:16 -07:00
|
|
|
!end.isValid() || end.value() > mLength) {
|
2013-09-17 04:30:32 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mSharedChannels && JS_GetTypedArrayLength(mJSChannels[aChannelNumber]) != mLength) {
|
|
|
|
// The array was probably neutered
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const float* sourceData = mSharedChannels ?
|
|
|
|
mSharedChannels->GetData(aChannelNumber) :
|
|
|
|
JS_GetFloat32ArrayData(mJSChannels[aChannelNumber]);
|
2013-11-04 16:02:55 -08:00
|
|
|
PodMove(aDestination.Data(), sourceData + aStartInChannel, length);
|
2013-09-17 04:30:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioBuffer::CopyToChannel(JSContext* aJSContext, const Float32Array& aSource,
|
|
|
|
uint32_t aChannelNumber, uint32_t aStartInChannel,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
uint32_t length = aSource.Length();
|
2013-09-27 17:11:26 -07:00
|
|
|
CheckedInt<uint32_t> end = aStartInChannel;
|
|
|
|
end += length;
|
2013-09-17 04:30:32 -07:00
|
|
|
if (aChannelNumber >= NumberOfChannels() ||
|
2013-10-08 19:32:16 -07:00
|
|
|
!end.isValid() || end.value() > mLength) {
|
2013-09-17 04:30:32 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mSharedChannels && JS_GetTypedArrayLength(mJSChannels[aChannelNumber]) != mLength) {
|
|
|
|
// The array was probably neutered
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!RestoreJSChannelData(aJSContext)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-04 16:02:55 -08:00
|
|
|
PodMove(JS_GetFloat32ArrayData(mJSChannels[aChannelNumber]) + aStartInChannel,
|
2013-09-17 04:30:32 -07:00
|
|
|
aSource.Data(), length);
|
|
|
|
}
|
|
|
|
|
2013-04-13 18:37:04 -07:00
|
|
|
void
|
2014-05-09 02:16:03 -07:00
|
|
|
AudioBuffer::SetRawChannelContents(uint32_t aChannel, float* aContents)
|
2013-04-13 18:37:04 -07:00
|
|
|
{
|
2013-05-05 08:15:58 -07:00
|
|
|
PodCopy(JS_GetFloat32ArrayData(mJSChannels[aChannel]), aContents, mLength);
|
2013-04-13 18:37:04 -07:00
|
|
|
}
|
|
|
|
|
2012-09-21 15:42:14 -07:00
|
|
|
JSObject*
|
|
|
|
AudioBuffer::GetChannelData(JSContext* aJSContext, uint32_t aChannel,
|
2013-02-04 15:07:25 -08:00
|
|
|
ErrorResult& aRv)
|
2012-09-21 15:42:14 -07:00
|
|
|
{
|
2013-02-04 15:07:25 -08:00
|
|
|
if (aChannel >= NumberOfChannels()) {
|
2012-09-21 15:42:14 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-02-04 15:07:25 -08:00
|
|
|
|
2013-05-04 14:40:20 -07:00
|
|
|
if (!RestoreJSChannelData(aJSContext)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-02-04 15:07:25 -08:00
|
|
|
|
|
|
|
return mJSChannels[aChannel];
|
2013-02-01 14:13:23 -08:00
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
static already_AddRefed<ThreadSharedFloatArrayBufferList>
|
|
|
|
StealJSArrayDataIntoThreadSharedFloatArrayBufferList(JSContext* aJSContext,
|
|
|
|
const nsTArray<JSObject*>& aJSArrays)
|
|
|
|
{
|
|
|
|
nsRefPtr<ThreadSharedFloatArrayBufferList> result =
|
|
|
|
new ThreadSharedFloatArrayBufferList(aJSArrays.Length());
|
|
|
|
for (uint32_t i = 0; i < aJSArrays.Length(); ++i) {
|
2014-04-30 02:10:33 -07:00
|
|
|
JS::Rooted<JSObject*> arrayBufferView(aJSContext, aJSArrays[i]);
|
2013-11-11 00:04:41 -08:00
|
|
|
JS::Rooted<JSObject*> arrayBuffer(aJSContext,
|
2014-04-30 02:10:33 -07:00
|
|
|
JS_GetArrayBufferViewBuffer(aJSContext, arrayBufferView));
|
2014-03-17 10:46:04 -07:00
|
|
|
uint8_t* stolenData = arrayBuffer
|
|
|
|
? (uint8_t*) JS_StealArrayBufferContents(aJSContext, arrayBuffer)
|
|
|
|
: nullptr;
|
|
|
|
if (stolenData) {
|
|
|
|
result->SetData(i, stolenData, reinterpret_cast<float*>(stolenData));
|
2013-02-04 15:07:25 -08:00
|
|
|
} else {
|
2013-06-29 05:30:35 -07:00
|
|
|
return nullptr;
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result.forget();
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
2013-02-04 22:29:28 -08:00
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
ThreadSharedFloatArrayBufferList*
|
2013-04-22 05:12:45 -07:00
|
|
|
AudioBuffer::GetThreadSharedChannelsForRate(JSContext* aJSContext)
|
2013-02-04 15:07:25 -08:00
|
|
|
{
|
|
|
|
if (!mSharedChannels) {
|
2013-06-25 09:01:07 -07:00
|
|
|
for (uint32_t i = 0; i < mJSChannels.Length(); ++i) {
|
|
|
|
if (mLength != JS_GetTypedArrayLength(mJSChannels[i])) {
|
|
|
|
// Probably one of the arrays was neutered
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
mSharedChannels =
|
|
|
|
StealJSArrayDataIntoThreadSharedFloatArrayBufferList(aJSContext, mJSChannels);
|
|
|
|
}
|
|
|
|
|
2013-03-18 17:54:32 -07:00
|
|
|
return mSharedChannels;
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
2013-03-07 15:14:46 -08:00
|
|
|
|
2014-01-04 10:15:41 -08:00
|
|
|
size_t
|
|
|
|
AudioBuffer::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = aMallocSizeOf(this);
|
|
|
|
amount += mJSChannels.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
if (mSharedChannels) {
|
2014-05-01 10:37:54 -07:00
|
|
|
amount += mSharedChannels->SizeOfIncludingThis(aMallocSizeOf);
|
2014-01-04 10:15:41 -08:00
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
|
|
|
}
|