Ocean
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 
14 #include "ocean/media/LiveVideo.h"
15 
16 namespace Ocean
17 {
18 
19 namespace Media
20 {
21 
22 namespace AVFoundation
23 {
24 
25 /**
26  * This class implements a AVFoundation live video object.
27  * @ingroup mediaavf
28  */
29 class AVFLiveVideo :
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 current exposure duration of this device.
58  * @see exposureDuration().
59  */
60  double exposureDuration(double* minDuration = nullptr, double* maxDuration = nullptr, ControlMode* exposureMode = nullptr) const override;
61 
62  /**
63  * Returns the current ISO of this device.
64  * @see iso().
65  */
66  float iso(float* minISO = nullptr, float* maxISO = nullptr, ControlMode* isoMode = nullptr) const override;
67 
68  /**
69  * Returns the current focus of this device.
70  * @see focus().
71  */
72  float focus(ControlMode* focusMode = nullptr) const override;
73 
74  /**
75  * Sets the exposure duriation of this device.
76  * @see setExposureDuration().
77  */
78  bool setExposureDuration(const double duration) override;
79 
80  /**
81  * Sets the ISO of this device.
82  * @see setISO().
83  */
84  bool setISO(const float iso) override;
85 
86  /**
87  * Sets the focus of this device.
88  * @see setFocus().
89  */
90  bool setFocus(const float position) override;
91 
92  /**
93  * Explicitly feeds a new external pixel buffer into this live video.
94  * 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>
95  * Do not call this function in case the live video is still receiving pixel buffers from the AVFoundation system.
96  * @param pixelBuffer A pointer to a buffer that contains the pixel data of the sample
97  * @param anyCamera The profile of the camera, invalid if unknown
98  * @param unixTimestamp Used to set timestamp of the Frame associated to the sample (measured in seconds since 1970-01-01 00:00 UTC)
99  * @param sampleTime The presentation time of the sample in seconds, used to set the relative timestamp of the frame associated to the sample
100  */
101  void feedNewSample(CVPixelBufferRef pixelBuffer, SharedAnyCamera anyCamera, const double unixTimestamp, const double sampleTime);
102 
103  protected:
104 
105  /**
106  * Creates a new live video by a given url.
107  * @param url Url of the live video
108  */
109  explicit AVFLiveVideo(const std::string& url);
110 
111  /**
112  * Destructs a AVFLiveVideo object.
113  */
114  ~AVFLiveVideo() override;
115 
116  /**
117  * Creates the capture device necessary for the live video.
118  * @return True, if succeeded
119  */
121 
122  /**
123  * Creates the capture session neccessary for the live video.
124  * This function needs a valid capture device object.
125  * @return True, if succeeded
126  */
128 
129  /**
130  * Releases the capture session.
131  */
133 
134  /**
135  * Internally starts the medium.
136  * @see AVFMedium::internalStart()
137  */
138  bool internalStart() override;
139 
140  /**
141  * Internally pauses the medium.
142  * @see AVFMedium::internalPause()
143  */
144  bool internalPause() override;
145 
146  /**
147  * Internally stops the medium.
148  * @see AVFMedium::internalStop()
149  */
150  bool internalStop() override;
151 
152  /**
153  * Called, if a new sample arrived.
154  * @see AVFFrameMedium::onNewSample(pixelBuffer, unixTimestamp, sampleTime)
155  */
156  void onNewSample(CVPixelBufferRef pixelBuffer, SharedAnyCamera anyCamera, const double unixTimestamp, const double sampleTime) override;
157 
158  /**
159  * Determines the exact session preset for a specified frame dimension.
160  * @param width The width of the frame in pixel, with range (0, infinity)
161  * @param height The height of the frame in pixel, with range (0, infinity)
162  * @param presetWidth The width of the frame matching to the resulting preset, with range (0, infinity), 0 if no valid preset exists
163  * @param presetHeight The height of the frame matching to the resulting preset, with range (0, infinity), 0 if no valid preset exists
164  * @return The preset matching to the specified frame dimension, nullptr if no exact preset exists
165  */
166  static NSString* determineExactPreset(const unsigned int width, const unsigned int height, unsigned int& presetWidth, unsigned int& presetHeight);
167 
168  /**
169  * Determines the best matching or next larger preset for a specified frame dimension.
170  * @param width The width of the frame in pixel, with range (0, infinity)
171  * @param height The height of the frame in pixel, with range (0, infinity)
172  * @param presetWidth The width of the frame matching to the resulting preset, with range (0, infinity)
173  * @param presetHeight The height of the frame matching to the resulting preset, with range (0, infinity)
174  * @return The best preset better or equal to the specified frame dimension
175  */
176  static NSString* determineNextLargerPreset(const unsigned int width, const unsigned int height, unsigned int& presetWidth, unsigned int& presetHeight);
177 
178  /**
179  * Returns the best matching (horizontal) field of view for a specified frame dimension.
180  * The field of view value is determine from the device's parameters/description, if available.<br>
181  * This function is not supported on non-IOS platforms.
182  * @param device The device for which the field of view will be determined, must be valid
183  * @param width The width of the frame in pixel, with range (0, infinity)
184  * @param height The height of the frame in pixel, with range (0, infinity)
185  * @return The best matching horizontal field of view in radian, -1 if no field of view could be determined
186  */
187  static double bestMatchingFieldOfView(AVCaptureDevice* device, const unsigned int width, const unsigned int height);
188 
189  protected:
190 
191  /// The capture device object.
192  AVCaptureDevice* captureDevice_ = nullptr;
193 
194  /// The capture session.
195  AVCaptureSession* captureSession_ = nullptr;
196 
197  /// The device input for the capture session.
198  AVCaptureDeviceInput* captureDeviceInput_ = nullptr;
199 
200  /// The video data output for the capture session.
201  AVCaptureVideoDataOutput* captureVideoDataOutput_ = nullptr;
202 
203  /// The delegate object for new sample callbacks.
205 
206  /// The number of measurements of ISO and exposure values.
207  unsigned int isoExposureMeasurements_ = 0u;
208 
209  /// The sum of ISO values.
210  float isoSum_ = 0.0f;
211 
212  /// The sum of exposure duration values, in seconds.
213  float exposureDurationSum_ = 0.0f;
214 
215  /// The approximated camera profile of this live video.
217 
218  /// The transformation between camera and device.
220 };
221 
222 }
223 
224 }
225 
226 }
227 
228 #endif // META_OCEAN_MEDIA_AVF_LIVE_VIDEO_H
This class implements a container for callback functions.
Definition: Callback.h:3456
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:213
AVCaptureDeviceInput * captureDeviceInput_
The device input for the capture session.
Definition: AVFLiveVideo.h:198
AVCaptureSession * captureSession_
The capture session.
Definition: AVFLiveVideo.h:195
static NSString * determineExactPreset(const unsigned int width, const unsigned int height, unsigned int &presetWidth, unsigned int &presetHeight)
Determines the exact session preset for a specified frame dimension.
float iso(float *minISO=nullptr, float *maxISO=nullptr, ControlMode *isoMode=nullptr) const override
Returns the current ISO of this device.
Callback< void, CVPixelBufferRef, SharedAnyCamera, double, double > OnNewSampleCallback
Definition of a callback function for new samples.
Definition: AVFLiveVideo.h:40
unsigned int isoExposureMeasurements_
The number of measurements of ISO and exposure values.
Definition: AVFLiveVideo.h:207
bool internalStop() override
Internally stops the medium.
bool createCaptureDevice()
Creates the capture device necessary for the live video.
bool setFocus(const float position) override
Sets the focus of this device.
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.
float isoSum_
The sum of ISO values.
Definition: AVFLiveVideo.h:210
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:219
SharedAnyCamera approximatedAnyCamera_
The approximated camera profile of this live video.
Definition: AVFLiveVideo.h:216
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:201
~AVFLiveVideo() override
Destructs a AVFLiveVideo object.
bool setISO(const float iso) override
Sets the ISO of this device.
static NSString * determineNextLargerPreset(const unsigned int width, const unsigned int height, unsigned int &presetWidth, unsigned int &presetHeight)
Determines the best matching or next larger preset for a specified frame dimension.
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.
bool setExposureDuration(const double duration) override
Sets the exposure duriation of this device.
AVCaptureDevice * captureDevice_
The capture device object.
Definition: AVFLiveVideo.h:192
NSObject< AVCaptureVideoDataOutputSampleBufferDelegate > * sampleBufferDelegate_
The delegate object for new sample callbacks.
Definition: AVFLiveVideo.h:204
bool createCaptureSession()
Creates the capture session neccessary for the live video.
bool internalPause() override
Internally pauses the medium.
void releaseCaptureSession()
Releases the capture session.
This class is the base class for all live videos.
Definition: LiveVideo.h:38
ControlMode
Definition of individual control modes.
Definition: LiveVideo.h:46
const std::string & url() const
Returns the url of the medium.
Definition: Medium.h:236
This template class implements a object reference with an internal reference counter.
Definition: base/ObjectRef.h:58
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