2016-11-28 00:22:15 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-25 17:48:06 -07:00
|
|
|
/* Copyright (c) 2016-2017 FIRST. All Rights Reserved. */
|
2016-11-28 00:22:15 -08: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_HTTPCAMERAIMPL_H_
|
|
|
|
|
#define CSCORE_HTTPCAMERAIMPL_H_
|
2016-11-28 00:22:15 -08:00
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <condition_variable>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <initializer_list>
|
2017-08-25 17:48:06 -07:00
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
2016-11-28 00:22:15 -08:00
|
|
|
#include <thread>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#include <llvm/SmallString.h>
|
|
|
|
|
#include <llvm/StringMap.h>
|
2017-08-25 18:10:47 -07:00
|
|
|
#include <support/HttpUtil.h>
|
2017-08-25 17:48:06 -07:00
|
|
|
#include <support/raw_istream.h>
|
2016-11-28 00:22:15 -08:00
|
|
|
|
|
|
|
|
#include "SourceImpl.h"
|
2017-08-25 17:48:06 -07:00
|
|
|
#include "cscore_cpp.h"
|
2016-11-28 00:22:15 -08:00
|
|
|
|
|
|
|
|
namespace cs {
|
|
|
|
|
|
|
|
|
|
class HttpCameraImpl : public SourceImpl {
|
|
|
|
|
public:
|
|
|
|
|
HttpCameraImpl(llvm::StringRef name, CS_HttpCameraKind kind);
|
|
|
|
|
~HttpCameraImpl() override;
|
|
|
|
|
|
|
|
|
|
void Start();
|
|
|
|
|
|
|
|
|
|
// Property functions
|
|
|
|
|
void SetProperty(int property, int value, CS_Status* status) override;
|
|
|
|
|
void SetStringProperty(int property, llvm::StringRef value,
|
|
|
|
|
CS_Status* status) override;
|
|
|
|
|
|
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-11-28 00:22:15 -08:00
|
|
|
bool SetVideoMode(const VideoMode& mode, CS_Status* status) override;
|
|
|
|
|
|
|
|
|
|
void NumSinksChanged() override;
|
|
|
|
|
void NumSinksEnabledChanged() override;
|
|
|
|
|
|
|
|
|
|
CS_HttpCameraKind GetKind() const;
|
|
|
|
|
bool SetUrls(llvm::ArrayRef<std::string> urls, CS_Status* status);
|
|
|
|
|
std::vector<std::string> GetUrls() const;
|
|
|
|
|
|
|
|
|
|
// Property data
|
|
|
|
|
class PropertyData : public PropertyImpl {
|
|
|
|
|
public:
|
|
|
|
|
PropertyData() = default;
|
2017-08-25 17:48:06 -07:00
|
|
|
explicit PropertyData(llvm::StringRef name_) : PropertyImpl{name_} {}
|
2016-11-28 00:22:15 -08:00
|
|
|
PropertyData(llvm::StringRef name_, llvm::StringRef httpParam_,
|
|
|
|
|
bool viaSettings_, CS_PropertyKind kind_, int minimum_,
|
|
|
|
|
int maximum_, int step_, int defaultValue_, int value_)
|
|
|
|
|
: PropertyImpl(name_, kind_, step_, defaultValue_, value_),
|
|
|
|
|
viaSettings(viaSettings_),
|
|
|
|
|
httpParam(httpParam_) {
|
|
|
|
|
hasMinimum = true;
|
|
|
|
|
minimum = minimum_;
|
|
|
|
|
hasMaximum = true;
|
|
|
|
|
maximum = maximum_;
|
|
|
|
|
}
|
|
|
|
|
~PropertyData() override = default;
|
|
|
|
|
|
|
|
|
|
bool viaSettings{false};
|
|
|
|
|
std::string httpParam;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
std::unique_ptr<PropertyImpl> CreateEmptyProperty(
|
|
|
|
|
llvm::StringRef name) const override;
|
|
|
|
|
|
|
|
|
|
bool CacheProperties(CS_Status* status) const override;
|
|
|
|
|
|
|
|
|
|
void CreateProperty(llvm::StringRef name, llvm::StringRef httpParam,
|
|
|
|
|
bool viaSettings, CS_PropertyKind kind, int minimum,
|
|
|
|
|
int maximum, int step, int defaultValue, int value) const;
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
void CreateEnumProperty(llvm::StringRef name, llvm::StringRef httpParam,
|
|
|
|
|
bool viaSettings, int defaultValue, int value,
|
|
|
|
|
std::initializer_list<T> choices) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// The camera streaming thread
|
|
|
|
|
void StreamThreadMain();
|
|
|
|
|
|
|
|
|
|
// Functions used by StreamThreadMain()
|
2017-08-25 18:10:47 -07:00
|
|
|
wpi::HttpConnection* DeviceStreamConnect(
|
|
|
|
|
llvm::SmallVectorImpl<char>& boundary);
|
2016-11-28 00:22:15 -08:00
|
|
|
void DeviceStream(wpi::raw_istream& is, llvm::StringRef boundary);
|
|
|
|
|
bool DeviceStreamFrame(wpi::raw_istream& is, std::string& imageBuf);
|
|
|
|
|
|
|
|
|
|
// The camera settings thread
|
|
|
|
|
void SettingsThreadMain();
|
2017-08-25 18:10:47 -07:00
|
|
|
void DeviceSendSettings(wpi::HttpRequest& req);
|
2016-11-28 00:22:15 -08:00
|
|
|
|
|
|
|
|
std::atomic_bool m_connected{false};
|
|
|
|
|
std::atomic_bool m_active{true}; // set to false to terminate thread
|
|
|
|
|
std::thread m_streamThread;
|
|
|
|
|
std::thread m_settingsThread;
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Variables protected by m_mutex
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// The camera connections
|
2017-08-25 18:10:47 -07:00
|
|
|
std::unique_ptr<wpi::HttpConnection> m_streamConn;
|
|
|
|
|
std::unique_ptr<wpi::HttpConnection> m_settingsConn;
|
2016-11-28 00:22:15 -08:00
|
|
|
|
|
|
|
|
CS_HttpCameraKind m_kind;
|
|
|
|
|
|
2017-08-25 18:10:47 -07:00
|
|
|
std::vector<wpi::HttpLocation> m_locations;
|
2017-08-25 17:48:06 -07:00
|
|
|
size_t m_nextLocation{0};
|
2016-11-28 00:22:15 -08:00
|
|
|
int m_prefLocation{-1}; // preferred location
|
|
|
|
|
|
|
|
|
|
std::condition_variable m_sinkEnabledCond;
|
|
|
|
|
|
|
|
|
|
llvm::StringMap<llvm::SmallString<16>> m_settings;
|
|
|
|
|
std::condition_variable m_settingsCond;
|
|
|
|
|
|
|
|
|
|
llvm::StringMap<llvm::SmallString<16>> m_streamSettings;
|
|
|
|
|
std::atomic_bool m_streamSettingsUpdated{false};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AxisCameraImpl : public HttpCameraImpl {
|
|
|
|
|
public:
|
2017-08-25 17:48:06 -07:00
|
|
|
explicit AxisCameraImpl(llvm::StringRef name)
|
|
|
|
|
: HttpCameraImpl{name, CS_HTTP_AXIS} {}
|
2016-11-28 00:22:15 -08:00
|
|
|
#if 0
|
|
|
|
|
void SetProperty(int property, int value, CS_Status* status) override;
|
|
|
|
|
void SetStringProperty(int property, llvm::StringRef value,
|
|
|
|
|
CS_Status* status) override;
|
|
|
|
|
#endif
|
|
|
|
|
protected:
|
|
|
|
|
bool CacheProperties(CS_Status* status) const override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace cs
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#endif // CSCORE_HTTPCAMERAIMPL_H_
|