• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
getInstance()25 OutputMixerOpenSL &OutputMixerOpenSL::getInstance() {
26     static OutputMixerOpenSL sInstance;
27     return sInstance;
28 }
29 
open()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
36         result = EngineOpenSLES::getInstance().createOutputMix(&mOutputMixObject);
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 
close()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 != nullptr) {
63             (*mOutputMixObject)->Destroy(mOutputMixObject);
64             mOutputMixObject = nullptr;
65         }
66     }
67 }
68 
createAudioPlayer(SLObjectItf * objectItf,SLDataSource * audioSource)69 SLresult OutputMixerOpenSL::createAudioPlayer(SLObjectItf *objectItf,
70                                               SLDataSource *audioSource) {
71     SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, mOutputMixObject};
72     SLDataSink audioSink = {&loc_outmix, NULL};
73     return EngineOpenSLES::getInstance().createAudioPlayer(objectItf, audioSource, &audioSink);
74 }
75