2016-09-16 17:57:17 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:16:20 -08:00
|
|
|
/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
|
2016-09-16 17:57:17 -07:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#ifndef CSCORE_USBCAMERAIMPL_H_
|
|
|
|
|
#define CSCORE_USBCAMERAIMPL_H_
|
2016-09-16 17:57:17 -07:00
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
#include <linux/videodev2.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#include <atomic>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/STLExtras.h>
|
|
|
|
|
#include <wpi/SmallVector.h>
|
|
|
|
|
#include <wpi/condition_variable.h>
|
|
|
|
|
#include <wpi/mutex.h>
|
|
|
|
|
#include <wpi/raw_istream.h>
|
|
|
|
|
#include <wpi/raw_ostream.h>
|
2016-09-16 17:57:17 -07:00
|
|
|
|
|
|
|
|
#include "SourceImpl.h"
|
2016-12-04 00:08:47 -08:00
|
|
|
#include "UsbCameraBuffer.h"
|
2016-12-22 22:08:08 -08:00
|
|
|
#include "UsbCameraProperty.h"
|
2016-09-16 17:57:17 -07:00
|
|
|
|
|
|
|
|
namespace cs {
|
|
|
|
|
|
2016-12-04 00:08:47 -08:00
|
|
|
class UsbCameraImpl : public SourceImpl {
|
2016-09-16 17:57:17 -07:00
|
|
|
public:
|
2018-04-29 23:33:19 -07:00
|
|
|
UsbCameraImpl(wpi::StringRef name, wpi::StringRef path);
|
2016-12-04 00:08:47 -08:00
|
|
|
~UsbCameraImpl() override;
|
2016-09-16 17:57:17 -07:00
|
|
|
|
2016-11-18 09:20:52 -08:00
|
|
|
void Start();
|
|
|
|
|
|
2016-09-16 17:57:17 -07:00
|
|
|
// Property functions
|
2016-09-20 22:17:12 -07:00
|
|
|
void SetProperty(int property, int value, CS_Status* status) override;
|
2018-04-29 23:33:19 -07:00
|
|
|
void SetStringProperty(int property, wpi::StringRef value,
|
2016-09-19 23:50:47 -07:00
|
|
|
CS_Status* status) override;
|
2016-09-16 17:57:17 -07:00
|
|
|
|
2017-01-01 14:24:13 -08:00
|
|
|
// Standard common camera properties
|
|
|
|
|
void SetBrightness(int brightness, CS_Status* status) override;
|
|
|
|
|
int GetBrightness(CS_Status* status) const override;
|
|
|
|
|
void SetWhiteBalanceAuto(CS_Status* status) override;
|
|
|
|
|
void SetWhiteBalanceHoldCurrent(CS_Status* status) override;
|
|
|
|
|
void SetWhiteBalanceManual(int value, CS_Status* status) override;
|
|
|
|
|
void SetExposureAuto(CS_Status* status) override;
|
|
|
|
|
void SetExposureHoldCurrent(CS_Status* status) override;
|
|
|
|
|
void SetExposureManual(int value, CS_Status* status) override;
|
|
|
|
|
|
2016-09-29 00:04:16 -07:00
|
|
|
bool SetVideoMode(const VideoMode& mode, CS_Status* status) override;
|
|
|
|
|
bool SetPixelFormat(VideoMode::PixelFormat pixelFormat,
|
|
|
|
|
CS_Status* status) override;
|
|
|
|
|
bool SetResolution(int width, int height, CS_Status* status) override;
|
|
|
|
|
bool SetFPS(int fps, CS_Status* status) override;
|
|
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
void NumSinksChanged() override;
|
|
|
|
|
void NumSinksEnabledChanged() override;
|
2016-09-16 17:57:17 -07:00
|
|
|
|
2016-11-18 17:21:03 -08:00
|
|
|
std::string GetPath() { return m_path; }
|
|
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
// Messages passed to/from camera thread
|
|
|
|
|
struct Message {
|
2016-11-15 23:54:50 -08:00
|
|
|
enum Kind {
|
2016-10-13 00:16:24 -07:00
|
|
|
kNone = 0,
|
|
|
|
|
kCmdSetMode,
|
|
|
|
|
kCmdSetPixelFormat,
|
|
|
|
|
kCmdSetResolution,
|
|
|
|
|
kCmdSetFPS,
|
|
|
|
|
kCmdSetProperty,
|
|
|
|
|
kCmdSetPropertyStr,
|
2017-08-25 17:48:06 -07:00
|
|
|
kNumSinksChanged, // no response
|
2016-10-13 00:16:24 -07:00
|
|
|
kNumSinksEnabledChanged, // no response
|
|
|
|
|
// Responses
|
|
|
|
|
kOk,
|
|
|
|
|
kError
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
explicit Message(Kind kind_)
|
|
|
|
|
: kind(kind_), from(std::this_thread::get_id()) {}
|
2016-10-15 17:24:47 -07:00
|
|
|
|
2016-11-15 23:54:50 -08:00
|
|
|
Kind kind;
|
2016-10-13 00:16:24 -07:00
|
|
|
int data[4];
|
|
|
|
|
std::string dataStr;
|
2016-12-23 10:58:11 -08:00
|
|
|
std::thread::id from;
|
2016-10-13 00:16:24 -07:00
|
|
|
};
|
2016-09-16 17:57:17 -07:00
|
|
|
|
2016-10-23 18:20:56 -07:00
|
|
|
protected:
|
2016-12-22 21:19:50 -08:00
|
|
|
std::unique_ptr<PropertyImpl> CreateEmptyProperty(
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef name) const override;
|
2016-12-22 20:51:04 -08:00
|
|
|
|
2016-10-23 18:20:56 -07:00
|
|
|
// Cache properties. Immediately successful if properties are already cached.
|
|
|
|
|
// If they are not, tries to connect to the camera to do so; returns false and
|
|
|
|
|
// sets status to CS_SOURCE_IS_DISCONNECTED if that too fails.
|
|
|
|
|
bool CacheProperties(CS_Status* status) const override;
|
|
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
private:
|
|
|
|
|
// Send a message to the camera thread and wait for a response (generic)
|
2016-12-23 10:58:11 -08:00
|
|
|
CS_StatusValue SendAndWait(Message&& msg) const;
|
2016-10-13 00:16:24 -07:00
|
|
|
// Send a message to the camera thread with no response
|
2016-12-23 10:58:11 -08:00
|
|
|
void Send(Message&& msg) const;
|
2016-10-13 00:16:24 -07:00
|
|
|
|
|
|
|
|
// The camera processing thread
|
2016-09-16 17:57:17 -07:00
|
|
|
void CameraThreadMain();
|
|
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
// Functions used by CameraThreadMain()
|
|
|
|
|
void DeviceDisconnect();
|
|
|
|
|
void DeviceConnect();
|
|
|
|
|
bool DeviceStreamOn();
|
|
|
|
|
bool DeviceStreamOff();
|
|
|
|
|
void DeviceProcessCommands();
|
2016-10-15 17:24:47 -07:00
|
|
|
void DeviceSetMode();
|
2016-10-13 00:16:24 -07:00
|
|
|
void DeviceSetFPS();
|
|
|
|
|
void DeviceCacheMode();
|
2016-12-22 22:08:08 -08:00
|
|
|
void DeviceCacheProperty(std::unique_ptr<UsbCameraProperty> rawProp);
|
2016-10-13 00:16:24 -07:00
|
|
|
void DeviceCacheProperties();
|
|
|
|
|
void DeviceCacheVideoModes();
|
2016-12-22 20:51:04 -08:00
|
|
|
|
|
|
|
|
// Command helper functions
|
2017-11-13 09:51:26 -08:00
|
|
|
CS_StatusValue DeviceProcessCommand(std::unique_lock<wpi::mutex>& lock,
|
2016-12-23 10:58:11 -08:00
|
|
|
const Message& msg);
|
2017-11-13 09:51:26 -08:00
|
|
|
CS_StatusValue DeviceCmdSetMode(std::unique_lock<wpi::mutex>& lock,
|
2016-12-22 22:36:07 -08:00
|
|
|
const Message& msg);
|
2017-11-13 09:51:26 -08:00
|
|
|
CS_StatusValue DeviceCmdSetProperty(std::unique_lock<wpi::mutex>& lock,
|
2016-12-22 22:36:07 -08:00
|
|
|
const Message& msg);
|
2016-12-22 20:51:04 -08:00
|
|
|
|
|
|
|
|
// Property helper functions
|
2016-12-22 22:08:08 -08:00
|
|
|
int RawToPercentage(const UsbCameraProperty& rawProp, int rawValue);
|
|
|
|
|
int PercentageToRaw(const UsbCameraProperty& rawProp, int percentValue);
|
2016-10-13 00:16:24 -07:00
|
|
|
|
2016-12-22 23:44:40 -08:00
|
|
|
void SetQuirks();
|
|
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
//
|
|
|
|
|
// Variables only used within camera thread
|
|
|
|
|
//
|
|
|
|
|
bool m_streaming;
|
|
|
|
|
bool m_modeSetPixelFormat{false};
|
|
|
|
|
bool m_modeSetResolution{false};
|
|
|
|
|
bool m_modeSetFPS{false};
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
unsigned m_capabilities = 0;
|
|
|
|
|
#endif
|
|
|
|
|
// Number of buffers to ask OS for
|
|
|
|
|
static constexpr int kNumBuffers = 4;
|
2017-01-02 23:16:35 -08:00
|
|
|
#ifdef __linux__
|
2016-12-04 00:08:47 -08:00
|
|
|
std::array<UsbCameraBuffer, kNumBuffers> m_buffers;
|
2017-01-02 23:16:35 -08:00
|
|
|
#endif
|
2016-10-13 00:16:24 -07:00
|
|
|
|
|
|
|
|
//
|
2016-10-23 18:20:56 -07:00
|
|
|
// Path never changes, so not protected by mutex.
|
2016-10-13 00:16:24 -07:00
|
|
|
//
|
2016-09-16 17:57:17 -07:00
|
|
|
std::string m_path;
|
|
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
#ifdef __linux__
|
2016-09-16 17:57:17 -07:00
|
|
|
std::atomic_int m_fd;
|
2016-10-13 00:16:24 -07:00
|
|
|
std::atomic_int m_command_fd; // for command eventfd
|
|
|
|
|
#endif
|
2016-09-16 17:57:17 -07:00
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
std::atomic_bool m_active; // set to false to terminate thread
|
2016-09-16 17:57:17 -07:00
|
|
|
std::thread m_cameraThread;
|
|
|
|
|
|
2016-12-22 23:44:40 -08:00
|
|
|
// Quirks
|
2017-11-17 09:30:56 -08:00
|
|
|
bool m_lifecam_exposure{false}; // Microsoft LifeCam exposure
|
2016-12-22 23:44:40 -08:00
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
//
|
|
|
|
|
// Variables protected by m_mutex
|
|
|
|
|
//
|
|
|
|
|
|
2016-12-23 10:58:11 -08:00
|
|
|
// Message queues
|
|
|
|
|
mutable std::vector<Message> m_commands;
|
|
|
|
|
mutable std::vector<std::pair<std::thread::id, CS_StatusValue>> m_responses;
|
2017-11-13 09:51:26 -08:00
|
|
|
mutable wpi::condition_variable m_responseCv;
|
2016-09-16 17:57:17 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace cs
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#endif // CSCORE_USBCAMERAIMPL_H_
|