Skip to content

Commit

Permalink
Merge pull request #14537 from hrydgard/android-opensl-error-global
Browse files Browse the repository at this point in the history
Store the OpenSL error in a global to avoid "No context"
  • Loading branch information
hrydgard committed Jun 16, 2021
2 parents 5ccac17 + 942958c commit 906b38e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
15 changes: 11 additions & 4 deletions android/jni/AndroidAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "android/jni/AndroidAudio.h"
#include "android/jni/OpenSLContext.h"

std::string g_error;
std::mutex g_errorMutex;

AudioContext::AudioContext(AndroidAudioCallback cb, int _FramesPerBuffer, int _SampleRate)
: audioCallback(cb), framesPerBuffer(_FramesPerBuffer), sampleRate(_SampleRate) {
if (framesPerBuffer == 0)
Expand All @@ -13,6 +16,12 @@ AudioContext::AudioContext(AndroidAudioCallback cb, int _FramesPerBuffer, int _S
framesPerBuffer = 4096;

sampleRate = _SampleRate;
g_error = "";
}

void AudioContext::SetErrorString(const std::string &error) {
std::unique_lock<std::mutex> lock(g_errorMutex);
g_error = error;
}

struct AndroidAudioState {
Expand Down Expand Up @@ -137,8 +146,6 @@ const std::string AndroidAudio_GetErrorString(AndroidAudioState *state) {
if (!state) {
return "No state";
}
if (!state->ctx) {
return "No context";
}
return state->ctx->GetErrorString();
std::unique_lock<std::mutex> lock(g_errorMutex);
return g_error;
}
10 changes: 1 addition & 9 deletions android/jni/AndroidAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,15 @@ class AudioContext {
virtual bool Init() { return false; }
virtual bool AudioRecord_Start(int sampleRate) { return false; };
virtual bool AudioRecord_Stop() { return false; };
const std::string &GetErrorString() {
std::unique_lock<std::mutex> lock(errorMutex_);
return error_;
}

virtual ~AudioContext() {}

protected:
void SetErrorString(const std::string &error) {
std::unique_lock<std::mutex> lock(errorMutex_);
error_ = error;
}
void SetErrorString(const std::string &error);
AndroidAudioCallback audioCallback;

int framesPerBuffer;
int sampleRate;
std::string error_ = "";
std::mutex errorMutex_;
};

Expand Down

0 comments on commit 906b38e

Please sign in to comment.