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