file_audio_device.h 5.6 KB

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