1 /* 2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_DAUDIO_HDF_OPERATE_H 17 #define OHOS_DAUDIO_HDF_OPERATE_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <mutex> 22 23 #include "idistributed_hardware_source.h" 24 #include "iservstat_listener_hdi.h" 25 #include "idevmgr_hdi.h" 26 #include "iservmgr_hdi.h" 27 #include "single_instance.h" 28 #include <v2_1/id_audio_manager.h> 29 30 namespace OHOS { 31 namespace DistributedHardware { 32 const std::string AUDIO_SERVICE_NAME = "daudio_primary_service"; 33 const std::string AUDIOEXT_SERVICE_NAME = "daudio_ext_service"; 34 const std::string HDF_LISTENER_SERVICE_NAME = "DHFWK"; 35 constexpr uint16_t AUDIO_INVALID_VALUE = 0xffff; 36 constexpr int32_t AUDIO_WAIT_TIME = 5000; 37 using OHOS::HDI::DeviceManager::V1_0::IDeviceManager; 38 using OHOS::HDI::ServiceManager::V1_0::IServiceManager; 39 using OHOS::HDI::ServiceManager::V1_0::IServStatListener; 40 using OHOS::HDI::ServiceManager::V1_0::ServiceStatus; 41 using OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub; 42 using OHOS::HDI::DistributedAudio::Audioext::V2_1::IDAudioManager; 43 using OHOS::HDI::DistributedAudio::Audioext::V2_1::IDAudioHdfCallback; 44 using OHOS::HDI::DistributedAudio::Audioext::V2_1::DAudioEvent; 45 46 class FwkDAudioHdfCallback; 47 class HdfDeathRecipient : public IRemoteObject::DeathRecipient { 48 public: 49 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 50 }; 51 class DaudioHdfOperate { 52 DECLARE_SINGLE_INSTANCE(DaudioHdfOperate); 53 54 public: 55 int32_t LoadDaudioHDFImpl(std::shared_ptr<HdfDeathCallback> callback); 56 int32_t UnLoadDaudioHDFImpl(); 57 void OnHdfHostDied(); 58 59 private: 60 int32_t WaitLoadService(const std::string& servName); 61 OHOS::sptr<IServStatListener> MakeServStatListener(); 62 int32_t LoadDevice(); 63 int32_t UnLoadDevice(); 64 int32_t RegisterHdfListener(); 65 int32_t UnRegisterHdfListener(); 66 int32_t AddHdfDeathBind(); 67 int32_t RemoveHdfDeathBind(); 68 int32_t MakeFwkDAudioHdfCallback(); 69 70 private: 71 OHOS::sptr<IDeviceManager> devmgr_; 72 OHOS::sptr<IServiceManager> servMgr_; 73 OHOS::sptr<IDAudioManager> audioSrvHdf_; 74 std::mutex fwkDAudioHdfCallbackMutex_; 75 OHOS::sptr<FwkDAudioHdfCallback> fwkDAudioHdfCallback_; 76 std::atomic<uint16_t> audioServStatus_ = AUDIO_INVALID_VALUE; 77 std::atomic<uint16_t> audioextServStatus_ = AUDIO_INVALID_VALUE; 78 std::condition_variable hdfOperateCon_; 79 std::mutex hdfOperateMutex_; 80 std::shared_ptr<HdfDeathCallback> hdfDeathCallback_; 81 sptr<HdfDeathRecipient> hdfDeathRecipient_ = sptr<HdfDeathRecipient>(new HdfDeathRecipient()); 82 }; 83 84 class DAudioHdfServStatListener : public OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub { 85 public: 86 using StatusCallback = std::function<void(const ServiceStatus &)>; DAudioHdfServStatListener(StatusCallback callback)87 explicit DAudioHdfServStatListener(StatusCallback callback) : callback_(std::move(callback)) 88 { 89 } 90 ~DAudioHdfServStatListener() override = default; 91 void OnReceive(const ServiceStatus& status) override; 92 93 private: 94 StatusCallback callback_; 95 }; 96 97 class FwkDAudioHdfCallback : public IDAudioHdfCallback { 98 protected: 99 int32_t NotifyEvent(int32_t devId, const DAudioEvent& event) override; 100 }; 101 } // namespace DistributedHardware 102 } // namespace OHOS 103 #endif // OHOS_DAUDIO_HDF_OPERATE_H