2016-09-16 17:57:17 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
|
|
|
|
/* 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2016-11-05 22:11:55 -07:00
|
|
|
#ifndef CS_USBCAMERAIMPL_H_
|
|
|
|
|
#define CS_USBCAMERAIMPL_H_
|
2016-09-16 17:57:17 -07:00
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <thread>
|
2016-10-13 00:16:24 -07:00
|
|
|
#include <vector>
|
2016-09-16 17:57:17 -07:00
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
#include <linux/videodev2.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "llvm/raw_ostream.h"
|
|
|
|
|
#include "llvm/SmallVector.h"
|
2016-10-13 00:16:24 -07:00
|
|
|
#include "llvm/STLExtras.h"
|
2016-09-16 17:57:17 -07:00
|
|
|
#include "support/raw_istream.h"
|
|
|
|
|
|
|
|
|
|
#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:
|
2016-12-04 00:08:47 -08:00
|
|
|
UsbCameraImpl(llvm::StringRef name, llvm::StringRef path);
|
|
|
|
|
~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;
|
2016-09-19 23:50:47 -07:00
|
|
|
void SetStringProperty(int property, llvm::StringRef value,
|
|
|
|
|
CS_Status* status) override;
|
2016-09-16 17:57:17 -07:00
|
|
|
|
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,
|
|
|
|
|
kNumSinksChanged, // no response
|
|
|
|
|
kNumSinksEnabledChanged, // no response
|
|
|
|
|
// Responses
|
|
|
|
|
kOk,
|
|
|
|
|
kError
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-15 23:54:50 -08:00
|
|
|
Message(Kind kind_) : kind(kind_) {}
|
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-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(
|
2016-12-22 20:51:04 -08:00
|
|
|
llvm::StringRef name) const override;
|
|
|
|
|
|
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:
|
|
|
|
|
// Message pool access
|
2016-11-15 23:54:50 -08:00
|
|
|
std::unique_ptr<Message> CreateMessage(Message::Kind kind) const {
|
2016-10-13 00:16:24 -07:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2016-11-15 23:54:50 -08:00
|
|
|
if (m_messagePool.empty()) return llvm::make_unique<Message>(kind);
|
2016-10-13 00:16:24 -07:00
|
|
|
auto rv = std::move(m_messagePool.back());
|
|
|
|
|
m_messagePool.pop_back();
|
2016-11-15 23:54:50 -08:00
|
|
|
rv->kind = kind;
|
2016-10-13 00:16:24 -07:00
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
void DestroyMessage(std::unique_ptr<Message> message) const {
|
|
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
|
m_messagePool.emplace_back(std::move(message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send a message to the camera thread and wait for a response (generic)
|
|
|
|
|
std::unique_ptr<Message> SendAndWait(std::unique_ptr<Message> msg) const;
|
|
|
|
|
// Send a message to the camera thread with no response
|
|
|
|
|
void Send(std::unique_ptr<Message> msg) const;
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
CS_StatusValue DeviceCmdSetProperty(std::unique_lock<std::mutex>& lock,
|
|
|
|
|
int property, bool setString, int value,
|
|
|
|
|
llvm::StringRef valueStr);
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// 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;
|
2016-12-04 00:08:47 -08:00
|
|
|
std::array<UsbCameraBuffer, kNumBuffers> m_buffers;
|
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-10-13 00:16:24 -07:00
|
|
|
//
|
|
|
|
|
// Variables protected by m_mutex
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// Message pool and queues
|
|
|
|
|
mutable std::vector<std::unique_ptr<Message>> m_messagePool;
|
|
|
|
|
mutable std::vector<std::unique_ptr<Message>> m_commands;
|
|
|
|
|
mutable std::vector<std::unique_ptr<Message>> m_responses;
|
|
|
|
|
mutable std::condition_variable m_responseCv;
|
2016-09-16 17:57:17 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace cs
|
|
|
|
|
|
2016-11-05 22:11:55 -07:00
|
|
|
#endif // CS_USBCAMERAIMPL_H_
|