Ocean
Loading...
Searching...
No Matches
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
20namespace Ocean
21{
22
23namespace Media
24{
25
26namespace 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 */
36class OCEAN_MEDIA_A_EXPORT AMovieRecorder : virtual public MovieRecorder
37{
38 friend class ALibrary;
39
40 protected:
41
42 /// The default bitrate of the recorder, in bits per second.
43 static constexpr int defaultBitrate_ = 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 preferred bit rate when recording the data.
70 * @see Recorder::setPreferredBitrate().
71 */
72 bool setPreferredBitrate(const unsigned int preferredBitrate) override;
73
74 /**
75 * Sets the recorder.
76 * @see ExplicitRecorder::start().
77 */
78 bool start() override;
79
80 /**
81 * Stops the recorder.
82 * @see ExplicitRecorder::stop().
83 */
84 bool stop() override;
85
86 /**
87 * Returns whether this recorder is currently recording.
88 * @return True, if so
89 */
90 bool isRecording() const override;
91
92 /**
93 * Returns a list of possible frame encoders for this recorder.
94 * @return Encoder names
95 */
96 Encoders frameEncoders() const override;
97
98 /**
99 * Returns a pointer to the most recent buffer to be filled immediately and locks it.
100 * Beware: Call unlockBufferToFill() once the image data is written to the frame.
101 * @param recorderFrame The resulting frame in which the image data can be copied, the frame type of this frame must not be changed
102 * @param respectFrameFrequency Flag determining that a buffer will be returned if it is time for a new frame only
103 * @return True if the buffer was successfully locked, otherwise false
104 */
105 bool lockBufferToFill(Frame& recorderFrame, const bool respectFrameFrequency = true) override;
106
107 /**
108 * Unlocks the filled buffer.
109 * Beware: The buffer has to be locked by lockBufferToFill() before.
110 */
111 void unlockBufferToFill() override;
112
113 protected:
114
115 /**
116 * Creates a new movie recorder object.
117 */
119
120 /**
121 * Destroys a movie recorder object.
122 */
123 ~AMovieRecorder() override;
124
125 /**
126 * Creates a new media muxer and all associated resources.
127 * @return True, if succeeded
128 */
130
131 /**
132 * Releases all resources.
133 */
134 void release();
135
136 protected:
137
138 /**
139 * 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.
140 * @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
141 * @return True if the read and write succeeded, false otherwise.
142 */
143 bool readCodecOutputBufferAndWriteToMuxer(const bool loopUntilEndOfStream = false);
144
145 protected:
146
147 /// The underlying media format
148 AMediaFormat* mediaFormat_ = nullptr;
149
150 /// The underlying media codec that takes frame data as input and outputs encoded video frames.
151 AMediaCodec* mediaCodec_ = nullptr;
152
153 /// The underlying media muxer that will save the result to a file containing the codec output.
154 AMediaMuxer* mediaMuxer_ = nullptr;
155
156 /// The underlying file being written to.
158
159 /// Current input buffer index of the codec, valid (>= 0) when the buffer is locked.
160 ssize_t bufferIndex_ = -1;
161
162 /// Current size of the input buffer for the codec, valid (> 0) when the buffer is locked.
163 size_t bufferSize_ = 0;
164
165 /// Output track for the muxer.
166 ssize_t trackIndex_ = -1;
167
168 /// The timestamp of the next frame.
169 double nextFrameTimestamp_ = 0.0;
170
171 /// True, if the recorder is actively recording frames; false, otherwise.
172 bool isRecording_ = false;
173
174 /// True, if this recorder is stopped.
175 bool isStopped_ = false;
176
177 /// The preferred bitrate of the recorder, in bits per second.
178 unsigned int preferredBitrate_ = defaultBitrate_;
179};
180
181#endif // defined(__ANDROID_API__) && __ANDROID_API__ >= 21
182
183}
184
185}
186
187}
188
189#endif // META_OCEAN_MEDIA_A_MOVIE_RECORDER_H
This class implements Ocean's image class.
Definition Frame.h:1808
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:37
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:157
~AMovieRecorder() override
Destroys a movie recorder object.
bool setPreferredBitrate(const unsigned int preferredBitrate) override
Sets the preferred bit rate when recording the data.
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