• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 LOCAL_DEVICE_MANAGER_H
17 #define LOCAL_DEVICE_MANAGER_H
18 
19 #include <iostream>
20 #include <cstring>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <vector>
24 #include <mutex>
25 #include "v4_0/iaudio_manager.h"
26 #include "hdf_remote_service.h"
27 #include "adapter/i_device_manager.h"
28 
29 namespace OHOS {
30 namespace AudioStandard {
31 typedef struct LocalAdapterWrapper {
32     struct IAudioAdapter *adapter_ = nullptr;
33     struct AudioAdapterDescriptor adapterDesc_ = {};
34     std::unordered_set<uint32_t> hdiRenderIds_;
35     std::unordered_set<uint32_t> hdiCaptureIds_;
36     std::mutex renderMtx_;
37     std::mutex captureMtx_;
38     int32_t routeHandle_ = -1;
39 } LocalAdapterWrapper;
40 
41 typedef struct LocalParameter {
42     std::string adapterName_ = "";
43     AudioParamKey key_ = AudioParamKey::NONE;
44     std::string condition_ = "";
45     std::string value_ = "";
46 } LocalParameter;
47 
48 class LocalDeviceManager : public IDeviceManager {
49 public:
50     LocalDeviceManager() = default;
51     ~LocalDeviceManager() = default;
52 
53     int32_t LoadAdapter(const std::string &adapterName) override;
54     void UnloadAdapter(const std::string &adapterName, bool force = false) override;
55 
56     void AllAdapterSetMicMute(bool isMute) override;
57 
58     void SetAudioParameter(const std::string &adapterName, const AudioParamKey key, const std::string &condition,
59         const std::string &value) override;
60     std::string GetAudioParameter(const std::string &adapterName, const AudioParamKey key,
61         const std::string &condition) override;
62     int32_t SetVoiceVolume(const std::string &adapterName, float volume) override;
63     int32_t SetOutputRoute(const std::string &adapterName, const std::vector<DeviceType> &devices,
64         int32_t streamId) override;
65     int32_t SetInputRoute(const std::string &adapterName, DeviceType device, int32_t streamId,
66         int32_t inputType) override;
67     void SetMicMute(const std::string &adapterName, bool isMute) override;
68 
69     void *CreateRender(const std::string &adapterName, void *param, void *deviceDesc, uint32_t &hdiRenderId) override;
70     void DestroyRender(const std::string &adapterName, uint32_t hdiRenderId) override;
71     void *CreateCapture(const std::string &adapterName, void *param, void *deviceDesc, uint32_t &hdiCaptureId) override;
72     void DestroyCapture(const std::string &adapterName, uint32_t hdiCaptureId) override;
73 
74     void DumpInfo(std::string &dumpString) override;
75 
76 private:
77     void InitAudioManager(void);
78     std::shared_ptr<LocalAdapterWrapper> GetAdapter(const std::string &adapterName, bool tryCreate = false);
79     int32_t SwitchAdapterDesc(struct AudioAdapterDescriptor *descs, const std::string &adapterName, uint32_t size);
80     uint32_t GetPortId(const std::string &adapterName, enum AudioPortDirection portFlag);
81     int32_t SetOutputPortPin(DeviceType outputDevice, AudioRouteNode &sink);
82     int32_t SetInputPortPin(DeviceType inputDevice, AudioRouteNode &source);
83     void SaveSetParameter(const std::string &adapterName, const AudioParamKey key, const std::string &condition,
84         const std::string &value);
85 
86 private:
87     static constexpr uint32_t MAX_AUDIO_ADAPTER_NUM = 5;
88     static constexpr uid_t UID_BLUETOOTH_SA = 1002;
89 
90     struct IAudioManager *audioManager_ = nullptr;
91     struct HdfRemoteService *hdfRemoteService_ = nullptr;
92     struct HdfDeathRecipient *hdfDeathRecipient_ = nullptr;
93     std::unordered_map<std::string, std::shared_ptr<LocalAdapterWrapper> > adapters_;
94     std::mutex adapterMtx_;
95     std::vector<LocalParameter> reSetParams_;
96 };
97 
98 } // namespace AudioStandard
99 } // namespace OHOS
100 
101 #endif // LOCAL_DEVICE_MANAGER_H
102