• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "output/photo_output_callback.h"
25 #include "camera_log.h"
26 #include "camera_util.h"
27 #include "photo_native_impl.h"
28 #include "media_asset_helper.h"
29 
30 class InnerPhotoOutputCallback : public OHOS::CameraStandard::PhotoStateCallback,
31                                  public OHOS::CameraStandard::PhotoAvailableCallback,
32                                  public OHOS::CameraStandard::PhotoAssetAvailableCallback {
33 public:
InnerPhotoOutputCallback(Camera_PhotoOutput * photoOutput)34     explicit InnerPhotoOutputCallback(Camera_PhotoOutput* photoOutput) : photoOutput_(photoOutput)
35     {
36         callback_.onFrameStart = nullptr;
37         callback_.onFrameShutter = nullptr;
38         callback_.onFrameEnd = nullptr;
39         callback_.onError = nullptr;
40     }
41     ~InnerPhotoOutputCallback() = default;
42 
SaveCallback(PhotoOutput_Callbacks * callback)43     void SaveCallback(PhotoOutput_Callbacks* callback)
44     {
45         callback_ = *callback;
46     }
47 
SaveCaptureStartWithInfoCallback(OH_PhotoOutput_CaptureStartWithInfo callback)48     void SaveCaptureStartWithInfoCallback(OH_PhotoOutput_CaptureStartWithInfo callback)
49     {
50         captureStartWithInfoCallback_ = callback;
51     }
52 
SaveCaptureEndCallback(OH_PhotoOutput_CaptureEnd callback)53     void SaveCaptureEndCallback(OH_PhotoOutput_CaptureEnd callback)
54     {
55         captureEndCallback_ = callback;
56     }
57 
SaveFrameShutterEndCallback(OH_PhotoOutput_OnFrameShutterEnd callback)58     void SaveFrameShutterEndCallback(OH_PhotoOutput_OnFrameShutterEnd callback)
59     {
60         frameShutterEndCallback_ = callback;
61     }
62 
SaveCaptureReadyCallback(OH_PhotoOutput_CaptureReady callback)63     void SaveCaptureReadyCallback(OH_PhotoOutput_CaptureReady callback)
64     {
65         captureReadyCallback_ = callback;
66     }
67 
SaveEstimatedCaptureDurationCallback(OH_PhotoOutput_EstimatedCaptureDuration callback)68     void SaveEstimatedCaptureDurationCallback(OH_PhotoOutput_EstimatedCaptureDuration callback)
69     {
70         estimatedCaptureDurationCallback_ = callback;
71     }
72 
SavePhotoAvailableCallback(OH_PhotoOutput_PhotoAvailable callback)73     void SavePhotoAvailableCallback(OH_PhotoOutput_PhotoAvailable callback)
74     {
75         photoAvailableCallback_ = callback;
76     }
77 
SavePhotoAssetAvailableCallback(OH_PhotoOutput_PhotoAssetAvailable callback)78     void SavePhotoAssetAvailableCallback(OH_PhotoOutput_PhotoAssetAvailable callback)
79     {
80         photoAssetAvailableCallback_ = callback;
81     }
82 
RemoveCallback(PhotoOutput_Callbacks * callback)83     void RemoveCallback(PhotoOutput_Callbacks* callback)
84     {
85         if (callback->onFrameStart) {
86             callback_.onFrameStart = nullptr;
87         }
88         if (callback->onFrameShutter) {
89             callback_.onFrameShutter = nullptr;
90         }
91         if (callback->onFrameEnd) {
92             callback_.onFrameEnd = nullptr;
93         }
94         if (callback->onError) {
95             callback_.onError = nullptr;
96         }
97     }
98 
RemoveCaptureStartWithInfoCallback(OH_PhotoOutput_CaptureStartWithInfo callback)99     void RemoveCaptureStartWithInfoCallback(OH_PhotoOutput_CaptureStartWithInfo callback)
100     {
101         if (callback != nullptr) {
102             captureStartWithInfoCallback_ = nullptr;
103         }
104     }
105 
RemoveCaptureEndCallback(OH_PhotoOutput_CaptureEnd callback)106     void RemoveCaptureEndCallback(OH_PhotoOutput_CaptureEnd callback)
107     {
108         if (callback != nullptr) {
109             captureEndCallback_ = nullptr;
110         }
111     }
112 
RemoveFrameShutterEndCallback(OH_PhotoOutput_OnFrameShutterEnd callback)113     void RemoveFrameShutterEndCallback(OH_PhotoOutput_OnFrameShutterEnd callback)
114     {
115         if (callback != nullptr) {
116             frameShutterEndCallback_ = nullptr;
117         }
118     }
119 
RemoveCaptureReadyCallback(OH_PhotoOutput_CaptureReady callback)120     void RemoveCaptureReadyCallback(OH_PhotoOutput_CaptureReady callback)
121     {
122         if (callback != nullptr) {
123             captureReadyCallback_ = nullptr;
124         }
125     }
126 
RemoveEstimatedCaptureDurationCallback(OH_PhotoOutput_EstimatedCaptureDuration callback)127     void RemoveEstimatedCaptureDurationCallback(OH_PhotoOutput_EstimatedCaptureDuration callback)
128     {
129         if (callback != nullptr) {
130             estimatedCaptureDurationCallback_ = nullptr;
131         }
132     }
133 
RemovePhotoAvailableCallback(OH_PhotoOutput_PhotoAvailable callback)134     void RemovePhotoAvailableCallback(OH_PhotoOutput_PhotoAvailable callback)
135     {
136         if (callback != nullptr) {
137             photoAvailableCallback_ = nullptr;
138         }
139     }
140 
RemovePhotoAssetAvailableCallback(OH_PhotoOutput_PhotoAssetAvailable callback)141     void RemovePhotoAssetAvailableCallback(OH_PhotoOutput_PhotoAssetAvailable callback)
142     {
143         if (callback != nullptr) {
144             photoAssetAvailableCallback_ = nullptr;
145         }
146     }
147 
OnCaptureStarted(const int32_t captureID)148     void OnCaptureStarted(const int32_t captureID) const override
149     {
150         MEDIA_DEBUG_LOG("OnCaptureStarted is called!, captureID: %{public}d", captureID);
151         Camera_CaptureStartInfo info;
152         info.captureId = captureID;
153         if (photoOutput_ != nullptr && captureStartWithInfoCallback_ != nullptr) {
154             captureStartWithInfoCallback_(photoOutput_, &info);
155         }
156     }
157 
158 // need fix
OnCaptureStarted(const int32_t captureID,uint32_t exposureTime)159     void OnCaptureStarted(const int32_t captureID, uint32_t exposureTime) const override
160     {
161         MEDIA_DEBUG_LOG("OnCaptureStarted is called!, captureID: %{public}d", captureID);
162         if (photoOutput_ != nullptr && callback_.onFrameStart != nullptr) {
163             callback_.onFrameStart(photoOutput_);
164         }
165     }
166 
167 // need fix
OnFrameShutter(const int32_t captureId,const uint64_t timestamp)168     void OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const override
169     {
170         MEDIA_DEBUG_LOG("onFrameShutter is called!, captureId: %{public}d", captureId);
171         Camera_FrameShutterInfo info;
172         info.captureId = captureId;
173         info.timestamp = timestamp;
174         if (photoOutput_ != nullptr && callback_.onFrameShutter != nullptr) {
175             callback_.onFrameShutter(photoOutput_, &info);
176         }
177     }
178 
OnFrameShutterEnd(const int32_t captureId,const uint64_t timestamp)179     void OnFrameShutterEnd(const int32_t captureId, const uint64_t timestamp) const override
180     {
181         MEDIA_DEBUG_LOG("OnFrameShutterEnd is called!, captureId: %{public}d", captureId);
182         Camera_FrameShutterInfo info;
183         info.captureId = captureId;
184         info.timestamp = timestamp;
185         if (photoOutput_ != nullptr && frameShutterEndCallback_ != nullptr) {
186             frameShutterEndCallback_(photoOutput_, &info);
187         }
188     }
189 
OnCaptureReady(const int32_t captureId,const uint64_t timestamp)190     void OnCaptureReady(const int32_t captureId, const uint64_t timestamp) const override
191     {
192         MEDIA_DEBUG_LOG("OnCaptureReady is called!, captureId: %{public}d", captureId);
193         if (photoOutput_ != nullptr && captureReadyCallback_ != nullptr) {
194             captureReadyCallback_(photoOutput_);
195         }
196     }
197 
OnEstimatedCaptureDuration(const int32_t duration)198     void OnEstimatedCaptureDuration(const int32_t duration) const override
199     {
200         MEDIA_DEBUG_LOG("OnEstimatedCaptureDuration is called!, duration: %{public}d", duration);
201         if (photoOutput_ != nullptr && estimatedCaptureDurationCallback_ != nullptr) {
202             estimatedCaptureDurationCallback_(photoOutput_, duration);
203         }
204     }
205 
OnCaptureEnded(const int32_t captureID,const int32_t frameCount)206     void OnCaptureEnded(const int32_t captureID, const int32_t frameCount) const override
207     {
208         MEDIA_DEBUG_LOG("OnCaptureEnded is called! captureID: %{public}d", captureID);
209         MEDIA_DEBUG_LOG("OnCaptureEnded is called! framecount: %{public}d", frameCount);
210         if (photoOutput_ != nullptr && callback_.onFrameEnd != nullptr) {
211             callback_.onFrameEnd(photoOutput_, frameCount);
212         }
213         if (photoOutput_ != nullptr && captureEndCallback_ != nullptr) {
214             captureEndCallback_(photoOutput_, frameCount);
215         }
216     }
217 
OnCaptureError(const int32_t captureId,const int32_t errorCode)218     void OnCaptureError(const int32_t captureId, const int32_t errorCode) const override
219     {
220         MEDIA_DEBUG_LOG("OnCaptureError is called!, errorCode: %{public}d", errorCode);
221         if (photoOutput_ != nullptr && callback_.onError != nullptr) {
222             callback_.onError(photoOutput_, OHOS::CameraStandard::FrameworkToNdkCameraError(errorCode));
223         }
224     }
225 
OnOfflineDeliveryFinished(const int32_t captureId)226     void OnOfflineDeliveryFinished(const int32_t captureId) const override
227     {
228         MEDIA_DEBUG_LOG("OnOfflineDeliveryFinished is called");
229     }
230 
OnPhotoAvailable(const std::shared_ptr<OHOS::Media::NativeImage> nativeImage,bool isRaw)231     void OnPhotoAvailable(const std::shared_ptr<OHOS::Media::NativeImage> nativeImage, bool isRaw) const override
232     {
233         MEDIA_DEBUG_LOG("OnPhotoAvailable E");
234         CHECK_RETURN_ELOG(photoOutput_ == nullptr, "photoOutput is null");
235         CHECK_RETURN_ELOG(photoAvailableCallback_ == nullptr, "callback is null");
236         OH_PhotoNative *photoNative = new (std::nothrow) OH_PhotoNative;
237         CHECK_RETURN_ELOG(photoNative == nullptr, "Create photo native failed");
238         if (!isRaw) {
239             photoNative->SetMainImage(nativeImage);
240         } else {
241             photoNative->SetRawImage(nativeImage);
242         }
243         photoAvailableCallback_(photoOutput_, photoNative);
244         MEDIA_DEBUG_LOG("OnPhotoAvailable X");
245     }
246 
OnPhotoAssetAvailable(const int32_t captureId,const std::string & uri,int32_t cameraShotType,const std::string & burstKey)247     void OnPhotoAssetAvailable(const int32_t captureId, const std::string &uri, int32_t cameraShotType,
248         const std::string &burstKey) const override
249     {
250         MEDIA_DEBUG_LOG("OnPhotoAssetAvailable E");
251         CHECK_RETURN_ELOG(photoOutput_ == nullptr, "photoOutput is null");
252         CHECK_RETURN_ELOG(photoAssetAvailableCallback_ == nullptr, "callback is null");
253         auto mediaAssetHelper = OHOS::Media::MediaAssetHelperFactory::CreateMediaAssetHelper();
254         CHECK_RETURN_ELOG(mediaAssetHelper == nullptr, "create media asset helper failed");
255         auto mediaAsset = mediaAssetHelper->GetMediaAsset(uri, cameraShotType, burstKey);
256         CHECK_RETURN_ELOG(mediaAsset == nullptr, "Create photo asset failed");
257         photoAssetAvailableCallback_(photoOutput_, mediaAsset);
258         MEDIA_DEBUG_LOG("OnPhotoAssetAvailable X");
259     }
260 
261 private:
262     Camera_PhotoOutput* photoOutput_;
263     PhotoOutput_Callbacks callback_;
264     OH_PhotoOutput_CaptureStartWithInfo captureStartWithInfoCallback_ = nullptr;
265     OH_PhotoOutput_CaptureEnd captureEndCallback_ = nullptr;
266     OH_PhotoOutput_OnFrameShutterEnd frameShutterEndCallback_ = nullptr;
267     OH_PhotoOutput_CaptureReady captureReadyCallback_ = nullptr;
268     OH_PhotoOutput_EstimatedCaptureDuration estimatedCaptureDurationCallback_ = nullptr;
269     OH_PhotoOutput_PhotoAvailable photoAvailableCallback_ = nullptr;
270     OH_PhotoOutput_PhotoAssetAvailable photoAssetAvailableCallback_ = nullptr;
271 };
272 
273 struct Camera_PhotoOutput {
274 public:
275     explicit Camera_PhotoOutput(OHOS::sptr<OHOS::CameraStandard::PhotoOutput> &innerPhotoOutput);
276     ~Camera_PhotoOutput();
277 
278     Camera_ErrorCode RegisterCallback(PhotoOutput_Callbacks* callback);
279 
280     Camera_ErrorCode UnregisterCallback(PhotoOutput_Callbacks* callback);
281 
282     Camera_ErrorCode RegisterCaptureStartWithInfoCallback(OH_PhotoOutput_CaptureStartWithInfo callback);
283 
284     Camera_ErrorCode UnregisterCaptureStartWithInfoCallback(OH_PhotoOutput_CaptureStartWithInfo callback);
285 
286     Camera_ErrorCode RegisterCaptureEndCallback(OH_PhotoOutput_CaptureEnd callback);
287 
288     Camera_ErrorCode UnregisterCaptureEndCallback(OH_PhotoOutput_CaptureEnd callback);
289 
290     Camera_ErrorCode RegisterFrameShutterEndCallback(OH_PhotoOutput_OnFrameShutterEnd callback);
291 
292     Camera_ErrorCode UnregisterFrameShutterEndCallback(OH_PhotoOutput_OnFrameShutterEnd callback);
293 
294     Camera_ErrorCode RegisterCaptureReadyCallback(OH_PhotoOutput_CaptureReady callback);
295 
296     Camera_ErrorCode UnregisterCaptureReadyCallback(OH_PhotoOutput_CaptureReady callback);
297 
298     Camera_ErrorCode RegisterEstimatedCaptureDurationCallback(OH_PhotoOutput_EstimatedCaptureDuration callback);
299 
300     Camera_ErrorCode UnregisterEstimatedCaptureDurationCallback(OH_PhotoOutput_EstimatedCaptureDuration callback);
301 
302     Camera_ErrorCode RegisterPhotoAvailableCallback(OH_PhotoOutput_PhotoAvailable callback);
303 
304     Camera_ErrorCode UnregisterPhotoAvailableCallback(OH_PhotoOutput_PhotoAvailable callback);
305 
306     Camera_ErrorCode RegisterPhotoAssetAvailableCallback(OH_PhotoOutput_PhotoAssetAvailable callback);
307 
308     Camera_ErrorCode UnregisterPhotoAssetAvailableCallback(OH_PhotoOutput_PhotoAssetAvailable callback);
309 
310     Camera_ErrorCode Capture();
311 
312     Camera_ErrorCode Capture_WithCaptureSetting(Camera_PhotoCaptureSetting setting);
313 
314     Camera_ErrorCode Release();
315 
316     Camera_ErrorCode IsMirrorSupported(bool* isSupported);
317 
318     Camera_ErrorCode EnableMirror(bool enableMirror);
319 
320     OHOS::sptr<OHOS::CameraStandard::PhotoOutput> GetInnerPhotoOutput();
321 
322     OH_PhotoNative* CreateCameraPhotoNative(std::shared_ptr<OHOS::Media::NativeImage> &image, bool isMain);
323 
324     Camera_ErrorCode IsMovingPhotoSupported(bool* isSupported);
325 
326     Camera_ErrorCode EnableMovingPhoto(bool enableMovingPhoto);
327 
328     Camera_ErrorCode GetActiveProfile(Camera_Profile** profile);
329 
330     Camera_ErrorCode GetPhotoRotation(int32_t imageRotation, Camera_ImageRotation* cameraImageRotation);
331 
332 private:
333 
334     OHOS::sptr<OHOS::CameraStandard::PhotoOutput> innerPhotoOutput_ = nullptr;
335     std::shared_ptr<InnerPhotoOutputCallback> innerCallback_ = nullptr;
336     uint8_t callbackFlag_ = 0;
337     OH_PhotoNative *photoNative_ = nullptr;
338     bool isMirrorEnable_ = false;
339 };
340 #endif // OHOS_PHOTO_OUTPUT_IMPL_H