diff --git a/services/services/screen_capture/server/audio_capturer_wrapper.cpp b/services/services/screen_capture/server/audio_capturer_wrapper.cpp index fa4542f894f678d1c2267e9f40a54837fd4a134b..696528c9b85df3150ce6ec93f63cc6ef26e0103f 100644 --- a/services/services/screen_capture/server/audio_capturer_wrapper.cpp +++ b/services/services/screen_capture/server/audio_capturer_wrapper.cpp @@ -222,11 +222,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 8ad746a0ddba72a3f629615999a616271f1accb5..173355b78d5a4a6d19efa213e4df2f39b73f5d1e 100644 --- a/services/services/screen_capture/server/screen_capture_server.cpp +++ b/services/services/screen_capture/server/screen_capture_server.cpp @@ -927,6 +927,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"); @@ -969,25 +994,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(); @@ -1980,7 +1989,7 @@ int32_t AudioDataSource::ReadAt(std::shared_ptr buffer, uint32_t lengt } if (type_ == AVScreenCaptureMixMode::MIX_MODE) { char* mixData = new char[innerAudioBuffer->length]; - char* srcData[2] = {NULL}; + char* srcData[2] = {nullptr}; srcData[0] = reinterpret_cast(innerAudioBuffer->buffer); srcData[1] = reinterpret_cast(micAudioBuffer->buffer); int channels = 2; @@ -2003,7 +2012,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); @@ -2013,8 +2022,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; @@ -2027,21 +2036,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 47f2661d2a2b135bff4096da166612e3c7c8d650..8137b15b466702a4769afc8534f655246ad9558e 100644 --- a/services/services/screen_capture/server/screen_capture_server.h +++ b/services/services/screen_capture/server/screen_capture_server.h @@ -212,6 +212,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();