2016-09-05 12:00:04 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-05-17 17:35:09 -07:00
|
|
|
/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
|
2016-09-05 12:00:04 -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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "SourceImpl.h"
|
|
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
2018-11-10 20:30:02 -08:00
|
|
|
#include <wpi/json.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/timestamp.h>
|
2016-12-20 20:48:31 -08:00
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
#include "Log.h"
|
2016-12-04 09:50:01 -08:00
|
|
|
#include "Notifier.h"
|
2018-03-04 21:01:55 -08:00
|
|
|
#include "Telemetry.h"
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
using namespace cs;
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
static constexpr size_t kMaxImagesAvail = 32;
|
2016-10-13 00:16:24 -07:00
|
|
|
|
2018-10-31 20:22:58 -07:00
|
|
|
SourceImpl::SourceImpl(const wpi::Twine& name, wpi::Logger& logger,
|
|
|
|
|
Notifier& notifier, Telemetry& telemetry)
|
|
|
|
|
: m_logger(logger),
|
|
|
|
|
m_notifier(notifier),
|
|
|
|
|
m_telemetry(telemetry),
|
|
|
|
|
m_name{name.str()} {
|
2018-04-29 23:33:19 -07:00
|
|
|
m_frame = Frame{*this, wpi::StringRef{}, 0};
|
2016-12-21 08:51:04 -08:00
|
|
|
}
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
SourceImpl::~SourceImpl() {
|
|
|
|
|
// Wake up anyone who is waiting. This also clears the current frame,
|
|
|
|
|
// which is good because its destructor will call back into the class.
|
|
|
|
|
Wakeup();
|
2016-10-13 00:16:24 -07:00
|
|
|
// Set a flag so ReleaseFrame() doesn't re-add them to m_framesAvail.
|
|
|
|
|
// Put in a block so we destroy before the destructor ends.
|
|
|
|
|
{
|
|
|
|
|
m_destroyFrames = true;
|
|
|
|
|
auto frames = std::move(m_framesAvail);
|
|
|
|
|
}
|
2016-09-05 12:00:04 -07:00
|
|
|
// Everything else can clean up itself.
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-29 12:53:41 -07:00
|
|
|
void SourceImpl::SetDescription(const wpi::Twine& description) {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_mutex);
|
2018-07-29 12:53:41 -07:00
|
|
|
m_description = description.str();
|
2016-10-23 18:20:56 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef SourceImpl::GetDescription(
|
|
|
|
|
wpi::SmallVectorImpl<char>& buf) const {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_mutex);
|
2016-10-23 18:20:56 -07:00
|
|
|
buf.append(m_description.begin(), m_description.end());
|
2018-04-29 23:33:19 -07:00
|
|
|
return wpi::StringRef{buf.data(), buf.size()};
|
2016-10-23 18:20:56 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-04 09:50:01 -08:00
|
|
|
void SourceImpl::SetConnected(bool connected) {
|
|
|
|
|
bool wasConnected = m_connected.exchange(connected);
|
|
|
|
|
if (wasConnected && !connected)
|
2018-10-31 20:22:58 -07:00
|
|
|
m_notifier.NotifySource(*this, CS_SOURCE_DISCONNECTED);
|
2016-12-04 09:50:01 -08:00
|
|
|
else if (!wasConnected && connected)
|
2018-10-31 20:22:58 -07:00
|
|
|
m_notifier.NotifySource(*this, CS_SOURCE_CONNECTED);
|
2016-12-04 09:50:01 -08:00
|
|
|
}
|
|
|
|
|
|
2016-10-22 22:09:47 -07:00
|
|
|
uint64_t SourceImpl::GetCurFrameTime() {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::unique_lock<wpi::mutex> lock{m_frameMutex};
|
2016-12-20 20:48:31 -08:00
|
|
|
return m_frame.GetTime();
|
2016-10-22 22:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Frame SourceImpl::GetCurFrame() {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::unique_lock<wpi::mutex> lock{m_frameMutex};
|
2016-10-22 22:09:47 -07:00
|
|
|
return m_frame;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
Frame SourceImpl::GetNextFrame() {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::unique_lock<wpi::mutex> lock{m_frameMutex};
|
2016-12-20 20:48:31 -08:00
|
|
|
auto oldTime = m_frame.GetTime();
|
|
|
|
|
m_frameCv.wait(lock, [=] { return m_frame.GetTime() != oldTime; });
|
2016-09-05 12:00:04 -07:00
|
|
|
return m_frame;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 01:17:12 -08:00
|
|
|
Frame SourceImpl::GetNextFrame(double timeout) {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::unique_lock<wpi::mutex> lock{m_frameMutex};
|
2017-02-17 01:17:12 -08:00
|
|
|
auto oldTime = m_frame.GetTime();
|
|
|
|
|
if (!m_frameCv.wait_for(
|
|
|
|
|
lock, std::chrono::milliseconds(static_cast<int>(timeout * 1000)),
|
|
|
|
|
[=] { return m_frame.GetTime() != oldTime; })) {
|
|
|
|
|
m_frame = Frame{*this, "timed out getting frame", wpi::Now()};
|
|
|
|
|
}
|
|
|
|
|
return m_frame;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
void SourceImpl::Wakeup() {
|
|
|
|
|
{
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock{m_frameMutex};
|
2018-04-29 23:33:19 -07:00
|
|
|
m_frame = Frame{*this, wpi::StringRef{}, 0};
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
2016-09-08 23:52:23 -07:00
|
|
|
m_frameCv.notify_all();
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-27 23:15:35 -07:00
|
|
|
void SourceImpl::SetBrightness(int brightness, CS_Status* status) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SourceImpl::GetBrightness(CS_Status* status) const {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SourceImpl::SetWhiteBalanceAuto(CS_Status* status) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SourceImpl::SetWhiteBalanceHoldCurrent(CS_Status* status) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SourceImpl::SetWhiteBalanceManual(int value, CS_Status* status) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SourceImpl::SetExposureAuto(CS_Status* status) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SourceImpl::SetExposureHoldCurrent(CS_Status* status) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SourceImpl::SetExposureManual(int value, CS_Status* status) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-23 18:20:56 -07:00
|
|
|
VideoMode SourceImpl::GetVideoMode(CS_Status* status) const {
|
2017-08-25 17:48:06 -07:00
|
|
|
if (!m_properties_cached && !CacheProperties(status)) return VideoMode{};
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_mutex);
|
2016-10-23 18:20:56 -07:00
|
|
|
return m_mode;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-29 00:04:16 -07:00
|
|
|
bool SourceImpl::SetPixelFormat(VideoMode::PixelFormat pixelFormat,
|
|
|
|
|
CS_Status* status) {
|
|
|
|
|
auto mode = GetVideoMode(status);
|
|
|
|
|
if (!mode) return false;
|
|
|
|
|
mode.pixelFormat = pixelFormat;
|
|
|
|
|
return SetVideoMode(mode, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SourceImpl::SetResolution(int width, int height, CS_Status* status) {
|
|
|
|
|
auto mode = GetVideoMode(status);
|
|
|
|
|
if (!mode) return false;
|
|
|
|
|
mode.width = width;
|
|
|
|
|
mode.height = height;
|
|
|
|
|
return SetVideoMode(mode, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SourceImpl::SetFPS(int fps, CS_Status* status) {
|
|
|
|
|
auto mode = GetVideoMode(status);
|
|
|
|
|
if (!mode) return false;
|
|
|
|
|
mode.fps = fps;
|
|
|
|
|
return SetVideoMode(mode, status);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-10 20:30:02 -08:00
|
|
|
bool SourceImpl::SetConfigJson(wpi::StringRef config, CS_Status* status) {
|
|
|
|
|
wpi::json j;
|
|
|
|
|
try {
|
|
|
|
|
j = wpi::json::parse(config);
|
2018-11-12 19:39:49 -08:00
|
|
|
} catch (const wpi::json::parse_error& e) {
|
2018-11-10 20:30:02 -08:00
|
|
|
SWARNING("SetConfigJson: parse error at byte " << e.byte << ": "
|
|
|
|
|
<< e.what());
|
|
|
|
|
*status = CS_PROPERTY_WRITE_FAILED;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return SetConfigJson(j, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SourceImpl::SetConfigJson(const wpi::json& config, CS_Status* status) {
|
|
|
|
|
VideoMode mode;
|
|
|
|
|
|
|
|
|
|
// pixel format
|
|
|
|
|
if (config.count("pixel format") != 0) {
|
|
|
|
|
try {
|
|
|
|
|
auto str = config.at("pixel format").get<std::string>();
|
|
|
|
|
wpi::StringRef s(str);
|
|
|
|
|
if (s.equals_lower("mjpeg")) {
|
|
|
|
|
mode.pixelFormat = cs::VideoMode::kMJPEG;
|
|
|
|
|
} else if (s.equals_lower("yuyv")) {
|
|
|
|
|
mode.pixelFormat = cs::VideoMode::kYUYV;
|
|
|
|
|
} else if (s.equals_lower("rgb565")) {
|
|
|
|
|
mode.pixelFormat = cs::VideoMode::kRGB565;
|
|
|
|
|
} else if (s.equals_lower("bgr")) {
|
|
|
|
|
mode.pixelFormat = cs::VideoMode::kBGR;
|
|
|
|
|
} else if (s.equals_lower("gray")) {
|
|
|
|
|
mode.pixelFormat = cs::VideoMode::kGray;
|
|
|
|
|
} else {
|
|
|
|
|
SWARNING("SetConfigJson: could not understand pixel format value '"
|
|
|
|
|
<< str << '\'');
|
|
|
|
|
}
|
2018-11-12 19:39:49 -08:00
|
|
|
} catch (const wpi::json::exception& e) {
|
2018-11-10 20:30:02 -08:00
|
|
|
SWARNING("SetConfigJson: could not read pixel format: " << e.what());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// width
|
|
|
|
|
if (config.count("width") != 0) {
|
|
|
|
|
try {
|
|
|
|
|
mode.width = config.at("width").get<unsigned int>();
|
2018-11-12 19:39:49 -08:00
|
|
|
} catch (const wpi::json::exception& e) {
|
2018-11-10 20:30:02 -08:00
|
|
|
SWARNING("SetConfigJson: could not read width: " << e.what());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// height
|
|
|
|
|
if (config.count("height") != 0) {
|
|
|
|
|
try {
|
|
|
|
|
mode.height = config.at("height").get<unsigned int>();
|
2018-11-12 19:39:49 -08:00
|
|
|
} catch (const wpi::json::exception& e) {
|
2018-11-10 20:30:02 -08:00
|
|
|
SWARNING("SetConfigJson: could not read height: " << e.what());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fps
|
|
|
|
|
if (config.count("fps") != 0) {
|
|
|
|
|
try {
|
|
|
|
|
mode.fps = config.at("fps").get<unsigned int>();
|
2018-11-12 19:39:49 -08:00
|
|
|
} catch (const wpi::json::exception& e) {
|
2018-11-10 20:30:02 -08:00
|
|
|
SWARNING("SetConfigJson: could not read fps: " << e.what());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if all of video mode is set, use SetVideoMode, otherwise piecemeal it
|
|
|
|
|
if (mode.pixelFormat != VideoMode::kUnknown && mode.width != 0 &&
|
|
|
|
|
mode.height != 0 && mode.fps != 0) {
|
|
|
|
|
SINFO("SetConfigJson: setting video mode to pixelFormat "
|
|
|
|
|
<< mode.pixelFormat << ", width " << mode.width << ", height "
|
|
|
|
|
<< mode.height << ", fps " << mode.fps);
|
|
|
|
|
SetVideoMode(mode, status);
|
|
|
|
|
} else {
|
|
|
|
|
if (mode.pixelFormat != cs::VideoMode::kUnknown) {
|
|
|
|
|
SINFO("SetConfigJson: setting pixelFormat " << mode.pixelFormat);
|
|
|
|
|
SetPixelFormat(static_cast<cs::VideoMode::PixelFormat>(mode.pixelFormat),
|
|
|
|
|
status);
|
|
|
|
|
}
|
|
|
|
|
if (mode.width != 0 && mode.height != 0) {
|
|
|
|
|
SINFO("SetConfigJson: setting width " << mode.width << ", height "
|
|
|
|
|
<< mode.height);
|
|
|
|
|
SetResolution(mode.width, mode.height, status);
|
|
|
|
|
}
|
|
|
|
|
if (mode.fps != 0) {
|
|
|
|
|
SINFO("SetConfigJson: setting fps " << mode.fps);
|
|
|
|
|
SetFPS(mode.fps, status);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// brightness
|
|
|
|
|
if (config.count("brightness") != 0) {
|
|
|
|
|
try {
|
|
|
|
|
int val = config.at("brightness").get<int>();
|
|
|
|
|
SINFO("SetConfigJson: setting brightness to " << val);
|
|
|
|
|
SetBrightness(val, status);
|
2018-11-12 19:39:49 -08:00
|
|
|
} catch (const wpi::json::exception& e) {
|
2018-11-10 20:30:02 -08:00
|
|
|
SWARNING("SetConfigJson: could not read brightness: " << e.what());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// white balance
|
|
|
|
|
if (config.count("white balance") != 0) {
|
|
|
|
|
try {
|
|
|
|
|
auto& setting = config.at("white balance");
|
|
|
|
|
if (setting.is_string()) {
|
|
|
|
|
auto str = setting.get<std::string>();
|
|
|
|
|
wpi::StringRef s(str);
|
|
|
|
|
if (s.equals_lower("auto")) {
|
|
|
|
|
SINFO("SetConfigJson: setting white balance to auto");
|
|
|
|
|
SetWhiteBalanceAuto(status);
|
|
|
|
|
} else if (s.equals_lower("hold")) {
|
|
|
|
|
SINFO("SetConfigJson: setting white balance to hold current");
|
|
|
|
|
SetWhiteBalanceHoldCurrent(status);
|
|
|
|
|
} else {
|
|
|
|
|
SWARNING("SetConfigJson: could not understand white balance value '"
|
|
|
|
|
<< str << '\'');
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
int val = setting.get<int>();
|
|
|
|
|
SINFO("SetConfigJson: setting white balance to " << val);
|
|
|
|
|
SetWhiteBalanceManual(val, status);
|
|
|
|
|
}
|
2018-11-12 19:39:49 -08:00
|
|
|
} catch (const wpi::json::exception& e) {
|
2018-11-10 20:30:02 -08:00
|
|
|
SWARNING("SetConfigJson: could not read white balance: " << e.what());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// exposure
|
|
|
|
|
if (config.count("exposure") != 0) {
|
|
|
|
|
try {
|
|
|
|
|
auto& setting = config.at("exposure");
|
|
|
|
|
if (setting.is_string()) {
|
|
|
|
|
auto str = setting.get<std::string>();
|
|
|
|
|
wpi::StringRef s(str);
|
|
|
|
|
if (s.equals_lower("auto")) {
|
|
|
|
|
SINFO("SetConfigJson: setting exposure to auto");
|
|
|
|
|
SetExposureAuto(status);
|
|
|
|
|
} else if (s.equals_lower("hold")) {
|
|
|
|
|
SINFO("SetConfigJson: setting exposure to hold current");
|
|
|
|
|
SetExposureHoldCurrent(status);
|
|
|
|
|
} else {
|
|
|
|
|
SWARNING("SetConfigJson: could not understand exposure value '"
|
|
|
|
|
<< str << '\'');
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
int val = setting.get<int>();
|
|
|
|
|
SINFO("SetConfigJson: setting exposure to " << val);
|
|
|
|
|
SetExposureManual(val, status);
|
|
|
|
|
}
|
2018-11-12 19:39:49 -08:00
|
|
|
} catch (const wpi::json::exception& e) {
|
2018-11-10 20:30:02 -08:00
|
|
|
SWARNING("SetConfigJson: could not read exposure: " << e.what());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// properties
|
2019-01-11 20:33:05 -08:00
|
|
|
if (config.count("properties") != 0)
|
|
|
|
|
SetPropertiesJson(config.at("properties"), m_logger, GetName(), status);
|
2018-11-10 20:30:02 -08:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string SourceImpl::GetConfigJson(CS_Status* status) {
|
|
|
|
|
std::string rv;
|
|
|
|
|
wpi::raw_string_ostream os(rv);
|
|
|
|
|
GetConfigJsonObject(status).dump(os, 4);
|
|
|
|
|
os.flush();
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpi::json SourceImpl::GetConfigJsonObject(CS_Status* status) {
|
|
|
|
|
wpi::json j;
|
|
|
|
|
|
|
|
|
|
// pixel format
|
|
|
|
|
wpi::StringRef pixelFormat;
|
|
|
|
|
switch (m_mode.pixelFormat) {
|
|
|
|
|
case VideoMode::kMJPEG:
|
|
|
|
|
pixelFormat = "mjpeg";
|
|
|
|
|
break;
|
|
|
|
|
case VideoMode::kYUYV:
|
|
|
|
|
pixelFormat = "yuyv";
|
|
|
|
|
break;
|
|
|
|
|
case VideoMode::kRGB565:
|
|
|
|
|
pixelFormat = "rgb565";
|
|
|
|
|
break;
|
|
|
|
|
case VideoMode::kBGR:
|
|
|
|
|
pixelFormat = "bgr";
|
|
|
|
|
break;
|
|
|
|
|
case VideoMode::kGray:
|
|
|
|
|
pixelFormat = "gray";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!pixelFormat.empty()) j.emplace("pixel format", pixelFormat);
|
|
|
|
|
|
|
|
|
|
// width
|
|
|
|
|
if (m_mode.width != 0) j.emplace("width", m_mode.width);
|
|
|
|
|
|
|
|
|
|
// height
|
|
|
|
|
if (m_mode.height != 0) j.emplace("height", m_mode.height);
|
|
|
|
|
|
|
|
|
|
// fps
|
|
|
|
|
if (m_mode.fps != 0) j.emplace("fps", m_mode.fps);
|
|
|
|
|
|
|
|
|
|
// TODO: output brightness, white balance, and exposure?
|
|
|
|
|
|
|
|
|
|
// properties
|
2019-01-11 20:33:05 -08:00
|
|
|
wpi::json props = GetPropertiesJsonObject(status);
|
2018-11-10 20:30:02 -08:00
|
|
|
if (props.is_array()) j.emplace("properties", props);
|
|
|
|
|
|
|
|
|
|
return j;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-23 18:20:56 -07:00
|
|
|
std::vector<VideoMode> SourceImpl::EnumerateVideoModes(
|
|
|
|
|
CS_Status* status) const {
|
|
|
|
|
if (!m_properties_cached && !CacheProperties(status))
|
|
|
|
|
return std::vector<VideoMode>{};
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_mutex);
|
2016-10-23 18:20:56 -07:00
|
|
|
return m_videoModes;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
std::unique_ptr<Image> SourceImpl::AllocImage(
|
2017-08-25 17:48:06 -07:00
|
|
|
VideoMode::PixelFormat pixelFormat, int width, int height, size_t size) {
|
2016-12-20 20:48:31 -08:00
|
|
|
std::unique_ptr<Image> image;
|
2016-10-13 00:16:24 -07:00
|
|
|
{
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock{m_poolMutex};
|
2016-10-13 00:16:24 -07:00
|
|
|
// find the smallest existing frame that is at least big enough.
|
|
|
|
|
int found = -1;
|
2017-08-25 17:48:06 -07:00
|
|
|
for (size_t i = 0; i < m_imagesAvail.size(); ++i) {
|
2016-10-13 00:16:24 -07:00
|
|
|
// is it big enough?
|
2016-12-20 20:48:31 -08:00
|
|
|
if (m_imagesAvail[i] && m_imagesAvail[i]->capacity() >= size) {
|
2016-10-13 00:16:24 -07:00
|
|
|
// is it smaller than the last found?
|
|
|
|
|
if (found < 0 ||
|
2016-12-20 20:48:31 -08:00
|
|
|
m_imagesAvail[i]->capacity() < m_imagesAvail[found]->capacity()) {
|
2016-10-13 00:16:24 -07:00
|
|
|
// yes, update
|
|
|
|
|
found = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if nothing found, allocate a new buffer
|
|
|
|
|
if (found < 0)
|
2016-12-20 20:48:31 -08:00
|
|
|
image.reset(new Image{size});
|
2016-10-13 00:16:24 -07:00
|
|
|
else
|
2016-12-20 20:48:31 -08:00
|
|
|
image = std::move(m_imagesAvail[found]);
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
// Initialize image
|
|
|
|
|
image->SetSize(size);
|
|
|
|
|
image->pixelFormat = pixelFormat;
|
|
|
|
|
image->width = width;
|
|
|
|
|
image->height = height;
|
2016-10-13 00:16:24 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
return image;
|
2016-11-12 01:58:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SourceImpl::PutFrame(VideoMode::PixelFormat pixelFormat, int width,
|
2018-04-29 23:33:19 -07:00
|
|
|
int height, wpi::StringRef data, Frame::Time time) {
|
2016-12-20 20:48:31 -08:00
|
|
|
auto image = AllocImage(pixelFormat, width, height, data.size());
|
2016-11-12 01:58:59 -08:00
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
// Copy in image data
|
2017-08-25 17:48:06 -07:00
|
|
|
SDEBUG4("Copying data to "
|
|
|
|
|
<< reinterpret_cast<const void*>(image->data()) << " from "
|
|
|
|
|
<< reinterpret_cast<const void*>(data.data()) << " (" << data.size()
|
|
|
|
|
<< " bytes)");
|
2016-12-20 20:48:31 -08:00
|
|
|
std::memcpy(image->data(), data.data(), data.size());
|
|
|
|
|
|
|
|
|
|
PutFrame(std::move(image), time);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SourceImpl::PutFrame(std::unique_ptr<Image> image, Frame::Time time) {
|
2018-03-04 21:01:55 -08:00
|
|
|
// Update telemetry
|
2018-10-31 20:22:58 -07:00
|
|
|
m_telemetry.RecordSourceFrames(*this, 1);
|
|
|
|
|
m_telemetry.RecordSourceBytes(*this, static_cast<int>(image->size()));
|
2018-03-04 21:01:55 -08:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
// Update frame
|
|
|
|
|
{
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock{m_frameMutex};
|
2016-12-20 20:48:31 -08:00
|
|
|
m_frame = Frame{*this, std::move(image), time};
|
|
|
|
|
}
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
// Signal listeners
|
|
|
|
|
m_frameCv.notify_all();
|
2016-12-02 19:14:59 -08:00
|
|
|
}
|
|
|
|
|
|
2018-07-29 12:53:41 -07:00
|
|
|
void SourceImpl::PutError(const wpi::Twine& msg, Frame::Time time) {
|
2016-10-13 00:16:24 -07:00
|
|
|
// Update frame
|
2016-09-05 12:00:04 -07:00
|
|
|
{
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock{m_frameMutex};
|
2016-12-20 20:48:31 -08:00
|
|
|
m_frame = Frame{*this, msg, time};
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
2016-10-13 00:16:24 -07:00
|
|
|
|
|
|
|
|
// Signal listeners
|
2016-09-08 23:52:23 -07:00
|
|
|
m_frameCv.notify_all();
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-22 21:19:50 -08:00
|
|
|
void SourceImpl::NotifyPropertyCreated(int propIndex, PropertyImpl& prop) {
|
2018-10-31 20:22:58 -07:00
|
|
|
m_notifier.NotifySourceProperty(*this, CS_SOURCE_PROPERTY_CREATED, prop.name,
|
|
|
|
|
propIndex, prop.propKind, prop.value,
|
|
|
|
|
prop.valueStr);
|
2016-12-22 21:19:50 -08:00
|
|
|
// also notify choices updated event for enum types
|
|
|
|
|
if (prop.propKind == CS_PROP_ENUM)
|
2018-10-31 20:22:58 -07:00
|
|
|
m_notifier.NotifySourceProperty(*this, CS_SOURCE_PROPERTY_CHOICES_UPDATED,
|
|
|
|
|
prop.name, propIndex, prop.propKind,
|
|
|
|
|
prop.value, wpi::Twine{});
|
2016-12-22 21:19:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SourceImpl::UpdatePropertyValue(int property, bool setString, int value,
|
2018-07-29 12:53:41 -07:00
|
|
|
const wpi::Twine& valueStr) {
|
2016-12-22 21:19:50 -08:00
|
|
|
auto prop = GetProperty(property);
|
|
|
|
|
if (!prop) return;
|
|
|
|
|
|
|
|
|
|
if (setString)
|
|
|
|
|
prop->SetValue(valueStr);
|
|
|
|
|
else
|
|
|
|
|
prop->SetValue(value);
|
|
|
|
|
|
|
|
|
|
// Only notify updates after we've notified created
|
2018-10-31 20:22:58 -07:00
|
|
|
if (m_properties_cached) {
|
|
|
|
|
m_notifier.NotifySourceProperty(*this, CS_SOURCE_PROPERTY_VALUE_UPDATED,
|
|
|
|
|
prop->name, property, prop->propKind,
|
|
|
|
|
prop->value, prop->valueStr);
|
|
|
|
|
}
|
2016-12-22 21:19:50 -08:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
void SourceImpl::ReleaseImage(std::unique_ptr<Image> image) {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock{m_poolMutex};
|
2016-10-13 00:16:24 -07:00
|
|
|
if (m_destroyFrames) return;
|
|
|
|
|
// Return the frame to the pool. First try to find an empty slot, otherwise
|
|
|
|
|
// add it to the end.
|
2016-12-20 20:48:31 -08:00
|
|
|
auto it = std::find(m_imagesAvail.begin(), m_imagesAvail.end(), nullptr);
|
2017-08-25 17:48:06 -07:00
|
|
|
if (it != m_imagesAvail.end()) {
|
2016-12-20 20:48:31 -08:00
|
|
|
*it = std::move(image);
|
2017-08-25 17:48:06 -07:00
|
|
|
} else if (m_imagesAvail.size() > kMaxImagesAvail) {
|
2016-10-13 00:16:24 -07:00
|
|
|
// Replace smallest buffer; don't need to check for null because the above
|
|
|
|
|
// find would have found it.
|
2016-12-20 20:48:31 -08:00
|
|
|
auto it2 = std::min_element(
|
|
|
|
|
m_imagesAvail.begin(), m_imagesAvail.end(),
|
|
|
|
|
[](const std::unique_ptr<Image>& a, const std::unique_ptr<Image>& b) {
|
|
|
|
|
return a->capacity() < b->capacity();
|
|
|
|
|
});
|
|
|
|
|
if ((*it2)->capacity() < image->capacity()) *it2 = std::move(image);
|
2017-08-25 17:48:06 -07:00
|
|
|
} else {
|
2016-12-20 20:48:31 -08:00
|
|
|
m_imagesAvail.emplace_back(std::move(image));
|
2017-08-25 17:48:06 -07:00
|
|
|
}
|
2016-12-20 20:48:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<Frame::Impl> SourceImpl::AllocFrameImpl() {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock{m_poolMutex};
|
2016-12-20 20:48:31 -08:00
|
|
|
|
2019-07-07 15:44:43 -07:00
|
|
|
if (m_framesAvail.empty()) return std::make_unique<Frame::Impl>(*this);
|
2016-12-20 20:48:31 -08:00
|
|
|
|
|
|
|
|
auto impl = std::move(m_framesAvail.back());
|
|
|
|
|
m_framesAvail.pop_back();
|
|
|
|
|
return impl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SourceImpl::ReleaseFrameImpl(std::unique_ptr<Frame::Impl> impl) {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock{m_poolMutex};
|
2016-12-20 20:48:31 -08:00
|
|
|
if (m_destroyFrames) return;
|
|
|
|
|
m_framesAvail.push_back(std::move(impl));
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|