Ocean
AVFMovie.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_H
9 #define META_OCEAN_MEDIA_AVF_MOVIE_H
10 
13 
14 #include "ocean/base/Callback.h"
15 #include "ocean/base/Thread.h"
16 
17 #include "ocean/media/Movie.h"
18 
19 namespace Ocean
20 {
21 
22 namespace Media
23 {
24 
25 namespace AVFoundation
26 {
27 
28 /**
29  * This class implements an AVFoundation movie object.
30  * @ingroup mediaavf
31  */
32 class AVFMovie :
33  virtual public AVFFrameMedium,
34  virtual public Movie,
35  protected Thread
36 {
37  friend class AVFLibrary;
38 
39  public:
40 
41  /**
42  * Definition of a callback function for finished playing.
43  */
45 
46  public:
47 
48  /**
49  * Clones this movie medium and returns a new independent instance of this medium.
50  * @see Medium::clone()
51  */
52  MediumRef clone() const override;
53 
54  /**
55  * Returns the duration of the movie medium.
56  * @see FiniteMedium::duration()
57  */
58  double duration() const override;
59 
60  /**
61  * Returns the duration of the movie medium without speed consideration.
62  * @see FiniteMedium::normalDuration()
63  */
64  double normalDuration() const override;
65 
66  /**
67  * Returns the recent position of the movie medium.
68  * @see FiniteMedium::position()
69  */
70  double position() const override;
71 
72  /**
73  * Sets the recent position of the movie medium.
74  * @see FiniteMedium::setPosition()
75  */
76  bool setPosition(const double position) override;
77 
78  /**
79  * Returns the speed of the movie medium.
80  * @see FiniteMedium::speed()
81  */
82  float speed() const override;
83 
84  /**
85  * Sets the speed of the movie medium.
86  * @see FiniteMedium::setSpeed()
87  */
88  bool setSpeed(const float speed) override;
89 
90  /**
91  * Returns the volume of the sound in db.
92  * @see SoundMedium::soundVolume()
93  */
94  float soundVolume() const override;
95 
96  /**
97  * Returns whether the movie medium is in a mute state.
98  * @see SoundMedium::soundMute()
99  */
100  bool soundMute() const override;
101 
102  /**
103  * Sets the volume of the sound in db.
104  * @see SoundMedium::setSoundVolume()
105  */
106  bool setSoundVolume(const float volume) override;
107 
108  /**
109  * Sets or unsets the movie medium to a mute state.
110  * @see SoundMedium::setSoundMute()
111  */
112  bool setSoundMute(const bool mute) override;
113 
114  /**
115  * Enables or disables the audio in this movie (has no effect if the movie does not have audio).
116  * @see Movie::setUseSound()
117  */
118  bool setUseSound(const bool state) override;
119 
120  protected:
121 
122  /**
123  * Creates a new movie by a given url.
124  * @param url Url of the movie
125  */
126  explicit AVFMovie(const std::string& url);
127 
128  /**
129  * Destructs a AVFMovie object.
130  */
131  ~AVFMovie() override;
132 
133  /**
134  * Internally starts the medium.
135  * @see AVFMedium::internalStart()
136  */
137  bool internalStart() override;
138 
139  /**
140  * Internally pauses the medium.
141  * @see AVFMedium::internalPause()
142  */
143  bool internalPause() override;
144 
145  /**
146  * Internally stops the medium.
147  * @see AVFMedium::internalStop()
148  */
149  bool internalStop() override;
150 
151  /**
152  * Event function for
153  */
155 
156  /**
157  * Creates a new asset reader by a given start time.
158  * Ensure that the current asset reader and track output are unset before.
159  * @param startTime The start time of the asset reader.
160  * @return True, if succeeded
161  */
162  bool createNewAssetReader(const double startTime);
163 
164  /**
165  * Creates a new player.
166  * @return True, if succeeded
167  */
169 
170  private:
171 
172  /**
173  * Thread run function.
174  */
175  void threadRun() override;
176 
177  protected:
178 
179  /// True to indicate that the player should be started.
180  bool playerShouldStart_ = false;
181 
182  /// The speed of the movie medium
183  float speed_ = 1.0f;
184 
185  /// The asset that holds the movie file
186  AVURLAsset* asset_ = nullptr;
187 
188  /// The asset reader for non playback time respect
189  AVAssetReader* assetReader_ = nullptr;
190 
191  /// The asset reader output
192  AVAssetReaderTrackOutput* assetReaderTrackOutput_ = nullptr;
193 
194  /// The player for playback time respect
195  AVPlayer* player_ = nullptr;
196 
197  /// The player item that is played
198  AVPlayerItem* playerItem_ = nullptr;
199 
200  /// The player output
201  AVPlayerItemVideoOutput* playerItemVideoOutput_ = nullptr;
202 
203  /// The player observer
204  NSObject* observer_ = nullptr;
205 
206 };
207 
208 }
209 
210 }
211 
212 }
213 
214 #endif // META_OCEAN_MEDIA_AVF_MOVIE_H
This is the base class for all AVFoundation frame mediums.
Definition: AVFFrameMedium.h:34
This class implements the AVFoundation library.
Definition: AVFLibrary.h:32
This class implements an AVFoundation movie object.
Definition: AVFMovie.h:36
bool createNewPlayer()
Creates a new player.
AVAssetReaderTrackOutput * assetReaderTrackOutput_
The asset reader output.
Definition: AVFMovie.h:192
Callback< void > FinishedPlayingCallback
Definition of a callback function for finished playing.
Definition: AVFMovie.h:44
double normalDuration() const override
Returns the duration of the movie medium without speed consideration.
AVPlayerItemVideoOutput * playerItemVideoOutput_
The player output.
Definition: AVFMovie.h:201
AVPlayerItem * playerItem_
The player item that is played.
Definition: AVFMovie.h:198
bool internalPause() override
Internally pauses the medium.
float speed() const override
Returns the speed of the movie medium.
AVURLAsset * asset_
The asset that holds the movie file.
Definition: AVFMovie.h:186
AVFMovie(const std::string &url)
Creates a new movie by a given url.
bool internalStart() override
Internally starts the medium.
bool createNewAssetReader(const double startTime)
Creates a new asset reader by a given start time.
double position() const override
Returns the recent position of the movie medium.
float speed_
The speed of the movie medium.
Definition: AVFMovie.h:183
~AVFMovie() override
Destructs a AVFMovie object.
AVAssetReader * assetReader_
The asset reader for non playback time respect.
Definition: AVFMovie.h:189
void onFinishedPlaying()
Event function for.
bool playerShouldStart_
True to indicate that the player should be started.
Definition: AVFMovie.h:180
bool setSpeed(const float speed) override
Sets the speed of the movie medium.
double duration() const override
Returns the duration of the movie medium.
void threadRun() override
Thread run function.
AVPlayer * player_
The player for playback time respect.
Definition: AVFMovie.h:195
bool internalStop() override
Internally stops the medium.
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 soundMute() const override
Returns whether the movie medium is in a mute state.
NSObject * observer_
The player observer.
Definition: AVFMovie.h:204
bool setSoundMute(const bool mute) override
Sets or unsets the movie medium to a mute state.
float soundVolume() const override
Returns the volume of the sound in db.
bool setPosition(const double position) override
Sets the recent position of the movie medium.
MediumRef clone() const override
Clones this movie medium and returns a new independent instance of this medium.
bool setSoundVolume(const float volume) override
Sets the volume of the sound in db.
const std::string & url() const
Returns the url of the medium.
Definition: Medium.h:236
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
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15