xref: /aosp_15_r20/external/oboe/src/opensles/OutputMixerOpenSLES.cpp (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1*05767d91SRobert Wu /*
2*05767d91SRobert Wu  * Copyright 2017 The Android Open Source Project
3*05767d91SRobert Wu  *
4*05767d91SRobert Wu  * Licensed under the Apache License, Version 2.0 (the "License");
5*05767d91SRobert Wu  * you may not use this file except in compliance with the License.
6*05767d91SRobert Wu  * You may obtain a copy of the License at
7*05767d91SRobert Wu  *
8*05767d91SRobert Wu  *      http://www.apache.org/licenses/LICENSE-2.0
9*05767d91SRobert Wu  *
10*05767d91SRobert Wu  * Unless required by applicable law or agreed to in writing, software
11*05767d91SRobert Wu  * distributed under the License is distributed on an "AS IS" BASIS,
12*05767d91SRobert Wu  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*05767d91SRobert Wu  * See the License for the specific language governing permissions and
14*05767d91SRobert Wu  * limitations under the License.
15*05767d91SRobert Wu  */
16*05767d91SRobert Wu 
17*05767d91SRobert Wu 
18*05767d91SRobert Wu #include "common/OboeDebug.h"
19*05767d91SRobert Wu #include "EngineOpenSLES.h"
20*05767d91SRobert Wu #include "OpenSLESUtilities.h"
21*05767d91SRobert Wu #include "OutputMixerOpenSLES.h"
22*05767d91SRobert Wu 
23*05767d91SRobert Wu using namespace oboe;
24*05767d91SRobert Wu 
getInstance()25*05767d91SRobert Wu OutputMixerOpenSL &OutputMixerOpenSL::getInstance() {
26*05767d91SRobert Wu     static OutputMixerOpenSL sInstance;
27*05767d91SRobert Wu     return sInstance;
28*05767d91SRobert Wu }
29*05767d91SRobert Wu 
open()30*05767d91SRobert Wu SLresult OutputMixerOpenSL::open() {
31*05767d91SRobert Wu     std::lock_guard<std::mutex> lock(mLock);
32*05767d91SRobert Wu 
33*05767d91SRobert Wu     SLresult result = SL_RESULT_SUCCESS;
34*05767d91SRobert Wu     if (mOpenCount++ == 0) {
35*05767d91SRobert Wu         // get the output mixer
36*05767d91SRobert Wu         result = EngineOpenSLES::getInstance().createOutputMix(&mOutputMixObject);
37*05767d91SRobert Wu         if (SL_RESULT_SUCCESS != result) {
38*05767d91SRobert Wu             LOGE("OutputMixerOpenSL() - createOutputMix() result:%s", getSLErrStr(result));
39*05767d91SRobert Wu             goto error;
40*05767d91SRobert Wu         }
41*05767d91SRobert Wu 
42*05767d91SRobert Wu         // realize the output mix
43*05767d91SRobert Wu         result = (*mOutputMixObject)->Realize(mOutputMixObject, SL_BOOLEAN_FALSE);
44*05767d91SRobert Wu         if (SL_RESULT_SUCCESS != result) {
45*05767d91SRobert Wu             LOGE("OutputMixerOpenSL() - Realize() mOutputMixObject result:%s", getSLErrStr(result));
46*05767d91SRobert Wu             goto error;
47*05767d91SRobert Wu         }
48*05767d91SRobert Wu     }
49*05767d91SRobert Wu 
50*05767d91SRobert Wu     return result;
51*05767d91SRobert Wu 
52*05767d91SRobert Wu error:
53*05767d91SRobert Wu     close();
54*05767d91SRobert Wu     return result;
55*05767d91SRobert Wu }
56*05767d91SRobert Wu 
close()57*05767d91SRobert Wu void OutputMixerOpenSL::close() {
58*05767d91SRobert Wu     std::lock_guard<std::mutex> lock(mLock);
59*05767d91SRobert Wu 
60*05767d91SRobert Wu     if (--mOpenCount == 0) {
61*05767d91SRobert Wu         // destroy output mix object, and invalidate all associated interfaces
62*05767d91SRobert Wu         if (mOutputMixObject != nullptr) {
63*05767d91SRobert Wu             (*mOutputMixObject)->Destroy(mOutputMixObject);
64*05767d91SRobert Wu             mOutputMixObject = nullptr;
65*05767d91SRobert Wu         }
66*05767d91SRobert Wu     }
67*05767d91SRobert Wu }
68*05767d91SRobert Wu 
createAudioPlayer(SLObjectItf * objectItf,SLDataSource * audioSource)69*05767d91SRobert Wu SLresult OutputMixerOpenSL::createAudioPlayer(SLObjectItf *objectItf,
70*05767d91SRobert Wu                                               SLDataSource *audioSource) {
71*05767d91SRobert Wu     SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, mOutputMixObject};
72*05767d91SRobert Wu     SLDataSink audioSink = {&loc_outmix, NULL};
73*05767d91SRobert Wu     return EngineOpenSLES::getInstance().createAudioPlayer(objectItf, audioSource, &audioSink);
74*05767d91SRobert Wu }
75