1 /*
2 * Copyright (c) 2023-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 "session_manager.h"
17
18 #include "dp_log.h"
19 #include "dps.h"
20 #include "session_command.h"
21
22 namespace OHOS {
23 namespace CameraStandard {
24 namespace DeferredProcessing {
SessionManager()25 SessionManager::SessionManager()
26 {
27 DP_DEBUG_LOG("entered.");
28 }
29
~SessionManager()30 SessionManager::~SessionManager()
31 {
32 DP_INFO_LOG("entered.");
33 photoSessionInfos_.clear();
34 videoSessionInfos_.clear();
35 }
36
Initialize()37 int32_t SessionManager::Initialize()
38 {
39 coordinator_ = SessionCoordinator::Create();
40 DP_CHECK_ERROR_RETURN_RET_LOG(coordinator_ == nullptr, DP_INIT_FAIL, "SessionCoordinator init failed.");
41
42 initialized_.store(true);
43 return DP_OK;
44 }
45
Start()46 void SessionManager::Start()
47 {
48 coordinator_->Start();
49 }
50
Stop()51 void SessionManager::Stop()
52 {
53 coordinator_->Stop();
54 }
55
CreateDeferredPhotoProcessingSession(const int32_t userId,const sptr<IDeferredPhotoProcessingSessionCallback> & callback)56 sptr<IDeferredPhotoProcessingSession> SessionManager::CreateDeferredPhotoProcessingSession(const int32_t userId,
57 const sptr<IDeferredPhotoProcessingSessionCallback>& callback)
58 {
59 DP_CHECK_ERROR_RETURN_RET_LOG(!initialized_.load(), nullptr, "failed due to uninitialized.");
60
61 DP_INFO_LOG("Create photo session for userId: %{public}d", userId);
62 auto sessionInfo = GetPhotoInfo(userId);
63 if (sessionInfo == nullptr) {
64 DP_INFO_LOG("Photo session creat susses");
65 sessionInfo = sptr<PhotoSessionInfo>::MakeSptr(userId, callback);
66 } else {
67 DP_DEBUG_LOG("Photo session already existed");
68 sessionInfo->SetCallback(callback);
69 }
70 auto ret = DPS_SendUrgentCommand<AddPhotoSessionCommand>(sessionInfo);
71 DP_CHECK_ERROR_RETURN_RET_LOG(ret != DP_OK, nullptr, "AddPhotoSession failed, ret: %{public}d", ret);
72
73 return sessionInfo->GetDeferredPhotoProcessingSession();
74 }
75
GetPhotoInfo(const int32_t userId)76 sptr<PhotoSessionInfo> SessionManager::GetPhotoInfo(const int32_t userId)
77 {
78 std::lock_guard<std::mutex> lock(photoSessionMutex_);
79 auto it = photoSessionInfos_.find(userId);
80 DP_CHECK_ERROR_RETURN_RET_LOG(it == photoSessionInfos_.end(), nullptr,
81 "Not find PhotoSessionInfo for userId: %{public}d", userId);
82 DP_INFO_LOG("DPS_PHOTO: Photo user count: %{public}zu", photoSessionInfos_.size());
83 return it->second;
84 }
85
AddPhotoSession(const sptr<PhotoSessionInfo> & sessionInfo)86 void SessionManager::AddPhotoSession(const sptr<PhotoSessionInfo>& sessionInfo)
87 {
88 std::lock_guard<std::mutex> lock(photoSessionMutex_);
89 int32_t userId = sessionInfo->GetUserId();
90 DP_INFO_LOG("Add photo session userId: %{public}d", userId);
91 photoSessionInfos_[userId] = sessionInfo;
92 }
93
DeletePhotoSession(const int32_t userId)94 void SessionManager::DeletePhotoSession(const int32_t userId)
95 {
96 std::lock_guard<std::mutex> lock(photoSessionMutex_);
97 if (photoSessionInfos_.erase(userId) > 0) {
98 DP_INFO_LOG("Delete photo session userId: %{public}d", userId);
99 }
100 }
101
GetCallback(const int32_t userId)102 sptr<IDeferredPhotoProcessingSessionCallback> SessionManager::GetCallback(const int32_t userId)
103 {
104 if (auto sessionInfo = GetPhotoInfo(userId)) {
105 return sessionInfo->GetRemoteCallback();
106 }
107 return nullptr;
108 }
109
CreateDeferredVideoProcessingSession(const int32_t userId,const sptr<IDeferredVideoProcessingSessionCallback> & callback)110 sptr<IDeferredVideoProcessingSession> SessionManager::CreateDeferredVideoProcessingSession(const int32_t userId,
111 const sptr<IDeferredVideoProcessingSessionCallback>& callback)
112 {
113 DP_CHECK_ERROR_RETURN_RET_LOG(!initialized_.load(), nullptr, "failed due to uninitialized.");
114
115 DP_INFO_LOG("Create video session for userId: %{public}d", userId);
116 std::lock_guard<std::mutex> lock(videoSessionMutex_);
117 auto sessionInfo = GetVideoInfo(userId);
118 if (sessionInfo == nullptr) {
119 DP_INFO_LOG("Video session creat susses");
120 sessionInfo = sptr<VideoSessionInfo>::MakeSptr(userId, callback);
121 videoSessionInfos_.emplace(userId, sessionInfo);
122 } else {
123 DP_DEBUG_LOG("Video session already existed");
124 sessionInfo->SetCallback(callback);
125 }
126 auto ret = DPS_SendUrgentCommand<AddVideoSessionCommand>(sessionInfo);
127 DP_CHECK_ERROR_RETURN_RET_LOG(ret != DP_OK, nullptr, "AddVideoSession failed, ret: %{public}d", ret);
128
129 return sessionInfo->GetDeferredVideoProcessingSession();
130 }
131
GetVideoInfo(const int32_t userId)132 sptr<VideoSessionInfo> SessionManager::GetVideoInfo(const int32_t userId)
133 {
134 auto it = videoSessionInfos_.find(userId);
135 DP_CHECK_ERROR_RETURN_RET_LOG(it == videoSessionInfos_.end(), nullptr,
136 "Not find VideoSessionInfo for userId: %{public}d", userId);
137
138 return it->second;
139 }
140
GetSessionCoordinator()141 std::shared_ptr<SessionCoordinator> SessionManager::GetSessionCoordinator()
142 {
143 return coordinator_;
144 }
145 } // namespace DeferredProcessing
146 } // namespace CameraStandard
147 } // namespace OHOS
148