1/* 2 * Copyright (c) 2023 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#ifndef OHOS_DCAMERA_CLIENT_DEMO_H 17#define OHOS_DCAMERA_CLIENT_DEMO_H 18 19#include <fstream> 20 21#include "anonymous_string.h" 22#include "camera_device_ability_items.h" 23#include "camera_input.h" 24#include "camera_manager.h" 25#include "camera_metadata_operator.h" 26#include "camera_output_capability.h" 27#include "capture_input.h" 28#include "capture_output.h" 29#include "capture_session.h" 30#include "dcamera_capture_info_cmd.h" 31#include "dcamera_utils_tools.h" 32#include "distributed_camera_constants.h" 33#include "distributed_camera_errno.h" 34#include "distributed_hardware_log.h" 35#include "metadata_utils.h" 36#include "photo_output.h" 37#include "preview_output.h" 38#include "surface.h" 39#include "video_output.h" 40 41namespace OHOS { 42namespace DistributedHardware { 43class DemoDCameraBufferConsumerListener : public IBufferConsumerListener { 44public: 45 explicit DemoDCameraBufferConsumerListener(const sptr<IConsumerSurface>& surface) : surface_(surface) 46 { 47 } 48 49 void OnBufferAvailable() 50 { 51 DHLOGI("DemoDCameraBufferConsumerListener::OnBufferAvailable"); 52 if (surface_ == nullptr) { 53 DHLOGE("DemoDCameraBufferConsumerListener surface is null"); 54 return; 55 } 56 57 int32_t flushFence = 0; 58 int64_t timestamp = 0; 59 OHOS::Rect damage; 60 OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr; 61 surface_->AcquireBuffer(buffer, flushFence, timestamp, damage); 62 if (buffer == nullptr) { 63 DHLOGE("DemoDCameraBufferConsumerListener AcquireBuffer failed"); 64 return; 65 } 66 67 width_ = buffer->GetWidth(); 68 height_ = buffer->GetHeight(); 69 size_ = buffer->GetSize(); 70 address_ = static_cast<char *>(buffer->GetVirAddr()); 71 buffer->GetExtraData()->ExtraGet("dataSize", dataSize_); 72 73#ifdef DCAMERA_COMMON 74 actualSize_ = width_ * height_ * RGB_BYTES_PER_PIXEL; 75#else 76 actualSize_ = width_ * height_ * YUV_BYTES_PER_PIXEL / Y2UV_RATIO; 77#endif 78 79 SaveFile(); 80 surface_->ReleaseBuffer(buffer, -1); 81 } 82 83protected: 84 virtual void SaveFile() const = 0; 85 86protected: 87 constexpr static int32_t Y2UV_RATIO = 2; 88 constexpr static int32_t YUV_BYTES_PER_PIXEL = 3; 89 constexpr static int32_t RGB_BYTES_PER_PIXEL = 4; 90 91 char *address_ = nullptr; 92 int32_t actualSize_ = 0; 93 int32_t dataSize_ = 0; 94 int32_t height_ = 0; 95 int32_t width_ = 0; 96 int32_t size_ = 0; 97 sptr<IConsumerSurface> surface_; 98}; 99 100class DemoDCameraPhotoSurfaceListener : public DemoDCameraBufferConsumerListener { 101public: 102 explicit DemoDCameraPhotoSurfaceListener(const sptr<IConsumerSurface>& surface) 103 : DemoDCameraBufferConsumerListener(surface) 104 { 105 } 106 107protected: 108 void SaveFile() const override 109 { 110 DHLOGI("DemoDCameraPhotoSurfaceListener::SaveFile width: %{public}d, height: %{public}d, size: %{public}d, " 111 "dataSize: %{public}d, actualSize: %{public}d", width_, height_, size_, dataSize_, actualSize_); 112 if ((address_ == nullptr) || (dataSize_ <= 0)) { 113 DHLOGE("DemoDCameraPhotoSurfaceListener invalid params, dataSize: %{public}d", dataSize_); 114 return; 115 } 116 117 std::ofstream ofs; 118 std::cout << "saving photo ..." << std::endl; 119 std::string fileName = "/data/log/dcamera_photo_" + std::to_string(GetNowTimeStampMs()) + ".jpg"; 120 ofs.open(fileName, std::ios::binary | std::ios::out); 121 if (!ofs.is_open()) { 122 DHLOGE("DemoDCameraPhotoSurfaceListener open file failed"); 123 return; 124 } 125 ofs.write(address_, dataSize_); 126 ofs.close(); 127 std::cout << "saving photo success" << std::endl; 128 } 129}; 130 131class DemoDCameraPreviewSurfaceListener : public DemoDCameraBufferConsumerListener { 132public: 133 explicit DemoDCameraPreviewSurfaceListener(const sptr<IConsumerSurface>& surface) 134 : DemoDCameraBufferConsumerListener(surface) 135 { 136 } 137 138protected: 139 void SaveFile() const override 140 { 141 DHLOGI("DemoDCameraPreviewSurfaceListener::SaveFile width: %{public}d, height: %{public}d, size: %{public}d, " 142 "dataSize: %{public}d, actualSize: %{public}d", width_, height_, size_, dataSize_, actualSize_); 143 if ((address_ == nullptr) || (actualSize_ <= 0)) { 144 DHLOGE("DemoDCameraPreviewSurfaceListener invalid params, actualSize: %{public}d", actualSize_); 145 return; 146 } 147 148 std::ofstream ofs; 149 std::cout << "saving preview ..." << std::endl; 150 std::string resolution = std::to_string(width_) + "_" + std::to_string(height_); 151 std::string fileName = "/data/log/dcamera_preview_" + resolution + ".yuv"; 152 ofs.open(fileName, std::ios::binary | std::ios::out | std::ios::app); 153 if (!ofs.is_open()) { 154 DHLOGE("DemoDCameraPreviewSurfaceListener open file failed"); 155 return; 156 } 157 ofs.write(address_, actualSize_); 158 ofs.close(); 159 std::cout << "saving preview success" << std::endl; 160 } 161}; 162 163class DemoDCameraVideoSurfaceListener : public DemoDCameraBufferConsumerListener { 164public: 165 explicit DemoDCameraVideoSurfaceListener(const sptr<IConsumerSurface>& surface) 166 : DemoDCameraBufferConsumerListener(surface) 167 { 168 } 169 170protected: 171 void SaveFile() const override 172 { 173 DHLOGI("DemoDCameraVideoSurfaceListener::SaveFile width: %{public}d, height: %{public}d, size: %{public}d, " 174 "dataSize: %{public}d, actualSize: %{public}d", width_, height_, size_, dataSize_, actualSize_); 175 if ((address_ == nullptr) || (actualSize_ <= 0)) { 176 DHLOGE("DemoDCameraVideoSurfaceListener invalid params, actualSize: %{public}d", actualSize_); 177 return; 178 } 179 180 std::ofstream ofs; 181 std::cout << "saving video ..." << std::endl; 182 std::string resolution = std::to_string(width_) + "_" + std::to_string(height_); 183 std::string fileName = "/data/log/dcamera_video_" + resolution + ".yuv"; 184 ofs.open(fileName, std::ios::binary | std::ios::out | std::ios::app); 185 if (!ofs.is_open()) { 186 DHLOGE("DemoDCameraVideoSurfaceListener open file failed"); 187 return; 188 } 189 ofs.write(address_, actualSize_); 190 ofs.close(); 191 std::cout << "saving video success" << std::endl; 192 } 193}; 194 195class DemoDCameraPhotoCallback : public CameraStandard::PhotoStateCallback { 196public: 197 void OnCaptureStarted(const int32_t captureID) const 198 { 199 DHLOGI("DemoDCameraPhotoCallback::OnCaptureStarted captureID: %{public}d", captureID); 200 } 201 202 void OnCaptureStarted(const int32_t captureID, uint32_t exposureTime) const 203 { 204 DHLOGI("DemoDCameraPhotoCallback::OnCaptureStarted captureID: %{public}d, exposureTime: %{public}u", 205 captureID, exposureTime); 206 } 207 208 void OnCaptureEnded(const int32_t captureID, int32_t frameCount) const 209 { 210 DHLOGI("DemoDCameraPhotoCallback::OnCaptureEnded captureID: %{public}d frameCount: %{public}d", 211 captureID, frameCount); 212 } 213 214 void OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const 215 { 216 DHLOGI("DemoDCameraPhotoCallback::OnFrameShutter captureID: %{public}d timestamp: %{public}" PRIu64, 217 captureId, timestamp); 218 } 219 220 void OnFrameShutterEnd(const int32_t captureId, const uint64_t timestamp) const 221 { 222 DHLOGI("DemoDCameraPhotoCallback::OnFrameShutterEnd captureID: %{public}d timestamp: %{public}" PRIu64, 223 captureId, timestamp); 224 } 225 226 void OnCaptureReady(const int32_t captureId, const uint64_t timestamp) const 227 { 228 DHLOGI("DemoDCameraPhotoCallback::OnFrameShutterEnd captureID: %{public}d timestamp: %{public}" PRIu64, 229 captureId, timestamp); 230 } 231 232 void OnEstimatedCaptureDuration(const int32_t duration) const 233 { 234 DHLOGI("DemoDCameraPhotoCallback::OnEstimatedCaptureDuration duration: %{public}d", duration); 235 } 236 237 void OnCaptureError(const int32_t captureId, const int32_t errorCode) const 238 { 239 DHLOGI("DemoDCameraPhotoCallback::OnCaptureError captureID: %{public}d errorCode: %{public}d", 240 captureId, errorCode); 241 } 242}; 243 244class DemoDCameraPreviewCallback : public CameraStandard::PreviewStateCallback { 245public: 246 void OnFrameStarted() const 247 { 248 DHLOGI("DemoDCameraPreviewCallback::OnFrameStarted."); 249 } 250 251 void OnFrameEnded(const int32_t frameCount) const 252 { 253 DHLOGI("DemoDCameraPreviewCallback::OnFrameEnded frameCount: %{public}d", frameCount); 254 } 255 256 void OnError(const int32_t errorCode) const 257 { 258 DHLOGI("DemoDCameraPreviewCallback::OnError errorCode: %{public}d", errorCode); 259 } 260 261 void OnSketchStatusDataChanged(const CameraStandard::SketchStatusData& statusData) const 262 { 263 DHLOGI("DemoDCameraPreviewCallback::OnSketchStatusDataChanged."); 264 } 265}; 266 267class DemoDCameraVideoCallback : public CameraStandard::VideoStateCallback { 268public: 269 void OnFrameStarted() const 270 { 271 DHLOGI("DemoDCameraVideoCallback::OnFrameStarted."); 272 } 273 274 void OnFrameEnded(const int32_t frameCount) const 275 { 276 DHLOGI("DemoDCameraVideoCallback::OnFrameEnded frameCount: %{public}d", frameCount); 277 } 278 279 void OnError(const int32_t errorCode) const 280 { 281 DHLOGI("DemoDCameraVideoCallback::OnError errorCode: %{public}d", errorCode); 282 } 283}; 284 285class DemoDCameraInputCallback : public CameraStandard::ErrorCallback { 286public: 287 void OnError(const int32_t errorType, const int32_t errorMsg) const 288 { 289 DHLOGI("DemoDCameraInputCallback::OnError errorType: %{public}d errorMsg: %{public}d", errorType, errorMsg); 290 } 291}; 292 293class DemoDCameraManagerCallback : public CameraStandard::CameraManagerCallback { 294public: 295 void OnCameraStatusChanged(const CameraStandard::CameraStatusInfo &cameraStatusInfo) const 296 { 297 DHLOGI("DemoDCameraManagerCallback::OnCameraStatusChanged cameraStatus: %{public}d", 298 cameraStatusInfo.cameraStatus); 299 } 300 301 void OnFlashlightStatusChanged(const std::string &cameraID, 302 const CameraStandard::FlashStatus flashStatus) const 303 { 304 DHLOGI("DemoDCameraManagerCallback::OnFlashlightStatusChanged cameraID: %{public}s, flashStatus: %{public}d", 305 GetAnonyString(cameraID).c_str(), flashStatus); 306 } 307}; 308 309class DemoDCameraSessionCallback : public CameraStandard::SessionCallback, public CameraStandard::FocusCallback { 310public: 311 void OnError(int32_t errorCode) 312 { 313 DHLOGI("DemoDCameraSessionCallback::OnError errorCode: %{public}d", errorCode); 314 } 315 316 void OnFocusState(FocusState state) 317 { 318 DHLOGI("DemoDCameraSessionCallback::OnFocusState state: %{public}d", state); 319 } 320}; 321 322int32_t InitCameraStandard(OHOS::CameraStandard::CameraPosition position); 323void InitCaptureInfo(int32_t width, int32_t height); 324void InitPhotoOutput(void); 325void InitPreviewOutput(void); 326void InitVideoOutput(void); 327void ConfigCaptureSession(void); 328void ReleaseResource(void); 329int32_t Capture(); 330int32_t Video(); 331std::shared_ptr<OHOS::CameraStandard::PhotoCaptureSetting> ConfigPhotoCaptureSetting(); 332void ConfigFocusFlashAndExposure(bool isVideo); 333int32_t GetPreviewProfiles(std::vector<OHOS::CameraStandard::Size> &previewResolution); 334bool IsValid(const OHOS::CameraStandard::Size& size); 335} // namespace DistributedHardware 336} // namespace OHOS 337#endif // OHOS_DCAMERA_CLIENT_DEMO_H