xref: /aosp_15_r20/frameworks/av/services/camera/libcameraservice/aidl/AidlCameraDeviceCallbacks.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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 CameraMetadataInfo = android::hardware::camera2::CameraMetadataInfo;
50 using CaptureResultMetadataQueue = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
51 
52 class AidlCameraDeviceCallbacks : public UBnCameraDeviceCallbacks {
53   public:
54     explicit AidlCameraDeviceCallbacks(const std::shared_ptr<SICameraDeviceCallback>& base);
55 
56     ~AidlCameraDeviceCallbacks() override;
57 
58     bool initializeLooper(int vndkVersion);
59 
60     binder::Status onDeviceError(int32_t errorCode,
61                                  const CaptureResultExtras& resultExtras) override;
62 
63     binder::Status onDeviceIdle() override;
64 
65     binder::Status onCaptureStarted(const CaptureResultExtras& resultExtras,
66                                     int64_t timestamp) override;
67 
68     binder::Status onResultReceived(
69             const CameraMetadataInfo &resultInfo,
70             const CaptureResultExtras& resultExtras,
71             const std::vector<PhysicalCaptureResultInfo>& physicalCaptureResultInfos) override;
72 
73     binder::Status onPrepared(int32_t streamId) override;
74 
75     binder::Status onRepeatingRequestError(int64_t lastFrameNumber,
76                                            int32_t repeatingRequestId) override;
77 
78     binder::Status onRequestQueueEmpty() override;
79 
80     binder::Status onClientSharedAccessPriorityChanged(bool primaryClient) override;
81 
82     status_t linkToDeath(const sp<DeathRecipient>& recipient, void* cookie,
83                          uint32_t flags) override;
84     status_t unlinkToDeath(const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
85                            wp<DeathRecipient>* outRecipient) override;
86 
setCaptureResultMetadataQueue(std::shared_ptr<CaptureResultMetadataQueue> metadataQueue)87     void setCaptureResultMetadataQueue(std::shared_ptr<CaptureResultMetadataQueue> metadataQueue) {
88         mCaptureResultMetadataQueue = std::move(metadataQueue);
89     }
90 
91  private:
92     // Wrapper struct so that parameters to onResultReceived callback may be
93     // sent through an AMessage.
94     struct ResultWrapper : public RefBase {
95         CameraMetadataNative mResult;
96         CaptureResultExtras mResultExtras;
97         std::vector<PhysicalCaptureResultInfo> mPhysicalCaptureResultInfos;
98 
ResultWrapperResultWrapper99         ResultWrapper(CameraMetadataNative &result,
100                       CaptureResultExtras  resultExtras,
101                       std::vector<PhysicalCaptureResultInfo> physicalCaptureResultInfos) :
102               // TODO: make this std::movable
103               mResult(result),
104               mResultExtras(std::move(resultExtras)),
105               mPhysicalCaptureResultInfos(std::move(physicalCaptureResultInfos)) { }
106     };
107 
108     struct CallbackHandler : public AHandler {
109         public:
110             void onMessageReceived(const sp<AMessage> &msg) override;
CallbackHandlerCallbackHandler111             CallbackHandler(AidlCameraDeviceCallbacks *converter, int vndkVersion) :
112                     mConverter(converter), mVndkVersion(vndkVersion) { }
113         private:
114             void processResultMessage(sp<ResultWrapper> &resultWrapper);
115             wp<AidlCameraDeviceCallbacks> mConverter = nullptr;
116             int mVndkVersion = -1;
117     };
118 
119     void convertResultMetadataToAidl(const camera_metadata * src, SCaptureMetadataInfo * dst);
120     enum {
121         kWhatResultReceived,
122     };
123 
124     static const char *kResultKey;
125 
126   private:
127     std::shared_ptr<SICameraDeviceCallback> mBase;
128     std::shared_ptr<CaptureResultMetadataQueue> mCaptureResultMetadataQueue = nullptr;
129     sp<CallbackHandler> mHandler = nullptr;
130     sp<ALooper> mCbLooper = nullptr;
131 
132     // Pipes death subscription from current NDK interface to VNDK mBase.
133     // Should consume calls to linkToDeath and unlinkToDeath.
134     DeathPipe mDeathPipe;
135 };
136 
137 } // namespace android::frameworks::cameraservice::device::implementation
138 #endif // FRAMEWORKS_AV_SERVICES_CAMERA_LIBCAMERASERVICE_AIDL_AIDLCAMERADEVICECALLBACKS_H_
139