• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "deferred_video_processor.h"
17 
18 #include "dps_video_report.h"
19 #include "dp_utils.h"
20 
21 namespace OHOS {
22 namespace CameraStandard {
23 namespace DeferredProcessing {
DeferredVideoProcessor(const std::shared_ptr<VideoJobRepository> & repository,const std::shared_ptr<VideoPostProcessor> & postProcessor,const std::shared_ptr<IVideoProcessCallbacks> & callback)24 DeferredVideoProcessor::DeferredVideoProcessor(const std::shared_ptr<VideoJobRepository>& repository,
25     const std::shared_ptr<VideoPostProcessor>& postProcessor, const std::shared_ptr<IVideoProcessCallbacks>& callback)
26     : repository_(repository), postProcessor_(postProcessor), callback_(callback)
27 {
28     DP_DEBUG_LOG("entered.");
29 }
30 
~DeferredVideoProcessor()31 DeferredVideoProcessor::~DeferredVideoProcessor()
32 {
33     DP_DEBUG_LOG("entered.");
34 }
35 
Initialize()36 void DeferredVideoProcessor::Initialize()
37 {
38     DP_DEBUG_LOG("entered.");
39     DP_CHECK_ERROR_RETURN_LOG(postProcessor_ == nullptr, "VideoPostProcessor is nullptr.");
40 
41     postProcessor_->Initialize();
42 }
43 
AddVideo(const std::string & videoId,const sptr<IPCFileDescriptor> & srcFd,const sptr<IPCFileDescriptor> & dstFd)44 void DeferredVideoProcessor::AddVideo(const std::string& videoId,
45     const sptr<IPCFileDescriptor>& srcFd, const sptr<IPCFileDescriptor>& dstFd)
46 {
47     DP_DEBUG_LOG("DPS_VIDEO: videoId: %{public}s", videoId.c_str());
48     DP_CHECK_ERROR_RETURN_LOG(repository_ == nullptr, "VideoJobRepository is nullptr.");
49 
50     repository_->AddVideoJob(videoId, srcFd, dstFd);
51 }
52 
RemoveVideo(const std::string & videoId,bool restorable)53 void DeferredVideoProcessor::RemoveVideo(const std::string& videoId, bool restorable)
54 {
55     DP_CHECK_ERROR_RETURN_LOG(repository_ == nullptr, "VideoJobRepository is nullptr.");
56 
57     bool isNeedStop = repository_->RemoveVideoJob(videoId, restorable);
58     DP_DEBUG_LOG("DPS_VIDEO: videoId: %{public}s, isNeedStop: %{public}d, restorable: %{public}d",
59         videoId.c_str(), isNeedStop, restorable);
60     DP_CHECK_ERROR_RETURN_LOG(postProcessor_ == nullptr, "VideoPostProcessor is nullptr.");
61 
62     DP_CHECK_EXECUTE(isNeedStop, postProcessor_->PauseRequest(videoId, SchedulerType::REMOVE));
63     DP_CHECK_EXECUTE(!restorable, postProcessor_->RemoveRequest(videoId));
64 }
65 
RestoreVideo(const std::string & videoId)66 void DeferredVideoProcessor::RestoreVideo(const std::string& videoId)
67 {
68     DP_DEBUG_LOG("DPS_VIDEO: videoId: %{public}s", videoId.c_str());
69     DP_CHECK_ERROR_RETURN_LOG(repository_ == nullptr, "VideoJobRepository is nullptr.");
70 
71     repository_->RestoreVideoJob(videoId);
72 }
73 
OnProcessDone(const int32_t userId,const std::string & videoId,const sptr<IPCFileDescriptor> & ipcFd)74 void DeferredVideoProcessor::OnProcessDone(const int32_t userId,
75     const std::string& videoId, const sptr<IPCFileDescriptor>& ipcFd)
76 {
77     DP_DEBUG_LOG("DPS_VIDEO: videoId: %{public}s, fd: %{public}d", videoId.c_str(), ipcFd->GetFd());
78     DP_CHECK_ERROR_RETURN_LOG(callback_ == nullptr, "IVideoProcessCallbacks is nullptr.");
79 
80     callback_->OnProcessDone(userId, videoId, ipcFd);
81     DP_CHECK_ERROR_RETURN_LOG(repository_ == nullptr, "VideoJobRepository is nullptr.");
82 
83     repository_->SetJobCompleted(videoId);
84     DfxVideoReport::GetInstance().ReportCompleteVideoEvent(videoId);
85 }
86 
OnError(const int32_t userId,const std::string & videoId,DpsError errorCode)87 void DeferredVideoProcessor::OnError(const int32_t userId, const std::string& videoId, DpsError errorCode)
88 {
89     DP_DEBUG_LOG("DPS_VIDEO: videoId: %{public}s, error: %{public}d", videoId.c_str(), errorCode);
90     DP_CHECK_ERROR_RETURN_LOG(callback_ == nullptr, "IVideoProcessCallbacks is nullptr.");
91 
92     DP_CHECK_EXECUTE(IsFatalError(errorCode), callback_->OnError(userId, videoId, errorCode));
93     DP_CHECK_ERROR_RETURN_LOG(repository_ == nullptr, "VideoJobRepository is nullptr.");
94 
95     if (errorCode == DpsError::DPS_ERROR_VIDEO_PROC_INTERRUPTED) {
96         repository_->SetJobPause(videoId);
97     } else if (errorCode == DpsError::DPS_ERROR_VIDEO_PROC_INVALID_VIDEO_ID ||
98         errorCode == DpsError::DPS_ERROR_VIDEO_PROC_FAILED) {
99         repository_->SetJobError(videoId);
100     } else {
101         repository_->SetJobFailed(videoId);
102     }
103 }
104 
OnStateChanged(const int32_t userId,DpsStatus statusCode)105 void DeferredVideoProcessor::OnStateChanged(const int32_t userId, DpsStatus statusCode)
106 {
107     DP_DEBUG_LOG("DPS_VIDEO: userId: %{public}d, status: %{public}d", userId, statusCode);
108 }
109 
PostProcess(const DeferredVideoWorkPtr & work)110 void DeferredVideoProcessor::PostProcess(const DeferredVideoWorkPtr& work)
111 {
112     DP_DEBUG_LOG("entered.");
113     DP_CHECK_ERROR_RETURN_LOG(repository_ == nullptr, "VideoJobRepository is nullptr.");
114 
115     auto videoId = work->GetDeferredVideoJob()->GetVideoId();
116     repository_->SetJobRunning(videoId);
117     DP_CHECK_ERROR_RETURN_LOG(postProcessor_ == nullptr, "VideoPostProcessor is nullptr.");
118 
119     postProcessor_->ProcessRequest(work);
120     DfxVideoReport::GetInstance().ReportResumeVideoEvent(videoId);
121 }
122 
PauseRequest(const SchedulerType & type)123 void DeferredVideoProcessor::PauseRequest(const SchedulerType& type)
124 {
125     DP_DEBUG_LOG("entered.");
126     DP_CHECK_ERROR_RETURN_LOG(repository_ == nullptr, "VideoJobRepository is nullptr.");
127 
128     std::vector<std::string> runningList;
129     repository_->GetRunningJobList(runningList);
130     DP_CHECK_ERROR_RETURN_LOG(postProcessor_ == nullptr, "VideoPostProcessor is nullptr.");
131 
132     for (const auto& videoId: runningList) {
133         postProcessor_->PauseRequest(videoId, type);
134         DfxVideoReport::GetInstance().ReportPauseVideoEvent(videoId, type);
135     }
136 }
137 
SetDefaultExecutionMode()138 void DeferredVideoProcessor::SetDefaultExecutionMode()
139 {
140     DP_DEBUG_LOG("entered.");
141     DP_CHECK_ERROR_RETURN_LOG(postProcessor_ == nullptr, "VideoPostProcessor is nullptr.");
142 
143     postProcessor_->SetDefaultExecutionMode();
144 }
145 
GetPendingVideos(std::vector<std::string> & pendingVideos)146 bool DeferredVideoProcessor::GetPendingVideos(std::vector<std::string>& pendingVideos)
147 {
148     DP_DEBUG_LOG("entered.");
149     DP_CHECK_ERROR_RETURN_RET_LOG(postProcessor_ == nullptr, false, "VideoPostProcessor is nullptr.");
150 
151     return postProcessor_->GetPendingVideos(pendingVideos);
152 }
153 
IsFatalError(DpsError errorCode)154 bool DeferredVideoProcessor::IsFatalError(DpsError errorCode)
155 {
156     return (errorCode == DpsError::DPS_ERROR_VIDEO_PROC_FAILED ||
157         errorCode == DpsError::DPS_ERROR_VIDEO_PROC_INVALID_VIDEO_ID);
158 }
159 } // namespace DeferredProcessing
160 } // namespace CameraStandard
161 } // namespace OHOS