• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "deferred_photo_processing_session.h"
17 
18 #include "dp_utils.h"
19 #include "dps.h"
20 #include "dps_event_report.h"
21 #include "photo_command.h"
22 #include "sync_command.h"
23 
24 namespace OHOS {
25 namespace CameraStandard {
26 namespace DeferredProcessing {
DeferredPhotoProcessingSession(const int32_t userId)27 DeferredPhotoProcessingSession::DeferredPhotoProcessingSession(const int32_t userId)
28     : userId_(userId)
29 {
30     DP_DEBUG_LOG("entered. userId: %{public}d", userId_);
31 }
32 
~DeferredPhotoProcessingSession()33 DeferredPhotoProcessingSession::~DeferredPhotoProcessingSession()
34 {
35     DP_DEBUG_LOG("entered.");
36     imageIds_.clear();
37 }
38 
BeginSynchronize()39 int32_t DeferredPhotoProcessingSession::BeginSynchronize()
40 {
41     DP_INFO_LOG("DPS_PHOTO: BeginSynchronize.");
42     std::lock_guard<std::mutex> lock(mutex_);
43     inSync_.store(true);
44     const std::string imageId = "default";
45     ReportEvent(imageId, DeferredProcessingServiceInterfaceCode::DPS_BEGIN_SYNCHRONIZE);
46     return DP_OK;
47 }
48 
EndSynchronize()49 int32_t DeferredPhotoProcessingSession::EndSynchronize()
50 {
51     DP_INFO_LOG("DPS_PHOTO: EndSynchronize photo job num: %{public}d", static_cast<int32_t>(imageIds_.size()));
52     std::lock_guard<std::mutex> lock(mutex_);
53     if (inSync_.load()) {
54         auto ret = DPS_SendCommand<PhotoSyncCommand>(userId_, imageIds_);
55         inSync_.store(false);
56         imageIds_.clear();
57         DP_CHECK_ERROR_RETURN_RET_LOG(ret != DP_OK, ret, "photo synchronize failed, ret: %{public}d", ret);
58 
59         const std::string imageId = "default";
60         ReportEvent(imageId, DeferredProcessingServiceInterfaceCode::DPS_END_SYNCHRONIZE);
61     }
62     return DP_OK;
63 }
64 
AddImage(const std::string & imageId,DpsMetadata & metadata,bool discardable)65 int32_t DeferredPhotoProcessingSession::AddImage(const std::string& imageId, DpsMetadata& metadata, bool discardable)
66 {
67     if (inSync_.load()) {
68         std::lock_guard<std::mutex> lock(mutex_);
69         DP_INFO_LOG("AddImage error, inSync!");
70         auto info = std::make_shared<PhotoInfo>(discardable, metadata);
71         imageIds_.emplace(imageId, info);
72     } else {
73         auto ret = DPS_SendCommand<AddPhotoCommand>(userId_, imageId, metadata, discardable);
74         DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK,
75             "DPS_PHOTO: add imageId: %{public}s failed. ret: %{public}d", imageId.c_str(), ret);
76     }
77 
78     ReportEvent(imageId, DeferredProcessingServiceInterfaceCode::DPS_ADD_IMAGE);
79     return DP_OK;
80 }
81 
RemoveImage(const std::string & imageId,bool restorable)82 int32_t DeferredPhotoProcessingSession::RemoveImage(const std::string& imageId, bool restorable)
83 {
84     if (inSync_.load()) {
85         DP_INFO_LOG("RemoveImage error, inSync!");
86     } else {
87         auto ret = DPS_SendCommand<RemovePhotoCommand>(userId_, imageId, restorable);
88         DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK,
89             "DPS_PHOTO: remove imageId: %{public}s failed. ret: %{public}d", imageId.c_str(), ret);
90     }
91 
92     ReportEvent(imageId, DeferredProcessingServiceInterfaceCode::DPS_REMOVE_IMAGE);
93     return DP_OK;
94 }
95 
RestoreImage(const std::string & imageId)96 int32_t DeferredPhotoProcessingSession::RestoreImage(const std::string& imageId)
97 {
98     if (inSync_.load()) {
99         DP_INFO_LOG("RestoreImage error, inSync!");
100     } else {
101         auto ret = DPS_SendCommand<RestorePhotoCommand>(userId_, imageId);
102         DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK,
103             "DPS_PHOTO: restore imageId: %{public}s failed. ret: %{public}u", imageId.c_str(), ret);
104     }
105 
106     ReportEvent(imageId, DeferredProcessingServiceInterfaceCode::DPS_RESTORE_IMAGE);
107     return DP_OK;
108 }
109 
ProcessImage(const std::string & appName,const std::string & imageId)110 int32_t DeferredPhotoProcessingSession::ProcessImage(const std::string& appName, const std::string& imageId)
111 {
112     if (inSync_) {
113         DP_INFO_LOG("ProcessImage error, inSync!");
114     } else {
115         auto ret = DPS_SendCommand<ProcessPhotoCommand>(userId_, imageId, appName);
116         DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK,
117             "DPS_PHOTO: process imageId: %{public}s failed. ret: %{public}u", imageId.c_str(), ret);
118     }
119 
120     ReportEvent(imageId, DeferredProcessingServiceInterfaceCode::DPS_PROCESS_IMAGE);
121     return 0;
122 }
123 
CancelProcessImage(const std::string & imageId)124 int32_t DeferredPhotoProcessingSession::CancelProcessImage(const std::string& imageId)
125 {
126     if (inSync_) {
127         DP_INFO_LOG("CancelProcessImage error, inSync!");
128     } else {
129         auto ret = DPS_SendCommand<CancelProcessPhotoCommand>(userId_, imageId);
130         DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK,
131             "DPS_PHOTO: cance process imageId: %{public}s failed. ret: %{public}u", imageId.c_str(), ret);
132     }
133 
134     ReportEvent(imageId, DeferredProcessingServiceInterfaceCode::DPS_CANCEL_PROCESS_IMAGE);
135     return 0;
136 }
137 
ReportEvent(const std::string & imageId,int32_t event)138 void DeferredPhotoProcessingSession::ReportEvent(const std::string& imageId, int32_t event)
139 {
140     DPSEventInfo dpsEventInfo;
141     dpsEventInfo.operatorStage = static_cast<uint32_t>(event);
142     dpsEventInfo.imageId = imageId;
143     dpsEventInfo.userId = userId_;
144     uint64_t beginTime = GetTimestampMilli();
145     switch (static_cast<int32_t>(event)) {
146         case static_cast<int32_t>(DeferredProcessingServiceInterfaceCode::DPS_BEGIN_SYNCHRONIZE): {
147             dpsEventInfo.synchronizeTimeBeginTime = beginTime;
148             DPSEventReport::GetInstance().ReportOperateImage(imageId, userId_, dpsEventInfo);
149             break;
150         }
151         case static_cast<int32_t>(DeferredProcessingServiceInterfaceCode::DPS_END_SYNCHRONIZE): {
152             dpsEventInfo.synchronizeTimeEndTime = beginTime;
153             DPSEventReport::GetInstance().ReportOperateImage(imageId, userId_, dpsEventInfo);
154             break;
155         }
156         case static_cast<int32_t>(DeferredProcessingServiceInterfaceCode::DPS_ADD_IMAGE): {
157             dpsEventInfo.dispatchTimeBeginTime = beginTime;
158             break;
159         }
160         case static_cast<int32_t>(DeferredProcessingServiceInterfaceCode::DPS_REMOVE_IMAGE): {
161             dpsEventInfo.removeTimeBeginTime = beginTime;
162             break;
163         }
164         case static_cast<int32_t>(DeferredProcessingServiceInterfaceCode::DPS_RESTORE_IMAGE): {
165             dpsEventInfo.restoreTimeBeginTime = beginTime;
166             break;
167         }
168         case static_cast<int32_t>(DeferredProcessingServiceInterfaceCode::DPS_PROCESS_IMAGE): {
169             dpsEventInfo.processTimeBeginTime = beginTime;
170             break;
171         }
172     }
173 
174     if (event == DeferredProcessingServiceInterfaceCode::DPS_BEGIN_SYNCHRONIZE) {
175         return;
176     } else if (event == DeferredProcessingServiceInterfaceCode::DPS_END_SYNCHRONIZE) {
177         DPSEventReport::GetInstance().ReportImageProcessResult(imageId, userId_);
178     } else {
179         DPSEventReport::GetInstance().UpdateEventInfo(dpsEventInfo);
180     }
181 }
182 } // namespace DeferredProcessing
183 } // namespace CameraStandard
184 } // namespace OHOS