blob: 5cff5b3877b467be01aa29e8c8e6620030ba90f6 [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
49using CaptureResultMetadataQueue = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
50
51class 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
78 status_t linkToDeath(const sp<DeathRecipient>& recipient, void* cookie,
79 uint32_t flags) override;
80 status_t unlinkToDeath(const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
81 wp<DeathRecipient>* outRecipient) override;
82
83 void setCaptureResultMetadataQueue(std::shared_ptr<CaptureResultMetadataQueue> metadataQueue) {
84 mCaptureResultMetadataQueue = std::move(metadataQueue);
85 }
86
87 private:
88 // Wrapper struct so that parameters to onResultReceived callback may be
89 // sent through an AMessage.
90 struct ResultWrapper : public RefBase {
91 CameraMetadataNative mResult;
92 CaptureResultExtras mResultExtras;
93 std::vector<PhysicalCaptureResultInfo> mPhysicalCaptureResultInfos;
94
95 ResultWrapper(CameraMetadataNative &result,
96 CaptureResultExtras resultExtras,
97 std::vector<PhysicalCaptureResultInfo> physicalCaptureResultInfos) :
98 // TODO: make this std::movable
99 mResult(result),
100 mResultExtras(std::move(resultExtras)),
101 mPhysicalCaptureResultInfos(std::move(physicalCaptureResultInfos)) { }
102 };
103
104 struct CallbackHandler : public AHandler {
105 public:
106 void onMessageReceived(const sp<AMessage> &msg) override;
107 CallbackHandler(AidlCameraDeviceCallbacks *converter, int vndkVersion) :
108 mConverter(converter), mVndkVersion(vndkVersion) { }
109 private:
110 void processResultMessage(sp<ResultWrapper> &resultWrapper);
111 wp<AidlCameraDeviceCallbacks> mConverter = nullptr;
112 int mVndkVersion = -1;
113 };
114
115 void convertResultMetadataToAidl(const camera_metadata * src, SCaptureMetadataInfo * dst);
116 enum {
117 kWhatResultReceived,
118 };
119
120 static const char *kResultKey;
121
122 private:
123 std::shared_ptr<SICameraDeviceCallback> mBase;
124 std::shared_ptr<CaptureResultMetadataQueue> mCaptureResultMetadataQueue = nullptr;
125 sp<CallbackHandler> mHandler = nullptr;
126 sp<ALooper> mCbLooper = nullptr;
127
128 // Pipes death subscription from current NDK interface to VNDK mBase.
129 // Should consume calls to linkToDeath and unlinkToDeath.
130 DeathPipe mDeathPipe;
131};
132
133} // namespace android::frameworks::cameraservice::device::implementation
134#endif // FRAMEWORKS_AV_SERVICES_CAMERA_LIBCAMERASERVICE_AIDL_AIDLCAMERADEVICECALLBACKS_H_