• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 #ifndef ANDROID_CARSERVICE_EVS_SERVICE_WRAPPER_H
17 #define ANDROID_CARSERVICE_EVS_SERVICE_WRAPPER_H
18 
19 #include "EvsCallbackThread.h"
20 #include "EvsServiceCallback.h"
21 #include "StreamHandler.h"
22 
23 #include <aidl/android/hardware/automotive/evs/BufferDesc.h>
24 #include <aidl/android/hardware/automotive/evs/EvsEventDesc.h>
25 #include <aidl/android/hardware/automotive/evs/IEvsCamera.h>
26 #include <aidl/android/hardware/automotive/evs/IEvsDisplay.h>
27 #include <aidl/android/hardware/automotive/evs/IEvsEnumerator.h>
28 
29 #include <mutex>
30 #include <set>
31 
32 namespace android::automotive::evs {
33 
34 /*
35  * This class wraps around HIDL transactions to the Extended View System service
36  * and the video stream managements.
37  */
38 class EvsServiceContext final : public EvsServiceCallback {
39 public:
40     EvsServiceContext(JavaVM* vm, jclass clazz);
41     virtual ~EvsServiceContext();
42 
43     /*
44      * Initializes the service context and connects to the native Extended View
45      * System service.
46      *
47      * @param env A pointer to the JNI environment
48      * @param env A reference to CarEvsService object
49      * @return false if it fails to connect to the native Extended View System
50      *         service or to register a death recipient.
51      *         true otherwise.
52      */
53     bool initialize(JNIEnv* env, jobject thiz) ACQUIRE(mLock);
54 
55     /*
56      * Requests to open a target camera device.
57      *
58      * @param id a string camera device identifier
59      * @return bool false if it has not connected to EVS service, fails to open
60      *              a camera device, or fails to initialize a stream handler;
61      *              true otherwise.
62      */
63     bool openCamera(const char* id) ACQUIRE(mLock);
64 
65     /*
66      * Requests to close an active camera device.
67      */
68     void closeCamera();
69 
70     /*
71      * Requests to start a video stream from a successfully opened camera device.
72      */
73     bool startVideoStream();
74 
75     /*
76      * Requests to stop an active video stream.
77      */
78     void stopVideoStream();
79 
80     /*
81      * Notifies that the client finishes with this buffer.
82      *
83      * @param frame a consumed frame buffer
84      */
85     void doneWithFrame(int bufferId);
86 
87     /*
88      * Tells whether or not we're connected to the Extended View System service
89      */
isAvailable()90     bool isAvailable() ACQUIRE(mLock) {
91         std::lock_guard<std::mutex> lock(mLock);
92         return mService != nullptr;
93     }
94 
95     /*
96      * Tells whether or not a target camera device is opened
97      */
isCameraOpened()98     bool isCameraOpened() ACQUIRE(mLock) {
99         std::lock_guard<std::mutex> lock(mLock);
100         return mCamera != nullptr;
101     }
102 
103     /*
104      * Implements EvsServiceCallback methods
105      */
106     void onNewEvent(const ::aidl::android::hardware::automotive::evs::EvsEventDesc&) override;
107     bool onNewFrame(const ::aidl::android::hardware::automotive::evs::BufferDesc&) override;
108 
109 private:
110     // Death recipient callback that is called when IEvsEnumerator service dies.
111     // The cookie is a pointer to a EvsServiceContext object.
112     static void onEvsServiceBinderDied(void* cookie);
113     void onEvsServiceDiedImpl();
114 
115     // Acquires the camera and the display exclusive ownerships.
116     void acquireCameraAndDisplayLocked() REQUIRES(mLock);
117 
118     // A mutex to protect shared resources
119     mutable std::mutex mLock;
120 
121     // Extended View System Enumerator service handle
122     std::shared_ptr<::aidl::android::hardware::automotive::evs::IEvsEnumerator> mService
123             GUARDED_BY(mLock);
124 
125     // A camera device opened for the rearview service
126     std::shared_ptr<::aidl::android::hardware::automotive::evs::IEvsCamera> mCamera
127             GUARDED_BY(mLock);
128 
129     // A handler of a video stream from the rearview camera device
130     std::shared_ptr<StreamHandler> mStreamHandler GUARDED_BY(mLock);
131 
132     // Extended View System display handle.  This would not be used but held by
133     // us to prevent other EVS clients from using EvsDisplay.
134     std::shared_ptr<::aidl::android::hardware::automotive::evs::IEvsDisplay> mDisplay;
135 
136     // A death recipient of Extended View System service
137     ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient GUARDED_BY(mLock);
138 
139     // Java VM
140     JavaVM* mVm;
141 
142     // Background thread to handle callbacks from the native Extended View
143     // System service
144     EvsCallbackThread mCallbackThread;
145 
146     // Reference to CarEvsService object
147     jobject mCarEvsServiceObj;
148 
149     // CarEvsService object's method to handle the accidental death of the
150     // native Extended View System service
151     jmethodID mDeathHandlerMethodId;
152 
153     // CarEvsService object's method to handle a new frame buffer
154     jmethodID mFrameHandlerMethodId;
155 
156     // CarEvsService object's method to handle a new stream event
157     jmethodID mEventHandlerMethodId;
158 
159     // Bookkeeps descriptors of received frame buffer IDs.
160     std::set<int> mBufferRecords GUARDED_BY(mLock);
161 
162     // A name of the camera device currently in use.
163     std::string mCameraIdInUse;
164 
165     // List of available camera devices
166     std::vector<::aidl::android::hardware::automotive::evs::CameraDesc> mCameraList;
167 
168     // Service name for EVS enumerator
169     static const char* kServiceName;
170 
171     // Maximum number of frames CarEvsService can hold.  This number has been
172     // chosen heuristically.
173     static constexpr int kMaxNumFramesInFlight = 6;
174 
175     // EVS service reserves a display ID 255 to allow the clients to open the main
176     // display exclusively.
177     static constexpr uint8_t kExclusiveMainDisplayId = 0xFF;
178 };
179 
180 }  // namespace android::automotive::evs
181 
182 #endif  // ANDROID_CARSERVICE_EVS_SERVICE_WRAPPER_H
183