• 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 "v5_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     std::mutex adapterMtx_;
34     struct AudioAdapterDescriptor adapterDesc_ = {};
35     std::unordered_set<uint32_t> hdiRenderIds_;
36     std::unordered_set<uint32_t> hdiCaptureIds_;
37     std::mutex renderMtx_;
38     std::mutex captureMtx_;
39     int32_t routeHandle_ = -1;
40 } LocalAdapterWrapper;
41 
42 typedef struct LocalParameter {
43     std::string adapterName_ = "";
44     AudioParamKey key_ = AudioParamKey::NONE;
45     std::string condition_ = "";
46     std::string value_ = "";
47 } LocalParameter;
48 
49 class LocalDeviceManager : public IDeviceManager {
50 public:
51     LocalDeviceManager() = default;
52     ~LocalDeviceManager() = default;
53 
54     int32_t LoadAdapter(const std::string &adapterName) override;
55     void UnloadAdapter(const std::string &adapterName, bool force = false) override;
56 
57     void AllAdapterSetMicMute(bool isMute) override;
58 
59     void SetAudioParameter(const std::string &adapterName, const AudioParamKey key, const std::string &condition,
60         const std::string &value) override;
61     std::string GetAudioParameter(const std::string &adapterName, const AudioParamKey key,
62         const std::string &condition) override;
63     int32_t SetVoiceVolume(const std::string &adapterName, float volume) override;
64     int32_t SetOutputRoute(const std::string &adapterName, const std::vector<DeviceType> &devices,
65         int32_t streamId) override;
66     int32_t SetInputRoute(const std::string &adapterName, DeviceType device, int32_t streamId,
67         int32_t inputType) override;
68     void SetMicMute(const std::string &adapterName, bool isMute) override;
69 
70     void *CreateRender(const std::string &adapterName, void *param, void *deviceDesc, uint32_t &hdiRenderId) override;
71     void DestroyRender(const std::string &adapterName, uint32_t hdiRenderId) override;
72     void *CreateCapture(const std::string &adapterName, void *param, void *deviceDesc, uint32_t &hdiCaptureId) override;
73     void DestroyCapture(const std::string &adapterName, uint32_t hdiCaptureId) override;
74 
75     void DumpInfo(std::string &dumpString) override;
76 
77     void SetDmDeviceType(uint16_t dmDeviceType, DeviceType deviceType) override;
78 
79     void SetAudioScene(const AudioScene scene) override;
80 
81 private:
82     void InitAudioManager(void);
83     std::shared_ptr<LocalAdapterWrapper> GetAdapter(const std::string &adapterName, bool tryCreate = false);
84     int32_t SwitchAdapterDesc(struct AudioAdapterDescriptor *descs, const std::string &adapterName, uint32_t size);
85     uint32_t GetPortId(const std::string &adapterName, enum AudioPortDirection portFlag);
86     int32_t SetOutputPortPin(DeviceType outputDevice, AudioRouteNode &sink);
87     int32_t SetInputPortPin(DeviceType inputDevice, AudioRouteNode &source);
88     void SaveSetParameter(const std::string &adapterName, const AudioParamKey key, const std::string &condition,
89         const std::string &value);
90     int32_t HandleNearlinkScene(DeviceType deviceType, AudioRouteNode &node);
91 
92 private:
93     static constexpr uint32_t MAX_AUDIO_ADAPTER_NUM = 5;
94     static constexpr uid_t UID_BLUETOOTH_SA = 1002;
95 
96     struct IAudioManager *audioManager_ = nullptr;
97     struct HdfRemoteService *hdfRemoteService_ = nullptr;
98     struct HdfDeathRecipient *hdfDeathRecipient_ = nullptr;
99     std::unordered_map<std::string, std::shared_ptr<LocalAdapterWrapper> > adapters_;
100     std::mutex adapterMtx_;
101     std::vector<LocalParameter> reSetParams_;
102     std::mutex reSetParamsMtx_;
103     std::unordered_map<DeviceType, uint16_t> dmDeviceTypeMap_;
104 
105     std::atomic<AudioScene> currentAudioScene_ = AUDIO_SCENE_DEFAULT;
106 };
107 
108 } // namespace AudioStandard
109 } // namespace OHOS
110 
111 #endif // LOCAL_DEVICE_MANAGER_H
112