huangcm
2025-02-26 a813214788f6e7b512df54f1c659cd0bdc9ac175
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
 * Copyright (C) 2011 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.
 */
 
/*
 * Contains implementation of a class EmulatedCameraFactory that manages cameras
 * available for emulation.
 */
 
//#define LOG_NDEBUG 0
#define LOG_TAG "EmulatedCamera_Factory"
 
#include "EmulatedCameraFactory.h"
#include "EmulatedCameraHotplugThread.h"
#include "EmulatedFakeCamera.h"
#include "EmulatedFakeCamera2.h"
#include "EmulatedFakeCamera3.h"
#include "EmulatedQemuCamera.h"
#include "EmulatedQemuCamera3.h"
 
#include <log/log.h>
#include <cutils/properties.h>
 
extern camera_module_t HAL_MODULE_INFO_SYM;
 
/*
 * A global instance of EmulatedCameraFactory is statically instantiated and
 * initialized when camera emulation HAL is loaded.
 */
android::EmulatedCameraFactory gEmulatedCameraFactory;
 
namespace android {
 
EmulatedCameraFactory::EmulatedCameraFactory() :
        mQemuClient(),
        mEmulatedCameras(nullptr),
        mEmulatedCameraNum(0),
        mFakeCameraNum(0),
        mConstructedOK(false),
        mCallbacks(nullptr) {
 
    /*
     * Figure out how many cameras need to be created, so we can allocate the
     * array of emulated cameras before populating it.
     */
    int emulatedCamerasSize = 0;
 
    // QEMU Cameras
    std::vector<QemuCameraInfo> qemuCameras;
    if (mQemuClient.connectClient(nullptr) == NO_ERROR) {
        findQemuCameras(&qemuCameras);
        emulatedCamerasSize += qemuCameras.size();
    }
 
    waitForQemuSfFakeCameraPropertyAvailable();
    // Fake Cameras
    if (isFakeCameraEmulationOn(/* backCamera */ true)) {
        mFakeCameraNum++;
    }
    if (isFakeCameraEmulationOn(/* backCamera */ false)) {
        mFakeCameraNum++;
    }
    emulatedCamerasSize += mFakeCameraNum;
 
    /*
     * We have the number of cameras we need to create, now allocate space for
     * them.
     */
    mEmulatedCameras = new EmulatedBaseCamera*[emulatedCamerasSize];
    if (mEmulatedCameras == nullptr) {
        ALOGE("%s: Unable to allocate emulated camera array for %d entries",
                __FUNCTION__, mEmulatedCameraNum);
        return;
    }
 
    createQemuCameras(qemuCameras);
 
    // Create fake cameras, if enabled.
    if (isFakeCameraEmulationOn(/* backCamera */ true)) {
        createFakeCamera(/* backCamera */ true);
    }
    if (isFakeCameraEmulationOn(/* backCamera */ false)) {
        createFakeCamera(/* backCamera */ false);
    }
 
    ALOGE("%d cameras are being emulated. %d of them are fake cameras.",
            mEmulatedCameraNum, mFakeCameraNum);
 
    // Create hotplug thread.
    {
        Vector<int> cameraIdVector;
        for (int i = 0; i < mEmulatedCameraNum; ++i) {
            cameraIdVector.push_back(i);
        }
        mHotplugThread = new EmulatedCameraHotplugThread(&cameraIdVector[0],
                                                         mEmulatedCameraNum);
        mHotplugThread->run("EmulatedCameraHotplugThread");
    }
 
    mConstructedOK = true;
}
 
EmulatedCameraFactory::~EmulatedCameraFactory() {
    if (mEmulatedCameras != nullptr) {
        for (int n = 0; n < mEmulatedCameraNum; n++) {
            if (mEmulatedCameras[n] != nullptr) {
                delete mEmulatedCameras[n];
            }
        }
        delete[] mEmulatedCameras;
    }
 
    if (mHotplugThread != nullptr) {
        mHotplugThread->requestExit();
        mHotplugThread->join();
    }
}
 
/******************************************************************************
 * Camera HAL API handlers.
 *
 * Each handler simply verifies existence of an appropriate EmulatedBaseCamera
 * instance, and dispatches the call to that instance.
 *
 *****************************************************************************/
 
int EmulatedCameraFactory::cameraDeviceOpen(int cameraId,
                                            hw_device_t **device) {
    ALOGV("%s: id = %d", __FUNCTION__, cameraId);
 
    *device = nullptr;
 
    if (!isConstructedOK()) {
        ALOGE("%s: EmulatedCameraFactory has failed to initialize",
                __FUNCTION__);
        return -EINVAL;
    }
 
    if (cameraId < 0 || cameraId >= getEmulatedCameraNum()) {
        ALOGE("%s: Camera id %d is out of bounds (%d)",
                __FUNCTION__, cameraId, getEmulatedCameraNum());
        return -ENODEV;
    }
 
    return mEmulatedCameras[cameraId]->connectCamera(device);
}
 
int EmulatedCameraFactory::getCameraInfo(int cameraId,
                                         struct camera_info *info) {
    ALOGV("%s: id = %d", __FUNCTION__, cameraId);
 
