1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <modules/audio_device/include/audio_device.h>
20 
21 #include <atomic>
22 
23 #include "host/frontend/webrtc/libcommon/audio_source.h"
24 
25 namespace cuttlefish {
26 namespace webrtc_streaming {
27 
28 class CfAudioDeviceModule : public webrtc::AudioDeviceModule,
29                             public AudioSource {
30  public:
31   CfAudioDeviceModule();
32   ~CfAudioDeviceModule() override = default;
33 
34   // Returns number of frames if there is data available, 0 if the stream is not
35   // playing (no clients or the streams are muted), -1 on error.
36   int GetMoreAudioData(void* data, int bytes_per_samples, int samples_per_channel,
37                        int num_channels, int sample_rate, bool& muted) override;
38 
39   // Retrieve the currently utilized audio layer
40   int32_t ActiveAudioLayer(AudioLayer* audioLayer) const override;
41 
42   // Full-duplex transportation of PCM audio
43   int32_t RegisterAudioCallback(webrtc::AudioTransport* audioCallback) override;
44 
45   // Main initialization and termination
46   int32_t Init() override;
47   int32_t Terminate() override;
48   bool Initialized() const override;
49 
50   // Device enumeration
51   int16_t PlayoutDevices() override;
52   int16_t RecordingDevices() override;
53   int32_t PlayoutDeviceName(uint16_t index,
54                             char name[webrtc::kAdmMaxDeviceNameSize],
55                             char guid[webrtc::kAdmMaxGuidSize]) override;
56   int32_t RecordingDeviceName(uint16_t index,
57                               char name[webrtc::kAdmMaxDeviceNameSize],
58                               char guid[webrtc::kAdmMaxGuidSize]) override;
59 
60   // Device selection
61   int32_t SetPlayoutDevice(uint16_t index) override;
62   int32_t SetPlayoutDevice(WindowsDeviceType device) override;
63   int32_t SetRecordingDevice(uint16_t index) override;
64   int32_t SetRecordingDevice(WindowsDeviceType device) override;
65 
66   // Audio transport initialization
67   int32_t PlayoutIsAvailable(bool* available) override;
68   int32_t InitPlayout() override;
69   bool PlayoutIsInitialized() const override;
70   int32_t RecordingIsAvailable(bool* available) override;
71   int32_t InitRecording() override;
72   bool RecordingIsInitialized() const override;
73 
74   // Audio transport control
75   int32_t StartPlayout() override;
76   int32_t StopPlayout() override;
77   bool Playing() const override;
78   int32_t StartRecording() override;
79   int32_t StopRecording() override;
80   bool Recording() const override;
81 
82   // Audio mixer initialization
83   int32_t InitSpeaker() override;
84   bool SpeakerIsInitialized() const override;
85   int32_t InitMicrophone() override;
86   bool MicrophoneIsInitialized() const override;
87 
88   // Speaker volume controls
89   int32_t SpeakerVolumeIsAvailable(bool* available) override;
90   int32_t SetSpeakerVolume(uint32_t volume) override;
91   int32_t SpeakerVolume(uint32_t* volume) const override;
92   int32_t MaxSpeakerVolume(uint32_t* maxVolume) const override;
93   int32_t MinSpeakerVolume(uint32_t* minVolume) const override;
94 
95   // Microphone volume controls
96   int32_t MicrophoneVolumeIsAvailable(bool* available) override;
97   int32_t SetMicrophoneVolume(uint32_t volume) override;
98   int32_t MicrophoneVolume(uint32_t* volume) const override;
99   int32_t MaxMicrophoneVolume(uint32_t* maxVolume) const override;
100   int32_t MinMicrophoneVolume(uint32_t* minVolume) const override;
101 
102   // Speaker mute control
103   int32_t SpeakerMuteIsAvailable(bool* available) override;
104   int32_t SetSpeakerMute(bool enable) override;
105   int32_t SpeakerMute(bool* enabled) const override;
106 
107   // Microphone mute control
108   int32_t MicrophoneMuteIsAvailable(bool* available) override;
109   int32_t SetMicrophoneMute(bool enable) override;
110   int32_t MicrophoneMute(bool* enabled) const override;
111 
112   // Stereo support
113   int32_t StereoPlayoutIsAvailable(bool* available) const override;
114   int32_t SetStereoPlayout(bool enable) override;
115   int32_t StereoPlayout(bool* enabled) const override;
116   int32_t StereoRecordingIsAvailable(bool* available) const override;
117   int32_t SetStereoRecording(bool enable) override;
118   int32_t StereoRecording(bool* enabled) const override;
119 
120   // Playout delay
121   int32_t PlayoutDelay(uint16_t* delayMS) const override;
122 
123   // Only supported on Android.
124   bool BuiltInAECIsAvailable() const override;
125   bool BuiltInAGCIsAvailable() const override;
126   bool BuiltInNSIsAvailable() const override;
127 
128   // Enables the built-in audio effects. Only supported on Android.
129   int32_t EnableBuiltInAEC(bool enable) override;
130   int32_t EnableBuiltInAGC(bool enable) override;
131   int32_t EnableBuiltInNS(bool enable) override;
132 
133   // Play underrun count. Only supported on Android (guest).
134   int32_t GetPlayoutUnderrunCount() const override;
135 
136  private:
137   webrtc::AudioTransport* audio_callback_ = nullptr;
138   bool stereo_playout_enabled_ = true;
139   bool stereo_recording_enabled_ = true;
140   std::atomic<bool> playing_ = false;
141   std::atomic<bool> recording_ = false;
142 };
143 }  // namespace webrtc_streaming
144 }  // namespace cuttlefish
145