1 /*
2 * Copyright (c) 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/metadata_output.h"
17
18 #include <algorithm>
19 #include <cinttypes>
20 #include <cstddef>
21 #include <cstdint>
22
23 #include "camera_device_ability_items.h"
24 #include "camera_error_code.h"
25 #include "camera_log.h"
26 #include "camera_manager.h"
27 #include "camera_security_utils.h"
28 #include "camera_util.h"
29 #include "session/capture_session.h"
30
31 namespace OHOS {
32 namespace CameraStandard {
33 sptr<MetadataObjectFactory> MetadataObjectFactory::metaFactoryInstance_;
34 std::mutex MetadataObjectFactory::instanceMutex_;
35
MetadataObject(const MetadataObjectType type,const int32_t timestamp,const Rect rect,const int32_t objectId,const int32_t confidence)36 MetadataObject::MetadataObject(const MetadataObjectType type, const int32_t timestamp, const Rect rect,
37 const int32_t objectId, const int32_t confidence)
38 : type_(type),
39 timestamp_(timestamp),
40 box_(rect),
41 objectId_(objectId),
42 confidence_(confidence)
43 {}
44
MetadataObject(const MetaObjectParms & parms)45 MetadataObject::MetadataObject(const MetaObjectParms &parms)
46 {
47 type_ = parms.type;
48 timestamp_ = parms.timestamp;
49 box_ = parms.box;
50 objectId_ = parms.objectId;
51 confidence_ = parms.confidence;
52 }
53
MetadataFaceObject(const MetaObjectParms & parms,const Rect leftEyeBoundingBox,const Rect rightEyeBoundingBox,const Emotion emotion,const int32_t emotionConfidence,const int32_t pitchAngle,const int32_t yawAngle,const int32_t rollAngle)54 MetadataFaceObject::MetadataFaceObject(const MetaObjectParms &parms, const Rect leftEyeBoundingBox,
55 const Rect rightEyeBoundingBox, const Emotion emotion,
56 const int32_t emotionConfidence, const int32_t pitchAngle,
57 const int32_t yawAngle, const int32_t rollAngle)
58 : MetadataObject(parms),
59 leftEyeBoundingBox_(leftEyeBoundingBox),
60 rightEyeBoundingBox_(rightEyeBoundingBox),
61 emotion_(emotion),
62 emotionConfidence_(emotionConfidence),
63 pitchAngle_(pitchAngle),
64 yawAngle_(yawAngle),
65 rollAngle_(rollAngle)
66 {}
67
MetadataHumanBodyObject(const MetaObjectParms & parms)68 MetadataHumanBodyObject::MetadataHumanBodyObject(const MetaObjectParms &parms) : MetadataObject(parms) {}
69
MetadataCatFaceObject(const MetaObjectParms & parms,const Rect leftEyeBoundingBox,const Rect rightEyeBoundingBox)70 MetadataCatFaceObject::MetadataCatFaceObject(const MetaObjectParms &parms, const Rect leftEyeBoundingBox,
71 const Rect rightEyeBoundingBox)
72 : MetadataObject(parms),
73 leftEyeBoundingBox_(leftEyeBoundingBox),
74 rightEyeBoundingBox_(rightEyeBoundingBox)
75 {}
76
MetadataCatBodyObject(const MetaObjectParms & parms)77 MetadataCatBodyObject::MetadataCatBodyObject(const MetaObjectParms &parms) : MetadataObject(parms) {}
78
MetadataDogFaceObject(const MetaObjectParms & parms,const Rect leftEyeBoundingBox,const Rect rightEyeBoundingBox)79 MetadataDogFaceObject::MetadataDogFaceObject(const MetaObjectParms &parms, const Rect leftEyeBoundingBox,
80 const Rect rightEyeBoundingBox)
81 : MetadataObject(parms),
82 leftEyeBoundingBox_(leftEyeBoundingBox),
83 rightEyeBoundingBox_(rightEyeBoundingBox)
84 {}
85
MetadataDogBodyObject(const MetaObjectParms & parms)86 MetadataDogBodyObject::MetadataDogBodyObject(const MetaObjectParms &parms) : MetadataObject(parms) {}
87
MetadataSalientDetectionObject(const MetaObjectParms & parms)88 MetadataSalientDetectionObject::MetadataSalientDetectionObject(const MetaObjectParms &parms) : MetadataObject(parms) {}
89
MetadataBarCodeDetectionObject(const MetaObjectParms & parms)90 MetadataBarCodeDetectionObject::MetadataBarCodeDetectionObject(const MetaObjectParms &parms) : MetadataObject(parms) {}
91
MetadataOutput(sptr<IConsumerSurface> surface,sptr<IStreamMetadata> & streamMetadata)92 MetadataOutput::MetadataOutput(sptr<IConsumerSurface> surface, sptr<IStreamMetadata> &streamMetadata)
93 : CaptureOutput(CAPTURE_OUTPUT_TYPE_METADATA, StreamType::METADATA, surface->GetProducer(), nullptr),
94 surface_(surface)
95 {
96 MEDIA_DEBUG_LOG("MetadataOutput::MetadataOutput construct enter");
97 }
98
~MetadataOutput()99 MetadataOutput::~MetadataOutput()
100 {
101 ReleaseSurface();
102 }
103
GetAppObjectCallback()104 std::shared_ptr<MetadataObjectCallback> MetadataOutput::GetAppObjectCallback()
105 {
106 // LCOV_EXCL_START
107 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
108 return appObjectCallback_;
109 // LCOV_EXCL_STOP
110 }
111
GetAppStateCallback()112 std::shared_ptr<MetadataStateCallback> MetadataOutput::GetAppStateCallback()
113 {
114 // LCOV_EXCL_START
115 MEDIA_DEBUG_LOG("CameraDeviceServiceCallback::GetAppStateCallback");
116 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
117 return appStateCallback_;
118 // LCOV_EXCL_STOP
119 }
120
GetSupportedMetadataObjectTypes()121 std::vector<MetadataObjectType> MetadataOutput::GetSupportedMetadataObjectTypes()
122 {
123 auto session = GetSession();
124 CHECK_RETURN_RET(session == nullptr, {});
125 auto inputDevice = session->GetInputDevice();
126 CHECK_RETURN_RET(inputDevice == nullptr, {});
127 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
128 CHECK_RETURN_RET(cameraObj == nullptr, {});
129 std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetCachedMetadata();
130 CHECK_RETURN_RET(metadata == nullptr, {});
131 camera_metadata_item_t item;
132 int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_STATISTICS_FACE_DETECT_MODE, &item);
133 CHECK_RETURN_RET(ret, {});
134 std::vector<MetadataObjectType> objectTypes;
135 for (size_t index = 0; index < item.count; index++) {
136 CHECK_EXECUTE(item.data.u8[index] == OHOS_CAMERA_FACE_DETECT_MODE_SIMPLE,
137 objectTypes.emplace_back(MetadataObjectType::FACE));
138 }
139 return objectTypes;
140 }
141
SetCapturingMetadataObjectTypes(std::vector<MetadataObjectType> metadataObjectTypes)142 void MetadataOutput::SetCapturingMetadataObjectTypes(std::vector<MetadataObjectType> metadataObjectTypes)
143 {
144 auto session = GetSession();
145 CHECK_RETURN((session == nullptr) || (session->GetInputDevice() == nullptr));
146 std::set<camera_face_detect_mode_t> objectTypes;
147 for (const auto& type : metadataObjectTypes) {
148 CHECK_EXECUTE(type == MetadataObjectType::FACE, objectTypes.insert(OHOS_CAMERA_FACE_DETECT_MODE_SIMPLE));
149 }
150 CHECK_EXECUTE(objectTypes.empty(), objectTypes.insert(OHOS_CAMERA_FACE_DETECT_MODE_OFF));
151
152 session->SetCaptureMetadataObjectTypes(objectTypes);
153 }
154
AddMetadataObjectTypes(std::vector<MetadataObjectType> metadataObjectTypes)155 int32_t MetadataOutput::AddMetadataObjectTypes(std::vector<MetadataObjectType> metadataObjectTypes)
156 {
157 const size_t maxSize4NonSystemApp = 1;
158 // LCOV_EXCL_START
159 if (!CameraSecurity::CheckSystemApp()) {
160 MEDIA_DEBUG_LOG("MetadataOutput::AddMetadataObjectTypes public calling for metadataOutput");
161 if (metadataObjectTypes.size() > maxSize4NonSystemApp ||
162 std::any_of(metadataObjectTypes.begin(), metadataObjectTypes.end(),
163 [](MetadataObjectType type) { return type != MetadataObjectType::FACE; })) {
164 return CameraErrorCode::INVALID_ARGUMENT;
165 }
166 }
167 auto session = GetSession();
168 CHECK_RETURN_RET_ELOG(session == nullptr || !session->IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
169 "MetadataOutput Failed to AddMetadataObjectTypes!, session not commited");
170
171 auto inputDevice = session->GetInputDevice();
172 CHECK_RETURN_RET_ELOG(inputDevice == nullptr, CameraErrorCode::SESSION_NOT_CONFIG,
173 "MetadataOutput Failed to AddMetadataObjectTypes!, inputDevice is null");
174
175 sptr<CameraDevice> cameraObj = inputDevice->GetCameraDeviceInfo();
176 CHECK_RETURN_RET_ELOG(cameraObj == nullptr, CameraErrorCode::SESSION_NOT_CONFIG,
177 "MetadataOutput Failed to AddMetadataObjectTypes!, cameraObj is null");
178
179 auto outoputCapability = CameraManager::GetInstance()->GetSupportedOutputCapability(cameraObj, session->GetMode());
180 CHECK_RETURN_RET_ELOG(outoputCapability == nullptr, CameraErrorCode::SESSION_NOT_CONFIG,
181 "MetadataOutput Failed to AddMetadataObjectTypes!, outoputCapability is null");
182
183 std::vector<MetadataObjectType> supportMetadataType = outoputCapability->GetSupportedMetadataObjectType();
184 for (const auto &type : supportMetadataType) {
185 MEDIA_DEBUG_LOG("MetadataOutput::AddMetadataObjectTypes, support type: %{public}d", type);
186 }
187 CHECK_RETURN_RET_ELOG(!checkValidType(metadataObjectTypes, supportMetadataType), CameraErrorCode::INVALID_ARGUMENT,
188 "MetadataOutput::AddMetadataObjectTypes, unsupported type!");
189
190 auto stream = GetStream();
191 CHECK_RETURN_RET_ELOG(stream == nullptr, CameraErrorCode::SESSION_NOT_CONFIG,
192 "MetadataOutput Failed to AddMetadataObjectTypes!, GetStream is nullptr");
193 std::vector<int32_t> numberOfTypes = convert(metadataObjectTypes);
194 int32_t errCode = static_cast<IStreamMetadata *>(stream.GetRefPtr())->EnableMetadataType(numberOfTypes);
195 CHECK_RETURN_RET_ELOG(
196 errCode != CAMERA_OK, CameraErrorCode::SERVICE_FATL_ERROR,
197 "MetadataOutput Failed to AddMetadataObjectTypes!, EnableMetadataType failed ret: %{public}d", errCode);
198 return CameraErrorCode::SUCCESS;
199 // LCOV_EXCL_STOP
200 }
201
RemoveMetadataObjectTypes(std::vector<MetadataObjectType> metadataObjectTypes)202 int32_t MetadataOutput::RemoveMetadataObjectTypes(std::vector<MetadataObjectType> metadataObjectTypes)
203 {
204 const size_t maxSize4NonSystemApp = 1;
205 // LCOV_EXCL_START
206 if (!CameraSecurity::CheckSystemApp()) {
207 MEDIA_DEBUG_LOG("MetadataOutput::RemoveMetadataObjectTypes public calling for metadataOutput");
208 if (metadataObjectTypes.size() > maxSize4NonSystemApp ||
209 std::any_of(metadataObjectTypes.begin(), metadataObjectTypes.end(),
210 [](MetadataObjectType type) { return type != MetadataObjectType::FACE; })) {
211 return CameraErrorCode::INVALID_ARGUMENT;
212 }
213 }
214 auto session = GetSession();
215 CHECK_RETURN_RET_ELOG(session == nullptr || !session->IsSessionCommited(), CameraErrorCode::SESSION_NOT_CONFIG,
216 "MetadataOutput Failed to RemoveMetadataObjectTypes!, session not commited");
217
218 auto stream = GetStream();
219 CHECK_RETURN_RET_ELOG(stream == nullptr, CameraErrorCode::SESSION_NOT_CONFIG,
220 "MetadataOutput Failed to AddMetadataObjectTypes!, GetStream is nullptr");
221
222 std::vector<int32_t> numberOfTypes = convert(metadataObjectTypes);
223 int32_t errCode = static_cast<IStreamMetadata *>(stream.GetRefPtr())->DisableMetadataType(numberOfTypes);
224 CHECK_RETURN_RET_ELOG(
225 errCode != CAMERA_OK, CameraErrorCode::SERVICE_FATL_ERROR,
226 "MetadataOutput Failed to AddMetadataObjectTypes!, EnableMetadataType failed ret: %{public}d", errCode);
227 return CameraErrorCode::SUCCESS;
228 // LCOV_EXCL_STOP
229 }
230
checkValidType(const std::vector<MetadataObjectType> & typeAdded,const std::vector<MetadataObjectType> & supportedType)231 bool MetadataOutput::checkValidType(const std::vector<MetadataObjectType> &typeAdded,
232 const std::vector<MetadataObjectType> &supportedType)
233 {
234 // LCOV_EXCL_START
235 return std::all_of(typeAdded.begin(), typeAdded.end(), [&supportedType](MetadataObjectType type) {
236 return std::find(supportedType.begin(), supportedType.end(), type) != supportedType.end();
237 });
238 // LCOV_EXCL_STOP
239 }
240
convert(const std::vector<MetadataObjectType> & typesOfMetadata)241 std::vector<int32_t> MetadataOutput::convert(const std::vector<MetadataObjectType> &typesOfMetadata)
242 {
243 // LCOV_EXCL_START
244 std::vector<int32_t> result(typesOfMetadata.size());
245 std::transform(typesOfMetadata.begin(), typesOfMetadata.end(), result.begin(),
246 [](MetadataObjectType obj) { return static_cast<int32_t>(obj); });
247 return result;
248 // LCOV_EXCL_STOP
249 }
250
SetCallback(std::shared_ptr<MetadataObjectCallback> metadataObjectCallback)251 void MetadataOutput::SetCallback(std::shared_ptr<MetadataObjectCallback> metadataObjectCallback)
252 {
253 // LCOV_EXCL_START
254 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
255 appObjectCallback_ = metadataObjectCallback;
256 if (appObjectCallback_ != nullptr) {
257 if (cameraMetadataCallback_ == nullptr) {
258 cameraMetadataCallback_ = new HStreamMetadataCallbackImpl(this);
259 }
260 }
261 auto stream = GetStream();
262 sptr<IStreamMetadata> itemStream = static_cast<IStreamMetadata*>(stream.GetRefPtr());
263 int32_t errorCode = CAMERA_OK;
264 if (itemStream) {
265 errorCode = itemStream->SetCallback(cameraMetadataCallback_);
266 } else {
267 MEDIA_ERR_LOG("MetadataOutput::SetCallback() itemStream is nullptr");
268 }
269 CHECK_RETURN(errorCode == CAMERA_OK);
270 MEDIA_ERR_LOG("MetadataOutput::SetCallback(): Failed to register callback, errorCode: %{public}d", errorCode);
271 cameraMetadataCallback_ = nullptr;
272 appObjectCallback_ = nullptr;
273 // LCOV_EXCL_STOP
274 }
275
SetCallback(std::shared_ptr<MetadataStateCallback> metadataStateCallback)276 void MetadataOutput::SetCallback(std::shared_ptr<MetadataStateCallback> metadataStateCallback)
277 {
278 // LCOV_EXCL_START
279 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
280 appStateCallback_ = metadataStateCallback;
281 // LCOV_EXCL_STOP
282 }
283
CreateStream()284 int32_t MetadataOutput::CreateStream()
285 {
286 return CameraErrorCode::SUCCESS;
287 }
288
Start()289 int32_t MetadataOutput::Start()
290 {
291 // LCOV_EXCL_START
292 MEDIA_DEBUG_LOG("MetadataOutput::Start is called");
293 auto session = GetSession();
294 CHECK_RETURN_RET_ELOG(session == nullptr || !session->IsSessionCommited(), CameraErrorCode::SUCCESS,
295 "MetadataOutput Failed to Start!, session not commited");
296 auto stream = GetStream();
297 CHECK_RETURN_RET_ELOG(
298 stream == nullptr, CameraErrorCode::SUCCESS, "MetadataOutput Failed to Start!, GetStream is nullptr");
299 int32_t errCode = static_cast<IStreamMetadata *>(stream.GetRefPtr())->Start();
300 CHECK_PRINT_ELOG(errCode != CAMERA_OK, "Failed to Start MetadataOutput!, errCode: %{public}d", errCode);
301 return CameraErrorCode::SUCCESS;
302 // LCOV_EXCL_STOP
303 }
304
CameraServerDied(pid_t pid)305 void MetadataOutput::CameraServerDied(pid_t pid)
306 {
307 // LCOV_EXCL_START
308 MEDIA_ERR_LOG("camera server has died, pid:%{public}d!", pid);
309 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
310 if (appStateCallback_ != nullptr) {
311 MEDIA_DEBUG_LOG("appCallback not nullptr");
312 int32_t serviceErrorType = ServiceToCameraError(CAMERA_INVALID_STATE);
313 appStateCallback_->OnError(serviceErrorType);
314 }
315 // LCOV_EXCL_STOP
316 }
317
Stop()318 int32_t MetadataOutput::Stop()
319 {
320 // LCOV_EXCL_START
321 MEDIA_DEBUG_LOG("MetadataOutput::Stop");
322 auto stream = GetStream();
323 CHECK_RETURN_RET_ELOG(stream == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
324 "MetadataOutput Failed to Stop!, GetStream is nullptr");
325 int32_t errCode = static_cast<IStreamMetadata*>(stream.GetRefPtr())->Stop();
326 CHECK_PRINT_ELOG(errCode != CAMERA_OK, "Failed to Stop MetadataOutput!, errCode: %{public}d", errCode);
327 return ServiceToCameraError(errCode);
328 // LCOV_EXCL_STOP
329 }
330
Release()331 int32_t MetadataOutput::Release()
332 {
333 // LCOV_EXCL_START
334 {
335 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
336 appObjectCallback_ = nullptr;
337 appStateCallback_ = nullptr;
338 cameraMetadataCallback_ = nullptr;
339 }
340 auto stream = GetStream();
341 CHECK_RETURN_RET_ELOG(stream == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
342 "MetadataOutput Failed to Release!, GetStream is nullptr");
343 int32_t errCode = static_cast<IStreamMetadata *>(stream.GetRefPtr())->Release();
344 CHECK_PRINT_ELOG(errCode != CAMERA_OK, "Failed to Release MetadataOutput!, errCode: %{public}d", errCode);
345 ReleaseSurface();
346 CaptureOutput::Release();
347 return ServiceToCameraError(errCode);
348 // LCOV_EXCL_STOP
349 }
350
ReleaseSurface()351 void MetadataOutput::ReleaseSurface()
352 {
353 std::lock_guard<std::mutex> lock(surfaceMutex_);
354 if (surface_ != nullptr) {
355 SurfaceError ret = surface_->UnregisterConsumerListener();
356 CHECK_PRINT_ELOG(ret != SURFACE_ERROR_OK, "Failed to unregister surface consumer listener");
357 surface_ = nullptr;
358 }
359 }
360
GetSurface()361 sptr<IConsumerSurface> MetadataOutput::GetSurface()
362 {
363 std::lock_guard<std::mutex> lock(surfaceMutex_);
364 return surface_;
365 }
366
ProcessMetadata(const int32_t streamId,const std::shared_ptr<OHOS::Camera::CameraMetadata> & result,std::vector<sptr<MetadataObject>> & metaObjects,bool isNeedMirror,bool isNeedFlip)367 void MetadataOutput::ProcessMetadata(const int32_t streamId,
368 const std::shared_ptr<OHOS::Camera::CameraMetadata> &result,
369 std::vector<sptr<MetadataObject>> &metaObjects, bool isNeedMirror, bool isNeedFlip)
370 {
371 bool ret = MetadataCommonUtils::ProcessMetaObjects(streamId, result, metaObjects, isNeedMirror,
372 isNeedFlip, RectBoxType::RECT_CAMERA);
373 // LCOV_EXCL_START
374 if (ret) {
375 reportFaceResults_ = true;
376 return;
377 }
378 reportLastFaceResults_ = false;
379 if (reportFaceResults_) {
380 reportLastFaceResults_ = true;
381 reportFaceResults_ = false;
382 }
383 // LCOV_EXCL_STOP
384 MEDIA_ERR_LOG("Camera not ProcessFaceRectangles");
385 return;
386 }
387
MetadataObjectListener(sptr<MetadataOutput> metadata)388 MetadataObjectListener::MetadataObjectListener(sptr<MetadataOutput> metadata) : metadata_(metadata) {}
389
ProcessMetadataBuffer(void * buffer,int64_t timestamp)390 int32_t MetadataObjectListener::ProcessMetadataBuffer(void *buffer, int64_t timestamp)
391 {
392 return CameraErrorCode::SUCCESS;
393 }
394
OnBufferAvailable()395 void MetadataObjectListener::OnBufferAvailable()
396 {
397 // LCOV_EXCL_START
398 MEDIA_INFO_LOG("MetadataOutput::OnBufferAvailable() is Called");
399 // metaoutput adapte later
400 bool adapterLater = true;
401 CHECK_RETURN(adapterLater);
402 auto metadataOutput = metadata_.promote();
403 CHECK_RETURN_ELOG(metadataOutput == nullptr, "OnBufferAvailable metadataOutput is null");
404 auto surface = metadataOutput->GetSurface();
405 CHECK_RETURN_ELOG(surface == nullptr, "OnBufferAvailable Metadata surface is null");
406 int32_t fence = -1;
407 int64_t timestamp;
408 OHOS::Rect damage;
409 sptr<SurfaceBuffer> buffer = nullptr;
410 SurfaceError surfaceRet = surface->AcquireBuffer(buffer, fence, timestamp, damage);
411 CHECK_RETURN_ELOG(surfaceRet != SURFACE_ERROR_OK, "OnBufferAvailable Failed to acquire surface buffer");
412 int32_t ret = ProcessMetadataBuffer(buffer->GetVirAddr(), timestamp);
413 if (ret) {
414 std::shared_ptr<MetadataStateCallback> appStateCallback = metadataOutput->GetAppStateCallback();
415 CHECK_EXECUTE(appStateCallback, appStateCallback->OnError(ret));
416 }
417 surface->ReleaseBuffer(buffer, -1);
418 // LCOV_EXCL_STOP
419 }
420
OnMetadataResult(const int32_t streamId,const std::shared_ptr<OHOS::Camera::CameraMetadata> & result)421 int32_t HStreamMetadataCallbackImpl::OnMetadataResult(const int32_t streamId,
422 const std::shared_ptr<OHOS::Camera::CameraMetadata> &result)
423 {
424 // LCOV_EXCL_START
425 auto metadataOutput = GetMetadataOutput();
426 CHECK_RETURN_RET_ELOG(metadataOutput == nullptr, CAMERA_OK,
427 "HStreamMetadataCallbackImpl::OnMetadataResult metadataOutput is nullptr");
428 auto session = metadataOutput->GetSession();
429 CHECK_RETURN_RET_ELOG(session == nullptr, SESSION_NOT_RUNNING,
430 "HStreamMetadataCallbackImpl OnMetadataResult error!, session is nullptr");
431 auto inputDevice = session->GetInputDevice();
432 bool isNeedMirror = false;
433 bool isNeedFlip = false;
434 if (inputDevice) {
435 auto inputDeviceInfo = inputDevice->GetCameraDeviceInfo();
436 isNeedMirror = (inputDeviceInfo->GetPosition() == CAMERA_POSITION_FRONT ||
437 inputDeviceInfo->GetPosition() == CAMERA_POSITION_FOLD_INNER);
438 isNeedFlip = inputDeviceInfo->GetUsedAsPosition() == CAMERA_POSITION_FRONT;
439 }
440 std::vector<sptr<MetadataObject>> metaObjects;
441 metadataOutput->ProcessMetadata(streamId, result, metaObjects, isNeedMirror, isNeedFlip);
442 auto objectCallback = metadataOutput->GetAppObjectCallback();
443 CHECK_RETURN_RET(objectCallback == nullptr, INVALID_ARGUMENT);
444 CHECK_EXECUTE((metadataOutput->reportFaceResults_ || metadataOutput->reportLastFaceResults_) && objectCallback,
445 objectCallback->OnMetadataObjectsAvailable(metaObjects));
446 return SUCCESS;
447 // LCOV_EXCL_STOP
448 }
449
MetadataObjectFactory()450 MetadataObjectFactory::MetadataObjectFactory() {}
451
GetInstance()452 sptr<MetadataObjectFactory> &MetadataObjectFactory::GetInstance()
453 {
454 if (metaFactoryInstance_ == nullptr) {
455 std::lock_guard<std::mutex> lock(instanceMutex_);
456 if (metaFactoryInstance_ == nullptr) {
457 MEDIA_INFO_LOG("Initializing MetadataObjectFactory instance");
458 metaFactoryInstance_ = new MetadataObjectFactory();
459 }
460 }
461 return metaFactoryInstance_;
462 }
463
createMetadataObject(MetadataObjectType type)464 sptr<MetadataObject> MetadataObjectFactory::createMetadataObject(MetadataObjectType type)
465 {
466 MetaObjectParms baseMetaParms = { type_, timestamp_, box_, objectId_, confidence_ };
467 sptr<MetadataObject> metadataObject;
468 // LCOV_EXCL_START
469 switch (type) {
470 case MetadataObjectType::FACE:
471 metadataObject = new MetadataFaceObject(baseMetaParms, leftEyeBoundingBox_, rightEyeBoundingBox_, emotion_,
472 emotionConfidence_, pitchAngle_, yawAngle_, rollAngle_);
473 break;
474 case MetadataObjectType::HUMAN_BODY:
475 metadataObject = new MetadataHumanBodyObject(baseMetaParms);
476 break;
477 case MetadataObjectType::CAT_FACE:
478 metadataObject = new MetadataCatFaceObject(baseMetaParms, leftEyeBoundingBox_, rightEyeBoundingBox_);
479 break;
480 case MetadataObjectType::CAT_BODY:
481 metadataObject = new MetadataCatBodyObject(baseMetaParms);
482 break;
483 case MetadataObjectType::DOG_FACE:
484 metadataObject = new MetadataDogFaceObject(baseMetaParms, leftEyeBoundingBox_, rightEyeBoundingBox_);
485 break;
486 case MetadataObjectType::DOG_BODY:
487 metadataObject = new MetadataDogBodyObject(baseMetaParms);
488 break;
489 case MetadataObjectType::SALIENT_DETECTION:
490 metadataObject = new MetadataSalientDetectionObject(baseMetaParms);
491 break;
492 case MetadataObjectType::BAR_CODE_DETECTION:
493 metadataObject = new MetadataBarCodeDetectionObject(baseMetaParms);
494 break;
495 default:
496 metadataObject = new MetadataObject(baseMetaParms);
497 }
498 // LCOV_EXCL_STOP
499 ResetParameters();
500 return metadataObject;
501 }
502
ResetParameters()503 void MetadataObjectFactory::ResetParameters()
504 {
505 type_ = MetadataObjectType::INVALID;
506 timestamp_ = 0;
507 box_ = { 0, 0, 0, 0 };
508 objectId_ = 0;
509 confidence_ = 0.0f;
510 leftEyeBoundingBox_ = { 0, 0, 0, 0 };
511 rightEyeBoundingBox_ = { 0, 0, 0, 0 };
512 emotion_ = Emotion::NEUTRAL;
513 emotionConfidence_ = 0;
514 pitchAngle_ = 0;
515 yawAngle_ = 0;
516 rollAngle_ = 0;
517 }
518 } // namespace CameraStandard
519 } // namespace OHOS
520