• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "stream_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 #include "icamera_ipc_checker.h"
33 
34 namespace OHOS {
35 namespace CameraStandard {
36 using OHOS::HDI::Camera::V1_0::BufferProducerSequenceable;
37 using namespace OHOS::HDI::Camera::V1_0;
38 class PhotoAssetIntf;
39 class PictureIntf;
40 class CameraServerPhotoProxy;
41 class HStreamOperator;
42 class PictureAssembler;
43 namespace DeferredProcessing {
44 class TaskManager;
45 }
46 class ConcurrentMap {
47 public:
48     void Insert(const int32_t& key, const std::shared_ptr<PhotoAssetIntf>& value);
49     std::shared_ptr<PhotoAssetIntf> Get(const int32_t& key);
50     void Release();
51     void Erase(const int32_t& key);
52     bool ReadyToUnlock(const int32_t& key, const int32_t& step, const int32_t& mode);
53     bool WaitForUnlock(const int32_t& key, const int32_t& step, const int32_t& mode,
54                     const std::chrono::seconds& timeout);
55     void IncreaseCaptureStep(const int32_t& key);
56 private:
57     std::map<int32_t, std::shared_ptr<PhotoAssetIntf>> map_;
58     std::map<int32_t, std::shared_ptr<std::mutex>> mutexes_;
59     std::map<int32_t, int32_t> step_;
60     std::map<int32_t, std::shared_ptr<std::condition_variable>> cv_;
61     std::mutex map_mutex_;
62 };
63 constexpr const char* BURST_UUID_UNSET = "";
64 class EXPORT_API HStreamCapture : public StreamCaptureStub, public HStreamCommon, public ICameraIpcChecker {
65 public:
66     HStreamCapture(sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height);
67     HStreamCapture(int32_t format, int32_t width, int32_t height);
68     ~HStreamCapture();
69 
70     int32_t LinkInput(wptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,
71         std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility) override;
72     void SetStreamInfo(StreamInfo_V1_1 &streamInfo) override;
73     int32_t SetThumbnail(bool isEnabled) override;
74     int32_t EnableRawDelivery(bool enabled) override;
75     int32_t EnableMovingPhoto(bool enabled) override;
76     int32_t SetBufferProducerInfo(const std::string& bufName, const sptr<OHOS::IBufferProducer> &producer) override;
77     int32_t DeferImageDeliveryFor(int32_t type) override;
78     int32_t Capture(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureSettings) override;
79     int32_t CancelCapture() override;
80     int32_t ConfirmCapture() override;
81     int32_t ReleaseStream(bool isDelay) override;
82     int32_t Release() override;
83     int32_t SetCallback(const sptr<IStreamCaptureCallback> &callback) override;
84     int32_t SetPhotoAvailableCallback(const sptr<IStreamCapturePhotoCallback> &callback) override;
85     int32_t SetPhotoAssetAvailableCallback(const sptr<IStreamCapturePhotoAssetCallback> &callback) override;
86     int32_t SetThumbnailCallback(const sptr<IStreamCaptureThumbnailCallback> &callback) override;
87     int32_t UnSetCallback() override;
88     int32_t UnSetPhotoAvailableCallback() override;
89     int32_t UnSetPhotoAssetAvailableCallback() override;
90     int32_t UnSetThumbnailCallback() override;
91     int32_t OnCaptureStarted(int32_t captureId);
92     int32_t OnCaptureStarted(int32_t captureId, uint32_t exposureTime);
93     int32_t OnCaptureEnded(int32_t captureId, int32_t frameCount);
94     int32_t OnCaptureError(int32_t captureId, int32_t errorType);
95     int32_t OnFrameShutter(int32_t captureId, uint64_t timestamp);
96     int32_t OnFrameShutterEnd(int32_t captureId, uint64_t timestamp);
97     int32_t OnCaptureReady(int32_t captureId, uint64_t timestamp);
98     int32_t OnOfflineDeliveryFinished(int32_t captureId);
99     int32_t OnPhotoAvailable(sptr<SurfaceBuffer> surfaceBuffer, const int64_t timestamp, bool isRaw);
100     int32_t OnPhotoAssetAvailable(
101         const int32_t captureId, const std::string &uri, int32_t cameraShotType, const std::string &burstKey);
102     int32_t OnThumbnailAvailable(sptr<SurfaceBuffer> surfaceBuffer, const int64_t timestamp);
103     void DumpStreamInfo(CameraInfoDumper& infoDumper) override;
104     void SetRotation(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting_, int32_t captureId);
105     void SetMode(int32_t modeName);
106     int32_t GetMode();
107     int32_t IsDeferredPhotoEnabled() override;
108     int32_t IsDeferredVideoEnabled() override;
109     int32_t SetMovingPhotoVideoCodecType(int32_t videoCodecType) override;
110     int32_t GetMovingPhotoVideoCodecType();
111     int32_t OperatePermissionCheck(uint32_t interfaceCode) override;
112     int32_t CallbackEnter([[maybe_unused]] uint32_t code) override;
113     int32_t CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result) override;
114     SafeMap<int32_t, int32_t> rotationMap_ = {};
115     bool IsBurstCapture(int32_t captureId) const;
116     bool IsBurstCover(int32_t captureId) const;
117     int32_t GetCurBurstSeq(int32_t captureId) const;
118     std::string GetBurstKey(int32_t captureId) const;
119     void SetBurstImages(int32_t captureId, std::string imageId);
120     void CheckResetBurstKey(int32_t captureId);
121     int32_t SetCameraPhotoRotation(bool isEnable) override;
122     int32_t CreateMediaLibraryPhotoAssetProxy(int32_t captureId);
123     int32_t UpdateMediaLibraryPhotoAssetProxy(sptr<CameraServerPhotoProxy> photoProxy);
124     std::shared_ptr<PhotoAssetIntf> GetPhotoAssetInstance(int32_t captureId);
125     bool GetAddPhotoProxyEnabled();
126     int32_t AcquireBufferToPrepareProxy(int32_t captureId);
127     int32_t EnableOfflinePhoto(bool isEnable) override;
128     bool IsHasEnableOfflinePhoto();
129     void SwitchToOffline();
130     bool IsHasSwitchToOffline();
131     void SetStreamOperator(wptr<HStreamOperator> hStreamOperator);
132     int32_t CreateMediaLibrary(const sptr<CameraPhotoProxy>& photoProxy, std::string& uri, int32_t& cameraShotType,
133         std::string& burstKey, int64_t timestamp) override;
134     int32_t CreateMediaLibrary(sptr<CameraServerPhotoProxy>& photoProxy, std::string& uri, int32_t& cameraShotType,
135         std::string& burstKey, int64_t timestamp);
136     int32_t CreateMediaLibrary(std::shared_ptr<PictureIntf> picture, sptr<CameraServerPhotoProxy> &photoProxy,
137         std::string &uri, int32_t &cameraShotType, std::string& burstKey, int64_t timestamp);
138     int32_t RequireMemorySize(int32_t memSize);
139 
140     bool isYuvCapture_ = false;
141     sptr<Surface> gainmapSurface_;
142     sptr<Surface> deepSurface_;
143     sptr<Surface> exifSurface_;
144     sptr<Surface> debugSurface_;
145     sptr<Surface> rawSurface_;
146     sptr<Surface> thumbnailSurface_;
147     sptr<IBufferConsumerListener> gainmapListener_ = nullptr;
148     sptr<IBufferConsumerListener> deepListener_ = nullptr;
149     sptr<IBufferConsumerListener> exifListener_ = nullptr;
150     sptr<IBufferConsumerListener> debugListener_ = nullptr;
151     sptr<PictureAssembler> pictureAssembler_;
152     std::map<int32_t, std::shared_ptr<PictureIntf>> captureIdPictureMap_;
153     std::shared_ptr<DeferredProcessing::TaskManager> photoTask_ = nullptr;
154     std::shared_ptr<DeferredProcessing::TaskManager> photoSubTask_ = nullptr;
155     std::shared_ptr<DeferredProcessing::TaskManager> thumbnailTask_ = nullptr;
156 
157     std::mutex g_photoImageMutex;
158     std::mutex g_assembleImageMutex;
159     std::map<int32_t, int32_t> captureIdAuxiliaryCountMap_;
160     std::map<int32_t, int32_t> captureIdCountMap_;
161     std::map<int32_t, uint32_t> captureIdHandleMap_;
162 
163     std::map<int32_t, sptr<CameraServerPhotoProxy>> photoProxyMap_;
164     std::map<int32_t, sptr<SurfaceBuffer>> captureIdGainmapMap_;
165     SafeMap<int32_t, sptr<SurfaceBuffer>> captureIdDepthMap_ = {};
166     std::map<int32_t, sptr<SurfaceBuffer>> captureIdExifMap_;
167     std::map<int32_t, sptr<SurfaceBuffer>> captureIdDebugMap_;
168 
169 private:
170     int32_t CheckBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings,
171                               const int32_t &preparedCaptureId);
172     void SetDataSpaceForCapture(StreamInfo_V1_1 &streamInfo);
173     int32_t PrepareBurst(int32_t captureId);
174     void ResetBurst();
175     void ResetBurstKey(int32_t captureId);
176     void EndBurstCapture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureMetadataSetting_);
177     void ProcessCaptureInfoPhoto(CaptureInfo& captureInfoPhoto,
178         const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings, int32_t captureId);
179     void SetCameraPhotoProxyInfo(sptr<CameraServerPhotoProxy> cameraPhotoProxy);
180     sptr<IStreamCaptureCallback> streamCaptureCallback_;
181     sptr<IStreamCapturePhotoCallback> photoAvaiableCallback_;
182     sptr<IStreamCapturePhotoAssetCallback> photoAssetAvaiableCallback_;
183     sptr<IStreamCaptureThumbnailCallback> thumbnailAvaiableCallback_;
184     void FillingPictureExtendStreamInfos(StreamInfo_V1_1 &streamInfo, int32_t format);
185     void FillingRawAndThumbnailStreamInfo(StreamInfo_V1_1 &streamInfo);
186     void UpdateJpegBasicInfo(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting,
187         int32_t& rotation);
188     void RegisterAuxiliaryConsumers();
189     void CreateCaptureSurface();
190     void CreateAuxiliarySurfaces();
191     void InitCaptureThread();
192     void SetRawCallback();
193     void GetLocation(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting);
194     std::mutex callbackLock_;
195     int32_t thumbnailSwitch_;
196     int32_t rawDeliverySwitch_;
197     int32_t movingPhotoSwitch_;
198     std::condition_variable testDelay_;
199     std::mutex testDelayMutex_;
200     sptr<BufferProducerSequenceable> thumbnailBufferQueue_;
201     sptr<BufferProducerSequenceable> rawBufferQueue_;
202     sptr<BufferProducerSequenceable> gainmapBufferQueue_;
203     sptr<BufferProducerSequenceable> deepBufferQueue_;
204     sptr<BufferProducerSequenceable> exifBufferQueue_;
205     sptr<BufferProducerSequenceable> debugBufferQueue_;
206     int32_t modeName_;
207     int32_t deferredPhotoSwitch_;
208     int32_t deferredVideoSwitch_;
209     bool enableCameraPhotoRotation_ = false;
210     std::atomic<bool> isCaptureReady_ = true;
211     std::string curBurstKey_ = BURST_UUID_UNSET;
212     bool isBursting_ = false;
213     std::map<int32_t, std::vector<std::string>> burstImagesMap_;
214     std::map<int32_t, std::string> burstkeyMap_;
215     std::map<int32_t, int32_t> burstNumMap_;
216     mutable std::mutex burstLock_;
217     int32_t burstNum_;
218     int32_t videoCodecType_ = 0;
219     std::mutex photoAssetLock_;
220     ConcurrentMap photoAssetProxy_;
221     bool mEnableOfflinePhoto_ = false;
222     bool mSwitchToOfflinePhoto_ = false;
223     int32_t mlastCaptureId = 0;
224     wptr<HStreamOperator> hStreamOperator_;
225     std::map<int32_t, std::unique_ptr<std::mutex>> mutexMap;
226     std::mutex photoCallbackLock_;
227     std::mutex thumbnailCallbackLock_;
228     sptr<IBufferConsumerListener> photoListener_ = nullptr;
229     sptr<IBufferConsumerListener> photoAssetListener_ = nullptr;
230     sptr<IBufferConsumerListener> thumbnailListener_ = nullptr;
231     double latitude_ = 0.0;
232     double longitude_ = 0.0;
233     double altitude_ = 0.0;
234 };
235 } // namespace CameraStandard
236 } // namespace OHOS
237 #endif // OHOS_CAMERA_H_STREAM_CAPTURE_H
238