diff --git a/services/cameraservice/cameraoperator/client/test/sample/BUILD.gn b/services/cameraservice/cameraoperator/client/test/sample/BUILD.gn index 861a0fec054b108c430d16bc415abfd4ec5532f9..e65157c3057cee80c9054d73aef81d5a610e2f4b 100644 --- a/services/cameraservice/cameraoperator/client/test/sample/BUILD.gn +++ b/services/cameraservice/cameraoperator/client/test/sample/BUILD.gn @@ -51,12 +51,20 @@ ohos_executable("dcamera_client_demo") { install_enable = false sources = [ - "dcamera_client_demo.cpp", "main.cpp", ] configs = [ ":module_private_config" ] + cflags = [ + "-fPIC", + "-Wall", + ] + + if ("${product_name}" == "m40") { + cflags += [ "-DPRODUCT_M40" ] + } + deps = [ "${camerastandard_path}/frameworks/native/camera:camera_framework", "${common_path}:distributed_camera_utils", @@ -81,6 +89,8 @@ ohos_executable("dcamera_client_demo") { "LOG_DOMAIN=0xD004100", ] + cflags_cc = cflags + subsystem_name = "distributedhardware" part_name = "distributed_camera" diff --git a/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.cpp b/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.cpp deleted file mode 100644 index 0bed1c4f0038043a3e30f2d12e720d3524d95490..0000000000000000000000000000000000000000 --- a/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.cpp +++ /dev/null @@ -1,145 +0,0 @@ -/* - * 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; - -namespace OHOS { -namespace DistributedHardware { -static 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; -} -} // namespace DistributedHardware -} // namespace OHOS \ 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 index c217874f98106964f8b9446bf1fde9c0459c79d3..e51c56c487596cf28d688a75ad65a76a289eabbf 100644 --- a/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.h +++ b/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.h @@ -16,192 +16,275 @@ #ifndef OHOS_DCAMERA_CLIENT_DEMO_H #define OHOS_DCAMERA_CLIENT_DEMO_H -#include -#include -#include -#include -#include +#include +#include "anonymous_string.h" #include "camera_device_ability_items.h" +#include "camera_info.h" #include "camera_input.h" #include "camera_manager.h" -#include "camera_metadata_info.h" +#include "camera_metadata_operator.h" #include "capture_input.h" #include "capture_output.h" #include "capture_session.h" -#include "preview_output.h" -#include "video_output.h" -#include "anonymous_string.h" -#include "dcamera_photo_surface_listener.h" +#include "dcamera_capture_info_cmd.h" #include "dcamera_utils_tools.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" - -constexpr int32_t FILE_PERMISSIONS_FLAG = 00766; -constexpr uint32_t CAPTURE_WIDTH = 640; -constexpr uint32_t CAPTURE_HEIGTH = 480; -constexpr int32_t LATITUDE = 0; -constexpr int32_t LONGITUDE = 1; -constexpr int32_t ALTITUDE = 2; -constexpr int32_t SLEEP_FIVE_SECOND = 5; -constexpr int32_t SLEEP_TWENTY_SECOND = 20; -constexpr int32_t PHOTO_FORMAT = 4; -constexpr int32_t VIDEO_FORMAT = 3; +#include "photo_output.h" +#include "preview_output.h" +#include "surface.h" +#include "video_output.h" namespace OHOS { namespace DistributedHardware { -class DemoDCameraPhotoCallback : public CameraStandard::PhotoCallback { +class DemoDCameraBufferConsumerListener : public IBufferConsumerListener { +public: + explicit DemoDCameraBufferConsumerListener(const sptr& surface) : surface_(surface) + { + } + + void OnBufferAvailable() + { + DHLOGI("DemoDCameraBufferConsumerListener::OnBufferAvailable"); + if (surface_ == nullptr) { + DHLOGE("DemoDCameraBufferConsumerListener surface is null"); + return; + } + + int32_t flushFence = 0; + int64_t timestamp = 0; + OHOS::Rect damage; + OHOS::sptr buffer = nullptr; + surface_->AcquireBuffer(buffer, flushFence, timestamp, damage); + if (buffer == nullptr) { + DHLOGE("DemoDCameraBufferConsumerListener AcquireBuffer failed"); + return; + } + + width_ = buffer->GetWidth(); + height_ = buffer->GetHeight(); + size_ = buffer->GetSize(); + address_ = static_cast(buffer->GetVirAddr()); + buffer->GetExtraData()->ExtraGet("dataSize", dataSize_); + + #ifdef PRODUCT_M40 + actualSize_ = width_ * height_ * YUV_BYTES_PER_PIXEL / Y2UV_RATIO; + #else + actualSize_ = width_ * height_ * RGB_BYTES_PER_PIXEL; + #endif + + SaveFile(); + surface_->ReleaseBuffer(buffer, -1); + } + +protected: + virtual void SaveFile() const = 0; + +protected: + constexpr static int32_t Y2UV_RATIO = 2; + constexpr static int32_t YUV_BYTES_PER_PIXEL = 3; + constexpr static int32_t RGB_BYTES_PER_PIXEL = 4; + + char *address_ = nullptr; + int32_t actualSize_ = 0; + int32_t dataSize_ = 0; + int32_t height_ = 0; + int32_t width_ = 0; + int32_t size_ = 0; + sptr surface_; +}; + +class DemoDCameraPhotoSurfaceListener : public DemoDCameraBufferConsumerListener +{ public: - explicit DemoDCameraPhotoCallback(const std::shared_ptr& callback): callback_(callback) + explicit DemoDCameraPhotoSurfaceListener(const sptr& surface) : DemoDCameraBufferConsumerListener(surface) { } + +protected: + void SaveFile() const + { + DHLOGI("DemoDCameraPhotoSurfaceListener::SaveFile width: %d, height: %d, size: %d, dataSize: %d, " + + "actualSize: %d", width_, height_, size_, dataSize_, actualSize_); + if ((address_ == nullptr) || (dataSize_ <= 0)) { + DHLOGE("DemoDCameraPhotoSurfaceListener invalid params, dataSize: %d", dataSize_); + return; + } + + std::ofstream ofs; + std::cout << "saving photo ..." << std::endl; + std::string fileName = "/data/log/dcamera_photo_" + std::to_string(GetNowTimeStampMs()) + ".jpg"; + ofs.open(fileName, std::ios::binary | std::ios::out); + if (!ofs.is_open()) { + DHLOGE("DemoDCameraPhotoSurfaceListener open file failed"); + return; + } + ofs.write(address_, dataSize_); + ofs.close(); + std::cout << "saving photo success" << std::endl; + } +}; + +class DemoDCameraPreviewSurfaceListener : public DemoDCameraBufferConsumerListener +{ +public: + explicit DemoDCameraPreviewSurfaceListener(const sptr& surface) : DemoDCameraBufferConsumerListener(surface) + { + } + +protected: + void SaveFile() const + { + DHLOGI("DemoDCameraPreviewSurfaceListener::SaveFile width: %d, height: %d, size: %d, dataSize: %d, " + + "actualSize: %d", width_, height_, size_, dataSize_, actualSize_); + if ((address_ == nullptr) || (actualSize_ <= 0)) { + DHLOGE("DemoDCameraPreviewSurfaceListener invalid params, actualSize: %d", actualSize_); + return; + } + + std::ofstream ofs; + std::cout << "saving preview ..." << std::endl; + std::string resolution = std::to_string(width_) + "_" + std::to_string(height_); + std::string fileName = "/data/log/dcamera_preview_" + resolution + ".yuv"; + ofs.open(fileName, std::ios::binary | std::ios::out | std::ios::app); + if (!ofs.is_open()) { + DHLOGE("DemoDCameraPreviewSurfaceListener open file failed"); + return; + } + ofs.write(address_, actualSize_); + ofs.close(); + std::cout << "saving preview success" << std::endl; + } +}; + +class DemoDCameraVideoSurfaceListener : public DemoDCameraBufferConsumerListener +{ +public: + explicit DemoDCameraVideoSurfaceListener(const sptr& surface) : DemoDCameraBufferConsumerListener(surface) + { + } + +protected: + void SaveFile() const + { + DHLOGI("DemoDCameraVideoSurfaceListener::SaveFile width: %d, height: %d, size: %d, dataSize: %d, " + + "actualSize: %d", width_, height_, size_, dataSize_, actualSize_); + if ((address_ == nullptr) || (actualSize_ <= 0)) { + DHLOGE("DemoDCameraVideoSurfaceListener invalid params, actualSize: %d", actualSize_); + return; + } + + std::ofstream ofs; + std::cout << "saving video ..." << std::endl; + std::string resolution = std::to_string(width_) + "_" + std::to_string(height_); + std::string fileName = "/data/log/dcamera_video_" + resolution + ".yuv"; + ofs.open(fileName, std::ios::binary | std::ios::out | std::ios::app); + if (!ofs.is_open()) { + DHLOGE("DemoDCameraVideoSurfaceListener open file failed"); + return; + } + ofs.write(address_, actualSize_); + ofs.close(); + std::cout << "saving video success" << std::endl; + } +}; + +class DemoDCameraPhotoCallback : public CameraStandard::PhotoCallback { +public: 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: - explicit 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: - explicit 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) { + DHLOGI("DemoDCameraInputCallback::OnFocusState state: %d", state); } -private: - std::shared_ptr callback_; }; class DemoDCameraManagerCallback : public CameraStandard::CameraManagerCallback { public: void OnCameraStatusChanged(const CameraStandard::CameraStatusInfo &cameraStatusInfo) const { + DHLOGI("DemoDCameraManagerCallback::OnCameraStatusChanged cameraStatus: %d", cameraStatusInfo.cameraStatus); } + void OnFlashlightStatusChanged(const std::string &cameraID, const CameraStandard::FlashlightStatus flashStatus) const { + DHLOGI("DemoDCameraManagerCallback::OnFlashlightStatusChanged cameraID: %s, flashStatus: %d", + GetAnonyString(cameraID).c_str(), flashStatus); } }; class DemoDCameraSessionCallback : public CameraStandard::SessionCallback { public: - explicit DemoDCameraSessionCallback(const std::shared_ptr& callback) : callback_(callback) - { - } void OnError(int32_t errorCode) { DHLOGI("DemoDCameraSessionCallback::OnError errorCode: %d", errorCode); } - -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 diff --git a/services/cameraservice/cameraoperator/client/test/sample/main.cpp b/services/cameraservice/cameraoperator/client/test/sample/main.cpp index a0066075c1a9c08b4c11fdce28fd262d5b3b3338..c7f753e9c4a488fd9b56d8c57ddf4befab44bd11 100644 --- a/services/cameraservice/cameraoperator/client/test/sample/main.cpp +++ b/services/cameraservice/cameraoperator/client/test/sample/main.cpp @@ -20,185 +20,201 @@ using namespace OHOS::Camera; using namespace OHOS::CameraStandard; using namespace OHOS::DistributedHardware; -static sptr g_photoSurface = nullptr; -static sptr g_previewSurface = nullptr; -static sptr g_videoSurface = nullptr; +constexpr double LATITUDE = 22.306; +constexpr double LONGITUDE = 52.12; +constexpr double ALTITUDE = 2.365; +constexpr int32_t PHOTO_WIDTH = 640; +constexpr int32_t PHOTO_HEIGTH = 480; +constexpr int32_t PREVIEW_WIDTH = 640; +constexpr int32_t PREVIEW_HEIGTH = 480; +constexpr int32_t VIDEO_WIDTH = 640; +constexpr int32_t VIDEO_HEIGTH = 480; +constexpr int32_t SLEEP_FIVE_SECOND = 5; +constexpr int32_t SLEEP_TWENTY_SECOND = 20; + +static sptr g_cameraInfo = nullptr; +static sptr g_cameraManager = nullptr; +static sptr g_cameraInput = nullptr; static sptr g_photoOutput = nullptr; static sptr g_previewOutput = nullptr; static sptr g_videoOutput = nullptr; -static std::shared_ptr g_photoListener = nullptr; -static std::shared_ptr g_previewListener = nullptr; -static std::shared_ptr g_videoListener = nullptr; -static std::shared_ptr g_photoResultCallback = nullptr; -static std::shared_ptr g_previewResultCallback = nullptr; -static std::shared_ptr g_videoResultCallback = nullptr; - -static sptr GetCameraInfo(sptr cameraManager) +static sptr g_captureSession = nullptr; +static std::shared_ptr g_photoInfo = nullptr; +static std::shared_ptr g_previewInfo = nullptr; +static std::shared_ptr g_videoInfo = nullptr; + +#ifdef PRODUCT_M40 + constexpr int32_t PHOTO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_JPEG; + constexpr int32_t PREVIEW_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_YCRCB_420_SP; + constexpr int32_t VIDEO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_YCRCB_420_SP; +#else + constexpr int32_t PHOTO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888; + constexpr int32_t PREVIEW_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888; + constexpr int32_t VIDEO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888; +#endif + +int32_t InitCameraStandard() { - std::vector> cameraObjList = cameraManager->GetCameras(); - for (auto& info : cameraObjList) { - DHLOGI("Distributed Camera Demo: %s, position: %d, camera type: %d, connection type: %d", - GetAnonyString(info->GetID()).c_str(), info->GetPosition(), info->GetCameraType(), - info->GetConnectionType()); + g_cameraManager = CameraManager::GetInstance(); + g_cameraManager->SetCallback(std::make_shared()); + + g_captureSession = g_cameraManager->CreateCaptureSession(); + g_captureSession->SetCallback(std::make_shared()); + + std::vector> cameraObjList = g_cameraManager->GetCameras(); + for (auto info : cameraObjList) { + DHLOGI("Camera: %s, position: %d, camera type: %d, connection type: %d", GetAnonyString(info->GetID()).c_str(), + info->GetPosition(), info->GetCameraType(), info->GetConnectionType()); + // OHOS_CAMERA_POSITION_FRONT or OHOS_CAMERA_POSITION_BACK + if ((info->GetPosition() == OHOS_CAMERA_POSITION_FRONT) && + (info->GetConnectionType() == OHOS_CAMERA_CONNECTION_TYPE_REMOTE)) { + g_cameraInfo = info; + break; + } } - return cameraObjList.back(); -} - -static void InitPhotoInfo(std::shared_ptr& photoInfo) -{ - photoInfo->width_ = CAPTURE_WIDTH; - photoInfo->height_ = CAPTURE_HEIGTH; - photoInfo->format_ = PHOTO_FORMAT; -} + if (g_cameraInfo == nullptr) { + DHLOGE("Distributed Camera Demo: have no remote camera"); + return DCAMERA_BAD_VALUE; + } -static void InitPreviewInfo(std::shared_ptr& previewInfo) -{ - previewInfo->width_ = CAPTURE_WIDTH; - previewInfo->height_ = CAPTURE_HEIGTH; - previewInfo->format_ = VIDEO_FORMAT; + g_cameraInput = g_cameraManager->CreateCameraInput(g_cameraInfo); + std::shared_ptr inputCallback = std::make_shared(); + ((sptr &)g_cameraInput)->SetErrorCallback(inputCallback); + ((sptr &)g_cameraInput)->SetFocusCallback(inputCallback); + return DCAMERA_OK; } -static void InitVideoInfo(std::shared_ptr& videoInfo) +void InitCaptureInfo() { - videoInfo->width_ = CAPTURE_WIDTH; - videoInfo->height_ = CAPTURE_HEIGTH; - videoInfo->format_ = VIDEO_FORMAT; + g_photoInfo = std::make_shared(); + g_photoInfo->width_ = PHOTO_WIDTH; + g_photoInfo->height_ = PHOTO_HEIGTH; + g_photoInfo->format_ = PHOTO_FORMAT; + + g_previewInfo = std::make_shared(); + g_previewInfo->width_ = PREVIEW_WIDTH; + g_previewInfo->height_ = PREVIEW_HEIGTH; + g_previewInfo->format_ = PREVIEW_FORMAT; + + g_videoInfo = std::make_shared(); + g_videoInfo->width_ = VIDEO_WIDTH; + g_videoInfo->height_ = VIDEO_HEIGTH; + g_videoInfo->format_ = VIDEO_FORMAT; } -static void SetPhotoOutput(sptr& cameraManager, std::shared_ptr& photoInfo, - std::shared_ptr& stateCallback) +void InitPhotoOutput() { DHLOGI("Distributed Camera Demo: Create PhotoOutput, width = %d, height = %d, format = %d", - photoInfo->width_, photoInfo->height_, photoInfo->format_); - g_photoSurface = Surface::CreateSurfaceAsConsumer(); - g_photoResultCallback = std::make_shared(); - g_photoSurface->SetDefaultWidthAndHeight(photoInfo->width_, photoInfo->height_); - g_photoSurface->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(photoInfo->format_)); - g_photoListener = std::make_shared(g_photoSurface, g_photoResultCallback); - g_photoSurface->RegisterConsumerListener((sptr &)g_photoListener); - g_photoOutput = cameraManager->CreatePhotoOutput(g_photoSurface); - ((sptr &)g_photoOutput)->SetCallback(std::make_shared(stateCallback)); + g_photoInfo->width_, g_photoInfo->height_, g_photoInfo->format_); + sptr photoSurface = Surface::CreateSurfaceAsConsumer(); + sptr photoListener = new DemoDCameraPhotoSurfaceListener(photoSurface); + photoSurface->SetDefaultWidthAndHeight(g_photoInfo->width_, g_photoInfo->height_); + photoSurface->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(g_photoInfo->format_)); + photoSurface->RegisterConsumerListener(photoListener); + g_photoOutput = g_cameraManager->CreatePhotoOutput(photoSurface); + ((sptr &)g_photoOutput)->SetCallback(std::make_shared()); } -static void SetPreviewOutput(sptr& cameraManager, std::shared_ptr& previewInfo, - std::shared_ptr& stateCallback) +void InitPreviewOutput() { DHLOGI("Distributed Camera Demo: Create PreviewOutput, width = %d, height = %d, format = %d", - previewInfo->width_, previewInfo->height_, previewInfo->format_); - g_previewSurface = Surface::CreateSurfaceAsConsumer(); - g_previewResultCallback = std::make_shared(); - g_previewSurface->SetDefaultWidthAndHeight(previewInfo->width_, previewInfo->height_); - g_previewSurface->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(previewInfo->format_)); - g_previewListener = std::make_shared(g_previewSurface, g_previewResultCallback); - g_previewSurface->RegisterConsumerListener((sptr &)g_previewListener); - g_previewOutput = cameraManager->CreateCustomPreviewOutput(g_previewSurface, - previewInfo->width_, previewInfo->height_); - ((sptr &)g_previewOutput)->SetCallback(std::make_shared(stateCallback)); + g_previewInfo->width_, g_previewInfo->height_, g_previewInfo->format_); + sptr previewSurface = Surface::CreateSurfaceAsConsumer(); + sptr previewListener = new DemoDCameraPreviewSurfaceListener(previewSurface); + previewSurface->SetDefaultWidthAndHeight(g_previewInfo->width_, g_previewInfo->height_); + previewSurface->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(g_previewInfo->format_)); + previewSurface->RegisterConsumerListener(previewListener); + g_previewOutput = g_cameraManager->CreatePreviewOutput(previewSurface); + ((sptr &)g_previewOutput)->SetCallback(std::make_shared()); } -static void SetVideoOutput(sptr& cameraManager, std::shared_ptr& videoInfo, - std::shared_ptr& stateCallback) +void InitVideoOutput() { DHLOGI("Distributed Camera Demo: Create VideoOutput, width = %d, height = %d, format = %d", - videoInfo->width_, videoInfo->height_, videoInfo->format_); - g_videoSurface = Surface::CreateSurfaceAsConsumer(); - g_videoResultCallback = std::make_shared(); - g_videoSurface->SetDefaultWidthAndHeight(videoInfo->width_, videoInfo->height_); - g_videoSurface->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(videoInfo->format_)); - g_videoListener = std::make_shared(g_videoSurface, g_videoResultCallback); - g_videoSurface->RegisterConsumerListener((sptr &)g_videoListener); - g_videoOutput = cameraManager->CreateVideoOutput(g_videoSurface); - ((sptr &)g_videoOutput)->SetCallback(std::make_shared(stateCallback)); + g_videoInfo->width_, g_videoInfo->height_, g_videoInfo->format_); + sptr videoSurface = Surface::CreateSurfaceAsConsumer(); + sptr videoListener = new DemoDCameraVideoSurfaceListener(videoSurface); + videoSurface->SetDefaultWidthAndHeight(g_videoInfo->width_, g_videoInfo->height_); + videoSurface->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(g_videoInfo->format_)); + videoSurface->RegisterConsumerListener(videoListener); + g_videoOutput = g_cameraManager->CreateVideoOutput(videoSurface); + ((sptr &)g_videoOutput)->SetCallback(std::make_shared()); } -static void SetCaptureSettings(std::shared_ptr& photoCaptureSettings) +void ConfigFocusAndExposure() { - 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 - std::unique_ptr location = std::make_unique(); - location->latitude = 22.306; // 22.306: latitude - location->longitude = 52.12; // 52.12:longitude - location->altitude = 2.365; // 2.365: altitude - photoCaptureSettings->SetLocation(location); -} - -static void SetFocusAndExposure(sptr& cameraInput) -{ - ((sptr &)cameraInput)->LockForControl(); + ((sptr &)g_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(); + std::vector biasRange = ((sptr &)g_cameraInput)->GetExposureBiasRange(); if (!biasRange.empty()) { - DHLOGI("Distributed Camera Demo: biasRange.size(): %d", biasRange.size()); + DHLOGI("Distributed Camera Demo: get %d exposure compensation range", biasRange.size()); exposureValue = biasRange[0]; } - ((sptr &)cameraInput)->SetFocusMode(focusMode); - ((sptr &)cameraInput)->SetExposureMode(exposureMode); - ((sptr &)cameraInput)->SetExposureBias(exposureValue); - ((sptr &)cameraInput)->UnlockForControl(); + ((sptr &)g_cameraInput)->SetFocusMode(focusMode); + ((sptr &)g_cameraInput)->SetExposureMode(exposureMode); + ((sptr &)g_cameraInput)->SetExposureBias(exposureValue); + ((sptr &)g_cameraInput)->UnlockForControl(); } -int main() +std::shared_ptr ConfigPhotoCaptureSetting() { - DHLOGI("========== Distributed Camera Demo Start =========="); - std::shared_ptr stateCallback = std::make_shared(); - - sptr cameraManager = CameraManager::GetInstance(); - cameraManager->SetCallback(std::make_shared()); - sptr captureSession = cameraManager->CreateCaptureSession(); - captureSession->SetCallback(std::make_shared(stateCallback)); - - 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(); - InitPhotoInfo(photoInfo); - - std::shared_ptr previewInfo = std::make_shared(); - InitPreviewInfo(previewInfo); - - std::shared_ptr videoInfo = std::make_shared(); - InitVideoInfo(videoInfo); - - SetPhotoOutput(cameraManager, photoInfo, stateCallback); - std::shared_ptr photoCaptureSettings = std::make_shared(); - SetCaptureSettings(photoCaptureSettings); + // Rotation + PhotoCaptureSetting::RotationConfig rotation = PhotoCaptureSetting::RotationConfig::Rotation_0; + photoCaptureSettings->SetRotation(rotation); + // QualityLevel + PhotoCaptureSetting::QualityLevel quality = PhotoCaptureSetting::QualityLevel::HIGH_QUALITY; + photoCaptureSettings->SetQuality(quality); + // Location + std::unique_ptr location = std::make_unique(); + location->latitude = LATITUDE; + location->longitude = LONGITUDE; + location->altitude = ALTITUDE; + photoCaptureSettings->SetLocation(location); + return photoCaptureSettings; +} - SetPreviewOutput(cameraManager, previewInfo, stateCallback); +int main() +{ + DHLOGI("========== Distributed Camera Demo Start =========="); + int32_t ret = InitCameraStandard(); + if (ret != DCAMERA_OK) { + std::cout << "have no remote camera" << std::endl; + return 0; + } - SetVideoOutput(cameraManager, videoInfo, stateCallback); + InitCaptureInfo(); + InitPhotoOutput(); + InitPreviewOutput(); + InitVideoOutput(); + + g_captureSession->BeginConfig(); + g_captureSession->AddInput(g_cameraInput); + g_captureSession->AddOutput(g_previewOutput); + g_captureSession->AddOutput(g_videoOutput); + g_captureSession->AddOutput(g_photoOutput); + g_captureSession->CommitConfig(); + g_captureSession->Start(); + sleep(SLEEP_FIVE_SECOND); - captureSession->BeginConfig(); - captureSession->AddInput(cameraInput); - captureSession->AddOutput(g_photoOutput); - captureSession->AddOutput(g_previewOutput); - captureSession->CommitConfig(); - captureSession->Start(); + ((sptr &)g_videoOutput)->Start(); sleep(SLEEP_FIVE_SECOND); - SetFocusAndExposure(cameraInput); + ConfigFocusAndExposure(); sleep(SLEEP_FIVE_SECOND); - ((sptr &)g_photoOutput)->Capture(photoCaptureSettings); + ((sptr &)g_photoOutput)->Capture(ConfigPhotoCaptureSetting()); sleep(SLEEP_TWENTY_SECOND); - captureSession->Stop(); - captureSession->Release(); - cameraInput->Release(); - cameraManager->SetCallback(nullptr); + ((sptr &)g_videoOutput)->Stop(); + sleep(SLEEP_FIVE_SECOND); + g_captureSession->Stop(); + g_captureSession->Release(); + g_cameraInput->Release(); DHLOGI("========== Distributed Camera Demo End =========="); return 0; } \ No newline at end of file