Ocean
AVFMovieRecorder.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_MOVIE_RECORDER_H
9 #define META_OCEAN_MEDIA_AVF_MOVIE_RECORDER_H
10 
13 
15 
16 namespace Ocean
17 {
18 
19 namespace Media
20 {
21 
22 namespace AVFoundation
23 {
24 
25 /**
26  * This class implements a AVFoundation movie recorder.
27  * The recorder can record mp4 files and mov files.
28  * @ingroup mediaavf
29  */
30 class AVFMovieRecorder : virtual public MovieRecorder
31 {
32  friend class AVFLibrary;
33 
34  public:
35 
36  /**
37  * Sets the filename of this recorder.
38  * The filename must be set before recordings starts.
39  * @see MovieRecorder::setFilename().
40  */
41  bool setFilename(const std::string& filename) override;
42 
43  /**
44  * Sets the recorder.
45  * @see ExplicitRecorder::start().
46  */
47  bool start() override;
48 
49  /**
50  * Stops the recorder.
51  * @see ExplicitRecorder::stop().
52  */
53  bool stop() override;
54 
55  /**
56  * Returns whether this recorder is currently recording.
57  * @return True, if so
58  */
59  bool isRecording() const override;
60 
61  /**
62  * Returns a list of possible frame encoders for this recorder.
63  * @return Encoder names
64  */
65  Encoders frameEncoders() const override;
66 
67  /**
68  * Returns a pointer to the most recent buffer to be filled immediately and locks it.
69  * Beware: Call unlockBufferToFill() once the image data is written to the frame.
70  * @param recorderFrame The resulting frame in which the image data can be copied, the frame type of this frame must not be changed
71  * @param respectFrameFrequency Flag determining that a buffer will be returned if it is time for a new frame only
72  */
73  bool lockBufferToFill(Frame& recorderFrame, const bool respectFrameFrequency = true) override;
74 
75  /**
76  * Unlocks the filled buffer.
77  * Beware: The buffer has to be locked by lockBufferToFill() before.
78  */
79  void unlockBufferToFill() override;
80 
81  /**
82  * Translates a frame encoder to a corresponding AVVideoCodecType.
83  * @param frameEncoder The name of the encoder
84  * @return The corresponding AVVideoCodecType value, if existing, invalid otherwise
85  * @see frameEncoders().
86  */
87  static AVVideoCodecType frameEncoderToVideoCodecType(const std::string& frameEncoder);
88 
89  /**
90  * Translates the file extension of a movie to a corresponding AVFileType.
91  * The following extensions are supported: mp4, mov.
92  * @param fileExtension The extension of the movie's filename
93  * @return The corresponding AVFileType value, if existing, invalid otherwise
94  */
95  static AVFileType fileExtensionToFileType(const std::string& fileExtension);
96 
97  protected:
98 
99  /**
100  * Creates a new movie recorder object.
101  */
103 
104  /**
105  * Destructs a movie recorder object.
106  */
107  ~AVFMovieRecorder() override;
108 
109  /**
110  * Creates a new asset writer and all associated resources.
111  * @return True, if succeeded
112  */
114 
115  /**
116  * Releases all resources.
117  */
118  void release();
119 
120  /**
121  * Returns the time for a given timestamp.
122  * @param timestamp Playback timestamp in seconds, with range [0, infinity)
123  */
124  inline CMTime time(const double timestamp);
125 
126  /**
127  * Returns the best matching pixel format for a preferred pixel format.
128  * Depending on the platform, not each preferred pixel format may be supported.
129  * This function allows to determine the best matching pixel format closes to the preferred pixel format.
130  * @param preferredPixelFormat The preferred pixel format, can be invalid
131  * @return The supported pixel format best matching with the preferred pixel format
132  */
134 
135  protected:
136 
137  /// The asset writer.
138  AVAssetWriter* assetWriter_ = nullptr;
139 
140  /// The input for the asset writer.
141  AVAssetWriterInput* assetWriterInput_ = nullptr;
142 
143  /// The pixel buffer adaptor for the input asset writer.
144  AVAssetWriterInputPixelBufferAdaptor* assetWriterInputPixelBufferAdaptor_ = nullptr;
145 
146  /// The pixel buffer to be filled.
147  CVPixelBufferRef pixelBuffer_ = nullptr;
148 
149  /// The pixel buffer accessor.
151 
152  /// The timestamp of the next frame.
153  double nextFrameTimestamp_ = 0.0;
154 
155  /// The timestamp of the previous frame.
156  double previousFrameTimestamp_ = -1.0;
157 
158  /// True, if the recoder is actively recording frames; False, otherwise.
159  bool isRecording_ = false;
160 
161  /// True, if this recorder is stopped.
162  bool isStopped_ = true;
163 };
164 
165 inline CMTime AVFMovieRecorder::time(const double timestamp)
166 {
167  ocean_assert(timestamp >= 0.0);
168 
169  return CMTimeMakeWithSeconds(timestamp, 600);
170 }
171 
172 }
173 
174 }
175 
176 }
177 
178 #endif // META_OCEAN_MEDIA_AVF_MOVIE_RECORDER_H
This class implements Ocean's image class.
Definition: Frame.h:1792
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition: Frame.h:183
This class implements the AVFoundation library.
Definition: AVFLibrary.h:32
This class implements a AVFoundation movie recorder.
Definition: AVFMovieRecorder.h:31
bool isRecording() const override
Returns whether this recorder is currently recording.
static FrameType::PixelFormat bestMatchingPixelFormat(const FrameType::PixelFormat preferredPixelFormat)
Returns the best matching pixel format for a preferred pixel format.
bool start() override
Sets the recorder.
AVAssetWriterInput * assetWriterInput_
The input for the asset writer.
Definition: AVFMovieRecorder.h:141
Encoders frameEncoders() const override
Returns a list of possible frame encoders for this recorder.
bool isStopped_
True, if this recorder is stopped.
Definition: AVFMovieRecorder.h:162
CVPixelBufferRef pixelBuffer_
The pixel buffer to be filled.
Definition: AVFMovieRecorder.h:147
static AVVideoCodecType frameEncoderToVideoCodecType(const std::string &frameEncoder)
Translates a frame encoder to a corresponding AVVideoCodecType.
void unlockBufferToFill() override
Unlocks the filled buffer.
double nextFrameTimestamp_
The timestamp of the next frame.
Definition: AVFMovieRecorder.h:153
AVAssetWriterInputPixelBufferAdaptor * assetWriterInputPixelBufferAdaptor_
The pixel buffer adaptor for the input asset writer.
Definition: AVFMovieRecorder.h:144
bool isRecording_
True, if the recoder is actively recording frames; False, otherwise.
Definition: AVFMovieRecorder.h:159
bool lockBufferToFill(Frame &recorderFrame, const bool respectFrameFrequency=true) override
Returns a pointer to the most recent buffer to be filled immediately and locks it.
PixelBufferAccessor pixelBufferAccessor_
The pixel buffer accessor.
Definition: AVFMovieRecorder.h:150
void release()
Releases all resources.
static AVFileType fileExtensionToFileType(const std::string &fileExtension)
Translates the file extension of a movie to a corresponding AVFileType.
bool createNewAssetWriter()
Creates a new asset writer and all associated resources.
AVAssetWriter * assetWriter_
The asset writer.
Definition: AVFMovieRecorder.h:138
CMTime time(const double timestamp)
Returns the time for a given timestamp.
Definition: AVFMovieRecorder.h:165
double previousFrameTimestamp_
The timestamp of the previous frame.
Definition: AVFMovieRecorder.h:156
~AVFMovieRecorder() override
Destructs a movie recorder object.
bool stop() override
Stops the recorder.
AVFMovieRecorder()
Creates a new movie recorder object.
bool setFilename(const std::string &filename) override
Sets the filename of this recorder.
This class allows to access a Core Video's CVPixelBuffer.
Definition: PixelBufferAccessor.h:31
const std::string & filename() const
Returns the filename of this recorder.
Definition: FileRecorder.h:113
const std::string & frameEncoder() const
Returns the name of the encoder used to encoder the frames.
Definition: FrameRecorder.h:152
This class is the base class for all movie recorder.
Definition: MovieRecorder.h:40
std::vector< std::string > Encoders
Definition of a vector holding encoder names.
Definition: Recorder.h:80
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15