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 #ifndef LOG_TAG
16 #define LOG_TAG "ObjectItf"
17 #endif
18
19 #include <common.h>
20
21 using namespace OHOS::AudioStandard;
22
Realize(SLObjectItf self,SLboolean async)23 static SLresult Realize(SLObjectItf self, SLboolean async)
24 {
25 if (self == nullptr) {
26 return SL_RESULT_PARAMETER_INVALID;
27 }
28 IObject *thiz = (IObject *) self;
29 thiz->mState = SL_OBJECT_STATE_REALIZED;
30 return SL_RESULT_SUCCESS;
31 }
32
Resume(SLObjectItf self,SLboolean async)33 static SLresult Resume(SLObjectItf self, SLboolean async)
34 {
35 return SL_RESULT_SUCCESS;
36 }
37
GetState(SLObjectItf self,SLuint32 * state)38 static SLresult GetState(SLObjectItf self, SLuint32 *state)
39 {
40 if (self == nullptr) {
41 return SL_RESULT_PARAMETER_INVALID;
42 }
43 IObject *thiz = (IObject *) self;
44 *state = thiz->mState;
45 return SL_RESULT_SUCCESS;
46 }
47
GetInterface(SLObjectItf self,const SLInterfaceID iid,void * interface)48 static SLresult GetInterface(SLObjectItf self, const SLInterfaceID iid, void *interface)
49 {
50 if (self == nullptr) {
51 return SL_RESULT_PARAMETER_INVALID;
52 }
53 if (iid == SL_IID_ENGINE) {
54 CEngine *cEngine = (CEngine *)self;
55 *(void **)interface = (void *)&(cEngine->mEngine.mItf);
56 return SL_RESULT_SUCCESS;
57 } else if (iid == SL_IID_PLAY) {
58 CAudioPlayer *cAudioPlayer = (CAudioPlayer *)self;
59 *(void **)interface = (void *)&(cAudioPlayer->mPlay.mItf);
60 return SL_RESULT_SUCCESS;
61 } else if (iid == SL_IID_VOLUME) {
62 CAudioPlayer *cAudioPlayer = (CAudioPlayer *)self;
63 *(void **)interface = (void *)&(cAudioPlayer->mVolume.mItf);
64 return SL_RESULT_SUCCESS;
65 } else if (iid == SL_IID_OH_BUFFERQUEUE) {
66 IObject *iObject = (IObject *)self;
67 if (iObject->mClass->mObjectId == SL_OBJECTID_AUDIOPLAYER) {
68 CAudioPlayer *cAudioPlayer = (CAudioPlayer *)iObject;
69 *(void **)interface = (void *)&(cAudioPlayer->mBufferQueue.mItf);
70 } else if (iObject->mClass->mObjectId == SL_OBJECTID_AUDIORECORDER) {
71 CAudioRecorder *cAudioRecorder = (CAudioRecorder *)iObject;
72 *(void **)interface = (void *)&(cAudioRecorder->mBufferQueue.mItf);
73 } else {
74 return SL_RESULT_FEATURE_UNSUPPORTED;
75 }
76 return SL_RESULT_SUCCESS;
77 } else if (iid == SL_IID_RECORD) {
78 CAudioRecorder *cAudioRecorder = (CAudioRecorder *)self;
79 *(void **)interface = (void *)&(cAudioRecorder->mRecord.mItf);
80 return SL_RESULT_SUCCESS;
81 } else {
82 AUDIO_ERR_LOG("GetInterface: SLInterfaceID not supported");
83 return SL_RESULT_FEATURE_UNSUPPORTED;
84 }
85 }
86
RegisterCallback(SLObjectItf self,slObjectCallback callback,void * pContext)87 static SLresult RegisterCallback(SLObjectItf self, slObjectCallback callback, void *pContext)
88 {
89 return SL_RESULT_FEATURE_UNSUPPORTED;
90 }
91
AbortAsyncOperation(SLObjectItf self)92 static void AbortAsyncOperation(SLObjectItf self)
93 {
94 return;
95 }
96
SetPriority(SLObjectItf self,SLint32 priority,SLboolean preemptable)97 static SLresult SetPriority(SLObjectItf self, SLint32 priority, SLboolean preemptable)
98 {
99 return SL_RESULT_FEATURE_UNSUPPORTED;
100 }
101
GetPriority(SLObjectItf self,SLint32 * pPriority,SLboolean * pPreemptable)102 static SLresult GetPriority(SLObjectItf self, SLint32 *pPriority, SLboolean *pPreemptable)
103 {
104 return SL_RESULT_FEATURE_UNSUPPORTED;
105 }
106
SetLossOfControlInterfaces(SLObjectItf self,SLint16 numInterfaces,SLInterfaceID * pInterfaceIDs,SLboolean enabled)107 static SLresult SetLossOfControlInterfaces(SLObjectItf self,
108 SLint16 numInterfaces, SLInterfaceID *pInterfaceIDs, SLboolean enabled)
109 {
110 return SL_RESULT_FEATURE_UNSUPPORTED;
111 }
112
Destroy(SLObjectItf self)113 void Destroy(SLObjectItf self)
114 {
115 if (self == nullptr) {
116 return;
117 }
118 IObject *iObject = (IObject *)self;
119 SLuint32 objectId = iObject->mClass->mObjectId;
120 switch (objectId) {
121 case SL_OBJECTID_AUDIOPLAYER:
122 AudioPlayerDestroy((void *)self);
123 break;
124 case SL_OBJECTID_ENGINE:
125 EngineDestory((void *)self);
126 break;
127 case SL_OBJECTID_OUTPUTMIX:
128 OutputMixDestroy((void *)self);
129 break;
130 case SL_OBJECTID_AUDIORECORDER:
131 AudioRecorderDestroy((void *)self);
132 break;
133 default:
134 AUDIO_ERR_LOG("objectId not supported");
135 break;
136 }
137 return;
138 }
139
140 static const struct SLObjectItf_ ObjectItf = {
141 Realize,
142 Resume,
143 GetState,
144 GetInterface,
145 RegisterCallback,
146 AbortAsyncOperation,
147 Destroy,
148 SetPriority,
149 GetPriority,
150 SetLossOfControlInterfaces
151 };
152
IObjectInit(void * self)153 void IObjectInit(void *self)
154 {
155 IObject *thiz = (IObject *) self;
156 thiz->mItf = &ObjectItf;
157 thiz->mState = SL_OBJECT_STATE_UNREALIZED;
158 }
159