The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1303 lines
51KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  20. DECLARE_JNI_CLASS (AndroidAudioManager, "android/media/AudioManager")
  21. #undef JNI_CLASS_MEMBERS
  22. //==============================================================================
  23. #ifndef SL_ANDROID_DATAFORMAT_PCM_EX
  24. #define SL_ANDROID_DATAFORMAT_PCM_EX ((SLuint32) 0x00000004)
  25. #endif
  26. #ifndef SL_ANDROID_PCM_REPRESENTATION_FLOAT
  27. #define SL_ANDROID_PCM_REPRESENTATION_FLOAT ((SLuint32) 0x00000003)
  28. #endif
  29. #ifndef SL_ANDROID_RECORDING_PRESET_UNPROCESSED
  30. #define SL_ANDROID_RECORDING_PRESET_UNPROCESSED ((SLuint32) 0x00000005)
  31. #endif
  32. //==============================================================================
  33. struct PCMDataFormatEx : SLDataFormat_PCM
  34. {
  35. SLuint32 representation;
  36. };
  37. //==============================================================================
  38. template <typename T> struct IntfIID;
  39. template <> struct IntfIID<SLObjectItf_> { static SLInterfaceID_ iid; };
  40. template <> struct IntfIID<SLEngineItf_> { static SLInterfaceID_ iid; };
  41. template <> struct IntfIID<SLOutputMixItf_> { static SLInterfaceID_ iid; };
  42. template <> struct IntfIID<SLPlayItf_> { static SLInterfaceID_ iid; };
  43. template <> struct IntfIID<SLRecordItf_> { static SLInterfaceID_ iid; };
  44. template <> struct IntfIID<SLAndroidSimpleBufferQueueItf_> { static SLInterfaceID_ iid; };
  45. template <> struct IntfIID<SLAndroidConfigurationItf_> { static SLInterfaceID_ iid; };
  46. SLInterfaceID_ IntfIID<SLObjectItf_>::iid = { 0x79216360, 0xddd7, 0x11db, 0xac16, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b} };
  47. SLInterfaceID_ IntfIID<SLEngineItf_>::iid = { 0x8d97c260, 0xddd4, 0x11db, 0x958f, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b} };
  48. SLInterfaceID_ IntfIID<SLOutputMixItf_>::iid = { 0x97750f60, 0xddd7, 0x11db, 0x92b1, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b} };
  49. SLInterfaceID_ IntfIID<SLPlayItf_>::iid = { 0xef0bd9c0, 0xddd7, 0x11db, 0xbf49, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b} };
  50. SLInterfaceID_ IntfIID<SLRecordItf_>::iid = { 0xc5657aa0, 0xdddb, 0x11db, 0x82f7, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b} };
  51. SLInterfaceID_ IntfIID<SLAndroidSimpleBufferQueueItf_>::iid = { 0x198e4940, 0xc5d7, 0x11df, 0xa2a6, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b} };
  52. SLInterfaceID_ IntfIID<SLAndroidConfigurationItf_>::iid = { 0x89f6a7e0, 0xbeac, 0x11df, 0x8b5c, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b} };
  53. template <typename SLObjectType>
  54. static void destroyObject (SLObjectType object)
  55. {
  56. if (object != nullptr && *object != nullptr)
  57. (*object)->Destroy (object);
  58. }
  59. template <>
  60. struct ContainerDeletePolicy<const SLObjectItf_* const>
  61. {
  62. static void destroy (SLObjectItf object)
  63. {
  64. destroyObject (object);
  65. }
  66. };
  67. //==============================================================================
  68. // Some life-time and type management of OpenSL objects
  69. class SlObjectRef
  70. {
  71. public:
  72. //==============================================================================
  73. SlObjectRef() noexcept {}
  74. SlObjectRef (const SlObjectRef& obj) noexcept : cb (obj.cb) {}
  75. SlObjectRef (SlObjectRef&& obj) noexcept : cb (std::move (obj.cb)) { obj.cb = nullptr; }
  76. explicit SlObjectRef (SLObjectItf o) : cb (new ControlBlock (o)) {}
  77. //==============================================================================
  78. SlObjectRef& operator= (const SlObjectRef& r) noexcept { cb = r.cb; return *this; }
  79. SlObjectRef& operator= (SlObjectRef&& r) noexcept { cb = std::move (r.cb); r.cb = nullptr; return *this; }
  80. SlObjectRef& operator= (std::nullptr_t) noexcept { cb = nullptr; return *this; }
  81. //==============================================================================
  82. const SLObjectItf_* operator*() noexcept { return *cb->ptr.get(); }
  83. SLObjectItf operator->() noexcept { return (cb == nullptr ? nullptr : cb->ptr.get()); }
  84. operator SLObjectItf() noexcept { return (cb == nullptr ? nullptr : cb->ptr.get()); }
  85. //==============================================================================
  86. bool operator== (nullptr_t) const noexcept { return (cb == nullptr || cb->ptr == nullptr); }
  87. bool operator!= (nullptr_t) const noexcept { return (cb != nullptr && cb->ptr != nullptr); }
  88. private:
  89. //==============================================================================
  90. struct ControlBlock : ReferenceCountedObject
  91. {
  92. ControlBlock() = default;
  93. ControlBlock (SLObjectItf o) : ptr (o) {}
  94. std::unique_ptr<const SLObjectItf_* const> ptr;
  95. };
  96. ReferenceCountedObjectPtr<ControlBlock> cb;
  97. };
  98. template <typename T>
  99. class SlRef : public SlObjectRef
  100. {
  101. public:
  102. //==============================================================================
  103. SlRef() noexcept {}
  104. SlRef (const SlRef& r) noexcept : SlObjectRef (r), type (r.type) {}
  105. SlRef (SlRef&& r) noexcept : SlObjectRef (std::move (r)), type (r.type) { r.type = nullptr; }
  106. //==============================================================================
  107. SlRef& operator= (const SlRef& r) noexcept { SlObjectRef::operator= (r); type = r.type; return *this; }
  108. SlRef& operator= (SlRef&& r) noexcept { SlObjectRef::operator= (std::move (r)); type = r.type; r.type = nullptr; return *this; }
  109. SlRef& operator= (std::nullptr_t) noexcept { SlObjectRef::operator= (nullptr); type = nullptr; return *this; }
  110. //==============================================================================
  111. T* const operator*() noexcept { return *type; }
  112. T* const* operator->() noexcept { return type; }
  113. operator T* const*() noexcept { return type; }
  114. //==============================================================================
  115. static SlRef cast (SlObjectRef& base) { return SlRef (base); }
  116. static SlRef cast (SlObjectRef&& base) { return SlRef (std::move (base)); }
  117. private:
  118. SlRef (SlObjectRef& base) : SlObjectRef (base)
  119. {
  120. if (auto obj = SlObjectRef::operator->())
  121. {
  122. auto err = (*obj)->GetInterface (obj, &IntfIID<T>::iid, &type);
  123. if (type != nullptr && err == SL_RESULT_SUCCESS)
  124. return;
  125. }
  126. *this = nullptr;
  127. }
  128. SlRef (SlObjectRef&& base) : SlObjectRef (std::move (base))
  129. {
  130. if (auto obj = SlObjectRef::operator->())
  131. {
  132. auto err = (*obj)->GetInterface (obj, &IntfIID<T>::iid, &type);
  133. base = nullptr;
  134. if (type != nullptr && err == SL_RESULT_SUCCESS)
  135. return;
  136. }
  137. *this = nullptr;
  138. }
  139. T* const* type = nullptr;
  140. };
  141. //==============================================================================
  142. template <typename T> struct BufferHelpers {};
  143. template <>
  144. struct BufferHelpers<int16>
  145. {
  146. enum { isFloatingPoint = 0 };
  147. static void initPCMDataFormat (PCMDataFormatEx& dataFormat, int numChannels, double sampleRate)
  148. {
  149. dataFormat.formatType = SL_DATAFORMAT_PCM;
  150. dataFormat.numChannels = (SLuint32) numChannels;
  151. dataFormat.samplesPerSec = (SLuint32) (sampleRate * 1000);
  152. dataFormat.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
  153. dataFormat.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
  154. dataFormat.channelMask = (numChannels == 1) ? SL_SPEAKER_FRONT_CENTER :
  155. (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT);
  156. dataFormat.endianness = SL_BYTEORDER_LITTLEENDIAN;
  157. dataFormat.representation = 0;
  158. }
  159. static void prepareCallbackBuffer (AudioBuffer<float>&, int16*) {}
  160. static void convertFromOpenSL (const int16* srcInterleaved, AudioBuffer<float>& audioBuffer)
  161. {
  162. for (int i = 0; i < audioBuffer.getNumChannels(); ++i)
  163. {
  164. using DstSampleType = AudioData::Pointer<AudioData::Float32, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::NonConst>;
  165. using SrcSampleType = AudioData::Pointer<AudioData::Int16, AudioData::LittleEndian, AudioData::Interleaved, AudioData::Const>;
  166. DstSampleType dstData (audioBuffer.getWritePointer (i));
  167. SrcSampleType srcData (srcInterleaved + i, audioBuffer.getNumChannels());
  168. dstData.convertSamples (srcData, audioBuffer.getNumSamples());
  169. }
  170. }
  171. static void convertToOpenSL (const AudioBuffer<float>& audioBuffer, int16* dstInterleaved)
  172. {
  173. for (int i = 0; i < audioBuffer.getNumChannels(); ++i)
  174. {
  175. using DstSampleType = AudioData::Pointer<AudioData::Int16, AudioData::LittleEndian, AudioData::Interleaved, AudioData::NonConst>;
  176. using SrcSampleType = AudioData::Pointer<AudioData::Float32, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::Const>;
  177. DstSampleType dstData (dstInterleaved + i, audioBuffer.getNumChannels());
  178. SrcSampleType srcData (audioBuffer.getReadPointer (i));
  179. dstData.convertSamples (srcData, audioBuffer.getNumSamples());
  180. }
  181. }
  182. };
  183. template <>
  184. struct BufferHelpers<float>
  185. {
  186. enum { isFloatingPoint = 1 };
  187. static void initPCMDataFormat (PCMDataFormatEx& dataFormat, int numChannels, double sampleRate)
  188. {
  189. dataFormat.formatType = SL_ANDROID_DATAFORMAT_PCM_EX;
  190. dataFormat.numChannels = (SLuint32) numChannels;
  191. dataFormat.samplesPerSec = (SLuint32) (sampleRate * 1000);
  192. dataFormat.bitsPerSample = 32;
  193. dataFormat.containerSize = 32;
  194. dataFormat.channelMask = (numChannels == 1) ? SL_SPEAKER_FRONT_CENTER :
  195. (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT);
  196. dataFormat.endianness = SL_BYTEORDER_LITTLEENDIAN;
  197. dataFormat.representation = SL_ANDROID_PCM_REPRESENTATION_FLOAT;
  198. }
  199. static void prepareCallbackBuffer (AudioBuffer<float>& audioBuffer, float* native)
  200. {
  201. if (audioBuffer.getNumChannels() == 1)
  202. audioBuffer.setDataToReferTo (&native, 1, audioBuffer.getNumSamples());
  203. }
  204. static void convertFromOpenSL (const float* srcInterleaved, AudioBuffer<float>& audioBuffer)
  205. {
  206. if (audioBuffer.getNumChannels() == 1)
  207. {
  208. jassert (srcInterleaved == audioBuffer.getWritePointer (0));
  209. return;
  210. }
  211. for (int i = 0; i < audioBuffer.getNumChannels(); ++i)
  212. {
  213. using DstSampleType = AudioData::Pointer<AudioData::Float32, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::NonConst>;
  214. using SrcSampleType = AudioData::Pointer<AudioData::Float32, AudioData::LittleEndian, AudioData::Interleaved, AudioData::Const>;
  215. DstSampleType dstData (audioBuffer.getWritePointer (i));
  216. SrcSampleType srcData (srcInterleaved + i, audioBuffer.getNumChannels());
  217. dstData.convertSamples (srcData, audioBuffer.getNumSamples());
  218. }
  219. }
  220. static void convertToOpenSL (const AudioBuffer<float>& audioBuffer, float* dstInterleaved)
  221. {
  222. if (audioBuffer.getNumChannels() == 1)
  223. {
  224. jassert (dstInterleaved == audioBuffer.getReadPointer (0));
  225. return;
  226. }
  227. for (int i = 0; i < audioBuffer.getNumChannels(); ++i)
  228. {
  229. using DstSampleType = AudioData::Pointer<AudioData::Float32, AudioData::LittleEndian, AudioData::Interleaved, AudioData::NonConst>;
  230. using SrcSampleType = AudioData::Pointer<AudioData::Float32, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::Const>;
  231. DstSampleType dstData (dstInterleaved + i, audioBuffer.getNumChannels());
  232. SrcSampleType srcData (audioBuffer.getReadPointer (i));
  233. dstData.convertSamples (srcData, audioBuffer.getNumSamples());
  234. }
  235. }
  236. };
  237. //==============================================================================
  238. using CreateEngineFunc = SLresult (*) (SLObjectItf*, SLuint32, const SLEngineOption*,
  239. SLuint32, const SLInterfaceID*, const SLboolean*);
  240. struct OpenSLEngineHolder
  241. {
  242. OpenSLEngineHolder()
  243. {
  244. if (auto createEngine = (CreateEngineFunc) slLibrary.getFunction ("slCreateEngine"))
  245. {
  246. SLObjectItf obj = nullptr;
  247. auto err = createEngine (&obj, 0, nullptr, 0, nullptr, nullptr);
  248. if (err != SL_RESULT_SUCCESS || obj == nullptr || *obj == nullptr
  249. || (*obj)->Realize (obj, 0) != SL_RESULT_SUCCESS)
  250. {
  251. destroyObject (obj);
  252. }
  253. engine = SlRef<SLEngineItf_>::cast (SlObjectRef (obj));
  254. }
  255. }
  256. DynamicLibrary slLibrary { "libOpenSLES.so" };
  257. SlRef<SLEngineItf_> engine;
  258. };
  259. OpenSLEngineHolder& getEngineHolder()
  260. {
  261. static OpenSLEngineHolder holder;
  262. return holder;
  263. }
  264. //==============================================================================
  265. class SLRealtimeThread;
  266. //==============================================================================
  267. class OpenSLAudioIODevice : public AudioIODevice
  268. {
  269. public:
  270. //==============================================================================
  271. template <typename T>
  272. class OpenSLSessionT;
  273. //==============================================================================
  274. // CRTP
  275. template <typename T, class Child, typename RunnerObjectType>
  276. struct OpenSLQueueRunner
  277. {
  278. OpenSLQueueRunner (OpenSLSessionT<T>& sessionToUse, int numChannelsToUse)
  279. : owner (sessionToUse),
  280. numChannels (numChannelsToUse),
  281. nativeBuffer (static_cast<size_t> (numChannels * owner.bufferSize * owner.numBuffers)),
  282. scratchBuffer (numChannelsToUse, owner.bufferSize),
  283. sampleBuffer (scratchBuffer.getArrayOfWritePointers(), numChannelsToUse, owner.bufferSize)
  284. {}
  285. ~OpenSLQueueRunner()
  286. {
  287. if (config != nullptr && javaProxy != nullptr)
  288. {
  289. javaProxy.clear();
  290. (*config)->ReleaseJavaProxy (config, /*SL_ANDROID_JAVA_PROXY_ROUTING*/1);
  291. }
  292. }
  293. bool init()
  294. {
  295. runner = crtp().createPlayerOrRecorder();
  296. if (runner == nullptr)
  297. return false;
  298. const bool supportsJavaProxy = (getAndroidSDKVersion() >= 24);
  299. if (supportsJavaProxy)
  300. {
  301. // may return nullptr on some platforms - that's ok
  302. config = SlRef<SLAndroidConfigurationItf_>::cast (runner);
  303. if (config != nullptr)
  304. {
  305. jobject audioRoutingJni;
  306. auto status = (*config)->AcquireJavaProxy (config, /*SL_ANDROID_JAVA_PROXY_ROUTING*/1,
  307. &audioRoutingJni);
  308. if (status == SL_RESULT_SUCCESS && audioRoutingJni != nullptr)
  309. javaProxy = GlobalRef (LocalRef<jobject>(getEnv()->NewLocalRef (audioRoutingJni)));
  310. }
  311. }
  312. queue = SlRef<SLAndroidSimpleBufferQueueItf_>::cast (runner);
  313. if (queue == nullptr)
  314. return false;
  315. return ((*queue)->RegisterCallback (queue, staticFinished, this) == SL_RESULT_SUCCESS);
  316. }
  317. void clear()
  318. {
  319. nextBlock.set (0);
  320. numBlocksOut.set (0);
  321. zeromem (nativeBuffer.get(), static_cast<size_t> (owner.bufferSize * numChannels * owner.numBuffers) * sizeof (T));
  322. scratchBuffer.clear();
  323. (*queue)->Clear (queue);
  324. }
  325. void enqueueBuffer()
  326. {
  327. (*queue)->Enqueue (queue, getCurrentBuffer(), static_cast<SLuint32> (getBufferSizeInSamples() * sizeof (T)));
  328. ++numBlocksOut;
  329. }
  330. bool isBufferAvailable() const { return (numBlocksOut.get() < owner.numBuffers); }
  331. T* getNextBuffer() { nextBlock.set((nextBlock.get() + 1) % owner.numBuffers); return getCurrentBuffer(); }
  332. T* getCurrentBuffer() { return nativeBuffer.get() + (static_cast<size_t> (nextBlock.get()) * getBufferSizeInSamples()); }
  333. size_t getBufferSizeInSamples() const { return static_cast<size_t> (owner.bufferSize * numChannels); }
  334. void finished (SLAndroidSimpleBufferQueueItf)
  335. {
  336. --numBlocksOut;
  337. owner.doSomeWorkOnAudioThread();
  338. }
  339. static void staticFinished (SLAndroidSimpleBufferQueueItf caller, void *pContext)
  340. {
  341. reinterpret_cast<OpenSLQueueRunner*> (pContext)->finished (caller);
  342. }
  343. // get the "this" pointer for CRTP
  344. Child& crtp() { return * ((Child*) this); }
  345. const Child& crtp() const { return * ((Child*) this); }
  346. OpenSLSessionT<T>& owner;
  347. SlRef<RunnerObjectType> runner;
  348. SlRef<SLAndroidSimpleBufferQueueItf_> queue;
  349. SlRef<SLAndroidConfigurationItf_> config;
  350. GlobalRef javaProxy;
  351. int numChannels;
  352. HeapBlock<T> nativeBuffer;
  353. AudioBuffer<float> scratchBuffer, sampleBuffer;
  354. Atomic<int> nextBlock { 0 }, numBlocksOut { 0 };
  355. };
  356. //==============================================================================
  357. template <typename T>
  358. struct OpenSLQueueRunnerPlayer : OpenSLQueueRunner<T, OpenSLQueueRunnerPlayer<T>, SLPlayItf_>
  359. {
  360. using Base = OpenSLQueueRunner<T, OpenSLQueueRunnerPlayer<T>, SLPlayItf_>;
  361. OpenSLQueueRunnerPlayer (OpenSLSessionT<T>& sessionToUse, int numChannelsToUse)
  362. : Base (sessionToUse, numChannelsToUse)
  363. {}
  364. SlRef<SLPlayItf_> createPlayerOrRecorder()
  365. {
  366. SLDataLocator_AndroidSimpleBufferQueue queueLocator = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, static_cast<SLuint32> (Base::owner.numBuffers) };
  367. SLDataLocator_OutputMix outputMix = { SL_DATALOCATOR_OUTPUTMIX, Base::owner.outputMix };
  368. PCMDataFormatEx dataFormat;
  369. BufferHelpers<T>::initPCMDataFormat (dataFormat, Base::numChannels, Base::owner.sampleRate);
  370. SLDataSource source = { &queueLocator, &dataFormat };
  371. SLDataSink sink = { &outputMix, nullptr };
  372. SLInterfaceID queueInterfaces[] = { &IntfIID<SLAndroidSimpleBufferQueueItf_>::iid, &IntfIID<SLAndroidConfigurationItf_>::iid };
  373. SLboolean interfaceRequired[] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_FALSE};
  374. SLObjectItf obj = nullptr;
  375. auto& holder = getEngineHolder();
  376. if (auto e = *holder.engine)
  377. {
  378. auto status = e->CreateAudioPlayer (holder.engine, &obj, &source, &sink, 2,
  379. queueInterfaces, interfaceRequired);
  380. if (status != SL_RESULT_SUCCESS || obj == nullptr || (*obj)->Realize(obj, 0) != SL_RESULT_SUCCESS)
  381. {
  382. destroyObject (obj);
  383. return {};
  384. }
  385. }
  386. return SlRef<SLPlayItf_>::cast (SlObjectRef (obj));
  387. }
  388. void setState (bool running) { (*Base::runner)->SetPlayState (Base::runner, running ? SL_PLAYSTATE_PLAYING : SL_PLAYSTATE_STOPPED); }
  389. };
  390. template <typename T>
  391. struct OpenSLQueueRunnerRecorder : public OpenSLQueueRunner<T, OpenSLQueueRunnerRecorder<T>, SLRecordItf_>
  392. {
  393. using Base = OpenSLQueueRunner<T, OpenSLQueueRunnerRecorder<T>, SLRecordItf_>;
  394. OpenSLQueueRunnerRecorder (OpenSLSessionT<T>& sessionToUse, int numChannelsToUse)
  395. : Base (sessionToUse, numChannelsToUse)
  396. {}
  397. SlRef<SLRecordItf_> createPlayerOrRecorder()
  398. {
  399. SLDataLocator_IODevice ioDeviceLocator = { SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT, SL_DEFAULTDEVICEID_AUDIOINPUT, nullptr };
  400. SLDataLocator_AndroidSimpleBufferQueue queueLocator = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, static_cast<SLuint32> (Base::owner.numBuffers) };
  401. PCMDataFormatEx dataFormat;
  402. BufferHelpers<T>::initPCMDataFormat (dataFormat, Base::numChannels, Base::owner.sampleRate);
  403. SLDataSource source = { &ioDeviceLocator, nullptr };
  404. SLDataSink sink = { &queueLocator, &dataFormat };
  405. SLInterfaceID queueInterfaces[] = { &IntfIID<SLAndroidSimpleBufferQueueItf_>::iid, &IntfIID<SLAndroidConfigurationItf_>::iid };
  406. SLboolean interfaceRequired[] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_FALSE };
  407. SLObjectItf obj = nullptr;
  408. auto& holder = getEngineHolder();
  409. if (auto e = *holder.engine)
  410. {
  411. auto status = e->CreateAudioRecorder (holder.engine, &obj, &source, &sink, 2, queueInterfaces, interfaceRequired);
  412. if (status != SL_RESULT_SUCCESS || obj == nullptr || (*obj)->Realize (obj, 0) != SL_RESULT_SUCCESS)
  413. {
  414. destroyObject (obj);
  415. return {};
  416. }
  417. }
  418. return SlRef<SLRecordItf_>::cast (SlObjectRef (obj));
  419. }
  420. bool setAudioPreprocessingEnabled (bool shouldEnable)
  421. {
  422. if (Base::config != nullptr)
  423. {
  424. const bool supportsUnprocessed = (getAndroidSDKVersion() >= 25);
  425. const SLuint32 recordingPresetValue
  426. = (shouldEnable ? SL_ANDROID_RECORDING_PRESET_GENERIC
  427. : (supportsUnprocessed ? SL_ANDROID_RECORDING_PRESET_UNPROCESSED
  428. : SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION));
  429. auto status = (*Base::config)->SetConfiguration (Base::config, SL_ANDROID_KEY_RECORDING_PRESET,
  430. &recordingPresetValue, sizeof (recordingPresetValue));
  431. return (status == SL_RESULT_SUCCESS);
  432. }
  433. return false;
  434. }
  435. void setState (bool running) { (*Base::runner)->SetRecordState (Base::runner, running ? SL_RECORDSTATE_RECORDING
  436. : SL_RECORDSTATE_STOPPED); }
  437. };
  438. //==============================================================================
  439. class OpenSLSession
  440. {
  441. public:
  442. OpenSLSession (int numInputChannels, int numOutputChannels,
  443. double samleRateToUse, int bufferSizeToUse,
  444. int numBuffersToUse)
  445. : inputChannels (numInputChannels), outputChannels (numOutputChannels),
  446. sampleRate (samleRateToUse), bufferSize (bufferSizeToUse), numBuffers (numBuffersToUse)
  447. {
  448. jassert (numInputChannels > 0 || numOutputChannels > 0);
  449. if (outputChannels > 0)
  450. {
  451. auto& holder = getEngineHolder();
  452. SLObjectItf obj = nullptr;
  453. auto err = (*holder.engine)->CreateOutputMix (holder.engine, &obj, 0, nullptr, nullptr);
  454. if (err != SL_RESULT_SUCCESS || obj == nullptr || *obj == nullptr
  455. || (*obj)->Realize (obj, 0) != SL_RESULT_SUCCESS)
  456. {
  457. destroyObject (obj);
  458. return;
  459. }
  460. outputMix = SlRef<SLOutputMixItf_>::cast (SlObjectRef (obj));
  461. }
  462. }
  463. virtual ~OpenSLSession() {}
  464. virtual bool openedOK() const { return (outputChannels == 0 || outputMix != nullptr); }
  465. virtual void start() { stop(); jassert (callback.get() != nullptr); running = true; }
  466. virtual void stop() { running = false; }
  467. virtual bool setAudioPreprocessingEnabled (bool shouldEnable) = 0;
  468. virtual bool supportsFloatingPoint() const noexcept = 0;
  469. virtual int getXRunCount() const noexcept = 0;
  470. void setCallback (AudioIODeviceCallback* callbackToUse)
  471. {
  472. if (! running)
  473. {
  474. callback.set (callbackToUse);
  475. return;
  476. }
  477. // don't set callback to null! stop the playback instead!
  478. jassert (callbackToUse != nullptr);
  479. // spin-lock until we can set the callback
  480. for (;;)
  481. {
  482. auto old = callback.get();
  483. if (old == callbackToUse)
  484. break;
  485. if (callback.compareAndSetBool (callbackToUse, old))
  486. break;
  487. Thread::sleep (1);
  488. }
  489. }
  490. void process (const float** inputChannelData, float** outputChannelData)
  491. {
  492. if (auto* cb = callback.exchange (nullptr))
  493. {
  494. cb->audioDeviceIOCallback (inputChannelData, inputChannels, outputChannelData, outputChannels, bufferSize);
  495. callback.set (cb);
  496. }
  497. else
  498. {
  499. for (int i = 0; i < outputChannels; ++i)
  500. zeromem (outputChannelData[i], sizeof(float) * static_cast<size_t> (bufferSize));
  501. }
  502. }
  503. static OpenSLSession* create (int numInputChannels, int numOutputChannels,
  504. double samleRateToUse, int bufferSizeToUse,
  505. int numBuffersToUse);
  506. //==============================================================================
  507. int inputChannels, outputChannels;
  508. double sampleRate;
  509. int bufferSize, numBuffers;
  510. bool running = false, audioProcessingEnabled = true;
  511. SlRef<SLOutputMixItf_> outputMix;
  512. Atomic<AudioIODeviceCallback*> callback { nullptr };
  513. };
  514. template <typename T>
  515. class OpenSLSessionT : public OpenSLSession
  516. {
  517. public:
  518. OpenSLSessionT (int numInputChannels, int numOutputChannels,
  519. double samleRateToUse, int bufferSizeToUse,
  520. int numBuffersToUse)
  521. : OpenSLSession (numInputChannels, numOutputChannels,
  522. samleRateToUse, bufferSizeToUse, numBuffersToUse)
  523. {
  524. jassert (numInputChannels > 0 || numOutputChannels > 0);
  525. if (OpenSLSession::openedOK())
  526. {
  527. if (inputChannels > 0)
  528. {
  529. recorder.reset (new OpenSLQueueRunnerRecorder<T> (*this, inputChannels));
  530. if (! recorder->init())
  531. {
  532. recorder = nullptr;
  533. return;
  534. }
  535. }
  536. if (outputChannels > 0)
  537. {
  538. player.reset (new OpenSLQueueRunnerPlayer<T> (*this, outputChannels));
  539. if (! player->init())
  540. {
  541. player = nullptr;
  542. return;
  543. }
  544. const bool supportsUnderrunCount = (getAndroidSDKVersion() >= 24);
  545. getUnderrunCount = supportsUnderrunCount ? getEnv()->GetMethodID (AudioTrack, "getUnderrunCount", "()I") : nullptr;
  546. }
  547. }
  548. }
  549. bool openedOK() const override
  550. {
  551. return OpenSLSession::openedOK() && (inputChannels == 0 || recorder != nullptr)
  552. && (outputChannels == 0 || player != nullptr);
  553. }
  554. void start() override
  555. {
  556. OpenSLSession::start();
  557. guard.set (0);
  558. if (inputChannels > 0)
  559. recorder->clear();
  560. if (outputChannels > 0)
  561. player->clear();
  562. // first enqueue all buffers
  563. for (int i = 0; i < numBuffers; ++i)
  564. doSomeWorkOnAudioThread();
  565. if (inputChannels > 0)
  566. recorder->setState (true);
  567. if (outputChannels > 0)
  568. player->setState (true);
  569. }
  570. void stop() override
  571. {
  572. OpenSLSession::stop();
  573. while (! guard.compareAndSetBool (1, 0))
  574. Thread::sleep (1);
  575. if (inputChannels > 0)
  576. recorder->setState (false);
  577. if (outputChannels > 0)
  578. player->setState (false);
  579. guard.set (0);
  580. }
  581. bool setAudioPreprocessingEnabled (bool shouldEnable) override
  582. {
  583. if (shouldEnable != audioProcessingEnabled)
  584. {
  585. audioProcessingEnabled = shouldEnable;
  586. if (recorder != nullptr)
  587. return recorder->setAudioPreprocessingEnabled (audioProcessingEnabled);
  588. }
  589. return true;
  590. }
  591. int getXRunCount() const noexcept override
  592. {
  593. if (player != nullptr && player->javaProxy != nullptr && getUnderrunCount != nullptr)
  594. return getEnv()->CallIntMethod (player->javaProxy, getUnderrunCount);
  595. return -1;
  596. }
  597. bool supportsFloatingPoint() const noexcept override { return (BufferHelpers<T>::isFloatingPoint != 0); }
  598. void doSomeWorkOnAudioThread()
  599. {
  600. // only the player or the recorder should enter this section at any time
  601. if (guard.compareAndSetBool (1, 0))
  602. {
  603. // are there enough buffers available to process some audio
  604. if ((inputChannels == 0 || recorder->isBufferAvailable()) && (outputChannels == 0 || player->isBufferAvailable()))
  605. {
  606. T* recorderBuffer = (inputChannels > 0 ? recorder->getNextBuffer() : nullptr);
  607. T* playerBuffer = (outputChannels > 0 ? player->getNextBuffer() : nullptr);
  608. const float** inputChannelData = nullptr;
  609. float** outputChannelData = nullptr;
  610. if (recorderBuffer != nullptr)
  611. {
  612. BufferHelpers<T>::prepareCallbackBuffer (recorder->sampleBuffer, recorderBuffer);
  613. BufferHelpers<T>::convertFromOpenSL (recorderBuffer, recorder->sampleBuffer);
  614. inputChannelData = recorder->sampleBuffer.getArrayOfReadPointers();
  615. }
  616. if (playerBuffer != nullptr)
  617. {
  618. BufferHelpers<T>::prepareCallbackBuffer (player->sampleBuffer, playerBuffer);
  619. outputChannelData = player->sampleBuffer.getArrayOfWritePointers();
  620. }
  621. process (inputChannelData, outputChannelData);
  622. if (recorderBuffer != nullptr)
  623. recorder->enqueueBuffer();
  624. if (playerBuffer != nullptr)
  625. {
  626. BufferHelpers<T>::convertToOpenSL (player->sampleBuffer, playerBuffer);
  627. player->enqueueBuffer();
  628. }
  629. }
  630. guard.set (0);
  631. }
  632. }
  633. //==============================================================================
  634. std::unique_ptr<OpenSLQueueRunnerPlayer<T>> player;
  635. std::unique_ptr<OpenSLQueueRunnerRecorder<T>> recorder;
  636. Atomic<int> guard;
  637. jmethodID getUnderrunCount = nullptr;
  638. };
  639. //==============================================================================
  640. OpenSLAudioIODevice (const String& deviceName) : AudioIODevice (deviceName, openSLTypeName)
  641. {
  642. // OpenSL has piss-poor support for determining latency, so the only way I can find to
  643. // get a number for this is by asking the AudioTrack/AudioRecord classes..
  644. AndroidAudioIODevice javaDevice (deviceName);
  645. // this is a total guess about how to calculate the latency, but seems to vaguely agree
  646. // with the devices I've tested.. YMMV
  647. inputLatency = (javaDevice.minBufferSizeIn * 2) / 3;
  648. outputLatency = (javaDevice.minBufferSizeOut * 2) / 3;
  649. const int64 longestLatency = jmax (inputLatency, outputLatency);
  650. const int64 totalLatency = inputLatency + outputLatency;
  651. inputLatency = (int) ((longestLatency * inputLatency) / totalLatency) & ~15;
  652. outputLatency = (int) ((longestLatency * outputLatency) / totalLatency) & ~15;
  653. // You can only create this class if you are sure that your hardware supports OpenSL
  654. jassert (getEngineHolder().slLibrary.getNativeHandle() != nullptr);
  655. }
  656. ~OpenSLAudioIODevice() override
  657. {
  658. close();
  659. }
  660. bool openedOk() const { return session != nullptr; }
  661. StringArray getOutputChannelNames() override
  662. {
  663. StringArray s;
  664. s.add ("Left");
  665. s.add ("Right");
  666. return s;
  667. }
  668. StringArray getInputChannelNames() override
  669. {
  670. StringArray s;
  671. s.add ("Audio Input");
  672. return s;
  673. }
  674. Array<double> getAvailableSampleRates() override
  675. {
  676. // see https://developer.android.com/ndk/guides/audio/opensl-for-android.html
  677. static const double rates[] = { 8000.0, 11025.0, 12000.0, 16000.0,
  678. 22050.0, 24000.0, 32000.0, 44100.0, 48000.0 };
  679. Array<double> retval (rates, numElementsInArray (rates));
  680. // make sure the native sample rate is part of the list
  681. double native = AndroidHighPerformanceAudioHelpers::getNativeSampleRate();
  682. if (native != 0.0 && ! retval.contains (native))
  683. retval.add (native);
  684. return retval;
  685. }
  686. Array<int> getAvailableBufferSizes() override
  687. {
  688. return AndroidHighPerformanceAudioHelpers::getAvailableBufferSizes (getAvailableSampleRates());
  689. }
  690. String open (const BigInteger& inputChannels,
  691. const BigInteger& outputChannels,
  692. double requestedSampleRate,
  693. int bufferSize) override
  694. {
  695. close();
  696. lastError.clear();
  697. sampleRate = (int) (requestedSampleRate > 0 ? requestedSampleRate : AndroidHighPerformanceAudioHelpers::getNativeSampleRate());
  698. auto preferredBufferSize = (bufferSize > 0) ? bufferSize : getDefaultBufferSize();
  699. audioBuffersToEnqueue = [this, preferredBufferSize]
  700. {
  701. if (AndroidHighPerformanceAudioHelpers::canUseHighPerformanceAudioPath (preferredBufferSize, sampleRate))
  702. return preferredBufferSize / AndroidHighPerformanceAudioHelpers::getNativeBufferSize();
  703. return 1;
  704. }();
  705. actualBufferSize = preferredBufferSize / audioBuffersToEnqueue;
  706. jassert ((actualBufferSize * audioBuffersToEnqueue) == preferredBufferSize);
  707. activeOutputChans = outputChannels;
  708. activeOutputChans.setRange (2, activeOutputChans.getHighestBit(), false);
  709. auto numOutputChannels = activeOutputChans.countNumberOfSetBits();
  710. activeInputChans = inputChannels;
  711. activeInputChans.setRange (1, activeInputChans.getHighestBit(), false);
  712. auto numInputChannels = activeInputChans.countNumberOfSetBits();
  713. if (numInputChannels > 0 && (! RuntimePermissions::isGranted (RuntimePermissions::recordAudio)))
  714. {
  715. // If you hit this assert, you probably forgot to get RuntimePermissions::recordAudio
  716. // before trying to open an audio input device. This is not going to work!
  717. jassertfalse;
  718. lastError = "Error opening OpenSL input device: the app was not granted android.permission.RECORD_AUDIO";
  719. }
  720. session.reset (OpenSLSession::create (numInputChannels, numOutputChannels,
  721. sampleRate, actualBufferSize, audioBuffersToEnqueue));
  722. if (session != nullptr)
  723. {
  724. session->setAudioPreprocessingEnabled (audioProcessingEnabled);
  725. }
  726. else
  727. {
  728. if (numInputChannels > 0 && numOutputChannels > 0 && RuntimePermissions::isGranted (RuntimePermissions::recordAudio))
  729. {
  730. // New versions of the Android emulator do not seem to support audio input anymore on OS X
  731. activeInputChans = BigInteger(0);
  732. numInputChannels = 0;
  733. session.reset (OpenSLSession::create (numInputChannels, numOutputChannels,
  734. sampleRate, actualBufferSize, audioBuffersToEnqueue));
  735. }
  736. }
  737. DBG ("OpenSL: numInputChannels = " << numInputChannels
  738. << ", numOutputChannels = " << numOutputChannels
  739. << ", nativeBufferSize = " << AndroidHighPerformanceAudioHelpers::getNativeBufferSize()
  740. << ", nativeSampleRate = " << AndroidHighPerformanceAudioHelpers::getNativeSampleRate()
  741. << ", actualBufferSize = " << actualBufferSize
  742. << ", audioBuffersToEnqueue = " << audioBuffersToEnqueue
  743. << ", sampleRate = " << sampleRate
  744. << ", supportsFloatingPoint = " << (session != nullptr && session->supportsFloatingPoint() ? "true" : "false"));
  745. if (session == nullptr)
  746. lastError = "Unknown error initializing opensl session";
  747. deviceOpen = (session != nullptr);
  748. return lastError;
  749. }
  750. void close() override
  751. {
  752. stop();
  753. session = nullptr;
  754. callback = nullptr;
  755. }
  756. int getOutputLatencyInSamples() override { return outputLatency; }
  757. int getInputLatencyInSamples() override { return inputLatency; }
  758. bool isOpen() override { return deviceOpen; }
  759. int getCurrentBufferSizeSamples() override { return actualBufferSize * audioBuffersToEnqueue; }
  760. int getCurrentBitDepth() override { return (session != nullptr && session->supportsFloatingPoint() ? 32 : 16); }
  761. BigInteger getActiveOutputChannels() const override { return activeOutputChans; }
  762. BigInteger getActiveInputChannels() const override { return activeInputChans; }
  763. String getLastError() override { return lastError; }
  764. bool isPlaying() override { return callback != nullptr; }
  765. int getXRunCount() const noexcept override { return (session != nullptr ? session->getXRunCount() : -1); }
  766. int getDefaultBufferSize() override
  767. {
  768. return AndroidHighPerformanceAudioHelpers::getDefaultBufferSize (getCurrentSampleRate());
  769. }
  770. double getCurrentSampleRate() override
  771. {
  772. return (sampleRate == 0.0 ? AndroidHighPerformanceAudioHelpers::getNativeSampleRate() : sampleRate);
  773. }
  774. void start (AudioIODeviceCallback* newCallback) override
  775. {
  776. if (session != nullptr && callback != newCallback)
  777. {
  778. auto oldCallback = callback;
  779. if (newCallback != nullptr)
  780. newCallback->audioDeviceAboutToStart (this);
  781. if (oldCallback != nullptr)
  782. {
  783. // already running
  784. if (newCallback == nullptr)
  785. stop();
  786. else
  787. session->setCallback (newCallback);
  788. oldCallback->audioDeviceStopped();
  789. }
  790. else
  791. {
  792. jassert (newCallback != nullptr);
  793. // session hasn't started yet
  794. session->setCallback (newCallback);
  795. session->start();
  796. }
  797. callback = newCallback;
  798. }
  799. }
  800. void stop() override
  801. {
  802. if (session != nullptr && callback != nullptr)
  803. {
  804. callback = nullptr;
  805. session->stop();
  806. session->setCallback (nullptr);
  807. }
  808. }
  809. bool setAudioPreprocessingEnabled (bool shouldAudioProcessingBeEnabled) override
  810. {
  811. audioProcessingEnabled = shouldAudioProcessingBeEnabled;
  812. if (session != nullptr)
  813. session->setAudioPreprocessingEnabled (audioProcessingEnabled);
  814. return true;
  815. }
  816. static const char* const openSLTypeName;
  817. private:
  818. //==============================================================================
  819. friend class SLRealtimeThread;
  820. //==============================================================================
  821. int actualBufferSize = 0, sampleRate = 0, audioBuffersToEnqueue = 0;
  822. int inputLatency, outputLatency;
  823. bool deviceOpen = false, audioProcessingEnabled = true;
  824. String lastError;
  825. BigInteger activeOutputChans, activeInputChans;
  826. AudioIODeviceCallback* callback = nullptr;
  827. std::unique_ptr<OpenSLSession> session;
  828. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenSLAudioIODevice)
  829. };
  830. OpenSLAudioIODevice::OpenSLSession* OpenSLAudioIODevice::OpenSLSession::create (int numInputChannels, int numOutputChannels,
  831. double samleRateToUse, int bufferSizeToUse,
  832. int numBuffersToUse)
  833. {
  834. std::unique_ptr<OpenSLSession> retval;
  835. auto sdkVersion = getAndroidSDKVersion();
  836. // SDK versions 21 and higher should natively support floating point...
  837. if (sdkVersion >= 21)
  838. {
  839. retval.reset (new OpenSLSessionT<float> (numInputChannels, numOutputChannels, samleRateToUse,
  840. bufferSizeToUse, numBuffersToUse));
  841. // ...however, some devices lie so re-try without floating point
  842. if (retval != nullptr && (! retval->openedOK()))
  843. retval = nullptr;
  844. }
  845. if (retval == nullptr)
  846. {
  847. retval.reset (new OpenSLSessionT<int16> (numInputChannels, numOutputChannels, samleRateToUse,
  848. bufferSizeToUse, numBuffersToUse));
  849. if (retval != nullptr && (! retval->openedOK()))
  850. retval = nullptr;
  851. }
  852. return retval.release();
  853. }
  854. //==============================================================================
  855. class OpenSLAudioDeviceType : public AudioIODeviceType
  856. {
  857. public:
  858. OpenSLAudioDeviceType() : AudioIODeviceType (OpenSLAudioIODevice::openSLTypeName) {}
  859. //==============================================================================
  860. void scanForDevices() override {}
  861. StringArray getDeviceNames (bool) const override { return StringArray (OpenSLAudioIODevice::openSLTypeName); }
  862. int getDefaultDeviceIndex (bool) const override { return 0; }
  863. int getIndexOfDevice (AudioIODevice* device, bool) const override { return device != nullptr ? 0 : -1; }
  864. bool hasSeparateInputsAndOutputs() const override { return false; }
  865. AudioIODevice* createDevice (const String& outputDeviceName,
  866. const String& inputDeviceName) override
  867. {
  868. std::unique_ptr<OpenSLAudioIODevice> dev;
  869. if (outputDeviceName.isNotEmpty() || inputDeviceName.isNotEmpty())
  870. dev.reset (new OpenSLAudioIODevice (outputDeviceName.isNotEmpty() ? outputDeviceName
  871. : inputDeviceName));
  872. return dev.release();
  873. }
  874. static bool isOpenSLAvailable()
  875. {
  876. DynamicLibrary library;
  877. return library.open ("libOpenSLES.so");
  878. }
  879. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenSLAudioDeviceType)
  880. };
  881. const char* const OpenSLAudioIODevice::openSLTypeName = "Android OpenSL";
  882. //==============================================================================
  883. bool isOpenSLAvailable() { return OpenSLAudioDeviceType::isOpenSLAvailable(); }
  884. AudioIODeviceType* AudioIODeviceType::createAudioIODeviceType_OpenSLES()
  885. {
  886. return isOpenSLAvailable() ? new OpenSLAudioDeviceType() : nullptr;
  887. }
  888. //==============================================================================
  889. class SLRealtimeThread
  890. {
  891. public:
  892. static constexpr int numBuffers = 4;
  893. SLRealtimeThread()
  894. {
  895. if (auto createEngine = (CreateEngineFunc) slLibrary.getFunction ("slCreateEngine"))
  896. {
  897. SLObjectItf obj = nullptr;
  898. auto err = createEngine (&obj, 0, nullptr, 0, nullptr, nullptr);
  899. if (err != SL_RESULT_SUCCESS || obj == nullptr || *obj == nullptr)
  900. return;
  901. if ((*obj)->Realize (obj, 0) != SL_RESULT_SUCCESS)
  902. {
  903. destroyObject (obj);
  904. return;
  905. }
  906. engine = SlRef<SLEngineItf_>::cast (SlObjectRef (obj));
  907. if (engine == nullptr)
  908. {
  909. destroyObject (obj);
  910. return;
  911. }
  912. obj = nullptr;
  913. err = (*engine)->CreateOutputMix (engine, &obj, 0, nullptr, nullptr);
  914. if (err != SL_RESULT_SUCCESS || obj == nullptr || (*obj)->Realize (obj, 0) != SL_RESULT_SUCCESS)
  915. {
  916. destroyObject (obj);
  917. return;
  918. }
  919. outputMix = SlRef<SLOutputMixItf_>::cast (SlObjectRef (obj));
  920. if (outputMix == nullptr)
  921. {
  922. destroyObject (obj);
  923. return;
  924. }
  925. SLDataLocator_AndroidSimpleBufferQueue queueLocator = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, static_cast<SLuint32> (numBuffers)};
  926. SLDataLocator_OutputMix outputMixLocator = {SL_DATALOCATOR_OUTPUTMIX, outputMix};
  927. PCMDataFormatEx dataFormat;
  928. BufferHelpers<int16>::initPCMDataFormat (dataFormat, 1, AndroidHighPerformanceAudioHelpers::getNativeSampleRate());
  929. SLDataSource source = { &queueLocator, &dataFormat };
  930. SLDataSink sink = { &outputMixLocator, nullptr };
  931. SLInterfaceID queueInterfaces[] = { &IntfIID<SLAndroidSimpleBufferQueueItf_>::iid };
  932. SLboolean trueFlag = SL_BOOLEAN_TRUE;
  933. obj = nullptr;
  934. err = (*engine)->CreateAudioPlayer (engine, &obj, &source, &sink, 1, queueInterfaces, &trueFlag);
  935. if (err != SL_RESULT_SUCCESS || obj == nullptr)
  936. return;
  937. if ((*obj)->Realize (obj, 0) != SL_RESULT_SUCCESS)
  938. {
  939. destroyObject (obj);
  940. return;
  941. }
  942. player = SlRef<SLPlayItf_>::cast (SlObjectRef (obj));
  943. if (player == nullptr)
  944. {
  945. destroyObject (obj);
  946. return;
  947. }
  948. queue = SlRef<SLAndroidSimpleBufferQueueItf_>::cast (player);
  949. if (queue == nullptr)
  950. return;
  951. if ((*queue)->RegisterCallback (queue, staticFinished, this) != SL_RESULT_SUCCESS)
  952. {
  953. queue = nullptr;
  954. return;
  955. }
  956. pthread_cond_init (&threadReady, nullptr);
  957. pthread_mutex_init (&threadReadyMutex, nullptr);
  958. }
  959. }
  960. bool isOk() const { return queue != nullptr; }
  961. pthread_t startThread (void* (*entry) (void*), void* userPtr)
  962. {
  963. memset (buffer.get(), 0, static_cast<size_t> (sizeof (int16) * static_cast<size_t> (bufferSize * numBuffers)));
  964. for (int i = 0; i < numBuffers; ++i)
  965. {
  966. int16* dst = buffer.get() + (bufferSize * i);
  967. (*queue)->Enqueue (queue, dst, static_cast<SLuint32> (static_cast<size_t> (bufferSize) * sizeof (int16)));
  968. }
  969. pthread_mutex_lock (&threadReadyMutex);
  970. threadEntryProc = entry;
  971. threadUserPtr = userPtr;
  972. (*player)->SetPlayState (player, SL_PLAYSTATE_PLAYING);
  973. pthread_cond_wait (&threadReady, &threadReadyMutex);
  974. pthread_mutex_unlock (&threadReadyMutex);
  975. return threadID;
  976. }
  977. void finished()
  978. {
  979. if (threadEntryProc != nullptr)
  980. {
  981. pthread_mutex_lock (&threadReadyMutex);
  982. threadID = pthread_self();
  983. pthread_cond_signal (&threadReady);
  984. pthread_mutex_unlock (&threadReadyMutex);
  985. threadEntryProc (threadUserPtr);
  986. threadEntryProc = nullptr;
  987. (*player)->SetPlayState (player, SL_PLAYSTATE_STOPPED);
  988. MessageManager::callAsync ([this] () { delete this; });
  989. }
  990. }
  991. private:
  992. //==============================================================================
  993. static void staticFinished (SLAndroidSimpleBufferQueueItf, void* context)
  994. {
  995. static_cast<SLRealtimeThread*> (context)->finished();
  996. }
  997. //==============================================================================
  998. DynamicLibrary slLibrary { "libOpenSLES.so" };
  999. SlRef<SLEngineItf_> engine;
  1000. SlRef<SLOutputMixItf_> outputMix;
  1001. SlRef<SLPlayItf_> player;
  1002. SlRef<SLAndroidSimpleBufferQueueItf_> queue;
  1003. int bufferSize = AndroidHighPerformanceAudioHelpers::getNativeBufferSize();
  1004. HeapBlock<int16> buffer { HeapBlock<int16> (static_cast<size_t> (1 * bufferSize * numBuffers)) };
  1005. void* (*threadEntryProc) (void*) = nullptr;
  1006. void* threadUserPtr = nullptr;
  1007. pthread_cond_t threadReady;
  1008. pthread_mutex_t threadReadyMutex;
  1009. pthread_t threadID;
  1010. };
  1011. //==============================================================================
  1012. pthread_t juce_createRealtimeAudioThread (void* (*entry) (void*), void* userPtr)
  1013. {
  1014. auto thread = std::make_unique<SLRealtimeThread>();
  1015. if (! thread->isOk())
  1016. return {};
  1017. auto threadID = thread->startThread (entry, userPtr);
  1018. // the thread will de-allocate itself
  1019. thread.release();
  1020. return threadID;
  1021. }
  1022. } // namespace juce