Finish most of USBCameraImpl.

The main thing not yet fully implemented is video mode setting.

Also fix a handful of bugs in HTTPSinkImpl.
This commit is contained in:
Peter Johnson
2016-10-13 00:16:24 -07:00
parent 7f88bd15d1
commit 417545d521
10 changed files with 1383 additions and 575 deletions

View File

@@ -7,18 +7,28 @@
#include "SourceImpl.h"
#include "llvm/STLExtras.h"
#include <algorithm>
#include <cstring>
#include "Log.h"
using namespace cs;
static constexpr std::size_t kMaxFramesAvail = 32;
SourceImpl::SourceImpl(llvm::StringRef name)
: m_name{name}, m_frame{*this, nullptr} {}
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.
EnableSink();
Wakeup();
// 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);
}
// Everything else can clean up itself.
}
@@ -60,33 +70,72 @@ bool SourceImpl::SetFPS(int fps, CS_Status* status) {
return SetVideoMode(mode, status);
}
void SourceImpl::StartFrame() {
std::lock_guard<std::mutex> lock{m_mutex};
if (m_frameData) return;
if (m_framesAvail.empty()) {
m_frameData = llvm::make_unique<Frame::Data>();
} else {
m_frameData = std::move(m_framesAvail.back());
m_framesAvail.pop_back();
m_frameData->refcount = 0;
}
}
//TODO: Image& SourceImpl::AddImage(std::size_t size) {}
void SourceImpl::FinishFrame() {
void SourceImpl::PutFrame(VideoMode::PixelFormat pixelFormat,
llvm::StringRef data, Frame::Time time) {
std::unique_ptr<Frame::Data> frameData;
{
std::lock_guard<std::mutex> lock{m_mutex};
std::lock_guard<std::mutex> lock2{m_frameMutex};
m_frame = Frame{*this, m_frameData.release()};
// find the smallest existing frame that is at least big enough.
int found = -1;
for (std::size_t i = 0; i < m_framesAvail.size(); ++i) {
// is it big enough?
if (m_framesAvail[i] && m_framesAvail[i]->capacity >= data.size()) {
// is it smaller than the last found?
if (found < 0 ||
m_framesAvail[i]->capacity < m_framesAvail[found]->capacity) {
// yes, update
found = i;
}
}
}
// if nothing found, allocate a new buffer
if (found < 0)
frameData.reset(new Frame::Data{data.size()});
else
frameData = std::move(m_framesAvail[found]);
}
// Initialize frame data
frameData->refcount = 0;
frameData->time = time;
frameData->size = data.size();
frameData->pixelFormat = pixelFormat;
// Copy in image data
DEBUG4("Copying data to " << ((void*)frameData->data) << " from "
<< ((void*)data.data()) << " (" << data.size()
<< " bytes)");
std::memcpy(frameData->data, data.data(), data.size());
// Update frame
{
std::lock_guard<std::mutex> lock{m_frameMutex};
m_frame = Frame{*this, std::move(frameData)};
}
// Signal listeners
m_frameCv.notify_all();
}
void SourceImpl::ReleaseFrame(Frame::Data* data) {
void SourceImpl::ReleaseFrame(std::unique_ptr<Frame::Data> data) {
std::lock_guard<std::mutex> lock{m_mutex};
// Return the image to the pool
m_imagesAvail.emplace_back(std::move(data->image));
// Return the frame to the pool
m_framesAvail.emplace_back(data);
if (m_destroyFrames) return;
// Return the frame to the pool. First try to find an empty slot, otherwise
// add it to the end.
auto it = std::find(m_framesAvail.begin(), m_framesAvail.end(), nullptr);
if (it != m_framesAvail.end())
(*it) = std::move(data);
else if (m_framesAvail.size() > kMaxFramesAvail) {
// Replace smallest buffer; don't need to check for null because the above
// find would have found it.
auto it2 = std::min_element(m_framesAvail.begin(), m_framesAvail.end(),
[](const std::unique_ptr<Frame::Data>& a,
const std::unique_ptr<Frame::Data>& b) {
return a->capacity < b->capacity;
});
if ((*it2)->capacity < data->capacity)
*it2 = std::move(data);
} else
m_framesAvail.emplace_back(std::move(data));
}