Ocean
Loading...
Searching...
No Matches
LiveVideo.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_MEDIA_LIVE_VIDEO_H
9#define META_OCEAN_MEDIA_LIVE_VIDEO_H
10
11#include "ocean/media/Media.h"
14
15namespace Ocean
16{
17
18namespace Media
19{
20
21// Forward declaration.
22class LiveVideo;
23
24/**
25 * Definition of a smart medium reference holding a live video object.
26 * @see SmartMediumRef, LiveVideo.
27 * @ingroup media
28 */
30
31/**
32 * This class is the base class for all live videos.
33 * @ingroup media
34 */
35class OCEAN_MEDIA_EXPORT LiveVideo :
36 public virtual FrameMedium,
37 public virtual LiveMedium
38{
39 public:
40
41 /**
42 * Definition of individual control modes.
43 * The modes are used for exposure, ISO, and focus.
44 */
45 enum ControlMode : uint32_t
46 {
47 /// An invalid control mode.
48 CM_INVALID = 0u,
49 /// The control is fixed (e.g., because the exposure, ISO, or focus was set manually).
51 /// The control is dynamic (e.g., because auto exposure, ISO, or focus is enabled).
52 CM_DYNAMIC
53 };
54
55 /**
56 * Definition of individual stream types.
57 */
58 enum StreamType : uint32_t
59 {
60 /// An invalid stream type.
61 ST_INVALID = 0u,
62 /// A stream composed of individual uncompressed frames with individual pixel formats (e.g., FORMAT_RGB24, FORMAT_YUYV16, etc.).
64 /// A stream composed of Motion JPEG frames.
66 /// A stream composed of compressed frames with individual codecs (e.g., H264, H265).
67 ST_CODEC
68 };
69
70 /**
71 * Definition of a vector holding stream types.
72 */
73 using StreamTypes = std::vector<StreamType>;
74
75 /**
76 * Definition of individual codec types.
77 */
78 enum CodecType : uint32_t
79 {
80 /// An invalid codec type.
81 CT_INVALID = 0u,
82 /// Codec using H.264 for encoding or decoding.
84 /// Codec using H.265 for encoding or decoding.
85 CT_H265
86 };
87
88 /**
89 * This class holds the relevant information describing the property of a stream.
90 */
91 class OCEAN_MEDIA_EXPORT StreamProperty
92 {
93 public:
94
95 /**
96 * Simple helper struct allowing to calculate a hash value for stream property.
97 */
98 struct Hash
99 {
100 /**
101 * Returns a hash value for a StreamProperty object.
102 * @param streamProperty The object for which the hash will be returned
103 * @return The hash value
104 */
105 inline size_t operator()(const StreamProperty& streamProperty) const;
106 };
107
108 public:
109
110 /**
111 * Default constructor creating an invalid object.
112 */
113 StreamProperty() = default;
114
115 /**
116 * Creates a new stream property object.
117 * @param streamType The type of the stream
118 * @param width The width of the stream in pixel
119 * @param height The height of the stream in pixel
120 * @param framePixelFormat The pixel format of the stream, invalid if stream type is not ST_FRAME
121 * @param codecType The type of the stream, invalid if stream type is not ST_CODEC
122 */
123 StreamProperty(const StreamType streamType, const unsigned int width, unsigned int height, const FrameType::PixelFormat framePixelFormat, const CodecType codecType);
124
125 /**
126 * Returns whether this configuration object holds a valid configuration.
127 * The configuration is valid if a valid stream type and a valid image resolution is defined.
128 * @return True, if so
129 */
130 inline bool isValid() const;
131
132 /**
133 * Returns whether this configuration object holds the same configuration as the given one.
134 * @param right The configuration object to compare
135 * @return True, if so
136 */
137 bool operator==(const StreamProperty& right) const;
138
139 public:
140
141 /// The type of the stream.
142 StreamType streamType_ = ST_INVALID;
143
144 /// The width of the stream in pixel.
145 unsigned int width_ = 0u;
146
147 /// The height of the stream in pixel.
148 unsigned int height_ = 0u;
149
150 /// The pixel format of the stream, only valid if the stream type is ST_FRAME.
151 FrameType::PixelFormat framePixelFormat_ = FrameType::FORMAT_UNDEFINED;
152
153 /// The codec of the stream, only valid if the stream type is ST_CODEC.
154 CodecType codecType_ = CT_INVALID;
155 };
156
157 /**
158 * This class holds the relevant information describing a video stream configuration.
159 */
160 class OCEAN_MEDIA_EXPORT StreamConfiguration : public StreamProperty
161 {
162 public:
163
164 /**
165 * Default constructor creating an invalid object.
166 */
168
169 /**
170 * Creates a new stream configuration object.
171 * @param streamProperty The property of the stream
172 * @param frameRates The frame rates of the stream in Hz
173 */
174 StreamConfiguration(const StreamProperty& streamProperty, std::vector<double>&& frameRates);
175
176 /**
177 * Creates a new stream configuration object.
178 * @param streamType The type of the stream
179 * @param width The width of the stream in pixel
180 * @param height The height of the stream in pixel
181 * @param frameRates The frame rates of the stream in Hz
182 * @param framePixelFormat The pixel format of the stream, invalid if stream type is not ST_FRAME
183 * @param codecType The type of the stream, invalid if stream type is not ST_CODEC
184 */
185 StreamConfiguration(const StreamType streamType, const unsigned int width, unsigned int height, std::vector<double>&& frameRates, const FrameType::PixelFormat framePixelFormat, const CodecType codecType);
186
187 /**
188 * Returns a string containing the readable information of this stream configuration.
189 * @return The readable information of this stream configuration
190 */
191 std::string toString() const;
192
193 public:
194
195 /// The frame rates of the stream in Hz.
196 std::vector<double> frameRates_;
197 };
198
199 /**
200 * Definition of a vector holding stream configurations.
201 */
202 using StreamConfigurations = std::vector<StreamConfiguration>;
203
204 public:
205
206 /**
207 * Returns the supported stream types.
208 * @return The types of all supported streams, empty if this object does not allow (or does not need) to select the stream type.
209 */
211
212 /**
213 * Returns the supported stream configurations for a given stream type.
214 * @param streamType The type of the stream for which the supported stream configurations will be returned, ST_INVALID to return all stream configurations for all stream types
215 * @return The supported stream configurations
216 */
217 virtual StreamConfigurations supportedStreamConfigurations(const StreamType streamType = ST_INVALID) const;
218
219 /**
220 * Returns the current exposure duration of this device.
221 * @param minDuration Optional resulting minimal duration to set, in seconds, with range (0, infinity), -1 if unknown
222 * @param maxDuration Optional resulting maximal duration to set, in seconds, with range [minDuration, infinity), -1 if unknown
223 * @param exposureMode Optional resulting exposure mode, nullptr if not of interest
224 * @return The duration in seconds, with range [minDuration, maxDuration], -1 if unknown
225 * @see setExposureDuration().
226 */
227 virtual double exposureDuration(double* minDuration = nullptr, double* maxDuration = nullptr, ControlMode* exposureMode = nullptr) const;
228
229 /**
230 * Returns the current ISO of this device.
231 * @param minISO Optional resulting minimal ISO to set, with range (0, infinity), -1 if unknown
232 * @param maxISO Optional resulting maximal ISO to set, with range (0, infinity), -1 if unknown
233 * @param isoMode Optional resulting ISO mode, nullptr if not of interest
234 * @return The current ISO, with range [minISO, maxISO], -1 if unknown
235 */
236 virtual float iso(float* minISO = nullptr, float* maxISO = nullptr, ControlMode* isoMode = nullptr) const;
237
238 /**
239 * Returns the current focus of this device.
240 * @return The device's focus, with range [0, 1] with 0 shortest distance and 1 furthest distance
241 * @param focusMode Optional resulting focus mode, nullptr if not of interest
242 */
243 virtual float focus(ControlMode* focusMode = nullptr) const;
244
245 /**
246 * Sets the preferred stream type.
247 * There is no guarantee that the device will use this stream type.
248 * @param streamType The preferred stream type to be set, must be valid
249 * @return True, if succeeded
250 * @see setPreferredStreamConfiguration().
251 */
252 virtual bool setPreferredStreamType(const StreamType streamType);
253
254 /**
255 * Sets the preferred stream configuration.
256 * Using this function will forward some settings to the underlying FrameMedium object via setPreferredFrameDimension(), setPreferredFramePixelFormat(), and setPreferredFrameFrequency().
257 * There is no guarantee that the device will use this stream type.
258 * @param streamConfiguration The preferred stream configuration to be set, must be valid
259 * @return True, if succeeded
260 * @see setPreferredStreamType(), FrameMedium::setPreferredFrameDimension(), FrameMedium::setPreferredFramePixelFormat(), and FrameMedium::setPreferredFrameFrequency()
261 */
262 virtual bool setPreferredStreamConfiguration(const StreamConfiguration& streamConfiguration);
263
264 /**
265 * Sets the exposure duration of this device.
266 * @param duration The exposure duration to be set, in seconds, with range (0, infinity), 0 for auto exposure, -1 for a one-time auto exposure
267 * @param allowShorterExposure True, to allow shorter exposure durations than the specified duration; False, to enforce the specified duration; Ignored if auto exposure is used
268 * @return True, if succeeded
269 * @see exposureDuration().
270 */
271 virtual bool setExposureDuration(const double duration, const bool allowShorterExposure = false);
272
273 /**
274 * Sets the ISO of this device.
275 * @param iso The ISO to be set, with range (0, infinity), -1 for auto ISO
276 * @return True, if succeeded
277 * @see iso().
278 */
279 virtual bool setISO(const float iso);
280
281 /**
282 * Sets the focus of this device.
283 * @param position The focus position to be set, with range [0, 1] with 0 shortest distance and 1 furthest distance, -1 for auto focus
284 * @return True, if succeeded
285 * @see focus().
286 */
287 virtual bool setFocus(const float position);
288
289 /**
290 * Translates a control mode to a string.
291 * @param controlMode The control mode to translate
292 * @return The translated string, 'Invalid' if the control mode is invalid or unknown
293 */
294 static std::string translateControlMode(const ControlMode controlMode);
295
296 /**
297 * Translates a stream type to a string.
298 * @param streamType The stream type to be translated
299 * @return The translated string, 'Invalid' if the stream type is invalid or unknown
300 */
301 static std::string translateStreamType(const StreamType streamType);
302
303 /**
304 * Translates a codec type to a string.
305 * @param codecType The stream type to be translated
306 * @return The translated string, 'Invalid' if the codec type is invalid or unknown
307 */
308 static std::string translateCodecType(const CodecType codecType);
309
310 protected:
311
312 /**
313 * Creates a new live video source by a given url.
314 * @param url Url of the live video source
315 */
316 explicit LiveVideo(const std::string& url);
317};
318
319inline size_t LiveVideo::StreamProperty::Hash::operator()(const StreamProperty& streamProperty) const
320{
321 size_t seed = std::hash<StreamType>{}(streamProperty.streamType_);
322 seed ^= std::hash<unsigned int>{}(streamProperty.width_) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
323 seed ^= std::hash<unsigned int>{}(streamProperty.height_) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
324 seed ^= std::hash<FrameType::PixelFormat>{}(streamProperty.framePixelFormat_) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
325 seed ^= std::hash<CodecType>{}(streamProperty.codecType_) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
326
327 return seed;
328}
329
331{
332 return streamType_ != ST_INVALID && width_ > 0u && height_ > 0u;
333}
334
335}
336
337}
338
339#endif // META_OCEAN_MEDIA_LIVE_VIDEO_H
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition Frame.h:183
This is the base class for all frame mediums.
Definition FrameMedium.h:53
This class it the base class for all live mediums.
Definition LiveMedium.h:38
This class holds the relevant information describing a video stream configuration.
Definition LiveVideo.h:161
std::vector< double > frameRates_
The frame rates of the stream in Hz.
Definition LiveVideo.h:196
std::string toString() const
Returns a string containing the readable information of this stream configuration.
StreamConfiguration(const StreamProperty &streamProperty, std::vector< double > &&frameRates)
Creates a new stream configuration object.
StreamConfiguration()=default
Default constructor creating an invalid object.
StreamConfiguration(const StreamType streamType, const unsigned int width, unsigned int height, std::vector< double > &&frameRates, const FrameType::PixelFormat framePixelFormat, const CodecType codecType)
Creates a new stream configuration object.
This class holds the relevant information describing the property of a stream.
Definition LiveVideo.h:92
FrameType::PixelFormat framePixelFormat_
The pixel format of the stream, only valid if the stream type is ST_FRAME.
Definition LiveVideo.h:151
StreamProperty(const StreamType streamType, const unsigned int width, unsigned int height, const FrameType::PixelFormat framePixelFormat, const CodecType codecType)
Creates a new stream property object.
CodecType codecType_
The codec of the stream, only valid if the stream type is ST_CODEC.
Definition LiveVideo.h:154
StreamProperty()=default
Default constructor creating an invalid object.
StreamType streamType_
The type of the stream.
Definition LiveVideo.h:142
unsigned int height_
The height of the stream in pixel.
Definition LiveVideo.h:148
unsigned int width_
The width of the stream in pixel.
Definition LiveVideo.h:145
bool operator==(const StreamProperty &right) const
Returns whether this configuration object holds the same configuration as the given one.
bool isValid() const
Returns whether this configuration object holds a valid configuration.
Definition LiveVideo.h:330
This class is the base class for all live videos.
Definition LiveVideo.h:38
virtual double exposureDuration(double *minDuration=nullptr, double *maxDuration=nullptr, ControlMode *exposureMode=nullptr) const
Returns the current exposure duration of this device.
CodecType
Definition of individual codec types.
Definition LiveVideo.h:79
@ CT_H264
Codec using H.264 for encoding or decoding.
Definition LiveVideo.h:83
LiveVideo(const std::string &url)
Creates a new live video source by a given url.
virtual bool setPreferredStreamType(const StreamType streamType)
Sets the preferred stream type.
virtual bool setExposureDuration(const double duration, const bool allowShorterExposure=false)
Sets the exposure duration of this device.
virtual StreamConfigurations supportedStreamConfigurations(const StreamType streamType=ST_INVALID) const
Returns the supported stream configurations for a given stream type.
StreamType
Definition of individual stream types.
Definition LiveVideo.h:59
@ ST_INVALID
An invalid stream type.
Definition LiveVideo.h:61
@ ST_MJPEG
A stream composed of Motion JPEG frames.
Definition LiveVideo.h:65
@ ST_FRAME
A stream composed of individual uncompressed frames with individual pixel formats (e....
Definition LiveVideo.h:63
ControlMode
Definition of individual control modes.
Definition LiveVideo.h:46
@ CM_FIXED
The control is fixed (e.g., because the exposure, ISO, or focus was set manually).
Definition LiveVideo.h:50
virtual bool setFocus(const float position)
Sets the focus of this device.
virtual bool setISO(const float iso)
Sets the ISO of this device.
std::vector< StreamConfiguration > StreamConfigurations
Definition of a vector holding stream configurations.
Definition LiveVideo.h:202
virtual StreamTypes supportedStreamTypes() const
Returns the supported stream types.
static std::string translateControlMode(const ControlMode controlMode)
Translates a control mode to a string.
virtual bool setPreferredStreamConfiguration(const StreamConfiguration &streamConfiguration)
Sets the preferred stream configuration.
virtual float iso(float *minISO=nullptr, float *maxISO=nullptr, ControlMode *isoMode=nullptr) const
Returns the current ISO of this device.
std::vector< StreamType > StreamTypes
Definition of a vector holding stream types.
Definition LiveVideo.h:73
static std::string translateStreamType(const StreamType streamType)
Translates a stream type to a string.
static std::string translateCodecType(const CodecType codecType)
Translates a codec type to a string.
virtual float focus(ControlMode *focusMode=nullptr) const
Returns the current focus of this device.
This class implements a smart medium reference.
Definition MediumRef.h:33
SmartMediumRef< LiveVideo > LiveVideoRef
Definition of a smart medium reference holding a live video object.
Definition LiveVideo.h:29
The namespace covering the entire Ocean framework.
Definition Accessor.h:15
Simple helper struct allowing to calculate a hash value for stream property.
Definition LiveVideo.h:99
size_t operator()(const StreamProperty &streamProperty) const
Returns a hash value for a StreamProperty object.
Definition LiveVideo.h:319