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 "media_log.h"
18
19 namespace OHOS {
20 namespace CameraStandard {
21 std::unordered_map<int32_t, int32_t> g_cameraToPixelFormat = {
22 {OHOS_CAMERA_FORMAT_RGBA_8888, PIXEL_FMT_RGBA_8888},
23 {OHOS_CAMERA_FORMAT_YCBCR_420_888, PIXEL_FMT_YCBCR_420_SP},
24 {OHOS_CAMERA_FORMAT_YCRCB_420_SP, PIXEL_FMT_YCRCB_420_SP},
25 {OHOS_CAMERA_FORMAT_JPEG, PIXEL_FMT_YCRCB_420_SP},
26 };
27
28 std::map<int, std::string> g_cameraPos = {
29 {0, "Front"},
30 {1, "Back"},
31 {2, "Other"},
32 };
33
34 std::map<int, std::string> g_cameraType = {
35 {0, "Wide-Angle"},
36 {1, "Ultra-Wide"},
37 {2, "TelePhoto"},
38 {3, "TrueDepth"},
39 {4, "Logical"},
40 {5, "Unspecified"},
41 };
42
43 std::map<int, std::string> g_cameraConType = {
44 {0, "Builtin"},
45 {1, "USB-Plugin"},
46 {2, "Remote"},
47 };
48
49 std::map<int, std::string> g_cameraFormat = {
50 {1, "RGBA_8888"},
51 {2, "YCBCR_420_888"},
52 {3, "YCRCB_420_SP"},
53 {4, "JPEG"},
54 };
55
56 std::map<int, std::string> g_cameraFocusMode = {
57 {0, "Manual"},
58 {1, "Continuous-Auto"},
59 {2, "Auto"},
60 {3, "Locked"},
61 };
62
63 std::map<int, std::string> g_cameraFlashMode = {
64 {0, "Close"},
65 {1, "Open"},
66 {2, "Auto"},
67 {3, "Always-Open"},
68 };
69
HdiToServiceError(Camera::CamRetCode ret)70 int32_t HdiToServiceError(Camera::CamRetCode ret)
71 {
72 enum CamServiceError err = CAMERA_UNKNOWN_ERROR;
73
74 switch (ret) {
75 case Camera::NO_ERROR:
76 err = CAMERA_OK;
77 break;
78 case Camera::CAMERA_BUSY:
79 err = CAMERA_DEVICE_BUSY;
80 break;
81 case Camera::INVALID_ARGUMENT:
82 err = CAMERA_INVALID_ARG;
83 break;
84 case Camera::CAMERA_CLOSED:
85 err = CAMERA_DEVICE_CLOSED;
86 break;
87 default:
88 MEDIA_ERR_LOG("HdiToServiceError() error code from hdi: %{public}d", ret);
89 break;
90 }
91 return err;
92 }
93
IsValidSize(std::shared_ptr<Camera::CameraMetadata> cameraAbility,int32_t format,int32_t width,int32_t height)94 bool IsValidSize(std::shared_ptr<Camera::CameraMetadata> cameraAbility, int32_t format, int32_t width, int32_t height)
95 {
96 #ifndef BALTIMORE_CAMERA
97 return true;
98 #endif
99 uint32_t unitLen = 3;
100 camera_metadata_item_t item;
101 int ret = Camera::FindCameraMetadataItem(cameraAbility->get(),
102 OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS, &item);
103 if (ret != CAM_META_SUCCESS) {
104 MEDIA_ERR_LOG("Failed to find stream configuration in camera ability with return code %{public}d", ret);
105 return false;
106 }
107 if (item.count % unitLen != 0) {
108 MEDIA_ERR_LOG("Invalid stream configuration count: %{public}d", item.count);
109 return false;
110 }
111 for (uint32_t index = 0; index < item.count; index += 3) {
112 if (item.data.i32[index] == format) {
113 if (item.data.i32[index + 1] == width && item.data.i32[index + 2] == height) {
114 MEDIA_INFO_LOG("Format:%{public}d, width:%{public}d, height:%{public}d found in supported streams",
115 format, width, height);
116 return true;
117 }
118 }
119 }
120 MEDIA_ERR_LOG("Format:%{public}d, width:%{public}d, height:%{public}d not found in supported streams",
121 format, width, height);
122 return false;
123 }
124 } // namespace CameraStandard
125 } // namespace OHOS
126