1 /*
2 * Copyright (c) 2024-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 "sync_command.h"
17
18 #include "dps.h"
19
20 namespace OHOS {
21 namespace CameraStandard {
22 namespace DeferredProcessing {
23
SyncCommand(const int32_t userId)24 SyncCommand::SyncCommand(const int32_t userId) : userId_(userId)
25 {
26 DP_DEBUG_LOG("entered. userId: %{public}d", userId_);
27 }
28
~SyncCommand()29 SyncCommand::~SyncCommand()
30 {
31 DP_DEBUG_LOG("entered.");
32 }
33
Initialize()34 int32_t SyncCommand::Initialize()
35 {
36 DP_CHECK_RETURN_RET(initialized_.load(), DP_OK);
37
38 schedulerManager_ = DPS_GetSchedulerManager();
39 DP_CHECK_ERROR_RETURN_RET_LOG(schedulerManager_ == nullptr, DP_NULL_POINTER, "SchedulerManager is nullptr.");
40 sessionManager_ = DPS_GetSessionManager();
41 DP_CHECK_ERROR_RETURN_RET_LOG(sessionManager_ == nullptr, DP_NULL_POINTER, "SessionManager is nullptr.");
42 initialized_.store(true);
43 return DP_OK;
44 }
45
46 // LCOV_EXCL_START
PhotoSyncCommand(const int32_t userId,const std::unordered_map<std::string,std::shared_ptr<DeferredPhotoProcessingSession::PhotoInfo>> & imageIds)47 PhotoSyncCommand::PhotoSyncCommand(const int32_t userId,
48 const std::unordered_map<std::string, std::shared_ptr<DeferredPhotoProcessingSession::PhotoInfo>>& imageIds)
49 : SyncCommand(userId), imageIds_(imageIds)
50 {
51 DP_DEBUG_LOG("PhotoSyncCommand, photo job num: %{public}d", static_cast<int32_t>(imageIds_.size()));
52 }
53
Executing()54 int32_t PhotoSyncCommand::Executing()
55 {
56 int32_t ret = Initialize();
57 if (ret != DP_OK) {
58 return ret;
59 }
60
61 auto processor = schedulerManager_->GetPhotoProcessor(userId_);
62 DP_CHECK_ERROR_RETURN_RET_LOG(processor == nullptr, DP_NULL_POINTER, "PhotoProcessor is nullptr.");
63
64 std::vector<std::string> hdiImageIds;
65 bool isSuccess = processor->GetPendingImages(hdiImageIds);
66 if (!isSuccess) {
67 for (const auto& it : imageIds_) {
68 processor->AddImage(it.first, it.second->discardable_, it.second->metadata_);
69 }
70 return DP_OK;
71 }
72
73 for (const auto& photoId : hdiImageIds) {
74 auto item = imageIds_.find(photoId);
75 if (item != imageIds_.end()) {
76 processor->AddImage(photoId, item->second->discardable_, item->second->metadata_);
77 imageIds_.erase(photoId);
78 }
79 }
80
81 auto info = sessionManager_->GetPhotoInfo(userId_);
82 if (info != nullptr) {
83 auto callbacks = info->GetRemoteCallback();
84 for (const auto& it : imageIds_) {
85 callbacks->OnError(it.first, ErrorCode::ERROR_IMAGE_PROC_INVALID_PHOTO_ID);
86 }
87 }
88 hdiImageIds.clear();
89 return DP_OK;
90 }
91 // LCOV_EXCL_STOP
92
VideoSyncCommand(const int32_t userId,const std::unordered_map<std::string,std::shared_ptr<DeferredVideoProcessingSession::VideoInfo>> & videoIds)93 VideoSyncCommand::VideoSyncCommand(const int32_t userId,
94 const std::unordered_map<std::string, std::shared_ptr<DeferredVideoProcessingSession::VideoInfo>>& videoIds)
95 : SyncCommand(userId), videoIds_(videoIds)
96 {
97 DP_DEBUG_LOG("VideoSyncCommand, video job num: %{public}d", static_cast<int32_t>(videoIds_.size()));
98 }
99
Executing()100 int32_t VideoSyncCommand::Executing()
101 {
102 int32_t ret = Initialize();
103 if (ret != DP_OK) {
104 return ret;
105 }
106
107 auto processor = schedulerManager_->GetVideoProcessor(userId_);
108 DP_CHECK_ERROR_RETURN_RET_LOG(processor == nullptr, DP_NULL_POINTER, "VideoProcessor is nullptr.");
109
110 std::vector<std::string> pendingVidoes;
111 bool isSuccess = processor->GetPendingVideos(pendingVidoes);
112 if (!isSuccess) {
113 for (const auto& it : videoIds_) {
114 processor->AddVideo(it.first, it.second->srcFd_, it.second->dstFd_);
115 }
116 return DP_OK;
117 }
118
119 std::set<std::string> hdiVideoIds(pendingVidoes.begin(), pendingVidoes.end());
120 for (const auto& videoId : hdiVideoIds) {
121 // LCOV_EXCL_START
122 auto item = videoIds_.find(videoId);
123 if (item != videoIds_.end()) {
124 processor->AddVideo(videoId, item->second->srcFd_, item->second->dstFd_);
125 videoIds_.erase(videoId);
126 } else {
127 processor->RemoveVideo(videoId, false);
128 }
129 // LCOV_EXCL_STOP
130 }
131
132 auto info = sessionManager_->GetVideoInfo(userId_);
133 if (info != nullptr) {
134 auto callbacks = info->GetRemoteCallback();
135 for (const auto& it : videoIds_) {
136 callbacks->OnError(it.first, static_cast<int32_t>(ErrorCode::ERROR_VIDEO_PROC_INVALID_VIDEO_ID));
137 }
138 }
139 pendingVidoes.clear();
140 hdiVideoIds.clear();
141 return DP_OK;
142 }
143 } // namespace DeferredProcessing
144 } // namespace CameraStandard
145 } // namespace OHOS