1 /*
2 * Copyright (c) 2025-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 #include "hcamera_session_manager.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <mutex>
21 #include <sched.h>
22
23 #include "camera_dynamic_loader.h"
24 #include "camera_util.h"
25 #include "hcamera_device_manager.h"
26 #include "hcapture_session.h"
27 #include "hmech_session.h"
28 #include "parameters.h"
29
30 namespace OHOS {
31 namespace CameraStandard {
32 static const int32_t GROUP_SIZE_MIN_LIMIT = 1; // Default session min size is 1
33 static const int32_t GROUP_SIZE_MAX_LIMIT = 10; // Default session max size is 10
34 static const int32_t MAX_SUPER_USER_ID = 100;
35 static int32_t g_mechLastUserid = 0;
36
GetGroupSizeLimit(pid_t pid)37 static size_t GetGroupSizeLimit(pid_t pid)
38 {
39 return HCameraDeviceManager::GetInstance()->IsProcessHasConcurrentDevice(pid) ? GROUP_SIZE_MAX_LIMIT :
40 GROUP_SIZE_MIN_LIMIT;
41 }
42
GetTotalSessionSize()43 size_t HCameraSessionManager::GetTotalSessionSize()
44 {
45 std::lock_guard<std::mutex> lock(totalSessionMapMutex_);
46 size_t totalSize = 0;
47 for (auto& it : totalSessionMap_) {
48 totalSize += it.second.size();
49 }
50 return totalSize;
51 }
52
GetGroupCount()53 size_t HCameraSessionManager::GetGroupCount()
54 {
55 std::lock_guard<std::mutex> lock(totalSessionMapMutex_);
56 return totalSessionMap_.size();
57 }
58
GetSessionSize(pid_t pid)59 size_t HCameraSessionManager::GetSessionSize(pid_t pid)
60 {
61 std::lock_guard<std::mutex> lock(totalSessionMapMutex_);
62 auto it = totalSessionMap_.find(pid);
63 return it != totalSessionMap_.end() ? it->second.size() : 0;
64 }
GetTotalSession()65 std::list<sptr<HCaptureSession>> HCameraSessionManager::GetTotalSession()
66 {
67 std::list<sptr<HCaptureSession>> totalList {};
68 std::lock_guard<std::mutex> lock(totalSessionMapMutex_);
69 for (auto& mapIt : totalSessionMap_) {
70 for (auto& listIt : mapIt.second) {
71 totalList.emplace_back(listIt);
72 }
73 }
74 return totalList;
75 }
76
GetGroupSessions(pid_t pid)77 std::list<sptr<HCaptureSession>> HCameraSessionManager::GetGroupSessions(pid_t pid)
78 {
79 std::list<sptr<HCaptureSession>> totalList {};
80 std::lock_guard<std::mutex> lock(totalSessionMapMutex_);
81 auto mapIt = totalSessionMap_.find(pid);
82 CHECK_RETURN_RET(mapIt == totalSessionMap_.end(), totalList);
83 for (auto& listIt : mapIt->second) {
84 totalList.emplace_back(listIt);
85 }
86 return totalList;
87 }
88
GetUserSessions(int32_t userId)89 std::vector<sptr<HCaptureSession>> HCameraSessionManager::GetUserSessions(int32_t userId)
90 {
91 std::vector<sptr<HCaptureSession>> userList {};
92 std::lock_guard<std::mutex> lock(totalSessionMapMutex_);
93 for (auto it = totalSessionMap_.begin(); it != totalSessionMap_.end(); ++it) {
94 for (auto& listIt : it->second) {
95 if (listIt->GetUserId() == userId) {
96 userList.emplace_back(listIt);
97 }
98 }
99 }
100 return userList;
101 }
102
GetGroupDefaultSession(pid_t pid)103 sptr<HCaptureSession> HCameraSessionManager::GetGroupDefaultSession(pid_t pid)
104 {
105 std::lock_guard<std::mutex> lock(totalSessionMapMutex_);
106 auto mapIt = totalSessionMap_.find(pid);
107 CHECK_RETURN_RET(mapIt == totalSessionMap_.end(), nullptr);
108 auto& list = mapIt->second;
109 return list.empty() ? nullptr : list.front();
110 }
111
GetMechSession(int32_t userId)112 sptr<HMechSession> HCameraSessionManager::GetMechSession(int32_t userId)
113 {
114 std::lock_guard<std::mutex> lock(mechMapMutex_);
115 auto mapIt = mechSessionMap_.find(userId);
116 if (mapIt == mechSessionMap_.end() && userId < MAX_SUPER_USER_ID) {
117 mapIt = mechSessionMap_.find(g_mechLastUserid);
118 }
119 CHECK_RETURN_RET(mapIt == mechSessionMap_.end(), nullptr);
120 return mapIt->second;
121 }
122
AddSession(sptr<HCaptureSession> session)123 CamServiceError HCameraSessionManager::AddSession(sptr<HCaptureSession> session)
124 {
125 CHECK_RETURN_RET(session == nullptr, CAMERA_INVALID_ARG);
126 auto pid = session->GetPid();
127 std::lock_guard<std::mutex> lock(totalSessionMapMutex_);
128 auto& list = totalSessionMap_[pid];
129 list.emplace_back(session);
130
131 return list.size() > GetGroupSizeLimit(pid) ? CAMERA_SESSION_MAX_INSTANCE_NUMBER_REACHED : CAMERA_OK;
132 }
133
AddSessionForPC(sptr<HCaptureSession> session)134 CamServiceError HCameraSessionManager::AddSessionForPC(sptr<HCaptureSession> session)
135 {
136 CHECK_RETURN_RET(session == nullptr, CAMERA_INVALID_ARG);
137 auto pid = session->GetPid();
138 std::lock_guard<std::mutex> lock(totalSessionMapMutex_);
139 auto& list = totalSessionMap_[pid];
140 list.emplace_back(session);
141 return CAMERA_OK;
142 }
143
AddMechSession(int32_t userId,sptr<HMechSession> mechSession)144 CamServiceError HCameraSessionManager::AddMechSession(int32_t userId,
145 sptr<HMechSession> mechSession)
146 {
147 if (mechSession == nullptr) {
148 return CAMERA_INVALID_ARG;
149 }
150 std::lock_guard<std::mutex> lock(mechMapMutex_);
151 mechSessionMap_.insert(std::make_pair(userId, mechSession));
152 g_mechLastUserid = userId;
153 return CAMERA_OK;
154 }
155
RemoveSession(sptr<HCaptureSession> session)156 void HCameraSessionManager::RemoveSession(sptr<HCaptureSession> session)
157 {
158 CHECK_RETURN(session == nullptr);
159 std::lock_guard<std::mutex> lock(totalSessionMapMutex_);
160 auto mapIt = totalSessionMap_.find(session->GetPid());
161 CHECK_RETURN(mapIt == totalSessionMap_.end());
162 auto& list = mapIt->second;
163 for (auto listIt = list.begin(); listIt != list.end(); listIt++) {
164 if (session == *listIt) {
165 list.erase(listIt);
166 break;
167 }
168 }
169 CHECK_RETURN(!list.empty());
170 RemoveGroupNoLock(mapIt);
171 }
172
RemoveMechSession(int32_t userId)173 void HCameraSessionManager::RemoveMechSession(int32_t userId)
174 {
175 std::lock_guard<std::mutex> lock(mechMapMutex_);
176 mechSessionMap_.erase(userId);
177 }
178
PreemptOverflowSessions(pid_t pid)179 void HCameraSessionManager::PreemptOverflowSessions(pid_t pid)
180 {
181 size_t limitSize = GetGroupSizeLimit(pid);
182 std::vector<sptr<HCaptureSession>> overflowSessions = {};
183 {
184 std::lock_guard<std::mutex> lock(totalSessionMapMutex_);
185 auto mapIt = totalSessionMap_.find(pid);
186 CHECK_RETURN(mapIt == totalSessionMap_.end());
187 auto& list = mapIt->second;
188 size_t currentListSize = list.size();
189 CHECK_RETURN(currentListSize <= limitSize);
190 size_t overflowSize = currentListSize - limitSize;
191 for (size_t i = 0; i < overflowSize; i++) {
192 auto previousSession = list.front();
193 overflowSessions.emplace_back(previousSession);
194 list.pop_front();
195 }
196 if (list.empty()) {
197 RemoveGroupNoLock(mapIt);
198 }
199 }
200 for (auto& session : overflowSessions) {
201 session->OnSessionPreempt();
202 }
203 }
204
RemoveGroup(pid_t pid)205 void HCameraSessionManager::RemoveGroup(pid_t pid)
206 {
207 std::lock_guard<std::mutex> lock(totalSessionMapMutex_);
208 auto mapIt = totalSessionMap_.find(pid);
209 CHECK_RETURN(mapIt == totalSessionMap_.end());
210 RemoveGroupNoLock(mapIt);
211 }
212
RemoveGroupNoLock(std::unordered_map<pid_t,SessionGroup>::iterator mapIt)213 void HCameraSessionManager::RemoveGroupNoLock(std::unordered_map<pid_t, SessionGroup>::iterator mapIt)
214 {
215 totalSessionMap_.erase(mapIt);
216 }
217 } // namespace CameraStandard
218 } // namespace OHOS
219