fake_audio_capture_module.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright 2012 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. // This class implements an AudioCaptureModule that can be used to detect if
  11. // audio is being received properly if it is fed by another AudioCaptureModule
  12. // in some arbitrary audio pipeline where they are connected. It does not play
  13. // out or record any audio so it does not need access to any hardware and can
  14. // therefore be used in the gtest testing framework.
  15. // Note P postfix of a function indicates that it should only be called by the
  16. // processing thread.
  17. #ifndef PC_TEST_FAKE_AUDIO_CAPTURE_MODULE_H_
  18. #define PC_TEST_FAKE_AUDIO_CAPTURE_MODULE_H_
  19. #include <memory>
  20. #include "api/scoped_refptr.h"
  21. #include "modules/audio_device/include/audio_device.h"
  22. #include "rtc_base/message_handler.h"
  23. #include "rtc_base/synchronization/mutex.h"
  24. #include "rtc_base/synchronization/sequence_checker.h"
  25. namespace rtc {
  26. class Thread;
  27. } // namespace rtc
  28. class FakeAudioCaptureModule : public webrtc::AudioDeviceModule,
  29. public rtc::MessageHandlerAutoCleanup {
  30. public:
  31. typedef uint16_t Sample;
  32. // The value for the following constants have been derived by running VoE
  33. // using a real ADM. The constants correspond to 10ms of mono audio at 44kHz.
  34. static const size_t kNumberSamples = 440;
  35. static const size_t kNumberBytesPerSample = sizeof(Sample);
  36. // Creates a FakeAudioCaptureModule or returns NULL on failure.
  37. static rtc::scoped_refptr<FakeAudioCaptureModule> Create();
  38. // Returns the number of frames that have been successfully pulled by the
  39. // instance. Note that correctly detecting success can only be done if the
  40. // pulled frame was generated/pushed from a FakeAudioCaptureModule.
  41. int frames_received() const RTC_LOCKS_EXCLUDED(mutex_);
  42. int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override;
  43. // Note: Calling this method from a callback may result in deadlock.
  44. int32_t RegisterAudioCallback(webrtc::AudioTransport* audio_callback) override
  45. RTC_LOCKS_EXCLUDED(mutex_);
  46. int32_t Init() override;
  47. int32_t Terminate() override;
  48. bool Initialized() const override;
  49. int16_t PlayoutDevices() override;
  50. int16_t RecordingDevices() override;
  51. int32_t PlayoutDeviceName(uint16_t index,
  52. char name[webrtc::kAdmMaxDeviceNameSize],
  53. char guid[webrtc::kAdmMaxGuidSize]) override;
  54. int32_t RecordingDeviceName(uint16_t index,
  55. char name[webrtc::kAdmMaxDeviceNameSize],
  56. char guid[webrtc::kAdmMaxGuidSize]) override;
  57. int32_t SetPlayoutDevice(uint16_t index) override;
  58. int32_t SetPlayoutDevice(WindowsDeviceType device) override;
  59. int32_t SetRecordingDevice(uint16_t index) override;
  60. int32_t SetRecordingDevice(WindowsDeviceType device) override;
  61. int32_t PlayoutIsAvailable(bool* available) override;
  62. int32_t InitPlayout() override;
  63. bool PlayoutIsInitialized() const override;
  64. int32_t RecordingIsAvailable(bool* available) override;
  65. int32_t InitRecording() override;
  66. bool RecordingIsInitialized() const override;
  67. int32_t StartPlayout() RTC_LOCKS_EXCLUDED(mutex_) override;
  68. int32_t StopPlayout() RTC_LOCKS_EXCLUDED(mutex_) override;
  69. bool Playing() const RTC_LOCKS_EXCLUDED(mutex_) override;
  70. int32_t StartRecording() RTC_LOCKS_EXCLUDED(mutex_) override;
  71. int32_t StopRecording() RTC_LOCKS_EXCLUDED(mutex_) override;
  72. bool Recording() const RTC_LOCKS_EXCLUDED(mutex_) override;
  73. int32_t InitSpeaker() override;
  74. bool SpeakerIsInitialized() const override;
  75. int32_t InitMicrophone() override;
  76. bool MicrophoneIsInitialized() const override;
  77. int32_t SpeakerVolumeIsAvailable(bool* available) override;
  78. int32_t SetSpeakerVolume(uint32_t volume) override;
  79. int32_t SpeakerVolume(uint32_t* volume) const override;
  80. int32_t MaxSpeakerVolume(uint32_t* max_volume) const override;
  81. int32_t MinSpeakerVolume(uint32_t* min_volume) const override;
  82. int32_t MicrophoneVolumeIsAvailable(bool* available) override;
  83. int32_t SetMicrophoneVolume(uint32_t volume)
  84. RTC_LOCKS_EXCLUDED(mutex_) override;
  85. int32_t MicrophoneVolume(uint32_t* volume) const
  86. RTC_LOCKS_EXCLUDED(mutex_) override;
  87. int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override;
  88. int32_t MinMicrophoneVolume(uint32_t* min_volume) const override;
  89. int32_t SpeakerMuteIsAvailable(bool* available) override;
  90. int32_t SetSpeakerMute(bool enable) override;
  91. int32_t SpeakerMute(bool* enabled) const override;
  92. int32_t MicrophoneMuteIsAvailable(bool* available) override;
  93. int32_t SetMicrophoneMute(bool enable) override;
  94. int32_t MicrophoneMute(bool* enabled) const override;
  95. int32_t StereoPlayoutIsAvailable(bool* available) const override;
  96. int32_t SetStereoPlayout(bool enable) override;
  97. int32_t StereoPlayout(bool* enabled) const override;
  98. int32_t StereoRecordingIsAvailable(bool* available) const override;
  99. int32_t SetStereoRecording(bool enable) override;
  100. int32_t StereoRecording(bool* enabled) const override;
  101. int32_t PlayoutDelay(uint16_t* delay_ms) const override;
  102. bool BuiltInAECIsAvailable() const override { return false; }
  103. int32_t EnableBuiltInAEC(bool enable) override { return -1; }
  104. bool BuiltInAGCIsAvailable() const override { return false; }
  105. int32_t EnableBuiltInAGC(bool enable) override { return -1; }
  106. bool BuiltInNSIsAvailable() const override { return false; }
  107. int32_t EnableBuiltInNS(bool enable) override { return -1; }
  108. int32_t GetPlayoutUnderrunCount() const override { return -1; }
  109. #if defined(WEBRTC_IOS)
  110. int GetPlayoutAudioParameters(
  111. webrtc::AudioParameters* params) const override {
  112. return -1;
  113. }
  114. int GetRecordAudioParameters(webrtc::AudioParameters* params) const override {
  115. return -1;
  116. }
  117. #endif // WEBRTC_IOS
  118. // End of functions inherited from webrtc::AudioDeviceModule.
  119. // The following function is inherited from rtc::MessageHandler.
  120. void OnMessage(rtc::Message* msg) override;
  121. protected:
  122. // The constructor is protected because the class needs to be created as a
  123. // reference counted object (for memory managment reasons). It could be
  124. // exposed in which case the burden of proper instantiation would be put on
  125. // the creator of a FakeAudioCaptureModule instance. To create an instance of
  126. // this class use the Create(..) API.
  127. FakeAudioCaptureModule();
  128. // The destructor is protected because it is reference counted and should not
  129. // be deleted directly.
  130. virtual ~FakeAudioCaptureModule();
  131. private:
  132. // Initializes the state of the FakeAudioCaptureModule. This API is called on
  133. // creation by the Create() API.
  134. bool Initialize();
  135. // SetBuffer() sets all samples in send_buffer_ to |value|.
  136. void SetSendBuffer(int value);
  137. // Resets rec_buffer_. I.e., sets all rec_buffer_ samples to 0.
  138. void ResetRecBuffer();
  139. // Returns true if rec_buffer_ contains one or more sample greater than or
  140. // equal to |value|.
  141. bool CheckRecBuffer(int value);
  142. // Returns true/false depending on if recording or playback has been
  143. // enabled/started.
  144. bool ShouldStartProcessing() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  145. // Starts or stops the pushing and pulling of audio frames.
  146. void UpdateProcessing(bool start) RTC_LOCKS_EXCLUDED(mutex_);
  147. // Starts the periodic calling of ProcessFrame() in a thread safe way.
  148. void StartProcessP();
  149. // Periodcally called function that ensures that frames are pulled and pushed
  150. // periodically if enabled/started.
  151. void ProcessFrameP() RTC_LOCKS_EXCLUDED(mutex_);
  152. // Pulls frames from the registered webrtc::AudioTransport.
  153. void ReceiveFrameP() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  154. // Pushes frames to the registered webrtc::AudioTransport.
  155. void SendFrameP() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  156. // Callback for playout and recording.
  157. webrtc::AudioTransport* audio_callback_ RTC_GUARDED_BY(mutex_);
  158. bool recording_ RTC_GUARDED_BY(
  159. mutex_); // True when audio is being pushed from the instance.
  160. bool playing_ RTC_GUARDED_BY(
  161. mutex_); // True when audio is being pulled by the instance.
  162. bool play_is_initialized_; // True when the instance is ready to pull audio.
  163. bool rec_is_initialized_; // True when the instance is ready to push audio.
  164. // Input to and output from RecordedDataIsAvailable(..) makes it possible to
  165. // modify the current mic level. The implementation does not care about the
  166. // mic level so it just feeds back what it receives.
  167. uint32_t current_mic_level_ RTC_GUARDED_BY(mutex_);
  168. // next_frame_time_ is updated in a non-drifting manner to indicate the next
  169. // wall clock time the next frame should be generated and received. started_
  170. // ensures that next_frame_time_ can be initialized properly on first call.
  171. bool started_ RTC_GUARDED_BY(mutex_);
  172. int64_t next_frame_time_ RTC_GUARDED_BY(process_thread_checker_);
  173. std::unique_ptr<rtc::Thread> process_thread_;
  174. // Buffer for storing samples received from the webrtc::AudioTransport.
  175. char rec_buffer_[kNumberSamples * kNumberBytesPerSample];
  176. // Buffer for samples to send to the webrtc::AudioTransport.
  177. char send_buffer_[kNumberSamples * kNumberBytesPerSample];
  178. // Counter of frames received that have samples of high enough amplitude to
  179. // indicate that the frames are not faked somewhere in the audio pipeline
  180. // (e.g. by a jitter buffer).
  181. int frames_received_;
  182. // Protects variables that are accessed from process_thread_ and
  183. // the main thread.
  184. mutable webrtc::Mutex mutex_;
  185. webrtc::SequenceChecker process_thread_checker_;
  186. };
  187. #endif // PC_TEST_FAKE_AUDIO_CAPTURE_MODULE_H_