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