• 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 "hos_camera_demo.h"
17 
18 namespace OHOS::Camera {
HosCameraDemo()19 HosCameraDemo::HosCameraDemo() {}
~HosCameraDemo()20 HosCameraDemo::~HosCameraDemo() {}
21 
SetStreamInfo(std::shared_ptr<StreamInfo> & streamInfo,const std::shared_ptr<StreamCustomer> & streamCustomer,const int streamId,const StreamIntent intent)22 void HosCameraDemo::SetStreamInfo(std::shared_ptr<StreamInfo>& streamInfo,
23     const std::shared_ptr<StreamCustomer>& streamCustomer,
24     const int streamId, const StreamIntent intent)
25 {
26     constexpr uint32_t datasapce = 8;
27     constexpr uint32_t tunneledMode = 5;
28 
29     if (intent == PREVIEW) {
30         constexpr uint32_t width = CAMERA_PREVIEW_WIDTH;
31         constexpr uint32_t height = CAMERA_PREVIEW_HEIGHT;
32         streamInfo->width_ = width;
33         streamInfo->height_ = height;
34     } else if (intent == STILL_CAPTURE) {
35         constexpr uint32_t width = CAMERA_CAPTURE_WIDTH;
36         constexpr uint32_t height = CAMERA_CAPTURE_HEIGHT;
37         streamInfo->width_ = width;
38         streamInfo->height_ = height;
39         streamInfo->encodeType_ = CAMERA_CAPTURE_ENCODE_TYPE;
40     } else {
41         constexpr uint32_t width = CAMERA_VIDEO_WIDTH;
42         constexpr uint32_t height = CAMERA_VIDEO_HEIGHT;
43         streamInfo->width_ = width;
44         streamInfo->height_ = height;
45         streamInfo->encodeType_ = CAMERA_VIDEO_ENCODE_TYPE;
46     }
47 
48     streamInfo->streamId_ = streamId;
49     streamInfo->format_ =  CAMERA_FORMAT;
50     streamInfo->datasapce_ = datasapce;
51     streamInfo->intent_ = intent;
52     streamInfo->tunneledMode_ = tunneledMode;
53     streamInfo->bufferQueue_ = streamCustomer->CreateProducer();
54     streamInfo->bufferQueue_->SetQueueSize(8); // 8:set bufferQueue size
55 }
56 
GetStreamOpt()57 void HosCameraDemo::GetStreamOpt()
58 {
59     int rc = 0;
60 
61     if (streamOperator_ == nullptr) {
62 #ifdef CAMERA_BUILT_ON_OHOS_LITE
63         const std::shared_ptr<IStreamOperatorCallback> streamOperatorCallback =
64             std::make_shared<StreamOperatorCallback>();
65 #else
66         const sptr<IStreamOperatorCallback> streamOperatorCallback = new StreamOperatorCallback();
67 #endif
68         rc = demoCameraDevice_->GetStreamOperator(streamOperatorCallback, streamOperator_);
69         if (rc != Camera::NO_ERROR) {
70             CAMERA_LOGE("demo test: GetStreamOpt GetStreamOperator fail\n");
71             streamOperator_ = nullptr;
72         }
73     }
74 }
75 
CaptureON(const int streamId,const int captureId,CaptureMode mode)76 RetCode HosCameraDemo::CaptureON(const int streamId, const int captureId, CaptureMode mode)
77 {
78     CAMERA_LOGD("demo test: CaptureON enter mode == %{public}d", mode);
79 
80     std::shared_ptr<Camera::CaptureInfo> captureInfo = std::make_shared<Camera::CaptureInfo>();
81     captureInfo->streamIds_ = {streamId};
82     captureInfo->captureSetting_ = ability_;
83     captureInfo->enableShutterCallback_ = false;
84 
85     int rc = streamOperator_->Capture(captureId, captureInfo, true);
86     if (rc != Camera::NO_ERROR) {
87         CAMERA_LOGE("demo test: CaptureStart Capture error\n");
88         streamOperator_->ReleaseStreams(captureInfo->streamIds_);
89         return RC_ERROR;
90     }
91 
92     if (mode == CAPTURE_PREVIEW) {
93         streamCustomerPreview_->ReceiveFrameOn(nullptr);
94     } else if (mode == CAPTURE_SNAPSHOT) {
95         streamCustomerCapture_->ReceiveFrameOn([this](void* addr, const uint32_t size) {
96             StoreImage(addr, size);
97         });
98     } else if (mode == CAPTURE_VIDEO) {
99         OpenVideoFile();
100 
101         streamCustomerVideo_->ReceiveFrameOn([this](void* addr, const uint32_t size) {
102             StoreVideo(addr, size);
103         });
104     }
105     CAMERA_LOGD("demo test: CaptureON exit");
106 
107     return RC_OK;
108 }
109 
CaptureOff(const int captureId,const CaptureMode mode)110 RetCode HosCameraDemo::CaptureOff(const int captureId, const CaptureMode mode)
111 {
112     int rc = 0;
113     CAMERA_LOGD("demo test: CaptureOff enter mode == %{public}d", mode);
114 
115     if (streamOperator_ == nullptr) {
116         CAMERA_LOGE("demo test: CaptureOff streamOperator_ is nullptr\n");
117         return RC_ERROR;
118     }
119 
120     if (mode == CAPTURE_PREVIEW) {
121         streamCustomerPreview_->ReceiveFrameOff();
122         rc = streamOperator_->CancelCapture(captureId);
123     } else if (mode == CAPTURE_SNAPSHOT) {
124         streamCustomerCapture_->ReceiveFrameOff();
125         rc = streamOperator_->CancelCapture(captureId);
126     } else if (mode == CAPTURE_VIDEO) {
127         streamCustomerVideo_->ReceiveFrameOff();
128         rc = streamOperator_->CancelCapture(captureId);
129         close(videoFd_);
130         videoFd_ = -1;
131     }
132 
133     if (rc != Camera::NO_ERROR) {
134         CAMERA_LOGE("demo test: CaptureOff CancelCapture error mode %{public}d rc == %{public}d\n", mode, rc);
135         return RC_ERROR;
136     }
137     CAMERA_LOGD("demo test: CaptureOff exit");
138 
139     return RC_OK;
140 }
141 
CreateStream(const int streamId,std::shared_ptr<StreamCustomer> & streamCustomer,StreamIntent intent)142 RetCode HosCameraDemo::CreateStream(const int streamId, std::shared_ptr<StreamCustomer>& streamCustomer,
143     StreamIntent intent)
144 {
145     int rc = 0;
146     CAMERA_LOGD("demo test: CreateStream enter");
147 
148     GetStreamOpt();
149     if (streamOperator_ == nullptr) {
150         CAMERA_LOGE("demo test: CreateStream GetStreamOpt() is nullptr\n");
151         return RC_ERROR;
152     }
153 
154     std::shared_ptr<StreamInfo> streamInfo = std::make_shared<StreamInfo>();
155     if (streamInfo == nullptr) {
156         CAMERA_LOGE("demo test: std::make_shared<Camera::StreamInfo>() is nullptr\n");
157         return RC_ERROR;
158     }
159 
160     SetStreamInfo(streamInfo, streamCustomer, streamId, intent);
161     if (streamInfo->bufferQueue_ == nullptr) {
162         CAMERA_LOGE("demo test: CreateStream CreateProducer(); is nullptr\n");
163         return RC_ERROR;
164     }
165 
166     std::vector<std::shared_ptr<StreamInfo>> streamInfos;
167     std::vector<std::shared_ptr<StreamInfo>>().swap(streamInfos);
168     streamInfos.push_back(streamInfo);
169 
170     rc = streamOperator_->CreateStreams(streamInfos);
171     if (rc != Camera::NO_ERROR) {
172         CAMERA_LOGE("demo test: CreateStream CreateStreams error\n");
173         return RC_ERROR;
174     }
175 
176     rc = streamOperator_->CommitStreams(Camera::NORMAL, ability_);
177     if (rc != Camera::NO_ERROR) {
178         CAMERA_LOGE("demo test: CreateStream CommitStreams error\n");
179         streamOperator_->ReleaseStreams({streamId});
180         return RC_ERROR;
181     }
182 
183     CAMERA_LOGD("demo test: CreateStream exit");
184 
185     return RC_OK;
186 }
187 
InitCameraDevice()188 RetCode HosCameraDemo::InitCameraDevice()
189 {
190     int rc = 0;
191 
192     CAMERA_LOGD("demo test: InitCameraDevice enter");
193 
194     if (demoCameraHost_ == nullptr) {
195         CAMERA_LOGE("demo test: InitCameraDevice demoCameraHost_ == nullptr");
196         return RC_ERROR;
197     }
198 
199     (void)demoCameraHost_->GetCameraIds(cameraIds_);
200     if (cameraIds_.empty()) {
201         return RC_ERROR;
202     }
203     const std::string cameraId = cameraIds_.front();
204     demoCameraHost_->GetCameraAbility(cameraId, ability_);
205 
206 #ifdef CAMERA_BUILT_ON_OHOS_LITE
207     std::shared_ptr<CameraDeviceCallback> callback = std::make_shared<CameraDeviceCallback>();
208 #else
209     sptr<CameraDeviceCallback> callback = new CameraDeviceCallback();
210 #endif
211     rc = demoCameraHost_->OpenCamera(cameraIds_.front(), callback, demoCameraDevice_);
212     if (rc != Camera::NO_ERROR || demoCameraDevice_ == nullptr) {
213         CAMERA_LOGE("demo test: InitCameraDevice OpenCamera failed");
214         return RC_ERROR;
215     }
216 
217     CAMERA_LOGD("demo test: InitCameraDevice exit");
218 
219     return RC_OK;
220 }
221 
ReleaseCameraDevice()222 void HosCameraDemo::ReleaseCameraDevice()
223 {
224     if (demoCameraDevice_ != nullptr) {
225         CAMERA_LOGD("demo test: ReleaseCameraDevice close Device");
226         demoCameraDevice_->Close();
227         demoCameraDevice_ = nullptr;
228     }
229 }
230 
InitSensors()231 RetCode HosCameraDemo::InitSensors()
232 {
233     int rc = 0;
234 
235     CAMERA_LOGD("demo test: InitSensors enter");
236 
237     if (demoCameraHost_ != nullptr) {
238         return RC_OK;
239     }
240 #ifdef CAMERA_BUILT_ON_OHOS_LITE
241     demoCameraHost_ = OHOS::Camera::CameraHost::CreateCameraHost();
242 #else
243     constexpr const char *DEMO_SERVICE_NAME = "camera_service";
244     demoCameraHost_ = ICameraHost::Get(DEMO_SERVICE_NAME);
245 #endif
246     if (demoCameraHost_ == nullptr) {
247         CAMERA_LOGE("demo test: ICameraHost::Get error");
248         return RC_ERROR;
249     }
250 
251 #ifdef CAMERA_BUILT_ON_OHOS_LITE
252     hostCallback_ = std::make_shared<CameraHostCallback>();
253 #else
254     hostCallback_ = new CameraHostCallback();
255 #endif
256     rc = demoCameraHost_->SetCallback(hostCallback_);
257     if (rc != Camera::NO_ERROR) {
258         CAMERA_LOGE("demo test: demoCameraHost_->SetCallback(hostCallback_) error");
259         return RC_ERROR;
260     }
261 
262     CAMERA_LOGD("demo test: InitSensors exit");
263 
264     return RC_OK;
265 }
266 
StoreImage(const void * bufStart,const uint32_t size) const267 void HosCameraDemo::StoreImage(const void* bufStart, const uint32_t size) const
268 {
269     constexpr uint32_t pathLen = 64;
270     char path[pathLen] = {0};
271 #ifdef CAMERA_BUILT_ON_OHOS_LITE
272     char prefix[] = "/userdata/photo/";
273 #else
274     char prefix[] = "/data/";
275 #endif
276 
277     int imgFD = 0;
278     int ret;
279 
280     struct timeval start = {};
281     gettimeofday(&start, nullptr);
282     if (sprintf_s(path, sizeof(path), "%spicture_%ld.jpeg", prefix, start.tv_usec) < 0) {
283         CAMERA_LOGE("sprintf_s error .....\n");
284         return;
285     }
286 
287     imgFD = open(path, O_RDWR | O_CREAT, 00766); // 00766:file operate permission
288     if (imgFD == -1) {
289         CAMERA_LOGE("demo test:open image file error %{public}s.....\n", strerror(errno));
290         return;
291     }
292 
293     CAMERA_LOGD("demo test:StoreImage %{public}s buf_start == %{public}p size == %{public}d\n", path, bufStart, size);
294 
295     ret = write(imgFD, bufStart, size);
296     if (ret == -1) {
297         CAMERA_LOGE("demo test:write image file error %{public}s.....\n", strerror(errno));
298     }
299 
300     close(imgFD);
301 }
302 
StoreVideo(const void * bufStart,const uint32_t size) const303 void HosCameraDemo::StoreVideo(const void* bufStart, const uint32_t size) const
304 {
305     int ret = 0;
306 
307     ret = write(videoFd_, bufStart, size);
308     if (ret == -1) {
309         CAMERA_LOGE("demo test:write video file error %{public}s.....\n", strerror(errno));
310     }
311     CAMERA_LOGD("demo test:StoreVideo buf_start == %{public}p size == %{public}d\n", bufStart, size);
312 }
313 
OpenVideoFile()314 void HosCameraDemo::OpenVideoFile()
315 {
316     constexpr uint32_t pathLen = 64;
317     char path[pathLen] = {0};
318 #ifdef CAMERA_BUILT_ON_OHOS_LITE
319     char prefix[] = "/userdata/video/";
320 #else
321     char prefix[] = "/data/";
322 #endif
323     auto seconds = time(nullptr);
324     if (sprintf_s(path, sizeof(path), "%svideo%ld.h264", prefix, seconds) < 0) {
325         CAMERA_LOGE("%{public}s: sprintf  failed", __func__);
326         return;
327     }
328     videoFd_ = open(path, O_RDWR | O_CREAT, 00766); // 00766:file operate permission
329     if (videoFd_ < 0) {
330         CAMERA_LOGE("demo test: StartVideo open %s %{public}s failed", path, strerror(errno));
331     }
332 }
333 
CreateStreams(const int streamIdSecond,StreamIntent intent)334 RetCode HosCameraDemo::CreateStreams(const int streamIdSecond, StreamIntent intent)
335 {
336     int rc = 0;
337     std::vector<std::shared_ptr<StreamInfo>> streamInfos;
338     std::vector<std::shared_ptr<StreamInfo>>().swap(streamInfos);
339 
340     CAMERA_LOGD("demo test: CreateStreams streamIdSecond = %{public}d", streamIdSecond);
341     GetStreamOpt();
342     if (streamOperator_ == nullptr) {
343         CAMERA_LOGE("demo test: CreateStreams GetStreamOpt() is nullptr\n");
344         return RC_ERROR;
345     }
346 
347     std::shared_ptr<StreamInfo> previewStreamInfo = std::make_shared<StreamInfo>();
348     if (previewStreamInfo == nullptr) {
349         CAMERA_LOGE("demo test: CreateStreams previewStreamInfo is nullptr\n");
350         return RC_ERROR;
351     }
352 
353     SetStreamInfo(previewStreamInfo, streamCustomerPreview_, STREAM_ID_PREVIEW, PREVIEW);
354     if (previewStreamInfo->bufferQueue_ == nullptr) {
355         CAMERA_LOGE("demo test: CreateStream CreateProducer(); is nullptr\n");
356         return RC_ERROR;
357     }
358     streamInfos.push_back(previewStreamInfo);
359 
360     std::shared_ptr<StreamInfo> secondStreamInfo = std::make_shared<StreamInfo>();
361     if (secondStreamInfo == nullptr) {
362         CAMERA_LOGE("demo test: CreateStreams previewStreamInfo is nullptr\n");
363         return RC_ERROR;
364     }
365 
366     if (streamIdSecond == STREAM_ID_CAPTURE) {
367         SetStreamInfo(secondStreamInfo, streamCustomerCapture_, STREAM_ID_CAPTURE, intent);
368     } else {
369         SetStreamInfo(secondStreamInfo, streamCustomerVideo_, STREAM_ID_VIDEO, intent);
370     }
371 
372     if (secondStreamInfo->bufferQueue_ == nullptr) {
373         CAMERA_LOGE("demo test: CreateStreams CreateProducer() secondStreamInfo is nullptr\n");
374         return RC_ERROR;
375     }
376     streamInfos.push_back(secondStreamInfo);
377 
378     rc = streamOperator_->CreateStreams(streamInfos);
379     if (rc != Camera::NO_ERROR) {
380         CAMERA_LOGE("demo test: CreateStream CreateStreams error\n");
381         return RC_ERROR;
382     }
383 
384     rc = streamOperator_->CommitStreams(Camera::NORMAL, ability_);
385     if (rc != Camera::NO_ERROR) {
386         CAMERA_LOGE("demo test: CreateStream CommitStreams error\n");
387         std::vector<int> streamIds = {STREAM_ID_PREVIEW, streamIdSecond};
388         streamOperator_->ReleaseStreams(streamIds);
389         return RC_ERROR;
390     }
391 
392     return RC_OK;
393 }
394 
CaptureOnDualStreams(const int streamIdSecond)395 RetCode HosCameraDemo::CaptureOnDualStreams(const int streamIdSecond)
396 {
397     int rc = 0;
398     CAMERA_LOGD("demo test: CaptuCaptureOnDualStreamsreON enter");
399 
400     std::shared_ptr<Camera::CaptureInfo> previewCaptureInfo = std::make_shared<Camera::CaptureInfo>();
401     previewCaptureInfo->streamIds_ = {STREAM_ID_PREVIEW};
402     previewCaptureInfo->captureSetting_ = ability_;
403     previewCaptureInfo->enableShutterCallback_ = false;
404 
405     rc = streamOperator_->Capture(CAPTURE_ID_PREVIEW, previewCaptureInfo, true);
406     if (rc != Camera::NO_ERROR) {
407         CAMERA_LOGE("demo test: CaptureOnDualStreams preview Capture error\n");
408         streamOperator_->ReleaseStreams(previewCaptureInfo->streamIds_);
409         return RC_ERROR;
410     }
411     streamCustomerPreview_->ReceiveFrameOn(nullptr);
412 
413     std::shared_ptr<Camera::CaptureInfo> secondCaptureInfo = std::make_shared<Camera::CaptureInfo>();
414     secondCaptureInfo->streamIds_ = {streamIdSecond};
415     secondCaptureInfo->captureSetting_ = ability_;
416     previewCaptureInfo->enableShutterCallback_ = false;
417 
418     if (streamIdSecond == STREAM_ID_CAPTURE) {
419         rc = streamOperator_->Capture(CAPTURE_ID_CAPTURE, secondCaptureInfo, true);
420         if (rc != Camera::NO_ERROR) {
421             CAMERA_LOGE("demo test: CaptureOnDualStreams CAPTURE_ID_CAPTURE error\n");
422             streamOperator_->ReleaseStreams(secondCaptureInfo->streamIds_);
423             return RC_ERROR;
424         }
425 
426         streamCustomerCapture_->ReceiveFrameOn([this](void* addr, const uint32_t size) {
427             StoreImage(addr, size);
428         });
429     } else {
430         rc = streamOperator_->Capture(CAPTURE_ID_VIDEO, secondCaptureInfo, true);
431         if (rc != Camera::NO_ERROR) {
432             CAMERA_LOGE("demo test: CaptureOnDualStreams CAPTURE_ID_VIDEO error\n");
433             streamOperator_->ReleaseStreams(secondCaptureInfo->streamIds_);
434             return RC_ERROR;
435         }
436 
437         OpenVideoFile();
438         streamCustomerVideo_->ReceiveFrameOn([this](void* addr, const uint32_t size) {
439             StoreVideo(addr, size);
440         });
441     }
442 
443     CAMERA_LOGD("demo test: CaptuCaptureOnDualStreamsreON exit");
444 
445     return RC_OK;
446 }
447 
StartDualStreams(const int streamIdSecond)448 RetCode HosCameraDemo::StartDualStreams(const int streamIdSecond)
449 {
450     RetCode rc = RC_OK;
451 
452     CAMERA_LOGD("demo test: StartDualStreams enter");
453 
454     if (streamCustomerPreview_ == nullptr) {
455         streamCustomerPreview_ = std::make_shared<StreamCustomer>();
456     }
457     if (isPreviewOn_ != 0) {
458         return RC_OK;
459     }
460     isPreviewOn_ = 1;
461     if (streamIdSecond == STREAM_ID_CAPTURE) {
462         if (streamCustomerCapture_ == nullptr) {
463             streamCustomerCapture_ = std::make_shared<StreamCustomer>();
464         }
465 
466         if (isCaptureOn_ == 0) {
467             isCaptureOn_ = 1;
468             rc = CreateStreams(streamIdSecond, STILL_CAPTURE);
469             if (rc != RC_OK) {
470                 CAMERA_LOGE("demo test:StartPreviewStream CreateStreams error");
471                 return RC_ERROR;
472             }
473         }
474     } else {
475         if (streamCustomerVideo_ == nullptr) {
476             streamCustomerVideo_ = std::make_shared<StreamCustomer>();
477         }
478 
479         if (isVideoOn_ == 0) {
480             isVideoOn_ = 1;
481             rc = CreateStreams(streamIdSecond, VIDEO);
482             if (rc != RC_OK) {
483                 CAMERA_LOGE("demo test:StartPreviewStream CreateStreams error");
484                 return RC_ERROR;
485             }
486         }
487     }
488 
489     CAMERA_LOGD("demo test: StartDualStreams exit");
490 
491     return RC_OK;
492 }
493 
StartCaptureStream()494 RetCode HosCameraDemo::StartCaptureStream()
495 {
496     RetCode rc = RC_OK;
497 
498     CAMERA_LOGD("demo test: StartCaptureStream enter");
499     if (streamCustomerCapture_ == nullptr) {
500         streamCustomerCapture_ = std::make_shared<StreamCustomer>();
501     }
502 
503     if (isCaptureOn_ == 0) {
504         isCaptureOn_ = 1;
505 
506         rc = CreateStream(STREAM_ID_CAPTURE, streamCustomerCapture_, STILL_CAPTURE);
507         if (rc != RC_OK) {
508             CAMERA_LOGE("demo test:StartCaptureStream CreateStream error");
509             return RC_ERROR;
510         }
511     }
512 
513     CAMERA_LOGD("demo test: StartCaptureStream exit");
514 
515     return RC_OK;
516 }
517 
StartVideoStream()518 RetCode HosCameraDemo::StartVideoStream()
519 {
520     RetCode rc = RC_OK;
521 
522     CAMERA_LOGD("demo test: StartVideoStream enter");
523     if (streamCustomerVideo_ == nullptr) {
524         streamCustomerVideo_ = std::make_shared<StreamCustomer>();
525     }
526 
527     if (isVideoOn_ == 0) {
528         isVideoOn_ = 1;
529 
530         rc = CreateStream(STREAM_ID_VIDEO, streamCustomerVideo_, VIDEO);
531         if (rc != RC_OK) {
532             CAMERA_LOGE("demo test:StartVideoStream CreateStream error");
533             return RC_ERROR;
534         }
535     }
536 
537     CAMERA_LOGD("demo test: StartVideoStream exit");
538 
539     return RC_OK;
540 }
541 
StartPreviewStream()542 RetCode HosCameraDemo::StartPreviewStream()
543 {
544     RetCode rc = RC_OK;
545 
546     CAMERA_LOGD("demo test: StartPreviewStream enter");
547 
548     if (streamCustomerPreview_ == nullptr) {
549         streamCustomerPreview_ = std::make_shared<StreamCustomer>();
550     }
551 
552     if (isPreviewOn_ == 0) {
553         isPreviewOn_ = 1;
554 
555         rc = CreateStream(STREAM_ID_PREVIEW, streamCustomerPreview_, PREVIEW);
556         if (rc != RC_OK) {
557             CAMERA_LOGE("demo test:StartPreviewStream CreateStream error");
558             return RC_ERROR;
559         }
560     }
561 
562     CAMERA_LOGD("demo test: StartPreviewStream exit");
563 
564     return RC_OK;
565 }
566 
ReleaseAllStream()567 RetCode HosCameraDemo::ReleaseAllStream()
568 {
569     std::vector<int> streamIds = {};
570 
571     CAMERA_LOGD("demo test: ReleaseAllStream enter");
572 
573     if (isPreviewOn_ != 1) {
574         CAMERA_LOGE("demo test: ReleaseAllStream preview is not running");
575         return RC_ERROR;
576     }
577 
578     if (isCaptureOn_ == 1) {
579         CAMERA_LOGD("demo test: ReleaseAllStream STREAM_ID_PREVIEW STREAM_ID_CAPTURE");
580         streamIds = {STREAM_ID_PREVIEW, STREAM_ID_CAPTURE};
581         streamOperator_->ReleaseStreams(streamIds);
582     } else {
583         CAMERA_LOGD("demo test: ReleaseAllStream STREAM_ID_PREVIEW STREAM_ID_VIDEO");
584         streamIds = {STREAM_ID_PREVIEW, STREAM_ID_VIDEO};
585         streamOperator_->ReleaseStreams(streamIds);
586     }
587 
588     isPreviewOn_ = 0;
589     isCaptureOn_ = 0;
590     isVideoOn_ = 0;
591 
592     CAMERA_LOGD("demo test: ReleaseAllStream exit");
593 
594     return RC_OK;
595 }
596 
QuitDemo()597 void HosCameraDemo::QuitDemo()
598 {
599     ReleaseCameraDevice();
600     CAMERA_LOGD("demo test: QuitDemo done\n");
601 }
602 
SetAwbMode(const int mode) const603 void HosCameraDemo::SetAwbMode(const int mode) const
604 {
605     CAMERA_LOGD("demo test: SetAwbMode enter\n");
606 
607     constexpr size_t entryCapacity = 100;
608     constexpr size_t dataCapacity = 2000;
609 
610     std::shared_ptr<CameraSetting> metaData = std::make_shared<CameraSetting>(entryCapacity, dataCapacity);
611 
612     const uint8_t awbMode = mode;
613     metaData->addEntry(OHOS_CONTROL_AWB_MODE, &awbMode, 1);
614     demoCameraDevice_->UpdateSettings(metaData);
615 
616     CAMERA_LOGD("demo test: SetAwbMode exit\n");
617 }
618 
SetAeExpo()619 void HosCameraDemo::SetAeExpo()
620 {
621     int32_t expo;
622 
623     CAMERA_LOGD("demo test: SetAeExpo enter\n");
624 
625     constexpr size_t entryCapacity = 100;
626     constexpr size_t dataCapacity = 2000;
627 
628     std::shared_ptr<CameraSetting> metaData = std::make_shared<CameraSetting>(entryCapacity, dataCapacity);
629 
630     if (aeStatus_) {
631         expo = 0xa0;
632     } else {
633         expo = 0x30;
634     }
635     aeStatus_ = !aeStatus_;
636     metaData->addEntry(OHOS_CONTROL_AE_EXPOSURE_COMPENSATION, &expo, 1);
637     demoCameraDevice_->UpdateSettings(metaData);
638 
639     CAMERA_LOGD("demo test: SetAeExpo exit\n");
640 }
641 
FlashlightOnOff(bool onOff)642 void HosCameraDemo::FlashlightOnOff(bool onOff)
643 {
644     CAMERA_LOGD("demo test: FlashlightOnOff enter\n");
645 
646     if (demoCameraHost_ == nullptr) {
647         CAMERA_LOGE("demo test: FlashlightOnOff demoCameraHost_ == nullptr\n");
648         return;
649     }
650 
651     demoCameraHost_->SetFlashlight(cameraIds_.front(), onOff);
652 
653     CAMERA_LOGD("demo test: FlashlightOnOff exit \n");
654 }
655 
StreamOffline(const int streamId)656 RetCode HosCameraDemo::StreamOffline(const int streamId)
657 {
658     int rc = 0;
659     constexpr size_t offlineDelayTime = 4;
660     CAMERA_LOGD("demo test: StreamOffline enter\n");
661 #ifdef CAMERA_BUILT_ON_OHOS_LITE
662     std::shared_ptr<IStreamOperatorCallback> streamOperatorCallback = std::make_shared<StreamOperatorCallback>();
663     std::shared_ptr<IOfflineStreamOperator> offlineStreamOperator = nullptr;
664 #else
665     sptr<IStreamOperatorCallback> streamOperatorCallback = new StreamOperatorCallback();
666     sptr<IOfflineStreamOperator> offlineStreamOperator = nullptr;
667 #endif
668     rc = streamOperator_->ChangeToOfflineStream({streamId}, streamOperatorCallback, offlineStreamOperator);
669     if (rc != NO_ERROR) {
670         CAMERA_LOGE("demo test: StreamOffline ChangeToOfflineStream error\n");
671         return RC_ERROR;
672     }
673 
674     CaptureOff(CAPTURE_ID_PREVIEW, CAPTURE_PREVIEW);
675     CaptureOff(CAPTURE_ID_CAPTURE, CAPTURE_SNAPSHOT);
676     sleep(1);
677     ReleaseAllStream();
678     ReleaseCameraDevice();
679     sleep(offlineDelayTime);
680 
681     CAMERA_LOGD("demo test: begin to release offlne stream");
682     rc = offlineStreamOperator->CancelCapture(CAPTURE_ID_CAPTURE);
683     if (rc != NO_ERROR) {
684         CAMERA_LOGE("demo test: StreamOffline offlineStreamOperator->CancelCapture error\n");
685         return RC_ERROR;
686     }
687 
688     rc = offlineStreamOperator->Release();
689     if (rc != OHOS::Camera::NO_ERROR) {
690         CAMERA_LOGE("demo test: StreamOffline offlineStreamOperator->Release() error\n");
691         return RC_ERROR;
692     }
693 
694     streamCustomerCapture_->ReceiveFrameOff();
695 
696     CAMERA_LOGD("demo test: StreamOffline exit\n");
697 
698     return RC_OK;
699 }
700 } // namespace OHOS::Camera
701