• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstring>
17 #include "camera_util.h"
18 #include "iservice_registry.h"
19 #include "media_log.h"
20 #include "system_ability_definition.h"
21 
22 #include "ipc_skeleton.h"
23 #include "input/camera_manager.h"
24 
25 using namespace std;
26 namespace OHOS {
27 namespace CameraStandard {
28 sptr<CameraManager> CameraManager::cameraManager_;
29 
30 const std::string CameraManager::surfaceFormat = "CAMERA_SURFACE_FORMAT";
31 
CameraManager()32 CameraManager::CameraManager()
33 {
34     Init();
35     cameraObjList = {};
36 }
37 
CreateListenerObject()38 int32_t CameraManager::CreateListenerObject()
39 {
40     listenerStub_ = new(std::nothrow) CameraListenerStub();
41     CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, CAMERA_ALLOC_ERROR,
42         "failed to new CameraListenerStub object");
43     CHECK_AND_RETURN_RET_LOG(serviceProxy_ != nullptr, CAMERA_ALLOC_ERROR,
44         "Camera service does not exist.");
45 
46     sptr<IRemoteObject> object = listenerStub_->AsObject();
47     CHECK_AND_RETURN_RET_LOG(object != nullptr, CAMERA_ALLOC_ERROR, "listener object is nullptr..");
48 
49     MEDIA_DEBUG_LOG("CreateListenerObject");
50     return serviceProxy_->SetListenerObject(object);
51 }
52 
53 class CameraStatusServiceCallback : public HCameraServiceCallbackStub {
54 public:
55     sptr<CameraManager> camMngr_ = nullptr;
CameraStatusServiceCallback()56     CameraStatusServiceCallback() : camMngr_(nullptr) {
57     }
58 
CameraStatusServiceCallback(const sptr<CameraManager> & cameraManager)59     explicit CameraStatusServiceCallback(const sptr<CameraManager>& cameraManager) : camMngr_(cameraManager) {
60     }
61 
~CameraStatusServiceCallback()62     ~CameraStatusServiceCallback()
63     {
64         camMngr_ = nullptr;
65     }
66 
OnCameraStatusChanged(const std::string & cameraId,const CameraStatus status)67     int32_t OnCameraStatusChanged(const std::string& cameraId, const CameraStatus status) override
68     {
69         CameraDeviceStatus deviceStatus;
70         CameraStatusInfo cameraStatusInfo;
71 
72         MEDIA_INFO_LOG("OnCameraStatusChanged: cameraId: %{public}s, status: %{public}d", cameraId.c_str(), status);
73         if (camMngr_ != nullptr && camMngr_->GetApplicationCallback() != nullptr) {
74             switch (status) {
75                 case CAMERA_STATUS_UNAVAILABLE:
76                     deviceStatus = CAMERA_DEVICE_STATUS_UNAVAILABLE;
77                     break;
78 
79                 case CAMERA_STATUS_AVAILABLE:
80                     deviceStatus = CAMERA_DEVICE_STATUS_AVAILABLE;
81                     break;
82 
83                 default:
84                     MEDIA_ERR_LOG("Unknown camera status: %{public}d", status);
85                     return CAMERA_INVALID_ARG;
86             }
87             cameraStatusInfo.cameraInfo = camMngr_->GetCameraInfo(cameraId);
88             cameraStatusInfo.cameraStatus = deviceStatus;
89             if (cameraStatusInfo.cameraInfo) {
90                 camMngr_->GetApplicationCallback()->OnCameraStatusChanged(cameraStatusInfo);
91             }
92         } else {
93             MEDIA_INFO_LOG("CameraManager::Callback not registered!, Ignore the callback");
94         }
95         return CAMERA_OK;
96     }
97 
OnFlashlightStatusChanged(const std::string & cameraId,const FlashStatus status)98     int32_t OnFlashlightStatusChanged(const std::string& cameraId, const FlashStatus status) override
99     {
100         FlashlightStatus flashlightStatus;
101 
102         MEDIA_INFO_LOG("OnFlashlightStatusChanged: cameraId: %{public}s, status: %{public}d", cameraId.c_str(), status);
103         if (camMngr_ != nullptr && camMngr_->GetApplicationCallback() != nullptr) {
104             switch (status) {
105                 case FLASH_STATUS_OFF:
106                     flashlightStatus = FLASHLIGHT_STATUS_OFF;
107                     break;
108 
109                 case FLASH_STATUS_ON:
110                     flashlightStatus = FLASHLIGHT_STATUS_ON;
111                     break;
112 
113                 case FLASH_STATUS_UNAVAILABLE:
114                     flashlightStatus = FLASHLIGHT_STATUS_UNAVAILABLE;
115                     break;
116 
117                 default:
118                     MEDIA_ERR_LOG("Unknown flashlight status: %{public}d", status);
119                     return CAMERA_INVALID_ARG;
120             }
121             camMngr_->GetApplicationCallback()->OnFlashlightStatusChanged(cameraId, flashlightStatus);
122         } else {
123             MEDIA_INFO_LOG("CameraManager::Callback not registered!, Ignore the callback");
124         }
125         return CAMERA_OK;
126     }
127 };
128 
CreateCaptureSession()129 sptr<CaptureSession> CameraManager::CreateCaptureSession()
130 {
131     sptr<ICaptureSession> captureSession = nullptr;
132     sptr<CaptureSession> result = nullptr;
133     int32_t retCode = CAMERA_OK;
134 
135     if (serviceProxy_ == nullptr) {
136         MEDIA_ERR_LOG("CameraManager::CreateCaptureSession serviceProxy_ is null");
137         return nullptr;
138     }
139     retCode = serviceProxy_->CreateCaptureSession(captureSession);
140     if (retCode == CAMERA_OK && captureSession != nullptr) {
141         result = new CaptureSession(captureSession);
142     } else {
143         MEDIA_ERR_LOG("Failed to get capture session object from hcamera service!, %{public}d", retCode);
144     }
145     return result;
146 }
147 
CreatePhotoOutput(sptr<Surface> & surface)148 sptr<PhotoOutput> CameraManager::CreatePhotoOutput(sptr<Surface> &surface)
149 {
150     sptr<IStreamCapture> streamCapture = nullptr;
151     sptr<PhotoOutput> result = nullptr;
152     int32_t retCode = CAMERA_OK;
153 
154     if (serviceProxy_ == nullptr || surface == nullptr) {
155         MEDIA_ERR_LOG("CameraManager::CreatePhotoOutput serviceProxy_ is null or surface is null");
156         return nullptr;
157     }
158     std::string format = surface->GetUserData(surfaceFormat);
159     retCode = serviceProxy_->CreatePhotoOutput(surface->GetProducer(), std::stoi(format), streamCapture);
160     if (retCode == CAMERA_OK) {
161         result = new PhotoOutput(streamCapture);
162     } else {
163         MEDIA_ERR_LOG("Failed to get stream capture object from hcamera service!, %{public}d", retCode);
164     }
165     return result;
166 }
167 
CreatePhotoOutput(const sptr<OHOS::IBufferProducer> & producer,int32_t format)168 sptr<PhotoOutput> CameraManager::CreatePhotoOutput(const sptr<OHOS::IBufferProducer> &producer, int32_t format)
169 {
170     sptr<IStreamCapture> streamCapture = nullptr;
171     sptr<PhotoOutput> result = nullptr;
172     int32_t retCode = CAMERA_OK;
173 
174     if (serviceProxy_ == nullptr || producer == nullptr) {
175         MEDIA_ERR_LOG("CameraManager::CreatePhotoOutput serviceProxy_ is null or producer is null");
176         return nullptr;
177     }
178     retCode = serviceProxy_->CreatePhotoOutput(producer, format, streamCapture);
179     if (retCode == CAMERA_OK) {
180         result = new PhotoOutput(streamCapture);
181     } else {
182         MEDIA_ERR_LOG("Failed to get stream capture object from hcamera service!, %{public}d", retCode);
183     }
184     return result;
185 }
186 
CreatePreviewOutput(sptr<Surface> surface)187 sptr<PreviewOutput> CameraManager::CreatePreviewOutput(sptr<Surface> surface)
188 {
189     sptr<IStreamRepeat> streamRepeat = nullptr;
190     sptr<PreviewOutput> result = nullptr;
191     int32_t retCode = CAMERA_OK;
192 
193     if (serviceProxy_ == nullptr || surface == nullptr) {
194         MEDIA_ERR_LOG("CameraManager::CreatePreviewOutput serviceProxy_ is null or surface is null");
195         return nullptr;
196     }
197     std::string format = surface->GetUserData(surfaceFormat);
198     retCode = serviceProxy_->CreatePreviewOutput(surface->GetProducer(), std::stoi(format), streamRepeat);
199     if (retCode == CAMERA_OK) {
200         result = new PreviewOutput(streamRepeat);
201     } else {
202         MEDIA_ERR_LOG("PreviewOutput: Failed to get stream repeat object from hcamera service!, %{public}d", retCode);
203     }
204     return result;
205 }
206 
CreatePreviewOutput(const sptr<OHOS::IBufferProducer> & producer,int32_t format)207 sptr<PreviewOutput> CameraManager::CreatePreviewOutput(const sptr<OHOS::IBufferProducer> &producer, int32_t format)
208 {
209     sptr<IStreamRepeat> streamRepeat = nullptr;
210     sptr<PreviewOutput> result = nullptr;
211     int32_t retCode = CAMERA_OK;
212 
213     if (serviceProxy_ == nullptr || producer == nullptr) {
214         MEDIA_ERR_LOG("CameraManager::CreatePreviewOutput serviceProxy_ is null or producer is null");
215         return nullptr;
216     }
217     retCode = serviceProxy_->CreatePreviewOutput(producer, format, streamRepeat);
218     if (retCode == CAMERA_OK) {
219         result = new PreviewOutput(streamRepeat);
220     } else {
221         MEDIA_ERR_LOG("PreviewOutput: Failed to get stream repeat object from hcamera service!, %{public}d", retCode);
222     }
223     return result;
224 }
225 
CreateCustomPreviewOutput(sptr<Surface> surface,int32_t width,int32_t height)226 sptr<PreviewOutput> CameraManager::CreateCustomPreviewOutput(sptr<Surface> surface, int32_t width, int32_t height)
227 {
228     sptr<IStreamRepeat> streamRepeat = nullptr;
229     sptr<PreviewOutput> result = nullptr;
230     int32_t retCode = CAMERA_OK;
231 
232     if (serviceProxy_ == nullptr || surface == nullptr || width == 0 || height == 0) {
233         MEDIA_ERR_LOG("CameraManager::CreatePreviewOutput serviceProxy_ is null or surface is null or invalid size");
234         return nullptr;
235     }
236     std::string format = surface->GetUserData(surfaceFormat);
237     retCode = serviceProxy_->CreateCustomPreviewOutput(surface->GetProducer(), std::stoi(format), width, height,
238                                                        streamRepeat);
239     if (retCode == CAMERA_OK) {
240         result = new PreviewOutput(streamRepeat);
241     } else {
242         MEDIA_ERR_LOG("PreviewOutput: Failed to get stream repeat object from hcamera service!, %{public}d", retCode);
243     }
244     return result;
245 }
246 
CreateCustomPreviewOutput(const sptr<OHOS::IBufferProducer> & producer,int32_t format,int32_t width,int32_t height)247 sptr<PreviewOutput> CameraManager::CreateCustomPreviewOutput(const sptr<OHOS::IBufferProducer> &producer,
248                                                              int32_t format, int32_t width, int32_t height)
249 {
250     sptr<IStreamRepeat> streamRepeat = nullptr;
251     sptr<PreviewOutput> result = nullptr;
252     int32_t retCode = CAMERA_OK;
253 
254     if ((serviceProxy_ == nullptr) || (producer == nullptr) || (width == 0) || (height == 0)) {
255         MEDIA_ERR_LOG("CameraManager::CreatePreviewOutput serviceProxy_ is null or producer is null or invalid size");
256         return nullptr;
257     }
258     retCode = serviceProxy_->CreateCustomPreviewOutput(producer, format, width, height, streamRepeat);
259     if (retCode == CAMERA_OK) {
260         result = new PreviewOutput(streamRepeat);
261     } else {
262         MEDIA_ERR_LOG("PreviewOutput: Failed to get stream repeat object from hcamera service!, %{public}d", retCode);
263     }
264     return result;
265 }
266 
CreateVideoOutput(sptr<Surface> & surface)267 sptr<VideoOutput> CameraManager::CreateVideoOutput(sptr<Surface> &surface)
268 {
269     sptr<IStreamRepeat> streamRepeat = nullptr;
270     sptr<VideoOutput> result = nullptr;
271     int32_t retCode = CAMERA_OK;
272 
273     if (serviceProxy_ == nullptr || surface == nullptr) {
274         MEDIA_ERR_LOG("CameraManager::CreateVideoOutput serviceProxy_ is null or surface is null");
275         return nullptr;
276     }
277     std::string format = surface->GetUserData(surfaceFormat);
278     retCode = serviceProxy_->CreateVideoOutput(surface->GetProducer(), std::stoi(format), streamRepeat);
279     if (retCode == CAMERA_OK) {
280         result = new VideoOutput(streamRepeat);
281     } else {
282         MEDIA_ERR_LOG("VideoOutpout: Failed to get stream repeat object from hcamera service! %{public}d", retCode);
283     }
284     return result;
285 }
286 
CreateVideoOutput(const sptr<OHOS::IBufferProducer> & producer,int32_t format)287 sptr<VideoOutput> CameraManager::CreateVideoOutput(const sptr<OHOS::IBufferProducer> &producer, int32_t format)
288 {
289     sptr<IStreamRepeat> streamRepeat = nullptr;
290     sptr<VideoOutput> result = nullptr;
291     int32_t retCode = CAMERA_OK;
292 
293     if (serviceProxy_ == nullptr || producer == nullptr) {
294         MEDIA_ERR_LOG("CameraManager::CreateVideoOutput serviceProxy_ is null or producer is null");
295         return nullptr;
296     }
297     retCode = serviceProxy_->CreateVideoOutput(producer, format, streamRepeat);
298     if (retCode == CAMERA_OK) {
299         result = new VideoOutput(streamRepeat);
300     } else {
301         MEDIA_ERR_LOG("VideoOutpout: Failed to get stream repeat object from hcamera service! %{public}d", retCode);
302     }
303     return result;
304 }
305 
Init()306 void CameraManager::Init()
307 {
308     sptr<IRemoteObject> object = nullptr;
309     cameraMngrCallback_ = nullptr;
310 
311     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
312     if (samgr == nullptr) {
313         MEDIA_ERR_LOG("Failed to get System ability manager");
314         return;
315     }
316     object = samgr->GetSystemAbility(CAMERA_SERVICE_ID);
317     if (object == nullptr) {
318         MEDIA_ERR_LOG("CameraManager::GetSystemAbility() is failed");
319         return;
320     }
321     serviceProxy_ = iface_cast<ICameraService>(object);
322     if (serviceProxy_ == nullptr) {
323         MEDIA_ERR_LOG("CameraManager::init serviceProxy_ is null.");
324         return;
325     } else {
326         sptr<CameraManager> helper = this;
327         cameraSvcCallback_ = new CameraStatusServiceCallback(helper);
328         SetCameraServiceCallback(cameraSvcCallback_);
329     }
330     pid_t pid = 0;
331     deathRecipient_ = new(std::nothrow) CameraDeathRecipient(pid);
332     CHECK_AND_RETURN_LOG(deathRecipient_ != nullptr, "failed to new CameraDeathRecipient.");
333 
334     deathRecipient_->SetNotifyCb(std::bind(&CameraManager::CameraServerDied, this, std::placeholders::_1));
335     bool result = object->AddDeathRecipient(deathRecipient_);
336     if (!result) {
337         MEDIA_ERR_LOG("failed to add deathRecipient");
338         return;
339     }
340 
341     int32_t ret = CreateListenerObject();
342     CHECK_AND_RETURN_LOG(ret == CAMERA_OK, "failed to new MediaListener.");
343 }
344 
CameraServerDied(pid_t pid)345 void CameraManager::CameraServerDied(pid_t pid)
346 {
347     MEDIA_ERR_LOG("camera server has died, pid:%{public}d!", pid);
348     if (serviceProxy_ != nullptr) {
349         (void)serviceProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
350         serviceProxy_ = nullptr;
351     }
352     listenerStub_ = nullptr;
353     deathRecipient_ = nullptr;
354 }
355 
CreateCameraDevice(std::string cameraId)356 sptr<ICameraDeviceService> CameraManager::CreateCameraDevice(std::string cameraId)
357 {
358     sptr<ICameraDeviceService> device = nullptr;
359     int32_t retCode = CAMERA_OK;
360 
361     if (serviceProxy_ == nullptr || cameraId.empty()) {
362         MEDIA_ERR_LOG("GetCameaDevice() serviceProxy_ is null or CameraID is empty: %{public}s", cameraId.c_str());
363         return nullptr;
364     }
365     retCode = serviceProxy_->CreateCameraDevice(cameraId, device);
366     if (retCode != CAMERA_OK) {
367         MEDIA_ERR_LOG("ret value from CreateCameraDevice, %{public}d", retCode);
368         return nullptr;
369     }
370     return device;
371 }
372 
SetCallback(std::shared_ptr<CameraManagerCallback> callback)373 void CameraManager::SetCallback(std::shared_ptr<CameraManagerCallback> callback)
374 {
375     if (callback == nullptr) {
376         MEDIA_INFO_LOG("CameraManager::SetCallback(): Application unregistering the callback");
377     }
378     cameraMngrCallback_ = callback;
379 }
380 
GetApplicationCallback()381 std::shared_ptr<CameraManagerCallback> CameraManager::GetApplicationCallback()
382 {
383     return cameraMngrCallback_;
384 }
385 
GetCameraInfo(std::string cameraId)386 sptr<CameraInfo> CameraManager::GetCameraInfo(std::string cameraId)
387 {
388     sptr<CameraInfo> cameraObj = nullptr;
389 
390     for (size_t i = 0; i < cameraObjList.size(); i++) {
391         if (cameraObjList[i]->GetID() == cameraId) {
392             cameraObj = cameraObjList[i];
393             break;
394         }
395     }
396     return cameraObj;
397 }
398 
GetInstance()399 sptr<CameraManager> &CameraManager::GetInstance()
400 {
401     if (CameraManager::cameraManager_ == nullptr) {
402         MEDIA_INFO_LOG("Initializing camera manager for first time!");
403         CameraManager::cameraManager_ = new CameraManager();
404     }
405     return CameraManager::cameraManager_;
406 }
407 
GetCameras()408 std::vector<sptr<CameraInfo>> CameraManager::GetCameras()
409 {
410     std::vector<std::string> cameraIds;
411     std::vector<std::shared_ptr<Camera::CameraMetadata>> cameraAbilityList;
412     int32_t retCode = -1;
413     sptr<CameraInfo> cameraObj = nullptr;
414     int32_t index = 0;
415 
416     if (cameraObjList.size() > 0) {
417         cameraObjList.clear();
418     }
419     if (serviceProxy_ == nullptr) {
420         MEDIA_ERR_LOG("CameraManager::SetCallback serviceProxy_ is null, returning empty list!");
421         return cameraObjList;
422     }
423     retCode = serviceProxy_->GetCameras(cameraIds, cameraAbilityList);
424     if (retCode == CAMERA_OK) {
425         for (auto& it : cameraIds) {
426             cameraObj = new CameraInfo(it, cameraAbilityList[index++]);
427             cameraObjList.emplace_back(cameraObj);
428         }
429     } else {
430         MEDIA_ERR_LOG("CameraManager::GetCameras failed!, retCode: %{public}d", retCode);
431     }
432     return cameraObjList;
433 }
434 
CreateCameraInput(sptr<CameraInfo> & camera)435 sptr<CameraInput> CameraManager::CreateCameraInput(sptr<CameraInfo> &camera)
436 {
437     sptr<CameraInput> cameraInput = nullptr;
438     sptr<ICameraDeviceService> deviceObj = nullptr;
439 
440     if (camera != nullptr) {
441         deviceObj = CreateCameraDevice(camera->GetID());
442         if (deviceObj != nullptr) {
443             cameraInput = new CameraInput(deviceObj, camera);
444         } else {
445             MEDIA_ERR_LOG("Returning null in CreateCameraInput");
446         }
447     } else {
448         MEDIA_ERR_LOG("CameraManager::CreateCameraInput: Camera object is null");
449     }
450     return cameraInput;
451 }
452 
SetCameraServiceCallback(sptr<ICameraServiceCallback> & callback)453 void CameraManager::SetCameraServiceCallback(sptr<ICameraServiceCallback>& callback)
454 {
455     int32_t retCode = CAMERA_OK;
456 
457     if (serviceProxy_ == nullptr) {
458         MEDIA_ERR_LOG("CameraManager::SetCallback serviceProxy_ is null");
459         return;
460     }
461     retCode = serviceProxy_->SetCallback(callback);
462     if (retCode != CAMERA_OK) {
463         MEDIA_ERR_LOG("CameraManager::Set service Callback failed, retCode: %{public}d", retCode);
464     }
465     return;
466 }
467 } // CameraStandard
468 } // OHOS
469