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 "session_manager.h"
17 
18 #include "dp_log.h"
19 #include "dps.h"
20 #include "session_command.h"
21 #include "session_coordinator.h"
22 
23 namespace OHOS {
24 namespace CameraStandard {
25 namespace DeferredProcessing {
26 
Create()27 std::shared_ptr<SessionManager> SessionManager::Create()
28 {
29     DP_DEBUG_LOG("entered.");
30     auto sessionManager = CreateShared<SessionManager>();
31     if (sessionManager) {
32         sessionManager->Initialize();
33     }
34     return sessionManager;
35 }
36 
SessionManager()37 SessionManager::SessionManager()
38     : initialized_(false),
39       photoSessionInfos_(),
40       videoSessionInfos_(),
41       coordinator_(std::make_shared<SessionCoordinator>())
42 {
43     DP_DEBUG_LOG("entered.");
44 }
45 
~SessionManager()46 SessionManager::~SessionManager()
47 {
48     DP_DEBUG_LOG("entered.");
49     initialized_ = false;
50     coordinator_ = nullptr;
51     photoSessionInfos_.clear();
52     videoSessionInfos_.Clear();
53 }
54 
Initialize()55 void SessionManager::Initialize()
56 {
57     coordinator_->Initialize();
58     initialized_ = true;
59     return;
60 }
61 
Start()62 void SessionManager::Start()
63 {
64     coordinator_->Start();
65     return;
66 }
67 
Stop()68 void SessionManager::Stop()
69 {
70     coordinator_->Stop();
71     return;
72 }
73 
CreateDeferredPhotoProcessingSession(const int32_t userId,const sptr<IDeferredPhotoProcessingSessionCallback> callback,std::shared_ptr<DeferredPhotoProcessor> processor,TaskManager * taskManager)74 sptr<IDeferredPhotoProcessingSession> SessionManager::CreateDeferredPhotoProcessingSession(const int32_t userId,
75     const sptr<IDeferredPhotoProcessingSessionCallback> callback, std::shared_ptr<DeferredPhotoProcessor> processor,
76     TaskManager* taskManager)
77 {
78     DP_CHECK_ERROR_RETURN_RET_LOG(!initialized_.load(), nullptr, "failed due to uninitialized.");
79 
80     DP_INFO_LOG("SessionManager::CreateDeferredPhotoProcessingSession create session for userId: %{public}d", userId);
81     for (auto it = photoSessionInfos_.begin(); it != photoSessionInfos_.end(); ++it) {
82         DP_DEBUG_LOG("dump photoSessionInfos_ userId: %{public}d", it->first);
83     }
84     std::lock_guard<std::mutex> lock(mutex_);
85     auto iter = photoSessionInfos_.find(userId);
86     if (iter != photoSessionInfos_.end()) {
87         DP_INFO_LOG("SessionManager::CreateDeferredPhotoProcessorSession failed due to photoSession already existed");
88         sptr<SessionInfo> sessionInfo = iter->second;
89         sessionInfo->SetCallback(callback);
90         coordinator_->NotifySessionCreated(userId, callback, taskManager);
91         return sessionInfo->GetDeferredPhotoProcessingSession();
92     }
93     sptr<SessionInfo> sessionInfo(new SessionInfo(userId, callback, this));
94     sessionInfo->CreateDeferredPhotoProcessingSession(userId, processor, taskManager, callback);
95     coordinator_->NotifySessionCreated(userId, callback, taskManager);
96     photoSessionInfos_[userId] = sessionInfo;
97     return sessionInfo->GetDeferredPhotoProcessingSession();
98 }
99 
GetImageProcCallbacks()100 std::shared_ptr<IImageProcessCallbacks> SessionManager::GetImageProcCallbacks()
101 {
102     DP_INFO_LOG("SessionManager::GetImageProcCallbacks enter.");
103     return coordinator_->GetImageProcCallbacks();
104 }
105 
GetCallback(const int32_t userId)106 sptr<IDeferredPhotoProcessingSessionCallback> SessionManager::GetCallback(const int32_t userId)
107 {
108     auto iter = photoSessionInfos_.find(userId);
109     if (iter != photoSessionInfos_.end()) {
110         DP_INFO_LOG("SessionManager::GetCallback");
111         sptr<SessionInfo> sessionInfo = iter->second;
112         return sessionInfo->GetRemoteCallback();
113     }
114     return nullptr;
115 }
116 
CreateDeferredVideoProcessingSession(const int32_t userId,const sptr<IDeferredVideoProcessingSessionCallback> callback)117 sptr<IDeferredVideoProcessingSession> SessionManager::CreateDeferredVideoProcessingSession(const int32_t userId,
118     const sptr<IDeferredVideoProcessingSessionCallback> callback)
119 {
120     DP_CHECK_ERROR_RETURN_RET_LOG(!initialized_.load(), nullptr, "failed due to uninitialized.");
121 
122     DP_INFO_LOG("create video session for userId: %{public}d", userId);
123     auto sessionInfo = GetSessionInfo(userId);
124     if (sessionInfo == nullptr) {
125         DP_INFO_LOG("video session creat susses");
126         sessionInfo = sptr<VideoSessionInfo>::MakeSptr(userId, callback);
127         videoSessionInfos_.Insert(userId, sessionInfo);
128     } else {
129         DP_DEBUG_LOG("video session already existed");
130         sessionInfo->SetCallback(callback);
131     }
132     auto ret = DPS_SendUrgentCommand<AddVideoSessionCommand>(sessionInfo);
133     DP_CHECK_ERROR_RETURN_RET_LOG(ret != DP_OK, nullptr, "AddVideoSession failed, ret: %{public}d", ret);
134 
135     return sessionInfo->GetDeferredVideoProcessingSession();
136 }
137 
GetSessionInfo(const int32_t userId)138 sptr<VideoSessionInfo> SessionManager::GetSessionInfo(const int32_t userId)
139 {
140     sptr<VideoSessionInfo> info;
141     DP_CHECK_RETURN_RET(videoSessionInfos_.Find(userId, info), info);
142 
143     DP_ERR_LOG("not get SessionInfo, userId: %{public}d", userId);
144     return nullptr;
145 }
146 
GetSessionCoordinator()147 std::shared_ptr<SessionCoordinator> SessionManager::GetSessionCoordinator()
148 {
149     return coordinator_;
150 }
151 
OnCallbackDied(const int32_t userId)152 void SessionManager::OnCallbackDied(const int32_t userId)
153 {
154     if (photoSessionInfos_.count(userId) != 0) {
155         coordinator_->NotifyCallbackDestroyed(userId);
156     }
157 }
158 } // namespace DeferredProcessing
159 } // namespace CameraStandard
160 } // namespace OHOS