src/core/android/SDL_android.c
changeset 12224 0582b4e1fa9f
parent 12201 8bdc4d340419
child 12242 df7260f149f2
equal deleted inserted replaced
12223:90162273424f 12224:0582b4e1fa9f
  1044 static jboolean captureBuffer16Bit = JNI_FALSE;
  1044 static jboolean captureBuffer16Bit = JNI_FALSE;
  1045 static jobject captureBuffer = NULL;
  1045 static jobject captureBuffer = NULL;
  1046 
  1046 
  1047 int Android_JNI_OpenAudioDevice(int iscapture, int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
  1047 int Android_JNI_OpenAudioDevice(int iscapture, int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
  1048 {
  1048 {
  1049     jboolean audioBufferStereo;
  1049     jboolean isStereo;
  1050     int audioBufferFrames;
  1050     int numBufferFrames;
  1051     jobject jbufobj = NULL;
  1051     jobject jbufobj = NULL;
  1052     jboolean isCopy;
  1052     jboolean isCopy;
  1053 
  1053 
  1054     JNIEnv *env = Android_JNI_GetEnv();
  1054     JNIEnv *env = Android_JNI_GetEnv();
  1055 
  1055 
  1056     if (!env) {
  1056     if (!env) {
  1057         LOGE("callback_handler: failed to attach current thread");
  1057         LOGE("callback_handler: failed to attach current thread");
  1058     }
  1058     }
  1059     Android_JNI_SetupThread();
  1059     Android_JNI_SetupThread();
  1060 
  1060 
  1061     audioBufferStereo = channelCount > 1;
  1061     isStereo = channelCount > 1;
  1062 
  1062 
  1063     if (iscapture) {
  1063     if (iscapture) {
  1064         __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device for capture");
  1064         __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device for capture");
  1065         captureBuffer16Bit = is16Bit;
  1065         captureBuffer16Bit = is16Bit;
  1066         if ((*env)->CallStaticIntMethod(env, mAudioManagerClass, midCaptureOpen, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames) != 0) {
  1066         if ((*env)->CallStaticIntMethod(env, mAudioManagerClass, midCaptureOpen, sampleRate, is16Bit, isStereo, desiredBufferFrames) != 0) {
  1067             /* Error during audio initialization */
  1067             /* Error during audio initialization */
  1068             __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: error on AudioRecord initialization!");
  1068             __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: error on AudioRecord initialization!");
  1069             return 0;
  1069             return 0;
  1070         }
  1070         }
  1071     } else {
  1071     } else {
  1072         __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device for output");
  1072         __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device for output");
  1073         audioBuffer16Bit = is16Bit;
  1073         audioBuffer16Bit = is16Bit;
  1074         if ((*env)->CallStaticIntMethod(env, mAudioManagerClass, midAudioOpen, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames) != 0) {
  1074         if ((*env)->CallStaticIntMethod(env, mAudioManagerClass, midAudioOpen, sampleRate, is16Bit, isStereo, desiredBufferFrames) != 0) {
  1075             /* Error during audio initialization */
  1075             /* Error during audio initialization */
  1076             __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: error on AudioTrack initialization!");
  1076             __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: error on AudioTrack initialization!");
  1077             return 0;
  1077             return 0;
  1078         }
  1078         }
  1079     }
  1079     }
  1080 
  1080 
  1081     /* Allocating the audio buffer from the Java side and passing it as the return value for audioInit no longer works on
  1081     /* Allocating the audio buffer from the Java side and passing it as the return value for audioInit no longer works on
  1082      * Android >= 4.2 due to a "stale global reference" error. So now we allocate this buffer directly from this side. */
  1082      * Android >= 4.2 due to a "stale global reference" error. So now we allocate this buffer directly from this side. */
  1083 
  1083 
  1084     if (is16Bit) {
  1084     if (is16Bit) {
  1085         jshortArray audioBufferLocal = (*env)->NewShortArray(env, desiredBufferFrames * (audioBufferStereo ? 2 : 1));
  1085         jshortArray audioBufferLocal = (*env)->NewShortArray(env, desiredBufferFrames * (isStereo ? 2 : 1));
  1086         if (audioBufferLocal) {
  1086         if (audioBufferLocal) {
  1087             jbufobj = (*env)->NewGlobalRef(env, audioBufferLocal);
  1087             jbufobj = (*env)->NewGlobalRef(env, audioBufferLocal);
  1088             (*env)->DeleteLocalRef(env, audioBufferLocal);
  1088             (*env)->DeleteLocalRef(env, audioBufferLocal);
  1089         }
  1089         }
  1090     }
  1090     }
  1091     else {
  1091     else {
  1092         jbyteArray audioBufferLocal = (*env)->NewByteArray(env, desiredBufferFrames * (audioBufferStereo ? 2 : 1));
  1092         jbyteArray audioBufferLocal = (*env)->NewByteArray(env, desiredBufferFrames * (isStereo ? 2 : 1));
  1093         if (audioBufferLocal) {
  1093         if (audioBufferLocal) {
  1094             jbufobj = (*env)->NewGlobalRef(env, audioBufferLocal);
  1094             jbufobj = (*env)->NewGlobalRef(env, audioBufferLocal);
  1095             (*env)->DeleteLocalRef(env, audioBufferLocal);
  1095             (*env)->DeleteLocalRef(env, audioBufferLocal);
  1096         }
  1096         }
  1097     }
  1097     }
  1108     }
  1108     }
  1109 
  1109 
  1110     isCopy = JNI_FALSE;
  1110     isCopy = JNI_FALSE;
  1111 
  1111 
  1112     if (is16Bit) {
  1112     if (is16Bit) {
  1113         if (!iscapture) {
  1113         if (iscapture) {
       
  1114             numBufferFrames = (*env)->GetArrayLength(env, (jshortArray)captureBuffer);
       
  1115         } else {
  1114             audioBufferPinned = (*env)->GetShortArrayElements(env, (jshortArray)audioBuffer, &isCopy);
  1116             audioBufferPinned = (*env)->GetShortArrayElements(env, (jshortArray)audioBuffer, &isCopy);
  1115         }
  1117             numBufferFrames = (*env)->GetArrayLength(env, (jshortArray)audioBuffer);
  1116         audioBufferFrames = (*env)->GetArrayLength(env, (jshortArray)audioBuffer);
  1118         }
  1117     } else {
  1119     } else {
  1118         if (!iscapture) {
  1120         if (iscapture) {
       
  1121             numBufferFrames = (*env)->GetArrayLength(env, (jbyteArray)captureBuffer);
       
  1122         } else {
  1119             audioBufferPinned = (*env)->GetByteArrayElements(env, (jbyteArray)audioBuffer, &isCopy);
  1123             audioBufferPinned = (*env)->GetByteArrayElements(env, (jbyteArray)audioBuffer, &isCopy);
  1120         }
  1124             numBufferFrames = (*env)->GetArrayLength(env, (jbyteArray)audioBuffer);
  1121         audioBufferFrames = (*env)->GetArrayLength(env, (jbyteArray)audioBuffer);
  1125         }
  1122     }
  1126     }
  1123 
  1127 
  1124     if (audioBufferStereo) {
  1128     if (isStereo) {
  1125         audioBufferFrames /= 2;
  1129         numBufferFrames /= 2;
  1126     }
  1130     }
  1127 
  1131 
  1128     return audioBufferFrames;
  1132     return numBufferFrames;
  1129 }
  1133 }
  1130 
  1134 
  1131 int Android_JNI_GetDisplayDPI(float *ddpi, float *xdpi, float *ydpi)
  1135 int Android_JNI_GetDisplayDPI(float *ddpi, float *xdpi, float *ydpi)
  1132 {
  1136 {
  1133     JNIEnv *env = Android_JNI_GetEnv();
  1137     JNIEnv *env = Android_JNI_GetEnv();