< prev index next >
modules/media/src/main/native/jfxmedia/platform/osx/avf/AVFAudioSpectrumUnit.cpp
Print this page
rev 9506 : 8156563: JavaFX Ensemble8 media sample hang and crash
Reviewed-by: almatvee, kcr
*** 1,7 ****
/*
! * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
--- 1,7 ----
/*
! * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
*** 41,50 ****
--- 41,51 ----
AVFKernelProcessor(),
mSpectrumCallbackProc(NULL),
mSpectrumCallbackContext(NULL),
mEnabled(true),
+
mBandCount(128),
mBands(NULL),
mUpdateInterval(kDefaultAudioSpectrumUpdateInterval),
mThreshold(kDefaultAudioSpectrumThreshold),
mProcessor(NULL),
*** 62,71 ****
--- 63,74 ----
mRebuildCrunch(true),
mSpectralCrunch(NULL)
{
mMixBuffer.mNumberBuffers = 1;
mMixBuffer.mBuffers[0].mData = NULL;
+
+ pthread_mutex_init(&mBandLock, NULL);
}
AVFAudioSpectrumUnit::~AVFAudioSpectrumUnit() {
if (mMixBuffer.mBuffers[0].mData) {
free(mMixBuffer.mBuffers[0].mData);
*** 83,92 ****
--- 86,98 ----
OSStatus AVFAudioSpectrumUnit::ProcessBufferLists(AudioUnitRenderActionFlags& ioActionFlags,
const AudioBufferList& inBuffer,
AudioBufferList& outBuffer,
UInt32 inFramesToProcess)
{
+ if (!mEnabled) {
+ return noErr;
+ }
// (Re)allocate mix buffer if needed
if (!mMixBuffer.mBuffers[0].mData || mMixBufferFrameCapacity < inFramesToProcess) {
// allocate buffer list (only need to do this once)
if (mMixBuffer.mBuffers[0].mData) {
free(mMixBuffer.mBuffers[0].mData);
*** 137,149 ****
mEnabled = isEnabled;
mRebuildCrunch = true;
}
void AVFAudioSpectrumUnit::SetBands(int bands, CBandsHolder* holder) {
mBandCount = bands;
! mBands = holder;
mRebuildCrunch = true;
}
size_t AVFAudioSpectrumUnit::GetBands() {
return mBandCount;
}
--- 143,166 ----
mEnabled = isEnabled;
mRebuildCrunch = true;
}
void AVFAudioSpectrumUnit::SetBands(int bands, CBandsHolder* holder) {
+ lockBands();
+ if (mBands) {
+ CBandsHolder::ReleaseRef(mBands);
+ mBands = NULL;
+ }
+ mBandCount = 0;
+ if (holder) {
+ mBands = CBandsHolder::AddRef(holder);
+ if (mBands) {
mBandCount = bands;
! }
! }
mRebuildCrunch = true;
+ unlockBands();
}
size_t AVFAudioSpectrumUnit::GetBands() {
return mBandCount;
}
*** 176,193 ****
mRebuildCrunch = true;
}
static void AVFAudioSpectrum_SpectralFunction(SpectralBufferList* inSpectra, void* inUserData) {
AVFAudioSpectrumUnit *unit = static_cast<AVFAudioSpectrumUnit*>(inUserData);
! if (unit) {
unit->SpectralFunction(inSpectra);
}
}
void AVFAudioSpectrumUnit::SpectralFunction(SpectralBufferList* inSpectra) {
// https://developer.apple.com/library/mac/documentation/Performance/Conceptual/vDSP_Programming_Guide/UsingFourierTransforms/UsingFourierTransforms.html
// Scale the results properly, scale factor is 2x for 1D real forward transforms
float scale = 2.0;
DSPSplitComplex *cplx = inSpectra->mDSPSplitComplex;
vDSP_vsmul(cplx->realp, 1, &scale, cplx->realp, 1, mBandCount);
vDSP_vsmul(cplx->imagp, 1, &scale, cplx->imagp, 1, mBandCount);
--- 193,218 ----
mRebuildCrunch = true;
}
static void AVFAudioSpectrum_SpectralFunction(SpectralBufferList* inSpectra, void* inUserData) {
AVFAudioSpectrumUnit *unit = static_cast<AVFAudioSpectrumUnit*>(inUserData);
! if (unit && unit->IsEnabled()) {
unit->SpectralFunction(inSpectra);
}
}
void AVFAudioSpectrumUnit::SpectralFunction(SpectralBufferList* inSpectra) {
// https://developer.apple.com/library/mac/documentation/Performance/Conceptual/vDSP_Programming_Guide/UsingFourierTransforms/UsingFourierTransforms.html
// Scale the results properly, scale factor is 2x for 1D real forward transforms
+
+ // lock now otherwise the bands could change while we're processing
+ lockBands();
+ if (!mBands || mBandCount <= 0 || !mEnabled) {
+ unlockBands();
+ return;
+ }
+
float scale = 2.0;
DSPSplitComplex *cplx = inSpectra->mDSPSplitComplex;
vDSP_vsmul(cplx->realp, 1, &scale, cplx->realp, 1, mBandCount);
vDSP_vsmul(cplx->imagp, 1, &scale, cplx->imagp, 1, mBandCount);
*** 219,231 ****
// Get averages
vDSP_vsdiv(mMagnitudes, 1, &divisor, mMagnitudes, 1, mBandCount);
vDSP_vsdiv(mPhases, 1, &divisor, mPhases, 1, mBandCount);
// Update band data
- if (mBands) {
mBands->UpdateBands(mBandCount, mMagnitudes, mPhases);
- }
// Call our listener to dispatch the spectrum event
if (mSpectrumCallbackProc) {
double duration = (double)mSamplesPerInterval / (double)mAudioUnit->GetSampleRate();
mSpectrumCallbackProc(mSpectrumCallbackContext, duration);
--- 244,254 ----
*** 234,256 ****
// Reset things
vDSP_vclr(mMagnitudes, 1, mBandCount);
vDSP_vclr(mPhases, 1, mBandCount);
mFFTCount = 0;
}
}
void AVFAudioSpectrumUnit::SetupSpectralProcessor() {
if (mSpectralCrunch) {
delete mSpectralCrunch;
mSpectralCrunch = NULL;
mWorkBuffer.free();
mMagnitudes.free();
mPhases.free();
}
! if (mEnabled && mBandCount > 0) {
// inFFTSize = 2x number of bins (this is adjusted properly later)
// inHopSize = the number of samples to increment per update, depends on
// how much oversampling we want
// inNumChannels = number of audio channels, we mix down to 1 since FX
// lamely only supports one channel spectrum output
--- 257,282 ----
// Reset things
vDSP_vclr(mMagnitudes, 1, mBandCount);
vDSP_vclr(mPhases, 1, mBandCount);
mFFTCount = 0;
}
+ unlockBands();
}
void AVFAudioSpectrumUnit::SetupSpectralProcessor() {
+ lockBands();
+
if (mSpectralCrunch) {
delete mSpectralCrunch;
mSpectralCrunch = NULL;
mWorkBuffer.free();
mMagnitudes.free();
mPhases.free();
}
! if (mEnabled && mBandCount > 0 && (mBands != NULL)) {
// inFFTSize = 2x number of bins (this is adjusted properly later)
// inHopSize = the number of samples to increment per update, depends on
// how much oversampling we want
// inNumChannels = number of audio channels, we mix down to 1 since FX
// lamely only supports one channel spectrum output
*** 286,291 ****
--- 312,318 ----
// Recalculate mSamplesPerInterval so we report duration correctly
mSamplesPerInterval = mFFTsPerInterval / kSpectrumOversampleFactor * mFFTSize;
} // else leave disabled
mRebuildCrunch = false;
+ unlockBands();
}
< prev index next >