From cdba79ca72ddfa822b344867004cb2bd3b8f6d28 Mon Sep 17 00:00:00 2001
From: Marcel Admiraal <madmiraal@users.noreply.github.com>
Date: Wed, 9 Jun 2021 17:45:51 +0100
Subject: [PATCH] Remove unused AudioDriverAndroid from Android

---
 platform/android/SCsub                        |   2 -
 platform/android/audio_driver_jandroid.cpp    | 185 ------------------
 platform/android/audio_driver_jandroid.h      |  78 --------
 .../src/org/godotengine/godot/GodotIO.java    |  89 ---------
 .../src/org/godotengine/godot/GodotLib.java   |   5 -
 platform/android/java_godot_lib_jni.cpp       |   7 -
 platform/android/java_godot_lib_jni.h         |   1 -
 platform/android/os_android.h                 |   3 -
 8 files changed, 370 deletions(-)
 delete mode 100644 platform/android/audio_driver_jandroid.cpp
 delete mode 100644 platform/android/audio_driver_jandroid.h

diff --git a/platform/android/SCsub b/platform/android/SCsub
index f2cea80611c..5a1425396bd 100644
--- a/platform/android/SCsub
+++ b/platform/android/SCsub
@@ -9,7 +9,6 @@ android_files = [
     "dir_access_jandroid.cpp",
     "thread_jandroid.cpp",
     "net_socket_android.cpp",
-    "audio_driver_jandroid.cpp",
     "java_godot_lib_jni.cpp",
     "java_class_wrapper.cpp",
     "java_godot_wrapper.cpp",
@@ -17,7 +16,6 @@ android_files = [
     "jni_utils.cpp",
     "android_keys_utils.cpp",
     "plugin/godot_plugin_jni.cpp",
-    #'power_android.cpp'
 ]
 
 env_android = env.Clone()
diff --git a/platform/android/audio_driver_jandroid.cpp b/platform/android/audio_driver_jandroid.cpp
deleted file mode 100644
index c8c588a7d2b..00000000000
--- a/platform/android/audio_driver_jandroid.cpp
+++ /dev/null
@@ -1,185 +0,0 @@
-/*************************************************************************/
-/*  audio_driver_jandroid.cpp                                            */
-/*************************************************************************/
-/*                       This file is part of:                           */
-/*                           GODOT ENGINE                                */
-/*                      https://godotengine.org                          */
-/*************************************************************************/
-/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
-/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */
-/*                                                                       */
-/* 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"
-
-#include "core/os/os.h"
-#include "core/project_settings.h"
-#include "thread_jandroid.h"
-
-AudioDriverAndroid *AudioDriverAndroid::s_ad = NULL;
-
-jobject AudioDriverAndroid::io;
-jmethodID AudioDriverAndroid::_init_audio;
-jmethodID AudioDriverAndroid::_write_buffer;
-jmethodID AudioDriverAndroid::_quit;
-jmethodID AudioDriverAndroid::_pause;
-bool AudioDriverAndroid::active = false;
-jclass AudioDriverAndroid::cls;
-int AudioDriverAndroid::audioBufferFrames = 0;
-int AudioDriverAndroid::mix_rate = 44100;
-bool AudioDriverAndroid::quit = false;
-jobject AudioDriverAndroid::audioBuffer = NULL;
-void *AudioDriverAndroid::audioBufferPinned = NULL;
-Mutex AudioDriverAndroid::mutex;
-int32_t *AudioDriverAndroid::audioBuffer32 = NULL;
-
-const char *AudioDriverAndroid::get_name() const {
-	return "Android";
-}
-
-Error AudioDriverAndroid::init() {
-	/*
-	// 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;
-	   }
-*/
-
-	//Android_JNI_SetupThread();
-
-	//        __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
-
-	JNIEnv *env = get_jni_env();
-	int mix_rate = GLOBAL_GET("audio/mix_rate");
-
-	int latency = GLOBAL_GET("audio/output_latency");
-	unsigned int buffer_size = next_power_of_2(latency * mix_rate / 1000);
-	print_verbose("Audio buffer size: " + itos(buffer_size));
-
-	audioBuffer = env->CallObjectMethod(io, _init_audio, mix_rate, buffer_size);
-
-	ERR_FAIL_COND_V(audioBuffer == NULL, ERR_INVALID_PARAMETER);
-
-	audioBuffer = env->NewGlobalRef(audioBuffer);
-
-	jboolean isCopy = JNI_FALSE;
-	audioBufferPinned = env->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
-	audioBufferFrames = env->GetArrayLength((jshortArray)audioBuffer);
-	audioBuffer32 = memnew_arr(int32_t, audioBufferFrames);
-
-	return OK;
-}
-
-void AudioDriverAndroid::start() {
-	active = true;
-}
-
-void AudioDriverAndroid::setup(jobject p_io) {
-	JNIEnv *env = get_jni_env();
-	io = p_io;
-
-	jclass c = env->GetObjectClass(io);
-	cls = (jclass)env->NewGlobalRef(c);
-
-	_init_audio = env->GetMethodID(cls, "audioInit", "(II)Ljava/lang/Object;");
-	_write_buffer = env->GetMethodID(cls, "audioWriteShortBuffer", "([S)V");
-	_quit = env->GetMethodID(cls, "audioQuit", "()V");
-	_pause = env->GetMethodID(cls, "audioPause", "(Z)V");
-}
-
-void AudioDriverAndroid::thread_func(JNIEnv *env) {
-	jclass cls = env->FindClass("org/godotengine/godot/Godot");
-	if (cls) {
-		cls = (jclass)env->NewGlobalRef(cls);
-	}
-	jfieldID fid = env->GetStaticFieldID(cls, "io", "Lorg/godotengine/godot/GodotIO;");
-	jobject ob = env->GetStaticObjectField(cls, fid);
-	jobject gob = env->NewGlobalRef(ob);
-	jclass c = env->GetObjectClass(gob);
-	jclass lcls = (jclass)env->NewGlobalRef(c);
-	_write_buffer = env->GetMethodID(lcls, "audioWriteShortBuffer", "([S)V");
-
-	while (!quit) {
-		int16_t *ptr = (int16_t *)audioBufferPinned;
-		int fc = audioBufferFrames;
-
-		if (!s_ad->active || mutex.try_lock() != OK) {
-			for (int i = 0; i < fc; i++) {
-				ptr[i] = 0;
-			}
-
-		} else {
-			s_ad->audio_server_process(fc / 2, audioBuffer32);
-
-			mutex.unlock();
-
-			for (int i = 0; i < fc; i++) {
-				ptr[i] = audioBuffer32[i] >> 16;
-			}
-		}
-		env->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)ptr, JNI_COMMIT);
-		env->CallVoidMethod(gob, _write_buffer, (jshortArray)audioBuffer);
-	}
-}
-
-int AudioDriverAndroid::get_mix_rate() const {
-	return mix_rate;
-}
-
-AudioDriver::SpeakerMode AudioDriverAndroid::get_speaker_mode() const {
-	return SPEAKER_MODE_STEREO;
-}
-
-void AudioDriverAndroid::lock() {
-	mutex.lock();
-}
-
-void AudioDriverAndroid::unlock() {
-	mutex.unlock();
-}
-
-void AudioDriverAndroid::finish() {
-	JNIEnv *env = get_jni_env();
-	env->CallVoidMethod(io, _quit);
-
-	if (audioBuffer) {
-		env->DeleteGlobalRef(audioBuffer);
-		audioBuffer = NULL;
-		audioBufferPinned = NULL;
-	}
-
-	active = false;
-}
-
-void AudioDriverAndroid::set_pause(bool p_pause) {
-	JNIEnv *env = get_jni_env();
-	env->CallVoidMethod(io, _pause, p_pause);
-}
-
-AudioDriverAndroid::AudioDriverAndroid() {
-	s_ad = this;
-	active = false;
-}
diff --git a/platform/android/audio_driver_jandroid.h b/platform/android/audio_driver_jandroid.h
deleted file mode 100644
index 9007fd2f818..00000000000
--- a/platform/android/audio_driver_jandroid.h
+++ /dev/null
@@ -1,78 +0,0 @@
-/*************************************************************************/
-/*  audio_driver_jandroid.h                                              */
-/*************************************************************************/
-/*                       This file is part of:                           */
-/*                           GODOT ENGINE                                */
-/*                      https://godotengine.org                          */
-/*************************************************************************/
-/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
-/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */
-/*                                                                       */
-/* 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.                */
-/*************************************************************************/
-
-#ifndef AUDIO_DRIVER_ANDROID_H
-#define AUDIO_DRIVER_ANDROID_H
-
-#include "servers/audio_server.h"
-
-#include "java_godot_lib_jni.h"
-
-class AudioDriverAndroid : public AudioDriver {
-	static Mutex mutex;
-	static AudioDriverAndroid *s_ad;
-	static jobject io;
-	static jmethodID _init_audio;
-	static jmethodID _write_buffer;
-	static jmethodID _quit;
-	static jmethodID _pause;
-	static bool active;
-	static bool quit;
-
-	static jclass cls;
-
-	static jobject audioBuffer;
-	static void *audioBufferPinned;
-	static int32_t *audioBuffer32;
-	static int audioBufferFrames;
-	static int mix_rate;
-
-public:
-	void set_singleton();
-
-	virtual const char *get_name() const;
-
-	virtual Error init();
-	virtual void start();
-	virtual int get_mix_rate() const;
-	virtual SpeakerMode get_speaker_mode() const;
-	virtual void lock();
-	virtual void unlock();
-	virtual void finish();
-
-	virtual void set_pause(bool p_pause);
-
-	static void setup(jobject p_io);
-	static void thread_func(JNIEnv *env);
-
-	AudioDriverAndroid();
-};
-
-#endif // AUDIO_DRIVER_ANDROID_H
diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java b/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java
index 71b1f1aa6c9..64c9d482543 100644
--- a/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java
+++ b/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java
@@ -329,95 +329,6 @@ public class GodotIO {
 		dirs = new SparseArray<AssetDir>();
 	}
 
