2015-09-03 10:05:02 +03: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 "AudioBlock.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-09-03 09:45:14 +03:00
|
|
|
/**
|
|
|
|
* Heap-allocated buffer of channels of 128-sample float arrays, with
|
|
|
|
* threadsafe refcounting. Typically you would allocate one of these, fill it
|
|
|
|
* in, and then treat it as immutable while it's shared.
|
2015-09-08 03:04:16 +03:00
|
|
|
*
|
|
|
|
* Downstream references are accounted specially so that the creator of the
|
|
|
|
* buffer can reuse and modify its contents next iteration if other references
|
|
|
|
* are all downstream temporary references held by AudioBlock.
|
|
|
|
*
|
2015-09-03 09:45:14 +03:00
|
|
|
* This only guarantees 4-byte alignment of the data. For alignment we simply
|
2015-09-08 03:04:16 +03:00
|
|
|
* assume that the memory from malloc is at least 4-byte aligned and that
|
|
|
|
* AudioBlockBuffer's size is divisible by 4.
|
2015-09-03 09:45:14 +03:00
|
|
|
*/
|
|
|
|
class AudioBlockBuffer final : public ThreadSharedObject {
|
|
|
|
public:
|
2015-09-08 03:04:16 +03:00
|
|
|
|
|
|
|
virtual AudioBlockBuffer* AsAudioBlockBuffer() override { return this; };
|
|
|
|
|
2015-09-03 09:45:14 +03:00
|
|
|
float* ChannelData(uint32_t aChannel)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<float*>(this + 1) + aChannel * WEBAUDIO_BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static already_AddRefed<AudioBlockBuffer> Create(uint32_t aChannelCount)
|
|
|
|
{
|
|
|
|
CheckedInt<size_t> size = WEBAUDIO_BLOCK_SIZE;
|
|
|
|
size *= aChannelCount;
|
|
|
|
size *= sizeof(float);
|
|
|
|
size += sizeof(AudioBlockBuffer);
|
|
|
|
if (!size.isValid()) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
void* m = moz_xmalloc(size.value());
|
|
|
|
nsRefPtr<AudioBlockBuffer> p = new (m) AudioBlockBuffer();
|
|
|
|
NS_ASSERTION((reinterpret_cast<char*>(p.get() + 1) - reinterpret_cast<char*>(p.get())) % 4 == 0,
|
|
|
|
"AudioBlockBuffers should be at least 4-byte aligned");
|
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2015-09-08 03:04:16 +03:00
|
|
|
// Graph thread only.
|
|
|
|
void DownstreamRefAdded() { ++mDownstreamRefCount; }
|
|
|
|
void DownstreamRefRemoved() {
|
|
|
|
MOZ_ASSERT(mDownstreamRefCount > 0);
|
|
|
|
--mDownstreamRefCount;
|
|
|
|
}
|
|
|
|
// Whether this is shared by any owners that are not downstream.
|
|
|
|
// Called only from owners with a reference that is not a downstream
|
|
|
|
// reference. Graph thread only.
|
|
|
|
bool HasLastingShares()
|
|
|
|
{
|
|
|
|
// mRefCnt is atomic and so reading its value is defined even when
|
|
|
|
// modifications may happen on other threads. mDownstreamRefCount is
|
|
|
|
// not modified on any other thread.
|
|
|
|
//
|
|
|
|
// If all other references are downstream references (managed on this, the
|
|
|
|
// graph thread), then other threads are not using this buffer and cannot
|
|
|
|
// add further references. This method can safely return false. The
|
|
|
|
// buffer contents can be modified.
|
|
|
|
//
|
|
|
|
// If there are other references that are not downstream references, then
|
|
|
|
// this method will return true. The buffer will be assumed to be still
|
|
|
|
// in use and so will not be reused.
|
|
|
|
nsrefcnt count = mRefCnt;
|
|
|
|
// This test is strictly less than because the caller has a reference
|
|
|
|
// that is not a downstream reference.
|
|
|
|
MOZ_ASSERT(mDownstreamRefCount < count);
|
|
|
|
return count != mDownstreamRefCount + 1;
|
|
|
|
}
|
|
|
|
|
2015-09-03 09:45:14 +03:00
|
|
|
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AudioBlockBuffer() {}
|
2015-09-08 03:04:16 +03:00
|
|
|
~AudioBlockBuffer() override { MOZ_ASSERT(mDownstreamRefCount == 0); }
|
|
|
|
|
|
|
|
nsAutoRefCnt mDownstreamRefCount;
|
2015-09-03 09:45:14 +03:00
|
|
|
};
|
|
|
|
|
2015-09-08 03:04:16 +03:00
|
|
|
AudioBlock::~AudioBlock()
|
|
|
|
{
|
|
|
|
ClearDownstreamMark();
|
|
|
|
}
|
|
|
|
|
2015-09-03 10:05:02 +03:00
|
|
|
void
|
2015-09-08 03:04:16 +03:00
|
|
|
AudioBlock::SetBuffer(ThreadSharedObject* aNewBuffer)
|
2015-09-03 10:05:02 +03:00
|
|
|
{
|
2015-09-08 03:04:16 +03:00
|
|
|
if (aNewBuffer == mBuffer) {
|
2015-09-03 10:05:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-08 03:04:16 +03:00
|
|
|
ClearDownstreamMark();
|
|
|
|
|
|
|
|
mBuffer = aNewBuffer;
|
|
|
|
|
|
|
|
if (!aNewBuffer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioBlockBuffer* buffer = aNewBuffer->AsAudioBlockBuffer();
|
|
|
|
if (buffer) {
|
|
|
|
buffer->DownstreamRefAdded();
|
|
|
|
mBufferIsDownstreamRef = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioBlock::ClearDownstreamMark() {
|
|
|
|
if (mBufferIsDownstreamRef) {
|
|
|
|
mBuffer->AsAudioBlockBuffer()->DownstreamRefRemoved();
|
|
|
|
mBufferIsDownstreamRef = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 00:01:55 +03:00
|
|
|
bool
|
|
|
|
AudioBlock::CanWrite() {
|
|
|
|
// If mBufferIsDownstreamRef is set then the buffer is not ours to use.
|
|
|
|
// It may be in use by another node which is not downstream.
|
|
|
|
return !mBufferIsDownstreamRef &&
|
|
|
|
!mBuffer->AsAudioBlockBuffer()->HasLastingShares();
|
2015-09-03 10:01:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioBlock::AllocateChannels(uint32_t aChannelCount)
|
2015-09-08 03:04:16 +03:00
|
|
|
{
|
2015-09-03 10:01:50 +03:00
|
|
|
MOZ_ASSERT(mDuration == WEBAUDIO_BLOCK_SIZE);
|
|
|
|
|
|
|
|
if (mBufferIsDownstreamRef) {
|
|
|
|
// This is not our buffer to re-use.
|
|
|
|
ClearDownstreamMark();
|
|
|
|
} else if (mBuffer && ChannelCount() == aChannelCount) {
|
|
|
|
AudioBlockBuffer* buffer = mBuffer->AsAudioBlockBuffer();
|
2015-09-08 03:04:16 +03:00
|
|
|
if (buffer && !buffer->HasLastingShares()) {
|
2015-09-03 10:01:50 +03:00
|
|
|
MOZ_ASSERT(mBufferFormat == AUDIO_FORMAT_FLOAT32);
|
2015-09-08 03:04:16 +03:00
|
|
|
// No need to allocate again.
|
2015-09-03 10:01:50 +03:00
|
|
|
mVolume = 1.0f;
|
2015-09-08 03:04:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 10:05:02 +03:00
|
|
|
// XXX for SIMD purposes we should do something here to make sure the
|
|
|
|
// channel buffers are 16-byte aligned.
|
2015-09-03 09:45:14 +03:00
|
|
|
nsRefPtr<AudioBlockBuffer> buffer = AudioBlockBuffer::Create(aChannelCount);
|
2015-09-03 10:01:50 +03:00
|
|
|
mChannelData.SetLength(aChannelCount);
|
2015-09-03 10:05:02 +03:00
|
|
|
for (uint32_t i = 0; i < aChannelCount; ++i) {
|
2015-09-03 10:01:50 +03:00
|
|
|
mChannelData[i] = buffer->ChannelData(i);
|
2015-09-03 10:05:02 +03:00
|
|
|
}
|
2015-09-03 10:01:50 +03:00
|
|
|
mBuffer = buffer.forget();
|
|
|
|
mVolume = 1.0f;
|
|
|
|
mBufferFormat = AUDIO_FORMAT_FLOAT32;
|
2015-09-03 10:05:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|