1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "CamPrvdr@2.7-external"
//#define LOG_NDEBUG 0
#include <log/log.h>
#include <cutils/properties.h>
#include <errno.h>
#include <linux/videodev2.h>
#include <sys/inotify.h>
#include <regex>
#include "ExternalCameraDevice_3_4.h"
#include "ExternalCameraDevice_3_5.h"
#include "ExternalCameraDevice_3_6.h"
#include "ExternalCameraProviderImpl_2_7.h"
namespace android {
namespace hardware {
namespace camera {
namespace provider {
namespace V2_7 {
namespace implementation {
namespace {
// "device@<version>/external/<id>"
const std::regex kDeviceNameRE("device@([0-9]+\\.[0-9]+)/external/(.+)");
const int kMaxDevicePathLen = 256;
const char* kDevicePath = "/dev/";
constexpr char kPrefix[] = "video";
constexpr int kPrefixLen = sizeof(kPrefix) - 1;
constexpr int kDevicePrefixLen = sizeof(kDevicePath) + kPrefixLen + 1;
bool matchDeviceName(int cameraIdOffset, const hidl_string& deviceName, std::string* deviceVersion,
std::string* cameraDevicePath) {
std::string deviceNameStd(deviceName.c_str());
std::smatch sm;
if (std::regex_match(deviceNameStd, sm, kDeviceNameRE)) {
if (deviceVersion != nullptr) {
*deviceVersion = sm[1];
}
if (cameraDevicePath != nullptr) {
*cameraDevicePath = "/dev/video" + std::to_string(std::stoi(sm[2]) - cameraIdOffset);
}
return true;
}
return false;
}
} // anonymous namespace
ExternalCameraProviderImpl_2_7::ExternalCameraProviderImpl_2_7()
: mCfg(ExternalCameraConfig::loadFromCfg()) {
mHotPlugThread = sp<HotplugThread>::make(this);
mHotPlugThread->run("ExtCamHotPlug", PRIORITY_BACKGROUND);
mPreferredHal3MinorVersion =
property_get_int32("ro.vendor.camera.external.hal3TrebleMinorVersion", 4);
ALOGV("Preferred HAL 3 minor version is %d", mPreferredHal3MinorVersion);
switch (mPreferredHal3MinorVersion) {
case 4:
case 5:
case 6:
// OK
break;
default:
ALOGW("Unknown minor camera device HAL version %d in property "
"'camera.external.hal3TrebleMinorVersion', defaulting to 4",
mPreferredHal3MinorVersion);
mPreferredHal3MinorVersion = 4;
}
}
ExternalCameraProviderImpl_2_7::~ExternalCameraProviderImpl_2_7() {
mHotPlugThread->requestExit();
}
Return<Status> ExternalCameraProviderImpl_2_7::setCallback(
const sp<ICameraProviderCallback>& callback) {
Mutex::Autolock _l(mLock);
mCallbacks = callback;
if (mCallbacks == nullptr) {
return Status::OK;
}
// Send a callback for all devices to initialize
{
for (const auto& pair : mCameraStatusMap) {
mCallbacks->cameraDeviceStatusChange(pair.first, pair.second);
}
}
return Status::OK;
}
Return<void> ExternalCameraProviderImpl_2_7::getVendorTags(
ICameraProvider::getVendorTags_cb _hidl_cb) {
// No vendor tag support for USB camera
hidl_vec<VendorTagSection> zeroSections;
_hidl_cb(Status::OK, zeroSections);
return Void();
}
Return<void> ExternalCameraProviderImpl_2_7::getCameraIdList(
ICameraProvider::getCameraIdList_cb _hidl_cb) {
// External camera HAL always report 0 camera, and extra cameras
// are just reported via cameraDeviceStatusChange callbacks
hidl_vec<hidl_string> hidlDeviceNameList;
_hidl_cb(Status::OK, hidlDeviceNameList);
return Void();
}
void ExternalCameraProviderImpl_2_7::updateAttachedCameras() {
ALOGV("%s start scaning for existing V4L2 devices", __FUNCTION__);
// Find existing /dev/video* devices
DIR* devdir = opendir(kDevicePath);
if (devdir == 0) {
ALOGE("%s: cannot open %s! Exiting threadloop", __FUNCTION__, kDevicePath);
return;
}
struct dirent* de;
while ((de = readdir(devdir)) != 0) {
// Find external v4l devices that's existing before we start watching and add them
if (!strncmp(kPrefix, de->d_name, kPrefixLen)) {
// TODO: This might reject some valid devices. Ex: internal is 33 and a device named 3
// is added.
std::string deviceId(de->d_name + kPrefixLen);
if (mCfg.mInternalDevices.count(deviceId) == 0) {
ALOGV("Non-internal v4l device %s found", de->d_name);
char v4l2DevicePath[kMaxDevicePathLen];
snprintf(v4l2DevicePath, kMaxDevicePathLen, "%s%s", kDevicePath, de->d_name);
deviceAdded(v4l2DevicePath);
}
}
}
closedir(devdir);
}
Return<void> ExternalCameraProviderImpl_2_7::isSetTorchModeSupported(
ICameraProvider::isSetTorchModeSupported_cb _hidl_cb) {
// setTorchMode API is supported, though right now no external camera device
// has a flash unit.
_hidl_cb(Status::OK, true);
return Void();
}
Return<void> ExternalCameraProviderImpl_2_7::getCameraDeviceInterface_V1_x(
const hidl_string&, ICameraProvider::getCameraDeviceInterface_V1_x_cb _hidl_cb) {
// External Camera HAL does not support HAL1
_hidl_cb(Status::OPERATION_NOT_SUPPORTED, nullptr);
return Void();
}
Return<void> ExternalCameraProviderImpl_2_7::getCameraDeviceInterface_V3_x(
const hidl_string& cameraDeviceName,
ICameraProvider::getCameraDeviceInterface_V3_x_cb _hidl_cb) {
std::string cameraDevicePath, deviceVersion;
bool match = matchDeviceName(mCfg.cameraIdOffset, cameraDeviceName, &deviceVersion,
&cameraDevicePath);
if (!match) {
_hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
return Void();
}
if (mCameraStatusMap.count(cameraDeviceName) == 0 ||
mCameraStatusMap[cameraDeviceName] != CameraDeviceStatus::PRESENT) {
_hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
return Void();
}
sp<device::V3_4::implementation::ExternalCameraDevice> deviceImpl;
switch (mPreferredHal3MinorVersion) {
case 4: {
ALOGV("Constructing v3.4 external camera device");
deviceImpl =
new device::V3_4::implementation::ExternalCameraDevice(cameraDevicePath, mCfg);
break;
}
case 5: {
ALOGV("Constructing v3.5 external camera device");
deviceImpl =
new device::V3_5::implementation::ExternalCameraDevice(cameraDevicePath, mCfg);
break;
}
case 6: {
ALOGV("Constructing v3.6 external camera device");
deviceImpl =
new device::V3_6::implementation::ExternalCameraDevice(cameraDevicePath, mCfg);
break;
}
default:
ALOGE("%s: Unknown HAL minor version %d!", __FUNCTION__, mPreferredHal3MinorVersion);
_hidl_cb(Status::INTERNAL_ERROR, nullptr);
return Void();
}
if (deviceImpl == nullptr || deviceImpl->isInitFailed()) {
ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraDevicePath.c_str());
_hidl_cb(Status::INTERNAL_ERROR, nullptr);
return Void();
}
IF_ALOGV() {
deviceImpl->getInterface()->interfaceChain(
[](::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
ALOGV("Device interface chain:");
for (auto iface : interfaceChain) {
ALOGV(" %s", iface.c_str());
}
});
}
_hidl_cb(Status::OK, deviceImpl->getInterface());
return Void();
}
void ExternalCameraProviderImpl_2_7::addExternalCamera(const char* devName) {
ALOGV("ExtCam: adding %s to External Camera HAL!", devName);
Mutex::Autolock _l(mLock);
std::string deviceName;
std::string cameraId =
std::to_string(mCfg.cameraIdOffset + std::atoi(devName + kDevicePrefixLen));
if (mPreferredHal3MinorVersion == 6) {
deviceName = std::string("device@3.6/external/") + cameraId;
} else if (mPreferredHal3MinorVersion == 5) {
deviceName = std::string("device@3.5/external/") + cameraId;
} else {
deviceName = std::string("device@3.4/external/") + cameraId;
}
mCameraStatusMap[deviceName] = CameraDeviceStatus::PRESENT;
if (mCallbacks != nullptr) {
mCallbacks->cameraDeviceStatusChange(deviceName, CameraDeviceStatus::PRESENT);
}
}
void ExternalCameraProviderImpl_2_7::deviceAdded(const char* devName) {
{
base::unique_fd fd(::open(devName, O_RDWR));
if (fd.get() < 0) {
ALOGE("%s open v4l2 device %s failed:%s", __FUNCTION__, devName, strerror(errno));
return;
}
struct v4l2_capability capability;
int ret = ioctl(fd.get(), VIDIOC_QUERYCAP, &capability);
if (ret < 0) {
ALOGE("%s v4l2 QUERYCAP %s failed", __FUNCTION__, devName);
return;
}
if (!(capability.device_caps & V4L2_CAP_VIDEO_CAPTURE)) {
ALOGW("%s device %s does not support VIDEO_CAPTURE", __FUNCTION__, devName);
return;
}
}
// See if we can initialize ExternalCameraDevice correctly
sp<device::V3_4::implementation::ExternalCameraDevice> deviceImpl =
new device::V3_4::implementation::ExternalCameraDevice(devName, mCfg);
if (deviceImpl == nullptr || deviceImpl->isInitFailed()) {
ALOGW("%s: Attempt to init camera device %s failed!", __FUNCTION__, devName);
return;
}
deviceImpl.clear();
addExternalCamera(devName);
return;
}
void ExternalCameraProviderImpl_2_7::deviceRemoved(const char* devName) {
Mutex::Autolock _l(mLock);
std::string deviceName;
std::string cameraId =
std::to_string(mCfg.cameraIdOffset + std::atoi(devName + kDevicePrefixLen));
if (mPreferredHal3MinorVersion == 6) {
deviceName = std::string("device@3.6/external/") + cameraId;
} else if (mPreferredHal3MinorVersion == 5) {
deviceName = std::string("device@3.5/external/") + cameraId;
} else {
deviceName = std::string("device@3.4/external/") + cameraId;
}
if (mCameraStatusMap.erase(deviceName) != 0) {
if (mCallbacks != nullptr) {
mCallbacks->cameraDeviceStatusChange(deviceName, CameraDeviceStatus::NOT_PRESENT);
}
} else {
ALOGE("%s: cannot find camera device %s", __FUNCTION__, devName);
}
}
ExternalCameraProviderImpl_2_7::HotplugThread::HotplugThread(ExternalCameraProviderImpl_2_7* parent)
: Thread(/*canCallJava*/ false),
mParent(parent),
mInternalDevices(parent->mCfg.mInternalDevices) {}
ExternalCameraProviderImpl_2_7::HotplugThread::~HotplugThread() {}
bool ExternalCameraProviderImpl_2_7::HotplugThread::threadLoop() {
// Update existing cameras
mParent->updateAttachedCameras();
// Watch new video devices
mINotifyFD = inotify_init();
if (mINotifyFD < 0) {
ALOGE("%s: inotify init failed! Exiting threadloop", __FUNCTION__);
return true;
}
mWd = inotify_add_watch(mINotifyFD, kDevicePath, IN_CREATE | IN_DELETE);
if (mWd < 0) {
ALOGE("%s: inotify add watch failed! Exiting threadloop", __FUNCTION__);
return true;
}
bool done = false;
char eventBuf[512];
while (!done) {
int offset = 0;
int ret = read(mINotifyFD, eventBuf, sizeof(eventBuf));
if (ret >= (int)sizeof(struct inotify_event)) {
while (offset < ret) {
struct inotify_event* event = (struct inotify_event*)&eventBuf[offset];
if (event->wd == mWd) {
ALOGV("%s inotify_event %s", __FUNCTION__, event->name);
if (!strncmp(kPrefix, event->name, kPrefixLen)) {
std::string deviceId(event->name + kPrefixLen);
if (mInternalDevices.count(deviceId) == 0) {
char v4l2DevicePath[kMaxDevicePathLen];
snprintf(v4l2DevicePath, kMaxDevicePathLen, "%s%s", kDevicePath,
event->name);
if (event->mask & IN_CREATE) {
mParent->deviceAdded(v4l2DevicePath);
}
if (event->mask & IN_DELETE) {
mParent->deviceRemoved(v4l2DevicePath);
}
}
}
}
offset += sizeof(struct inotify_event) + event->len;
}
}
}
return true;
}
Return<void> ExternalCameraProviderImpl_2_7::notifyDeviceStateChange(
hidl_bitfield<DeviceState> /*newState*/) {
return Void();
}
Return<void> ExternalCameraProviderImpl_2_7::getConcurrentStreamingCameraIds(
ICameraProvider::getConcurrentStreamingCameraIds_cb _hidl_cb) {
hidl_vec<hidl_vec<hidl_string>> hidl_camera_id_combinations;
_hidl_cb(Status::OK, hidl_camera_id_combinations);
return Void();
}
Return<void> ExternalCameraProviderImpl_2_7::isConcurrentStreamCombinationSupported(
const hidl_vec<::android::hardware::camera::provider::V2_6::
CameraIdAndStreamCombination>& /* configs */,
ICameraProvider::isConcurrentStreamCombinationSupported_cb _hidl_cb) {
_hidl_cb(Status::OK, false);
return Void();
}
Return<void> ExternalCameraProviderImpl_2_7::isConcurrentStreamCombinationSupported_2_7(
const hidl_vec<CameraIdAndStreamCombination>& /* configs */,
ICameraProvider::isConcurrentStreamCombinationSupported_2_7_cb _hidl_cb) {
_hidl_cb(Status::OK, false);
return Void();
}
} // namespace implementation
} // namespace V2_7
} // namespace provider
} // namespace camera
} // namespace hardware
} // namespace android
|