From 49650c5e0f528627e81023df2ab8d253ab637651 Mon Sep 17 00:00:00 2001 From: zhangdi Date: Thu, 25 Apr 2024 21:06:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=85=E5=A4=96=E5=90=8C=E5=BD=95=E9=9C=80?= =?UTF-8?q?=E6=B1=82=E4=BB=A3=E7=A0=81codecheck=E5=91=8A=E8=AD=A6=E6=95=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangdi --- .../server/audio_capturer_wrapper.cpp | 6 +- .../server/screen_capture_server.cpp | 81 ++++++++++--------- .../server/screen_capture_server.h | 1 + 3 files changed, 50 insertions(+), 38 deletions(-) diff --git a/services/services/screen_capture/server/audio_capturer_wrapper.cpp b/services/services/screen_capture/server/audio_capturer_wrapper.cpp index 4105d38b1..35341475f 100644 --- a/services/services/screen_capture/server/audio_capturer_wrapper.cpp +++ b/services/services/screen_capture/server/audio_capturer_wrapper.cpp @@ -210,11 +210,13 @@ int32_t AudioCapturerWrapper::GetBufferSize(size_t &size) { using namespace std::chrono_literals; std::unique_lock lock(bufferMutex_); - MEDIA_LOGD("0x%{public}06" PRIXPTR " GetBufferSize Buffer S, name:%{public}s", FAKE_POINTER(this), threadName_.c_str()); + MEDIA_LOGD("0x%{public}06" PRIXPTR " GetBufferSize Buffer S, name:%{public}s", + FAKE_POINTER(this), threadName_.c_str()); CHECK_AND_RETURN_RET_LOG(isRunning_.load(), MSERR_UNKNOWN, "GetBufferSize failed, not running"); CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr && audioCapturer_->GetBufferSize(size) >= 0, MSERR_NO_MEMORY, "CaptureAudio GetBufferSize failed"); - MEDIA_LOGD("0x%{public}06" PRIXPTR " GetBufferSize Buffer E, name:%{public}s", FAKE_POINTER(this), threadName_.c_str()); + MEDIA_LOGD("0x%{public}06" PRIXPTR " GetBufferSize Buffer E, name:%{public}s", + FAKE_POINTER(this), threadName_.c_str()); return MSERR_OK; } diff --git a/services/services/screen_capture/server/screen_capture_server.cpp b/services/services/screen_capture/server/screen_capture_server.cpp index 08553ef95..a95a674ac 100644 --- a/services/services/screen_capture/server/screen_capture_server.cpp +++ b/services/services/screen_capture/server/screen_capture_server.cpp @@ -926,6 +926,31 @@ int32_t ScreenCaptureServer::InitVideoCap(VideoCaptureInfo videoInfo) return MSERR_OK; } +int32_t ScreenCaptureServer::InitRecorderInfo(std::shared_ptr &recorder, AudioCaptureInfo audioInfo) +{ + int32_t ret = MSERR_OK; + ret = recorder->SetOutputFormat(fileFormat_); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetOutputFormat failed"); + ret = recorder->SetAudioEncoder(audioSourceId_, captureConfig_.audioInfo.audioEncInfo.audioCodecformat); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioEncoder failed"); + ret = recorder->SetAudioSampleRate(audioSourceId_, audioInfo.audioSampleRate); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioSampleRate failed"); + ret = recorder->SetAudioChannels(audioSourceId_, audioInfo.audioChannels); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioChannels failed"); + ret = recorder->SetAudioEncodingBitRate(audioSourceId_, captureConfig_.audioInfo.audioEncInfo.audioBitrate); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioEncodingBitRate failed"); + ret = recorder->SetVideoEncoder(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoCodec); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoEncoder failed"); + ret = recorder->SetVideoSize(videoSourceId_, captureConfig_.videoInfo.videoCapInfo.videoFrameWidth, + captureConfig_.videoInfo.videoCapInfo.videoFrameHeight); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoSize failed"); + ret = recorder->SetVideoFrameRate(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoFrameRate); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoFrameRate failed"); + ret = recorder->SetVideoEncodingBitRate(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoBitrate); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoEncodingBitRate failed"); + return MSERR_OK; +} + int32_t ScreenCaptureServer::InitRecorder() { CHECK_AND_RETURN_RET_LOG(outputFd_ > 0, MSERR_INVALID_OPERATION, "the outputFd is invalid"); @@ -962,25 +987,9 @@ int32_t ScreenCaptureServer::InitRecorder() ret = recorder_->SetVideoSource(captureConfig_.videoInfo.videoCapInfo.videoSource, videoSourceId_); CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoSource failed"); - ret = recorder_->SetOutputFormat(fileFormat_); - CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetOutputFormat failed"); - ret = recorder_->SetAudioEncoder(audioSourceId_, captureConfig_.audioInfo.audioEncInfo.audioCodecformat); - CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioEncoder failed"); - ret = recorder_->SetAudioSampleRate(audioSourceId_, audioInfo.audioSampleRate); - CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioSampleRate failed"); - ret = recorder_->SetAudioChannels(audioSourceId_, audioInfo.audioChannels); - CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioChannels failed"); - ret = recorder_->SetAudioEncodingBitRate(audioSourceId_, captureConfig_.audioInfo.audioEncInfo.audioBitrate); - CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioEncodingBitRate failed"); - ret = recorder_->SetVideoEncoder(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoCodec); - CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoEncoder failed"); - ret = recorder_->SetVideoSize(videoSourceId_, captureConfig_.videoInfo.videoCapInfo.videoFrameWidth, - captureConfig_.videoInfo.videoCapInfo.videoFrameHeight); - CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoSize failed"); - ret = recorder_->SetVideoFrameRate(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoFrameRate); - CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoFrameRate failed"); - ret = recorder_->SetVideoEncodingBitRate(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoBitrate); - CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoEncodingBitRate failed"); + ret = InitRecorderInfo(recorder_, audioInfo); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "InitRecorderInfo failed"); + ret = recorder_->SetOutputFile(outputFd_); CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetOutputFile failed"); ret = recorder_->Prepare(); @@ -1973,13 +1982,13 @@ int32_t AudioDataSource::ReadAt(std::shared_ptr buffer, uint32_t lengt MEDIA_LOGE("buffer->memory_ is nullptr"); return MSERR_INVALID_VAL; } - char* mix_data = new char[innerAudioBuffer->length]; - char* srcData[2] = {NULL}; + char* mixData = new char[innerAudioBuffer->length]; + char* srcData[2] = {nullptr}; srcData[0] = reinterpret_cast(innerAudioBuffer->buffer); srcData[1] = reinterpret_cast(micAudioBuffer->buffer); int channels = 2; - MixAudio(srcData, mix_data, channels, innerAudioBuffer->length); - bufferMem->Write((uint8_t*)mix_data, innerAudioBuffer->length, 0); + MixAudio(srcData, mixData, channels, innerAudioBuffer->length); + bufferMem->Write((uint8_t*)mixData, innerAudioBuffer->length, 0); return screenCaptureServer_->ReleaseAudioBufferMix(type_); } else { MEDIA_LOGE("AudioDataSource AcquireAudioBufferMix failed"); @@ -1989,7 +1998,7 @@ int32_t AudioDataSource::ReadAt(std::shared_ptr buffer, uint32_t lengt int32_t AudioDataSource::GetSize(int64_t &size) { - size_t bufferLen; + size_t bufferLen = 0; int32_t ret = screenCaptureServer_->GetInnerAudioCaptureBufferSize(bufferLen); MEDIA_LOGD("AudioDataSource::GetSize : %{public}zu", bufferLen); size = static_cast(bufferLen); @@ -1999,8 +2008,8 @@ int32_t AudioDataSource::GetSize(int64_t &size) void AudioDataSource::MixAudio(char** srcData, char* mixData, int channels, int bufferSize) { MEDIA_LOGD("AudioDataSource MixAudio"); - int const MAX = 32767; - int const MIN = -32768; + int const max = 32767; + int const min = -32768; double const splitNum = 32; int const doubleChannels = 2; double coefficient = 1; @@ -2013,21 +2022,21 @@ void AudioDataSource::MixAudio(char** srcData, char* mixData, int channels, int for (totalNum = 0; totalNum < bufferSize / channels; totalNum++) { int temp = 0; for (channelNum = 0; channelNum < channels; channelNum++) { - temp += *(short*)(srcData[channelNum] + totalNum * channels); + temp += *reinterpret_cast(srcData[channelNum] + totalNum * channels); } - output = (int)(temp * coefficient); - if (output > MAX) { - coefficient = (double)MAX / (double)(output); - output = MAX; + output = static_cast(temp * coefficient); + if (output > max) { + coefficient = static_cast(max) / static_cast(output); + output = max; } - if (output < MIN) { - coefficient = (double)MIN / (double)(output); - output = MIN; + if (output < min) { + coefficient = static_cast(min) / static_cast(output); + output = min; } if (coefficient < 1) { - coefficient += ((double)1 - coefficient) / splitNum; + coefficient += (static_cast(1) - coefficient) / splitNum; } - *(short*)(mixData + totalNum * doubleChannels) = (short)output; + *reinterpret_cast(mixData + totalNum * doubleChannels) = static_cast(output); } } } // namespace Media diff --git a/services/services/screen_capture/server/screen_capture_server.h b/services/services/screen_capture/server/screen_capture_server.h index 910c764b9..9d54aeb3a 100644 --- a/services/services/screen_capture/server/screen_capture_server.h +++ b/services/services/screen_capture/server/screen_capture_server.h @@ -205,6 +205,7 @@ private: int32_t StartScreenCaptureInner(bool isPrivacyAuthorityEnabled); int32_t OnStartScreenCapture(); void PostStartScreenCapture(bool isSuccess); + int32_t InitRecorderInfo(std::shared_ptr &recorder, AudioCaptureInfo audioInfo); int32_t InitRecorder(); int32_t StartScreenCaptureFile(); int32_t StartScreenCaptureStream(); -- Gitee