2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#ifndef CSCORE_SOURCEIMPL_H_
|
|
|
|
|
#define CSCORE_SOURCEIMPL_H_
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-12-04 09:50:01 -08:00
|
|
|
#include <atomic>
|
2016-09-05 12:00:04 -07:00
|
|
|
#include <cstddef>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <string_view>
|
2016-09-05 12:00:04 -07:00
|
|
|
#include <vector>
|
|
|
|
|
|
2018-10-31 20:22:58 -07:00
|
|
|
#include <wpi/Logger.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/condition_variable.h>
|
|
|
|
|
#include <wpi/mutex.h>
|
2016-12-22 21:19:50 -08:00
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
#include "Frame.h"
|
2018-10-31 20:22:58 -07:00
|
|
|
#include "Handle.h"
|
2016-12-20 20:48:31 -08:00
|
|
|
#include "Image.h"
|
2018-07-27 22:12:30 -07:00
|
|
|
#include "PropertyContainer.h"
|
2017-08-25 17:48:06 -07:00
|
|
|
#include "cscore_cpp.h"
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2018-11-10 20:30:02 -08:00
|
|
|
namespace wpi {
|
|
|
|
|
class json;
|
|
|
|
|
} // namespace wpi
|
|
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
namespace cs {
|
|
|
|
|
|
2018-10-31 20:22:58 -07:00
|
|
|
class Notifier;
|
|
|
|
|
class Telemetry;
|
|
|
|
|
|
2018-07-27 22:12:30 -07:00
|
|
|
class SourceImpl : public PropertyContainer {
|
2016-09-05 12:00:04 -07:00
|
|
|
friend class Frame;
|
|
|
|
|
|
|
|
|
|
public:
|
2021-06-06 16:13:58 -07:00
|
|
|
SourceImpl(std::string_view name, wpi::Logger& logger, Notifier& notifier,
|
2018-10-31 20:22:58 -07:00
|
|
|
Telemetry& telemetry);
|
2020-12-28 00:10:13 -08:00
|
|
|
~SourceImpl() override;
|
2016-10-23 18:20:56 -07:00
|
|
|
SourceImpl(const SourceImpl& oth) = delete;
|
|
|
|
|
SourceImpl& operator=(const SourceImpl& oth) = delete;
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2018-11-06 19:42:39 -08:00
|
|
|
virtual void Start() = 0;
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view GetName() const { return m_name; }
|
2016-10-23 18:20:56 -07:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void SetDescription(std::string_view description);
|
|
|
|
|
std::string_view GetDescription(wpi::SmallVectorImpl<char>& buf) const;
|
2016-10-23 18:20:56 -07:00
|
|
|
|
2018-07-29 21:18:45 -07:00
|
|
|
void SetConnectionStrategy(CS_ConnectionStrategy strategy) {
|
|
|
|
|
m_strategy = static_cast<int>(strategy);
|
2022-12-25 07:36:00 -08:00
|
|
|
NumSinksChanged();
|
2018-07-29 21:18:45 -07:00
|
|
|
}
|
|
|
|
|
bool IsEnabled() const {
|
|
|
|
|
return m_strategy == CS_CONNECTION_KEEP_OPEN ||
|
|
|
|
|
(m_strategy == CS_CONNECTION_AUTO_MANAGE && m_numSinksEnabled > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// User-visible connection status
|
2016-12-04 09:50:01 -08:00
|
|
|
void SetConnected(bool connected);
|
|
|
|
|
bool IsConnected() const { return m_connected; }
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-09-08 23:52:23 -07:00
|
|
|
// Functions to keep track of the overall number of sinks connected to this
|
|
|
|
|
// source. Primarily used by sinks to determine if other sinks are using
|
|
|
|
|
// the same source.
|
|
|
|
|
int GetNumSinks() const { return m_numSinks; }
|
2016-10-13 00:16:24 -07:00
|
|
|
void AddSink() {
|
|
|
|
|
++m_numSinks;
|
|
|
|
|
NumSinksChanged();
|
|
|
|
|
}
|
|
|
|
|
void RemoveSink() {
|
|
|
|
|
--m_numSinks;
|
|
|
|
|
NumSinksChanged();
|
|
|
|
|
}
|
2016-09-08 23:52:23 -07:00
|
|
|
|
|
|
|
|
// Functions to keep track of the number of sinks connected to this source
|
|
|
|
|
// that are "enabled", in other words, listening for new images. Primarily
|
|
|
|
|
// used by sources to determine whether they should actually bother trying
|
|
|
|
|
// to get source frames.
|
2016-10-13 00:16:24 -07:00
|
|
|
int GetNumSinksEnabled() const { return m_numSinksEnabled; }
|
2016-09-08 23:52:23 -07:00
|
|
|
|
|
|
|
|
void EnableSink() {
|
|
|
|
|
++m_numSinksEnabled;
|
2016-10-13 00:16:24 -07:00
|
|
|
NumSinksEnabledChanged();
|
2016-09-08 23:52:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DisableSink() {
|
|
|
|
|
--m_numSinksEnabled;
|
2016-10-13 00:16:24 -07:00
|
|
|
NumSinksEnabledChanged();
|
2016-09-08 23:52:23 -07:00
|
|
|
}
|
|
|
|
|
|
2016-10-22 22:09:47 -07:00
|
|
|
// Gets the current frame time (without waiting for a new one).
|
|
|
|
|
uint64_t GetCurFrameTime();
|
|
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
// Gets the current frame (without waiting for a new one).
|
|
|
|
|
Frame GetCurFrame();
|
|
|
|
|
|
|
|
|
|
// Blocking function that waits for the next frame and returns it.
|
|
|
|
|
Frame GetNextFrame();
|
|
|
|
|
|
2017-02-17 01:17:12 -08:00
|
|
|
// Blocking function that waits for the next frame and returns it (with
|
|
|
|
|
// timeout in seconds). If timeout expires, returns empty frame.
|
|
|
|
|
Frame GetNextFrame(double timeout);
|
|
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
// Force a wakeup of all GetNextFrame() callers by sending an empty frame.
|
|
|
|
|
void Wakeup();
|
|
|
|
|
|
2017-01-01 14:24:13 -08:00
|
|
|
// Standard common camera properties
|
2018-07-27 23:15:35 -07:00
|
|
|
virtual void SetBrightness(int brightness, CS_Status* status);
|
|
|
|
|
virtual int GetBrightness(CS_Status* status) const;
|
|
|
|
|
virtual void SetWhiteBalanceAuto(CS_Status* status);
|
|
|
|
|
virtual void SetWhiteBalanceHoldCurrent(CS_Status* status);
|
|
|
|
|
virtual void SetWhiteBalanceManual(int value, CS_Status* status);
|
|
|
|
|
virtual void SetExposureAuto(CS_Status* status);
|
|
|
|
|
virtual void SetExposureHoldCurrent(CS_Status* status);
|
|
|
|
|
virtual void SetExposureManual(int value, CS_Status* status);
|
2017-01-01 14:24:13 -08:00
|
|
|
|
2016-09-29 00:04:16 -07:00
|
|
|
// Video mode functions
|
2016-10-23 18:20:56 -07:00
|
|
|
VideoMode GetVideoMode(CS_Status* status) const;
|
2016-09-29 00:04:16 -07:00
|
|
|
virtual bool SetVideoMode(const VideoMode& mode, CS_Status* status) = 0;
|
|
|
|
|
|
|
|
|
|
// These have default implementations but can be overridden for custom
|
|
|
|
|
// or optimized behavior.
|
|
|
|
|
virtual bool SetPixelFormat(VideoMode::PixelFormat pixelFormat,
|
|
|
|
|
CS_Status* status);
|
|
|
|
|
virtual bool SetResolution(int width, int height, CS_Status* status);
|
|
|
|
|
virtual bool SetFPS(int fps, CS_Status* status);
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SetConfigJson(std::string_view config, CS_Status* status);
|
2018-11-10 20:30:02 -08:00
|
|
|
virtual bool SetConfigJson(const wpi::json& config, CS_Status* status);
|
|
|
|
|
std::string GetConfigJson(CS_Status* status);
|
|
|
|
|
virtual wpi::json GetConfigJsonObject(CS_Status* status);
|
|
|
|
|
|
2016-10-23 18:20:56 -07:00
|
|
|
std::vector<VideoMode> EnumerateVideoModes(CS_Status* status) const;
|
2016-10-13 00:16:24 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
std::unique_ptr<Image> AllocImage(VideoMode::PixelFormat pixelFormat,
|
2017-08-25 17:48:06 -07:00
|
|
|
int width, int height, size_t size);
|
2016-11-12 01:58:59 -08:00
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
protected:
|
2018-07-27 22:12:30 -07:00
|
|
|
void NotifyPropertyCreated(int propIndex, PropertyImpl& prop) override;
|
|
|
|
|
void UpdatePropertyValue(int property, bool setString, int value,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view valueStr) override;
|
2018-07-27 22:12:30 -07:00
|
|
|
|
2016-11-10 00:00:20 -08:00
|
|
|
void PutFrame(VideoMode::PixelFormat pixelFormat, int width, int height,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view data, Frame::Time time);
|
2016-12-20 20:48:31 -08:00
|
|
|
void PutFrame(std::unique_ptr<Image> image, Frame::Time time);
|
2021-06-06 16:13:58 -07:00
|
|
|
void PutError(std::string_view msg, Frame::Time time);
|
2016-10-13 00:16:24 -07:00
|
|
|
|
|
|
|
|
// Notification functions for corresponding atomics
|
|
|
|
|
virtual void NumSinksChanged() = 0;
|
|
|
|
|
virtual void NumSinksEnabledChanged() = 0;
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-09-08 23:52:23 -07:00
|
|
|
std::atomic_int m_numSinks{0};
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-10-23 18:20:56 -07:00
|
|
|
protected:
|
2018-07-27 22:12:30 -07:00
|
|
|
// Cached video modes (protected with m_mutex)
|
2016-10-23 18:20:56 -07:00
|
|
|
mutable std::vector<VideoMode> m_videoModes;
|
|
|
|
|
// Current video mode
|
|
|
|
|
mutable VideoMode m_mode;
|
|
|
|
|
|
2018-10-31 20:22:58 -07:00
|
|
|
wpi::Logger& m_logger;
|
|
|
|
|
Notifier& m_notifier;
|
|
|
|
|
Telemetry& m_telemetry;
|
|
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
private:
|
2016-12-20 20:48:31 -08:00
|
|
|
void ReleaseImage(std::unique_ptr<Image> image);
|
|
|
|
|
std::unique_ptr<Frame::Impl> AllocFrameImpl();
|
|
|
|
|
void ReleaseFrameImpl(std::unique_ptr<Frame::Impl> data);
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
std::string m_name;
|
2016-10-23 18:20:56 -07:00
|
|
|
std::string m_description;
|
2016-09-08 23:52:23 -07:00
|
|
|
|
2018-07-29 21:18:45 -07:00
|
|
|
std::atomic_int m_strategy{CS_CONNECTION_AUTO_MANAGE};
|
|
|
|
|
std::atomic_int m_numSinksEnabled{0};
|
|
|
|
|
|
2017-11-13 09:51:26 -08:00
|
|
|
wpi::mutex m_frameMutex;
|
|
|
|
|
wpi::condition_variable m_frameCv;
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
bool m_destroyFrames{false};
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
// Pool of frames/images to reduce malloc traffic.
|
2017-11-13 09:51:26 -08:00
|
|
|
wpi::mutex m_poolMutex;
|
2016-12-20 20:48:31 -08:00
|
|
|
std::vector<std::unique_ptr<Frame::Impl>> m_framesAvail;
|
|
|
|
|
std::vector<std::unique_ptr<Image>> m_imagesAvail;
|
2016-12-04 09:50:01 -08:00
|
|
|
|
|
|
|
|
std::atomic_bool m_connected{false};
|
2017-08-14 22:27:28 -07:00
|
|
|
|
|
|
|
|
// Most recent frame (returned to callers of GetNextFrame)
|
|
|
|
|
// Access protected by m_frameMutex.
|
|
|
|
|
// MUST be located below m_poolMutex as the Frame destructor calls back
|
|
|
|
|
// into SourceImpl::ReleaseImage, which locks m_poolMutex.
|
|
|
|
|
Frame m_frame;
|
2016-09-05 12:00:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace cs
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#endif // CSCORE_SOURCEIMPL_H_
|