1 /* 2 * Copyright 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef OBOE_ENGINE_OPENSLES_H 18 #define OBOE_ENGINE_OPENSLES_H 19 20 #include <atomic> 21 #include <mutex> 22 23 #include <SLES/OpenSLES.h> 24 #include <SLES/OpenSLES_Android.h> 25 26 namespace oboe { 27 28 typedef SLresult (*prototype_slCreateEngine)( 29 SLObjectItf *pEngine, 30 SLuint32 numOptions, 31 const SLEngineOption *pEngineOptions, 32 SLuint32 numInterfaces, 33 const SLInterfaceID *pInterfaceIds, 34 const SLboolean *pInterfaceRequired 35 ); 36 37 /** 38 * INTERNAL USE ONLY 39 */ 40 class EngineOpenSLES { 41 public: 42 static EngineOpenSLES &getInstance(); 43 44 bool linkOpenSLES(); 45 46 SLresult open(); 47 48 void close(); 49 50 SLresult createOutputMix(SLObjectItf *objectItf); 51 52 SLresult createAudioPlayer(SLObjectItf *objectItf, 53 SLDataSource *audioSource, 54 SLDataSink *audioSink); 55 SLresult createAudioRecorder(SLObjectItf *objectItf, 56 SLDataSource *audioSource, 57 SLDataSink *audioSink); 58 getIidEngine()59 SLInterfaceID getIidEngine() { return LOCAL_SL_IID_ENGINE; } getIidAndroidSimpleBufferQueue()60 SLInterfaceID getIidAndroidSimpleBufferQueue() { return LOCAL_SL_IID_ANDROIDSIMPLEBUFFERQUEUE; } getIidAndroidConfiguration()61 SLInterfaceID getIidAndroidConfiguration() { return LOCAL_SL_IID_ANDROIDCONFIGURATION; } getIidRecord()62 SLInterfaceID getIidRecord() { return LOCAL_SL_IID_RECORD; } getIidBufferQueue()63 SLInterfaceID getIidBufferQueue() { return LOCAL_SL_IID_BUFFERQUEUE; } getIidVolume()64 SLInterfaceID getIidVolume() { return LOCAL_SL_IID_VOLUME; } getIidPlay()65 SLInterfaceID getIidPlay() { return LOCAL_SL_IID_PLAY; } 66 67 private: 68 // Make this a safe Singleton 69 EngineOpenSLES()= default; 70 ~EngineOpenSLES()= default; 71 EngineOpenSLES(const EngineOpenSLES&)= delete; 72 EngineOpenSLES& operator=(const EngineOpenSLES&)= delete; 73 74 SLInterfaceID getIidPointer(const char *symbolName); 75 76 /** 77 * Close the OpenSL ES engine. 78 * This must be called under mLock 79 */ 80 void close_l(); 81 82 std::mutex mLock; 83 int32_t mOpenCount = 0; 84 85 static constexpr int32_t kLinkStateUninitialized = 0; 86 static constexpr int32_t kLinkStateGood = 1; 87 static constexpr int32_t kLinkStateBad = 2; 88 int32_t mDynamicLinkState = kLinkStateUninitialized; 89 SLObjectItf mEngineObject = nullptr; 90 SLEngineItf mEngineInterface = nullptr; 91 92 // These symbols are loaded using dlsym(). 93 prototype_slCreateEngine mFunction_slCreateEngine = nullptr; 94 void *mLibOpenSlesLibraryHandle = nullptr; 95 SLInterfaceID LOCAL_SL_IID_ENGINE = nullptr; 96 SLInterfaceID LOCAL_SL_IID_ANDROIDSIMPLEBUFFERQUEUE = nullptr; 97 SLInterfaceID LOCAL_SL_IID_ANDROIDCONFIGURATION = nullptr; 98 SLInterfaceID LOCAL_SL_IID_RECORD = nullptr; 99 SLInterfaceID LOCAL_SL_IID_BUFFERQUEUE = nullptr; 100 SLInterfaceID LOCAL_SL_IID_VOLUME = nullptr; 101 SLInterfaceID LOCAL_SL_IID_PLAY = nullptr; 102 }; 103 104 } // namespace oboe 105 106 107 #endif //OBOE_ENGINE_OPENSLES_H 108