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 WEBRTC_AUDIO_DEVICE_FILE_AUDIO_DEVICE_H
12#define WEBRTC_AUDIO_DEVICE_FILE_AUDIO_DEVICE_H
13
14#include <stdio.h>
15
16#include <string>
17
18#include "webrtc/modules/audio_device/audio_device_generic.h"
19#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
20#include "webrtc/system_wrappers/include/file_wrapper.h"
21#include "webrtc/system_wrappers/include/clock.h"
22
23namespace rtc {
24class PlatformThread;
25}  // namespace rtc
26
27namespace webrtc {
28class EventWrapper;
29
30// This is a fake audio device which plays audio from a file as its microphone
31// and plays out into a file.
32class 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 int32_t id,
41                  const char* inputFilename,
42                  const char* outputFilename);
43  virtual ~FileAudioDevice();
44
45  // Retrieve the currently utilized audio layer
46  int32_t ActiveAudioLayer(
47      AudioDeviceModule::AudioLayer& audioLayer) const override;
48
49  // Main initializaton and termination
50  int32_t Init() override;
51  int32_t Terminate() override;
52  bool Initialized() const override;
53
54  // Device enumeration
55  int16_t PlayoutDevices() override;
56  int16_t RecordingDevices() override;
57  int32_t PlayoutDeviceName(uint16_t index,
58                            char name[kAdmMaxDeviceNameSize],
59                            char guid[kAdmMaxGuidSize]) override;
60  int32_t RecordingDeviceName(uint16_t index,
61                              char name[kAdmMaxDeviceNameSize],
62                              char guid[kAdmMaxGuidSize]) override;
63
64  // Device selection
65  int32_t SetPlayoutDevice(uint16_t index) override;
66  int32_t SetPlayoutDevice(
67      AudioDeviceModule::WindowsDeviceType device) override;
68  int32_t SetRecordingDevice(uint16_t index) override;
69  int32_t SetRecordingDevice(
70      AudioDeviceModule::WindowsDeviceType device) override;
71
72  // Audio transport initialization
73  int32_t PlayoutIsAvailable(bool& available) override;
74  int32_t InitPlayout() override;
75  bool PlayoutIsInitialized() const override;
76  int32_t RecordingIsAvailable(bool& available) override;
77  int32_t InitRecording() override;
78  bool RecordingIsInitialized() const override;
79
80  // Audio transport control
81  int32_t StartPlayout() override;
82  int32_t StopPlayout() override;
83  bool Playing() const override;
84  int32_t StartRecording() override;
85  int32_t StopRecording() override;
86  bool Recording() const override;
87
88  // Microphone Automatic Gain Control (AGC)
89  int32_t SetAGC(bool enable) override;
90  bool AGC() const override;
91
92  // Volume control based on the Windows Wave API (Windows only)
93  int32_t SetWaveOutVolume(uint16_t volumeLeft, uint16_t volumeRight) override;
94  int32_t WaveOutVolume(uint16_t& volumeLeft,
95                        uint16_t& volumeRight) const override;
96
97  // Audio mixer initialization
98  int32_t InitSpeaker() override;
99  bool SpeakerIsInitialized() const override;
100  int32_t InitMicrophone() override;
101  bool MicrophoneIsInitialized() const override;
102
103  // Speaker volume controls
104  int32_t SpeakerVolumeIsAvailable(bool& available) override;
105  int32_t SetSpeakerVolume(uint32_t volume) override;
106  int32_t SpeakerVolume(uint32_t& volume) const override;
107  int32_t MaxSpeakerVolume(uint32_t& maxVolume) const override;
108  int32_t MinSpeakerVolume(uint32_t& minVolume) const override;
109  int32_t SpeakerVolumeStepSize(uint16_t& stepSize) const override;
110
111  // Microphone volume controls
112  int32_t MicrophoneVolumeIsAvailable(bool& available) override;
113  int32_t SetMicrophoneVolume(uint32_t volume) override;
114  int32_t MicrophoneVolume(uint32_t& volume) const override;
115  int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const override;
116  int32_t MinMicrophoneVolume(uint32_t& minVolume) const override;
117  int32_t MicrophoneVolumeStepSize(uint16_t& stepSize) const override;
118
119  // Speaker mute control
120  int32_t SpeakerMuteIsAvailable(bool& available) override;
121  int32_t SetSpeakerMute(bool enable) override;
122  int32_t SpeakerMute(bool& enabled) const override;
123
124  // Microphone mute control
125  int32_t MicrophoneMuteIsAvailable(bool& available) override;
126  int32_t SetMicrophoneMute(bool enable) override;
127  int32_t MicrophoneMute(bool& enabled) const override;
128
129  // Microphone boost control
130  int32_t MicrophoneBoostIsAvailable(bool& available) override;
131  int32_t SetMicrophoneBoost(bool enable) override;
132  int32_t MicrophoneBoost(bool& enabled) const override;
133
134  // Stereo support
135  int32_t StereoPlayoutIsAvailable(bool& available) override;
136  int32_t SetStereoPlayout(bool enable) override;
137  int32_t StereoPlayout(bool& enabled) const override;
138  int32_t StereoRecordingIsAvailable(bool& available) override;
139  int32_t SetStereoRecording(bool enable) override;
140  int32_t StereoRecording(bool& enabled) const override;
141
142  // Delay information and control
143  int32_t SetPlayoutBuffer(const AudioDeviceModule::BufferType type,
144                           uint16_t sizeMS) override;
145  int32_t PlayoutBuffer(AudioDeviceModule::BufferType& type,
146                        uint16_t& sizeMS) const override;
147  int32_t PlayoutDelay(uint16_t& delayMS) const override;
148  int32_t RecordingDelay(uint16_t& delayMS) const override;
149
150  // CPU load
151  int32_t CPULoad(uint16_t& load) const override;
152
153  bool PlayoutWarning() const override;
154  bool PlayoutError() const override;
155  bool RecordingWarning() const override;
156  bool RecordingError() const override;
157  void ClearPlayoutWarning() override;
158  void ClearPlayoutError() override;
159  void ClearRecordingWarning() override;
160  void ClearRecordingError() override;
161
162  void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) override;
163
164 private:
165  static bool RecThreadFunc(void*);
166  static bool PlayThreadFunc(void*);
167  bool RecThreadProcess();
168  bool PlayThreadProcess();
169
170  int32_t _playout_index;
171  int32_t _record_index;
172  AudioDeviceModule::BufferType _playBufType;
173  AudioDeviceBuffer* _ptrAudioBuffer;
174  int8_t* _recordingBuffer;  // In bytes.
175  int8_t* _playoutBuffer;  // In bytes.
176  uint32_t _recordingFramesLeft;
177  uint32_t _playoutFramesLeft;
178  CriticalSectionWrapper& _critSect;
179
180  size_t _recordingBufferSizeIn10MS;
181  size_t _recordingFramesIn10MS;
182  size_t _playoutFramesIn10MS;
183
184  // TODO(pbos): Make plain members instead of pointers and stop resetting them.
185  rtc::scoped_ptr<rtc::PlatformThread> _ptrThreadRec;
186  rtc::scoped_ptr<rtc::PlatformThread> _ptrThreadPlay;
187
188  bool _playing;
189  bool _recording;
190  uint64_t _lastCallPlayoutMillis;
191  uint64_t _lastCallRecordMillis;
192
193  FileWrapper& _outputFile;
194  FileWrapper& _inputFile;
195  std::string _outputFilename;
196  std::string _inputFilename;
197
198  Clock* _clock;
199};
200
201}  // namespace webrtc
202
203#endif  // WEBRTC_AUDIO_DEVICE_FILE_AUDIO_DEVICE_H
204