Ocean
AMicrophone.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_MICROPHONE_H
9 #define META_OCEAN_MEDIA_ANDROID_A_MICROPHONE_H
10 
13 
14 #include "ocean/media/Microphone.h"
15 
16 #include <SLES/OpenSLES_Android.h>
17 
18 #include <queue>
19 
20 namespace Ocean
21 {
22 
23 namespace Media
24 {
25 
26 namespace Android
27 {
28 
29 /**
30  * This class implements a microphone class for Android.
31  * @ingroup mediaandroid
32  */
33 class OCEAN_MEDIA_A_EXPORT AMicrophone final :
34  virtual public AMedium,
35  virtual public Microphone
36 {
37  friend class ALibrary;
38 
39  protected:
40 
41  /// The number of buffers in the queue.
42  static constexpr SLuint32 numberBuffers_ = 4u;
43 
44  /**
45  * Definition of a vector holding sample elements.
46  */
47  typedef std::vector<int16_t> Buffer;
48 
49  public:
50 
51  /**
52  * Starts the medium.
53  * @see Medium::start().
54  */
55  bool start() override;
56 
57  /**
58  * Pauses the medium.
59  * @see Medium::pause():
60  */
61  bool pause() override;
62 
63  /**
64  * Stops the medium.
65  * @see Medium::stop().
66  */
67  bool stop() override;
68 
69  /**
70  * Returns whether the medium is started currently.
71  * @see Medium::isStarted().
72  */
73  bool isStarted() const override;
74 
75  /**
76  * Returns the start timestamp.
77  * @see FiniteMedium::startTimestamp().
78  */
79  Timestamp startTimestamp() const override;
80 
81  /**
82  * Returns the pause timestamp.
83  * @see FiniteMedium::pauseTimestamp().
84  */
85  Timestamp pauseTimestamp() const override;
86 
87  /**
88  * Returns the stop timestamp.
89  * @see FiniteMedium::stopTimestamp().
90  */
91  Timestamp stopTimestamp() const override;
92 
93  /**
94  * Returns the size of one mono chunk in elements.
95  * @return The size of one mono chunk, in elements
96  */
97  static constexpr size_t monoChunkElements();
98 
99  protected:
100 
101  /**
102  * Creates a new medium by a given url.
103  * @param slEngineInterface The interface of the SL engine, must be valid
104  * @param url Url of the medium
105  */
106  AMicrophone(const SLEngineItf& slEngineInterface, const std::string& url);
107 
108  /**
109  * Destructs the live video object.
110  */
111  ~AMicrophone() override;
112 
113  /**
114  * Initializes the audio and all corresponding resources.
115  * @param slEngineInterface The interface of the SL engine, must be valid
116  * @param microphoneTypes The types of the microphone to create
117  * @param microphoneConfigurations The configurations of the microphone to create
118  * @return True, if succeeded
119  */
120  bool initialize(const SLEngineItf& slEngineInterface, const MicrophoneTypes microphoneTypes, const MicrophoneConfigurations microphoneConfigurations);
121 
122  /**
123  * Releases the audio and all corresponding resources.
124  * @return True, if succeeded
125  */
126  bool release();
127 
128  /**
129  * Event callback function to fill the OpenSL buffer queue.
130  * @param bufferQueue The buffer queue to fill with the next samples, must be valid
131  */
132  void onFillBufferQueueCallback(SLAndroidSimpleBufferQueueItf bufferQueue);
133 
134  /**
135  * Static event callback function to fill the OpenSL buffer queue.
136  * @param bufferQueue The buffer queue to fill with the next samples, must be valid
137  * @param context The context of the event (the ALiveAudio object), must be valid
138  */
139  static void onFillBufferQueueCallback(SLAndroidSimpleBufferQueueItf bufferQueue, void* context);
140 
141  protected:
142 
143  /// Start timestamp.
144  Timestamp startTimestamp_ = Timestamp(false);
145 
146  /// Pause timestamp.
147  Timestamp pauseTimestamp_ = Timestamp(false);
148 
149  /// Stop timestamp.
150  Timestamp stopTimestamp_ = Timestamp(false);
151 
152  /// The SL recorder object.
153  SLObjectItf slRecorder_ = nullptr;
154 
155  /// The SL recorder interface.
156  SLRecordItf slRecordInterface_ = nullptr;
157 
158  /// THe SL buffer queue interface.
159  SLAndroidSimpleBufferQueueItf slBufferQueueInterface_ = nullptr;
160 
161  /// The buffer for the sample queue.
162  std::queue<Buffer> bufferQueue_;
163 
164  /// The SL effect interface for the acoustic echo cancel effect.
165  SLAndroidEffectItf slAcousticEchoCancelEffect_ = nullptr;
166 
167  /// The SL implementation id of the acoustic echo cancel effect.
168  SLInterfaceID slAcousticEchoCancelerEffectImplementationId_ = nullptr;
169 };
170 
172 {
173  constexpr size_t samplesPerSecondMono = 48000; // 48kHz
174 
175  return samplesPerSecondMono / 50; // 20ms
176 }
177 
178 }
179 
180 }
181 
182 }
183 
184 #endif // META_OCEAN_MEDIA_ANDROID_A_MICROPHONE_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 microphone class for Android.
Definition: AMicrophone.h:36
bool start() override
Starts the medium.
bool pause() override
Pauses the medium.
bool stop() override
Stops the medium.
std::vector< int16_t > Buffer
Definition of a vector holding sample elements.
Definition: AMicrophone.h:47
Timestamp pauseTimestamp() const override
Returns the pause timestamp.
void onFillBufferQueueCallback(SLAndroidSimpleBufferQueueItf bufferQueue)
Event callback function to fill the OpenSL buffer queue.
static void onFillBufferQueueCallback(SLAndroidSimpleBufferQueueItf bufferQueue, void *context)
Static event callback function to fill the OpenSL buffer queue.
bool initialize(const SLEngineItf &slEngineInterface, const MicrophoneTypes microphoneTypes, const MicrophoneConfigurations microphoneConfigurations)
Initializes the audio and all corresponding resources.
bool release()
Releases the audio and all corresponding resources.
~AMicrophone() override
Destructs the live video object.
Timestamp stopTimestamp() const override
Returns the stop timestamp.
std::queue< Buffer > bufferQueue_
The buffer for the sample queue.
Definition: AMicrophone.h:162
bool isStarted() const override
Returns whether the medium is started currently.
AMicrophone(const SLEngineItf &slEngineInterface, const std::string &url)
Creates a new medium by a given url.
static constexpr size_t monoChunkElements()
Returns the size of one mono chunk in elements.
Definition: AMicrophone.h:171
Timestamp startTimestamp() const override
Returns the start timestamp.
This class is the base class for all microphones.
Definition: media/Microphone.h:37
MicrophoneTypes
Definition of individual microphone types.
Definition: media/Microphone.h:44
MicrophoneConfigurations
Definition of individual microphone configurations.
Definition: media/Microphone.h:59
This class implements a timestamp.
Definition: Timestamp.h:36
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15