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