1 /*
2 * Copyright (c) 2025 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 #ifndef LOG_TAG
17 #define LOG_TAG "SourceAdapter"
18 #endif
19
20 #include "source/source_adapter.h"
21 #include "audio_errors.h"
22 #include "audio_hdi_log.h"
23 #include "source/i_audio_capture_source.h"
24 #include "manager/hdi_adapter_manager.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 using namespace OHOS::AudioStandard;
31
GetCaptureSource(uint32_t captureId)32 static inline std::shared_ptr<IAudioCaptureSource> GetCaptureSource(uint32_t captureId)
33 {
34 HdiAdapterManager &manager = HdiAdapterManager::GetInstance();
35 return manager.GetCaptureSource(captureId, true);
36 }
37
InitSourceAdapter(struct SourceAdapter * adapter,const char * deviceClass,const int32_t sourceType,const char * info)38 int32_t InitSourceAdapter(struct SourceAdapter *adapter, const char *deviceClass, const int32_t sourceType,
39 const char *info)
40 {
41 CHECK_AND_RETURN_RET_LOG(adapter != nullptr, ERR_INVALID_HANDLE, "adapter is nullptr");
42
43 adapter->captureId = HDI_INVALID_ID;
44 adapter->deviceClass = nullptr;
45 if (info == nullptr) {
46 adapter->captureId = HdiAdapterManager::GetInstance().GetCaptureIdByDeviceClass(deviceClass,
47 static_cast<SourceType>(sourceType), HDI_ID_INFO_DEFAULT, true);
48 } else {
49 adapter->captureId = HdiAdapterManager::GetInstance().GetCaptureIdByDeviceClass(deviceClass,
50 static_cast<SourceType>(sourceType), std::string(info), true);
51 }
52 std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
53 if (source == nullptr) {
54 AUDIO_ERR_LOG("get source fail, deviceClass: %{public}s, info: %{public}s, captureId: %{public}u",
55 deviceClass, info, adapter->captureId);
56 HdiAdapterManager::GetInstance().ReleaseId(adapter->captureId);
57 return ERR_OPERATION_FAILED;
58 }
59 adapter->deviceClass = strdup(deviceClass);
60 adapter->attr = nullptr;
61 return SUCCESS;
62 }
63
DeInitSourceAdapter(struct SourceAdapter * adapter)64 void DeInitSourceAdapter(struct SourceAdapter *adapter)
65 {
66 CHECK_AND_RETURN_LOG(adapter != nullptr, "adapter is nullptr");
67 HdiAdapterManager::GetInstance().ReleaseId(adapter->captureId);
68 if (adapter->deviceClass != nullptr) {
69 free(const_cast<char *>(adapter->deviceClass));
70 adapter->deviceClass = nullptr;
71 }
72 }
73
SourceAdapterInit(struct SourceAdapter * adapter,const struct SourceAdapterAttr * attr)74 int32_t SourceAdapterInit(struct SourceAdapter *adapter, const struct SourceAdapterAttr *attr)
75 {
76 CHECK_AND_RETURN_RET_LOG(adapter != nullptr && adapter->captureId != HDI_INVALID_ID, ERR_INVALID_HANDLE,
77 "invalid adapter");
78 CHECK_AND_RETURN_RET_LOG(attr != nullptr, ERR_INVALID_PARAM, "attr is nullptr");
79 std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
80 CHECK_AND_RETURN_RET_LOG(source != nullptr, ERR_INVALID_HANDLE, "get source fail");
81 if (source->IsInited()) {
82 return SUCCESS;
83 }
84
85 IAudioSourceAttr sourceAttr = {
86 .adapterName = attr->adapterName,
87 .openMicSpeaker = attr->openMicSpeaker,
88 .format = static_cast<AudioSampleFormat>(attr->format),
89 .sampleRate = attr->sampleRate,
90 .channel = attr->channel,
91 .volume = attr->volume,
92 .bufferSize = attr->bufferSize,
93 .isBigEndian = attr->isBigEndian,
94 .filePath = attr->filePath,
95 .deviceNetworkId = attr->deviceNetworkId,
96 .deviceType = attr->deviceType,
97 .sourceType = attr->sourceType,
98 .channelLayout = attr->channelLayout,
99 .hasEcConfig = attr->hasEcConfig,
100 .formatEc = static_cast<AudioSampleFormat>(attr->formatEc),
101 .sampleRateEc = attr->sampleRateEc,
102 .channelEc = attr->channelEc,
103 };
104
105 return source->Init(sourceAttr);
106 }
107
SourceAdapterDeInit(struct SourceAdapter * adapter)108 void SourceAdapterDeInit(struct SourceAdapter *adapter)
109 {
110 CHECK_AND_RETURN_LOG(adapter != nullptr && adapter->captureId != HDI_INVALID_ID, "invalid adapter");
111 std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
112 CHECK_AND_RETURN_LOG(source != nullptr, "get source fail");
113 if (!source->IsInited()) {
114 return;
115 }
116
117 source->DeInit();
118 }
119
SourceAdapterStart(struct SourceAdapter * adapter)120 int32_t SourceAdapterStart(struct SourceAdapter *adapter)
121 {
122 CHECK_AND_RETURN_RET_LOG(adapter != nullptr && adapter->captureId != HDI_INVALID_ID, ERR_INVALID_HANDLE,
123 "invalid adapter");
124 std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
125 CHECK_AND_RETURN_RET_LOG(source != nullptr, ERR_INVALID_HANDLE, "get source fail");
126 CHECK_AND_RETURN_RET_LOG(source->IsInited(), ERR_ILLEGAL_STATE, "source not init");
127
128 return source->Start();
129 }
130
SourceAdapterStop(struct SourceAdapter * adapter)131 int32_t SourceAdapterStop(struct SourceAdapter *adapter)
132 {
133 CHECK_AND_RETURN_RET_LOG(adapter != nullptr && adapter->captureId != HDI_INVALID_ID, ERR_INVALID_HANDLE,
134 "invalid adapter");
135 std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
136 CHECK_AND_RETURN_RET_LOG(source != nullptr, ERR_INVALID_HANDLE, "get source fail");
137 CHECK_AND_RETURN_RET_LOG(source->IsInited(), ERR_ILLEGAL_STATE, "source not init");
138
139 return source->Stop();
140 }
141
SourceAdapterCaptureFrame(struct SourceAdapter * adapter,char * frame,uint64_t requestBytes,uint64_t * replyBytes)142 int32_t SourceAdapterCaptureFrame(struct SourceAdapter *adapter, char *frame, uint64_t requestBytes,
143 uint64_t *replyBytes)
144 {
145 CHECK_AND_RETURN_RET_LOG(adapter != nullptr && adapter->captureId != HDI_INVALID_ID, ERR_INVALID_HANDLE,
146 "invalid adapter");
147 std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
148 CHECK_AND_RETURN_RET_LOG(source != nullptr, ERR_INVALID_HANDLE, "get source fail");
149 CHECK_AND_RETURN_RET_LOG(source->IsInited(), ERR_ILLEGAL_STATE, "source not init");
150
151 return source->CaptureFrame(frame, requestBytes, *replyBytes);
152 }
153
SourceAdapterCaptureFrameWithEc(struct SourceAdapter * adapter,struct SourceAdapterFrameDesc * fdesc,uint64_t * replyBytes,struct SourceAdapterFrameDesc * fdescEc,uint64_t * replyBytesEc)154 int32_t SourceAdapterCaptureFrameWithEc(struct SourceAdapter *adapter, struct SourceAdapterFrameDesc *fdesc,
155 uint64_t *replyBytes, struct SourceAdapterFrameDesc *fdescEc, uint64_t *replyBytesEc)
156 {
157 CHECK_AND_RETURN_RET_LOG(adapter != nullptr && adapter->captureId != HDI_INVALID_ID, ERR_INVALID_HANDLE,
158 "invalid adapter");
159 std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
160 CHECK_AND_RETURN_RET_LOG(source != nullptr, ERR_INVALID_HANDLE, "get source fail");
161 CHECK_AND_RETURN_RET_LOG(source->IsInited(), ERR_ILLEGAL_STATE, "source not init");
162
163 return source->CaptureFrameWithEc(reinterpret_cast<FrameDesc *>(fdesc), *replyBytes,
164 reinterpret_cast<FrameDesc *>(fdescEc), *replyBytesEc);
165 }
166
SourceAdapterSetVolume(struct SourceAdapter * adapter,float left,float right)167 int32_t SourceAdapterSetVolume(struct SourceAdapter *adapter, float left, float right)
168 {
169 CHECK_AND_RETURN_RET_LOG(adapter != nullptr && adapter->captureId != HDI_INVALID_ID, ERR_INVALID_HANDLE,
170 "invalid adapter");
171 std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
172 CHECK_AND_RETURN_RET_LOG(source != nullptr, ERR_INVALID_HANDLE, "get source fail");
173 CHECK_AND_RETURN_RET_LOG(source->IsInited(), ERR_ILLEGAL_STATE, "source not init");
174
175 return source->SetVolume(left, right);
176 }
177
SourceAdapterGetVolume(struct SourceAdapter * adapter,float * left,float * right)178 int32_t SourceAdapterGetVolume(struct SourceAdapter *adapter, float *left, float *right)
179 {
180 CHECK_AND_RETURN_RET_LOG(adapter != nullptr && adapter->captureId != HDI_INVALID_ID, ERR_INVALID_HANDLE,
181 "invalid adapter");
182 std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
183 CHECK_AND_RETURN_RET_LOG(source != nullptr, ERR_INVALID_HANDLE, "get source fail");
184 CHECK_AND_RETURN_RET_LOG(source->IsInited(), ERR_ILLEGAL_STATE, "source not init");
185 CHECK_AND_RETURN_RET_LOG(left != nullptr && right != nullptr, ERR_INVALID_PARAM, "invalid param");
186
187 return source->GetVolume(*left, *right);
188 }
189
SourceAdapterSetMute(struct SourceAdapter * adapter,bool isMute)190 int32_t SourceAdapterSetMute(struct SourceAdapter *adapter, bool isMute)
191 {
192 CHECK_AND_RETURN_RET_LOG(adapter != nullptr && adapter->captureId != HDI_INVALID_ID, ERR_INVALID_HANDLE,
193 "invalid adapter");
194 std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
195 CHECK_AND_RETURN_RET_LOG(source != nullptr, ERR_INVALID_HANDLE, "get source fail");
196 CHECK_AND_RETURN_RET_LOG(source->IsInited(), ERR_ILLEGAL_STATE, "source not init");
197
198 return source->SetMute(isMute);
199 }
200
SourceAdapterGetMute(struct SourceAdapter * adapter)201 bool SourceAdapterGetMute(struct SourceAdapter *adapter)
202 {
203 bool isMute = false;
204 CHECK_AND_RETURN_RET_LOG(adapter != nullptr && adapter->captureId != HDI_INVALID_ID, isMute, "invalid adapter");
205 std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
206 CHECK_AND_RETURN_RET_LOG(source != nullptr, isMute, "get source fail");
207 CHECK_AND_RETURN_RET_LOG(source->IsInited(), isMute, "source not init");
208
209 source->GetMute(isMute);
210 return isMute;
211 }
212
SourceAdapterUpdateAppsUid(struct SourceAdapter * adapter,const int32_t appsUid[PA_MAX_OUTPUTS_PER_SOURCE],const size_t size)213 int32_t SourceAdapterUpdateAppsUid(struct SourceAdapter *adapter, const int32_t appsUid[PA_MAX_OUTPUTS_PER_SOURCE],
214 const size_t size)
215 {
216 CHECK_AND_RETURN_RET_LOG(adapter != nullptr && adapter->captureId != HDI_INVALID_ID, ERR_INVALID_HANDLE,
217 "invalid adapter");
218 std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
219 CHECK_AND_RETURN_RET_LOG(source != nullptr, ERR_INVALID_HANDLE, "get source fail");
220 CHECK_AND_RETURN_RET_LOG(source->IsInited(), ERR_ILLEGAL_STATE, "source not init");
221
222 return source->UpdateAppsUid(appsUid, size);
223 }
224
225 #ifdef __cplusplus
226 }
227 #endif
228