[cscore] Add USB camera change event (#3123)

This commit is contained in:
Peter Johnson
2021-01-31 18:52:48 -08:00
committed by GitHub
parent 5337258888
commit ba6fe8ff2e
12 changed files with 202 additions and 4 deletions

View File

@@ -36,7 +36,10 @@ static void def_log_func(unsigned int level, const char* file,
wpi::errs() << oss.str();
}
Instance::Instance() : telemetry(notifier), networkListener(logger, notifier) {
Instance::Instance()
: telemetry(notifier),
networkListener(logger, notifier),
usbCameraListener(logger, notifier) {
SetDefaultLogger();
}
@@ -52,6 +55,7 @@ void Instance::Shutdown() {
m_sinks.FreeAll();
m_sources.FreeAll();
networkListener.Stop();
usbCameraListener.Stop();
telemetry.Stop();
notifier.Stop();
}

View File

@@ -18,6 +18,7 @@
#include "SourceImpl.h"
#include "Telemetry.h"
#include "UnlimitedHandleResource.h"
#include "UsbCameraListener.h"
namespace cs {
@@ -54,6 +55,7 @@ class Instance {
Notifier notifier;
Telemetry telemetry;
NetworkListener networkListener;
UsbCameraListener usbCameraListener;
private:
UnlimitedHandleResource<Handle, SourceData, Handle::kSource> m_sources;

View File

@@ -94,3 +94,7 @@ void Notifier::NotifyNetworkInterfacesChanged() {
void Notifier::NotifyTelemetryUpdated() {
Send(UINT_MAX, RawEvent::kTelemetryUpdated);
}
void Notifier::NotifyUsbCamerasChanged() {
Send(UINT_MAX, RawEvent::kUsbCamerasChanged);
}

View File

@@ -83,6 +83,7 @@ class Notifier : public wpi::CallbackManager<Notifier, impl::NotifierThread> {
const wpi::Twine& valueStr);
void NotifyNetworkInterfacesChanged();
void NotifyTelemetryUpdated();
void NotifyUsbCamerasChanged();
};
} // namespace cs

View File

@@ -0,0 +1,31 @@
// 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.
#ifndef CSCORE_USBCAMERALISTENER_H_
#define CSCORE_USBCAMERALISTENER_H_
#include <memory>
#include <wpi/Logger.h>
namespace cs {
class Notifier;
class UsbCameraListener {
public:
UsbCameraListener(wpi::Logger& logger, Notifier& notifier);
~UsbCameraListener();
void Start();
void Stop();
private:
class Impl;
std::unique_ptr<Impl> m_impl;
};
} // namespace cs
#endif // CSCORE_USBCAMERALISTENER_H_

View File

@@ -718,6 +718,13 @@ static void StartBackground(int eventMask, bool immediateNotify) {
inst.notifier.NotifyNetworkInterfacesChanged();
}
}
if ((eventMask & CS_USB_CAMERAS_CHANGED) != 0) {
// start network interface event listener
inst.usbCameraListener.Start();
if (immediateNotify) {
inst.notifier.NotifyUsbCamerasChanged();
}
}
}
CS_Listener AddListener(std::function<void(const RawEvent& event)> callback,