Ocean
Loading...
Searching...
No Matches
AVFLiveVideo.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_AVF_LIVE_VIDEO_H
9#define META_OCEAN_MEDIA_AVF_LIVE_VIDEO_H
10
13
15
16namespace Ocean
17{
18
19namespace Media
20{
21
22namespace AVFoundation
23{
24
25/**
26 * This class implements a AVFoundation live video object.
27 * @ingroup mediaavf
28 */
30 virtual public AVFFrameMedium,
31 virtual public LiveVideo
32{
33 friend class AVFLibrary;
34
35 public:
36
37 /**
38 * Definition of a callback function for new samples.
39 */
41
42 public:
43
44 /**
45 * Clones this live video medium and returns a new independent instance of this medium.
46 * @see Medium::clone()
47 */
48 MediumRef clone() const override;
49
50 /**
51 * Returns the transformation between the camera and device.
52 * @see FrameMedium::device_T_camera().
53 */
55
56 /**
57 * Returns the supported stream types.
58 * @see LiveVideo::supportedStreamTypes().
59 */
61
62 /**
63 * Returns the supported stream configurations for a given stream type.
64 * @see LiveVideo::supportedStreamConfigurations().
65 */
67
68 /**
69 * Returns the current exposure duration of this device.
70 * @see exposureDuration().
71 */
72 double exposureDuration(double* minDuration = nullptr, double* maxDuration = nullptr, ControlMode* exposureMode = nullptr) const override;
73
74 /**
75 * Returns the current ISO of this device.
76 * @see iso().
77 */
78 float iso(float* minISO = nullptr, float* maxISO = nullptr, ControlMode* isoMode = nullptr) const override;
79
80 /**
81 * Returns the current focus of this device.
82 * @see focus().
83 */
84 float focus(ControlMode* focusMode = nullptr) const override;
85
86 /**
87 * Sets the preferred stream type.
88 * @see LiveVideo::setPreferredStreamConfiguration().
89 */
90 bool setPreferredStreamType(const StreamType streamType) override;
91
92 /**
93 * Sets the preferred stream configuration.
94 * @see LiveVideo::setPreferredStreamConfiguration().
95 */
96 bool setPreferredStreamConfiguration(const StreamConfiguration& streamConfiguration) override;
97
98 /**
99 * Sets the exposure duriation of this device.
100 * @see setExposureDuration().
101 */
102 bool setExposureDuration(const double duration, const bool allowShorterExposure = false) override;
103
104 /**
105 * Sets the ISO of this device.
106 * @see setISO().
107 */
108 bool setISO(const float iso) override;
109
110 /**
111 * Sets the focus of this device.
112 * @see setFocus().
113 */
114 bool setFocus(const float position) override;
115
116 /**
117 * Sets the known camera profile of this frame medium.
118 * @see FrameMedium::setCamera()
119 */
120 bool setCamera(SharedAnyCamera&& camera) override;
121
122 /**
123 * Returns whether video stabilization is currently enabled.
124 * @see videoStabilization().
125 */
126 bool videoStabilization() const override;
127
128 /**
129 * Sets whether video stabilization should be enabled.
130 * @see setVideoStabilization().
131 */
132 bool setVideoStabilization(const bool enable) override;
133
134 /**
135 * Explicitly feeds a new external pixel buffer into this live video.
136 * This function is intended for situations in which this live video does not receive the pixel buffers anymore from the system (e.g., when ARKit is accessing the video stream).<br>
137 * Do not call this function in case the live video is still receiving pixel buffers from the AVFoundation system.
138 * @param pixelBuffer A pointer to a buffer that contains the pixel data of the sample
139 * @param camera The profile of the camera, invalid if unknown
140 * @param unixTimestamp Used to set timestamp of the Frame associated to the sample (measured in seconds since 1970-01-01 00:00 UTC)
141 * @param sampleTime The presentation time of the sample in seconds, used to set the relative timestamp of the frame associated to the sample
142 */
143 void feedNewSample(CVPixelBufferRef pixelBuffer, SharedAnyCamera camera, const double unixTimestamp, const double sampleTime);
144
145 protected:
146
147 /**
148 * Creates a new live video by a given url.
149 * @param url Url of the live video
150 */
151 explicit AVFLiveVideo(const std::string& url);
152
153 /**
154 * Destructs a AVFLiveVideo object.
155 */
156 ~AVFLiveVideo() override;
157
158 /**
159 * Creates the capture device necessary for the live video.
160 * @return True, if succeeded
161 */
163
164 /**
165 * Creates the capture session neccessary for the live video.
166 * This function needs a valid capture device object.
167 * @return True, if succeeded
168 */
170
171 /**
172 * Releases the capture session.
173 */
175
176 /**
177 * Internally starts the medium.
178 * @see AVFMedium::internalStart()
179 */
180 bool internalStart() override;
181
182 /**
183 * Internally pauses the medium.
184 * @see AVFMedium::internalPause()
185 */
186 bool internalPause() override;
187
188 /**
189 * Internally stops the medium.
190 * @see AVFMedium::internalStop()
191 */
192 bool internalStop() override;
193
194 /**
195 * Called, if a new sample arrived.
196 * @see AVFFrameMedium::onNewSample(pixelBuffer, unixTimestamp, sampleTime)
197 */
198 void onNewSample(CVPixelBufferRef pixelBuffer, SharedAnyCamera camera, const double unixTimestamp, const double sampleTime) override;
199
200 /**
201 * Returns the best matching capture device format for a specified frame dimension, pixel format and frame frequency.
202 * @param captureDevice The capture device for which the best matching format will be determined, must be valid
203 * @param preferredWidth The preferred width of the frame in pixel, with range [0, infinity), 0 to use any width
204 * @param preferredHeight The preferred height of the frame in pixel, with range [0, infinity), 0 to use any height
205 * @param preferredFrameFrequency The preferred frame frequency of the frame, with range (0, infinity), 0 to use any frame frequency
206 * @param preferredPixelFormat The preferred pixel format of the frame, FrameType::FORMAT_UNDEFINED to use any pixel format
207 * @param explicitFrameRate The resulting explicit frame rate of the best matching format, with range (0, infinity)
208 * @return The best matching capture device format, nullptr if no matching format could be found
209 */
210 static AVCaptureDeviceFormat* bestMatchingCaptureDeviceFormat(AVCaptureDevice* captureDevice, const unsigned int preferredWidth, const unsigned int preferredHeight, const double preferredFrameFrequency, const FrameType::PixelFormat preferredPixelFormat, double& explicitFrameRate);
211
212 /**
213 * Returns the best matching (horizontal) field of view for a specified frame dimension.
214 * The field of view value is determine from the device's parameters/description, if available.<br>
215 * This function is not supported on non-IOS platforms.
216 * @param device The device for which the field of view will be determined, must be valid
217 * @param width The width of the frame in pixel, with range (0, infinity)
218 * @param height The height of the frame in pixel, with range (0, infinity)
219 * @return The best matching horizontal field of view in radian, -1 if no field of view could be determined
220 */
221 static double bestMatchingFieldOfView(AVCaptureDevice* device, const unsigned int width, const unsigned int height);
222
223 /**
224 * Determines the available stream configurations for the capture device.
225 * @return The resulting stream configurations
226 */
228
229 protected:
230
231 /// The capture device object.
232 AVCaptureDevice* captureDevice_ = nullptr;
233
234 /// The capture session.
235 AVCaptureSession* captureSession_ = nullptr;
236
237 /// The device input for the capture session.
238 AVCaptureDeviceInput* captureDeviceInput_ = nullptr;
239
240 /// The video data output for the capture session.
241 AVCaptureVideoDataOutput* captureVideoDataOutput_ = nullptr;
242
243 /// The delegate object for new sample callbacks.
245
246 /// The manual exposure duration of live video device, in seconds -1 or 0 if no manual exposure is selected.
247 double exposureDuration_ = -1.0;
248
249 /// The manual ISO of the live video device, -1 if no manual exposure is selected.
250 float iso_ = -1.0f;
251
252 /// Whether video stabilization is enabled (true) or disabled (false).
254
255 /// The number of measurements of ISO and exposure values.
256 unsigned int isoExposureMeasurements_ = 0u;
257
258 /// The sum of ISO values.
259 float isoSum_ = 0.0f;
260
261 /// The sum of exposure duration values, in seconds.
263
264 /// The camera profile for all images.
266
267 /// The approximated camera profile of this live video.
269
270 /// True, if the medium waits for the first frame; False, if several frames have been received already.
272
273 /// The transformation between camera and device.
275
276 /// The stream configurations available for this camera.
278};
279
280}
281
282}
283
284}
285
286#endif // META_OCEAN_MEDIA_AVF_LIVE_VIDEO_H
This class implements a container for callback functions.
Definition Callback.h:3454
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition Frame.h:183
This is the base class for all AVFoundation frame mediums.
Definition AVFFrameMedium.h:34
This class implements the AVFoundation library.
Definition AVFLibrary.h:32
This class implements a AVFoundation live video object.
Definition AVFLiveVideo.h:32
float exposureDurationSum_
The sum of exposure duration values, in seconds.
Definition AVFLiveVideo.h:262
bool setExposureDuration(const double duration, const bool allowShorterExposure=false) override
Sets the exposure duriation of this device.
AVCaptureDeviceInput * captureDeviceInput_
The device input for the capture session.
Definition AVFLiveVideo.h:238
AVCaptureSession * captureSession_
The capture session.
Definition AVFLiveVideo.h:235
bool videoStabilizationEnabled_
Whether video stabilization is enabled (true) or disabled (false).
Definition AVFLiveVideo.h:253
float iso(float *minISO=nullptr, float *maxISO=nullptr, ControlMode *isoMode=nullptr) const override
Returns the current ISO of this device.
bool setCamera(SharedAnyCamera &&camera) override
Sets the known camera profile of this frame medium.
unsigned int isoExposureMeasurements_
The number of measurements of ISO and exposure values.
Definition AVFLiveVideo.h:256
bool setPreferredStreamConfiguration(const StreamConfiguration &streamConfiguration) override
Sets the preferred stream configuration.
bool internalStop() override
Internally stops the medium.
StreamConfigurations availableStreamConfigurations_
The stream configurations available for this camera.
Definition AVFLiveVideo.h:277
bool createCaptureDevice()
Creates the capture device necessary for the live video.
bool setFocus(const float position) override
Sets the focus of this device.
SharedAnyCamera camera_
The camera profile for all images.
Definition AVFLiveVideo.h:265
StreamConfigurations supportedStreamConfigurations(const StreamType streamType=ST_INVALID) const override
Returns the supported stream configurations for a given stream type.
AVFLiveVideo(const std::string &url)
Creates a new live video by a given url.
double exposureDuration(double *minDuration=nullptr, double *maxDuration=nullptr, ControlMode *exposureMode=nullptr) const override
Returns the current exposure duration of this device.
static AVCaptureDeviceFormat * bestMatchingCaptureDeviceFormat(AVCaptureDevice *captureDevice, const unsigned int preferredWidth, const unsigned int preferredHeight, const double preferredFrameFrequency, const FrameType::PixelFormat preferredPixelFormat, double &explicitFrameRate)
Returns the best matching capture device format for a specified frame dimension, pixel format and fra...
float isoSum_
The sum of ISO values.
Definition AVFLiveVideo.h:259
static double bestMatchingFieldOfView(AVCaptureDevice *device, const unsigned int width, const unsigned int height)
Returns the best matching (horizontal) field of view for a specified frame dimension.
float focus(ControlMode *focusMode=nullptr) const override
Returns the current focus of this device.
HomogenousMatrixD4 device_T_camera_
The transformation between camera and device.
Definition AVFLiveVideo.h:274
HomogenousMatrixD4 device_T_camera() const override
Returns the transformation between the camera and device.
AVCaptureVideoDataOutput * captureVideoDataOutput_
The video data output for the capture session.
Definition AVFLiveVideo.h:241
bool waitingForFirstFrame_
True, if the medium waits for the first frame; False, if several frames have been received already.
Definition AVFLiveVideo.h:271
~AVFLiveVideo() override
Destructs a AVFLiveVideo object.
void feedNewSample(CVPixelBufferRef pixelBuffer, SharedAnyCamera camera, const double unixTimestamp, const double sampleTime)
Explicitly feeds a new external pixel buffer into this live video.
bool setISO(const float iso) override
Sets the ISO of this device.
bool videoStabilization() const override
Returns whether video stabilization is currently enabled.
double exposureDuration_
The manual exposure duration of live video device, in seconds -1 or 0 if no manual exposure is select...
Definition AVFLiveVideo.h:247
bool internalStart() override
Internally starts the medium.
MediumRef clone() const override
Clones this live video medium and returns a new independent instance of this medium.
void onNewSample(CVPixelBufferRef pixelBuffer, SharedAnyCamera camera, const double unixTimestamp, const double sampleTime) override
Called, if a new sample arrived.
AVCaptureDevice * captureDevice_
The capture device object.
Definition AVFLiveVideo.h:232
NSObject< AVCaptureVideoDataOutputSampleBufferDelegate > * sampleBufferDelegate_
The delegate object for new sample callbacks.
Definition AVFLiveVideo.h:244
float iso_
The manual ISO of the live video device, -1 if no manual exposure is selected.
Definition AVFLiveVideo.h:250
StreamTypes supportedStreamTypes() const override
Returns the supported stream types.
bool setVideoStabilization(const bool enable) override
Sets whether video stabilization should be enabled.
bool createCaptureSession()
Creates the capture session neccessary for the live video.
SharedAnyCamera approximatedCamera_
The approximated camera profile of this live video.
Definition AVFLiveVideo.h:268
bool setPreferredStreamType(const StreamType streamType) override
Sets the preferred stream type.
bool internalPause() override
Internally pauses the medium.
StreamConfigurations determineAvailableStreamConfigurations() const
Determines the available stream configurations for the capture device.
void releaseCaptureSession()
Releases the capture session.
FrameFrequency preferredFrameFrequency() const
Returns the preferred frame frequency in Hz.
Definition FrameMedium.h:668
This class holds the relevant information describing a video stream configuration.
Definition LiveVideo.h:166
This class is the base class for all live videos.
Definition LiveVideo.h:38
StreamType
Definition of individual stream types.
Definition LiveVideo.h:64
@ ST_INVALID
An invalid stream type.
Definition LiveVideo.h:66
ControlMode
Definition of individual control modes.
Definition LiveVideo.h:46
std::vector< StreamConfiguration > StreamConfigurations
Definition of a vector holding stream configurations.
Definition LiveVideo.h:207
std::vector< StreamType > StreamTypes
Definition of a vector holding stream types.
Definition LiveVideo.h:78
const std::string & url() const
Returns the url of the medium.
Definition Medium.h:236
std::shared_ptr< AnyCamera > SharedAnyCamera
Definition of a shared pointer holding an AnyCamera object with Scalar precision.
Definition AnyCamera.h:61
HomogenousMatrixT4< double > HomogenousMatrixD4
Instantiation of the HomogenousMatrixT4 template class using a double precision float data type.
Definition HomogenousMatrix4.h:51
The namespace covering the entire Ocean framework.
Definition Accessor.h:15