Android audio device selection (#6824) Make it possible to select a specific audio device for Android
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDL.java b/android-project/app/src/main/java/org/libsdl/app/SDL.java
index dafc0cb..44c21c1 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDL.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDL.java
@@ -29,6 +29,7 @@ public class SDL {
// This function stores the current activity (SDL or not)
public static void setContext(Context context) {
+ SDLAudioManager.setContext(context);
mContext = context;
}
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
index e2748f2..535e163 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
@@ -393,7 +393,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
mHIDDeviceManager = HIDDeviceManager.acquire(this);
// Set up the surface
- mSurface = createSDLSurface(getApplication());
+ mSurface = createSDLSurface(this);
mLayout = new RelativeLayout(this);
mLayout.addView(mSurface);
@@ -588,6 +588,8 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
mHIDDeviceManager = null;
}
+ SDLAudioManager.release(this);
+
if (SDLActivity.mBrokenLibraries) {
super.onDestroy();
return;
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLAudioManager.java b/android-project/app/src/main/java/org/libsdl/app/SDLAudioManager.java
index 2bfc718..94aa49a 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLAudioManager.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLAudioManager.java
@@ -1,5 +1,8 @@
package org.libsdl.app;
+import android.content.Context;
+import android.media.AudioDeviceCallback;
+import android.media.AudioDeviceInfo;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
@@ -8,34 +11,59 @@ import android.media.MediaRecorder;
import android.os.Build;
import android.util.Log;
-public class SDLAudioManager
-{
+import java.util.Arrays;
+
+public class SDLAudioManager {
protected static final String TAG = "SDLAudio";
protected static AudioTrack mAudioTrack;
protected static AudioRecord mAudioRecord;
+ protected static Context mContext;
+
+ private static final AudioDeviceCallback mAudioDeviceCallback = new AudioDeviceCallback() {
+ @Override
+ public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
+ Arrays.stream(addedDevices).forEach(deviceInfo -> addAudioDevice(deviceInfo.isSink(), deviceInfo.getId()));
+ }
+
+ @Override
+ public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
+ Arrays.stream(removedDevices).forEach(deviceInfo -> removeAudioDevice(deviceInfo.isSink(), deviceInfo.getId()));
+ }
+ };
public static void initialize() {
mAudioTrack = null;
mAudioRecord = null;
}
+ public static void setContext(Context context) {
+ mContext = context;
+ if (context != null) {
+ registerAudioDeviceCallback();
+ }
+ }
+
+ public static void release(Context context) {
+ unregisterAudioDeviceCallback(context);
+ }
+
// Audio
protected static String getAudioFormatString(int audioFormat) {
switch (audioFormat) {
- case AudioFormat.ENCODING_PCM_8BIT:
- return "8-bit";
- case AudioFormat.ENCODING_PCM_16BIT:
- return "16-bit";
- case AudioFormat.ENCODING_PCM_FLOAT:
- return "float";
- default:
- return Integer.toString(audioFormat);
+ case AudioFormat.ENCODING_PCM_8BIT:
+ return "8-bit";
+ case AudioFormat.ENCODING_PCM_16BIT:
+ return "16-bit";
+ case AudioFormat.ENCODING_PCM_FLOAT:
+ return "float";
+ default:
+ return Integer.toString(audioFormat);
}
}
- protected static int[] open(boolean isCapture, int sampleRate, int audioFormat, int desiredChannels, int desiredFrames) {
+ protected static int[] open(boolean isCapture, int sampleRate, int audioFormat, int desiredChannels, int desiredFrames, int deviceId) {
int channelConfig;
int sampleSize;
int frameSize;
@@ -201,6 +229,10 @@ public class SDLAudioManager
return null;
}
+ if (deviceId != 0) {
+ mAudioRecord.setPreferredDevice(getOutputAudioDeviceInfo(deviceId));
+ }
+
mAudioRecord.startRecording();
}
@@ -224,6 +256,10 @@ public class SDLAudioManager
return null;
}
+ if (deviceId != 0) {
+ mAudioTrack.setPreferredDevice(getInputAudioDeviceInfo(deviceId));
+ }
+
mAudioTrack.play();
}
@@ -238,11 +274,53 @@ public class SDLAudioManager
return results;
}
+ private static AudioDeviceInfo getInputAudioDeviceInfo(int deviceId) {
+ AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+ return Arrays.stream(audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS))
+ .filter(deviceInfo -> deviceInfo.getId() == deviceId)
+ .findFirst()
+ .orElse(null);
+ }
+
+ private static AudioDeviceInfo getOutputAudioDeviceInfo(int deviceId) {
+ AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+ return Arrays.stream(audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS))
+ .filter(deviceInfo -> deviceInfo.getId() == deviceId)
+ .findFirst()
+ .orElse(null);
+ }
+
+ private static void registerAudioDeviceCallback() {
+ AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+ audioManager.registerAudioDeviceCallback(mAudioDeviceCallback, null);
+ }
+
+ private static void unregisterAudioDeviceCallback(Context context) {
+ AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+ audioManager.unregisterAudioDeviceCallback(mAudioDeviceCallback);
+ }
+
+ /**
+ * This method is called by SDL using JNI.
+ */
+ public static int[] getAudioOutputDevices() {
+ AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+ return Arrays.stream(audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)).mapToInt(AudioDeviceInfo::getId).toArray();
+ }
+
+ /**
+ * This method is called by SDL using JNI.
+ */
+ public static int[] getAudioInputDevices() {
+ AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+ return Arrays.stream(audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)).mapToInt(AudioDeviceInfo::getId).toArray();
+ }
+
/**
* This method is called by SDL using JNI.
*/
- public static int[] audioOpen(int sampleRate, int audioFormat, int desiredChannels, int desiredFrames) {
- return open(false, sampleRate, audioFormat, desiredChannels, desiredFrames);
+ public static int[] audioOpen(int sampleRate, int audioFormat, int desiredChannels, int desiredFrames, int deviceId) {
+ return open(false, sampleRate, audioFormat, desiredChannels, desiredFrames, deviceId);
}
/**
@@ -326,8 +404,8 @@ public class SDLAudioManager
/**
* This method is called by SDL using JNI.
*/
- public static int[] captureOpen(int sampleRate, int audioFormat, int desiredChannels, int desiredFrames) {
- return open(true, sampleRate, audioFormat, desiredChannels, desiredFrames);
+ public static int[] captureOpen(int sampleRate, int audioFormat, int desiredChannels, int desiredFrames, int deviceId) {
+ return open(true, sampleRate, audioFormat, desiredChannels, desiredFrames, deviceId);
}
/** This method is called by SDL using JNI. */
@@ -391,4 +469,9 @@ public class SDLAudioManager
}
public static native int nativeSetupJNI();
+
+ public static native void removeAudioDevice(boolean isCapture, int deviceId);
+
+ public static native void addAudioDevice(boolean isCapture, int deviceId);
+
}
diff --git a/include/SDL_surface.h b/include/SDL_surface.h
index 561b2be..838de65 100644
--- a/include/SDL_surface.h
+++ b/include/SDL_surface.h
@@ -683,7 +683,7 @@ extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface
* it might be easier to call but it doesn't have access to palette
* information for the destination surface, in case that would be important.
*
- * \param surface the existing SDL_Surface structure to convert
+ * \param src the existing SDL_Surface structure to convert
* \param pixel_format the SDL_PixelFormatEnum that the new surface is
* optimized for
* \param flags the flags are unused and should be set to 0; this is a
diff --git a/src/audio/aaudio/SDL_aaudio.c b/src/audio/aaudio/SDL_aaudio.c
index 92d2233..7b7e662 100644
--- a/src/audio/aaudio/SDL_aaudio.c
+++ b/src/audio/aaudio/SDL_aaudio.c
@@ -70,6 +70,45 @@ void aaudio_errorCallback(AAudioStream *stream, void *userData, aaudio_result_t
#define LIB_AAUDIO_SO "libaaudio.so"
+static void aaudio_DetectDevices(void)
+{
+ int *inputs;
+ inputs = SDL_malloc(sizeof(int) * 100);
+ SDL_zerop(inputs);
+ int inputs_length = 0;
+
+ Android_JNI_GetAudioInputDevices(inputs, &inputs_length);
+
+ for (int i = 0; i < inputs_length; ++i) {
+ int device_id = inputs[i];
+ int n = (int) (log10(device_id) + 1);
+ char device_name[n];
+ SDL_itoa(device_id, device_name, 10);
+ SDL_Log("Adding input device with name %s", device_name);
+ SDL_AddAudioDevice(SDL_FALSE, SDL_strdup(device_name), NULL, (void *) ((size_t) device_id + 1));
+ }
+
+ SDL_free(inputs);
+
+ int *outputs;
+ outputs = SDL_malloc(sizeof(int) * 100);
+ SDL_zerop(outputs);
+ int outputs_length = 0;
+
+ Android_JNI_GetAudioOutputDevices(outputs, &outputs_length);
+
+ for (int i = 0; i < outputs_length; ++i) {
+ int device_id = outputs[i];
+ int n = (int) (log10(device_id) + 1);
+ char device_name[n];
+ SDL_itoa(device_id, device_name, 10);
+ SDL_Log("Adding output device with name %s", device_name);
+ SDL_AddAudioDevice(SDL_TRUE, SDL_strdup(device_name), NULL, (void *) ((size_t) device_id + 1));
+ }
+
+ SDL_free(outputs);
+}
+
static int aaudio_OpenDevice(_THIS, const char *devname)
{
struct SDL_PrivateAudioData *private;
@@ -101,6 +140,11 @@ static int aaudio_OpenDevice(_THIS, const char *devname)
ctx.AAudioStreamBuilder_setSampleRate(ctx.builder, this->spec.freq);
ctx.AAudioStreamBuilder_setChannelCount(ctx.builder, this->spec.channels);
+ if(devname != NULL) {
+ int aaudio_device_id = SDL_atoi(devname);
+ LOGI("Opening device id %d", aaudio_device_id);
+ ctx.AAudioStreamBuilder_setDeviceId(ctx.builder, aaudio_device_id);
+ }
{
aaudio_direction_t direction = (iscapture ? AAUDIO_DIRECTION_INPUT : AAUDIO_DIRECTION_OUTPUT);
ctx.AAudioStreamBuilder_setDirection(ctx.builder, direction);
@@ -300,17 +344,19 @@ static SDL_bool aaudio_Init(SDL_AudioDriverImpl *impl)
goto failure;
}
+ impl->DetectDevices = aaudio_DetectDevices;
impl->Deinitialize = aaudio_Deinitialize;
impl->OpenDevice = aaudio_OpenDevice;
impl->CloseDevice = aaudio_CloseDevice;
impl->PlayDevice = aaudio_PlayDevice;
impl->GetDeviceBuf = aaudio_GetDeviceBuf;
impl->CaptureFromDevice = aaudio_CaptureFromDevice;
+ impl->AllowsArbitraryDeviceNames = SDL_TRUE;
/* and the capabilities */
impl->HasCaptureSupport = SDL_TRUE;
- impl->OnlyHasDefaultOutputDevice = SDL_TRUE;
- impl->OnlyHasDefaultCaptureDevice = SDL_TRUE;
+ impl->OnlyHasDefaultOutputDevice = SDL_FALSE;
+ impl->OnlyHasDefaultCaptureDevice = SDL_FALSE;
/* this audio target is available. */
LOGI("SDL aaudio_Init OK");
diff --git a/src/audio/aaudio/SDL_aaudiofuncs.h b/src/audio/aaudio/SDL_aaudiofuncs.h
index 57f84d4..79b24d2 100644
--- a/src/audio/aaudio/SDL_aaudiofuncs.h
+++ b/src/audio/aaudio/SDL_aaudiofuncs.h
@@ -24,7 +24,7 @@
SDL_PROC(const char *, AAudio_convertResultToText, (aaudio_result_t returnCode))
SDL_PROC(const char *, AAudio_convertStreamStateToText, (aaudio_stream_state_t state))
SDL_PROC(aaudio_result_t, AAudio_createStreamBuilder, (AAudioStreamBuilder * *builder))
-SDL_PROC_UNUSED(void, AAudioStreamBuilder_setDeviceId, (AAudioStreamBuilder * builder, int32_t deviceId))
+SDL_PROC(void, AAudioStreamBuilder_setDeviceId, (AAudioStreamBuilder * builder, int32_t deviceId))
SDL_PROC(void, AAudioStreamBuilder_setSampleRate, (AAudioStreamBuilder * builder, int32_t sampleRate))
SDL_PROC(void, AAudioStreamBuilder_setChannelCount, (AAudioStreamBuilder * builder, int32_t channelCount))
SDL_PROC_UNUSED(void, AAudioStreamBuilder_setSamplesPerFrame, (AAudioStreamBuilder * builder, int32_t samplesPerFrame))
diff --git a/src/audio/android/SDL_androidaudio.c b/src/audio/android/SDL_androidaudio.c
index 60779b0..3944e92 100644
--- a/src/audio/android/SDL_androidaudio.c
+++ b/src/audio/android/SDL_androidaudio.c
@@ -35,6 +35,44 @@
static SDL_AudioDevice *audioDevice = NULL;
static SDL_AudioDevice *captureDevice = NULL;
+static void ANDROIDAUDIO_DetectDevices(void) {
+ int *inputs;
+ inputs = SDL_malloc(sizeof(int) * 100);
+ SDL_zerop(inputs);
+ int inputs_length = 0;
+
+ Android_JNI_GetAudioInputDevices(inputs, &inputs_length);
+
+ for (int i = 0; i < inputs_length; ++i) {
+ int device_id = inputs[i];
+ int n = (int) (log10(device_id) + 1);
+ char device_name[n];
+ SDL_itoa(device_id, device_name, 10);
+ SDL_Log("Adding input device with name %s", device_name);
+ SDL_AddAudioDevice(SDL_FALSE, SDL_strdup(device_name), NULL, (void *) ((size_t) device_id + 1));
+ }
+
+ SDL_free(inputs);
+
+ int *outputs;
+ outputs = SDL_malloc(sizeof(int) * 100);
+ SDL_zerop(outputs);
+ int outputs_length = 0;
+
+ Android_JNI_GetAudioOutputDevices(outputs, &outputs_length);
+
+ for (int i = 0; i < outputs_length; ++i) {
+ int device_id = outputs[i];
+ int n = (int) (log10(device_id) + 1);
+ char device_name[n];
+ SDL_itoa(device_id, device_name, 10);
+ SDL_Log("Adding output device with name %s", device_name);
+ SDL_AddAudioDevice(SDL_TRUE, SDL_strdup(device_name), NULL, (void *) ((size_t) device_id + 1));
+ }
+
+ SDL_free(outputs);
+}
+
static int ANDROIDAUDIO_OpenDevice(_THIS, const char *devname)
{
SDL_AudioFormat test_format;
@@ -63,12 +101,18 @@ static int ANDROIDAUDIO_OpenDevice(_THIS, const char *devname)
}
}
+ int audio_device_id = 0;
+
+ if(devname != NULL) {
+ audio_device_id = SDL_atoi(devname);
+ }
+
if (!test_format) {
/* Didn't find a compatible format :( */
return SDL_SetError("%s: Unsupported audio format", "android");
}
- if (Android_JNI_OpenAudioDevice(iscapture, &this->spec) < 0) {
+ if (Android_JNI_OpenAudioDevice(iscapture, audio_device_id, &this->spec) < 0) {
return -1;
}
@@ -116,17 +160,19 @@ static void ANDROIDAUDIO_CloseDevice(_THIS)
static SDL_bool ANDROIDAUDIO_Init(SDL_AudioDriverImpl *impl)
{
/* Set the function pointers */
+ impl->DetectDevices = ANDROIDAUDIO_DetectDevices;
impl->OpenDevice = ANDROIDAUDIO_OpenDevice;
impl->PlayDevice = ANDROIDAUDIO_PlayDevice;
impl->GetDeviceBuf = ANDROIDAUDIO_GetDeviceBuf;
impl->CloseDevice = ANDROIDAUDIO_CloseDevice;
impl->CaptureFromDevice = ANDROIDAUDIO_CaptureFromDevice;
impl->FlushCapture = ANDROIDAUDIO_FlushCapture;
+ impl->AllowsArbitraryDeviceNames = SDL_TRUE;
/* and the capabilities */
impl->HasCaptureSupport = SDL_TRUE;
- impl->OnlyHasDefaultOutputDevice = SDL_TRUE;
- impl->OnlyHasDefaultCaptureDevice = SDL_TRUE;
+ impl->OnlyHasDefaultOutputDevice = SDL_FALSE;
+ impl->OnlyHasDefaultCaptureDevice = SDL_FALSE;
return SDL_TRUE; /* this audio target is available. */
}
diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c
index 7df8a69..ac8f9a3 100644
--- a/src/core/android/SDL_android.c
+++ b/src/core/android/SDL_android.c
@@ -221,8 +221,18 @@ static JNINativeMethod SDLInputConnection_tab[] = {
JNIEXPORT void JNICALL SDL_JAVA_AUDIO_INTERFACE(nativeSetupJNI)(
JNIEnv *env, jclass jcls);
+JNIEXPORT void JNICALL
+SDL_JAVA_AUDIO_INTERFACE(addAudioDevice)(JNIEnv *env, jclass jcls, jboolean is_capture,
+ jint device_id);
+
+JNIEXPORT void JNICALL
+SDL_JAVA_AUDIO_INTERFACE(removeAudioDevice)(JNIEnv *env, jclass jcls, jboolean is_capture,
+ jint device_id);
+
static JNINativeMethod SDLAudioManager_tab[] = {
- { "nativeSetupJNI", "()I", SDL_JAVA_AUDIO_INTERFACE(nativeSetupJNI) }
+ { "nativeSetupJNI", "()I", SDL_JAVA_AUDIO_INTERFACE(nativeSetupJNI) },
+ { "addAudioDevice", "(ZI)V", SDL_JAVA_AUDIO_INTERFACE(addAudioDevice) },
+ { "removeAudioDevice", "(ZI)V", SDL_JAVA_AUDIO_INTERFACE(removeAudioDevice) }
};
/* Java class SDLControllerManager */
@@ -330,6 +340,8 @@ static jmethodID midSupportsRelativeMouse;
static jclass mAudioManagerClass;
/* method signatures */
+static jmethodID midGetAudioOutputDevices;
+static jmethodID midGetAudioInputDevices;
static jmethodID midAudioOpen;
static jmethodID midAudioWriteByteBuffer;
static jmethodID midAudioWriteShortBuffer;
@@ -655,8 +667,14 @@ JNIEXPORT void JNICALL SDL_JAVA_AUDIO_INTERFACE(nativeSetupJNI)(JNIEnv *env, jcl
mAudioManagerClass = (jclass)((*env)->NewGlobalRef(env, cls));
+ midGetAudioOutputDevices = (*env)->GetStaticMethodID(env, mAudioManagerClass,
+ "getAudioOutputDevices",
+ "()[I");
+ midGetAudioInputDevices = (*env)->GetStaticMethodID(env, mAudioManagerClass,
+ "getAudioInputDevices",
+ "()[I");
midAudioOpen = (*env)->GetStaticMethodID(env, mAudioManagerClass,
- "audioOpen", "(IIII)[I");
+ "audioOpen", "(IIIII)[I");
midAudioWriteByteBuffer = (*env)->GetStaticMethodID(env, mAudioManagerClass,
"audioWriteByteBuffer", "([B)V");
midAudioWriteShortBuffer = (*env)->GetStaticMethodID(env, mAudioManagerClass,
@@ -666,7 +684,7 @@ JNIEXPORT void JNICALL SDL_JAVA_AUDIO_INTERFACE(nativeSetupJNI)(JNIEnv *env, jcl
midAudioClose = (*env)->GetStaticMethodID(env, mAudioManagerClass,
"audioClose", "()V");
midCaptureOpen = (*env)->GetStaticMethodID(env, mAudioManagerClass,
- "captureOpen", "(IIII)[I");
+ "captureOpen", "(IIIII)[I");
midCaptureReadByteBuffer = (*env)->GetStaticMethodID(env, mAudioManagerClass,
"captureReadByteBuffer", "([BZ)I");
midCaptureReadShortBuffer = (*env)->GetStaticMethodID(env, mAudioManagerClass,
@@ -678,9 +696,13 @@ JNIEXPORT void JNICALL SDL_JAVA_AUDIO_INTERFACE(nativeSetupJNI)(JNIEnv *env, jcl
midAudioSetThreadPriority = (*env)->GetStaticMethodID(env, mAudioManagerClass,
"audioSetThreadPriority", "(ZI)V");
- if (!midAudioOpen || !midAudioWriteByteBuffer || !midAudioWriteShortBuffer || !midAudioWriteFloatBuffer || !midAudioClose ||
- !midCaptureOpen || !midCaptureReadByteBuffer || !midCaptureReadShortBuffer || !midCaptureReadFloatBuffer || !midCaptureClose || !midAudioSetThreadPriority) {
- __android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLAudioManager.java?");
+ if (!midGetAudioOutputDevices || !midGetAudioInputDevices || !midAudioOpen ||
+ !midAudioWriteByteBuffer || !midAudioWriteShortBuffer || !midAudioWriteFloatBuffer ||
+ !midAudioClose ||
+ !midCaptureOpen || !midCaptureReadByteBuffer || !midCaptureReadShortBuffer ||
+ !midCaptureReadFloatBuffer || !midCaptureClose || !midAudioSetThreadPriority) {
+ __android_log_print(ANDROID_LOG_WARN, "SDL",
+ "Missing some Java callbacks, do you have the latest version of SDLAudioManager.java?");
}
checkJNIReady();
@@ -911,6 +933,26 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativePermissionResult)(
SDL_AtomicSet(&bPermissionRequestPending, SDL_FALSE);
}
+extern void SDL_AddAudioDevice(const SDL_bool iscapture, const char *name, SDL_AudioSpec *spec, void *handle);
+extern void SDL_RemoveAudioDevice(const SDL_bool iscapture, void *handle);
+
+JNIEXPORT void JNICALL
+SDL_JAVA_AUDIO_INTERFACE(addAudioDevice)(JNIEnv *env, jclass jcls, jboolean is_capture,
+ jint device_id) {
+ int n = (int) (log10(device_id) + 1);
+ char device_name[n];
+ SDL_itoa(device_id, device_name, 10);
+ SDL_Log("Adding device with name %s, capture %d", device_name, is_capture);
+ SDL_AddAudioDevice(is_capture, SDL_strdup(device_name), NULL, (void *) ((size_t) device_id + 1));
+}
+
+JNIEXPORT void JNICALL
+SDL_JAVA_AUDIO_INTERFACE(removeAudioDevice)(JNIEnv *env, jclass jcls, jboolean is_capture,
+ jint device_id) {
+ SDL_Log("Removing device with handle %d, capture %d", device_id + 1, is_capture);
+ SDL_RemoveAudioDevice(is_capture, (void *) ((size_t) device_id + 1));
+}
+
/* Paddown */
JNIEXPORT jint JNICALL SDL_JAVA_CONTROLLER_INTERFACE(onNativePadDown)(
JNIEnv *env, jclass jcls,
@@ -1429,7 +1471,29 @@ static void *audioBufferPinned = NULL;
static int captureBufferFormat = 0;
static jobject captureBuffer = NULL;
-int Android_JNI_OpenAudioDevice(int iscapture, SDL_AudioSpec *spec)
+void Android_JNI_GetAudioOutputDevices(int *devices, int *length) {
+ JNIEnv *env = Android_JNI_GetEnv();
+ jintArray result;
+
+ result = (*env)->CallStaticObjectMethod(env, mAudioManagerClass, midGetAudioOutputDevices);
+
+ *length = (*env)->GetArrayLength(env, result);
+
+ (*env)->GetIntArrayRegion(env, result, 0, *length, devices);
+}
+
+void Android_JNI_GetAudioInputDevices(int * devices, int *length) {
+ JNIEnv *env = Android_JNI_GetEnv();
+ jintArray result;
+
+ result = (*env)->CallStaticObjectMethod(env, mAudioManagerClass, midGetAudioInputDevices);
+
+ *length = (*env)->GetArrayLength(env, result);
+
+ (*env)->GetIntArrayRegion(env, result, 0, *length, devices);
+}
+
+int Android_JNI_OpenAudioDevice(int iscapture, int device_id, SDL_AudioSpec *spec)
{
int audioformat;
jobject jbufobj = NULL;
@@ -1455,10 +1519,10 @@ int Android_JNI_OpenAudioDevice(int iscapture, SDL_AudioSpec *spec)
if (iscapture) {
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device for capture");
- result = (*env)->CallStaticObjectMethod(env, mAudioManagerClass, midCaptureOpen, spec->freq, audioformat, spec->channels, spec->samples);
+ result = (*env)->CallStaticObjectMethod(env, mAudioManagerClass, midCaptureOpen, spec->freq, audioformat, spec->channels, spec->samples, device_id);
} else {
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device for output");
- result = (*env)->CallStaticObjectMethod(env, mAudioManagerClass, midAudioOpen, spec->freq, audioformat, spec->channels, spec->samples);
+ result = (*env)->CallStaticObjectMethod(env, mAudioManagerClass, midAudioOpen, spec->freq, audioformat, spec->channels, spec->samples, device_id);
}
if (result == NULL) {
/* Error during audio initialization, error printed from Java */
diff --git a/src/core/android/SDL_android.h b/src/core/android/SDL_android.h
index 62ae653..a102712 100644
--- a/src/core/android/SDL_android.h
+++ b/src/core/android/SDL_android.h
@@ -52,7 +52,9 @@ extern SDL_DisplayOrientation Android_JNI_GetDisplayOrientation(void);
extern int Android_JNI_GetDisplayDPI(float *ddpi, float *xdpi, float *ydpi);
/* Audio support */
-extern int Android_JNI_OpenAudioDevice(int iscapture, SDL_AudioSpec *spec);
+extern void Android_JNI_GetAudioOutputDevices(int* devices, int *length);
+extern void Android_JNI_GetAudioInputDevices(int* devices, int *length);
+extern int Android_JNI_OpenAudioDevice(int iscapture, int device_id, SDL_AudioSpec *spec);
extern void *Android_JNI_GetAudioBuffer(void);
extern void Android_JNI_WriteAudioBuffer(void);
extern int Android_JNI_CaptureAudioBuffer(void *buffer, int buflen);