1 /*
2 * Copyright (C) 2021 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 #include <algorithm>
16
17 #include "telephony_errors.h"
18
19 #include "telephony_log_wrapper.h"
20
21 #include "input/camera_manager.h"
22 #include "file_ex.h"
23
24 #include "cellular_call_connection.h"
25 #include "video_control_manager.h"
26
27 namespace OHOS {
28 namespace Telephony {
29 namespace {
30 const int16_t CAMERA_ROTATION_0 = 0;
31 const int16_t CAMERA_ROTATION_90 = 90;
32 const int16_t CAMERA_ROTATION_180 = 180;
33 const int16_t CAMERA_ROTATION_270 = 270;
34 const int16_t VIDEO_WINDOWS_Z_BOTTOM = 0;
35 const int16_t VIDEO_WINDOWS_Z_TOP = 1;
36 const float MIN_CAMERA_ZOOM = 0.1;
37 const float MAX_CAMERA_ZOOM = 10.0;
38 const std::string SUPPORT_PICTURE_EXT = "png";
39 } // namespace
40
VideoControlManager()41 VideoControlManager::VideoControlManager() : isOpenCamera_(false) {}
42
~VideoControlManager()43 VideoControlManager::~VideoControlManager() {}
44
ControlCamera(std::u16string cameraId,int32_t callingUid,int32_t callingPid)45 int32_t VideoControlManager::ControlCamera(std::u16string cameraId, int32_t callingUid, int32_t callingPid)
46 {
47 if (cameraId.empty()) {
48 return CloseCamera(cameraId, callingUid, callingPid);
49 } else {
50 return OpenCamera(cameraId, callingUid, callingPid);
51 }
52 }
53
SetPreviewWindow(VideoWindow & window)54 int32_t VideoControlManager::SetPreviewWindow(VideoWindow &window)
55 {
56 return CALL_ERR_VIDEO_INVALID_COORDINATES;
57 }
58
SetDisplayWindow(VideoWindow & window)59 int32_t VideoControlManager::SetDisplayWindow(VideoWindow &window)
60 {
61 return CALL_ERR_VIDEO_INVALID_COORDINATES;
62 }
63
SetCameraZoom(float zoomRatio)64 int32_t VideoControlManager::SetCameraZoom(float zoomRatio)
65 {
66 // param check
67 if (zoomRatio < MIN_CAMERA_ZOOM || zoomRatio > MAX_CAMERA_ZOOM) {
68 TELEPHONY_LOGE("camera zoom error!!");
69 return CALL_ERR_VIDEO_INVALID_ZOOM;
70 }
71 return DelayedSingleton<CellularCallConnection>::GetInstance()->SetCameraZoom(zoomRatio);
72 }
73
SetPausePicture(std::u16string path)74 int32_t VideoControlManager::SetPausePicture(std::u16string path)
75 {
76 std::string tempPath(Str16ToStr8(path));
77 // param check
78 if (FileExists(tempPath) && IsPngFile(tempPath)) {
79 return DelayedSingleton<CellularCallConnection>::GetInstance()->SetPausePicture(path);
80 } else {
81 TELEPHONY_LOGE("invalid path");
82 return CALL_ERR_INVALID_PATH;
83 }
84 }
85
SetDeviceDirection(int32_t rotation)86 int32_t VideoControlManager::SetDeviceDirection(int32_t rotation)
87 {
88 // param check
89 if (rotation == CAMERA_ROTATION_0 || rotation == CAMERA_ROTATION_90 || rotation == CAMERA_ROTATION_180 ||
90 rotation == CAMERA_ROTATION_270) {
91 return DelayedSingleton<CellularCallConnection>::GetInstance()->SetDeviceDirection(rotation);
92 }
93 TELEPHONY_LOGE("error rotation:%{public}d", rotation);
94 return CALL_ERR_VIDEO_INVALID_ROTATION;
95 }
96
OpenCamera(std::u16string cameraId,int32_t callingUid,int32_t callingPid)97 int32_t VideoControlManager::OpenCamera(std::u16string cameraId, int32_t callingUid, int32_t callingPid)
98 {
99 // cameraId check
100 std::string id(Str16ToStr8(cameraId));
101 bool bRet = ContainCameraID(id);
102 if (!bRet) {
103 TELEPHONY_LOGE("camera id is error!!");
104 return CALL_ERR_VIDEO_INVALID_CAMERA_ID;
105 }
106 int32_t errCode =
107 DelayedSingleton<CellularCallConnection>::GetInstance()->ControlCamera(cameraId, callingUid, callingPid);
108 if (errCode == TELEPHONY_SUCCESS) {
109 isOpenCamera_ = true;
110 }
111 return errCode;
112 }
113
CloseCamera(std::u16string cameraId,int32_t callingUid,int32_t callingPid)114 int32_t VideoControlManager::CloseCamera(std::u16string cameraId, int32_t callingUid, int32_t callingPid)
115 {
116 if (isOpenCamera_) {
117 int32_t errCode = DelayedSingleton<CellularCallConnection>::GetInstance()->ControlCamera(
118 cameraId, callingUid, callingPid);
119 if (errCode == TELEPHONY_SUCCESS) {
120 isOpenCamera_ = false;
121 }
122 return errCode;
123 }
124 TELEPHONY_LOGE("Camera not turned on");
125 return CALL_ERR_CAMERA_NOT_TURNED_ON;
126 }
127
ContainCameraID(std::string id)128 bool VideoControlManager::ContainCameraID(std::string id)
129 {
130 using namespace OHOS::CameraStandard;
131 sptr<CameraManager> camManagerObj = CameraManager::GetInstance();
132 std::vector<sptr<CameraStandard::CameraDevice>> cameraObjList = camManagerObj->GetSupportedCameras();
133 bool bRet = false;
134 for (auto &it : cameraObjList) {
135 if (id.compare(it->GetID()) == 0) {
136 bRet = true;
137 TELEPHONY_LOGI("Contain Camera ID: : %{public}s", id.c_str());
138 break;
139 }
140 }
141 return bRet;
142 }
143
IsPngFile(std::string fileName)144 bool VideoControlManager::IsPngFile(std::string fileName)
145 {
146 size_t len = SUPPORT_PICTURE_EXT.length();
147 if (fileName.length() <= len + 1) {
148 TELEPHONY_LOGE("file not support: %{public}s", fileName.c_str());
149 return false;
150 }
151 std::string ext = fileName.substr(fileName.length() - len, len);
152 std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
153 if (!((ext == SUPPORT_PICTURE_EXT))) {
154 TELEPHONY_LOGE("file not support: %{public}s", fileName.c_str());
155 return false;
156 }
157 return true;
158 }
159
CheckWindow(VideoWindow & window)160 bool VideoControlManager::CheckWindow(VideoWindow &window)
161 {
162 if (window.width <= 0 || window.height <= 0) {
163 TELEPHONY_LOGE("width or height value error");
164 return false;
165 }
166 if (window.z != VIDEO_WINDOWS_Z_BOTTOM && window.z != VIDEO_WINDOWS_Z_TOP) {
167 TELEPHONY_LOGE("z value error %{public}d", window.z);
168 return false;
169 }
170 return true;
171 }
172 } // namespace Telephony
173 } // namespace OHOS
174