1 /*
2  * Copyright (c) 2024-2024 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_PHOTO_LISTENER_IMPL_H
17 #define OHOS_PHOTO_LISTENER_IMPL_H
18 
19 #include "kits/native/include/camera/photo_output.h"
20 #include "surface.h"
21 #include "native_image.h"
22 #include "inner_api/native/camera/include/utils/camera_buffer_handle_utils.h"
23 
24 namespace OHOS {
25 namespace CameraStandard {
26 
27 class PhotoBufferProcessor : public Media::IBufferProcessor {
28 public:
PhotoBufferProcessor(sptr<Surface> photoSurface)29     explicit PhotoBufferProcessor(sptr<Surface> photoSurface) : photoSurface_(photoSurface) {}
~PhotoBufferProcessor()30     ~PhotoBufferProcessor()
31     {
32         photoSurface_ = nullptr;
33     }
BufferRelease(sptr<SurfaceBuffer> & buffer)34     void BufferRelease(sptr<SurfaceBuffer>& buffer) override
35     {
36         if (photoSurface_ != nullptr) {
37             photoSurface_->ReleaseBuffer(buffer, -1);
38         }
39     }
40 
41 private:
42     sptr<Surface> photoSurface_ = nullptr;
43 };
44 
45 class PhotoListener : public IBufferConsumerListener {
46 public:
47     explicit PhotoListener(Camera_PhotoOutput* photoOutput, sptr<Surface> surface);
48     ~PhotoListener();
49     void OnBufferAvailable() override;
50     void SetCallbackFlag(uint8_t callbackFlag);
51     void SetPhotoAvailableCallback(OH_PhotoOutput_PhotoAvailable callback);
52     void SetPhotoAssetAvailableCallback(OH_PhotoOutput_PhotoAssetAvailable callback);
53     void UnregisterPhotoAvailableCallback(OH_PhotoOutput_PhotoAvailable callback);
54     void UnregisterPhotoAssetAvailableCallback(OH_PhotoOutput_PhotoAssetAvailable callback);
55 
56 private:
57     void ExecutePhoto(sptr<SurfaceBuffer> surfaceBuffer, int64_t timestamp);
58     void ExecutePhotoAsset(sptr<SurfaceBuffer> surfaceBuffer, CameraBufferExtraData extraData,
59         bool isHighQuality, int64_t timestamp);
60     void DeepCopyBuffer(sptr<SurfaceBuffer> newSurfaceBuffer, sptr<SurfaceBuffer> surfaceBuffer);
61     void CreateMediaLibrary(sptr<SurfaceBuffer> surfaceBuffer, BufferHandle *bufferHandle,
62         CameraBufferExtraData extraData, bool isHighQuality, std::string &uri,
63         int32_t &cameraShotType, std::string &burstKey, int64_t timestamp);
64     CameraBufferExtraData GetCameraBufferExtraData(const sptr<SurfaceBuffer> &surfaceBuffer);
65 
66     Camera_PhotoOutput* photoOutput_;
67     sptr<Surface> photoSurface_;
68     OH_PhotoOutput_PhotoAvailable photoCallback_ = nullptr;
69     OH_PhotoOutput_PhotoAssetAvailable photoAssetCallback_ = nullptr;
70     uint8_t callbackFlag_ = 0;
71     std::shared_ptr<PhotoBufferProcessor> bufferProcessor_;
72 };
73 
74 class RawPhotoListener : public IBufferConsumerListener {
75 public:
76     explicit RawPhotoListener(Camera_PhotoOutput* photoOutput, const sptr<Surface> rawPhotoSurface);
77     ~RawPhotoListener();
78     void OnBufferAvailable() override;
79     void SetCallback(OH_PhotoOutput_PhotoAvailable callback);
80     void UnregisterCallback(OH_PhotoOutput_PhotoAvailable callback);
81 
82 private:
83     void ExecuteRawPhoto(sptr<SurfaceBuffer> surfaceBuffer, int64_t timestamp);
84 
85     Camera_PhotoOutput* photoOutput_;
86     sptr<Surface> rawPhotoSurface_;
87     OH_PhotoOutput_PhotoAvailable callback_ = nullptr;
88     std::shared_ptr<PhotoBufferProcessor> bufferProcessor_;
89 };
90 
91 } // namespace CameraStandard
92 } // namespace OHOS
93 
94 #endif // OHOS_PHOTO_LISTENER_IMPL_H