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