1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <common.h>
17 
18 using namespace OHOS::AudioStandard;
19 
20 static SLuint32 audioPlayerId = 0;
21 static SLuint32 audioRecorderId = 0;
22 static std::mutex playerIdMutex;
23 
CreateLEDDevice(SLEngineItf self,SLObjectItf * pDevice,SLuint32 deviceID,SLuint32 numInterfaces,const SLInterfaceID * pInterfaceIds,const SLboolean * pInterfaceRequired)24 static SLresult CreateLEDDevice(
25     SLEngineItf self, SLObjectItf *pDevice, SLuint32 deviceID, SLuint32 numInterfaces,
26     const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
27 {
28     return SL_RESULT_FEATURE_UNSUPPORTED;
29 }
30 
CreateVibraDevice(SLEngineItf self,SLObjectItf * pDevice,SLuint32 deviceID,SLuint32 numInterfaces,const SLInterfaceID * pInterfaceIds,const SLboolean * pInterfaceRequired)31 static SLresult CreateVibraDevice(
32     SLEngineItf self, SLObjectItf *pDevice, SLuint32 deviceID, SLuint32 numInterfaces,
33     const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
34 {
35     return SL_RESULT_FEATURE_UNSUPPORTED;
36 }
37 
CreateAudioPlayer(SLEngineItf self,SLObjectItf * pPlayer,SLDataSource * pAudioSrc,SLDataSink * pAudioSnk,SLuint32 numInterfaces,const SLInterfaceID * pInterfaceIds,const SLboolean * pInterfaceRequired)38 static SLresult CreateAudioPlayer(
39     SLEngineItf self, SLObjectItf *pPlayer, SLDataSource *pAudioSrc, SLDataSink *pAudioSnk, SLuint32 numInterfaces,
40     const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
41 {
42     if (pPlayer == nullptr) {
43         return SL_RESULT_PARAMETER_INVALID;
44     }
45     ClassTable *audioPlayerClass = ObjectIdToClass(SL_OBJECTID_AUDIOPLAYER);
46     CAudioPlayer *thiz = (CAudioPlayer *) Construct(audioPlayerClass, self);
47     if (thiz == nullptr) {
48         return SL_RESULT_PARAMETER_INVALID;
49     }
50     std::unique_lock<std::mutex> lock(playerIdMutex);
51     thiz->mId = audioPlayerId++;
52     lock.unlock();
53     IObjectInit(&thiz->mObject);
54     IPlayInit(&thiz->mPlay, audioPlayerId);
55     IVolumeInit(&thiz->mVolume, audioPlayerId);
56     IOHBufferQueueInit(&thiz->mBufferQueue, SL_IID_PLAY, audioPlayerId);
57     *pPlayer = &thiz->mObject.mItf;
58     SLresult ret = AudioPlayerAdapter::GetInstance()->
59         CreateAudioPlayerAdapter(audioPlayerId, pAudioSrc, pAudioSnk, OHOS::AudioStandard::STREAM_MUSIC);
60     if (ret != SL_RESULT_SUCCESS) {
61         return SL_RESULT_RESOURCE_ERROR;
62     }
63 
64     return SL_RESULT_SUCCESS;
65 }
66 
CreateAudioRecorder(SLEngineItf self,SLObjectItf * pRecorder,SLDataSource * pAudioSrc,SLDataSink * pAudioSnk,SLuint32 numInterfaces,const SLInterfaceID * pInterfaceIds,const SLboolean * pInterfaceRequired)67 static SLresult CreateAudioRecorder(
68     SLEngineItf self, SLObjectItf *pRecorder, SLDataSource *pAudioSrc, SLDataSink *pAudioSnk, SLuint32 numInterfaces,
69     const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
70 {
71     if (pRecorder == nullptr) {
72         return SL_RESULT_PARAMETER_INVALID;
73     }
74     ClassTable *audioRecorderClass = ObjectIdToClass(SL_OBJECTID_AUDIORECORDER);
75     CAudioRecorder *thiz = (CAudioRecorder *) Construct(audioRecorderClass, self);
76     if (thiz == nullptr) {
77         return SL_RESULT_PARAMETER_INVALID;
78     }
79     thiz->mId = audioRecorderId;
80     IObjectInit(&thiz->mObject);
81     IRecordInit(&thiz->mRecord, audioRecorderId);
82     IOHBufferQueueInit(&thiz->mBufferQueue, SL_IID_RECORD, audioRecorderId);
83     *pRecorder = &thiz->mObject.mItf;
84     SLresult ret = AudioCapturerAdapter::GetInstance()->
85         CreateAudioCapturerAdapter(audioRecorderId, pAudioSrc, pAudioSnk, OHOS::AudioStandard::STREAM_MUSIC);
86     if (ret != SL_RESULT_SUCCESS) {
87         return SL_RESULT_RESOURCE_ERROR;
88     }
89     audioRecorderId++;
90 
91     return SL_RESULT_SUCCESS;
92 }
93 
CreateMidiPlayer(SLEngineItf self,SLObjectItf * pPlayer,SLDataSource * pMIDISrc,SLDataSource * pBankSrc,SLDataSink * pAudioOutput,SLDataSink * pVibra,SLDataSink * pLEDArray,SLuint32 numInterfaces,const SLInterfaceID * pInterfaceIds,const SLboolean * pInterfaceRequired)94 static SLresult CreateMidiPlayer(
95     SLEngineItf self, SLObjectItf *pPlayer, SLDataSource *pMIDISrc, SLDataSource *pBankSrc, SLDataSink *pAudioOutput,
96     SLDataSink *pVibra, SLDataSink *pLEDArray, SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
97     const SLboolean *pInterfaceRequired)
98 {
99     return SL_RESULT_FEATURE_UNSUPPORTED;
100 }
101 
CreateListener(SLEngineItf self,SLObjectItf * pListener,SLuint32 numInterfaces,const SLInterfaceID * pInterfaceIds,const SLboolean * pInterfaceRequired)102 static SLresult CreateListener(
103     SLEngineItf self, SLObjectItf *pListener, SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
104     const SLboolean *pInterfaceRequired)
105 {
106     return SL_RESULT_FEATURE_UNSUPPORTED;
107 }
108 
Create3DGroup(SLEngineItf self,SLObjectItf * pGroup,SLuint32 numInterfaces,const SLInterfaceID * pInterfaceIds,const SLboolean * pInterfaceRequired)109 static SLresult Create3DGroup(
110     SLEngineItf self, SLObjectItf *pGroup, SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
111     const SLboolean *pInterfaceRequired)
112 {
113     return SL_RESULT_FEATURE_UNSUPPORTED;
114 }
115 
CreateOutputMix(SLEngineItf self,SLObjectItf * pMix,SLuint32 numInterfaces,const SLInterfaceID * pInterfaceIds,const SLboolean * pInterfaceRequired)116 static SLresult CreateOutputMix(
117     SLEngineItf self, SLObjectItf *pMix, SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
118     const SLboolean *pInterfaceRequired)
119 {
120     if (pMix == nullptr) {
121         return SL_RESULT_PARAMETER_INVALID;
122     }
123     ClassTable *outputMixClass = ObjectIdToClass(SL_OBJECTID_OUTPUTMIX);
124     COutputMix *thiz = (COutputMix *) Construct(outputMixClass, self);
125     if (thiz == nullptr) {
126         return SL_RESULT_PARAMETER_INVALID;
127     }
128     IObjectInit(&thiz->mObject);
129     *pMix = &thiz->mObject.mItf;
130     return SL_RESULT_SUCCESS;
131 }
132 
CreateMetadataExtractor(SLEngineItf self,SLObjectItf * pMetadataExtractor,SLDataSource * pDataSource,SLuint32 numInterfaces,const SLInterfaceID * pInterfaceIds,const SLboolean * pInterfaceRequired)133 static SLresult CreateMetadataExtractor(
134     SLEngineItf self, SLObjectItf *pMetadataExtractor, SLDataSource *pDataSource, SLuint32 numInterfaces,
135     const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
136 {
137     return SL_RESULT_FEATURE_UNSUPPORTED;
138 }
139 
CreateExtensionObject(SLEngineItf self,SLObjectItf * pObject,void * pParameters,SLuint32 objectID,SLuint32 numInterfaces,const SLInterfaceID * pInterfaceIds,const SLboolean * pInterfaceRequired)140 static SLresult CreateExtensionObject(
141     SLEngineItf self, SLObjectItf *pObject, void *pParameters, SLuint32 objectID, SLuint32 numInterfaces,
142     const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
143 {
144     return SL_RESULT_FEATURE_UNSUPPORTED;
145 }
146 
QueryNumSupportedInterfaces(SLEngineItf self,SLuint32 objectID,SLuint32 * pNumSupportedInterfaces)147 static SLresult QueryNumSupportedInterfaces(SLEngineItf self, SLuint32 objectID, SLuint32 *pNumSupportedInterfaces)
148 {
149     return SL_RESULT_FEATURE_UNSUPPORTED;
150 }
151 
QuerySupportedInterfaces(SLEngineItf self,SLuint32 objectID,SLuint32 index,SLInterfaceID * pInterfaceId)152 static SLresult QuerySupportedInterfaces(
153     SLEngineItf self, SLuint32 objectID, SLuint32 index, SLInterfaceID *pInterfaceId)
154 {
155     return SL_RESULT_FEATURE_UNSUPPORTED;
156 }
157 
QueryNumSupportedExtensions(SLEngineItf self,SLuint32 * pNumExtensions)158 static SLresult QueryNumSupportedExtensions(SLEngineItf self, SLuint32 *pNumExtensions)
159 {
160     return SL_RESULT_FEATURE_UNSUPPORTED;
161 }
162 
QuerySupportedExtension(SLEngineItf self,SLuint32 index,SLchar * pExtensionName,SLint16 * pNameLength)163 static SLresult QuerySupportedExtension(
164     SLEngineItf self, SLuint32 index, SLchar *pExtensionName, SLint16 *pNameLength)
165 {
166     return SL_RESULT_FEATURE_UNSUPPORTED;
167 }
168 
IsExtensionSupported(SLEngineItf self,const SLchar * pExtensionName,SLboolean * pSupported)169 static SLresult IsExtensionSupported(SLEngineItf self, const SLchar *pExtensionName, SLboolean *pSupported)
170 {
171     return SL_RESULT_FEATURE_UNSUPPORTED;
172 }
173 
174 static const struct SLEngineItf_ EngineItf = {
175     CreateLEDDevice,
176     CreateVibraDevice,
177     CreateAudioPlayer,
178     CreateAudioRecorder,
179     CreateMidiPlayer,
180     CreateListener,
181     Create3DGroup,
182     CreateOutputMix,
183     CreateMetadataExtractor,
184     CreateExtensionObject,
185     QueryNumSupportedInterfaces,
186     QuerySupportedInterfaces,
187     QueryNumSupportedExtensions,
188     QuerySupportedExtension,
189     IsExtensionSupported
190 };
191 
IEngineInit(void * self)192 void IEngineInit(void *self)
193 {
194     IEngine *thiz = (IEngine *) self;
195     thiz->mItf = &EngineItf;
196 }
197