• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "modules/audio_device/audio_device_generic.h"
20 #include "rtc_base/synchronization/mutex.h"
21 #include "rtc_base/system/file_wrapper.h"
22 #include "rtc_base/time_utils.h"
23 
24 namespace rtc {
25 class PlatformThread;
26 }  // namespace rtc
27 
28 namespace webrtc {
29 
30 // This is a fake audio device which plays audio from a file as its microphone
31 // and plays out into a file.
32 class FileAudioDevice : public AudioDeviceGeneric {
33  public:
34   // Constructs a file audio device with |id|. It will read audio from
35   // |inputFilename| and record output audio to |outputFilename|.
36   //
37   // The input file should be a readable 48k stereo raw file, and the output
38   // file should point to a writable location. The output format will also be
39   // 48k stereo raw audio.
40   FileAudioDevice(const char* inputFilename, const char* outputFilename);
41   virtual ~FileAudioDevice();
42 
43   // Retrieve the currently utilized audio layer
44   int32_t ActiveAudioLayer(
45       AudioDeviceModule::AudioLayer& audioLayer) const override;
46 
47   // Main initializaton and termination
48   InitStatus Init() override;
49   int32_t Terminate() override;
50   bool Initialized() const override;
51 
52   // Device enumeration
53   int16_t PlayoutDevices() override;
54   int16_t RecordingDevices() override;
55   int32_t PlayoutDeviceName(uint16_t index,
56                             char name[kAdmMaxDeviceNameSize],
57                             char guid[kAdmMaxGuidSize]) override;
58   int32_t RecordingDeviceName(uint16_t index,
59                               char name[kAdmMaxDeviceNameSize],
60                               char guid[kAdmMaxGuidSize]) override;
61 
62   // Device selection
63   int32_t SetPlayoutDevice(uint16_t index) override;
64   int32_t SetPlayoutDevice(
65       AudioDeviceModule::WindowsDeviceType device) override;
66   int32_t SetRecordingDevice(uint16_t index) override;
67   int32_t SetRecordingDevice(
68       AudioDeviceModule::WindowsDeviceType device) override;
69 
70   // Audio transport initialization
71   int32_t PlayoutIsAvailable(bool& available) override;
72   int32_t InitPlayout() override;
73   bool PlayoutIsInitialized() const override;
74   int32_t RecordingIsAvailable(bool& available) override;
75   int32_t InitRecording() override;
76   bool RecordingIsInitialized() const override;
77 
78   // Audio transport control
79   int32_t StartPlayout() override;
80   int32_t StopPlayout() override;
81   bool Playing() const override;
82   int32_t StartRecording() override;
83   int32_t StopRecording() override;
84   bool Recording() const override;
85 
86   // Audio mixer initialization
87   int32_t InitSpeaker() override;
88   bool SpeakerIsInitialized() const override;
89   int32_t InitMicrophone() override;
90   bool MicrophoneIsInitialized() const override;
91 
92   // Speaker volume controls
93   int32_t SpeakerVolumeIsAvailable(bool& available) override;
94   int32_t SetSpeakerVolume(uint32_t volume) override;
95   int32_t SpeakerVolume(uint32_t& volume) const override;
96   int32_t MaxSpeakerVolume(uint32_t& maxVolume) const override;
97   int32_t MinSpeakerVolume(uint32_t& minVolume) const override;
98 
99   // Microphone volume controls
100   int32_t MicrophoneVolumeIsAvailable(bool& available) override;
101   int32_t SetMicrophoneVolume(uint32_t volume) override;
102   int32_t MicrophoneVolume(uint32_t& volume) const override;
103   int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const override;
104   int32_t MinMicrophoneVolume(uint32_t& minVolume) const override;
105 
106   // Speaker mute control
107   int32_t SpeakerMuteIsAvailable(bool& available) override;
108   int32_t SetSpeakerMute(bool enable) override;
109   int32_t SpeakerMute(bool& enabled) const override;
110 
111   // Microphone mute control
112   int32_t MicrophoneMuteIsAvailable(bool& available) override;
113   int32_t SetMicrophoneMute(bool enable) override;
114   int32_t MicrophoneMute(bool& enabled) const override;
115 
116   // Stereo support
117   int32_t StereoPlayoutIsAvailable(bool& available) override;
118   int32_t SetStereoPlayout(bool enable) override;
119   int32_t StereoPlayout(bool& enabled) const override;
120   int32_t StereoRecordingIsAvailable(bool& available) override;
121   int32_t SetStereoRecording(bool enable) override;
122   int32_t StereoRecording(bool& enabled) const override;
123 
124   // Delay information and control
125   int32_t PlayoutDelay(uint16_t& delayMS) const override;
126 
127   void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) override;
128 
129  private:
130   static void RecThreadFunc(void*);
131   static void PlayThreadFunc(void*);
132   bool RecThreadProcess();
133   bool PlayThreadProcess();
134 
135   int32_t _playout_index;
136   int32_t _record_index;
137   AudioDeviceBuffer* _ptrAudioBuffer;
138   int8_t* _recordingBuffer;  // In bytes.
139   int8_t* _playoutBuffer;    // In bytes.
140   uint32_t _recordingFramesLeft;
141   uint32_t _playoutFramesLeft;
142   Mutex mutex_;
143 
144   size_t _recordingBufferSizeIn10MS;
145   size_t _recordingFramesIn10MS;
146   size_t _playoutFramesIn10MS;
147 
148   // TODO(pbos): Make plain members instead of pointers and stop resetting them.
149   std::unique_ptr<rtc::PlatformThread> _ptrThreadRec;
150   std::unique_ptr<rtc::PlatformThread> _ptrThreadPlay;
151 
152   bool _playing;
153   bool _recording;
154   int64_t _lastCallPlayoutMillis;
155   int64_t _lastCallRecordMillis;
156 
157   FileWrapper _outputFile;
158   FileWrapper _inputFile;
159   std::string _outputFilename;
160   std::string _inputFilename;
161 };
162 
163 }  // namespace webrtc
164 
165 #endif  // AUDIO_DEVICE_FILE_AUDIO_DEVICE_H_
166