1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "VsockCameraDevice"
18 //#define LOG_NDEBUG 0
19 #include <log/log.h>
20
21 #include <algorithm>
22 #include <array>
23 #include "CameraMetadata.h"
24 #include "android-base/macros.h"
25 #include "include/convert.h"
26 #include "vsock_camera_device_3_4.h"
27 #include "vsock_camera_device_session_3_4.h"
28
29 namespace android::hardware::camera::device::V3_4::implementation {
30
VsockCameraDevice(const std::string & id,const Settings & settings,std::shared_ptr<cuttlefish::VsockConnection> connection)31 VsockCameraDevice::VsockCameraDevice(
32 const std::string& id, const Settings& settings,
33 std::shared_ptr<cuttlefish::VsockConnection> connection)
34 : id_(id),
35 metadata_(settings.width, settings.height, settings.frame_rate),
36 connection_(connection),
37 is_open_(false) {
38 ALOGI("%s", __FUNCTION__);
39 }
40
~VsockCameraDevice()41 VsockCameraDevice::~VsockCameraDevice() { ALOGI("%s", __FUNCTION__); }
42
getResourceCost(ICameraDevice::getResourceCost_cb _hidl_cb)43 Return<void> VsockCameraDevice::getResourceCost(
44 ICameraDevice::getResourceCost_cb _hidl_cb) {
45 CameraResourceCost resCost;
46 resCost.resourceCost = 100;
47 _hidl_cb(Status::OK, resCost);
48 return Void();
49 }
50
getCameraCharacteristics(ICameraDevice::getCameraCharacteristics_cb _hidl_cb)51 Return<void> VsockCameraDevice::getCameraCharacteristics(
52 ICameraDevice::getCameraCharacteristics_cb _hidl_cb) {
53 V3_2::CameraMetadata hidl_vec;
54 const camera_metadata_t* metadata_ptr = metadata_.getAndLock();
55 V3_2::implementation::convertToHidl(metadata_ptr, &hidl_vec);
56 _hidl_cb(Status::OK, hidl_vec);
57 metadata_.unlock(metadata_ptr);
58 return Void();
59 }
60
setTorchMode(TorchMode)61 Return<Status> VsockCameraDevice::setTorchMode(TorchMode) {
62 return Status::OPERATION_NOT_SUPPORTED;
63 }
64
open(const sp<ICameraDeviceCallback> & callback,ICameraDevice::open_cb _hidl_cb)65 Return<void> VsockCameraDevice::open(const sp<ICameraDeviceCallback>& callback,
66 ICameraDevice::open_cb _hidl_cb) {
67 if (callback == nullptr) {
68 ALOGE("%s: cannot open camera %s. callback is null!", __FUNCTION__,
69 id_.c_str());
70 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
71 return Void();
72 }
73
74 bool was_open = is_open_.exchange(true);
75
76 if (was_open) {
77 ALOGE("%s: cannot open an already opened camera!", __FUNCTION__);
78 _hidl_cb(Status::CAMERA_IN_USE, nullptr);
79 return Void();
80 }
81 ALOGI("%s: Initializing device for camera %s", __FUNCTION__, id_.c_str());
82 frame_provider_ = std::make_shared<cuttlefish::VsockFrameProvider>();
83 frame_provider_->start(connection_, metadata_.getPreferredWidth(),
84 metadata_.getPreferredHeight());
85 session_ = new VsockCameraDeviceSession(metadata_, frame_provider_, callback);
86 _hidl_cb(Status::OK, session_);
87 return Void();
88 }
89
dumpState(const::android::hardware::hidl_handle & handle)90 Return<void> VsockCameraDevice::dumpState(
91 const ::android::hardware::hidl_handle& handle) {
92 if (handle.getNativeHandle() == nullptr) {
93 ALOGE("%s: handle must not be null", __FUNCTION__);
94 return Void();
95 }
96 if (handle->numFds != 1 || handle->numInts != 0) {
97 ALOGE("%s: handle must contain 1 FD and 0 integers! Got %d FDs and %d ints",
98 __FUNCTION__, handle->numFds, handle->numInts);
99 return Void();
100 }
101 int fd = handle->data[0];
102 dprintf(fd, "Camera:%s\n", id_.c_str());
103 return Void();
104 }
105
106 } // namespace android::hardware::camera::device::V3_4::implementation
107