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_CAMERASERVICE_V2_0_CAMERASERVICE_H
18 #define ANDROID_FRAMEWORKS_CAMERASERVICE_V2_0_CAMERASERVICE_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.1/ICameraService.h>
25 #include <android/frameworks/cameraservice/service/2.0/types.h>
26 #include <android/frameworks/cameraservice/device/2.0/types.h>
27 
28 #include <hidl/Status.h>
29 
30 #include <CameraService.h>
31 
32 namespace android {
33 namespace frameworks {
34 namespace cameraservice {
35 namespace service {
36 namespace V2_0 {
37 namespace implementation {
38 
39 using hardware::hidl_string;
40 using hardware::ICameraServiceListener;
41 using hardware::Return;
42 
43 using HCameraDeviceCallback = frameworks::cameraservice::device::V2_0::ICameraDeviceCallback;
44 using HCameraMetadata = frameworks::cameraservice::service::V2_0::CameraMetadata;
45 using HCameraService = frameworks::cameraservice::service::V2_1::ICameraService;
46 using HCameraServiceListener = frameworks::cameraservice::service::V2_0::ICameraServiceListener;
47 using HCameraServiceListener2_1 = frameworks::cameraservice::service::V2_1::ICameraServiceListener;
48 using HStatus = frameworks::cameraservice::common::V2_0::Status;
49 using HCameraStatusAndId = frameworks::cameraservice::service::V2_0::CameraStatusAndId;
50 
51 struct HidlCameraService final : public HCameraService {
52 
~HidlCameraServicefinal53     ~HidlCameraService() { };
54 
55     // Methods from ::android::frameworks::cameraservice::service::V2.0::ICameraService follow.
56 
57     Return<void> connectDevice(const sp<HCameraDeviceCallback>& callback,
58                                const hidl_string& cameraId, connectDevice_cb _hidl_cb) override;
59 
60     Return<void> addListener(const sp<HCameraServiceListener>& listener,
61                              addListener_cb _hidl_cb) override;
62 
63     Return<HStatus> removeListener(const sp<HCameraServiceListener>& listener) override;
64 
65     Return<void> getCameraCharacteristics(const hidl_string& cameraId,
66                                           getCameraCharacteristics_cb _hidl_cb) override;
67 
68     Return<void> getCameraVendorTagSections(getCameraVendorTagSections_cb _hidl_cb) override;
69 
70     Return<void> addListener_2_1(const sp<HCameraServiceListener2_1>& listener,
71                                  addListener_2_1_cb _hidl_cb) override;
72 
73     // This method should only be called by the cameraservers main thread to
74     // instantiate the hidl cameraserver.
75     static sp<HidlCameraService> getInstance(android::CameraService *cs);
76 
77 private:
HidlCameraServicefinal78     HidlCameraService(android::CameraService *cs) : mAidlICameraService(cs) { };
79 
80     sp<hardware::ICameraServiceListener> searchListenerCacheLocked(
81         sp<HCameraServiceListener> listener, /*removeIfFound*/ bool shouldRemove = false);
82 
83 
84     template<class T>
85     HStatus addListenerInternal(const sp<T>& listener,
86                                 std::vector<hardware::CameraStatus>* cameraStatusAndIds);
87 
88     void addToListenerCacheLocked(sp<HCameraServiceListener> hListener,
89                                   sp<hardware::ICameraServiceListener> csListener);
90 
91     android::CameraService *const mAidlICameraService = nullptr;
92 
93     Mutex mListenerListLock;
94 
95     using HIListeners =
96         std::pair<sp<HCameraServiceListener>, sp<ICameraServiceListener>>;
97     std::list<HIListeners> mListeners;
98 };
99 
100 }  // namespace implementation
101 }  // namespace V2_0
102 }  // namespace service
103 }  // namespace cameraservice
104 }  // namespace frameworks
105 }  // namespace android
106 
107 #endif  // ANDROID_FRAMEWORKS_CAMERASERVICE_V2_0_CAMERASERVICE_H
108