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 "nsContentUtils.h"
|
|
|
|
#include "AudioContext.h"
|
|
|
|
#include "jsfriendapi.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
2013-02-04 15:07:25 -08:00
|
|
|
#include "AudioSegment.h"
|
2013-03-08 08:49:56 -08:00
|
|
|
#include "nsIScriptError.h"
|
2013-03-17 00:55:15 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
2012-09-21 15:42:14 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(AudioBuffer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(AudioBuffer)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AudioBuffer)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
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();
|
2012-11-05 10:49:35 -08:00
|
|
|
|
|
|
|
NS_HOLD_JS_OBJECTS(this, AudioBuffer);
|
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();
|
2012-11-05 10:49:35 -08:00
|
|
|
NS_DROP_JS_OBJECTS(this, AudioBuffer);
|
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) {
|
|
|
|
JSObject* array = JS_NewFloat32Array(aJSContext, mLength);
|
|
|
|
if (!array) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-04 15:07:25 -08:00
|
|
|
mJSChannels.AppendElement(array);
|
2012-09-21 15:42:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2013-03-11 16:03:47 -07:00
|
|
|
AudioBuffer::WrapObject(JSContext* aCx, JSObject* aScope)
|
2012-09-21 15:42:14 -07:00
|
|
|
{
|
2013-03-11 16:03:47 -07:00
|
|
|
return AudioBufferBinding::Wrap(aCx, aScope, this);
|
2012-09-21 15:42:14 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
void
|
|
|
|
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.
|
|
|
|
JSObject* array = JS_NewFloat32Array(aJSContext, mLength);
|
|
|
|
memcpy(JS_GetFloat32ArrayData(array), data, sizeof(float)*mLength);
|
|
|
|
mJSChannels[i] = array;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSharedChannels = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
RestoreJSChannelData(aJSContext);
|
|
|
|
|
|
|
|
return mJSChannels[aChannel];
|
2013-02-01 14:13:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioBuffer::SetChannelDataFromArrayBufferContents(JSContext* aJSContext,
|
|
|
|
uint32_t aChannel,
|
|
|
|
void* aContents)
|
|
|
|
{
|
2013-02-04 15:07:25 -08:00
|
|
|
RestoreJSChannelData(aJSContext);
|
|
|
|
|
|
|
|
MOZ_ASSERT(aChannel < NumberOfChannels());
|
2013-02-01 14:13:23 -08:00
|
|
|
JSObject* arrayBuffer = JS_NewArrayBufferWithContents(aJSContext, aContents);
|
2013-02-04 15:07:25 -08:00
|
|
|
mJSChannels[aChannel] = JS_NewFloat32ArrayWithBuffer(aJSContext, arrayBuffer,
|
|
|
|
0, -1);
|
|
|
|
MOZ_ASSERT(mLength == JS_GetTypedArrayLength(mJSChannels[aChannel]));
|
2012-09-21 15:42:14 -07: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) {
|
|
|
|
JSObject* arrayBuffer = JS_GetArrayBufferViewBuffer(aJSArrays[i]);
|
|
|
|
void* dataToFree = nullptr;
|
|
|
|
uint8_t* stolenData = nullptr;
|
|
|
|
if (arrayBuffer &&
|
|
|
|
JS_StealArrayBufferContents(aJSContext, arrayBuffer, &dataToFree,
|
|
|
|
&stolenData)) {
|
|
|
|
result->SetData(i, dataToFree, reinterpret_cast<float*>(stolenData));
|
|
|
|
} else {
|
|
|
|
result->Clear();
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-03-18 17:54:32 -07:00
|
|
|
AudioBuffer::GetThreadSharedChannelsForRate(JSContext* aJSContext, uint32_t* aRate,
|
2013-02-04 15:07:25 -08:00
|
|
|
uint32_t* aLength)
|
|
|
|
{
|
|
|
|
if (!mSharedChannels) {
|
|
|
|
// Steal JS data
|
|
|
|
mSharedChannels =
|
|
|
|
StealJSArrayDataIntoThreadSharedFloatArrayBufferList(aJSContext, mJSChannels);
|
|
|
|
}
|
|
|
|
|
2013-03-18 17:54:32 -07:00
|
|
|
*aLength = mLength;
|
|
|
|
*aRate = mSampleRate;
|
|
|
|
return mSharedChannels;
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
2013-03-07 15:14:46 -08:00
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
|
|
|
}
|