1 /*
2  * Copyright (C) 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_STREAMHANDLER_H
17 #define ANDROID_CARSERVICE_STREAMHANDLER_H
18 
19 #include "EvsServiceCallback.h"
20 
21 #include <android/hardware/automotive/evs/1.1/IEvsCamera.h>
22 #include <android/hardware/automotive/evs/1.1/IEvsCameraStream.h>
23 #include <android/hardware/automotive/evs/1.1/IEvsDisplay.h>
24 #include <ui/GraphicBuffer.h>
25 
26 #include <list>
27 
28 namespace android {
29 namespace automotive {
30 namespace evs {
31 
32 /*
33  * StreamHandler:
34  * This class can be used to receive camera imagery from an IEvsCamera implementation.  It will
35  * hold onto the most recent image buffer, returning older ones.
36  * Note that the video frames are delivered on a background thread, while the control interface
37  * is actuated from the applications foreground thread.
38  */
39 class StreamHandler : public android::hardware::automotive::evs::V1_1::IEvsCameraStream {
40 public:
41     StreamHandler(android::sp<android::hardware::automotive::evs::V1_1::IEvsCamera>& pCamera,
42                   EvsServiceCallback* callback, int maxNumFramesInFlight);
43     virtual ~StreamHandler();
44     void shutdown();
45     bool startStream();
46     bool asyncStopStream();
47     void blockingStopStream();
48     bool isRunning();
49     void doneWithFrame(const android::hardware::automotive::evs::V1_1::BufferDesc& buffer);
50 
51 private:
52     // Implementation for ::android::hardware::automotive::evs::V1_0::IEvsCameraStream
53     android::hardware::Return<void> deliverFrame(
54             const android::hardware::automotive::evs::V1_0::BufferDesc& buffer) override;
55 
56     // Implementation for ::android::hardware::automotive::evs::V1_1::IEvsCameraStream
57     android::hardware::Return<void> deliverFrame_1_1(
58             const android::hardware::hidl_vec<android::hardware::automotive::evs::V1_1::BufferDesc>&
59                     buffer) override;
60     android::hardware::Return<void> notify(
61             const android::hardware::automotive::evs::V1_1::EvsEventDesc& event) override;
62 
63     // Values initialized as startup
64     android::sp<android::hardware::automotive::evs::V1_1::IEvsCamera> mEvsCamera;
65 
66     // Since we get frames delivered to us asnchronously via the ICarCameraStream interface,
67     // we need to protect all member variables that may be modified while we're streaming
68     // (ie: those below)
69     std::mutex mLock;
70     std::condition_variable mCondition;
71     bool mRunning = false;
72 
73     // Callbacks to forward EVS events and frames
74     EvsServiceCallback* mCallback;
75 
76     std::list<android::hardware::automotive::evs::V1_1::BufferDesc> mReceivedBuffers
77             GUARDED_BY(mLock);
78     int mMaxNumFramesInFlight;
79 };
80 
81 }  // namespace evs
82 }  // namespace automotive
83 }  // namespace android
84 
85 #endif  // ANDROID_CARSERVICE_STREAMHANDLER_H
86