1 /*
2  * Copyright (C) 2016 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_HARDWARE_AUTOMOTIVE_EVS_V1_1_VIDEOCAPTURE_H
17 #define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_VIDEOCAPTURE_H
18 
19 #include <atomic>
20 #include <functional>
21 #include <set>
22 #include <thread>
23 
24 #include <linux/videodev2.h>
25 
26 typedef v4l2_buffer imageBuffer;
27 
28 
29 class VideoCapture {
30 public:
31     bool open(const char* deviceName, const int32_t width = 0, const int32_t height = 0);
32     void close();
33 
34     bool startStream(std::function<void(VideoCapture*, imageBuffer*, void*)> callback = nullptr);
35     void stopStream();
36 
37     // Valid only after open()
getWidth()38     __u32   getWidth()          { return mWidth; };
getHeight()39     __u32   getHeight()         { return mHeight; };
getStride()40     __u32   getStride()         { return mStride; };
getV4LFormat()41     __u32   getV4LFormat()      { return mFormat; };
42 
43     // NULL until stream is started
getLatestData()44     void* getLatestData()       { return mPixelBuffer; };
45 
isFrameReady()46     bool isFrameReady()         { return mFrameReady; };
markFrameConsumed()47     void markFrameConsumed()    { returnFrame(); };
48 
isOpen()49     bool isOpen()               { return mDeviceFd >= 0; };
50 
51     int setParameter(struct v4l2_control& control);
52     int getParameter(struct v4l2_control& control);
53     std::set<uint32_t> enumerateCameraControls();
54 
55 private:
56     void collectFrames();
57     void markFrameReady();
58     bool returnFrame();
59 
60     int mDeviceFd = -1;
61 
62     v4l2_buffer mBufferInfo = {};
63     void* mPixelBuffer = nullptr;
64 
65     __u32   mFormat = 0;
66     __u32   mWidth  = 0;
67     __u32   mHeight = 0;
68     __u32   mStride = 0;
69 
70     std::function<void(VideoCapture*, imageBuffer*, void*)> mCallback;
71 
72     std::thread mCaptureThread;             // The thread we'll use to dispatch frames
73     std::atomic<int> mRunMode;              // Used to signal the frame loop (see RunModes below)
74     std::atomic<bool> mFrameReady;          // Set when a frame has been delivered
75 
76     // Careful changing these -- we're using bit-wise ops to manipulate these
77     enum RunModes {
78         STOPPED     = 0,
79         RUN         = 1,
80         STOPPING    = 2,
81     };
82 };
83 
84 #endif // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_VIDEOCAPTURE_
85