1 /*
2  * Copyright (c) 2023-2023 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_OUTPUT_IMPL_H
17 #define OHOS_PHOTO_OUTPUT_IMPL_H
18 
19 #include <mutex>
20 
21 #include "kits/native/include/camera/camera.h"
22 #include "kits/native/include/camera/photo_output.h"
23 #include "output/photo_output.h"
24 #include "camera_log.h"
25 #include "camera_util.h"
26 #include "photo_listener_impl.h"
27 #include "photo_native_impl.h"
28 
29 class InnerPhotoOutputCallback : public OHOS::CameraStandard::PhotoStateCallback {
30 public:
InnerPhotoOutputCallback(Camera_PhotoOutput * photoOutput)31     explicit InnerPhotoOutputCallback(Camera_PhotoOutput* photoOutput) : photoOutput_(photoOutput)
32     {
33         callback_.onFrameStart = nullptr;
34         callback_.onFrameShutter = nullptr;
35         callback_.onFrameEnd = nullptr;
36         callback_.onError = nullptr;
37     }
38     ~InnerPhotoOutputCallback() = default;
39 
SaveCallback(PhotoOutput_Callbacks * callback)40     void SaveCallback(PhotoOutput_Callbacks* callback)
41     {
42         callback_ = *callback;
43     }
44 
SaveCaptureStartWithInfoCallback(OH_PhotoOutput_CaptureStartWithInfo callback)45     void SaveCaptureStartWithInfoCallback(OH_PhotoOutput_CaptureStartWithInfo callback)
46     {
47         captureStartWithInfoCallback_ = callback;
48     }
49 
SaveCaptureEndCallback(OH_PhotoOutput_CaptureEnd callback)50     void SaveCaptureEndCallback(OH_PhotoOutput_CaptureEnd callback)
51     {
52         captureEndCallback_ = callback;
53     }
54 
SaveFrameShutterEndCallback(OH_PhotoOutput_OnFrameShutterEnd callback)55     void SaveFrameShutterEndCallback(OH_PhotoOutput_OnFrameShutterEnd callback)
56     {
57         frameShutterEndCallback_ = callback;
58     }
59 
SaveCaptureReadyCallback(OH_PhotoOutput_CaptureReady callback)60     void SaveCaptureReadyCallback(OH_PhotoOutput_CaptureReady callback)
61     {
62         captureReadyCallback_ = callback;
63     }
64 
SaveEstimatedCaptureDurationCallback(OH_PhotoOutput_EstimatedCaptureDuration callback)65     void SaveEstimatedCaptureDurationCallback(OH_PhotoOutput_EstimatedCaptureDuration callback)
66     {
67         estimatedCaptureDurationCallback_ = callback;
68     }
69 
RemoveCallback(PhotoOutput_Callbacks * callback)70     void RemoveCallback(PhotoOutput_Callbacks* callback)
71     {
72         if (callback->onFrameStart) {
73             callback_.onFrameStart = nullptr;
74         }
75         if (callback->onFrameShutter) {
76             callback_.onFrameShutter = nullptr;
77         }
78         if (callback->onFrameEnd) {
79             callback_.onFrameEnd = nullptr;
80         }
81         if (callback->onError) {
82             callback_.onError = nullptr;
83         }
84     }
85 
RemoveCaptureStartWithInfoCallback(OH_PhotoOutput_CaptureStartWithInfo callback)86     void RemoveCaptureStartWithInfoCallback(OH_PhotoOutput_CaptureStartWithInfo callback)
87     {
88         if (callback != nullptr) {
89             captureStartWithInfoCallback_ = nullptr;
90         }
91     }
92 
RemoveCaptureEndCallback(OH_PhotoOutput_CaptureEnd callback)93     void RemoveCaptureEndCallback(OH_PhotoOutput_CaptureEnd callback)
94     {
95         if (callback != nullptr) {
96             captureEndCallback_ = nullptr;
97         }
98     }
99 
RemoveFrameShutterEndCallback(OH_PhotoOutput_OnFrameShutterEnd callback)100     void RemoveFrameShutterEndCallback(OH_PhotoOutput_OnFrameShutterEnd callback)
101     {
102         if (callback != nullptr) {
103             frameShutterEndCallback_ = nullptr;
104         }
105     }
106 
RemoveCaptureReadyCallback(OH_PhotoOutput_CaptureReady callback)107     void RemoveCaptureReadyCallback(OH_PhotoOutput_CaptureReady callback)
108     {
109         if (callback != nullptr) {
110             captureReadyCallback_ = nullptr;
111         }
112     }
113 
RemoveEstimatedCaptureDurationCallback(OH_PhotoOutput_EstimatedCaptureDuration callback)114     void RemoveEstimatedCaptureDurationCallback(OH_PhotoOutput_EstimatedCaptureDuration callback)
115     {
116         if (callback != nullptr) {
117             estimatedCaptureDurationCallback_ = nullptr;
118         }
119     }
120 
OnCaptureStarted(const int32_t captureID)121     void OnCaptureStarted(const int32_t captureID) const override
122     {
123         MEDIA_DEBUG_LOG("OnCaptureStarted is called!, captureID: %{public}d", captureID);
124         Camera_CaptureStartInfo info;
125         info.captureId = captureID;
126         if (photoOutput_ != nullptr && captureStartWithInfoCallback_ != nullptr) {
127             captureStartWithInfoCallback_(photoOutput_, &info);
128         }
129     }
130 
131 // need fix
OnCaptureStarted(const int32_t captureID,uint32_t exposureTime)132     void OnCaptureStarted(const int32_t captureID, uint32_t exposureTime) const override
133     {
134         MEDIA_DEBUG_LOG("OnCaptureStarted is called!, captureID: %{public}d", captureID);
135         if (photoOutput_ != nullptr && callback_.onFrameStart != nullptr) {
136             callback_.onFrameStart(photoOutput_);
137         }
138     }
139 
140 // need fix
OnFrameShutter(const int32_t captureId,const uint64_t timestamp)141     void OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const override
142     {
143         MEDIA_DEBUG_LOG("onFrameShutter is called!, captureId: %{public}d", captureId);
144         Camera_FrameShutterInfo info;
145         info.captureId = captureId;
146         info.timestamp = timestamp;
147         if (photoOutput_ != nullptr && callback_.onFrameShutter != nullptr) {
148             callback_.onFrameShutter(photoOutput_, &info);
149         }
150     }
151 
OnFrameShutterEnd(const int32_t captureId,const uint64_t timestamp)152     void OnFrameShutterEnd(const int32_t captureId, const uint64_t timestamp) const override
153     {
154         MEDIA_DEBUG_LOG("OnFrameShutterEnd is called!, captureId: %{public}d", captureId);
155         Camera_FrameShutterInfo info;
156         info.captureId = captureId;
157         info.timestamp = timestamp;
158         if (photoOutput_ != nullptr && frameShutterEndCallback_ != nullptr) {
159             frameShutterEndCallback_(photoOutput_, &info);
160         }
161     }
162 
OnCaptureReady(const int32_t captureId,const uint64_t timestamp)163     void OnCaptureReady(const int32_t captureId, const uint64_t timestamp) const override
164     {
165         MEDIA_DEBUG_LOG("OnCaptureReady is called!, captureId: %{public}d", captureId);
166         if (photoOutput_ != nullptr && captureReadyCallback_ != nullptr) {
167             captureReadyCallback_(photoOutput_);
168         }
169     }
170 
OnEstimatedCaptureDuration(const int32_t duration)171     void OnEstimatedCaptureDuration(const int32_t duration) const override
172     {
173         MEDIA_DEBUG_LOG("OnEstimatedCaptureDuration is called!, duration: %{public}d", duration);
174         if (photoOutput_ != nullptr && estimatedCaptureDurationCallback_ != nullptr) {
175             estimatedCaptureDurationCallback_(photoOutput_, duration);
176         }
177     }
178 
OnCaptureEnded(const int32_t captureID,const int32_t frameCount)179     void OnCaptureEnded(const int32_t captureID, const int32_t frameCount) const override
180     {
181         MEDIA_DEBUG_LOG("OnCaptureEnded is called! captureID: %{public}d", captureID);
182         MEDIA_DEBUG_LOG("OnCaptureEnded is called! framecount: %{public}d", frameCount);
183         if (photoOutput_ != nullptr && callback_.onFrameEnd != nullptr) {
184             callback_.onFrameEnd(photoOutput_, frameCount);
185         }
186         if (photoOutput_ != nullptr && captureEndCallback_ != nullptr) {
187             captureEndCallback_(photoOutput_, frameCount);
188         }
189     }
190 
OnCaptureError(const int32_t captureId,const int32_t errorCode)191     void OnCaptureError(const int32_t captureId, const int32_t errorCode) const override
192     {
193         MEDIA_DEBUG_LOG("OnCaptureError is called!, errorCode: %{public}d", errorCode);
194         if (photoOutput_ != nullptr && callback_.onError != nullptr) {
195             callback_.onError(photoOutput_, OHOS::CameraStandard::FrameworkToNdkCameraError(errorCode));
196         }
197     }
198 
199 private:
200     Camera_PhotoOutput* photoOutput_;
201     PhotoOutput_Callbacks callback_;
202     OH_PhotoOutput_CaptureStartWithInfo captureStartWithInfoCallback_ = nullptr;
203     OH_PhotoOutput_CaptureEnd captureEndCallback_ = nullptr;
204     OH_PhotoOutput_OnFrameShutterEnd frameShutterEndCallback_ = nullptr;
205     OH_PhotoOutput_CaptureReady captureReadyCallback_ = nullptr;
206     OH_PhotoOutput_EstimatedCaptureDuration estimatedCaptureDurationCallback_ = nullptr;
207 };
208 
209 struct Camera_PhotoOutput {
210 public:
211     explicit Camera_PhotoOutput(OHOS::sptr<OHOS::CameraStandard::PhotoOutput> &innerPhotoOutput);
212     ~Camera_PhotoOutput();
213 
214     Camera_ErrorCode RegisterCallback(PhotoOutput_Callbacks* callback);
215 
216     Camera_ErrorCode UnregisterCallback(PhotoOutput_Callbacks* callback);
217 
218     Camera_ErrorCode RegisterCaptureStartWithInfoCallback(OH_PhotoOutput_CaptureStartWithInfo callback);
219 
220     Camera_ErrorCode UnregisterCaptureStartWithInfoCallback(OH_PhotoOutput_CaptureStartWithInfo callback);
221 
222     Camera_ErrorCode RegisterCaptureEndCallback(OH_PhotoOutput_CaptureEnd callback);
223 
224     Camera_ErrorCode UnregisterCaptureEndCallback(OH_PhotoOutput_CaptureEnd callback);
225 
226     Camera_ErrorCode RegisterFrameShutterEndCallback(OH_PhotoOutput_OnFrameShutterEnd callback);
227 
228     Camera_ErrorCode UnregisterFrameShutterEndCallback(OH_PhotoOutput_OnFrameShutterEnd callback);
229 
230     Camera_ErrorCode RegisterCaptureReadyCallback(OH_PhotoOutput_CaptureReady callback);
231 
232     Camera_ErrorCode UnregisterCaptureReadyCallback(OH_PhotoOutput_CaptureReady callback);
233 
234     Camera_ErrorCode RegisterEstimatedCaptureDurationCallback(OH_PhotoOutput_EstimatedCaptureDuration callback);
235 
236     Camera_ErrorCode UnregisterEstimatedCaptureDurationCallback(OH_PhotoOutput_EstimatedCaptureDuration callback);
237 
238     Camera_ErrorCode RegisterPhotoAvailableCallback(OH_PhotoOutput_PhotoAvailable callback);
239 
240     Camera_ErrorCode UnregisterPhotoAvailableCallback(OH_PhotoOutput_PhotoAvailable callback);
241 
242     Camera_ErrorCode RegisterPhotoAssetAvailableCallback(OH_PhotoOutput_PhotoAssetAvailable callback);
243 
244     Camera_ErrorCode UnregisterPhotoAssetAvailableCallback(OH_PhotoOutput_PhotoAssetAvailable callback);
245 
246     Camera_ErrorCode Capture();
247 
248     Camera_ErrorCode Capture_WithCaptureSetting(Camera_PhotoCaptureSetting setting);
249 
250     Camera_ErrorCode Release();
251 
252     Camera_ErrorCode IsMirrorSupported(bool* isSupported);
253 
254     Camera_ErrorCode EnableMirror(bool enableMirror);
255 
256     OHOS::sptr<OHOS::CameraStandard::PhotoOutput> GetInnerPhotoOutput();
257 
258     void SetPhotoSurface(OHOS::sptr<OHOS::Surface> &photoSurface);
259 
260     OH_PhotoNative* CreateCameraPhotoNative(std::shared_ptr<OHOS::Media::NativeImage> &image, bool isMain);
261 
262     Camera_ErrorCode IsMovingPhotoSupported(bool* isSupported);
263 
264     Camera_ErrorCode EnableMovingPhoto(bool enableMovingPhoto);
265 
266     Camera_ErrorCode GetActiveProfile(Camera_Profile** profile);
267 
268     Camera_ErrorCode GetPhotoRotation(int32_t imageRotation, Camera_ImageRotation* cameraImageRotation);
269 
270 private:
271     Camera_ErrorCode RegisterRawPhotoAvailableCallback(OH_PhotoOutput_PhotoAvailable callback);
272 
273     OHOS::sptr<OHOS::CameraStandard::PhotoOutput> innerPhotoOutput_ = nullptr;
274     std::shared_ptr<InnerPhotoOutputCallback> innerCallback_ = nullptr;
275     OHOS::sptr<OHOS::Surface> photoSurface_ = nullptr;
276     OHOS::sptr<OHOS::CameraStandard::PhotoListener> photoListener_ = nullptr;
277     OHOS::sptr<OHOS::CameraStandard::RawPhotoListener> rawPhotoListener_ = nullptr;
278     uint8_t callbackFlag_ = 0;
279     OH_PhotoNative *photoNative_ = nullptr;
280     bool isMirrorEnable_ = false;
281 };
282 #endif // OHOS_PHOTO_OUTPUT_IMPL_H