2016-09-05 12:00:04 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:16:20 -08:00
|
|
|
/* Copyright (c) 2016-2018 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-04-29 23:33:19 -07:00
|
|
|
#include <wpi/STLExtras.h>
|
|
|
|
|
#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-07-29 12:53:41 -07:00
|
|
|
SourceImpl::SourceImpl(const wpi::Twine& name) : 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)
|
|
|
|
|
Notifier::GetInstance().NotifySource(*this, CS_SOURCE_DISCONNECTED);
|
|
|
|
|
else if (!wasConnected && connected)
|
|
|
|
|
Notifier::GetInstance().NotifySource(*this, CS_SOURCE_CONNECTED);
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
Telemetry::GetInstance().RecordSourceFrames(*this, 1);
|
|
|
|
|
Telemetry::GetInstance().RecordSourceBytes(*this,
|
|
|
|
|
static_cast<int>(image->size()));
|
|
|
|
|
|
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) {
|
|
|
|
|
auto& notifier = Notifier::GetInstance();
|
2017-01-02 19:43:04 -08:00
|
|
|
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)
|
|
|
|
|
notifier.NotifySourceProperty(*this, CS_SOURCE_PROPERTY_CHOICES_UPDATED,
|
2017-01-02 19:43:04 -08:00
|
|
|
prop.name, propIndex, prop.propKind,
|
2018-07-29 12:53:41 -07:00
|
|
|
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
|
|
|
|
|
if (m_properties_cached)
|
|
|
|
|
Notifier::GetInstance().NotifySourceProperty(
|
2017-01-02 19:43:04 -08:00
|
|
|
*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
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
if (m_framesAvail.empty()) return wpi::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
|
|
|
}
|