| 6 |
| 7 | #include "OpenSLEngineWrapper.h" |
| 8 | #include "../../logging.h" |
| 9 | #include <cstddef> |
| 10 |
| 11 | #define CHECK_SL_ERROR(res, msg) \ |
| 12 | if (res != SL_RESULT_SUCCESS) \ |
| 13 | { \ |
| 14 | LOGE(msg); \ |
| 15 | return nullptr; \ |
| 16 | } |
| 17 |
| 18 | using namespace tgvoip; |
| 19 | using namespace tgvoip::audio; |
| 20 |
| 21 | SLObjectItf OpenSLEngineWrapper::sharedEngineObj = nullptr; |
| 22 | SLEngineItf OpenSLEngineWrapper::sharedEngine = nullptr; |
| 23 | int OpenSLEngineWrapper::count = 0; |
| 24 |
| 25 | void OpenSLEngineWrapper::DestroyEngine() |
| 26 | { |
| 27 | count--; |
| 28 | LOGI("release: engine instance count %d", count); |
| 29 | if (count == 0) |
| 30 | { |
| 31 | (*sharedEngineObj)->Destroy(sharedEngineObj); |
| 32 | sharedEngineObj = nullptr; |
| 33 | sharedEngine = nullptr; |
| 34 | } |
| 35 | LOGI("after release"); |
| 36 | } |
| 37 |
| 38 | SLEngineItf OpenSLEngineWrapper::CreateEngine() |
| 39 | { |
| 40 | count++; |
| 41 | if (sharedEngine) |
| 42 | return sharedEngine; |
| 43 | const SLInterfaceID pIDs[1] = {SL_IID_ENGINE}; |
| 44 | const SLboolean pIDsRequired[1] = {SL_BOOLEAN_TRUE}; |
| 45 | SLresult result = slCreateEngine(&sharedEngineObj, 0, nullptr, 1, pIDs, pIDsRequired); |
| 46 | CHECK_SL_ERROR(result, "Error creating engine"); |
| 47 |
| 48 | result = (*sharedEngineObj)->Realize(sharedEngineObj, SL_BOOLEAN_FALSE); |
| 49 | CHECK_SL_ERROR(result, "Error realizing engine"); |
| 50 |
| 51 | result = (*sharedEngineObj)->GetInterface(sharedEngineObj, SL_IID_ENGINE, &sharedEngine); |
| 52 | CHECK_SL_ERROR(result, "Error getting engine interface"); |
| 53 | return sharedEngine; |
| 54 | } |
| 55 |