2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_driver_jandroid.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "audio_driver_jandroid.h"
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "os/os.h"
|
2017-07-22 10:47:04 +00:00
|
|
|
#include "project_settings.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "thread_jandroid.h"
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifndef ANDROID_NATIVE_ACTIVITY
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
AudioDriverAndroid *AudioDriverAndroid::s_ad = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
jobject AudioDriverAndroid::io;
|
|
|
|
jmethodID AudioDriverAndroid::_init_audio;
|
|
|
|
jmethodID AudioDriverAndroid::_write_buffer;
|
|
|
|
jmethodID AudioDriverAndroid::_quit;
|
|
|
|
jmethodID AudioDriverAndroid::_pause;
|
2017-03-05 15:44:50 +00:00
|
|
|
bool AudioDriverAndroid::active = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
jclass AudioDriverAndroid::cls;
|
2017-03-05 15:44:50 +00:00
|
|
|
int AudioDriverAndroid::audioBufferFrames = 0;
|
|
|
|
int AudioDriverAndroid::mix_rate = 44100;
|
|
|
|
bool AudioDriverAndroid::quit = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
jobject AudioDriverAndroid::audioBuffer = NULL;
|
2017-03-05 15:44:50 +00:00
|
|
|
void *AudioDriverAndroid::audioBufferPinned = NULL;
|
|
|
|
Mutex *AudioDriverAndroid::mutex = NULL;
|
|
|
|
int32_t *AudioDriverAndroid::audioBuffer32 = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const char *AudioDriverAndroid::get_name() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return "Android";
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Error AudioDriverAndroid::init() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
mutex = Mutex::create();
|
2017-03-05 15:44:50 +00:00
|
|
|
/*
|
2014-02-10 01:10:30 +00:00
|
|
|
// TODO: pass in/return a (Java) device ID, also whether we're opening for input or output
|
|
|
|
this->spec.samples = Android_JNI_OpenAudioDevice(this->spec.freq, this->spec.format == AUDIO_U8 ? 0 : 1, this->spec.channels, this->spec.samples);
|
|
|
|
SDL_CalculateAudioSpec(&this->spec);
|
|
|
|
|
|
|
|
if (this->spec.samples == 0) {
|
|
|
|
// Init failed?
|
|
|
|
SDL_SetError("Java-side initialization failed!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2017-01-14 11:26:56 +00:00
|
|
|
//Android_JNI_SetupThread();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
// __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
JNIEnv *env = ThreadAndroid::get_env();
|
2017-03-05 15:44:50 +00:00
|
|
|
int mix_rate = GLOBAL_DEF("audio/mix_rate", 44100);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int latency = GLOBAL_DEF("audio/output_latency", 25);
|
|
|
|
latency = 50;
|
2017-08-17 21:35:55 +00:00
|
|
|
unsigned int buffer_size = next_power_of_2(latency * mix_rate / 1000);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (OS::get_singleton()->is_stdout_verbose()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
print_line("audio buffer size: " + itos(buffer_size));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "Initializing audio! params: %i,%i ", mix_rate, buffer_size);
|
|
|
|
audioBuffer = env->CallObjectMethod(io, _init_audio, mix_rate, buffer_size);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(audioBuffer == NULL, ERR_INVALID_PARAMETER);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
audioBuffer = env->NewGlobalRef(audioBuffer);
|
|
|
|
|
|
|
|
jboolean isCopy = JNI_FALSE;
|
|
|
|
audioBufferPinned = env->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
|
|
|
|
audioBufferFrames = env->GetArrayLength((jshortArray)audioBuffer);
|
2017-03-05 15:44:50 +00:00
|
|
|
audioBuffer32 = memnew_arr(int32_t, audioBufferFrames);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AudioDriverAndroid::start() {
|
|
|
|
active = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AudioDriverAndroid::setup(jobject p_io) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
JNIEnv *env = ThreadAndroid::get_env();
|
2017-03-05 15:44:50 +00:00
|
|
|
io = p_io;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
jclass c = env->GetObjectClass(io);
|
|
|
|
cls = (jclass)env->NewGlobalRef(c);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "starting to attempt get methods");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_init_audio = env->GetMethodID(cls, "audioInit", "(II)Ljava/lang/Object;");
|
2017-03-05 15:44:50 +00:00
|
|
|
if (_init_audio != 0) {
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _init_audio ok!!");
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "audioinit ok!");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_write_buffer = env->GetMethodID(cls, "audioWriteShortBuffer", "([S)V");
|
2017-03-05 15:44:50 +00:00
|
|
|
if (_write_buffer != 0) {
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _write_buffer ok!!");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_quit = env->GetMethodID(cls, "audioQuit", "()V");
|
2017-03-05 15:44:50 +00:00
|
|
|
if (_quit != 0) {
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _quit ok!!");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_pause = env->GetMethodID(cls, "audioPause", "(Z)V");
|
2017-03-05 15:44:50 +00:00
|
|
|
if (_quit != 0) {
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _pause ok!!");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverAndroid::thread_func(JNIEnv *env) {
|
|
|
|
|
2016-01-08 20:53:00 +00:00
|
|
|
jclass cls = env->FindClass("org/godotengine/godot/Godot");
|
2014-02-10 01:10:30 +00:00
|
|
|
if (cls) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
cls = (jclass)env->NewGlobalRef(cls);
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "*******CLASS FOUND!!!");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-01-08 20:53:00 +00:00
|
|
|
jfieldID fid = env->GetStaticFieldID(cls, "io", "Lorg/godotengine/godot/GodotIO;");
|
2017-03-05 15:44:50 +00:00
|
|
|
jobject ob = env->GetStaticObjectField(cls, fid);
|
2014-02-10 01:10:30 +00:00
|
|
|
jobject gob = env->NewGlobalRef(ob);
|
|
|
|
jclass c = env->GetObjectClass(gob);
|
|
|
|
jclass lcls = (jclass)env->NewGlobalRef(c);
|
|
|
|
_write_buffer = env->GetMethodID(lcls, "audioWriteShortBuffer", "([S)V");
|
2017-03-05 15:44:50 +00:00
|
|
|
if (_write_buffer != 0) {
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _write_buffer ok!!");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (!quit) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int16_t *ptr = (int16_t *)audioBufferPinned;
|
2014-02-10 01:10:30 +00:00
|
|
|
int fc = audioBufferFrames;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!s_ad->active || mutex->try_lock() != OK) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < fc; i++) {
|
|
|
|
ptr[i] = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
s_ad->audio_server_process(fc / 2, audioBuffer32);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
mutex->unlock();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < fc; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ptr[i] = audioBuffer32[i] >> 16;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
env->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)ptr, JNI_COMMIT);
|
|
|
|
env->CallVoidMethod(gob, _write_buffer, (jshortArray)audioBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int AudioDriverAndroid::get_mix_rate() const {
|
|
|
|
|
|
|
|
return mix_rate;
|
|
|
|
}
|
2016-07-21 10:07:01 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
AudioDriver::SpeakerMode AudioDriverAndroid::get_speaker_mode() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-16 18:19:45 +00:00
|
|
|
return SPEAKER_MODE_STEREO;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-07-21 10:07:01 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AudioDriverAndroid::lock() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (mutex)
|
|
|
|
mutex->lock();
|
|
|
|
}
|
2016-07-21 10:07:01 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void AudioDriverAndroid::unlock() {
|
|
|
|
|
|
|
|
if (mutex)
|
|
|
|
mutex->unlock();
|
|
|
|
}
|
2016-07-21 10:07:01 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AudioDriverAndroid::finish() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
JNIEnv *env = ThreadAndroid::get_env();
|
|
|
|
env->CallVoidMethod(io, _quit);
|
|
|
|
|
|
|
|
if (audioBuffer) {
|
|
|
|
env->DeleteGlobalRef(audioBuffer);
|
|
|
|
audioBuffer = NULL;
|
|
|
|
audioBufferPinned = NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
active = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverAndroid::set_pause(bool p_pause) {
|
|
|
|
|
|
|
|
JNIEnv *env = ThreadAndroid::get_env();
|
2017-03-05 15:44:50 +00:00
|
|
|
env->CallVoidMethod(io, _pause, p_pause);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 10:07:01 +00:00
|
|
|
AudioDriverAndroid::AudioDriverAndroid() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
s_ad = this;
|
|
|
|
active = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|