1 /*
2 * Copyright 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 #define LOG_TAG "VsockCameraProvider"
17 #include "vsock_camera_provider_2_7.h"
18 #include <cutils/properties.h>
19 #include <log/log.h>
20 #include "vsock_camera_server.h"
21
22 namespace android::hardware::camera::provider::V2_7::implementation {
23
24 namespace {
25 VsockCameraServer gCameraServer;
26 constexpr auto kDeviceName = "device@3.4/external/0";
27 } // namespace
28
29 using android::hardware::camera::provider::V2_7::ICameraProvider;
HIDL_FETCH_ICameraProvider(const char * name)30 extern "C" ICameraProvider* HIDL_FETCH_ICameraProvider(const char* name) {
31 return (strcmp(name, "external/0") == 0)
32 ? new VsockCameraProvider(&gCameraServer)
33 : nullptr;
34 }
35
VsockCameraProvider(VsockCameraServer * server)36 VsockCameraProvider::VsockCameraProvider(VsockCameraServer* server) {
37 server_ = server;
38 if (!server->isRunning()) {
39 constexpr static const auto camera_port_property =
40 "ro.boot.vsock_camera_port";
41 constexpr static const auto camera_cid_property =
42 "ro.boot.vsock_camera_cid";
43 auto port = property_get_int32(camera_port_property, -1);
44 auto cid = property_get_int32(camera_cid_property, -1);
45 if (port > 0) {
46 server->start(port, cid);
47 }
48 }
49 }
50
~VsockCameraProvider()51 VsockCameraProvider::~VsockCameraProvider() {
52 server_->setConnectedCallback(nullptr);
53 }
54
setCallback(const sp<ICameraProviderCallback> & callback)55 Return<Status> VsockCameraProvider::setCallback(
56 const sp<ICameraProviderCallback>& callback) {
57 {
58 std::lock_guard<std::mutex> lock(mutex_);
59 callbacks_ = callback;
60 }
61 server_->setConnectedCallback(
62 [this](std::shared_ptr<cuttlefish::VsockConnection> connection,
63 VsockCameraDevice::Settings settings) {
64 connection_ = connection;
65 settings_ = settings;
66 deviceAdded(kDeviceName);
67 connection_->SetDisconnectCallback(
68 [this] { deviceRemoved(kDeviceName); });
69 });
70 return Status::OK;
71 }
72
getVendorTags(ICameraProvider::getVendorTags_cb _hidl_cb)73 Return<void> VsockCameraProvider::getVendorTags(
74 ICameraProvider::getVendorTags_cb _hidl_cb) {
75 // No vendor tag support
76 hidl_vec<VendorTagSection> empty;
77 _hidl_cb(Status::OK, empty);
78 return Void();
79 }
80
getCameraIdList(ICameraProvider::getCameraIdList_cb _hidl_cb)81 Return<void> VsockCameraProvider::getCameraIdList(
82 ICameraProvider::getCameraIdList_cb _hidl_cb) {
83 // External camera HAL always report 0 camera, and extra cameras
84 // are just reported via cameraDeviceStatusChange callbacks
85 hidl_vec<hidl_string> empty;
86 _hidl_cb(Status::OK, empty);
87 return Void();
88 }
89
isSetTorchModeSupported(ICameraProvider::isSetTorchModeSupported_cb _hidl_cb)90 Return<void> VsockCameraProvider::isSetTorchModeSupported(
91 ICameraProvider::isSetTorchModeSupported_cb _hidl_cb) {
92 // setTorchMode API is supported, though right now no external camera device
93 // has a flash unit.
94 _hidl_cb(Status::OK, true);
95 return Void();
96 }
97
getCameraDeviceInterface_V1_x(const hidl_string &,ICameraProvider::getCameraDeviceInterface_V1_x_cb _hidl_cb)98 Return<void> VsockCameraProvider::getCameraDeviceInterface_V1_x(
99 const hidl_string&,
100 ICameraProvider::getCameraDeviceInterface_V1_x_cb _hidl_cb) {
101 // External Camera HAL does not support HAL1
102 _hidl_cb(Status::OPERATION_NOT_SUPPORTED, nullptr);
103 return Void();
104 }
105
getCameraDeviceInterface_V3_x(const hidl_string & name_hidl_str,ICameraProvider::getCameraDeviceInterface_V3_x_cb _hidl_cb)106 Return<void> VsockCameraProvider::getCameraDeviceInterface_V3_x(
107 const hidl_string& name_hidl_str,
108 ICameraProvider::getCameraDeviceInterface_V3_x_cb _hidl_cb) {
109 std::string name(name_hidl_str.c_str());
110 if (name != kDeviceName) {
111 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
112 return Void();
113 }
114
115 _hidl_cb(Status::OK, new VsockCameraDevice(name, settings_, connection_));
116 return Void();
117 }
118
notifyDeviceStateChange(hardware::hidl_bitfield<DeviceState>)119 Return<void> VsockCameraProvider::notifyDeviceStateChange(
120 hardware::hidl_bitfield<DeviceState> /*newState*/) {
121 return Void();
122 }
123
getConcurrentStreamingCameraIds(getConcurrentStreamingCameraIds_cb _hidl_cb)124 Return<void> VsockCameraProvider::getConcurrentStreamingCameraIds(
125 getConcurrentStreamingCameraIds_cb _hidl_cb) {
126 hidl_vec<hidl_vec<hidl_string>> hidl_camera_id_combinations;
127 _hidl_cb(Status::OK, hidl_camera_id_combinations);
128 return Void();
129 }
130
isConcurrentStreamCombinationSupported(const hidl_vec<::android::hardware::camera::provider::V2_6::CameraIdAndStreamCombination> &,isConcurrentStreamCombinationSupported_cb _hidl_cb)131 Return<void> VsockCameraProvider::isConcurrentStreamCombinationSupported(
132 const hidl_vec<::android::hardware::camera::provider::V2_6::CameraIdAndStreamCombination>&,
133 isConcurrentStreamCombinationSupported_cb _hidl_cb) {
134 _hidl_cb(Status::OK, false);
135 return Void();
136 }
137
isConcurrentStreamCombinationSupported_2_7(const hidl_vec<::android::hardware::camera::provider::V2_7::CameraIdAndStreamCombination> &,isConcurrentStreamCombinationSupported_2_7_cb _hidl_cb)138 Return<void> VsockCameraProvider::isConcurrentStreamCombinationSupported_2_7(
139 const hidl_vec<::android::hardware::camera::provider::V2_7::CameraIdAndStreamCombination>&,
140 isConcurrentStreamCombinationSupported_2_7_cb _hidl_cb) {
141 _hidl_cb(Status::OK, false);
142 return Void();
143 }
144
deviceAdded(const char * name)145 void VsockCameraProvider::deviceAdded(const char* name) {
146 std::lock_guard<std::mutex> lock(mutex_);
147 if (callbacks_ != nullptr) {
148 callbacks_->cameraDeviceStatusChange(name, CameraDeviceStatus::PRESENT);
149 }
150 }
151
deviceRemoved(const char * name)152 void VsockCameraProvider::deviceRemoved(const char* name) {
153 std::lock_guard<std::mutex> lock(mutex_);
154 if (callbacks_ != nullptr) {
155 callbacks_->cameraDeviceStatusChange(name, CameraDeviceStatus::NOT_PRESENT);
156 }
157 }
158
159 } // namespace android::hardware::camera::provider::V2_7::implementation
160