diff --git a/bundle.json b/bundle.json index 28059c098fda3606abf2cf3f8b4f2575a9a26090..f1de60294239475c5917655824098bf77df1a714 100644 --- a/bundle.json +++ b/bundle.json @@ -91,6 +91,7 @@ } ], "test":[ + "//foundation/distributedhardware/distributed_camera/services/cameraservice/cameraoperator/client/test/sample:dcamera_client_demo", "//foundation/distributedhardware/distributed_camera/services/cameraservice/cameraoperator/client/test/unittest:camera_client_test", "//foundation/distributedhardware/distributed_camera/services/cameraservice/cameraoperator/handler/test/unittest:camera_handler_test", "//foundation/distributedhardware/distributed_camera/services/cameraservice/sinkservice/test/unittest:sink_service_test", diff --git a/dcamera_client_demo.h b/dcamera_client_demo.h new file mode 100644 index 0000000000000000000000000000000000000000..ffa4891d3b1f403bfb087c1fdd4c631e678e10ae --- /dev/null +++ b/dcamera_client_demo.h @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DCAMERA_CLIENT_DEMO_H +#define OHOS_DCAMERA_CLIENT_DEMO_H + +#include +#include +#include +#include +#include + +#include "camera_device_ability_items.h" +#include "camera_metadata_info.h" +#include "capture_input.h" +#include "capture_output.h" + +#include "anonymous_string.h" +#include "dcamera_input_callback.h" +#include "dcamera_manager_callback.h" +#include "dcamera_photo_callback.h" +#include "dcamera_photo_surface_listener.h" +#include "dcamera_preview_callback.h" +#include "dcamera_session_callback.h" +#include "dcamera_utils_tools.h" +#include "dcamera_video_callback.h" +#include "dcamera_video_surface_listener.h" +#include "distributed_camera_constants.h" +#include "distributed_camera_errno.h" +#include "distributed_hardware_log.h" +#include "metadata_utils.h" + +static int32_t FILE_PERMISSIONS_FLAG = 00766; +const uint32_t CAPTURE_WIDTH = 640; +const uint32_t CAPTURE_HEIGTH = 480; +const int32_t LATITUDE = 0; +const int32_t LONGITUDE = 1; +const int32_t ALTITUDE = 2; +const int32_t SLEEP_FIVE_SECOND = 5; +const int32_t SLEEP_TWENTY_SECOND = 20; + +namespace OHOS { +namespace DistributedHardware { +class DemoDCameraPhotoCallback : public CameraStandard::PhotoCallback { +public: + DemoDCameraPhotoCallback(const std::shared_ptr& callback): callback_(callback) + { + } + void OnCaptureStarted(const int32_t captureID) const + { + DHLOGI("DemoDCameraPhotoCallback::OnCaptureStarted captureID: %d", captureID); + } + void OnCaptureEnded(const int32_t captureID, int32_t frameCount) const + { + DHLOGI("DemoDCameraPhotoCallback::OnCaptureEnded captureID: %d frameCount: %d", captureID, frameCount); + } + void OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const + { + DHLOGI("DemoDCameraPhotoCallback::OnFrameShutter captureID: %d timestamp: %llu", captureId, timestamp); + } + void OnCaptureError(const int32_t captureId, const int32_t errorCode) const + { + DHLOGI("DemoDCameraPhotoCallback::OnCaptureError captureID: %d errorCode: %d", captureId, errorCode); + } + +private: + std::shared_ptr callback_; +}; + +class DemoDCameraPreviewCallback : public CameraStandard::PreviewCallback { +public: + DemoDCameraPreviewCallback(const std::shared_ptr& callback) : callback_(callback) + { + } + void OnFrameStarted() const { + DHLOGI("DemoDCameraPreviewCallback::OnFrameStarted."); + } + void OnFrameEnded(const int32_t frameCount) const { + DHLOGI("DemoDCameraPreviewCallback::OnFrameEnded frameCount: %d", frameCount); + } + void OnError(const int32_t errorCode) const { + DHLOGI("DemoDCameraPreviewCallback::OnError errorCode: %d", errorCode); + } + +private: + std::shared_ptr callback_; +}; + +class DemoDCameraVideoCallback : public CameraStandard::VideoCallback { +public: + DemoDCameraVideoCallback(const std::shared_ptr& callback) : callback_(callback) + { + } + void OnFrameStarted() const { + DHLOGI("DemoDCameraVideoCallback::OnFrameStarted."); + } + void OnFrameEnded(const int32_t frameCount) const { + DHLOGI("DemoDCameraVideoCallback::OnFrameEnded frameCount: %d", frameCount); + } + void OnError(const int32_t errorCode) const { + DHLOGI("DemoDCameraVideoCallback::OnError errorCode: %d", errorCode); + } + +private: + std::shared_ptr callback_; +}; + +class DemoDCameraInputCallback : public CameraStandard::ErrorCallback, public CameraStandard::FocusCallback { +public: + explicit DemoDCameraInputCallback(const std::shared_ptr& callback) : callback_(callback) + { + + } + void OnError(const int32_t errorType, const int32_t errorMsg) const + { + DHLOGI("DemoDCameraInputCallback::OnError errorType: %d errorMsg: %d", errorType, errorMsg); + } + void OnFocusState(FocusState state) + { + } +private: + std::shared_ptr callback_; +}; + +class DCameraDemoStateCallback : public StateCallback { +public: + void OnStateChanged(std::shared_ptr& event) override + { + DHLOGI("DCameraDemoStateCallback::OnStateChanged type: %d, result: %d", + event->eventType_, event->eventResult_); + } + + void OnMetadataResult(std::vector>& settings) override; +}; + +class DCameraDemoPhotoResultCallback : public ResultCallback { +public: + void OnPhotoResult(std::shared_ptr& buffer) override; + void OnVideoResult(std::shared_ptr& buffer) override + { + DHLOGI("DCameraDemoPhotoResultCallback::OnVideoResult"); + } +}; + +class DCameraDemoPreviewResultCallback : public ResultCallback { +public: + void OnPhotoResult(std::shared_ptr& buffer) override + { + DHLOGI("DCameraDemoPreviewResultCallback::OnPhotoResult"); + } + + void OnVideoResult(std::shared_ptr& buffer) override; +}; + +class DCameraDemoVideoResultCallback : public ResultCallback { +public: + void OnPhotoResult(std::shared_ptr& buffer) override + { + DHLOGI("DCameraDemoVideoResultCallback::OnPhotoResult"); + } + + void OnVideoResult(std::shared_ptr& buffer) override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DCAMERA_CLIENT_DEMO_H \ No newline at end of file diff --git a/services/cameraservice/cameraoperator/client/test/sample/BUILD.gn b/services/cameraservice/cameraoperator/client/test/sample/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..f385f1be096e7f8d0551fac036f84f72c795df55 --- /dev/null +++ b/services/cameraservice/cameraoperator/client/test/sample/BUILD.gn @@ -0,0 +1,97 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributedhardware/distributed_camera/distributedcamera.gni") + +config("module_private_config") { + include_dirs = [ + "//third_party/jsoncpp/include", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "${graphicstandard_path}/frameworks/surface/include", + "//drivers/peripheral/camera/interfaces/metadata/include", + "${camerastandard_path}/interfaces/inner_api/native/camera/include", + "${camerastandard_path}/interfaces/inner_api/native/camera/include/input", + "${camerastandard_path}/interfaces/inner_api/native/camera/include/output", + "${camerastandard_path}/interfaces/inner_api/native/camera/include/session", + "${camerastandard_path}/services/camera_service/binder/base/include", + "${camerastandard_path}/services/camera_service/binder/client/include", + "${camerastandard_path}/services/camera_service/binder/server/include", + "${camerastandard_path}/services/camera_service/include", + "${fwk_common_path}/log/include", + "${fwk_common_path}/utils/include", + "${fwk_utils_path}/include/log", + "${fwk_utils_path}/include", + ] + + include_dirs += [ + "${distributedcamera_hdf_path}/interfaces/include", + "${common_path}/include/constants", + "${common_path}/include/utils", + "${services_path}/cameraservice/base/include", + "${services_path}/cameraservice/cameraoperator/client/include", + "${services_path}/cameraservice/cameraoperator/client/include/callback", + "${services_path}/cameraservice/cameraoperator/client/include/listener", + ] +} + +ohos_executable("dcamera_client_demo") { + install_enable = false + + sources = [ + "dcamera_client_demo.cpp", + "main.cpp" + ] + + cflags = [ "-fPIC" ] + cflags += [ "-Wall" ] + cflags_cc = cflags + + configs = [ ":module_private_config" ] + + deps = [ + "//third_party/jsoncpp:jsoncpp", + "//utils/native/base:utils", + "${fwk_utils_path}:distributedhardwareutils", + "${common_path}:distributed_camera_utils", + "${graphicstandard_path}:libsurface", + "${camerastandard_path}/frameworks/native/camera:camera_framework", + "//drivers/peripheral/camera/interfaces/metadata:metadata", + "${services_path}/cameraservice/cameraoperator/client:distributed_camera_client", + "${services_path}/cameraservice/sinkservice:distributed_camera_sink" + ] + + public_deps = [ "//drivers/interface/distributed_camera/v1_0:libdistributed_camera_provider_stub_1.0" ] + + external_deps = [ + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "startup_l2:syspara", + "multimedia_media_standard:media_client", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DCameraClientDemo\"", + "LOG_DOMAIN=0xD004100", + ] + + subsystem_name = "distributedhardware" + + part_name = "distributed_camera" +} \ No newline at end of file diff --git a/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.cpp b/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.cpp new file mode 100644 index 0000000000000000000000000000000000000000..412abfefc34142698ec5e37af84fb109da1df173 --- /dev/null +++ b/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.cpp @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dcamera_client_demo.h" + +using namespace OHOS; +using namespace OHOS::Camera; +using namespace OHOS::CameraStandard; +using namespace OHOS::DistributedHardware; + +const std::map METADATA_FOCUS_STATE_MAP = { + { OHOS_CAMERA_FOCUS_STATE_SCAN, FocusCallback::SCAN }, + { OHOS_CAMERA_FOCUS_STATE_FOCUSED, FocusCallback::FOCUSED }, + { OHOS_CAMERA_FOCUS_STATE_UNFOCUSED, FocusCallback::UNFOCUSED } +}; + +void DCameraDemoStateCallback::OnMetadataResult(std::vector>& settings) +{ + DHLOGI("DCameraDemoStateCallback::OnMetadataResult"); + for (auto dcSetting : settings) { + DCSettingsType dcSettingType = dcSetting->type_; + std::string dcSettingValue = dcSetting->value_; + DHLOGI("DCameraDemoStateCallback::OnMetadataResult dcSetting type: %d", dcSettingType); + + std::string metadataStr = Base64Decode(dcSettingValue); + std::shared_ptr cameraMetadata = MetadataUtils::DecodeFromString(metadataStr); + camera_metadata_item_t item; + int32_t ret = FindCameraMetadataItem(cameraMetadata->get(), OHOS_CONTROL_FOCUS_STATE, &item); + if (ret != CAM_META_SUCCESS) { + DHLOGE("DCameraDemoStateCallback::OnMetadataResult camera find metadata item failed, ret: %d", ret); + return; + } + + camera_focus_state_t focusState = static_cast(item.data.u8[0]); + auto iter = METADATA_FOCUS_STATE_MAP.find(focusState); + if (iter == METADATA_FOCUS_STATE_MAP.end()) { + DHLOGE("DCameraDemoStateCallback::OnMetadataResult metadata focus state map find focus state failed"); + return; + } + DHLOGI("DCameraDemoStateCallback::OnMetadataResult focusState: %d", iter->second); + } +} + +void DCameraDemoPhotoResultCallback::OnPhotoResult(std::shared_ptr& buffer) +{ + DHLOGI("DCameraDemoPhotoResultCallback::OnPhotoResult"); + std::cout << "saving photo ..." << std::endl; + char path[1024] = {0}; + int32_t ret = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/data/log/dcamera_photo_%lld.jpg", + GetNowTimeStampMs()); + if (ret < 0) { + DHLOGE("DCameraDemoPhotoResultCallback::OnPhotoResult create photo file failed, ret: %d", ret); + return; + } + + DHLOGI("DCameraDemoPhotoResultCallback::OnPhotoResult saving photo to file %s", path); + int32_t fd = open(path, O_RDWR | O_CREAT, FILE_PERMISSIONS_FLAG); + if (fd == -1) { + DHLOGE("DCameraDemoPhotoResultCallback::OnPhotoResult open file failed, error: %s", strerror(errno)); + return; + } + + ret = write(fd, buffer->Data(), buffer->Capacity()); + if (ret == -1) { + DHLOGE("DCameraDemoPhotoResultCallback::OnPhotoResult write file failed, error: %s", strerror(errno)); + } + + std::cout << "saving photo success" << std::endl; + close(fd); + return; +} + +void DCameraDemoPreviewResultCallback::OnVideoResult(std::shared_ptr& buffer) +{ + DHLOGI("DCameraDemoPreviewResultCallback::OnVideoResult"); + std::cout << "saving preview ..." << std::endl; + char path[1024] = {0}; + int32_t ret = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/data/log/dcamera_preview_%lld.yuv", + GetNowTimeStampMs()); + if (ret < 0) { + DHLOGE("DCameraDemoPreviewResultCallback::OnVideoResult create preview file failed, ret: %d", ret); + return; + } + + DHLOGI("DCameraDemoPreviewResultCallback::OnVideoResult saving preview to file %s", path); + int32_t fd = open(path, O_RDWR | O_CREAT, FILE_PERMISSIONS_FLAG); + if (fd == -1) { + DHLOGE("DCameraDemoPreviewResultCallback::OnVideoResult open file failed, error: %s", strerror(errno)); + return; + } + + ret = write(fd, buffer->Data(), buffer->Capacity()); + if (ret == -1) { + DHLOGE("DCameraDemoPreviewResultCallback::OnVideoResult write file failed, error: %s", strerror(errno)); + } + + std::cout << "saving preview success" << std::endl; + close(fd); + return; +} + +void DCameraDemoVideoResultCallback::OnVideoResult(std::shared_ptr& buffer) +{ + DHLOGI("DCameraDemoVideoResultCallback::OnVideoResult"); + std::cout << "saving video ..." << std::endl; + char path[1024] = {0}; + int32_t ret = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/data/log/dcamera_video_%lld.yuv", + GetNowTimeStampMs()); + if (ret < 0) { + DHLOGE("DCameraDemoVideoResultCallback::OnVideoResult create video file failed, ret: %d", ret); + return; + } + + DHLOGI("DCameraDemoVideoResultCallback::OnVideoResult saving video to file %s", path); + int32_t fd = open(path, O_RDWR | O_CREAT, FILE_PERMISSIONS_FLAG); + if (fd == -1) { + DHLOGE("DCameraDemoVideoResultCallback::OnVideoResult open file failed, error: %s", strerror(errno)); + return; + } + + ret = write(fd, buffer->Data(), buffer->Capacity()); + if (ret == -1) { + DHLOGE("DCameraDemoVideoResultCallback::OnVideoResult write file failed, error: %s", strerror(errno)); + } + + std::cout << "saving video success" << std::endl; + close(fd); + return; +} \ No newline at end of file diff --git a/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.h b/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.h new file mode 100644 index 0000000000000000000000000000000000000000..ffa4891d3b1f403bfb087c1fdd4c631e678e10ae --- /dev/null +++ b/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.h @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DCAMERA_CLIENT_DEMO_H +#define OHOS_DCAMERA_CLIENT_DEMO_H + +#include +#include +#include +#include +#include + +#include "camera_device_ability_items.h" +#include "camera_metadata_info.h" +#include "capture_input.h" +#include "capture_output.h" + +#include "anonymous_string.h" +#include "dcamera_input_callback.h" +#include "dcamera_manager_callback.h" +#include "dcamera_photo_callback.h" +#include "dcamera_photo_surface_listener.h" +#include "dcamera_preview_callback.h" +#include "dcamera_session_callback.h" +#include "dcamera_utils_tools.h" +#include "dcamera_video_callback.h" +#include "dcamera_video_surface_listener.h" +#include "distributed_camera_constants.h" +#include "distributed_camera_errno.h" +#include "distributed_hardware_log.h" +#include "metadata_utils.h" + +static int32_t FILE_PERMISSIONS_FLAG = 00766; +const uint32_t CAPTURE_WIDTH = 640; +const uint32_t CAPTURE_HEIGTH = 480; +const int32_t LATITUDE = 0; +const int32_t LONGITUDE = 1; +const int32_t ALTITUDE = 2; +const int32_t SLEEP_FIVE_SECOND = 5; +const int32_t SLEEP_TWENTY_SECOND = 20; + +namespace OHOS { +namespace DistributedHardware { +class DemoDCameraPhotoCallback : public CameraStandard::PhotoCallback { +public: + DemoDCameraPhotoCallback(const std::shared_ptr& callback): callback_(callback) + { + } + void OnCaptureStarted(const int32_t captureID) const + { + DHLOGI("DemoDCameraPhotoCallback::OnCaptureStarted captureID: %d", captureID); + } + void OnCaptureEnded(const int32_t captureID, int32_t frameCount) const + { + DHLOGI("DemoDCameraPhotoCallback::OnCaptureEnded captureID: %d frameCount: %d", captureID, frameCount); + } + void OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const + { + DHLOGI("DemoDCameraPhotoCallback::OnFrameShutter captureID: %d timestamp: %llu", captureId, timestamp); + } + void OnCaptureError(const int32_t captureId, const int32_t errorCode) const + { + DHLOGI("DemoDCameraPhotoCallback::OnCaptureError captureID: %d errorCode: %d", captureId, errorCode); + } + +private: + std::shared_ptr callback_; +}; + +class DemoDCameraPreviewCallback : public CameraStandard::PreviewCallback { +public: + DemoDCameraPreviewCallback(const std::shared_ptr& callback) : callback_(callback) + { + } + void OnFrameStarted() const { + DHLOGI("DemoDCameraPreviewCallback::OnFrameStarted."); + } + void OnFrameEnded(const int32_t frameCount) const { + DHLOGI("DemoDCameraPreviewCallback::OnFrameEnded frameCount: %d", frameCount); + } + void OnError(const int32_t errorCode) const { + DHLOGI("DemoDCameraPreviewCallback::OnError errorCode: %d", errorCode); + } + +private: + std::shared_ptr callback_; +}; + +class DemoDCameraVideoCallback : public CameraStandard::VideoCallback { +public: + DemoDCameraVideoCallback(const std::shared_ptr& callback) : callback_(callback) + { + } + void OnFrameStarted() const { + DHLOGI("DemoDCameraVideoCallback::OnFrameStarted."); + } + void OnFrameEnded(const int32_t frameCount) const { + DHLOGI("DemoDCameraVideoCallback::OnFrameEnded frameCount: %d", frameCount); + } + void OnError(const int32_t errorCode) const { + DHLOGI("DemoDCameraVideoCallback::OnError errorCode: %d", errorCode); + } + +private: + std::shared_ptr callback_; +}; + +class DemoDCameraInputCallback : public CameraStandard::ErrorCallback, public CameraStandard::FocusCallback { +public: + explicit DemoDCameraInputCallback(const std::shared_ptr& callback) : callback_(callback) + { + + } + void OnError(const int32_t errorType, const int32_t errorMsg) const + { + DHLOGI("DemoDCameraInputCallback::OnError errorType: %d errorMsg: %d", errorType, errorMsg); + } + void OnFocusState(FocusState state) + { + } +private: + std::shared_ptr callback_; +}; + +class DCameraDemoStateCallback : public StateCallback { +public: + void OnStateChanged(std::shared_ptr& event) override + { + DHLOGI("DCameraDemoStateCallback::OnStateChanged type: %d, result: %d", + event->eventType_, event->eventResult_); + } + + void OnMetadataResult(std::vector>& settings) override; +}; + +class DCameraDemoPhotoResultCallback : public ResultCallback { +public: + void OnPhotoResult(std::shared_ptr& buffer) override; + void OnVideoResult(std::shared_ptr& buffer) override + { + DHLOGI("DCameraDemoPhotoResultCallback::OnVideoResult"); + } +}; + +class DCameraDemoPreviewResultCallback : public ResultCallback { +public: + void OnPhotoResult(std::shared_ptr& buffer) override + { + DHLOGI("DCameraDemoPreviewResultCallback::OnPhotoResult"); + } + + void OnVideoResult(std::shared_ptr& buffer) override; +}; + +class DCameraDemoVideoResultCallback : public ResultCallback { +public: + void OnPhotoResult(std::shared_ptr& buffer) override + { + DHLOGI("DCameraDemoVideoResultCallback::OnPhotoResult"); + } + + void OnVideoResult(std::shared_ptr& buffer) override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DCAMERA_CLIENT_DEMO_H \ No newline at end of file diff --git a/services/cameraservice/cameraoperator/client/test/sample/main.cpp b/services/cameraservice/cameraoperator/client/test/sample/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6cb951603806e3eadf1e3a3a1181388517ec5d7 --- /dev/null +++ b/services/cameraservice/cameraoperator/client/test/sample/main.cpp @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dcamera_client_demo.h" + +using namespace OHOS; +using namespace OHOS::Camera; +using namespace OHOS::CameraStandard; +using namespace OHOS::DistributedHardware; + +static sptr GetCameraInfo(sptr cameraManager) +{ + std::vector> cameraObjList = cameraManager->GetCameras(); + for (auto& info : cameraObjList) { + DHLOGI("Distributed Camera Demo: %s position: %d, camera type: %d, connection type: %d", + info->GetID().c_str(), info->GetPosition(), info->GetCameraType(), info->GetConnectionType()); + } + + return cameraObjList.back(); +} + +static void InitCaptureInfo(std::shared_ptr& captureInfo) +{ + captureInfo->width_ = CAPTURE_WIDTH; + captureInfo->height_ = CAPTURE_HEIGTH; + captureInfo->format_ = 1; +} + +static void SetPhotoSurface(sptr& photoSurface, std::shared_ptr photoInfo) +{ + std::shared_ptr photoResultCallback = std::make_shared(); + photoSurface->SetDefaultWidthAndHeight(photoInfo->width_, photoInfo->height_); + photoSurface->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(photoInfo->format_)); + std::shared_ptr photoListener = + std::make_shared(photoSurface, photoResultCallback); + photoSurface->RegisterConsumerListener((sptr &)photoListener); +} + +static void SetPreviewSurface(sptr& previewSurface, std::shared_ptr previewInfo) +{ + std::shared_ptr previewResultCallback = std::make_shared(); + previewSurface->SetDefaultWidthAndHeight(previewInfo->width_, previewInfo->height_); + previewSurface->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(previewInfo->format_)); + std::shared_ptr previewListener = + std::make_shared(previewSurface, previewResultCallback); + previewSurface->RegisterConsumerListener((sptr &)previewListener); +} + +static void SetVideoSurface(sptr& videoSurface, std::shared_ptr videoInfo) +{ + std::shared_ptr videoResultCallback = std::make_shared(); + videoSurface->SetDefaultWidthAndHeight(videoInfo->width_, videoInfo->height_); + videoSurface->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(videoInfo->format_)); + std::shared_ptr videoListener = + std::make_shared(videoSurface, videoResultCallback); + videoSurface->RegisterConsumerListener((sptr &)videoListener); +} + +static void SetCaptureSettings(std::shared_ptr& photoCaptureSettings) +{ + DHLOGI("Distributed Camera Demo: SetCaptureSettings"); + // rotation + PhotoCaptureSetting::RotationConfig rotation = PhotoCaptureSetting::RotationConfig::Rotation_0; + photoCaptureSettings->SetRotation(rotation); + // jpeg quality + PhotoCaptureSetting::QualityLevel quality = PhotoCaptureSetting::QualityLevel::HIGH_QUALITY; + photoCaptureSettings->SetQuality(quality); + // gps coordinates + double gpsCoordinates[3] = { 33.52256, 56.32586, 22.665132 }; + std::unique_ptr location = std::make_unique(); + location->latitude = gpsCoordinates[LATITUDE]; + location->longitude = gpsCoordinates[LONGITUDE]; + location->altitude = gpsCoordinates[ALTITUDE]; + photoCaptureSettings->SetLocation(location); +} + +static void SetFocusAndExposure(sptr& cameraInput) +{ + ((sptr &)cameraInput)->LockForControl(); + camera_focus_mode_enum_t focusMode = OHOS_CAMERA_FOCUS_MODE_AUTO; + camera_exposure_mode_enum_t exposureMode = OHOS_CAMERA_EXPOSURE_MODE_AUTO; + int32_t exposureValue = 0; + std::vector biasRange = ((sptr &)cameraInput)->GetExposureBiasRange(); + if (!biasRange.empty()) { + DHLOGI("Distributed Camera Demo: biasRange.size(): %d", biasRange.size()); + exposureValue = biasRange[0]; + } + ((sptr &)cameraInput)->SetFocusMode(focusMode); // 对焦模式 + ((sptr &)cameraInput)->SetExposureMode(exposureMode); // 曝光模式 + ((sptr &)cameraInput)->SetExposureBias(exposureValue); // 曝光补偿 + ((sptr &)cameraInput)->UnlockForControl(); +} + +int main() { + DHLOGI("========== Distributed Camera Demo Start =========="); + std::shared_ptr stateCallback = std::make_shared(); + + DHLOGI("Distributed Camera Demo: Create CameraManager and CaptureSession"); + sptr cameraManager = CameraManager::GetInstance(); + cameraManager->SetCallback(std::make_shared()); + sptr captureSession = cameraManager->CreateCaptureSession(); + captureSession->SetCallback(std::make_shared(stateCallback)); + + DHLOGI("Distributed Camera Demo: Create CameraInfo and CaptureInput"); + sptr cameraInfo = GetCameraInfo(cameraManager); + sptr cameraInput = cameraManager->CreateCameraInput(cameraInfo); + std::shared_ptr inputCallback = std::make_shared(stateCallback); + ((sptr &)cameraInput)->SetErrorCallback(inputCallback); + ((sptr &)cameraInput)->SetFocusCallback(inputCallback); + + std::shared_ptr photoInfo = std::make_shared(); + InitCaptureInfo(photoInfo); + + std::shared_ptr previewInfo = std::make_shared(); + InitCaptureInfo(previewInfo); + + std::shared_ptr videoInfo = std::make_shared(); + InitCaptureInfo(videoInfo); + + DHLOGI("Distributed Camera Demo: Create PhotoOutput, width = %d, height = %d, format = %d", + photoInfo->width_, photoInfo->height_, photoInfo->format_); + sptr photoSurface = Surface::CreateSurfaceAsConsumer(); + SetPhotoSurface(photoSurface, photoInfo); + sptr photoOutput = cameraManager->CreatePhotoOutput(photoSurface); + ((sptr &)photoOutput)->SetCallback(std::make_shared(stateCallback)); + + std::shared_ptr photoCaptureSettings = std::make_shared(); + SetCaptureSettings(photoCaptureSettings); + + DHLOGI("Distributed Camera Demo: Create PreviewOutput, width = %d, height = %d, format = %d", + previewInfo->width_, previewInfo->height_, previewInfo->format_); + sptr previewSurface = Surface::CreateSurfaceAsConsumer(); + SetPreviewSurface(previewSurface, previewInfo); + sptr previewOutput = cameraManager->CreateCustomPreviewOutput(previewSurface, + previewInfo->width_, previewInfo->height_); + ((sptr &)previewOutput)->SetCallback(std::make_shared(stateCallback)); + + DHLOGI("Distributed Camera Demo: Create VideoOutput, width = %d, height = %d, format = %d", + videoInfo->width_, videoInfo->height_, videoInfo->format_); + sptr videoSurface = Surface::CreateSurfaceAsConsumer(); + SetVideoSurface(videoSurface, videoInfo); + sptr videoOutput = cameraManager->CreateVideoOutput(videoSurface); + ((sptr &)videoOutput)->SetCallback(std::make_shared(stateCallback)); + + captureSession->BeginConfig(); + captureSession->AddInput(cameraInput); + captureSession->AddOutput(photoOutput); + captureSession->AddOutput(previewOutput); + captureSession->CommitConfig(); + captureSession->Start(); + sleep(SLEEP_FIVE_SECOND); + + SetFocusAndExposure(cameraInput); + sleep(SLEEP_FIVE_SECOND); + + ((sptr &)photoOutput)->Capture(photoCaptureSettings); + sleep(SLEEP_TWENTY_SECOND); + + captureSession->Stop(); + captureSession->Release(); + cameraInput->Release(); + cameraManager->SetCallback(nullptr); + + DHLOGI("========== Distributed Camera Demo End =========="); + return 0; +} \ No newline at end of file