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 "video_job_repository.h"
17
18 namespace OHOS {
19 namespace CameraStandard {
20 namespace DeferredProcessing {
VideoJobRepository(const int32_t userId)21 VideoJobRepository::VideoJobRepository(const int32_t userId) : userId_(userId)
22 {
23 DP_DEBUG_LOG("entered, userid: %{public}d", userId_);
24 jobQueue_ = std::make_shared<VideoJobQueue>([] (DeferredVideoJobPtr a, DeferredVideoJobPtr b) {return *a > *b;});
25 }
26
~VideoJobRepository()27 VideoJobRepository::~VideoJobRepository()
28 {
29 DP_DEBUG_LOG("entered, userid: %{public}d", userId_);
30 ClearCatch();
31 }
32
33 // LCOV_EXCL_START
AddVideoJob(const std::string & videoId,const sptr<IPCFileDescriptor> & srcFd,const sptr<IPCFileDescriptor> & dstFd)34 void VideoJobRepository::AddVideoJob(const std::string& videoId,
35 const sptr<IPCFileDescriptor>& srcFd, const sptr<IPCFileDescriptor>& dstFd)
36 {
37 DP_INFO_LOG("DPS_VIDEO: AddVideoJob videoId: %{public}s", videoId.c_str());
38 DeferredVideoJobPtr jobPtr = std::make_shared<DeferredVideoJob>(videoId, srcFd, dstFd);
39 DeferredVideoJobPtr jobPtrFind = GetJobUnLocked(videoId);
40 DP_CHECK_RETURN_LOG(jobPtrFind != nullptr, "already existed, videoId: %{public}s", videoId.c_str());
41
42 jobPtr->SetJobState(VideoJobState::PENDING);
43 jobMap_.emplace(videoId, jobPtr);
44 jobQueue_->Push(jobPtr);
45 DP_INFO_LOG("DPS_VIDEO: Add video job size: %{public}d, videoId: %{public}s, srcFd: %{public}d",
46 static_cast<int>(jobQueue_->GetSize()), videoId.c_str(), srcFd->GetFd());
47 }
48 // LCOV_EXCL_STOP
49
RemoveVideoJob(const std::string & videoId,bool restorable)50 bool VideoJobRepository::RemoveVideoJob(const std::string& videoId, bool restorable)
51 {
52 DP_INFO_LOG("DPS_VIDEO: RemoveVideoJob videoId: %{public}s, restorable: %{public}d", videoId.c_str(), restorable);
53 DeferredVideoJobPtr jobPtrFind = GetJobUnLocked(videoId);
54 bool isNeedStop = false;
55 DP_CHECK_RETURN_RET_LOG(jobPtrFind == nullptr, isNeedStop,
56 "does not existed, videoId: %{public}s", videoId.c_str());
57
58 // LCOV_EXCL_START
59 isNeedStop = jobPtrFind->GetCurStatus() == VideoJobState::RUNNING;
60 if (!restorable) {
61 jobMap_.erase(videoId);
62 jobQueue_->Remove(jobPtrFind);
63 DP_INFO_LOG("DPS_VIDEO: job size: %{public}d, videoId: %{public}s",
64 static_cast<int>(jobQueue_->GetSize()), videoId.c_str());
65 }
66 jobPtrFind->SetJobState(VideoJobState::DELETED);
67 UpdateRunningCountUnLocked(false, jobPtrFind);
68 return isNeedStop;
69 // LCOV_EXCL_STOP
70 }
71
RestoreVideoJob(const std::string & videoId)72 void VideoJobRepository::RestoreVideoJob(const std::string& videoId)
73 {
74 DP_INFO_LOG("DPS_VIDEO: RestoreVideoJob videoId: %{public}s", videoId.c_str());
75 DeferredVideoJobPtr jobPtrFind = GetJobUnLocked(videoId);
76 DP_CHECK_RETURN_LOG(jobPtrFind == nullptr, "does not existed, videoId: %{public}s", videoId.c_str());
77
78 // LCOV_EXCL_START
79 bool statusChanged = jobPtrFind->SetJobState(VideoJobState::PENDING);
80 DP_CHECK_EXECUTE(statusChanged, jobQueue_->Update(jobPtrFind));
81 // LCOV_EXCL_STOP
82 }
83
SetJobPending(const std::string & videoId)84 void VideoJobRepository::SetJobPending(const std::string& videoId)
85 {
86 DP_INFO_LOG("DPS_VIDEO: SetJobPending videoId: %{public}s", videoId.c_str());
87 DeferredVideoJobPtr jobPtrFind = GetJobUnLocked(videoId);
88 DP_CHECK_RETURN_LOG(jobPtrFind == nullptr, "does not existed, videoId: %{public}s", videoId.c_str());
89
90 // LCOV_EXCL_START
91 bool statusChanged = jobPtrFind->SetJobState(VideoJobState::PENDING);
92 UpdateRunningCountUnLocked(statusChanged, jobPtrFind);
93 NotifyJobChangedUnLocked(statusChanged, jobPtrFind);
94 // LCOV_EXCL_STOP
95 }
96
SetJobRunning(const std::string & videoId)97 void VideoJobRepository::SetJobRunning(const std::string& videoId)
98 {
99 DP_INFO_LOG("DPS_VIDEO: SetJobRunning videoId: %{public}s", videoId.c_str());
100 DeferredVideoJobPtr jobPtrFind = GetJobUnLocked(videoId);
101 DP_CHECK_RETURN_LOG(jobPtrFind == nullptr, "does not existed, videoId: %{public}s", videoId.c_str());
102
103 // LCOV_EXCL_START
104 bool statusChanged = jobPtrFind->SetJobState(VideoJobState::RUNNING);
105 UpdateRunningCountUnLocked(statusChanged, jobPtrFind);
106 // LCOV_EXCL_STOP
107 }
108
SetJobCompleted(const std::string & videoId)109 void VideoJobRepository::SetJobCompleted(const std::string& videoId)
110 {
111 DP_INFO_LOG("DPS_VIDEO: SetJobCompleted videoId: %{public}s", videoId.c_str());
112 DeferredVideoJobPtr jobPtrFind = GetJobUnLocked(videoId);
113 DP_CHECK_RETURN_LOG(jobPtrFind == nullptr, "does not existed, videoId: %{public}s", videoId.c_str());
114
115 // LCOV_EXCL_START
116 bool statusChanged = jobPtrFind->SetJobState(VideoJobState::COMPLETED);
117 UpdateRunningCountUnLocked(statusChanged, jobPtrFind);
118 NotifyJobChangedUnLocked(statusChanged, jobPtrFind);
119 // LCOV_EXCL_STOP
120 }
121
SetJobFailed(const std::string & videoId)122 void VideoJobRepository::SetJobFailed(const std::string& videoId)
123 {
124 DP_INFO_LOG("DPS_VIDEO: SetJobFailed videoId: %{public}s", videoId.c_str());
125 DeferredVideoJobPtr jobPtrFind = GetJobUnLocked(videoId);
126 DP_CHECK_RETURN_LOG(jobPtrFind == nullptr, "does not existed, videoId: %{public}s", videoId.c_str());
127
128 // LCOV_EXCL_START
129 bool statusChanged = jobPtrFind->SetJobState(VideoJobState::FAILED);
130 UpdateRunningCountUnLocked(statusChanged, jobPtrFind);
131 NotifyJobChangedUnLocked(statusChanged, jobPtrFind);
132 // LCOV_EXCL_STOP
133 }
134
SetJobPause(const std::string & videoId)135 void VideoJobRepository::SetJobPause(const std::string& videoId)
136 {
137 DP_INFO_LOG("DPS_VIDEO: SetJobPause videoId: %{public}s", videoId.c_str());
138 DeferredVideoJobPtr jobPtrFind = GetJobUnLocked(videoId);
139 DP_CHECK_RETURN_LOG(jobPtrFind == nullptr, "does not existed, videoId: %{public}s", videoId.c_str());
140
141 // LCOV_EXCL_START
142 bool statusChanged = jobPtrFind->SetJobState(VideoJobState::PAUSE);
143 UpdateRunningCountUnLocked(statusChanged, jobPtrFind);
144 // LCOV_EXCL_STOP
145 }
146
SetJobError(const std::string & videoId)147 void VideoJobRepository::SetJobError(const std::string& videoId)
148 {
149 DP_INFO_LOG("DPS_VIDEO: SetJobError videoId: %{public}s", videoId.c_str());
150 DeferredVideoJobPtr jobPtrFind = GetJobUnLocked(videoId);
151 DP_CHECK_RETURN_LOG(jobPtrFind == nullptr, "does not existed, videoId: %{public}s", videoId.c_str());
152
153 // LCOV_EXCL_START
154 bool statusChanged = jobPtrFind->SetJobState(VideoJobState::ERROR);
155 UpdateRunningCountUnLocked(statusChanged, jobPtrFind);
156 NotifyJobChangedUnLocked(statusChanged, jobPtrFind);
157 // LCOV_EXCL_STOP
158 }
159
GetJob()160 DeferredVideoJobPtr VideoJobRepository::GetJob()
161 {
162 DP_INFO_LOG("DPS_VIDEO: Video job size: %{public}d, running num: %{public}d",
163 jobQueue_->GetSize(), static_cast<int32_t>(runningSet_.size()));
164 auto jobPtr = jobQueue_->Peek();
165 DP_CHECK_RETURN_RET(jobPtr == nullptr || jobPtr->GetCurStatus() >= VideoJobState::RUNNING, nullptr);
166
167 if (jobPtr->GetCurStatus() == VideoJobState::FAILED) {
168 jobPtr->SetJobState(VideoJobState::PENDING);
169 jobQueue_->Update(jobPtr);
170 }
171 return jobPtr;
172 }
173
174
GetRunningJobCounts()175 int32_t VideoJobRepository::GetRunningJobCounts()
176 {
177 DP_DEBUG_LOG("Video running jobs num: %{public}d", static_cast<int32_t>(runningSet_.size()));
178 return static_cast<int32_t>(runningSet_.size());
179 }
180
GetRunningJobList(std::vector<std::string> & list)181 void VideoJobRepository::GetRunningJobList(std::vector<std::string>& list)
182 {
183 DP_DEBUG_LOG("Video running jobs num: %{public}d", static_cast<int32_t>(runningSet_.size()));
184 list.clear();
185 list.reserve(runningSet_.size());
186 std::copy(runningSet_.begin(), runningSet_.end(), std::back_inserter(list));
187 }
188
RegisterJobListener(const std::weak_ptr<IVideoJobRepositoryListener> & listener)189 void VideoJobRepository::RegisterJobListener(const std::weak_ptr<IVideoJobRepositoryListener>& listener)
190 {
191 DP_DEBUG_LOG("entered.");
192 jobListener_ = listener;
193 }
194
GetJobUnLocked(const std::string & videoId)195 DeferredVideoJobPtr VideoJobRepository::GetJobUnLocked(const std::string& videoId)
196 {
197 auto it = jobMap_.find(videoId);
198 if (it != jobMap_.end()) {
199 DP_DEBUG_LOG("video job videoId: %{public}s", videoId.c_str());
200 return it->second;
201 }
202 return nullptr;
203 }
204
NotifyJobChangedUnLocked(bool statusChanged,DeferredVideoJobPtr jobPtr)205 void VideoJobRepository::NotifyJobChangedUnLocked(bool statusChanged, DeferredVideoJobPtr jobPtr)
206 {
207 DP_INFO_LOG("DPS_VIDEO: JobStateChanged: %{public}d, videoId: %{public}s",
208 statusChanged, jobPtr->GetVideoId().c_str());
209 if (auto listenerSptr = jobListener_.lock()) {
210 listenerSptr->OnVideoJobChanged(jobPtr);
211 }
212 }
213
214 // LCOV_EXCL_START
UpdateRunningCountUnLocked(bool statusChanged,const DeferredVideoJobPtr & jobPtr)215 void VideoJobRepository::UpdateRunningCountUnLocked(bool statusChanged, const DeferredVideoJobPtr& jobPtr)
216 {
217 DP_CHECK_EXECUTE(statusChanged, jobQueue_->Update(jobPtr));
218
219 if (statusChanged && (jobPtr->GetPreStatus() == VideoJobState::RUNNING)) {
220 runningSet_.erase(jobPtr->GetVideoId());
221 }
222 if (statusChanged && (jobPtr->GetCurStatus() == VideoJobState::RUNNING)) {
223 runningSet_.emplace(jobPtr->GetVideoId());
224 }
225 DP_INFO_LOG("DPS_VIDEO: Video running jobs num: %{public}d, videoId: %{public}s",
226 static_cast<int32_t>(runningSet_.size()), jobPtr->GetVideoId().c_str());
227 }
228 // LCOV_EXCL_STOP
229
ClearCatch()230 void VideoJobRepository::ClearCatch()
231 {
232 jobQueue_->Clear();
233 jobMap_.clear();
234 runningSet_.clear();
235 }
236 } // namespace DeferredProcessing
237 } // namespace CameraStandard
238 } // namespace OHOS