Home | History | Annotate | Download | only in test
      1 /*
      2  * libjingle
      3  * Copyright 2012, Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 // This class implements an AudioCaptureModule that can be used to detect if
     29 // audio is being received properly if it is fed by another AudioCaptureModule
     30 // in some arbitrary audio pipeline where they are connected. It does not play
     31 // out or record any audio so it does not need access to any hardware and can
     32 // therefore be used in the gtest testing framework.
     33 
     34 // Note P postfix of a function indicates that it should only be called by the
     35 // processing thread.
     36 
     37 #ifndef TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_
     38 #define TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_
     39 
     40 #include "talk/base/basictypes.h"
     41 #include "talk/base/criticalsection.h"
     42 #include "talk/base/messagehandler.h"
     43 #include "talk/base/scoped_ref_ptr.h"
     44 #include "webrtc/common_types.h"
     45 #include "webrtc/modules/audio_device/include/audio_device.h"
     46 
     47 namespace talk_base {
     48 
     49 class Thread;
     50 
     51 }  // namespace talk_base
     52 
     53 class FakeAudioCaptureModule
     54     : public webrtc::AudioDeviceModule,
     55       public talk_base::MessageHandler {
     56  public:
     57   typedef uint16 Sample;
     58 
     59   // The value for the following constants have been derived by running VoE
     60   // using a real ADM. The constants correspond to 10ms of mono audio at 44kHz.
     61   enum{kNumberSamples = 440};
     62   enum{kNumberBytesPerSample = sizeof(Sample)};
     63 
     64   // Creates a FakeAudioCaptureModule or returns NULL on failure.
     65   // |process_thread| is used to push and pull audio frames to and from the
     66   // returned instance. Note: ownership of |process_thread| is not handed over.
     67   static talk_base::scoped_refptr<FakeAudioCaptureModule> Create(
     68       talk_base::Thread* process_thread);
     69 
     70   // Returns the number of frames that have been successfully pulled by the
     71   // instance. Note that correctly detecting success can only be done if the
     72   // pulled frame was generated/pushed from a FakeAudioCaptureModule.
     73   int frames_received() const;
     74 
     75   // Following functions are inherited from webrtc::AudioDeviceModule.
     76   // Only functions called by PeerConnection are implemented, the rest do
     77   // nothing and return success. If a function is not expected to be called by
     78   // PeerConnection an assertion is triggered if it is in fact called.
     79   virtual int32_t Version(char* version,
     80                           uint32_t& remaining_buffer_in_bytes,
     81                           uint32_t& position) const;
     82   virtual int32_t TimeUntilNextProcess();
     83   virtual int32_t Process();
     84   virtual int32_t ChangeUniqueId(const int32_t id);
     85 
     86   virtual int32_t ActiveAudioLayer(AudioLayer* audio_layer) const;
     87 
     88   virtual ErrorCode LastError() const;
     89   virtual int32_t RegisterEventObserver(
     90       webrtc::AudioDeviceObserver* event_callback);
     91 
     92   // Note: Calling this method from a callback may result in deadlock.
     93   virtual int32_t RegisterAudioCallback(webrtc::AudioTransport* audio_callback);
     94 
     95   virtual int32_t Init();
     96   virtual int32_t Terminate();
     97   virtual bool Initialized() const;
     98 
     99   virtual int16_t PlayoutDevices();
    100   virtual int16_t RecordingDevices();
    101   virtual int32_t PlayoutDeviceName(uint16_t index,
    102                                     char name[webrtc::kAdmMaxDeviceNameSize],
    103                                     char guid[webrtc::kAdmMaxGuidSize]);
    104   virtual int32_t RecordingDeviceName(uint16_t index,
    105                                       char name[webrtc::kAdmMaxDeviceNameSize],
    106                                       char guid[webrtc::kAdmMaxGuidSize]);
    107 
    108   virtual int32_t SetPlayoutDevice(uint16_t index);
    109   virtual int32_t SetPlayoutDevice(WindowsDeviceType device);
    110   virtual int32_t SetRecordingDevice(uint16_t index);
    111   virtual int32_t SetRecordingDevice(WindowsDeviceType device);
    112 
    113   virtual int32_t PlayoutIsAvailable(bool* available);
    114   virtual int32_t InitPlayout();
    115   virtual bool PlayoutIsInitialized() const;
    116   virtual int32_t RecordingIsAvailable(bool* available);
    117   virtual int32_t InitRecording();
    118   virtual bool RecordingIsInitialized() const;
    119 
    120   virtual int32_t StartPlayout();
    121   virtual int32_t StopPlayout();
    122   virtual bool Playing() const;
    123   virtual int32_t StartRecording();
    124   virtual int32_t StopRecording();
    125   virtual bool Recording() const;
    126 
    127   virtual int32_t SetAGC(bool enable);
    128   virtual bool AGC() const;
    129 
    130   virtual int32_t SetWaveOutVolume(uint16_t volume_left,
    131                                    uint16_t volume_right);
    132   virtual int32_t WaveOutVolume(uint16_t* volume_left,
    133                                 uint16_t* volume_right) const;
    134 
    135   virtual int32_t SpeakerIsAvailable(bool* available);
    136   virtual int32_t InitSpeaker();
    137   virtual bool SpeakerIsInitialized() const;
    138   virtual int32_t MicrophoneIsAvailable(bool* available);
    139   virtual int32_t InitMicrophone();
    140   virtual bool MicrophoneIsInitialized() const;
    141 
    142   virtual int32_t SpeakerVolumeIsAvailable(bool* available);
    143   virtual int32_t SetSpeakerVolume(uint32_t volume);
    144   virtual int32_t SpeakerVolume(uint32_t* volume) const;
    145   virtual int32_t MaxSpeakerVolume(uint32_t* max_volume) const;
    146   virtual int32_t MinSpeakerVolume(uint32_t* min_volume) const;
    147   virtual int32_t SpeakerVolumeStepSize(uint16_t* step_size) const;
    148 
    149   virtual int32_t MicrophoneVolumeIsAvailable(bool* available);
    150   virtual int32_t SetMicrophoneVolume(uint32_t volume);
    151   virtual int32_t MicrophoneVolume(uint32_t* volume) const;
    152   virtual int32_t MaxMicrophoneVolume(uint32_t* max_volume) const;
    153 
    154   virtual int32_t MinMicrophoneVolume(uint32_t* min_volume) const;
    155   virtual int32_t MicrophoneVolumeStepSize(uint16_t* step_size) const;
    156 
    157   virtual int32_t SpeakerMuteIsAvailable(bool* available);
    158   virtual int32_t SetSpeakerMute(bool enable);
    159   virtual int32_t SpeakerMute(bool* enabled) const;
    160 
    161   virtual int32_t MicrophoneMuteIsAvailable(bool* available);
    162   virtual int32_t SetMicrophoneMute(bool enable);
    163   virtual int32_t MicrophoneMute(bool* enabled) const;
    164 
    165   virtual int32_t MicrophoneBoostIsAvailable(bool* available);
    166   virtual int32_t SetMicrophoneBoost(bool enable);
    167   virtual int32_t MicrophoneBoost(bool* enabled) const;
    168 
    169   virtual int32_t StereoPlayoutIsAvailable(bool* available) const;
    170   virtual int32_t SetStereoPlayout(bool enable);
    171   virtual int32_t StereoPlayout(bool* enabled) const;
    172   virtual int32_t StereoRecordingIsAvailable(bool* available) const;
    173   virtual int32_t SetStereoRecording(bool enable);
    174   virtual int32_t StereoRecording(bool* enabled) const;
    175   virtual int32_t SetRecordingChannel(const ChannelType channel);
    176   virtual int32_t RecordingChannel(ChannelType* channel) const;
    177 
    178   virtual int32_t SetPlayoutBuffer(const BufferType type,
    179                                    uint16_t size_ms = 0);
    180   virtual int32_t PlayoutBuffer(BufferType* type,
    181                                 uint16_t* size_ms) const;
    182   virtual int32_t PlayoutDelay(uint16_t* delay_ms) const;
    183   virtual int32_t RecordingDelay(uint16_t* delay_ms) const;
    184 
    185   virtual int32_t CPULoad(uint16_t* load) const;
    186 
    187   virtual int32_t StartRawOutputFileRecording(
    188       const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]);
    189   virtual int32_t StopRawOutputFileRecording();
    190   virtual int32_t StartRawInputFileRecording(
    191       const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]);
    192   virtual int32_t StopRawInputFileRecording();
    193 
    194   virtual int32_t SetRecordingSampleRate(const uint32_t samples_per_sec);
    195   virtual int32_t RecordingSampleRate(uint32_t* samples_per_sec) const;
    196   virtual int32_t SetPlayoutSampleRate(const uint32_t samples_per_sec);
    197   virtual int32_t PlayoutSampleRate(uint32_t* samples_per_sec) const;
    198 
    199   virtual int32_t ResetAudioDevice();
    200   virtual int32_t SetLoudspeakerStatus(bool enable);
    201   virtual int32_t GetLoudspeakerStatus(bool* enabled) const;
    202   // End of functions inherited from webrtc::AudioDeviceModule.
    203 
    204   // The following function is inherited from talk_base::MessageHandler.
    205   virtual void OnMessage(talk_base::Message* msg);
    206 
    207  protected:
    208   // The constructor is protected because the class needs to be created as a
    209   // reference counted object (for memory managment reasons). It could be
    210   // exposed in which case the burden of proper instantiation would be put on
    211   // the creator of a FakeAudioCaptureModule instance. To create an instance of
    212   // this class use the Create(..) API.
    213   explicit FakeAudioCaptureModule(talk_base::Thread* process_thread);
    214   // The destructor is protected because it is reference counted and should not
    215   // be deleted directly.
    216   virtual ~FakeAudioCaptureModule();
    217 
    218  private:
    219   // Initializes the state of the FakeAudioCaptureModule. This API is called on
    220   // creation by the Create() API.
    221   bool Initialize();
    222   // SetBuffer() sets all samples in send_buffer_ to |value|.
    223   void SetSendBuffer(int value);
    224   // Resets rec_buffer_. I.e., sets all rec_buffer_ samples to 0.
    225   void ResetRecBuffer();
    226   // Returns true if rec_buffer_ contains one or more sample greater than or
    227   // equal to |value|.
    228   bool CheckRecBuffer(int value);
    229 
    230   // Returns true/false depending on if recording or playback has been
    231   // enabled/started.
    232   bool ShouldStartProcessing();
    233 
    234   // Starts or stops the pushing and pulling of audio frames.
    235   void UpdateProcessing(bool start);
    236 
    237   // Starts the periodic calling of ProcessFrame() in a thread safe way.
    238   void StartProcessP();
    239   // Periodcally called function that ensures that frames are pulled and pushed
    240   // periodically if enabled/started.
    241   void ProcessFrameP();
    242   // Pulls frames from the registered webrtc::AudioTransport.
    243   void ReceiveFrameP();
    244   // Pushes frames to the registered webrtc::AudioTransport.
    245   void SendFrameP();
    246   // Stops the periodic calling of ProcessFrame() in a thread safe way.
    247   void StopProcessP();
    248 
    249   // The time in milliseconds when Process() was last called or 0 if no call
    250   // has been made.
    251   uint32 last_process_time_ms_;
    252 
    253   // Callback for playout and recording.
    254   webrtc::AudioTransport* audio_callback_;
    255 
    256   bool recording_; // True when audio is being pushed from the instance.
    257   bool playing_; // True when audio is being pulled by the instance.
    258 
    259   bool play_is_initialized_; // True when the instance is ready to pull audio.
    260   bool rec_is_initialized_; // True when the instance is ready to push audio.
    261 
    262   // Input to and output from RecordedDataIsAvailable(..) makes it possible to
    263   // modify the current mic level. The implementation does not care about the
    264   // mic level so it just feeds back what it receives.
    265   uint32_t current_mic_level_;
    266 
    267   // next_frame_time_ is updated in a non-drifting manner to indicate the next
    268   // wall clock time the next frame should be generated and received. started_
    269   // ensures that next_frame_time_ can be initialized properly on first call.
    270   bool started_;
    271   uint32 next_frame_time_;
    272 
    273   // User provided thread context.
    274   talk_base::Thread* process_thread_;
    275 
    276   // Buffer for storing samples received from the webrtc::AudioTransport.
    277   char rec_buffer_[kNumberSamples * kNumberBytesPerSample];
    278   // Buffer for samples to send to the webrtc::AudioTransport.
    279   char send_buffer_[kNumberSamples * kNumberBytesPerSample];
    280 
    281   // Counter of frames received that have samples of high enough amplitude to
    282   // indicate that the frames are not faked somewhere in the audio pipeline
    283   // (e.g. by a jitter buffer).
    284   int frames_received_;
    285 
    286   // Protects variables that are accessed from process_thread_ and
    287   // the main thread.
    288   mutable talk_base::CriticalSection crit_;
    289   // Protects |audio_callback_| that is accessed from process_thread_ and
    290   // the main thread.
    291   talk_base::CriticalSection crit_callback_;
    292 };
    293 
    294 #endif  // TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_
    295