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 "sync_command.h"
17 
18 #include "dps.h"
19 
20 namespace OHOS {
21 namespace CameraStandard {
22 namespace DeferredProcessing {
23 
SyncCommand(const int32_t userId)24 SyncCommand::SyncCommand(const int32_t userId) : userId_(userId)
25 {
26     DP_DEBUG_LOG("entered. userId: %{public}d", userId_);
27 }
28 
~SyncCommand()29 SyncCommand::~SyncCommand()
30 {
31     DP_DEBUG_LOG("entered.");
32     schedulerManager_ = nullptr;
33     sessionManager_ = nullptr;
34     processor_ = nullptr;
35 }
36 
Initialize()37 int32_t SyncCommand::Initialize()
38 {
39     DP_CHECK_RETURN_RET(initialized_.load(), DP_OK);
40 
41     schedulerManager_ = DPS_GetSchedulerManager();
42     DP_CHECK_ERROR_RETURN_RET_LOG(schedulerManager_ == nullptr, DP_NULL_POINTER, "SchedulerManager is nullptr.");
43     sessionManager_ = DPS_GetSessionManager();
44     DP_CHECK_ERROR_RETURN_RET_LOG(sessionManager_ == nullptr, DP_NULL_POINTER, "SessionManager is nullptr.");
45     processor_ = schedulerManager_->GetVideoProcessor(userId_);
46     DP_CHECK_ERROR_RETURN_RET_LOG(processor_ == nullptr, DP_NULL_POINTER, "VideoProcessor is nullptr.");
47     initialized_.store(true);
48     return DP_OK;
49 }
50 
VideoSyncCommand(const int32_t userId,const std::unordered_map<std::string,std::shared_ptr<DeferredVideoProcessingSession::VideoInfo>> & videoIds)51 VideoSyncCommand::VideoSyncCommand(const int32_t userId,
52     const std::unordered_map<std::string, std::shared_ptr<DeferredVideoProcessingSession::VideoInfo>>& videoIds)
53     : SyncCommand(userId), videoIds_(videoIds)
54 {
55     DP_DEBUG_LOG("VideoSyncCommand, video job num: %{public}d", static_cast<int32_t>(videoIds_.size()));
56 }
57 
Executing()58 int32_t VideoSyncCommand::Executing()
59 {
60     if (int32_t ret = Initialize() != DP_OK) {
61         return ret;
62     }
63 
64     std::vector<std::string> pendingVidoes;
65     bool isSuccess = processor_->GetPendingVideos(pendingVidoes);
66     if (!isSuccess) {
67         for (const auto& it : videoIds_) {
68             processor_->AddVideo(it.first, it.second->srcFd_, it.second->dstFd_);
69         }
70         return DP_OK;
71     }
72 
73     std::set<std::string> hdiVideoIds(pendingVidoes.begin(), pendingVidoes.end());
74     for (auto& videoId : hdiVideoIds) {
75         auto item = videoIds_.find(videoId);
76         if (item != videoIds_.end()) {
77             processor_->AddVideo(videoId, item->second->srcFd_, item->second->dstFd_);
78             videoIds_.erase(videoId);
79         } else {
80             processor_->RemoveVideo(videoId, false);
81         }
82     }
83     auto info = sessionManager_->GetSessionInfo(userId_);
84     if (info != nullptr) {
85         auto callbacks =  info->GetRemoteCallback();
86         for (const auto& it : videoIds_) {
87             callbacks->OnError(it.first, ErrorCode::ERROR_VIDEO_PROC_INVALID_VIDEO_ID);
88         }
89     }
90     pendingVidoes.clear();
91     return DP_OK;
92 }
93 } // namespace DeferredProcessing
94 } // namespace CameraStandard
95 } // namespace OHOS