• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "stream_operator.h"
17 #include "buffer_producer_wrapper.h"
18 #include "offline_stream_operator.h"
19 #include "stream_operator_callback_wrapper.h"
20 
21 namespace OHOS::Camera {
StreamOperator()22 StreamOperator::StreamOperator() {}
23 
~StreamOperator()24 StreamOperator::~StreamOperator() {}
25 
Init(StreamOperatorCIF * op)26 void StreamOperator::Init(StreamOperatorCIF* op)
27 {
28     operator_ = op;
29 }
30 
IsStreamsSupported(OperationMode mode,const std::shared_ptr<CameraMetadata> & modeSetting,const std::shared_ptr<StreamInfo> & pInfo,StreamSupportType & pType)31 CamRetCode StreamOperator::IsStreamsSupported(OperationMode mode,
32                                               const std::shared_ptr<CameraMetadata>& modeSetting,
33                                               const std::shared_ptr<StreamInfo>& pInfo,
34                                               StreamSupportType& pType)
35 {
36     if (operator_ == nullptr) {
37         return INSUFFICIENT_RESOURCES;
38     }
39 
40     StreamInfoCIF si;
41     si.streamId = pInfo->streamId_;
42     si.width = pInfo->width_;
43     si.height = pInfo->height_;
44     si.format = pInfo->format_;
45     si.dataspace = pInfo->dataspace_;
46     si.intent = pInfo->intent_;
47     si.tunneledMode = pInfo->tunneledMode_;
48     si.minFrameDuration = pInfo->minFrameDuration_;
49 
50     BufferProducerCIF producer;
51     BindBufferProducer(pInfo->streamId_, pInfo->bufferQueue_);
52     producer.RequestBuffer = BufferProducerRequestBuffer;
53     producer.CancelBuffer = BufferProducerCancelBuffer;
54     producer.FlushBuffer = BufferProducerFlushBuffer;
55     producer.GetQueueSize = BufferProducerGetQueueSize;
56     producer.SetQueueSize = BufferProducerSetQueueSize;
57     producer.DetachBufferProducer = BufferProducerDetachBufferProducer;
58     si.producer = producer;
59 
60     CameraMetadataCIF* meta = modeSetting->get();
61 
62     int type = -1;
63     int ret = operator_->IsStreamSupported(static_cast<int>(mode), meta, si, &type);
64     pType = static_cast<StreamSupportType>(ret);
65 
66     return static_cast<CamRetCode>(pType);
67 }
68 
CreateStreams(const std::vector<std::shared_ptr<StreamInfo>> & streamInfos)69 CamRetCode StreamOperator::CreateStreams(const std::vector<std::shared_ptr<StreamInfo>>& streamInfos)
70 {
71     if (operator_ == nullptr) {
72         return INSUFFICIENT_RESOURCES;
73     }
74 
75     int count = streamInfos.size();
76     if (count <= 0) {
77         return DEVICE_ERROR;
78     }
79     StreamInfoCIF* sis = new StreamInfoCIF[count];
80     if (sis == nullptr) {
81         return INSUFFICIENT_RESOURCES;
82     }
83 
84     for (int i = 0; i < count; i++) {
85         sis[i].streamId = streamInfos[i]->streamId_;
86         sis[i].width = streamInfos[i]->width_;
87         sis[i].height = streamInfos[i]->height_;
88         sis[i].format = streamInfos[i]->format_;
89         sis[i].dataspace = streamInfos[i]->dataspace_;
90         sis[i].intent = streamInfos[i]->intent_;
91         sis[i].tunneledMode = streamInfos[i]->tunneledMode_;
92         sis[i].minFrameDuration = streamInfos[i]->minFrameDuration_;
93 
94         BufferProducerCIF producer;
95         BindBufferProducer(streamInfos[i]->streamId_, streamInfos[i]->bufferQueue_);
96         producer.RequestBuffer = BufferProducerRequestBuffer;
97         producer.CancelBuffer = BufferProducerCancelBuffer;
98         producer.FlushBuffer = BufferProducerFlushBuffer;
99         producer.GetQueueSize = BufferProducerGetQueueSize;
100         producer.SetQueueSize = BufferProducerSetQueueSize;
101         producer.DetachBufferProducer = BufferProducerDetachBufferProducer;
102         sis[i].producer = producer;
103     }
104 
105     int ret = operator_->CreateStreams(sis, count);
106     if (sis != nullptr) {
107         delete[] sis;
108     }
109     return static_cast<CamRetCode>(ret);
110 }
111 
ReleaseStreams(const std::vector<int> & streamIds)112 CamRetCode StreamOperator::ReleaseStreams(const std::vector<int>& streamIds)
113 {
114     if (operator_ == nullptr) {
115         return INSUFFICIENT_RESOURCES;
116     }
117 
118     int count = streamIds.size();
119     if (count <= 0) {
120         return DEVICE_ERROR;
121     }
122     int* ids = new int[count];
123     if (ids == nullptr) {
124         return INSUFFICIENT_RESOURCES;
125     }
126 
127     for (int i = 0; i < count; i++) {
128         ids[i] = streamIds[i];
129     }
130 
131     int ret = operator_->ReleaseStreams(ids, count);
132 
133     if (ids != nullptr) {
134         delete[] ids;
135     }
136     return static_cast<CamRetCode>(ret);
137 }
138 
CommitStreams(OperationMode mode,const std::shared_ptr<CameraMetadata> & modeSetting)139 CamRetCode StreamOperator::CommitStreams(OperationMode mode, const std::shared_ptr<CameraMetadata>& modeSetting)
140 {
141     if (operator_ == nullptr) {
142         return INSUFFICIENT_RESOURCES;
143     }
144 
145     CameraMetadataCIF* meta = modeSetting->get();
146     int ret = operator_->CommitStreams(static_cast<int>(mode), meta);
147 
148     return static_cast<CamRetCode>(ret);
149 }
150 
GetStreamAttributes(std::vector<std::shared_ptr<StreamAttribute>> & attributes)151 CamRetCode StreamOperator::GetStreamAttributes(std::vector<std::shared_ptr<StreamAttribute>>& attributes)
152 {
153     if (operator_ == nullptr) {
154         return INSUFFICIENT_RESOURCES;
155     }
156 
157     int count = 0;
158     StreamAttributeCIF* sa = nullptr;
159     int ret = operator_->GetStreamAttributes(&sa, &count);
160     if (ret != NO_ERROR || sa == nullptr || count == 0) {
161         return DEVICE_ERROR;
162     }
163 
164     for (int i = 0; i < count; i++) {
165         std::shared_ptr<StreamAttribute> it = std::make_shared<StreamAttribute>();
166         if (it == nullptr) {
167             return INSUFFICIENT_RESOURCES;
168         }
169         it->streamId_ = sa[i].streamId;
170         it->width_ = sa[i].width;
171         it->height_ = sa[i].height;
172         it->overrideFormat_ = sa[i].overrideFormat;
173         it->overrideDataspace_ = sa[i].overrideDataspace;
174         it->producerUsage_ = sa[i].producerUsage;
175         it->producerBufferCount_ = sa[i].producerBufferCount;
176         it->maxBatchCaptureCount_ = sa[i].maxBatchCaptureCount;
177         it->maxCaptureCount_ = sa[i].maxCaptureCount;
178         attributes.push_back(it);
179     }
180 
181     return static_cast<CamRetCode>(ret);
182 }
183 
AttachBufferQueue(int streamId,const OHOS::sptr<OHOS::IBufferProducer> & producer)184 CamRetCode StreamOperator::AttachBufferQueue(int streamId, const OHOS::sptr<OHOS::IBufferProducer>& producer)
185 {
186     if (operator_ == nullptr) {
187         return INSUFFICIENT_RESOURCES;
188     }
189 
190     BufferProducerCIF bufferQueue;
191     BindBufferProducer(streamId, const_cast<OHOS::sptr<OHOS::IBufferProducer>&>(producer));
192     bufferQueue.RequestBuffer = BufferProducerRequestBuffer;
193     bufferQueue.CancelBuffer = BufferProducerCancelBuffer;
194     bufferQueue.FlushBuffer = BufferProducerFlushBuffer;
195     bufferQueue.GetQueueSize = BufferProducerGetQueueSize;
196     bufferQueue.SetQueueSize = BufferProducerSetQueueSize;
197     bufferQueue.DetachBufferProducer = BufferProducerDetachBufferProducer;
198 
199     int ret = operator_->AttachBufferQueue(streamId, bufferQueue);
200 
201     return static_cast<CamRetCode>(ret);
202 }
203 
DetachBufferQueue(int streamId)204 CamRetCode StreamOperator::DetachBufferQueue(int streamId)
205 {
206     if (operator_ == nullptr) {
207         return INSUFFICIENT_RESOURCES;
208     }
209 
210     int ret = operator_->DetachBufferQueue(streamId);
211 
212     return static_cast<CamRetCode>(ret);
213 }
214 
Capture(int captureId,const std::shared_ptr<CaptureInfo> & pInfo,bool isStreaming)215 CamRetCode StreamOperator::Capture(int captureId, const std::shared_ptr<CaptureInfo>& pInfo, bool isStreaming)
216 {
217     if (operator_ == nullptr) {
218         return INSUFFICIENT_RESOURCES;
219     }
220 
221     if (pInfo == nullptr) {
222         return NO_ERROR;
223     }
224 
225     int count = pInfo->streamIds_.size();
226     if (count <= 0) {
227         return DEVICE_ERROR;
228     }
229     int* ids = new int[count];
230     if (ids == nullptr) {
231         return INSUFFICIENT_RESOURCES;
232     }
233 
234     CaptureInfoCIF info;
235     info.streamIds = ids;
236     info.count = count;
237     // info.captureSetting
238     info.enableShutterCallback = pInfo->enableShutterCallback_;
239 
240     int ret = operator_->Capture(captureId, info, static_cast<int>(isStreaming));
241 
242     return static_cast<CamRetCode>(ret);
243 }
244 
CancelCapture(int captureId)245 CamRetCode StreamOperator::CancelCapture(int captureId)
246 {
247     if (operator_ == nullptr) {
248         return INSUFFICIENT_RESOURCES;
249     }
250 
251     int ret = operator_->CancelCapture(captureId);
252 
253     return static_cast<CamRetCode>(ret);
254 }
255 
ChangeToOfflineStream(const std::vector<int> & streamIds,OHOS::sptr<IStreamOperatorCallback> & callback,OHOS::sptr<IOfflineStreamOperator> & offlineOperator)256 CamRetCode StreamOperator::ChangeToOfflineStream(const std::vector<int>& streamIds,
257                                                  OHOS::sptr<IStreamOperatorCallback>& callback,
258                                                  OHOS::sptr<IOfflineStreamOperator>& offlineOperator)
259 {
260     if (operator_ == nullptr) {
261         return INSUFFICIENT_RESOURCES;
262     }
263 
264     BindStreamOperatorCallback(callback);
265 
266     StreamOperatorCallbackCIF cb;
267     cb.OnCaptureStarted = StreamCBOnCaptureStarted;
268     cb.OnCaptureEnded = StreamCBOnCaptureEnded;
269     cb.OnCaptureError = StreamCBOnCaptureError;
270     cb.OnFrameShutter = StreamCBOnFrameShutter;
271 
272     int count = streamIds.size();
273     if (count <= 0) {
274         return DEVICE_ERROR;
275     }
276     int* ids = new int[count];
277     if (ids == nullptr) {
278         return INSUFFICIENT_RESOURCES;
279     }
280     for (int i = 0; i < count; i++) {
281         ids[i] = streamIds[i];
282     }
283 
284     OfflineStreamOperatorCIF* op = nullptr;
285     int ret = operator_->ChangeToOfflineStream(ids, count, cb, op);
286     auto os = new OfflineStreamOperator();
287     os->Init(op);
288 
289     if (ids != nullptr) {
290         delete[] ids;
291     }
292 
293     return static_cast<CamRetCode>(ret);
294 }
295 
296 } // end namespace OHOS::Camera
297