2016-09-05 12:00:04 -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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#ifndef CAMERASERVER_FRAME_H_
|
|
|
|
|
#define CAMERASERVER_FRAME_H_
|
|
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
|
|
#include "llvm/SmallVector.h"
|
|
|
|
|
|
|
|
|
|
#include "Image.h"
|
|
|
|
|
|
|
|
|
|
namespace cs {
|
|
|
|
|
|
|
|
|
|
class SourceImpl;
|
|
|
|
|
|
|
|
|
|
class Frame {
|
|
|
|
|
friend class SourceImpl;
|
|
|
|
|
|
|
|
|
|
struct Data {
|
|
|
|
|
std::atomic_int refcount{0};
|
|
|
|
|
std::chrono::system_clock::time_point timestamp;
|
2016-09-18 14:14:35 -07:00
|
|
|
Image image;
|
2016-09-05 12:00:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
2016-09-16 18:46:36 -07:00
|
|
|
Frame() noexcept : m_source{nullptr}, m_data{nullptr} {}
|
|
|
|
|
|
|
|
|
|
Frame(SourceImpl& source, Data* data) noexcept : m_source{&source},
|
|
|
|
|
m_data{data} {
|
2016-09-05 12:00:04 -07:00
|
|
|
if (m_data) ++(m_data->refcount);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-16 18:46:36 -07:00
|
|
|
Frame(const Frame& frame) noexcept : m_source{frame.m_source},
|
|
|
|
|
m_data{frame.m_data} {
|
2016-09-05 12:00:04 -07:00
|
|
|
if (m_data) ++(m_data->refcount);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-16 18:46:36 -07:00
|
|
|
Frame(Frame&& other) noexcept : Frame() { swap(*this, other); }
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
~Frame() { DecRef(); }
|
|
|
|
|
|
2016-09-16 18:46:36 -07:00
|
|
|
Frame& operator=(Frame other) noexcept {
|
|
|
|
|
swap(*this, other);
|
2016-09-05 12:00:04 -07:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
explicit operator bool() const { return m_data; }
|
|
|
|
|
|
2016-09-18 14:14:35 -07:00
|
|
|
std::size_t size() const {
|
|
|
|
|
if (!m_data) return 0;
|
|
|
|
|
return m_data->image.size();
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-18 14:14:35 -07:00
|
|
|
const char* data() const {
|
|
|
|
|
if (!m_data) return nullptr;
|
|
|
|
|
return m_data->image.data();
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::chrono::system_clock::time_point time() const {
|
|
|
|
|
if (!m_data) return std::chrono::system_clock::time_point{};
|
|
|
|
|
return m_data->timestamp;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-16 18:46:36 -07:00
|
|
|
friend void swap(Frame& first, Frame& second) noexcept {
|
|
|
|
|
using std::swap;
|
|
|
|
|
swap(first.m_source, second.m_source);
|
|
|
|
|
swap(first.m_data, second.m_data);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
private:
|
|
|
|
|
void DecRef() {
|
|
|
|
|
if (m_data && --(m_data->refcount) == 0) ReleaseFrame();
|
|
|
|
|
}
|
|
|
|
|
void ReleaseFrame();
|
|
|
|
|
|
|
|
|
|
SourceImpl* m_source;
|
|
|
|
|
Data* m_data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace cs
|
|
|
|
|
|
|
|
|
|
#endif // CAMERASERVER_FRAME_H_
|