Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #ifndef FRAMEWORKS_AV_SERVICES_CAMERA_LIBCAMERASERVICE_AIDL_AIDLCAMERADEVICECALLBACKS_H_ |
| 18 | #define FRAMEWORKS_AV_SERVICES_CAMERA_LIBCAMERASERVICE_AIDL_AIDLCAMERADEVICECALLBACKS_H_ |
| 19 | |
| 20 | #include <CameraService.h> |
| 21 | #include <aidl/DeathPipe.h> |
| 22 | #include <aidl/android/frameworks/cameraservice/device/BnCameraDeviceCallback.h> |
| 23 | #include <aidl/android/frameworks/cameraservice/device/CaptureMetadataInfo.h> |
| 24 | #include <aidl/android/frameworks/cameraservice/device/PhysicalCaptureResultInfo.h> |
| 25 | #include <android/hardware/camera2/BnCameraDeviceCallbacks.h> |
| 26 | #include <fmq/AidlMessageQueue.h> |
| 27 | #include <media/stagefright/foundation/AHandler.h> |
| 28 | #include <media/stagefright/foundation/ALooper.h> |
| 29 | #include <media/stagefright/foundation/AMessage.h> |
| 30 | #include <mutex> |
| 31 | #include <thread> |
| 32 | #include <utility> |
| 33 | |
| 34 | namespace android::frameworks::cameraservice::device::implementation { |
| 35 | |
| 36 | // VNDK classes |
| 37 | using SCaptureMetadataInfo = ::aidl::android::frameworks::cameraservice::device::CaptureMetadataInfo; |
| 38 | using SICameraDeviceCallback = |
| 39 | ::aidl::android::frameworks::cameraservice::device::ICameraDeviceCallback; |
| 40 | // NDK classes |
| 41 | using UBnCameraDeviceCallbacks = ::android::hardware::camera2::BnCameraDeviceCallbacks; |
| 42 | |
| 43 | using ::aidl::android::hardware::common::fmq::MQDescriptor; |
| 44 | using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite; |
| 45 | using ::android::AidlMessageQueue; |
| 46 | using ::android::frameworks::cameraservice::utils::DeathPipe; |
| 47 | using ::android::hardware::camera2::impl::CameraMetadataNative; |
| 48 | |
| 49 | using CaptureResultMetadataQueue = AidlMessageQueue<int8_t, SynchronizedReadWrite>; |
| 50 | |
| 51 | class AidlCameraDeviceCallbacks : public UBnCameraDeviceCallbacks { |
| 52 | public: |
| 53 | explicit AidlCameraDeviceCallbacks(const std::shared_ptr<SICameraDeviceCallback>& base); |
| 54 | |
| 55 | ~AidlCameraDeviceCallbacks() override; |
| 56 | |
| 57 | bool initializeLooper(int vndkVersion); |
| 58 | |
| 59 | binder::Status onDeviceError(int32_t errorCode, |
| 60 | const CaptureResultExtras& resultExtras) override; |
| 61 | |
| 62 | binder::Status onDeviceIdle() override; |
| 63 | |
| 64 | binder::Status onCaptureStarted(const CaptureResultExtras& resultExtras, |
| 65 | int64_t timestamp) override; |
| 66 | |
| 67 | binder::Status onResultReceived( |
| 68 | const CameraMetadataNative& result, const CaptureResultExtras& resultExtras, |
| 69 | const std::vector<PhysicalCaptureResultInfo>& physicalCaptureResultInfos) override; |
| 70 | |
| 71 | binder::Status onPrepared(int32_t streamId) override; |
| 72 | |
| 73 | binder::Status onRepeatingRequestError(int64_t lastFrameNumber, |
| 74 | int32_t repeatingRequestId) override; |
| 75 | |
| 76 | binder::Status onRequestQueueEmpty() override; |
| 77 | |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame] | 78 | binder::Status onClientSharedAccessPriorityChanged(bool primaryClient) override; |
| 79 | |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 80 | status_t linkToDeath(const sp<DeathRecipient>& recipient, void* cookie, |
| 81 | uint32_t flags) override; |
| 82 | status_t unlinkToDeath(const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags, |
| 83 | wp<DeathRecipient>* outRecipient) override; |
| 84 | |
| 85 | void setCaptureResultMetadataQueue(std::shared_ptr<CaptureResultMetadataQueue> metadataQueue) { |
| 86 | mCaptureResultMetadataQueue = std::move(metadataQueue); |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | // Wrapper struct so that parameters to onResultReceived callback may be |
| 91 | // sent through an AMessage. |
| 92 | struct ResultWrapper : public RefBase { |
| 93 | CameraMetadataNative mResult; |
| 94 | CaptureResultExtras mResultExtras; |
| 95 | std::vector<PhysicalCaptureResultInfo> mPhysicalCaptureResultInfos; |
| 96 | |
| 97 | ResultWrapper(CameraMetadataNative &result, |
| 98 | CaptureResultExtras resultExtras, |
| 99 | std::vector<PhysicalCaptureResultInfo> physicalCaptureResultInfos) : |
| 100 | // TODO: make this std::movable |
| 101 | mResult(result), |
| 102 | mResultExtras(std::move(resultExtras)), |
| 103 | mPhysicalCaptureResultInfos(std::move(physicalCaptureResultInfos)) { } |
| 104 | }; |
| 105 | |
| 106 | struct CallbackHandler : public AHandler { |
| 107 | public: |
| 108 | void onMessageReceived(const sp<AMessage> &msg) override; |
| 109 | CallbackHandler(AidlCameraDeviceCallbacks *converter, int vndkVersion) : |
| 110 | mConverter(converter), mVndkVersion(vndkVersion) { } |
| 111 | private: |
| 112 | void processResultMessage(sp<ResultWrapper> &resultWrapper); |
| 113 | wp<AidlCameraDeviceCallbacks> mConverter = nullptr; |
| 114 | int mVndkVersion = -1; |
| 115 | }; |
| 116 | |
| 117 | void convertResultMetadataToAidl(const camera_metadata * src, SCaptureMetadataInfo * dst); |
| 118 | enum { |
| 119 | kWhatResultReceived, |
| 120 | }; |
| 121 | |
| 122 | static const char *kResultKey; |
| 123 | |
| 124 | private: |
| 125 | std::shared_ptr<SICameraDeviceCallback> mBase; |
| 126 | std::shared_ptr<CaptureResultMetadataQueue> mCaptureResultMetadataQueue = nullptr; |
| 127 | sp<CallbackHandler> mHandler = nullptr; |
| 128 | sp<ALooper> mCbLooper = nullptr; |
| 129 | |
| 130 | // Pipes death subscription from current NDK interface to VNDK mBase. |
| 131 | // Should consume calls to linkToDeath and unlinkToDeath. |
| 132 | DeathPipe mDeathPipe; |
| 133 | }; |
| 134 | |
| 135 | } // namespace android::frameworks::cameraservice::device::implementation |
| 136 | #endif // FRAMEWORKS_AV_SERVICES_CAMERA_LIBCAMERASERVICE_AIDL_AIDLCAMERADEVICECALLBACKS_H_ |