xref: /aosp_15_r20/external/webrtc/modules/audio_device/dummy/file_audio_device.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef AUDIO_DEVICE_FILE_AUDIO_DEVICE_H_
12 #define AUDIO_DEVICE_FILE_AUDIO_DEVICE_H_
13 
14 #include <stdio.h>
15 
16 #include <memory>
17 #include <string>
18 
19 #include "absl/strings/string_view.h"
20 #include "modules/audio_device/audio_device_generic.h"
21 #include "rtc_base/platform_thread.h"
22 #include "rtc_base/synchronization/mutex.h"
23 #include "rtc_base/system/file_wrapper.h"
24 #include "rtc_base/time_utils.h"
25 
26 namespace webrtc {
27 
28 // This is a fake audio device which plays audio from a file as its microphone
29 // and plays out into a file.
30 class FileAudioDevice : public AudioDeviceGeneric {
31  public:
32   // Constructs a file audio device with `id`. It will read audio from
33   // `inputFilename` and record output audio to `outputFilename`.
34   //
35   // The input file should be a readable 48k stereo raw file, and the output
36   // file should point to a writable location. The output format will also be
37   // 48k stereo raw audio.
38   FileAudioDevice(absl::string_view inputFilename,
39                   absl::string_view outputFilename);
40   virtual ~FileAudioDevice();
41 
42   // Retrieve the currently utilized audio layer
43   int32_t ActiveAudioLayer(
44       AudioDeviceModule::AudioLayer& audioLayer) const override;
45 
46   // Main initializaton and termination
47   InitStatus Init() override;
48   int32_t Terminate() override;
49   bool Initialized() const override;
50 
51   // Device enumeration
52   int16_t PlayoutDevices() override;
53   int16_t RecordingDevices() override;
54   int32_t PlayoutDeviceName(uint16_t index,
55                             char name[kAdmMaxDeviceNameSize],
56                             char guid[kAdmMaxGuidSize]) override;
57   int32_t RecordingDeviceName(uint16_t index,
58                               char name[kAdmMaxDeviceNameSize],
59                               char guid[kAdmMaxGuidSize]) override;
60 
61   // Device selection
62   int32_t SetPlayoutDevice(uint16_t index) override;
63   int32_t SetPlayoutDevice(
64       AudioDeviceModule::WindowsDeviceType device) override;
65   int32_t SetRecordingDevice(uint16_t index) override;
66   int32_t SetRecordingDevice(
67       AudioDeviceModule::WindowsDeviceType device) override;
68 
69   // Audio transport initialization
70   int32_t PlayoutIsAvailable(bool& available) override;
71   int32_t InitPlayout() override;
72   bool PlayoutIsInitialized() const override;
73   int32_t RecordingIsAvailable(bool& available) override;
74   int32_t InitRecording() override;
75   bool RecordingIsInitialized() const override;
76 
77   // Audio transport control
78   int32_t StartPlayout() override;
79   int32_t StopPlayout() override;
80   bool Playing() const override;
81   int32_t StartRecording() override;
82   int32_t StopRecording() override;
83   bool Recording() const override;
84 
85   // Audio mixer initialization
86   int32_t InitSpeaker() override;
87   bool SpeakerIsInitialized() const override;
88   int32_t InitMicrophone() override;
89   bool MicrophoneIsInitialized() const override;
90 
91   // Speaker volume controls
92   int32_t SpeakerVolumeIsAvailable(bool& available) override;
93   int32_t SetSpeakerVolume(uint32_t volume) override;
94   int32_t SpeakerVolume(uint32_t& volume) const override;
95   int32_t MaxSpeakerVolume(uint32_t& maxVolume) const override;
96   int32_t MinSpeakerVolume(uint32_t& minVolume) const override;
97 
98   // Microphone volume controls
99   int32_t MicrophoneVolumeIsAvailable(bool& available) override;
100   int32_t SetMicrophoneVolume(uint32_t volume) override;
101   int32_t MicrophoneVolume(uint32_t& volume) const override;
102   int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const override;
103   int32_t MinMicrophoneVolume(uint32_t& minVolume) const override;
104 
105   // Speaker mute control
106   int32_t SpeakerMuteIsAvailable(bool& available) override;
107   int32_t SetSpeakerMute(bool enable) override;
108   int32_t SpeakerMute(bool& enabled) const override;
109 
110   // Microphone mute control
111   int32_t MicrophoneMuteIsAvailable(bool& available) override;
112   int32_t SetMicrophoneMute(bool enable) override;
113   int32_t MicrophoneMute(bool& enabled) const override;
114 
115   // Stereo support
116   int32_t StereoPlayoutIsAvailable(bool& available) override;
117   int32_t SetStereoPlayout(bool enable) override;
118   int32_t StereoPlayout(bool& enabled) const override;
119   int32_t StereoRecordingIsAvailable(bool& available) override;
120   int32_t SetStereoRecording(bool enable) override;
121   int32_t StereoRecording(bool& enabled) const override;
122 
123   // Delay information and control
124   int32_t PlayoutDelay(uint16_t& delayMS) const override;
125 
126   void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) override;
127 
128  private:
129   static void RecThreadFunc(void*);
130   static void PlayThreadFunc(void*);
131   bool RecThreadProcess();
132   bool PlayThreadProcess();
133 
134   int32_t _playout_index;
135   int32_t _record_index;
136   AudioDeviceBuffer* _ptrAudioBuffer;
137   int8_t* _recordingBuffer;  // In bytes.
138   int8_t* _playoutBuffer;    // In bytes.
139   uint32_t _recordingFramesLeft;
140   uint32_t _playoutFramesLeft;
141   Mutex mutex_;
142 
143   size_t _recordingBufferSizeIn10MS;
144   size_t _recordingFramesIn10MS;
145   size_t _playoutFramesIn10MS;
146 
147   rtc::PlatformThread _ptrThreadRec;
148   rtc::PlatformThread _ptrThreadPlay;
149 
150   bool _playing;
151   bool _recording;
152   int64_t _lastCallPlayoutMillis;
153   int64_t _lastCallRecordMillis;
154 
155   FileWrapper _outputFile;
156   FileWrapper _inputFile;
157   std::string _outputFilename;
158   std::string _inputFilename;
159 };
160 
161 }  // namespace webrtc
162 
163 #endif  // AUDIO_DEVICE_FILE_AUDIO_DEVICE_H_
164