• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #ifndef LOG_TAG
16 #define LOG_TAG "IAudioCapturerSource"
17 #endif
18 
19 #include "i_audio_capturer_source.h"
20 
21 #include <cstring>
22 #include <string>
23 
24 #include "audio_hdi_log.h"
25 #include "audio_errors.h"
26 #include "i_audio_capturer_source_intf.h"
27 #include "audio_capturer_source.h"
28 #include "audio_capturer_file_source.h"
29 #include "bluetooth_capturer_source.h"
30 #ifdef DAUDIO_ENABLE
31 #include "remote_audio_capturer_source.h"
32 #endif
33 
34 using namespace std;
35 
36 namespace OHOS {
37 namespace AudioStandard {
GetInstance(const char * deviceClass,const char * deviceNetworkId,const SourceType sourceType,const char * sourceName)38 IAudioCapturerSource *IAudioCapturerSource::GetInstance(const char *deviceClass, const char *deviceNetworkId,
39     const SourceType sourceType, const char *sourceName)
40 {
41     AUDIO_DEBUG_LOG("%{public}s Source:GetInstance deviceNetworkId:[%{public}s] sourceType:[%{public}d]",
42         deviceClass, deviceNetworkId, sourceType);
43     const char *deviceClassPrimary = "primary";
44     const char *deviceClassUsb = "usb";
45     const char *deviceClassA2DP = "a2dp";
46 #ifdef FEATURE_FILE_IO
47     const char *deviceClassFile = "file_io";
48 #endif
49 #ifdef DAUDIO_ENABLE
50     const char *deviceClassRemote = "remote";
51 #endif
52 
53     if (!strcmp(deviceClass, deviceClassPrimary)) {
54         return AudioCapturerSource::GetInstance("primary", sourceType, sourceName);
55     }
56     if (!strcmp(deviceClass, deviceClassUsb)) {
57         return AudioCapturerSource::GetInstance("usb", sourceType, sourceName);
58     }
59     if (!strcmp(deviceClass, deviceClassA2DP)) {
60         return BluetoothCapturerSource::GetInstance();
61     }
62 #ifdef FEATURE_FILE_IO
63     if (!strcmp(deviceClass, deviceClassFile)) {
64         static AudioCapturerFileSource audioCapturer;
65         return &audioCapturer;
66     }
67 #endif
68 #ifdef DAUDIO_ENABLE
69     if (!strcmp(deviceClass, deviceClassRemote)) {
70         std::string networkId = deviceNetworkId;
71         RemoteAudioCapturerSource *rSource = RemoteAudioCapturerSource::GetInstance(networkId);
72         return rSource;
73     }
74 #endif
75     return nullptr;
76 }
77 
GetAllInstance(std::vector<IAudioCapturerSource * > & allInstance)78 void IAudioCapturerSource::GetAllInstance(std::vector<IAudioCapturerSource *> &allInstance)
79 {
80 #ifdef DAUDIO_ENABLE
81     RemoteAudioCapturerSource::GetAllInstance(allInstance);
82 #endif
83     allInstance.push_back(AudioCapturerSource::GetInstance());
84     allInstance.push_back(AudioCapturerSource::GetInstance("usb", SourceType::SOURCE_TYPE_MIC, "Usb_Arm_Speaker_In"));
85     allInstance.push_back(BluetoothCapturerSource::GetInstance());
86 }
87 
Create(CaptureAttr * attr)88 IAudioCapturerSource *IAudioCapturerSource::Create(CaptureAttr *attr)
89 {
90     return AudioCapturerSource::Create(attr);
91 }
92 
93 } // namespace AudioStandard
94 } // namesapce OHOS
95 
96 #ifdef __cplusplus
97 extern "C" {
98 #endif
99 
100 using namespace OHOS::AudioStandard;
101 
FillinSourceWapper(const char * deviceClass,const char * deviceNetworkId,const int32_t sourceType,const char * sourceName,void ** wapper)102 int32_t FillinSourceWapper(const char *deviceClass, const char *deviceNetworkId,
103     const int32_t sourceType, const char *sourceName, void **wapper)
104 {
105     IAudioCapturerSource *iSource = IAudioCapturerSource::GetInstance(deviceClass,
106         deviceNetworkId,
107         static_cast<SourceType>(sourceType),
108         sourceName);
109 
110     if (iSource != nullptr) {
111         *wapper = static_cast<void *>(iSource);
112         return SUCCESS;
113     }
114     return ERROR;
115 }
116 
117 IAudioCapturerSource *iAudioCapturerSource = nullptr;
118 
IAudioCapturerSourceInit(void * wapper,const SourceAttr * attr)119 int32_t IAudioCapturerSourceInit(void *wapper, const SourceAttr *attr)
120 {
121     int32_t ret;
122 
123     IAudioCapturerSource *iAudioCapturerSource = static_cast<IAudioCapturerSource *>(wapper);
124     CHECK_AND_RETURN_RET_LOG(iAudioCapturerSource != nullptr, ERR_INVALID_HANDLE, "null audioCapturerSource");
125     if (iAudioCapturerSource->IsInited()) {
126         return SUCCESS;
127     }
128 
129     IAudioSourceAttr iAttr = {};
130     iAttr.adapterName = attr->adapterName;
131     iAttr.openMicSpeaker = attr->openMicSpeaker;
132     iAttr.format = attr->format;
133     iAttr.sampleRate = attr->sampleRate;
134     iAttr.channel = attr->channel;
135     iAttr.volume = attr->volume;
136     iAttr.bufferSize = attr->bufferSize;
137     iAttr.isBigEndian = attr->isBigEndian;
138     iAttr.filePath = attr->filePath;
139     iAttr.deviceNetworkId = attr->deviceNetworkId;
140     iAttr.deviceType = attr->deviceType;
141     iAttr.sourceType = attr->sourceType;
142     iAttr.channelLayout = attr->channelLayout;
143     iAttr.hasEcConfig = attr->hasEcConfig;
144     iAttr.formatEc = attr->formatEc;
145     iAttr.sampleRateEc = attr->sampleRateEc;
146     iAttr.channelEc = attr->channelEc;
147     ret = iAudioCapturerSource->Init(iAttr);
148 
149     return ret;
150 }
151 
IAudioCapturerSourceDeInit(void * wapper)152 void IAudioCapturerSourceDeInit(void *wapper)
153 {
154     IAudioCapturerSource *iAudioCapturerSource = static_cast<IAudioCapturerSource *>(wapper);
155     CHECK_AND_RETURN_LOG(iAudioCapturerSource != nullptr, "null audioCapturerSource");
156     if (iAudioCapturerSource->IsInited())
157         iAudioCapturerSource->DeInit();
158 }
159 
IAudioCapturerSourceStop(void * wapper)160 int32_t IAudioCapturerSourceStop(void *wapper)
161 {
162     IAudioCapturerSource *iAudioCapturerSource = static_cast<IAudioCapturerSource *>(wapper);
163     CHECK_AND_RETURN_RET_LOG(iAudioCapturerSource != nullptr, ERR_INVALID_HANDLE, "null audioCapturerSource");
164     if (!iAudioCapturerSource->IsInited())
165         return SUCCESS;
166 
167     int32_t ret = iAudioCapturerSource->Stop();
168 
169     return ret;
170 }
171 
IAudioCapturerSourceStart(void * wapper)172 int32_t IAudioCapturerSourceStart(void *wapper)
173 {
174     IAudioCapturerSource *iAudioCapturerSource = static_cast<IAudioCapturerSource *>(wapper);
175     CHECK_AND_RETURN_RET_LOG(iAudioCapturerSource != nullptr, ERR_INVALID_HANDLE, "null audioCapturerSource");
176     bool isInited = iAudioCapturerSource->IsInited();
177     CHECK_AND_RETURN_RET_LOG(isInited, ERR_DEVICE_INIT,
178         "audioCapturer Not Inited! Init the capturer first");
179 
180     int32_t ret = iAudioCapturerSource->Start();
181 
182     return ret;
183 }
184 
IAudioCapturerSourceFrame(void * wapper,char * frame,uint64_t requestBytes,uint64_t * replyBytes)185 int32_t IAudioCapturerSourceFrame(void *wapper, char *frame, uint64_t requestBytes, uint64_t *replyBytes)
186 {
187     IAudioCapturerSource *iAudioCapturerSource = static_cast<IAudioCapturerSource *>(wapper);
188     CHECK_AND_RETURN_RET_LOG(iAudioCapturerSource != nullptr, ERR_INVALID_HANDLE, "null audioCapturerSource");
189     bool isInited = iAudioCapturerSource->IsInited();
190     CHECK_AND_RETURN_RET_LOG(isInited, ERR_DEVICE_INIT,
191         "audioCapturer Not Inited! Init the capturer first");
192 
193     int32_t ret = iAudioCapturerSource->CaptureFrame(frame, requestBytes, *replyBytes);
194 
195     return ret;
196 }
197 
IAudioCapturerSourceFrameWithEc(void * wapper,FrameDesc * fdesc,uint64_t * replyBytes,FrameDesc * fdescEc,uint64_t * replyBytesEc)198 int32_t IAudioCapturerSourceFrameWithEc(void *wapper,
199     FrameDesc *fdesc, uint64_t *replyBytes,
200     FrameDesc *fdescEc, uint64_t *replyBytesEc)
201 {
202     IAudioCapturerSource *iAudioCapturerSource = static_cast<IAudioCapturerSource *>(wapper);
203     CHECK_AND_RETURN_RET_LOG(iAudioCapturerSource != nullptr, ERR_INVALID_HANDLE, "null audioCapturerSource");
204     bool isInited = iAudioCapturerSource->IsInited();
205     CHECK_AND_RETURN_RET_LOG(isInited, ERR_DEVICE_INIT,
206         "audioCapturer Not Inited! Init the capturer first");
207 
208     return iAudioCapturerSource->CaptureFrameWithEc(fdesc, *replyBytes, fdescEc, *replyBytesEc);
209 }
210 
IAudioCapturerSourceSetVolume(void * wapper,float left,float right)211 int32_t IAudioCapturerSourceSetVolume(void *wapper, float left, float right)
212 {
213     IAudioCapturerSource *iAudioCapturerSource = static_cast<IAudioCapturerSource *>(wapper);
214     CHECK_AND_RETURN_RET_LOG(iAudioCapturerSource != nullptr, ERR_INVALID_HANDLE, "null audioCapturerSource");
215     bool isInited = iAudioCapturerSource->IsInited();
216     CHECK_AND_RETURN_RET_LOG(isInited, ERR_DEVICE_INIT,
217         "audioCapturer Not Inited! Init the capturer first");
218 
219     int32_t ret = iAudioCapturerSource->SetVolume(left, right);
220 
221     return ret;
222 }
223 
IAudioCapturerSourceGetVolume(void * wapper,float * left,float * right)224 int32_t IAudioCapturerSourceGetVolume(void *wapper, float *left, float *right)
225 {
226     IAudioCapturerSource *iAudioCapturerSource = static_cast<IAudioCapturerSource *>(wapper);
227     CHECK_AND_RETURN_RET_LOG(iAudioCapturerSource != nullptr, ERR_INVALID_HANDLE, "null audioCapturerSource");
228     bool isInited = iAudioCapturerSource->IsInited();
229     CHECK_AND_RETURN_RET_LOG(isInited, ERR_DEVICE_INIT,
230         "audioCapturer Not Inited! Init the capturer first");
231     int32_t ret = iAudioCapturerSource->GetVolume(*left, *right);
232 
233     return ret;
234 }
235 
IAudioCapturerSourceIsMuteRequired(void * wapper)236 bool IAudioCapturerSourceIsMuteRequired(void *wapper)
237 {
238     bool muteStat = false;
239     IAudioCapturerSource *iAudioCapturerSource = static_cast<IAudioCapturerSource *>(wapper);
240     CHECK_AND_RETURN_RET_LOG(iAudioCapturerSource != nullptr, muteStat, "null audioCapturerSource");
241     bool isInited = iAudioCapturerSource->IsInited();
242     CHECK_AND_RETURN_RET_LOG(isInited, muteStat,
243         "audioCapturer Not Inited! Init the capturer first");
244     iAudioCapturerSource->GetMute(muteStat);
245     return muteStat;
246 }
247 
IAudioCapturerSourceSetMute(void * wapper,bool isMute)248 int32_t IAudioCapturerSourceSetMute(void *wapper, bool isMute)
249 {
250     IAudioCapturerSource *iAudioCapturerSource = static_cast<IAudioCapturerSource *>(wapper);
251     CHECK_AND_RETURN_RET_LOG(iAudioCapturerSource != nullptr, ERR_INVALID_HANDLE, "null audioCapturerSource");
252     bool isInited = iAudioCapturerSource->IsInited();
253     CHECK_AND_RETURN_RET_LOG(isInited, ERR_DEVICE_INIT,
254         "audioCapturer Not Inited! Init the capturer first");
255 
256     int32_t ret = iAudioCapturerSource->SetMute(isMute);
257 
258     return ret;
259 }
260 
IAudioCapturerSourceUpdateAppsUid(void * wapper,const int32_t appsUid[MAX_MIX_CHANNELS],const size_t size)261 int32_t IAudioCapturerSourceUpdateAppsUid(void *wapper, const int32_t appsUid[MAX_MIX_CHANNELS],
262     const size_t size)
263 {
264     IAudioCapturerSource *iAudioCapturerSource = static_cast<IAudioCapturerSource *>(wapper);
265     CHECK_AND_RETURN_RET_LOG(iAudioCapturerSource != nullptr, ERR_INVALID_HANDLE, "null audioCapturerSource");
266     bool isInited = iAudioCapturerSource->IsInited();
267     CHECK_AND_RETURN_RET_LOG(isInited, ERR_DEVICE_INIT,
268         "audioCapturer Not Inited! Init the capturer first");
269 
270     int32_t ret = iAudioCapturerSource->UpdateAppsUid(appsUid, size);
271 
272     return ret;
273 }
274 
IAudioCapturerSourceGetCaptureId(void * wapper,uint32_t * captureId)275 int32_t IAudioCapturerSourceGetCaptureId(void *wapper, uint32_t *captureId)
276 {
277     IAudioCapturerSource *iAudioCapturerSource = static_cast<IAudioCapturerSource *>(wapper);
278     CHECK_AND_RETURN_RET_LOG(iAudioCapturerSource != nullptr, ERR_INVALID_HANDLE, "null audioCapturerSource");
279     bool isInited = iAudioCapturerSource->IsInited();
280     CHECK_AND_RETURN_RET_LOG(isInited, ERR_DEVICE_INIT, "audioCapturer Not Inited! Init the capturer first");
281 
282     int32_t ret = iAudioCapturerSource->GetCaptureId(*captureId);
283     return ret;
284 }
285 #ifdef __cplusplus
286 }
287 #endif
288