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("VideoSyncCommand, video job num: %{public}d", static_cast<int32_t>(imageIds_.size()));
52 }
53
Executing()54 int32_t PhotoSyncCommand::Executing()
55 {
56 if (int32_t ret = Initialize() != DP_OK) {
57 return ret;
58 }
59
60 auto processor = schedulerManager_->GetPhotoProcessor(userId_);
61 DP_CHECK_ERROR_RETURN_RET_LOG(processor == nullptr, DP_NULL_POINTER, "PhotoProcessor is nullptr.");
62
63 std::vector<std::string> pendingImages;
64 bool isSuccess = processor->GetPendingImages(pendingImages);
65 if (!isSuccess) {
66 for (const auto& it : imageIds_) {
67 processor->AddImage(it.first, it.second->discardable_, it.second->metadata_);
68 }
69 return DP_OK;
70 }
71
72 std::set<std::string> hdiImageIds(pendingImages.begin(), pendingImages.end());
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 } else {
79 processor->RemoveImage(photoId, false);
80 }
81 }
82
83 auto info = sessionManager_->GetPhotoInfo(userId_);
84 if (info != nullptr) {
85 auto callbacks = info->GetRemoteCallback();
86 for (const auto& it : imageIds_) {
87 callbacks->OnError(it.first, ErrorCode::ERROR_IMAGE_PROC_INVALID_PHOTO_ID);
88 }
89 }
90 pendingImages.clear();
91 hdiImageIds.clear();
92 return DP_OK;
93 }
94 // LCOV_EXCL_STOP
95
VideoSyncCommand(const int32_t userId,const std::unordered_map<std::string,std::shared_ptr<DeferredVideoProcessingSession::VideoInfo>> & videoIds)96 VideoSyncCommand::VideoSyncCommand(const int32_t userId,
97 const std::unordered_map<std::string, std::shared_ptr<DeferredVideoProcessingSession::VideoInfo>>& videoIds)
98 : SyncCommand(userId), videoIds_(videoIds)
99 {
100 DP_DEBUG_LOG("VideoSyncCommand, video job num: %{public}d", static_cast<int32_t>(videoIds_.size()));
101 }
102
Executing()103 int32_t VideoSyncCommand::Executing()
104 {
105 if (int32_t ret = Initialize() != DP_OK) {
106 return ret;
107 }
108
109 auto processor = schedulerManager_->GetVideoProcessor(userId_);
110 DP_CHECK_ERROR_RETURN_RET_LOG(processor == nullptr, DP_NULL_POINTER, "VideoProcessor is nullptr.");
111
112 std::vector<std::string> pendingVidoes;
113 bool isSuccess = processor->GetPendingVideos(pendingVidoes);
114 if (!isSuccess) {
115 for (const auto& it : videoIds_) {
116 processor->AddVideo(it.first, it.second->srcFd_, it.second->dstFd_);
117 }
118 return DP_OK;
119 }
120
121 std::set<std::string> hdiVideoIds(pendingVidoes.begin(), pendingVidoes.end());
122 for (const auto& videoId : hdiVideoIds) {
123 // LCOV_EXCL_START
124 auto item = videoIds_.find(videoId);
125 if (item != videoIds_.end()) {
126 processor->AddVideo(videoId, item->second->srcFd_, item->second->dstFd_);
127 videoIds_.erase(videoId);
128 } else {
129 processor->RemoveVideo(videoId, false);
130 }
131 // LCOV_EXCL_STOP
132 }
133
134 auto info = sessionManager_->GetVideoInfo(userId_);
135 if (info != nullptr) {
136 auto callbacks = info->GetRemoteCallback();
137 for (const auto& it : videoIds_) {
138 callbacks->OnError(it.first, ErrorCode::ERROR_VIDEO_PROC_INVALID_VIDEO_ID);
139 }
140 }
141 pendingVidoes.clear();
142 hdiVideoIds.clear();
143 return DP_OK;
144 }
145 } // namespace DeferredProcessing
146 } // namespace CameraStandard
147 } // namespace OHOS