Ocean
Loading...
Searching...
No Matches
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
16namespace Ocean
17{
18
19namespace Media
20{
21
22namespace 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 */
30class 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 preferred bit rate when recording the data.
45 * @see Recorder::setPreferredBitrate().
46 */
47 bool setPreferredBitrate(const unsigned int preferredBitrate) override;
48
49 /**
50 * Sets the recorder.
51 * @see ExplicitRecorder::start().
52 */
53 bool start() override;
54
55 /**
56 * Stops the recorder.
57 * @see ExplicitRecorder::stop().
58 */
59 bool stop() override;
60
61 /**
62 * Returns whether this recorder is currently recording.
63 * @return True, if so
64 */
65 bool isRecording() const override;
66
67 /**
68 * Returns a list of possible frame encoders for this recorder.
69 * @return Encoder names
70 */
71 Encoders frameEncoders() const override;
72
73 /**
74 * Returns a pointer to the most recent buffer to be filled immediately and locks it.
75 * Beware: Call unlockBufferToFill() once the image data is written to the frame.
76 * @param recorderFrame The resulting frame in which the image data can be copied, the frame type of this frame must not be changed
77 * @param respectFrameFrequency Flag determining that a buffer will be returned if it is time for a new frame only
78 */
79 bool lockBufferToFill(Frame& recorderFrame, const bool respectFrameFrequency = true) override;
80
81 /**
82 * Unlocks the filled buffer.
83 * Beware: The buffer has to be locked by lockBufferToFill() before.
84 */
85 void unlockBufferToFill() override;
86
87 /**
88 * Translates a frame encoder to a corresponding AVVideoCodecType.
89 * @param frameEncoder The name of the encoder
90 * @return The corresponding AVVideoCodecType value, if existing, invalid otherwise
91 * @see frameEncoders().
92 */
93 static AVVideoCodecType frameEncoderToVideoCodecType(const std::string& frameEncoder);
94
95 /**
96 * Translates the file extension of a movie to a corresponding AVFileType.
97 * The following extensions are supported: mp4, mov.
98 * @param fileExtension The extension of the movie's filename
99 * @return The corresponding AVFileType value, if existing, invalid otherwise
100 */
101 static AVFileType fileExtensionToFileType(const std::string& fileExtension);
102
103 protected:
104
105 /**
106 * Creates a new movie recorder object.
107 */
109
110 /**
111 * Destructs a movie recorder object.
112 */
114
115 /**
116 * Creates a new asset writer and all associated resources.
117 * @return True, if succeeded
118 */
120
121 /**
122 * Releases all resources.
123 */
124 void release();
125
126 /**
127 * Returns the time for a given timestamp.
128 * @param timestamp Playback timestamp in seconds, with range [0, infinity)
129 */
130 inline CMTime time(const double timestamp);
131
132 /**
133 * Returns the best matching pixel format for a preferred pixel format.
134 * Depending on the platform, not each preferred pixel format may be supported.
135 * This function allows to determine the best matching pixel format closes to the preferred pixel format.
136 * @param preferredPixelFormat The preferred pixel format, can be invalid
137 * @return The supported pixel format best matching with the preferred pixel format
138 */
140
141 protected:
142
143 /// The asset writer.
144 AVAssetWriter* assetWriter_ = nullptr;
145
146 /// The input for the asset writer.
147 AVAssetWriterInput* assetWriterInput_ = nullptr;
148
149 /// The pixel buffer adaptor for the input asset writer.
150 AVAssetWriterInputPixelBufferAdaptor* assetWriterInputPixelBufferAdaptor_ = nullptr;
151
152 /// The pixel buffer to be filled.
153 CVPixelBufferRef pixelBuffer_ = nullptr;
154
155 /// The pixel buffer accessor.
157
158 /// The timestamp of the next frame.
160
161 /// The timestamp of the previous frame.
163
164 /// True, if the recoder is actively recording frames; False, otherwise.
165 bool isRecording_ = false;
166
167 /// True, if this recorder is stopped.
168 bool isStopped_ = true;
169
170 /// The preferred bit rate, in bits per seconds, with range (0, infinity).
171 unsigned int preferredBitrate_ = 0u;
172};
173
174inline CMTime AVFMovieRecorder::time(const double timestamp)
175{
176 ocean_assert(timestamp >= 0.0);
177
178 return CMTimeMakeWithSeconds(timestamp, 600);
179}
180
181}
182
183}
184
185}
186
187#endif // META_OCEAN_MEDIA_AVF_MOVIE_RECORDER_H
This class implements Ocean's image class.
Definition Frame.h:1808
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 setPreferredBitrate(const unsigned int preferredBitrate) override
Sets the preferred bit rate when recording the data.
bool start() override
Sets the recorder.
AVAssetWriterInput * assetWriterInput_
The input for the asset writer.
Definition AVFMovieRecorder.h:147
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:168
CVPixelBufferRef pixelBuffer_
The pixel buffer to be filled.
Definition AVFMovieRecorder.h:153
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:159
AVAssetWriterInputPixelBufferAdaptor * assetWriterInputPixelBufferAdaptor_
The pixel buffer adaptor for the input asset writer.
Definition AVFMovieRecorder.h:150
bool isRecording_
True, if the recoder is actively recording frames; False, otherwise.
Definition AVFMovieRecorder.h:165
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:156
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:144
CMTime time(const double timestamp)
Returns the time for a given timestamp.
Definition AVFMovieRecorder.h:174
unsigned int preferredBitrate_
The preferred bit rate, in bits per seconds, with range (0, infinity).
Definition AVFMovieRecorder.h:171
double previousFrameTimestamp_
The timestamp of the previous frame.
Definition AVFMovieRecorder.h:162
~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