1 /*
2 * Copyright (C) 2024 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 #include "audio_capturer_wrapper.h"
17
18 #include "media_log.h"
19 #include "media_errors.h"
20 #include "media_utils.h"
21 #include "ipc_skeleton.h"
22 #include "locale_config.h"
23
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "ScreenCaptureACW"};
26 }
27
28 namespace OHOS {
29 namespace Media {
30
OnInterrupt(const InterruptEvent & interruptEvent)31 void AudioCapturerCallbackImpl::OnInterrupt(const InterruptEvent &interruptEvent)
32 {
33 MEDIA_LOGI("AudioCapturerCallbackImpl OnInterrupt hintType:%{public}d, eventType:%{public}d, forceType:%{public}d",
34 interruptEvent.hintType, interruptEvent.eventType, interruptEvent.forceType);
35 }
36
OnStateChange(const CapturerState state)37 void AudioCapturerCallbackImpl::OnStateChange(const CapturerState state)
38 {
39 MEDIA_LOGI("AudioCapturerCallbackImpl OnStateChange state:%{public}d", state);
40 switch (state) {
41 case CAPTURER_PREPARED:
42 MEDIA_LOGD("AudioCapturerCallbackImpl OnStateChange CAPTURER_PREPARED");
43 break;
44 default:
45 MEDIA_LOGD("AudioCapturerCallbackImpl OnStateChange NOT A VALID state");
46 break;
47 }
48 }
49
Start(const OHOS::AudioStandard::AppInfo & appInfo)50 int32_t AudioCapturerWrapper::Start(const OHOS::AudioStandard::AppInfo &appInfo)
51 {
52 std::lock_guard<std::mutex> lock(mutex_);
53 if (isRunning_.load()) {
54 MEDIA_LOGE("Start failed, is running, threadName:%{public}s", threadName_.c_str());
55 return MSERR_UNKNOWN;
56 }
57 #ifdef SUPPORT_CALL
58 if (isInTelCall_.load()) {
59 MEDIA_LOGE("Start failed, is in telephony call, threadName:%{public}s", threadName_.c_str());
60 return MSERR_UNKNOWN;
61 }
62 #endif
63 appInfo_ = appInfo;
64 std::shared_ptr<AudioCapturer> audioCapturer = CreateAudioCapturer(appInfo);
65 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, MSERR_UNKNOWN, "Start failed, create AudioCapturer failed");
66 if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
67 .compare(bundleName_) == 0) {
68 std::vector<SourceType> targetSources = {
69 SourceType::SOURCE_TYPE_MIC,
70 SourceType::SOURCE_TYPE_VOICE_RECOGNITION,
71 SourceType::SOURCE_TYPE_VOICE_MESSAGE,
72 SourceType::SOURCE_TYPE_CAMCORDER
73 };
74 std::string region = Global::I18n::LocaleConfig::GetSystemRegion();
75 if (region == "CN") {
76 targetSources.push_back(SourceType::SOURCE_TYPE_VOICE_COMMUNICATION);
77 }
78 int32_t ret = audioCapturer->SetAudioSourceConcurrency(targetSources);
79 if (ret != MSERR_OK) {
80 MEDIA_LOGE("SetAudioSourceConcurrency failed, ret:%{public}d, threadName:%{public}s", ret,
81 threadName_.c_str());
82 }
83 }
84 if (!audioCapturer->Start()) {
85 MEDIA_LOGE("Start failed, AudioCapturer Start failed, threadName:%{public}s", threadName_.c_str());
86 audioCapturer->Release();
87 audioCapturer = nullptr;
88 OnStartFailed(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL, SCREEN_CAPTURE_ERR_UNKNOWN);
89 return MSERR_UNKNOWN;
90 }
91 MEDIA_LOGI("0x%{public}06" PRIXPTR "Start success, threadName:%{public}s", FAKE_POINTER(this), threadName_.c_str());
92
93 isRunning_.store(true);
94 readAudioLoop_ = std::make_unique<std::thread>([this] { this->CaptureAudio(); });
95 audioCapturer_ = audioCapturer;
96 captureState_.store(CAPTURER_RECORDING);
97 return MSERR_OK;
98 }
99
Pause()100 int32_t AudioCapturerWrapper::Pause()
101 {
102 std::lock_guard<std::mutex> lock(mutex_);
103 MEDIA_LOGI("0x%{public}06" PRIXPTR " Pause S, threadName:%{public}s", FAKE_POINTER(this), threadName_.c_str());
104 if (isRunning_.load()) {
105 isRunning_.store(false);
106 if (readAudioLoop_ != nullptr && readAudioLoop_->joinable()) {
107 readAudioLoop_->join();
108 readAudioLoop_.reset();
109 readAudioLoop_ = nullptr;
110 }
111 if (audioCapturer_ != nullptr) {
112 if (!audioCapturer_->Pause()) {
113 MEDIA_LOGE("AudioCapturer Pause failed, threadName:%{public}s", threadName_.c_str());
114 }
115 }
116 }
117 captureState_.store(CAPTURER_PAUSED);
118 MEDIA_LOGI("0x%{public}06" PRIXPTR " Pause E, threadName:%{public}s", FAKE_POINTER(this), threadName_.c_str());
119 return MSERR_OK;
120 }
121
Resume()122 int32_t AudioCapturerWrapper::Resume()
123 {
124 std::lock_guard<std::mutex> lock(mutex_);
125 if (isRunning_.load()) {
126 MEDIA_LOGE("Resume failed, is running, threadName:%{public}s", threadName_.c_str());
127 return MSERR_UNKNOWN;
128 }
129 #ifdef SUPPORT_CALL
130 if (isInTelCall_.load()) {
131 MEDIA_LOGE("Resume failed, is in telephony call, threadName:%{public}s", threadName_.c_str());
132 return MSERR_UNKNOWN;
133 }
134 #endif
135 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, MSERR_UNKNOWN, "Resume failed, audioCapturer_ is nullptr");
136
137 if (!audioCapturer_->Start()) {
138 MEDIA_LOGE("AudioCapturer Start failed, threadName:%{public}s", threadName_.c_str());
139 OnStartFailed(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL, SCREEN_CAPTURE_ERR_UNKNOWN);
140 return MSERR_UNKNOWN;
141 }
142 MEDIA_LOGI("0x%{public}06" PRIXPTR "Resume success, threadName:%{public}s", FAKE_POINTER(this),
143 threadName_.c_str());
144
145 isRunning_.store(true);
146 readAudioLoop_ = std::make_unique<std::thread>([this] { this->CaptureAudio(); });
147 captureState_.store(CAPTURER_RECORDING);
148 return MSERR_OK;
149 }
150
Stop()151 int32_t AudioCapturerWrapper::Stop()
152 {
153 std::lock_guard<std::mutex> lock(mutex_);
154 MEDIA_LOGI("0x%{public}06" PRIXPTR " Stop S, threadName:%{public}s", FAKE_POINTER(this), threadName_.c_str());
155 isRunning_.store(false);
156 if (readAudioLoop_ != nullptr && readAudioLoop_->joinable()) {
157 readAudioLoop_->join();
158 readAudioLoop_.reset();
159 readAudioLoop_ = nullptr;
160 }
161 if (audioCapturer_ != nullptr) {
162 audioCapturer_->Stop();
163 audioCapturer_->Release();
164 audioCapturer_ = nullptr;
165 }
166 std::unique_lock<std::mutex> bufferLock(bufferMutex_);
167 MEDIA_LOGD("0x%{public}06" PRIXPTR " Stop pop, threadName:%{public}s", FAKE_POINTER(this), threadName_.c_str());
168 while (!availBuffers_.empty()) {
169 if (availBuffers_.front() != nullptr) {
170 free(availBuffers_.front()->buffer);
171 availBuffers_.front()->buffer = nullptr;
172 }
173 availBuffers_.pop();
174 }
175 MEDIA_LOGI("0x%{public}06" PRIXPTR " Stop E, threadName:%{public}s", FAKE_POINTER(this), threadName_.c_str());
176 captureState_.store(CAPTURER_STOPED);
177 return MSERR_OK;
178 }
179
UpdateAudioCapturerConfig(ScreenCaptureContentFilter & filter)180 int32_t AudioCapturerWrapper::UpdateAudioCapturerConfig(ScreenCaptureContentFilter &filter)
181 {
182 MEDIA_LOGI("AudioCapturerWrapper::UpdateAudioCapturerConfig start");
183 contentFilter_ = filter;
184 AudioPlaybackCaptureConfig config;
185 SetInnerStreamUsage(config.filterOptions.usages);
186 if (contentFilter_.filteredAudioContents.find(
187 AVScreenCaptureFilterableAudioContent::SCREEN_CAPTURE_CURRENT_APP_AUDIO) !=
188 contentFilter_.filteredAudioContents.end()) {
189 config.filterOptions.pids.push_back(appInfo_.appPid);
190 config.filterOptions.pidFilterMode = OHOS::AudioStandard::FilterMode::EXCLUDE;
191 MEDIA_LOGI("UpdateAudioCapturerConfig exclude current app audio");
192 }
193 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, MSERR_INVALID_VAL,
194 "AudioCapturerWrapper::UpdateAudioCapturerConfig audioCapturer_ is nullptr");
195 int32_t ret = audioCapturer_->UpdatePlaybackCaptureConfig(config);
196 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_VAL,
197 "AudioCapturerWrapper::UpdateAudioCapturerConfig failed");
198 MEDIA_LOGI("AudioCapturerWrapper::UpdateAudioCapturerConfig success");
199 return MSERR_OK;
200 }
201
GetAudioCapturerState()202 AudioCapturerWrapperState AudioCapturerWrapper::GetAudioCapturerState()
203 {
204 return captureState_.load();
205 }
206
SetInnerStreamUsage(std::vector<OHOS::AudioStandard::StreamUsage> & usages)207 void AudioCapturerWrapper::SetInnerStreamUsage(std::vector<OHOS::AudioStandard::StreamUsage> &usages)
208 {
209 // If do not call this function, the audio framework use MUSIC/MOVIE/GAME/AUDIOBOOK
210 usages.push_back(AudioStandard::StreamUsage::STREAM_USAGE_MUSIC);
211 usages.push_back(AudioStandard::StreamUsage::STREAM_USAGE_ALARM);
212 usages.push_back(AudioStandard::StreamUsage::STREAM_USAGE_MOVIE);
213 usages.push_back(AudioStandard::StreamUsage::STREAM_USAGE_GAME);
214 usages.push_back(AudioStandard::StreamUsage::STREAM_USAGE_AUDIOBOOK);
215 usages.push_back(AudioStandard::StreamUsage::STREAM_USAGE_NAVIGATION);
216 usages.push_back(AudioStandard::StreamUsage::STREAM_USAGE_UNKNOWN);
217 usages.push_back(AudioStandard::StreamUsage::STREAM_USAGE_VOICE_ASSISTANT);
218 usages.push_back(AudioStandard::StreamUsage::STREAM_USAGE_VOICE_MESSAGE);
219 if (contentFilter_.filteredAudioContents.find(
220 AVScreenCaptureFilterableAudioContent::SCREEN_CAPTURE_NOTIFICATION_AUDIO) ==
221 contentFilter_.filteredAudioContents.end()) {
222 usages.push_back(OHOS::AudioStandard::StreamUsage::STREAM_USAGE_NOTIFICATION);
223 }
224 std::string region = Global::I18n::LocaleConfig::GetSystemRegion();
225 if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
226 .compare(bundleName_) == 0 && region == "CN") {
227 usages.push_back(AudioStandard::StreamUsage::STREAM_USAGE_VOICE_COMMUNICATION);
228 usages.push_back(AudioStandard::StreamUsage::STREAM_USAGE_VIDEO_COMMUNICATION);
229 }
230 }
231
CreateAudioCapturer(const OHOS::AudioStandard::AppInfo & appInfo)232 std::shared_ptr<AudioCapturer> AudioCapturerWrapper::CreateAudioCapturer(const OHOS::AudioStandard::AppInfo &appInfo)
233 {
234 bundleName_ = GetClientBundleName(appInfo.appUid);
235 OHOS::AudioStandard::AppInfo newInfo = appInfo;
236 AudioCapturerOptions capturerOptions;
237 capturerOptions.streamInfo.samplingRate = static_cast<AudioSamplingRate>(audioInfo_.audioSampleRate);
238 capturerOptions.streamInfo.channels = static_cast<AudioChannel>(audioInfo_.audioChannels);
239 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
240 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
241 if (audioInfo_.audioSource == AudioCaptureSourceType::SOURCE_DEFAULT ||
242 audioInfo_.audioSource == AudioCaptureSourceType::MIC) {
243 if (isInVoIPCall_.load()) {
244 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_VOICE_COMMUNICATION;
245 } else {
246 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC; // Audio Source Type Mic is 0
247 }
248 } else if (audioInfo_.audioSource == AudioCaptureSourceType::ALL_PLAYBACK ||
249 audioInfo_.audioSource == AudioCaptureSourceType::APP_PLAYBACK) {
250 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_PLAYBACK_CAPTURE;
251 SetInnerStreamUsage(capturerOptions.playbackCaptureConfig.filterOptions.usages);
252 std::string region = Global::I18n::LocaleConfig::GetSystemRegion();
253 if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
254 .compare(bundleName_) == 0 && region == "CN") {
255 newInfo.appTokenId = IPCSkeleton::GetSelfTokenID();
256 newInfo.appFullTokenId = IPCSkeleton::GetSelfTokenID();
257 }
258 }
259 if (contentFilter_.filteredAudioContents.find(
260 AVScreenCaptureFilterableAudioContent::SCREEN_CAPTURE_CURRENT_APP_AUDIO) !=
261 contentFilter_.filteredAudioContents.end()) {
262 capturerOptions.playbackCaptureConfig.filterOptions.pids.push_back(appInfo.appPid);
263 capturerOptions.playbackCaptureConfig.filterOptions.pidFilterMode =
264 OHOS::AudioStandard::FilterMode::EXCLUDE;
265 MEDIA_LOGI("createAudioCapturer exclude current app audio");
266 }
267 capturerOptions.capturerInfo.capturerFlags = 0;
268 capturerOptions.strategy = { AudioConcurrencyMode::MIX_WITH_OTHERS };
269 std::shared_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions, newInfo);
270 CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, nullptr, "AudioCapturer::Create failed");
271 std::shared_ptr<AudioCapturerCallbackImpl> callback = std::make_shared<AudioCapturerCallbackImpl>();
272 int ret = audioCapturer->SetCapturerCallback(callback);
273 if (ret != MSERR_OK) {
274 audioCapturer->Release();
275 MEDIA_LOGE("SetCapturerCallback failed, threadName:%{public}s", threadName_.c_str());
276 return nullptr;
277 }
278 audioCaptureCallback_ = callback;
279 return audioCapturer;
280 }
281
PartiallyPrintLog(int32_t lineNumber,std::string str)282 void AudioCapturerWrapper::PartiallyPrintLog(int32_t lineNumber, std::string str)
283 {
284 if (captureAudioLogCountMap_.count(lineNumber) == 0) {
285 captureAudioLogCountMap_[lineNumber] = 0;
286 }
287 if (captureAudioLogCountMap_[lineNumber] % AC_LOG_SKIP_NUM == 0) {
288 MEDIA_LOGE("%{public}s", str.c_str());
289 captureAudioLogCountMap_[lineNumber] = 0;
290 }
291 captureAudioLogCountMap_[lineNumber]++;
292 }
293
RelativeSleep(int64_t nanoTime)294 int32_t AudioCapturerWrapper::RelativeSleep(int64_t nanoTime)
295 {
296 int32_t ret = -1; // -1 for bad result.
297 CHECK_AND_RETURN_RET_LOG(nanoTime > 0, ret,
298 "ACW AbsoluteSleep invalid sleep time :%{public}" PRId64 " ns", nanoTime);
299 struct timespec time;
300 time.tv_sec = nanoTime / AUDIO_NS_PER_SECOND;
301 time.tv_nsec = nanoTime - (time.tv_sec * AUDIO_NS_PER_SECOND); // Avoids % operation.
302 clockid_t clockId = CLOCK_MONOTONIC;
303 const int relativeFlag = 0; // flag of relative sleep.
304 ret = clock_nanosleep(clockId, relativeFlag, &time, nullptr);
305 if (ret != 0) {
306 MEDIA_LOGI("ACW RelativeSleep may failed, ret is :%{public}d", ret);
307 }
308 return ret;
309 }
310
CaptureAudio()311 int32_t AudioCapturerWrapper::CaptureAudio()
312 {
313 MEDIA_LOGI("0x%{public}06" PRIXPTR " CaptureAudio S, name:%{public}s", FAKE_POINTER(this), threadName_.c_str());
314 std::string name = threadName_.substr(0, std::min(threadName_.size(), static_cast<size_t>(MAX_THREAD_NAME_LENGTH)));
315 pthread_setname_np(pthread_self(), name.c_str());
316 size_t bufferLen = 0;
317 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr && audioCapturer_->GetBufferSize(bufferLen) == MSERR_OK &&
318 bufferLen > 0, MSERR_NO_MEMORY, "CaptureAudio GetBufferSize failed");
319 Timestamp timestamp;
320 std::shared_ptr<AudioBuffer> audioBuffer;
321 while (true) {
322 CHECK_AND_RETURN_RET_LOG(isRunning_.load(), MSERR_OK, "CaptureAudio is not running, stop capture %{public}s",
323 name.c_str());
324 uint8_t *buffer = static_cast<uint8_t *>(malloc(bufferLen));
325 CHECK_AND_RETURN_RET_LOG(buffer != nullptr, MSERR_OK, "CaptureAudio buffer is no memory, stop capture"
326 " %{public}s", name.c_str());
327 audioBuffer = std::make_shared<AudioBuffer>(buffer, 0, 0, audioInfo_.audioSource);
328 CHECK_AND_RETURN_RET_LOG(audioBuffer != nullptr, MSERR_OK, "CaptureAudio make_shared AudioBuffer failed"
329 " %{public}s", name.c_str());
330 memset_s(audioBuffer->buffer, bufferLen, 0, bufferLen);
331 int32_t bufferRead = audioCapturer_->Read(*(audioBuffer->buffer), bufferLen, true);
332 if (bufferRead <= 0) {
333 RelativeSleep(OHOS::Media::AUDIO_CAPTURE_READ_FAILED_WAIT_TIME);
334 PartiallyPrintLog(__LINE__, "CaptureAudio read audio buffer failed " + name);
335 continue;
336 }
337 audioBuffer->length = bufferRead;
338 audioCapturer_->GetAudioTime(timestamp, Timestamp::Timestampbase::MONOTONIC);
339 int64_t audioTime = timestamp.time.tv_nsec + timestamp.time.tv_sec * SEC_TO_NANOSECOND;
340 audioBuffer->timestamp = audioTime;
341 {
342 std::unique_lock<std::mutex> lock(bufferMutex_);
343 CHECK_AND_RETURN_RET_LOG(isRunning_.load(), MSERR_OK, "CaptureAudio is not running, ignore and stop"
344 " %{public}s", name.c_str());
345 if (availBuffers_.size() > MAX_AUDIO_BUFFER_SIZE) {
346 PartiallyPrintLog(__LINE__, "consume slow, drop audio frame" + name);
347 continue;
348 }
349 availBuffers_.push(audioBuffer);
350 }
351 bufferCond_.notify_all();
352 CHECK_AND_RETURN_RET_LOG(isRunning_.load(), MSERR_OK, "CaptureAudio is not running, ignore and stop"
353 " %{public}s", name.c_str());
354 CHECK_AND_RETURN_RET_LOG(screenCaptureCb_ != nullptr, MSERR_OK,
355 "no consumer, will drop audio frame %{public}s", name.c_str());
356 screenCaptureCb_->OnAudioBufferAvailable(true, audioInfo_.audioSource);
357 }
358 MEDIA_LOGI("0x%{public}06" PRIXPTR " CaptureAudio E, name:%{public}s", FAKE_POINTER(this), threadName_.c_str());
359 return MSERR_OK;
360 }
361
AcquireAudioBuffer(std::shared_ptr<AudioBuffer> & audioBuffer)362 int32_t AudioCapturerWrapper::AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer)
363 {
364 using namespace std::chrono_literals;
365 std::unique_lock<std::mutex> lock(bufferMutex_);
366 CHECK_AND_RETURN_RET_LOG(isRunning_.load(), MSERR_UNKNOWN, "AcquireAudioBuffer failed, not running");
367 MEDIA_LOGD("0x%{public}06" PRIXPTR " Acquire Buffer S, name:%{public}s", FAKE_POINTER(this), threadName_.c_str());
368
369 if (!bufferCond_.wait_for(lock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS),
370 [this] { return !availBuffers_.empty() || captureState_.load() == CAPTURER_RELEASED; })) {
371 MEDIA_LOGE("AcquireAudioBuffer timeout, threadName:%{public}s", threadName_.c_str());
372 return MSERR_UNKNOWN;
373 }
374 if (availBuffers_.empty()) {
375 MEDIA_LOGE("CAPTURER_RELEASED, threadName:%{public}s", threadName_.c_str());
376 return MSERR_UNKNOWN;
377 }
378 CHECK_AND_RETURN_RET_LOG(availBuffers_.front() != nullptr, MSERR_UNKNOWN, "AcquireAudioBuffer availBuffers_.front()"
379 " is nullptr %{public}s", threadName_.c_str());
380 audioBuffer = availBuffers_.front();
381 MEDIA_LOGD("0x%{public}06" PRIXPTR " Acquire Buffer E, name:%{public}s", FAKE_POINTER(this), threadName_.c_str());
382 return MSERR_OK;
383 }
384
GetBufferSize(size_t & size)385 int32_t AudioCapturerWrapper::GetBufferSize(size_t &size)
386 {
387 using namespace std::chrono_literals;
388 std::unique_lock<std::mutex> lock(bufferMutex_);
389 MEDIA_LOGD("0x%{public}06" PRIXPTR " GetBufferSize Buffer S, name:%{public}s",
390 FAKE_POINTER(this), threadName_.c_str());
391 if (!isRunning_.load()) {
392 MEDIA_LOGD("GetBufferSize failed, not running, name:%{public}s", threadName_.c_str());
393 return MSERR_UNKNOWN;
394 }
395 CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr && audioCapturer_->GetBufferSize(size) >= 0,
396 MSERR_NO_MEMORY, "CaptureAudio GetBufferSize failed");
397 MEDIA_LOGD("0x%{public}06" PRIXPTR " GetBufferSize Buffer E, name:%{public}s",
398 FAKE_POINTER(this), threadName_.c_str());
399 return MSERR_OK;
400 }
401
ReleaseAudioBuffer()402 int32_t AudioCapturerWrapper::ReleaseAudioBuffer()
403 {
404 using namespace std::chrono_literals;
405 std::unique_lock<std::mutex> lock(bufferMutex_);
406 MEDIA_LOGD("0x%{public}06" PRIXPTR " Release Buffer S, name:%{public}s", FAKE_POINTER(this), threadName_.c_str());
407 CHECK_AND_RETURN_RET_LOG(isRunning_.load(), MSERR_UNKNOWN, "ReleaseAudioBuffer failed, not running");
408 CHECK_AND_RETURN_RET_LOG(!availBuffers_.empty(), MSERR_UNKNOWN, "ReleaseAudioBuffer failed, no frame to release");
409 availBuffers_.pop();
410 MEDIA_LOGD("0x%{public}06" PRIXPTR " Release Buffer E, name:%{public}s", FAKE_POINTER(this), threadName_.c_str());
411 return MSERR_OK;
412 }
413
SetIsInVoIPCall(bool isInVoIPCall)414 void AudioCapturerWrapper::SetIsInVoIPCall(bool isInVoIPCall)
415 {
416 isInVoIPCall_.store(isInVoIPCall);
417 }
418
419 #ifdef SUPPORT_CALL
SetIsInTelCall(bool isInTelCall)420 void AudioCapturerWrapper::SetIsInTelCall(bool isInTelCall)
421 {
422 isInTelCall_.store(isInTelCall);
423 }
424 #endif
425
OnStartFailed(ScreenCaptureErrorType errorType,int32_t errorCode)426 void AudioCapturerWrapper::OnStartFailed(ScreenCaptureErrorType errorType, int32_t errorCode)
427 {
428 if (screenCaptureCb_ != nullptr) {
429 screenCaptureCb_->OnError(errorType, errorCode);
430 }
431 }
432
~AudioCapturerWrapper()433 AudioCapturerWrapper::~AudioCapturerWrapper()
434 {
435 Stop();
436 captureState_.store(CAPTURER_RELEASED);
437 bufferCond_.notify_all();
438 }
439
OnStartFailed(ScreenCaptureErrorType errorType,int32_t errorCode)440 void MicAudioCapturerWrapper::OnStartFailed(ScreenCaptureErrorType errorType, int32_t errorCode)
441 {
442 (void)errorType;
443 (void)errorCode;
444 if (screenCaptureCb_ != nullptr) {
445 screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
446 }
447 }
448 } // namespace Media
449 } // namespace OHOS