android.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2010 by Chris Robinson
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <jni.h>
  23. #include <pthread.h>
  24. #include "alMain.h"
  25. #include "AL/al.h"
  26. #include "AL/alc.h"
  27. static const ALCchar android_device[] = "Android Legacy";
  28. static JavaVM* javaVM = NULL;
  29. static jclass cAudioTrack = NULL;
  30. static jmethodID mAudioTrack;
  31. static jmethodID mGetMinBufferSize;
  32. static jmethodID mPlay;
  33. static jmethodID mStop;
  34. static jmethodID mRelease;
  35. static jmethodID mWrite;
  36. __attribute__((visibility("default"))) jint JNI_OnLoad(JavaVM* vm, void* reserved)
  37. {
  38. (void)reserved;
  39. javaVM = vm;
  40. return JNI_VERSION_1_2;
  41. }
  42. static JNIEnv* GetEnv()
  43. {
  44. JNIEnv* env = NULL;
  45. if (javaVM) (*javaVM)->GetEnv(javaVM, (void**)&env, JNI_VERSION_1_2);
  46. return env;
  47. }
  48. typedef struct
  49. {
  50. pthread_t thread;
  51. volatile int running;
  52. } AndroidData;
  53. #define STREAM_MUSIC 3
  54. #define CHANNEL_CONFIGURATION_MONO 2
  55. #define CHANNEL_CONFIGURATION_STEREO 3
  56. #define ENCODING_PCM_8BIT 3
  57. #define ENCODING_PCM_16BIT 2
  58. #define MODE_STREAM 1
  59. static void* thread_function(void* arg)
  60. {
  61. ALCdevice* device = (ALCdevice*)arg;
  62. AndroidData* data = (AndroidData*)device->ExtraData;
  63. JNIEnv* env;
  64. (*javaVM)->AttachCurrentThread(javaVM, &env, NULL);
  65. (*env)->PushLocalFrame(env, 2);
  66. int sampleRateInHz = device->Frequency;
  67. int channelConfig = ChannelsFromDevFmt(device->FmtChans) == 1 ? CHANNEL_CONFIGURATION_MONO : CHANNEL_CONFIGURATION_STEREO;
  68. int audioFormat = BytesFromDevFmt(device->FmtType) == 1 ? ENCODING_PCM_8BIT : ENCODING_PCM_16BIT;
  69. int bufferSizeInBytes = (*env)->CallStaticIntMethod(env, cAudioTrack,
  70. mGetMinBufferSize, sampleRateInHz, channelConfig, audioFormat);
  71. int bufferSizeInSamples = bufferSizeInBytes / FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  72. jobject track = (*env)->NewObject(env, cAudioTrack, mAudioTrack,
  73. STREAM_MUSIC, sampleRateInHz, channelConfig, audioFormat, device->NumUpdates * bufferSizeInBytes, MODE_STREAM);
  74. #ifdef HAVE_ANDROID_LOW_LATENCY
  75. int started = 0;
  76. size_t overallBytes = 0;
  77. #else
  78. (*env)->CallNonvirtualVoidMethod(env, track, cAudioTrack, mPlay);
  79. #endif
  80. jarray buffer = (*env)->NewByteArray(env, bufferSizeInBytes);
  81. while (data->running)
  82. {
  83. void* pBuffer = (*env)->GetPrimitiveArrayCritical(env, buffer, NULL);
  84. if (pBuffer)
  85. {
  86. aluMixData(device, pBuffer, bufferSizeInSamples);
  87. (*env)->ReleasePrimitiveArrayCritical(env, buffer, pBuffer, 0);
  88. #ifdef HAVE_ANDROID_LOW_LATENCY
  89. if (bufferSizeInBytes >= 0)
  90. {
  91. if (started)
  92. {
  93. #endif
  94. (*env)->CallNonvirtualIntMethod(env, track, cAudioTrack, mWrite, buffer, 0, bufferSizeInBytes);
  95. #ifdef HAVE_ANDROID_LOW_LATENCY
  96. }
  97. else
  98. {
  99. overallBytes += (*env)->CallNonvirtualIntMethod(env, track, cAudioTrack, mWrite, buffer, 0, bufferSizeInBytes);
  100. if (overallBytes >= (device->NumUpdates * bufferSizeInBytes))
  101. {
  102. (*env)->CallNonvirtualVoidMethod(env, track, cAudioTrack, mPlay);
  103. started = 1;
  104. }
  105. }
  106. }
  107. #endif
  108. }
  109. else
  110. {
  111. AL_PRINT("Failed to get pointer to array bytes");
  112. }
  113. }
  114. (*env)->CallNonvirtualVoidMethod(env, track, cAudioTrack, mStop);
  115. (*env)->CallNonvirtualVoidMethod(env, track, cAudioTrack, mRelease);
  116. (*env)->PopLocalFrame(env, NULL);
  117. (*javaVM)->DetachCurrentThread(javaVM);
  118. return NULL;
  119. }
  120. static ALCenum android_open_playback(ALCdevice *device, const ALCchar *deviceName)
  121. {
  122. JNIEnv* env = GetEnv();
  123. AndroidData* data;
  124. if (!cAudioTrack)
  125. {
  126. /* Cache AudioTrack class and it's method id's
  127. * And do this only once!
  128. */
  129. cAudioTrack = (*env)->FindClass(env, "android/media/AudioTrack");
  130. if (!cAudioTrack)
  131. {
  132. AL_PRINT("android.media.AudioTrack class is not found. Are you running at least 1.5 version?");
  133. return ALC_INVALID_VALUE;
  134. }
  135. cAudioTrack = (*env)->NewGlobalRef(env, cAudioTrack);
  136. mAudioTrack = (*env)->GetMethodID(env, cAudioTrack, "<init>", "(IIIIII)V");
  137. mGetMinBufferSize = (*env)->GetStaticMethodID(env, cAudioTrack, "getMinBufferSize", "(III)I");
  138. mPlay = (*env)->GetMethodID(env, cAudioTrack, "play", "()V");
  139. mStop = (*env)->GetMethodID(env, cAudioTrack, "stop", "()V");
  140. mRelease = (*env)->GetMethodID(env, cAudioTrack, "release", "()V");
  141. mWrite = (*env)->GetMethodID(env, cAudioTrack, "write", "([BII)I");
  142. }
  143. if (!deviceName)
  144. {
  145. deviceName = android_device;
  146. }
  147. else if (strcmp(deviceName, android_device) != 0)
  148. {
  149. return ALC_INVALID_VALUE;
  150. }
  151. data = (AndroidData*)calloc(1, sizeof(*data));
  152. device->szDeviceName = strdup(deviceName);
  153. device->ExtraData = data;
  154. device->FmtChans = DevFmtStereo;
  155. device->FmtType = DevFmtShort;
  156. #ifdef HAVE_ANDROID_LOW_LATENCY
  157. device->Frequency = 22050;
  158. device->NumUpdates = 1;
  159. #endif
  160. return ALC_NO_ERROR;
  161. }
  162. static void android_close_playback(ALCdevice *device)
  163. {
  164. AndroidData* data = (AndroidData*)device->ExtraData;
  165. if (data != NULL)
  166. {
  167. free(data);
  168. device->ExtraData = NULL;
  169. }
  170. }
  171. static ALCboolean android_reset_playback(ALCdevice *device)
  172. {
  173. AndroidData* data = (AndroidData*)device->ExtraData;
  174. device->FmtChans = ChannelsFromDevFmt(device->FmtChans) >= 2 ? DevFmtStereo : DevFmtMono;
  175. SetDefaultChannelOrder(device);
  176. return ALC_TRUE;
  177. }
  178. static ALCboolean android_start_playback(ALCdevice* device)
  179. {
  180. AndroidData* data = (AndroidData*)device->ExtraData;
  181. data->running = 1;
  182. pthread_create(&data->thread, NULL, thread_function, device);
  183. return ALC_TRUE;
  184. }
  185. static void android_stop_playback(ALCdevice *device)
  186. {
  187. AndroidData* data = (AndroidData*)device->ExtraData;
  188. if (data->running)
  189. {
  190. data->running = 0;
  191. pthread_join(data->thread, NULL);
  192. }
  193. }
  194. static const BackendFuncs android_funcs = {
  195. android_open_playback,
  196. android_close_playback,
  197. android_reset_playback,
  198. android_start_playback,
  199. android_stop_playback,
  200. NULL,
  201. NULL,
  202. NULL,
  203. NULL,
  204. NULL,
  205. NULL
  206. };
  207. ALCboolean alc_android_init(BackendFuncs *func_list)
  208. {
  209. *func_list = android_funcs;
  210. return ALC_TRUE;
  211. }
  212. void alc_android_deinit(void)
  213. {
  214. JNIEnv* env = GetEnv();
  215. /* release cached AudioTrack class */
  216. (*env)->DeleteGlobalRef(env, cAudioTrack);
  217. }
  218. void alc_android_probe(enum DevProbe type)
  219. {
  220. switch(type)
  221. {
  222. case ALL_DEVICE_PROBE:
  223. AppendAllDeviceList(android_device);
  224. break;
  225. case CAPTURE_DEVICE_PROBE:
  226. break;
  227. }
  228. }