1  /*
2   * Copyright (C) 2017 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 EVS_VTS_STREAMHANDLER_H
18  #define EVS_VTS_STREAMHANDLER_H
19  
20  #include <aidl/android/hardware/automotive/evs/BnEvsCameraStream.h>
21  #include <aidl/android/hardware/automotive/evs/BufferDesc.h>
22  #include <aidl/android/hardware/automotive/evs/DisplayState.h>
23  #include <aidl/android/hardware/automotive/evs/EvsEventDesc.h>
24  #include <aidl/android/hardware/automotive/evs/IEvsCamera.h>
25  #include <aidl/android/hardware/graphics/common/HardwareBuffer.h>
26  #include <ui/GraphicBuffer.h>
27  
28  #include <queue>
29  
30  /*
31   * StreamHandler:
32   * This class can be used to receive camera imagery from an IEvsCamera implementation.  It will
33   * hold onto the most recent image buffer, returning older ones.
34   * Note that the video frames are delivered on a background thread, while the control interface
35   * is actuated from the applications foreground thread.
36   */
37  class StreamHandler final : public aidl::android::hardware::automotive::evs::BnEvsCameraStream {
38  public:
~StreamHandler()39      virtual ~StreamHandler() { shutdown(); };
40  
41      StreamHandler(
42              const std::shared_ptr<aidl::android::hardware::automotive::evs::IEvsCamera>& cameraObj,
43              uint32_t numBuffers = 2, bool useOwnBuffers = false,
44              android_pixel_format_t format = HAL_PIXEL_FORMAT_RGBA_8888, int32_t width = 640,
45              int32_t height = 360);
46      void shutdown();
47  
48      bool startStream();
49      void asyncStopStream();
50      void blockingStopStream();
51  
52      bool isRunning();
53  
54      bool newFrameAvailable();
55      const aidl::android::hardware::automotive::evs::BufferDesc& getNewFrame();
56      void doneWithFrame(const aidl::android::hardware::automotive::evs::BufferDesc& buffer);
57  
58  private:
59      // Implementation for aidl::android::hardware::automotive::evs::IEvsCameraStream
60      ndk::ScopedAStatus deliverFrame(
61              const std::vector<aidl::android::hardware::automotive::evs::BufferDesc>& buffers)
62              override;
63      ndk::ScopedAStatus notify(
64              const aidl::android::hardware::automotive::evs::EvsEventDesc& event) override;
65  
66      // Values initialized as startup
67      std::shared_ptr<aidl::android::hardware::automotive::evs::IEvsCamera> mCamera;
68  
69      // Since we get frames delivered to us asnchronously via the IEvsCameraStream interface,
70      // we need to protect all member variables that may be modified while we're streaming
71      // (i.e.: those below)
72      std::mutex mLock;
73      std::condition_variable mSignal;
74  
75      bool mRunning = false;
76  
77      aidl::android::hardware::automotive::evs::BufferDesc mBuffers[2];
78      int mHeldBuffer = -1;   // Index of the one currently held by the client
79      int mReadyBuffer = -1;  // Index of the newest available buffer
80      std::vector<aidl::android::hardware::automotive::evs::BufferDesc> mOwnBuffers;
81      bool mUseOwnBuffers;
82  };
83  
84  #endif  // EVS_VTS_STREAMHANDLER_H
85