blob: 901e9a75ed0acf22a8bf6b38af7946b3997d2a6e [file] [log] [blame]
Phil Burk420715b2017-12-14 12:26:38 -08001/*
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
23using namespace oboe;
24
Phil Burk7040a9d2017-12-15 10:54:38 -080025OutputMixerOpenSL &OutputMixerOpenSL::getInstance() {
Phil Burk420715b2017-12-14 12:26:38 -080026 static OutputMixerOpenSL sInstance;
Phil Burk7040a9d2017-12-15 10:54:38 -080027 return sInstance;
Phil Burk420715b2017-12-14 12:26:38 -080028}
29
30SLresult 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 Burk7040a9d2017-12-15 10:54:38 -080036 result = EngineOpenSLES::getInstance().createOutputMix(&mOutputMixObject);
Phil Burk420715b2017-12-14 12:26:38 -080037 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
52error:
53 close();
54 return result;
55}
56
57void 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
69SLresult OutputMixerOpenSL::createAudioPlayer(SLObjectItf *objectItf,
70 SLDataSource *audioSource) {
71 SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, mOutputMixObject};
72 SLDataSink audioSink = {&loc_outmix, NULL};
Phil Burk7040a9d2017-12-15 10:54:38 -080073 return EngineOpenSLES::getInstance().createAudioPlayer(objectItf, audioSource, &audioSink);
Phil Burk420715b2017-12-14 12:26:38 -080074}