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
16 #include "camera_device_impl.h"
17 #include "camera_host_config.h"
18 #include "ipipeline_core.h"
19 #include "idevice_manager.h"
20
21 namespace OHOS::Camera {
CreateCameraDevice(const std::string & cameraId)22 std::shared_ptr<CameraDevice> CameraDevice::CreateCameraDevice(const std::string &cameraId)
23 {
24 // create pipelineCore
25 std::shared_ptr<IPipelineCore> pipelineCore = IPipelineCore::Create();
26 if (pipelineCore == nullptr) {
27 CAMERA_LOGW("create pipeline core failed. [cameraId = %{public}s]", cameraId.c_str());
28 return nullptr;
29 }
30
31 RetCode rc = pipelineCore->Init();
32 if (rc != RC_OK) {
33 CAMERA_LOGW("pipeline core init failed. [cameraId = %{public}s]", cameraId.c_str());
34 return nullptr;
35 }
36
37 std::shared_ptr<CameraDeviceImpl> device = std::make_shared<CameraDeviceImpl>(cameraId, pipelineCore);
38 if (device == nullptr) {
39 CAMERA_LOGW("create camera device failed. [cameraId = %{public}s]", cameraId.c_str());
40 return nullptr;
41 }
42 CAMERA_LOGD("create camera device success. [cameraId = %{public}s]", cameraId.c_str());
43
44 // set deviceManager metadata & dev status callback
45 std::shared_ptr<IDeviceManager> deviceManager = IDeviceManager::GetInstance();
46 if (deviceManager != nullptr) {
47 deviceManager->SetMetaDataCallBack([device](const std::shared_ptr<CameraMetadata> &metadata) {
48 std::static_pointer_cast<CameraDevice>(device)->OnMetadataChanged(metadata);
49 });
50 deviceManager->SetDevStatusCallBack([device]() {
51 std::static_pointer_cast<CameraDevice>(device)->OnDevStatusErr();
52 });
53 SetMemoryType(deviceManager, cameraId);
54 }
55
56 return device;
57 }
58
SetMemoryType(std::shared_ptr<IDeviceManager> deviceManager,const std::string & cameraId)59 void CameraDevice::SetMemoryType(std::shared_ptr<IDeviceManager> deviceManager, const std::string &cameraId)
60 {
61 std::shared_ptr<CameraAbility> ability = nullptr;
62 CameraHostConfig *config = CameraHostConfig::GetInstance();
63 if (config == nullptr) {
64 return;
65 }
66 RetCode rc = config->GetCameraAbility(cameraId, ability);
67 if (rc != RC_OK) {
68 return;
69 }
70 common_metadata_header_t *metadata = ability->get();
71 if (metadata == nullptr) {
72 CAMERA_LOGE("CameraDevice::SetMemoryType ability get metadata is null.");
73 return;
74 }
75 camera_metadata_item_t entry;
76 int ret = FindCameraMetadataItem(metadata, OHOS_ABILITY_MEMORY_TYPE, &entry);
77 if (ret != 0) {
78 CAMERA_LOGE("CameraDevice::SetMemoryType FindCameraMetadataItem err.");
79 return;
80 }
81 uint8_t memType = *(entry.data.u8);
82 CAMERA_LOGD("func[CameraDevice::%{public}s] memType[%{public}d]", __func__, memType);
83 deviceManager->SetMemoryType(memType);
84 return;
85 }
86 } // end namespace OHOS::Camera
87
88