OpenTera WebRTC API (C++) 1.2.5
OpenteraAudioDeviceModule.h
1 #ifndef OPENTERA_WEBRTC_NATIVE_CLIENT_BLACK_HOLE_AUDIO_CAPTURE_MODULE_H
2 #define OPENTERA_WEBRTC_NATIVE_CLIENT_BLACK_HOLE_AUDIO_CAPTURE_MODULE_H
3 
4 #include <OpenteraWebrtcNativeClient/Utils/ClassMacro.h>
5 #include <OpenteraWebrtcNativeClient/Sinks/AudioSink.h>
6 
7 #include <modules/audio_device/include/audio_device.h>
8 
9 #include <atomic>
10 #include <memory>
11 #include <mutex>
12 #include <thread>
13 
14 namespace opentera
15 {
19  class OpenteraAudioDeviceModule : public webrtc::AudioDeviceModule
20  {
21  AudioSinkCallback m_onMixedAudioFrameReceived;
22 
23  bool m_isPlayoutInitialized;
24  bool m_isRecordingInitialized;
25  bool m_isSpeakerInitialized;
26  bool m_isMicrophoneInitialized;
27 
28  bool m_isPlaying;
29  bool m_isRecording;
30 
31  std::atomic_bool m_playoutThreadStopped;
32  std::unique_ptr<std::thread> m_thread;
33  webrtc::AudioTransport* m_audioTransport;
34  std::mutex m_audioTransportCaptureMutex;
35 
36  std::mutex m_setCallbackMutex;
37 
38  public:
40  ~OpenteraAudioDeviceModule() override;
41 
42  DECLARE_NOT_COPYABLE(OpenteraAudioDeviceModule);
43  DECLARE_NOT_MOVABLE(OpenteraAudioDeviceModule);
44 
45  void setOnMixedAudioFrameReceived(const AudioSinkCallback& onMixedAudioFrameReceived);
46  void sendFrame(
47  const void* audioData,
48  int bitsPerSample,
49  int sampleRate,
50  size_t numberOfChannels,
51  size_t numberOfFrames,
52  uint32_t audioDelayMs,
53  bool isTyping);
54 
55  // Retrieve the currently utilized audio layer
56  int32_t ActiveAudioLayer(AudioLayer* audioLayer) const override;
57 
58  // Full-duplex transportation of PCM audio
59  int32_t RegisterAudioCallback(webrtc::AudioTransport* audioTransport) override;
60 
61  // Main initialization and termination
62  int32_t Init() override;
63  int32_t Terminate() override;
64  [[nodiscard]] bool Initialized() const override;
65 
66  // Device enumeration
67  int16_t PlayoutDevices() override;
68  int16_t RecordingDevices() override;
69  int32_t PlayoutDeviceName(
70  uint16_t index,
71  char name[webrtc::kAdmMaxDeviceNameSize],
72  char guid[webrtc::kAdmMaxGuidSize]) override;
73  int32_t RecordingDeviceName(
74  uint16_t index,
75  char name[webrtc::kAdmMaxDeviceNameSize],
76  char guid[webrtc::kAdmMaxGuidSize]) override;
77 
78  // Device selection
79  int32_t SetPlayoutDevice(uint16_t index) override;
80  int32_t SetPlayoutDevice(WindowsDeviceType device) override;
81  int32_t SetRecordingDevice(uint16_t index) override;
82  int32_t SetRecordingDevice(WindowsDeviceType device) override;
83 
84  // Audio transport initialization
85  int32_t PlayoutIsAvailable(bool* available) override;
86  int32_t InitPlayout() override;
87  [[nodiscard]] bool PlayoutIsInitialized() const override;
88  int32_t RecordingIsAvailable(bool* available) override;
89  int32_t InitRecording() override;
90  [[nodiscard]] bool RecordingIsInitialized() const override;
91 
92  // Audio transport control
93  int32_t StartPlayout() override;
94  int32_t StopPlayout() override;
95  [[nodiscard]] bool Playing() const override;
96  int32_t StartRecording() override;
97  int32_t StopRecording() override;
98  [[nodiscard]] bool Recording() const override;
99 
100  // Audio mixer initialization
101  int32_t InitSpeaker() override;
102  [[nodiscard]] bool SpeakerIsInitialized() const override;
103  int32_t InitMicrophone() override;
104  [[nodiscard]] bool MicrophoneIsInitialized() const override;
105 
106  // Speaker volume controls
107  int32_t SpeakerVolumeIsAvailable(bool* available) override;
108  int32_t SetSpeakerVolume(uint32_t volume) override;
109  int32_t SpeakerVolume(uint32_t* volume) const override;
110  int32_t MaxSpeakerVolume(uint32_t* maxVolume) const override;
111  int32_t MinSpeakerVolume(uint32_t* minVolume) const override;
112 
113  // Microphone volume controls
114  int32_t MicrophoneVolumeIsAvailable(bool* available) override;
115  int32_t SetMicrophoneVolume(uint32_t volume) override;
116  int32_t MicrophoneVolume(uint32_t* volume) const override;
117  int32_t MaxMicrophoneVolume(uint32_t* maxVolume) const override;
118  int32_t MinMicrophoneVolume(uint32_t* minVolume) const override;
119 
120  // Speaker mute control
121  int32_t SpeakerMuteIsAvailable(bool* available) override;
122  int32_t SetSpeakerMute(bool enable) override;
123  int32_t SpeakerMute(bool* enabled) const override;
124 
125  // Microphone mute control
126  int32_t MicrophoneMuteIsAvailable(bool* available) override;
127  int32_t SetMicrophoneMute(bool enable) override;
128  int32_t MicrophoneMute(bool* enabled) const override;
129 
130  // Stereo support
131  int32_t StereoPlayoutIsAvailable(bool* available) const override;
132  int32_t SetStereoPlayout(bool enable) override;
133  int32_t StereoPlayout(bool* enabled) const override;
134  int32_t StereoRecordingIsAvailable(bool* available) const override;
135  int32_t SetStereoRecording(bool enable) override;
136  int32_t StereoRecording(bool* enabled) const override;
137 
138  // Playout delay
139  int32_t PlayoutDelay(uint16_t* delayMS) const override;
140 
141  // Only supported on Android.
142  [[nodiscard]] bool BuiltInAECIsAvailable() const override;
143  [[nodiscard]] bool BuiltInAGCIsAvailable() const override;
144  [[nodiscard]] bool BuiltInNSIsAvailable() const override;
145 
146  // Enables the built-in audio effects. Only supported on Android.
147  int32_t EnableBuiltInAEC(bool enable) override;
148  int32_t EnableBuiltInAGC(bool enable) override;
149  int32_t EnableBuiltInNS(bool enable) override;
150 
151  private:
152  void stopPlayoutThreadIfStarted();
153  void startPlayoutThreadIfStoppedAndTransportValid();
154 
155  void run();
156  };
157 }
158 
159 #endif
This class is used to fake a AudioDeviceModule so we can use a custom AudioSource.
Definition: OpenteraAudioDeviceModule.h:20