Ocean
AVFLiveAudio.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_LIVE_AUDIO_H
9 #define META_OCEAN_MEDIA_AVF_LIVE_AUDIO_H
10 
13 
14 #include "ocean/media/LiveAudio.h"
15 
16 
17 namespace Ocean
18 {
19 
20 namespace Media
21 {
22 
23 namespace AVFoundation
24 {
25 
26 /**
27  * This class implements an AVFoundation live audio object.
28  * @ingroup mediaavf
29  */
30 class AVFLiveAudio :
31  virtual public AVFMedium,
32  virtual public LiveAudio
33 {
34  friend class AVFLibrary;
35 
36  public:
37 
38  /**
39  * Adds new samples in case this audio object receives the audio data from a buffer/stream.
40  * @param sampleType The type of the samples, must be valid
41  * @param data The sample data, must be valid
42  * @param size The size of the sample data, in bytes, with range [1, infinity)
43  * @return True, if succeeded
44  */
45  bool addSamples(const SampleType sampleType, const void* data, const size_t size) override;
46 
47  /**
48  * Returns whether a new sample needs to be added.
49  * @see LiveAudio::needNewSamples().
50  */
51  bool needNewSamples() const override;
52 
53  /**
54  * Returns the volume of the sound in db.
55  * @see SoundMedium::soundVolume().
56  */
57  float soundVolume() const override;
58 
59  /**
60  * Returns whether the sound medium is in a mute state.
61  * @see SoundMedium::soundMute().
62  */
63  bool soundMute() const override;
64 
65  /**
66  * Sets the volume of the sound in db.
67  * @see SoundMedium::setSoundVolume().
68  */
69  bool setSoundVolume(const float volume) override;
70 
71  /**
72  * Sets or un-sets the sound medium to a mute state.
73  * @see SoundMedium::setSoundMute().
74  */
75  bool setSoundMute(const bool mute) override;
76 
77  protected:
78 
79  /**
80  * Creates a new medium by a given url.
81  * @param url Url of the medium
82  */
83  explicit AVFLiveAudio(const std::string& url);
84 
85  /**
86  * Destructs the media object.
87  */
88  ~AVFLiveAudio() override;
89 
90  /**
91  * Internally starts the medium.
92  * @see AVFMedium::internalStart()
93  */
94  bool internalStart() override;
95 
96  /**
97  * Internally pauses the medium.
98  * @see AVFMedium::internalPause()
99  */
100  bool internalPause() override;
101 
102  /**
103  * Internally stops the medium.
104  * @see AVFMedium::internalStop()
105  */
106  bool internalStop() override;
107 
108  /**
109  * Extracts and sends mono samles form a PCM buffer.
110  * @param avAudioPCMBuffer The PCM buffer from which the samples will be extracted, must be valid
111  * @return True, if succeeded
112  */
113  bool sendSamplesMono(AVAudioPCMBuffer* avAudioPCMBuffer);
114 
115  protected:
116 
117  /// The audio engine.
118  AVAudioEngine* avAudioEngine_ = nullptr;
119 
120  /// The audio player node.
121  AVAudioPlayerNode* avAudioPlayerNode_ = nullptr;
122 
123  /// The audio mixer node e.g., allowing to control the volume.
124  AVAudioMixerNode* avAudioMixerNode_ = nullptr;
125 
126  /// The audio format of the audio player node.
127  AVAudioFormat* avAudioFormat_ = nullptr;
128 
129  /// Optional convert from external audio format to internal audio format.
130  AVAudioConverter* avAudioConverter_ = nullptr;
131 
132  /// Optional audio format for int16 samples.
133  AVAudioFormat* avAudioFormatInternalInt16_ = nullptr;
134 
135  /// Optional audio buffer for int16 samples.
136  AVAudioPCMBuffer* avAudioPCMBufferInternalInt16_ = nullptr;
137 
138  /// The previous volume before the medium was muted; -1 if the medium is not muted.
139  float previousVolume_ = -1.0f;
140 
141  /// True, if the audio session was started.
142  bool audioSessionStarted_ = false;
143 
144  /// True, if the audio object needs a new samples.
145  std::atomic_bool needNewSamples_ = false;
146 };
147 
148 }
149 
150 }
151 
152 }
153 
154 #endif // META_OCEAN_MEDIA_AVF_LIVE_AUDIO_H
This class implements the AVFoundation library.
Definition: AVFLibrary.h:32
This class implements an AVFoundation live audio object.
Definition: AVFLiveAudio.h:33
AVAudioPCMBuffer * avAudioPCMBufferInternalInt16_
Optional audio buffer for int16 samples.
Definition: AVFLiveAudio.h:136
bool needNewSamples() const override
Returns whether a new sample needs to be added.
bool addSamples(const SampleType sampleType, const void *data, const size_t size) override
Adds new samples in case this audio object receives the audio data from a buffer/stream.
bool audioSessionStarted_
True, if the audio session was started.
Definition: AVFLiveAudio.h:142
~AVFLiveAudio() override
Destructs the media object.
AVFLiveAudio(const std::string &url)
Creates a new medium by a given url.
AVAudioPlayerNode * avAudioPlayerNode_
The audio player node.
Definition: AVFLiveAudio.h:121
AVAudioEngine * avAudioEngine_
The audio engine.
Definition: AVFLiveAudio.h:118
float soundVolume() const override
Returns the volume of the sound in db.
bool soundMute() const override
Returns whether the sound medium is in a mute state.
AVAudioConverter * avAudioConverter_
Optional convert from external audio format to internal audio format.
Definition: AVFLiveAudio.h:130
float previousVolume_
The previous volume before the medium was muted; -1 if the medium is not muted.
Definition: AVFLiveAudio.h:139
bool setSoundMute(const bool mute) override
Sets or un-sets the sound medium to a mute state.
bool sendSamplesMono(AVAudioPCMBuffer *avAudioPCMBuffer)
Extracts and sends mono samles form a PCM buffer.
std::atomic_bool needNewSamples_
True, if the audio object needs a new samples.
Definition: AVFLiveAudio.h:145
bool setSoundVolume(const float volume) override
Sets the volume of the sound in db.
AVAudioFormat * avAudioFormatInternalInt16_
Optional audio format for int16 samples.
Definition: AVFLiveAudio.h:133
AVAudioMixerNode * avAudioMixerNode_
The audio mixer node e.g., allowing to control the volume.
Definition: AVFLiveAudio.h:124
bool internalPause() override
Internally pauses the medium.
bool internalStop() override
Internally stops the medium.
AVAudioFormat * avAudioFormat_
The audio format of the audio player node.
Definition: AVFLiveAudio.h:127
bool internalStart() override
Internally starts the medium.
This is the base class for all AVFoundation mediums.
Definition: AVFMedium.h:36
This class is the base class for all live audios.
Definition: LiveAudio.h:39
SampleType
Definition of individual sample types.
Definition: LiveAudio.h:46
const std::string & url() const
Returns the url of the medium.
Definition: Medium.h:236
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15