blob: 6504cdc9456ef362bfb20b54632f71a06232363d [file] [log] [blame]
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -07001/*
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
34namespace android::frameworks::cameraservice::device::implementation {
35
36// VNDK classes
37using SCaptureMetadataInfo = ::aidl::android::frameworks::cameraservice::device::CaptureMetadataInfo;
38using SICameraDeviceCallback =
39 ::aidl::android::frameworks::cameraservice::device::ICameraDeviceCallback;
40// NDK classes
41using UBnCameraDeviceCallbacks = ::android::hardware::camera2::BnCameraDeviceCallbacks;
42
43using ::aidl::android::hardware::common::fmq::MQDescriptor;
44using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
45using ::android::AidlMessageQueue;
46using ::android::frameworks::cameraservice::utils::DeathPipe;
47using ::android::hardware::camera2::impl::CameraMetadataNative;
48
Jayant Chowdharydcae7962024-08-20 21:20:10 +000049using CameraMetadataInfo = android::hardware::camera2::CameraMetadataInfo;
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070050using CaptureResultMetadataQueue = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
51
52class 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(
Jayant Chowdharydcae7962024-08-20 21:20:10 +000069 const CameraMetadataInfo &resultInfo,
70 const CaptureResultExtras& resultExtras,
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070071 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
Jyoti Bhayana1f9600b2024-10-29 20:25:32 -070080 binder::Status onClientSharedAccessPriorityChanged(bool primaryClient) override;
81
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070082 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
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
99 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;
111 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_