10a5c7a17Sopenharmony_ci/* 20a5c7a17Sopenharmony_ci * Copyright (c) 2020-2022 Huawei Device Co., Ltd. 30a5c7a17Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 40a5c7a17Sopenharmony_ci * you may not use this file except in compliance with the License. 50a5c7a17Sopenharmony_ci * You may obtain a copy of the License at 60a5c7a17Sopenharmony_ci * 70a5c7a17Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 80a5c7a17Sopenharmony_ci * 90a5c7a17Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 100a5c7a17Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 110a5c7a17Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 120a5c7a17Sopenharmony_ci * See the License for the specific language governing permissions and 130a5c7a17Sopenharmony_ci * limitations under the License. 140a5c7a17Sopenharmony_ci */ 150a5c7a17Sopenharmony_ci#include "camera_service.h" 160a5c7a17Sopenharmony_ci#include "hal_camera.h" 170a5c7a17Sopenharmony_ci#include "media_log.h" 180a5c7a17Sopenharmony_ci#include "codec_interface.h" 190a5c7a17Sopenharmony_ci 200a5c7a17Sopenharmony_ciusing namespace std; 210a5c7a17Sopenharmony_cinamespace OHOS { 220a5c7a17Sopenharmony_cinamespace Media { 230a5c7a17Sopenharmony_ciCameraService::CameraService() {} 240a5c7a17Sopenharmony_ci 250a5c7a17Sopenharmony_ciCameraService::~CameraService() 260a5c7a17Sopenharmony_ci{ 270a5c7a17Sopenharmony_ci auto iter = deviceMap_.begin(); 280a5c7a17Sopenharmony_ci while (iter != deviceMap_.end()) { 290a5c7a17Sopenharmony_ci if (iter->second != nullptr) { 300a5c7a17Sopenharmony_ci iter->second->StopLoopingCapture(-1); 310a5c7a17Sopenharmony_ci int32_t ret = HalCameraDeviceClose((uint32_t)std::atoi(iter->first.c_str())); 320a5c7a17Sopenharmony_ci if (ret != 0) { 330a5c7a17Sopenharmony_ci MEDIA_ERR_LOG("HalCameraDeviceClose failed. ret(%d)", ret); 340a5c7a17Sopenharmony_ci } 350a5c7a17Sopenharmony_ci deviceMap_.erase(iter++); 360a5c7a17Sopenharmony_ci } else { 370a5c7a17Sopenharmony_ci ++iter; 380a5c7a17Sopenharmony_ci } 390a5c7a17Sopenharmony_ci } 400a5c7a17Sopenharmony_ci int32_t ret = HalCameraDeinit(); 410a5c7a17Sopenharmony_ci if (ret != 0) { 420a5c7a17Sopenharmony_ci MEDIA_ERR_LOG("HiCameraDeInit return failed ret(%d).", ret); 430a5c7a17Sopenharmony_ci } 440a5c7a17Sopenharmony_ci} 450a5c7a17Sopenharmony_ci 460a5c7a17Sopenharmony_ciCameraService *CameraService::GetInstance() 470a5c7a17Sopenharmony_ci{ 480a5c7a17Sopenharmony_ci static CameraService instance; 490a5c7a17Sopenharmony_ci return &instance; 500a5c7a17Sopenharmony_ci} 510a5c7a17Sopenharmony_ci 520a5c7a17Sopenharmony_civoid CameraService::Initialize() 530a5c7a17Sopenharmony_ci{ 540a5c7a17Sopenharmony_ci int32_t ret = HalCameraInit(); 550a5c7a17Sopenharmony_ci if (ret != 0) { 560a5c7a17Sopenharmony_ci MEDIA_ERR_LOG("HiCameraInit failed. ret(%d)", ret); 570a5c7a17Sopenharmony_ci } 580a5c7a17Sopenharmony_ci} 590a5c7a17Sopenharmony_ci 600a5c7a17Sopenharmony_ciCameraAbility *CameraService::GetCameraAbility(std::string &cameraId) 610a5c7a17Sopenharmony_ci{ 620a5c7a17Sopenharmony_ci std::map<string, CameraAbility*>::iterator iter = deviceAbilityMap_.find(cameraId); 630a5c7a17Sopenharmony_ci if (iter != deviceAbilityMap_.end()) { 640a5c7a17Sopenharmony_ci return iter->second; 650a5c7a17Sopenharmony_ci } 660a5c7a17Sopenharmony_ci CameraAbility *ability = new (nothrow) CameraAbility; 670a5c7a17Sopenharmony_ci if (ability == nullptr) { 680a5c7a17Sopenharmony_ci return nullptr; 690a5c7a17Sopenharmony_ci } 700a5c7a17Sopenharmony_ci uint32_t streamCapNum; 710a5c7a17Sopenharmony_ci StreamCap *streamCap = nullptr; 720a5c7a17Sopenharmony_ci int32_t ret = HalCameraGetStreamCapNum(atoi(cameraId.c_str()), &streamCapNum); 730a5c7a17Sopenharmony_ci streamCap = new StreamCap[streamCapNum]; 740a5c7a17Sopenharmony_ci for (uint32_t pos = 0; pos < streamCapNum; pos++) { 750a5c7a17Sopenharmony_ci streamCap[pos].type = CAP_DESC_ENUM; 760a5c7a17Sopenharmony_ci } 770a5c7a17Sopenharmony_ci ret = HalCameraGetStreamCap(atoi(cameraId.c_str()), streamCap, streamCapNum); 780a5c7a17Sopenharmony_ci list<CameraPicSize> range; 790a5c7a17Sopenharmony_ci for (int pos = 0; pos < streamCapNum; pos++) { 800a5c7a17Sopenharmony_ci CameraPicSize tmpSize = {.width = (uint32_t)streamCap[pos].u.formatEnum.width, 810a5c7a17Sopenharmony_ci .height = (uint32_t)streamCap[pos].u.formatEnum.height}; 820a5c7a17Sopenharmony_ci range.emplace_back(tmpSize); 830a5c7a17Sopenharmony_ci } 840a5c7a17Sopenharmony_ci ability->SetParameterRange(CAM_FORMAT_YVU420, range); 850a5c7a17Sopenharmony_ci ability->SetParameterRange(CAM_FORMAT_JPEG, range); 860a5c7a17Sopenharmony_ci ability->SetParameterRange(CAM_FORMAT_H264, range); 870a5c7a17Sopenharmony_ci ability->SetParameterRange(CAM_FORMAT_H265, range); 880a5c7a17Sopenharmony_ci AbilityInfo cameraAbility = {}; 890a5c7a17Sopenharmony_ci HalCameraGetAbility(atoi(cameraId.c_str()), &cameraAbility); 900a5c7a17Sopenharmony_ci list<int32_t> afModes; 910a5c7a17Sopenharmony_ci for (int i = 0; i < cameraAbility.afModeNum; i++) { 920a5c7a17Sopenharmony_ci afModes.emplace_back(cameraAbility.afModes[i]); 930a5c7a17Sopenharmony_ci } 940a5c7a17Sopenharmony_ci ability->SetParameterRange(CAM_AF_MODE, afModes); 950a5c7a17Sopenharmony_ci list<int32_t> aeModes; 960a5c7a17Sopenharmony_ci for (int i = 0; i < cameraAbility.aeModeNum; i++) { 970a5c7a17Sopenharmony_ci aeModes.emplace_back(cameraAbility.aeModes[i]); 980a5c7a17Sopenharmony_ci } 990a5c7a17Sopenharmony_ci ability->SetParameterRange(CAM_AE_MODE, aeModes); 1000a5c7a17Sopenharmony_ci delete[] streamCap; 1010a5c7a17Sopenharmony_ci deviceAbilityMap_.insert(pair<string, CameraAbility*>(cameraId, ability)); 1020a5c7a17Sopenharmony_ci return ability; 1030a5c7a17Sopenharmony_ci} 1040a5c7a17Sopenharmony_ci 1050a5c7a17Sopenharmony_ciCameraInfo *CameraService::GetCameraInfo(std::string &cameraId) 1060a5c7a17Sopenharmony_ci{ 1070a5c7a17Sopenharmony_ci std::map<string, CameraInfo*>::iterator iter = deviceInfoMap_.find(cameraId); 1080a5c7a17Sopenharmony_ci if (iter != deviceInfoMap_.end()) { 1090a5c7a17Sopenharmony_ci return iter->second; 1100a5c7a17Sopenharmony_ci } 1110a5c7a17Sopenharmony_ci AbilityInfo deviceAbility; 1120a5c7a17Sopenharmony_ci int32_t ret = HalCameraGetAbility((uint32_t)std::atoi(cameraId.c_str()), &deviceAbility); 1130a5c7a17Sopenharmony_ci if (ret != MEDIA_OK) { 1140a5c7a17Sopenharmony_ci MEDIA_ERR_LOG("HalCameraGetAbility failed. ret(%d)", ret); 1150a5c7a17Sopenharmony_ci return nullptr; 1160a5c7a17Sopenharmony_ci } 1170a5c7a17Sopenharmony_ci CameraInfo *info = new (nothrow) CameraInfoImpl(deviceAbility.type, deviceAbility.orientation); 1180a5c7a17Sopenharmony_ci if (info == nullptr) { 1190a5c7a17Sopenharmony_ci return nullptr; 1200a5c7a17Sopenharmony_ci } 1210a5c7a17Sopenharmony_ci deviceInfoMap_.insert(pair<string, CameraInfo*>(cameraId, info)); 1220a5c7a17Sopenharmony_ci return info; 1230a5c7a17Sopenharmony_ci} 1240a5c7a17Sopenharmony_ci 1250a5c7a17Sopenharmony_ciCameraDevice *CameraService::GetCameraDevice(std::string &cameraId) 1260a5c7a17Sopenharmony_ci{ 1270a5c7a17Sopenharmony_ci std::map<string, CameraDevice*>::iterator iter = deviceMap_.find(cameraId); 1280a5c7a17Sopenharmony_ci if (iter != deviceMap_.end()) { 1290a5c7a17Sopenharmony_ci return iter->second; 1300a5c7a17Sopenharmony_ci } 1310a5c7a17Sopenharmony_ci return nullptr; 1320a5c7a17Sopenharmony_ci} 1330a5c7a17Sopenharmony_ci 1340a5c7a17Sopenharmony_cilist<string> CameraService::GetCameraIdList() 1350a5c7a17Sopenharmony_ci{ 1360a5c7a17Sopenharmony_ci uint8_t camNum = 0; 1370a5c7a17Sopenharmony_ci HalCameraGetDeviceNum(&camNum); 1380a5c7a17Sopenharmony_ci uint32_t *cameraList = new uint32_t[camNum]; 1390a5c7a17Sopenharmony_ci HalCameraGetDeviceList(cameraList, camNum); 1400a5c7a17Sopenharmony_ci list<string> cameraStrList; 1410a5c7a17Sopenharmony_ci for (uint32_t pos = 0; pos < camNum; pos++) { 1420a5c7a17Sopenharmony_ci cameraStrList.push_back(to_string(cameraList[pos])); 1430a5c7a17Sopenharmony_ci } 1440a5c7a17Sopenharmony_ci delete[] cameraList; 1450a5c7a17Sopenharmony_ci return cameraStrList; 1460a5c7a17Sopenharmony_ci} 1470a5c7a17Sopenharmony_ci 1480a5c7a17Sopenharmony_ciuint8_t CameraService::GetCameraModeNum() 1490a5c7a17Sopenharmony_ci{ 1500a5c7a17Sopenharmony_ci uint8_t num; 1510a5c7a17Sopenharmony_ci int32_t ret = HalCameraGetModeNum(&num); 1520a5c7a17Sopenharmony_ci if (ret == MEDIA_OK) { 1530a5c7a17Sopenharmony_ci return num; 1540a5c7a17Sopenharmony_ci } 1550a5c7a17Sopenharmony_ci return 0; 1560a5c7a17Sopenharmony_ci} 1570a5c7a17Sopenharmony_ci 1580a5c7a17Sopenharmony_ciint32_t CameraService::CreateCamera(string cameraId) 1590a5c7a17Sopenharmony_ci{ 1600a5c7a17Sopenharmony_ci int32_t ret = HalCameraDeviceOpen((uint32_t)std::atoi(cameraId.c_str())); 1610a5c7a17Sopenharmony_ci if (ret != 0) { 1620a5c7a17Sopenharmony_ci MEDIA_ERR_LOG("HalCameraDeviceOpen failed. ret(%d)", ret); 1630a5c7a17Sopenharmony_ci return CameraServiceCallback::CAMERA_STATUS_CREATE_FAILED; 1640a5c7a17Sopenharmony_ci } 1650a5c7a17Sopenharmony_ci CameraDevice *device = new (nothrow) CameraDevice((uint32_t)std::atoi(cameraId.c_str())); 1660a5c7a17Sopenharmony_ci if (device == nullptr) { 1670a5c7a17Sopenharmony_ci MEDIA_FATAL_LOG("New device object failed."); 1680a5c7a17Sopenharmony_ci return MEDIA_ERR; 1690a5c7a17Sopenharmony_ci } 1700a5c7a17Sopenharmony_ci if (device->Initialize() != MEDIA_OK) { 1710a5c7a17Sopenharmony_ci MEDIA_FATAL_LOG("device Initialize failed."); 1720a5c7a17Sopenharmony_ci delete device; 1730a5c7a17Sopenharmony_ci return MEDIA_ERR; 1740a5c7a17Sopenharmony_ci } 1750a5c7a17Sopenharmony_ci deviceMap_.insert(pair<string, CameraDevice*>(cameraId, device)); 1760a5c7a17Sopenharmony_ci return CameraServiceCallback::CAMERA_STATUS_CREATED; 1770a5c7a17Sopenharmony_ci} 1780a5c7a17Sopenharmony_ci 1790a5c7a17Sopenharmony_ciint32_t CameraService::CloseCamera(string cameraId) 1800a5c7a17Sopenharmony_ci{ 1810a5c7a17Sopenharmony_ci CameraDevice *device = GetCameraDevice(cameraId); 1820a5c7a17Sopenharmony_ci if (device != NULL) { 1830a5c7a17Sopenharmony_ci device->StopLoopingCapture(-1); 1840a5c7a17Sopenharmony_ci deviceMap_.erase(cameraId); 1850a5c7a17Sopenharmony_ci } 1860a5c7a17Sopenharmony_ci int32_t ret = HalCameraDeviceClose((uint32_t)std::atoi(cameraId.c_str())); 1870a5c7a17Sopenharmony_ci if (ret != 0) { 1880a5c7a17Sopenharmony_ci MEDIA_ERR_LOG("HalCameraDeviceClose failed. ret(%d)", ret); 1890a5c7a17Sopenharmony_ci } 1900a5c7a17Sopenharmony_ci return CameraServiceCallback::CAMERA_STATUS_CLOSE; 1910a5c7a17Sopenharmony_ci} 1920a5c7a17Sopenharmony_ci 1930a5c7a17Sopenharmony_ciint32_t CameraService::SetCameraMode(uint8_t modeIndex) 1940a5c7a17Sopenharmony_ci{ 1950a5c7a17Sopenharmony_ci CodecDeinit(); 1960a5c7a17Sopenharmony_ci int32_t ret = HalCameraSetMode(modeIndex); 1970a5c7a17Sopenharmony_ci CodecInit(); 1980a5c7a17Sopenharmony_ci return ret; 1990a5c7a17Sopenharmony_ci} 2000a5c7a17Sopenharmony_ci} // namespace Media 2010a5c7a17Sopenharmony_ci} // namespace OHOS