diff --git a/include/cscore_oo.h b/include/cscore_oo.h index d0c347b67d..a394d69e25 100644 --- a/include/cscore_oo.h +++ b/include/cscore_oo.h @@ -8,6 +8,8 @@ #ifndef CSCORE_OO_H_ #define CSCORE_OO_H_ +#include + #include "cscore_cpp.h" namespace cs { @@ -268,6 +270,19 @@ class HttpCamera : public VideoSource { HttpCamera(llvm::StringRef name, llvm::StringRef url, CameraKind kind = kUnknown); + /// Create a source for a MJPEG-over-HTTP (IP) camera. + /// @param name Source name (arbitrary unique identifier) + /// @param url Camera URL (e.g. "http://10.x.y.11/video/stream.mjpg") + /// @param kind Camera kind (e.g. kAxis) + HttpCamera(llvm::StringRef name, const char* url, CameraKind kind = kUnknown); + + /// Create a source for a MJPEG-over-HTTP (IP) camera. + /// @param name Source name (arbitrary unique identifier) + /// @param url Camera URL (e.g. "http://10.x.y.11/video/stream.mjpg") + /// @param kind Camera kind (e.g. kAxis) + HttpCamera(llvm::StringRef name, const std::string& url, + CameraKind kind = kUnknown); + /// Create a source for a MJPEG-over-HTTP (IP) camera. /// @param name Source name (arbitrary unique identifier) /// @param urls Array of Camera URLs @@ -275,6 +290,14 @@ class HttpCamera : public VideoSource { HttpCamera(llvm::StringRef name, llvm::ArrayRef urls, CameraKind kind = kUnknown); + /// Create a source for a MJPEG-over-HTTP (IP) camera. + /// @param name Source name (arbitrary unique identifier) + /// @param urls Array of Camera URLs + /// @param kind Camera kind (e.g. kAxis) + template + HttpCamera(llvm::StringRef name, std::initializer_list urls, + CameraKind kind = kUnknown); + /// Get the kind of HTTP camera. /// Autodetection can result in returning a different value than the camera /// was created with. @@ -283,10 +306,54 @@ class HttpCamera : public VideoSource { /// Change the URLs used to connect to the camera. void SetUrls(llvm::ArrayRef urls); + /// Change the URLs used to connect to the camera. + template + void SetUrls(std::initializer_list urls); + /// Get the URLs used to connect to the camera. std::vector GetUrls() const; }; +/// A source that represents an Axis IP camera. +class AxisCamera : public HttpCamera { + static std::string HostToUrl(llvm::StringRef host); + static std::vector HostToUrl(llvm::ArrayRef hosts); + template + static std::vector HostToUrl(std::initializer_list hosts); + + public: + /// Create a source for an Axis IP camera. + /// @param name Source name (arbitrary unique identifier) + /// @param host Camera host IP or DNS name (e.g. "10.x.y.11") + /// @param kind Camera kind (e.g. kAxis) + AxisCamera(llvm::StringRef name, llvm::StringRef host); + + /// Create a source for an Axis IP camera. + /// @param name Source name (arbitrary unique identifier) + /// @param host Camera host IP or DNS name (e.g. "10.x.y.11") + /// @param kind Camera kind (e.g. kAxis) + AxisCamera(llvm::StringRef name, const char* host); + + /// Create a source for an Axis IP camera. + /// @param name Source name (arbitrary unique identifier) + /// @param host Camera host IP or DNS name (e.g. "10.x.y.11") + /// @param kind Camera kind (e.g. kAxis) + AxisCamera(llvm::StringRef name, const std::string& host); + + /// Create a source for an Axis IP camera. + /// @param name Source name (arbitrary unique identifier) + /// @param hosts Array of Camera host IPs/DNS names + /// @param kind Camera kind (e.g. kAxis) + AxisCamera(llvm::StringRef name, llvm::ArrayRef hosts); + + /// Create a source for an Axis IP camera. + /// @param name Source name (arbitrary unique identifier) + /// @param hosts Array of Camera host IPs/DNS names + /// @param kind Camera kind (e.g. kAxis) + template + AxisCamera(llvm::StringRef name, std::initializer_list hosts); +}; + /// A source for user code to provide OpenCV images as video frames. class CvSource : public VideoSource { public: @@ -343,6 +410,13 @@ class CvSource : public VideoSource { /// @param choices Choices void SetEnumPropertyChoices(const VideoProperty& property, llvm::ArrayRef choices); + + /// Configure enum property choices. + /// @param property Property + /// @param choices Choices + template + void SetEnumPropertyChoices(const VideoProperty& property, + std::initializer_list choices); }; /// A sink for video that accepts a sequence of frames. diff --git a/include/cscore_oo.inl b/include/cscore_oo.inl index 55022930e2..a2ee883e05 100644 --- a/include/cscore_oo.inl +++ b/include/cscore_oo.inl @@ -229,6 +229,17 @@ inline HttpCamera::HttpCamera(llvm::StringRef name, llvm::StringRef url, &m_status); } +inline HttpCamera::HttpCamera(llvm::StringRef name, const char* url, + CameraKind kind) { + m_handle = CreateHttpCamera( + name, url, static_cast(static_cast(kind)), + &m_status); +} + +inline HttpCamera::HttpCamera(llvm::StringRef name, const std::string& url, + CameraKind kind) + : HttpCamera(name, llvm::StringRef{url}, kind) {} + inline HttpCamera::HttpCamera(llvm::StringRef name, llvm::ArrayRef urls, CameraKind kind) { @@ -237,6 +248,17 @@ inline HttpCamera::HttpCamera(llvm::StringRef name, &m_status); } +template +inline HttpCamera::HttpCamera(llvm::StringRef name, + std::initializer_list urls, CameraKind kind) { + std::vector vec; + vec.reserve(urls.size()); + for (const auto& url : urls) vec.emplace_back(url); + m_handle = CreateHttpCamera( + name, vec, static_cast(static_cast(kind)), + &m_status); +} + inline HttpCamera::CameraKind HttpCamera::GetCameraKind() const { m_status = 0; return static_cast( @@ -248,11 +270,64 @@ inline void HttpCamera::SetUrls(llvm::ArrayRef urls) { ::cs::SetHttpCameraUrls(m_handle, urls, &m_status); } +template +inline void HttpCamera::SetUrls(std::initializer_list urls) { + std::vector vec; + vec.reserve(urls.size()); + for (const auto& url : urls) vec.emplace_back(url); + m_status = 0; + ::cs::SetHttpCameraUrls(m_handle, vec, &m_status); +} + inline std::vector HttpCamera::GetUrls() const { m_status = 0; return ::cs::GetHttpCameraUrls(m_handle, &m_status); } +inline std::string AxisCamera::HostToUrl(llvm::StringRef host) { + std::string rv{"http://"}; + rv += host; + rv += "/mjpg/video.mjpg"; + return rv; +} + +inline std::vector AxisCamera::HostToUrl( + llvm::ArrayRef hosts) { + std::vector rv; + rv.reserve(hosts.size()); + for (const auto& host : hosts) + rv.emplace_back(HostToUrl(llvm::StringRef{host})); + return rv; +} + +template +inline std::vector AxisCamera::HostToUrl( + std::initializer_list hosts) { + std::vector rv; + rv.reserve(hosts.size()); + for (const auto& host : hosts) + rv.emplace_back(HostToUrl(llvm::StringRef{host})); + return rv; +} + +inline AxisCamera::AxisCamera(llvm::StringRef name, llvm::StringRef host) + : HttpCamera(name, HostToUrl(host), kAxis) {} + +inline AxisCamera::AxisCamera(llvm::StringRef name, const char* host) + : HttpCamera(name, HostToUrl(host), kAxis) {} + +inline AxisCamera::AxisCamera(llvm::StringRef name, const std::string& host) + : HttpCamera(name, HostToUrl(llvm::StringRef{host}), kAxis) {} + +inline AxisCamera::AxisCamera(llvm::StringRef name, + llvm::ArrayRef hosts) + : HttpCamera(name, HostToUrl(hosts), kAxis) {} + +template +inline AxisCamera::AxisCamera(llvm::StringRef name, + std::initializer_list hosts) + : HttpCamera(name, HostToUrl(hosts), kAxis) {} + inline CvSource::CvSource(llvm::StringRef name, const VideoMode& mode) { m_handle = CreateCvSource(name, mode, &m_status); } @@ -300,6 +375,16 @@ inline void CvSource::SetEnumPropertyChoices( SetSourceEnumPropertyChoices(m_handle, property.m_handle, choices, &m_status); } +template +inline void CvSource::SetEnumPropertyChoices(const VideoProperty& property, + std::initializer_list choices) { + std::vector vec; + vec.reserve(choices.size()); + for (const auto& choice : choices) vec.emplace_back(choice); + m_status = 0; + SetSourceEnumPropertyChoices(m_handle, property.m_handle, vec, &m_status); +} + inline VideoSink::VideoSink(const VideoSink& sink) : m_handle(sink.m_handle == 0 ? 0 : CopySink(sink.m_handle, &m_status)) {} diff --git a/java/src/edu/wpi/cscore/AxisCamera.java b/java/src/edu/wpi/cscore/AxisCamera.java new file mode 100644 index 0000000000..0957edf26c --- /dev/null +++ b/java/src/edu/wpi/cscore/AxisCamera.java @@ -0,0 +1,37 @@ +/*----------------------------------------------------------------------------*/ +/* 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. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.cscore; + +/// A source that represents an Axis IP camera. +public class AxisCamera extends HttpCamera { + private static String hostToUrl(String host) { + return "http://" + host + "/mjpg/video.mjpg"; + } + + private static String[] hostToUrl(String[] hosts) { + String[] urls = new String[hosts.length]; + for (int i = 0; i < hosts.length; i++) { + urls[i] = hostToUrl(hosts[i]); + } + return urls; + } + + /// Create a source for an Axis IP camera. + /// @param name Source name (arbitrary unique identifier) + /// @param host Camera host IP or DNS name (e.g. "10.x.y.11") + public AxisCamera(String name, String host) { + super(name, hostToUrl(host), CameraKind.kAxis); + } + + /// Create a source for an Axis IP camera. + /// @param name Source name (arbitrary unique identifier) + /// @param hosts Array of Camera host IPs/DNS names + public AxisCamera(String name, String[] hosts) { + super(name, hostToUrl(hosts), CameraKind.kAxis); + } +}