WPILibC++ 2023.4.3
SendableCameraWrapper.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8#include <memory>
9#include <span>
10#include <string>
11#include <string_view>
12
13#ifndef DYNAMIC_CAMERA_SERVER
14#include <cscore_oo.h>
15#include <fmt/format.h>
18#else
19namespace cs {
20class VideoSource;
21} // namespace cs
22using CS_Handle = int; // NOLINT
23using CS_Source = CS_Handle; // NOLINT
24#endif
25
29
30namespace frc {
31
32class SendableCameraWrapper;
33
34namespace detail {
35constexpr const char* kProtocol = "camera_server://";
36std::shared_ptr<SendableCameraWrapper>& GetSendableCameraWrapper(
37 std::string_view cameraName);
39} // namespace detail
40
41/**
42 * A wrapper to make video sources sendable and usable from Shuffleboard.
43 */
45 : public wpi::Sendable,
46 public wpi::SendableHelper<SendableCameraWrapper> {
47 private:
48 struct private_init {};
49
50 public:
51 /**
52 * Creates a new sendable wrapper. Private constructor to avoid direct
53 * instantiation with multiple wrappers floating around for the same camera.
54 *
55 * @param cameraName the name of the camera to wrap
56 */
57 SendableCameraWrapper(std::string_view cameraName, const private_init&);
58
59 /**
60 * Creates a new sendable wrapper. Private constructor to avoid direct
61 * instantiation with multiple wrappers floating around for the same camera.
62 *
63 * @param cameraName the name of the camera to wrap
64 * @param cameraUrls camera URLs
65 */
67 std::span<const std::string> cameraUrls,
68 const private_init&);
69
70 /**
71 * Gets a sendable wrapper object for the given video source, creating the
72 * wrapper if one does not already exist for the source.
73 *
74 * @param source the video source to wrap
75 * @return a sendable wrapper object for the video source, usable in
76 * Shuffleboard via ShuffleboardTab::Add() and ShuffleboardLayout::Add()
77 */
80
82 std::span<const std::string> cameraUrls);
83
84 void InitSendable(wpi::SendableBuilder& builder) override;
85
86 private:
87 std::string m_uri;
89};
90
91#ifndef DYNAMIC_CAMERA_SERVER
93 const private_init&)
94 : m_uri(detail::kProtocol) {
96 m_uri += name;
97}
98
100 std::string_view cameraName, std::span<const std::string> cameraUrls,
101 const private_init&)
102 : SendableCameraWrapper(cameraName, private_init{}) {
105 fmt::format("/CameraPublisher/{}/streams", cameraName))
106 .Publish();
107 m_streams.Set(cameraUrls);
108}
109
111 const cs::VideoSource& source) {
112 return Wrap(source.GetHandle());
113}
114
116 CS_Status status = 0;
117 auto name = cs::GetSourceName(source, &status);
118 auto& wrapper = detail::GetSendableCameraWrapper(name);
119 if (!wrapper) {
120 wrapper = std::make_shared<SendableCameraWrapper>(name, private_init{});
121 }
122 return *wrapper;
123}
124
126 std::string_view cameraName, std::span<const std::string> cameraUrls) {
127 auto& wrapper = detail::GetSendableCameraWrapper(cameraName);
128 if (!wrapper) {
129 wrapper = std::make_shared<SendableCameraWrapper>(cameraName, cameraUrls,
130 private_init{});
131 }
132 return *wrapper;
133}
134#endif
135
136} // namespace frc
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation source
Definition: ThirdPartyNotices.txt:114
A source for video that provides a sequence of frames.
Definition: cscore_oo.h:96
A wrapper to make video sources sendable and usable from Shuffleboard.
Definition: SendableCameraWrapper.h:46
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
static SendableCameraWrapper & Wrap(const cs::VideoSource &source)
Gets a sendable wrapper object for the given video source, creating the wrapper if one does not alrea...
Definition: SendableCameraWrapper.h:110
SendableCameraWrapper(std::string_view cameraName, const private_init &)
Creates a new sendable wrapper.
Definition: SendableCameraWrapper.h:92
static NetworkTableInstance GetDefault()
Get global default instance.
Definition: NetworkTableInstance.inc:22
StringArrayTopic GetStringArrayTopic(std::string_view name) const
Gets a string array topic.
NetworkTables StringArray publisher.
Definition: StringArrayTopic.h:110
void Set(ParamType value, int64_t time=0)
Publish a new value.
Definition: StringArrayTopic.inc:48
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition: StringArrayTopic.inc:89
Definition: SendableBuilder.h:18
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:19
Interface for Sendable objects.
Definition: Sendable.h:16
basic_string_view< char > string_view
Definition: core.h:520
std::string GetSourceName(CS_Source source, CS_Status *status)
CS_Handle CS_Source
Definition: cscore_c.h:51
int CS_Status
Definition: cscore_c.h:44
int CS_Handle
Definition: cscore_c.h:46
CameraServer (cscore) namespace.
Definition: cscore_cpp.h:31
Definition: chrono.h:303
constexpr const char * kProtocol
Definition: SendableCameraWrapper.h:35
std::shared_ptr< SendableCameraWrapper > & GetSendableCameraWrapper(std::string_view cameraName)
void AddToSendableRegistry(wpi::Sendable *sendable, std::string_view name)
Definition: AprilTagFieldLayout.h:22
GHC_FS_API file_status status(const path &p, std::error_code &ec) noexcept
Definition: filesystem.hpp:4892
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:87