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 #include "output/photo_output.h"
17 #include "camera_util.h"
18 #include "hstream_capture_callback_stub.h"
19 #include "media_log.h"
20
21 using namespace std;
22 namespace OHOS {
23 namespace CameraStandard {
PhotoCaptureSetting()24 PhotoCaptureSetting::PhotoCaptureSetting()
25 {
26 int32_t items = 10;
27 int32_t dataLength = 100;
28 captureMetadataSetting_ = std::make_shared<Camera::CameraMetadata>(items, dataLength);
29 }
30
GetQuality()31 PhotoCaptureSetting::QualityLevel PhotoCaptureSetting::GetQuality()
32 {
33 uint8_t normalQuality = 90;
34 uint8_t lowQuality = 50;
35 QualityLevel quality = LOW_QUALITY;
36 camera_metadata_item_t item;
37
38 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_QUALITY, &item);
39 if (ret != CAM_META_SUCCESS) {
40 return NORMAL_QUALITY;
41 }
42 if (item.data.u8[0] > normalQuality) {
43 quality = HIGH_QUALITY;
44 } else if (item.data.u8[0] > lowQuality) {
45 quality = NORMAL_QUALITY;
46 }
47 return quality;
48 }
49
SetQuality(PhotoCaptureSetting::QualityLevel qualityLevel)50 void PhotoCaptureSetting::SetQuality(PhotoCaptureSetting::QualityLevel qualityLevel)
51 {
52 bool status = false;
53 camera_metadata_item_t item;
54 uint8_t highQuality = 100;
55 uint8_t normalQuality = 90;
56 uint8_t quality = 50;
57
58 if (qualityLevel == HIGH_QUALITY) {
59 quality = highQuality;
60 } else if (qualityLevel == NORMAL_QUALITY) {
61 quality = normalQuality;
62 }
63 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_QUALITY, &item);
64 if (ret == CAM_META_ITEM_NOT_FOUND) {
65 status = captureMetadataSetting_->addEntry(OHOS_JPEG_QUALITY, &quality, 1);
66 } else if (ret == CAM_META_SUCCESS) {
67 status = captureMetadataSetting_->updateEntry(OHOS_JPEG_QUALITY, &quality, 1);
68 }
69
70 if (!status) {
71 MEDIA_ERR_LOG("PhotoCaptureSetting::SetQuality Failed to set Quality");
72 }
73 return;
74 }
75
GetRotation()76 PhotoCaptureSetting::RotationConfig PhotoCaptureSetting::GetRotation()
77 {
78 RotationConfig rotation;
79 camera_metadata_item_t item;
80
81 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
82 if (ret == CAM_META_SUCCESS) {
83 rotation = static_cast<RotationConfig>(item.data.i32[0]);
84 return rotation;
85 }
86 return RotationConfig::Rotation_0;
87 }
88
SetRotation(PhotoCaptureSetting::RotationConfig rotationValue)89 void PhotoCaptureSetting::SetRotation(PhotoCaptureSetting::RotationConfig rotationValue)
90 {
91 bool status = false;
92 camera_metadata_item_t item;
93 int32_t rotation = rotationValue;
94
95 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
96 if (ret == CAM_META_ITEM_NOT_FOUND) {
97 status = captureMetadataSetting_->addEntry(OHOS_JPEG_ORIENTATION, &rotation, 1);
98 } else if (ret == CAM_META_SUCCESS) {
99 status = captureMetadataSetting_->updateEntry(OHOS_JPEG_ORIENTATION, &rotation, 1);
100 }
101
102 if (!status) {
103 MEDIA_ERR_LOG("PhotoCaptureSetting::SetRotation Failed to set Rotation");
104 }
105 return;
106 }
107
SetGpsLocation(double latitude,double longitude)108 void PhotoCaptureSetting::SetGpsLocation(double latitude, double longitude)
109 {
110 double gpsCoordinates[2];
111 gpsCoordinates[0] = latitude;
112 gpsCoordinates[1] = longitude;
113 bool status = false;
114 camera_metadata_item_t item;
115
116 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_GPS_COORDINATES, &item);
117 if (ret == CAM_META_ITEM_NOT_FOUND) {
118 status = captureMetadataSetting_->addEntry(OHOS_JPEG_GPS_COORDINATES, gpsCoordinates,
119 sizeof(gpsCoordinates) / sizeof(gpsCoordinates[0]));
120 } else if (ret == CAM_META_SUCCESS) {
121 status = captureMetadataSetting_->updateEntry(OHOS_JPEG_GPS_COORDINATES, gpsCoordinates,
122 sizeof(gpsCoordinates) / sizeof(gpsCoordinates[0]));
123 }
124
125 if (!status) {
126 MEDIA_ERR_LOG("PhotoCaptureSetting::SetGpsLocation Failed to set GPS co-ordinates");
127 }
128 return;
129 }
130
IsMirrored()131 bool PhotoCaptureSetting::IsMirrored()
132 {
133 bool isMirrorEnabled = false;
134 camera_metadata_item_t item;
135 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_CONTROL_CAPTURE_MIRROR, &item);
136 if (ret == CAM_META_SUCCESS) {
137 isMirrorEnabled = (item.data.u8[0] > 0) ? true : false;
138 }
139 return isMirrorEnabled;
140 }
141
SetMirror(bool enable)142 void PhotoCaptureSetting::SetMirror(bool enable)
143 {
144 bool status = false;
145 camera_metadata_item_t item;
146 uint8_t mirror = enable;
147
148 int ret = Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_CONTROL_CAPTURE_MIRROR, &item);
149 if (ret == CAM_META_ITEM_NOT_FOUND) {
150 status = captureMetadataSetting_->addEntry(OHOS_CONTROL_CAPTURE_MIRROR, &mirror, 1);
151 } else if (ret == CAM_META_SUCCESS) {
152 status = captureMetadataSetting_->updateEntry(OHOS_CONTROL_CAPTURE_MIRROR, &mirror, 1);
153 }
154
155 if (!status) {
156 MEDIA_ERR_LOG("PhotoCaptureSetting::SetMirror Failed to set mirroring in photo capture setting");
157 }
158 return;
159 }
160
GetCaptureMetadataSetting()161 std::shared_ptr<Camera::CameraMetadata> PhotoCaptureSetting::GetCaptureMetadataSetting()
162 {
163 return captureMetadataSetting_;
164 }
165
166 class HStreamCaptureCallbackImpl : public HStreamCaptureCallbackStub {
167 public:
168 sptr<PhotoOutput> photoOutput_ = nullptr;
HStreamCaptureCallbackImpl()169 HStreamCaptureCallbackImpl() : photoOutput_(nullptr) {
170 }
171
HStreamCaptureCallbackImpl(const sptr<PhotoOutput> & photoOutput)172 explicit HStreamCaptureCallbackImpl(const sptr<PhotoOutput>& photoOutput) : photoOutput_(photoOutput) {
173 }
174
~HStreamCaptureCallbackImpl()175 ~HStreamCaptureCallbackImpl()
176 {
177 photoOutput_ = nullptr;
178 }
179
OnCaptureStarted(const int32_t captureId)180 int32_t OnCaptureStarted(const int32_t captureId) override
181 {
182 if (photoOutput_ != nullptr && photoOutput_->GetApplicationCallback() != nullptr) {
183 photoOutput_->GetApplicationCallback()->OnCaptureStarted(captureId);
184 } else {
185 MEDIA_INFO_LOG("Discarding HStreamCaptureCallbackImpl::OnCaptureStarted callback");
186 }
187 return CAMERA_OK;
188 }
189
OnCaptureEnded(const int32_t captureId,const int32_t frameCount)190 int32_t OnCaptureEnded(const int32_t captureId, const int32_t frameCount) override
191 {
192 if (photoOutput_ != nullptr && photoOutput_->GetApplicationCallback() != nullptr) {
193 photoOutput_->GetApplicationCallback()->OnCaptureEnded(captureId, frameCount);
194 } else {
195 MEDIA_INFO_LOG("Discarding HStreamCaptureCallbackImpl::OnCaptureEnded callback");
196 }
197 return CAMERA_OK;
198 }
199
OnCaptureError(const int32_t captureId,const int32_t errorCode)200 int32_t OnCaptureError(const int32_t captureId, const int32_t errorCode) override
201 {
202 if (photoOutput_ != nullptr && photoOutput_->GetApplicationCallback() != nullptr) {
203 photoOutput_->GetApplicationCallback()->OnCaptureError(captureId, errorCode);
204 } else {
205 MEDIA_INFO_LOG("Discarding HStreamCaptureCallbackImpl::OnCaptureError callback");
206 }
207 return CAMERA_OK;
208 }
209
OnFrameShutter(const int32_t captureId,const uint64_t timestamp)210 int32_t OnFrameShutter(const int32_t captureId, const uint64_t timestamp) override
211 {
212 if (photoOutput_ != nullptr && photoOutput_->GetApplicationCallback() != nullptr) {
213 photoOutput_->GetApplicationCallback()->OnFrameShutter(captureId, timestamp);
214 } else {
215 MEDIA_INFO_LOG("Discarding HStreamCaptureCallbackImpl::OnFrameShutter callback");
216 }
217 return CAMERA_OK;
218 }
219 };
220
PhotoOutput(sptr<IStreamCapture> & streamCapture)221 PhotoOutput::PhotoOutput(sptr<IStreamCapture> &streamCapture)
222 : CaptureOutput(CAPTURE_OUTPUT_TYPE::PHOTO_OUTPUT), streamCapture_(streamCapture)
223 {}
224
SetCallback(std::shared_ptr<PhotoCallback> callback)225 void PhotoOutput::SetCallback(std::shared_ptr<PhotoCallback> callback)
226 {
227 int32_t errorCode = CAMERA_OK;
228
229 appCallback_ = callback;
230 if (appCallback_ != nullptr) {
231 if (cameraSvcCallback_ == nullptr) {
232 cameraSvcCallback_ = new HStreamCaptureCallbackImpl(this);
233 }
234 errorCode = streamCapture_->SetCallback(cameraSvcCallback_);
235 if (errorCode != CAMERA_OK) {
236 MEDIA_ERR_LOG("PhotoOutput::SetCallback: Failed to register callback, errorCode: %{public}d", errorCode);
237 cameraSvcCallback_ = nullptr;
238 appCallback_ = nullptr;
239 }
240 }
241 return;
242 }
243
GetApplicationCallback()244 std::shared_ptr<PhotoCallback> PhotoOutput::GetApplicationCallback()
245 {
246 return appCallback_;
247 }
248
GetStreamCapture()249 sptr<IStreamCapture> PhotoOutput::GetStreamCapture()
250 {
251 return streamCapture_;
252 }
253
Capture(std::shared_ptr<PhotoCaptureSetting> photoCaptureSettings)254 int32_t PhotoOutput::Capture(std::shared_ptr<PhotoCaptureSetting> photoCaptureSettings)
255 {
256 return streamCapture_->Capture(photoCaptureSettings->GetCaptureMetadataSetting());
257 }
258
Capture()259 int32_t PhotoOutput::Capture()
260 {
261 int32_t items = 0;
262 int32_t dataLength = 0;
263 std::shared_ptr<Camera::CameraMetadata> captureMetadataSetting =
264 std::make_shared<Camera::CameraMetadata>(items, dataLength);
265 return streamCapture_->Capture(captureMetadataSetting);
266 }
267
CancelCapture()268 int32_t PhotoOutput::CancelCapture()
269 {
270 return streamCapture_->CancelCapture();
271 }
272
Release()273 void PhotoOutput::Release()
274 {
275 int32_t retCode = streamCapture_->Release();
276 if (retCode != CAMERA_OK) {
277 MEDIA_ERR_LOG("Failed to release Camera Input!, retCode: %{public}d", retCode);
278 }
279 return;
280 }
281 } // CameraStandard
282 } // OHOS
283