• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <set>
19 
20 #include "camera_util.h"
21 #include "camera_log.h"
22 #include "input/camera_input.h"
23 #include "session/capture_session.h"
24 
25 namespace OHOS {
26 namespace CameraStandard {
MetadataFaceObject(double timestamp,Rect rect)27 MetadataFaceObject::MetadataFaceObject(double timestamp, Rect rect)
28     : MetadataObject(MetadataObjectType::FACE, timestamp, rect)
29 {}
30 
MetadataObject(MetadataObjectType type,double timestamp,Rect rect)31 MetadataObject::MetadataObject(MetadataObjectType type, double timestamp, Rect rect)
32     : type_(type), timestamp_(timestamp), box_(rect)
33 {}
34 
GetType()35 MetadataObjectType MetadataObject::GetType()
36 {
37     return type_;
38 }
GetTimestamp()39 double MetadataObject::GetTimestamp()
40 {
41     return timestamp_;
42 }
GetBoundingBox()43 Rect MetadataObject::GetBoundingBox()
44 {
45     return box_;
46 }
47 
MetadataOutput(sptr<Surface> surface,sptr<IStreamMetadata> & streamMetadata)48 MetadataOutput::MetadataOutput(sptr<Surface> surface, sptr<IStreamMetadata> &streamMetadata)
49     : CaptureOutput(CAPTURE_OUTPUT_TYPE_METADATA, StreamType::METADATA, streamMetadata)
50 {
51     surface_ = surface;
52 }
53 
~MetadataOutput()54 MetadataOutput::~MetadataOutput()
55 {
56     if (surface_) {
57         SurfaceError ret = surface_->UnregisterConsumerListener();
58         if (ret != SURFACE_ERROR_OK) {
59             MEDIA_ERR_LOG("Failed to unregister surface consumer listener");
60         }
61         surface_ = nullptr;
62     }
63     appObjectCallback_ = nullptr;
64     appStateCallback_ = nullptr;
65 }
66 
GetSupportedMetadataObjectTypes()67 std::vector<MetadataObjectType> MetadataOutput::GetSupportedMetadataObjectTypes()
68 {
69     CaptureSession* captureSession = GetSession();
70     if ((captureSession == nullptr) || (captureSession->inputDevice_ == nullptr)) {
71         return {};
72     }
73     sptr<CameraDevice> cameraObj = captureSession->inputDevice_->GetCameraDeviceInfo();
74     std::shared_ptr<Camera::CameraMetadata> metadata = cameraObj->GetMetadata();
75     camera_metadata_item_t item;
76     int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_STATISTICS_FACE_DETECT_MODE, &item);
77     if (ret) {
78         return {};
79     }
80     std::vector<MetadataObjectType> objectTypes;
81     for (size_t index = 0; index < item.count; index++) {
82         if (item.data.u8[index] == OHOS_CAMERA_FACE_DETECT_MODE_SIMPLE) {
83             objectTypes.emplace_back(MetadataObjectType::FACE);
84         }
85     }
86     return objectTypes;
87 }
88 
SetCapturingMetadataObjectTypes(std::vector<MetadataObjectType> metadataObjectTypes)89 void MetadataOutput::SetCapturingMetadataObjectTypes(std::vector<MetadataObjectType> metadataObjectTypes)
90 {
91     CaptureSession* captureSession = GetSession();
92     if ((captureSession == nullptr) || (captureSession->inputDevice_ == nullptr)) {
93         return;
94     }
95     std::set<camera_face_detect_mode_t> objectTypes;
96     for (const auto &type : metadataObjectTypes) {
97         if (type == MetadataObjectType::FACE) {
98             objectTypes.insert(OHOS_CAMERA_FACE_DETECT_MODE_SIMPLE);
99         }
100     }
101     if (objectTypes.empty()) {
102         objectTypes.insert(OHOS_CAMERA_FACE_DETECT_MODE_OFF);
103     }
104 
105     captureSession->SetCaptureMetadataObjectTypes(objectTypes);
106 }
107 
SetCallback(std::shared_ptr<MetadataObjectCallback> metadataObjectCallback)108 void MetadataOutput::SetCallback(std::shared_ptr<MetadataObjectCallback> metadataObjectCallback)
109 {
110     appObjectCallback_ = metadataObjectCallback;
111 }
112 
SetCallback(std::shared_ptr<MetadataStateCallback> metadataStateCallback)113 void MetadataOutput::SetCallback(std::shared_ptr<MetadataStateCallback> metadataStateCallback)
114 {
115     appStateCallback_ = metadataStateCallback;
116 }
117 
Start()118 int32_t MetadataOutput::Start()
119 {
120     CaptureSession* captureSession = GetSession();
121     if (captureSession == nullptr || !captureSession->IsSessionCommited()) {
122         MEDIA_ERR_LOG("MetadataOutput Failed to Start!, session not config");
123         return CameraErrorCode::SESSION_NOT_CONFIG;
124     }
125     if (GetStream() == nullptr) {
126         MEDIA_ERR_LOG("MetadataOutput Failed to Start!, GetStream is nullptr");
127         return CameraErrorCode::SERVICE_FATL_ERROR;
128     }
129     int32_t errCode = static_cast<IStreamMetadata *>(GetStream().GetRefPtr())->Start();
130     if (errCode != CAMERA_OK) {
131         MEDIA_ERR_LOG("Failed to Start MetadataOutput!, errCode: %{public}d", errCode);
132     }
133     return ServiceToCameraError(errCode);
134 }
135 
Stop()136 int32_t MetadataOutput::Stop()
137 {
138     if (GetStream() == nullptr) {
139         MEDIA_ERR_LOG("MetadataOutput Failed to Stop!, GetStream is nullptr");
140         return CameraErrorCode::SERVICE_FATL_ERROR;
141     }
142     int32_t errCode = static_cast<IStreamMetadata *>(GetStream().GetRefPtr())->Stop();
143     if (errCode != CAMERA_OK) {
144         MEDIA_ERR_LOG("Failed to Stop MetadataOutput!, errCode: %{public}d", errCode);
145     }
146     return ServiceToCameraError(errCode);
147 }
148 
Release()149 int32_t MetadataOutput::Release()
150 {
151     if (GetStream() == nullptr) {
152         MEDIA_ERR_LOG("MetadataOutput Failed to Release!, GetStream is nullptr");
153         return CameraErrorCode::SERVICE_FATL_ERROR;
154     }
155     int32_t errCode = static_cast<IStreamMetadata *>(GetStream().GetRefPtr())->Release();
156     if (errCode != CAMERA_OK) {
157         MEDIA_ERR_LOG("Failed to release MetadataOutput!, errCode: %{public}d", errCode);
158     }
159     if (surface_) {
160         SurfaceError ret = surface_->UnregisterConsumerListener();
161         if (ret != SURFACE_ERROR_OK) {
162             MEDIA_ERR_LOG("Failed to unregister surface consumer listener");
163         }
164         surface_ = nullptr;
165     }
166     appObjectCallback_ = nullptr;
167     appStateCallback_ = nullptr;
168     CaptureOutput::Release();
169     return ServiceToCameraError(errCode);
170 }
171 
MetadataObjectListener(sptr<MetadataOutput> metadata)172 MetadataObjectListener::MetadataObjectListener(sptr<MetadataOutput> metadata) : metadata_(metadata)
173 {}
174 
ProcessFaceRectangles(int64_t timestamp,const camera_metadata_item_t & metadataItem,std::vector<sptr<MetadataObject>> & metaObjects)175 int32_t MetadataObjectListener::ProcessFaceRectangles(int64_t timestamp, const camera_metadata_item_t &metadataItem,
176                                                       std::vector<sptr<MetadataObject>> &metaObjects)
177 {
178     constexpr int32_t rectangleUnitLen = 4;
179 
180     if (metadataItem.count % rectangleUnitLen) {
181         MEDIA_ERR_LOG("Metadata item: %{public}d count: %{public}d is invalid", metadataItem.item, metadataItem.count);
182         return ERROR_UNKNOWN;
183     }
184     metaObjects.reserve(metadataItem.count / rectangleUnitLen);
185     float* start = metadataItem.data.f;
186     float* end = metadataItem.data.f + metadataItem.count;
187     for (; start < end; start += rectangleUnitLen) {
188         sptr<MetadataObject> metadataObject = new(std::nothrow) MetadataFaceObject(timestamp,
189             (Rect) {start[0], start[1], start[2], start[3]});
190         if (!metadataObject) {
191             MEDIA_ERR_LOG("Failed to allocate MetadataFaceObject");
192             return ERROR_INSUFFICIENT_RESOURCES;
193         }
194         metaObjects.emplace_back(metadataObject);
195     }
196     return CAMERA_OK;
197 }
198 
ProcessMetadataBuffer(void * buffer,int64_t timestamp)199 int32_t MetadataObjectListener::ProcessMetadataBuffer(void* buffer, int64_t timestamp)
200 {
201     if (!buffer) {
202         MEDIA_ERR_LOG("Buffer is null");
203         return ERROR_UNKNOWN;
204     }
205     common_metadata_header_t* metadata = (common_metadata_header_t *)buffer;
206     uint32_t itemCount = Camera::GetCameraMetadataItemCount(metadata);
207     camera_metadata_item_t metadataItem;
208     std::vector<sptr<MetadataObject>> metaObjects;
209     for (uint32_t i = 0; i < itemCount; i++) {
210         int32_t ret = Camera::GetCameraMetadataItem(metadata, i, &metadataItem);
211         if (ret) {
212             MEDIA_ERR_LOG("Failed to get metadata item at index: %{public}d, with return code: %{public}d", i, ret);
213             return ERROR_UNKNOWN;
214         }
215         if (metadataItem.item == OHOS_STATISTICS_FACE_RECTANGLES) {
216             ret = ProcessFaceRectangles(timestamp, metadataItem, metaObjects);
217             if (ret) {
218                 MEDIA_ERR_LOG("Failed to process face rectangles");
219                 return ret;
220             }
221         }
222     }
223     std::shared_ptr<MetadataObjectCallback> appObjectCallback = metadata_->appObjectCallback_;
224     if (!metaObjects.empty() && appObjectCallback) {
225         appObjectCallback->OnMetadataObjectsAvailable(metaObjects);
226     }
227     return CAMERA_OK;
228 }
229 
OnBufferAvailable()230 void MetadataObjectListener::OnBufferAvailable()
231 {
232     if (!metadata_) {
233         MEDIA_ERR_LOG("Metadata is null");
234         return;
235     }
236     sptr<Surface> surface = metadata_->surface_;
237     if (!surface) {
238         MEDIA_ERR_LOG("Metadata surface is null");
239         return;
240     }
241     int32_t fence = -1;
242     int64_t timestamp;
243     OHOS::Rect damage;
244     sptr<SurfaceBuffer> buffer = nullptr;
245     SurfaceError surfaceRet = surface->AcquireBuffer(buffer, fence, timestamp, damage);
246     if (surfaceRet != SURFACE_ERROR_OK) {
247         MEDIA_ERR_LOG("Failed to acquire surface buffer");
248         return;
249     }
250     int32_t ret = ProcessMetadataBuffer(buffer->GetVirAddr(), timestamp);
251     if (ret) {
252         std::shared_ptr<MetadataStateCallback> appStateCallback = metadata_->appStateCallback_;
253         if (appStateCallback) {
254             appStateCallback->OnError(ret);
255         }
256     }
257 
258     surface->ReleaseBuffer(buffer, -1);
259 }
260 } // CameraStandard
261 } // OHOS
262