Field gets overwritten when opening audiostream in OBOE

183 views Asked by At

I am using OBOE to build an sound app. I have in my AudioEngine.cpp defined couple of fields (audiosample - float, starting position field - int field, sample lengths field - int field etc.) However one of the fields (sample lengts - int field) gets overwritten when I open the audiostream with playbackBuilder. See code

 AudioStreamBuilder playbackBuilder;

playbackBuilder.setPerformanceMode(PerformanceMode::LowLatency);
playbackBuilder.setSharingMode(SharingMode::Exclusive);
playbackBuilder.setChannelCount(ChannelCount::Mono);
playbackBuilder.setFormat(AudioFormat::Float);
playbackBuilder.setSampleRate(44100);
playbackBuilder.setCallback(this);
LOGD("samplelength[46]=%d",sampleLengths_[46]);
Result result = playbackBuilder.openStream(&outputStream_);
if (result != Result::OK) {
    LOGE("Error opening output stream_ %s",convertToText(result));
    return false;
}
LOGD("samplelength[46]=%d",sampleLengths_[46]);
auto setBufferSizeResult = outputStream_->setBufferSizeInFrames(outputStream_->getFramesPerBurst() * 2);
if (setBufferSizeResult) {
    LOGD("New buffer size is %d in frames",setBufferSizeResult.value());
}

The output from logcat is:

D/OboeAudio: samplelength[46]=2393
D/OboeAudio: openStream() OUTPUT -------- OboeVersion1.4.0 --------
D/OboeAudio: AAudioLoader():  dlopen(libaaudio.so) returned 0x73861583b1301b8f
I/cesthingsplaye: Waiting for a blocking GC ProfileSaver
D/OboeAudio: AudioStreamAAudio() call isSupported()
I/AAudio: AAudioStreamBuilder_openStream() called ----------------------------------------
I/AudioStreamBuilder: rate   =  44100, channels  = 1, format   = 5, sharing = EX, dir = OUTPUT
I/AudioStreamBuilder: device =      0, sessionId = -1, perfMode = 12, callback: ON with frames = 0
I/AudioStreamBuilder: usage  =      1, contentType = 2, inputPreset = 6, allowedCapturePolicy = 0
D/AudioStreamBuilder: build() EXCLUSIVE sharing mode not supported. Use SHARED.
D/AudioStreamTrack: open(), request notificationFrames = -8, frameCount = 0
I/cesthingsplaye: WaitForGcToComplete blocked ProfileSaver on ClassLinker for 26.177ms
I/cesthingsplaye: WaitForGcToComplete blocked HeapTrim on ProfileSaver for 5.568ms
W/AudioTrack: createTrack_l(0): AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount 0 -> 1772
W/AudioStreamTrack: open() flags changed from 0x00000104 to 0x00000000
W/AudioStreamTrack: open() perfMode changed from 12 to 10
I/AAudio: AAudioStreamBuilder_openStream() returns 0 = AAUDIO_OK for s#1 ----------------
D/OboeAudio: AudioStreamAAudio.open() app    format = 2
D/OboeAudio: AudioStreamAAudio.open() sample rate   = 44100
D/OboeAudio: AudioStreamAAudio.open() capacity      = 1772
D/OboeAudio: AudioStreamAAudio.open: AAudioStream_Open() returned AAUDIO_OK, mAAudioStream = 0x731d2dd580
D/OboeAudio: samplelength[46]=0

NB: Value 2393 of the 46th element is exactly what I expect it to be. It is not a random number. It is initialized with the lines below. However it gets overwritteb (actualy the whole field) when opening stream.

This is the initialization code for this particular field - sampleLengths_ that gets overwritten:

sampleLengthsArrayLen_ = sampleLengthArrayLen;
sampleLengths_ = new int[sampleLengthArrayLen];
sampleLengths_ = sampleLengths;

Any hints why this happens? Bad memory mnagement? Thanks a lot. j

2

There are 2 answers

2
webaloman On BEST ANSWER

The solution is instead of C-style array to use Standard Library container. In my case its std::vector as I don't know the exact size at compile time. So I obtain an C-style array from JNI and create a vector , in this way the values are not overwritten . C-style array do not somehow offer protection for the data when opening stream in OBOE.

vector<int> sampleLengths_;
sampleLengthsArrayLen_ = sampleLengthArrayLen;
sampleLengths_.assign(sampleLengths,sampleLengths+sampleLengthArrayLen);
4
donturner On

I strongly suspect that this is because you haven't initialised sampleLengths so the values you're reading are actually just random pieces of memory which get changed during program execution.

C++ supports initialization of an int array to all zeros by doing:

int sampleLengths[10] = { 0 };