diff --git a/frameworks/native/audioadapter/BUILD.gn b/frameworks/native/audioadapter/BUILD.gn index f26ec5147a56b4d47d010b1fa92fad071931e362..e728a0aa3e56c84928f8a34693061dc03d2ca168 100644 --- a/frameworks/native/audioadapter/BUILD.gn +++ b/frameworks/native/audioadapter/BUILD.gn @@ -24,7 +24,13 @@ ohos_shared_library("pulse_audio_service_adapter") { boundary_sanitize = true } install_enable = true - sources = [ "src/pulse_audio_service_adapter_impl.cpp" ] + + sources = [ + "src/audio_service_adapter.cpp", + "src/pro_audio_service_adapter_impl.cpp", + "src/pulse_audio_service_adapter_impl.cpp", + ] + cflags = [ "-fPIC" ] cflags += [ "-Wall" ] cflags += [ "-Os" ] @@ -35,9 +41,17 @@ ohos_shared_library("pulse_audio_service_adapter") { "../audioutils/include", "../../../interfaces/inner_api/native/audiocommon/include", "../../../services/audio_service/server/include", + "../hdiadapter/common/include", + "../hdiadapter/sink/common", + "../hdiadapter/source/common", + "../../../services/audio_policy/server/include/service/common", + "../../../services/audio_service/common/include", ] - deps = [ "../audioutils:audio_utils" ] + deps = [ + "../../../services/audio_service:audio_common", + "../audioutils:audio_utils", + ] external_deps = [ "c_utils:utils", diff --git a/frameworks/native/audioadapter/include/audio_service_adapter.h b/frameworks/native/audioadapter/include/audio_service_adapter.h index 427118206021eb04fd063f19458722fb2d567cc3..124ba367a1ce27dcd74cb1091070a7958bd77e54 100644 --- a/frameworks/native/audioadapter/include/audio_service_adapter.h +++ b/frameworks/native/audioadapter/include/audio_service_adapter.h @@ -22,6 +22,7 @@ #include #include "audio_effect.h" +#include "audio_module_info.h" namespace OHOS { namespace AudioStandard { @@ -42,7 +43,7 @@ public: * @param cb callback reference for AudioServiceAdapterCallback class * @return Returns instance of class that extends AudioServiceAdapter */ - static std::unique_ptr CreateAudioAdapter(std::unique_ptr cb); + static std::shared_ptr CreateAudioAdapter(std::unique_ptr cb); /** * @brief Connect to underlining audio server @@ -62,6 +63,7 @@ public: * defined in {@link audio_errors.h} otherwise. */ virtual uint32_t OpenAudioPort(std::string audioPortName, std::string moduleArgs) = 0; + virtual int32_t OpenAudioPort(std::string audioPortName, const AudioModuleInfo& audioModuleInfo) = 0; /** * @brief closes/unloads the audio modules loaded. @@ -177,6 +179,36 @@ public: */ virtual int32_t MoveSinkInputByIndexOrName(uint32_t sinkInputId, uint32_t sinkIndex, std::string sinkName) = 0; + /** + * @brief Get current effect property. + * + * @return int32_t the result. + */ + virtual int32_t GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray) = 0; + + /** + * @brief Get current effect property. + * + * @return int32_t the result. + */ + virtual int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) = 0; + + /** + * @brief Get current enhance property. + * + * @return int32_t the result. + */ + virtual int32_t GetAudioEnhanceProperty(AudioEffectPropertyArrayV3 &propertyArray, + DeviceType deviceType = DEVICE_TYPE_NONE) = 0; + + /** + * @brief Get current enhance property. + * + * @return int32_t the result. + */ + virtual int32_t GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray, + DeviceType deviceType = DEVICE_TYPE_NONE) = 0; + virtual ~AudioServiceAdapter(); }; } // namespace AudioStandard diff --git a/frameworks/native/audioadapter/include/pro_audio_service_adapter_impl.h b/frameworks/native/audioadapter/include/pro_audio_service_adapter_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..cdac8b3f0f42d4ca655e463820440eed640fad1a --- /dev/null +++ b/frameworks/native/audioadapter/include/pro_audio_service_adapter_impl.h @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ST_PRO_AUDIO_SERVICE_ADAPTER_H +#define ST_PRO_AUDIO_SERVICE_ADAPTER_H +#include +#include "safe_map.h" +#include +#include "audio_service_adapter.h" +#include "audio_module_info.h" + +namespace OHOS { +namespace AudioStandard { + +class ProAudioServiceAdapterImpl : public AudioServiceAdapter, + public std::enable_shared_from_this { +public: + explicit ProAudioServiceAdapterImpl(std::unique_ptr &cb); + ~ProAudioServiceAdapterImpl(); + + bool Connect() override; + uint32_t OpenAudioPort(std::string audioPortName, std::string moduleArgs) override; + int32_t OpenAudioPort(std::string audioPortName, const AudioModuleInfo& audioModuleInfo) override; + int32_t CloseAudioPort(int32_t audioHandleIndex, bool isSync = false) override; + int32_t SetDefaultSink(std::string name) override; + int32_t SetDefaultSource(std::string name) override; + int32_t SetSourceOutputMute(int32_t uid, bool setMute) override; + int32_t SuspendAudioDevice(std::string &audioPortName, bool isSuspend) override; + bool SetSinkMute(const std::string &sinkName, bool isMute, bool isSync = false) override; + std::vector GetAllSinkInputs() override; + std::vector GetAllSourceOutputs() override; + void Disconnect() override; + + std::vector GetTargetSinks(std::string adapterName) override; + std::vector GetAllSinks() override; + int32_t SetLocalDefaultSink(std::string name) override; + int32_t MoveSinkInputByIndexOrName(uint32_t sinkInputId, uint32_t sinkIndex, std::string sinkName) override; + int32_t MoveSourceOutputByIndexOrName( + uint32_t sourceOutputId, uint32_t sourceIndex, std::string sourceName) override; + int32_t GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray) override; + int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) override; + int32_t GetAudioEnhanceProperty(AudioEffectPropertyArrayV3 &propertyArray, + DeviceType deviceType = DEVICE_TYPE_NONE) override; + int32_t GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray, + DeviceType deviceType = DEVICE_TYPE_NONE) override; + // callback Member functions + +private: + std::mutex lock_; + // for status operation wait and notify + std::mutex callbackMutex_; + std::condition_variable callbackCV_; + + bool isFinishOpenAudioPort_ = false; + int32_t AudioPortIndex_ = 0; + bool isFinishCloseAudioPort_ = false; + + bool isFinishGetAllSinkInputs_ = false; + std::vector sinkInputs_; + bool isFinishGetAllSourceOutputs_ = false; + std::vector sourceOutputs_; + bool isFinishGetAllSinks_ = false; + std::vector sinks_; + bool isFinishMoveSinkInputByIndexOrName_ = false; + bool isFinishMoveSourceOutputByIndexOrName_ = false; + + int32_t SourceOutputMuteStreamSet_ = 0; + bool isFinishSetSourceOutputMute_ = false; + bool isFinishSetSinkMute_ = false; + + bool isFinishGetAudioEffectPropertyV3_ = false; + bool isFinishGetAudioEffectProperty_ = false; + bool isFinishGetAudioEnhancePropertyV3_ = false; + bool isFinishGetAudioEnhanceProperty_ = false; +}; +} // namespace AudioStandard +} // namespace OHOS +#endif // ST_PULSEAUDIO_AUDIO_SERVICE_ADAPTER_H diff --git a/frameworks/native/audioadapter/include/pulse_audio_service_adapter_impl.h b/frameworks/native/audioadapter/include/pulse_audio_service_adapter_impl.h index 604bbbf534408a3765e39d6360c2c51c19c8ff53..681cf6de10dfed9a678eb8e78fdb16285e61992e 100644 --- a/frameworks/native/audioadapter/include/pulse_audio_service_adapter_impl.h +++ b/frameworks/native/audioadapter/include/pulse_audio_service_adapter_impl.h @@ -32,6 +32,7 @@ public: bool Connect() override; uint32_t OpenAudioPort(std::string audioPortName, std::string moduleArgs) override; + int32_t OpenAudioPort(std::string audioPortName, const AudioModuleInfo& audioModuleInfo) override; int32_t CloseAudioPort(int32_t audioHandleIndex, bool isSync = false) override; int32_t SetDefaultSink(std::string name) override; int32_t SetDefaultSource(std::string name) override; @@ -48,6 +49,12 @@ public: int32_t MoveSinkInputByIndexOrName(uint32_t sinkInputId, uint32_t sinkIndex, std::string sinkName) override; int32_t MoveSourceOutputByIndexOrName(uint32_t sourceOutputId, uint32_t sourceIndex, std::string sourceName) override; + int32_t GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray) override { return 0; } + int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) override { return 0; } + int32_t GetAudioEnhanceProperty(AudioEffectPropertyArrayV3 &propertyArray, + DeviceType deviceType = DEVICE_TYPE_NONE) override { return 0; } + int32_t GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray, + DeviceType deviceType = DEVICE_TYPE_NONE) override { return 0; } // Static Member functions static void PaGetSinksCb(pa_context *c, const pa_sink_info *i, int eol, void *userdata); diff --git a/frameworks/native/audioadapter/src/audio_service_adapter.cpp b/frameworks/native/audioadapter/src/audio_service_adapter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0452aa1013b620d43a03e28af1f206c1fd190af9 --- /dev/null +++ b/frameworks/native/audioadapter/src/audio_service_adapter.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LOG_TAG +#define LOG_TAG "AudioServiceAdapter" +#endif + +#include "pro_audio_service_adapter_impl.h" +#if defined(_WIN32) || defined(_WIN64) +#include +#endif +#include +#include + +#include "audio_errors.h" +#include "audio_common_log.h" +#include "audio_info.h" +#include "audio_utils.h" +#include +#include + +#include "pulse_audio_service_adapter_impl.h" +#include "pro_audio_service_adapter_impl.h" + +using namespace std; + +namespace OHOS { +namespace AudioStandard { + +AudioServiceAdapter::~AudioServiceAdapter() = default; + +std::shared_ptr AudioServiceAdapter::CreateAudioAdapter( + std::unique_ptr cb) +{ + CHECK_AND_RETURN_RET_LOG(cb != nullptr, nullptr, "CreateAudioAdapter cb is nullptr!"); + AUDIO_INFO_LOG("CreateAudioAdapter"); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + return make_shared(cb); + } else { + return make_shared(cb); + } +} + +} // namespace AudioStandard +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/audioadapter/src/pro_audio_service_adapter_impl.cpp b/frameworks/native/audioadapter/src/pro_audio_service_adapter_impl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68c0a4548770ca99044f376ce5ab05dfa85f9bdd --- /dev/null +++ b/frameworks/native/audioadapter/src/pro_audio_service_adapter_impl.cpp @@ -0,0 +1,351 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ST_PRO_AUDIO_SERVICE_ADAPTER_IMPL_H +#define ST_PRO_AUDIO_SERVICE_ADAPTER_IMPL_H +#ifndef LOG_TAG +#define LOG_TAG "ProAudioServiceAdapterImpl" +#endif + +#include "pro_audio_service_adapter_impl.h" +#if defined(_WIN32) || defined(_WIN64) +#include +#endif +#include +#include + +#include "audio_errors.h" +#include "audio_common_log.h" +#include "audio_info.h" +#include "audio_utils.h" +#include +#include + +using namespace std; +namespace OHOS { +namespace AudioStandard { +static unique_ptr g_audioServiceAdapterCallback; +static const int32_t OPERATION_TIMEOUT_IN_MS = 1000; // 1000ms + +ProAudioServiceAdapterImpl::~ProAudioServiceAdapterImpl() = default; + +ProAudioServiceAdapterImpl::ProAudioServiceAdapterImpl(unique_ptr &cb) +{ + g_audioServiceAdapterCallback = move(cb); +} + +bool ProAudioServiceAdapterImpl::Connect() +{ + AUDIO_INFO_LOG("Connected RegiesterServiceCallback"); + return true; +} + +uint32_t ProAudioServiceAdapterImpl::OpenAudioPort(string audioPortName, string moduleArgs) +{ + AUDIO_PRERELEASE_LOGI("ERROR OpenAudioPort enter."); + return SUCCESS; +} + +int32_t ProAudioServiceAdapterImpl::OpenAudioPort(string audioPortName, const AudioModuleInfo &audioModuleInfo) +{ + AUDIO_PRERELEASE_LOGI("OpenAudioPort enter."); + Trace trace("OpenAudioPort"); + lock_guard lock(lock_); + isFinishOpenAudioPort_ = false; + std::unique_lock waitLock(callbackMutex_); + bool stopWaiting = callbackCV_.wait_for(waitLock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { + return isFinishOpenAudioPort_; // will be true when got notified. + }); + if (!stopWaiting) { + AUDIO_ERR_LOG("OpenAudioPort timeout"); + return ERROR; + } + AUDIO_INFO_LOG("OpenAudioPort leave"); + return AudioPortIndex_; +} + +int32_t ProAudioServiceAdapterImpl::CloseAudioPort(int32_t audioHandleIndex, bool isSync) +{ + AUDIO_INFO_LOG("try to close module:%{public}d", audioHandleIndex); + Trace trace("CloseAudioPort"); + lock_guard lock(lock_); + AUDIO_INFO_LOG("CloseAudioPort: audioHandleIndex: [%{public}d] isSync [%{public}d]", audioHandleIndex, isSync); + if (isSync) { + isFinishCloseAudioPort_ = false; + std::unique_lock waitLock(callbackMutex_); + bool stopWaiting = callbackCV_.wait_for(waitLock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { + return isFinishCloseAudioPort_; // will be true when got notified. + }); + if (!stopWaiting) { + AUDIO_ERR_LOG("CloseAudioPort timeout"); + return ERROR; + } + } + AUDIO_INFO_LOG("CloseAudioPort leave"); + return SUCCESS; +} + +int32_t ProAudioServiceAdapterImpl::SuspendAudioDevice(string &audioPortName, bool isSuspend) +{ + lock_guard lock(lock_); + Trace trace("SuspendAudioDevice"); + AUDIO_INFO_LOG("SuspendAudioDevice [%{public}s] : [%{public}d]", audioPortName.c_str(), isSuspend); + return SUCCESS; +} + +bool ProAudioServiceAdapterImpl::SetSinkMute(const std::string &sinkName, bool isMute, bool isSync) +{ + AUDIO_INFO_LOG("SetSinkMute: [%{public}s] : [%{public}d] isSync [%{public}d]", sinkName.c_str(), isMute, isSync); + lock_guard lock(lock_); + Trace trace("SetSinkMute:" + sinkName + "isMute:" + std::to_string(isMute)); + if (isSync) { + isFinishSetSinkMute_ = false; + std::unique_lock waitLock(callbackMutex_); + bool stopWaiting = callbackCV_.wait_for(waitLock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { + return isFinishSetSinkMute_; // will be true when got notified. + }); + if (!stopWaiting) { + AUDIO_ERR_LOG("SetSinkMute timeout"); + return ERROR; + } + } + AUDIO_INFO_LOG("SetSinkMute leave"); + return SUCCESS; +} + +int32_t ProAudioServiceAdapterImpl::SetDefaultSink(string name) +{ + lock_guard lock(lock_); + Trace trace("SetDefaultSink:" + name); + AUDIO_INFO_LOG("SetDefaultSink: [%{public}s]", name.c_str()); + return SUCCESS; +} + +int32_t ProAudioServiceAdapterImpl::SetDefaultSource(string name) +{ + lock_guard lock(lock_); + Trace trace("SetDefaultSource:" + name); + AUDIO_INFO_LOG("SetDefaultSink: [%{public}s]", name.c_str()); + return SUCCESS; +} + +std::vector ProAudioServiceAdapterImpl::GetAllSinks() +{ + AUDIO_INFO_LOG("GetAllSinks: enter"); + lock_guard lock(lock_); + Trace trace("GetAllSinks"); + isFinishGetAllSinks_ = false; + std::unique_lock waitLock(callbackMutex_); + bool stopWaiting = callbackCV_.wait_for(waitLock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { + return isFinishGetAllSinks_; // will be true when got notified. + }); + if (!stopWaiting) { + AUDIO_ERR_LOG("GetAllSinks timeout"); + sinks_.clear(); + } + AUDIO_INFO_LOG("GetAllSinks leave"); + return sinks_; +} + +std::vector ProAudioServiceAdapterImpl::GetTargetSinks(std::string adapterName) +{ + std::vector sinkInfos = GetAllSinks(); + Trace trace("GetTargetSinks:" + adapterName); + std::vector targetSinkIds = {}; + for (size_t i = 0; i < sinkInfos.size(); i++) { + if (sinkInfos[i].adapterName == adapterName) { + targetSinkIds.push_back(sinkInfos[i].sinkId); + } + } + AUDIO_INFO_LOG("GetTargetSinks: adapterName %{public}s", adapterName.c_str()); + return targetSinkIds; +} + +int32_t ProAudioServiceAdapterImpl::SetLocalDefaultSink(std::string name) +{ + AUDIO_INFO_LOG("SetLocalDefaultSink sink name: %{public}s", name.c_str()); + return SUCCESS; +} + +int32_t ProAudioServiceAdapterImpl::MoveSinkInputByIndexOrName( + uint32_t sinkInputId, uint32_t sinkIndex, std::string sinkName) +{ + AUDIO_INFO_LOG("MoveSinkInputByIndexOrName sinkInputId %{public}d, sinkIndex %{public}d, sinkName %{public}s", + sinkInputId, + sinkIndex, + sinkName.c_str()); + lock_guard lock(lock_); + Trace trace("MoveSinkInputByIndexOrName: " + std::to_string(sinkInputId) + " index:" + std::to_string(sinkIndex) + + " sink:" + sinkName); + isFinishMoveSinkInputByIndexOrName_ = false; + std::unique_lock waitLock(callbackMutex_); + bool stopWaiting = callbackCV_.wait_for(waitLock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { + return isFinishMoveSinkInputByIndexOrName_; // will be true when got notified. + }); + if (!stopWaiting) { + AUDIO_ERR_LOG("MoveSinkInputByIndexOrName timeout"); + return ERROR; + } + AUDIO_INFO_LOG("MoveSinkInputByIndexOrName leave"); + return SUCCESS; +} + +int32_t ProAudioServiceAdapterImpl::MoveSourceOutputByIndexOrName( + uint32_t sourceOutputId, uint32_t sourceIndex, std::string sourceName) +{ + AUDIO_INFO_LOG( + "MoveSourceOutputByIndexOrName sourceOutputId %{public}d, sourceIndex %{public}d, sourceName %{public}s", + sourceOutputId, + sourceIndex, + sourceName.c_str()); + lock_guard lock(lock_); + Trace trace("MoveSourceOutputByIndexOrName: " + std::to_string(sourceOutputId) + + " index:" + std::to_string(sourceIndex) + " source:" + sourceName); + isFinishMoveSourceOutputByIndexOrName_ = false; + std::unique_lock waitLock(callbackMutex_); + bool stopWaiting = callbackCV_.wait_for(waitLock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { + return isFinishMoveSourceOutputByIndexOrName_; // will be true when got notified. + }); + if (!stopWaiting) { + AUDIO_ERR_LOG("MoveSourceOutputByIndexOrName timeout"); + return ERROR; + } + AUDIO_INFO_LOG("MoveSourceOutputByIndexOrName leave"); + return SUCCESS; +} + +int32_t ProAudioServiceAdapterImpl::SetSourceOutputMute(int32_t uid, bool setMute) +{ + AUDIO_INFO_LOG("SetSourceOutputMute uid %{public}d, setMute %{public}d", uid, setMute); + lock_guard lock(lock_); + isFinishSetSourceOutputMute_ = false; + SourceOutputMuteStreamSet_ = 0; + std::unique_lock waitLock(callbackMutex_); + bool stopWaiting = callbackCV_.wait_for(waitLock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { + return isFinishSetSourceOutputMute_; // will be true when got notified. + }); + if (!stopWaiting) { + AUDIO_ERR_LOG("SetSourceOutputMute timeout"); + return ERROR; + } + AUDIO_INFO_LOG("SetSourceOutputMute leave"); + return SourceOutputMuteStreamSet_; +} + +std::vector ProAudioServiceAdapterImpl::GetAllSinkInputs() +{ + AUDIO_INFO_LOG("GetAllSinkInputs Enter"); + lock_guard lock(lock_); + isFinishGetAllSinkInputs_ = false; + std::unique_lock waitLock(callbackMutex_); + bool stopWaiting = callbackCV_.wait_for(waitLock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { + return isFinishGetAllSinkInputs_; // will be true when got notified. + }); + if (!stopWaiting) { + AUDIO_ERR_LOG("GetAllSinkInputs timeout"); + sinkInputs_.clear(); + } + AUDIO_INFO_LOG("GetAllSinkInputs leave"); + return sinkInputs_; +} + +std::vector ProAudioServiceAdapterImpl::GetAllSourceOutputs() +{ + AUDIO_INFO_LOG("GetAllSourceOutputs"); + lock_guard lock(lock_); + isFinishGetAllSourceOutputs_ = false; + std::unique_lock waitLock(callbackMutex_); + bool stopWaiting = callbackCV_.wait_for(waitLock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { + return isFinishGetAllSourceOutputs_; // will be true when got notified. + }); + if (!stopWaiting) { + AUDIO_ERR_LOG("GetAllSourceOutputs timeout"); + sourceOutputs_.clear(); + } + AUDIO_INFO_LOG("GetAllSourceOutputs leave"); + return sourceOutputs_; +} + +void ProAudioServiceAdapterImpl::Disconnect() +{ + AUDIO_INFO_LOG("Disconnect not support"); +} + +int32_t ProAudioServiceAdapterImpl::GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray) +{ + AUDIO_INFO_LOG("GetAudioEffectProperty"); + lock_guard lock(lock_); + isFinishGetAudioEffectPropertyV3_ = false; + std::unique_lock waitLock(callbackMutex_); + bool stopWaiting = callbackCV_.wait_for(waitLock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { + return isFinishGetAudioEffectPropertyV3_; + }); + if (!stopWaiting) { + AUDIO_WARNING_LOG("wait for notify timeout"); + } + return SUCCESS; +} + +int32_t ProAudioServiceAdapterImpl::GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) +{ + AUDIO_INFO_LOG("GetAudioEffectProperty"); + lock_guard lock(lock_); + isFinishGetAudioEffectProperty_ = false; + std::unique_lock waitLock(callbackMutex_); + bool stopWaiting = callbackCV_.wait_for(waitLock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { + return isFinishGetAudioEffectProperty_; + }); + if (!stopWaiting) { + AUDIO_WARNING_LOG("wait for notify timeout"); + } + return SUCCESS; +} + +int32_t ProAudioServiceAdapterImpl::GetAudioEnhanceProperty(AudioEffectPropertyArrayV3 &propertyArray, + DeviceType deviceType) +{ + AUDIO_INFO_LOG("GetAudioEnhancePropertyV3"); + lock_guard lock(lock_); + isFinishGetAudioEnhancePropertyV3_ = false; + std::unique_lock waitLock(callbackMutex_); + bool stopWaiting = callbackCV_.wait_for(waitLock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { + return isFinishGetAudioEnhancePropertyV3_; + }); + if (!stopWaiting) { + AUDIO_WARNING_LOG("wait for notify timeout"); + } + return SUCCESS; +} + +int32_t ProAudioServiceAdapterImpl::GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray, + DeviceType deviceType) +{ + AUDIO_INFO_LOG("GetAudioEnhanceProperty"); + lock_guard lock(lock_); + isFinishGetAudioEnhanceProperty_ = false; + std::unique_lock waitLock(callbackMutex_); + bool stopWaiting = callbackCV_.wait_for(waitLock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { + return isFinishGetAudioEnhanceProperty_; + }); + if (!stopWaiting) { + AUDIO_WARNING_LOG("wait for notify timeout"); + } + return SUCCESS; +} + +} // namespace AudioStandard +} // namespace OHOS + +#endif // ST_PULSEAUDIO_AUDIO_SERVICE_ADAPTER_IMPL_H diff --git a/frameworks/native/audioadapter/src/pulse_audio_service_adapter_impl.cpp b/frameworks/native/audioadapter/src/pulse_audio_service_adapter_impl.cpp index b067333e4836d10caefa48c42727c4d05e262ccc..811addef6048d5d21a524d2acf69cd90baab78ff 100644 --- a/frameworks/native/audioadapter/src/pulse_audio_service_adapter_impl.cpp +++ b/frameworks/native/audioadapter/src/pulse_audio_service_adapter_impl.cpp @@ -72,15 +72,8 @@ static const unordered_map STREAM_TYPE_STRING_ENUM {"camcorder", STREAM_CAMCORDER} }; -AudioServiceAdapter::~AudioServiceAdapter() = default; PulseAudioServiceAdapterImpl::~PulseAudioServiceAdapterImpl() = default; -unique_ptr AudioServiceAdapter::CreateAudioAdapter(unique_ptr cb) -{ - CHECK_AND_RETURN_RET_LOG(cb != nullptr, nullptr, "CreateAudioAdapter cb is nullptr!"); - return make_unique(cb); -} - PulseAudioServiceAdapterImpl::PulseAudioServiceAdapterImpl(unique_ptr &cb) { g_audioServiceAdapterCallback = move(cb); @@ -172,6 +165,12 @@ Fail: return false; } +int32_t PulseAudioServiceAdapterImpl::OpenAudioPort(string audioPortName, const AudioModuleInfo& audioModuleInfo) +{ + AUDIO_PRERELEASE_LOGE("OpenAudioPort enter the INCORRECT func."); + return 0; +} + uint32_t PulseAudioServiceAdapterImpl::OpenAudioPort(string audioPortName, string moduleArgs) { AUDIO_PRERELEASE_LOGI("Enter, port name: %{public}s", audioPortName.c_str()); diff --git a/frameworks/native/audioeffect/include/audio_effect_chain.h b/frameworks/native/audioeffect/include/audio_effect_chain.h index 989ba33279b202b12df16a57936326122c5b8c7b..5fb3a3ce7d3cca73e749e5b0894d20a0187c6f3c 100644 --- a/frameworks/native/audioeffect/include/audio_effect_chain.h +++ b/frameworks/native/audioeffect/include/audio_effect_chain.h @@ -19,12 +19,10 @@ #include #include "audio_effect.h" -#include "audio_utils.h" #ifdef SENSOR_ENABLE #include "audio_head_tracker.h" #endif -#include "audio_effect_hdi_param.h" #ifdef WINDOW_MANAGER_ENABLE #include "audio_effect_rotation.h" #endif diff --git a/frameworks/native/audioeffect/include/audio_effect_chain_manager.h b/frameworks/native/audioeffect/include/audio_effect_chain_manager.h index 42bad17b978b7deb77163ceccbf5ed81cfbc8e8f..96d3358a960a09fd605bab6b7887f4ec288a7655 100644 --- a/frameworks/native/audioeffect/include/audio_effect_chain_manager.h +++ b/frameworks/native/audioeffect/include/audio_effect_chain_manager.h @@ -35,7 +35,7 @@ #ifdef SENSOR_ENABLE #include "audio_head_tracker.h" #endif -#include "audio_effect_hdi_param.h" + #ifdef WINDOW_MANAGER_ENABLE #include "audio_effect_rotation.h" #endif @@ -44,6 +44,8 @@ namespace OHOS { namespace AudioStandard { +class AudioEffectHdiParam; + const uint32_t DEFAULT_FRAMELEN = 1440; const uint32_t DEFAULT_NUM_CHANNEL = STEREO; const uint32_t DEFAULT_MCH_NUM_CHANNEL = CHANNEL_6; @@ -64,7 +66,6 @@ struct SessionEffectInfo { std::string sceneType; uint32_t channels; uint64_t channelLayout; - std::string spatializationEnabled; int32_t streamUsage; int32_t systemVolumeType; }; @@ -104,17 +105,26 @@ enum SceneTypeOperation { REMOVE_SCENE_TYPE = 1, }; +enum ProcessClusterOperation { + NO_NEED_TO_CREATE_PROCESSCLUSTER, + CREATE_NEW_PROCESSCLUSTER, + CREATE_DEFAULT_PROCESSCLUSTER, + USE_DEFAULT_PROCESSCLUSTER, + USE_NONE_PROCESSCLUSTER, + CREATE_EXTRA_PROCESSCLUSTER +}; + class AudioEffectChainManager { public: AudioEffectChainManager(); ~AudioEffectChainManager(); static AudioEffectChainManager *GetInstance(); - void InitAudioEffectChainManager(std::vector &effectChains, + void InitAudioEffectChainManager(const std::vector &effectChains, const EffectChainManagerParam &effectChainManagerParam, - std::vector> &effectLibraryList); - void ConstructEffectChainMgrMaps(std::vector &effectChains, + const std::vector> &effectLibraryList); + void ConstructEffectChainMgrMaps(const std::vector &effectChains, const EffectChainManagerParam &effectChainManagerParam, - std::vector> &effectLibraryList); + const std::vector> &effectLibraryList); bool CheckAndAddSessionID(const std::string &sessionID); int32_t CreateAudioEffectChainDynamic(const std::string &sceneType); bool CheckAndRemoveSessionID(const std::string &sessionID); @@ -132,7 +142,7 @@ public: int32_t ReturnEffectChannelInfo(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout); int32_t ReturnMultiChannelInfo(uint32_t *channels, uint64_t *channelLayout); int32_t EffectRotationUpdate(const uint32_t rotationState); - int32_t EffectVolumeUpdate(std::shared_ptr audioEffectVolume); + int32_t EffectVolumeUpdate(); int32_t StreamVolumeUpdate(const std::string sessionIDString, const float streamVolume); uint32_t GetLatency(const std::string &sessionId); int32_t SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType); @@ -154,6 +164,8 @@ public: int32_t QueryEffectChannelInfo(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout); int32_t QueryHdiSupportedChannelInfo(uint32_t &channels, uint64_t &channelLayout); void LoadEffectProperties(); + ProcessClusterOperation CheckProcessClusterInstances(const std::string &sceneType); + int32_t GetOutputChannelInfo(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout); private: int32_t SetAudioEffectChainDynamic(std::string &sceneType, const std::string &effectMode); void UpdateSensorState(); diff --git a/frameworks/native/audioeffect/include/audio_effect_hdi_param.h b/frameworks/native/audioeffect/include/audio_effect_hdi_param.h index 61f7bc1ac879c843cd1520b47889348cca14fded..37312ff2aef8395051e214fd6777a3851a0f08ac 100644 --- a/frameworks/native/audioeffect/include/audio_effect_hdi_param.h +++ b/frameworks/native/audioeffect/include/audio_effect_hdi_param.h @@ -18,8 +18,7 @@ #include #include "v1_0/ieffect_model.h" - -const uint32_t SEND_HDI_COMMAND_LEN = 20; +#include "audio_effect.h" namespace OHOS { namespace AudioStandard { diff --git a/frameworks/native/audioeffect/include/audio_enhance_chain_manager.h b/frameworks/native/audioeffect/include/audio_enhance_chain_manager.h index 348efdce2430f3e51bfa1ead862a1b78bc17fc3b..bd59dabd34723262c0b241176fc4bc7a86655bc3 100644 --- a/frameworks/native/audioeffect/include/audio_enhance_chain_manager.h +++ b/frameworks/native/audioeffect/include/audio_enhance_chain_manager.h @@ -34,9 +34,9 @@ public: AudioEnhanceChainManager(); ~AudioEnhanceChainManager(); static AudioEnhanceChainManager* GetInstance(); - void InitAudioEnhanceChainManager(std::vector &enhanceChains, + void InitAudioEnhanceChainManager(const std::vector &enhanceChains, const EffectChainManagerParam &managerParam, - std::vector> &enhanceLibraryList); + const std::vector> &enhanceLibraryList); int32_t CreateAudioEnhanceChainDynamic(const uint64_t sceneKeyCode, const AudioEnhanceDeviceAttr &deviceAttr); int32_t ReleaseAudioEnhanceChainDynamic(const uint64_t sceneKeyCode); bool ExistAudioEnhanceChain(const uint64_t sceneKeyCode); @@ -89,9 +89,9 @@ private: void GetDeviceTypeName(DeviceType deviceType, std::string &deviceName); void GetDeviceNameByCaptureId(const uint32_t captureId, std::string &deviceName); // construct when init - void ConstructEnhanceChainMgrMaps(std::vector &enhanceChains, + void ConstructEnhanceChainMgrMaps(const std::vector &enhanceChains, const EffectChainManagerParam &managerParam, - std::vector> &enhanceLibraryList); + const std::vector> &enhanceLibraryList); void ConstructDeviceEnhances(); std::map> sceneTypeToEnhanceChainMap_; diff --git a/frameworks/native/audioeffect/libaudio_effect.versionscript b/frameworks/native/audioeffect/libaudio_effect.versionscript index 1f74ea8f0d7e14e3834733ef7b91d6326daa810d..fd74047e5086794e2e5d8d86dcef53abea32e896 100644 --- a/frameworks/native/audioeffect/libaudio_effect.versionscript +++ b/frameworks/native/audioeffect/libaudio_effect.versionscript @@ -77,6 +77,33 @@ *InitEffectBuffer*; *EffectChainManagerQueryHdiSupportedChannelLayout*; *LoadEffectProperties*; + *ApplyAudioEffectChain*; + *CheckAndAddSessionID*; + *UpdateSceneTypeList*; + *CreateAudioEffectChainDynamic*; + *SessionInfoMapAdd*; + *SessionInfoMapDelete*; + *UpdateMultichannelConfig*; + *UpdateDefaultAudioEffect*; + *UpdateStreamUsage*; + *CheckAndRemoveSessionID*; + *ReleaseAudioEffectChainDynamic*; + *CreateAudioEnhanceChainDynamic*; + *ReleaseAudioEnhanceChainDynamic*; + *GetOutputChannelInfo*; + *ReturnEffectChannelInfo*; + *ExistAudioEnhanceChain*; + *AudioEnhanceChainGetAlgoConfig*; + *IsEmptyEnhanceChain*; + *InitEnhanceBuffer*; + *CopyToEnhanceBuffer*; + *CopyEcToEnhanceBuffer*; + *CopyMicRefToEnhanceBuffer*; + *CopyFromEnhanceBuffer*; + *ApplyAudioEnhanceChain*; + *ApplyAudioEnhanceChainDefault*; + *SendInitCommand*; + *CheckProcessClusterInstances*; local: *; -}; +}; \ No newline at end of file diff --git a/frameworks/native/audioeffect/src/audio_effect_chain.cpp b/frameworks/native/audioeffect/src/audio_effect_chain.cpp index 1307c779363f00e0f2a3560d6fd00d1ead402b55..97527a71f1195308066c8be23b36fb4c1a386569 100644 --- a/frameworks/native/audioeffect/src/audio_effect_chain.cpp +++ b/frameworks/native/audioeffect/src/audio_effect_chain.cpp @@ -25,6 +25,7 @@ #include "securec.h" #include "media_monitor_manager.h" #include "audio_effect_map.h" +#include "audio_utils.h" namespace OHOS { namespace AudioStandard { diff --git a/frameworks/native/audioeffect/src/audio_effect_chain_adapter.cpp b/frameworks/native/audioeffect/src/audio_effect_chain_adapter.cpp index cd9e71e66a1edcd636c2c5961f0251f5e4c42eee..a7da688cc57b49853c78cd88d8728e3915d5874e 100644 --- a/frameworks/native/audioeffect/src/audio_effect_chain_adapter.cpp +++ b/frameworks/native/audioeffect/src/audio_effect_chain_adapter.cpp @@ -168,9 +168,8 @@ int32_t EffectChainManagerMultichannelUpdate(const char *sceneType) int32_t EffectChainManagerVolumeUpdate(const char *sessionID) { AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); - std::shared_ptr audioEffectVolume = AudioEffectVolume::GetInstance(); CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERR_INVALID_HANDLE, "null audioEffectChainManager"); - if (audioEffectChainManager->EffectVolumeUpdate(audioEffectVolume) != SUCCESS) { + if (audioEffectChainManager->EffectVolumeUpdate() != SUCCESS) { return ERROR; } return SUCCESS; @@ -240,15 +239,12 @@ int32_t EffectChainManagerAddSessionInfo(const char *sceneType, const char *sess std::string sceneTypeString = ""; std::string sessionIDString = ""; std::string sceneModeString = ""; - std::string spatializationEnabledString = ""; - if (sceneType && pack.channelLayout && sessionID && pack.sceneMode && - pack.spatializationEnabled && pack.streamUsage && pack.systemVolumeType) { + if (sceneType && pack.channelLayout && sessionID && pack.sceneMode && pack.streamUsage) { sceneTypeString = sceneType; channelLayoutNum = std::strtoull(pack.channelLayout, nullptr, BASE_TEN); sessionIDString = sessionID; sceneModeString = pack.sceneMode; - spatializationEnabledString = pack.spatializationEnabled; streamUsage = static_cast(std::strtol(pack.streamUsage, nullptr, BASE_TEN)); systemVolumeType = static_cast(std::strtol(pack.systemVolumeType, nullptr, BASE_TEN)); } else { @@ -261,7 +257,6 @@ int32_t EffectChainManagerAddSessionInfo(const char *sceneType, const char *sess info.sceneType = sceneTypeString; info.channels = pack.channels; info.channelLayout = channelLayoutNum; - info.spatializationEnabled = spatializationEnabledString; info.streamUsage = streamUsage; info.systemVolumeType = systemVolumeType; return audioEffectChainManager->SessionInfoMapAdd(sessionIDString, info); diff --git a/frameworks/native/audioeffect/src/audio_effect_chain_manager.cpp b/frameworks/native/audioeffect/src/audio_effect_chain_manager.cpp index 9cad4c8488da18bf7172ffdec67e034abdc2f6bf..4aab832921bd092a7053fa9da05a7acf2c1e04b7 100644 --- a/frameworks/native/audioeffect/src/audio_effect_chain_manager.cpp +++ b/frameworks/native/audioeffect/src/audio_effect_chain_manager.cpp @@ -20,11 +20,13 @@ #include "audio_effect.h" #include "audio_errors.h" #include "audio_effect_log.h" +#include "audio_utils.h" #include "securec.h" #include "system_ability_definition.h" #include "audio_setting_provider.h" #include "audio_device_type.h" #include "audio_effect_map.h" +#include "audio_effect_hdi_param.h" namespace OHOS { namespace AudioStandard { @@ -236,9 +238,9 @@ void AudioEffectChainManager::InitHdiStateInner() } // Boot initialize -void AudioEffectChainManager::InitAudioEffectChainManager(std::vector &effectChains, +void AudioEffectChainManager::InitAudioEffectChainManager(const std::vector &effectChains, const EffectChainManagerParam &effectChainManagerParam, - std::vector> &effectLibraryList) + const std::vector> &effectLibraryList) { std::lock_guard lock(dynamicMutex_); maxEffectChainCount_ = effectChainManagerParam.maxExtraNum + 1; @@ -252,9 +254,9 @@ void AudioEffectChainManager::InitAudioEffectChainManager(std::vector &effectChains, +void AudioEffectChainManager::ConstructEffectChainMgrMaps(const std::vector &effectChains, const EffectChainManagerParam &effectChainManagerParam, - std::vector> &effectLibraryList) + const std::vector> &effectLibraryList) { const std::unordered_map &map = effectChainManagerParam.sceneTypeToChainNameMap; std::set effectSet; @@ -413,6 +415,21 @@ bool AudioEffectChainManager::ExistAudioEffectChain(const std::string &sceneType return ExistAudioEffectChainInner(sceneType, effectMode); } +int32_t AudioEffectChainManager::GetOutputChannelInfo(const std::string &sceneType, + uint32_t &channels, uint64_t &channelLayout) +{ + std::lock_guard lock(dynamicMutex_); + std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName(); + + auto it = sceneTypeToEffectChainMap_.find(sceneTypeAndDeviceKey); + CHECK_AND_RETURN_RET_LOG(it != sceneTypeToEffectChainMap_.end() && it->second != nullptr, + ERROR, "effect chain not found for scene type: %{public}s", sceneTypeAndDeviceKey.c_str()); + + auto audioEffectChain = it->second; + audioEffectChain->UpdateBufferConfig(channels, channelLayout); + return SUCCESS; +} + int32_t AudioEffectChainManager::ApplyAudioEffectChain(const std::string &sceneType, std::unique_ptr &bufferAttr) { @@ -534,9 +551,10 @@ int32_t AudioEffectChainManager::SendEffectApVolume(std::shared_ptr audioEffectVolume) +int32_t AudioEffectChainManager::EffectVolumeUpdate() { std::lock_guard lock(dynamicMutex_); + std::shared_ptr audioEffectVolume = AudioEffectVolume::GetInstance(); return EffectVolumeUpdateInner(audioEffectVolume); } @@ -563,7 +581,7 @@ int32_t AudioEffectChainManager::SetEffectSystemVolume(const int32_t systemVolum AUDIO_INFO_LOG("systemVolumeType: %{public}d, systemVolume: %{public}f", systemVolumeType, audioEffectVolume->GetSystemVolume(systemVolumeType)); - return SUCCESS; + return EffectVolumeUpdateInner(audioEffectVolume); } #ifdef WINDOW_MANAGER_ENABLE @@ -730,8 +748,7 @@ int32_t AudioEffectChainManager::SessionInfoMapAdd(const std::string &sessionID, if (!sessionIDToEffectInfoMap_.count(sessionID)) { sceneTypeToSessionIDMap_[info.sceneType].insert(sessionID); sessionIDToEffectInfoMap_[sessionID] = info; - } else if (sessionIDToEffectInfoMap_[sessionID].sceneMode != info.sceneMode || - sessionIDToEffectInfoMap_[sessionID].spatializationEnabled != info.spatializationEnabled) { + } else if (sessionIDToEffectInfoMap_[sessionID].sceneMode != info.sceneMode) { sessionIDToEffectInfoMap_[sessionID] = info; } else { return ERROR; @@ -1738,6 +1755,47 @@ bool AudioEffectChainManager::IsEffectChainStop(const std::string &sceneType, co return true; } +ProcessClusterOperation AudioEffectChainManager::CheckProcessClusterInstances(const std::string &sceneType) +{ + std::lock_guard lock(dynamicMutex_); + CHECK_AND_RETURN_RET_LOG(sceneType != "SCENE_EXTRA", CREATE_EXTRA_PROCESSCLUSTER, "scene type is extra"); + CHECK_AND_RETURN_RET_LOG(!GetOffloadEnabled(), USE_NONE_PROCESSCLUSTER, "offload, use none processCluster"); + std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName(); + std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName(); + + if (sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) { + if (sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] == nullptr) { + AUDIO_WARNING_LOG("scene type %{public}s has null process cluster", sceneTypeAndDeviceKey.c_str()); + } else { + AUDIO_INFO_LOG("process cluster already exist, current count: %{public}d, default count: %{public}d", + sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey], defaultEffectChainCount_); + if (isDefaultEffectChainExisted_ && sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] == + sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey]) { + return USE_DEFAULT_PROCESSCLUSTER; + } + return NO_NEED_TO_CREATE_PROCESSCLUSTER; + } + } + + bool isPriorScene = std::find(priorSceneList_.begin(), priorSceneList_.end(), sceneType) != priorSceneList_.end(); + if (isPriorScene) { + AUDIO_INFO_LOG("create prior process cluster: %{public}s", sceneType.c_str()); + return CREATE_NEW_PROCESSCLUSTER; + } + if ((maxEffectChainCount_ - static_cast(sceneTypeToSpecialEffectSet_.size())) > 1) { + AUDIO_INFO_LOG("max audio process cluster count not reached, create special process cluster: %{public}s", + sceneType.c_str()); + return CREATE_NEW_PROCESSCLUSTER; + } else if (!isDefaultEffectChainExisted_) { + AUDIO_INFO_LOG("max audio process cluster count reached, create current and default process cluster"); + return CREATE_DEFAULT_PROCESSCLUSTER; + } else { + AUDIO_INFO_LOG("max audio process cluster count reached and default already exist: %{public}d", + defaultEffectChainCount_); + return USE_DEFAULT_PROCESSCLUSTER; + } +} + int32_t AudioEffectChainManager::QueryEffectChannelInfo(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout) { diff --git a/frameworks/native/audioeffect/src/audio_effect_hdi_param.cpp b/frameworks/native/audioeffect/src/audio_effect_hdi_param.cpp index 517216da0656ffccafc037931198bfcf98a83ea4..c21cded71b584f4d56adb5ac1a0c49ded0619621 100644 --- a/frameworks/native/audioeffect/src/audio_effect_hdi_param.cpp +++ b/frameworks/native/audioeffect/src/audio_effect_hdi_param.cpp @@ -16,7 +16,6 @@ #define LOG_TAG "AudioEffectHdiParam" #endif -#include "audio_effect.h" #include "audio_effect_hdi_param.h" #include "audio_errors.h" #include "audio_effect_log.h" diff --git a/frameworks/native/audioeffect/src/audio_enhance_chain_manager.cpp b/frameworks/native/audioeffect/src/audio_enhance_chain_manager.cpp index ed19b9991f02dc634fd9cc9f124fc4ee5eef1a6e..b7f990e201531151eca7e170d36eef0b90e1698e 100644 --- a/frameworks/native/audioeffect/src/audio_enhance_chain_manager.cpp +++ b/frameworks/native/audioeffect/src/audio_enhance_chain_manager.cpp @@ -137,8 +137,9 @@ void AudioEnhanceChainManager::ResetInfo() isMute_ = false; } -void AudioEnhanceChainManager::ConstructEnhanceChainMgrMaps(std::vector &enhanceChains, - const EffectChainManagerParam &managerParam, std::vector> &enhanceLibraryList) +void AudioEnhanceChainManager::ConstructEnhanceChainMgrMaps(const std::vector &enhanceChains, + const EffectChainManagerParam &managerParam, + const std::vector> &enhanceLibraryList) { std::set enhanceSet; for (EffectChain enhanceChain : enhanceChains) { @@ -240,8 +241,9 @@ void AudioEnhanceChainManager::UpdateEnhancePropertyMapFromDb(DeviceType deviceT } } -void AudioEnhanceChainManager::InitAudioEnhanceChainManager(std::vector &enhanceChains, - const EffectChainManagerParam &managerParam, std::vector> &enhanceLibraryList) +void AudioEnhanceChainManager::InitAudioEnhanceChainManager(const std::vector &enhanceChains, + const EffectChainManagerParam &managerParam, + const std::vector> &enhanceLibraryList) { std::lock_guard lock(chainManagerMutex_); normalSceneLimit_ = managerParam.maxExtraNum; diff --git a/frameworks/native/audioeffect/test/unittest/effect_unit_test/include/audio_effect_chain_manager_unit_test.h b/frameworks/native/audioeffect/test/unittest/effect_unit_test/include/audio_effect_chain_manager_unit_test.h index 2849371e57ba17d1ae2f49c8794b5f38ae681105..6c00fee03312c4c6f6064147b4a240d2b550032d 100644 --- a/frameworks/native/audioeffect/test/unittest/effect_unit_test/include/audio_effect_chain_manager_unit_test.h +++ b/frameworks/native/audioeffect/test/unittest/effect_unit_test/include/audio_effect_chain_manager_unit_test.h @@ -18,6 +18,7 @@ #include "gtest/gtest.h" #include "audio_effect_chain_manager.h" +#include "audio_effect_hdi_param.h" namespace OHOS { namespace AudioStandard { diff --git a/frameworks/native/audioeffect/test/unittest/effect_unit_test/src/audio_effect_chain_manager_unit_test.cpp b/frameworks/native/audioeffect/test/unittest/effect_unit_test/src/audio_effect_chain_manager_unit_test.cpp index 2be00c8fa8717764dbcfe95e94f9bdd6e1bb4baa..c1415b03eed4ce782064d718cd3e21c29d619971 100644 --- a/frameworks/native/audioeffect/test/unittest/effect_unit_test/src/audio_effect_chain_manager_unit_test.cpp +++ b/frameworks/native/audioeffect/test/unittest/effect_unit_test/src/audio_effect_chain_manager_unit_test.cpp @@ -64,7 +64,6 @@ SessionEffectInfo DEFAULT_INFO = { "SCENE_MOVIE", INFOCHANNELS, INFOCHANNELLAYOUT, - "0", }; } @@ -168,7 +167,6 @@ HWTEST(AudioEffectChainManagerUnitTest, CreateAudioEffectChainDynamic_005, TestS "SCENE_MOVIE", INFOCHANNELS, INFOCHANNELLAYOUT, - "0", 10, }; @@ -1365,7 +1363,7 @@ HWTEST(AudioEffectChainManagerUnitTest, EffectVolumeUpdate_001, TestSize.Level1) const std::string sessionIDString = "12345"; const float streamVolume = 0.5; audioEffectVolume->SetStreamVolume(sessionIDString, streamVolume); - int32_t ret = AudioEffectChainManager::GetInstance()->EffectVolumeUpdate(audioEffectVolume); + int32_t ret = AudioEffectChainManager::GetInstance()->EffectVolumeUpdate(); EXPECT_EQ(ret, SUCCESS); } @@ -1480,7 +1478,6 @@ HWTEST(AudioEffectChainManagerUnitTest, GetLatency_007, TestSize.Level1) AudioEffectChainManager::GetInstance()->deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP; AudioEffectChainManager::GetInstance()->spkOffloadEnabled_ = false; AudioEffectChainManager::GetInstance()->btOffloadEnabled_ = false; - AudioEffectChainManager::GetInstance()->sessionIDToEffectInfoMap_[sessionID].spatializationEnabled = "0"; uint32_t result = AudioEffectChainManager::GetInstance()->GetLatency(sessionID); EXPECT_EQ(0, result); AudioEffectChainManager::GetInstance()->ResetInfo(); @@ -1499,7 +1496,6 @@ HWTEST(AudioEffectChainManagerUnitTest, GetLatency_008, TestSize.Level1) AudioEffectChainManager::GetInstance()->deviceType_ = DEVICE_TYPE_SPEAKER; AudioEffectChainManager::GetInstance()->spkOffloadEnabled_ = false; AudioEffectChainManager::GetInstance()->btOffloadEnabled_ = false; - AudioEffectChainManager::GetInstance()->sessionIDToEffectInfoMap_[sessionID].spatializationEnabled = "0"; uint32_t result = AudioEffectChainManager::GetInstance()->GetLatency(sessionID); EXPECT_EQ(0, result); AudioEffectChainManager::GetInstance()->ResetInfo(); @@ -2161,7 +2157,6 @@ HWTEST(AudioEffectChainManagerUnitTest, SessionInfoMapAdd_003, TestSize.Level1) "SCENE_MOVIE", INFOCHANNELS, INFOCHANNELLAYOUT, - "0", }; ret = AudioEffectChainManager::GetInstance()->SessionInfoMapAdd(sessionID, info); EXPECT_EQ(ret, SUCCESS); @@ -2170,7 +2165,6 @@ HWTEST(AudioEffectChainManagerUnitTest, SessionInfoMapAdd_003, TestSize.Level1) "SCENE_MOVIE", INFOCHANNELS, INFOCHANNELLAYOUT, - "1", }; ret = AudioEffectChainManager::GetInstance()->SessionInfoMapAdd(sessionID, info2); EXPECT_EQ(ret, SUCCESS); @@ -2268,7 +2262,6 @@ HWTEST(AudioEffectChainManagerUnitTest, FindMaxSessionID_001, TestSize.Level1) "SCENE_MOVIE", INFOCHANNELS, INFOCHANNELLAYOUT, - "0", }; EXPECT_NE(AudioEffectChainManager::GetInstance(), nullptr); AudioEffectChainManager::GetInstance()->sessionIDToEffectInfoMap_[sessionID] = sessionEffectInfo; @@ -2294,7 +2287,6 @@ HWTEST(AudioEffectChainManagerUnitTest, FindMaxSessionID_002, TestSize.Level1) "SCENE_MOVIE", INFOCHANNELS, INFOCHANNELLAYOUT, - "0", }; EXPECT_NE(AudioEffectChainManager::GetInstance(), nullptr); diff --git a/frameworks/native/audioutils/include/audio_utils.h b/frameworks/native/audioutils/include/audio_utils.h index af3561cb4d70a90fcc093b09c61e642375d7c207..4717a75cf744e13bfa2bc5cfec2732f28b3589fb 100644 --- a/frameworks/native/audioutils/include/audio_utils.h +++ b/frameworks/native/audioutils/include/audio_utils.h @@ -218,6 +218,8 @@ bool SetSysPara(const std::string& key, int32_t value); template bool GetSysPara(const char *key, T &value); +int32_t GetEngineFlag(); + enum AudioDumpFileType { AUDIO_APP = 0, OTHER_NATIVE_SERVICE = 1, diff --git a/frameworks/native/audioutils/src/audio_utils.cpp b/frameworks/native/audioutils/src/audio_utils.cpp index 0a0b2ff46d94708e96e59f864a0d259236134e4e..05e8e30060401ca081d5bdd0ce2919a11d04e62e 100644 --- a/frameworks/native/audioutils/src/audio_utils.cpp +++ b/frameworks/native/audioutils/src/audio_utils.cpp @@ -1097,6 +1097,18 @@ template bool GetSysPara(const char *key, uint32_t &value); template bool GetSysPara(const char *key, int64_t &value); template bool GetSysPara(const char *key, std::string &value); +int32_t GetEngineFlag() +{ + std::string para = "const.multimedia.audio.proaudioEnable"; + static int32_t engineFlag = -1; + if (engineFlag == -1) { + bool res = GetSysPara(para.c_str(), engineFlag); + AUDIO_DEBUG_LOG("get %{public}s = %{public}d", para.c_str(), engineFlag); + CHECK_AND_RETURN_RET_LOG(res, engineFlag, "get %{public}s fail", para.c_str()); + } + return engineFlag; +} + std::map DumpFileUtil::g_lastPara = {}; FILE *DumpFileUtil::OpenDumpFileInner(std::string para, std::string fileName, AudioDumpFileType fileType) diff --git a/frameworks/native/hdiadapter/source/bluetooth/bluetooth_capturer_source.cpp b/frameworks/native/hdiadapter/source/bluetooth/bluetooth_capturer_source.cpp index 7381bf41dbd3799b072b9c1c9431b508045c1323..9547b8e2f31875c8deb8f3cd91bc2683cccae064 100644 --- a/frameworks/native/hdiadapter/source/bluetooth/bluetooth_capturer_source.cpp +++ b/frameworks/native/hdiadapter/source/bluetooth/bluetooth_capturer_source.cpp @@ -583,15 +583,18 @@ int32_t BluetoothCapturerSourceInner::SetInputRoute(DeviceType inputDevice, cons int32_t BluetoothCapturerSourceInner::SetAudioRouteInfoForEnhanceChain(const DeviceType &inputDevice, const std::string &deviceName) { - AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); - CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); uint32_t captureId = 0; int32_t ret = GetCaptureId(captureId); if (ret != SUCCESS) { AUDIO_WARNING_LOG("GetCaptureId failed"); } - audioEnhanceChainManager->SetInputDevice(captureId, inputDevice, deviceName); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag != 1) { + AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); + CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); + audioEnhanceChainManager->SetInputDevice(captureId, inputDevice, deviceName); + } return SUCCESS; } diff --git a/frameworks/native/hdiadapter/source/file/audio_capturer_file_source.cpp b/frameworks/native/hdiadapter/source/file/audio_capturer_file_source.cpp index aa40d129ae839c0452d1c6b625644cc25c51528d..d44ca4695c7f7f1ac38a01f5e1117d1a2b646ad9 100644 --- a/frameworks/native/hdiadapter/source/file/audio_capturer_file_source.cpp +++ b/frameworks/native/hdiadapter/source/file/audio_capturer_file_source.cpp @@ -132,6 +132,9 @@ void AudioCapturerFileSource::DeInit() int32_t AudioCapturerFileSource::Init(const IAudioSourceAttr &attr) { + if (attr.filePath == nullptr) { + return ERROR; + } const char *filePath = attr.filePath; char realPath[PATH_MAX + 1] = {0x00}; std::string sourceFilePath(filePath); diff --git a/frameworks/native/hdiadapter/source/primary/audio_capturer_source.cpp b/frameworks/native/hdiadapter/source/primary/audio_capturer_source.cpp index 2c8a1868332e41739e5bcc54cbefbe5a26ee71d2..89bbb8e92042b8a74bf755a476f4850734b29428 100644 --- a/frameworks/native/hdiadapter/source/primary/audio_capturer_source.cpp +++ b/frameworks/native/hdiadapter/source/primary/audio_capturer_source.cpp @@ -1765,17 +1765,20 @@ int32_t AudioCapturerSourceInner::SetAudioRouteInfoForEnhanceChain(const DeviceT AUDIO_ERR_LOG("non blocking source not support SetAudioRouteInfoForEnhanceChain"); return SUCCESS; } - AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); - CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); uint32_t captureId = 0; int32_t ret = GetCaptureId(captureId); if (ret != SUCCESS) { AUDIO_WARNING_LOG("GetCaptureId failed"); } - if (halName_ == "usb") { - audioEnhanceChainManager->SetInputDevice(captureId, DEVICE_TYPE_USB_ARM_HEADSET, deviceName); - } else { - audioEnhanceChainManager->SetInputDevice(captureId, inputDevice, deviceName); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag != 1) { + AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); + CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); + if (halName_ == "usb") { + audioEnhanceChainManager->SetInputDevice(captureId, DEVICE_TYPE_USB_ARM_HEADSET, deviceName); + } else { + audioEnhanceChainManager->SetInputDevice(captureId, inputDevice, deviceName); + } } return SUCCESS; } diff --git a/frameworks/native/hdiadapter_new/sink/file_audio_render_sink.cpp b/frameworks/native/hdiadapter_new/sink/file_audio_render_sink.cpp index c0b5c16e4d5a731f977c981e0897657f20891453..d898160895f7286d2eaf2c455897feeb6f9217f8 100644 --- a/frameworks/native/hdiadapter_new/sink/file_audio_render_sink.cpp +++ b/frameworks/native/hdiadapter_new/sink/file_audio_render_sink.cpp @@ -30,6 +30,9 @@ FileAudioRenderSink::~FileAudioRenderSink() int32_t FileAudioRenderSink::Init(const IAudioSinkAttr &attr) { + if (attr.filePath == nullptr) { + return ERROR; + } filePath_.assign(attr.filePath); return SUCCESS; } diff --git a/frameworks/native/hdiadapter_new/source/file_audio_capture_source.cpp b/frameworks/native/hdiadapter_new/source/file_audio_capture_source.cpp index dcc31f8d7ce1ed732fbf82ef8f73a91991539dc4..467f887c31a750bcb8235aff41c2fb6a8c408a79 100644 --- a/frameworks/native/hdiadapter_new/source/file_audio_capture_source.cpp +++ b/frameworks/native/hdiadapter_new/source/file_audio_capture_source.cpp @@ -30,6 +30,9 @@ FileAudioCaptureSource::~FileAudioCaptureSource() int32_t FileAudioCaptureSource::Init(const IAudioSourceAttr &attr) { + if (attr.filePath == nullptr) { + return ERROR; + } std::string filePath(attr.filePath); std::string dirPath; std::string fileName; diff --git a/interfaces/inner_api/native/audiocommon/include/audio_effect.h b/interfaces/inner_api/native/audiocommon/include/audio_effect.h index 0dcca3852f37a533089c60822341bac5d59edd51..d5c92769fde3f440fdfdee89984a35af42770620 100644 --- a/interfaces/inner_api/native/audiocommon/include/audio_effect.h +++ b/interfaces/inner_api/native/audiocommon/include/audio_effect.h @@ -32,6 +32,7 @@ namespace OHOS { namespace AudioStandard { // audio effect manager info constexpr int32_t AUDIO_EFFECT_COUNT_UPPER_LIMIT = 20; +constexpr uint32_t SEND_HDI_COMMAND_LEN = 20; enum HdiSetParamCommandCode { HDI_INIT = 0, @@ -195,6 +196,7 @@ enum AudioEnhanceScene { SCENE_PRE_ENHANCE = 2, SCENE_ASR = 4, SCENE_VOICE_MESSAGE = 5, + SCENE_NONE = 6, }; /** diff --git a/interfaces/inner_api/native/audiocommon/include/audio_engine_log.h b/interfaces/inner_api/native/audiocommon/include/audio_engine_log.h new file mode 100644 index 0000000000000000000000000000000000000000..c3da2f651ba4a53607c45566815b3624dca1d4bb --- /dev/null +++ b/interfaces/inner_api/native/audiocommon/include/audio_engine_log.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_HPAE_LOG_H +#define OHOS_HPAE_LOG_H + +#include "audio_log.h" + +#undef LOG_DOMAIN +#define LOG_DOMAIN 0xD002B88 +#endif // OHOS_HPAE_LOG_H diff --git a/interfaces/inner_api/native/audiocommon/include/audio_stream_info.h b/interfaces/inner_api/native/audiocommon/include/audio_stream_info.h index 640a4e4e53a65c116c66e81b486c1664a3a8194c..db72d5394a536af430965ba5533a5f67a36000a8 100644 --- a/interfaces/inner_api/native/audiocommon/include/audio_stream_info.h +++ b/interfaces/inner_api/native/audiocommon/include/audio_stream_info.h @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -584,6 +585,24 @@ struct AudioStreamData { int32_t volumeEnd; std::unordered_map isInnerCapeds; }; + +struct AudioCallBackStreamInfo { + uint64_t framePosition; + uint64_t framesWritten; + uint64_t timestamp; + uint64_t latency = 0; + int8_t *inputData; + size_t requestDataLen; + std::string deviceClass; + std::string deviceNetId; + bool needData; +}; + +struct AudioChannelInfo { + AudioChannelLayout channelLayout; + uint32_t numChannels; +}; + } // namespace AudioStandard } // namespace OHOS #endif // AUDIO_STREAM_INFO_H diff --git a/services/audio_policy/etc/audio_config.para b/services/audio_policy/etc/audio_config.para index 2d29a0d9298e442bf91949610d882207999fd110..998c038b056311fc76ddf6771d418dc80ade97ae 100644 --- a/services/audio_policy/etc/audio_config.para +++ b/services/audio_policy/etc/audio_config.para @@ -16,3 +16,4 @@ const.multimedia.audio.volumestep = 1 persist.multimedia.audio.safevolume = 15 persist.multimedia.audio.safevolume.timeout = 1140 persist.multimedia.audio.firstboot = 1 +const.multimedia.audio.proaudioEnable = 0 diff --git a/services/audio_policy/server/include/service/interface/iaudio_policy_interface.h b/services/audio_policy/server/include/service/interface/iaudio_policy_interface.h index 20a43dfde81dffc235debb62ffb321631dc2bd4a..553795d3b549fc544f7715fa9ff9e91d486fbadf 100644 --- a/services/audio_policy/server/include/service/interface/iaudio_policy_interface.h +++ b/services/audio_policy/server/include/service/interface/iaudio_policy_interface.h @@ -188,6 +188,13 @@ public: virtual int32_t SetDoubleRingVolumeDb(const AudioStreamType &streamType, const int32_t &volumeLevel) = 0; + virtual int32_t GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray) const = 0; + + virtual int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) const = 0; + + virtual int32_t GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray, + DeviceType deviceType = DEVICE_TYPE_NONE) const = 0; + virtual int32_t GetDeviceVolume(DeviceType deviceType, AudioStreamType streamType) = 0; virtual void SaveRingerModeInfo(AudioRingerMode ringMode, std::string callerName, std::string invocationTime) = 0; diff --git a/services/audio_policy/server/include/service/manager/audio_adapter_manager.h b/services/audio_policy/server/include/service/manager/audio_adapter_manager.h index 50af4fb63289c6b2774eb1fdc79ab35108e893de..606adb081436532d07ca8f71004d8df93e3d20a7 100644 --- a/services/audio_policy/server/include/service/manager/audio_adapter_manager.h +++ b/services/audio_policy/server/include/service/manager/audio_adapter_manager.h @@ -251,6 +251,13 @@ public: std::vector GetStreamVolumeInfo(AdjustStreamVolume volumeType); + int32_t GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray) const; + + int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) const; + + int32_t GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray, + DeviceType deviceType = DEVICE_TYPE_NONE) const; + int32_t GetDeviceVolume(DeviceType deviceType, AudioStreamType streamType); void UpdateSafeVolumeByS4(); @@ -361,7 +368,7 @@ private: return *reinterpret_cast(const_cast(&data[0])); } - std::unique_ptr audioServiceAdapter_; + std::shared_ptr audioServiceAdapter_; std::unordered_map minVolumeIndexMap_; std::unordered_map maxVolumeIndexMap_; std::mutex systemSoundMutex_; 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 2766fff277cfaa6c820d02cd4fb7bc02161f8f27..94b3724e52fb8db69f2654ae49462be583d2a849 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -1807,7 +1807,12 @@ int32_t AudioPolicyService::SetAudioEffectProperty(const AudioEffectPropertyArra int32_t AudioPolicyService::GetAudioEnhanceProperty(AudioEffectPropertyArrayV3 &propertyArray) { int32_t ret = AUDIO_OK; - ret = AudioServerProxy::GetInstance().GetAudioEffectPropertyProxy(propertyArray); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + return audioPolicyManager_.GetAudioEffectProperty(propertyArray); + } else { + ret = AudioServerProxy::GetInstance().GetAudioEffectPropertyProxy(propertyArray); + } CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ret, "get audio enhance property fail"); auto oIter = propertyArray.property.begin(); while (oIter != propertyArray.property.end()) { @@ -1822,7 +1827,12 @@ int32_t AudioPolicyService::GetAudioEnhanceProperty(AudioEffectPropertyArrayV3 & int32_t AudioPolicyService::GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray) { - return AudioServerProxy::GetInstance().GetAudioEffectPropertyProxy(propertyArray); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + return audioPolicyManager_.GetAudioEffectProperty(propertyArray); + } else { + return AudioServerProxy::GetInstance().GetAudioEffectPropertyProxy(propertyArray); + } } int32_t AudioPolicyService::GetSupportedAudioEffectProperty(AudioEffectPropertyArray &propertyArray) @@ -1873,7 +1883,12 @@ int32_t AudioPolicyService::SetAudioEffectProperty(const AudioEffectPropertyArra int32_t AudioPolicyService::GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) { - return AudioServerProxy::GetInstance().GetAudioEffectPropertyProxy(propertyArray); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + return audioPolicyManager_.GetAudioEffectProperty(propertyArray); + } else { + return AudioServerProxy::GetInstance().GetAudioEffectPropertyProxy(propertyArray); + } } int32_t AudioPolicyService::SetAudioEnhanceProperty(const AudioEnhancePropertyArray &propertyArray) @@ -1907,7 +1922,12 @@ int32_t AudioPolicyService::GetAudioEnhanceProperty(AudioEnhancePropertyArray &p int32_t AudioPolicyService::GetAudioEnhancePropertyByDevice(DeviceType deviceType, AudioEnhancePropertyArray &propertyArray) { - return AudioServerProxy::GetInstance().GetAudioEnhancePropertyProxy(propertyArray, deviceType); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + return audioPolicyManager_.GetAudioEnhanceProperty(propertyArray, deviceType); + } else { + return AudioServerProxy::GetInstance().GetAudioEnhancePropertyProxy(propertyArray, deviceType); + } } void AudioPolicyService::UpdateEffectBtOffloadSupported(const bool &isSupported) diff --git a/services/audio_policy/server/src/service/manager/audio_adapter_manager.cpp b/services/audio_policy/server/src/service/manager/audio_adapter_manager.cpp index 1cc5514c8ef9597c6266778ec06595d9b7de2830..4ed1a9406df46fa8e1fff3f70641c16d3b62d192 100644 --- a/services/audio_policy/server/src/service/manager/audio_adapter_manager.cpp +++ b/services/audio_policy/server/src/service/manager/audio_adapter_manager.cpp @@ -1022,13 +1022,21 @@ AudioIOHandle AudioAdapterManager::OpenAudioPort(std::shared_ptr AUDIO_INFO_LOG("Adapter load-module %{public}s, route flag: %{public}u", moduleArgs.c_str(), pipeInfo->routeFlag_); curActiveCount_++; AudioIOHandle ioHandle = HDI_INVALID_ID; - if (IsPaRoute(pipeInfo->routeFlag_)) { - AUDIO_INFO_LOG("Is pa route"); - return OpenPaAudioPort(pipeInfo, paIndex, moduleArgs); - } - AUDIO_INFO_LOG("Not pa route"); - return OpenNotPaAudioPort(pipeInfo, paIndex); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + ioHandle = audioServiceAdapter_->OpenAudioPort(pipeInfo->moduleInfo_.lib, pipeInfo->moduleInfo_); + paIndex = ioHandle; + return ioHandle; + } else { + if (IsPaRoute(pipeInfo->routeFlag_)) { + AUDIO_INFO_LOG("Is pa route"); + return OpenPaAudioPort(pipeInfo, paIndex, moduleArgs); + } + + AUDIO_INFO_LOG("Not pa route"); + return OpenNotPaAudioPort(pipeInfo, paIndex); + } } AudioIOHandle AudioAdapterManager::OpenPaAudioPort(std::shared_ptr pipeInfo, uint32_t &paIndex, @@ -1150,25 +1158,33 @@ AudioIOHandle AudioAdapterManager::OpenAudioPort(const AudioModuleInfo &audioMod curActiveCount_++; AudioIOHandle ioHandle = HDI_INVALID_ID; CHECK_AND_RETURN_RET_LOG(audioServerProxy_ != nullptr, ioHandle, "audioServerProxy_ null"); - std::string identity = IPCSkeleton::ResetCallingIdentity(); - if (audioModuleInfo.lib == "libmodule-inner-capturer-sink.z.so") { - std::string idInfo = audioModuleInfo.name; - IAudioSinkAttr attr = GetAudioSinkAttr(audioModuleInfo); - ioHandle = audioServerProxy_->CreateSinkPort(HDI_ID_BASE_RENDER, HDI_ID_TYPE_PRIMARY, idInfo, attr); + + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + ioHandle = audioServiceAdapter_->OpenAudioPort(audioModuleInfo.lib, audioModuleInfo); + paIndex = ioHandle; } else { - if (audioModuleInfo.role == HDI_AUDIO_PORT_SINK_ROLE) { - std::string idInfo = GetHdiSinkIdInfo(audioModuleInfo); + std::string identity = IPCSkeleton::ResetCallingIdentity(); + if (audioModuleInfo.lib == "libmodule-inner-capturer-sink.z.so") { + std::string idInfo = audioModuleInfo.name; IAudioSinkAttr attr = GetAudioSinkAttr(audioModuleInfo); - ioHandle = audioServerProxy_->CreateHdiSinkPort(audioModuleInfo.className, idInfo, attr); - } else if (audioModuleInfo.role == HDI_AUDIO_PORT_SOURCE_ROLE) { - std::string idInfo = GetHdiSourceIdInfo(audioModuleInfo); - IAudioSourceAttr attr = GetAudioSourceAttr(audioModuleInfo); - ioHandle = audioServerProxy_->CreateHdiSourcePort(audioModuleInfo.className, idInfo, attr); + ioHandle = audioServerProxy_->CreateSinkPort(HDI_ID_BASE_RENDER, HDI_ID_TYPE_PRIMARY, idInfo, attr); + } else { + if (audioModuleInfo.role == HDI_AUDIO_PORT_SINK_ROLE) { + std::string idInfo = GetHdiSinkIdInfo(audioModuleInfo); + IAudioSinkAttr attr = GetAudioSinkAttr(audioModuleInfo); + ioHandle = audioServerProxy_->CreateHdiSinkPort(audioModuleInfo.className, idInfo, attr); + } else if (audioModuleInfo.role == HDI_AUDIO_PORT_SOURCE_ROLE) { + std::string idInfo = GetHdiSourceIdInfo(audioModuleInfo); + IAudioSourceAttr attr = GetAudioSourceAttr(audioModuleInfo); + ioHandle = audioServerProxy_->CreateHdiSourcePort(audioModuleInfo.className, idInfo, attr); + } } + IPCSkeleton::SetCallingIdentity(identity); + + paIndex = audioServiceAdapter_->OpenAudioPort(audioModuleInfo.lib, moduleArgs.c_str()); } - IPCSkeleton::SetCallingIdentity(identity); - paIndex = audioServiceAdapter_->OpenAudioPort(audioModuleInfo.lib, moduleArgs.c_str()); AUDIO_INFO_LOG("Open %{public}u port, paIndex: %{public}u end.", ioHandle, paIndex); return ioHandle; } @@ -1194,6 +1210,36 @@ int32_t AudioAdapterManager::GetCurActivateCount() const return curActiveCount_ > 0 ? curActiveCount_ : 0; } +int32_t AudioAdapterManager::GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray) const +{ + CHECK_AND_RETURN_RET_LOG(audioServiceAdapter_ != nullptr, ERR_OPERATION_FAILED, "ServiceAdapter is null"); + int32_t ret = 0; + AudioEffectPropertyArrayV3 effectPropertyArray = {}; + ret = audioServiceAdapter_->GetAudioEffectProperty(effectPropertyArray); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "GetAudioEffectProperty failed"); + propertyArray.property.insert(propertyArray.property.end(), + effectPropertyArray.property.begin(), effectPropertyArray.property.end()); + AudioEffectPropertyArrayV3 enhancePropertyArray = {}; + ret = audioServiceAdapter_->GetAudioEnhanceProperty(enhancePropertyArray); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "GetAudioEnhanceProperty failed"); + propertyArray.property.insert(propertyArray.property.end(), + enhancePropertyArray.property.begin(), enhancePropertyArray.property.end()); + return ret; +} + +int32_t AudioAdapterManager::GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) const +{ + CHECK_AND_RETURN_RET_LOG(audioServiceAdapter_ != nullptr, ERR_OPERATION_FAILED, "ServiceAdapter is null"); + return audioServiceAdapter_->GetAudioEffectProperty(propertyArray); +} + +int32_t AudioAdapterManager::GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray, + DeviceType deviceType) const +{ + CHECK_AND_RETURN_RET_LOG(audioServiceAdapter_ != nullptr, ERR_OPERATION_FAILED, "ServiceAdapter is null"); + return audioServiceAdapter_->GetAudioEnhanceProperty(propertyArray, deviceType); +} + void UpdateSinkArgs(const AudioModuleInfo &audioModuleInfo, std::string &args) { if (!audioModuleInfo.name.empty()) { diff --git a/services/audio_service/BUILD.gn b/services/audio_service/BUILD.gn index 4949f8d05ec75af75d4322e265ac8ee6fb017847..aefed958526fe1fc48b63419da8cc614146a2ce4 100644 --- a/services/audio_service/BUILD.gn +++ b/services/audio_service/BUILD.gn @@ -308,6 +308,7 @@ config("audio_service_config") { "client/include", "server/include", "server/include/config", + "../audio_policy/server/include/service/common", "../audio_policy/common/definitions/include", "../audio_policy/server/include/service/common", "../audio_policy/server/include/service/effect", @@ -362,6 +363,9 @@ audio_ohos_library("audio_process_service") { "server/src/capturer_in_server.cpp", "server/src/core_service_handler.cpp", "server/src/core_service_provider_proxy.cpp", + "server/src/hpae_adapter_manager.cpp", + "server/src/hpae_capturer_stream_impl.cpp", + "server/src/hpae_renderer_stream_impl.cpp", "server/src/i_stream_manager.cpp", "server/src/ipc_offline_stream_stub.cpp", "server/src/ipc_stream_in_server.cpp", diff --git a/services/audio_service/server/include/audio_endpoint_private.h b/services/audio_service/server/include/audio_endpoint_private.h index d3300373eb70b14b6ed7d84d651e04671c7a8f95..bd8071dda7510069c8f1f192490882109175762b 100644 --- a/services/audio_service/server/include/audio_endpoint_private.h +++ b/services/audio_service/server/include/audio_endpoint_private.h @@ -39,6 +39,7 @@ public: virtual ~MockCallbacks() = default; void OnStatusUpdate(IOperation operation) override; int32_t OnWriteData(size_t length) override; + int32_t OnWriteData(int8_t *inputData, size_t requestDataLen) override; private: uint32_t streamIndex_ = 0; }; diff --git a/services/audio_service/server/include/capturer_in_server.h b/services/audio_service/server/include/capturer_in_server.h index fbc24d913bedfe86b1d255e10d9a79208823e4a2..572ebc9e7dd1c1e5ba4d81f4d729555dae3d4ac7 100644 --- a/services/audio_service/server/include/capturer_in_server.h +++ b/services/audio_service/server/include/capturer_in_server.h @@ -32,6 +32,7 @@ public: virtual ~CapturerInServer(); void OnStatusUpdate(IOperation operation) override; int32_t OnReadData(size_t length) override; + int32_t OnReadData(std::vector& outputData, size_t requestDataLen) override; int32_t ResolveBuffer(std::shared_ptr &buffer); int32_t GetSessionId(uint32_t &sessionId); diff --git a/services/audio_service/server/include/hpae_adapter_manager.h b/services/audio_service/server/include/hpae_adapter_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..ce86f15490c9c86fef8923c9217cc55d4cf6eaa9 --- /dev/null +++ b/services/audio_service/server/include/hpae_adapter_manager.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HPAE_ADAPTER_MANAGER_H +#define HPAE_ADAPTER_MANAGER_H + +#include +#include + +#include "audio_timer.h" +#include "i_stream_manager.h" +#include "audio_effect.h" + +namespace OHOS { +namespace AudioStandard { + +class HpaeAdapterManager : public IStreamManager { +public: + HpaeAdapterManager(ManagerType type); + + int32_t CreateRender(AudioProcessConfig processConfig, std::shared_ptr &stream) override; + int32_t ReleaseRender(uint32_t streamIndex_) override; + int32_t StartRender(uint32_t streamIndex) override; + int32_t StopRender(uint32_t streamIndex) override; + int32_t PauseRender(uint32_t streamIndex) override; + int32_t GetStreamCount() const noexcept override; + int32_t TriggerStartIfNecessary() override; + int32_t CreateCapturer(AudioProcessConfig processConfig, std::shared_ptr &stream) override; + int32_t ReleaseCapturer(uint32_t streamIndex_) override; + int32_t AddUnprocessStream(int32_t appUid) override; + uint64_t GetLatency() noexcept override; + void GetAllSinkInputs(std::vector &sinkInputs) override; +private: + int32_t GetDeviceNameForConnect(AudioProcessConfig processConfig, uint32_t sessionId, std::string &deviceName); + // audio channel index + std::shared_ptr CreateRendererStream(AudioProcessConfig processConfig, + const std::string &deviceName = ""); + std::shared_ptr CreateCapturerStream(AudioProcessConfig processConfig, + const std::string &deviceName = ""); + + ManagerType managerType_ = PLAYBACK; + std::mutex streamMapMutex_; + std::mutex paElementsMutex_; + std::map> rendererStreamMap_; + std::map> capturerStreamMap_; + std::mutex sinkInputsMutex_; + std::vector sinkInputs_; +}; +} // namespace AudioStandard +} // namespace OHOS +#endif // HPAE_ADAPTER_MANAGER_H diff --git a/services/audio_service/server/include/hpae_capturer_stream_impl.h b/services/audio_service/server/include/hpae_capturer_stream_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..c4576cca38d9a0a807f4ebe7a7d6a5be0992b60b --- /dev/null +++ b/services/audio_service/server/include/hpae_capturer_stream_impl.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HPAE_CAPTURER_STREAM_IMPL_H +#define HPAE_CAPTURER_STREAM_IMPL_H + +#include "i_capturer_stream.h" + +namespace OHOS { +namespace AudioStandard { +class HpaeCapturerStreamImpl : public std::enable_shared_from_this, public ICapturerStream { +public: + HpaeCapturerStreamImpl(AudioProcessConfig processConfig); + ~HpaeCapturerStreamImpl(); + int32_t InitParams(const std::string &deviceName = ""); + int32_t Start() override; + int32_t Pause(bool isStandby = false) override; + int32_t Flush() override; + int32_t Drain(bool stopFlag = false) override { return 0; }; + int32_t Stop() override; + int32_t Release() override; + int32_t GetStreamFramesRead(uint64_t &framesRead) override; + int32_t GetCurrentTimeStamp(uint64_t ×tamp) override; + int32_t GetLatency(uint64_t &latency) override; + + void RegisterStatusCallback(const std::weak_ptr &callback) override; + void RegisterReadCallback(const std::weak_ptr &callback) override; + BufferDesc DequeueBuffer(size_t length) override; + int32_t EnqueueBuffer(const BufferDesc &bufferDesc) override; + int32_t GetMinimumBufferSize(size_t &minBufferSize) const override; + void GetByteSizePerFrame(size_t &byteSizePerFrame) const override; + void GetSpanSizePerFrame(size_t &spanSizeInFrame) const override; + void SetStreamIndex(uint32_t index) override; + uint32_t GetStreamIndex() override; + int32_t DropBuffer() override; + void AbortCallback(int32_t abortTimes); + +private: + + uint32_t streamIndex_ = static_cast(-1); // invalid index + + AudioProcessConfig processConfig_ = {}; + std::weak_ptr statusCallback_; + std::weak_ptr readCallback_; + State state_ = INVALID; + + size_t byteSizePerFrame_ = 0; + size_t spanSizeInFrame_ = 0; + size_t minBufferSize_ = 0; + + size_t totalBytesRead_ = 0; + + FILE *capturerServerDumpFile_ = nullptr; + + // Only for debug + int32_t abortFlag_ = 0; + + uint32_t capturerId_ = 0; +}; +} // namespace AudioStandard +} // namespace OHOS +#endif // hpae_capturer_stream_impl_H diff --git a/services/audio_service/server/include/hpae_renderer_stream_impl.h b/services/audio_service/server/include/hpae_renderer_stream_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..da7d13b806ffa22b7b35adf4a750e191926c16d1 --- /dev/null +++ b/services/audio_service/server/include/hpae_renderer_stream_impl.h @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HPAE_RENDERER_STREAM_IMPL_H +#define HPAE_RENDERER_STREAM_IMPL_H + +#include +#include +#include "i_renderer_stream.h" + +namespace OHOS { +namespace AudioStandard { + +class HpaeRendererStreamImpl : public std::enable_shared_from_this, + public IStreamCallback, + public IRendererStream { +public: + HpaeRendererStreamImpl(AudioProcessConfig processConfig); + ~HpaeRendererStreamImpl(); + int32_t InitParams(const std::string &deviceName = ""); + int32_t Start() override; + int32_t Pause(bool isStandby = false) override; + int32_t Flush() override; + int32_t Drain(bool stopFlag = false) override; + int32_t Stop() override; + int32_t Release() override; + int32_t GetStreamFramesWritten(uint64_t &framesWritten) override; + int32_t GetCurrentTimeStamp(uint64_t ×tamp) override; + int32_t GetCurrentPosition(uint64_t &framePosition, uint64_t ×tamp, uint64_t &latency) override; + int32_t GetLatency(uint64_t &latency) override; + int32_t SetRate(int32_t rate) override; + int32_t SetAudioEffectMode(int32_t effectMode) override; + int32_t GetAudioEffectMode(int32_t &effectMode) override; + int32_t SetPrivacyType(int32_t privacyType) override; + int32_t GetPrivacyType(int32_t &privacyType) override; + + void RegisterStatusCallback(const std::weak_ptr &callback) override; + void RegisterWriteCallback(const std::weak_ptr &callback) override; + BufferDesc DequeueBuffer(size_t length) override; + int32_t EnqueueBuffer(const BufferDesc &bufferDesc) override; + int32_t GetMinimumBufferSize(size_t &minBufferSize) const override; + void GetByteSizePerFrame(size_t &byteSizePerFrame) const override; + void GetSpanSizePerFrame(size_t &spanSizeInFrame) const override; + void SetStreamIndex(uint32_t index) override; + uint32_t GetStreamIndex() override; + void AbortCallback(int32_t abortTimes); + // offload + int32_t SetOffloadMode(int32_t state, bool isAppBack) override; + int32_t UnsetOffloadMode() override; + int32_t GetOffloadApproximatelyCacheTime(uint64_t ×tamp, uint64_t &paWriteIndex, + uint64_t &cacheTimeDsp, uint64_t &cacheTimePa) override; + int32_t OffloadSetVolume(float volume) override; + size_t GetWritableSize() override; + int32_t UpdateMaxLength(uint32_t maxLength) override; + // offload end + + int32_t UpdateSpatializationState(bool spatializationEnabled, bool headTrackingEnabled) override; + int32_t Peek(std::vector *audioBuffer, int32_t &index) override; + int32_t ReturnIndex(int32_t index) override; + AudioProcessConfig GetAudioProcessConfig() const noexcept override; + int32_t SetClientVolume(float clientVolume) override; + void BlockStream() noexcept override; + int32_t OnStreamData(AudioCallBackStreamInfo& callBackStremInfo) override; +private: + void SyncOffloadMode(); + + uint32_t streamIndex_ = static_cast(-1); // invalid index + AudioProcessConfig processConfig_; + std::weak_ptr statusCallback_; + std::weak_ptr writeCallback_; + State state_ = INVALID; + + size_t byteSizePerFrame_ = 0; + size_t spanSizeInFrame_ = 0; + size_t minBufferSize_ = 0; + + int32_t renderRate_ = 0; + int32_t effectMode_ = -1; + int32_t privacyType_ = 0; + + float powerVolumeFactor_ = 1.0f; + // Only for debug + int32_t abortFlag_ = 0; + // offload + bool offloadEnable_ = false; + std::atomic offloadStatePolicy_ = OFFLOAD_DEFAULT; + // offload end + float clientVolume_ = 1.0f; + // latency position timeStamp + std::shared_mutex latencyMutex_; + uint64_t framePosition_ = 0; + uint64_t timestamp_ = 0; + uint64_t latency_ = 0; + uint64_t framesWritten_ = 0; + std::string deviceClass_; + std::string deviceNetId_; + // record latency +}; +} // namespace AudioStandard +} // namespace OHOS +#endif // pro_renderer_stream_impl_H diff --git a/services/audio_service/server/include/i_capturer_stream.h b/services/audio_service/server/include/i_capturer_stream.h index 94a5683873d334fcd3aa8a85af6a53bc2574c04d..67f964f2e0c31f5d2acd29665bf100c0c41580bf 100644 --- a/services/audio_service/server/include/i_capturer_stream.h +++ b/services/audio_service/server/include/i_capturer_stream.h @@ -24,6 +24,7 @@ namespace AudioStandard { class IReadCallback { public: virtual int32_t OnReadData(size_t length) = 0; + virtual int32_t OnReadData(std::vector& outputData, size_t requestDataLen) = 0; }; class ICapturerStream : public IStream { diff --git a/services/audio_service/server/include/i_renderer_stream.h b/services/audio_service/server/include/i_renderer_stream.h index 6277db77c2890c0946a630b8b9191ab15bc96fb7..f5f882dfc2cb754652dc667ebad3f4869a35a998 100644 --- a/services/audio_service/server/include/i_renderer_stream.h +++ b/services/audio_service/server/include/i_renderer_stream.h @@ -24,6 +24,12 @@ namespace AudioStandard { class IWriteCallback { public: virtual int32_t OnWriteData(size_t length) = 0; + virtual int32_t OnWriteData(int8_t *inputData, size_t requestDataLen) = 0; +}; + +class IStreamCallback { +public: + virtual int32_t OnStreamData(AudioCallBackStreamInfo& callBackStremInfo) = 0; }; class IRendererStream : public IStream { diff --git a/services/audio_service/server/include/renderer_in_server.h b/services/audio_service/server/include/renderer_in_server.h index 7302d6bbb406a8b81f714b65bd84d00e8b492bd2..261fcf41b6a90afb1fe1f9da21bc4c78aadce2f4 100644 --- a/services/audio_service/server/include/renderer_in_server.h +++ b/services/audio_service/server/include/renderer_in_server.h @@ -22,6 +22,7 @@ #include "oh_audio_buffer.h" #include "i_stream_manager.h" #include "audio_effect.h" +#include "audio_ring_cache.h" #include "player_dfx_writer.h" @@ -30,11 +31,16 @@ namespace AudioStandard { class StreamCallbacks : public IStatusCallback, public IWriteCallback { public: explicit StreamCallbacks(uint32_t streamIndex); - virtual ~StreamCallbacks() = default; + virtual ~StreamCallbacks(); void OnStatusUpdate(IOperation operation) override; int32_t OnWriteData(size_t length) override; + int32_t OnWriteData(int8_t *inputData, size_t requestDataLen) override; + std::unique_ptr& GetDupRingBuffer(); private: uint32_t streamIndex_ = 0; + FILE *dumpDupOut_ = nullptr; + std::string dumpDupOutFileName_ = ""; + std::unique_ptr dupRingBuffer_ = nullptr; }; class RendererInServer : public IStatusCallback, public IWriteCallback, @@ -47,6 +53,7 @@ public: void HandleOperationFlushed(); void HandleOperationStarted(); int32_t OnWriteData(size_t length) override; + int32_t OnWriteData(int8_t *inputData, size_t requestDataLen) override; int32_t ResolveBuffer(std::shared_ptr &buffer); int32_t GetSessionId(uint32_t &sessionId); @@ -91,6 +98,7 @@ public: int32_t EnableInnerCap(int32_t innerCapId); int32_t DisableInnerCap(int32_t innerCapId); int32_t InitDupStream(int32_t innerCapId); + std::unique_ptr& GetDupRingBuffer(); // for dual tone int32_t EnableDualTone(); @@ -128,8 +136,11 @@ private: bool ShouldEnableStandBy(); int32_t OffloadSetVolumeInner(); void InnerCaptureOtherStream(const BufferDesc &bufferDesc, CaptureInfo &captureInfo); + void InnerCaptureEnqueueBuffer(const BufferDesc &bufferDesc, CaptureInfo &captureInfo); int32_t StartInner(); int64_t GetLastAudioDuration(); + int32_t CreateDupBufferInner(int32_t innerCapId); + int32_t WriteDupBufferInner(const BufferDesc &bufferDesc); private: std::mutex statusLock_; @@ -145,8 +156,15 @@ private: // for inner-cap std::mutex dupMutex_; + size_t dupTotalSizeInFrame_ = 0; + size_t dupSpanSizeInFrame_ = 0; + size_t dupSpanSizeInByte_ = 0; + size_t dupByteSizePerFrame_ = 0; + FILE *dumpDupIn_ = nullptr; + std::string dumpDupInFileName_ = ""; std::shared_ptr dupStreamCallback_ = nullptr; std::unordered_map captureInfos_; + std::unique_ptr dupRingBuffer_ = nullptr; // for dual sink tone std::mutex dualToneMutex_; @@ -170,6 +188,10 @@ private: float oldAppliedVolume_ = MAX_FLOAT_VOLUME; std::mutex updateIndexLock_; int64_t startedTime_ = 0; + int64_t pausedTime_ = 0; + int64_t stopedTime_ = 0; + int64_t flushedTime_ = 0; + int64_t drainedTime_ = 0; uint32_t underrunCount_ = 0; std::atomic standByCounter_ = 0; int64_t enterStandbyTime_ = 0; diff --git a/services/audio_service/server/src/audio_endpoint.cpp b/services/audio_service/server/src/audio_endpoint.cpp index e486be805ea11aaeabe0f5dd39e4494032c5d9dd..0daf47391440aea3c3d27c7bb1931d98b6252a1f 100644 --- a/services/audio_service/server/src/audio_endpoint.cpp +++ b/services/audio_service/server/src/audio_endpoint.cpp @@ -178,6 +178,12 @@ int32_t MockCallbacks::OnWriteData(size_t length) return SUCCESS; } +int32_t MockCallbacks::OnWriteData(int8_t *inputData, size_t requestDataLen) +{ + Trace trace("DupStream::OnWriteData length " + std::to_string(requestDataLen)); + return SUCCESS; +} + bool AudioEndpointInner::ShouldInnerCap(int32_t innerCapId) { bool shouldBecapped = false; diff --git a/services/audio_service/server/src/audio_server.cpp b/services/audio_service/server/src/audio_server.cpp index b1b9e660d4754c06f9e49054e0ae22477fc429f2..5809737d9977e1c33da729c9c926e7867ef00e74 100644 --- a/services/audio_service/server/src/audio_server.cpp +++ b/services/audio_service/server/src/audio_server.cpp @@ -308,12 +308,16 @@ int32_t AudioServer::Dump(int32_t fd, const std::vector &args) argQue.push(args[index]); } std::string dumpString; - - AudioServerDump dumpObj; - int32_t res = dumpObj.Initialize(); - CHECK_AND_RETURN_RET_LOG(res == AUDIO_DUMP_SUCCESS, AUDIO_DUMP_INIT_ERR, - "Audio Service Dump Not initialised\n"); - dumpObj.AudioDataDump(dumpString, argQue); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE dump"); + } else { + AudioServerDump dumpObj; + int32_t res = dumpObj.Initialize(); + CHECK_AND_RETURN_RET_LOG(res == AUDIO_DUMP_SUCCESS, AUDIO_DUMP_INIT_ERR, + "Audio Service Dump Not initialised\n"); + dumpObj.AudioDataDump(dumpString, argQue); + } return write(fd, dumpString.c_str(), dumpString.size()); } @@ -348,15 +352,20 @@ void AudioServer::OnStart() } AddSystemAbilityListener(AUDIO_POLICY_SERVICE_ID); AddSystemAbilityListener(RES_SCHED_SYS_ABILITY_ID); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_INFO_LOG("HPAE IHpaeManager Init\n"); + } else { #ifdef PA - int32_t ret = pthread_create(&m_paDaemonThread, nullptr, AudioServer::paDaemonThread, nullptr); - pthread_setname_np(m_paDaemonThread, "OS_PaDaemon"); - if (ret != 0) { - AUDIO_ERR_LOG("pthread_create failed %d", ret); - WriteServiceStartupError(); - } - AUDIO_DEBUG_LOG("Created paDaemonThread\n"); + int32_t ret = pthread_create(&m_paDaemonThread, nullptr, AudioServer::paDaemonThread, nullptr); + pthread_setname_np(m_paDaemonThread, "OS_PaDaemon"); + if (ret != 0) { + AUDIO_ERR_LOG("pthread_create failed %d", ret); + WriteServiceStartupError(); + } + AUDIO_DEBUG_LOG("Created paDaemonThread\n"); #endif + } RegisterAudioCapturerSourceCallback(); RegisterAudioRendererSinkCallback(); diff --git a/services/audio_service/server/src/audio_server_effect.cpp b/services/audio_service/server/src/audio_server_effect.cpp index a6332800cbd1aea3360badec9b1f8b99ffebda9f..2e5f5592b4a878ad90322ea4166d46848f434e7e 100644 --- a/services/audio_service/server/src/audio_server_effect.cpp +++ b/services/audio_service/server/src/audio_server_effect.cpp @@ -22,6 +22,7 @@ #include "audio_enhance_chain_manager.h" #include "common/hdi_adapter_info.h" #include "manager/hdi_adapter_manager.h" +#include "audio_utils.h" namespace OHOS { namespace AudioStandard { @@ -30,19 +31,21 @@ using namespace std; void AudioServer::RecognizeAudioEffectType(const std::string &mainkey, const std::string &subkey, const std::string &extraSceneType) { - AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); - if (audioEffectChainManager == nullptr) { - AUDIO_ERR_LOG("audioEffectChainManager is nullptr"); - return; - } - audioEffectChainManager->UpdateParamExtra(mainkey, subkey, extraSceneType); - - AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); - if (audioEnhanceChainManager == nullptr) { - AUDIO_ERR_LOG("audioEnhanceChainManager is nullptr"); - return; + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE RecognizeAudioEffectType"); + } else { + AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); + if (audioEffectChainManager == nullptr) { + AUDIO_ERR_LOG("audioEffectChainManager is nullptr"); + return; + } + audioEffectChainManager->UpdateParamExtra(mainkey, subkey, extraSceneType); + + AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); + CHECK_AND_RETURN_LOG(audioEnhanceChainManager != nullptr, "audioEnhanceChainManager is nullptr"); + return audioEnhanceChainManager->UpdateExtraSceneType(mainkey, subkey, extraSceneType); } - audioEnhanceChainManager->UpdateExtraSceneType(mainkey, subkey, extraSceneType); } bool AudioServer::CreateEffectChainManager(std::vector &effectChains, @@ -52,12 +55,17 @@ bool AudioServer::CreateEffectChainManager(std::vector &effectChain AUDIO_ERR_LOG("not audio calling!"); return false; } - AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); - audioEffectChainManager->InitAudioEffectChainManager(effectChains, effectParam, - audioEffectServer_->GetEffectEntries()); - AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); - audioEnhanceChainManager->InitAudioEnhanceChainManager(effectChains, enhanceParam, - audioEffectServer_->GetEffectEntries()); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_INFO_LOG("HPAE AudioEffectChainManager Init"); + } else { + AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); + audioEffectChainManager->InitAudioEffectChainManager(effectChains, effectParam, + audioEffectServer_->GetEffectEntries()); + AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); + audioEnhanceChainManager->InitAudioEnhanceChainManager(effectChains, enhanceParam, + audioEffectServer_->GetEffectEntries()); + } return true; } @@ -68,8 +76,14 @@ void AudioServer::SetOutputDeviceSink(int32_t deviceType, std::string &sinkName) AUDIO_ERR_LOG("not audio calling!"); return; } - AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); - audioEffectChainManager->SetOutputDeviceSink(deviceType, sinkName); + + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE SetOutputDeviceSink"); + } else { + AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); + audioEffectChainManager->SetOutputDeviceSink(deviceType, sinkName); + } return; } @@ -77,12 +91,19 @@ int32_t AudioServer::UpdateSpatializationState(AudioSpatializationState spatiali { int32_t callingUid = IPCSkeleton::GetCallingUid(); CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_NOT_SUPPORTED, "refused for %{public}d", callingUid); - AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); - if (audioEffectChainManager == nullptr) { - AUDIO_ERR_LOG("audioEffectChainManager is nullptr"); - return ERROR; + + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE UpdateSpatializationState"); + return SUCCESS; + } else { + AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); + if (audioEffectChainManager == nullptr) { + AUDIO_ERR_LOG("audioEffectChainManager is nullptr"); + return ERROR; + } + return audioEffectChainManager->UpdateSpatializationState(spatializationState); } - return audioEffectChainManager->UpdateSpatializationState(spatializationState); } int32_t AudioServer::UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType) @@ -90,25 +111,33 @@ int32_t AudioServer::UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDevic int32_t callingUid = IPCSkeleton::GetCallingUid(); CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_NOT_SUPPORTED, "refused for %{public}d", callingUid); - AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); - CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr"); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE UpdateSpatialDeviceType"); + return SUCCESS; + } else { + AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); + CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr"); - return audioEffectChainManager->UpdateSpatialDeviceType(spatialDeviceType); + return audioEffectChainManager->UpdateSpatialDeviceType(spatialDeviceType); + } } int32_t AudioServer::SetSystemVolumeToEffect(const AudioStreamType streamType, float volume) { AudioVolumeType systemVolumeType = VolumeUtils::GetVolumeTypeFromStreamType(streamType); - AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); - CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr"); - AUDIO_INFO_LOG("streamType: %{public}d, systemVolume: %{public}f", streamType, volume); - audioEffectChainManager->SetEffectSystemVolume(systemVolumeType, volume); - - std::shared_ptr audioEffectVolume = AudioEffectVolume::GetInstance(); - CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume"); - audioEffectChainManager->EffectVolumeUpdate(audioEffectVolume); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE SetSystemVolumeToEffect"); + } else { + AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); + CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr"); + AUDIO_INFO_LOG("streamType: %{public}d, systemVolume: %{public}f", streamType, volume); + audioEffectChainManager->SetEffectSystemVolume(systemVolumeType, volume); + audioEffectChainManager->EffectVolumeUpdate(); + } return SUCCESS; } @@ -117,9 +146,15 @@ int32_t AudioServer::SetSpatializationSceneType(AudioSpatializationSceneType spa int32_t callingUid = IPCSkeleton::GetCallingUid(); CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_NOT_SUPPORTED, "refused for %{public}d", callingUid); - AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); - CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr"); - return audioEffectChainManager->SetSpatializationSceneType(spatializationSceneType); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE SetSpatializationSceneType"); + return SUCCESS; + } else { + AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); + CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr"); + return audioEffectChainManager->SetSpatializationSceneType(spatializationSceneType); + } } uint32_t AudioServer::GetEffectLatency(const std::string &sessionId) @@ -144,9 +179,14 @@ void AudioServer::LoadHdiEffectModel() int32_t callingUid = IPCSkeleton::GetCallingUid(); CHECK_AND_RETURN_LOG(PermissionUtil::VerifyIsAudio(), "load hdi effect model refused for %{public}d", callingUid); - AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); - CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "audioEffectChainManager is nullptr"); - audioEffectChainManager->InitHdiState(); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE LoadHdiEffectModel"); + } else { + AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); + CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "audioEffectChainManager is nullptr"); + audioEffectChainManager->InitHdiState(); + } } int32_t AudioServer::SetAudioEffectProperty(const AudioEffectPropertyArrayV3 &propertyArray, @@ -197,9 +237,16 @@ int32_t AudioServer::SetAudioEffectProperty(const AudioEffectPropertyArray &prop int32_t callingUid = IPCSkeleton::GetCallingUid(); CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED, "SetA udio Effect Property refused for %{public}d", callingUid); - AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); - CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr"); - return audioEffectChainManager->SetAudioEffectProperty(propertyArray); + + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE SetAudioEffectProperty"); + return SUCCESS; + } else { + AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); + CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr"); + return audioEffectChainManager->SetAudioEffectProperty(propertyArray); + } } int32_t AudioServer::GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) @@ -218,9 +265,15 @@ int32_t AudioServer::SetAudioEnhanceProperty(const AudioEnhancePropertyArray &pr int32_t callingUid = IPCSkeleton::GetCallingUid(); CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED, "Set Audio Enhance Property refused for %{public}d", callingUid); - AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); - CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); - return audioEnhanceChainManager->SetAudioEnhanceProperty(propertyArray, deviceType); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE SetAudioEnhanceProperty"); + return SUCCESS; + } else { + AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); + CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); + return audioEnhanceChainManager->SetAudioEnhanceProperty(propertyArray, deviceType); + } } int32_t AudioServer::GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray, @@ -236,17 +289,29 @@ int32_t AudioServer::GetAudioEnhanceProperty(AudioEnhancePropertyArray &property int32_t AudioServer::SetAudioEffectChainProperty(const AudioEffectPropertyArrayV3 &propertyArray) { - AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); - CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr"); - return audioEffectChainManager->SetAudioEffectProperty(propertyArray); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE SetAudioEffectProperty"); + return SUCCESS; + } else { + AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); + CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr"); + return audioEffectChainManager->SetAudioEffectProperty(propertyArray); + } } int32_t AudioServer::SetAudioEnhanceChainProperty(const AudioEffectPropertyArrayV3 &propertyArray, const DeviceType& deviceType) { - AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); - CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); - return audioEnhanceChainManager->SetAudioEnhanceProperty(propertyArray, deviceType); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE SetAudioEnhanceProperty"); + return SUCCESS; + } else { + AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); + CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); + return audioEnhanceChainManager->SetAudioEnhanceProperty(propertyArray, deviceType); + } } int32_t AudioServer::GetAudioEffectPropertyArray(AudioEffectPropertyArrayV3 &propertyArray) @@ -269,9 +334,14 @@ void AudioServer::UpdateEffectBtOffloadSupported(const bool &isSupported) int32_t callingUid = IPCSkeleton::GetCallingUid(); CHECK_AND_RETURN_LOG(PermissionUtil::VerifyIsAudio(), "refused for %{public}d", callingUid); - AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); - CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "audioEffectChainManager is nullptr"); - audioEffectChainManager->UpdateEffectBtOffloadSupported(isSupported); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE UpdateEffectBtOffloadSupported"); + } else { + AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); + CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "audioEffectChainManager is nullptr"); + audioEffectChainManager->UpdateEffectBtOffloadSupported(isSupported); + } } void AudioServer::SetRotationToEffect(const uint32_t rotate) @@ -279,9 +349,14 @@ void AudioServer::SetRotationToEffect(const uint32_t rotate) int32_t callingUid = IPCSkeleton::GetCallingUid(); CHECK_AND_RETURN_LOG(PermissionUtil::VerifyIsAudio(), "set rotation to effect refused for %{public}d", callingUid); - AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); - CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "audioEffectChainManager is nullptr"); - audioEffectChainManager->EffectRotationUpdate(rotate); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE EffectRotationUpdate"); + } else { + AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance(); + CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "audioEffectChainManager is nullptr"); + audioEffectChainManager->EffectRotationUpdate(rotate); + } std::string value = "rotation=" + std::to_string(rotate); HdiAdapterManager &manager = HdiAdapterManager::GetInstance(); @@ -292,8 +367,6 @@ void AudioServer::SetRotationToEffect(const uint32_t rotate) int32_t AudioServer::SetVolumeInfoForEnhanceChain(const AudioStreamType &streamType) { - AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); - CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); AudioVolumeType volumeType = VolumeUtils::GetVolumeTypeFromStreamType(streamType); DeviceType deviceType = PolicyHandler::GetInstance().GetActiveOutPutDevice(); Volume vol = {false, 0.0f, 0}; @@ -303,14 +376,29 @@ int32_t AudioServer::SetVolumeInfoForEnhanceChain(const AudioStreamType &streamT PolicyHandler::GetInstance().GetActiveOutPutDevice() == DEVICE_TYPE_BLUETOOTH_A2DP) { systemVol = 1.0f; // 1.0f for a2dp abs volume } - return audioEnhanceChainManager->SetVolumeInfo(volumeType, systemVol); + + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE SetVolumeInfo"); + return SUCCESS; + } else { + AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); + CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); + return audioEnhanceChainManager->SetVolumeInfo(volumeType, systemVol); + } } int32_t AudioServer::SetMicrophoneMuteForEnhanceChain(const bool &isMute) { - AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); - CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); - return audioEnhanceChainManager->SetMicrophoneMuteInfo(isMute); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE SetMicrophoneMuteInfo"); + return SUCCESS; + } else { + AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); + CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); + return audioEnhanceChainManager->SetMicrophoneMuteInfo(isMute); + } } bool AudioServer::LoadAudioEffectLibraries(const std::vector libraries, const std::vector effects, diff --git a/services/audio_service/server/src/capturer_in_server.cpp b/services/audio_service/server/src/capturer_in_server.cpp index f06d31ad1cf3ad94814374ba8b5764154d5cbc60..1b8ccf4ef99ed5712dae7d0b5c30ed78cb9f24e4 100644 --- a/services/audio_service/server/src/capturer_in_server.cpp +++ b/services/audio_service/server/src/capturer_in_server.cpp @@ -229,8 +229,11 @@ bool CapturerInServer::IsReadDataOverFlow(size_t length, uint64_t currentWriteFr overFlowLogFlag_ = 0; } overFlowLogFlag_++; - BufferDesc dstBuffer = stream_->DequeueBuffer(length); - stream_->EnqueueBuffer(dstBuffer); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag != 1) { + BufferDesc dstBuffer = stream_->DequeueBuffer(length); + stream_->EnqueueBuffer(dstBuffer); + } stateListener->OnOperationHandled(UPDATE_STREAM, currentWriteFrame); return true; } @@ -295,6 +298,57 @@ int32_t CapturerInServer::OnReadData(size_t length) return SUCCESS; } +int32_t CapturerInServer::OnReadData(std::vector& outputData, size_t requestDataLen) +{ + CHECK_AND_RETURN_RET_LOG(requestDataLen >= spanSizeInBytes_, ERR_READ_FAILED, + "Length %{public}zu is less than spanSizeInBytes %{public}zu", requestDataLen, spanSizeInBytes_); + std::shared_ptr stateListener = streamListener_.lock(); + CHECK_AND_RETURN_RET_LOG(stateListener != nullptr, ERR_READ_FAILED, "IStreamListener is nullptr"); + uint64_t currentWriteFrame = audioServerBuffer_->GetCurWriteFrame(); + if (IsReadDataOverFlow(requestDataLen, currentWriteFrame, stateListener)) { + return ERR_READ_FAILED; + } + Trace trace("CapturerInServer::ReadData:" + std::to_string(currentWriteFrame)); + OptResult result = ringCache_->GetWritableSize(); + CHECK_AND_RETURN_RET_LOG(result.ret == OPERATION_SUCCESS, ERR_READ_FAILED, + "RingCache write invalid size %{public}zu", result.size); + + BufferDesc srcBuffer = {nullptr, requestDataLen, 0}; + srcBuffer.buffer = reinterpret_cast(outputData.data()); + + ringCache_->Enqueue({srcBuffer.buffer, srcBuffer.bufLength}); + result = ringCache_->GetReadableSize(); + if (result.ret != OPERATION_SUCCESS || result.size < spanSizeInBytes_) { + return SUCCESS; + } + + BufferDesc dstBuffer = {nullptr, 0, 0}; + uint64_t curWritePos = audioServerBuffer_->GetCurWriteFrame(); + if (audioServerBuffer_->GetWriteBuffer(curWritePos, dstBuffer) < 0) { + return ERR_READ_FAILED; + } + if ((processConfig_.capturerInfo.sourceType == SOURCE_TYPE_PLAYBACK_CAPTURE && processConfig_.innerCapMode == + LEGACY_MUTE_CAP) || muteFlag_) { + dstBuffer.buffer = dischargeBuffer_.get(); // discharge valid data. + } + if (muteFlag_) { + memset_s(static_cast(dstBuffer.buffer), dstBuffer.bufLength, 0, dstBuffer.bufLength); + } + ringCache_->Dequeue({dstBuffer.buffer, dstBuffer.bufLength}); + if (AudioDump::GetInstance().GetVersionType() == DumpFileUtil::BETA_VERSION) { + DumpFileUtil::WriteDumpFile(dumpS2C_, static_cast(dstBuffer.buffer), dstBuffer.bufLength); + AudioCacheMgr::GetInstance().CacheData(dumpFileName_, + static_cast(dstBuffer.buffer), dstBuffer.bufLength); + } + + uint64_t nextWriteFrame = currentWriteFrame + spanSizeInFrame_; + audioServerBuffer_->SetCurWriteFrame(nextWriteFrame); + audioServerBuffer_->SetHandleInfo(currentWriteFrame, ClockTime::GetCurNano()); + + stateListener->OnOperationHandled(UPDATE_STREAM, currentWriteFrame); + return SUCCESS; +} + int32_t CapturerInServer::UpdateReadIndex() { AUDIO_DEBUG_LOG("audioServerBuffer_->GetAvailableDataFrames(): %{public}d, needStart: %{public}d", diff --git a/services/audio_service/server/src/hpae_adapter_manager.cpp b/services/audio_service/server/src/hpae_adapter_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4b4d62832486400cb2cf966152a2bff1ea01592 --- /dev/null +++ b/services/audio_service/server/src/hpae_adapter_manager.cpp @@ -0,0 +1,289 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef LOG_TAG +#define LOG_TAG "HpaeAdapterManager" +#endif + +#include "hpae_adapter_manager.h" +#include +#include +#include "audio_common_log.h" +#include "audio_errors.h" +#include "hpae_renderer_stream_impl.h" +#include "hpae_capturer_stream_impl.h" +#include "audio_utils.h" +#include "audio_info.h" +#include "policy_handler.h" +namespace OHOS { +namespace AudioStandard { + +const char* PRO_INNER_CAPTURER_SOURCE = "Speaker.monitor"; +const char* PRO_NEW_INNER_CAPTURER_SOURCE = "InnerCapturerSink.monitor"; +const char* PRO_MONITOR_SOURCE_SUFFIX = ".monitor"; + +HpaeAdapterManager::HpaeAdapterManager(ManagerType type) +{ + AUDIO_INFO_LOG("Constructor with type:%{public}d", type); + managerType_ = type; +} + +int32_t HpaeAdapterManager::CreateRender(AudioProcessConfig processConfig, std::shared_ptr &stream) +{ + AUDIO_DEBUG_LOG("Create renderer start"); + uint32_t sessionId = 0; + sessionId = processConfig.originalSessionId; + if (managerType_ == DUP_PLAYBACK || + processConfig.originalSessionId < MIN_STREAMID || processConfig.originalSessionId > MAX_STREAMID) { + sessionId = PolicyHandler::GetInstance().GenerateSessionId(processConfig.appInfo.appUid); + AUDIO_ERR_LOG("Create [%{public}d] type renderer:[%{public}u] error", + managerType_, processConfig.originalSessionId); + } + AUDIO_INFO_LOG("Create [%{public}d] type renderer:[%{public}u]", managerType_, sessionId); + std::string deviceName; + int32_t ret = GetDeviceNameForConnect(processConfig, processConfig.originalSessionId, deviceName); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR_INVALID_PARAM, "getdevicename err: %{public}d", ret); + + processConfig.originalSessionId = sessionId; + // HpaeAdapterManager is solely responsible for creating paStream objects + std::shared_ptr rendererStream = CreateRendererStream(processConfig, deviceName); + CHECK_AND_RETURN_RET_LOG(rendererStream != nullptr, ERR_DEVICE_INIT, "Failed to init pa stream"); + rendererStream->SetStreamIndex(sessionId); + std::lock_guard lock(streamMapMutex_); + rendererStreamMap_[sessionId] = rendererStream; + stream = rendererStream; + + std::lock_guard mutex(sinkInputsMutex_); + SinkInput sinkInput; + sinkInput.streamId = sessionId; + sinkInput.streamType = processConfig.streamType; + sinkInput.uid = processConfig.appInfo.appUid; + sinkInput.pid = processConfig.appInfo.appPid; + sinkInput.paStreamId = sessionId; + sinkInputs_.push_back(sinkInput); + return SUCCESS; +} + +int32_t HpaeAdapterManager::ReleaseRender(uint32_t streamIndex) +{ + AUDIO_DEBUG_LOG("Release [%{public}d] type render:[%{public}u]", managerType_, streamIndex); + std::unique_lock lock(streamMapMutex_); + auto it = rendererStreamMap_.find(streamIndex); + if (it == rendererStreamMap_.end()) { + AUDIO_WARNING_LOG("No matching stream"); + return SUCCESS; + } + std::shared_ptr currentRender = rendererStreamMap_[streamIndex]; + rendererStreamMap_[streamIndex] = nullptr; + rendererStreamMap_.erase(streamIndex); + lock.unlock(); + + if (currentRender->Release() < 0) { + AUDIO_WARNING_LOG("Release stream %{public}d failed", streamIndex); + return ERR_OPERATION_FAILED; + } + + AUDIO_INFO_LOG("rendererStreamMap_.size() : %{public}zu", rendererStreamMap_.size()); + if (rendererStreamMap_.size() == 0) { + AUDIO_INFO_LOG("Release the last stream"); + } + + std::lock_guard mutex(sinkInputsMutex_); + sinkInputs_.erase( + std::remove_if(sinkInputs_.begin(), + sinkInputs_.end(), + [&](const SinkInput &sinkInput) { return static_cast(sinkInput.streamId) == streamIndex; }), + sinkInputs_.end()); + return SUCCESS; +} + +int32_t HpaeAdapterManager::StartRender(uint32_t streamIndex) +{ + AUDIO_DEBUG_LOG("Enter StartRender"); + std::lock_guard lock(streamMapMutex_); + auto it = rendererStreamMap_.find(streamIndex); + if (it == rendererStreamMap_.end()) { + AUDIO_WARNING_LOG("No matching stream"); + return SUCCESS; + } + return rendererStreamMap_[streamIndex]->Start(); +} + +int32_t HpaeAdapterManager::StopRender(uint32_t streamIndex) +{ + AUDIO_DEBUG_LOG("Enter StopRender"); + std::lock_guard lock(streamMapMutex_); + auto it = rendererStreamMap_.find(streamIndex); + if (it == rendererStreamMap_.end()) { + AUDIO_WARNING_LOG("No matching stream"); + return SUCCESS; + } + return rendererStreamMap_[streamIndex]->Stop(); +} + +int32_t HpaeAdapterManager::PauseRender(uint32_t streamIndex) +{ + AUDIO_DEBUG_LOG("Enter PauseRender"); + std::lock_guard lock(streamMapMutex_); + auto it = rendererStreamMap_.find(streamIndex); + if (it == rendererStreamMap_.end()) { + AUDIO_WARNING_LOG("No matching stream"); + return SUCCESS; + } + rendererStreamMap_[streamIndex]->Pause(); + return SUCCESS; +} + +int32_t HpaeAdapterManager::TriggerStartIfNecessary() +{ + return SUCCESS; +} + +int32_t HpaeAdapterManager::GetStreamCount() const noexcept +{ + if (managerType_ == RECORDER) { + return capturerStreamMap_.size(); + } else { + return rendererStreamMap_.size(); + } +} + +int32_t HpaeAdapterManager::GetDeviceNameForConnect(AudioProcessConfig processConfig, uint32_t sessionId, + std::string &deviceName) +{ + deviceName = ""; + if (processConfig.audioMode == AUDIO_MODE_RECORD) { + if (processConfig.isWakeupCapturer) { + int32_t ret = PolicyHandler::GetInstance().SetWakeUpAudioCapturerFromAudioServer(processConfig); + if (ret < 0) { + AUDIO_ERR_LOG("ErrorCode: %{public}d", ret); + return ERROR; + } + deviceName = PRIMARY_WAKEUP; + } + if (processConfig.isInnerCapturer) { + if (processConfig.innerCapMode == MODERN_INNER_CAP) { + deviceName = std::string(INNER_CAPTURER_SINK) + std::to_string(processConfig.innerCapId); + } else { + deviceName = PRO_INNER_CAPTURER_SOURCE; + } + } else if (processConfig.capturerInfo.sourceType == SOURCE_TYPE_REMOTE_CAST) { + deviceName = std::string(REMOTE_CAST_INNER_CAPTURER_SINK_NAME) + std::string(PRO_MONITOR_SOURCE_SUFFIX); + } + return PolicyHandler::GetInstance().NotifyCapturerAdded(processConfig.capturerInfo, + processConfig.streamInfo, sessionId); + } else if (managerType_ == DUP_PLAYBACK) { + deviceName = std::string(INNER_CAPTURER_SINK) + std::to_string(processConfig.innerCapId); + } + return SUCCESS; +} + +int32_t HpaeAdapterManager::CreateCapturer(AudioProcessConfig processConfig, std::shared_ptr &stream) +{ + AUDIO_DEBUG_LOG("Create capturer start"); + CHECK_AND_RETURN_RET_LOG(managerType_ == RECORDER, ERROR, "Invalid managerType:%{public}d", managerType_); + uint32_t sessionId = 0; + if (processConfig.originalSessionId < MIN_STREAMID || processConfig.originalSessionId > MAX_STREAMID) { + sessionId = PolicyHandler::GetInstance().GenerateSessionId(processConfig.appInfo.appUid); + AUDIO_ERR_LOG("Create capturer originalSessionId is error %{public}d", processConfig.originalSessionId); + } else { + sessionId = processConfig.originalSessionId; + } + processConfig.originalSessionId = sessionId; + std::string deviceName; + int32_t ret = GetDeviceNameForConnect(processConfig, processConfig.originalSessionId, deviceName); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR_INVALID_PARAM, "getdevicename err: %{public}d", ret); + + // HpaeAdapterManager is solely responsible for creating paStream objects + std::shared_ptr capturerStream = CreateCapturerStream(processConfig, deviceName); + CHECK_AND_RETURN_RET_LOG(capturerStream != nullptr, ERR_DEVICE_INIT, "Failed to init pa stream"); + capturerStream->SetStreamIndex(sessionId); + std::lock_guard lock(streamMapMutex_); + capturerStreamMap_[sessionId] = capturerStream; + stream = capturerStream; + return SUCCESS; +} + +int32_t HpaeAdapterManager::AddUnprocessStream(int32_t appUid) +{ + return SUCCESS; +} + +int32_t HpaeAdapterManager::ReleaseCapturer(uint32_t streamIndex) +{ + AUDIO_DEBUG_LOG("Enter ReleaseCapturer"); + std::lock_guard lock(streamMapMutex_); + auto it = capturerStreamMap_.find(streamIndex); + if (it == capturerStreamMap_.end()) { + AUDIO_WARNING_LOG("No matching stream"); + return SUCCESS; + } + + if (capturerStreamMap_[streamIndex]->Release() < 0) { + AUDIO_WARNING_LOG("Release stream %{public}d failed", streamIndex); + return ERR_OPERATION_FAILED; + } + + capturerStreamMap_[streamIndex] = nullptr; + capturerStreamMap_.erase(streamIndex); + if (capturerStreamMap_.size() == 0) { + AUDIO_INFO_LOG("Release the last stream"); + } + return SUCCESS; +} + +std::shared_ptr HpaeAdapterManager::CreateRendererStream(AudioProcessConfig processConfig, + const std::string &deviceName) +{ + std::lock_guard lock(paElementsMutex_); + if (managerType_ == DUP_PLAYBACK) { + // todo check + processConfig.isInnerCapturer = true; + AUDIO_INFO_LOG("Create dup playback renderer stream"); + } + std::shared_ptr rendererStream = + std::make_shared(processConfig); + if (rendererStream->InitParams(deviceName) != SUCCESS) { + AUDIO_ERR_LOG("Create rendererStream Failed"); + return nullptr; + } + return rendererStream; +} + +std::shared_ptr HpaeAdapterManager::CreateCapturerStream(AudioProcessConfig processConfig, + const std::string &deviceName) +{ + std::lock_guard lock(paElementsMutex_); + std::shared_ptr capturerStream = + std::make_shared(processConfig); + if (capturerStream->InitParams(deviceName) != SUCCESS) { + AUDIO_ERR_LOG("Create capturerStream Failed, error"); + return nullptr; + } + return capturerStream; +} + +uint64_t HpaeAdapterManager::GetLatency() noexcept +{ + return 0; +} + +void HpaeAdapterManager::GetAllSinkInputs(std::vector &sinkInputs) +{ + std::lock_guard lock(paElementsMutex_); + sinkInputs = sinkInputs_; + return; +} +} // namespace AudioStandard +} // namespace OHOS diff --git a/services/audio_service/server/src/hpae_capturer_stream_impl.cpp b/services/audio_service/server/src/hpae_capturer_stream_impl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d767cfcde5cb42f6e7f42ed614c85615b390d50b --- /dev/null +++ b/services/audio_service/server/src/hpae_capturer_stream_impl.cpp @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef LOG_TAG +#define LOG_TAG "HpaeCapturerStreamImpl" +#endif + +#include "safe_map.h" +#include "hpae_capturer_stream_impl.h" +#include "audio_errors.h" +#include "audio_capturer_log.h" +#include "audio_utils.h" +#include "policy_handler.h" +#include +#include + +namespace OHOS { +namespace AudioStandard { +static SafeMap> paCapturerMap_; +const int32_t MIN_BUFFER_SIZE = 2; +const int32_t FRAME_LEN_ON_MS = 20; +const int32_t MSEC_PER_SEC = 1000; + +static inline int32_t GetSizeFromFormat(int32_t format) +{ + return format != SAMPLE_F32LE ? ((format) + 1) : (4); // float 4 +} + +HpaeCapturerStreamImpl::HpaeCapturerStreamImpl(AudioProcessConfig processConfig) +{ + processConfig_ = processConfig; + spanSizeInFrame_ = static_cast(FRAME_LEN_ON_MS * + (static_cast(processConfig.streamInfo.samplingRate) / MSEC_PER_SEC)); + byteSizePerFrame_ = (processConfig.streamInfo.channels * GetSizeFromFormat(processConfig.streamInfo.format)); + minBufferSize_ = MIN_BUFFER_SIZE * byteSizePerFrame_ * spanSizeInFrame_; +} + +HpaeCapturerStreamImpl::~HpaeCapturerStreamImpl() +{ + AUDIO_DEBUG_LOG("~HpaeCapturerStreamImpl"); + if (capturerServerDumpFile_) { + fclose(capturerServerDumpFile_); + capturerServerDumpFile_ = nullptr; + } + paCapturerMap_.Erase(this); +} + +int32_t HpaeCapturerStreamImpl::InitParams(const std::string &deviceName) +{ + paCapturerMap_.Insert(this, weak_from_this()); + + return SUCCESS; +} + +int32_t HpaeCapturerStreamImpl::Start() +{ + AUDIO_INFO_LOG("Start"); + state_ = RUNNING; + return SUCCESS; +} + +int32_t HpaeCapturerStreamImpl::Pause(bool isStandby) +{ + AUDIO_INFO_LOG("Pause"); + return SUCCESS; +} + +int32_t HpaeCapturerStreamImpl::GetStreamFramesRead(uint64_t &framesRead) +{ + // to do callback data report + framesRead = 0; + return SUCCESS; +} + +int32_t HpaeCapturerStreamImpl::GetCurrentTimeStamp(uint64_t ×tamp) +{ + timestamp = 0; + return SUCCESS; +} + +int32_t HpaeCapturerStreamImpl::GetLatency(uint64_t &latency) +{ + latency = 0; + AUDIO_DEBUG_LOG("total latency: %{public}" PRIu64 "ms", latency); + return SUCCESS; +} + +int32_t HpaeCapturerStreamImpl::Flush() +{ + AUDIO_INFO_LOG("Flush"); + return SUCCESS; +} + +int32_t HpaeCapturerStreamImpl::Stop() +{ + AUDIO_INFO_LOG("Stop"); + state_ = STOPPING; + return SUCCESS; +} + +int32_t HpaeCapturerStreamImpl::Release() +{ + if (state_ == RUNNING) { + AUDIO_ERR_LOG("%{public}u Release state_ is RUNNING", processConfig_.originalSessionId); + } + AUDIO_INFO_LOG("Release Enter"); + state_ = RELEASED; + // to do check closeaudioport + if (processConfig_.capturerInfo.sourceType == SOURCE_TYPE_WAKEUP) { + PolicyHandler::GetInstance().NotifyWakeUpCapturerRemoved(); + } + return SUCCESS; +} + +// to do callback data report +void HpaeCapturerStreamImpl::RegisterStatusCallback(const std::weak_ptr &callback) +{ + statusCallback_ = callback; +} + +void HpaeCapturerStreamImpl::RegisterReadCallback(const std::weak_ptr &callback) +{ + AUDIO_INFO_LOG("RegisterReadCallback start"); + readCallback_ = callback; +} + +BufferDesc HpaeCapturerStreamImpl::DequeueBuffer(size_t length) +{ + BufferDesc bufferDesc; + return bufferDesc; +} + +int32_t HpaeCapturerStreamImpl::EnqueueBuffer(const BufferDesc &bufferDesc) +{ + AUDIO_DEBUG_LOG("After capturere EnqueueBuffer"); + return SUCCESS; +} + +int32_t HpaeCapturerStreamImpl::DropBuffer() +{ + AUDIO_DEBUG_LOG("After capturere DropBuffer"); + return SUCCESS; +} + +int32_t HpaeCapturerStreamImpl::GetMinimumBufferSize(size_t &minBufferSize) const +{ + minBufferSize = minBufferSize_; + return SUCCESS; +} + +void HpaeCapturerStreamImpl::GetByteSizePerFrame(size_t &byteSizePerFrame) const +{ + byteSizePerFrame = byteSizePerFrame_; +} + +void HpaeCapturerStreamImpl::GetSpanSizePerFrame(size_t &spanSizeInFrame) const +{ + spanSizeInFrame = spanSizeInFrame_; +} + +void HpaeCapturerStreamImpl::SetStreamIndex(uint32_t index) +{ + AUDIO_INFO_LOG("Using index/sessionId %{public}u", index); + streamIndex_ = index; +} + +uint32_t HpaeCapturerStreamImpl::GetStreamIndex() +{ + return streamIndex_; +} + +void HpaeCapturerStreamImpl::AbortCallback(int32_t abortTimes) +{ + abortFlag_ += abortTimes; +} + +} // namespace AudioStandard +} // namespace OHOS diff --git a/services/audio_service/server/src/hpae_renderer_stream_impl.cpp b/services/audio_service/server/src/hpae_renderer_stream_impl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f700dfdb35212778452e0647d912537927ff73a4 --- /dev/null +++ b/services/audio_service/server/src/hpae_renderer_stream_impl.cpp @@ -0,0 +1,375 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef LOG_TAG +#define LOG_TAG "HpaeRendererStreamImpl" +#endif + +#ifdef FEATURE_POWER_MANAGER +#include "power_mgr_client.h" +#endif + +#include "hpae_renderer_stream_impl.h" +#include "sink/i_audio_render_sink.h" +#include "manager/hdi_adapter_manager.h" +#include +#include "safe_map.h" +#include "audio_errors.h" +#include "audio_service_log.h" +#include "audio_utils.h" +#include "audio_stream_info.h" +#include "audio_effect_map.h" + +namespace OHOS { +namespace AudioStandard { + +const int32_t MIN_BUFFER_SIZE = 2; +const int32_t FRAME_LEN_ON_MS = 20; +const int32_t MSEC_PER_SEC = 1000; + +static inline int32_t GetSizeFromFormat(int32_t format) +{ + return format != SAMPLE_F32LE ? ((format) + 1) : (4); // float 4 +} + +HpaeRendererStreamImpl::HpaeRendererStreamImpl(AudioProcessConfig processConfig) +{ + processConfig_ = processConfig; + spanSizeInFrame_ = static_cast(FRAME_LEN_ON_MS * + (static_cast(processConfig.streamInfo.samplingRate) / MSEC_PER_SEC)); + byteSizePerFrame_ = (processConfig.streamInfo.channels * GetSizeFromFormat(processConfig.streamInfo.format)); + minBufferSize_ = MIN_BUFFER_SIZE * byteSizePerFrame_ * spanSizeInFrame_; +} +HpaeRendererStreamImpl::~HpaeRendererStreamImpl() +{ + AUDIO_DEBUG_LOG("~HpaeRendererStreamImpl"); +} + +int32_t HpaeRendererStreamImpl::InitParams(const std::string &deviceName) +{ + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::Start() +{ + AUDIO_INFO_LOG("Start"); + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::Pause(bool isStandby) +{ + AUDIO_INFO_LOG("Pause"); + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::Flush() +{ + AUDIO_PRERELEASE_LOGI("Flush Enter"); + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::Drain(bool stopFlag) +{ + AUDIO_INFO_LOG("Drain Enter %{public}d", stopFlag); + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::Stop() +{ + AUDIO_INFO_LOG("Stop Enter"); + state_ = STOPPING; + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::Release() +{ + if (state_ == RUNNING) { + AUDIO_ERR_LOG("%{public}u Release state_ is RUNNING", processConfig_.originalSessionId); + } + AUDIO_INFO_LOG("Release Enter"); + state_ = RELEASED; + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::GetStreamFramesWritten(uint64_t &framesWritten) +{ + framesWritten = framesWritten_; + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::GetCurrentTimeStamp(uint64_t ×tamp) +{ + std::shared_lock lock(latencyMutex_); + timestamp = timestamp_; + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::GetCurrentPosition(uint64_t &framePosition, uint64_t ×tamp, uint64_t &latency) +{ + std::shared_lock lock(latencyMutex_); + framePosition = framePosition_; + timestamp = timestamp_; + latency = latency_; + return SUCCESS; +} + +static std::shared_ptr GetRenderSinkInstance(std::string deviceClass, std::string deviceNetId) +{ + uint32_t renderId = HDI_INVALID_ID; + if (deviceNetId.empty()) { + renderId = HdiAdapterManager::GetInstance().GetRenderIdByDeviceClass(deviceClass, HDI_ID_INFO_DEFAULT, false); + } else { + renderId = HdiAdapterManager::GetInstance().GetRenderIdByDeviceClass(deviceClass, deviceNetId, false); + } + return HdiAdapterManager::GetInstance().GetRenderSink(renderId, true); +} + +int32_t HpaeRendererStreamImpl::GetLatency(uint64_t &latency) +{ + std::shared_lock lock(latencyMutex_); + if (deviceClass_ != "offload") { + uint32_t SinkLatency = 0; + std::shared_ptr audioRendererSink = GetRenderSinkInstance(deviceClass_, deviceNetId_); + if (audioRendererSink) { + audioRendererSink->GetLatency(SinkLatency); + } + latency = SinkLatency + latency_; + return SUCCESS; + } + timespec tm {}; + clock_gettime(CLOCK_MONOTONIC, &tm); + auto timestamp = static_cast(tm.tv_sec) * 1000000000ll + static_cast(tm.tv_nsec); + auto interval = (timestamp - timestamp_) / 1000; + latency = latency_ > interval ? latency_ - interval : 0; + AUDIO_DEBUG_LOG("HpaeRendererStreamImpl::GetLatency latency_ %{public}" PRIu64 "," + "interval %{public}llu latency %{public}" PRIu64, latency_, interval, latency); + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::SetRate(int32_t rate) +{ + AUDIO_INFO_LOG("SetRate in"); + renderRate_ = rate; + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::SetAudioEffectMode(int32_t effectMode) +{ + AUDIO_INFO_LOG("SetAudioEffectMode: %d", effectMode); + + effectMode_ = effectMode; + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::GetAudioEffectMode(int32_t &effectMode) +{ + effectMode = effectMode_; + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::SetPrivacyType(int32_t privacyType) +{ + AUDIO_DEBUG_LOG("SetInnerCapturerState: %d", privacyType); + privacyType_ = privacyType; + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::GetPrivacyType(int32_t &privacyType) +{ + privacyType_ = privacyType; + return SUCCESS; +} + + +void HpaeRendererStreamImpl::RegisterStatusCallback(const std::weak_ptr &callback) +{ + AUDIO_DEBUG_LOG("RegisterStatusCallback in"); + statusCallback_ = callback; +} + +void HpaeRendererStreamImpl::RegisterWriteCallback(const std::weak_ptr &callback) +{ + AUDIO_DEBUG_LOG("RegisterWriteCallback in"); + writeCallback_ = callback; +} + +int32_t HpaeRendererStreamImpl::OnStreamData(AudioCallBackStreamInfo& callBackStreamInfo) +{ + { + std::unique_lock lock(latencyMutex_); + framePosition_ = callBackStreamInfo.framePosition; + timestamp_ = callBackStreamInfo.timestamp; + latency_ = callBackStreamInfo.latency; + framesWritten_ = callBackStreamInfo.framesWritten; + deviceClass_ = callBackStreamInfo.deviceClass; + deviceNetId_ = callBackStreamInfo.deviceNetId; + } + if (callBackStreamInfo.needData && writeCallback_.lock()) { + return writeCallback_.lock()->OnWriteData(callBackStreamInfo.inputData, callBackStreamInfo.requestDataLen); + } + return SUCCESS; +} + +BufferDesc HpaeRendererStreamImpl::DequeueBuffer(size_t length) +{ + BufferDesc bufferDesc; + return bufferDesc; +} + +int32_t HpaeRendererStreamImpl::EnqueueBuffer(const BufferDesc &bufferDesc) +{ + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::GetMinimumBufferSize(size_t &minBufferSize) const +{ + minBufferSize = minBufferSize_; + return SUCCESS; +} + +void HpaeRendererStreamImpl::GetByteSizePerFrame(size_t &byteSizePerFrame) const +{ + byteSizePerFrame = byteSizePerFrame_; +} + +void HpaeRendererStreamImpl::GetSpanSizePerFrame(size_t &spanSizeInFrame) const +{ + spanSizeInFrame = spanSizeInFrame_; +} + +void HpaeRendererStreamImpl::SetStreamIndex(uint32_t index) +{ + AUDIO_INFO_LOG("Using index/sessionId %d", index); + streamIndex_ = index; +} + +uint32_t HpaeRendererStreamImpl::GetStreamIndex() +{ + return streamIndex_; +} + +void HpaeRendererStreamImpl::AbortCallback(int32_t abortTimes) +{ + abortFlag_ += abortTimes; +} + +// offload + +size_t HpaeRendererStreamImpl::GetWritableSize() +{ + return 0; +} + +int32_t HpaeRendererStreamImpl::OffloadSetVolume(float volume) +{ + std::shared_ptr audioRendererSinkInstance = GetRenderSinkInstance("offload", ""); + if (audioRendererSinkInstance == nullptr) { + AUDIO_ERR_LOG("Renderer is null."); + return ERROR; + } + return audioRendererSinkInstance->SetVolume(volume, volume); +} + +int32_t HpaeRendererStreamImpl::UpdateSpatializationState(bool spatializationEnabled, bool headTrackingEnabled) +{ + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::GetOffloadApproximatelyCacheTime(uint64_t ×tamp, uint64_t &paWriteIndex, + uint64_t &cacheTimeDsp, uint64_t &cacheTimePa) +{ + return SUCCESS; +} + +void HpaeRendererStreamImpl::SyncOffloadMode() +{ + std::shared_ptr statusCallback = statusCallback_.lock(); + if (statusCallback != nullptr) { + if (offloadEnable_) { + statusCallback->OnStatusUpdate(OPERATION_SET_OFFLOAD_ENABLE); + } else { + statusCallback->OnStatusUpdate(OPERATION_UNSET_OFFLOAD_ENABLE); + } + } +} + +int32_t HpaeRendererStreamImpl::SetOffloadMode(int32_t state, bool isAppBack) +{ +#ifdef FEATURE_POWER_MANAGER + static const std::set screenOffTable = { + PowerMgr::PowerState::INACTIVE, PowerMgr::PowerState::STAND_BY, + PowerMgr::PowerState::DOZE, PowerMgr::PowerState::SLEEP, + PowerMgr::PowerState::HIBERNATE, + }; + AudioOffloadType statePolicy = OFFLOAD_DEFAULT; + statePolicy = screenOffTable.count(static_cast(state)) ? + OFFLOAD_INACTIVE_BACKGROUND : OFFLOAD_ACTIVE_FOREGROUND; + + AUDIO_INFO_LOG("calling set stream offloadMode PowerState: %{public}d, isAppBack: %{public}d", state, isAppBack); + + if (offloadStatePolicy_.load() == statePolicy) { + return SUCCESS; + } + + offloadEnable_ = true; + SyncOffloadMode(); + offloadStatePolicy_.store(statePolicy); +#else + AUDIO_INFO_LOG("SetStreamOffloadMode not available, FEATURE_POWER_MANAGER no define"); +#endif + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::UnsetOffloadMode() +{ + offloadEnable_ = false; + SyncOffloadMode(); + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::UpdateMaxLength(uint32_t maxLength) +{ + return SUCCESS; +} + +AudioProcessConfig HpaeRendererStreamImpl::GetAudioProcessConfig() const noexcept +{ + return processConfig_; +} + +int32_t HpaeRendererStreamImpl::Peek(std::vector *audioBuffer, int32_t &index) +{ + return SUCCESS; +} + +int32_t HpaeRendererStreamImpl::ReturnIndex(int32_t index) +{ + return SUCCESS; +} + +void HpaeRendererStreamImpl::BlockStream() noexcept +{ + return; +} +// offload end + +int32_t HpaeRendererStreamImpl::SetClientVolume(float clientVolume) +{ + AUDIO_PRERELEASE_LOGI("set client volume success"); + clientVolume_ = clientVolume; + return SUCCESS; +} + +} // namespace AudioStandard +} // namespace OHOS diff --git a/services/audio_service/server/src/i_stream_manager.cpp b/services/audio_service/server/src/i_stream_manager.cpp index e8a844f2cd0cd15d20218bb04bc60d40bffada94..fc5e8ec44afe078ab5f281906b1e339ce80315a2 100644 --- a/services/audio_service/server/src/i_stream_manager.cpp +++ b/services/audio_service/server/src/i_stream_manager.cpp @@ -13,7 +13,10 @@ * limitations under the License. */ +#include "audio_utils.h" +#include "audio_engine_log.h" #include "pa_adapter_manager.h" +#include "hpae_adapter_manager.h" #include "pro_audio_stream_manager.h" namespace OHOS { @@ -29,27 +32,51 @@ IStreamManager &IStreamManager::GetPlaybackManager(ManagerType managerType) return voipManager; case PLAYBACK: default: - static PaAdapterManager adapterManager(PLAYBACK); - return adapterManager; + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + static HpaeAdapterManager adapterManager(PLAYBACK); + return adapterManager; + } else { + static PaAdapterManager adapterManager(PLAYBACK); + return adapterManager; + } } } IStreamManager &IStreamManager::GetDupPlaybackManager() { - static PaAdapterManager adapterManager(DUP_PLAYBACK); - return adapterManager; + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + static HpaeAdapterManager adapterManager(DUP_PLAYBACK); + return adapterManager; + } else { + static PaAdapterManager adapterManager(DUP_PLAYBACK); + return adapterManager; + } } IStreamManager &IStreamManager::GetDualPlaybackManager() { - static PaAdapterManager adapterManager(DUAL_PLAYBACK); - return adapterManager; + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + static HpaeAdapterManager adapterManager(DUAL_PLAYBACK); + return adapterManager; + } else { + static PaAdapterManager adapterManager(DUAL_PLAYBACK); + return adapterManager; + } } IStreamManager &IStreamManager::GetRecorderManager() { - static PaAdapterManager adapterManager(RECORDER); - return adapterManager; + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + static HpaeAdapterManager adapterManager(RECORDER); + return adapterManager; + } else { + static PaAdapterManager adapterManager(RECORDER); + return adapterManager; + } } } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_service/server/src/pa_renderer_stream_impl.cpp b/services/audio_service/server/src/pa_renderer_stream_impl.cpp index 7757ca037bf3018da20358331aec7715bdf708f8..ee389a7cc991bbc8e84c9e32cc8dd202da18f88e 100644 --- a/services/audio_service/server/src/pa_renderer_stream_impl.cpp +++ b/services/audio_service/server/src/pa_renderer_stream_impl.cpp @@ -1197,10 +1197,6 @@ int32_t PaRendererStreamImpl::SetOffloadMode(int32_t state, bool isAppBack) return SUCCESS; } - if ((offloadStatePolicy_ == offloadNextStateTargetPolicy_) && (offloadStatePolicy_ == statePolicy)) { - return SUCCESS; - } - offloadEnable_ = true; SyncOffloadMode(); if (OffloadUpdatePolicy(statePolicy, false) != SUCCESS) { diff --git a/services/audio_service/server/src/renderer_in_server.cpp b/services/audio_service/server/src/renderer_in_server.cpp index 487ee443b1dee9b909f6e79d4f6f0b9316ea1294..8290af56053af1c96266f13b2ec6577fc9277922 100644 --- a/services/audio_service/server/src/renderer_in_server.cpp +++ b/services/audio_service/server/src/renderer_in_server.cpp @@ -58,6 +58,9 @@ namespace { constexpr int32_t RELEASE_TIMEOUT_IN_SEC = 10; // 10S constexpr int32_t DEFAULT_SPAN_SIZE = 1; constexpr size_t MSEC_PER_SEC = 1000; + const int32_t DUP_OFFLOAD_LEN = 7000; + const int32_t DUP_COMMON_LEN = 40; + const int32_t DUP_DEFAULT_LEN = 20; } RendererInServer::RendererInServer(AudioProcessConfig processConfig, std::weak_ptr streamListener) @@ -86,7 +89,12 @@ int32_t RendererInServer::ConfigServerBuffer() return SUCCESS; } stream_->GetSpanSizePerFrame(spanSizeInFrame_); - totalSizeInFrame_ = spanSizeInFrame_ * DEFAULT_SPAN_SIZE; + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + totalSizeInFrame_ = spanSizeInFrame_ * 2; // 2 * 2 = 4 frames + } else { + totalSizeInFrame_ = spanSizeInFrame_ * DEFAULT_SPAN_SIZE; + } stream_->GetByteSizePerFrame(byteSizePerFrame_); if (totalSizeInFrame_ == 0 || spanSizeInFrame_ == 0 || totalSizeInFrame_ % spanSizeInFrame_ != 0) { AUDIO_ERR_LOG("ConfigProcessBuffer: ERR_INVALID_PARAM"); @@ -304,6 +312,7 @@ void RendererInServer::OnStatusUpdateSub(IOperation operation) { std::shared_ptr stateListener = streamListener_.lock(); CHECK_AND_RETURN_LOG(stateListener != nullptr, "StreamListener is nullptr"); + int32_t engineFlag = GetEngineFlag(); switch (operation) { case OPERATION_RELEASED: stateListener->OnOperationHandled(RELEASE_STREAM, 0); @@ -331,6 +340,11 @@ void RendererInServer::OnStatusUpdateSub(IOperation operation) case OPERATION_SET_OFFLOAD_ENABLE: case OPERATION_UNSET_OFFLOAD_ENABLE: offloadEnable_ = operation == OPERATION_SET_OFFLOAD_ENABLE ? true : false; + if (engineFlag == 1 && offloadEnable_ == true && dupStreamCallback_ != nullptr && + dupStreamCallback_->GetDupRingBuffer() != nullptr) { + dupTotalSizeInFrame_ = dupSpanSizeInFrame_ * (DUP_OFFLOAD_LEN / DUP_DEFAULT_LEN); + dupStreamCallback_->GetDupRingBuffer()->ReConfig(dupTotalSizeInFrame_ * dupByteSizePerFrame_, false); + } stateListener->OnOperationHandled(SET_OFFLOAD_ENABLE, operation == OPERATION_SET_OFFLOAD_ENABLE ? 1 : 0); break; default: @@ -613,6 +627,59 @@ int32_t RendererInServer::WriteData() return SUCCESS; } +int32_t RendererInServer::OnWriteData(int8_t *inputData, size_t requestDataLen) +{ + uint64_t currentReadFrame = audioServerBuffer_->GetCurReadFrame(); + uint64_t currentWriteFrame = audioServerBuffer_->GetCurWriteFrame(); + Trace trace1(traceTag_ + " WriteData"); // RendererInServer::sessionid:100001 WriteData + if (currentReadFrame + spanSizeInFrame_ > currentWriteFrame) { + Trace trace2(traceTag_ + " near underrun"); // RendererInServer::sessionid:100001 near underrun + FutexTool::FutexWake(audioServerBuffer_->GetFutex()); + if (!offloadEnable_) { + CHECK_AND_RETURN_RET_LOG(currentWriteFrame >= currentReadFrame, ERR_OPERATION_FAILED, + "invalid write and read position."); + uint64_t dataSize = currentWriteFrame - currentReadFrame; + AUDIO_INFO_LOG("sessionId: %{public}u OHAudioBuffer %{public}" PRIu64 "size is not enough", + streamIndex_, dataSize); + } + return ERR_OPERATION_FAILED; + } + + BufferDesc bufferDesc = {nullptr, 0, 0}; // will be changed in GetReadbuffer + if (audioServerBuffer_->GetReadbuffer(currentReadFrame, bufferDesc) == SUCCESS) { + if (bufferDesc.buffer == nullptr) { + AUDIO_ERR_LOG("The buffer is null!"); + return ERR_INVALID_PARAM; + } + VolumeHandle(bufferDesc); + Trace::CountVolume(traceTag_, *bufferDesc.buffer); + if (processConfig_.streamType != STREAM_ULTRASONIC) { + if (currentReadFrame + spanSizeInFrame_ == currentWriteFrame) { + DoFadingOut(bufferDesc); + } + } + memcpy_s(inputData, requestDataLen, bufferDesc.buffer, bufferDesc.bufLength); + if (AudioDump::GetInstance().GetVersionType() == DumpFileUtil::BETA_VERSION) { + DumpFileUtil::WriteDumpFile(dumpC2S_, static_cast(bufferDesc.buffer), bufferDesc.bufLength); + AudioCacheMgr::GetInstance().CacheData(dumpFileName_, + static_cast(bufferDesc.buffer), bufferDesc.bufLength); + } + + OtherStreamEnqueue(bufferDesc); + + WriteMuteDataSysEvent(bufferDesc); + memset_s(bufferDesc.buffer, bufferDesc.bufLength, 0, bufferDesc.bufLength); // clear is needed for reuse. + uint64_t nextReadFrame = currentReadFrame + spanSizeInFrame_; + audioServerBuffer_->SetCurReadFrame(nextReadFrame); + } else { + Trace trace3("RendererInServer::WriteData GetReadbuffer failed"); + } + FutexTool::FutexWake(audioServerBuffer_->GetFutex()); + standByCounter_ = 0; + lastWriteTime_ = ClockTime::GetCurNano(); + return SUCCESS; +} + void RendererInServer::OtherStreamEnqueue(const BufferDesc &bufferDesc) { // for inner capture @@ -629,21 +696,36 @@ void RendererInServer::OtherStreamEnqueue(const BufferDesc &bufferDesc) } } +void RendererInServer::InnerCaptureEnqueueBuffer(const BufferDesc &bufferDesc, CaptureInfo &captureInfo) +{ + int32_t engineFlag = GetEngineFlag(); + if (renderEmptyCountForInnerCap_ > 0) { + size_t emptyBufferSize = static_cast(renderEmptyCountForInnerCap_) * spanSizeInByte_; + auto buffer = std::make_unique(emptyBufferSize); + BufferDesc emptyBufferDesc = {buffer.get(), emptyBufferSize, emptyBufferSize}; + memset_s(emptyBufferDesc.buffer, emptyBufferDesc.bufLength, 0, emptyBufferDesc.bufLength); + if (engineFlag == 1) { + WriteDupBufferInner(emptyBufferDesc); + } else { + captureInfo.dupStream->EnqueueBuffer(emptyBufferDesc); + } + renderEmptyCountForInnerCap_ = 0; + } + if (engineFlag == 1) { + AUDIO_INFO_LOG("OtherStreamEnqueue running"); + WriteDupBufferInner(bufferDesc); + } else { + captureInfo.dupStream->EnqueueBuffer(bufferDesc); // what if enqueue fail? + } +} + void RendererInServer::InnerCaptureOtherStream(const BufferDesc &bufferDesc, CaptureInfo &captureInfo) { if (captureInfo.isInnerCapEnabled) { Trace traceDup("RendererInServer::WriteData DupSteam write"); std::lock_guard lock(dupMutex_); if (captureInfo.dupStream != nullptr) { - if (renderEmptyCountForInnerCap_ > 0) { - size_t emptyBufferSize = static_cast(renderEmptyCountForInnerCap_) * spanSizeInByte_; - auto buffer = std::make_unique(emptyBufferSize); - BufferDesc emptyBufferDesc = {buffer.get(), emptyBufferSize, emptyBufferSize}; - memset_s(emptyBufferDesc.buffer, emptyBufferDesc.bufLength, 0, emptyBufferDesc.bufLength); - captureInfo.dupStream->EnqueueBuffer(emptyBufferDesc); - renderEmptyCountForInnerCap_ = 0; - } - captureInfo.dupStream->EnqueueBuffer(bufferDesc); // what if enqueue fail? + InnerCaptureEnqueueBuffer(bufferDesc, captureInfo); } } } @@ -851,6 +933,7 @@ int32_t RendererInServer::Pause() } } } + pausedTime_ = ClockTime::GetCurNano(); if (isDualToneEnabled_ && dualToneStream_ != nullptr) { //Joint judgment ensures that there is a double ring and there is a stream to enter. stream_->SetAudioEffectMode(effectModeWhenDual_); @@ -901,7 +984,7 @@ int32_t RendererInServer::Flush() "writeFrame: %{public}" PRIu64 "", readFrame, spanSizeInFrame_, writeFrame); audioServerBuffer_->SetCurReadFrame(readFrame); } - + flushedTime_ = ClockTime::GetCurNano(); int ret = stream_->Flush(); CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Flush stream failed, reason: %{public}d", ret); { @@ -943,6 +1026,7 @@ int32_t RendererInServer::Drain(bool stopFlag) fadeoutFlag_ = DO_FADINGOUT; } DrainAudioBuffer(); + drainedTime_ = ClockTime::GetCurNano(); AudioPerformanceMonitor::GetInstance().ClearSilenceMonitor(streamIndex_); int ret = stream_->Drain(stopFlag); CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Drain stream failed, reason: %{public}d", ret); @@ -991,8 +1075,9 @@ int32_t RendererInServer::Stop() AUDIO_INFO_LOG("fadeoutFlag_ = NO_FADING"); fadeoutFlag_ = NO_FADING; } - int32_t ret = (managerType_ == DIRECT_PLAYBACK || managerType_ == VOIP_PLAYBACK) ? - IStreamManager::GetPlaybackManager(managerType_).StopRender(streamIndex_) : stream_->Stop(); + int32_t ret = (managerType_ == DIRECT_PLAYBACK || managerType_ == VOIP_PLAYBACK) + ? IStreamManager::GetPlaybackManager(managerType_).StopRender(streamIndex_) + : stream_->Stop(); { std::lock_guard lock(dupMutex_); for (auto &capInfo : captureInfos_) { @@ -1190,6 +1275,10 @@ int32_t RendererInServer::DisableInnerCap(int32_t innerCapId) AudioVolume::GetInstance()->RemoveStreamVolume(dupStreamIndex); captureInfos_[innerCapId].dupStream = nullptr; } + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + DumpFileUtil::CloseDumpFile(&dumpDupIn_); + } return SUCCESS; } @@ -1208,6 +1297,15 @@ int32_t RendererInServer::InitDupStream(int32_t innerCapId) processConfig_.rendererInfo.streamUsage, processConfig_.appInfo.appUid, processConfig_.appInfo.appPid, isSystemApp, processConfig_.rendererInfo.volumeMode); + dupStreamCallback_ = std::make_shared(dupStreamIndex); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + ret = CreateDupBufferInner(innerCapId); + dumpDupInFileName_ = std::to_string(dupStreamIndex) + "_dup_in_" + ".pcm"; + DumpFileUtil::OpenDumpFile(DumpFileUtil::DUMP_SERVER_PARA, dumpDupInFileName_, &dumpDupIn_); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR, "Config dup buffer failed"); + } + // todo check index dupStreamCallback_ = std::make_shared(dupStreamIndex); capInfo.dupStream->RegisterStatusCallback(dupStreamCallback_); capInfo.dupStream->RegisterWriteCallback(dupStreamCallback_); @@ -1310,6 +1408,19 @@ int32_t RendererInServer::InitDualToneStream() StreamCallbacks::StreamCallbacks(uint32_t streamIndex) : streamIndex_(streamIndex) { AUDIO_INFO_LOG("DupStream %{public}u create StreamCallbacks", streamIndex_); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + dumpDupOutFileName_ = std::to_string(streamIndex_) + "_dup_out_" + ".pcm"; + DumpFileUtil::OpenDumpFile(DumpFileUtil::DUMP_SERVER_PARA, dumpDupOutFileName_, &dumpDupOut_); + } +} + +StreamCallbacks::~StreamCallbacks() +{ + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + DumpFileUtil::CloseDumpFile(&dumpDupOut_); + } } void StreamCallbacks::OnStatusUpdate(IOperation operation) @@ -1323,6 +1434,35 @@ int32_t StreamCallbacks::OnWriteData(size_t length) return SUCCESS; } +int32_t StreamCallbacks::OnWriteData(int8_t *inputData, size_t requestDataLen) +{ + Trace trace("DupStream::OnWriteData length " + std::to_string(requestDataLen)); + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + std::unique_ptr &dupBuffer = + AudioService::GetInstance()->GetRendererBySessionID(streamIndex_)->GetDupRingBuffer(); + // no need mutex + // todo wait readable + AUDIO_INFO_LOG("StreamCallbacks::OnWriteData running"); + OptResult result = dupBuffer->GetReadableSize(); + CHECK_AND_RETURN_RET_LOG(result.ret == OPERATION_SUCCESS, ERROR, + "dupBuffer get readable size failed, size is:%{public}zu", result.size); + CHECK_AND_RETURN_RET_LOG((result.size != 0) && (result.size >= requestDataLen), ERROR, + "Readable size is invaild, result.size:%{public}zu, requstDataLen:%{public}zu", + result.size, requestDataLen); + AUDIO_DEBUG_LOG("requstDataLen is:%{public}zu readSize is:%{public}zu", requestDataLen, result.size); + result = dupBuffer->Dequeue({reinterpret_cast(inputData), requestDataLen}); + CHECK_AND_RETURN_RET_LOG(result.ret == OPERATION_SUCCESS, ERROR, "dupBuffer dequeue failed");\ + DumpFileUtil::WriteDumpFile(dumpDupOut_, static_cast(inputData), requestDataLen); + } + return SUCCESS; +} + +std::unique_ptr& StreamCallbacks::GetDupRingBuffer() +{ + return dupRingBuffer_; +} + int32_t RendererInServer::SetOffloadMode(int32_t state, bool isAppBack) { int32_t ret = stream_->SetOffloadMode(state, isAppBack); @@ -1526,11 +1666,17 @@ int32_t RendererInServer::SetStreamVolumeInfoForEnhanceChain() { uint32_t sessionId = streamIndex_; float streamVolume = audioServerBuffer_->GetStreamVolume(); - AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); - CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); - int32_t ret = audioEnhanceChainManager->SetStreamVolumeInfo(sessionId, streamVolume); - CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "SetStreamVolumeInfo failed"); - return ret; + int32_t engineFlag = GetEngineFlag(); + if (engineFlag == 1) { + AUDIO_DEBUG_LOG("HPAE SetStreamVolumeInfoForEnhanceChain"); + return SUCCESS; + } else { + AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance(); + CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr"); + int32_t ret = audioEnhanceChainManager->SetStreamVolumeInfo(sessionId, streamVolume); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "SetStreamVolumeInfo failed"); + return ret; + } } void RendererInServer::OnDataLinkConnectionUpdate(IOperation operation) @@ -1687,5 +1833,61 @@ int32_t RendererInServer::SetSourceDuration(int64_t duration) return SUCCESS; } +std::unique_ptr& RendererInServer::GetDupRingBuffer() +{ + return dupRingBuffer_; +} + +int32_t RendererInServer::CreateDupBufferInner(int32_t innerCapId) +{ + // todo dynamic + if (dupStreamCallback_->GetDupRingBuffer() != nullptr) { + AUDIO_INFO_LOG("dup buffer already configed!"); + return SUCCESS; + } + + auto &capInfo = captureInfos_[innerCapId]; + capInfo.dupStream->GetSpanSizePerFrame(dupSpanSizeInFrame_); + // todo offload 350 frames primary 1 frame + dupTotalSizeInFrame_ = dupSpanSizeInFrame_ * (DUP_COMMON_LEN/DUP_DEFAULT_LEN); + capInfo.dupStream->GetByteSizePerFrame(dupByteSizePerFrame_); + if (dupSpanSizeInFrame_ == 0 || dupByteSizePerFrame_ == 0) { + AUDIO_ERR_LOG("ERR_INVALID_PARAM"); + return ERR_INVALID_PARAM; + } + dupSpanSizeInByte_ = dupSpanSizeInFrame_ * dupByteSizePerFrame_; + CHECK_AND_RETURN_RET_LOG(dupSpanSizeInByte_ != 0, ERR_OPERATION_FAILED, "Config dup buffer failed"); + AUDIO_INFO_LOG("dupTotalSizeInFrame_: %{public}zu, dupSpanSizeInFrame_: %{public}zu," + "dupByteSizePerFrame_:%{public}zu dupSpanSizeInByte_: %{public}zu,", + dupTotalSizeInFrame_, dupSpanSizeInFrame_, dupByteSizePerFrame_, dupSpanSizeInByte_); + + // create dupBuffer in server + dupStreamCallback_->GetDupRingBuffer() = AudioRingCache::Create(dupTotalSizeInFrame_ * dupByteSizePerFrame_); + CHECK_AND_RETURN_RET_LOG(dupStreamCallback_->GetDupRingBuffer() != nullptr, ERR_OPERATION_FAILED, + "Create dup buffer failed"); + return SUCCESS; +} + +int32_t RendererInServer::WriteDupBufferInner(const BufferDesc &bufferDesc) +{ + size_t targetSize = bufferDesc.bufLength; + OptResult result = dupStreamCallback_->GetDupRingBuffer()->GetWritableSize(); + // todo get writeable size failed + CHECK_AND_RETURN_RET_LOG(result.ret == OPERATION_SUCCESS, ERROR, + "DupRingBuffer write invalid size is:%{public}zu", result.size); + size_t writableSize = result.size; + AUDIO_DEBUG_LOG("targetSize: %{public}zu, writableSize: %{public}zu", targetSize, writableSize); + size_t writeSize = std::min(writableSize, targetSize); + BufferWrap bufferWrap = {bufferDesc.buffer, writeSize}; + if (writeSize > 0) { + result = dupStreamCallback_->GetDupRingBuffer()->Enqueue(bufferWrap); + if (result.ret != OPERATION_SUCCESS) { + AUDIO_ERR_LOG("RingCache Enqueue failed ret:%{public}d size:%{public}zu", result.ret, result.size); + } + DumpFileUtil::WriteDumpFile(dumpDupIn_, static_cast(bufferDesc.buffer), writeSize); + } + return SUCCESS; +} + } // namespace AudioStandard } // namespace OHOS diff --git a/test/fuzztest/audioeffect_fuzzer/audio_effect_fuzzer.cpp b/test/fuzztest/audioeffect_fuzzer/audio_effect_fuzzer.cpp index bd9943124105f8e0cc3d622a31b0051c22985369..0acb03dce911006770dedf5b1d3e64970f768bc7 100644 --- a/test/fuzztest/audioeffect_fuzzer/audio_effect_fuzzer.cpp +++ b/test/fuzztest/audioeffect_fuzzer/audio_effect_fuzzer.cpp @@ -50,7 +50,6 @@ SessionEffectInfo DEFAULT_INFO = { SCENETYPEDEFAULT, INFOCHANNELS, INFOCHANNELLAYOUT, - "0", }; /* diff --git a/test/fuzztest/audioeffectchain_fuzzer/audio_effect_chain_fuzzer.cpp b/test/fuzztest/audioeffectchain_fuzzer/audio_effect_chain_fuzzer.cpp index 9496ee045903374dbee5d52eb9d5999732d06ec4..e952d5532411c10eb01317a343840a2c43c0536b 100644 --- a/test/fuzztest/audioeffectchain_fuzzer/audio_effect_chain_fuzzer.cpp +++ b/test/fuzztest/audioeffectchain_fuzzer/audio_effect_chain_fuzzer.cpp @@ -15,6 +15,8 @@ #include #include +#include "securec.h" + #include "audio_effect.h" #include "audio_effect_chain.h" #include "audio_effect_log.h" diff --git a/test/fuzztest/audioeffectenhance_fuzzer/audio_effect_enhance_fuzzer.cpp b/test/fuzztest/audioeffectenhance_fuzzer/audio_effect_enhance_fuzzer.cpp index 77cf8b095ccba326c41a5993166a8c786c7d3554..522c5b325baa7b90ceddc658f47771266cdd9807 100644 --- a/test/fuzztest/audioeffectenhance_fuzzer/audio_effect_enhance_fuzzer.cpp +++ b/test/fuzztest/audioeffectenhance_fuzzer/audio_effect_enhance_fuzzer.cpp @@ -17,6 +17,8 @@ #include #include #include +#include "securec.h" + #include "audio_effect_log.h" #include "audio_effect_chain.h" #include "audio_effect_chain_adapter.h" @@ -141,7 +143,6 @@ void AudioEffectChainManagerEnhanceFuzzTest() "SCENE_MOVIE", INFOCHANNELS, INFOCHANNELLAYOUT, - "0", }; AudioEffectChainManager::GetInstance()->sessionIDToEffectInfoMap_[sessionID] = sessionEffectInfo; diff --git a/test/fuzztest/audiopolicy_fuzzer/audio_policy_fuzzer.cpp b/test/fuzztest/audiopolicy_fuzzer/audio_policy_fuzzer.cpp index 75f4509334718411388736223e77462c4eda8204..009a1aaf2f0d1b984d31183c2882f706e4de5a35 100644 --- a/test/fuzztest/audiopolicy_fuzzer/audio_policy_fuzzer.cpp +++ b/test/fuzztest/audiopolicy_fuzzer/audio_policy_fuzzer.cpp @@ -48,6 +48,7 @@ typedef void (*TestPtr)(const uint8_t *, size_t); sptr GetServerPtr() { static sptr server = sptr::MakeSptr(SYSTEM_ABILITY_ID, RUN_ON_CREATE); + server->ConnectServiceAdapter(); if (!g_hasServerInit) { server->OnStart(); server->OnAddSystemAbility(AUDIO_DISTRIBUTED_SERVICE_ID, "");