    if (!isConstructedOK()) {
        ALOGE("%s: EmulatedCameraFactory has failed to initialize",
                __FUNCTION__);
        return -EINVAL;
    }
 
    if (cameraId < 0 || cameraId >= getEmulatedCameraNum()) {
        ALOGE("%s: Camera id %d is out of bounds (%d)",
                __FUNCTION__, cameraId, getEmulatedCameraNum());
        return -ENODEV;
    }
 
    return mEmulatedCameras[cameraId]->getCameraInfo(info);
}
 
int EmulatedCameraFactory::setCallbacks(
        const camera_module_callbacks_t *callbacks) {
    ALOGV("%s: callbacks = %p", __FUNCTION__, callbacks);
 
    mCallbacks = callbacks;
 
    return OK;
}
 
void EmulatedCameraFactory::getVendorTagOps(vendor_tag_ops_t* ops) {
    ALOGV("%s: ops = %p", __FUNCTION__, ops);
    // No vendor tags defined for emulator yet, so not touching ops.
}
 
/****************************************************************************
 * Camera HAL API callbacks.
 ***************************************************************************/
 
int EmulatedCameraFactory::device_open(const hw_module_t *module, const char
        *name, hw_device_t **device) {
    /*
     * Simply verify the parameters, and dispatch the call inside the
     * EmulatedCameraFactory instance.
     */
 
    if (module != &HAL_MODULE_INFO_SYM.common) {
        ALOGE("%s: Invalid module %p expected %p",
                __FUNCTION__, module, &HAL_MODULE_INFO_SYM.common);
        return -EINVAL;
    }
    if (name == nullptr) {
        ALOGE("%s: NULL name is not expected here", __FUNCTION__);
        return -EINVAL;
    }
 
    return gEmulatedCameraFactory.cameraDeviceOpen(atoi(name), device);
}
 
int EmulatedCameraFactory::get_number_of_cameras() {
    return gEmulatedCameraFactory.getEmulatedCameraNum();
}
 
int EmulatedCameraFactory::get_camera_info(int camera_id,
                        struct camera_info *info) {
    return gEmulatedCameraFactory.getCameraInfo(camera_id, info);
}
 
int EmulatedCameraFactory::set_callbacks(
        const camera_module_callbacks_t *callbacks) {
    return gEmulatedCameraFactory.setCallbacks(callbacks);
}
 
void EmulatedCameraFactory::get_vendor_tag_ops(vendor_tag_ops_t *ops) {
    gEmulatedCameraFactory.getVendorTagOps(ops);
}
 
int EmulatedCameraFactory::open_legacy(const struct hw_module_t *module,
        const char *id, uint32_t halVersion, struct hw_device_t **device) {
    // Not supporting legacy open.
    return -ENOSYS;
}
 
/********************************************************************************
 * Internal API
 *******************************************************************************/
 
/*
 * Camera information tokens passed in response to the "list" factory query.
 */
 
// Device name token.
static const char *kListNameToken = "name=";
// Frame dimensions token.
static const char *kListDimsToken = "framedims=";
// Facing direction token.
static const char *kListDirToken = "dir=";
 
 
bool EmulatedCameraFactory::getTokenValue(const char *token,
        const std::string &s, char **value) {
    // Find the start of the token.
    size_t tokenStart = s.find(token);
    if (tokenStart == std::string::npos) {
        return false;
    }
 
    // Advance to the beginning of the token value.
    size_t valueStart = tokenStart + strlen(token);
 
    // Find the length of the token value.
    size_t valueLength = s.find(' ', valueStart) - valueStart;
 
    // Extract the value substring.
    std::string valueStr = s.substr(valueStart, valueLength);
 
    // Convert to char*.
    *value = new char[valueStr.length() + 1];
    if (*value == nullptr) {
        return false;
    }
    strcpy(*value, valueStr.c_str());
 
    ALOGV("%s: Parsed value is \"%s\"", __FUNCTION__, *value);
 
    return true;
}
 
void EmulatedCameraFactory::findQemuCameras(
        std::vector<QemuCameraInfo> *qemuCameras) {
    // Obtain camera list.
    char *cameraList = nullptr;
    status_t res = mQemuClient.listCameras(&cameraList);
 
    /*
     * Empty list, or list containing just an EOL means that there were no
     * connected cameras found.
     */
    if (res != NO_ERROR || cameraList == nullptr || *cameraList == '\0' ||
        *cameraList == '\n') {
        if (cameraList != nullptr) {
            free(cameraList);
        }
        return;
    }
 
    /*
     * Calculate number of connected cameras. Number of EOLs in the camera list
     * is the number of the connected cameras.
     */
 
    std::string cameraListStr(cameraList);
    free(cameraList);
 
    size_t lineBegin = 0;
    size_t lineEnd = cameraListStr.find('\n');
    while (lineEnd != std::string::npos) {
        std::string cameraStr = cameraListStr.substr(lineBegin, lineEnd - lineBegin);
        // Parse the 'name', 'framedims', and 'dir' tokens.
        char *name, *frameDims, *dir;
        if (getTokenValue(kListNameToken, cameraStr, &name) &&
                getTokenValue(kListDimsToken, cameraStr, &frameDims) &&
                getTokenValue(kListDirToken, cameraStr, &dir)) {
            // Push the camera info if it was all successfully parsed.
            qemuCameras->push_back(QemuCameraInfo{
                .name = name,
                .frameDims = frameDims,
                .dir = dir,
            });
        } else {
            ALOGW("%s: Bad camera information: %s", __FUNCTION__,
                    cameraStr.c_str());
        }
        // Skip over the newline for the beginning of the next line.
        lineBegin = lineEnd + 1;
        lineEnd = cameraListStr.find('\n', lineBegin);
    }
}
 
void EmulatedCameraFactory::createQemuCameras(
        const std::vector<QemuCameraInfo> &qemuCameras) {
    /*
     * Iterate the list, creating, and initializing emulated QEMU cameras for each
     * entry in the list.
     */
 
    /*
     * We use this index only for determining which direction the webcam should
     * face. Otherwise, mEmulatedCameraNum represents the camera ID and the
     * index into mEmulatedCameras.
     */
    int qemuIndex = 0;
    for (const auto &cameraInfo : qemuCameras) {
        /*
         * Here, we're assuming the first webcam is intended to be the back
         * camera and any other webcams are front cameras.
         */
        int halVersion = 0;
        if (qemuIndex == 0) {
            halVersion = getCameraHalVersion(/* backCamera */ true);
        } else {
            halVersion = getCameraHalVersion(/* backCamera */ false);
        }
 
        // Create and initialize QEMU camera.
        EmulatedBaseCamera *qemuCam = nullptr;
        status_t res;
        switch (halVersion) {
            case 1:
                EmulatedQemuCamera *qemuCamOne;
                qemuCamOne = new EmulatedQemuCamera(
                        mEmulatedCameraNum, &HAL_MODULE_INFO_SYM.common);
                if (qemuCamOne == nullptr) {
                    ALOGE("%s: Unable to instantiate EmulatedQemuCamera",
                            __FUNCTION__);
                } else {
                    /*
                     * We have to initialize in each switch case, because
                     * EmulatedBaseCamera::Initialize has a different method
                     * signature.
                     *
                     * TODO: Having an EmulatedBaseQemuCamera class
                     * could fix this issue.
                     */
                    res = qemuCamOne->Initialize(
                            cameraInfo.name,
                            cameraInfo.frameDims,
                            cameraInfo.dir);
                }
                qemuCam = qemuCamOne;
                break;
            case 2:
                ALOGE("%s: QEMU support for camera hal version %d is not "
                        "implemented", __FUNCTION__, halVersion);
                break;
            case 3:
                EmulatedQemuCamera3 *qemuCamThree;
                qemuCamThree = new EmulatedQemuCamera3(
                        mEmulatedCameraNum, &HAL_MODULE_INFO_SYM.common);
                if (qemuCamThree == nullptr) {
                    ALOGE("%s: Unable to instantiate EmulatedQemuCamera3",
                            __FUNCTION__);
                } else {
                    res = qemuCamThree->Initialize(
                            cameraInfo.name,
                            cameraInfo.frameDims,
                            cameraInfo.dir);
                }
                qemuCam = qemuCamThree;
                break;
            default:
                ALOGE("%s: Unknown camera hal version requested: %d",
                        __FUNCTION__, halVersion);
        }
 
        if (qemuCam == nullptr) {
            ALOGE("%s: Unable to instantiate EmulatedQemuCamera",
                    __FUNCTION__);
        } else {
            if (res == NO_ERROR) {
                mEmulatedCameras[mEmulatedCameraNum] = qemuCam;
                qemuIndex++;
                mEmulatedCameraNum++;
            } else {
                delete qemuCam;
            }
        }
    }
}
 
void EmulatedCameraFactory::createFakeCamera(bool backCamera) {
    int halVersion = getCameraHalVersion(backCamera);
 
    /*
     * Create and initialize the fake camera, using the index into
     * mEmulatedCameras as the camera ID.
     */
    switch (halVersion) {
        case 1:
            mEmulatedCameras[mEmulatedCameraNum] =
                    new EmulatedFakeCamera(mEmulatedCameraNum, backCamera,
                            &HAL_MODULE_INFO_SYM.common);
            break;
        case 2:
            mEmulatedCameras[mEmulatedCameraNum] =
                    new EmulatedFakeCamera2(mEmulatedCameraNum, backCamera,
                            &HAL_MODULE_INFO_SYM.common);
            break;
        case 3:
            {
                const char *key = "ro.kernel.qemu.camera.fake.rotating";
                char prop[PROPERTY_VALUE_MAX];
                if (property_get(key, prop, nullptr) > 0) {
                    mEmulatedCameras[mEmulatedCameraNum] =
                        new EmulatedFakeCamera(mEmulatedCameraNum, backCamera,
                                &HAL_MODULE_INFO_SYM.common);
                } else {
                    mEmulatedCameras[mEmulatedCameraNum] =
                        new EmulatedFakeCamera3(mEmulatedCameraNum, backCamera,
                                &HAL_MODULE_INFO_SYM.common);
                }
            }
            break;
        default:
            ALOGE("%s: Unknown %s camera hal version requested: %d",
                    __FUNCTION__, backCamera ? "back" : "front", halVersion);
    }
 
    if (mEmulatedCameras[mEmulatedCameraNum] == nullptr) {
        ALOGE("%s: Unable to instantiate fake camera class", __FUNCTION__);
    } else {
        ALOGV("%s: %s camera device version is %d", __FUNCTION__,
                backCamera ? "Back" : "Front", halVersion);
        status_t res = mEmulatedCameras[mEmulatedCameraNum]->Initialize();
        if (res == NO_ERROR) {
            // Camera creation and initialization was successful.
            mEmulatedCameraNum++;
        } else {
            ALOGE("%s: Unable to initialize %s camera %d: %s (%d)",
                    __FUNCTION__, backCamera ? "back" : "front",
                    mEmulatedCameraNum, strerror(-res), res);
            delete mEmulatedCameras[mEmulatedCameraNum];
        }
    }
}
 
void EmulatedCameraFactory::waitForQemuSfFakeCameraPropertyAvailable() {
    /*
     * Camera service may start running before qemu-props sets
     * qemu.sf.fake_camera to any of the follwing four values:
     * "none,front,back,both"; so we need to wait.
     *
     * android/camera/camera-service.c
     * bug: 30768229
     */
    int numAttempts = 100;
    char prop[PROPERTY_VALUE_MAX];
    bool timeout = true;
    for (int i = 0; i < numAttempts; ++i) {
        if (property_get("qemu.sf.fake_camera", prop, nullptr) != 0 ) {
            timeout = false;
            break;
        }
        usleep(5000);
    }
    if (timeout) {
        ALOGE("timeout (%dms) waiting for property qemu.sf.fake_camera to be set\n", 5 * numAttempts);
    }
}
 
bool EmulatedCameraFactory::isFakeCameraEmulationOn(bool backCamera) {
    /*
     * Defined by 'qemu.sf.fake_camera' boot property. If the property exists,
     * and if it's set to 'both', then fake cameras are used to emulate both
     * sides. If it's set to 'back' or 'front', then a fake camera is used only
     * to emulate the back or front camera, respectively.
     */
    char prop[PROPERTY_VALUE_MAX];
    if ((property_get("qemu.sf.fake_camera", prop, nullptr) > 0) &&
        (!strcmp(prop, "both") ||
         !strcmp(prop, backCamera ? "back" : "front"))) {
        return true;
    } else {
        return false;
    }
}
 
int EmulatedCameraFactory::getCameraHalVersion(bool backCamera) {
    /*
     * Defined by 'qemu.sf.front_camera_hal_version' and
     * 'qemu.sf.back_camera_hal_version' boot properties. If the property
     * doesn't exist, it is assumed we are working with HAL v1.
     */
    char prop[PROPERTY_VALUE_MAX];
    const char *propQuery = backCamera ?
            "qemu.sf.back_camera_hal" :
            "qemu.sf.front_camera_hal";
    if (property_get(propQuery, prop, nullptr) > 0) {
        char *propEnd = prop;
        int val = strtol(prop, &propEnd, 10);
        if (*propEnd == '\0') {
            return val;
        }
        // Badly formatted property. It should just be a number.
        ALOGE("qemu.sf.back_camera_hal is not a number: %s", prop);
    }
    return 3;
}
 
void EmulatedCameraFactory::onStatusChanged(int cameraId, int newStatus) {
 
    EmulatedBaseCamera *cam = mEmulatedCameras[cameraId];
    if (!cam) {
        ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
        return;
    }
 
    /*
     * (Order is important)
     * Send the callback first to framework, THEN close the camera.
     */
 
    if (newStatus == cam->getHotplugStatus()) {
        ALOGW("%s: Ignoring transition to the same status", __FUNCTION__);
        return;
    }
 
    const camera_module_callbacks_t* cb = mCallbacks;
    if (cb != nullptr && cb->camera_device_status_change != nullptr) {
        cb->camera_device_status_change(cb, cameraId, newStatus);
    }
 
    if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) {
        cam->unplugCamera();
    } else if (newStatus == CAMERA_DEVICE_STATUS_PRESENT) {
        cam->plugCamera();
    }
}
 
/********************************************************************************
 * Initializer for the static member structure.
 *******************************************************************************/
 
// Entry point for camera HAL API.
struct hw_module_methods_t EmulatedCameraFactory::mCameraModuleMethods = {
    .open = EmulatedCameraFactory::device_open
};
 
}; // end of namespace android