1 /*
2  * Copyright (C) 2018 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 ANDROID_FRAMEWORKS_AIDL_CAMERADEVICECALLBACKS_H
18 #define ANDROID_FRAMEWORKS_AIDL_CAMERADEVICECALLBACKS_H
19 
20 #include <mutex>
21 #include <thread>
22 
23 #include <android/frameworks/cameraservice/common/2.0/types.h>
24 #include <android/frameworks/cameraservice/service/2.0/types.h>
25 #include <android/frameworks/cameraservice/device/2.0/ICameraDeviceCallback.h>
26 #include <android/frameworks/cameraservice/device/2.0/types.h>
27 #include <android/hardware/camera2/BnCameraDeviceCallbacks.h>
28 #include <media/stagefright/foundation/ALooper.h>
29 #include <media/stagefright/foundation/AHandler.h>
30 #include <media/stagefright/foundation/AMessage.h>
31 #include <fmq/MessageQueue.h>
32 #include <hidl/MQDescriptor.h>
33 #include <hidl/Status.h>
34 #include <CameraService.h>
35 #include <hidl/CameraHybridInterface.h>
36 
37 namespace android {
38 namespace frameworks {
39 namespace cameraservice {
40 namespace device {
41 namespace V2_0 {
42 namespace implementation {
43 
44 using camerahybrid::H2BConverter;
45 using HCameraDeviceCallback = cameraservice::device::V2_0::ICameraDeviceCallback;
46 using HPhysicalCaptureResultInfo = cameraservice::device::V2_0::PhysicalCaptureResultInfo;
47 using android::frameworks::cameraservice::device::V2_0::FmqSizeOrMetadata;
48 
49 using hardware::camera2::BnCameraDeviceCallbacks;
50 using hardware::camera2::ICameraDeviceCallbacks;
51 using hardware::camera2::impl::CaptureResultExtras;
52 using hardware::camera2::impl::CameraMetadataNative;
53 using hardware::camera2::impl::PhysicalCaptureResultInfo;
54 using hardware::kSynchronizedReadWrite;
55 using hardware::MessageQueue;
56 using CaptureResultMetadataQueue = MessageQueue<uint8_t, kSynchronizedReadWrite>;
57 
58 struct H2BCameraDeviceCallbacks :
59     public H2BConverter<HCameraDeviceCallback, ICameraDeviceCallbacks, BnCameraDeviceCallbacks> {
60     H2BCameraDeviceCallbacks(const sp<HalInterface>& base);
61 
62     ~H2BCameraDeviceCallbacks();
63 
64     bool initializeLooper();
65 
66     virtual binder::Status onDeviceError(int32_t errorCode,
67                                          const CaptureResultExtras& resultExtras) override;
68 
69     virtual binder::Status onDeviceIdle() override;
70 
71     virtual binder::Status onCaptureStarted(const CaptureResultExtras& resultExtras,
72                                             int64_t timestamp) override;
73 
74     virtual binder::Status onResultReceived(
75         const CameraMetadataNative& result, const CaptureResultExtras& resultExtras,
76         const std::vector<PhysicalCaptureResultInfo>& physicalCaptureResultInfos) override;
77 
78     virtual binder::Status onPrepared(int32_t streamId) override;
79 
80     virtual binder::Status onRepeatingRequestError(int64_t lastFrameNumber,
81                                                    int32_t repeatingRequestId) override;
82 
83     virtual binder::Status onRequestQueueEmpty() override;
84 
setCaptureResultMetadataQueueH2BCameraDeviceCallbacks85     void setCaptureResultMetadataQueue(std::shared_ptr<CaptureResultMetadataQueue> metadataQueue) {
86         mCaptureResultMetadataQueue = 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;
ResultWrapperH2BCameraDeviceCallbacks::ResultWrapper96         ResultWrapper(CameraMetadataNative &result,
97                       const CaptureResultExtras resultExtras,
98                       const std::vector<PhysicalCaptureResultInfo> &physicalCaptureResultInfos) :
99       // TODO: make this std::movable
100       mResult(result), mResultExtras(resultExtras), mPhysicalCaptureResultInfos(physicalCaptureResultInfos) { }
101     };
102 
103     struct CallbackHandler : public AHandler {
104         public:
105             void onMessageReceived(const sp<AMessage> &msg) override;
CallbackHandlerH2BCameraDeviceCallbacks::CallbackHandler106             CallbackHandler(H2BCameraDeviceCallbacks *converter) : mConverter(converter) { }
107         private:
108             void processResultMessage(sp<ResultWrapper> &resultWrapper);
109             wp<H2BCameraDeviceCallbacks> mConverter = nullptr;
110             Mutex mMetadataQueueLock;
111     };
112 
113     void convertResultMetadataToHidl(const camera_metadata *rawMetadata,
114                                      FmqSizeOrMetadata *resultMetadata);
115     enum {
116         kWhatResultReceived,
117     };
118 
119     static const char *kResultKey;
120 
121     std::shared_ptr<CaptureResultMetadataQueue> mCaptureResultMetadataQueue = nullptr;
122     sp<CallbackHandler> mHandler = nullptr;
123     sp<ALooper> mCbLooper = nullptr;
124 };
125 
126 } // implementation
127 } // V2_0
128 } // device
129 } // cameraservice
130 } // frameworks
131 } // android
132 #endif // ANDROID_FRAMEWORKS_AIDL_CAMERADEVICECALLBACKS_H
133