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 * Returns whether video stabilization is currently enabled.
118 * @see videoStabilization().
119 */
120 bool videoStabilization() const override;
121
122 /**
123 * Sets whether video stabilization should be enabled.
124 * @see setVideoStabilization().
125 */
126 bool setVideoStabilization(const bool enable) override;
127
128 /**
129 * Explicitly feeds a new external pixel buffer into this live video.
130 * 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>
131 * Do not call this function in case the live video is still receiving pixel buffers from the AVFoundation system.
132 * @param pixelBuffer A pointer to a buffer that contains the pixel data of the sample
133 * @param anyCamera The profile of the camera, invalid if unknown
134 * @param unixTimestamp Used to set timestamp of the Frame associated to the sample (measured in seconds since 1970-01-01 00:00 UTC)
135 * @param sampleTime The presentation time of the sample in seconds, used to set the relative timestamp of the frame associated to the sample
136 */
137 void feedNewSample(CVPixelBufferRef pixelBuffer, SharedAnyCamera anyCamera, const double unixTimestamp, const double sampleTime);
138
139 protected:
140
141 /**
142 * Creates a new live video by a given url.
143 * @param url Url of the live video
144 */
145 explicit AVFLiveVideo(const std::string& url);
146
147 /**
148 * Destructs a AVFLiveVideo object.
149 */
150 ~AVFLiveVideo() override;
151
152 /**
153 * Creates the capture device necessary for the live video.
154 * @return True, if succeeded
155 */
157
158 /**
159 * Creates the capture session neccessary for the live video.
160 * This function needs a valid capture device object.
161 * @return True, if succeeded
162 */
164
165 /**
166 * Releases the capture session.
167 */
169
170 /**
171 * Internally starts the medium.
172 * @see AVFMedium::internalStart()
173 */
174 bool internalStart() override;
175
176 /**
177 * Internally pauses the medium.
178 * @see AVFMedium::internalPause()
179 */
180 bool internalPause() override;
181
182 /**
183 * Internally stops the medium.
184 * @see AVFMedium::internalStop()
185 */
186 bool internalStop() override;
187
188 /**
189 * Called, if a new sample arrived.
190 * @see AVFFrameMedium::onNewSample(pixelBuffer, unixTimestamp, sampleTime)
191 */
192 void onNewSample(CVPixelBufferRef pixelBuffer, SharedAnyCamera anyCamera, const double unixTimestamp, const double sampleTime) override;
193
194 /**
195 * Returns the best matching capture device format for a specified frame dimension, pixel format and frame frequency.
196 * @param captureDevice The capture device for which the best matching format will be determined, must be valid
197 * @param preferredWidth The preferred width of the frame in pixel, with range [0, infinity), 0 to use any width
198 * @param preferredHeight The preferred height of the frame in pixel, with range [0, infinity), 0 to use any height
199 * @param preferredFrameFrequency The preferred frame frequency of the frame, with range (0, infinity), 0 to use any frame frequency
200 * @param preferredPixelFormat The preferred pixel format of the frame, FrameType::FORMAT_UNDEFINED to use any pixel format
201 * @param explicitFrameRate The resulting explicit frame rate of the best matching format, with range (0, infinity)
202 * @return The best matching capture device format, nullptr if no matching format could be found
203 */
204 static AVCaptureDeviceFormat* bestMatchingCaptureDeviceFormat(AVCaptureDevice* captureDevice, const unsigned int preferredWidth, const unsigned int preferredHeight, const double preferredFrameFrequency, const FrameType::PixelFormat preferredPixelFormat, double& explicitFrameRate);
205
206 /**
207 * Returns the best matching (horizontal) field of view for a specified frame dimension.
208 * The field of view value is determine from the device's parameters/description, if available.<br>
209 * This function is not supported on non-IOS platforms.
210 * @param device The device for which the field of view will be determined, must be valid
211 * @param width The width of the frame in pixel, with range (0, infinity)
212 * @param height The height of the frame in pixel, with range (0, infinity)
213 * @return The best matching horizontal field of view in radian, -1 if no field of view could be determined
214 */
215 static double bestMatchingFieldOfView(AVCaptureDevice* device, const unsigned int width, const unsigned int height);
216
217 /**
218 * Determines the available stream configurations for the capture device.
219 * @return The resulting stream configurations
220 */
222
223 protected:
224
225 /// The capture device object.
226 AVCaptureDevice* captureDevice_ = nullptr;
227
228 /// The capture session.
229 AVCaptureSession* captureSession_ = nullptr;
230
231 /// The device input for the capture session.
232 AVCaptureDeviceInput* captureDeviceInput_ = nullptr;
233
234 /// The video data output for the capture session.
235 AVCaptureVideoDataOutput* captureVideoDataOutput_ = nullptr;
236
237 /// The delegate object for new sample callbacks.
239
240 /// The manual exposure duration of live video device, in seconds -1 or 0 if no manual exposure is selected.
241 double exposureDuration_ = -1.0;
242
243 /// The manual ISO of the live video device, -1 if no manual exposure is selected.
244 float iso_ = -1.0f;
245
246 /// Whether video stabilization is enabled (true) or disabled (false).
248
249 /// The number of measurements of ISO and exposure values.
250 unsigned int isoExposureMeasurements_ = 0u;
251
252 /// The sum of ISO values.
253 float isoSum_ = 0.0f;
254
255 /// The sum of exposure duration values, in seconds.
257
258 /// The approximated camera profile of this live video.
260
261 /// The transformation between camera and device.
263
264 /// The stream configurations available for this camera.
266};
267
268}
269
270}
271
272}
273
274#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:256
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:232
AVCaptureSession * captureSession_
The capture session.
Definition AVFLiveVideo.h:229
bool videoStabilizationEnabled_
Whether video stabilization is enabled (true) or disabled (false).
Definition AVFLiveVideo.h:247
float iso(float *minISO=nullptr, float *maxISO=nullptr, ControlMode *isoMode=nullptr) const override
Returns the current ISO of this device.
unsigned int isoExposureMeasurements_
The number of measurements of ISO and exposure values.
Definition AVFLiveVideo.h:250
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:265
bool createCaptureDevice()
Creates the capture device necessary for the live video.
bool setFocus(const float position) override
Sets the focus of this device.
StreamConfigurations supportedStreamConfigurations(const StreamType streamType=ST_INVALID) const override
Returns the supported stream configurations for a given stream type.
void onNewSample(CVPixelBufferRef pixelBuffer, SharedAnyCamera anyCamera, const double unixTimestamp, const double sampleTime) override
Called, if a new sample arrived.
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:253
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.
void feedNewSample(CVPixelBufferRef pixelBuffer, SharedAnyCamera anyCamera, const double unixTimestamp, const double sampleTime)
Explicitly feeds a new external pixel buffer into this live video.
HomogenousMatrixD4 device_T_camera_
The transformation between camera and device.
Definition AVFLiveVideo.h:262
SharedAnyCamera approximatedAnyCamera_
The approximated camera profile of this live video.
Definition AVFLiveVideo.h:259
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:235
~AVFLiveVideo() override
Destructs a AVFLiveVideo object.
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:241
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.
AVCaptureDevice * captureDevice_
The capture device object.
Definition AVFLiveVideo.h:226
NSObject< AVCaptureVideoDataOutputSampleBufferDelegate > * sampleBufferDelegate_
The delegate object for new sample callbacks.
Definition AVFLiveVideo.h:238
float iso_
The manual ISO of the live video device, -1 if no manual exposure is selected.
Definition AVFLiveVideo.h:244
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.
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:60
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