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