Ocean
media/Microphone.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_MICROPHONE_H
9 #define META_OCEAN_MEDIA_MICROPHONE_H
10 
11 #include "ocean/media/Media.h"
12 #include "ocean/media/LiveMedium.h"
13 
15 
16 namespace Ocean
17 {
18 
19 namespace Media
20 {
21 
22 // Forward declaration.
23 class Microphone;
24 
25 /**
26  * Definition of a smart medium reference holding a microphone object.
27  * @see SmartMediumRef, Microphone.
28  * @ingroup media
29  */
31 
32 /**
33  * This class is the base class for all microphones.
34  * @ingroup media
35  */
36 class OCEAN_MEDIA_EXPORT Microphone : public virtual LiveMedium
37 {
38  public:
39 
40  /**
41  * Definition of individual microphone types.
42  */
43  enum MicrophoneTypes : uint32_t
44  {
45  /// An unknown microphone type.
46  MT_UNKNOWN = 0u,
47  /// A built-in microphone (e.g., in a mobile phone).
48  MT_BUILTIN = 1u << 0u,
49  /// An external microphone (e.g., as part of headphones).
50  MT_EXTERNAL = 1u << 1u,
51  /// Any microphone either built-in or external.
52  MT_ANY = MT_BUILTIN | MT_EXTERNAL
53  };
54 
55  /**
56  * Definition of individual microphone configurations.
57  */
58  enum MicrophoneConfigurations : uint32_t
59  {
60  /// A default microphone configuration.
61  MC_DEFAULT = 0u,
62  /// The microphone is configured for voice communication and can include features like an Acoustic Echo Canceler, may have some latency.
63  MC_VOICE_COMMUNICATION = 1u << 0u,
64  /// The microphone is configured for performance with low latency.
65  MC_PERFORMANCE = 1u << 1u,
66  /// The microphone is gained.
67  MC_GAINED = 1u << 2u
68  };
69 
70  /**
71  * Definition of individual samples types.
72  */
73  enum SamplesType : uint32_t
74  {
75  /// An invalid samples type
76  ST_INVALID = 0u,
77  /// Samples with 16bit signed integer precision as mono channel and 48kHz
78  ST_INTEGER_16_MONO_48
79  };
80 
81  /**
82  * Definition of a callback function for microphone samples.
83  * @param samplesType The type of the samples
84  * @param data The samples data, must be valid
85  * @param size The size of the data, in bytes, with range [1, infinity)
86  */
87  using SamplesCallbackFunction = std::function<void(const SamplesType samplesType, const void* data, const size_t size)>;
88 
89  /**
90  * Definition of a subscription object for microphone samples.
91  */
93 
94  protected:
95 
96  /**
97  * Definition of a subscription handler for voip samples events.
98  */
100 
101  public:
102 
103  /**
104  * Returns the type of this microphone.
105  * @return The microphone's type
106  */
108 
109  /**
110  * Returns the configuration of this microphone.
111  * @return The microphone's configuration
112  */
114 
115  /**
116  * Adds a new callback function for samples events.
117  * @param samplesCallbackFunction The callback function to add, must be valid
118  * @return The subscription object, the callback function will exist as long as the subscription object exists
119  */
120  [[nodiscard]] virtual SamplesScopedSubscription addSamplesCallback(SamplesCallbackFunction samplesCallbackFunction);
121 
122  protected:
123 
124  /**
125  * Creates a new microphone by a given url.
126  * @param url Url of the microphone
127  */
128  explicit Microphone(const std::string& url);
129 
130  /**
131  * Sends samples to all subscribed callback functions.
132  * @param samplesType The type of the samples
133  * @param data The samples data, must be valid
134  * @param size The size of the data, in bytes, with range [1, infinity)
135  */
136  inline void sendSamples(const SamplesType samplesType, const void* data, const size_t size);
137 
138  protected:
139 
140  /// The microphone's types.
141  MicrophoneTypes microphoneTypes_ = MT_UNKNOWN;
142 
143  /// The microphone's configurations.
144  MicrophoneConfigurations microphoneConfigurations_ = MC_DEFAULT;
145 
146  /// The handler for samples callback functions.
148 };
149 
150 inline void Microphone::sendSamples(const SamplesType samplesType, const void* data, const size_t size)
151 {
152  samplesCallbackHandler_.callCallbacks(samplesType, data, size);
153 }
154 
155 }
156 
157 }
158 
159 #endif // META_OCEAN_MEDIA_MICROPHONE_H
This class it the base class for all live mediums.
Definition: LiveMedium.h:38
This class is the base class for all microphones.
Definition: media/Microphone.h:37
std::function< void(const SamplesType samplesType, const void *data, const size_t size)> SamplesCallbackFunction
Definition of a callback function for microphone samples.
Definition: media/Microphone.h:87
SamplesCallbackHandler samplesCallbackHandler_
The handler for samples callback functions.
Definition: media/Microphone.h:147
SamplesType
Definition of individual samples types.
Definition: media/Microphone.h:74
virtual MicrophoneConfigurations microphoneConfigurations() const
Returns the configuration of this microphone.
MicrophoneTypes
Definition of individual microphone types.
Definition: media/Microphone.h:44
virtual MicrophoneTypes microphoneTypes() const
Returns the type of this microphone.
MicrophoneConfigurations
Definition of individual microphone configurations.
Definition: media/Microphone.h:59
virtual SamplesScopedSubscription addSamplesCallback(SamplesCallbackFunction samplesCallbackFunction)
Adds a new callback function for samples events.
void sendSamples(const SamplesType samplesType, const void *data, const size_t size)
Sends samples to all subscribed callback functions.
Definition: media/Microphone.h:150
Microphone(const std::string &url)
Creates a new microphone by a given url.
This class implements a smart medium reference.
Definition: MediumRef.h:33
TCallbackFunction::result_type callCallbacks(TArgs &&... args)
Calls all callback functions of this handler.
Definition: ScopedSubscription.h:351
ScopedSubscriptionT< unsigned int, ScopedSubscriptionHandler > ScopedSubscriptionType
Definition of a scoped subscription object.
Definition: ScopedSubscription.h:144
This class implements a subscription object which can be used unique subscriptions to e....
Definition: ScopedSubscription.h:28
SmartMediumRef< Microphone > MicrophoneRef
Definition of a smart medium reference holding a microphone object.
Definition: media/Microphone.h:23
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15