From e54ecbe96efc852361d6adffb12527aa31d93637 Mon Sep 17 00:00:00 2001 From: liyuhang Date: Sun, 8 Jun 2025 09:35:44 +0000 Subject: [PATCH 1/3] Create normal stream if creating fast stream fails Signed-off-by: liyuhang Change-Id: I3554e96dac234d3bf889f0e4e049b67d8440f092 --- .../include/audio_capturer_private.h | 1 + .../audiocapturer/src/audio_capturer.cpp | 32 ++++++++++++---- .../include/audio_renderer_private.h | 1 + .../audiorenderer/src/audio_renderer.cpp | 37 +++++++++++++------ .../service_main/src/audio_core_service.cpp | 4 +- 5 files changed, 55 insertions(+), 20 deletions(-) diff --git a/frameworks/native/audiocapturer/include/audio_capturer_private.h b/frameworks/native/audiocapturer/include/audio_capturer_private.h index 8f87d88b65..ce3f3113af 100644 --- a/frameworks/native/audiocapturer/include/audio_capturer_private.h +++ b/frameworks/native/audiocapturer/include/audio_capturer_private.h @@ -169,6 +169,7 @@ private: IAudioStream::StreamClass SetCaptureInfo(AudioStreamParams &audioStreamParams); std::shared_ptr GetStreamDescBySwitchInfo( const IAudioStream::SwitchInfo &switchInfo, const RestoreInfo &restoreInfo); + int32_t HandleCreateFastStreamError(AudioStreamParams &audioStreamParams); std::shared_ptr inputDeviceChangeCallback_ = nullptr; bool isSwitching_ = false; mutable std::shared_mutex switchStreamMutex_; diff --git a/frameworks/native/audiocapturer/src/audio_capturer.cpp b/frameworks/native/audiocapturer/src/audio_capturer.cpp index 62f2e42548..3b07277609 100644 --- a/frameworks/native/audiocapturer/src/audio_capturer.cpp +++ b/frameworks/native/audiocapturer/src/audio_capturer.cpp @@ -305,14 +305,7 @@ int32_t AudioCapturerPrivate::SetParams(const AudioCapturerParams params) ret = InitAudioStream(audioStreamParams); // When the fast stream creation fails, a normal stream is created if (ret != SUCCESS && streamClass == IAudioStream::FAST_STREAM) { - AUDIO_INFO_LOG("Create fast Stream fail, record by normal stream"); - streamClass = IAudioStream::PA_STREAM; - audioStream_ = IAudioStream::GetRecordStream(streamClass, audioStreamParams, audioStreamType_, appInfo_.appUid); - CHECK_AND_RETURN_RET_LOG(audioStream_ != nullptr, ERR_INVALID_PARAM, "Get normal record stream failed"); - ret = InitAudioStream(audioStreamParams); - CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Init normal audio stream failed"); - audioStream_->SetCaptureMode(CAPTURE_MODE_CALLBACK); - callbackLoopTid_ = audioStream_->GetCallbackLoopTid(); + ret = HandleCreateFastStreamError(audioStreamParams); } CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "InitAudioStream failed"); @@ -2061,5 +2054,28 @@ int32_t AudioCapturerPrivate::CheckAndStopAudioCapturer(std::string callingFunc) Stop(); return SUCCESS; } + +int32_t AudioCapturerPrivate::HandleCreateFastStreamError(AudioStreamParams &audioStreamParams) +{ + AUDIO_INFO_LOG("Create fast Stream fail, record by normal stream"); + IAudioStream::StreamClass streamClass = IAudioStream::PA_STREAM; + capturerInfo_.capturerFlags = AUDIO_FLAG_FORCED_NORMAL; + + // Create stream desc and pipe + std::shared_ptr streamDesc = ConvertToStreamDescriptor(audioStreamParams); + uint32_t flag = AUDIO_INPUT_FLAG_NORMAL; + uint32_t sessionId = 0; + int32_t ret = AudioPolicyManager::GetInstance().CreateCapturerClient(streamDesc, flag, sessionId); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "CreateCapturerClient failed"); + AUDIO_INFO_LOG("Create normal capturer, id: %{public}u", sessionId); + + audioStream_ = IAudioStream::GetRecordStream(streamClass, audioStreamParams, audioStreamType_, appInfo_.appUid); + CHECK_AND_RETURN_RET_LOG(audioStream_ != nullptr, ERR_INVALID_PARAM, "Get normal record stream failed"); + ret = InitAudioStream(audioStreamParams); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Init normal audio stream failed"); + audioStream_->SetCaptureMode(CAPTURE_MODE_CALLBACK); + callbackLoopTid_ = audioStream_->GetCallbackLoopTid(); + return ret; +} } // namespace AudioStandard } // namespace OHOS diff --git a/frameworks/native/audiorenderer/include/audio_renderer_private.h b/frameworks/native/audiorenderer/include/audio_renderer_private.h index e5273d4495..db9f9e0ec7 100644 --- a/frameworks/native/audiorenderer/include/audio_renderer_private.h +++ b/frameworks/native/audiorenderer/include/audio_renderer_private.h @@ -234,6 +234,7 @@ private: int32_t SetPitch(float pitch); FastStatus GetFastStatusInner(); void FastStatusChangeCallback(FastStatus status); + int32_t HandleCreateFastStreamError(AudioStreamParams &audioStreamParams, AudioStreamType audioStreamType); std::shared_ptr audioInterruptCallback_ = nullptr; std::shared_ptr audioStreamCallback_ = nullptr; diff --git a/frameworks/native/audiorenderer/src/audio_renderer.cpp b/frameworks/native/audiorenderer/src/audio_renderer.cpp index a7cfb62763..72a2f2bac9 100644 --- a/frameworks/native/audiorenderer/src/audio_renderer.cpp +++ b/frameworks/native/audiorenderer/src/audio_renderer.cpp @@ -610,17 +610,7 @@ int32_t AudioRendererPrivate::SetParams(const AudioRendererParams params) ret = InitAudioStream(audioStreamParams); // When the fast stream creation fails, a normal stream is created if (ret != SUCCESS && streamClass == IAudioStream::FAST_STREAM) { - AUDIO_INFO_LOG("Create fast Stream fail, play by normal stream."); - streamClass = IAudioStream::PA_STREAM; - isFastRenderer_ = false; - audioStream_ = IAudioStream::GetPlaybackStream(streamClass, audioStreamParams, audioStreamType, - appInfo_.appUid); - CHECK_AND_RETURN_RET_LOG(audioStream_ != nullptr, - ERR_INVALID_PARAM, "SetParams GetPlayBackStream failed when create normal stream."); - ret = InitAudioStream(audioStreamParams); - CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "InitAudioStream failed"); - audioStream_->SetRenderMode(RENDER_MODE_CALLBACK); - callbackLoopTid_ = audioStream_->GetCallbackLoopTid(); + ret = HandleCreateFastStreamError(audioStreamParams, audioStreamType); } CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "SetAudioStreamInfo Failed"); @@ -2761,5 +2751,30 @@ int32_t AudioRendererPrivate::CheckAndStopAudioRenderer(std::string callingFunc) Stop(); return SUCCESS; } + +int32_t AudioRendererPrivate::HandleCreateFastStreamError(AudioStreamParams &audioStreamParams, + AudioStreamType audioStreamType) +{ + AUDIO_INFO_LOG("Create fast Stream fail, play by normal stream."); + IAudioStream::StreamClass streamClass = IAudioStream::PA_STREAM; + isFastRenderer_ = false; + rendererInfo_.rendererFlags = AUDIO_FLAG_FORCED_NORMAL; + + // Create stream desc and pipe + std::shared_ptr streamDesc = ConvertToStreamDescriptor(audioStreamParams); + uint32_t flag = AUDIO_OUTPUT_FLAG_NORMAL; + uint32_t sessionId = 0; + int32_t ret = AudioPolicyManager::GetInstance().CreateRendererClient(streamDesc, flag, sessionId); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "CreateRendererClient failed"); + AUDIO_INFO_LOG("Create normal renderer, id: %{public}u", sessionId); + + audioStream_ = IAudioStream::GetPlaybackStream(streamClass, audioStreamParams, audioStreamType, appInfo_.appUid); + CHECK_AND_RETURN_RET_LOG(audioStream_ != nullptr, ERR_INVALID_PARAM, "Re-create normal stream failed."); + ret = InitAudioStream(audioStreamParams); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "InitAudioStream failed"); + audioStream_->SetRenderMode(RENDER_MODE_CALLBACK); + callbackLoopTid_ = audioStream_->GetCallbackLoopTid(); + return ret; +} } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_policy/server/service/service_main/src/audio_core_service.cpp b/services/audio_policy/server/service/service_main/src/audio_core_service.cpp index 13db0d3a6d..8593dff62d 100644 --- a/services/audio_policy/server/service/service_main/src/audio_core_service.cpp +++ b/services/audio_policy/server/service/service_main/src/audio_core_service.cpp @@ -268,6 +268,7 @@ void AudioCoreService::UpdatePlaybackStreamFlag(std::shared_ptrnewDeviceDescs_.front()->deviceType_); // fast/normal has done in audioRendererPrivate if (streamDesc->rendererInfo_.originalFlag == AUDIO_FLAG_FORCED_NORMAL || + streamDesc->rendererInfo_.rendererFlags == AUDIO_FLAG_FORCED_NORMAL || (streamDesc->rendererInfo_.streamUsage == STREAM_USAGE_VIDEO_COMMUNICATION && streamDesc->rendererInfo_.samplingRate != SAMPLE_RATE_48000)) { streamDesc->audioFlag_ = AUDIO_OUTPUT_FLAG_NORMAL; @@ -334,7 +335,8 @@ AudioFlag AudioCoreService::SetFlagForSpecialStream(std::shared_ptr streamDesc) { - if (streamDesc->capturerInfo_.originalFlag == AUDIO_FLAG_FORCED_NORMAL) { + if (streamDesc->capturerInfo_.originalFlag == AUDIO_FLAG_FORCED_NORMAL || + streamDesc->capturerInfo_.capturerFlags == AUDIO_FLAG_FORCED_NORMAL) { streamDesc->audioFlag_ = AUDIO_INPUT_FLAG_NORMAL; AUDIO_INFO_LOG("Forced normal cases"); return; -- Gitee From 66245dd2c36403c416292e65c815da91ae9355cf Mon Sep 17 00:00:00 2001 From: liyuhang Date: Sun, 8 Jun 2025 12:20:26 +0000 Subject: [PATCH 2/3] Code check Signed-off-by: liyuhang Change-Id: I2d2dd0e26e10ac71e42a7db87c7ca5f197181d2d --- frameworks/native/audiocapturer/src/audio_capturer.cpp | 7 ++++--- frameworks/native/audiorenderer/src/audio_renderer.cpp | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/frameworks/native/audiocapturer/src/audio_capturer.cpp b/frameworks/native/audiocapturer/src/audio_capturer.cpp index 3b07277609..b111764ea9 100644 --- a/frameworks/native/audiocapturer/src/audio_capturer.cpp +++ b/frameworks/native/audiocapturer/src/audio_capturer.cpp @@ -356,6 +356,7 @@ std::shared_ptr AudioCapturerPrivate::ConvertToStreamDesc streamDesc->appInfo_ = appInfo_; streamDesc->callerUid_ = static_cast(getuid()); streamDesc->callerPid_ = static_cast(getpid()); + streamDesc->sessionId_ = audioStreamParams.originalSessionId; return streamDesc; } @@ -2064,10 +2065,10 @@ int32_t AudioCapturerPrivate::HandleCreateFastStreamError(AudioStreamParams &aud // Create stream desc and pipe std::shared_ptr streamDesc = ConvertToStreamDescriptor(audioStreamParams); uint32_t flag = AUDIO_INPUT_FLAG_NORMAL; - uint32_t sessionId = 0; - int32_t ret = AudioPolicyManager::GetInstance().CreateCapturerClient(streamDesc, flag, sessionId); + int32_t ret = AudioPolicyManager::GetInstance().CreateCapturerClient(streamDesc, flag, + audioStreamParams.originalSessionId); CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "CreateCapturerClient failed"); - AUDIO_INFO_LOG("Create normal capturer, id: %{public}u", sessionId); + AUDIO_INFO_LOG("Create normal capturer, id: %{public}u", audioStreamParams.originalSessionId); audioStream_ = IAudioStream::GetRecordStream(streamClass, audioStreamParams, audioStreamType_, appInfo_.appUid); CHECK_AND_RETURN_RET_LOG(audioStream_ != nullptr, ERR_INVALID_PARAM, "Get normal record stream failed"); diff --git a/frameworks/native/audiorenderer/src/audio_renderer.cpp b/frameworks/native/audiorenderer/src/audio_renderer.cpp index 72a2f2bac9..e7b0712e0c 100644 --- a/frameworks/native/audiorenderer/src/audio_renderer.cpp +++ b/frameworks/native/audiorenderer/src/audio_renderer.cpp @@ -674,6 +674,7 @@ std::shared_ptr AudioRendererPrivate::ConvertToStreamDesc streamDesc->appInfo_ = appInfo_; streamDesc->callerUid_ = static_cast(getuid()); streamDesc->callerPid_ = static_cast(getpid()); + streamDesc->sessionId_ = audioStreamParams.originalSessionId; return streamDesc; } @@ -2763,10 +2764,10 @@ int32_t AudioRendererPrivate::HandleCreateFastStreamError(AudioStreamParams &aud // Create stream desc and pipe std::shared_ptr streamDesc = ConvertToStreamDescriptor(audioStreamParams); uint32_t flag = AUDIO_OUTPUT_FLAG_NORMAL; - uint32_t sessionId = 0; - int32_t ret = AudioPolicyManager::GetInstance().CreateRendererClient(streamDesc, flag, sessionId); + int32_t ret = AudioPolicyManager::GetInstance().CreateRendererClient(streamDesc, flag, + audioStreamParams.originalSessionId); CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "CreateRendererClient failed"); - AUDIO_INFO_LOG("Create normal renderer, id: %{public}u", sessionId); + AUDIO_INFO_LOG("Create normal renderer, id: %{public}u", audioStreamParams.originalSessionId); audioStream_ = IAudioStream::GetPlaybackStream(streamClass, audioStreamParams, audioStreamType, appInfo_.appUid); CHECK_AND_RETURN_RET_LOG(audioStream_ != nullptr, ERR_INVALID_PARAM, "Re-create normal stream failed."); -- Gitee From 7b037cc331fe0c88575c12c81bca3b93a84a20f5 Mon Sep 17 00:00:00 2001 From: liyuhang Date: Sun, 8 Jun 2025 12:38:07 +0000 Subject: [PATCH 3/3] Handle re-create stream error Signed-off-by: liyuhang Change-Id: I706c879b419fab38230cfbacdcbeca5d51b5260d --- .../audiocapturer/src/audio_capturer.cpp | 18 +++++++++++------- .../audiorenderer/src/audio_renderer.cpp | 18 +++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/frameworks/native/audiocapturer/src/audio_capturer.cpp b/frameworks/native/audiocapturer/src/audio_capturer.cpp index b111764ea9..8e2c87b888 100644 --- a/frameworks/native/audiocapturer/src/audio_capturer.cpp +++ b/frameworks/native/audiocapturer/src/audio_capturer.cpp @@ -1511,6 +1511,12 @@ bool AudioCapturerPrivate::FinishOldStream(IAudioStream::StreamClass targetClass bool AudioCapturerPrivate::GenerateNewStream(IAudioStream::StreamClass targetClass, RestoreInfo restoreInfo, CapturerState previousState, IAudioStream::SwitchInfo &switchInfo) { + std::shared_ptr streamDesc = GetStreamDescBySwitchInfo(switchInfo, restoreInfo); + uint32_t flag = AUDIO_INPUT_FLAG_NORMAL; + int32_t ret = AudioPolicyManager::GetInstance().CreateCapturerClient( + streamDesc, flag, switchInfo.params.originalSessionId); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, false, "CreateCapturerClient failed"); + bool switchResult = false; std::shared_ptr oldAudioStream = nullptr; std::shared_ptr newAudioStream = IAudioStream::GetRecordStream(targetClass, switchInfo.params, @@ -1522,6 +1528,11 @@ bool AudioCapturerPrivate::GenerateNewStream(IAudioStream::StreamClass targetCla int32_t initResult = SetSwitchInfo(switchInfo, newAudioStream); if (initResult != SUCCESS && switchInfo.capturerInfo.originalFlag != AUDIO_FLAG_NORMAL) { AUDIO_ERR_LOG("Re-create stream failed, crate normal ipc stream"); + streamDesc->capturerInfo_.capturerFlags = AUDIO_FLAG_FORCED_NORMAL; + int32_t ret = AudioPolicyManager::GetInstance().CreateCapturerClient( + streamDesc, flag, switchInfo.params.originalSessionId); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, false, "CreateRendererClient failed"); + newAudioStream = IAudioStream::GetRecordStream(IAudioStream::PA_STREAM, switchInfo.params, switchInfo.eStreamType, appInfo_.appUid); CHECK_AND_RETURN_RET_LOG(newAudioStream != nullptr, false, "Get ipc stream failed"); @@ -1602,13 +1613,6 @@ bool AudioCapturerPrivate::SwitchToTargetStream(IAudioStream::StreamClass target switchResult = FinishOldStream(targetClass, restoreInfo, previousState, switchInfo); CHECK_AND_RETURN_RET_LOG(switchResult, false, "Finish old stream failed"); - // Create stream and pipe - std::shared_ptr streamDesc = GetStreamDescBySwitchInfo(switchInfo, restoreInfo); - uint32_t flag = AUDIO_INPUT_FLAG_NORMAL; - int32_t ret = AudioPolicyManager::GetInstance().CreateCapturerClient( - streamDesc, flag, switchInfo.params.originalSessionId); - CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "CreateCapturerClient failed"); - // Create and start new stream. switchResult = GenerateNewStream(targetClass, restoreInfo, previousState, switchInfo); CHECK_AND_RETURN_RET_LOG(switchResult, false, "Generate new stream failed"); diff --git a/frameworks/native/audiorenderer/src/audio_renderer.cpp b/frameworks/native/audiorenderer/src/audio_renderer.cpp index e7b0712e0c..181fe91d70 100644 --- a/frameworks/native/audiorenderer/src/audio_renderer.cpp +++ b/frameworks/native/audiorenderer/src/audio_renderer.cpp @@ -2078,6 +2078,12 @@ bool AudioRendererPrivate::FinishOldStream(IAudioStream::StreamClass targetClass bool AudioRendererPrivate::GenerateNewStream(IAudioStream::StreamClass targetClass, RestoreInfo restoreInfo, RendererState previousState, IAudioStream::SwitchInfo &switchInfo) { + std::shared_ptr streamDesc = GetStreamDescBySwitchInfo(switchInfo, restoreInfo); + uint32_t flag = AUDIO_OUTPUT_FLAG_NORMAL; + int32_t ret = AudioPolicyManager::GetInstance().CreateRendererClient( + streamDesc, flag, switchInfo.params.originalSessionId); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, false, "CreateRendererClient failed"); + bool switchResult = false; std::shared_ptr oldAudioStream = nullptr; // create new IAudioStream @@ -2091,6 +2097,11 @@ bool AudioRendererPrivate::GenerateNewStream(IAudioStream::StreamClass targetCla switchResult = SetSwitchInfo(switchInfo, newAudioStream); if (!switchResult && switchInfo.rendererInfo.originalFlag != AUDIO_FLAG_NORMAL) { AUDIO_ERR_LOG("Re-create stream failed, create normal ipc stream"); + streamDesc->rendererInfo_.rendererFlags = AUDIO_FLAG_FORCED_NORMAL; + int32_t ret = AudioPolicyManager::GetInstance().CreateRendererClient(streamDesc, flag, + switchInfo.params.originalSessionId); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, false, "CreateRendererClient failed"); + newAudioStream = IAudioStream::GetPlaybackStream(IAudioStream::PA_STREAM, switchInfo.params, switchInfo.eStreamType, appInfo_.appUid); targetClass = IAudioStream::PA_STREAM; @@ -2179,13 +2190,6 @@ bool AudioRendererPrivate::SwitchToTargetStream(IAudioStream::StreamClass target switchResult = FinishOldStream(targetClass, restoreInfo, previousState, switchInfo); CHECK_AND_RETURN_RET_LOG(switchResult, false, "Finish old stream failed"); - // Create stream and pipe - std::shared_ptr streamDesc = GetStreamDescBySwitchInfo(switchInfo, restoreInfo); - uint32_t flag = AUDIO_OUTPUT_FLAG_NORMAL; - int32_t ret = AudioPolicyManager::GetInstance().CreateRendererClient( - streamDesc, flag, switchInfo.params.originalSessionId); - CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "CreateRendererClient failed"); - // Create and start new stream. switchResult = GenerateNewStream(targetClass, restoreInfo, previousState, switchInfo); CHECK_AND_RETURN_RET_LOG(switchResult, false, "Generate new stream failed"); -- Gitee