• 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 "call_ability_report_proxy.h"
20 #include "cellular_call_connection.h"
21 #include "file_ex.h"
22 #include "telephony_errors.h"
23 #include "telephony_log_wrapper.h"
24 
25 #ifdef ABILITY_CAMERA_SUPPORT
26 #include "input/camera_manager.h"
27 #endif
28 
29 namespace OHOS {
30 namespace Telephony {
31 namespace {
32 const int16_t CAMERA_ROTATION_0 = 0;
33 const int16_t CAMERA_ROTATION_90 = 90;
34 const int16_t CAMERA_ROTATION_180 = 180;
35 const int16_t CAMERA_ROTATION_270 = 270;
36 const int16_t VIDEO_WINDOWS_Z_BOTTOM = 0;
37 const int16_t VIDEO_WINDOWS_Z_TOP = 1;
38 const float MIN_CAMERA_ZOOM = 0.1;
39 const float MAX_CAMERA_ZOOM = 10.0;
40 const std::string SUPPORT_PICTURE_EXT = "png";
41 } // namespace
42 
VideoControlManager()43 VideoControlManager::VideoControlManager() : isOpenCamera_(false) {}
44 
~VideoControlManager()45 VideoControlManager::~VideoControlManager() {}
46 
ControlCamera(int32_t callId,std::u16string & cameraId,int32_t callingUid,int32_t callingPid)47 int32_t VideoControlManager::ControlCamera(
48     int32_t callId, std::u16string &cameraId, int32_t callingUid, int32_t callingPid)
49 {
50     if (cameraId.empty()) {
51         return CloseCamera(callId, cameraId, callingUid, callingPid);
52     } else {
53         return OpenCamera(callId, cameraId, callingUid, callingPid);
54     }
55 }
56 
SetPreviewWindow(int32_t callId,std::string & surfaceId,sptr<Surface> surface)57 int32_t VideoControlManager::SetPreviewWindow(int32_t callId, std::string &surfaceId, sptr<Surface> surface)
58 {
59     int32_t ret = TELEPHONY_ERR_FAIL;
60     ret = CallPolicy::VideoCallPolicy(callId);
61     if (ret != TELEPHONY_SUCCESS) {
62         TELEPHONY_LOGE("check prerequisites failed !");
63         return ret;
64     }
65     sptr<CallBase> callPtr = CallObjectManager::GetOneCallObject(callId);
66     if (callPtr == nullptr) {
67         TELEPHONY_LOGE("the call object is nullptr, callId:%{public}d", callId);
68         return TELEPHONY_ERR_LOCAL_PTR_NULL;
69     }
70     if (callPtr->GetCallType() == CallType::TYPE_IMS) {
71         sptr<IMSCall> netCall = reinterpret_cast<IMSCall *>(callPtr.GetRefPtr());
72         ret = netCall->SetPreviewWindow(surfaceId, surface);
73         if (ret != TELEPHONY_SUCCESS) {
74             TELEPHONY_LOGE("SetPreviewWindow failed!");
75             return ret;
76         }
77     }
78     return TELEPHONY_SUCCESS;
79 }
80 
SetDisplayWindow(int32_t callId,std::string & surfaceId,sptr<Surface> surface)81 int32_t VideoControlManager::SetDisplayWindow(int32_t callId, std::string &surfaceId, sptr<Surface> surface)
82 {
83     int32_t ret = TELEPHONY_ERR_FAIL;
84     ret = CallPolicy::VideoCallPolicy(callId);
85     if (ret != TELEPHONY_SUCCESS) {
86         TELEPHONY_LOGE("check prerequisites failed !");
87         return ret;
88     }
89     sptr<CallBase> callPtr = CallObjectManager::GetOneCallObject(callId);
90     if (callPtr == nullptr) {
91         TELEPHONY_LOGE("the call object is nullptr, callId:%{public}d", callId);
92         return TELEPHONY_ERR_LOCAL_PTR_NULL;
93     }
94     if (callPtr->GetCallType() == CallType::TYPE_IMS) {
95         sptr<IMSCall> netCall = reinterpret_cast<IMSCall *>(callPtr.GetRefPtr());
96         ret = netCall->SetDisplayWindow(surfaceId, surface);
97         if (ret != TELEPHONY_SUCCESS) {
98             TELEPHONY_LOGE("SetDisplayWindow failed!");
99             return ret;
100         }
101     }
102     return TELEPHONY_SUCCESS;
103 }
104 
SetCameraZoom(float zoomRatio)105 int32_t VideoControlManager::SetCameraZoom(float zoomRatio)
106 {
107     // param check
108     if (zoomRatio < MIN_CAMERA_ZOOM || zoomRatio > MAX_CAMERA_ZOOM) {
109         TELEPHONY_LOGE("camera zoom error!!");
110         return CALL_ERR_VIDEO_INVALID_ZOOM;
111     }
112     return DelayedSingleton<CellularCallConnection>::GetInstance()->SetCameraZoom(zoomRatio);
113 }
114 
SetPausePicture(int32_t callId,std::u16string & path)115 int32_t VideoControlManager::SetPausePicture(int32_t callId, std::u16string &path)
116 {
117     int32_t ret = TELEPHONY_ERR_FAIL;
118     ret = CallPolicy::VideoCallPolicy(callId);
119     if (ret != TELEPHONY_SUCCESS) {
120         TELEPHONY_LOGE("check prerequisites failed !");
121         return ret;
122     }
123     std::string tempPath(Str16ToStr8(path));
124     sptr<CallBase> callPtr = CallObjectManager::GetOneCallObject(callId);
125     if (callPtr == nullptr) {
126         TELEPHONY_LOGE("the call object is nullptr, callId:%{public}d", callId);
127         return TELEPHONY_ERR_LOCAL_PTR_NULL;
128     }
129     if (callPtr->GetCallType() == CallType::TYPE_IMS) {
130         sptr<IMSCall> netCall = reinterpret_cast<IMSCall *>(callPtr.GetRefPtr());
131         ret = netCall->SetPausePicture(tempPath);
132         if (ret != TELEPHONY_SUCCESS) {
133             TELEPHONY_LOGE("SetPausePicture failed!");
134             return ret;
135         }
136     }
137     return TELEPHONY_SUCCESS;
138 }
139 
SetDeviceDirection(int32_t callId,int32_t rotation)140 int32_t VideoControlManager::SetDeviceDirection(int32_t callId, int32_t rotation)
141 {
142     int32_t ret = TELEPHONY_ERR_FAIL;
143     ret = CallPolicy::VideoCallPolicy(callId);
144     if (ret != TELEPHONY_SUCCESS) {
145         TELEPHONY_LOGE("check prerequisites failed !");
146         return ret;
147     }
148     // param check
149     if (rotation == CAMERA_ROTATION_0 || rotation == CAMERA_ROTATION_90 || rotation == CAMERA_ROTATION_180 ||
150         rotation == CAMERA_ROTATION_270) {
151         sptr<CallBase> callPtr = CallObjectManager::GetOneCallObject(callId);
152         if (callPtr == nullptr) {
153             TELEPHONY_LOGE("the call object is nullptr, callId:%{public}d", callId);
154             return TELEPHONY_ERR_LOCAL_PTR_NULL;
155         }
156         if (callPtr->GetCallType() == CallType::TYPE_IMS) {
157             sptr<IMSCall> netCall = reinterpret_cast<IMSCall *>(callPtr.GetRefPtr());
158             ret = netCall->SetDeviceDirection(rotation);
159             if (ret != TELEPHONY_SUCCESS) {
160                 TELEPHONY_LOGE("SetDeviceDirection failed!");
161                 return ret;
162             }
163         }
164         return TELEPHONY_SUCCESS;
165     }
166     TELEPHONY_LOGE("error rotation:%{public}d", rotation);
167     return CALL_ERR_VIDEO_INVALID_ROTATION;
168 }
169 
UpdateImsCallMode(int32_t callId,ImsCallMode callMode)170 int32_t VideoControlManager::UpdateImsCallMode(int32_t callId, ImsCallMode callMode)
171 {
172     int32_t ret = TELEPHONY_ERR_FAIL;
173     ret = CallPolicy::VideoCallPolicy(callId);
174     if (ret != TELEPHONY_SUCCESS) {
175         TELEPHONY_LOGE("check prerequisites failed !");
176         return ret;
177     }
178     sptr<CallBase> callPtr = CallObjectManager::GetForegroundLiveCallByCallId(callId);
179     if (callPtr == nullptr) {
180         callPtr = CallObjectManager::GetOneCallObject(callId);
181         TELEPHONY_LOGI("GetForegroundLiveCallByCallId is null use default call");
182     }
183     if (callPtr == nullptr) {
184         TELEPHONY_LOGE("the call object is nullptr, callId:%{public}d", callId);
185         return TELEPHONY_ERR_LOCAL_PTR_NULL;
186     }
187     if (callPtr->GetCallType() == CallType::TYPE_IMS) {
188         // only netcall type support update call media mode
189         sptr<IMSCall> netCall = reinterpret_cast<IMSCall *>(callPtr.GetRefPtr());
190         TELEPHONY_LOGI("ims call update media request");
191         ret = netCall->UpdateImsCallMode(callMode);
192         if (ret != TELEPHONY_SUCCESS) {
193             TELEPHONY_LOGE("UpdateImsCallMode failed!. %{public}d", ret);
194         }
195     }
196     return ret;
197 }
198 
ReportImsCallModeInfo(CallMediaModeInfo & imsCallModeInfo)199 int32_t VideoControlManager::ReportImsCallModeInfo(CallMediaModeInfo &imsCallModeInfo)
200 {
201     return DelayedSingleton<CallAbilityReportProxy>::GetInstance()->ReportImsCallModeChange(imsCallModeInfo);
202 }
203 
OpenCamera(int32_t callId,std::u16string & cameraId,int32_t callingUid,int32_t callingPid)204 int32_t VideoControlManager::OpenCamera(
205     int32_t callId, std::u16string &cameraId, int32_t callingUid, int32_t callingPid)
206 {
207     // cameraId check
208     std::string id(Str16ToStr8(cameraId));
209     int32_t ret = TELEPHONY_ERR_FAIL;
210     ret = CallPolicy::VideoCallPolicy(callId);
211     if (ret != TELEPHONY_SUCCESS) {
212         TELEPHONY_LOGE("check prerequisites failed !");
213         return ret;
214     }
215     sptr<CallBase> callPtr = CallObjectManager::GetOneCallObject(callId);
216     if (callPtr == nullptr || callPtr.GetRefPtr() == nullptr) {
217         TELEPHONY_LOGE("the call object is nullptr, callId:%{public}d", callId);
218         return TELEPHONY_ERR_LOCAL_PTR_NULL;
219     }
220     if (callPtr->GetCallType() == CallType::TYPE_IMS) {
221         sptr<IMSCall> netCall = reinterpret_cast<IMSCall *>(callPtr.GetRefPtr());
222         if (netCall == nullptr) {
223             TELEPHONY_LOGE("the netCall is nullptr, callId:%{public}d", callId);
224             return TELEPHONY_ERR_LOCAL_PTR_NULL;
225         }
226         ret = netCall->ControlCamera(id, callingUid, callingPid);
227         if (ret == TELEPHONY_SUCCESS) {
228             isOpenCamera_ = true;
229         }
230         ret = netCall->RequestCameraCapabilities();
231         if (ret != TELEPHONY_SUCCESS) {
232             TELEPHONY_LOGE("RequestCameraCapabilities failed!");
233             return ret;
234         }
235     }
236     return ret;
237 }
238 
CloseCamera(int32_t callId,std::u16string & cameraId,int32_t callingUid,int32_t callingPid)239 int32_t VideoControlManager::CloseCamera(
240     int32_t callId, std::u16string &cameraId, int32_t callingUid, int32_t callingPid)
241 {
242     std::string id(Str16ToStr8(cameraId));
243     int32_t ret = TELEPHONY_ERR_FAIL;
244     ret = CallPolicy::VideoCallPolicy(callId);
245     if (ret != TELEPHONY_SUCCESS) {
246         TELEPHONY_LOGE("check prerequisites failed !");
247         return ret;
248     }
249     if (isOpenCamera_) {
250         sptr<CallBase> callPtr = CallObjectManager::GetOneCallObject(callId);
251         if (callPtr == nullptr) {
252             TELEPHONY_LOGE("the call object is nullptr, callId:%{public}d", callId);
253             return TELEPHONY_ERR_LOCAL_PTR_NULL;
254         }
255         if (callPtr->GetCallType() == CallType::TYPE_IMS) {
256             sptr<IMSCall> netCall = reinterpret_cast<IMSCall *>(callPtr.GetRefPtr());
257             ret = netCall->ControlCamera(id, callingUid, callingPid);
258             if (ret == TELEPHONY_SUCCESS) {
259                 isOpenCamera_ = true;
260             }
261         }
262         return ret;
263     }
264     TELEPHONY_LOGE("Camera not turned on");
265     return CALL_ERR_CAMERA_NOT_TURNED_ON;
266 }
267 
ContainCameraID(std::string id)268 bool VideoControlManager::ContainCameraID(std::string id)
269 {
270     bool bRet = false;
271 #ifdef ABILITY_CAMERA_SUPPORT
272     using namespace OHOS::CameraStandard;
273     sptr<CameraManager> camManagerObj = CameraManager::GetInstance();
274     std::vector<sptr<CameraStandard::CameraDevice>> cameraObjList = camManagerObj->GetSupportedCameras();
275 
276     for (auto &it : cameraObjList) {
277         if (id.compare(it->GetID()) == 0) {
278             bRet = true;
279             TELEPHONY_LOGI("Contain Camera ID:  : %{public}s", id.c_str());
280             break;
281         }
282     }
283 #endif
284     return bRet;
285 }
286 
CancelCallUpgrade(int32_t callId)287 int32_t VideoControlManager::CancelCallUpgrade(int32_t callId)
288 {
289     int32_t ret = TELEPHONY_ERR_FAIL;
290     ret = CallPolicy::VideoCallPolicy(callId);
291     if (ret != TELEPHONY_SUCCESS) {
292         TELEPHONY_LOGE("check prerequisites failed !");
293         return ret;
294     }
295     sptr<CallBase> callPtr = CallObjectManager::GetOneCallObject(callId);
296     if (callPtr == nullptr) {
297         TELEPHONY_LOGE("the call object is nullptr, callId:%{public}d", callId);
298         return TELEPHONY_ERR_LOCAL_PTR_NULL;
299     }
300     if (callPtr->GetCallType() == CallType::TYPE_IMS) {
301         sptr<IMSCall> netCall = reinterpret_cast<IMSCall *>(callPtr.GetRefPtr());
302         ret = netCall->CancelCallUpgrade();
303         if (ret != TELEPHONY_SUCCESS) {
304             TELEPHONY_LOGE("CancelCallUpgrade failed!");
305             return ret;
306         }
307     }
308     return TELEPHONY_SUCCESS;
309 }
310 
RequestCameraCapabilities(int32_t callId)311 int32_t VideoControlManager::RequestCameraCapabilities(int32_t callId)
312 {
313     int32_t ret = TELEPHONY_ERR_FAIL;
314     ret = CallPolicy::VideoCallPolicy(callId);
315     if (ret != TELEPHONY_SUCCESS) {
316         TELEPHONY_LOGE("check prerequisites failed !");
317         return ret;
318     }
319     sptr<CallBase> callPtr = CallObjectManager::GetOneCallObject(callId);
320     if (callPtr == nullptr) {
321         TELEPHONY_LOGE("the call object is nullptr, callId:%{public}d", callId);
322         return TELEPHONY_ERR_LOCAL_PTR_NULL;
323     }
324     if (callPtr->GetCallType() == CallType::TYPE_IMS) {
325         sptr<IMSCall> netCall = reinterpret_cast<IMSCall *>(callPtr.GetRefPtr());
326         ret = netCall->RequestCameraCapabilities();
327         if (ret != TELEPHONY_SUCCESS) {
328             TELEPHONY_LOGE("RequestCameraCapabilities failed!");
329             return ret;
330         }
331     }
332     return TELEPHONY_SUCCESS;
333 }
334 
CheckWindow(VideoWindow & window)335 bool VideoControlManager::CheckWindow(VideoWindow &window)
336 {
337     if (window.width <= 0 || window.height <= 0) {
338         TELEPHONY_LOGE("width or height value error");
339         return false;
340     }
341     if (window.z != VIDEO_WINDOWS_Z_BOTTOM && window.z != VIDEO_WINDOWS_Z_TOP) {
342         TELEPHONY_LOGE("z value error %{public}d", window.z);
343         return false;
344     }
345     return true;
346 }
347 } // namespace Telephony
348 } // namespace OHOS
349