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"
|
2016-04-13 22:31:50 +03:00
|
|
|
#include "AlignmentUtils.h"
|
2015-09-03 10:05:02 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
2016-04-13 22:31:50 +03:00
|
|
|
* We guarantee 16 byte alignment of the channel data.
|
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)
|
|
|
|
{
|
2016-04-13 22:31:50 +03:00
|
|
|
float* base = reinterpret_cast<float*>(((uintptr_t)(this + 1) + 15) & ~0x0F);
|
|
|
|
ASSERT_ALIGNED16(base);
|
|
|
|
return base + aChannel * WEBAUDIO_BLOCK_SIZE;
|
2015-09-03 09:45:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static already_AddRefed<AudioBlockBuffer> Create(uint32_t aChannelCount)
|
|
|
|
{
|
|
|
|
CheckedInt<size_t> size = WEBAUDIO_BLOCK_SIZE;
|
|
|
|
size *= aChannelCount;
|
|
|
|
size *= sizeof(float);
|
|
|
|
size += sizeof(AudioBlockBuffer);
|
2016-04-13 22:31:50 +03:00
|
|
|
size += 15; //padding for alignment
|
2015-09-03 09:45:14 +03:00
|
|
|
if (!size.isValid()) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2016-04-13 22:31:50 +03:00
|
|
|
|
2017-08-30 03:17:07 +03:00
|
|
|
void* m = operator new(size.value());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AudioBlockBuffer> p = new (m) AudioBlockBuffer();
|
2015-09-03 09:45:14 +03:00
|
|
|
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-10-18 08:24:48 +03:00
|
|
|
RefPtr<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
|