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() {
45         if (mFrames.empty()) {
46             // No frame is available
47             return nullptr;
48         }
49 
50         // Return a pointer to the buffer captured most recently
51         const int latestBufferId = *mFrames.end();
52         return mPixelBuffers[latestBufferId];
53     }
54 
isFrameReady()55     bool isFrameReady()             { return !mFrames.empty(); }
markFrameConsumed(int id)56     void markFrameConsumed(int id)  { returnFrame(id); }
57 
isOpen()58     bool isOpen()                   { return mDeviceFd >= 0; }
59 
60     int setParameter(struct v4l2_control& control);
61     int getParameter(struct v4l2_control& control);
62     std::set<uint32_t> enumerateCameraControls();
63 
64 private:
65     void collectFrames();
66     bool returnFrame(int id);
67 
68     int mDeviceFd = -1;
69 
70     int mNumBuffers = 0;
71     std::unique_ptr<v4l2_buffer[]> mBufferInfos = nullptr;
72     std::unique_ptr<void*[]>       mPixelBuffers = nullptr;
73 
74     __u32   mFormat = 0;
75     __u32   mWidth  = 0;
76     __u32   mHeight = 0;
77     __u32   mStride = 0;
78 
79     std::function<void(VideoCapture*, imageBuffer*, void*)> mCallback;
80 
81     std::thread mCaptureThread;             // The thread we'll use to dispatch frames
82     std::atomic<int> mRunMode;              // Used to signal the frame loop (see RunModes below)
83     std::set<int> mFrames;                  // Set of available frame buffers
84 
85     // Careful changing these -- we're using bit-wise ops to manipulate these
86     enum RunModes {
87         STOPPED     = 0,
88         RUN         = 1,
89         STOPPING    = 2,
90     };
91 };
92 
93 #endif // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_VIDEOCAPTURE_
94