• 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 REMOTE_DEVICE_MANAGER_H
17 #define REMOTE_DEVICE_MANAGER_H
18 
19 #include <iostream>
20 #include <cstring>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <mutex>
24 #include <v1_0/iaudio_manager.h>
25 #include <v1_0/iaudio_callback.h>
26 #include <v1_0/audio_types.h>
27 #include "audio_info.h"
28 #include "adapter/i_device_manager.h"
29 
30 namespace OHOS {
31 namespace AudioStandard {
32 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::IAudioAdapter RemoteIAudioAdapter;
33 typedef struct OHOS::HDI::DistributedAudio::Audio::V1_0::AudioAdapterDescriptor RemoteAudioAdapterDescriptor;
34 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::IAudioCallback RemoteIAudioCallback;
35 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioCallbackType RemoteAudioCallbackType;
36 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioExtParamKey RemoteAudioExtParamKey;
37 typedef enum OHOS::HDI::DistributedAudio::Audio::V1_0::AudioPortDirection RemoteAudioPortDirection;
38 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::IAudioManager RemoteIAudioManager;
39 typedef OHOS::HDI::DistributedAudio::Audio::V1_0::AudioRouteNode RemoteAudioRouteNode;
40 
41 class RemoteAdapterHdiCallback final : public RemoteIAudioCallback {
42 public:
43     RemoteAdapterHdiCallback(const std::string &adapterName);
~RemoteAdapterHdiCallback()44     ~RemoteAdapterHdiCallback() override {}
45 
46     int32_t RenderCallback(RemoteAudioCallbackType type, int8_t &reserved, int8_t &cookie) override;
47     int32_t ParamCallback(RemoteAudioExtParamKey key, const std::string &condition, const std::string &value,
48         int8_t &reserved, int8_t cookie) override;
49 
50 private:
51     const std::string adapterName_;
52 };
53 
54 typedef struct RemoteAdapterWrapper {
RemoteAdapterWrapperRemoteAdapterWrapper55     RemoteAdapterWrapper(const std::string &adapterName) : adapterName_(adapterName) {}
56 
57     sptr<RemoteIAudioAdapter> adapter_ = nullptr;
58     std::string adapterName_ = "";
59     std::mutex adapterMtx_;
60     RemoteAudioAdapterDescriptor adapterDesc_ = {};
61     std::unordered_set<uint32_t> hdiRenderIds_;
62     std::unordered_set<uint32_t> hdiCaptureIds_;
63     std::mutex renderMtx_;
64     std::mutex captureMtx_;
65     int32_t routeHandle_ = -1;
66     std::unordered_map<uint32_t, IDeviceManagerCallback *> renderCallbacks_;
67     std::unordered_map<uint32_t, IDeviceManagerCallback *> captureCallbacks_;
68     std::mutex renderCallbackMtx_;
69     std::mutex captureCallbackMtx_;
70     sptr<RemoteIAudioCallback> hdiCallback_ = nullptr;
71     bool isValid_ = true;
72 } RemoteAdapterWrapper;
73 
74 class RemoteDeviceManager : public IDeviceManager {
75 public:
76     RemoteDeviceManager() = default;
77     ~RemoteDeviceManager() = default;
78 
79     int32_t LoadAdapter(const std::string &adapterName) override;
80     void UnloadAdapter(const std::string &adapterName, bool force = false) override;
81 
82     void AllAdapterSetMicMute(bool isMute) override;
83 
84     void SetAudioParameter(const std::string &adapterName, const AudioParamKey key, const std::string &condition,
85         const std::string &value) override;
86     std::string GetAudioParameter(const std::string &adapterName, const AudioParamKey key,
87         const std::string &condition) override;
88     int32_t SetVoiceVolume(const std::string &adapterName, float volume) override;
89     int32_t SetOutputRoute(const std::string &adapterName, const std::vector<DeviceType> &devices,
90         int32_t streamId) override;
91     int32_t SetInputRoute(const std::string &adapterName, DeviceType device, int32_t streamId,
92         int32_t inputType) override;
93     void SetMicMute(const std::string &adapterName, bool isMute) override;
94     int32_t HandleEvent(const std::string &adapterName, const AudioParamKey key, const char *condition,
95         const char *value, void *reserved) override;
96     void RegistRenderSinkCallback(const std::string &adapterName, uint32_t hdiRenderId,
97         IDeviceManagerCallback *callback) override;
98     void RegistCaptureSourceCallback(const std::string &adapterName, uint32_t hdiCaptureId,
99         IDeviceManagerCallback *callback) override;
100     void UnRegistRenderSinkCallback(const std::string &adapterName, uint32_t hdiRenderId) override;
101     void UnRegistCaptureSourceCallback(const std::string &adapterName, uint32_t hdiCaptureId) override;
102 
103     void *CreateRender(const std::string &adapterName, void *param, void *deviceDesc, uint32_t &hdiRenderId) override;
104     void DestroyRender(const std::string &adapterName, uint32_t hdiRenderId) override;
105     void *CreateCapture(const std::string &adapterName, void *param, void *deviceDesc, uint32_t &hdiCaptureId) override;
106     void DestroyCapture(const std::string &adapterName, uint32_t hdiCaptureId) override;
107 
108     void DumpInfo(std::string &dumpString) override;
109 
110     void SetDmDeviceType(uint16_t dmDeviceType, DeviceType deviceType) override;
111 
112     void SetAudioScene(const AudioScene scene) override;
113 
114 private:
115     void InitAudioManager(void);
116     std::shared_ptr<RemoteAdapterWrapper> GetAdapter(const std::string &adapterName, bool tryCreate = false);
117     int32_t SwitchAdapterDesc(const std::vector<RemoteAudioAdapterDescriptor> &descs, const std::string &adapterName);
118     uint32_t GetPortId(RemoteAudioPortDirection portFlag);
119     int32_t HandleStateChangeEvent(const std::string &adapterName, const AudioParamKey key, const char *condition,
120         const char *value);
121     int32_t HandleRenderParamEvent(const std::string &adapterName, const AudioParamKey key, const char *condition,
122         const char *value);
123     int32_t HandleCaptureParamEvent(const std::string &adapterName, const AudioParamKey key, const char *condition,
124         const char *value);
125     int32_t SetOutputPortPin(DeviceType outputDevice, RemoteAudioRouteNode &sink);
126     int32_t SetInputPortPin(DeviceType inputDevice, RemoteAudioRouteNode &source);
127 
128 private:
129     static constexpr uint32_t MAX_AUDIO_ADAPTER_NUM = 5;
130     static constexpr int32_t EVENT_DES_SIZE = 60;
131     static constexpr int32_t ADAPTER_STATE_CONTENT_DES_SIZE = 60;
132     static constexpr int32_t PARAMS_STATE_NUM = 2;
133     static constexpr char DAUDIO_DEV_TYPE_SPK = '1';
134     static constexpr char DAUDIO_DEV_TYPE_MIC = '2';
135 
136     sptr<RemoteIAudioManager> audioManager_ = nullptr;
137     std::mutex managerMtx_;
138     std::unordered_map<std::string, std::shared_ptr<RemoteAdapterWrapper> > adapters_;
139     std::mutex adapterMtx_;
140 };
141 
142 } // namespace AudioStandard
143 } // namespace OHOS
144 
145 #endif // REMOTE_DEVICE_MANAGER_H
146