• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (CheckWindow(window)) {
57         return DelayedSingleton<CellularCallConnection>::GetInstance()->SetPreviewWindow(window);
58     }
59     return CALL_ERR_VIDEO_INVALID_COORDINATES;
60 }
61 
SetDisplayWindow(VideoWindow & window)62 int32_t VideoControlManager::SetDisplayWindow(VideoWindow &window)
63 {
64     if (CheckWindow(window)) {
65         return DelayedSingleton<CellularCallConnection>::GetInstance()->SetDisplayWindow(window);
66     }
67     return CALL_ERR_VIDEO_INVALID_COORDINATES;
68 }
69 
SetCameraZoom(float zoomRatio)70 int32_t VideoControlManager::SetCameraZoom(float zoomRatio)
71 {
72     // param check
73     if (zoomRatio < MIN_CAMERA_ZOOM || zoomRatio > MAX_CAMERA_ZOOM) {
74         TELEPHONY_LOGE("camera zoom error!!");
75         return CALL_ERR_VIDEO_INVALID_ZOOM;
76     }
77     return DelayedSingleton<CellularCallConnection>::GetInstance()->SetCameraZoom(zoomRatio);
78 }
79 
SetPausePicture(std::u16string path)80 int32_t VideoControlManager::SetPausePicture(std::u16string path)
81 {
82     std::string tempPath(Str16ToStr8(path));
83     // param check
84     if (FileExists(tempPath) && IsPngFile(tempPath)) {
85         return DelayedSingleton<CellularCallConnection>::GetInstance()->SetPausePicture(path);
86     } else {
87         TELEPHONY_LOGE("invalid path");
88         return CALL_ERR_INVALID_PATH;
89     }
90 }
91 
SetDeviceDirection(int32_t rotation)92 int32_t VideoControlManager::SetDeviceDirection(int32_t rotation)
93 {
94     // param check
95     if (rotation == CAMERA_ROTATION_0 || rotation == CAMERA_ROTATION_90 || rotation == CAMERA_ROTATION_180 ||
96         rotation == CAMERA_ROTATION_270) {
97         return DelayedSingleton<CellularCallConnection>::GetInstance()->SetDeviceDirection(rotation);
98     }
99     TELEPHONY_LOGE("error rotation:%{public}d", rotation);
100     return CALL_ERR_VIDEO_INVALID_ROTATION;
101 }
102 
OpenCamera(std::u16string cameraId,int32_t callingUid,int32_t callingPid)103 int32_t VideoControlManager::OpenCamera(std::u16string cameraId, int32_t callingUid, int32_t callingPid)
104 {
105     // cameraId check
106     std::string id(Str16ToStr8(cameraId));
107     bool bRet = ContainCameraID(id);
108     if (!bRet) {
109         TELEPHONY_LOGE("camera id is error!!");
110         return CALL_ERR_VIDEO_INVALID_CAMERA_ID;
111     }
112     int32_t errCode =
113         DelayedSingleton<CellularCallConnection>::GetInstance()->ControlCamera(cameraId, callingUid, callingPid);
114     if (errCode == TELEPHONY_SUCCESS) {
115         isOpenCamera_ = true;
116     }
117     return errCode;
118 }
119 
CloseCamera(std::u16string cameraId,int32_t callingUid,int32_t callingPid)120 int32_t VideoControlManager::CloseCamera(std::u16string cameraId, int32_t callingUid, int32_t callingPid)
121 {
122     if (isOpenCamera_) {
123         int32_t errCode = DelayedSingleton<CellularCallConnection>::GetInstance()->ControlCamera(
124             cameraId, callingUid, callingPid);
125         if (errCode == TELEPHONY_SUCCESS) {
126             isOpenCamera_ = false;
127         }
128         return errCode;
129     }
130     TELEPHONY_LOGE("Camera not turned on");
131     return CALL_ERR_CAMERA_NOT_TURNED_ON;
132 }
133 
ContainCameraID(std::string id)134 bool VideoControlManager::ContainCameraID(std::string id)
135 {
136     using namespace OHOS::CameraStandard;
137     sptr<CameraManager> camManagerObj = CameraManager::GetInstance();
138     std::vector<sptr<CameraStandard::CameraInfo>> cameraObjList = camManagerObj->GetCameras();
139     bool bRet = false;
140     for (auto &it : cameraObjList) {
141         if (id.compare(it->GetID()) == 0) {
142             bRet = true;
143             TELEPHONY_LOGI("Contain Camera ID:  : %{public}s", id.c_str());
144             break;
145         }
146     }
147     return bRet;
148 }
149 
IsPngFile(std::string fileName)150 bool VideoControlManager::IsPngFile(std::string fileName)
151 {
152     int32_t len = SUPPORT_PICTURE_EXT.length();
153     std::string ext = fileName.substr(fileName.length() - len, len);
154     std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
155     if (!((ext == SUPPORT_PICTURE_EXT))) {
156         TELEPHONY_LOGE("file not support: %{public}s", fileName.c_str());
157         return false;
158     }
159     return true;
160 }
161 
CheckWindow(VideoWindow & window)162 bool VideoControlManager::CheckWindow(VideoWindow &window)
163 {
164     if (window.width <= 0 || window.height <= 0) {
165         TELEPHONY_LOGE("width or height value error");
166         return false;
167     }
168     if (window.z != VIDEO_WINDOWS_Z_BOTTOM && window.z != VIDEO_WINDOWS_Z_TOP) {
169         TELEPHONY_LOGE("z value error %{public}d", window.z);
170         return false;
171     }
172     return true;
173 }
174 } // namespace Telephony
175 } // namespace OHOS
176