• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2023 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 OHOS_CAMERA_H_CAMERA_DEVICE_MANAGER_H
17 #define OHOS_CAMERA_H_CAMERA_DEVICE_MANAGER_H
18 
19 #include <refbase.h>
20 #include <set>
21 #include "hcamera_device.h"
22 #include "camera_util.h"
23 #include "mem_mgr_client.h"
24 #include "safe_map.h"
25 
26 namespace OHOS {
27 namespace CameraStandard {
28 
29 
30 class CameraProcessPriority : public RefBase {
31 public:
CameraProcessPriority(int32_t uid,int32_t state,int32_t focusState)32     CameraProcessPriority(int32_t uid, int32_t state, int32_t focusState) : processUid_(uid),
33         processState_(state), focusState_(focusState) {}
34 
35     inline bool operator == (const CameraProcessPriority& rhs) const
36     {
37         return (this->processState_ == rhs.processState_) && (this->focusState_ == rhs.focusState_);
38     }
39 
40     inline bool operator < (const CameraProcessPriority& rhs) const
41     {
42         if (this->processUid_ < maxSysUid_) {
43             MEDIA_DEBUG_LOG("this->processUid_ :%{public}d", this->processUid_);
44             return true;
45         } else if (this->processUid_ >= maxSysUid_ && rhs.processUid_ < maxSysUid_) {
46             MEDIA_DEBUG_LOG("this->processUid_ :%{public}d, rhs.processUid_ :%{public}d",
47                 this->processUid_, rhs.processUid_);
48             return false;
49         }
50         if (this->processState_ == rhs.processState_) {
51             MEDIA_DEBUG_LOG("this->processState_ :%{public}d == rhs.processState_: %{public}d",
52                 this->processState_, rhs.processState_);
53             return this->focusState_ < rhs.focusState_;
54         } else {
55             MEDIA_DEBUG_LOG("this->processState:%{public}d, rhs.processState_:%{public}d",
56                 this->processState_, rhs.processState_);
57             return this->processState_ > rhs.processState_;
58         }
59     }
60 
61     inline bool operator > (const CameraProcessPriority& rhs) const
62     {
63         return rhs < *this;
64     }
65 
66     inline bool operator <= (const CameraProcessPriority& rhs) const
67     {
68         if (this->processUid_ < maxSysUid_ && rhs.processUid_ < maxSysUid_) {
69             return true;
70         }
71         return !(*this > rhs);
72     }
73 
74     inline bool operator >= (const CameraProcessPriority& rhs) const
75     {
76         if (this->processUid_ < maxSysUid_ && rhs.processUid_ < maxSysUid_) {
77             return false;
78         }
79         return !(*this < rhs);
80     }
81 
SetProcessState(int32_t state)82     inline void SetProcessState(int32_t state)
83     {
84         processState_ = state;
85     }
86 
SetProcessFocusState(int32_t focusState)87     inline void SetProcessFocusState(int32_t focusState)
88     {
89         focusState_ = focusState;
90     }
91 
SetProcessUid(int32_t uid)92     inline void SetProcessUid(int32_t uid)
93     {
94         processUid_ = uid;
95     }
96 
GetUid()97     inline int32_t GetUid() const{ return processUid_; }
98 
GetState()99     inline int32_t GetState() const { return processState_; }
100 
GetFocusState()101     inline int32_t GetFocusState() const { return focusState_; }
102 
103 private:
104     const int32_t maxSysUid_ = 10000;
105     int32_t processUid_;
106     int32_t processState_;
107     int32_t focusState_;
108 };
109 
110 class HCameraDeviceHolder : public RefBase {
111 public:
HCameraDeviceHolder(int32_t pid,int32_t uid,int32_t state,int32_t focusState,sptr<HCameraDevice> device,uint32_t accessTokenId,int32_t cost,const std::set<std::string> & conflicting,int32_t firstTokenId)112     HCameraDeviceHolder(int32_t pid, int32_t uid, int32_t state, int32_t focusState,
113         sptr<HCameraDevice> device, uint32_t accessTokenId, int32_t cost, const std::set<std::string> &conflicting,
114         int32_t firstTokenId)
115         :pid_(pid), uid_(uid), accessTokenId_(accessTokenId), device_(device), cost_(cost), conflicting_(conflicting),
116         firstTokenId_(firstTokenId)
117     {
118         processPriority_ = new CameraProcessPriority(uid, state, focusState);
119     }
SetPid(int32_t pid)120     inline void SetPid(int32_t pid) { pid_ = pid; }
SetUid(int32_t uid)121     inline void SetUid(int32_t uid) { uid_ = uid; }
SetState(int32_t state)122     inline void SetState(int32_t state)
123     {
124         processPriority_->SetProcessState(state);
125     }
SetFocusState(int32_t focusState)126     inline void SetFocusState(int32_t focusState)
127     {
128         processPriority_->SetProcessFocusState(focusState);
129     }
130 
SetPriorityUid(int32_t uid)131     inline void SetPriorityUid(int32_t uid)
132     {
133         processPriority_->SetProcessUid(uid);
134     }
135 
GetPid()136     inline int32_t GetPid() const {return pid_;}
137 
GetUid()138     inline int32_t GetUid() const{ return uid_; }
139 
GetState()140     inline int32_t GetState() const { return processPriority_->GetState(); }
141 
GetFocusState()142     inline int32_t GetFocusState() const { return processPriority_->GetFocusState(); }
143 
GetAccessTokenId()144     inline uint32_t GetAccessTokenId() const { return accessTokenId_; }
145 
GetDevice()146     inline sptr<HCameraDevice> GetDevice() const { return device_; }
147 
GetPriority()148     inline sptr<CameraProcessPriority> GetPriority() const {return processPriority_;}
149 
GetCost()150     inline int32_t GetCost() const{ return cost_; }
151 
IsConflicting(const std::string & cameraId)152     inline bool IsConflicting(const std::string &cameraId) const
153     {
154         std::string curCameraId = device_->GetCameraId();
155         if (cameraId == curCameraId) {
156             return true;
157         }
158         for (const auto &x : conflicting_) {
159             if (cameraId == x) {
160                 return true;
161             }
162         }
163         return false;
164     }
165 
GetFirstTokenID()166     inline uint32_t GetFirstTokenID() const { return firstTokenId_; }
167 
GetConflicting()168     inline std::set<std::string> GetConflicting() const { return conflicting_; }
169 
170 private:
171     int32_t pid_;
172     int32_t uid_;
173     uint32_t accessTokenId_;
174     sptr<CameraProcessPriority> processPriority_;
175     sptr<HCameraDevice> device_;
176     int32_t cost_;
177     std::set<std::string> conflicting_;
178     int32_t firstTokenId_;
179 };
180 
181 class CameraConcurrentSelector : public RefBase {
182 public:
183     CameraConcurrentSelector() = default;
184     ~CameraConcurrentSelector() = default;
185 
186     /**
187     * @brief Setting up a baseline camera and producing a table of concurrent cameras
188     */
189     void SetRequestCameraId(sptr<HCameraDeviceHolder> requestCameraHolder);
190 
191     /**
192     * @brief Check and save whether the camera can be retained.
193     */
194     bool SaveConcurrentCameras(std::vector<sptr<HCameraDeviceHolder>> holdersSortedByProprity,
195                                           sptr<HCameraDeviceHolder> holderWaitToConfirm);
196 
197     /**
198     * @brief Return to cameras that must be retained.
199     *
200     * @return Return to cameras that must be retained.
201     */
GetCamerasRetainable()202     inline std::vector<sptr<HCameraDeviceHolder>> GetCamerasRetainable()
203     {
204         return listOfCameraRetainable_;
205     }
206 
207     /**
208     * @brief Return the Concurrent List.
209     *
210     * @return Return the Concurrent List.
211     */
GetConcurrentCameraTable()212     inline std::vector<std::vector<std::int32_t>> GetConcurrentCameraTable()
213     {
214         return concurrentCameraTable_;
215     }
216 
217     bool CanOpenCameraconcurrently(std::vector<sptr<HCameraDeviceHolder>> reservedCameras,
218                                    std::vector<std::vector<std::int32_t>> concurrentCameraTable);
219 
220 private:
221     sptr<HCameraDeviceHolder> requestCameraHolder_;
222     std::vector<sptr<HCameraDeviceHolder>> listOfCameraRetainable_ = {};
223     std::vector<std::vector<std::int32_t>> concurrentCameraTable_ = {};
224     int32_t GetCameraIdNumber(std::string);
225     bool ConcurrentWithRetainedDevicesOrNot(sptr<HCameraDeviceHolder> cameraIdNeedConfirm);
226 };
227 
228 class HCameraDeviceManager : public RefBase {
229 public:
230     /**
231     * @brief the default maxinum "cost" allowed before evicting.
232     *
233     */
234     static constexpr int32_t DEFAULT_MAX_COST = 100;
235 
236     ~HCameraDeviceManager();
237     /**
238     * @brief Get camera device manager instance.
239     *
240     * @return Returns pointer to camera device manager instance.
241     */
242     static sptr<HCameraDeviceManager> &GetInstance();
243 
244     /**
245     * @brief Add opened device in camera device manager.
246     *
247     * @param device Device that have been turned on.
248     * @param pid Pid for opening the device.
249     */
250     void AddDevice(pid_t pid, sptr<HCameraDevice> device);
251 
252     /**
253     * @brief remove camera in camera device manager.
254     *
255     * @param device Device that have been turned off.
256     */
257     void RemoveDevice(const std::string &cameraId);
258 
259     /**
260     * @brief Get cameraHolder by active process pid.
261     *
262     * @param pid Pid of active process.
263     */
264     std::vector<sptr<HCameraDeviceHolder>> GetCameraHolderByPid(pid_t pid);
265 
266     /**
267     * @brief Get cameras by active process pid.
268     *
269     * @param pid Pid of active process.
270     */
271     std::vector<sptr<HCameraDevice>> GetCamerasByPid(pid_t pidRequest);
272 
273     /**
274     * @brief Get process pid device manager instance.
275     *
276     * @return Returns pointer to camera device manager instance.
277     */
278     std::vector<pid_t> GetActiveClient();
279 
280     /**
281     * @brief Get the existing holder object.
282     *
283     * @return Returns the list of existing holders.
284     */
285 
286     std::vector<sptr<HCameraDeviceHolder>> GetActiveCameraHolders();
287 
288     void SetStateOfACamera(std::string cameraId, int32_t state);
289 
290     bool IsMultiCameraActive(int32_t pid);
291 
292     void SetPeerCallback(const sptr<ICameraBroker>& callback);
293 
294     void UnsetPeerCallback();
295 
296     size_t GetActiveCamerasCount();
297 
298     SafeMap<std::string, int32_t> &GetCameraStateOfASide();
299 
300     /**
301     * @brief remove camera in camera device manager.
302     *
303     * @param camerasNeedEvict Devices that need to be shut down.
304     * @param cameraIdRequestOpen device is requested to turn on.
305     */
306     bool GetConflictDevices(std::vector<sptr<HCameraDevice>> &cameraNeedEvict, sptr<HCameraDevice> cameraIdRequestOpen,
307                             int32_t concurrentTypeOfRequest);
308     void UpdateCameraHolders(const std::vector<pid_t>& pidOfActiveClients,
309                             sptr<HCameraDeviceHolder> requestHolder);
310     /**
311     * @brief handle active camera evictions in camera device manager.
312     *
313     * @param evictedClients Devices that need to be shut down.
314     * @param cameraRequestOpen device is requested to turn on.
315     */
316     bool HandleCameraEvictions(std::vector<sptr<HCameraDeviceHolder>> &evictedClients,
317                                sptr<HCameraDeviceHolder> &cameraRequestOpen);
318 
319     bool IsProcessHasConcurrentDevice(pid_t pid);
320 
321     std::mutex mapMutex_;
322 private:
323     HCameraDeviceManager();
324     static sptr<HCameraDeviceManager> cameraDeviceManager_;
325     static std::mutex instanceMutex_;
326     std::map<pid_t, std::vector<sptr<HCameraDeviceHolder>>> pidToCameras_;
327     SafeMap<std::string, int32_t> stateOfRgmCamera_;
328     // LRU ordered, most recent at end
329     std::vector<sptr<HCameraDeviceHolder>> activeCameras_;
330     std::vector<sptr<HCameraDeviceHolder>> holderSortedByProprity_;
331     sptr<ICameraBroker> peerCallback_;
332     std::mutex peerCbMutex_;
333     sptr<CameraConcurrentSelector> concurrentSelector_;
334     std::string GetACameraId();
335     bool IsAllowOpen(pid_t activeClient);
336     int32_t GetCurrentCost() const;
337     void DetermineHighestPriorityOwner(int32_t &highestPriorityOwner, int32_t &owner,
338         sptr<CameraProcessPriority> requestPriority);
339     std::vector<sptr<HCameraDeviceHolder>> WouldEvict(sptr<HCameraDeviceHolder> &cameraRequestOpen);
340     void GenerateEachProcessCameraState(int32_t& processState, uint32_t processTokenId);
341     void PrintClientInfo(sptr<HCameraDeviceHolder> activeCameraHolder, sptr<HCameraDeviceHolder> requestCameraHolder);
342     sptr<HCameraDeviceHolder> GenerateCameraHolder(sptr<HCameraDevice> device, pid_t pid, int32_t uid,
343         uint32_t accessTokenId, uint32_t firstTokenId);
344     std::vector<sptr<HCameraDeviceHolder>> SortDeviceByPriority();
345     void RefreshCameraDeviceHolderState(sptr<HCameraDeviceHolder> requestCameraHolder);
346 };
347 } // namespace CameraStandard
348 } // namespace OHOS
349 #endif // OHOS_CAMERA_H_CAMERA_DEVICE_MANAGER_H