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