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_PHOTO_OUTPUT_H
17 #define OHOS_CAMERA_PHOTO_OUTPUT_H
18 
19 #include <atomic>
20 #include <cstdint>
21 #include <iostream>
22 #include <mutex>
23 
24 #include "camera_metadata_info.h"
25 #include "capture_output.h"
26 #include "hstream_capture_callback_stub.h"
27 #include "istream_capture.h"
28 #include "camera_photo_proxy.h"
29 
30 namespace OHOS::Media {
31     class Picture;
32 }
33 namespace OHOS {
34 namespace CameraStandard {
35 namespace DeferredProcessing {
36     class TaskManager;
37 }
38 
39 class PhotoStateCallback {
40 public:
41     PhotoStateCallback() = default;
42     virtual ~PhotoStateCallback() = default;
43 
44     /**
45      * @brief Called when camera capture started.
46      *
47      * @param captureID Obtain the constant capture id for the photo capture callback.
48      */
49     virtual void OnCaptureStarted(const int32_t captureID) const = 0;
50 
51     /**
52      * @brief Called when camera capture started.
53      *
54      * @param captureID Obtain the constant capture id for the photo capture callback.
55      */
56     virtual void OnCaptureStarted(const int32_t captureID, uint32_t exposureTime) const = 0;
57 
58     /**
59      * @brief Called when camera capture ended.
60      *
61      * @param captureID Obtain the constant capture id for the photo capture callback.
62      * @param frameCount Obtain the constant number of frames for the photo capture callback.
63      */
64     virtual void OnCaptureEnded(const int32_t captureID, const int32_t frameCount) const = 0;
65 
66     /**
67      * @brief Called when frame Shutter.
68      *
69      * @param captureId Obtain the constant capture id for the photo capture callback.
70      * @param timestamp Represents timestamp information for the photo capture callback
71      */
72     virtual void OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const = 0;
73 
74     /**
75      * @brief Called when frame Shutter end.
76      *
77      * @param captureId Obtain the constant capture id for the photo capture callback.
78      * @param timestamp Represents timestamp information for the photo capture callback
79      */
80     virtual void OnFrameShutterEnd(const int32_t captureId, const uint64_t timestamp) const = 0;
81 
82     /**
83      * @brief Called when capture ready end.
84      *
85      * @param captureId Obtain the constant capture id for the photo capture callback.
86      * @param timestamp Represents timestamp information for the photo capture callback
87      */
88     virtual void OnCaptureReady(const int32_t captureId, const uint64_t timestamp) const = 0;
89 
90     /**
91      * @brief Called when EstimatedCaptureDuration.
92      *
93      * @param duration Obtain the duration for the photo capture callback.
94      */
95     virtual void OnEstimatedCaptureDuration(const int32_t duration) const = 0;
96 
97     /**
98      * @brief Called when error occured during camera capture.
99      *
100      * @param captureId Indicates the pointer in which captureId will be requested.
101      * @param errorCode Indicates a {@link ErrorCode} which will give information for photo capture callback error
102      */
103     virtual void OnCaptureError(const int32_t captureId, const int32_t errorCode) const = 0;
104 };
105 
106 class [[deprecated]] PhotoCallback {
107 public:
108     PhotoCallback() = default;
109     virtual ~PhotoCallback() = default;
110 
111     /**
112      * @brief Called when camera capture started.
113      *
114      * @param captureID Obtain the constant capture id for the photo capture callback.
115      */
116     virtual void OnCaptureStarted(const int32_t captureID) const = 0;
117 
118     /**
119      * @brief Called when camera capture ended.
120      *
121      * @param captureID Obtain the constant capture id for the photo capture callback.
122      * @param frameCount Obtain the constant number of frames for the photo capture callback.
123      */
124     virtual void OnCaptureEnded(const int32_t captureID, const int32_t frameCount) const = 0;
125 
126     /**
127      * @brief Called when camera capture ended.
128      *
129      * @param captureId Obtain the constant capture id for the photo capture callback.
130      * @param timestamp Represents timestamp information for the photo capture callback
131      */
132     virtual void OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const = 0;
133 
134     /**
135      * @brief Called when error occured during camera capture.
136      *
137      * @param captureId Indicates the pointer in which captureId will be requested.
138      * @param errorCode Indicates a {@link ErrorCode} which will give information for photo capture callback error
139      */
140     virtual void OnCaptureError(const int32_t captureId, const int32_t errorCode) const = 0;
141 };
142 
143 typedef struct Location {
144     /**
145      * Latitude.
146      */
147     double latitude = -1;
148     /**
149      * Longitude.
150      */
151     double longitude = -1;
152     /**
153      * Altitude.
154      */
155     double altitude = -1;
156 } Location;
157 
158 class PhotoCaptureSetting {
159 public:
160     enum QualityLevel {
161         QUALITY_LEVEL_HIGH = 0,
162         QUALITY_LEVEL_MEDIUM,
163         QUALITY_LEVEL_LOW,
164         HIGH_QUALITY [[deprecated]] = 0,
165         NORMAL_QUALITY [[deprecated]],
166         LOW_QUALITY [[deprecated]]
167     };
168     enum RotationConfig { Rotation_0 = 0, Rotation_90 = 90, Rotation_180 = 180, Rotation_270 = 270 };
169     PhotoCaptureSetting();
170     virtual ~PhotoCaptureSetting() = default;
171 
172     /**
173      * @brief Get the quality level for the photo capture settings.
174      *
175      * @return Returns the quality level.
176      */
177     QualityLevel GetQuality();
178 
179     /**
180      * @brief Set the quality level for the photo capture settings.
181      *
182      * @param qualityLevel to be set.
183      */
184     void SetQuality(QualityLevel qualityLevel);
185 
186     /**
187      * @brief Get rotation information for the photo capture settings.
188      *
189      * @return Returns the RotationConfig.
190      */
191     RotationConfig GetRotation();
192 
193     /**
194      * @brief Set the Rotation for the photo capture settings.
195      *
196      * @param rotationvalue to be set.
197      */
198     void SetRotation(RotationConfig rotationvalue);
199 
200     /**
201      * @brief Set the GPS Location for the photo capture settings.
202      *
203      * @param latitude value to be set.
204      * @param longitude value to be set.
205      */
206     void SetGpsLocation(double latitude, double longitude);
207 
208     /**
209      * @brief Set the GPS Location for the photo capture settings.
210      *
211      * @param location value to be set.
212      */
213     void SetLocation(std::shared_ptr<Location>& location);
214 
215     /**
216      * @brief Get the GPS Location for the photo capture settings.
217      *
218      * @param location value to be set.
219      */
220     void GetLocation(std::shared_ptr<Location>& location);
221 
222     /**
223      * @brief Set the mirror option for the photo capture.
224      *
225      * @param boolean true/false to set/unset mirror respectively.
226      */
227     void SetMirror(bool enable);
228 
229     /**
230      * @brief Get mirror information for the photo capture settings.
231      *
232      * @return Returns the mirror information.
233      */
234     bool GetMirror();
235 
236     /**
237      * @brief Set burst capture state.
238      */
239     void SetBurstCaptureState(uint8_t burstState);
240 
241     /**
242      * @brief Get the photo capture settings metadata information.
243      *
244      * @return Returns the pointer where CameraMetadata information is present.
245      */
246     std::shared_ptr<OHOS::Camera::CameraMetadata> GetCaptureMetadataSetting();
247 
248 private:
249     std::shared_ptr<OHOS::Camera::CameraMetadata> captureMetadataSetting_;
250     std::shared_ptr<Location> location_;
251     std::mutex locationMutex_;
252 };
253 
254 constexpr uint8_t CAPTURE_PHOTO = 1 << 0;
255 constexpr uint8_t CAPTURE_DEFERRED_PHOTO = 1 << 1;
256 constexpr uint8_t CAPTURE_PHOTO_ASSET = 1 << 2;
257 constexpr int32_t CAPTURE_ROTATION_BASE = 360;
258 constexpr int32_t ROTATION_45_DEGREES = 45;
259 constexpr int32_t ROTATION_90_DEGREES = 90;
260 class PhotoOutput : public CaptureOutput {
261 public:
262     explicit PhotoOutput(sptr<IBufferProducer> bufferProducer);
263     virtual ~PhotoOutput();
264 
265     /**
266      * @brief Set the photo callback.
267      *
268      * @param callback Requested for the pointer where photo callback is present.
269      */
270     void SetCallback(std::shared_ptr<PhotoStateCallback> callback);
271 
272     /**
273      * @brief Set the thumbnail callback.
274      *
275      * @param listener set IBufferConsumerListener when on interface is called.
276      */
277     void SetThumbnailListener(sptr<IBufferConsumerListener>& listener);
278 
279     /**
280      * @brief Set the Thumbnail profile.
281      *
282      * @param isEnabled quickThumbnail is enabled.
283      */
284     int32_t SetThumbnail(bool isEnabled);
285 
286     /**
287      * @brief To enable the raw imgage delivery.
288      *
289      * @return Returns the result of the raw imgage delivery enable.
290      */
291     int32_t EnableRawDelivery(bool enabled);
292 
293     /**
294      * @brief Get the photo rotation.
295      *
296      * @return result of the photo rotation angle.
297      */
298     int32_t GetPhotoRotation(int32_t imageRotation);
299 
300     /**
301      * @brief Set the Thumbnail profile.
302      *
303      * @param isEnabled quickThumbnail is enabled.
304      */
305     int32_t SetRawPhotoInfo(sptr<Surface>& surface);
306 
307     /**
308      * @brief Set the photo callback.
309      *
310      * @param callback Requested for the pointer where photo callback is present.
311      */
312     [[deprecated]] void SetCallback(std::shared_ptr<PhotoCallback> callback);
313 
314     /**
315      * @brief Photo capture request using photocapturesettings.
316      *
317      * @param photoCaptureSettings Requested for the photoCaptureSettings object which has metadata
318      * information such as: Rotation, Location, Mirror & Quality.
319      */
320     int32_t Capture(std::shared_ptr<PhotoCaptureSetting> photoCaptureSettings);
321 
322     /**
323      * @brief Initiate for the photo capture.
324      */
325     int32_t Capture();
326 
327     /**
328      * @brief cancelling the photo capture. Applicable only for burst/ continuous capture.
329      */
330     int32_t CancelCapture();
331 
332     /**
333      * @brief cancelling the photo capture. Applicable only for burst/ continuous capture.
334      */
335     int32_t ConfirmCapture();
336 
337     int32_t CreateStream() override;
338 
339     /**
340      * @brief Releases the instance of PhotoOutput.
341      */
342     int32_t Release() override;
343 
344     /**
345      * @brief Get the application callback information.
346      *
347      * @return Returns the pointer to PhotoStateCallback.
348      */
349     std::shared_ptr<PhotoStateCallback> GetApplicationCallback();
350 
351     /**
352      * @brief To check the photo capture is mirrored or not.
353      *
354      * @return Returns true/false if the photo capture is mirrored/not-mirrored respectively.
355      */
356     bool IsMirrorSupported();
357 
358     /**
359      * @brief To enable the photo capture mirror.
360      *
361      * @return Returns the result of the photo capture mirror enable.
362      */
363     int32_t EnableMirror(bool isEnable);
364 
365     /**
366      * @brief To check the quick thumbnail is supported or not.
367      *
368      * @return Returns true/false if the quick thumbnail is supported/not-supported respectively.
369      */
370     int32_t IsQuickThumbnailSupported();
371 
372     /**
373      * @brief To check the raw image devlivery is supported or not.
374      *
375      * @return Returns true/false if the raw image devlivery is supported/not-supported respectively.
376      */
377     int32_t IsRawDeliverySupported();
378 
379     /**
380      * @brief Set the deferredImageDelivery type.
381      *
382      */
383     int32_t DeferImageDeliveryFor(DeferredDeliveryImageType type);
384 
385     /**
386      * @brief To check the deferredImageDelivery capability is supported or not.
387      *
388      * @return Returns true/false if the deferredImageDelivery is supported/not-supported respectively.
389      */
390     int32_t IsDeferredImageDeliverySupported(DeferredDeliveryImageType type);
391 
392     /**
393      * @brief Set the callbackFlag when on photoAssetAvailable.
394      */
395     void SetCallbackFlag(uint8_t callbackFlag);
396 
397     /**
398     * @brief Set the flag when on native surface.
399     */
400     void SetNativeSurface(bool SetNativeSurface);
401 
402     /**
403      * @brief To check the deferredImageDelivery capability is enable or not.
404      *
405      * @return Returns true/false if the deferredImageDelivery is enable/not-enable respectively.
406      */
407     int32_t IsDeferredImageDeliveryEnabled(DeferredDeliveryImageType type);
408 
409     void ProcessSnapshotDurationUpdates(int32_t snapshotDuration);
410 
411     /**
412      * @brief To check the auto high quality photo is supported or not.
413      *
414      * @return Returns true/false if the auto high quality photo is supported/not-supported respectively.
415      */
416     int32_t IsAutoHighQualityPhotoSupported(int32_t& isAutoHighQualityPhotoSupported);
417 
418     /**
419      * @brief To enable the auto high quality photo.
420      *
421      * @return Returns the result of the auto high quality photo enable.
422      */
423     int32_t EnableAutoHighQualityPhoto(bool enabled);
424 
425     /**
426      * @brief To get status by callbackFlags.
427      *
428      * @return Returns the result to check enable deferred.
429      */
430     bool IsEnableDeferred();
431 
432     /**
433      * @brief Check whether the current mode supports auto cloud image enhance.
434      *
435      * @return Return the supported result.
436      */
437     int32_t IsAutoCloudImageEnhancementSupported(bool& isAutoCloudImageEnhancementSupported);
438 
439     /**
440      * @brief To enable the auto cloud image enhuance.
441      *
442      * @return Returns the result of the auto cloud image enhuance enable.
443      */
444     int32_t EnableAutoCloudImageEnhancement(bool enabled);
445 
446     /**
447      * @brief Get default photo capture setting.
448      *
449      * @return default photo capture setting.
450      */
451     std::shared_ptr<PhotoCaptureSetting> GetDefaultCaptureSetting();
452     int32_t SetMovingPhotoVideoCodecType(int32_t videoCodecType);
453 
454     int32_t GetSupportedAuxiliaryPictureTypes(std::vector<std::int32_t> &types);
455 
456     /**
457      * @brief Check the depth data delivery capability is supported or not.
458      */
459     bool IsDepthDataDeliverySupported();
460 
461     /**
462      * @brief Enable the depth data delivery.
463      */
464     int32_t EnableDepthDataDelivery(bool enabled);
465 
466     sptr<Surface> thumbnailSurface_;
467 
468     sptr<Surface> rawPhotoSurface_;
469 
470     sptr<Surface> deferredSurface_;
471 
472     sptr<Surface> gainmapSurface_;
473     sptr<Surface> deepSurface_;
474     sptr<Surface> exifSurface_;
475     sptr<Surface> debugSurface_;
476     sptr<SurfaceBuffer> gainmapSurfaceBuffer_;
477     sptr<SurfaceBuffer> deepSurfaceBuffer_;
478     sptr<SurfaceBuffer> exifSurfaceBuffer_;
479     sptr<SurfaceBuffer> debugSurfaceBuffer_;
480     bool IsYuvOrHeifPhoto();
481     void CreateMultiChannel();
482 
483     void SetAuxiliaryPhotoHandle(uint32_t handle);
484     uint32_t GetAuxiliaryPhotoHandle();
485 
486     void AcquireBufferToPrepareProxy(int32_t captureId);
487 
488     uint32_t watchDogHandle_ = 0;
489     std::mutex watchDogHandleMutex_;
490     std::map<int32_t, int32_t> captureIdAuxiliaryCountMap_;
491     std::map<int32_t, int32_t> captureIdCountMap_;
492     std::map<int32_t, uint32_t> captureIdHandleMap_;
493     std::map<int32_t, std::unique_ptr<Media::Picture>> captureIdPictureMap_;
494 
495     std::map<int32_t, sptr<CameraPhotoProxy>> photoProxyMap_;
496     std::map<int32_t, sptr<SurfaceBuffer>> captureIdGainmapMap_;
497     std::map<int32_t, sptr<SurfaceBuffer>> captureIdDepthMap_;
498     std::map<int32_t, sptr<SurfaceBuffer>> captureIdExifMap_;
499     std::map<int32_t, sptr<SurfaceBuffer>> captureIdDebugMap_;
500     std::atomic<bool> isRawImageDelivery_ = false;
501     std::shared_ptr<DeferredProcessing::TaskManager> taskManager_;
502 private:
503     std::mutex callbackMutex_;
504     uint8_t callbackFlag_ = CAPTURE_DEFERRED_PHOTO;
505     bool isNativeSurface_ = false;
506     DeferredDeliveryImageType deferredType_ = DeferredDeliveryImageType::DELIVERY_NONE;
507     std::shared_ptr<PhotoStateCallback> appCallback_;
508     sptr<IStreamCaptureCallback> cameraSvcCallback_;
509     std::shared_ptr<PhotoCaptureSetting> defaultCaptureSetting_;
510     void CameraServerDied(pid_t pid) override;
511 };
512 
513 class HStreamCaptureCallbackImpl : public HStreamCaptureCallbackStub {
514 public:
HStreamCaptureCallbackImpl(PhotoOutput * photoOutput)515     explicit HStreamCaptureCallbackImpl(PhotoOutput* photoOutput) : innerPhotoOutput_(photoOutput) {}
516 
517     ~HStreamCaptureCallbackImpl() = default;
518 
519     /**
520      * @brief Called when camera capture started.
521      *
522      * @param captureID Obtain the constant capture id for the photo capture callback.
523      */
524     int32_t OnCaptureStarted(const int32_t captureId) override;
525 
526     /**
527      * @brief Called when camera capture started.
528      *
529      * @param captureID Obtain the constant capture id for the photo capture callback.
530      */
531     int32_t OnCaptureStarted(const int32_t captureId, uint32_t exposureTime) override;
532     /**
533      * @brief Called when camera capture ended.
534      *
535      * @param captureID Obtain the constant capture id for the photo capture callback.
536      * @param frameCount Obtain the constant number of frames for the photo capture callback.
537      */
538     int32_t OnCaptureEnded(const int32_t captureId, const int32_t frameCount) override;
539 
540     /**
541      * @brief Called when error occured during camera capture.
542      *
543      * @param captureId Indicates the pointer in which captureId will be requested.
544      * @param errorCode Indicates a {@link ErrorCode} which will give information for photo capture callback error
545      */
546     int32_t OnCaptureError(const int32_t captureId, const int32_t errorCode) override;
547 
548     /**
549      * @brief Called when camera capture ended.
550      *
551      * @param captureId Obtain the constant capture id for the photo capture callback.
552      * @param timestamp Represents timestamp information for the photo capture callback
553      */
554     int32_t OnFrameShutter(const int32_t captureId, const uint64_t timestamp) override;
555 
556     /**
557      * @brief Called when camera capture ended.
558      *
559      * @param captureId Obtain the constant capture id for the photo capture callback.
560      * @param timestamp Represents timestamp information for the photo capture callback
561      */
562     int32_t OnFrameShutterEnd(const int32_t captureId, const uint64_t timestamp) override;
563 
564     /**
565      * @brief Called when camera capture ended.
566      *
567      * @param captureId Obtain the constant capture id for the photo capture callback.
568      * @param timestamp Represents timestamp information for the photo capture callback
569      */
570     int32_t OnCaptureReady(const int32_t captureId, const uint64_t timestamp) override;
571 
GetPhotoOutput()572     inline sptr<PhotoOutput> GetPhotoOutput()
573     {
574         if (innerPhotoOutput_ == nullptr) {
575             return nullptr;
576         }
577         return innerPhotoOutput_.promote();
578     }
579 
580 private:
581     wptr<PhotoOutput> innerPhotoOutput_ = nullptr;
582 };
583 } // namespace CameraStandard
584 } // namespace OHOS
585 #endif // OHOS_CAMERA_PHOTO_OUTPUT_H
586