Ocean
AMovie.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_ANDROID_A_MOVIE_H
9 #define META_OCEAN_MEDIA_ANDROID_A_MOVIE_H
10 
13 
14 #if defined(__ANDROID_API__) && __ANDROID_API__ >= 24
15 
17 #include "ocean/base/Thread.h"
18 
19 #include "ocean/media/Movie.h"
20 
21 #include <media/NdkMediaExtractor.h>
22 
23 #include <atomic>
24 
25 #include <unistd.h>
26 
27 namespace Ocean
28 {
29 
30 namespace Media
31 {
32 
33 namespace Android
34 {
35 
36 /**
37  * This class implements a movie recorder class for Android.
38  * NOTE: The supported pixel formats for recording may vary from device to device.
39  * @ingroup mediaandroid
40  */
41 class OCEAN_MEDIA_A_EXPORT AMovie final :
42  virtual public AMedium,
43  virtual public Movie,
44  protected Thread
45 {
46  friend class ALibrary;
47 
48  protected:
49 
50  /**
51  * Definition of a scoped file based on a file descriptor.
52  */
53  using ScopedFileDescriptor = ScopedObjectCompileTimeT<int, int, int, close, 0, true, -1>;
54 
55  public:
56 
57  /**
58  * Clones this movie medium and returns a new independent instance of this medium.
59  * @see Medium::clone()
60  */
61  MediumRef clone() const override;
62 
63  /**
64  * Starts the medium.
65  * @see Medium::start().
66  */
67  bool start() override;
68 
69  /**
70  * Pauses the medium.
71  * @see Medium::pause():
72  */
73  bool pause() override;
74 
75  /**
76  * Stops the medium.
77  * @see Medium::stop().
78  */
79  bool stop() override;
80 
81  /**
82  * Returns whether the medium is started currently.
83  * @see Medium::isStarted().
84  */
85  bool isStarted() const override;
86 
87  /**
88  * Returns the duration of the finite medium.
89  * @see FiniteMedium::duration().
90  */
91  double duration() const override;
92 
93  /**
94  * Returns the duration without speed consideration.
95  * @see FiniteMedium::normalDuration().
96  */
97  double normalDuration() const override;
98 
99  /**
100  * Returns the recent position of the finite medium.
101  * @see FiniteMedium::position().
102  */
103  double position() const override;
104 
105  /**
106  * Sets the recent position of the finite medium.
107  * @see FiniteMedium::setPosition().
108  */
109  bool setPosition(const double position) override;
110 
111  /**
112  * Returns the speed of the finite medium.
113  * @see FiniteMedium::speed().
114  */
115  float speed() const override;
116 
117  /**
118  * Sets the speed of the finite medium.
119  * @see FiniteMedium::setSpeed().
120  */
121  bool setSpeed(const float speed) override;
122 
123  /**
124  * Returns the volume of the sound in db.
125  * @see SoundMedium::soundVolume().
126  */
127  float soundVolume() const override;
128 
129  /**
130  * Returns whether the sound medium is in a mute state.
131  * @see SoundMedium::soundMute().
132  */
133  bool soundMute() const override;
134 
135  /**
136  * Sets the volume of the sound in db.
137  * @see SoundMedium::setSoundVolume().
138  */
139  bool setSoundVolume(const float volume) override;
140 
141  /**
142  * Sets or un-sets the sound medium to a mute state.
143  * @see SoundMedium::setSoundMute().
144  */
145  bool setSoundMute(const bool mute) override;
146 
147  /**
148  * Returns whether the sound of this movie is enabled or disabled.
149  * @see Movie::useSound().
150  */
151  bool useSound() const override;
152 
153  /**
154  * Enables or disables the audio in this movie (has no effect if the movie does not have audio).
155  * @see Movie::setUseSound()
156  */
157  bool setUseSound(const bool state) override;
158 
159  /**
160  * Returns the start timestamp.
161  * @see FiniteMedium::startTimestamp().
162  */
163  Timestamp startTimestamp() const override;
164 
165  /**
166  * Returns the pause timestamp.
167  * @see FiniteMedium::pauseTimestamp().
168  */
169  Timestamp pauseTimestamp() const override;
170 
171  /**
172  * Returns the stop timestamp.
173  * @see FiniteMedium::stopTimestamp().
174  */
175  Timestamp stopTimestamp() const override;
176 
177  protected:
178 
179  /**
180  * Creates a new movie object.
181  * @param url The URL of the movie, must be valid
182  */
183  explicit AMovie(const std::string& url);
184 
185  /**
186  * Destroys a movie object.
187  */
188  ~AMovie() override;
189 
190  /**
191  * Releases the movie and all associated resources.
192  */
193  void release();
194 
195  /**
196  * The thread run function.
197  */
198  void threadRun() override;
199 
200  /**
201  * Creates and initializes the media extractor for the medium's url.
202  * @return True, if succeeded
203  */
205 
206  /**
207  * Creates and initializes the media codecs for the medium.
208  * @return True, if at least the video codec could be initialized successfully
209  */
211 
212  /**
213  * Creates the media codec for a specific track.
214  * @param trackFormat The format of the track for which the code will be created, must be valid
215  * @param mime The mime of the format, must be valid
216  * @param trackIndex The index of the track for which the codec will be created, must be valid
217  * @return The media codec if succeeded; nullptr otherwise
218  */
219  AMediaCodec* createCodecForTrack(AMediaFormat* trackFormat, const char* mime, const size_t trackIndex);
220 
221  /**
222  * Processes the input buffer of a media codec.
223  * @param mediaCodec The media codec which input buffer will be processed, must be valid
224  * @param normalRelativePresentationTime The resulting relative presentation time in seconds for normal speed
225  * @return False, if no more buffers need to be processed
226  */
227  bool processInputBuffer(AMediaCodec* const mediaCodec, double& normalRelativePresentationTime);
228 
229  /**
230  * Extracts the audio samples from an output buffer of a audio codec.
231  * @param mediaCodec The media codec to which the output buffer belongs, must be valid
232  * @return The resulting extracted frame, invalid if the frame could not be extracted
233  */
234  static bool extractAudioSamplesFromCodecOutputBuffer(AMediaCodec* const mediaCodec);
235 
236  protected:
237 
238  /// Optional file description if used.
240 
241  /// The media extractor to be used t
242  AMediaExtractor* mediaExtractor_ = nullptr;
243 
244  /// The media codecs for the video track.
245  AMediaCodec* videoMediaCodec_ = nullptr;
246 
247  /// The media codecs for the audio track.
248  AMediaCodec* audioMediaCodec_ = nullptr;
249 
250  /// The timestamp when the medium started.
251  Timestamp startTimestamp_ = Timestamp(false);
252 
253  /// The timestamp when the medium paused.
254  Timestamp pauseTimestamp_ = Timestamp(false);
255 
256  /// The timestamp when the medium stopped.
257  Timestamp stopTimestamp_ = Timestamp(false);
258 
259  /// The playback speed of this movie.
260  std::atomic<float> speed_ = 1.0f;
261 
262  /// The duration of the movie with normal speed.
263  double normalDuration_ = -1.0;
264 
265  /// True, to enable audio in the movie; False, to disable audio, currently AMovie does not support audio.
266  bool useSound_ = false;
267 };
268 
269 }
270 
271 }
272 
273 }
274 
275 #endif // __ANDROID_API__
276 
277 #endif // META_OCEAN_MEDIA_ANDROID_A_MOVIE_H
This class implements the android library.
Definition: ALibrary.h:36
This class implements the base class for all Medium objects in the Android library.
Definition: AMedium.h:35
This class implements a movie recorder class for Android.
Definition: AMovie.h:45
bool setUseSound(const bool state) override
Enables or disables the audio in this movie (has no effect if the movie does not have audio).
bool processInputBuffer(AMediaCodec *const mediaCodec, double &normalRelativePresentationTime)
Processes the input buffer of a media codec.
Timestamp startTimestamp() const override
Returns the start timestamp.
AMovie(const std::string &url)
Creates a new movie object.
bool setSpeed(const float speed) override
Sets the speed of the finite medium.
AMediaCodec * createCodecForTrack(AMediaFormat *trackFormat, const char *mime, const size_t trackIndex)
Creates the media codec for a specific track.
ScopedFileDescriptor fileDescriptor_
Optional file description if used.
Definition: AMovie.h:239
static bool extractAudioSamplesFromCodecOutputBuffer(AMediaCodec *const mediaCodec)
Extracts the audio samples from an output buffer of a audio codec.
bool stop() override
Stops the medium.
Timestamp stopTimestamp() const override
Returns the stop timestamp.
bool soundMute() const override
Returns whether the sound medium is in a mute state.
bool setPosition(const double position) override
Sets the recent position of the finite medium.
void release()
Releases the movie and all associated resources.
bool initializeMediaExtractor()
Creates and initializes the media extractor for the medium's url.
MediumRef clone() const override
Clones this movie medium and returns a new independent instance of this medium.
double duration() const override
Returns the duration of the finite medium.
void threadRun() override
The thread run function.
bool setSoundVolume(const float volume) override
Sets the volume of the sound in db.
~AMovie() override
Destroys a movie object.
bool useSound() const override
Returns whether the sound of this movie is enabled or disabled.
bool start() override
Starts the medium.
Timestamp pauseTimestamp() const override
Returns the pause timestamp.
float speed() const override
Returns the speed of the finite medium.
bool isStarted() const override
Returns whether the medium is started currently.
double normalDuration() const override
Returns the duration without speed consideration.
float soundVolume() const override
Returns the volume of the sound in db.
bool setSoundMute(const bool mute) override
Sets or un-sets the sound medium to a mute state.
double position() const override
Returns the recent position of the finite medium.
bool pause() override
Pauses the medium.
bool initializeMediaCodecs()
Creates and initializes the media codecs for the medium.
This class is the base class for all movies.
Definition: Movie.h:40
This template class implements a object reference with an internal reference counter.
Definition: base/ObjectRef.h:58
This class implements a thread.
Definition: Thread.h:115
This class implements a timestamp.
Definition: Timestamp.h:36
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15