1 /*
2 * Copyright (c) 2021 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 "offline_pipeline_manager.h"
17
18 namespace OHOS::Camera {
GetInstance()19 OfflinePipelineManager& OfflinePipelineManager::GetInstance()
20 {
21 static OfflinePipelineManager manager;
22 return manager;
23 }
24
~OfflinePipelineManager()25 OfflinePipelineManager::~OfflinePipelineManager()
26 {
27 CAMERA_LOGV("dtor");
28 offlinePipelineList_.clear();
29 }
30
SwitchToOfflinePipeline(int32_t streamId,int32_t streamType,const std::shared_ptr<IStreamPipelineCore> & pipeline,std::function<void (std::shared_ptr<IBuffer> &)> callback)31 RetCode OfflinePipelineManager::SwitchToOfflinePipeline(int32_t streamId,
32 int32_t streamType,
33 const std::shared_ptr<IStreamPipelineCore>& pipeline,
34 std::function<void(std::shared_ptr<IBuffer>&)> callback)
35 {
36 (void)streamType;
37 auto op = GetOfflinePipeline(streamId, pipeline);
38 if (op == nullptr) {
39 CAMERA_LOGW("can't get offlinepipeline, abort switch.");
40 return RC_ERROR;
41 }
42
43 op->BindOfflineStreamCallback(callback);
44 op->SwitchToOfflineMode();
45
46 {
47 std::lock_guard<std::mutex> l(lock_);
48 offlinePipelineList_.emplace_back(std::make_pair(streamId, op));
49 }
50
51 return RC_OK;
52 }
53
CancelCapture(int32_t streamId,int32_t captureId)54 RetCode OfflinePipelineManager::CancelCapture(int32_t streamId, int32_t captureId)
55 {
56 auto op = FindOfflinePipeline(streamId);
57 if (op == nullptr) {
58 return RC_ERROR;
59 }
60
61 return op->CancelCapture(captureId);
62 }
63
DestoryOfflinePipeline(int32_t id)64 RetCode OfflinePipelineManager::DestoryOfflinePipeline(int32_t id)
65 {
66 std::shared_ptr<OfflinePipeline> op = FindOfflinePipeline(id);
67 if (op == nullptr) {
68 CAMERA_LOGE("cannot release stream %{public}d", id);
69 return RC_ERROR;
70 }
71
72 op->FlushOfflineStream();
73
74 {
75 std::lock_guard<std::mutex> l(lock_);
76 auto it = std::find_if(offlinePipelineList_.begin(), offlinePipelineList_.end(),
77 [&id](const std::pair<int32_t, std::shared_ptr<OfflinePipeline>>& stream) {
78 return id == stream.first;
79 });
80 offlinePipelineList_.erase(it);
81 }
82
83 return RC_OK;
84 }
85
DestoryOfflinePipelines()86 RetCode OfflinePipelineManager::DestoryOfflinePipelines()
87 {
88 std::lock_guard<std::mutex> l(lock_);
89 if (offlinePipelineList_.empty()) {
90 return RC_OK;
91 }
92
93 for (auto it : offlinePipelineList_) {
94 it.second->FlushOfflineStream();
95 }
96
97 offlinePipelineList_.clear();
98
99 return RC_OK;
100 }
101
GetOfflinePipeline(int32_t streamId,const std::shared_ptr<IStreamPipelineCore> & pipeline)102 std::shared_ptr<OfflinePipeline> OfflinePipelineManager::GetOfflinePipeline(int32_t streamId,
103 const std::shared_ptr<IStreamPipelineCore>& pipeline)
104 {
105 if (pipeline == nullptr) {
106 CAMERA_LOGE("online pipeline is nullptr.");
107 return nullptr;
108 }
109 return pipeline->GetOfflinePipeline(streamId);
110 }
111
FindOfflinePipeline(int32_t id)112 std::shared_ptr<OfflinePipeline> OfflinePipelineManager::FindOfflinePipeline(int32_t id)
113 {
114 std::lock_guard<std::mutex> l(lock_);
115 std::shared_ptr<OfflinePipeline> op = nullptr;
116 for (const auto& it : offlinePipelineList_) {
117 if (it.first == id) {
118 op = it.second;
119 }
120 }
121
122 if (op == nullptr) {
123 return nullptr;
124 }
125
126 return op;
127 }
128
CheckCaptureIdExist(int32_t id,int32_t captureId)129 bool OfflinePipelineManager::CheckCaptureIdExist(int32_t id, int32_t captureId)
130 {
131 auto op = FindOfflinePipeline(id);
132 if (op == nullptr) {
133 CAMERA_LOGW("can't get offlinepipeline");
134 return false;
135 }
136
137 return op->CheckOwnerOfCaptureId(captureId);
138 }
139 } // namespace OHOS::Camera
140