-	/////////////////////////
-	// AUDIO
-	/////////////////////////
-
-	private Object buf;
-	private Thread mAudioThread;
-	private AudioTrack mAudioTrack;
-
-	public Object audioInit(int sampleRate, int desiredFrames) {
-		int channelConfig = AudioFormat.CHANNEL_OUT_STEREO;
-		int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
-		int frameSize = 4;
-
-		System.out.printf("audioInit: initializing audio:\n");
-
-		//Log.v("Godot", "Godot audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + ((float)sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer");
-
-		// Let the user pick a larger buffer if they really want -- but ye
-		// gods they probably shouldn't, the minimums are horrifyingly high
-		// latency already
-		desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize);
-
-		mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
-				channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);
-
-		audioStartThread();
-
-		//Log.v("Godot", "Godot audio: got " + ((mAudioTrack.getChannelCount() >= 2) ? "stereo" : "mono") + " " + ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit" : "8-bit") + " " + ((float)mAudioTrack.getSampleRate() / 1000f) + "kHz, " + desiredFrames + " frames buffer");
-
-		buf = new short[desiredFrames * 2];
-		return buf;
-	}
-
-	public void audioStartThread() {
-		mAudioThread = new Thread(new Runnable() {
-			public void run() {
-				mAudioTrack.play();
-				GodotLib.audio();
-			}
-		});
-
-		// I'd take REALTIME if I could get it!
-		mAudioThread.setPriority(Thread.MAX_PRIORITY);
-		mAudioThread.start();
-	}
-
-	public void audioWriteShortBuffer(short[] buffer) {
-		for (int i = 0; i < buffer.length;) {
-			int result = mAudioTrack.write(buffer, i, buffer.length - i);
-			if (result > 0) {
-				i += result;
-			} else if (result == 0) {
-				try {
-					Thread.sleep(1);
-				} catch (InterruptedException e) {
-					// Nom nom
-				}
-			} else {
-				Log.w("Godot", "Godot audio: error return from write(short)");
-				return;
-			}
-		}
-	}
-
-	public void audioQuit() {
-		if (mAudioThread != null) {
-			try {
-				mAudioThread.join();
-			} catch (Exception e) {
-				Log.v("Godot", "Problem stopping audio thread: " + e);
-			}
-			mAudioThread = null;
-
-			//Log.v("Godot", "Finished waiting for audio thread");
-		}
-
-		if (mAudioTrack != null) {
-			mAudioTrack.stop();
-			mAudioTrack = null;
-		}
-	}
-
-	public void audioPause(boolean p_pause) {
-		if (p_pause)
-			mAudioTrack.pause();
-		else
-			mAudioTrack.play();
-	}
-
 	/////////////////////////
 	// MISCELLANEOUS OS IO
 	/////////////////////////
diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotLib.java b/platform/android/java/lib/src/org/godotengine/godot/GodotLib.java
index a700f7a116c..bd54ec7601b 100644
--- a/platform/android/java/lib/src/org/godotengine/godot/GodotLib.java
+++ b/platform/android/java/lib/src/org/godotengine/godot/GodotLib.java
@@ -172,11 +172,6 @@ public class GodotLib {
 	 */
 	public static native void focusout();
 
-	/**
-	 * Invoked when the audio thread is started.
-	 */
-	public static native void audio();
-
 	/**
 	 * Used to access Godot global properties.
 	 * @param p_key Property key
diff --git a/platform/android/java_godot_lib_jni.cpp b/platform/android/java_godot_lib_jni.cpp
index b9f4a2f3cbc..682020de8e2 100644
--- a/platform/android/java_godot_lib_jni.cpp
+++ b/platform/android/java_godot_lib_jni.cpp
@@ -36,7 +36,6 @@
 #include "android_keys_utils.h"
 #include "api/java_class_wrapper.h"
 #include "api/jni_singleton.h"
-#include "audio_driver_jandroid.h"
 #include "core/engine.h"
 #include "core/project_settings.h"
 #include "dir_access_jandroid.h"
@@ -136,7 +135,6 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *en
 	FileAccessAndroid::asset_manager = AAssetManager_fromJava(env, amgr);
 
 	DirAccessJAndroid::setup(godot_io_java->get_instance());
-	AudioDriverAndroid::setup(godot_io_java->get_instance());
 	NetSocketAndroid::setup(godot_java->get_member_object("netUtils", "Lorg/godotengine/godot/utils/GodotNetUtils;", env));
 
 	os_android = new OS_Android(godot_java, godot_io_java, p_use_apk_expansion);
@@ -413,11 +411,6 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusout(JNIEnv *env,
 	os_android->main_loop_focusout();
 }
 
-JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_audio(JNIEnv *env, jclass clazz) {
-	setup_android_thread();
-	AudioDriverAndroid::thread_func(env);
-}
-
 JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getGlobal(JNIEnv *env, jclass clazz, jstring path) {
 	String js = jstring_to_string(path, env);
 
diff --git a/platform/android/java_godot_lib_jni.h b/platform/android/java_godot_lib_jni.h
index 4b81e087985..e57134435ff 100644
--- a/platform/android/java_godot_lib_jni.h
+++ b/platform/android/java_godot_lib_jni.h
@@ -56,7 +56,6 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joybutton(JNIEnv *env
 JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyaxis(JNIEnv *env, jclass clazz, jint p_device, jint p_axis, jfloat p_value);
 JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, jclass clazz, jint p_device, jint p_hat_x, jint p_hat_y);
 JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jclass clazz, jint p_device, jboolean p_connected, jstring p_name);
-JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_audio(JNIEnv *env, jclass clazz);
 JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z);
 JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gravity(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z);
 JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z);
diff --git a/platform/android/os_android.h b/platform/android/os_android.h
index 301317612c8..bdd53e066d9 100644
--- a/platform/android/os_android.h
+++ b/platform/android/os_android.h
@@ -31,13 +31,11 @@
 #ifndef OS_ANDROID_H
 #define OS_ANDROID_H
 
-#include "audio_driver_jandroid.h"
 #include "audio_driver_opensl.h"
 #include "core/os/input.h"
 #include "core/os/main_loop.h"
 #include "drivers/unix/os_unix.h"
 #include "main/input_default.h"
-//#include "power_android.h"
 #include "servers/audio_server.h"
 #include "servers/visual/rasterizer.h"
 
@@ -80,7 +78,6 @@ private:
 
 	mutable String data_dir_cache;
 
-	//AudioDriverAndroid audio_driver_android;
 	AudioDriverOpenSL audio_driver_android;
 
 	const char *gl_extensions;