• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "capturer_clock_manager.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 using namespace OHOS::AudioStandard;
32 
GetCaptureSource(uint32_t captureId)33 static inline std::shared_ptr<IAudioCaptureSource> GetCaptureSource(uint32_t captureId)
34 {
35     HdiAdapterManager &manager = HdiAdapterManager::GetInstance();
36     return manager.GetCaptureSource(captureId, true);
37 }
38 
InitSourceAdapter(struct SourceAdapter * adapter,const char * deviceClass,const int32_t sourceType,const char * info)39 int32_t InitSourceAdapter(struct SourceAdapter *adapter, const char *deviceClass, const int32_t sourceType,
40     const char *info)
41 {
42     CHECK_AND_RETURN_RET_LOG(adapter != nullptr, ERR_INVALID_HANDLE, "adapter is nullptr");
43 
44     adapter->captureId = HDI_INVALID_ID;
45     adapter->deviceClass = strdup(deviceClass);
46     CHECK_AND_RETURN_RET_LOG(adapter->deviceClass != nullptr, ERR_OPERATION_FAILED, "strdup fail");
47     if (info == nullptr) {
48         adapter->captureId = HdiAdapterManager::GetInstance().GetCaptureIdByDeviceClass(deviceClass,
49             static_cast<SourceType>(sourceType), HDI_ID_INFO_DEFAULT, true);
50     } else {
51         adapter->captureId = HdiAdapterManager::GetInstance().GetCaptureIdByDeviceClass(deviceClass,
52             static_cast<SourceType>(sourceType), std::string(info), true);
53     }
54     std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
55     if (source == nullptr) {
56         AUDIO_ERR_LOG("get source fail, deviceClass: %{public}s, info: %{public}s, captureId: %{public}u",
57             deviceClass, info, adapter->captureId);
58         HdiAdapterManager::GetInstance().ReleaseId(adapter->captureId);
59         return ERR_OPERATION_FAILED;
60     }
61     adapter->attr = nullptr;
62     return SUCCESS;
63 }
64 
DeInitSourceAdapter(struct SourceAdapter * adapter)65 void DeInitSourceAdapter(struct SourceAdapter *adapter)
66 {
67     CHECK_AND_RETURN_LOG(adapter != nullptr, "adapter is nullptr");
68     HdiAdapterManager::GetInstance().ReleaseId(adapter->captureId);
69     if (adapter->deviceClass != nullptr) {
70         free(const_cast<char *>(adapter->deviceClass));
71         adapter->deviceClass = nullptr;
72     }
73 }
74 
SourceAdapterInit(struct SourceAdapter * adapter,const struct SourceAdapterAttr * attr)75 int32_t SourceAdapterInit(struct SourceAdapter *adapter, const struct SourceAdapterAttr *attr)
76 {
77     CHECK_AND_RETURN_RET_LOG(adapter != nullptr && adapter->captureId != HDI_INVALID_ID, ERR_INVALID_HANDLE,
78         "invalid adapter");
79     CHECK_AND_RETURN_RET_LOG(attr != nullptr, ERR_INVALID_PARAM, "attr is nullptr");
80     std::shared_ptr<IAudioCaptureSource> source = GetCaptureSource(adapter->captureId);
81     CHECK_AND_RETURN_RET_LOG(source != nullptr, ERR_INVALID_HANDLE, "get source fail");
82     if (source->IsInited()) {
83         return SUCCESS;
84     }
85 
86     IAudioSourceAttr sourceAttr = {};
87     sourceAttr.adapterName = attr->adapterName;
88     sourceAttr.openMicSpeaker = attr->openMicSpeaker;
89     sourceAttr.format = static_cast<AudioSampleFormat>(attr->format);
90     sourceAttr.sampleRate = attr->sampleRate;
91     sourceAttr.channel = attr->channel;
92     sourceAttr.volume = attr->volume;
93     sourceAttr.bufferSize = attr->bufferSize;
94     sourceAttr.isBigEndian = attr->isBigEndian;
95     sourceAttr.filePath = attr->filePath;
96     sourceAttr.deviceNetworkId = attr->deviceNetworkId;
97     sourceAttr.deviceType = attr->deviceType;
98     sourceAttr.sourceType = attr->sourceType;
99     sourceAttr.channelLayout = attr->channelLayout;
100     sourceAttr.hasEcConfig = attr->hasEcConfig;
101     sourceAttr.formatEc = static_cast<AudioSampleFormat>(attr->formatEc);
102     sourceAttr.sampleRateEc = attr->sampleRateEc;
103     sourceAttr.channelEc = attr->channelEc;
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 
SourceAdapterUpdateSessionUid(struct SourceAdapter * adapter,const int32_t sessionId[PA_MAX_OUTPUTS_PER_SOURCE],const size_t size)225 int32_t SourceAdapterUpdateSessionUid(struct SourceAdapter *adapter, const int32_t sessionId[PA_MAX_OUTPUTS_PER_SOURCE],
226     const size_t size)
227 {
228     CHECK_AND_RETURN_RET_LOG(adapter != nullptr && adapter->captureId != HDI_INVALID_ID, ERR_INVALID_HANDLE,
229         "invalid adapter");
230 
231     std::shared_ptr<AudioSourceClock> clock =
232         CapturerClockManager::GetInstance().GetAudioSourceClock(adapter->captureId);
233     CHECK_AND_RETURN_RET_LOG(clock != nullptr, ERR_INVALID_HANDLE, "AudioSourceClock unfound!");
234 
235     std::vector<int32_t> sessionIdList;
236     for (size_t i = 0; i < size; i++) {
237         sessionIdList.push_back(sessionId[i]);
238     }
239     clock->UpdateSessionId(sessionIdList);
240 
241     return SUCCESS;
242 }
243 
244 #ifdef __cplusplus
245 }
246 #endif
247