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