Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2014. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | #pragma once |
| 8 | |
| 9 | #include "USBCamera.h" |
| 10 | #include "ErrorBase.h" |
| 11 | #include "nivision.h" |
| 12 | #include "NIIMAQdx.h" |
| 13 | |
| 14 | #include "HAL/cpp/priority_mutex.h" |
| 15 | #include <thread> |
| 16 | #include <memory> |
| 17 | #include <condition_variable> |
| 18 | #include <tuple> |
| 19 | #include <vector> |
| 20 | |
| 21 | class CameraServer : public ErrorBase { |
| 22 | private: |
| 23 | static constexpr uint16_t kPort = 1180; |
| 24 | static constexpr uint8_t kMagicNumber[] = {0x01, 0x00, 0x00, 0x00}; |
| 25 | static constexpr uint32_t kSize640x480 = 0; |
| 26 | static constexpr uint32_t kSize320x240 = 1; |
| 27 | static constexpr uint32_t kSize160x120 = 2; |
| 28 | static constexpr int32_t kHardwareCompression = -1; |
| 29 | static constexpr uint32_t kMaxImageSize = 200000; |
| 30 | |
| 31 | protected: |
| 32 | CameraServer(); |
| 33 | |
| 34 | std::shared_ptr<USBCamera> m_camera; |
| 35 | std::thread m_serverThread; |
| 36 | std::thread m_captureThread; |
| 37 | priority_recursive_mutex m_imageMutex; |
| 38 | std::condition_variable_any m_newImageVariable; |
| 39 | std::vector<uint8_t*> m_dataPool; |
| 40 | unsigned int m_quality; |
| 41 | bool m_autoCaptureStarted; |
| 42 | bool m_hwClient; |
| 43 | std::tuple<uint8_t*, unsigned int, unsigned int, bool> m_imageData; |
| 44 | |
| 45 | void Serve(); |
| 46 | void AutoCapture(); |
| 47 | void SetImageData(uint8_t* data, unsigned int size, unsigned int start = 0, |
| 48 | bool imaqData = false); |
| 49 | void FreeImageData( |
| 50 | std::tuple<uint8_t*, unsigned int, unsigned int, bool> imageData); |
| 51 | |
| 52 | struct Request { |
| 53 | uint32_t fps; |
| 54 | int32_t compression; |
| 55 | uint32_t size; |
| 56 | }; |
| 57 | |
| 58 | public: |
| 59 | static CameraServer* GetInstance(); |
| 60 | void SetImage(Image const* image); |
| 61 | |
| 62 | void StartAutomaticCapture( |
| 63 | char const* cameraName = USBCamera::kDefaultCameraName); |
| 64 | |
| 65 | /** |
| 66 | * Start automatically capturing images to send to the dashboard. |
| 67 | * |
| 68 | * You should call this method to just see a camera feed on the |
| 69 | * dashboard without doing any vision processing on the roboRIO. |
| 70 | * {@link #SetImage} should not be called after this is called. |
| 71 | * |
| 72 | * @param camera The camera interface (eg. USBCamera) |
| 73 | */ |
| 74 | void StartAutomaticCapture(std::shared_ptr<USBCamera> camera); |
| 75 | |
| 76 | bool IsAutoCaptureStarted(); |
| 77 | |
| 78 | void SetQuality(unsigned int quality); |
| 79 | unsigned int GetQuality(); |
| 80 | |
| 81 | void SetSize(unsigned int size); |
| 82 | }; |