diff --git a/frameworks/native/audiocapturer/include/audio_capturer_private.h b/frameworks/native/audiocapturer/include/audio_capturer_private.h index aaf9d52b1a0f882c27bae4570d15c35460611ca3..365b821af48133d38fded3f7b6cb7535612e95d3 100644 --- a/frameworks/native/audiocapturer/include/audio_capturer_private.h +++ b/frameworks/native/audiocapturer/include/audio_capturer_private.h @@ -27,6 +27,7 @@ namespace AudioStandard { constexpr uint32_t INVALID_SESSION_ID = static_cast(-1); class AudioCapturerStateChangeCallbackImpl; class CapturerPolicyServiceDiedCallback; +class InputDeviceChangeWithInfoCallbackImpl; class AudioCapturerPrivate : public AudioCapturer { public: @@ -54,7 +55,7 @@ public: const std::shared_ptr &callback) override; void UnsetCapturerPeriodPositionCallback() override; int32_t SetBufferDuration(uint64_t bufferDuration) const override; - int32_t SetCaptureMode(AudioCaptureMode renderMode)const override; + int32_t SetCaptureMode(AudioCaptureMode renderMode) override; AudioCaptureMode GetCaptureMode()const override; int32_t SetCapturerReadCallback(const std::shared_ptr &callback) override; int32_t GetBufferDesc(BufferDesc &bufDesc)const override; @@ -87,6 +88,8 @@ public: uint32_t GetOverflowCount() const override; + void SwitchStream(const uint32_t sessionId, const int32_t streamFlag); + std::shared_ptr audioStream_; AudioCapturerInfo capturerInfo_ = {}; AudioPlaybackCaptureConfig filterConfig_ = {{{}, FilterMode::INCLUDE, {}, FilterMode::INCLUDE}, false}; @@ -114,10 +117,17 @@ public: private: int32_t InitAudioInterruptCallback(); + int32_t InitInputDeviceChangeCallback(); + void SetSwitchInfo(IAudioStream::SwitchInfo info, std::shared_ptr audioStream); + bool SwitchToTargetStream(IAudioStream::StreamClass targetClass, uint32_t &newSessionId); void InitLatencyMeasurement(const AudioStreamParams &audioStreamParams); int32_t InitAudioStream(const AudioStreamParams &AudioStreamParams); void CheckSignalData(uint8_t *buffer, size_t bufferSize) const; void WriteOverflowEvent() const; + IAudioStream::StreamClass GetPreferredStreamClass(AudioStreamParams audioStreamParams); + std::shared_ptr inputDeviceChangeCallback_ = nullptr; + bool isSwitching_ = false; + mutable std::mutex switchStreamMutex_; std::shared_ptr audioStreamCallback_ = nullptr; std::shared_ptr audioInterruptCallback_ = nullptr; AppInfo appInfo_ = {}; @@ -135,6 +145,8 @@ private: bool latencyMeasEnabled_ = false; std::shared_ptr signalDetectAgent_ = nullptr; FILE *dumpFile_ = nullptr; + AudioCaptureMode audioCaptureMode_ = CAPTURE_MODE_NORMAL; + bool isFastVoipSupported_ = false; }; class AudioCapturerInterruptCallbackImpl : public AudioInterruptCallback { @@ -190,6 +202,33 @@ private: AudioCapturerPrivate *capturer_; }; +class InputDeviceChangeWithInfoCallbackImpl : public DeviceChangeWithInfoCallback { +public: + InputDeviceChangeWithInfoCallbackImpl() = default; + + virtual ~InputDeviceChangeWithInfoCallbackImpl() = default; + + void OnDeviceChangeWithInfo( + const uint32_t sessionId, const DeviceInfo &deviceInfo, const AudioStreamDeviceChangeReason reason) override; + + void OnRecreateStreamEvent(const uint32_t sessionId, const int32_t streamFlag) override; + + void SetAudioCapturerObj(AudioCapturerPrivate * capturerObj) + { + std::lock_guard lock(mutex_); + capturer_ = capturerObj; + } + + void UnsetAudioCapturerObj() + { + std::lock_guard lock(mutex_); + capturer_ = nullptr; + } +private: + AudioCapturerPrivate *capturer_; + std::mutex mutex_; +}; + class CapturerPolicyServiceDiedCallback : public RendererOrCapturerPolicyServiceDiedCallback { public: CapturerPolicyServiceDiedCallback(); diff --git a/frameworks/native/audiocapturer/src/audio_capturer.cpp b/frameworks/native/audiocapturer/src/audio_capturer.cpp index 6d00bc3db2c965df08562aaad3151c94328e462c..b7425cf438db94854ba5dbbd3deeb30c2e64e409 100644 --- a/frameworks/native/audiocapturer/src/audio_capturer.cpp +++ b/frameworks/native/audiocapturer/src/audio_capturer.cpp @@ -46,6 +46,11 @@ AudioCapturer::~AudioCapturer() = default; AudioCapturerPrivate::~AudioCapturerPrivate() { AUDIO_INFO_LOG("~AudioCapturerPrivate"); + std::shared_ptr inputDeviceChangeCallback = inputDeviceChangeCallback_; + if (inputDeviceChangeCallback != nullptr) { + inputDeviceChangeCallback->UnsetAudioCapturerObj(); + } + AudioPolicyManager::GetInstance().UnregisterDeviceChangeWithInfoCallback(sessionID_); if (audioStream_ != nullptr) { audioStream_->ReleaseAudioStream(true); audioStream_ = nullptr; @@ -122,11 +127,11 @@ std::unique_ptr AudioCapturer::Create(const AudioCapturerOptions AUDIO_DEBUG_LOG("Set application cache path"); capturer->cachePath_ = cachePath; } - AUDIO_INFO_LOG("StreamClientState for Capturer::Create. sourceType: %{public}d, uid: %{public}d", - sourceType, appInfo.appUid); + AUDIO_INFO_LOG("Capturer sourceType: %{public}d, uid: %{public}d", sourceType, appInfo.appUid); // InitPlaybackCapturer will be replaced by UpdatePlaybackCaptureConfig. capturer->capturerInfo_.sourceType = sourceType; capturer->capturerInfo_.capturerFlags = capturerOptions.capturerInfo.capturerFlags; + capturer->capturerInfo_.originalFlag = capturerOptions.capturerInfo.capturerFlags; capturer->filterConfig_ = capturerOptions.playbackCaptureConfig; if (capturer->SetParams(params) != SUCCESS) { AudioCapturer::SendCapturerCreateError(sourceType, ERR_OPERATION_FAILED); @@ -218,19 +223,30 @@ int32_t AudioCapturerPrivate::GetFrameCount(uint32_t &frameCount) const return audioStream_->GetFrameCount(frameCount); } +IAudioStream::StreamClass AudioCapturerPrivate::GetPreferredStreamClass(AudioStreamParams audioStreamParams) +{ + int32_t flag = AudioPolicyManager::GetInstance().GetPreferredInputStreamType(capturerInfo_); + AUDIO_INFO_LOG("Preferred capturer flag: %{public}d", flag); + if (flag == AUDIO_FLAG_MMAP && IAudioStream::IsStreamSupported(capturerInfo_.originalFlag, audioStreamParams)) { + capturerInfo_.capturerFlags = AUDIO_FLAG_MMAP; + return IAudioStream::FAST_STREAM; + } + if (flag == AUDIO_FLAG_VOIP_FAST) { + // It is not possible to directly create a fast VoIP stream + isFastVoipSupported_ = true; + } + + capturerInfo_.capturerFlags = AUDIO_FLAG_NORMAL; + return IAudioStream::PA_STREAM; +} + int32_t AudioCapturerPrivate::SetParams(const AudioCapturerParams params) { Trace trace("AudioCapturer::SetParams"); AUDIO_INFO_LOG("StreamClientState for Capturer::SetParams."); AudioStreamParams audioStreamParams = ConvertToAudioStreamParams(params); - IAudioStream::StreamClass streamClass = IAudioStream::PA_STREAM; - if (capturerInfo_.capturerFlags == STREAM_FLAG_FAST && - IAudioStream::IsStreamSupported(capturerInfo_.capturerFlags, audioStreamParams) && - AudioPolicyManager::GetInstance().GetPreferredInputStreamType(capturerInfo_) == AUDIO_FLAG_MMAP) { - AUDIO_INFO_LOG("Create stream with flag AUDIO_FLAG_MMAP"); - streamClass = IAudioStream::FAST_STREAM; - } + IAudioStream::StreamClass streamClass = GetPreferredStreamClass(audioStreamParams); // check AudioStreamParams for fast stream if (audioStream_ == nullptr) { @@ -253,9 +269,35 @@ int32_t AudioCapturerPrivate::SetParams(const AudioCapturerParams params) DumpFileUtil::OpenDumpFile(DUMP_CLIENT_PARA, DUMP_AUDIO_CAPTURER_FILENAME, &dumpFile_); + ret = InitInputDeviceChangeCallback(); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Init input device change callback failed"); + return InitAudioInterruptCallback(); } +int32_t AudioCapturerPrivate::InitInputDeviceChangeCallback() +{ + CHECK_AND_RETURN_RET_LOG(GetCurrentInputDevices(currentDeviceInfo_) == SUCCESS, ERROR, + "Get current device info failed"); + + if (!inputDeviceChangeCallback_) { + inputDeviceChangeCallback_ = std::make_shared(); + CHECK_AND_RETURN_RET_LOG(inputDeviceChangeCallback_ != nullptr, ERROR, "Memory allocation failed"); + } + + inputDeviceChangeCallback_->SetAudioCapturerObj(this); + + uint32_t sessionId; + int32_t ret = GetAudioStreamId(sessionId); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Get sessionId failed"); + + ret = AudioPolicyManager::GetInstance().RegisterDeviceChangeWithInfoCallback(sessionId, + inputDeviceChangeCallback_); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Register failed"); + + return SUCCESS; +} + int32_t AudioCapturerPrivate::InitAudioStream(const AudioStreamParams &audioStreamParams) { Trace trace("AudioCapturer::InitAudioStream"); @@ -449,6 +491,7 @@ bool AudioCapturerPrivate::Start() const Trace trace("AudioCapturer::Start"); AUDIO_INFO_LOG("StreamClientState for Capturer::Start. id %{public}u, sourceType: %{public}d", sessionID_, audioInterrupt_.audioFocusType.sourceType); + CHECK_AND_RETURN_RET_LOG(!isSwitching_, false, "Operation failed, in switching"); if (capturerInfo_.sourceType != SOURCE_TYPE_VOICE_CALL) { bool recordingStateChange = audioStream_->CheckRecordingStateChange(appInfo_.appTokenId, @@ -493,6 +536,7 @@ bool AudioCapturerPrivate::Pause() const { Trace trace("AudioCapturer::Pause"); AUDIO_INFO_LOG("StreamClientState for Capturer::Pause. id %{public}u", sessionID_); + CHECK_AND_RETURN_RET_LOG(!isSwitching_, false, "Operation failed, in switching"); if (capturerInfo_.sourceType != SOURCE_TYPE_VOICE_CALL) { if (!audioStream_->CheckRecordingStateChange(appInfo_.appTokenId, appInfo_.appFullTokenId, @@ -516,6 +560,7 @@ bool AudioCapturerPrivate::Stop() const { Trace trace("AudioCapturer::Stop"); AUDIO_INFO_LOG("StreamClientState for Capturer::Stop. id %{public}u", sessionID_); + CHECK_AND_RETURN_RET_LOG(!isSwitching_, false, "Operation failed, in switching"); if (capturerInfo_.sourceType != SOURCE_TYPE_VOICE_CALL) { if (!audioStream_->CheckRecordingStateChange(appInfo_.appTokenId, appInfo_.appFullTokenId, @@ -731,8 +776,29 @@ AudioStreamType AudioCapturer::FindStreamTypeBySourceType(SourceType sourceType) } } -int32_t AudioCapturerPrivate::SetCaptureMode(AudioCaptureMode captureMode) const +int32_t AudioCapturerPrivate::SetCaptureMode(AudioCaptureMode captureMode) { + AUDIO_INFO_LOG("Capture mode: %{public}d", captureMode); + audioCaptureMode_ = captureMode; + + if (capturerInfo_.sourceType == SOURCE_TYPE_VOICE_COMMUNICATION && captureMode == CAPTURE_MODE_CALLBACK && + isFastVoipSupported_) { + AUDIO_INFO_LOG("Switch to fast voip stream"); + uint32_t sessionId = 0; + int32_t ret = audioStream_->GetAudioSessionID(sessionId); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Get audio session Id failed"); + uint32_t newSessionId = 0; + if (!SwitchToTargetStream(IAudioStream::VOIP_STREAM, newSessionId)) { + AUDIO_ERR_LOG("Switch to target stream failed"); + return ERROR; + } + ret = AudioPolicyManager::GetInstance().RegisterDeviceChangeWithInfoCallback(newSessionId, + inputDeviceChangeCallback_); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Register device change callback for new session failed"); + ret = AudioPolicyManager::GetInstance().UnregisterDeviceChangeWithInfoCallback(sessionId); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Unregister device change callback for old session failed"); + } + return audioStream_->SetCaptureMode(captureMode); } @@ -971,6 +1037,111 @@ uint32_t AudioCapturerPrivate::GetOverflowCount() const return audioStream_->GetOverflowCount(); } +void AudioCapturerPrivate::SetSwitchInfo(IAudioStream::SwitchInfo info, std::shared_ptr audioStream) +{ + CHECK_AND_RETURN_LOG(audioStream, "stream is nullptr"); + + audioStream->SetStreamTrackerState(false); + audioStream->SetApplicationCachePath(info.cachePath); + audioStream->SetClientID(info.clientPid, info.clientUid, appInfo_.appTokenId); + audioStream->SetCapturerInfo(info.capturerInfo); + audioStream->SetAudioStreamInfo(info.params, capturerProxyObj_); + audioStream->SetCaptureMode(info.captureMode); + + // set callback + if ((info.renderPositionCb != nullptr) && (info.frameMarkPosition > 0)) { + audioStream->SetRendererPositionCallback(info.frameMarkPosition, info.renderPositionCb); + } + + if ((info.capturePositionCb != nullptr) && (info.frameMarkPosition > 0)) { + audioStream->SetCapturerPositionCallback(info.frameMarkPosition, info.capturePositionCb); + } + + if ((info.renderPeriodPositionCb != nullptr) && (info.framePeriodNumber > 0)) { + audioStream->SetRendererPeriodPositionCallback(info.framePeriodNumber, info.renderPeriodPositionCb); + } + + if ((info.capturePeriodPositionCb != nullptr) && (info.framePeriodNumber > 0)) { + audioStream->SetCapturerPeriodPositionCallback(info.framePeriodNumber, info.capturePeriodPositionCb); + } + + audioStream->SetStreamCallback(info.audioStreamCallback); +} + +bool AudioCapturerPrivate::SwitchToTargetStream(IAudioStream::StreamClass targetClass, uint32_t &newSessionId) +{ + bool switchResult = false; + if (audioStream_) { + Trace trace("SwitchToTargetStream"); + isSwitching_ = true; + CapturerState previousState = GetStatus(); + AUDIO_INFO_LOG("Previous stream state: %{public}d", previousState); + if (previousState == CAPTURER_RUNNING) { + // stop old stream + switchResult = audioStream_->StopAudioStream(); + CHECK_AND_RETURN_RET_LOG(switchResult, false, "StopAudioStream failed."); + } + std::lock_guard lock(switchStreamMutex_); + // switch new stream + IAudioStream::SwitchInfo info; + audioStream_->GetSwitchInfo(info); + if (targetClass == IAudioStream::VOIP_STREAM) { + info.capturerInfo.originalFlag = AUDIO_FLAG_VOIP_FAST; + } + std::shared_ptr newAudioStream = IAudioStream::GetRecordStream(targetClass, info.params, + info.eStreamType, appInfo_.appPid); + CHECK_AND_RETURN_RET_LOG(newAudioStream != nullptr, false, "GetRecordStream failed."); + AUDIO_INFO_LOG("Get new stream success!"); + + // set new stream info + SetSwitchInfo(info, newAudioStream); + + // release old stream and restart audio stream + switchResult = audioStream_->ReleaseAudioStream(); + CHECK_AND_RETURN_RET_LOG(switchResult, false, "release old stream failed."); + + if (previousState == CAPTURER_RUNNING) { + // restart audio stream + switchResult = newAudioStream->StartAudioStream(); + CHECK_AND_RETURN_RET_LOG(switchResult, false, "start new stream failed."); + } + audioStream_ = newAudioStream; + isSwitching_ = false; + audioStream_->GetAudioSessionID(newSessionId); + switchResult= true; + } + return switchResult; +} + +void AudioCapturerPrivate::SwitchStream(const uint32_t sessionId, const int32_t streamFlag) +{ + IAudioStream::StreamClass targetClass = IAudioStream::PA_STREAM; + switch (streamFlag) { + case AUDIO_FLAG_NORMAL: + capturerInfo_.capturerFlags = AUDIO_FLAG_NORMAL; + targetClass = IAudioStream::PA_STREAM; + break; + case AUDIO_FLAG_MMAP: + capturerInfo_.capturerFlags = AUDIO_FLAG_MMAP; + targetClass = IAudioStream::FAST_STREAM; + break; + case AUDIO_FLAG_VOIP_FAST: + capturerInfo_.capturerFlags = AUDIO_FLAG_VOIP_FAST; + targetClass = IAudioStream::VOIP_STREAM; + break; + } + + uint32_t newSessionId = 0; + if (!SwitchToTargetStream(targetClass, newSessionId)) { + AUDIO_ERR_LOG("Switch to target stream failed"); + } + int32_t ret = AudioPolicyManager::GetInstance().RegisterDeviceChangeWithInfoCallback(newSessionId, + inputDeviceChangeCallback_); + CHECK_AND_RETURN_LOG(ret == SUCCESS, "Register device change callback for new session failed"); + ret = AudioPolicyManager::GetInstance().UnregisterDeviceChangeWithInfoCallback(sessionId); + CHECK_AND_RETURN_LOG(ret == SUCCESS, "Unregister device change callback for old session failed"); +} + AudioCapturerStateChangeCallbackImpl::AudioCapturerStateChangeCallbackImpl() { AUDIO_DEBUG_LOG("AudioCapturerStateChangeCallbackImpl instance create"); @@ -1108,6 +1279,18 @@ void AudioCapturerStateChangeCallbackImpl::HandleCapturerDestructor() capturer_ = nullptr; } +void InputDeviceChangeWithInfoCallbackImpl::OnDeviceChangeWithInfo( + const uint32_t sessionId, const DeviceInfo &deviceInfo, const AudioStreamDeviceChangeReason reason) +{ + AUDIO_INFO_LOG("For capturer, OnDeviceChangeWithInfo callback is not support"); +} + +void InputDeviceChangeWithInfoCallbackImpl::OnRecreateStreamEvent(const uint32_t sessionId, const int32_t streamFlag) +{ + AUDIO_INFO_LOG("Enter"); + capturer_->SwitchStream(sessionId, streamFlag); +} + CapturerPolicyServiceDiedCallback::CapturerPolicyServiceDiedCallback() { AUDIO_DEBUG_LOG("CapturerPolicyServiceDiedCallback create"); diff --git a/frameworks/native/audiopolicy/include/audio_policy_manager.h b/frameworks/native/audiopolicy/include/audio_policy_manager.h index c7ed9fd50e39183bddadc6553b4b63b336e44dee..bcfe8ca213a8e4e161f08782ec6a1e5f8f94828f 100644 --- a/frameworks/native/audiopolicy/include/audio_policy_manager.h +++ b/frameworks/native/audiopolicy/include/audio_policy_manager.h @@ -175,10 +175,10 @@ public: int32_t UnregisterAudioCapturerEventListener(const int32_t clientPid); - int32_t RegisterOutputDeviceChangeWithInfoCallback( - const uint32_t sessionID, const std::shared_ptr &callback); + int32_t RegisterDeviceChangeWithInfoCallback( + const uint32_t sessionID, const std::shared_ptr &callback); - int32_t UnregisterOutputDeviceChangeWithInfoCallback(const uint32_t sessionID); + int32_t UnregisterDeviceChangeWithInfoCallback(const uint32_t sessionID); int32_t RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo, const std::shared_ptr &clientTrackerObj); diff --git a/frameworks/native/audiorenderer/include/audio_renderer_private.h b/frameworks/native/audiorenderer/include/audio_renderer_private.h index 064d46b05825f5c24012a09396bc614f63e65eed..eec45de7f7a85e109785ef9df877ae96113fb56b 100644 --- a/frameworks/native/audiorenderer/include/audio_renderer_private.h +++ b/frameworks/native/audiorenderer/include/audio_renderer_private.h @@ -69,7 +69,7 @@ public: const std::shared_ptr &callback) override; void UnsetRendererPeriodPositionCallback() override; int32_t SetBufferDuration(uint64_t bufferDuration) const override; - int32_t SetRenderMode(AudioRenderMode renderMode) const override; + int32_t SetRenderMode(AudioRenderMode renderMode) override; AudioRenderMode GetRenderMode() const override; int32_t SetRendererWriteCallback(const std::shared_ptr &callback) override; int32_t SetRendererFirstFrameWritingCallback( @@ -93,6 +93,7 @@ public: int32_t GetCurrentOutputDevices(DeviceInfo &deviceInfo) const override; uint32_t GetUnderflowCount() const override; bool IsDeviceChanged(DeviceInfo &newDeviceInfo); + void SwitchStream(const uint32_t sessionId, const int32_t streamFlag); int32_t RegisterAudioRendererEventListener(const int32_t clientPid, const std::shared_ptr &callback) override; int32_t UnregisterAudioRendererEventListener(const int32_t clientPid) override; @@ -147,18 +148,19 @@ public: protected: // Method for switching between normal and low latency paths - void SwitchStream(bool isLowLatencyDevice); + void SwitchStream(bool isLowLatencyDevice, bool isHalNeedChange); private: int32_t InitAudioInterruptCallback(); int32_t InitOutputDeviceChangeCallback(); int32_t InitAudioStream(AudioStreamParams audioStreamParams); void SetSwitchInfo(IAudioStream::SwitchInfo info, std::shared_ptr audioStream); - bool SwitchToTargetStream(IAudioStream::StreamClass targetClass); + bool SwitchToTargetStream(IAudioStream::StreamClass targetClass, uint32_t &newSessionId); void SetSelfRendererStateCallback(); void InitLatencyMeasurement(const AudioStreamParams &audioStreamParams); void MockPcmData(uint8_t *buffer, size_t bufferSize) const; void WriteUnderrunEvent() const; + IAudioStream::StreamClass GetPreferredStreamClass(AudioStreamParams audioStreamParams); std::shared_ptr audioInterruptCallback_ = nullptr; std::shared_ptr audioStreamCallback_ = nullptr; @@ -178,6 +180,8 @@ private: std::shared_ptr latencyMeasurement_ = nullptr; bool isSwitching_ = false; mutable std::mutex switchStreamMutex_; + mutable AudioRenderMode audioRenderMode_ = RENDER_MODE_NORMAL; + bool isFastVoipSupported_ = false; float speed_ = 1.0; bool isOffloadAllowed_ = true; @@ -230,21 +234,32 @@ private: std::mutex mutex_; }; -class OutputDeviceChangeWithInfoCallbackImpl : public OutputDeviceChangeWithInfoCallback { +class OutputDeviceChangeWithInfoCallbackImpl : public DeviceChangeWithInfoCallback { public: OutputDeviceChangeWithInfoCallbackImpl() = default; virtual ~OutputDeviceChangeWithInfoCallbackImpl() = default; - void OnOutputDeviceChangeWithInfo( + + void OnDeviceChangeWithInfo( const uint32_t sessionId, const DeviceInfo &deviceInfo, const AudioStreamDeviceChangeReason reason) override; + + void OnRecreateStreamEvent(const uint32_t sessionId, const int32_t streamFlag) override; + void SaveCallback(const std::weak_ptr &callback) { callback_ = callback; } + + void RemoveCallback() + { + callback_.reset(); + } + void SetAudioRendererObj(AudioRendererPrivate *rendererObj) { std::lock_guard lock(mutex_); renderer_ = rendererObj; } + void UnsetAudioRendererObj() { std::lock_guard lock(mutex_); diff --git a/frameworks/native/audiorenderer/src/audio_renderer.cpp b/frameworks/native/audiorenderer/src/audio_renderer.cpp index f27967fb5a1d623c8a7ec7ded6ab2a2971db6c83..f22d87c2d6fa141da86e889bdf065334cccc9e48 100644 --- a/frameworks/native/audiorenderer/src/audio_renderer.cpp +++ b/frameworks/native/audiorenderer/src/audio_renderer.cpp @@ -67,6 +67,7 @@ std::mutex AudioRenderer::createRendererMutex_; AudioRenderer::~AudioRenderer() = default; AudioRendererPrivate::~AudioRendererPrivate() { + AUDIO_INFO_LOG("Destruct in"); abortRestore_ = true; std::shared_ptr audioDeviceChangeCallback = audioDeviceChangeCallback_; if (audioDeviceChangeCallback != nullptr) { @@ -79,7 +80,7 @@ AudioRendererPrivate::~AudioRendererPrivate() if (outputDeviceChangeCallback != nullptr) { outputDeviceChangeCallback->UnsetAudioRendererObj(); } - AudioPolicyManager::GetInstance().UnregisterOutputDeviceChangeWithInfoCallback(sessionID_); + AudioPolicyManager::GetInstance().UnregisterDeviceChangeWithInfoCallback(sessionID_); RendererState state = GetStatus(); if (state != RENDERER_RELEASED && state != RENDERER_NEW) { @@ -219,6 +220,7 @@ std::unique_ptr AudioRenderer::Create(const std::string cachePath audioRenderer->rendererInfo_.contentType = rendererOptions.rendererInfo.contentType; audioRenderer->rendererInfo_.streamUsage = rendererOptions.rendererInfo.streamUsage; audioRenderer->rendererInfo_.rendererFlags = rendererFlags; + audioRenderer->rendererInfo_.originalFlag = rendererFlags; audioRenderer->privacyType_ = rendererOptions.privacyType; AudioRendererParams params = SetStreamInfoToParams(rendererOptions.streamInfo); if (audioRenderer->SetParams(params) != SUCCESS) { @@ -345,11 +347,21 @@ int32_t AudioRendererPrivate::InitAudioInterruptCallback() int32_t AudioRendererPrivate::InitOutputDeviceChangeCallback() { + CHECK_AND_RETURN_RET_LOG(GetCurrentOutputDevices(currentDeviceInfo_) == SUCCESS, ERROR, + "Get current device info failed"); + + if (!outputDeviceChangeCallback_) { + outputDeviceChangeCallback_ = std::make_shared(); + CHECK_AND_RETURN_RET_LOG(outputDeviceChangeCallback_ != nullptr, ERROR, "Memory allocation failed"); + } + + outputDeviceChangeCallback_->SetAudioRendererObj(this); + uint32_t sessionId; int32_t ret = GetAudioStreamId(sessionId); CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Get sessionId failed"); - ret = AudioPolicyManager::GetInstance().RegisterOutputDeviceChangeWithInfoCallback(sessionId, + ret = AudioPolicyManager::GetInstance().RegisterDeviceChangeWithInfoCallback(sessionId, outputDeviceChangeCallback_); CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Register failed"); @@ -405,6 +417,24 @@ AudioPrivacyType AudioRendererPrivate::GetAudioPrivacyType() return privacyType_; } +IAudioStream::StreamClass AudioRendererPrivate::GetPreferredStreamClass(AudioStreamParams audioStreamParams) +{ + int32_t flag = AudioPolicyManager::GetInstance().GetPreferredOutputStreamType(rendererInfo_); + AUDIO_INFO_LOG("Preferred renderer flag: %{public}d", flag); + if (flag == AUDIO_FLAG_MMAP && IAudioStream::IsStreamSupported(rendererInfo_.originalFlag, audioStreamParams)) { + rendererInfo_.rendererFlags = AUDIO_FLAG_MMAP; + return IAudioStream::FAST_STREAM; + } + if (flag == AUDIO_FLAG_VOIP_FAST) { + // It is not possible to directly create a fast VoIP stream + isFastVoipSupported_ = true; + } + + AUDIO_INFO_LOG("Preferred renderer flag: AUDIO_FLAG_NORMAL"); + rendererInfo_.rendererFlags = AUDIO_FLAG_NORMAL; + return IAudioStream::PA_STREAM; +} + int32_t AudioRendererPrivate::SetParams(const AudioRendererParams params) { Trace trace("AudioRenderer::SetParams"); @@ -412,17 +442,9 @@ int32_t AudioRendererPrivate::SetParams(const AudioRendererParams params) AudioStreamParams audioStreamParams = ConvertToAudioStreamParams(params); AudioStreamType audioStreamType = IAudioStream::GetStreamType(rendererInfo_.contentType, rendererInfo_.streamUsage); - IAudioStream::StreamClass streamClass = IAudioStream::PA_STREAM; - if (rendererInfo_.rendererFlags == STREAM_FLAG_FAST && - IAudioStream::IsStreamSupported(rendererInfo_.rendererFlags, audioStreamParams) && - AudioPolicyManager::GetInstance().GetPreferredOutputStreamType(rendererInfo_) == AUDIO_FLAG_MMAP) { - isFastRenderer_ = true; - streamClass = IAudioStream::FAST_STREAM; - } else { - streamClass = IAudioStream::PA_STREAM; - } - AUDIO_INFO_LOG("Create stream with falg: %{public}d, streamClass: %{public}d", rendererInfo_.rendererFlags, - streamClass); + IAudioStream::StreamClass streamClass = GetPreferredStreamClass(audioStreamParams); + AUDIO_INFO_LOG("Create stream with flag: %{public}d, original flag: %{public}d, streamClass: %{public}d", + rendererInfo_.rendererFlags, rendererInfo_.originalFlag, streamClass); // check AudioStreamParams for fast stream // As fast stream only support specified audio format, we should call GetPlaybackStream with audioStreamParams. @@ -457,10 +479,8 @@ int32_t AudioRendererPrivate::SetParams(const AudioRendererParams params) DumpFileUtil::OpenDumpFile(DUMP_CLIENT_PARA, std::to_string(sessionID_) + '_' + DUMP_AUDIO_RENDERER_FILENAME, &dumpFile_); - if (outputDeviceChangeCallback_ != nullptr) { - ret = InitOutputDeviceChangeCallback(); - CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "InitOutputDeviceChangeCallback Failed"); - } + ret = InitOutputDeviceChangeCallback(); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "InitOutputDeviceChangeCallback Failed"); return InitAudioInterruptCallback(); } @@ -729,7 +749,7 @@ bool AudioRendererPrivate::Release() const // Unregister the callaback in policy server (void)AudioPolicyManager::GetInstance().UnsetAudioInterruptCallback(sessionID_); - AudioPolicyManager::GetInstance().UnregisterOutputDeviceChangeWithInfoCallback(sessionID_); + AudioPolicyManager::GetInstance().UnregisterDeviceChangeWithInfoCallback(sessionID_); return result; } @@ -955,8 +975,29 @@ std::vector AudioRenderer::GetSupportedEncodingTypes() return AUDIO_SUPPORTED_ENCODING_TYPES; } -int32_t AudioRendererPrivate::SetRenderMode(AudioRenderMode renderMode) const +int32_t AudioRendererPrivate::SetRenderMode(AudioRenderMode renderMode) { + AUDIO_INFO_LOG("Render mode: %{public}d", renderMode); + audioRenderMode_ = renderMode; + + if (rendererInfo_.streamUsage == STREAM_USAGE_VOICE_COMMUNICATION && renderMode == RENDER_MODE_CALLBACK && + isFastVoipSupported_) { + AUDIO_INFO_LOG("Switch to fast voip stream"); + uint32_t sessionId = 0; + int32_t ret = audioStream_->GetAudioSessionID(sessionId); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Get audio session Id failed"); + uint32_t newSessionId = 0; + if (!SwitchToTargetStream(IAudioStream::VOIP_STREAM, newSessionId)) { + AUDIO_ERR_LOG("Switch to target stream failed"); + return ERROR; + } + ret = AudioPolicyManager::GetInstance().RegisterDeviceChangeWithInfoCallback(newSessionId, + outputDeviceChangeCallback_); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Register device change callback for new session failed"); + ret = AudioPolicyManager::GetInstance().UnregisterDeviceChangeWithInfoCallback(sessionId); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Unregister device change callback for old session failed"); + } + return audioStream_->SetRenderMode(renderMode); } @@ -1183,23 +1224,6 @@ int32_t AudioRendererPrivate::RegisterOutputDeviceChangeWithInfoCallback( return ERR_INVALID_PARAM; } - if (GetCurrentOutputDevices(currentDeviceInfo_) != SUCCESS) { - AUDIO_ERR_LOG("get current device info failed"); - return ERROR; - } - - if (!outputDeviceChangeCallback_) { - outputDeviceChangeCallback_ = std::make_shared(); - if (!outputDeviceChangeCallback_) { - AUDIO_ERR_LOG("Memory Allocation Failed !!"); - return ERROR; - } - } - - int32_t ret = InitOutputDeviceChangeCallback(); - CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "InitOutputDeviceChangeCallback Failed"); - - outputDeviceChangeCallback_->SetAudioRendererObj(this); outputDeviceChangeCallback_->SaveCallback(callback); AUDIO_DEBUG_LOG("RegisterOutputDeviceChangeWithInfoCallback successful!"); return SUCCESS; @@ -1221,13 +1245,7 @@ int32_t AudioRendererPrivate::UnregisterOutputDeviceChangeWithInfoCallback() return ret; } - ret = AudioPolicyManager::GetInstance().UnregisterOutputDeviceChangeWithInfoCallback(sessionId); - if (ret != 0) { - AUDIO_ERR_LOG("UnregisterAudioRendererEventListener failed"); - return ERROR; - } - - DestroyOutputDeviceChangeWithInfoCallback(); + outputDeviceChangeCallback_->RemoveCallback(); return SUCCESS; } @@ -1263,7 +1281,7 @@ void AudioRendererPrivate::SetSwitchInfo(IAudioStream::SwitchInfo info, std::sha { CHECK_AND_RETURN_LOG(audioStream, "stream is nullptr"); - audioStream->SetStreamTrackerState(info.streamTrackerRegistered); + audioStream->SetStreamTrackerState(false); audioStream->SetApplicationCachePath(info.cachePath); audioStream->SetClientID(info.clientPid, info.clientUid, appInfo_.appTokenId); audioStream->SetPrivacyType(info.privacyType); @@ -1298,22 +1316,26 @@ void AudioRendererPrivate::SetSwitchInfo(IAudioStream::SwitchInfo info, std::sha audioStream->SetRendererFirstFrameWritingCallback(info.rendererFirstFrameWritingCallback); } -bool AudioRendererPrivate::SwitchToTargetStream(IAudioStream::StreamClass targetClass) +bool AudioRendererPrivate::SwitchToTargetStream(IAudioStream::StreamClass targetClass, uint32_t &newSessionId) { - std::lock_guard lock(switchStreamMutex_); bool switchResult = false; if (audioStream_) { Trace trace("SwitchToTargetStream"); isSwitching_ = true; RendererState previousState = GetStatus(); + AUDIO_INFO_LOG("Previous stream state: %{public}d", previousState); if (previousState == RENDERER_RUNNING) { // stop old stream switchResult = audioStream_->StopAudioStream(); CHECK_AND_RETURN_RET_LOG(switchResult, false, "StopAudioStream failed."); } + std::lock_guard lock(switchStreamMutex_); // switch new stream IAudioStream::SwitchInfo info; audioStream_->GetSwitchInfo(info); + if (targetClass == IAudioStream::VOIP_STREAM) { + info.rendererInfo.originalFlag = AUDIO_FLAG_VOIP_FAST; + } std::shared_ptr newAudioStream = IAudioStream::GetPlaybackStream(targetClass, info.params, info.eStreamType, appInfo_.appPid); CHECK_AND_RETURN_RET_LOG(newAudioStream != nullptr, false, "SetParams GetPlayBackStream failed."); @@ -1333,6 +1355,7 @@ bool AudioRendererPrivate::SwitchToTargetStream(IAudioStream::StreamClass target } audioStream_ = newAudioStream; isSwitching_ = false; + audioStream_->GetAudioSessionID(newSessionId); switchResult= true; } std::shared_ptr bean = std::make_shared( @@ -1348,33 +1371,33 @@ bool AudioRendererPrivate::SwitchToTargetStream(IAudioStream::StreamClass target return switchResult; } -void AudioRendererPrivate::SwitchStream(bool isLowLatencyDevice) +void AudioRendererPrivate::SwitchStream(const uint32_t sessionId, const int32_t streamFlag) { - // switch stream for fast renderer only - if (audioStream_ != nullptr && isFastRenderer_ && !isSwitching_) { - bool needSwitch = false; - IAudioStream::StreamClass currentClass = audioStream_->GetStreamClass(); - IAudioStream::StreamClass targetClass = IAudioStream::PA_STREAM; - if (currentClass == IAudioStream::FAST_STREAM && !isLowLatencyDevice) { - needSwitch = true; - rendererInfo_.rendererFlags = 0; // Normal renderer type + IAudioStream::StreamClass targetClass = IAudioStream::PA_STREAM; + switch (streamFlag) { + case AUDIO_FLAG_NORMAL: + rendererInfo_.rendererFlags = AUDIO_FLAG_NORMAL; targetClass = IAudioStream::PA_STREAM; - } - if (currentClass == IAudioStream::PA_STREAM && isLowLatencyDevice) { - needSwitch = true; - rendererInfo_.rendererFlags = STREAM_FLAG_FAST; + break; + case AUDIO_FLAG_MMAP: + rendererInfo_.rendererFlags = AUDIO_FLAG_MMAP; targetClass = IAudioStream::FAST_STREAM; - } - if (needSwitch) { - if (!SwitchToTargetStream(targetClass) && audioRendererErrorCallback_) { - audioRendererErrorCallback_->OnError(ERROR_SYSTEM); - } - } else { - AUDIO_WARNING_LOG("need not SwitchStream!"); - } - } else { - AUDIO_DEBUG_LOG("Do not SwitchStream , is low latency renderer: %{public}d!", isFastRenderer_); + break; + case AUDIO_FLAG_VOIP_FAST: + rendererInfo_.rendererFlags = AUDIO_FLAG_VOIP_FAST; + targetClass = IAudioStream::VOIP_STREAM; + break; } + + uint32_t newSessionId = 0; + if (!SwitchToTargetStream(targetClass, newSessionId) && audioRendererErrorCallback_) { + audioRendererErrorCallback_->OnError(ERROR_SYSTEM); + } + int32_t ret = AudioPolicyManager::GetInstance().RegisterDeviceChangeWithInfoCallback(newSessionId, + outputDeviceChangeCallback_); + CHECK_AND_RETURN_LOG(ret == SUCCESS, "Register device change callback for new session failed"); + ret = AudioPolicyManager::GetInstance().UnregisterDeviceChangeWithInfoCallback(sessionId); + CHECK_AND_RETURN_LOG(ret == SUCCESS, "Unregister device change callback for old session failed"); } bool AudioRendererPrivate::IsDeviceChanged(DeviceInfo &newDeviceInfo) @@ -1410,10 +1433,6 @@ void AudioRendererStateChangeCallbackImpl::OnRendererStateChange( } isDevicedChanged = renderer_->IsDeviceChanged(deviceInfo); if (isDevicedChanged) { - if (deviceInfo.deviceType != DEVICE_TYPE_NONE && deviceInfo.deviceType != DEVICE_TYPE_INVALID) { - // switch audio channel - renderer_->SwitchStream(deviceInfo.isLowLatencyDevice); - } CHECK_AND_RETURN_LOG(cb != nullptr, "OnStateChange cb == nullptr."); } } @@ -1423,7 +1442,7 @@ void AudioRendererStateChangeCallbackImpl::OnRendererStateChange( } } -void OutputDeviceChangeWithInfoCallbackImpl::OnOutputDeviceChangeWithInfo( +void OutputDeviceChangeWithInfoCallbackImpl::OnDeviceChangeWithInfo( const uint32_t sessionId, const DeviceInfo &deviceInfo, const AudioStreamDeviceChangeReason reason) { AUDIO_INFO_LOG("OnRendererStateChange"); @@ -1434,6 +1453,12 @@ void OutputDeviceChangeWithInfoCallbackImpl::OnOutputDeviceChangeWithInfo( } } +void OutputDeviceChangeWithInfoCallbackImpl::OnRecreateStreamEvent(const uint32_t sessionId, const int32_t streamFlag) +{ + AUDIO_INFO_LOG("Enter, session id: %{public}d, stream flag: %{public}d", sessionId, streamFlag); + renderer_->SwitchStream(sessionId, streamFlag); +} + AudioEffectMode AudioRendererPrivate::GetAudioEffectMode() const { return audioStream_->GetAudioEffectMode(); diff --git a/frameworks/native/audiostream/include/i_audio_stream.h b/frameworks/native/audiostream/include/i_audio_stream.h index 84e372618b301f9bbe2e9b9884853d6ca76db0c7..fa130eb02d0fbbdf719de64a4c7f153e782dd09e 100644 --- a/frameworks/native/audiostream/include/i_audio_stream.h +++ b/frameworks/native/audiostream/include/i_audio_stream.h @@ -53,6 +53,7 @@ public: enum StreamClass : uint32_t { PA_STREAM = 0, FAST_STREAM, + VOIP_STREAM, }; struct SwitchInfo { @@ -76,6 +77,7 @@ public: std::shared_ptr proxyObj; AudioPrivacyType privacyType; float volume; + int32_t rendererFlags = AUDIO_FLAG_NORMAL; bool streamTrackerRegistered = false; @@ -95,6 +97,7 @@ public: // callback info std::shared_ptr audioStreamCallback; std::shared_ptr rendererWriteCallback; + std::shared_ptr capturerReadCallback; std::shared_ptr rendererFirstFrameWritingCallback; }; diff --git a/frameworks/native/hdiadapter/sink/bluetooth/bluetooth_renderer_sink.cpp b/frameworks/native/hdiadapter/sink/bluetooth/bluetooth_renderer_sink.cpp index 280a4174aa440e74d3bc8fc69ddffee8a49135ec..867d772d05e28f22165c2aedfee2b4418e2f8478 100644 --- a/frameworks/native/hdiadapter/sink/bluetooth/bluetooth_renderer_sink.cpp +++ b/frameworks/native/hdiadapter/sink/bluetooth/bluetooth_renderer_sink.cpp @@ -885,9 +885,9 @@ int64_t BluetoothRendererSinkInner::BytesToNanoTime(size_t lens) int32_t BluetoothRendererSinkInner::PrepareMmapBuffer() { - uint32_t totalBifferInMs = 40; // 5 * (6 + 2 * (1)) = 40ms, the buffer size, not latency. + uint32_t totalBufferInMs = 40; // 5 * (6 + 2 * (1)) = 40ms, the buffer size, not latency. frameSizeInByte_ = PcmFormatToBits(attr_.format) * attr_.channel / PCM_8_BIT; - uint32_t reqBufferFrameSize = totalBifferInMs * (attr_.sampleRate / SECOND_TO_MILLISECOND); + uint32_t reqBufferFrameSize = totalBufferInMs * (attr_.sampleRate / SECOND_TO_MILLISECOND); struct AudioMmapBufferDescriptor desc = {0}; // reqBufferFrameSize means frames in total, for example, 40ms * 48K = 1920 diff --git a/frameworks/native/hdiadapter/sink/fast/fast_audio_renderer_sink.cpp b/frameworks/native/hdiadapter/sink/fast/fast_audio_renderer_sink.cpp index 7997698699d03a97c3a3cbd774c0abe6aac61ef0..92beca8e83da328fe7bb0fd2a299190cee9808d1 100644 --- a/frameworks/native/hdiadapter/sink/fast/fast_audio_renderer_sink.cpp +++ b/frameworks/native/hdiadapter/sink/fast/fast_audio_renderer_sink.cpp @@ -350,9 +350,9 @@ void FastAudioRendererSinkInner::ReleaseMmapBuffer() int32_t FastAudioRendererSinkInner::PrepareMmapBuffer() { - uint32_t totalBifferInMs = 40; // 5 * (6 + 2 * (1)) = 40ms, the buffer size, not latency. + uint32_t totalBufferInMs = 40; // 5 * (6 + 2 * (1)) = 40ms, the buffer size, not latency. frameSizeInByte_ = PcmFormatToBits(attr_.format) * attr_.channel / PCM_8_BIT; - uint32_t reqBufferFrameSize = totalBifferInMs * (attr_.sampleRate / 1000); + uint32_t reqBufferFrameSize = totalBufferInMs * (attr_.sampleRate / 1000); struct AudioMmapBufferDescriptor desc = {0}; int32_t ret = audioRender_->ReqMmapBuffer(audioRender_, reqBufferFrameSize, &desc); diff --git a/frameworks/native/hdiadapter/sink/remote_fast/remote_fast_audio_renderer_sink.cpp b/frameworks/native/hdiadapter/sink/remote_fast/remote_fast_audio_renderer_sink.cpp index f56b6f04411ec5a662d74220079104bee2fcdb1c..c511150dd332e9310750885973472a7937823e51 100644 --- a/frameworks/native/hdiadapter/sink/remote_fast/remote_fast_audio_renderer_sink.cpp +++ b/frameworks/native/hdiadapter/sink/remote_fast/remote_fast_audio_renderer_sink.cpp @@ -313,9 +313,9 @@ int32_t RemoteFastAudioRendererSinkInner::PrepareMmapBuffer() { CHECK_AND_RETURN_RET_LOG(audioRender_ != nullptr, ERR_INVALID_HANDLE, "PrepareMmapBuffer: Audio render is null."); - int32_t totalBifferInMs = 40; // 5 * (6 + 2 * (1)) = 40ms, the buffer size, not latency. + int32_t totalBufferInMs = 40; // 5 * (6 + 2 * (1)) = 40ms, the buffer size, not latency. frameSizeInByte_ = PcmFormatToBits(attr_.format) * attr_.channel / PCM_8_BIT; - int32_t reqBufferFrameSize = totalBifferInMs * (attr_.sampleRate / 1000); + int32_t reqBufferFrameSize = totalBufferInMs * (attr_.sampleRate / 1000); struct AudioMmapBufferDescriptor desc; int32_t ret = audioRender_->ReqMmapBuffer(reqBufferFrameSize, desc); diff --git a/frameworks/native/hdiadapter/source/fast/fast_audio_capturer_source.cpp b/frameworks/native/hdiadapter/source/fast/fast_audio_capturer_source.cpp index 7400c10f78fa11f1dff525d2912ca78102e82cd0..1bacfb2eeed3916bbff593bdf6b034568cb4d07e 100644 --- a/frameworks/native/hdiadapter/source/fast/fast_audio_capturer_source.cpp +++ b/frameworks/native/hdiadapter/source/fast/fast_audio_capturer_source.cpp @@ -246,12 +246,44 @@ AudioFormat FastAudioCapturerSourceInner::ConvertToHdiFormat(HdiAdapterFormat fo return hdiFormat; } +static enum AudioInputType ConvertToHDIAudioInputType(const int32_t currSourceType) +{ + enum AudioInputType hdiAudioInputType; + switch (currSourceType) { + case SOURCE_TYPE_INVALID: + hdiAudioInputType = AUDIO_INPUT_DEFAULT_TYPE; + break; + case SOURCE_TYPE_MIC: + case SOURCE_TYPE_PLAYBACK_CAPTURE: + case SOURCE_TYPE_ULTRASONIC: + hdiAudioInputType = AUDIO_INPUT_MIC_TYPE; + break; + case SOURCE_TYPE_WAKEUP: + hdiAudioInputType = AUDIO_INPUT_SPEECH_WAKEUP_TYPE; + break; + case SOURCE_TYPE_VOICE_COMMUNICATION: + hdiAudioInputType = AUDIO_INPUT_VOICE_COMMUNICATION_TYPE; + break; + case SOURCE_TYPE_VOICE_RECOGNITION: + hdiAudioInputType = AUDIO_INPUT_VOICE_RECOGNITION_TYPE; + break; + case SOURCE_TYPE_VOICE_CALL: + hdiAudioInputType = AUDIO_INPUT_VOICE_CALL_TYPE; + break; + default: + hdiAudioInputType = AUDIO_INPUT_MIC_TYPE; + break; + } + return hdiAudioInputType; +} + int32_t FastAudioCapturerSourceInner::CreateCapture(const struct AudioPort &capturePort) { int32_t ret; struct AudioSampleAttributes param; // User needs to set InitAttrsCapture(param); + param.sourceType = static_cast(ConvertToHDIAudioInputType(attr_.sourceType)); param.type = attr_.audioStreamFlag == AUDIO_FLAG_VOIP_FAST ? AUDIO_MMAP_VOIP : AUDIO_MMAP_NOIRQ; // enable mmap! param.sampleRate = attr_.sampleRate; param.format = ConvertToHdiFormat(attr_.format); @@ -282,11 +314,15 @@ int32_t FastAudioCapturerSourceInner::CreateCapture(const struct AudioPort &capt case DEVICE_TYPE_USB_HEADSET: deviceDesc.pins = PIN_IN_USB_EXT; break; + case DEVICE_TYPE_BLUETOOTH_SCO: + deviceDesc.pins = PIN_IN_BLUETOOTH_SCO_HEADSET; + break; default: AUDIO_WARNING_LOG("Unsupported device type:%{public}d, use default mic instead", attr_.deviceType); deviceDesc.pins = PIN_IN_MIC; break; } + AUDIO_INFO_LOG("Capturer device type: %{public}d", attr_.deviceType); ret = audioAdapter_->CreateCapture(audioAdapter_, &deviceDesc, ¶m, &audioCapture_, &captureId_); CHECK_AND_RETURN_RET_LOG(audioCapture_ != nullptr && ret >= 0, @@ -345,9 +381,9 @@ int32_t FastAudioCapturerSourceInner::GetMmapHandlePosition(uint64_t &frames, in int32_t FastAudioCapturerSourceInner::PrepareMmapBuffer() { - uint32_t totalBifferInMs = 40; // 5 * (6 + 2 * (1)) = 40ms, the buffer size, not latency. + uint32_t totalBufferInMs = 40; // 5 * (6 + 2 * (1)) = 40ms, the buffer size, not latency. uint32_t frameSizeInByte = PcmFormatToBits(attr_.format) * attr_.channel / PCM_8_BIT; - uint32_t reqBufferFrameSize = totalBifferInMs * (attr_.sampleRate / 1000); + uint32_t reqBufferFrameSize = totalBufferInMs * (attr_.sampleRate / 1000); struct AudioMmapBufferDescriptor desc = {0}; int32_t ret = audioCapture_->ReqMmapBuffer(audioCapture_, reqBufferFrameSize, &desc); @@ -506,6 +542,9 @@ static int32_t SetInputPortPin(DeviceType inputDevice, AudioRouteNode &source) source.ext.device.type = PIN_IN_HS_MIC; source.ext.device.desc = const_cast("pin_in_hs_mic"); break; + case DEVICE_TYPE_BLUETOOTH_SCO: + source.ext.device.type = PIN_IN_BLUETOOTH_SCO_HEADSET; + source.ext.device.desc = (char *)"pin_in_bluetooth_sco_headset"; default: ret = ERR_NOT_SUPPORTED; break; diff --git a/frameworks/native/hdiadapter/source/remote_fast/remote_fast_audio_capturer_source.cpp b/frameworks/native/hdiadapter/source/remote_fast/remote_fast_audio_capturer_source.cpp index 51f73a15c3c919b898126a7687331fc3d22fa437..80843874f5a8f2466f73bf043eda7f9c5376560b 100644 --- a/frameworks/native/hdiadapter/source/remote_fast/remote_fast_audio_capturer_source.cpp +++ b/frameworks/native/hdiadapter/source/remote_fast/remote_fast_audio_capturer_source.cpp @@ -313,7 +313,9 @@ int32_t RemoteFastAudioCapturerSourceInner::InitAshmem(const struct AudioSampleA { CHECK_AND_RETURN_RET_LOG(audioCapture_ != nullptr, ERR_INVALID_HANDLE, "InitAshmem: Audio capture is null."); - int32_t reqSize = 1440; + int32_t totalBufferInMs = 40; // 5 * (6 + 2 * (1)) = 40ms, the buffer size, not latency. + int32_t reqSize = totalBufferInMs * (attr_.sampleRate / 1000); + struct AudioMmapBufferDescriptor desc; int32_t ret = audioCapture_->ReqMmapBuffer(reqSize, desc); CHECK_AND_RETURN_RET_LOG((ret == 0), ERR_OPERATION_FAILED, diff --git a/frameworks/native/toneplayer/src/toneplayer_impl.cpp b/frameworks/native/toneplayer/src/toneplayer_impl.cpp index 8059b107492d69bf8cd293e751695351d9aede1a..8ddca027cc1bfde47717bce044505e5e42bfca94 100644 --- a/frameworks/native/toneplayer/src/toneplayer_impl.cpp +++ b/frameworks/native/toneplayer/src/toneplayer_impl.cpp @@ -51,7 +51,7 @@ TonePlayerImpl::TonePlayerImpl(const std::string cachePath, const AudioRendererI // streamUsage::STREAM_USAGE_MEDIA; rendererOptions_.rendererInfo.streamUsage = rendereInfo.streamUsage; - rendererOptions_.rendererInfo.rendererFlags = 0; // use 0 for normal + rendererOptions_.rendererInfo.rendererFlags = AUDIO_FLAG_NORMAL; // use 0 for normal supportedTones_ = AudioPolicyManager::GetInstance().GetSupportedTones(); toneInfo_ = NULL; initialToneInfo_ = NULL; diff --git a/interfaces/inner_api/native/audiocapturer/include/audio_capturer.h b/interfaces/inner_api/native/audiocapturer/include/audio_capturer.h index d9943ca1eaeadd55e879ae614fb74cc8e60293a6..05215190aa8460ed4b5939cdc45b48ef83cacf33 100644 --- a/interfaces/inner_api/native/audiocapturer/include/audio_capturer.h +++ b/interfaces/inner_api/native/audiocapturer/include/audio_capturer.h @@ -479,7 +479,7 @@ public: * defined in {@link audio_errors.h} otherwise. * @since 9 */ - virtual int32_t SetCaptureMode(AudioCaptureMode captureMode) const = 0; + virtual int32_t SetCaptureMode(AudioCaptureMode captureMode) = 0; /** * @brief Obtains the capture mode. diff --git a/interfaces/inner_api/native/audiocommon/include/audio_info.h b/interfaces/inner_api/native/audiocommon/include/audio_info.h index d4dd6910ad8fa9805758563e19aecc5c0f604544..5d8fbbf48bfb383880c90bdb1bf2fad602d8105d 100644 --- a/interfaces/inner_api/native/audiocommon/include/audio_info.h +++ b/interfaces/inner_api/native/audiocommon/include/audio_info.h @@ -301,10 +301,11 @@ struct A2dpDeviceConfigInfo { struct AudioRendererInfo { ContentType contentType = CONTENT_TYPE_UNKNOWN; StreamUsage streamUsage = STREAM_USAGE_UNKNOWN; - int32_t rendererFlags = 0; + int32_t rendererFlags = AUDIO_FLAG_NORMAL; std::string sceneType = ""; bool spatializationEnabled = false; bool headTrackingEnabled = false; + int32_t originalFlag = AUDIO_FLAG_NORMAL; AudioPipeType pipeType = PIPE_TYPE_UNKNOWN; AudioSamplingRate samplingRate = SAMPLE_RATE_8000; uint8_t encodingType = 0; @@ -315,6 +316,7 @@ struct AudioRendererInfo { return parcel.WriteInt32(static_cast(contentType)) && parcel.WriteInt32(static_cast(streamUsage)) && parcel.WriteInt32(rendererFlags) + && parcel.WriteInt32(originalFlag) && parcel.WriteString(sceneType) && parcel.WriteBool(spatializationEnabled) && parcel.WriteBool(headTrackingEnabled) @@ -328,6 +330,7 @@ struct AudioRendererInfo { contentType = static_cast(parcel.ReadInt32()); streamUsage = static_cast(parcel.ReadInt32()); rendererFlags = parcel.ReadInt32(); + originalFlag = parcel.ReadInt32(); sceneType = parcel.ReadString(); spatializationEnabled = parcel.ReadBool(); headTrackingEnabled = parcel.ReadBool(); @@ -342,6 +345,7 @@ class AudioCapturerInfo { public: SourceType sourceType = SOURCE_TYPE_INVALID; int32_t capturerFlags = 0; + int32_t originalFlag = AUDIO_FLAG_NORMAL; AudioPipeType pipeType = PIPE_TYPE_UNKNOWN; AudioSamplingRate samplingRate = SAMPLE_RATE_8000; uint8_t encodingType = 0; @@ -358,18 +362,20 @@ public: ~AudioCapturerInfo()= default; bool Marshalling(Parcel &parcel) const { - return parcel.WriteInt32(static_cast(sourceType)) - && parcel.WriteInt32(capturerFlags) - && parcel.WriteInt32(static_cast(pipeType)) - && parcel.WriteInt32(static_cast(samplingRate)) - && parcel.WriteUint8(encodingType) - && parcel.WriteUint64(channelLayout) - && parcel.WriteString(sceneType); + return parcel.WriteInt32(static_cast(sourceType)) && + parcel.WriteInt32(capturerFlags) && + parcel.WriteInt32(originalFlag) && + parcel.WriteInt32(static_cast(pipeType)) && + parcel.WriteInt32(static_cast(samplingRate)) && + parcel.WriteUint8(encodingType) && + parcel.WriteUint64(channelLayout) && + parcel.WriteString(sceneType); } void Unmarshalling(Parcel &parcel) { sourceType = static_cast(parcel.ReadInt32()); capturerFlags = parcel.ReadInt32(); + originalFlag = parcel.ReadInt32(); pipeType = static_cast(parcel.ReadInt32()); samplingRate = static_cast(parcel.ReadInt32()); encodingType = parcel.ReadUint8(); @@ -740,6 +746,10 @@ public: && parcel.WriteInt32(clientPid) && parcel.WriteInt32(tokenId) && parcel.WriteInt32(channelCount) + && parcel.WriteInt32(static_cast(rendererInfo.contentType)) + && parcel.WriteInt32(static_cast(rendererInfo.streamUsage)) + && parcel.WriteInt32(rendererInfo.rendererFlags) + && parcel.WriteInt32(rendererInfo.originalFlag) && rendererInfo.Marshalling(parcel) && parcel.WriteInt32(static_cast(rendererState)) && outputDeviceInfo.Marshalling(parcel); @@ -753,6 +763,10 @@ public: && parcel.WriteInt32(clientPid) && parcel.WriteInt32(tokenId) && parcel.WriteInt32(channelCount) + && parcel.WriteInt32(static_cast(rendererInfo.contentType)) + && parcel.WriteInt32(static_cast(rendererInfo.streamUsage)) + && parcel.WriteInt32(rendererInfo.rendererFlags) + && parcel.WriteInt32(rendererInfo.originalFlag) && rendererInfo.Marshalling(parcel) && parcel.WriteInt32(hasSystemPermission ? static_cast(rendererState) : RENDERER_INVALID) @@ -768,6 +782,10 @@ public: tokenId = parcel.ReadInt32(); channelCount = parcel.ReadInt32(); + rendererInfo.contentType = static_cast(parcel.ReadInt32()); + rendererInfo.streamUsage = static_cast(parcel.ReadInt32()); + rendererInfo.rendererFlags = parcel.ReadInt32(); + rendererInfo.originalFlag = parcel.ReadInt32(); rendererInfo.Unmarshalling(parcel); rendererState = static_cast(parcel.ReadInt32()); diff --git a/interfaces/inner_api/native/audiomanager/include/audio_stream_manager.h b/interfaces/inner_api/native/audiomanager/include/audio_stream_manager.h index cf26d7918e4a434af7672aa23016cde668dc0bd8..85a6b770c4a7c50f34b8f828b59468f421ee3ae5 100644 --- a/interfaces/inner_api/native/audiomanager/include/audio_stream_manager.h +++ b/interfaces/inner_api/native/audiomanager/include/audio_stream_manager.h @@ -35,12 +35,14 @@ public: const std::vector> &audioRendererChangeInfos) = 0; }; -class OutputDeviceChangeWithInfoCallback { +class DeviceChangeWithInfoCallback { public: - virtual ~OutputDeviceChangeWithInfoCallback() = default; + virtual ~DeviceChangeWithInfoCallback() = default; - virtual void OnOutputDeviceChangeWithInfo( + virtual void OnDeviceChangeWithInfo( const uint32_t sessionId, const DeviceInfo &deviceInfo, const AudioStreamDeviceChangeReason reason) = 0; + + virtual void OnRecreateStreamEvent(const uint32_t sessionId, const int32_t streamFlag) = 0; }; class AudioCapturerStateChangeCallback { diff --git a/interfaces/inner_api/native/audiorenderer/include/audio_renderer.h b/interfaces/inner_api/native/audiorenderer/include/audio_renderer.h index e891e8232655594ed1c2ebc1856bf0aa624fd05f..2e5849227f2b876201e922b633a9ee91bc1fcb09 100644 --- a/interfaces/inner_api/native/audiorenderer/include/audio_renderer.h +++ b/interfaces/inner_api/native/audiorenderer/include/audio_renderer.h @@ -636,7 +636,7 @@ public: * defined in {@link audio_errors.h} otherwise. * @since 8 */ - virtual int32_t SetRenderMode(AudioRenderMode renderMode) const = 0; + virtual int32_t SetRenderMode(AudioRenderMode renderMode) = 0; /** * @brief Obtains the render mode. diff --git a/services/audio_policy/client/include/audio_policy_client_stub.h b/services/audio_policy/client/include/audio_policy_client_stub.h index 29825218a938aa796d01de2f8fdb29f394f47ec5..d0f3b67b7d07ceafb33089137b15dbf49235b42a 100644 --- a/services/audio_policy/client/include/audio_policy_client_stub.h +++ b/services/audio_policy/client/include/audio_policy_client_stub.h @@ -42,6 +42,8 @@ private: void HandleRendererStateChange(MessageParcel &data, MessageParcel &reply); void HandleCapturerStateChange(MessageParcel &data, MessageParcel &reply); void HandleRendererDeviceChange(MessageParcel &data, MessageParcel &reply); + void HandleRecreateRendererStreamEvent(MessageParcel &data, MessageParcel &reply); + void HandleRecreateCapturerStreamEvent(MessageParcel &data, MessageParcel &reply); void HandleHeadTrackingDeviceChange(MessageParcel &data, MessageParcel &reply); void HandleSpatializationEnabledChange(MessageParcel &data, MessageParcel &reply); void HandleHeadTrackingEnabledChange(MessageParcel &data, MessageParcel &reply); @@ -60,6 +62,8 @@ private: &AudioPolicyClientStub::HandleRendererStateChange, &AudioPolicyClientStub::HandleCapturerStateChange, &AudioPolicyClientStub::HandleRendererDeviceChange, + &AudioPolicyClientStub::HandleRecreateRendererStreamEvent, + &AudioPolicyClientStub::HandleRecreateCapturerStreamEvent, &AudioPolicyClientStub::HandleHeadTrackingDeviceChange, &AudioPolicyClientStub::HandleSpatializationEnabledChange, &AudioPolicyClientStub::HandleHeadTrackingEnabledChange, diff --git a/services/audio_policy/client/include/audio_policy_client_stub_impl.h b/services/audio_policy/client/include/audio_policy_client_stub_impl.h index 47084a0406b9697497fdb2faaa3409d9f19a312d..1c5fba159c229e2837bbf4c9dd95e2ddfa366cb3 100644 --- a/services/audio_policy/client/include/audio_policy_client_stub_impl.h +++ b/services/audio_policy/client/include/audio_policy_client_stub_impl.h @@ -50,9 +50,10 @@ public: int32_t RemoveRendererStateChangeCallback(); int32_t AddCapturerStateChangeCallback(const std::shared_ptr &cb); int32_t RemoveCapturerStateChangeCallback(); - int32_t AddOutputDeviceChangeWithInfoCallback( - const uint32_t sessionId, const std::shared_ptr &cb); - int32_t RemoveOutputDeviceChangeWithInfoCallback(const uint32_t sessionId); + int32_t AddDeviceChangeWithInfoCallback( + const uint32_t sessionId, const std::shared_ptr &cb); + int32_t RemoveDeviceChangeWithInfoCallback(const uint32_t sessionId); + int32_t AddHeadTrackingDataRequestedChangeCallback(const std::string &macAddress, const std::shared_ptr &cb); int32_t RemoveHeadTrackingDataRequestedChangeCallback(const std::string &macAddress); @@ -61,6 +62,8 @@ public: int32_t AddHeadTrackingEnabledChangeCallback(const std::shared_ptr &cb); int32_t RemoveHeadTrackingEnabledChangeCallback(); + void OnRecreateRendererStreamEvent(const uint32_t sessionId, const int32_t streamFlag) override; + void OnRecreateCapturerStreamEvent(const uint32_t sessionId, const int32_t streamFlag) override; void OnVolumeKeyEvent(VolumeEvent volumeEvent) override; void OnAudioFocusInfoChange(const std::list> &focusInfoList) override; void OnAudioFocusRequested(const AudioInterrupt &requestFocus) override; @@ -97,7 +100,7 @@ private: std::vector> headTrackingEnabledChangeCallbackList_; std::unordered_map> outputDeviceChangeWithInfoCallbackMap_; + std::shared_ptr> deviceChangeWithInfoCallbackMap_; std::unordered_map> headTrackingDataRequestedChangeCallbackMap_; @@ -111,7 +114,7 @@ private: std::mutex pInputDeviceChangeMutex_; std::mutex rendererStateChangeMutex_; std::mutex capturerStateChangeMutex_; - std::mutex outputDeviceChangeWithInfoCallbackMutex_; + std::mutex deviceChangeWithInfoCallbackMutex_; std::mutex headTrackingDataRequestedChangeMutex_; std::mutex spatializationEnabledChangeMutex_; std::mutex headTrackingEnabledChangeMutex_; diff --git a/services/audio_policy/client/src/audio_policy_client_stub.cpp b/services/audio_policy/client/src/audio_policy_client_stub.cpp index fe79caae2420664220e4173e8245188493c3af0f..741767d141c0044f00ef5e4aeeade038f95d3399 100644 --- a/services/audio_policy/client/src/audio_policy_client_stub.cpp +++ b/services/audio_policy/client/src/audio_policy_client_stub.cpp @@ -198,6 +198,20 @@ void AudioPolicyClientStub::HandleRendererDeviceChange(MessageParcel &data, Mess OnRendererDeviceChange(sessionId, deviceInfo, reason); } +void AudioPolicyClientStub::HandleRecreateRendererStreamEvent(MessageParcel &data, MessageParcel &reply) +{ + const uint32_t sessionId = data.ReadUint32(); + const uint32_t streamFlag = data.ReadUint32(); + OnRecreateRendererStreamEvent(sessionId, streamFlag); +} + +void AudioPolicyClientStub::HandleRecreateCapturerStreamEvent(MessageParcel &data, MessageParcel &reply) +{ + const uint32_t sessionId = data.ReadUint32(); + const uint32_t streamFlag = data.ReadUint32(); + OnRecreateCapturerStreamEvent(sessionId, streamFlag); +} + void AudioPolicyClientStub::HandleHeadTrackingDeviceChange(MessageParcel &data, MessageParcel &reply) { std::unordered_map changeInfo; diff --git a/services/audio_policy/client/src/audio_policy_client_stub_impl.cpp b/services/audio_policy/client/src/audio_policy_client_stub_impl.cpp index d9673c4fb9b46892f94ee2f01526ee6570ff24f9..23742439c40a28893bb743862f13e60280cd0930 100644 --- a/services/audio_policy/client/src/audio_policy_client_stub_impl.cpp +++ b/services/audio_policy/client/src/audio_policy_client_stub_impl.cpp @@ -280,34 +280,34 @@ int32_t AudioPolicyClientStubImpl::RemoveRendererStateChangeCallback() return SUCCESS; } -int32_t AudioPolicyClientStubImpl::AddOutputDeviceChangeWithInfoCallback( - const uint32_t sessionId, const std::shared_ptr &cb) +int32_t AudioPolicyClientStubImpl::AddDeviceChangeWithInfoCallback( + const uint32_t sessionId, const std::shared_ptr &cb) { - std::lock_guard lockCbMap(outputDeviceChangeWithInfoCallbackMutex_); - outputDeviceChangeWithInfoCallbackMap_[sessionId] = cb; + std::lock_guard lockCbMap(deviceChangeWithInfoCallbackMutex_); + deviceChangeWithInfoCallbackMap_[sessionId] = cb; return SUCCESS; } -int32_t AudioPolicyClientStubImpl::RemoveOutputDeviceChangeWithInfoCallback(const uint32_t sessionId) +int32_t AudioPolicyClientStubImpl::RemoveDeviceChangeWithInfoCallback(const uint32_t sessionId) { - std::lock_guard lockCbMap(outputDeviceChangeWithInfoCallbackMutex_); - outputDeviceChangeWithInfoCallbackMap_.erase(sessionId); + std::lock_guard lockCbMap(deviceChangeWithInfoCallbackMutex_); + deviceChangeWithInfoCallbackMap_.erase(sessionId); return SUCCESS; } void AudioPolicyClientStubImpl::OnRendererDeviceChange(const uint32_t sessionId, const DeviceInfo &deviceInfo, const AudioStreamDeviceChangeReason reason) { - std::shared_ptr callback = nullptr; + std::shared_ptr callback = nullptr; { - std::lock_guard lockCbMap(outputDeviceChangeWithInfoCallbackMutex_); - if (outputDeviceChangeWithInfoCallbackMap_.count(sessionId) == 0) { + std::lock_guard lockCbMap(deviceChangeWithInfoCallbackMutex_); + if (deviceChangeWithInfoCallbackMap_.count(sessionId) == 0) { return; } - callback = outputDeviceChangeWithInfoCallbackMap_.at(sessionId); + callback = deviceChangeWithInfoCallbackMap_.at(sessionId); } if (callback != nullptr) { - callback->OnOutputDeviceChangeWithInfo(sessionId, deviceInfo, reason); + callback->OnDeviceChangeWithInfo(sessionId, deviceInfo, reason); } } @@ -320,6 +320,40 @@ void AudioPolicyClientStubImpl::OnRendererStateChange( } } +void AudioPolicyClientStubImpl::OnRecreateRendererStreamEvent(const uint32_t sessionId, const int32_t streamFlag) +{ + AUDIO_INFO_LOG("Enter"); + std::shared_ptr callback = nullptr; + { + std::lock_guard lockCbMap(deviceChangeWithInfoCallbackMutex_); + if (deviceChangeWithInfoCallbackMap_.count(sessionId) == 0) { + AUDIO_ERR_LOG("No session id %{public}d", sessionId); + return; + } + callback = deviceChangeWithInfoCallbackMap_.at(sessionId); + } + if (callback != nullptr) { + callback->OnRecreateStreamEvent(sessionId, streamFlag); + } +} + +void AudioPolicyClientStubImpl::OnRecreateCapturerStreamEvent(const uint32_t sessionId, const int32_t streamFlag) +{ + AUDIO_INFO_LOG("Enter"); + std::shared_ptr callback = nullptr; + { + std::lock_guard lockCbMap(deviceChangeWithInfoCallbackMutex_); + if (deviceChangeWithInfoCallbackMap_.count(sessionId) == 0) { + AUDIO_ERR_LOG("No session id %{public}d", sessionId); + return; + } + callback = deviceChangeWithInfoCallbackMap_.at(sessionId); + } + if (callback != nullptr) { + callback->OnRecreateStreamEvent(sessionId, streamFlag); + } +} + int32_t AudioPolicyClientStubImpl::AddCapturerStateChangeCallback( const std::shared_ptr &cb) { diff --git a/services/audio_policy/client/src/audio_policy_manager.cpp b/services/audio_policy/client/src/audio_policy_manager.cpp index 06456486bbbb9f0c87f0ece3863f5ae844fc72dd..3d7e3cf3f41471c3a5cc0ee3e7bb8f66cc647d44 100644 --- a/services/audio_policy/client/src/audio_policy_manager.cpp +++ b/services/audio_policy/client/src/audio_policy_manager.cpp @@ -774,8 +774,8 @@ int32_t AudioPolicyManager::UnregisterAudioCapturerEventListener(const int32_t c return SUCCESS; } -int32_t AudioPolicyManager::RegisterOutputDeviceChangeWithInfoCallback( - const uint32_t sessionID, const std::shared_ptr &callback) +int32_t AudioPolicyManager::RegisterDeviceChangeWithInfoCallback( + const uint32_t sessionID, const std::shared_ptr &callback) { AUDIO_DEBUG_LOG("Entered %{public}s", __func__); const sptr gsp = GetAudioPolicyManagerProxy(); @@ -796,15 +796,15 @@ int32_t AudioPolicyManager::RegisterOutputDeviceChangeWithInfoCallback( } } - audioPolicyClientStubCB_->AddOutputDeviceChangeWithInfoCallback(sessionID, callback); + audioPolicyClientStubCB_->AddDeviceChangeWithInfoCallback(sessionID, callback); return SUCCESS; } -int32_t AudioPolicyManager::UnregisterOutputDeviceChangeWithInfoCallback(const uint32_t sessionID) +int32_t AudioPolicyManager::UnregisterDeviceChangeWithInfoCallback(const uint32_t sessionID) { AUDIO_DEBUG_LOG("Entered %{public}s", __func__); if (audioPolicyClientStubCB_ != nullptr) { - audioPolicyClientStubCB_->RemoveOutputDeviceChangeWithInfoCallback(sessionID); + audioPolicyClientStubCB_->RemoveDeviceChangeWithInfoCallback(sessionID); } return SUCCESS; } diff --git a/services/audio_policy/common/include/audio_policy_client.h b/services/audio_policy/common/include/audio_policy_client.h index 04351b0fcd9369a351d03c32f80239fa5812aa46..ad92a4ebb75fee3c36b32a72fc3fc7e5cafc8881 100644 --- a/services/audio_policy/common/include/audio_policy_client.h +++ b/services/audio_policy/common/include/audio_policy_client.h @@ -42,6 +42,8 @@ enum class AudioPolicyClientCode { ON_RENDERERSTATE_CHANGE, ON_CAPTURERSTATE_CHANGE, ON_RENDERER_DEVICE_CHANGE, + ON_RECREATE_RENDERER_STREAM_EVENT, + ON_RECREATE_CAPTURER_STREAM_EVENT, ON_HEAD_TRACKING_DEVICE_CHANGE, ON_SPATIALIZATION_ENABLED_CHANGE, ON_HEAD_TRACKING_ENABLED_CHANGE, @@ -64,6 +66,8 @@ public: std::vector> &audioCapturerChangeInfos) = 0; virtual void OnRendererDeviceChange(const uint32_t sessionId, const DeviceInfo &deviceInfo, const AudioStreamDeviceChangeReason reason) = 0; + virtual void OnRecreateRendererStreamEvent(const uint32_t sessionId, const int32_t streamFlag) = 0; + virtual void OnRecreateCapturerStreamEvent(const uint32_t sessionId, const int32_t streamFlag) = 0; virtual void OnHeadTrackingDeviceChange(const std::unordered_map &changeInfo) = 0; virtual void OnSpatializationEnabledChange(const bool &enabled) = 0; virtual void OnHeadTrackingEnabledChange(const bool &enabled) = 0; diff --git a/services/audio_policy/server/include/audio_policy_client_proxy.h b/services/audio_policy/server/include/audio_policy_client_proxy.h index ad305664e6b9b9ef7e57c32462e5ffde5e5a0257..46cebdbe81d2d8aab8ace3310cf24a335121a077 100644 --- a/services/audio_policy/server/include/audio_policy_client_proxy.h +++ b/services/audio_policy/server/include/audio_policy_client_proxy.h @@ -44,6 +44,8 @@ public: std::vector> &audioCapturerChangeInfos) override; void OnRendererDeviceChange(const uint32_t sessionId, const DeviceInfo &deviceInfo, const AudioStreamDeviceChangeReason reason) override; + void OnRecreateRendererStreamEvent(const uint32_t sessionId, const int32_t streamFlag) override; + void OnRecreateCapturerStreamEvent(const uint32_t sessionId, const int32_t streamFlag) override; void OnHeadTrackingDeviceChange(const std::unordered_map &changeInfo) override; void OnSpatializationEnabledChange(const bool &enabled) override; void OnHeadTrackingEnabledChange(const bool &enabled) override; diff --git a/services/audio_policy/server/include/audio_policy_server_handler.h b/services/audio_policy/server/include/audio_policy_server_handler.h index 51d318840dc0b48df266028f4760f1db3fd28503..db98e1d499ef4fd36fe856e9151b7fb6495a62e2 100644 --- a/services/audio_policy/server/include/audio_policy_server_handler.h +++ b/services/audio_policy/server/include/audio_policy_server_handler.h @@ -62,6 +62,8 @@ public: ON_CAPTURER_CREATE, ON_CAPTURER_REMOVED, ON_WAKEUP_CLOSE, + RECREATE_RENDERER_STREAM_EVENT, + RECREATE_CAPTURER_STREAM_EVENT, HEAD_TRACKING_DEVICE_CHANGE, SPATIALIZATION_ENABLED_CHANGE, HEAD_TRACKING_ENABLED_CHANGE, @@ -85,6 +87,7 @@ public: bool headTrackingEnabled; std::vector> audioRendererChangeInfos; std::vector> audioCapturerChangeInfos; + int32_t streamFlag; std::unordered_map headTrackingDeviceChangeInfo; }; @@ -149,6 +152,8 @@ public: uint64_t sessionId, bool isSync, int32_t &error); bool SendCapturerRemovedEvent(uint64_t sessionId, bool isSync); bool SendWakeupCloseEvent(bool isSync); + bool SendRecreateRendererStreamEvent(int32_t clientId, uint32_t sessionID, int32_t streamFlag); + bool SendRecreateCapturerStreamEvent(int32_t clientId, uint32_t sessionID, int32_t streamFlag); bool SendHeadTrackingDeviceChangeEvent(const std::unordered_map &changeInfo); void AddAudioDeviceRefinerCb(const sptr &callback); int32_t RemoveAudioDeviceRefinerCb(); @@ -180,7 +185,9 @@ private: void HandleRendererDeviceChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); void HandleCapturerCreateEvent(const AppExecFwk::InnerEvent::Pointer &event); void HandleCapturerRemovedEvent(const AppExecFwk::InnerEvent::Pointer &event); - void HandleWakeupCloaseEvent(const AppExecFwk::InnerEvent::Pointer &event); + void HandleWakeupCloseEvent(const AppExecFwk::InnerEvent::Pointer &event); + void HandleSendRecreateRendererStreamEvent(const AppExecFwk::InnerEvent::Pointer &event); + void HandleSendRecreateCapturerStreamEvent(const AppExecFwk::InnerEvent::Pointer &event); void HandleHeadTrackingDeviceChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); void HandleSpatializatonEnabledChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); void HandleHeadTrackingEnabledChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); diff --git a/services/audio_policy/server/include/service/audio_policy_service.h b/services/audio_policy/server/include/service/audio_policy_service.h index 9d3eb34091b9f0fcd2abe7d143a16a1a2cfa4f56..1f881fbd592dbbd93a8bb5c41846a4ac9a10540b 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -182,6 +182,8 @@ public: void GetGlobalConfigs(GlobalConfigs &globalConfigs); + bool GetVoipConfig(); + // Audio Policy Parser callbacks void OnAudioPolicyXmlParsingCompleted(const std::unordered_map adapterInfoMap); @@ -194,6 +196,8 @@ public: void OnGlobalConfigsParsed(GlobalConfigs &globalConfigs); + void OnVoipConfigParsed(bool enableFastVoip); + void OnUpdateRouteSupport(bool isSupported); int32_t GetDeviceNameFromDataShareHelper(std::string &deviceName); @@ -790,6 +794,29 @@ private: int32_t ShowDialog(); + int32_t GetVoipPlaybackDeviceInfo(const AudioProcessConfig &config, DeviceInfo &deviceInfo); + + int32_t GetVoipRecordDeviceInfo(const AudioProcessConfig &config, DeviceInfo &deviceInfo); + + int32_t GetVoipDeviceInfo(const AudioProcessConfig &config, DeviceInfo &deviceInfo, int32_t type, + std::vector> &preferredDeviceList); + + int32_t GetPreferredOutputStreamTypeInner(StreamUsage streamUsage, DeviceType deviceType, int32_t flags); + + int32_t GetPreferredInputStreamTypeInner(SourceType sourceType, DeviceType deviceType, int32_t flags); + + bool NotifyRecreateRendererStream(bool isUpdateActiveDevice, + const std::unique_ptr &rendererChangeInfo); + + void TriggerRecreateRendererStreamCallback(int32_t callerPid, int32_t sessionId, int32_t streamFlag); + + bool NotifyRecreateCapturerStream(bool isUpdateActiveDevice, + const std::unique_ptr &capturerChangeInfo); + + void TriggerRecreateCapturerStreamCallback(int32_t callerPid, int32_t sessionId, int32_t streamFlag); + + bool HasLowLatencyCapability(DeviceType deviceType, bool isRemote); + int32_t HandleAbsBluetoothVolume(const std::string &macAddress, const int32_t volumeLevel); DeviceUsage GetDeviceUsage(const AudioDeviceDescriptor &desc); @@ -826,6 +853,7 @@ private: uint64_t audioLatencyInMsec_ = 50; uint32_t sinkLatencyInMsec_ {0}; bool isOffloadAvailable_ = false; + bool enableFastVoip_ = false; std::unordered_map spatialDeviceMap_; diff --git a/services/audio_policy/server/include/service/interface/iport_observer.h b/services/audio_policy/server/include/service/interface/iport_observer.h index e4c901f11485a08922fc17b8151d9403ecc348d6..94a5a78992a77e9617e94daed612bb88bc843b93 100644 --- a/services/audio_policy/server/include/service/interface/iport_observer.h +++ b/services/audio_policy/server/include/service/interface/iport_observer.h @@ -32,6 +32,7 @@ public: virtual void OnVolumeGroupParsed(std::unordered_map& volumeGroupData) = 0; virtual void OnInterruptGroupParsed(std::unordered_map& interruptGroupData) = 0; virtual void OnGlobalConfigsParsed(GlobalConfigs &globalConfig) = 0; + virtual void OnVoipConfigParsed(bool enableFastVoip) = 0; }; } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_policy/server/src/audio_policy_client_proxy.cpp b/services/audio_policy/server/src/audio_policy_client_proxy.cpp index 9d74041da644fb7626363e8e8408bec6072638a0..9acd4ccba1f48b1812dca2ae380ca65ea70d4f9b 100644 --- a/services/audio_policy/server/src/audio_policy_client_proxy.cpp +++ b/services/audio_policy/server/src/audio_policy_client_proxy.cpp @@ -304,6 +304,48 @@ void AudioPolicyClientProxy::OnRendererDeviceChange(const uint32_t sessionId, reply.ReadInt32(); } +void AudioPolicyClientProxy::OnRecreateRendererStreamEvent(const uint32_t sessionId, const int32_t streamFlag) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_ASYNC); + if (!data.WriteInterfaceToken(GetDescriptor())) { + AUDIO_ERR_LOG("WriteInterfaceToken failed"); + return; + } + + data.WriteInt32(static_cast(AudioPolicyClientCode::ON_RECREATE_RENDERER_STREAM_EVENT)); + + data.WriteUint32(sessionId); + data.WriteInt32(streamFlag); + int error = Remote()->SendRequest(static_cast(UPDATE_CALLBACK_CLIENT), data, reply, option); + if (error != 0) { + AUDIO_ERR_LOG("Error while sending recreate stream event: %{public}d", error); + } + reply.ReadInt32(); +} + +void AudioPolicyClientProxy::OnRecreateCapturerStreamEvent(const uint32_t sessionId, const int32_t streamFlag) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_ASYNC); + if (!data.WriteInterfaceToken(GetDescriptor())) { + AUDIO_ERR_LOG("WriteInterfaceToken failed"); + return; + } + + data.WriteInt32(static_cast(AudioPolicyClientCode::ON_RECREATE_CAPTURER_STREAM_EVENT)); + + data.WriteUint32(sessionId); + data.WriteInt32(streamFlag); + int error = Remote()->SendRequest(static_cast(UPDATE_CALLBACK_CLIENT), data, reply, option); + if (error != 0) { + AUDIO_ERR_LOG("Error while sending recreate stream event: %{public}d", error); + } + reply.ReadInt32(); +} + void AudioPolicyClientProxy::OnHeadTrackingDeviceChange(const std::unordered_map &changeInfo) { MessageParcel data; diff --git a/services/audio_policy/server/src/audio_policy_server_handler.cpp b/services/audio_policy/server/src/audio_policy_server_handler.cpp index 32f61b9e3cce391f6e04b8258b02049af708dafa..c123ee1029bad481cb301114aaf38077a456495b 100644 --- a/services/audio_policy/server/src/audio_policy_server_handler.cpp +++ b/services/audio_policy/server/src/audio_policy_server_handler.cpp @@ -379,6 +379,26 @@ bool AudioPolicyServerHandler::SendWakeupCloseEvent(bool isSync) return ret; } +bool AudioPolicyServerHandler::SendRecreateRendererStreamEvent(int32_t clientId, uint32_t sessionID, int32_t streamFlag) +{ + std::shared_ptr eventContextObj = std::make_shared(); + CHECK_AND_RETURN_RET_LOG(eventContextObj != nullptr, false, "EventContextObj get nullptr"); + eventContextObj->clientId = clientId; + eventContextObj->sessionId = sessionID; + eventContextObj->streamFlag = streamFlag; + return SendSyncEvent(AppExecFwk::InnerEvent::Get(EventAudioServerCmd::RECREATE_RENDERER_STREAM_EVENT)); +} + +bool AudioPolicyServerHandler::SendRecreateCapturerStreamEvent(int32_t clientId, uint32_t sessionID, int32_t streamFlag) +{ + std::shared_ptr eventContextObj = std::make_shared(); + CHECK_AND_RETURN_RET_LOG(eventContextObj != nullptr, false, "EventContextObj get nullptr"); + eventContextObj->clientId = clientId; + eventContextObj->sessionId = sessionID; + eventContextObj->streamFlag = streamFlag; + return SendSyncEvent(AppExecFwk::InnerEvent::Get(EventAudioServerCmd::RECREATE_CAPTURER_STREAM_EVENT)); +} + bool AudioPolicyServerHandler::SendHeadTrackingDeviceChangeEvent( const std::unordered_map &changeInfo) { @@ -692,11 +712,39 @@ void AudioPolicyServerHandler::HandleCapturerRemovedEvent(const AppExecFwk::Inne AudioPolicyService::GetAudioPolicyService().OnCapturerSessionRemoved(sessionId); } -void AudioPolicyServerHandler::HandleWakeupCloaseEvent(const AppExecFwk::InnerEvent::Pointer &event) +void AudioPolicyServerHandler::HandleWakeupCloseEvent(const AppExecFwk::InnerEvent::Pointer &event) { AudioPolicyService::GetAudioPolicyService().CloseWakeUpAudioCapturer(); } +void AudioPolicyServerHandler::HandleSendRecreateRendererStreamEvent(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::shared_ptr eventContextObj = event->GetSharedObject(); + CHECK_AND_RETURN_LOG(eventContextObj != nullptr, "EventContextObj get nullptr"); + std::lock_guard lock(runnerMutex_); + if (audioPolicyClientProxyAPSCbsMap_.count(eventContextObj->clientId) == 0) { + AUDIO_ERR_LOG("No client id %{public}d", eventContextObj->clientId); + } + sptr rendererCb = audioPolicyClientProxyAPSCbsMap_.at(eventContextObj->clientId); + CHECK_AND_RETURN_LOG(rendererCb != nullptr, "Callback for id %{public}d is null", eventContextObj->clientId); + + rendererCb->OnRecreateRendererStreamEvent(eventContextObj->sessionId, eventContextObj->streamFlag); +} + +void AudioPolicyServerHandler::HandleSendRecreateCapturerStreamEvent(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::shared_ptr eventContextObj = event->GetSharedObject(); + CHECK_AND_RETURN_LOG(eventContextObj != nullptr, "EventContextObj get nullptr"); + std::lock_guard lock(runnerMutex_); + if (audioPolicyClientProxyAPSCbsMap_.count(eventContextObj->clientId) == 0) { + AUDIO_ERR_LOG("No client id %{public}d", eventContextObj->clientId); + } + sptr capturerCb = audioPolicyClientProxyAPSCbsMap_.at(eventContextObj->clientId); + CHECK_AND_RETURN_LOG(capturerCb != nullptr, "Callback for id %{public}d is null", eventContextObj->clientId); + + capturerCb->OnRecreateCapturerStreamEvent(eventContextObj->sessionId, eventContextObj->streamFlag); +} + void AudioPolicyServerHandler::HandleHeadTrackingDeviceChangeEvent(const AppExecFwk::InnerEvent::Pointer &event) { std::shared_ptr eventContextObj = event->GetSharedObject(); @@ -782,7 +830,13 @@ void AudioPolicyServerHandler::HandleServiceEvent(const uint32_t &eventId, HandleCapturerRemovedEvent(event); break; case EventAudioServerCmd::ON_WAKEUP_CLOSE: - HandleWakeupCloaseEvent(event); + HandleWakeupCloseEvent(event); + break; + case EventAudioServerCmd::RECREATE_RENDERER_STREAM_EVENT: + HandleSendRecreateRendererStreamEvent(event); + break; + case EventAudioServerCmd::RECREATE_CAPTURER_STREAM_EVENT: + HandleSendRecreateCapturerStreamEvent(event); break; case EventAudioServerCmd::DATABASE_UPDATE: HandleUpdateKvDataEvent(event); diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index 42bcae7bf9713056de9ec2a80d653b73bf557431..13d374e1abb99c82a52c60f32e435ec53d775237 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -2086,6 +2086,7 @@ void AudioPolicyService::FetchOutputDevice(vector &rendererChangeInfo) +{ + AUDIO_INFO_LOG("Is update active device: %{public}d, current rendererFlag: %{public}d, origianl flag: %{public}d", + isUpdateActiveDevice, rendererChangeInfo->rendererInfo.rendererFlags, + rendererChangeInfo->rendererInfo.originalFlag); + CHECK_AND_RETURN_RET_LOG(isUpdateActiveDevice, false, "isUpdateActiveDevice is false"); + CHECK_AND_RETURN_RET_LOG(rendererChangeInfo->rendererInfo.originalFlag != AUDIO_FLAG_NORMAL, false, + "original flag is normal"); + // different hal + if ((strcmp(GetSinkPortName(rendererChangeInfo->outputDeviceInfo.deviceType).c_str(), + GetSinkPortName(currentActiveDevice_.deviceType_).c_str())) || + ((rendererChangeInfo->outputDeviceInfo.networkId == LOCAL_NETWORK_ID) ^ + (currentActiveDevice_.networkId_ == LOCAL_NETWORK_ID))) { + int32_t streamClass = GetPreferredOutputStreamTypeInner(rendererChangeInfo->rendererInfo.streamUsage, + currentActiveDevice_.deviceType_, rendererChangeInfo->rendererInfo.originalFlag); + TriggerRecreateRendererStreamCallback(rendererChangeInfo->callerPid, + rendererChangeInfo->sessionId, streamClass); + return true; + } + return false; +} + +void AudioPolicyService::TriggerRecreateRendererStreamCallback(int32_t callerPid, int32_t sessionId, int32_t streamFlag) +{ + Trace trace("AudioPolicyService::TriggerRecreateRendererStreamCallback"); + AUDIO_INFO_LOG("Trigger recreate renderer stream, pid: %{public}d, sessionId: %{public}d, flag: %{public}d", + callerPid, sessionId, streamFlag); + if (audioPolicyServerHandler_ != nullptr) { + audioPolicyServerHandler_->SendRecreateRendererStreamEvent(callerPid, sessionId, streamFlag); + } else { + AUDIO_WARNING_LOG("No audio policy server handler"); + } +} + void AudioPolicyService::FetchStreamForA2dpOffload(vector> &rendererChangeInfos) { AUDIO_INFO_LOG("start for %{public}zu stream", rendererChangeInfos.size()); @@ -2120,8 +2156,7 @@ void AudioPolicyService::FetchStreamForA2dpOffload(vectormacAddress_).c_str()); - if (rendererChangeInfo->rendererInfo.rendererFlags == AUDIO_FLAG_MMAP && - a2dpOffloadFlag_ == A2DP_OFFLOAD) { + if (rendererChangeInfo->rendererInfo.rendererFlags == AUDIO_FLAG_MMAP) { const sptr gsp = GetAudioServerProxy(); CHECK_AND_RETURN_LOG(gsp != nullptr, "Service proxy unavailable"); gsp->ResetAudioEndpoint(); @@ -2217,6 +2252,9 @@ void AudioPolicyService::FetchInputDevice(vectorsessionId, desc->deviceType_); @@ -2243,6 +2281,41 @@ void AudioPolicyService::WriteInputRouteChangeEvent(unique_ptr &capturerChangeInfo) +{ + AUDIO_INFO_LOG("Is update active device: %{public}d, current capturerFlag: %{public}d, origianl flag: %{public}d", + isUpdateActiveDevice, capturerChangeInfo->capturerInfo.capturerFlags, + capturerChangeInfo->capturerInfo.originalFlag); + CHECK_AND_RETURN_RET_LOG(isUpdateActiveDevice, false, "isUpdateActiveDevice is false"); + CHECK_AND_RETURN_RET_LOG(capturerChangeInfo->capturerInfo.originalFlag == AUDIO_FLAG_MMAP, false, + "original flag is false"); + // different hal + if ((strcmp(GetSinkPortName(capturerChangeInfo->inputDeviceInfo.deviceType).c_str(), + GetSinkPortName(currentActiveDevice_.deviceType_).c_str())) || + ((capturerChangeInfo->inputDeviceInfo.networkId == LOCAL_NETWORK_ID) ^ + (currentActiveDevice_.networkId_ == LOCAL_NETWORK_ID))) { + int32_t streamClass = GetPreferredInputStreamTypeInner(capturerChangeInfo->capturerInfo.sourceType, + currentActiveDevice_.deviceType_, capturerChangeInfo->capturerInfo.originalFlag); + TriggerRecreateCapturerStreamCallback(capturerChangeInfo->callerPid, + capturerChangeInfo->sessionId, streamClass); + return true; + } + return false; +} + +void AudioPolicyService::TriggerRecreateCapturerStreamCallback(int32_t callerPid, int32_t sessionId, int32_t streamFlag) +{ + Trace trace("AudioPolicyService::TriggerRecreateCapturerStreamCallback"); + AUDIO_INFO_LOG("Trigger recreate capturer stream, pid: %{public}d, sessionId: %{public}d, flag: %{public}d", + callerPid, sessionId, streamFlag); + if (audioPolicyServerHandler_ != nullptr) { + audioPolicyServerHandler_->SendRecreateCapturerStreamEvent(callerPid, sessionId, streamFlag); + } else { + AUDIO_WARNING_LOG("No audio policy server handler"); + } +} + void AudioPolicyService::FetchDevice(bool isOutputDevice, const AudioStreamDeviceChangeReason reason) { Trace trace("AudioPolicyService::FetchDevice reason:" + std::to_string(static_cast(reason))); @@ -4058,6 +4131,11 @@ void AudioPolicyService::OnGlobalConfigsParsed(GlobalConfigs &globalConfigs) globalConfigs_ = globalConfigs; } +void AudioPolicyService::OnVoipConfigParsed(bool enableFastVoip) +{ + enableFastVoip_ = enableFastVoip; +} + void AudioPolicyService::GetAudioAdapterInfos(std::unordered_map &adapterInfoMap) { adapterInfoMap = adapterInfoMap_; @@ -4083,6 +4161,11 @@ void AudioPolicyService::GetGlobalConfigs(GlobalConfigs &globalConfigs) globalConfigs = globalConfigs_; } +bool AudioPolicyService::GetVoipConfig() +{ + return enableFastVoip_; +} + void AudioPolicyService::AddAudioPolicyClientProxyMap(int32_t clientPid, const sptr& cb) { if (audioPolicyServerHandler_ != nullptr) { @@ -4141,7 +4224,7 @@ static void UpdateCapturerInfoWhenNoPermission(const unique_ptrdeviceType_); + return GetPreferredOutputStreamTypeInner(rendererInfo.streamUsage, preferredDeviceList[0]->deviceType_, + rendererInfo.rendererFlags); +} + +int32_t AudioPolicyService::GetPreferredOutputStreamTypeInner(StreamUsage streamUsage, DeviceType deviceType, + int32_t flags) +{ + if (streamUsage == STREAM_USAGE_VOICE_COMMUNICATION && enableFastVoip_) { + return AUDIO_FLAG_VOIP_FAST; + } + std::string sinkPortName = GetSinkPortName(deviceType); if (adapterInfoMap_.find(static_cast(classStrToEnum[sinkPortName])) == adapterInfoMap_.end()) { return AUDIO_FLAG_INVALID; } @@ -5051,17 +5144,17 @@ int32_t AudioPolicyService::GetPreferredOutputStreamType(AudioRendererInfo &rend return AUDIO_FLAG_INVALID; } - AudioPipeDeviceInfo* deviceInfo = adapterInfo.GetDeviceInfoByDeviceType(preferredDeviceList[0]->deviceType_); + AudioPipeDeviceInfo* deviceInfo = adapterInfo.GetDeviceInfoByDeviceType(deviceType); CHECK_AND_RETURN_RET_LOG(deviceInfo != nullptr, AUDIO_FLAG_INVALID, "Device type is not supported"); for (auto &supportPipe : deviceInfo->supportPipes_) { PipeInfo* pipeInfo = adapterInfo.GetPipeByName(supportPipe); if (pipeInfo == nullptr) { continue; } - if (rendererInfo.rendererFlags == AUDIO_FLAG_MMAP && pipeInfo->audioFlag_ == AUDIO_FLAG_MMAP) { + if (flags == AUDIO_FLAG_MMAP && pipeInfo->audioFlag_ == AUDIO_FLAG_MMAP) { return AUDIO_FLAG_MMAP; } - if (rendererInfo.rendererFlags == AUDIO_FLAG_VOIP_FAST && pipeInfo->audioUsage_ == AUDIO_USAGE_VOIP && + if (flags == AUDIO_FLAG_VOIP_FAST && pipeInfo->audioUsage_ == AUDIO_USAGE_VOIP && pipeInfo->audioFlag_ == AUDIO_FLAG_MMAP) { return AUDIO_FLAG_VOIP_FAST; } @@ -5076,7 +5169,17 @@ int32_t AudioPolicyService::GetPreferredInputStreamType(AudioCapturerInfo &captu if (preferredDeviceList.size() == 0) { return AUDIO_FLAG_INVALID; } - std::string sourcePortName = GetSourcePortName(preferredDeviceList[0]->deviceType_); + return GetPreferredInputStreamTypeInner(capturerInfo.sourceType, preferredDeviceList[0]->deviceType_, + capturerInfo.originalFlag); +} + +int32_t AudioPolicyService::GetPreferredInputStreamTypeInner(SourceType sourceType, DeviceType deviceType, + int32_t flags) +{ + if (sourceType == SOURCE_TYPE_VOICE_COMMUNICATION && enableFastVoip_) { + return AUDIO_FLAG_VOIP_FAST; + } + std::string sourcePortName = GetSourcePortName(deviceType); if (adapterInfoMap_.find(static_cast(classStrToEnum[sourcePortName])) == adapterInfoMap_.end()) { return AUDIO_FLAG_INVALID; } @@ -5088,17 +5191,17 @@ int32_t AudioPolicyService::GetPreferredInputStreamType(AudioCapturerInfo &captu AUDIO_ERR_LOG("Invalid adapter"); return AUDIO_FLAG_INVALID; } - AudioPipeDeviceInfo* deviceInfo = adapterInfo.GetDeviceInfoByDeviceType(preferredDeviceList[0]->deviceType_); + AudioPipeDeviceInfo* deviceInfo = adapterInfo.GetDeviceInfoByDeviceType(deviceType); CHECK_AND_RETURN_RET_LOG(deviceInfo != nullptr, AUDIO_FLAG_INVALID, "Device type is not supported"); for (auto &supportPipe : deviceInfo->supportPipes_) { PipeInfo* pipeInfo = adapterInfo.GetPipeByName(supportPipe); if (pipeInfo == nullptr) { continue; } - if (capturerInfo.capturerFlags == AUDIO_FLAG_MMAP && pipeInfo->audioFlag_ == AUDIO_FLAG_MMAP) { + if (flags == AUDIO_FLAG_MMAP && pipeInfo->audioFlag_ == AUDIO_FLAG_MMAP) { return AUDIO_FLAG_MMAP; } - if (capturerInfo.capturerFlags == AUDIO_FLAG_VOIP_FAST && pipeInfo->audioUsage_ == AUDIO_USAGE_VOIP && + if (flags == AUDIO_FLAG_VOIP_FAST && pipeInfo->audioUsage_ == AUDIO_USAGE_VOIP && pipeInfo->audioFlag_ == AUDIO_FLAG_MMAP) { return AUDIO_FLAG_VOIP_FAST; } @@ -5222,22 +5325,28 @@ void AudioPolicyService::RegiestPolicy() int32_t AudioPolicyService::GetProcessDeviceInfo(const AudioProcessConfig &config, DeviceInfo &deviceInfo) { AUDIO_INFO_LOG("%{public}s", ProcessConfig::DumpProcessConfig(config).c_str()); - // todo - // check process in routerMap, return target device for it - // put the currentActiveDevice_ in deviceinfo, so it can create with current using device. - // genarate the unique deviceid? - - if (config.audioMode == AUDIO_MODE_RECORD) { - deviceInfo.deviceId = 1; - deviceInfo.networkId = LOCAL_NETWORK_ID; - deviceInfo.deviceRole = INPUT_DEVICE; - deviceInfo.deviceType = currentActiveInputDevice_.deviceType_; - } else { + if (config.audioMode == AUDIO_MODE_PLAYBACK) { + if (config.rendererInfo.streamUsage == STREAM_USAGE_VOICE_COMMUNICATION) { + return GetVoipPlaybackDeviceInfo(config, deviceInfo); + } deviceInfo.deviceId = 6; // 6 for test deviceInfo.networkId = LOCAL_NETWORK_ID; deviceInfo.deviceType = currentActiveDevice_.deviceType_; deviceInfo.deviceRole = OUTPUT_DEVICE; + } else { + if (config.capturerInfo.sourceType == SOURCE_TYPE_VOICE_COMMUNICATION) { + return GetVoipRecordDeviceInfo(config, deviceInfo); + } + deviceInfo.deviceId = 1; + deviceInfo.networkId = LOCAL_NETWORK_ID; + deviceInfo.deviceRole = INPUT_DEVICE; + deviceInfo.deviceType = currentActiveInputDevice_.deviceType_; } + + // todo + // check process in routerMap, return target device for it + // put the currentActiveDevice_ in deviceinfo, so it can create with current using device. + // genarate the unique deviceid? AudioStreamInfo targetStreamInfo = {SAMPLE_RATE_48000, ENCODING_PCM, SAMPLE_S16LE, STEREO}; // note: read from xml deviceInfo.audioStreamInfo = targetStreamInfo; deviceInfo.deviceName = "mmap_device"; @@ -5251,6 +5360,53 @@ int32_t AudioPolicyService::GetProcessDeviceInfo(const AudioProcessConfig &confi return SUCCESS; } +int32_t AudioPolicyService::GetVoipPlaybackDeviceInfo(const AudioProcessConfig &config, DeviceInfo &deviceInfo) +{ + AudioRendererInfo rendererInfo = config.rendererInfo; + std::vector> preferredDeviceList = GetPreferredOutputDeviceDescriptors(rendererInfo); + int32_t type = GetPreferredOutputStreamTypeInner(rendererInfo.streamUsage, preferredDeviceList[0]->deviceType_, + rendererInfo.originalFlag); + deviceInfo.deviceRole = OUTPUT_DEVICE; + return GetVoipDeviceInfo(config, deviceInfo, type, preferredDeviceList); +} + +int32_t AudioPolicyService::GetVoipRecordDeviceInfo(const AudioProcessConfig &config, DeviceInfo &deviceInfo) +{ + AudioCapturerInfo capturerInfo = config.capturerInfo; + std::vector> preferredDeviceList = GetPreferredInputDeviceDescriptors(capturerInfo); + int32_t type = GetPreferredInputStreamTypeInner(capturerInfo.sourceType, preferredDeviceList[0]->deviceType_, + capturerInfo.originalFlag); + deviceInfo.deviceRole = INPUT_DEVICE; + return GetVoipDeviceInfo(config, deviceInfo, type, preferredDeviceList); +} + +int32_t AudioPolicyService::GetVoipDeviceInfo(const AudioProcessConfig &config, DeviceInfo &deviceInfo, int32_t type, + std::vector> &preferredDeviceList) +{ + if (type == AUDIO_FLAG_NORMAL) { + AUDIO_WARNING_LOG("Current device %{public}d not support", type); + return ERROR; + } + deviceInfo.deviceId = preferredDeviceList[0]->deviceId_; + deviceInfo.networkId = preferredDeviceList[0]->networkId_; + deviceInfo.deviceType = preferredDeviceList[0]->deviceType_; + deviceInfo.deviceName = preferredDeviceList[0]->deviceName_; + if (config.streamInfo.samplingRate <= SAMPLE_RATE_16000) { + deviceInfo.audioStreamInfo = {SAMPLE_RATE_16000, ENCODING_PCM, SAMPLE_S16LE, STEREO}; + } else { + deviceInfo.audioStreamInfo = {SAMPLE_RATE_48000, ENCODING_PCM, SAMPLE_S16LE, STEREO}; + } + std::lock_guard lock(routerMapMutex_); + if (fastRouterMap_.count(config.appInfo.appUid) && + fastRouterMap_[config.appInfo.appUid].second == deviceInfo.deviceRole) { + deviceInfo.networkId = fastRouterMap_[config.appInfo.appUid].first; + AUDIO_INFO_LOG("use networkid in fastRouterMap_ :%{public}s ", deviceInfo.networkId.c_str()); + } + deviceInfo.a2dpOffloadFlag = a2dpOffloadFlag_; + deviceInfo.isLowLatencyDevice = true; + return SUCCESS; +} + int32_t AudioPolicyService::InitSharedVolume(std::shared_ptr &buffer) { AUDIO_INFO_LOG("InitSharedVolume start"); diff --git a/services/audio_policy/server/src/service/config/audio_policy_parser.cpp b/services/audio_policy/server/src/service/config/audio_policy_parser.cpp index 9660693b5b4f8eff218f25dd4aa69ac5342e26a1..b064005012163c73e57b32f8508de6b18fd7fc8e 100644 --- a/services/audio_policy/server/src/service/config/audio_policy_parser.cpp +++ b/services/audio_policy/server/src/service/config/audio_policy_parser.cpp @@ -470,6 +470,9 @@ void AudioPolicyParser::ParseConfigs(xmlNode &node, PipeInfo &pipeInfo) } configNode = configNode->next; } + if (pipeInfo.audioUsage_ == AUDIO_USAGE_VOIP && pipeInfo.audioFlag_ == AUDIO_FLAG_MMAP) { + portObserver_.OnVoipConfigParsed(true); + } pipeInfo.configInfos_ = configInfos; } diff --git a/services/audio_service/BUILD.gn b/services/audio_service/BUILD.gn index 5550c520f612c6b66ed1bc243424b8aa58bab765..4de0b29ef79e0ec66465aeb4c2332414ef66a0a2 100644 --- a/services/audio_service/BUILD.gn +++ b/services/audio_service/BUILD.gn @@ -358,6 +358,7 @@ ohos_shared_library("audio_service") { "../../frameworks/native/hdiadapter/sink:renderer_sink_adapter", "../../frameworks/native/hdiadapter/source:audio_capturer_source", "../../frameworks/native/hdiadapter/source:capturer_source_adapter", + "../../frameworks/native/hdiadapter/source:fast_audio_capturer_source", "../../frameworks/native/playbackcapturer:playback_capturer", ] diff --git a/services/audio_service/client/include/renderer_in_client_private.h b/services/audio_service/client/include/renderer_in_client_private.h index 72245fab5a4add51b67287ee67ba60cbecf98694..0da3369b204822775e556227f30ab85e70ce6608 100644 --- a/services/audio_service/client/include/renderer_in_client_private.h +++ b/services/audio_service/client/include/renderer_in_client_private.h @@ -196,6 +196,8 @@ public: int32_t RemoveRendererOrCapturerPolicyServiceDiedCB() override; bool RestoreAudioStream() override; + void GetStreamSwitchInfo(SwitchInfo &info); + bool GetOffloadEnable() override; bool GetSpatializationEnabled() override; bool GetHighResolutionEnabled() override; diff --git a/services/audio_service/client/src/audio_process_in_client.cpp b/services/audio_service/client/src/audio_process_in_client.cpp index 509becb7c6f6fe26a51a81087c277c596f4b668c..957206ce0130cc74dfb3f9bf0239797d1e94cb23 100644 --- a/services/audio_service/client/src/audio_process_in_client.cpp +++ b/services/audio_service/client/src/audio_process_in_client.cpp @@ -294,7 +294,10 @@ std::shared_ptr AudioProcessInClient::Create(const AudioPr sptr gasp = AudioProcessInClientInner::GetAudioServerProxy(); CHECK_AND_RETURN_RET_LOG(gasp != nullptr, nullptr, "Create failed, can not get service."); AudioProcessConfig resetConfig = config; - resetConfig.streamInfo = AudioProcessInClientInner::g_targetStreamInfo; + if (config.rendererInfo.streamUsage != STREAM_USAGE_VOICE_COMMUNICATION && + config.capturerInfo.sourceType != SOURCE_TYPE_VOICE_COMMUNICATION) { + resetConfig.streamInfo = AudioProcessInClientInner::g_targetStreamInfo; + } sptr ipcProxy = gasp->CreateAudioProcess(resetConfig); CHECK_AND_RETURN_RET_LOG(ipcProxy != nullptr, nullptr, "Create failed with null ipcProxy."); sptr iProcessProxy = iface_cast(ipcProxy); @@ -667,7 +670,13 @@ int32_t AudioProcessInClientInner::GetBufferDesc(BufferDesc &bufDesc) const bool AudioProcessInClient::CheckIfSupport(const AudioProcessConfig &config) { - if (config.streamInfo.encoding != ENCODING_PCM || config.streamInfo.samplingRate != SAMPLE_RATE_48000) { + if (config.rendererInfo.streamUsage != STREAM_USAGE_VOICE_COMMUNICATION && + config.capturerInfo.sourceType != SOURCE_TYPE_VOICE_COMMUNICATION && + config.streamInfo.samplingRate != SAMPLE_RATE_48000) { + return false; + } + + if (config.streamInfo.encoding != ENCODING_PCM) { return false; } diff --git a/services/audio_service/client/src/capturer_in_client.cpp b/services/audio_service/client/src/capturer_in_client.cpp index 0f9bca932611486d807aa3cdbb8042f4edbeeac8..5fde3070e6d9d6f70a9142b410b874ff2b1d76ee 100644 --- a/services/audio_service/client/src/capturer_in_client.cpp +++ b/services/audio_service/client/src/capturer_in_client.cpp @@ -236,6 +236,8 @@ private: int32_t InitSharedBuffer(); int32_t FlushRingCache(); + void GetStreamSwitchInfo(IAudioStream::SwitchInfo& info); + int32_t StateCmdTypeToParams(int64_t ¶ms, State state, StateChangeCmdType cmdType); int32_t ParamsToStateCmdType(int64_t params, State &state, StateChangeCmdType &cmdType); @@ -1815,7 +1817,31 @@ void CapturerInClientInner::SetStreamTrackerState(bool trackerRegisteredState) void CapturerInClientInner::GetSwitchInfo(IAudioStream::SwitchInfo& info) { - // in plan + info.params = streamParams_; + + info.rendererInfo = rendererInfo_; + info.capturerInfo = capturerInfo_; + info.eStreamType = eStreamType_; + info.state = state_; + info.sessionId = sessionId_; + info.streamTrackerRegistered = streamTrackerRegistered_; + GetStreamSwitchInfo(info); +} + +void CapturerInClientInner::GetStreamSwitchInfo(IAudioStream::SwitchInfo& info) +{ + info.cachePath = cachePath_; + info.overFlowCount = overflowCount_; + info.clientPid = clientPid_; + info.clientUid = clientUid_; + + info.frameMarkPosition = capturerMarkPosition_; + info.capturePositionCb = capturerPositionCallback_; + + info.framePeriodNumber = capturerPeriodSize_; + info.capturePeriodPositionCb = capturerPeriodPositionCallback_; + + info.capturerReadCallback = readCb_; } bool CapturerInClientInner::GetOffloadEnable() diff --git a/services/audio_service/client/src/fast_audio_stream.cpp b/services/audio_service/client/src/fast_audio_stream.cpp index 3fa32ce67ac2c266ae3facc165e59f6f22d1b407..7d6ae4016b7ac0a2b2eb307ea2a584015d6f070f 100644 --- a/services/audio_service/client/src/fast_audio_stream.cpp +++ b/services/audio_service/client/src/fast_audio_stream.cpp @@ -103,10 +103,12 @@ int32_t FastAudioStream::SetAudioStreamInfo(const AudioStreamParams info, config.rendererInfo.contentType = rendererInfo_.contentType; config.rendererInfo.streamUsage = rendererInfo_.streamUsage; config.rendererInfo.rendererFlags = STREAM_FLAG_FAST; + config.rendererInfo.originalFlag = rendererInfo_.originalFlag; } else if (eMode_ == AUDIO_MODE_RECORD) { AUDIO_DEBUG_LOG("FastAudioStream: Initialize recording"); config.capturerInfo.sourceType = capturerInfo_.sourceType; config.capturerInfo.capturerFlags = STREAM_FLAG_FAST; + config.capturerInfo.originalFlag = capturerInfo_.originalFlag; } else { return ERR_INVALID_OPERATION; } diff --git a/services/audio_service/client/src/i_audio_stream.cpp b/services/audio_service/client/src/i_audio_stream.cpp index 0c94a80c1415ce98c0ed5b05ec9ae3063b11de2b..5e9eb7f970ae7d73f727f9377004539c85df9475 100644 --- a/services/audio_service/client/src/i_audio_stream.cpp +++ b/services/audio_service/client/src/i_audio_stream.cpp @@ -204,7 +204,7 @@ std::shared_ptr IAudioStream::GetPlaybackStream(StreamClass stream AudioStreamType eStreamType, int32_t appUid) { Trace trace("IAudioStream::GetPlaybackStream"); - if (streamClass == FAST_STREAM) { + if (streamClass == FAST_STREAM || streamClass == VOIP_STREAM) { (void)params; AUDIO_INFO_LOG("Create fast playback stream"); return std::make_shared(eStreamType, AUDIO_MODE_PLAYBACK, appUid); @@ -221,7 +221,7 @@ std::shared_ptr IAudioStream::GetRecordStream(StreamClass streamCl AudioStreamType eStreamType, int32_t appUid) { Trace trace("IAudioStream::GetRecordStream"); - if (streamClass == FAST_STREAM) { + if (streamClass == FAST_STREAM || streamClass == VOIP_STREAM) { (void)params; AUDIO_INFO_LOG("Create fast record stream"); return std::make_shared(eStreamType, AUDIO_MODE_RECORD, appUid); diff --git a/services/audio_service/client/src/renderer_in_client.cpp b/services/audio_service/client/src/renderer_in_client.cpp index f4cbbdfd07c3c1cdcb511b51da8ae80b237f835c..2af8fbf73910329b869a577adb4279b491ef3dda 100644 --- a/services/audio_service/client/src/renderer_in_client.cpp +++ b/services/audio_service/client/src/renderer_in_client.cpp @@ -1977,7 +1977,35 @@ void RendererInClientInner::SetStreamTrackerState(bool trackerRegisteredState) void RendererInClientInner::GetSwitchInfo(IAudioStream::SwitchInfo& info) { - // in plan + info.params = streamParams_; + + info.rendererInfo = rendererInfo_; + info.capturerInfo = capturerInfo_; + info.eStreamType = eStreamType_; + info.renderMode = renderMode_; + info.state = state_; + info.sessionId = sessionId_; + info.streamTrackerRegistered = streamTrackerRegistered_; + GetStreamSwitchInfo(info); +} + +void RendererInClientInner::GetStreamSwitchInfo(IAudioStream::SwitchInfo& info) +{ + info.cachePath = cachePath_; + info.underFlowCount = underrunCount_; + info.effectMode = effectMode_; + info.renderRate = rendererRate_; + info.clientPid = clientPid_; + info.clientUid = clientUid_; + info.volume = clientVolume_; + + info.frameMarkPosition = rendererMarkPosition_; + info.renderPositionCb = rendererPositionCallback_; + + info.framePeriodNumber = rendererPeriodSize_; + info.renderPeriodPositionCb = rendererPeriodPositionCallback_; + + info.rendererWriteCallback = writeCb_; } IAudioStream::StreamClass RendererInClientInner::GetStreamClass() diff --git a/services/audio_service/common/src/audio_process_config.cpp b/services/audio_service/common/src/audio_process_config.cpp index ce6843570248da9ac26a5e1fdefb6c9aa7501c04..fe4c55323aac87b88ca1b4ef35761860d9cee58a 100644 --- a/services/audio_service/common/src/audio_process_config.cpp +++ b/services/audio_service/common/src/audio_process_config.cpp @@ -210,6 +210,7 @@ int32_t ProcessConfig::WriteConfigToParcel(const AudioProcessConfig &config, Mes parcel.WriteInt32(config.rendererInfo.contentType); parcel.WriteInt32(config.rendererInfo.streamUsage); parcel.WriteInt32(config.rendererInfo.rendererFlags); + parcel.WriteInt32(config.rendererInfo.originalFlag); parcel.WriteString(config.rendererInfo.sceneType); parcel.WriteBool(config.rendererInfo.spatializationEnabled); parcel.WriteBool(config.rendererInfo.headTrackingEnabled); @@ -220,6 +221,7 @@ int32_t ProcessConfig::WriteConfigToParcel(const AudioProcessConfig &config, Mes // AudioCapturerInfo parcel.WriteInt32(config.capturerInfo.sourceType); parcel.WriteInt32(config.capturerInfo.capturerFlags); + parcel.WriteInt32(config.capturerInfo.originalFlag); // streamType parcel.WriteInt32(config.streamType); @@ -255,6 +257,7 @@ int32_t ProcessConfig::ReadConfigFromParcel(AudioProcessConfig &config, MessageP config.rendererInfo.contentType = static_cast(parcel.ReadInt32()); config.rendererInfo.streamUsage = static_cast(parcel.ReadInt32()); config.rendererInfo.rendererFlags = parcel.ReadInt32(); + config.rendererInfo.originalFlag = parcel.ReadInt32(); config.rendererInfo.sceneType = parcel.ReadString(); config.rendererInfo.spatializationEnabled = parcel.ReadBool(); config.rendererInfo.headTrackingEnabled = parcel.ReadBool(); @@ -265,6 +268,7 @@ int32_t ProcessConfig::ReadConfigFromParcel(AudioProcessConfig &config, MessageP // AudioCapturerInfo config.capturerInfo.sourceType = static_cast(parcel.ReadInt32()); config.capturerInfo.capturerFlags = parcel.ReadInt32(); + config.capturerInfo.originalFlag = parcel.ReadInt32(); // streamType config.streamType = static_cast(parcel.ReadInt32()); diff --git a/services/audio_service/server/include/audio_service.h b/services/audio_service/server/include/audio_service.h index 9583e2c94f05bbb54ba40395c5e0c8ae05c54d9e..521d9547434b7de800551b2201148ad107f0bfed 100644 --- a/services/audio_service/server/include/audio_service.h +++ b/services/audio_service/server/include/audio_service.h @@ -79,6 +79,7 @@ private: bool ShouldBeInnerCap(const AudioProcessConfig &rendererConfig); int32_t OnInitInnerCapList(); // for first InnerCap filter take effect. int32_t OnUpdateInnerCapList(); // for some InnerCap filter has already take effect. + bool IsEndpointTypeVoip(const AudioProcessConfig &config); private: std::mutex processListMutex_; diff --git a/services/audio_service/server/src/audio_endpoint.cpp b/services/audio_service/server/src/audio_endpoint.cpp index 1a00c443f82be0e6a2a0d490ebed3a8e9a912d14..69e7686ef37a968ae545b6e11322b39ce4bada94 100644 --- a/services/audio_service/server/src/audio_endpoint.cpp +++ b/services/audio_service/server/src/audio_endpoint.cpp @@ -46,6 +46,8 @@ namespace OHOS { namespace AudioStandard { namespace { static constexpr int32_t VOLUME_SHIFT_NUMBER = 16; // 1 >> 16 = 65536, max volume + static constexpr int64_t RECORD_DELAY_TIME = 4000000; // 4ms + static constexpr int64_t RECORD_VOIP_DELAY_TIME = 10000000; // 10ms static constexpr int64_t MAX_SPAN_DURATION_IN_NANO = 100000000; // 100ms static constexpr int64_t DELAY_STOP_HDI_TIME = 10000000000; // 10s static constexpr int64_t DELAY_STOP_HDI_TIME_FOR_ZERO_VOLUME = 4000000000; // 4s @@ -1337,7 +1339,8 @@ void AudioEndpointInner::GetAllReadyProcessData(std::vector &au AudioStreamType streamType = processList_[i]->GetAudioStreamType(); AudioVolumeType volumeType = PolicyHandler::GetInstance().GetVolumeTypeFromStreamType(streamType); DeviceType deviceType = PolicyHandler::GetInstance().GetActiveOutPutDevice(); - if (deviceInfo_.networkId == LOCAL_NETWORK_ID && !isSupportAbsVolume_ && + if (deviceInfo_.networkId == LOCAL_NETWORK_ID && + (deviceInfo_.deviceType != DEVICE_TYPE_BLUETOOTH_A2DP || !isSupportAbsVolume_) && PolicyHandler::GetInstance().GetSharedVolume(volumeType, deviceType, vol)) { streamData.volumeStart = vol.isMute ? 0 : static_cast(curReadSpan->volumeStart * vol.volumeFloat); } else { @@ -1469,7 +1472,7 @@ bool AudioEndpointInner::RecordPrepareNextLoop(uint64_t curReadPos, int64_t &wak { uint64_t nextHandlePos = curReadPos + dstSpanSizeInframe_; int64_t nextHdiWriteTime = GetPredictNextWriteTime(nextHandlePos); - int64_t tempDelay = 4000000; // 4ms + int64_t tempDelay = endpointType_ == TYPE_VOIP_MMAP ? RECORD_VOIP_DELAY_TIME : RECORD_DELAY_TIME; wakeUpTime = nextHdiWriteTime + tempDelay; int32_t ret = dstAudioBuffer_->SetCurWriteFrame(nextHandlePos); diff --git a/services/audio_service/server/src/audio_server.cpp b/services/audio_service/server/src/audio_server.cpp index 10d194bb5b2467583ad8f99719a33e0ee8cb5ea4..063321f5bb83abe4f24a1cb082a598a1ca29fd93 100644 --- a/services/audio_service/server/src/audio_server.cpp +++ b/services/audio_service/server/src/audio_server.cpp @@ -32,6 +32,7 @@ #include "hisysevent.h" #include "audio_capturer_source.h" +#include "fast_audio_capturer_source.h" #include "audio_errors.h" #include "audio_log.h" #include "audio_asr.h" @@ -812,7 +813,7 @@ int32_t AudioServer::SetAudioScene(AudioScene audioScene, DeviceType activeOutpu int32_t AudioServer::SetIORoute(DeviceType type, DeviceFlag flag) { AUDIO_INFO_LOG("SetIORoute deviceType: %{public}d, flag: %{public}d", type, flag); - AudioCapturerSource *audioCapturerSourceInstance; + IAudioCapturerSource *audioCapturerSourceInstance; IAudioRendererSink *audioRendererSinkInstance; if (type == DEVICE_TYPE_USB_ARM_HEADSET) { audioCapturerSourceInstance = AudioCapturerSource::GetInstance("usb"); @@ -820,6 +821,9 @@ int32_t AudioServer::SetIORoute(DeviceType type, DeviceFlag flag) } else { audioCapturerSourceInstance = AudioCapturerSource::GetInstance("primary"); audioRendererSinkInstance = IAudioRendererSink::GetInstance("primary", ""); + if (!audioCapturerSourceInstance->IsInited()) { + audioCapturerSourceInstance = FastAudioCapturerSource::GetInstance(); + } } CHECK_AND_RETURN_RET_LOG(audioCapturerSourceInstance != nullptr && audioRendererSinkInstance != nullptr, ERR_INVALID_PARAM, "SetIORoute failed for null instance!"); diff --git a/services/audio_service/server/src/audio_service.cpp b/services/audio_service/server/src/audio_service.cpp index b8fb2a20fb656876b4475e347a9101ef47db40fa..2f489625341d24098fbd86e32c27e184256b7407 100644 --- a/services/audio_service/server/src/audio_service.cpp +++ b/services/audio_service/server/src/audio_service.cpp @@ -319,14 +319,24 @@ int32_t AudioService::OnCapturerFilterRemove(uint32_t sessionId) return SUCCESS; } +bool AudioService::IsEndpointTypeVoip(const AudioProcessConfig &config) +{ + if ((config.rendererInfo.streamUsage == STREAM_USAGE_VOICE_COMMUNICATION && + config.rendererInfo.rendererFlags == AUDIO_FLAG_VOIP_FAST) || + (config.capturerInfo.sourceType == SOURCE_TYPE_VOICE_COMMUNICATION && + config.capturerInfo.capturerFlags == AUDIO_FLAG_VOIP_FAST)) { + return true; + } + return false; +} + sptr AudioService::GetAudioProcess(const AudioProcessConfig &config) { Trace trace("AudioService::GetAudioProcess for " + std::to_string(config.appInfo.appPid)); AUDIO_INFO_LOG("GetAudioProcess dump %{public}s", ProcessConfig::DumpProcessConfig(config).c_str()); DeviceInfo deviceInfo = GetDeviceInfoForProcess(config); std::shared_ptr audioEndpoint = GetAudioEndpointForDevice(deviceInfo, config.streamType, - config.rendererInfo.streamUsage == STREAM_USAGE_VOICE_COMMUNICATION || - config.capturerInfo.sourceType == SOURCE_TYPE_VOICE_COMMUNICATION); + IsEndpointTypeVoip(config)); CHECK_AND_RETURN_RET_LOG(audioEndpoint != nullptr, nullptr, "no endpoint found for the process"); uint32_t totalSizeInframe = 0; @@ -370,8 +380,7 @@ void AudioService::ResetAudioEndpoint() DeviceInfo deviceInfo = GetDeviceInfoForProcess(config); std::shared_ptr audioEndpoint = GetAudioEndpointForDevice(deviceInfo, config.streamType, - config.rendererInfo.streamUsage == STREAM_USAGE_VOICE_COMMUNICATION || - config.capturerInfo.sourceType == SOURCE_TYPE_VOICE_COMMUNICATION); + IsEndpointTypeVoip(config)); CHECK_AND_RETURN_LOG(audioEndpoint != nullptr, "Get new endpoint failed"); ret = LinkProcessToEndpoint((*paired).first, audioEndpoint); @@ -511,8 +520,10 @@ DeviceInfo AudioService::GetDeviceInfoForProcess(const AudioProcessConfig &confi std::shared_ptr AudioService::GetAudioEndpointForDevice(DeviceInfo &deviceInfo, AudioStreamType streamType, bool isVoipStream) { + int32_t endpointSeparateFlag = -1; + GetSysPara("persist.multimedia.audioflag.fast.disableseparate", endpointSeparateFlag); if (deviceInfo.deviceRole == INPUT_DEVICE || deviceInfo.networkId != LOCAL_NETWORK_ID || - deviceInfo.deviceRole == OUTPUT_DEVICE) { + deviceInfo.deviceRole == OUTPUT_DEVICE || endpointSeparateFlag == 1) { // Create shared stream. int32_t endpointFlag = AUDIO_FLAG_MMAP; if (isVoipStream) { diff --git a/test/fuzztest/audiopolicyanother_fuzzer/audio_policy_another_fuzzer.cpp b/test/fuzztest/audiopolicyanother_fuzzer/audio_policy_another_fuzzer.cpp index 1c1c9bffd4e6094a8642b000846f60c601d3a10f..3c7d48a127d66dbbee2812706ba73517ad4ce939 100644 --- a/test/fuzztest/audiopolicyanother_fuzzer/audio_policy_another_fuzzer.cpp +++ b/test/fuzztest/audiopolicyanother_fuzzer/audio_policy_another_fuzzer.cpp @@ -58,13 +58,16 @@ void AudioVolumeFuzzTest(const uint8_t *rawData, size_t size) std::string sceneType(reinterpret_cast(rawData), size - 1); bool spatializationEnabled = *reinterpret_cast(rawData); bool headTrackingEnabled = *reinterpret_cast(rawData); + int32_t originalFlag = *reinterpret_cast(rawData); + AudioRendererInfo rendererInfo = { contentType, streamUsage, rendererFlags, sceneType, spatializationEnabled, - headTrackingEnabled + headTrackingEnabled, + originalFlag }; AudioPolicyServerPtr->GetPreferredOutputStreamType(rendererInfo);