• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 LOG_TAG
16 #define LOG_TAG "AudioRouteMap"
17 #endif
18 
19 #include "audio_router_map.h"
20 #include "parameter.h"
21 #include "parameters.h"
22 #include "audio_utils.h"
23 #include "audio_policy_log.h"
24 #include "audio_errors.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 static const size_t FAST_ROUTE_LIMIT = 1024;
29 
GetDeviceInfoByUidAndPid(int32_t uid,int32_t pid)30 std::string AudioRouteMap::GetDeviceInfoByUidAndPid(int32_t uid, int32_t pid)
31 {
32     std::string selectedDevice = "";
33     std::lock_guard<std::mutex> lock(routerMapMutex_);
34     if (!routerMap_.count(uid)) {
35         AUDIO_INFO_LOG("GetSelectedDeviceInfo no such uid[%{public}d]", uid);
36         return "";
37     }
38     if (routerMap_[uid].second == pid) {
39         selectedDevice = routerMap_[uid].first;
40     } else if (routerMap_[uid].second == -1) {
41         routerMap_[uid].second = pid;
42         selectedDevice = routerMap_[uid].first;
43     } else {
44         AUDIO_INFO_LOG("GetSelectedDeviceInfo: uid[%{public}d] changed pid, get local as defalut", uid);
45         routerMap_.erase(uid);
46         selectedDevice = LOCAL_NETWORK_ID;
47     }
48     return selectedDevice;
49 }
50 
DelRouteMapInfoByKey(int32_t uid)51 bool AudioRouteMap::DelRouteMapInfoByKey(int32_t uid)
52 {
53     std::lock_guard<std::mutex> lock(routerMapMutex_);
54     return routerMap_.erase(uid);
55 }
56 
AddRouteMapInfo(int32_t uid,std::string device,int32_t pid)57 void AudioRouteMap::AddRouteMapInfo(int32_t uid, std::string device, int32_t pid)
58 {
59     std::lock_guard<std::mutex> lock(routerMapMutex_);
60     routerMap_[uid] = std::pair(device, pid);
61 }
62 
AddFastRouteMapInfo(int32_t uid,std::string device,DeviceRole role)63 int32_t AudioRouteMap::AddFastRouteMapInfo(int32_t uid, std::string device, DeviceRole role)
64 {
65     std::lock_guard<std::mutex> lock(fastRouterMapMutex_);
66     if (fastRouterMap_.size() > FAST_ROUTE_LIMIT) {
67         return ERROR;
68     }
69     fastRouterMap_[uid] = std::make_pair(device, role);
70     return SUCCESS;
71 }
72 
RemoveDeviceInRouterMap(std::string networkId)73 void AudioRouteMap::RemoveDeviceInRouterMap(std::string networkId)
74 {
75     std::lock_guard<std::mutex> lock(routerMapMutex_);
76     std::unordered_map<int32_t, std::pair<std::string, int32_t>>::iterator it;
77     for (it = routerMap_.begin();it != routerMap_.end();) {
78         if (it->second.first == networkId) {
79             it = routerMap_.erase(it);
80         } else {
81             it++;
82         }
83     }
84 }
85 
RemoveDeviceInFastRouterMap(std::string networkId)86 void AudioRouteMap::RemoveDeviceInFastRouterMap(std::string networkId)
87 {
88     std::lock_guard<std::mutex> lock(fastRouterMapMutex_);
89     std::unordered_map<int32_t, std::pair<std::string, DeviceRole>>::iterator it;
90     for (it = fastRouterMap_.begin();it != fastRouterMap_.end();) {
91         if (it->second.first == networkId) {
92             it = fastRouterMap_.erase(it);
93         } else {
94             it++;
95         }
96     }
97 }
98 
GetNetworkIDInFastRouterMap(int32_t uid,DeviceRole role,std::string & newworkId)99 void AudioRouteMap::GetNetworkIDInFastRouterMap(int32_t uid, DeviceRole role, std::string& newworkId)
100 {
101     std::lock_guard<std::mutex> lock(fastRouterMapMutex_);
102     if (fastRouterMap_.count(uid) &&
103         fastRouterMap_[uid].second == role) {
104         newworkId = fastRouterMap_[uid].first;
105         AUDIO_INFO_LOG("use networkid in fastRouterMap_ :%{public}s ", GetEncryptStr(newworkId).c_str());
106     }
107 }
108 
109 }
110 }
111