Refactor source IsConnected() and connected notification.

This commit is contained in:
Peter Johnson
2016-12-04 09:50:01 -08:00
parent 81fd0eefac
commit 7c2f994a66
6 changed files with 16 additions and 20 deletions

View File

@@ -37,8 +37,6 @@ void CvSourceImpl::Start() {
CreateProperty("jpeg_quality", CS_PROP_INTEGER, 0, 100, 1, 80, 80);
}
bool CvSourceImpl::IsConnected() const { return m_connected; }
bool CvSourceImpl::CacheProperties(CS_Status* status) const {
// Doesn't need to do anything.
m_properties_cached = true;
@@ -112,14 +110,6 @@ void CvSourceImpl::NotifyError(llvm::StringRef msg) {
PutError(msg, wpi::Now());
}
void CvSourceImpl::SetConnected(bool connected) {
bool was_connected = m_connected.exchange(connected);
if (was_connected && !connected)
Notifier::GetInstance().NotifySource(*this, CS_SOURCE_DISCONNECTED);
else if (!was_connected && connected)
Notifier::GetInstance().NotifySource(*this, CS_SOURCE_CONNECTED);
}
int CvSourceImpl::CreateProperty(llvm::StringRef name, CS_PropertyKind kind,
int minimum, int maximum, int step,
int defaultValue, int value) {

View File

@@ -23,8 +23,6 @@ class CvSourceImpl : public SourceImpl {
void Start();
bool IsConnected() const override;
// Property functions
void SetProperty(int property, int value, CS_Status* status) override;
void SetStringProperty(int property, llvm::StringRef value,
@@ -38,7 +36,6 @@ class CvSourceImpl : public SourceImpl {
// OpenCV-specific functions
void PutFrame(cv::Mat& image);
void NotifyError(llvm::StringRef msg);
void SetConnected(bool connected);
int CreateProperty(llvm::StringRef name, CS_PropertyKind kind, int minimum,
int maximum, int step, int defaultValue, int value);
int CreateProperty(llvm::StringRef name, CS_PropertyKind kind, int minimum,

View File

@@ -11,6 +11,7 @@
#include <cstring>
#include "Log.h"
#include "Notifier.h"
using namespace cs;
@@ -44,6 +45,14 @@ llvm::StringRef SourceImpl::GetDescription(
return llvm::StringRef{buf.data(), buf.size()};
}
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);
}
uint64_t SourceImpl::GetCurFrameTime() {
std::unique_lock<std::mutex> lock{m_frameMutex};
return m_frame.time();

View File

@@ -8,6 +8,7 @@
#ifndef CS_SOURCEIMPL_H_
#define CS_SOURCEIMPL_H_
#include <atomic>
#include <condition_variable>
#include <cstddef>
#include <memory>
@@ -37,7 +38,8 @@ class SourceImpl {
void SetDescription(llvm::StringRef description);
llvm::StringRef GetDescription(llvm::SmallVectorImpl<char>& buf) const;
virtual bool IsConnected() const = 0;
void SetConnected(bool connected);
bool IsConnected() const { return m_connected; }
// 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
@@ -210,6 +212,8 @@ class SourceImpl {
// Pool of frame data to reduce malloc traffic.
std::mutex m_poolMutex;
std::vector<std::unique_ptr<Frame::Data>> m_framesAvail;
std::atomic_bool m_connected{false};
};
} // namespace cs

View File

@@ -595,7 +595,7 @@ void UsbCameraImpl::DeviceDisconnect() {
close(fd);
// Notify
Notifier::GetInstance().NotifySource(*this, CS_SOURCE_DISCONNECTED);
SetConnected(false);
}
void UsbCameraImpl::DeviceConnect() {
@@ -691,7 +691,7 @@ void UsbCameraImpl::DeviceConnect() {
SetDescription(GetDescriptionImpl(m_path.c_str()));
// Notify
Notifier::GetInstance().NotifySource(*this, CS_SOURCE_CONNECTED);
SetConnected(true);
}
bool UsbCameraImpl::DeviceStreamOn() {
@@ -1297,8 +1297,6 @@ bool UsbCameraImpl::CacheProperties(CS_Status* status) const {
return true;
}
bool UsbCameraImpl::IsConnected() const { return m_fd >= 0; }
void UsbCameraImpl::SetProperty(int property, int value, CS_Status* status) {
auto msg = CreateMessage(Message::kCmdSetProperty);
msg->data[0] = property;

View File

@@ -33,8 +33,6 @@ class UsbCameraImpl : public SourceImpl {
void Start();
bool IsConnected() const override;
// Property functions
void SetProperty(int property, int value, CS_Status* status) override;
void SetStringProperty(int property, llvm::StringRef value,