Phil Burk | 420715b | 2017-12-14 12:26:38 -0800 | [diff] [blame] | 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 | |
| 18 | #include "common/OboeDebug.h" |
| 19 | #include "EngineOpenSLES.h" |
| 20 | #include "OpenSLESUtilities.h" |
| 21 | #include "OutputMixerOpenSLES.h" |
| 22 | |
| 23 | using namespace oboe; |
| 24 | |
Phil Burk | 7040a9d | 2017-12-15 10:54:38 -0800 | [diff] [blame] | 25 | OutputMixerOpenSL &OutputMixerOpenSL::getInstance() { |
Phil Burk | 420715b | 2017-12-14 12:26:38 -0800 | [diff] [blame] | 26 | static OutputMixerOpenSL sInstance; |
Phil Burk | 7040a9d | 2017-12-15 10:54:38 -0800 | [diff] [blame] | 27 | return sInstance; |
Phil Burk | 420715b | 2017-12-14 12:26:38 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | SLresult OutputMixerOpenSL::open() { |
| 31 | std::lock_guard<std::mutex> lock(mLock); |
| 32 | |
| 33 | SLresult result = SL_RESULT_SUCCESS; |
| 34 | if (mOpenCount++ == 0) { |
| 35 | // get the output mixer |
Phil Burk | 7040a9d | 2017-12-15 10:54:38 -0800 | [diff] [blame] | 36 | result = EngineOpenSLES::getInstance().createOutputMix(&mOutputMixObject); |
Phil Burk | 420715b | 2017-12-14 12:26:38 -0800 | [diff] [blame] | 37 | if (SL_RESULT_SUCCESS != result) { |
| 38 | LOGE("OutputMixerOpenSL() - createOutputMix() result:%s", getSLErrStr(result)); |
| 39 | goto error; |
| 40 | } |
| 41 | |
| 42 | // realize the output mix |
| 43 | result = (*mOutputMixObject)->Realize(mOutputMixObject, SL_BOOLEAN_FALSE); |
| 44 | if (SL_RESULT_SUCCESS != result) { |
| 45 | LOGE("OutputMixerOpenSL() - Realize() mOutputMixObject result:%s", getSLErrStr(result)); |
| 46 | goto error; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return result; |
| 51 | |
| 52 | error: |
| 53 | close(); |
| 54 | return result; |
| 55 | } |
| 56 | |
| 57 | void OutputMixerOpenSL::close() { |
| 58 | std::lock_guard<std::mutex> lock(mLock); |
| 59 | |
| 60 | if (--mOpenCount == 0) { |
| 61 | // destroy output mix object, and invalidate all associated interfaces |
| 62 | if (mOutputMixObject != NULL) { |
| 63 | (*mOutputMixObject)->Destroy(mOutputMixObject); |
| 64 | mOutputMixObject = NULL; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | SLresult OutputMixerOpenSL::createAudioPlayer(SLObjectItf *objectItf, |
| 70 | SLDataSource *audioSource) { |
| 71 | SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, mOutputMixObject}; |
| 72 | SLDataSink audioSink = {&loc_outmix, NULL}; |
Phil Burk | 7040a9d | 2017-12-15 10:54:38 -0800 | [diff] [blame] | 73 | return EngineOpenSLES::getInstance().createAudioPlayer(objectItf, audioSource, &audioSink); |
Phil Burk | 420715b | 2017-12-14 12:26:38 -0800 | [diff] [blame] | 74 | } |