Ocean
AVFMedium.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_MEDIUM_H
9 #define META_OCEAN_MEDIA_AVF_MEDIUM_H
10 
12 
13 #include "ocean/base/Singleton.h"
14 
15 #include "ocean/media/Medium.h"
16 
17 #include <AVFoundation/AVFoundation.h>
18 
19 namespace Ocean
20 {
21 
22 namespace Media
23 {
24 
25 namespace AVFoundation
26 {
27 
28 /**
29  * This is the base class for all AVFoundation mediums.
30  * Beware: When using AV foundation objects within a console application on macOS, the main loop needs to be executed regularly.<br>
31  * The main loop is handling events neccessary in AVFoundation, however a console application commonly is a blocking application and therefore never invokes the main loop.
32  * @see Platform::Apple::System::runMainLoop().
33  * @ingroup mediaavf
34  */
35 class AVFMedium : virtual public Medium
36 {
37  protected:
38 
39  /**
40  * This class implements a manager for audio sessions.
41  */
42  class AudioSessionManager : public Singleton<AudioSessionManager>
43  {
44  friend class Singleton<AudioSessionManager>;
45 
46  public:
47 
48 #ifndef OCEAN_PLATFORM_BUILD_APPLE_MACOS
49 
50  /**
51  * Initializes the audio session.
52  * @param avAudioSessionCategory The preferred category to be used
53  * @param avAudioSessionMode The preferred mode to be used
54  */
55  void initialize(const AVAudioSessionCategory avAudioSessionCategory, const AVAudioSessionMode avAudioSessionMode);
56 
57 #endif // OCEAN_PLATFORM_BUILD_APPLE_MACOS
58 
59  /**
60  * Starts an audio session.
61  * Each successful call of start() needs to be balanced with a call of stop().
62  * @return True, if the session was started successfully; False, if the session could not be started
63  * @see stop().
64  */
65  bool start();
66 
67  /**
68  * Stops an audio session after is has been started.
69  * @see start().
70  */
71  void stop();
72 
73  /**
74  * Requests record permission.
75  * The session must have been started already.
76  */
78 
79  protected:
80 
81  /**
82  * Default constructor.
83  */
84  AudioSessionManager() = default;
85 
86  protected:
87 
88 #ifndef OCEAN_PLATFORM_BUILD_APPLE_MACOS
89 
90  /// The usage counter.
91  unsigned int usageCounter_ = 0u;
92 
93  /// The audio session for iOS platforms.
94  AVAudioSession* avAudioSession_ = nullptr;
95 
96  /// The category which is used.
97  AVAudioSessionCategory avAudioSessionCategory_ = AVAudioSessionCategoryAmbient;
98 
99  /// The mode which is used.
100  AVAudioSessionMode avAudioSessionMode_ = AVAudioSessionModeDefault;
101 
102  /// The manager's lock.
104 
105 #endif // OCEAN_PLATFORM_BUILD_APPLE_MACOS
106  };
107 
108  public:
109 
110  /**
111  * Returns whether the medium is started currently.
112  * @see Medium::isStarted()
113  */
114  bool isStarted() const override;
115 
116  /**
117  * Returns the start timestamp.
118  * @return Start timestamp
119  */
120  Timestamp startTimestamp() const override;
121 
122  /**
123  * Returns the pause timestamp.
124  * @return Pause timestamp
125  */
126  Timestamp pauseTimestamp() const override;
127 
128  /**
129  * Returns the stop timestamp.
130  * @return Stop timestamp
131  */
132  Timestamp stopTimestamp() const override;
133 
134  protected:
135 
136  /**
137  * Creates a new medium by a given url.
138  * @param url Url of the medium
139  */
140  explicit AVFMedium(const std::string& url);
141 
142  /**
143  * Destructs a AVFMedium object.
144  */
145  ~AVFMedium() override;
146 
147  /**
148  * Starts the medium.
149  * The internalStart() function will be called inside.
150  * @see Medium::start()
151  */
152  bool start() override;
153 
154  /**
155  * Pauses the medium.
156  * The internalPause() function will be called inside.
157  * @see Medium::pause()
158  */
159  bool pause() override;
160 
161  /**
162  * Stops the medium.
163  * The internalStop() function will be called inside.
164  * @see Medium::stop()
165  */
166  bool stop() override;
167 
168  /**
169  * Internally starts the medium.
170  * @return True, if succeeded or if the medium is already started
171  */
172  virtual bool internalStart() = 0;
173 
174  /**
175  * Internally pauses the medium.
176  * @return True, if succeeded or if the medium is already paused
177  */
178  virtual bool internalPause() = 0;
179 
180  /**
181  * Internally stops the medium.
182  * @return True, if succeeded or if the medium is already stopped
183  */
184  virtual bool internalStop() = 0;
185 
186  protected:
187 
188  /// Start timestamp
190 
191  /// Pause timestamp
193 
194  /// Stop timestamp
196 };
197 
198 }
199 
200 }
201 
202 }
203 
204 #endif // META_OCEAN_MEDIA_AVF_MEDIUM_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a manager for audio sessions.
Definition: AVFMedium.h:43
void requestRecordPermission()
Requests record permission.
unsigned int usageCounter_
The usage counter.
Definition: AVFMedium.h:91
void stop()
Stops an audio session after is has been started.
AVAudioSession * avAudioSession_
The audio session for iOS platforms.
Definition: AVFMedium.h:94
AVAudioSessionMode avAudioSessionMode_
The mode which is used.
Definition: AVFMedium.h:100
Lock lock_
The manager's lock.
Definition: AVFMedium.h:103
void initialize(const AVAudioSessionCategory avAudioSessionCategory, const AVAudioSessionMode avAudioSessionMode)
Initializes the audio session.
AVAudioSessionCategory avAudioSessionCategory_
The category which is used.
Definition: AVFMedium.h:97
This is the base class for all AVFoundation mediums.
Definition: AVFMedium.h:36
bool stop() override
Stops the medium.
Timestamp startTimestamp() const override
Returns the start timestamp.
~AVFMedium() override
Destructs a AVFMedium object.
bool isStarted() const override
Returns whether the medium is started currently.
AVFMedium(const std::string &url)
Creates a new medium by a given url.
Timestamp startTimestamp_
Start timestamp.
Definition: AVFMedium.h:189
Timestamp stopTimestamp_
Stop timestamp.
Definition: AVFMedium.h:195
Timestamp stopTimestamp() const override
Returns the stop timestamp.
virtual bool internalPause()=0
Internally pauses the medium.
Timestamp pauseTimestamp() const override
Returns the pause timestamp.
Timestamp pauseTimestamp_
Pause timestamp.
Definition: AVFMedium.h:192
bool start() override
Starts the medium.
virtual bool internalStart()=0
Internally starts the medium.
bool pause() override
Pauses the medium.
virtual bool internalStop()=0
Internally stops the medium.
This is the base class for all mediums.
Definition: Medium.h:48
const std::string & url() const
Returns the url of the medium.
Definition: Medium.h:236
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
This class implements a timestamp.
Definition: Timestamp.h:36
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15