Audio plugin host https://kx.studio/carla
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.

1310 lines
51KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 (AndroidHighPerformanceAudioHelpers::getNativeBufferSizeHint(),
  689. getAvailableSampleRates());
  690. }
  691. String open (const BigInteger& inputChannels,
  692. const BigInteger& outputChannels,
  693. double requestedSampleRate,
  694. int bufferSize) override
  695. {
  696. close();
  697. lastError.clear();
  698. sampleRate = (int) (requestedSampleRate > 0 ? requestedSampleRate : AndroidHighPerformanceAudioHelpers::getNativeSampleRate());
  699. auto preferredBufferSize = (bufferSize > 0) ? bufferSize : getDefaultBufferSize();
  700. audioBuffersToEnqueue = [this, preferredBufferSize]
  701. {
  702. using namespace AndroidHighPerformanceAudioHelpers;
  703. auto nativeBufferSize = getNativeBufferSizeHint();
  704. if (canUseHighPerformanceAudioPath (nativeBufferSize, preferredBufferSize, sampleRate))
  705. return preferredBufferSize / nativeBufferSize;
  706. return 1;
  707. }();
  708. actualBufferSize = preferredBufferSize / audioBuffersToEnqueue;
  709. jassert ((actualBufferSize * audioBuffersToEnqueue) == preferredBufferSize);
  710. activeOutputChans = outputChannels;
  711. activeOutputChans.setRange (2, activeOutputChans.getHighestBit(), false);
  712. auto numOutputChannels = activeOutputChans.countNumberOfSetBits();
  713. activeInputChans = inputChannels;
  714. activeInputChans.setRange (1, activeInputChans.getHighestBit(), false);
  715. auto numInputChannels = activeInputChans.countNumberOfSetBits();
  716. if (numInputChannels > 0 && (! RuntimePermissions::isGranted (RuntimePermissions::recordAudio)))
  717. {
  718. // If you hit this assert, you probably forgot to get RuntimePermissions::recordAudio
  719. // before trying to open an audio input device. This is not going to work!
  720. jassertfalse;
  721. lastError = "Error opening OpenSL input device: the app was not granted android.permission.RECORD_AUDIO";
  722. }
  723. session.reset (OpenSLSession::create (numInputChannels, numOutputChannels,
  724. sampleRate, actualBufferSize, audioBuffersToEnqueue));
  725. if (session != nullptr)
  726. {
  727. session->setAudioPreprocessingEnabled (audioProcessingEnabled);
  728. }
  729. else
  730. {
  731. if (numInputChannels > 0 && numOutputChannels > 0 && RuntimePermissions::isGranted (RuntimePermissions::recordAudio))
  732. {
  733. // New versions of the Android emulator do not seem to support audio input anymore on OS X
  734. activeInputChans = BigInteger(0);
  735. numInputChannels = 0;
  736. session.reset (OpenSLSession::create (numInputChannels, numOutputChannels,
  737. sampleRate, actualBufferSize, audioBuffersToEnqueue));
  738. }
  739. }
  740. DBG ("OpenSL: numInputChannels = " << numInputChannels
  741. << ", numOutputChannels = " << numOutputChannels
  742. << ", nativeBufferSize = " << AndroidHighPerformanceAudioHelpers::getNativeBufferSizeHint()
  743. << ", nativeSampleRate = " << AndroidHighPerformanceAudioHelpers::getNativeSampleRate()
  744. << ", actualBufferSize = " << actualBufferSize
  745. << ", audioBuffersToEnqueue = " << audioBuffersToEnqueue
  746. << ", sampleRate = " << sampleRate
  747. << ", supportsFloatingPoint = " << (session != nullptr && session->supportsFloatingPoint() ? "true" : "false"));
  748. if (session == nullptr)
  749. lastError = "Unknown error initializing opensl session";
  750. deviceOpen = (session != nullptr);
  751. return lastError;
  752. }
  753. void close() override
  754. {
  755. stop();
  756. session = nullptr;
  757. callback = nullptr;
  758. }
  759. int getOutputLatencyInSamples() override { return outputLatency; }
  760. int getInputLatencyInSamples() override { return inputLatency; }
  761. bool isOpen() override { return deviceOpen; }
  762. int getCurrentBufferSizeSamples() override { return actualBufferSize * audioBuffersToEnqueue; }
  763. int getCurrentBitDepth() override { return (session != nullptr && session->supportsFloatingPoint() ? 32 : 16); }
  764. BigInteger getActiveOutputChannels() const override { return activeOutputChans; }
  765. BigInteger getActiveInputChannels() const override { return activeInputChans; }
  766. String getLastError() override { return lastError; }
  767. bool isPlaying() override { return callback != nullptr; }
  768. int getXRunCount() const noexcept override { return (session != nullptr ? session->getXRunCount() : -1); }
  769. int getDefaultBufferSize() override
  770. {
  771. return AndroidHighPerformanceAudioHelpers::getDefaultBufferSize (AndroidHighPerformanceAudioHelpers::getNativeBufferSizeHint(),
  772. getCurrentSampleRate());
  773. }
  774. double getCurrentSampleRate() override
  775. {
  776. return (sampleRate == 0.0 ? AndroidHighPerformanceAudioHelpers::getNativeSampleRate() : sampleRate);
  777. }
  778. void start (AudioIODeviceCallback* newCallback) override
  779. {
  780. if (session != nullptr && callback != newCallback)
  781. {
  782. auto oldCallback = callback;
  783. if (newCallback != nullptr)
  784. newCallback->audioDeviceAboutToStart (this);
  785. if (oldCallback != nullptr)
  786. {
  787. // already running
  788. if (newCallback == nullptr)
  789. stop();
  790. else
  791. session->setCallback (newCallback);
  792. oldCallback->audioDeviceStopped();
  793. }
  794. else
  795. {
  796. jassert (newCallback != nullptr);
  797. // session hasn't started yet
  798. session->setCallback (newCallback);
  799. session->start();
  800. }
  801. callback = newCallback;
  802. }
  803. }
  804. void stop() override
  805. {
  806. if (session != nullptr && callback != nullptr)
  807. {
  808. callback = nullptr;
  809. session->stop();
  810. session->setCallback (nullptr);
  811. }
  812. }
  813. bool setAudioPreprocessingEnabled (bool shouldAudioProcessingBeEnabled) override
  814. {
  815. audioProcessingEnabled = shouldAudioProcessingBeEnabled;
  816. if (session != nullptr)
  817. session->setAudioPreprocessingEnabled (audioProcessingEnabled);
  818. return true;
  819. }
  820. static const char* const openSLTypeName;
  821. private:
  822. //==============================================================================
  823. friend class SLRealtimeThread;
  824. //==============================================================================
  825. int actualBufferSize = 0, sampleRate = 0, audioBuffersToEnqueue = 0;
  826. int inputLatency, outputLatency;
  827. bool deviceOpen = false, audioProcessingEnabled = true;
  828. String lastError;
  829. BigInteger activeOutputChans, activeInputChans;
  830. AudioIODeviceCallback* callback = nullptr;
  831. std::unique_ptr<OpenSLSession> session;
  832. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenSLAudioIODevice)
  833. };
  834. OpenSLAudioIODevice::OpenSLSession* OpenSLAudioIODevice::OpenSLSession::create (int numInputChannels, int numOutputChannels,
  835. double samleRateToUse, int bufferSizeToUse,
  836. int numBuffersToUse)
  837. {
  838. std::unique_ptr<OpenSLSession> retval;
  839. auto sdkVersion = getAndroidSDKVersion();
  840. // SDK versions 21 and higher should natively support floating point...
  841. if (sdkVersion >= 21)
  842. {
  843. retval.reset (new OpenSLSessionT<float> (numInputChannels, numOutputChannels, samleRateToUse,
  844. bufferSizeToUse, numBuffersToUse));
  845. // ...however, some devices lie so re-try without floating point
  846. if (retval != nullptr && (! retval->openedOK()))
  847. retval = nullptr;
  848. }
  849. if (retval == nullptr)
  850. {
  851. retval.reset (new OpenSLSessionT<int16> (numInputChannels, numOutputChannels, samleRateToUse,
  852. bufferSizeToUse, numBuffersToUse));
  853. if (retval != nullptr && (! retval->openedOK()))
  854. retval = nullptr;
  855. }
  856. return retval.release();
  857. }
  858. //==============================================================================
  859. class OpenSLAudioDeviceType : public AudioIODeviceType
  860. {
  861. public:
  862. OpenSLAudioDeviceType() : AudioIODeviceType (OpenSLAudioIODevice::openSLTypeName) {}
  863. //==============================================================================
  864. void scanForDevices() override {}
  865. StringArray getDeviceNames (bool) const override { return StringArray (OpenSLAudioIODevice::openSLTypeName); }
  866. int getDefaultDeviceIndex (bool) const override { return 0; }
  867. int getIndexOfDevice (AudioIODevice* device, bool) const override { return device != nullptr ? 0 : -1; }
  868. bool hasSeparateInputsAndOutputs() const override { return false; }
  869. AudioIODevice* createDevice (const String& outputDeviceName,
  870. const String& inputDeviceName) override
  871. {
  872. std::unique_ptr<OpenSLAudioIODevice> dev;
  873. if (outputDeviceName.isNotEmpty() || inputDeviceName.isNotEmpty())
  874. dev.reset (new OpenSLAudioIODevice (outputDeviceName.isNotEmpty() ? outputDeviceName
  875. : inputDeviceName));
  876. return dev.release();
  877. }
  878. static bool isOpenSLAvailable()
  879. {
  880. DynamicLibrary library;
  881. return library.open ("libOpenSLES.so");
  882. }
  883. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenSLAudioDeviceType)
  884. };
  885. const char* const OpenSLAudioIODevice::openSLTypeName = "Android OpenSL";
  886. //==============================================================================
  887. bool isOpenSLAvailable() { return OpenSLAudioDeviceType::isOpenSLAvailable(); }
  888. AudioIODeviceType* AudioIODeviceType::createAudioIODeviceType_OpenSLES()
  889. {
  890. return isOpenSLAvailable() ? new OpenSLAudioDeviceType() : nullptr;
  891. }
  892. //==============================================================================
  893. class SLRealtimeThread
  894. {
  895. public:
  896. static constexpr int numBuffers = 4;
  897. SLRealtimeThread()
  898. {
  899. if (auto createEngine = (CreateEngineFunc) slLibrary.getFunction ("slCreateEngine"))
  900. {
  901. SLObjectItf obj = nullptr;
  902. auto err = createEngine (&obj, 0, nullptr, 0, nullptr, nullptr);
  903. if (err != SL_RESULT_SUCCESS || obj == nullptr || *obj == nullptr)
  904. return;
  905. if ((*obj)->Realize (obj, 0) != SL_RESULT_SUCCESS)
  906. {
  907. destroyObject (obj);
  908. return;
  909. }
  910. engine = SlRef<SLEngineItf_>::cast (SlObjectRef (obj));
  911. if (engine == nullptr)
  912. {
  913. destroyObject (obj);
  914. return;
  915. }
  916. obj = nullptr;
  917. err = (*engine)->CreateOutputMix (engine, &obj, 0, nullptr, nullptr);
  918. if (err != SL_RESULT_SUCCESS || obj == nullptr || (*obj)->Realize (obj, 0) != SL_RESULT_SUCCESS)
  919. {
  920. destroyObject (obj);
  921. return;
  922. }
  923. outputMix = SlRef<SLOutputMixItf_>::cast (SlObjectRef (obj));
  924. if (outputMix == nullptr)
  925. {
  926. destroyObject (obj);
  927. return;
  928. }
  929. SLDataLocator_AndroidSimpleBufferQueue queueLocator = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, static_cast<SLuint32> (numBuffers)};
  930. SLDataLocator_OutputMix outputMixLocator = {SL_DATALOCATOR_OUTPUTMIX, outputMix};
  931. PCMDataFormatEx dataFormat;
  932. BufferHelpers<int16>::initPCMDataFormat (dataFormat, 1, AndroidHighPerformanceAudioHelpers::getNativeSampleRate());
  933. SLDataSource source = { &queueLocator, &dataFormat };
  934. SLDataSink sink = { &outputMixLocator, nullptr };
  935. SLInterfaceID queueInterfaces[] = { &IntfIID<SLAndroidSimpleBufferQueueItf_>::iid };
  936. SLboolean trueFlag = SL_BOOLEAN_TRUE;
  937. obj = nullptr;
  938. err = (*engine)->CreateAudioPlayer (engine, &obj, &source, &sink, 1, queueInterfaces, &trueFlag);
  939. if (err != SL_RESULT_SUCCESS || obj == nullptr)
  940. return;
  941. if ((*obj)->Realize (obj, 0) != SL_RESULT_SUCCESS)
  942. {
  943. destroyObject (obj);
  944. return;
  945. }
  946. player = SlRef<SLPlayItf_>::cast (SlObjectRef (obj));
  947. if (player == nullptr)
  948. {
  949. destroyObject (obj);
  950. return;
  951. }
  952. queue = SlRef<SLAndroidSimpleBufferQueueItf_>::cast (player);
  953. if (queue == nullptr)
  954. return;
  955. if ((*queue)->RegisterCallback (queue, staticFinished, this) != SL_RESULT_SUCCESS)
  956. {
  957. queue = nullptr;
  958. return;
  959. }
  960. pthread_cond_init (&threadReady, nullptr);
  961. pthread_mutex_init (&threadReadyMutex, nullptr);
  962. }
  963. }
  964. bool isOk() const { return queue != nullptr; }
  965. pthread_t startThread (void* (*entry) (void*), void* userPtr)
  966. {
  967. memset (buffer.get(), 0, static_cast<size_t> (sizeof (int16) * static_cast<size_t> (bufferSize * numBuffers)));
  968. for (int i = 0; i < numBuffers; ++i)
  969. {
  970. int16* dst = buffer.get() + (bufferSize * i);
  971. (*queue)->Enqueue (queue, dst, static_cast<SLuint32> (static_cast<size_t> (bufferSize) * sizeof (int16)));
  972. }
  973. pthread_mutex_lock (&threadReadyMutex);
  974. threadEntryProc = entry;
  975. threadUserPtr = userPtr;
  976. (*player)->SetPlayState (player, SL_PLAYSTATE_PLAYING);
  977. pthread_cond_wait (&threadReady, &threadReadyMutex);
  978. pthread_mutex_unlock (&threadReadyMutex);
  979. return threadID;
  980. }
  981. void finished()
  982. {
  983. if (threadEntryProc != nullptr)
  984. {
  985. pthread_mutex_lock (&threadReadyMutex);
  986. threadID = pthread_self();
  987. pthread_cond_signal (&threadReady);
  988. pthread_mutex_unlock (&threadReadyMutex);
  989. threadEntryProc (threadUserPtr);
  990. threadEntryProc = nullptr;
  991. (*player)->SetPlayState (player, SL_PLAYSTATE_STOPPED);
  992. MessageManager::callAsync ([this]() { delete this; });
  993. }
  994. }
  995. private:
  996. //==============================================================================
  997. static void staticFinished (SLAndroidSimpleBufferQueueItf, void* context)
  998. {
  999. static_cast<SLRealtimeThread*> (context)->finished();
  1000. }
  1001. //==============================================================================
  1002. DynamicLibrary slLibrary { "libOpenSLES.so" };
  1003. SlRef<SLEngineItf_> engine;
  1004. SlRef<SLOutputMixItf_> outputMix;
  1005. SlRef<SLPlayItf_> player;
  1006. SlRef<SLAndroidSimpleBufferQueueItf_> queue;
  1007. int bufferSize = AndroidHighPerformanceAudioHelpers::getNativeBufferSizeHint();
  1008. HeapBlock<int16> buffer { HeapBlock<int16> (static_cast<size_t> (1 * bufferSize * numBuffers)) };
  1009. void* (*threadEntryProc) (void*) = nullptr;
  1010. void* threadUserPtr = nullptr;
  1011. pthread_cond_t threadReady;
  1012. pthread_mutex_t threadReadyMutex;
  1013. pthread_t threadID;
  1014. };
  1015. //==============================================================================
  1016. pthread_t juce_createRealtimeAudioThread (void* (*entry) (void*), void* userPtr)
  1017. {
  1018. auto thread = std::make_unique<SLRealtimeThread>();
  1019. if (! thread->isOk())
  1020. return {};
  1021. auto threadID = thread->startThread (entry, userPtr);
  1022. // the thread will de-allocate itself
  1023. thread.release();
  1024. return threadID;
  1025. }
  1026. } // namespace juce