第13讲 cameraserver进程启动之PhysicalCameraDeviceStatusChange详解 - Android Camera Native Framework

https://deepinout.com/android-camera-native-framework/cameraserver-process-startup-physicalcameradevicestatuschange-details.html

在这里插入图片描述

initializeProviderInfoCommon

initializeProviderInfoCommon主要完成2件事情:

  1. 调用addDevice将CameraDevice保持在mDevices中
  2. 处理Cached Status回调
    前面的课程已经介绍了addDevice的逻辑,本讲介绍处理Cached Status回调。

什么时候会有Cache Status?
在CameraServer初始化Provider过程中,HAL通知发生了physicalCameraDeviceStatusChange 或 cameraDeviceStatusChange

在这里插入图片描述

2 Process Cache Status Callbacks

对于每个cachedstatus, 获取camera id,创建cached status, 通知camera service
在这里插入图片描述
/frameworks/av/services/camera/libcameraservice/common/CameraProviderManager.cpp

// Process cached status callbacks
std::unique_ptr> cachedStatus =
        std::make_unique>();
{
    std::lock_guard lock(mInitLock);
    // 1. 对于每个cachedstatus, 获取camera id,创建cached status, 
    for (auto& statusInfo : mCachedStatus) {
        std::string id, physicalId;
        status_t res = OK;
        if (statusInfo.isPhysicalCameraStatus) {
            res = physicalCameraDeviceStatusChangeLocked(&id, &physicalId,
                statusInfo.cameraId, statusInfo.physicalCameraId, statusInfo.status);
        } else {
            res = cameraDeviceStatusChangeLocked(&id, statusInfo.cameraId, statusInfo.status);
        }
        if (res == OK) {
            cachedStatus->emplace_back(statusInfo.isPhysicalCameraStatus,
                    id.c_str(), physicalId.c_str(), statusInfo.status);
        }
    }
    mCachedStatus.clear();

    mInitialized = true;
}


// 2. 回调
// The cached status change callbacks cannot be fired directly from this
// function, due to same-thread deadlock trying to acquire mInterfaceMutex
// twice.
if (listener != nullptr) {
    mInitialStatusCallbackFuture = std::async(std::launch::async,
            &CameraProviderManager::ProviderInfo::notifyInitialStatusChange, this,
            listener, std::move(cachedStatus));
}

3 physicalCameraDeviceStatusChangeLocked

在这里插入图片描述frameworks/av/services/camera/libcameraservice/common/CameraProviderManager.cpp

status_t CameraProviderManager::ProviderInfo::physicalCameraDeviceStatusChangeLocked(
            std::string* id, std::string* physicalId,
            const std::string& cameraDeviceName,
            const std::string& physicalCameraDeviceName,
            CameraDeviceStatus newStatus) {
    bool known = false;
    std::string cameraId;
    for (auto& deviceInfo : mDevices) {
        if (deviceInfo->mName == cameraDeviceName) {
            cameraId = deviceInfo->mId;
            if (!deviceInfo->mIsLogicalCamera) {
                ALOGE("%s: Invalid combination of camera id %s, physical id %s",
                        __FUNCTION__, cameraId.c_str(), physicalCameraDeviceName.c_str());
                return BAD_VALUE;
            }
            if (std::find(deviceInfo->mPhysicalIds.begin(), deviceInfo->mPhysicalIds.end(),
                    physicalCameraDeviceName) == deviceInfo->mPhysicalIds.end()) {
                ALOGE("%s: Invalid combination of camera id %s, physical id %s",
                        __FUNCTION__, cameraId.c_str(), physicalCameraDeviceName.c_str());
                return BAD_VALUE;
            }
            ALOGI("Camera device %s physical device %s status is now %s",
                    cameraDeviceName.c_str(), physicalCameraDeviceName.c_str(),
                    FrameworkDeviceStatusToString(newStatus));
            known = true;
            break;
        }
    }
    // Previously unseen device; status must not be NOT_PRESENT
    if (!known) {
        ALOGW("Camera provider %s says an unknown camera device %s-%s is not present. Curious.",
                mProviderName.c_str(), cameraDeviceName.c_str(),
                physicalCameraDeviceName.c_str());
        return BAD_VALUE;
    }

    *id = cameraId;
    *physicalId = physicalCameraDeviceName.c_str();
    return OK;
}

4 Physical onDeviceStatusChanged

关于SystemCameraKind的说明如下:
在这里插入图片描述

在这里插入图片描述frameworks/av/services/camera/libcameraservice/CameraService.cpp

void CameraService::onDeviceStatusChanged(const String8& id,
        const String8& physicalId,
        CameraDeviceStatus newHalStatus) {
    ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d",
            __FUNCTION__, id.string(), physicalId.string(), newHalStatus);

    StatusInternal newStatus = mapToInternal(newHalStatus);

    std::shared_ptr state = getCameraState(id);

    if (state == nullptr) {
        ALOGE("%s: Physical camera id %s status change on a non-present ID %s",
                __FUNCTION__, id.string(), physicalId.string());
        return;
    }

    StatusInternal logicalCameraStatus = state->getStatus();
    if (logicalCameraStatus != StatusInternal::PRESENT &&
            logicalCameraStatus != StatusInternal::NOT_AVAILABLE) {
        ALOGE("%s: Physical camera id %s status %d change for an invalid logical camera state %d",
                __FUNCTION__, physicalId.string(), newHalStatus, logicalCameraStatus);
        return;
    }

    bool updated = false;
    if (newStatus == StatusInternal::PRESENT) {
        updated = state->removeUnavailablePhysicalId(physicalId);
    } else {
        updated = state->addUnavailablePhysicalId(physicalId);
    }

    if (updated) {
        String8 idCombo = id + " : " + physicalId;
        if (newStatus == StatusInternal::PRESENT) {
            logDeviceAdded(idCombo,
                    String8::format("Device status changed to %d", newStatus));
        } else {
            logDeviceRemoved(idCombo,
                    String8::format("Device status changed to %d", newStatus));
        }
        // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275)
        SystemCameraKind deviceKind = SystemCameraKind::PUBLIC;
        if (getSystemCameraKind(id, &deviceKind) != OK) {
            ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.string());
            return;
        }
        String16 id16(id), physicalId16(physicalId);
        Mutex::Autolock lock(mStatusListenerLock);
        for (auto& listener : mListenerList) {
            if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(),
                    listener->getListenerPid(), listener->getListenerUid())) {
                ALOGV("Skipping discovery callback for system-only camera device %s",
                        id.c_str());
                continue;
            }
            listener->getListener()->onPhysicalCameraStatusChanged(mapToInterface(newStatus),
                    id16, physicalId16);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值