WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
MjpegServer.hpp
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 <string>
8#include <string_view>
9
10#include "wpi/cs/VideoSink.hpp"
11#include "wpi/cs/cscore_cpp.hpp"
12
13namespace wpi::cs {
14
15/**
16 * A sink that acts as a MJPEG-over-HTTP network server.
17 */
18class MjpegServer : public VideoSink {
19 public:
20 MjpegServer() = default;
21
22 /**
23 * Create a MJPEG-over-HTTP server sink.
24 *
25 * @param name Sink name (arbitrary unique identifier)
26 * @param listenAddress TCP listen address (empty string for all addresses)
27 * @param port TCP port number
28 */
29 MjpegServer(std::string_view name, std::string_view listenAddress, int port) {
30 m_handle = CreateMjpegServer(name, listenAddress, port, &m_status);
31 }
32
33 /**
34 * Create a MJPEG-over-HTTP server sink.
35 *
36 * @param name Sink name (arbitrary unique identifier)
37 * @param port TCP port number
38 */
39 MjpegServer(std::string_view name, int port) : MjpegServer(name, "", port) {}
40
41 /**
42 * Get the listen address of the server.
43 */
44 std::string GetListenAddress() const {
45 m_status = 0;
47 }
48
49 /**
50 * Get the port number of the server.
51 */
52 int GetPort() const {
53 m_status = 0;
55 }
56
57 /**
58 * Set the stream resolution for clients that don't specify it.
59 *
60 * <p>It is not necessary to set this if it is the same as the source
61 * resolution.
62 *
63 * <p>Setting this different than the source resolution will result in
64 * increased CPU usage, particularly for MJPEG source cameras, as it will
65 * decompress, resize, and recompress the image, instead of using the
66 * camera's MJPEG image directly.
67 *
68 * @param width width, 0 for unspecified
69 * @param height height, 0 for unspecified
70 */
71 void SetResolution(int width, int height) {
72 m_status = 0;
74 &m_status);
75 SetProperty(GetSinkProperty(m_handle, "height", &m_status), height,
76 &m_status);
77 }
78
79 /**
80 * Set the stream frames per second (FPS) for clients that don't specify it.
81 *
82 * <p>It is not necessary to set this if it is the same as the source FPS.
83 *
84 * @param fps FPS, 0 for unspecified
85 */
86 void SetFPS(int fps) {
87 m_status = 0;
89 }
90
91 /**
92 * Set the compression for clients that don't specify it.
93 *
94 * <p>Setting this will result in increased CPU usage for MJPEG source cameras
95 * as it will decompress and recompress the image instead of using the
96 * camera's MJPEG image directly.
97 *
98 * @param quality JPEG compression quality (0-100), -1 for unspecified
99 */
100 void SetCompression(int quality) {
101 m_status = 0;
102 SetProperty(GetSinkProperty(m_handle, "compression", &m_status), quality,
103 &m_status);
104 }
105
106 /**
107 * Set the default compression used for non-MJPEG sources. If not set,
108 * 80 is used. This function has no effect on MJPEG source cameras; use
109 * SetCompression() instead to force recompression of MJPEG source images.
110 *
111 * @param quality JPEG compression quality (0-100)
112 */
113 void SetDefaultCompression(int quality) {
114 m_status = 0;
115 SetProperty(GetSinkProperty(m_handle, "default_compression", &m_status),
116 quality, &m_status);
117 }
118};
119
120} // namespace wpi::cs
@ name
Definition base.h:690
void SetResolution(int width, int height)
Set the stream resolution for clients that don't specify it.
Definition MjpegServer.hpp:71
std::string GetListenAddress() const
Get the listen address of the server.
Definition MjpegServer.hpp:44
void SetCompression(int quality)
Set the compression for clients that don't specify it.
Definition MjpegServer.hpp:100
void SetFPS(int fps)
Set the stream frames per second (FPS) for clients that don't specify it.
Definition MjpegServer.hpp:86
int GetPort() const
Get the port number of the server.
Definition MjpegServer.hpp:52
MjpegServer(std::string_view name, int port)
Create a MJPEG-over-HTTP server sink.
Definition MjpegServer.hpp:39
MjpegServer(std::string_view name, std::string_view listenAddress, int port)
Create a MJPEG-over-HTTP server sink.
Definition MjpegServer.hpp:29
void SetDefaultCompression(int quality)
Set the default compression used for non-MJPEG sources.
Definition MjpegServer.hpp:113
VideoSink() noexcept=default
CS_Sink m_handle
Definition VideoSink.hpp:229
CS_Status m_status
Definition VideoSink.hpp:228
std::string GetMjpegServerListenAddress(CS_Sink sink, CS_Status *status)
int GetMjpegServerPort(CS_Sink sink, CS_Status *status)
void SetProperty(CS_Property property, int value, CS_Status *status)
CS_Sink CreateMjpegServer(std::string_view name, std::string_view listenAddress, int port, CS_Status *status)
CS_Property GetSinkProperty(CS_Sink sink, std::string_view name, CS_Status *status)
CameraServer (cscore) namespace.
Definition CvSource.hpp:15