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 #include "deferred_processing_service.h"
17 
18 #include "events_info.h"
19 #include "events_monitor.h"
20 #include "dp_log.h"
21 #include "dps.h"
22 
23 namespace OHOS {
24 namespace CameraStandard {
25 namespace DeferredProcessing {
DeferredProcessingService()26 DeferredProcessingService::DeferredProcessingService()
27 {
28     DP_DEBUG_LOG("entered.");
29 }
30 
~DeferredProcessingService()31 DeferredProcessingService::~DeferredProcessingService()
32 {
33     DP_DEBUG_LOG("entered.");
34     DP_CHECK_RETURN(!initialized_.load());
35 
36     photoTaskManagerMap_.clear();
37     DPS_Destroy();
38 }
39 
Initialize()40 void DeferredProcessingService::Initialize()
41 {
42     DP_DEBUG_LOG("entered.");
43     DP_CHECK_RETURN(initialized_.load());
44 
45     DPS_Initialize();
46     EventsMonitor::GetInstance().Initialize();
47     EventsInfo::GetInstance().Initialize();
48     initialized_.store(true);
49 }
50 
Start()51 void DeferredProcessingService::Start()
52 {
53     DP_DEBUG_LOG("entered.");
54 }
55 
Stop()56 void DeferredProcessingService::Stop()
57 {
58     DP_DEBUG_LOG("entered.");
59 }
60 
NotifyLowQualityImage(const int32_t userId,const std::string & imageId,std::shared_ptr<Media::Picture> picture)61 void DeferredProcessingService::NotifyLowQualityImage(const int32_t userId, const std::string& imageId,
62     std::shared_ptr<Media::Picture> picture)
63 {
64     DP_INFO_LOG("entered.");
65     auto sessionManager = DPS_GetSessionManager();
66     if (sessionManager != nullptr && sessionManager->GetCallback(userId) != nullptr) {
67         sessionManager->GetCallback(userId)->OnDeliveryLowQualityImage(imageId, picture);
68     } else {
69         DP_INFO_LOG("DeferredPhotoProcessingSessionCallback::NotifyLowQualityImage not set!, Discarding callback");
70     }
71 }
72 
CreateDeferredPhotoProcessingSession(const int32_t userId,const sptr<IDeferredPhotoProcessingSessionCallback> callbacks)73 sptr<IDeferredPhotoProcessingSession> DeferredProcessingService::CreateDeferredPhotoProcessingSession(
74     const int32_t userId, const sptr<IDeferredPhotoProcessingSessionCallback> callbacks)
75 {
76     DP_INFO_LOG("DeferredProcessingService::CreateDeferredPhotoProcessingSession create session, userId: %{public}d",
77         userId);
78     TaskManager* taskManager = GetPhotoTaskManager(userId);
79     auto schedulerManager = DPS_GetSchedulerManager();
80     auto sessionManager = DPS_GetSessionManager();
81     if (schedulerManager == nullptr || sessionManager == nullptr) {
82         DP_ERR_LOG("schedulerManager or sessionManager is nullptr.");
83         return nullptr;
84     }
85     std::shared_ptr<IImageProcessCallbacks> sessionImageProcCallbacks = sessionManager->GetImageProcCallbacks();
86     auto processor = schedulerManager->GetPhotoProcessor(userId, taskManager, sessionImageProcCallbacks);
87     sptr<IDeferredPhotoProcessingSession> session = sessionManager->CreateDeferredPhotoProcessingSession(userId,
88         callbacks, processor, taskManager);
89     return session;
90 }
91 
CreateDeferredVideoProcessingSession(const int32_t userId,const sptr<IDeferredVideoProcessingSessionCallback> callbacks)92 sptr<IDeferredVideoProcessingSession> DeferredProcessingService::CreateDeferredVideoProcessingSession(
93     const int32_t userId, const sptr<IDeferredVideoProcessingSessionCallback> callbacks)
94 {
95     DP_INFO_LOG("create video session, userId: %{public}d", userId);
96     auto sessionManager = DPS_GetSessionManager();
97     DP_CHECK_ERROR_RETURN_RET_LOG(sessionManager == nullptr, nullptr,
98         "SessionManager is null, userId: %{public}d", userId);
99     return sessionManager->CreateDeferredVideoProcessingSession(userId, callbacks);
100 }
101 
GetPhotoTaskManager(const int32_t userId)102 TaskManager* DeferredProcessingService::GetPhotoTaskManager(const int32_t userId)
103 {
104     std::lock_guard<std::mutex> lock(taskManagerMutex_);
105     DP_INFO_LOG("entered, userId: %{public}d", userId);
106     if (photoTaskManagerMap_.count(userId) == 0) {
107         constexpr uint32_t numThreads = 1;
108         std::shared_ptr<TaskManager> taskManager =
109             std::make_shared<TaskManager>("PhotoProcTaskManager_userid_" + std::to_string(userId),
110             numThreads, true);
111         photoTaskManagerMap_[userId] = taskManager;
112     }
113     return photoTaskManagerMap_[userId].get();
114 }
115 
NotifyCameraSessionStatus(const int32_t userId,const std::string & cameraId,bool running,bool isSystemCamera)116 void DeferredProcessingService::NotifyCameraSessionStatus(const int32_t userId, const std::string& cameraId,
117     bool running, bool isSystemCamera)
118 {
119     DP_INFO_LOG("entered, userId: %{public}d, cameraId: %s, running: %{public}d, isSystemCamera: %{public}d: ",
120         userId, cameraId.c_str(), running, isSystemCamera);
121     EventsMonitor::GetInstance().NotifyCameraSessionStatus(userId, cameraId, running, isSystemCamera);
122 }
123 } // namespace DeferredProcessing
124 } // namespace CameraStandard
125 } // namespace OHOS