• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 #ifndef ST_AUDIO_AFFINITY_MANAGER_H
16 #define ST_AUDIO_AFFINITY_MANAGER_H
17 
18 #include <vector>
19 #include <string>
20 #include <memory>
21 #include <unordered_map>
22 #include "audio_device_info.h"
23 #include "audio_system_manager.h"
24 
25 namespace OHOS {
26 namespace AudioStandard {
27 
28 using AFFINITYDEVINFOMAP = std::unordered_map<std::string, std::unordered_map<int32_t, AffinityDeviceInfo>>;
29 using AFFINITYDEVMAP = std::unordered_map<int32_t, std::shared_ptr<AudioDeviceDescriptor>>;
30 
31 class AudioAffinityManager {
32 public:
GetAudioAffinityManager()33     static AudioAffinityManager& GetAudioAffinityManager()
34     {
35         static AudioAffinityManager audioAffinityManager;
36         return audioAffinityManager;
37     }
38 
39     void ParseAffinityXml();
40 
41     void OnXmlParsingCompleted(std::vector<AffinityDeviceInfo> &xmlData);
42 
43     std::shared_ptr<AudioDeviceDescriptor> GetRendererDevice(int32_t clientUID);
44     std::shared_ptr<AudioDeviceDescriptor> GetCapturerDevice(int32_t clientUID);
45 
46     void AddSelectRendererDevice(int32_t clientUID, const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor);
47     void RemoveOfflineRendererDevice(const AudioDeviceDescriptor &updatedDesc);
48     void DelSelectRendererDevice(int32_t clientUID);
49 
50     void AddSelectCapturerDevice(int32_t clientUID, const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor);
51     void RemoveOfflineCapturerDevice(const AudioDeviceDescriptor &updatedDesc);
52     void DelSelectCapturerDevice(int32_t clientUID);
53 
54 private:
AudioAffinityManager()55     AudioAffinityManager(){};
~AudioAffinityManager()56     ~AudioAffinityManager(){};
57 
58     AffinityDeviceInfo GetAffinityDeviceInfoByDeviceType(
59         const std::vector<AffinityDeviceInfo> &affinityDeviceInfoArray,
60         const DeviceType &deviceType, const std::string &networkID);
61     std::unordered_map<int32_t, AffinityDeviceInfo> GetActiveAffinityDeviceMapByGroupName(
62         const AFFINITYDEVINFOMAP &activeGroupNameMap, const std::string &groupName);
63     int32_t GetAffinityClientUID(const int32_t &clientUID,
64         std::unordered_map<int32_t, AffinityDeviceInfo> &affinityDeviceInfoMap);
65     int32_t GetAffinityClientUID(const int32_t &clientUID, const DeviceType &deviceType, const std::string &networkID,
66         const std::vector<AffinityDeviceInfo> &affinityDeviceInfoArray,
67         AFFINITYDEVINFOMAP &activeGroupNameMap);
68 
69     void DelActiveGroupAffinityMap(const int32_t &clientUID,
70         std::unordered_map<int32_t, AffinityDeviceInfo> &affinityDeviceInfoMap);
71     void DelActiveGroupAffinityMap(const int32_t &clientUID, const DeviceType &deviceType,
72         const std::string &networkID, std::vector<AffinityDeviceInfo> &affinityDeviceArray,
73         AFFINITYDEVINFOMAP &activeGroupNameMap);
74 
75     AFFINITYDEVMAP activeRendererDeviceMap_;
76     AFFINITYDEVMAP activeCapturerDeviceMap_;
77 
78     std::vector<AffinityDeviceInfo> rendererAffinityDeviceArray_;
79     std::vector<AffinityDeviceInfo> capturerAffinityDeviceArray_;
80 
81     AFFINITYDEVINFOMAP activeRendererGroupAffinityMap_;
82     AFFINITYDEVINFOMAP activeCapturerGroupAffinityMap_;
83 
84     std::mutex rendererMapMutex_;
85     std::mutex capturerMapMutex_;
86 };
87 } // namespace AudioStandard
88 } // namespace OHOS
89 #endif //ST_AUDIO_AFFINITY_MANAGER_H
90