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