From 95b8032bf875e6eec1613117b253644cb86b063d Mon Sep 17 00:00:00 2001 From: FileStream Date: Fri, 17 May 2024 18:49:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=BD=95=E5=B1=8F?= =?UTF-8?q?=E5=BA=94=E7=94=A8mic=E9=80=9A=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: FileStream --- .../avscreen_capture_napi.cpp | 1 + .../server/audio_capturer_wrapper.cpp | 6 ++ .../server/audio_capturer_wrapper.h | 1 + .../server/screen_capture_server.cpp | 74 ++++++++++++++++++- .../server/screen_capture_server.h | 3 + 5 files changed, 84 insertions(+), 1 deletion(-) diff --git a/frameworks/js/avscreen_capture/avscreen_capture_napi.cpp b/frameworks/js/avscreen_capture/avscreen_capture_napi.cpp index 90b5565e2..b24cdb957 100644 --- a/frameworks/js/avscreen_capture/avscreen_capture_napi.cpp +++ b/frameworks/js/avscreen_capture/avscreen_capture_napi.cpp @@ -362,6 +362,7 @@ napi_value AVScreenCaptureNapi::JsSetMicrophoneEnabled(napi_env env, napi_callba asyncCtx->deferred = CommonNapi::CreatePromise(env, asyncCtx->callbackRef, result); asyncCtx->task_ = AVScreenCaptureNapi::GetSetMicrophoneEnableTask(asyncCtx, enable); + (void)asyncCtx->napi->taskQue_->EnqueueTask(asyncCtx->task_); napi_value resource = nullptr; napi_create_string_utf8(env, opt.c_str(), NAPI_AUTO_LENGTH, &resource); diff --git a/services/services/screen_capture/server/audio_capturer_wrapper.cpp b/services/services/screen_capture/server/audio_capturer_wrapper.cpp index 46091bb8c..4b82dd004 100644 --- a/services/services/screen_capture/server/audio_capturer_wrapper.cpp +++ b/services/services/screen_capture/server/audio_capturer_wrapper.cpp @@ -105,6 +105,12 @@ void AudioCapturerWrapper::SetIsMuted(bool isMuted) isMuted_.store(isMuted); } +bool AudioCapturerWrapper::GetIsMuted() +{ + MEDIA_LOGD("0x%{public}06" PRIXPTR " GetIsMuted isMuted:%{public}d", FAKE_POINTER(this), isMuted_.load()); + return isMuted_.load(); +} + void AudioCapturerWrapper::SetInnerStreamUsage(std::vector &usages) { // If do not call this function, the audio framework use MUSIC/MOVIE/GAME/AUDIOBOOK diff --git a/services/services/screen_capture/server/audio_capturer_wrapper.h b/services/services/screen_capture/server/audio_capturer_wrapper.h index 8dc8cc591..6dfeff52b 100644 --- a/services/services/screen_capture/server/audio_capturer_wrapper.h +++ b/services/services/screen_capture/server/audio_capturer_wrapper.h @@ -48,6 +48,7 @@ public: int32_t Start(const OHOS::AudioStandard::AppInfo &appInfo); int32_t Stop(); void SetIsMuted(bool isMuted); + bool GetIsMuted(); int32_t CaptureAudio(); int32_t AcquireAudioBuffer(std::shared_ptr &audioBuffer); int32_t GetBufferSize(size_t &size); diff --git a/services/services/screen_capture/server/screen_capture_server.cpp b/services/services/screen_capture/server/screen_capture_server.cpp index 003e41da9..8b6706808 100644 --- a/services/services/screen_capture/server/screen_capture_server.cpp +++ b/services/services/screen_capture/server/screen_capture_server.cpp @@ -75,7 +75,7 @@ static const std::string ICON_PATH_STOP = "/etc/screencapture/stop.png"; static const std::string BUNDLE_NAME = "com.ohos.systemui"; static const std::string ABILITY_NAME = "com.ohos.systemui.dialog"; static const std::string BACK_GROUND_COLOR = "#E84026"; -static const std::string SCREEN_RECORDER_BUNDLE_NAME = "com.huawei.ohos.screenrecorder"; +static const std::string SCREEN_RECORDER_BUNDLE_NAME = "com.huawei.hmos.screenrecorder"; static const int32_t SVG_HEIGHT = 80; static const int32_t SVG_WIDTH = 80; static const int32_t MDPI = 160; @@ -817,6 +817,47 @@ int32_t ScreenCaptureServer::StartAudioCapture() return MSERR_OK; } +int32_t ScreenCaptureServer::StartInnerAudioCapture() +{ + MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "StartInnerAudioCapture start, dataType:%{public}d, " + "innerCapInfo.state:%{public}d.", + FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.innerCapInfo.state); + std::shared_ptr innerCapture; + if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) { + MediaTrace trace("ScreenCaptureServer::StartInnerAudioCaptureInner"); + innerCapture = std::make_shared(captureConfig_.audioInfo.innerCapInfo, screenCaptureCb_, + std::string("OS_InnerAudioCapture"), contentFilter_); + int32_t ret = innerCapture->Start(appInfo_); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartInnerAudioCapture failed"); + } + innerAudioCapture_ = innerCapture; + MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "StartInnerAudioCapture OK.", FAKE_POINTER(this)); + return MSERR_OK; +} + +int32_t ScreenCaptureServer::StartMicAudioCapture() +{ + MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "StartMicAudioCapture start, dataType:%{public}d, " + "micCapInfo.state:%{public}d.", + FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.micCapInfo.state); + std::shared_ptr micCapture; + if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) { + MediaTrace trace("ScreenCaptureServer::StartMicAudioCaptureInner"); + ScreenCaptureContentFilter contentFilterMic; + micCapture = std::make_shared(captureConfig_.audioInfo.micCapInfo, screenCaptureCb_, + std::string("OS_MicAudioCapture"), contentFilterMic); + int32_t ret = micCapture->Start(appInfo_); + if (ret != MSERR_OK) { + screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE); + } + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartMicAudioCapture failed"); + micCapture->SetIsMuted(!isMicrophoneOn_); + } + micAudioCapture_ = micCapture; + MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "StartMicAudioCapture OK.", FAKE_POINTER(this)); + return MSERR_OK; +} + int32_t ScreenCaptureServer::StartScreenCaptureStream() { MediaTrace trace("ScreenCaptureServer::StartScreenCaptureStream"); @@ -867,6 +908,15 @@ int32_t ScreenCaptureServer::StartScreenCaptureFile() ret = StartAudioCapture(); CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartAudioCapture failed, ret:%{public}d, dataType:%{public}d", ret, captureConfig_.dataType); + + int32_t retInner = StartInnerAudioCapture(); + CHECK_AND_RETURN_RET_LOG(retInner == MSERR_OK, retInner, "StartInnerAudioCapture failed, ret:%{public}d," + "dataType:%{public}d", retInner, captureConfig_.dataType); + int32_t retMic = StartMicAudioCapture(); + if (retMic != MSERR_OK) { + MEDIA_LOGE("StartMicAudioCapture failed"); + } + ret = recorder_->Start(); MEDIA_LOGI("StartScreenCaptureFile RecorderServer E"); @@ -1659,6 +1709,9 @@ int32_t ScreenCaptureServer::AcquireAudioBufferMix(std::shared_ptr return MSERR_UNKNOWN; } } + if (type == AVScreenCaptureMixMode::MIX_MODE && innerAudioCapture_ != nullptr) { + return innerAudioCapture_->AcquireAudioBuffer(innerAudioBuffer); + } if (type == AVScreenCaptureMixMode::MIC_MODE && micAudioCapture_ != nullptr) { return micAudioCapture_->AcquireAudioBuffer(micAudioBuffer); } @@ -1819,6 +1872,16 @@ int32_t ScreenCaptureServer::SetMicrophoneEnabled(bool isMicrophone) return MSERR_OK; } +bool ScreenCaptureServer::GetMicWorkingState() +{ + MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR "GetMicWorkingState isMicrophoneOn_:%{public}d", + FAKE_POINTER(this), isMicrophoneOn_); + if (micAudioCapture_ != nullptr) { + return !micAudioCapture_->GetIsMuted(); + } + return false; +} + int32_t ScreenCaptureServer::SetCanvasRotation(bool canvasRotation) { MediaTrace trace("ScreenCaptureServer::SetCanvasRotation"); @@ -2134,6 +2197,9 @@ int32_t AudioDataSource::ReadAt(std::shared_ptr buffer, uint32_t lengt std::shared_ptr innerAudioBuffer = nullptr; std::shared_ptr micAudioBuffer = nullptr; int32_t ret = MSERR_OK; + if (screenCaptureServer_ == nullptr) { + return MSERR_UNKNOWN; + } ret = screenCaptureServer_->AcquireAudioBufferMix(innerAudioBuffer, micAudioBuffer, type_); if (ret == MSERR_OK) { MEDIA_LOGD("AcquireAudioBufferMix sucess"); @@ -2143,6 +2209,12 @@ int32_t AudioDataSource::ReadAt(std::shared_ptr buffer, uint32_t lengt return MSERR_INVALID_VAL; } if (type_ == AVScreenCaptureMixMode::MIX_MODE) { + if (!screenCaptureServer_->GetMicWorkingState()) { + MEDIA_LOGD("AVScreenCaptureMixMode MIX_MODE MIC OFF"); + bufferMem->Write((uint8_t*)innerAudioBuffer->buffer, innerAudioBuffer->length, 0); + return screenCaptureServer_->ReleaseAudioBufferMix(type_); + } + MEDIA_LOGD("AVScreenCaptureMixMode MIX_MODE MIC ON"); char* mixData = new char[innerAudioBuffer->length]; char* srcData[2] = {nullptr}; srcData[0] = reinterpret_cast(innerAudioBuffer->buffer); diff --git a/services/services/screen_capture/server/screen_capture_server.h b/services/services/screen_capture/server/screen_capture_server.h index 2fec0e519..ee03b6570 100644 --- a/services/services/screen_capture/server/screen_capture_server.h +++ b/services/services/screen_capture/server/screen_capture_server.h @@ -228,6 +228,7 @@ public: int32_t ReleaseAudioBuffer(AudioCaptureSourceType type) override; int32_t ReleaseVideoBuffer() override; int32_t SetMicrophoneEnabled(bool isMicrophone) override; + bool GetMicWorkingState(); int32_t SetCanvasRotation(bool canvasRotation) override; void Release() override; int32_t ExcludeContent(ScreenCaptureContentFilter &contentFilter) override; @@ -252,6 +253,8 @@ private: int32_t StartScreenCaptureFile(); int32_t StartScreenCaptureStream(); int32_t StartAudioCapture(); + int32_t StartInnerAudioCapture(); + int32_t StartMicAudioCapture(); int32_t StartVideoCapture(); int32_t StartHomeVideoCapture(); int32_t StopScreenCaptureInner(AVScreenCaptureStateCode stateCode); -- Gitee From 1f8f2940f8772226bc9afc49edd318b665480c1d Mon Sep 17 00:00:00 2001 From: FileStream Date: Fri, 17 May 2024 18:49:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=BD=95=E5=B1=8F?= =?UTF-8?q?=E5=BA=94=E7=94=A8mic=E9=80=9A=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: FileStream --- .../avscreen_capture_napi.cpp | 1 + .../server/audio_capturer_wrapper.cpp | 6 ++ .../server/audio_capturer_wrapper.h | 1 + .../server/screen_capture_server.cpp | 73 ++++++++++++++++++- .../server/screen_capture_server.h | 3 + 5 files changed, 83 insertions(+), 1 deletion(-) diff --git a/frameworks/js/avscreen_capture/avscreen_capture_napi.cpp b/frameworks/js/avscreen_capture/avscreen_capture_napi.cpp index 90b5565e2..b24cdb957 100644 --- a/frameworks/js/avscreen_capture/avscreen_capture_napi.cpp +++ b/frameworks/js/avscreen_capture/avscreen_capture_napi.cpp @@ -362,6 +362,7 @@ napi_value AVScreenCaptureNapi::JsSetMicrophoneEnabled(napi_env env, napi_callba asyncCtx->deferred = CommonNapi::CreatePromise(env, asyncCtx->callbackRef, result); asyncCtx->task_ = AVScreenCaptureNapi::GetSetMicrophoneEnableTask(asyncCtx, enable); + (void)asyncCtx->napi->taskQue_->EnqueueTask(asyncCtx->task_); napi_value resource = nullptr; napi_create_string_utf8(env, opt.c_str(), NAPI_AUTO_LENGTH, &resource); diff --git a/services/services/screen_capture/server/audio_capturer_wrapper.cpp b/services/services/screen_capture/server/audio_capturer_wrapper.cpp index 46091bb8c..4b82dd004 100644 --- a/services/services/screen_capture/server/audio_capturer_wrapper.cpp +++ b/services/services/screen_capture/server/audio_capturer_wrapper.cpp @@ -105,6 +105,12 @@ void AudioCapturerWrapper::SetIsMuted(bool isMuted) isMuted_.store(isMuted); } +bool AudioCapturerWrapper::GetIsMuted() +{ + MEDIA_LOGD("0x%{public}06" PRIXPTR " GetIsMuted isMuted:%{public}d", FAKE_POINTER(this), isMuted_.load()); + return isMuted_.load(); +} + void AudioCapturerWrapper::SetInnerStreamUsage(std::vector &usages) { // If do not call this function, the audio framework use MUSIC/MOVIE/GAME/AUDIOBOOK diff --git a/services/services/screen_capture/server/audio_capturer_wrapper.h b/services/services/screen_capture/server/audio_capturer_wrapper.h index 8dc8cc591..6dfeff52b 100644 --- a/services/services/screen_capture/server/audio_capturer_wrapper.h +++ b/services/services/screen_capture/server/audio_capturer_wrapper.h @@ -48,6 +48,7 @@ public: int32_t Start(const OHOS::AudioStandard::AppInfo &appInfo); int32_t Stop(); void SetIsMuted(bool isMuted); + bool GetIsMuted(); int32_t CaptureAudio(); int32_t AcquireAudioBuffer(std::shared_ptr &audioBuffer); int32_t GetBufferSize(size_t &size); diff --git a/services/services/screen_capture/server/screen_capture_server.cpp b/services/services/screen_capture/server/screen_capture_server.cpp index 003e41da9..15d9dccc6 100644 --- a/services/services/screen_capture/server/screen_capture_server.cpp +++ b/services/services/screen_capture/server/screen_capture_server.cpp @@ -75,7 +75,6 @@ static const std::string ICON_PATH_STOP = "/etc/screencapture/stop.png"; static const std::string BUNDLE_NAME = "com.ohos.systemui"; static const std::string ABILITY_NAME = "com.ohos.systemui.dialog"; static const std::string BACK_GROUND_COLOR = "#E84026"; -static const std::string SCREEN_RECORDER_BUNDLE_NAME = "com.huawei.ohos.screenrecorder"; static const int32_t SVG_HEIGHT = 80; static const int32_t SVG_WIDTH = 80; static const int32_t MDPI = 160; @@ -817,6 +816,47 @@ int32_t ScreenCaptureServer::StartAudioCapture() return MSERR_OK; } +int32_t ScreenCaptureServer::StartInnerAudioCapture() +{ + MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "StartInnerAudioCapture start, dataType:%{public}d, " + "innerCapInfo.state:%{public}d.", + FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.innerCapInfo.state); + std::shared_ptr innerCapture; + if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) { + MediaTrace trace("ScreenCaptureServer::StartInnerAudioCaptureInner"); + innerCapture = std::make_shared(captureConfig_.audioInfo.innerCapInfo, screenCaptureCb_, + std::string("OS_InnerAudioCapture"), contentFilter_); + int32_t ret = innerCapture->Start(appInfo_); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartInnerAudioCapture failed"); + } + innerAudioCapture_ = innerCapture; + MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "StartInnerAudioCapture OK.", FAKE_POINTER(this)); + return MSERR_OK; +} + +int32_t ScreenCaptureServer::StartMicAudioCapture() +{ + MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "StartMicAudioCapture start, dataType:%{public}d, " + "micCapInfo.state:%{public}d.", + FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.micCapInfo.state); + std::shared_ptr micCapture; + if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) { + MediaTrace trace("ScreenCaptureServer::StartMicAudioCaptureInner"); + ScreenCaptureContentFilter contentFilterMic; + micCapture = std::make_shared(captureConfig_.audioInfo.micCapInfo, screenCaptureCb_, + std::string("OS_MicAudioCapture"), contentFilterMic); + int32_t ret = micCapture->Start(appInfo_); + if (ret != MSERR_OK) { + screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE); + } + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartMicAudioCapture failed"); + micCapture->SetIsMuted(!isMicrophoneOn_); + } + micAudioCapture_ = micCapture; + MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "StartMicAudioCapture OK.", FAKE_POINTER(this)); + return MSERR_OK; +} + int32_t ScreenCaptureServer::StartScreenCaptureStream() { MediaTrace trace("ScreenCaptureServer::StartScreenCaptureStream"); @@ -867,6 +907,15 @@ int32_t ScreenCaptureServer::StartScreenCaptureFile() ret = StartAudioCapture(); CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartAudioCapture failed, ret:%{public}d, dataType:%{public}d", ret, captureConfig_.dataType); + + int32_t retInner = StartInnerAudioCapture(); + CHECK_AND_RETURN_RET_LOG(retInner == MSERR_OK, retInner, "StartInnerAudioCapture failed, ret:%{public}d," + "dataType:%{public}d", retInner, captureConfig_.dataType); + int32_t retMic = StartMicAudioCapture(); + if (retMic != MSERR_OK) { + MEDIA_LOGE("StartMicAudioCapture failed"); + } + ret = recorder_->Start(); MEDIA_LOGI("StartScreenCaptureFile RecorderServer E"); @@ -1659,6 +1708,9 @@ int32_t ScreenCaptureServer::AcquireAudioBufferMix(std::shared_ptr return MSERR_UNKNOWN; } } + if (type == AVScreenCaptureMixMode::MIX_MODE && innerAudioCapture_ != nullptr) { + return innerAudioCapture_->AcquireAudioBuffer(innerAudioBuffer); + } if (type == AVScreenCaptureMixMode::MIC_MODE && micAudioCapture_ != nullptr) { return micAudioCapture_->AcquireAudioBuffer(micAudioBuffer); } @@ -1819,6 +1871,16 @@ int32_t ScreenCaptureServer::SetMicrophoneEnabled(bool isMicrophone) return MSERR_OK; } +bool ScreenCaptureServer::GetMicWorkingState() +{ + MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR "GetMicWorkingState isMicrophoneOn_:%{public}d", + FAKE_POINTER(this), isMicrophoneOn_); + if (micAudioCapture_ != nullptr) { + return !micAudioCapture_->GetIsMuted(); + } + return false; +} + int32_t ScreenCaptureServer::SetCanvasRotation(bool canvasRotation) { MediaTrace trace("ScreenCaptureServer::SetCanvasRotation"); @@ -2134,6 +2196,9 @@ int32_t AudioDataSource::ReadAt(std::shared_ptr buffer, uint32_t lengt std::shared_ptr innerAudioBuffer = nullptr; std::shared_ptr micAudioBuffer = nullptr; int32_t ret = MSERR_OK; + if (screenCaptureServer_ == nullptr) { + return MSERR_UNKNOWN; + } ret = screenCaptureServer_->AcquireAudioBufferMix(innerAudioBuffer, micAudioBuffer, type_); if (ret == MSERR_OK) { MEDIA_LOGD("AcquireAudioBufferMix sucess"); @@ -2143,6 +2208,12 @@ int32_t AudioDataSource::ReadAt(std::shared_ptr buffer, uint32_t lengt return MSERR_INVALID_VAL; } if (type_ == AVScreenCaptureMixMode::MIX_MODE) { + if (!screenCaptureServer_->GetMicWorkingState()) { + MEDIA_LOGD("AVScreenCaptureMixMode MIX_MODE MIC OFF"); + bufferMem->Write((uint8_t*)innerAudioBuffer->buffer, innerAudioBuffer->length, 0); + return screenCaptureServer_->ReleaseAudioBufferMix(type_); + } + MEDIA_LOGD("AVScreenCaptureMixMode MIX_MODE MIC ON"); char* mixData = new char[innerAudioBuffer->length]; char* srcData[2] = {nullptr}; srcData[0] = reinterpret_cast(innerAudioBuffer->buffer); diff --git a/services/services/screen_capture/server/screen_capture_server.h b/services/services/screen_capture/server/screen_capture_server.h index 2fec0e519..ee03b6570 100644 --- a/services/services/screen_capture/server/screen_capture_server.h +++ b/services/services/screen_capture/server/screen_capture_server.h @@ -228,6 +228,7 @@ public: int32_t ReleaseAudioBuffer(AudioCaptureSourceType type) override; int32_t ReleaseVideoBuffer() override; int32_t SetMicrophoneEnabled(bool isMicrophone) override; + bool GetMicWorkingState(); int32_t SetCanvasRotation(bool canvasRotation) override; void Release() override; int32_t ExcludeContent(ScreenCaptureContentFilter &contentFilter) override; @@ -252,6 +253,8 @@ private: int32_t StartScreenCaptureFile(); int32_t StartScreenCaptureStream(); int32_t StartAudioCapture(); + int32_t StartInnerAudioCapture(); + int32_t StartMicAudioCapture(); int32_t StartVideoCapture(); int32_t StartHomeVideoCapture(); int32_t StopScreenCaptureInner(AVScreenCaptureStateCode stateCode); -- Gitee