Ocean
AMovieRecorder.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_A_MOVIE_RECORDER_H
9 #define META_OCEAN_MEDIA_A_MOVIE_RECORDER_H
10 
13 
15 
17 
18 #include <cstdio>
19 
20 namespace Ocean
21 {
22 
23 namespace Media
24 {
25 
26 namespace Android
27 {
28 
29 #if defined(__ANDROID_API__) && __ANDROID_API__ >= 21
30 
31 /**
32  * This class implements a movie recorder class for Android.
33  * NOTE: The supported pixel formats for recording may vary from device to device.
34  * @ingroup mediaandroid
35  */
36 class OCEAN_MEDIA_A_EXPORT AMovieRecorder : virtual public MovieRecorder
37 {
38  friend class ALibrary;
39 
40  protected:
41 
42  /// Bitrate at which videos are saved.
43  static constexpr int DEFAULT_BITRATE = 2000000; // 2 Mbps
44 
45  /// Frequency at which I-frames are saved to the output.
46  static constexpr int DEFAULT_IFRAME_INTERVAL_SECONDS = 1;
47 
48  /**
49  * Definition of a scoped object for FILE pointers.
50  */
52 
53  public:
54 
55  /**
56  * Sets the filename of this recorder.
57  * The filename must be set before recordings starts.
58  * @see MovieRecorder::setFilename().
59  */
60  bool setFilename(const std::string& filename) override;
61 
62  /**
63  * Sets the preferred frame type of this recorder.
64  * @see FrameRecorder::setPreferredFrameType().
65  */
66  bool setPreferredFrameType(const FrameType& type) override;
67 
68  /**
69  * Sets the recorder.
70  * @see ExplicitRecorder::start().
71  */
72  bool start() override;
73 
74  /**
75  * Stops the recorder.
76  * @see ExplicitRecorder::stop().
77  */
78  bool stop() override;
79 
80  /**
81  * Returns whether this recorder is currently recording.
82  * @return True, if so
83  */
84  bool isRecording() const override;
85 
86  /**
87  * Returns a list of possible frame encoders for this recorder.
88  * @return Encoder names
89  */
90  Encoders frameEncoders() const override;
91 
92  /**
93  * Returns a pointer to the most recent buffer to be filled immediately and locks it.
94  * Beware: Call unlockBufferToFill() once the image data is written to the frame.
95  * @param recorderFrame The resulting frame in which the image data can be copied, the frame type of this frame must not be changed
96  * @param respectFrameFrequency Flag determining that a buffer will be returned if it is time for a new frame only
97  * @return True if the buffer was successfully locked, otherwise false
98  */
99  bool lockBufferToFill(Frame& recorderFrame, const bool respectFrameFrequency = true) override;
100 
101  /**
102  * Unlocks the filled buffer.
103  * Beware: The buffer has to be locked by lockBufferToFill() before.
104  */
105  void unlockBufferToFill() override;
106 
107  protected:
108 
109  /**
110  * Creates a new movie recorder object.
111  */
113 
114  /**
115  * Destroys a movie recorder object.
116  */
117  ~AMovieRecorder() override;
118 
119  /**
120  * Creates a new media muxer and all associated resources.
121  * @return True, if succeeded
122  */
124 
125  /**
126  * Releases all resources.
127  */
128  void release();
129 
130  protected:
131 
132  /**
133  * Reads output from the codec and writes the resulting buffer to the muxer. This function should only be called if input data was previously submitted to the codec.
134  * @param loopUntilEndOfStream If true, continuously read from the buffer until an end-of-stream flag is presented; this should only be used when the recording is complete and a buffer with the EOS flag set has been submitted as input to the codec
135  * @return True if the read and write succeeded, false otherwise.
136  */
137  bool readCodecOutputBufferAndWriteToMuxer(const bool loopUntilEndOfStream = false);
138 
139  protected:
140 
141  /// The underlying media format
142  AMediaFormat* mediaFormat_ = nullptr;
143 
144  /// The underlying media codec that takes frame data as input and outputs encoded video frames.
145  AMediaCodec* mediaCodec_ = nullptr;
146 
147  /// The underlying media muxer that will save the result to a file containing the codec output.
148  AMediaMuxer* mediaMuxer_ = nullptr;
149 
150  /// The underlying file being written to.
152 
153  /// Current input buffer index of the codec, valid (>= 0) when the buffer is locked.
154  ssize_t bufferIndex_ = -1;
155 
156  /// Current size of the input buffer for the codec, valid (> 0) when the buffer is locked.
157  size_t bufferSize_ = 0;
158 
159  /// Output track for the muxer.
160  ssize_t trackIndex_ = -1;
161 
162  /// The timestamp of the next frame.
163  double nextFrameTimestamp_ = 0.0;
164 
165  /// True, if the recorder is actively recording frames; false, otherwise.
166  bool isRecording_ = false;
167 
168  /// True, if this recorder is stopped.
169  bool isStopped_ = false;
170 };
171 
172 #endif // defined(__ANDROID_API__) && __ANDROID_API__ >= 21
173 
174 }
175 
176 }
177 
178 }
179 
180 #endif // META_OCEAN_MEDIA_A_MOVIE_RECORDER_H
This class implements Ocean's image class.
Definition: Frame.h:1792
Definition of a frame type composed by the frame dimension, pixel format and pixel origin.
Definition: Frame.h:30
This class implements the android library.
Definition: ALibrary.h:36
This class implements a movie recorder class for Android.
Definition: AMovieRecorder.h:37
void release()
Releases all resources.
Encoders frameEncoders() const override
Returns a list of possible frame encoders for this recorder.
bool setFilename(const std::string &filename) override
Sets the filename of this recorder.
bool stop() override
Stops the recorder.
bool setPreferredFrameType(const FrameType &type) override
Sets the preferred frame type of this recorder.
bool isRecording() const override
Returns whether this recorder is currently recording.
void unlockBufferToFill() override
Unlocks the filled buffer.
bool createNewMediaCodec()
Creates a new media muxer and all associated resources.
bool readCodecOutputBufferAndWriteToMuxer(const bool loopUntilEndOfStream=false)
Reads output from the codec and writes the resulting buffer to the muxer.
ScopedFILE file_
The underlying file being written to.
Definition: AMovieRecorder.h:151
~AMovieRecorder() override
Destroys a movie recorder object.
AMovieRecorder()
Creates a new movie recorder object.
bool lockBufferToFill(Frame &recorderFrame, const bool respectFrameFrequency=true) override
Returns a pointer to the most recent buffer to be filled immediately and locks it.
bool start() override
Sets the recorder.
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