• 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 #include "hstream_capture.h"
17 #include <cstdint>
18 
19 #include "camera_log.h"
20 #include "camera_service_ipc_interface_code.h"
21 #include "camera_util.h"
22 #include "hstream_common.h"
23 #include "ipc_skeleton.h"
24 #include "metadata_utils.h"
25 
26 namespace OHOS {
27 namespace CameraStandard {
28 using namespace OHOS::HDI::Camera::V1_0;
29 static const int32_t CAPTURE_ROTATE_360 = 360;
HStreamCapture(sptr<OHOS::IBufferProducer> producer,int32_t format,int32_t width,int32_t height)30 HStreamCapture::HStreamCapture(sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height)
31     : HStreamCommon(StreamType::CAPTURE, producer, format, width, height)
32 {
33     MEDIA_INFO_LOG("HStreamCapture::HStreamCapture!");
34 }
35 
~HStreamCapture()36 HStreamCapture::~HStreamCapture()
37 {
38     MEDIA_INFO_LOG("HStreamCapture::~HStreamCapture!");
39 }
40 
LinkInput(sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)41 int32_t HStreamCapture::LinkInput(sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,
42     std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)
43 {
44     return HStreamCommon::LinkInput(streamOperator, cameraAbility);
45 }
46 
SetStreamInfo(StreamInfo_V1_1 & streamInfo)47 void HStreamCapture::SetStreamInfo(StreamInfo_V1_1 &streamInfo)
48 {
49     HStreamCommon::SetStreamInfo(streamInfo);
50     streamInfo.v1_0.intent_ = STILL_CAPTURE;
51     streamInfo.v1_0.encodeType_ = ENCODE_TYPE_JPEG;
52     HDI::Camera::V1_1::ExtendedStreamInfo extendedStreamInfo;
53     extendedStreamInfo.type = HDI::Camera::V1_1::EXTENDED_STREAM_INFO_QUICK_THUMBNAIL;
54     extendedStreamInfo.bufferQueue = thumbnailBufferQueue_;
55     // quickThumbnial do not need these param
56     extendedStreamInfo.width = 0;
57     extendedStreamInfo.height = 0;
58     extendedStreamInfo.format = 0;
59     extendedStreamInfo.dataspace = 0;
60     streamInfo.extendedStreamInfos = {extendedStreamInfo};
61 }
62 
SetThumbnail(bool isEnabled,const sptr<OHOS::IBufferProducer> & producer)63 int32_t HStreamCapture::SetThumbnail(bool isEnabled, const sptr<OHOS::IBufferProducer> &producer)
64 {
65     if (isEnabled && producer != nullptr) {
66         thumbnailSwitch_ = 1;
67         thumbnailBufferQueue_ = new BufferProducerSequenceable(producer);
68     } else {
69         thumbnailSwitch_ = 0;
70         thumbnailBufferQueue_ = nullptr;
71     }
72     return CAMERA_OK;
73 }
74 
75 
DeferImageDeliveryFor(int32_t type)76 int32_t HStreamCapture::DeferImageDeliveryFor(int32_t type)
77 {
78     MEDIA_INFO_LOG("HStreamCapture::DeferImageDeliveryFor type: %{public}d", type);
79     if (type == OHOS::HDI::Camera::V1_2::STILL_IMAGE) {
80         MEDIA_INFO_LOG("HStreamCapture STILL_IMAGE");
81         deferredPhotoSwitch_ = 1;
82     } else if (type == OHOS::HDI::Camera::V1_2::MOVING_IMAGE) {
83         MEDIA_INFO_LOG("HStreamCapture MOVING_IMAGE");
84         deferredVideoSwitch_ = 1;
85     } else {
86         MEDIA_INFO_LOG("HStreamCapture NONE");
87         deferredPhotoSwitch_ = 0;
88         deferredVideoSwitch_ = 0;
89     }
90     return CAMERA_OK;
91 }
92 
Capture(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureSettings)93 int32_t HStreamCapture::Capture(const std::shared_ptr<OHOS::Camera::CameraMetadata>& captureSettings)
94 {
95     CAMERA_SYNC_TRACE;
96     auto streamOperator = GetStreamOperator();
97     if (streamOperator == nullptr) {
98         return CAMERA_INVALID_STATE;
99     }
100 
101     auto preparedCaptureId = GetPreparedCaptureId();
102     if (preparedCaptureId != CAPTURE_ID_UNSET) {
103         MEDIA_ERR_LOG("HStreamCapture::Capture, Already started with captureID: %{public}d", preparedCaptureId);
104         return CAMERA_INVALID_STATE;
105     }
106 
107     int32_t ret = PrepareCaptureId();
108     preparedCaptureId = GetPreparedCaptureId();
109     if (ret != CAMERA_OK || preparedCaptureId == CAPTURE_ID_UNSET) {
110         MEDIA_ERR_LOG("HStreamCapture::Capture Failed to allocate a captureId");
111         return ret;
112     }
113     CaptureInfo captureInfoPhoto;
114     captureInfoPhoto.streamIds_ = { GetStreamId() };
115     std::vector<uint8_t> setting;
116     if (!OHOS::Camera::GetCameraMetadataItemCount(captureSettings->get())) {
117         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
118         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, setting);
119         captureInfoPhoto.captureSetting_ = setting;
120     } else {
121         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(captureSettings, setting);
122         captureInfoPhoto.captureSetting_ = setting;
123     }
124     captureInfoPhoto.enableShutterCallback_ = true;
125     // debug log for jpeg quality
126     std::shared_ptr<OHOS::Camera::CameraMetadata> captureMetadataSetting_ = nullptr;
127     OHOS::Camera::MetadataUtils::ConvertVecToMetadata(captureInfoPhoto.captureSetting_, captureMetadataSetting_);
128     if (captureMetadataSetting_ != nullptr) {
129         // print quality, mirror log
130         PrintDebugLog(captureMetadataSetting_);
131         // convert rotation with application set rotation
132         SetRotation(captureMetadataSetting_);
133 
134         // update settings
135         std::vector<uint8_t> finalSetting;
136         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(captureMetadataSetting_, finalSetting);
137         captureInfoPhoto.captureSetting_ = finalSetting;
138     }
139     auto callingTokenId = IPCSkeleton::GetCallingTokenID();
140     const std::string permissionName = "ohos.permission.CAMERA";
141     AddCameraPermissionUsedRecord(callingTokenId, permissionName);
142     MEDIA_INFO_LOG("HStreamCapture::Capture Starting photo capture with capture ID: %{public}d", preparedCaptureId);
143     CamRetCode rc = (CamRetCode)(streamOperator->Capture(preparedCaptureId, captureInfoPhoto, false));
144     if (rc != HDI::Camera::V1_0::NO_ERROR) {
145         ResetCaptureId();
146         MEDIA_ERR_LOG("HStreamCapture::Capture failed with error Code: %{public}d", rc);
147         ret = HdiToServiceError(rc);
148     }
149     camera_metadata_item_t item;
150     camera_position_enum_t cameraPosition = OHOS_CAMERA_POSITION_FRONT;
151     {
152         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
153         int32_t result = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_ABILITY_CAMERA_POSITION,
154                                                               &item);
155         if (result == CAM_META_SUCCESS && item.count > 0) {
156             cameraPosition = static_cast<camera_position_enum_t>(item.data.u8[0]);
157         }
158     }
159 
160     int32_t NightMode = 4;
161     if (GetMode() == NightMode && cameraPosition == OHOS_CAMERA_POSITION_BACK) {
162         return ret;
163     }
164     ResetCaptureId();
165     return ret;
166 }
167 
PrintDebugLog(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureMetadataSetting_)168 void HStreamCapture::PrintDebugLog(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting_)
169 {
170     camera_metadata_item_t item;
171     int result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_QUALITY, &item);
172     if (result != CAM_META_SUCCESS) {
173         MEDIA_DEBUG_LOG("HStreamCapture::Failed to find OHOS_JPEG_QUALITY tag");
174     } else {
175         MEDIA_DEBUG_LOG("HStreamCapture::find OHOS_JPEG_QUALITY value = %{public}d", item.data.u8[0]);
176     }
177 
178     // debug log for capture mirror
179     result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(),
180                                                    OHOS_CONTROL_CAPTURE_MIRROR, &item);
181     if (result != CAM_META_SUCCESS) {
182         MEDIA_DEBUG_LOG("HStreamCapture::Failed to find OHOS_CONTROL_CAPTURE_MIRROR tag");
183     } else {
184         MEDIA_DEBUG_LOG("HStreamCapture::find OHOS_CONTROL_CAPTURE_MIRROR value = %{public}d", item.data.u8[0]);
185     }
186 }
187 
SetRotation(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureMetadataSetting_)188 void HStreamCapture::SetRotation(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting_)
189 {
190     // set orientation for capture
191     // sensor orientation, counter-clockwise rotation
192     int32_t sensorOrientation = 0;
193     int result;
194     camera_metadata_item_t item;
195     {
196         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
197         if (cameraAbility_ == nullptr) {
198             return;
199         }
200         result = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_SENSOR_ORIENTATION, &item);
201         if (result == CAM_META_SUCCESS && item.count > 0) {
202             sensorOrientation = item.data.i32[0];
203         }
204         MEDIA_INFO_LOG("set rotation sensor orientation %{public}d", sensorOrientation);
205 
206         camera_position_enum_t cameraPosition = OHOS_CAMERA_POSITION_BACK;
207         result = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_ABILITY_CAMERA_POSITION, &item);
208         if (result == CAM_META_SUCCESS && item.count > 0) {
209             cameraPosition = static_cast<camera_position_enum_t>(item.data.u8[0]);
210         }
211         MEDIA_INFO_LOG("set rotation camera position %{public}d", cameraPosition);
212     }
213 
214     // rotation from application
215     int32_t rotationValue = 0;
216     result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
217     if (result == CAM_META_SUCCESS && item.count > 0) {
218         rotationValue = item.data.i32[0];
219     }
220     MEDIA_INFO_LOG("set rotation app rotationValue %{public}d", rotationValue);
221 
222     // real rotation
223     int32_t rotation = sensorOrientation + rotationValue;
224     if (rotation >= CAPTURE_ROTATE_360) {
225         rotation = rotation - CAPTURE_ROTATE_360;
226     }
227     MEDIA_INFO_LOG("set rotation camera real rotation %{public}d", rotation);
228 
229     bool status = false;
230     if (result == CAM_META_ITEM_NOT_FOUND) {
231         status = captureMetadataSetting_->addEntry(OHOS_JPEG_ORIENTATION, &rotation, 1);
232     } else if (result == CAM_META_SUCCESS) {
233         status = captureMetadataSetting_->updateEntry(OHOS_JPEG_ORIENTATION, &rotation, 1);
234     }
235     result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
236     if (result != CAM_META_SUCCESS) {
237         MEDIA_ERR_LOG("set rotation Failed to find OHOS_JPEG_ORIENTATION tag");
238     }
239     if (!status) {
240         MEDIA_ERR_LOG("set rotation Failed to set Rotation");
241     }
242 }
243 
CancelCapture()244 int32_t HStreamCapture::CancelCapture()
245 {
246     CAMERA_SYNC_TRACE;
247     // Cancel cature is dummy till continuous/burst mode is supported
248     StopStream();
249     return CAMERA_OK;
250 }
251 
SetMode(int32_t modeName)252 void HStreamCapture::SetMode(int32_t modeName)
253 {
254     modeName_ = modeName;
255     MEDIA_INFO_LOG("HStreamCapture SetMode modeName = %{public}d", modeName);
256 }
257 
GetMode()258 int32_t HStreamCapture::GetMode()
259 {
260     MEDIA_INFO_LOG("HStreamCapture GetMode modeName = %{public}d", modeName_);
261     return modeName_;
262 }
263 
ConfirmCapture()264 int32_t HStreamCapture::ConfirmCapture()
265 {
266     CAMERA_SYNC_TRACE;
267     auto streamOperator = GetStreamOperator();
268     if (streamOperator == nullptr) {
269         return CAMERA_INVALID_STATE;
270     }
271     auto preparedCaptureId = GetPreparedCaptureId();
272     MEDIA_INFO_LOG("HStreamCapture::ConfirmCapture with capture ID: %{public}d", preparedCaptureId);
273     sptr<OHOS::HDI::Camera::V1_2::IStreamOperator> streamOperatorV1_2 =
274         OHOS::HDI::Camera::V1_2::IStreamOperator::CastFrom(streamOperator);
275     if (streamOperatorV1_2 == nullptr) {
276         MEDIA_ERR_LOG("HStreamCapture::ConfirmCapture streamOperatorV1_2 castFrom failed!");
277         return CAMERA_UNKNOWN_ERROR;
278     }
279     OHOS::HDI::Camera::V1_2::CamRetCode rc =
280         (OHOS::HDI::Camera::V1_2::CamRetCode)(streamOperatorV1_2->ConfirmCapture(preparedCaptureId));
281     int32_t ret = 0;
282     if (rc != HDI::Camera::V1_2::NO_ERROR) {
283         MEDIA_ERR_LOG("HStreamCapture::ConfirmCapture failed with error Code: %{public}d", rc);
284         ret = HdiToServiceErrorV1_2(rc);
285     }
286     ResetCaptureId();
287     return ret;
288 }
289 
Release()290 int32_t HStreamCapture::Release()
291 {
292     return ReleaseStream(false);
293 }
294 
ReleaseStream(bool isDelay)295 int32_t HStreamCapture::ReleaseStream(bool isDelay)
296 {
297     {
298         std::lock_guard<std::mutex> lock(callbackLock_);
299         streamCaptureCallback_ = nullptr;
300     }
301     return HStreamCommon::ReleaseStream(isDelay);
302 }
303 
SetCallback(sptr<IStreamCaptureCallback> & callback)304 int32_t HStreamCapture::SetCallback(sptr<IStreamCaptureCallback> &callback)
305 {
306     if (callback == nullptr) {
307         MEDIA_ERR_LOG("HStreamCapture::SetCallback callback is null");
308         return CAMERA_INVALID_ARG;
309     }
310     std::lock_guard<std::mutex> lock(callbackLock_);
311     streamCaptureCallback_ = callback;
312     return CAMERA_OK;
313 }
314 
OnCaptureStarted(int32_t captureId)315 int32_t HStreamCapture::OnCaptureStarted(int32_t captureId)
316 {
317     CAMERA_SYNC_TRACE;
318     std::lock_guard<std::mutex> lock(callbackLock_);
319     if (streamCaptureCallback_ != nullptr) {
320         streamCaptureCallback_->OnCaptureStarted(captureId);
321     }
322     return CAMERA_OK;
323 }
324 
OnCaptureStarted(int32_t captureId,uint32_t exposureTime)325 int32_t HStreamCapture::OnCaptureStarted(int32_t captureId, uint32_t exposureTime)
326 {
327     CAMERA_SYNC_TRACE;
328     std::lock_guard<std::mutex> lock(callbackLock_);
329     if (streamCaptureCallback_ != nullptr) {
330         streamCaptureCallback_->OnCaptureStarted(captureId, exposureTime);
331     }
332     return CAMERA_OK;
333 }
334 
OnCaptureEnded(int32_t captureId,int32_t frameCount)335 int32_t HStreamCapture::OnCaptureEnded(int32_t captureId, int32_t frameCount)
336 {
337     CAMERA_SYNC_TRACE;
338     std::lock_guard<std::mutex> lock(callbackLock_);
339     if (streamCaptureCallback_ != nullptr) {
340         streamCaptureCallback_->OnCaptureEnded(captureId, frameCount);
341     }
342     auto preparedCaptureId = GetPreparedCaptureId();
343     if (preparedCaptureId != CAPTURE_ID_UNSET) {
344         MEDIA_INFO_LOG("HStreamCapture::OnCaptureEnded capturId = %{public}d already used, need release",
345                        preparedCaptureId);
346         ResetCaptureId();
347     }
348     return CAMERA_OK;
349 }
350 
OnCaptureError(int32_t captureId,int32_t errorCode)351 int32_t HStreamCapture::OnCaptureError(int32_t captureId, int32_t errorCode)
352 {
353     std::lock_guard<std::mutex> lock(callbackLock_);
354     if (streamCaptureCallback_ != nullptr) {
355         int32_t captureErrorCode;
356         if (errorCode == BUFFER_LOST) {
357             captureErrorCode = CAMERA_STREAM_BUFFER_LOST;
358         } else {
359             captureErrorCode = CAMERA_UNKNOWN_ERROR;
360         }
361         CAMERA_SYSEVENT_FAULT(CreateMsg("Photo OnCaptureError! captureId:%d & "
362                                         "errorCode:%{public}d", captureId, captureErrorCode));
363         streamCaptureCallback_->OnCaptureError(captureId, captureErrorCode);
364     }
365     auto preparedCaptureId = GetPreparedCaptureId();
366     if (preparedCaptureId != CAPTURE_ID_UNSET) {
367         MEDIA_INFO_LOG("HStreamCapture::OnCaptureError capturId = %{public}d already used, need release",
368                        preparedCaptureId);
369         ResetCaptureId();
370     }
371     return CAMERA_OK;
372 }
373 
OnFrameShutter(int32_t captureId,uint64_t timestamp)374 int32_t HStreamCapture::OnFrameShutter(int32_t captureId, uint64_t timestamp)
375 {
376     CAMERA_SYNC_TRACE;
377     std::lock_guard<std::mutex> lock(callbackLock_);
378     if (streamCaptureCallback_ != nullptr) {
379         streamCaptureCallback_->OnFrameShutter(captureId, timestamp);
380     }
381     return CAMERA_OK;
382 }
383 
DumpStreamInfo(std::string & dumpString)384 void HStreamCapture::DumpStreamInfo(std::string& dumpString)
385 {
386     dumpString += "capture stream:\n";
387     dumpString += "ThumbnailSwitch:[" + std::to_string(thumbnailSwitch_);
388     if (thumbnailBufferQueue_) {
389         dumpString += "] ThumbnailBuffer producer Id:["
390             + std::to_string(thumbnailBufferQueue_->producer_->GetUniqueId());
391     }
392     dumpString += "]\n";
393     HStreamCommon::DumpStreamInfo(dumpString);
394 }
395 
OperatePermissionCheck(uint32_t interfaceCode)396 int32_t HStreamCapture::OperatePermissionCheck(uint32_t interfaceCode)
397 {
398     switch (static_cast<StreamCaptureInterfaceCode>(interfaceCode)) {
399         case CAMERA_STREAM_CAPTURE_START: {
400             auto callerToken = IPCSkeleton::GetCallingTokenID();
401             if (callerToken_ != callerToken) {
402                 MEDIA_ERR_LOG("HStreamCapture::OperatePermissionCheck fail, callerToken_ is : %{public}d, now token "
403                               "is %{public}d",
404                     callerToken_, callerToken);
405                 return CAMERA_OPERATION_NOT_ALLOWED;
406             }
407             break;
408         }
409         default:
410             break;
411     }
412     return CAMERA_OK;
413 }
414 
IsDeferredPhotoEnabled()415 int32_t HStreamCapture::IsDeferredPhotoEnabled()
416 {
417     MEDIA_INFO_LOG("HStreamCapture IsDeferredPhotoEnabled  deferredPhotoSwitch_: %{public}d", deferredPhotoSwitch_);
418     if (deferredPhotoSwitch_ == 1) {
419         return 1;
420     }
421     MEDIA_INFO_LOG("HStreamCapture IsDeferredPhotoEnabled return 0");
422     return 0;
423 }
424 
IsDeferredVideoEnabled()425 int32_t HStreamCapture::IsDeferredVideoEnabled()
426 {
427     MEDIA_INFO_LOG("HStreamCapture IsDeferredVideoEnabled  deferredVideoSwitch_: %{public}d", deferredVideoSwitch_);
428     if (deferredVideoSwitch_ == 1) {
429         return 1;
430     }
431     return 0;
432 }
433 } // namespace CameraStandard
434 } // namespace OHOS
435