1 /* 2 * Copyright (c) 2025 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_CAMERA_PHOTO_OUTPUT_CALLBACK_H 17 #define OHOS_CAMERA_PHOTO_OUTPUT_CALLBACK_H 18 19 #include "hstream_capture_photo_callback_stub.h" 20 #include "hstream_capture_thumbnail_callback_stub.h" 21 #include "stream_capture_photo_asset_callback_stub.h" 22 #include <native_image.h> 23 #include <pixel_map.h> 24 25 26 namespace OHOS { 27 namespace CameraStandard { 28 class PhotoOutput; 29 namespace DeferredProcessing { 30 class TaskManager; 31 } 32 class PhotoAvailableCallback { 33 public: 34 PhotoAvailableCallback() = default; 35 virtual ~PhotoAvailableCallback() = default; 36 37 virtual void OnPhotoAvailable(const std::shared_ptr<Media::NativeImage> nativeImage, bool isRaw) const = 0; 38 }; 39 40 class PhotoAssetAvailableCallback { 41 public: 42 PhotoAssetAvailableCallback() = default; 43 virtual ~PhotoAssetAvailableCallback() = default; 44 45 virtual void OnPhotoAssetAvailable(const int32_t captureId, const std::string &uri, 46 int32_t cameraShotType, const std::string &burstKey) const = 0; 47 }; 48 49 class ThumbnailCallback { 50 public: 51 ThumbnailCallback() = default; 52 virtual ~ThumbnailCallback() = default; 53 54 virtual void OnThumbnailAvailable(int32_t captureId, int64_t timestamp, 55 std::unique_ptr<Media::PixelMap> pixelMap) const = 0; 56 }; 57 58 class CameraBufferProcessor : public Media::IBufferProcessor { 59 public: CameraBufferProcessor(sptr<Surface> surface)60 explicit CameraBufferProcessor(sptr<Surface> surface) : surface_(surface) {} ~CameraBufferProcessor()61 ~CameraBufferProcessor() 62 { 63 surface_ = nullptr; 64 } BufferRelease(sptr<SurfaceBuffer> & buffer)65 void BufferRelease(sptr<SurfaceBuffer>& buffer) override 66 { 67 if (surface_ != nullptr) { 68 surface_->ReleaseBuffer(buffer, -1); 69 } 70 } 71 72 private: 73 wptr<Surface> surface_ = nullptr; 74 }; 75 76 class HStreamCapturePhotoCallbackImpl : public HStreamCapturePhotoCallbackStub { 77 public: HStreamCapturePhotoCallbackImpl(PhotoOutput * photoOutput)78 explicit HStreamCapturePhotoCallbackImpl(PhotoOutput* photoOutput) : innerPhotoOutput_(photoOutput) {} 79 80 ~HStreamCapturePhotoCallbackImpl() = default; 81 82 int32_t OnPhotoAvailable(sptr<SurfaceBuffer> surfaceBuffer, int64_t timestamp, bool isRaw) override; 83 GetPhotoOutput()84 inline sptr<PhotoOutput> GetPhotoOutput() 85 { 86 if (innerPhotoOutput_ == nullptr) { 87 return nullptr; 88 } 89 return innerPhotoOutput_.promote(); 90 } 91 92 private: 93 wptr<PhotoOutput> innerPhotoOutput_ = nullptr; 94 }; 95 96 class HStreamCapturePhotoAssetCallbackImpl : public StreamCapturePhotoAssetCallbackStub { 97 public: HStreamCapturePhotoAssetCallbackImpl(PhotoOutput * photoOutput)98 explicit HStreamCapturePhotoAssetCallbackImpl(PhotoOutput *photoOutput) : innerPhotoOutput_(photoOutput) 99 {} 100 101 ~HStreamCapturePhotoAssetCallbackImpl() = default; 102 103 int32_t OnPhotoAssetAvailable( 104 int32_t captureId, const std::string &uri, int32_t cameraShotType, const std::string &burstKey) override; 105 GetPhotoOutput()106 inline sptr<PhotoOutput> GetPhotoOutput() 107 { 108 if (innerPhotoOutput_ == nullptr) { 109 return nullptr; 110 } 111 return innerPhotoOutput_.promote(); 112 } 113 114 private: 115 wptr<PhotoOutput> innerPhotoOutput_ = nullptr; 116 }; 117 118 class HStreamCaptureThumbnailCallbackImpl : public HStreamCaptureThumbnailCallbackStub { 119 public: HStreamCaptureThumbnailCallbackImpl(PhotoOutput * photoOutput)120 explicit HStreamCaptureThumbnailCallbackImpl(PhotoOutput* photoOutput) : innerPhotoOutput_(photoOutput) {} 121 122 ~HStreamCaptureThumbnailCallbackImpl() = default; 123 124 int32_t OnThumbnailAvailable(sptr<SurfaceBuffer> surfaceBuffer, int64_t timestamp) override; 125 GetPhotoOutput()126 inline sptr<PhotoOutput> GetPhotoOutput() 127 { 128 if (innerPhotoOutput_ == nullptr) { 129 return nullptr; 130 } 131 return innerPhotoOutput_.promote(); 132 } 133 134 private: 135 std::unique_ptr<Media::PixelMap> CreatePixelMapFromSurfaceBuffer(sptr<SurfaceBuffer> &surfaceBuffer, 136 int32_t width, int32_t height, bool isHdr); 137 std::unique_ptr<Media::PixelMap> SetPixelMapYuvInfo(sptr<SurfaceBuffer> &surfaceBuffer, 138 std::unique_ptr<Media::PixelMap> pixelMap, bool isHdr); 139 140 wptr<PhotoOutput> innerPhotoOutput_ = nullptr; 141 }; 142 143 class PhotoNativeConsumer : public IBufferConsumerListener { 144 public: 145 explicit PhotoNativeConsumer(wptr<PhotoOutput> photoOutput); 146 ~PhotoNativeConsumer() override; 147 void OnBufferAvailable() override; 148 void ClearTaskManager(); 149 std::shared_ptr<DeferredProcessing::TaskManager> GetDefaultTaskManager(); 150 151 private: 152 void ExecuteOnBufferAvailable(); 153 void ExecutePhotoAvailable(sptr<SurfaceBuffer> surfaceBuffer, int64_t timestamp); 154 void ExecutePhotoAssetAvailable(sptr<SurfaceBuffer> surfaceBuffer, int64_t timestamp); 155 156 std::mutex taskManagerMutex_; 157 std::shared_ptr<DeferredProcessing::TaskManager> taskManager_ = nullptr; 158 159 wptr<PhotoOutput> innerPhotoOutput_ = nullptr; 160 }; 161 } // namespace CameraStandard 162 } // namespace OHOS 163 #endif // OHOS_CAMERA_PHOTO_OUTPUT_CALLBACK_H 164