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_processing_session.h"
17 
18 #include "dps_video_report.h"
19 #include "dps.h"
20 #include "video_command.h"
21 #include "sync_command.h"
22 
23 namespace OHOS {
24 namespace CameraStandard {
25 namespace DeferredProcessing {
DeferredVideoProcessingSession(const int32_t userId)26 DeferredVideoProcessingSession::DeferredVideoProcessingSession(const int32_t userId)
27     : userId_(userId)
28 {
29     DP_DEBUG_LOG("entered. userId: %{public}d", userId_);
30 }
31 
~DeferredVideoProcessingSession()32 DeferredVideoProcessingSession::~DeferredVideoProcessingSession()
33 {
34     DP_DEBUG_LOG("entered.");
35     videoIds_.clear();
36 }
37 
BeginSynchronize()38 int32_t DeferredVideoProcessingSession::BeginSynchronize()
39 {
40     std::lock_guard<std::mutex> lock(mutex_);
41     DP_INFO_LOG("entered.");
42     inSync_.store(true);
43     return DP_OK;
44 }
45 
EndSynchronize()46 int32_t DeferredVideoProcessingSession::EndSynchronize()
47 {
48     std::lock_guard<std::mutex> lock(mutex_);
49     if (inSync_.load()) {
50         DP_INFO_LOG("entered, video job num: %{public}d", static_cast<int32_t>(videoIds_.size()));
51         auto ret = DPS_SendCommand<VideoSyncCommand>(userId_, videoIds_);
52         inSync_.store(false);
53         DP_CHECK_ERROR_RETURN_RET_LOG(ret != DP_OK, ret, "video synchronize failed, ret: %{public}d", ret);
54         videoIds_.clear();
55     }
56     return DP_OK;
57 }
58 
AddVideo(const std::string & videoId,const sptr<IPCFileDescriptor> & srcFd,const sptr<IPCFileDescriptor> & dstFd)59 int32_t DeferredVideoProcessingSession::AddVideo(const std::string& videoId,
60     const sptr<IPCFileDescriptor>& srcFd, const sptr<IPCFileDescriptor>& dstFd)
61 {
62     auto infd = sptr<IPCFileDescriptor>::MakeSptr(dup(srcFd->GetFd()));
63     auto outFd = sptr<IPCFileDescriptor>::MakeSptr(dup(dstFd->GetFd()));
64     if (inSync_.load()) {
65         std::lock_guard<std::mutex> lock(mutex_);
66         DP_INFO_LOG("AddVideo error, inSync!");
67         auto info = std::make_shared<VideoInfo>(infd, outFd);
68         videoIds_.emplace(videoId, info);
69         return DP_OK;
70     }
71 
72     auto ret = DPS_SendCommand<AddVideoCommand>(userId_, videoId, infd, outFd);
73     DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK, "add videoId: %{public}s failed. ret: %{public}d", videoId.c_str(), ret);
74     DfxVideoReport::GetInstance().ReportAddVideoEvent(videoId, GetDpsCallerInfo());
75     return ret;
76 }
77 
RemoveVideo(const std::string & videoId,bool restorable)78 int32_t DeferredVideoProcessingSession::RemoveVideo(const std::string& videoId, bool restorable)
79 {
80     DP_CHECK_RETURN_RET_LOG(inSync_.load(), DP_OK, "RemoveVideo error, inSync!");
81 
82     auto ret = DPS_SendCommand<RemoveVideoCommand>(userId_, videoId, restorable);
83     DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK, "remove videoId: %{public}s failed. ret: %{public}d", videoId.c_str(), ret);
84 
85     DfxVideoReport::GetInstance().ReportRemoveVideoEvent(videoId, GetDpsCallerInfo());
86     return ret;
87 }
88 
RestoreVideo(const std::string & videoId)89 int32_t DeferredVideoProcessingSession::RestoreVideo(const std::string& videoId)
90 {
91     DP_CHECK_RETURN_RET_LOG(inSync_.load(), DP_OK, "RestoreVideo error, inSync!");
92 
93     auto ret = DPS_SendCommand<RestoreCommand>(userId_, videoId);
94     DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK, "restore videoId: %{public}s failed. ret: %{public}u", videoId.c_str(), ret);
95     return ret;
96 }
97 
98 } // namespace DeferredProcessing
99 } // namespace CameraStandard
100 } // namespace OHOS