2020-08-15 10:03:39 +00:00
|
|
|
#include "Common/Log.h"
|
|
|
|
|
2020-07-28 12:22:50 +00:00
|
|
|
#include "android/jni/AndroidAudio.h"
|
|
|
|
#include "android/jni/OpenSLContext.h"
|
|
|
|
|
2021-06-16 05:28:32 +00:00
|
|
|
std::string g_error;
|
|
|
|
std::mutex g_errorMutex;
|
|
|
|
|
2020-07-28 12:22:50 +00:00
|
|
|
AudioContext::AudioContext(AndroidAudioCallback cb, int _FramesPerBuffer, int _SampleRate)
|
|
|
|
: audioCallback(cb), framesPerBuffer(_FramesPerBuffer), sampleRate(_SampleRate) {
|
|
|
|
if (framesPerBuffer == 0)
|
|
|
|
framesPerBuffer = 256;
|
|
|
|
if (framesPerBuffer < 32)
|
|
|
|
framesPerBuffer = 32;
|
|
|
|
if (framesPerBuffer > 4096)
|
|
|
|
framesPerBuffer = 4096;
|
|
|
|
|
|
|
|
sampleRate = _SampleRate;
|
2021-06-16 05:28:32 +00:00
|
|
|
g_error = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioContext::SetErrorString(const std::string &error) {
|
|
|
|
std::unique_lock<std::mutex> lock(g_errorMutex);
|
|
|
|
g_error = error;
|
2020-07-28 12:22:50 +00:00
|
|
|
}
|
2012-07-07 20:58:27 +00:00
|
|
|
|
2017-08-01 13:28:45 +00:00
|
|
|
struct AndroidAudioState {
|
2017-08-01 13:42:47 +00:00
|
|
|
AudioContext *ctx = nullptr;
|
|
|
|
AndroidAudioCallback callback = nullptr;
|
2019-08-06 12:20:10 +00:00
|
|
|
// output
|
2017-08-01 13:42:47 +00:00
|
|
|
int frames_per_buffer = 0;
|
|
|
|
int sample_rate = 0;
|
2019-08-06 12:20:10 +00:00
|
|
|
// input
|
|
|
|
int input_enable = 0;
|
|
|
|
int input_sample_rate = 0;
|
2013-06-04 21:53:41 +00:00
|
|
|
};
|
|
|
|
|
2019-09-17 21:26:42 +00:00
|
|
|
AndroidAudioState *AndroidAudio_Init(AndroidAudioCallback callback, int optimalFramesPerBuffer, int optimalSampleRate) {
|
2017-08-01 13:28:45 +00:00
|
|
|
AndroidAudioState *state = new AndroidAudioState();
|
2015-07-19 15:27:02 +00:00
|
|
|
state->callback = callback;
|
2013-06-19 12:27:13 +00:00
|
|
|
state->frames_per_buffer = optimalFramesPerBuffer ? optimalFramesPerBuffer : 256;
|
|
|
|
state->sample_rate = optimalSampleRate ? optimalSampleRate : 44100;
|
2017-08-01 13:28:45 +00:00
|
|
|
return state;
|
2012-07-08 11:23:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 09:03:43 +00:00
|
|
|
bool AndroidAudio_Recording_SetSampleRate(AndroidAudioState *state, int sampleRate) {
|
|
|
|
if (!state) {
|
|
|
|
ERROR_LOG(AUDIO, "AndroidAudioState not initialized, cannot set recording sample rate");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
state->input_sample_rate = sampleRate;
|
2020-10-24 10:36:24 +00:00
|
|
|
INFO_LOG(AUDIO, "AndroidAudio_Recording_SetSampleRate=%d", sampleRate);
|
2020-10-24 09:03:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AndroidAudio_Recording_Start(AndroidAudioState *state) {
|
2019-08-06 12:20:10 +00:00
|
|
|
if (!state) {
|
|
|
|
ERROR_LOG(AUDIO, "AndroidAudioState not initialized, cannot start recording!");
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-24 09:03:43 +00:00
|
|
|
state->input_enable = 1;
|
2019-08-06 12:20:10 +00:00
|
|
|
if (!state->ctx) {
|
|
|
|
ERROR_LOG(AUDIO, "OpenSLContext not initialized, cannot start recording!");
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-24 09:03:43 +00:00
|
|
|
state->ctx->AudioRecord_Start(state->input_sample_rate);
|
2019-08-06 12:20:10 +00:00
|
|
|
INFO_LOG(AUDIO, "AndroidAudio_Recording_Start");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AndroidAudio_Recording_Stop(AndroidAudioState *state) {
|
|
|
|
if (!state) {
|
|
|
|
ERROR_LOG(AUDIO, "AndroidAudioState not initialized, cannot stop recording!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!state->ctx) {
|
|
|
|
ERROR_LOG(AUDIO, "OpenSLContext not initialized, cannot stop recording!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
state->input_enable = 0;
|
|
|
|
state->input_sample_rate = 0;
|
|
|
|
state->ctx->AudioRecord_Stop();
|
|
|
|
INFO_LOG(AUDIO, "AndroidAudio_Recording_Stop");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-24 10:36:24 +00:00
|
|
|
bool AndroidAudio_Recording_State(AndroidAudioState *state) {
|
|
|
|
if (!state) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return state->input_enable;
|
|
|
|
}
|
|
|
|
|
2017-08-01 13:28:45 +00:00
|
|
|
bool AndroidAudio_Resume(AndroidAudioState *state) {
|
2013-06-04 21:53:41 +00:00
|
|
|
if (!state) {
|
2020-08-15 10:03:39 +00:00
|
|
|
ERROR_LOG(AUDIO, "Audio was shutdown, cannot resume!");
|
2013-06-04 21:53:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-01 13:42:47 +00:00
|
|
|
if (!state->ctx) {
|
2020-08-15 10:03:39 +00:00
|
|
|
INFO_LOG(AUDIO, "Calling OpenSLWrap_Init_T...");
|
2017-08-01 13:42:47 +00:00
|
|
|
state->ctx = new OpenSLContext(state->callback, state->frames_per_buffer, state->sample_rate);
|
2020-08-15 10:03:39 +00:00
|
|
|
INFO_LOG(AUDIO, "Returned from OpenSLWrap_Init_T");
|
2017-08-01 13:42:47 +00:00
|
|
|
bool init_retval = state->ctx->Init();
|
|
|
|
if (!init_retval) {
|
|
|
|
delete state->ctx;
|
|
|
|
state->ctx = nullptr;
|
|
|
|
}
|
2019-08-06 12:20:10 +00:00
|
|
|
if (state->input_enable) {
|
|
|
|
state->ctx->AudioRecord_Start(state->input_sample_rate);
|
|
|
|
}
|
2012-07-08 11:23:30 +00:00
|
|
|
return init_retval;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-07 20:58:27 +00:00
|
|
|
|
2017-08-01 13:28:45 +00:00
|
|
|
bool AndroidAudio_Pause(AndroidAudioState *state) {
|
2013-06-04 21:53:41 +00:00
|
|
|
if (!state) {
|
2020-08-15 10:03:39 +00:00
|
|
|
ERROR_LOG(AUDIO, "Audio was shutdown, cannot pause!");
|
2013-06-04 21:53:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-01 13:42:47 +00:00
|
|
|
if (state->ctx) {
|
2020-08-15 10:03:39 +00:00
|
|
|
INFO_LOG(AUDIO, "Calling OpenSLWrap_Shutdown_T...");
|
2017-08-01 13:42:47 +00:00
|
|
|
delete state->ctx;
|
|
|
|
state->ctx = nullptr;
|
2020-08-15 10:03:39 +00:00
|
|
|
INFO_LOG(AUDIO, "Returned from OpenSLWrap_Shutdown_T ...");
|
2012-07-08 11:23:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-07-07 20:58:27 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 13:28:45 +00:00
|
|
|
bool AndroidAudio_Shutdown(AndroidAudioState *state) {
|
2013-06-04 21:53:41 +00:00
|
|
|
if (!state) {
|
2020-08-15 10:03:39 +00:00
|
|
|
ERROR_LOG(AUDIO, "Audio already shutdown!");
|
2017-08-01 13:28:45 +00:00
|
|
|
return false;
|
2013-06-04 21:53:41 +00:00
|
|
|
}
|
2017-08-01 13:42:47 +00:00
|
|
|
if (state->ctx) {
|
2020-08-15 10:03:39 +00:00
|
|
|
ERROR_LOG(AUDIO, "Should not shut down when playing! Something is wrong!");
|
2017-08-01 13:28:45 +00:00
|
|
|
return false;
|
2012-07-08 11:37:49 +00:00
|
|
|
}
|
2013-06-04 21:53:41 +00:00
|
|
|
delete state;
|
2020-08-15 10:03:39 +00:00
|
|
|
INFO_LOG(AUDIO, "OpenSLWrap completely unloaded.");
|
2017-08-01 13:28:45 +00:00
|
|
|
return true;
|
2012-07-07 20:58:27 +00:00
|
|
|
}
|
2021-06-14 22:23:45 +00:00
|
|
|
|
|
|
|
const std::string AndroidAudio_GetErrorString(AndroidAudioState *state) {
|
|
|
|
if (!state) {
|
|
|
|
return "No state";
|
|
|
|
}
|
2021-06-16 05:28:32 +00:00
|
|
|
std::unique_lock<std::mutex> lock(g_errorMutex);
|
|
|
|
return g_error;
|
2021-06-14 22:23:45 +00:00
|
|
|
}
|