1 /*
2 * Copyright (c) 2023-2024 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_coordinator.h"
17 #include <mutex>
18
19 #include "dp_log.h"
20 #include "buffer_info.h"
21 #include "dps_event_report.h"
22 #include "picture_interface.h"
23 #include "steady_clock.h"
24 #include "picture.h"
25 #include "video_session_info.h"
26
27 namespace OHOS {
28 namespace CameraStandard {
29 namespace DeferredProcessing {
MapDpsErrorCode(DpsError errorCode)30 ErrorCode MapDpsErrorCode(DpsError errorCode)
31 {
32 ErrorCode code = ErrorCode::ERROR_IMAGE_PROC_ABNORMAL;
33 switch (errorCode) {
34 case DpsError::DPS_ERROR_SESSION_SYNC_NEEDED:
35 code = ErrorCode::ERROR_SESSION_SYNC_NEEDED;
36 break;
37 case DpsError::DPS_ERROR_SESSION_NOT_READY_TEMPORARILY:
38 code = ErrorCode::ERROR_SESSION_NOT_READY_TEMPORARILY;
39 break;
40 case DpsError::DPS_ERROR_IMAGE_PROC_INVALID_PHOTO_ID:
41 code = ErrorCode::ERROR_IMAGE_PROC_INVALID_PHOTO_ID;
42 break;
43 case DpsError::DPS_ERROR_IMAGE_PROC_FAILED:
44 code = ErrorCode::ERROR_IMAGE_PROC_FAILED;
45 break;
46 case DpsError::DPS_ERROR_IMAGE_PROC_TIMEOUT:
47 code = ErrorCode::ERROR_IMAGE_PROC_TIMEOUT;
48 break;
49 case DpsError::DPS_ERROR_IMAGE_PROC_ABNORMAL:
50 code = ErrorCode::ERROR_IMAGE_PROC_ABNORMAL;
51 break;
52 case DpsError::DPS_ERROR_IMAGE_PROC_INTERRUPTED:
53 code = ErrorCode::ERROR_IMAGE_PROC_INTERRUPTED;
54 break;
55 case DpsError::DPS_ERROR_VIDEO_PROC_INVALID_VIDEO_ID:
56 code = ErrorCode::ERROR_VIDEO_PROC_INVALID_VIDEO_ID;
57 break;
58 case DpsError::DPS_ERROR_VIDEO_PROC_FAILED:
59 code = ErrorCode::ERROR_VIDEO_PROC_FAILED;
60 break;
61 case DpsError::DPS_ERROR_VIDEO_PROC_TIMEOUT:
62 code = ErrorCode::ERROR_VIDEO_PROC_TIMEOUT;
63 break;
64 case DpsError::DPS_ERROR_VIDEO_PROC_INTERRUPTED:
65 code = ErrorCode::ERROR_VIDEO_PROC_INTERRUPTED;
66 break;
67 default:
68 DP_WARNING_LOG("unexpected error code: %{public}d", errorCode);
69 break;
70 }
71 return code;
72 }
73
MapDpsStatus(DpsStatus statusCode)74 StatusCode MapDpsStatus(DpsStatus statusCode)
75 {
76 StatusCode code = StatusCode::SESSION_STATE_IDLE;
77 switch (statusCode) {
78 case DpsStatus::DPS_SESSION_STATE_IDLE:
79 code = StatusCode::SESSION_STATE_IDLE;
80 break;
81 case DpsStatus::DPS_SESSION_STATE_RUNNALBE:
82 code = StatusCode::SESSION_STATE_RUNNALBE;
83 break;
84 case DpsStatus::DPS_SESSION_STATE_RUNNING:
85 code = StatusCode::SESSION_STATE_RUNNING;
86 break;
87 case DpsStatus::DPS_SESSION_STATE_SUSPENDED:
88 code = StatusCode::SESSION_STATE_SUSPENDED;
89 break;
90 case DpsStatus::DPS_SESSION_STATE_PREEMPTED:
91 code = StatusCode::SESSION_STATE_PREEMPTED;
92 break;
93 default:
94 DP_WARNING_LOG("unexpected error code: %{public}d", statusCode);
95 break;
96 }
97 return code;
98 }
99
100 class SessionCoordinator::ImageProcCallbacks : public IImageProcessCallbacks {
101 public:
ImageProcCallbacks(const std::weak_ptr<SessionCoordinator> & coordinator)102 explicit ImageProcCallbacks(const std::weak_ptr<SessionCoordinator>& coordinator) : coordinator_(coordinator)
103 {
104 DP_DEBUG_LOG("entered.");
105 }
106
107 ~ImageProcCallbacks() = default;
108
OnProcessDone(const int32_t userId,const std::string & imageId,const std::shared_ptr<BufferInfo> & bufferInfo)109 void OnProcessDone(const int32_t userId, const std::string& imageId,
110 const std::shared_ptr<BufferInfo>& bufferInfo) override
111 {
112 DP_CHECK_ERROR_RETURN_LOG(bufferInfo == nullptr, "bufferInfo is nullptr.");
113 sptr<IPCFileDescriptor> ipcFd = bufferInfo->GetIPCFileDescriptor();
114 int32_t dataSize = bufferInfo->GetDataSize();
115 uint32_t cloudImageEnhanceFlag = bufferInfo->GetCloudImageEnhanceFlag();
116 if (auto coordinator = coordinator_.lock()) {
117 coordinator->OnProcessDone(userId, imageId, ipcFd, dataSize, cloudImageEnhanceFlag);
118 }
119 }
120
OnProcessDoneExt(int userId,const std::string & imageId,const std::shared_ptr<BufferInfoExt> & bufferInfo)121 void OnProcessDoneExt(int userId, const std::string& imageId,
122 const std::shared_ptr<BufferInfoExt>& bufferInfo) override
123 {
124 DP_CHECK_ERROR_RETURN_LOG(bufferInfo == nullptr, "bufferInfo is nullptr.");
125 uint32_t cloudImageEnhanceFlag = bufferInfo->GetCloudImageEnhanceFlag();
126 if (auto coordinator = coordinator_.lock()) {
127 coordinator->OnProcessDoneExt(userId, imageId,
128 bufferInfo->GetPicture(), cloudImageEnhanceFlag);
129 }
130 }
131
OnError(const int userId,const std::string & imageId,DpsError errorCode)132 void OnError(const int userId, const std::string& imageId, DpsError errorCode) override
133 {
134 if (auto coordinator = coordinator_.lock()) {
135 coordinator->OnError(userId, imageId, errorCode);
136 }
137 }
138
OnStateChanged(const int32_t userId,DpsStatus statusCode)139 void OnStateChanged(const int32_t userId, DpsStatus statusCode) override
140 {
141 if (auto coordinator = coordinator_.lock()) {
142 coordinator->OnStateChanged(userId, statusCode);
143 }
144 }
145
146 private:
147 std::weak_ptr<SessionCoordinator> coordinator_;
148 };
149
150 class SessionCoordinator::VideoProcCallbacks : public IVideoProcessCallbacks {
151 public:
VideoProcCallbacks(const std::weak_ptr<SessionCoordinator> & coordinator)152 explicit VideoProcCallbacks(const std::weak_ptr<SessionCoordinator>& coordinator) : coordinator_(coordinator)
153 {
154 }
155
156 ~VideoProcCallbacks() = default;
157
OnProcessDone(const int32_t userId,const std::string & videoId,const sptr<IPCFileDescriptor> & ipcFd)158 void OnProcessDone(const int32_t userId, const std::string& videoId,
159 const sptr<IPCFileDescriptor>& ipcFd) override
160 {
161 auto video = coordinator_.lock();
162 DP_CHECK_ERROR_RETURN_LOG(video == nullptr, "SessionCoordinator is nullptr.");
163 video->OnVideoProcessDone(userId, videoId, ipcFd);
164 }
165
OnError(const int32_t userId,const std::string & videoId,DpsError errorCode)166 void OnError(const int32_t userId, const std::string& videoId, DpsError errorCode) override
167 {
168 auto video = coordinator_.lock();
169 DP_CHECK_ERROR_RETURN_LOG(video == nullptr, "SessionCoordinator is nullptr.");
170 video->OnVideoError(userId, videoId, errorCode);
171 }
172
OnStateChanged(const int32_t userId,DpsStatus statusCode)173 void OnStateChanged(const int32_t userId, DpsStatus statusCode) override
174 {
175 auto video = coordinator_.lock();
176 DP_CHECK_ERROR_RETURN_LOG(video == nullptr, "SessionCoordinator is nullptr.");
177 video->OnStateChanged(userId, statusCode);
178 }
179
180 private:
181 std::weak_ptr<SessionCoordinator> coordinator_;
182 };
183
SessionCoordinator()184 SessionCoordinator::SessionCoordinator()
185 {
186 DP_DEBUG_LOG("entered.");
187 }
188
~SessionCoordinator()189 SessionCoordinator::~SessionCoordinator()
190 {
191 DP_DEBUG_LOG("entered.");
192 photoCallbackMap_.clear();
193 pendingImageResults_.clear();
194 videoCallbackMap_.clear();
195 pendingRequestResults_.clear();
196 }
197
Initialize()198 void SessionCoordinator::Initialize()
199 {
200 imageProcCallbacks_ = std::make_shared<ImageProcCallbacks>(weak_from_this());
201 videoProcCallbacks_ = std::make_shared<VideoProcCallbacks>(weak_from_this());
202 }
203
Start()204 void SessionCoordinator::Start()
205 {
206 //dps_log
207 }
208
Stop()209 void SessionCoordinator::Stop()
210 {
211 //dps_log
212 }
213
GetImageProcCallbacks()214 std::shared_ptr<IImageProcessCallbacks> SessionCoordinator::GetImageProcCallbacks()
215 {
216 return imageProcCallbacks_;
217 }
218
GetVideoProcCallbacks()219 std::shared_ptr<IVideoProcessCallbacks> SessionCoordinator::GetVideoProcCallbacks()
220 {
221 return videoProcCallbacks_;
222 }
223
AddPhotoSession(const sptr<PhotoSessionInfo> & sessionInfo)224 void SessionCoordinator::AddPhotoSession(const sptr<PhotoSessionInfo>& sessionInfo)
225 {
226 int32_t userId = sessionInfo->GetUserId();
227 DP_INFO_LOG("Add photo session userId: %{public}d", userId);
228 auto callback = sessionInfo->GetRemoteCallback();
229 if (callback != nullptr) {
230 photoCallbackMap_[userId] = callback;
231 }
232 }
233
DeletePhotoSession(const int32_t userId)234 void SessionCoordinator::DeletePhotoSession(const int32_t userId)
235 {
236 if (photoCallbackMap_.find(userId) != photoCallbackMap_.end()) {
237 DP_INFO_LOG("Delete photo session userId: %{public}d", userId);
238 photoCallbackMap_.erase(userId);
239 }
240 }
241
OnProcessDone(const int32_t userId,const std::string & imageId,const sptr<IPCFileDescriptor> & ipcFd,const int32_t dataSize,uint32_t cloudImageEnhanceFlag)242 void SessionCoordinator::OnProcessDone(const int32_t userId, const std::string& imageId,
243 const sptr<IPCFileDescriptor>& ipcFd, const int32_t dataSize, uint32_t cloudImageEnhanceFlag)
244 {
245 DP_INFO_LOG("DPS_OHOTO: userId: %{public}d, imageId: %{public}s, map size: %{public}d,"
246 "cloudImageEnhanceFlag: %{public}u.", userId, imageId.c_str(), static_cast<int32_t>(photoCallbackMap_.size()),
247 cloudImageEnhanceFlag);
248 auto callback = GetRemoteImageCallback(userId);
249 if (callback != nullptr) {
250 callback->OnProcessImageDone(imageId, ipcFd, dataSize, cloudImageEnhanceFlag);
251 } else {
252 DP_INFO_LOG("callback is null, cache request imageId: %{public}s.", imageId.c_str());
253 pendingImageResults_.push_back({ CallbackType::ON_PROCESS_DONE, userId, imageId, ipcFd, dataSize,
254 DpsError::DPS_ERROR_SESSION_SYNC_NEEDED, DpsStatus::DPS_SESSION_STATE_IDLE, cloudImageEnhanceFlag });
255 }
256 }
257
OnProcessDoneExt(int userId,const std::string & imageId,std::shared_ptr<PictureIntf> picture,uint32_t cloudImageEnhanceFlag)258 void SessionCoordinator::OnProcessDoneExt(int userId, const std::string& imageId,
259 std::shared_ptr<PictureIntf> picture, uint32_t cloudImageEnhanceFlag)
260 {
261 DP_INFO_LOG("DPS_OHOTO: userId: %{public}d, imageId: %{public}s, map size: %{public}d,"
262 "cloudImageEnhanceFlag: %{public}u.", userId, imageId.c_str(), static_cast<int32_t>(photoCallbackMap_.size()),
263 cloudImageEnhanceFlag);
264 auto callback = GetRemoteImageCallback(userId);
265 if (callback != nullptr) {
266 DP_INFO_LOG("entered, imageId: %s", imageId.c_str());
267 callback->OnProcessImageDone(imageId, picture, cloudImageEnhanceFlag);
268 } else {
269 DP_INFO_LOG("callback is null, cache request imageId: %{public}s.", imageId.c_str());
270 }
271 }
272
OnError(const int userId,const std::string & imageId,DpsError errorCode)273 void SessionCoordinator::OnError(const int userId, const std::string& imageId, DpsError errorCode)
274 {
275 DP_INFO_LOG("DPS_OHOTO: userId: %{public}d, imageId: %{public}s, map size: %{public}d.",
276 userId, imageId.c_str(), static_cast<int32_t>(photoCallbackMap_.size()));
277 auto callback = GetRemoteImageCallback(userId);
278 if (callback != nullptr) {
279 callback->OnError(imageId, MapDpsErrorCode(errorCode));
280 } else {
281 DP_INFO_LOG("callback is null, cache request imageId: %{public}s, errorCode: %{public}d.",
282 imageId.c_str(), errorCode);
283 pendingImageResults_.push_back({ CallbackType::ON_ERROR, userId, imageId, nullptr, 0, errorCode });
284 }
285 }
286
OnStateChanged(const int32_t userId,DpsStatus statusCode)287 void SessionCoordinator::OnStateChanged(const int32_t userId, DpsStatus statusCode)
288 {
289 DP_INFO_LOG("DPS_OHOTO: userId: %{public}d, map size: %{public}d, statusCode: %{public}d.",
290 userId, static_cast<int32_t>(photoCallbackMap_.size()), statusCode);
291 sptr<IDeferredPhotoProcessingSessionCallback> spCallback = GetRemoteImageCallback(userId);
292 if (spCallback != nullptr) {
293 spCallback->OnStateChanged(MapDpsStatus(statusCode));
294 } else {
295 DP_INFO_LOG("callback is null, cache request statusCode: %{public}d.", statusCode);
296 pendingImageResults_.push_back({CallbackType::ON_STATE_CHANGED, userId, "", nullptr, 0,
297 DpsError::DPS_ERROR_IMAGE_PROC_ABNORMAL, statusCode});
298 }
299 }
300
ProcessPendingResults(sptr<IDeferredPhotoProcessingSessionCallback> callback)301 void SessionCoordinator::ProcessPendingResults(sptr<IDeferredPhotoProcessingSessionCallback> callback)
302 {
303 DP_INFO_LOG("entered.");
304 while (!pendingImageResults_.empty()) {
305 auto result = pendingImageResults_.front();
306 if (result.callbackType == CallbackType::ON_PROCESS_DONE) {
307 callback->OnProcessImageDone(result.imageId, result.ipcFd, result.dataSize,
308 result.cloudImageEnhanceFlag);
309 uint64_t endTime = SteadyClock::GetTimestampMilli();
310 DPSEventReport::GetInstance().ReportImageProcessResult(result.imageId, result.userId, endTime);
311 }
312 if (result.callbackType == CallbackType::ON_ERROR) {
313 callback->OnError(result.imageId, MapDpsErrorCode(result.errorCode));
314 }
315 if (result.callbackType == CallbackType::ON_STATE_CHANGED) {
316 callback->OnStateChanged(MapDpsStatus(result.statusCode));
317 }
318 pendingImageResults_.pop_front();
319 }
320 }
321
AddVideoSession(const sptr<VideoSessionInfo> & sessionInfo)322 void SessionCoordinator::AddVideoSession(const sptr<VideoSessionInfo>& sessionInfo)
323 {
324 int32_t userId = sessionInfo->GetUserId();
325 DP_INFO_LOG("Add vidoe session userId: %{public}d", userId);
326 auto callback = sessionInfo->GetRemoteCallback();
327 if (callback != nullptr) {
328 videoCallbackMap_[userId] = callback;
329 }
330 }
331
DeleteVideoSession(const int32_t userId)332 void SessionCoordinator::DeleteVideoSession(const int32_t userId)
333 {
334 if (videoCallbackMap_.erase(userId) > 0) {
335 DP_INFO_LOG("Delete VideoSession userId: %{public}d", userId);
336 }
337 }
338
OnVideoProcessDone(const int32_t userId,const std::string & videoId,const sptr<IPCFileDescriptor> & ipcFd)339 void SessionCoordinator::OnVideoProcessDone(const int32_t userId, const std::string& videoId,
340 const sptr<IPCFileDescriptor> &ipcFd)
341 {
342 DP_INFO_LOG("DPS_VIDEO: userId: %{public}d, userMap size: %{public}d",
343 userId, static_cast<int32_t>(videoCallbackMap_.size()));
344 auto callback = GetRemoteVideoCallback(userId);
345 if (callback != nullptr) {
346 callback->OnProcessVideoDone(videoId, ipcFd);
347 } else {
348 DP_INFO_LOG("callback is null, videoId: %{public}s.", videoId.c_str());
349 }
350 }
351
OnVideoError(const int32_t userId,const std::string & videoId,DpsError errorCode)352 void SessionCoordinator::OnVideoError(const int32_t userId, const std::string& videoId, DpsError errorCode)
353 {
354 DP_INFO_LOG("DPS_VIDEO: userId: %{public}d, userMap: %{public}d, videoId: %{public}s, error: %{public}d",
355 userId, static_cast<int32_t>(videoCallbackMap_.size()), videoId.c_str(), errorCode);
356 auto callback = GetRemoteVideoCallback(userId);
357 if (callback != nullptr) {
358 callback->OnError(videoId, MapDpsErrorCode(errorCode));
359 } else {
360 DP_INFO_LOG("callback is null, videoId: %{public}s, errorCode: %{public}d", videoId.c_str(), errorCode);
361 }
362 }
363
OnVideoStateChanged(const int32_t userId,DpsStatus statusCode)364 void SessionCoordinator::OnVideoStateChanged(const int32_t userId, DpsStatus statusCode)
365 {
366 DP_DEBUG_LOG("entered.");
367 }
368
ProcessVideoResults(sptr<IDeferredVideoProcessingSessionCallback> callback)369 void SessionCoordinator::ProcessVideoResults(sptr<IDeferredVideoProcessingSessionCallback> callback)
370 {
371 DP_DEBUG_LOG("entered.");
372 while (!pendingRequestResults_.empty()) {
373 auto result = pendingRequestResults_.front();
374 if (result.callbackType == CallbackType::ON_PROCESS_DONE) {
375 callback->OnProcessVideoDone(result.requestId, result.ipcFd);
376 }
377 if (result.callbackType == CallbackType::ON_ERROR) {
378 callback->OnError(result.requestId, MapDpsErrorCode(result.errorCode));
379 }
380 if (result.callbackType == CallbackType::ON_STATE_CHANGED) {
381 callback->OnStateChanged(MapDpsStatus(result.statusCode));
382 }
383 pendingRequestResults_.pop_front();
384 }
385 }
386 } // namespace DeferredProcessing
387 } // namespace CameraStandard
388 } // namespace OHOS