• 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 "camera_util.h"
17 #include <securec.h>
18 #include "camera_log.h"
19 #include "accesstoken_kit.h"
20 
21 namespace OHOS {
22 namespace CameraStandard {
23 std::unordered_map<int32_t, int32_t> g_cameraToPixelFormat = {
24     {OHOS_CAMERA_FORMAT_RGBA_8888, PIXEL_FMT_RGBA_8888},
25     {OHOS_CAMERA_FORMAT_YCBCR_420_888, PIXEL_FMT_YCBCR_420_SP},
26     {OHOS_CAMERA_FORMAT_YCRCB_420_SP, PIXEL_FMT_YCRCB_420_SP},
27     {OHOS_CAMERA_FORMAT_JPEG, PIXEL_FMT_YCRCB_420_SP},
28 };
29 
30 std::map<int, std::string> g_cameraPos = {
31     {0, "Front"},
32     {1, "Back"},
33     {2, "Other"},
34 };
35 
36 std::map<int, std::string> g_cameraType = {
37     {0, "Wide-Angle"},
38     {1, "Ultra-Wide"},
39     {2, "TelePhoto"},
40     {3, "TrueDepth"},
41     {4, "Logical"},
42     {5, "Unspecified"},
43 };
44 
45 std::map<int, std::string> g_cameraConType = {
46     {0, "Builtin"},
47     {1, "USB-Plugin"},
48     {2, "Remote"},
49 };
50 
51 std::map<int, std::string> g_cameraFormat = {
52     {1, "RGBA_8888"},
53     {2, "YCBCR_420_888"},
54     {3, "YCRCB_420_SP"},
55     {4, "JPEG"},
56 };
57 
58 std::map<int, std::string> g_cameraFocusMode = {
59     {0, "Manual"},
60     {1, "Continuous-Auto"},
61     {2, "Auto"},
62     {3, "Locked"},
63 };
64 
65 std::map<int, std::string> g_cameraExposureMode = {
66     {0, "Manual"},
67     {1, "Continuous-Auto"},
68     {2, "Locked"},
69     {3, "Auto"},
70 };
71 
72 std::map<int, std::string> g_cameraFlashMode = {
73     {0, "Close"},
74     {1, "Open"},
75     {2, "Auto"},
76     {3, "Always-Open"},
77 };
78 
79 std::map<int, std::string> g_cameraVideoStabilizationMode = {
80     {0, "Off"},
81     {1, "Low"},
82     {2, "Middle"},
83     {3, "High"},
84     {4, "Auto"},
85 };
86 
87 static std::mutex g_captureIdsMutex;
88 static std::map<int32_t, bool> g_captureIds;
89 
HdiToServiceError(CamRetCode ret)90 int32_t HdiToServiceError(CamRetCode ret)
91 {
92     enum CamServiceError err = CAMERA_UNKNOWN_ERROR;
93 
94     switch (ret) {
95         case HDI::Camera::V1_0::NO_ERROR:
96             err = CAMERA_OK;
97             break;
98         case HDI::Camera::V1_0::CAMERA_BUSY:
99             err = CAMERA_DEVICE_BUSY;
100             break;
101         case HDI::Camera::V1_0::INVALID_ARGUMENT:
102             err = CAMERA_INVALID_ARG;
103             break;
104         case HDI::Camera::V1_0::CAMERA_CLOSED:
105             err = CAMERA_DEVICE_CLOSED;
106             break;
107         default:
108             MEDIA_ERR_LOG("HdiToServiceError() error code from hdi: %{public}d", ret);
109             break;
110     }
111     return err;
112 }
113 
CreateMsg(const char * format,...)114 std::string CreateMsg(const char* format, ...)
115 {
116     va_list args;
117     va_start(args, format);
118     char msg[MAX_STRING_SIZE] = {0};
119     if (vsnprintf_s(msg, sizeof(msg), sizeof(msg) - 1, format, args) < 0) {
120         MEDIA_ERR_LOG("failed to call vsnprintf_s");
121         va_end(args);
122         return "";
123     }
124     va_end(args);
125     return msg;
126 }
127 
AllocateCaptureId(int32_t & captureId)128 int32_t AllocateCaptureId(int32_t &captureId)
129 {
130     std::lock_guard<std::mutex> lock(g_captureIdsMutex);
131     static int32_t currentCaptureId = 0;
132     for (int32_t i = 0; i < INT_MAX; i++) {
133         if (currentCaptureId == INT_MAX) {
134             currentCaptureId = 0;
135             MEDIA_INFO_LOG("Restarting CaptureId");
136         }
137         currentCaptureId++;
138         if (g_captureIds.find(currentCaptureId) == g_captureIds.end()) {
139             g_captureIds[currentCaptureId] = true;
140             captureId = currentCaptureId;
141             return CAMERA_OK;
142         }
143     }
144     return CAMERA_CAPTURE_LIMIT_EXCEED;
145 }
146 
ReleaseCaptureId(int32_t captureId)147 void ReleaseCaptureId(int32_t captureId)
148 {
149     std::lock_guard<std::mutex> lock(g_captureIdsMutex);
150     g_captureIds.erase(captureId);
151     return;
152 }
153 
IsValidTokenId(uint32_t tokenId)154 bool IsValidTokenId(uint32_t tokenId)
155 {
156     return Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId) ==
157         Security::AccessToken::ATokenTypeEnum::TOKEN_HAP;
158 }
159 
IsValidSize(std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility,int32_t format,int32_t width,int32_t height)160 bool IsValidSize(
161     std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility, int32_t format, int32_t width, int32_t height)
162 {
163     constexpr uint32_t unitLen = 3;
164     camera_metadata_item_t item;
165     int ret = Camera::FindCameraMetadataItem(cameraAbility->get(),
166                                              OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS, &item);
167     if (ret != CAM_META_SUCCESS) {
168         MEDIA_ERR_LOG("Failed to find stream configuration in camera ability with return code %{public}d", ret);
169         return false;
170     }
171     if (item.count % unitLen != 0) {
172         MEDIA_ERR_LOG("Invalid stream configuration count: %{public}u", item.count);
173         return false;
174     }
175     for (uint32_t index = 0; index < item.count; index += 3) {
176         if (item.data.i32[index] == format) {
177             if (item.data.i32[index + 1] == width && item.data.i32[index + 2] == height) {
178                 MEDIA_INFO_LOG("Format:%{public}d, width:%{public}d, height:%{public}d found in supported streams",
179                                format, width, height);
180                 return true;
181             }
182         }
183     }
184     MEDIA_ERR_LOG("Format:%{public}d, width:%{public}d, height:%{public}d not found in supported streams",
185                   format, width, height);
186     return false;
187 }
188 } // namespace CameraStandard
189 } // namespace OHOS
190