1 /* 2 * Copyright (c) 2021-2022 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_H_STREAM_CAPTURE_H 17 #define OHOS_CAMERA_H_STREAM_CAPTURE_H 18 #include <condition_variable> 19 #define EXPORT_API __attribute__((visibility("default"))) 20 21 #include <atomic> 22 #include <cstdint> 23 #include <iostream> 24 #include <refbase.h> 25 26 #include "camera_metadata_info.h" 27 #include "hstream_capture_stub.h" 28 #include "hstream_common.h" 29 #include "v1_0/istream_operator.h" 30 #include "v1_2/istream_operator.h" 31 #include "safe_map.h" 32 33 namespace OHOS { 34 namespace CameraStandard { 35 using OHOS::HDI::Camera::V1_0::BufferProducerSequenceable; 36 using namespace OHOS::HDI::Camera::V1_0; 37 class PhotoAssetIntf; 38 class PictureIntf; 39 class CameraServerPhotoProxy; 40 class HStreamOperator; 41 class ConcurrentMap { 42 public: 43 void Insert(const int32_t& key, const std::shared_ptr<PhotoAssetIntf>& value); 44 std::shared_ptr<PhotoAssetIntf> Get(const int32_t& key); 45 void Release(); 46 void Erase(const int32_t& key); 47 std::mutex& GetMutex(const int32_t& key); 48 std::condition_variable& GetCv(const int32_t& key); 49 bool ReadyToUnlock(const int32_t& key, const int32_t& step, const int32_t& mode); 50 void IncreaseCaptureStep(const int32_t& key); 51 private: 52 std::map<int32_t, std::shared_ptr<PhotoAssetIntf>> map_; 53 std::map<int32_t, std::mutex> mutexes_; 54 std::map<int32_t, int32_t> step_; 55 std::map<int32_t, std::condition_variable> cv_; 56 std::mutex map_mutex_; 57 }; 58 constexpr const char* BURST_UUID_UNSET = ""; 59 class EXPORT_API HStreamCapture : public HStreamCaptureStub, public HStreamCommon { 60 public: 61 HStreamCapture(sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height); 62 ~HStreamCapture(); 63 64 int32_t LinkInput(wptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator, 65 std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility) override; 66 void SetStreamInfo(StreamInfo_V1_1 &streamInfo) override; 67 int32_t SetThumbnail(bool isEnabled, const sptr<OHOS::IBufferProducer> &producer) override; 68 int32_t EnableRawDelivery(bool enabled) override; 69 int32_t EnableMovingPhoto(bool enabled) override; 70 int32_t SetBufferProducerInfo(const std::string bufName, const sptr<OHOS::IBufferProducer> &producer) override; 71 int32_t DeferImageDeliveryFor(int32_t type) override; 72 int32_t Capture(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureSettings) override; 73 int32_t CancelCapture() override; 74 int32_t ConfirmCapture() override; 75 int32_t ReleaseStream(bool isDelay) override; 76 int32_t Release() override; 77 int32_t SetCallback(sptr<IStreamCaptureCallback> &callback) override; 78 int32_t UnSetCallback() override; 79 int32_t OnCaptureStarted(int32_t captureId); 80 int32_t OnCaptureStarted(int32_t captureId, uint32_t exposureTime); 81 int32_t OnCaptureEnded(int32_t captureId, int32_t frameCount); 82 int32_t OnCaptureError(int32_t captureId, int32_t errorType); 83 int32_t OnFrameShutter(int32_t captureId, uint64_t timestamp); 84 int32_t OnFrameShutterEnd(int32_t captureId, uint64_t timestamp); 85 int32_t OnCaptureReady(int32_t captureId, uint64_t timestamp); 86 int32_t OnOfflineDeliveryFinished(int32_t captureId); 87 void DumpStreamInfo(CameraInfoDumper& infoDumper) override; 88 void SetRotation(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting_, int32_t captureId); 89 void SetMode(int32_t modeName); 90 int32_t GetMode(); 91 int32_t IsDeferredPhotoEnabled() override; 92 int32_t IsDeferredVideoEnabled() override; 93 int32_t SetMovingPhotoVideoCodecType(int32_t videoCodecType) override; 94 int32_t GetMovingPhotoVideoCodecType(); 95 int32_t OperatePermissionCheck(uint32_t interfaceCode) override; 96 SafeMap<int32_t, int32_t> rotationMap_ = {}; 97 bool IsBurstCapture(int32_t captureId) const; 98 bool IsBurstCover(int32_t captureId) const; 99 int32_t GetCurBurstSeq(int32_t captureId) const; 100 std::string GetBurstKey(int32_t captureId) const; 101 void SetBurstImages(int32_t captureId, std::string imageId); 102 void CheckResetBurstKey(int32_t captureId); 103 int32_t SetCameraPhotoRotation(bool isEnable) override; 104 int32_t CreateMediaLibraryPhotoAssetProxy(int32_t captureId); 105 int32_t UpdateMediaLibraryPhotoAssetProxy(sptr<CameraPhotoProxy> photoProxy) override; 106 std::shared_ptr<PhotoAssetIntf> GetPhotoAssetInstance(int32_t captureId); 107 bool GetAddPhotoProxyEnabled(); 108 int32_t AcquireBufferToPrepareProxy(int32_t captureId) override; 109 int32_t EnableOfflinePhoto(bool isEnable) override; 110 bool IsHasEnableOfflinePhoto(); 111 void SwitchToOffline(); 112 bool IsHasSwitchToOffline(); 113 void SetStreamOperator(wptr<HStreamOperator> hStreamOperator); 114 int32_t CreateMediaLibrary(sptr<CameraPhotoProxy>& photoProxy, std::string& uri, int32_t& cameraShotType, 115 std::string& burstKey, int64_t timestamp) override; 116 int32_t CreateMediaLibrary(std::shared_ptr<PictureIntf> picture, sptr<CameraPhotoProxy> &photoProxy, 117 std::string &uri, int32_t &cameraShotType, std::string& burstKey, int64_t timestamp) override; 118 119 private: 120 int32_t CheckBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings, 121 const int32_t &preparedCaptureId); 122 int32_t PrepareBurst(int32_t captureId); 123 void ResetBurst(); 124 void ResetBurstKey(int32_t captureId); 125 void EndBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureMetadataSetting_); 126 void ProcessCaptureInfoPhoto(CaptureInfo& captureInfoPhoto, 127 const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings, int32_t captureId); 128 void SetCameraPhotoProxyInfo(sptr<CameraServerPhotoProxy> cameraPhotoProxy); 129 sptr<IStreamCaptureCallback> streamCaptureCallback_; 130 void FillingPictureExtendStreamInfos(StreamInfo_V1_1 &streamInfo, int32_t format); 131 void FillingRawAndThumbnailStreamInfo(StreamInfo_V1_1 &streamInfo); 132 std::mutex callbackLock_; 133 int32_t thumbnailSwitch_; 134 int32_t rawDeliverySwitch_; 135 int32_t movingPhotoSwitch_; 136 std::condition_variable testDelay_; 137 std::mutex testDelayMutex_; 138 sptr<BufferProducerSequenceable> thumbnailBufferQueue_; 139 sptr<BufferProducerSequenceable> rawBufferQueue_; 140 sptr<BufferProducerSequenceable> gainmapBufferQueue_; 141 sptr<BufferProducerSequenceable> deepBufferQueue_; 142 sptr<BufferProducerSequenceable> exifBufferQueue_; 143 sptr<BufferProducerSequenceable> debugBufferQueue_; 144 int32_t modeName_; 145 int32_t deferredPhotoSwitch_; 146 int32_t deferredVideoSwitch_; 147 bool enableCameraPhotoRotation_ = false; 148 std::atomic<bool> isCaptureReady_ = true; 149 std::string curBurstKey_ = BURST_UUID_UNSET; 150 bool isBursting_ = false; 151 std::map<int32_t, std::vector<std::string>> burstImagesMap_; 152 std::map<int32_t, std::string> burstkeyMap_; 153 std::map<int32_t, int32_t> burstNumMap_; 154 mutable std::mutex burstLock_; 155 int32_t burstNum_; 156 int32_t videoCodecType_ = 0; 157 std::mutex photoAssetLock_; 158 ConcurrentMap photoAssetProxy_; 159 bool mEnableOfflinePhoto_ = false; 160 bool mSwitchToOfflinePhoto_ = false; 161 int32_t mlastCaptureId = 0; 162 wptr<HStreamOperator> hStreamOperator_; 163 std::map<int32_t, std::unique_ptr<std::mutex>> mutexMap; 164 }; 165 } // namespace CameraStandard 166 } // namespace OHOS 167 #endif // OHOS_CAMERA_H_STREAM_CAPTURE_H 168