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.

1395 lines
55KB

  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 = getNativeSampleRate();
  682. if (native != 0.0 && ! retval.contains (native))
  683. retval.add (native);
  684. return retval;
  685. }
  686. Array<int> getAvailableBufferSizes() override
  687. {
  688. // we need to offer the lowest possible buffer size which
  689. // is the native buffer size
  690. auto nativeBufferSize = getNativeBufferSize();
  691. auto minBuffersToQueue = getMinimumBuffersToEnqueue();
  692. auto maxBuffersToQueue = getMaximumBuffersToEnqueue();
  693. Array<int> retval;
  694. for (int i = minBuffersToQueue; i <= maxBuffersToQueue; ++i)
  695. retval.add (i * nativeBufferSize);
  696. return retval;
  697. }
  698. String open (const BigInteger& inputChannels,
  699. const BigInteger& outputChannels,
  700. double requestedSampleRate,
  701. int bufferSize) override
  702. {
  703. close();
  704. lastError.clear();
  705. sampleRate = (int) (requestedSampleRate > 0 ? requestedSampleRate : getNativeSampleRate());
  706. auto totalPreferredBufferSize = (bufferSize <= 0) ? getDefaultBufferSize() : bufferSize;
  707. auto nativeBufferSize = getNativeBufferSize();
  708. bool useHighPerformanceAudioPath = canUseHighPerformanceAudioPath (totalPreferredBufferSize, sampleRate);
  709. audioBuffersToEnqueue = useHighPerformanceAudioPath ? (totalPreferredBufferSize / nativeBufferSize) : 1;
  710. actualBufferSize = totalPreferredBufferSize / audioBuffersToEnqueue;
  711. jassert ((actualBufferSize * audioBuffersToEnqueue) == totalPreferredBufferSize);
  712. activeOutputChans = outputChannels;
  713. activeOutputChans.setRange (2, activeOutputChans.getHighestBit(), false);
  714. auto numOutputChannels = activeOutputChans.countNumberOfSetBits();
  715. activeInputChans = inputChannels;
  716. activeInputChans.setRange (1, activeInputChans.getHighestBit(), false);
  717. auto numInputChannels = activeInputChans.countNumberOfSetBits();
  718. if (numInputChannels > 0 && (! RuntimePermissions::isGranted (RuntimePermissions::recordAudio)))
  719. {
  720. // If you hit this assert, you probably forgot to get RuntimePermissions::recordAudio
  721. // before trying to open an audio input device. This is not going to work!
  722. jassertfalse;
  723. lastError = "Error opening OpenSL input device: the app was not granted android.permission.RECORD_AUDIO";
  724. }
  725. session.reset (OpenSLSession::create (numInputChannels, numOutputChannels,
  726. sampleRate, actualBufferSize, audioBuffersToEnqueue));
  727. if (session != nullptr)
  728. {
  729. session->setAudioPreprocessingEnabled (audioProcessingEnabled);
  730. }
  731. else
  732. {
  733. if (numInputChannels > 0 && numOutputChannels > 0 && RuntimePermissions::isGranted (RuntimePermissions::recordAudio))
  734. {
  735. // New versions of the Android emulator do not seem to support audio input anymore on OS X
  736. activeInputChans = BigInteger(0);
  737. numInputChannels = 0;
  738. session.reset (OpenSLSession::create (numInputChannels, numOutputChannels,
  739. sampleRate, actualBufferSize, audioBuffersToEnqueue));
  740. }
  741. }
  742. DBG ("OpenSL: numInputChannels = " << numInputChannels
  743. << ", numOutputChannels = " << numOutputChannels
  744. << ", nativeBufferSize = " << getNativeBufferSize()
  745. << ", nativeSampleRate = " << getNativeSampleRate()
  746. << ", actualBufferSize = " << actualBufferSize
  747. << ", audioBuffersToEnqueue = " << audioBuffersToEnqueue
  748. << ", sampleRate = " << sampleRate
  749. << ", supportsFloatingPoint = " << (session != nullptr && session->supportsFloatingPoint() ? "true" : "false"));
  750. if (session == nullptr)
  751. lastError = "Unknown error initializing opensl session";
  752. deviceOpen = (session != nullptr);
  753. return lastError;
  754. }
  755. void close() override
  756. {
  757. stop();
  758. session = nullptr;
  759. callback = nullptr;
  760. }
  761. int getOutputLatencyInSamples() override { return outputLatency; }
  762. int getInputLatencyInSamples() override { return inputLatency; }
  763. bool isOpen() override { return deviceOpen; }
  764. int getCurrentBufferSizeSamples() override { return actualBufferSize * audioBuffersToEnqueue; }
  765. int getCurrentBitDepth() override { return (session != nullptr && session->supportsFloatingPoint() ? 32 : 16); }
  766. BigInteger getActiveOutputChannels() const override { return activeOutputChans; }
  767. BigInteger getActiveInputChannels() const override { return activeInputChans; }
  768. String getLastError() override { return lastError; }
  769. bool isPlaying() override { return callback != nullptr; }
  770. int getXRunCount() const noexcept override { return (session != nullptr ? session->getXRunCount() : -1); }
  771. int getDefaultBufferSize() override
  772. {
  773. auto defaultBufferLength = (hasLowLatencyAudioPath() ? defaultBufferSizeForLowLatencyDeviceMs
  774. : defaultBufferSizeForStandardLatencyDeviceMs);
  775. auto defaultBuffersToEnqueue = buffersToQueueForBufferDuration (defaultBufferLength, getCurrentSampleRate());
  776. return defaultBuffersToEnqueue * getNativeBufferSize();
  777. }
  778. double getCurrentSampleRate() override
  779. {
  780. return (sampleRate == 0.0 ? getNativeSampleRate() : sampleRate);
  781. }
  782. void start (AudioIODeviceCallback* newCallback) override
  783. {
  784. if (session != nullptr && callback != newCallback)
  785. {
  786. auto oldCallback = callback;
  787. if (newCallback != nullptr)
  788. newCallback->audioDeviceAboutToStart (this);
  789. if (oldCallback != nullptr)
  790. {
  791. // already running
  792. if (newCallback == nullptr)
  793. stop();
  794. else
  795. session->setCallback (newCallback);
  796. oldCallback->audioDeviceStopped();
  797. }
  798. else
  799. {
  800. jassert (newCallback != nullptr);
  801. // session hasn't started yet
  802. session->setCallback (newCallback);
  803. session->start();
  804. }
  805. callback = newCallback;
  806. }
  807. }
  808. void stop() override
  809. {
  810. if (session != nullptr && callback != nullptr)
  811. {
  812. callback = nullptr;
  813. session->stop();
  814. session->setCallback (nullptr);
  815. }
  816. }
  817. bool setAudioPreprocessingEnabled (bool shouldAudioProcessingBeEnabled) override
  818. {
  819. audioProcessingEnabled = shouldAudioProcessingBeEnabled;
  820. if (session != nullptr)
  821. session->setAudioPreprocessingEnabled (audioProcessingEnabled);
  822. return true;
  823. }
  824. static const char* const openSLTypeName;
  825. private:
  826. //==============================================================================
  827. friend class SLRealtimeThread;
  828. //==============================================================================
  829. int actualBufferSize = 0, sampleRate = 0, audioBuffersToEnqueue = 0;
  830. int inputLatency, outputLatency;
  831. bool deviceOpen = false, audioProcessingEnabled = true;
  832. String lastError;
  833. BigInteger activeOutputChans, activeInputChans;
  834. AudioIODeviceCallback* callback = nullptr;
  835. std::unique_ptr<OpenSLSession> session;
  836. enum
  837. {
  838. defaultBufferSizeForLowLatencyDeviceMs = 40,
  839. defaultBufferSizeForStandardLatencyDeviceMs = 100
  840. };
  841. static int getMinimumBuffersToEnqueue (double sampleRateToCheck = getNativeSampleRate())
  842. {
  843. if (canUseHighPerformanceAudioPath (getNativeBufferSize(), (int) sampleRateToCheck))
  844. {
  845. // see https://developer.android.com/ndk/guides/audio/opensl/opensl-prog-notes.html#sandp
  846. // "For Android 4.2 (API level 17) and earlier, a buffer count of two or more is required
  847. // for lower latency. Beginning with Android 4.3 (API level 18), a buffer count of one
  848. // is sufficient for lower latency."
  849. return (getAndroidSDKVersion() >= 18 ? 1 : 2);
  850. }
  851. // we will not use the low-latency path so we can use the absolute minimum number of buffers
  852. // to queue
  853. return 1;
  854. }
  855. int getMaximumBuffersToEnqueue() noexcept
  856. {
  857. constexpr auto maxBufferSizeMs = 200;
  858. auto availableSampleRates = getAvailableSampleRates();
  859. auto maximumSampleRate = findMaximum(availableSampleRates.getRawDataPointer(), availableSampleRates.size());
  860. // ensure we don't return something crazy small
  861. return jmax (8, buffersToQueueForBufferDuration (maxBufferSizeMs, maximumSampleRate));
  862. }
  863. static int buffersToQueueForBufferDuration (int bufferDurationInMs, double sampleRate) noexcept
  864. {
  865. auto maxBufferFrames = static_cast<int> (std::ceil (bufferDurationInMs * sampleRate / 1000.0));
  866. auto maxNumBuffers = static_cast<int> (std::ceil (static_cast<double> (maxBufferFrames)
  867. / static_cast<double> (getNativeBufferSize())));
  868. return jmax (getMinimumBuffersToEnqueue (sampleRate), maxNumBuffers);
  869. }
  870. //==============================================================================
  871. static double getNativeSampleRate()
  872. {
  873. return audioManagerGetProperty ("android.media.property.OUTPUT_SAMPLE_RATE").getDoubleValue();
  874. }
  875. static int getNativeBufferSize()
  876. {
  877. const int val = audioManagerGetProperty ("android.media.property.OUTPUT_FRAMES_PER_BUFFER").getIntValue();
  878. return val > 0 ? val : 512;
  879. }
  880. static bool isProAudioDevice()
  881. {
  882. return androidHasSystemFeature ("android.hardware.audio.pro") || isSapaSupported();
  883. }
  884. static bool hasLowLatencyAudioPath()
  885. {
  886. return androidHasSystemFeature ("android.hardware.audio.low_latency");
  887. }
  888. static bool canUseHighPerformanceAudioPath (int requestedBufferSize, int requestedSampleRate)
  889. {
  890. return ((requestedBufferSize % getNativeBufferSize()) == 0)
  891. && (requestedSampleRate == getNativeSampleRate())
  892. && isProAudioDevice();
  893. }
  894. //==============================================================================
  895. // Some minimum Sapa support to check if this device supports pro audio
  896. static bool isSamsungDevice()
  897. {
  898. return SystemStats::getDeviceManufacturer().containsIgnoreCase ("SAMSUNG");
  899. }
  900. static bool isSapaSupported()
  901. {
  902. static bool supported = isSamsungDevice() && DynamicLibrary().open ("libapa_jni.so");
  903. return supported;
  904. }
  905. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenSLAudioIODevice)
  906. };
  907. OpenSLAudioIODevice::OpenSLSession* OpenSLAudioIODevice::OpenSLSession::create (int numInputChannels, int numOutputChannels,
  908. double samleRateToUse, int bufferSizeToUse,
  909. int numBuffersToUse)
  910. {
  911. std::unique_ptr<OpenSLSession> retval;
  912. auto sdkVersion = getAndroidSDKVersion();
  913. // SDK versions 21 and higher should natively support floating point...
  914. if (sdkVersion >= 21)
  915. {
  916. retval.reset (new OpenSLSessionT<float> (numInputChannels, numOutputChannels, samleRateToUse,
  917. bufferSizeToUse, numBuffersToUse));
  918. // ...however, some devices lie so re-try without floating point
  919. if (retval != nullptr && (! retval->openedOK()))
  920. retval = nullptr;
  921. }
  922. if (retval == nullptr)
  923. {
  924. retval.reset (new OpenSLSessionT<int16> (numInputChannels, numOutputChannels, samleRateToUse,
  925. bufferSizeToUse, numBuffersToUse));
  926. if (retval != nullptr && (! retval->openedOK()))
  927. retval = nullptr;
  928. }
  929. return retval.release();
  930. }
  931. //==============================================================================
  932. class OpenSLAudioDeviceType : public AudioIODeviceType
  933. {
  934. public:
  935. OpenSLAudioDeviceType() : AudioIODeviceType (OpenSLAudioIODevice::openSLTypeName) {}
  936. //==============================================================================
  937. void scanForDevices() override {}
  938. StringArray getDeviceNames (bool) const override { return StringArray (OpenSLAudioIODevice::openSLTypeName); }
  939. int getDefaultDeviceIndex (bool) const override { return 0; }
  940. int getIndexOfDevice (AudioIODevice* device, bool) const override { return device != nullptr ? 0 : -1; }
  941. bool hasSeparateInputsAndOutputs() const override { return false; }
  942. AudioIODevice* createDevice (const String& outputDeviceName,
  943. const String& inputDeviceName) override
  944. {
  945. std::unique_ptr<OpenSLAudioIODevice> dev;
  946. if (outputDeviceName.isNotEmpty() || inputDeviceName.isNotEmpty())
  947. dev.reset (new OpenSLAudioIODevice (outputDeviceName.isNotEmpty() ? outputDeviceName
  948. : inputDeviceName));
  949. return dev.release();
  950. }
  951. static bool isOpenSLAvailable()
  952. {
  953. DynamicLibrary library;
  954. return library.open ("libOpenSLES.so");
  955. }
  956. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenSLAudioDeviceType)
  957. };
  958. const char* const OpenSLAudioIODevice::openSLTypeName = "Android OpenSL";
  959. //==============================================================================
  960. bool isOpenSLAvailable() { return OpenSLAudioDeviceType::isOpenSLAvailable(); }
  961. AudioIODeviceType* AudioIODeviceType::createAudioIODeviceType_OpenSLES()
  962. {
  963. return isOpenSLAvailable() ? new OpenSLAudioDeviceType() : nullptr;
  964. }
  965. //==============================================================================
  966. class SLRealtimeThread
  967. {
  968. public:
  969. static constexpr int numBuffers = 4;
  970. SLRealtimeThread()
  971. {
  972. if (auto createEngine = (CreateEngineFunc) slLibrary.getFunction ("slCreateEngine"))
  973. {
  974. SLObjectItf obj = nullptr;
  975. auto err = createEngine (&obj, 0, nullptr, 0, nullptr, nullptr);
  976. if (err != SL_RESULT_SUCCESS || obj == nullptr || *obj == nullptr)
  977. return;
  978. if ((*obj)->Realize (obj, 0) != SL_RESULT_SUCCESS)
  979. {
  980. destroyObject (obj);
  981. return;
  982. }
  983. engine = SlRef<SLEngineItf_>::cast (SlObjectRef (obj));
  984. if (engine == nullptr)
  985. {
  986. destroyObject (obj);
  987. return;
  988. }
  989. obj = nullptr;
  990. err = (*engine)->CreateOutputMix (engine, &obj, 0, nullptr, nullptr);
  991. if (err != SL_RESULT_SUCCESS || obj == nullptr || (*obj)->Realize (obj, 0) != SL_RESULT_SUCCESS)
  992. {
  993. destroyObject (obj);
  994. return;
  995. }
  996. outputMix = SlRef<SLOutputMixItf_>::cast (SlObjectRef (obj));
  997. if (outputMix == nullptr)
  998. {
  999. destroyObject (obj);
  1000. return;
  1001. }
  1002. SLDataLocator_AndroidSimpleBufferQueue queueLocator = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, static_cast<SLuint32> (numBuffers)};
  1003. SLDataLocator_OutputMix outputMixLocator = {SL_DATALOCATOR_OUTPUTMIX, outputMix};
  1004. PCMDataFormatEx dataFormat;
  1005. BufferHelpers<int16>::initPCMDataFormat (dataFormat, 1, OpenSLAudioIODevice::getNativeSampleRate());
  1006. SLDataSource source = { &queueLocator, &dataFormat };
  1007. SLDataSink sink = { &outputMixLocator, nullptr };
  1008. SLInterfaceID queueInterfaces[] = { &IntfIID<SLAndroidSimpleBufferQueueItf_>::iid };
  1009. SLboolean trueFlag = SL_BOOLEAN_TRUE;
  1010. obj = nullptr;
  1011. err = (*engine)->CreateAudioPlayer (engine, &obj, &source, &sink, 1, queueInterfaces, &trueFlag);
  1012. if (err != SL_RESULT_SUCCESS || obj == nullptr)
  1013. return;
  1014. if ((*obj)->Realize (obj, 0) != SL_RESULT_SUCCESS)
  1015. {
  1016. destroyObject (obj);
  1017. return;
  1018. }
  1019. player = SlRef<SLPlayItf_>::cast (SlObjectRef (obj));
  1020. if (player == nullptr)
  1021. {
  1022. destroyObject (obj);
  1023. return;
  1024. }
  1025. queue = SlRef<SLAndroidSimpleBufferQueueItf_>::cast (player);
  1026. if (queue == nullptr)
  1027. return;
  1028. if ((*queue)->RegisterCallback (queue, staticFinished, this) != SL_RESULT_SUCCESS)
  1029. {
  1030. queue = nullptr;
  1031. return;
  1032. }
  1033. pthread_cond_init (&threadReady, nullptr);
  1034. pthread_mutex_init (&threadReadyMutex, nullptr);
  1035. }
  1036. }
  1037. bool isOK() const { return queue != nullptr; }
  1038. pthread_t startThread (void* (*entry) (void*), void* userPtr)
  1039. {
  1040. memset (buffer.get(), 0, static_cast<size_t> (sizeof (int16) * static_cast<size_t> (bufferSize * numBuffers)));
  1041. for (int i = 0; i < numBuffers; ++i)
  1042. {
  1043. int16* dst = buffer.get() + (bufferSize * i);
  1044. (*queue)->Enqueue (queue, dst, static_cast<SLuint32> (static_cast<size_t> (bufferSize) * sizeof (int16)));
  1045. }
  1046. pthread_mutex_lock (&threadReadyMutex);
  1047. threadEntryProc = entry;
  1048. threadUserPtr = userPtr;
  1049. (*player)->SetPlayState (player, SL_PLAYSTATE_PLAYING);
  1050. pthread_cond_wait (&threadReady, &threadReadyMutex);
  1051. pthread_mutex_unlock (&threadReadyMutex);
  1052. return threadID;
  1053. }
  1054. void finished()
  1055. {
  1056. if (threadEntryProc != nullptr)
  1057. {
  1058. pthread_mutex_lock (&threadReadyMutex);
  1059. threadID = pthread_self();
  1060. pthread_cond_signal (&threadReady);
  1061. pthread_mutex_unlock (&threadReadyMutex);
  1062. threadEntryProc (threadUserPtr);
  1063. threadEntryProc = nullptr;
  1064. (*player)->SetPlayState (player, SL_PLAYSTATE_STOPPED);
  1065. MessageManager::callAsync ([this] () { delete this; });
  1066. }
  1067. }
  1068. private:
  1069. //==============================================================================
  1070. static void staticFinished (SLAndroidSimpleBufferQueueItf, void* context)
  1071. {
  1072. static_cast<SLRealtimeThread*> (context)->finished();
  1073. }
  1074. //==============================================================================
  1075. DynamicLibrary slLibrary { "libOpenSLES.so" };
  1076. SlRef<SLEngineItf_> engine;
  1077. SlRef<SLOutputMixItf_> outputMix;
  1078. SlRef<SLPlayItf_> player;
  1079. SlRef<SLAndroidSimpleBufferQueueItf_> queue;
  1080. int bufferSize = OpenSLAudioIODevice::getNativeBufferSize();
  1081. HeapBlock<int16> buffer { HeapBlock<int16> (static_cast<size_t> (1 * bufferSize * numBuffers)) };
  1082. void* (*threadEntryProc) (void*) = nullptr;
  1083. void* threadUserPtr = nullptr;
  1084. pthread_cond_t threadReady;
  1085. pthread_mutex_t threadReadyMutex;
  1086. pthread_t threadID;
  1087. };
  1088. pthread_t juce_createRealtimeAudioThread (void* (*entry) (void*), void* userPtr)
  1089. {
  1090. std::unique_ptr<SLRealtimeThread> thread (new SLRealtimeThread);
  1091. if (! thread->isOK())
  1092. return 0;
  1093. pthread_t threadID = thread->startThread (entry, userPtr);
  1094. // the thread will de-allocate itself
  1095. thread.release();
  1096. return threadID;
  1097. }
  1098. } // namespace juce