1 /*
2  * Copyright (c) 2024-2024 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 "camera_napi_object_types.h"
17 
18 #include <cstdint>
19 #include <memory>
20 #include <utility>
21 
22 #include "camera_device.h"
23 #include "camera_manager.h"
24 #include "camera_napi_metadata_utils.h"
25 #include "camera_napi_object.h"
26 #include "js_native_api_types.h"
27 #include "metadata_output.h"
28 
29 namespace OHOS {
30 namespace CameraStandard {
31 
GenerateNapiValue(napi_env env)32 napi_value CameraNapiObjectTypes::GenerateNapiValue(napi_env env)
33 {
34     napi_value value = GetCameraNapiObject().CreateNapiObjFromMap(env);
35     ptrHolder_.clear();
36     return value;
37 };
38 
GetCameraNapiObject()39 CameraNapiObject& CameraNapiObjSize::GetCameraNapiObject()
40 {
41     return *Hold<CameraNapiObject>(
42         CameraNapiObject::CameraNapiObjFieldMap { { "width", &size_.width }, { "height", &size_.height } });
43 }
44 
GetCameraNapiObject()45 CameraNapiObject& CameraNapiObjFrameRateRange::GetCameraNapiObject()
46 {
47     return *Hold<CameraNapiObject>(
48         CameraNapiObject::CameraNapiObjFieldMap { { "min", &frameRateRange_[0] }, { "max", &frameRateRange_[1] } });
49 }
50 
GetCameraNapiObject()51 CameraNapiObject& CameraNapiObjProfile::GetCameraNapiObject()
52 {
53     auto sizeObj = Hold<CameraNapiObjSize>(profile_.size_);
54     auto format = Hold<int32_t>(profile_.format_);
55     return *Hold<CameraNapiObject>(
56         CameraNapiObject::CameraNapiObjFieldMap { { "format", format }, { "size", &sizeObj->GetCameraNapiObject() } });
57 }
58 
GetCameraNapiObject()59 CameraNapiObject& CameraNapiObjVideoProfile::GetCameraNapiObject()
60 {
61     auto format = Hold<int32_t>(videoProfile_.format_);
62     auto sizeObj = Hold<CameraNapiObjSize>(videoProfile_.size_);
63     auto frameRateRange = Hold<CameraNapiObjFrameRateRange>(videoProfile_.framerates_);
64     return *Hold<CameraNapiObject>(CameraNapiObject::CameraNapiObjFieldMap {
65         { "format", format },
66         { "size", &sizeObj->GetCameraNapiObject() },
67         { "frameRateRange", &frameRateRange->GetCameraNapiObject() } });
68 }
69 
GetCameraNapiObject()70 CameraNapiObject& CameraNapiObjDepthProfile::GetCameraNapiObject()
71 {
72     auto format = Hold<int32_t>(depthProfile_.format_);
73     auto sizeObj = Hold<CameraNapiObjSize>(depthProfile_.size_);
74     auto dataAccuracy = Hold<int32_t>(depthProfile_.dataAccuracy_);
75     return *Hold<CameraNapiObject>(CameraNapiObject::CameraNapiObjFieldMap {
76         { "format", format },
77         { "size", &sizeObj->GetCameraNapiObject() },
78         { "dataAccuracy", dataAccuracy } });
79 }
80 
GetCameraNapiObject()81 CameraNapiObject& CameraNapiObjCameraDevice::GetCameraNapiObject()
82 {
83     auto cameraId = Hold<std::string>(cameraDevice_.GetID());
84     auto cameraPosition = Hold<int32_t>(cameraDevice_.GetPosition());
85     auto cameraType = Hold<int32_t>(cameraDevice_.GetCameraType());
86     auto connectionType = Hold<int32_t>(cameraDevice_.GetConnectionType());
87     auto hostDeviceName = Hold<std::string>(cameraDevice_.GetHostName());
88     auto hostDeviceType = Hold<uint32_t>(cameraDevice_.GetDeviceType());
89     auto cameraOrientation = Hold<int32_t>(cameraDevice_.GetCameraOrientation());
90     return *Hold<CameraNapiObject>(CameraNapiObject::CameraNapiObjFieldMap {
91         { "cameraId", cameraId },
92         { "cameraPosition", cameraPosition },
93         { "cameraType", cameraType },
94         { "connectionType", connectionType },
95         { "hostDeviceName", hostDeviceName },
96         { "hostDeviceType", hostDeviceType },
97         { "cameraOrientation", cameraOrientation } });
98 }
99 
GetCameraNapiObject()100 CameraNapiObject& CameraNapiBoundingBox::GetCameraNapiObject()
101 {
102     return *Hold<CameraNapiObject>(CameraNapiObject::CameraNapiObjFieldMap {
103         { "topLeftX", &rect_.topLeftX },
104         { "topLeftY", &rect_.topLeftY },
105         { "width", &rect_.width },
106         { "height", &rect_.height } });
107 }
108 
GetCameraNapiObject()109 CameraNapiObject& CameraNapiObjMetadataObject::GetCameraNapiObject()
110 {
111     auto type = Hold<int32_t>(CameraNapiMetadataUtils::MapMetadataObjSupportedTypesEnum(metadataObject_.GetType()));
112     auto timestamp = Hold<int32_t>(metadataObject_.GetTimestamp());
113     auto boundingBox = Hold<CameraNapiBoundingBox>(*Hold<Rect>(metadataObject_.GetBoundingBox()));
114     auto objectId = Hold<int32_t>(metadataObject_.GetObjectId());
115     auto confidence = Hold<int32_t>(metadataObject_.GetConfidence());
116     return *Hold<CameraNapiObject>(CameraNapiObject::CameraNapiObjFieldMap {
117         { "type", type },
118         { "timestamp", timestamp },
119         { "boundingBox", &boundingBox->GetCameraNapiObject() },
120         { "objectId", objectId },
121         { "confidence", confidence }});
122 }
123 
GetCameraNapiObject()124 CameraNapiObject& CameraNapiObjCameraOutputCapability::GetCameraNapiObject()
125 {
126     auto previewProfiles = Hold<std::list<CameraNapiObject>>();
127     auto nativePreviewProfiles = Hold<std::vector<Profile>>(cameraOutputCapability_.GetPreviewProfiles());
128     for (auto& profile : *nativePreviewProfiles) {
129         previewProfiles->emplace_back(std::move(Hold<CameraNapiObjProfile>(profile)->GetCameraNapiObject()));
130     }
131 
132     auto photoProfiles = Hold<std::list<CameraNapiObject>>();
133     auto nativePhotoProfiles = Hold<std::vector<Profile>>(cameraOutputCapability_.GetPhotoProfiles());
134     for (auto& profile : *nativePhotoProfiles) {
135         photoProfiles->emplace_back(std::move(Hold<CameraNapiObjProfile>(profile)->GetCameraNapiObject()));
136     }
137 
138     auto videoProfiles = Hold<std::list<CameraNapiObject>>();
139     auto nativeVideoProfiles = Hold<std::vector<VideoProfile>>(cameraOutputCapability_.GetVideoProfiles());
140     for (auto& profile : *nativeVideoProfiles) {
141         videoProfiles->emplace_back(std::move(Hold<CameraNapiObjVideoProfile>(profile)->GetCameraNapiObject()));
142     }
143 
144     auto depthProfiles = Hold<std::list<CameraNapiObject>>();
145     auto nativeDepthProfiles = Hold<std::vector<DepthProfile>>(cameraOutputCapability_.GetDepthProfiles());
146     for (auto& profile : *nativeDepthProfiles) {
147         depthProfiles->emplace_back(std::move(Hold<CameraNapiObjDepthProfile>(profile)->GetCameraNapiObject()));
148     }
149 
150     auto supportedMetadataObjectTypes = Hold<std::vector<int32_t>>();
151     auto nativeSupportedMetadataObjectTypes = cameraOutputCapability_.GetSupportedMetadataObjectType();
152     for (auto& type : nativeSupportedMetadataObjectTypes) {
153         supportedMetadataObjectTypes->emplace_back(static_cast<int32_t>(type));
154     }
155 
156     return *Hold<CameraNapiObject>(CameraNapiObject::CameraNapiObjFieldMap {
157         { "previewProfiles", previewProfiles },
158         { "photoProfiles", photoProfiles },
159         { "videoProfiles", videoProfiles },
160         { "depthProfiles", depthProfiles },
161         { "supportedMetadataObjectTypes", supportedMetadataObjectTypes } });
162 }
163 } // namespace CameraStandard
164 } // namespace OHOS
165