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 
slCreateEngine(SLObjectItf * pEngine,SLuint32 numOptions,const SLEngineOption * pEngineOptions,SLuint32 numInterfaces,const SLInterfaceID * pInterfaceIds,const SLboolean * pInterfaceRequired)18 SLresult SLAPIENTRY slCreateEngine(SLObjectItf *pEngine, SLuint32 numOptions,
19     const SLEngineOption *pEngineOptions, SLuint32 numInterfaces,
20     const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
21 {
22     if (pEngine == nullptr) {
23         return SL_RESULT_PARAMETER_INVALID;
24     }
25     ClassTable *engineClass = ObjectIdToClass(SL_OBJECTID_ENGINE);
26     CEngine *thiz = (CEngine *) Construct(engineClass, nullptr);
27     if (thiz == nullptr) {
28         return SL_RESULT_PARAMETER_INVALID;
29     }
30     IObjectInit(&thiz->mObject);
31     IEngineInit(&thiz->mEngine);
32     *pEngine = &thiz->mObject.mItf;
33     return SL_RESULT_SUCCESS;
34 }
35 
ObjectIdToClass(SLuint32 objectId)36 ClassTable *ObjectIdToClass(SLuint32 objectId)
37 {
38     ClassTable *classTable = nullptr;
39     if (objectId == SL_OBJECTID_ENGINE) {
40         classTable = (ClassTable *) &EngineTab;
41     } else if (objectId == SL_OBJECTID_AUDIOPLAYER) {
42         classTable = (ClassTable *) &AudioPlayerTab;
43     } else if (objectId == SL_OBJECTID_AUDIORECORDER) {
44         classTable = (ClassTable *) &AudioRecorderTab;
45     } else if (objectId == SL_OBJECTID_OUTPUTMIX) {
46         classTable = (ClassTable *) &OutputMixTab;
47     }
48     return classTable;
49 }
50 
Construct(const ClassTable * classTable,SLEngineItf engine)51 IObject *Construct(const ClassTable *classTable, SLEngineItf engine)
52 {
53     if (classTable == nullptr) {
54         return nullptr;
55     }
56     IObject *thiz = (IObject *) calloc(1, classTable->mSize);
57     if (thiz != nullptr) {
58         IEngine *thisEngine = (IEngine *) engine;
59         if (thisEngine != nullptr) {
60             thiz->mEngine = (CEngine *) thisEngine->mThis;
61         } else {
62             thiz->mEngine = (CEngine *) thiz;
63         }
64         thiz->mClass = classTable;
65     }
66     return thiz;
67 }
68