Home | History | Annotate | Download | only in test
      1 /*
      2  *  Copyright (c) 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 
     11 #include <assert.h>
     12 #include <ctype.h>
     13 #include <stdio.h>
     14 #include <string.h>
     15 
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 #include "webrtc/modules/audio_device/test/func_test_manager.h"
     18 #include "webrtc/system_wrappers/interface/sleep.h"
     19 #include "webrtc/test/testsupport/fileutils.h"
     20 
     21 #include "webrtc/modules/audio_device/audio_device_config.h"
     22 #include "webrtc/modules/audio_device/audio_device_impl.h"
     23 
     24 #ifndef __GNUC__
     25 // Disable warning message ('sprintf': name was marked as #pragma deprecated)
     26 #pragma warning( disable : 4995 )
     27 // Disable warning message 4996 ('scanf': This function or variable may be unsafe)
     28 #pragma warning( disable : 4996 )
     29 #endif
     30 
     31 const char* RecordedMicrophoneFile = "recorded_microphone_mono_48.pcm";
     32 const char* RecordedMicrophoneVolumeFile =
     33 "recorded_microphone_volume_mono_48.pcm";
     34 const char* RecordedMicrophoneMuteFile = "recorded_microphone_mute_mono_48.pcm";
     35 const char* RecordedMicrophoneBoostFile =
     36 "recorded_microphone_boost_mono_48.pcm";
     37 const char* RecordedMicrophoneAGCFile = "recorded_microphone_AGC_mono_48.pcm";
     38 const char* RecordedSpeakerFile = "recorded_speaker_48.pcm";
     39 
     40 // Helper functions
     41 #if !defined(WEBRTC_IOS)
     42 char* GetFilename(char* filename)
     43 {
     44     return filename;
     45 }
     46 const char* GetFilename(const char* filename)
     47 {
     48     return filename;
     49 }
     50 char* GetResource(char* resource)
     51 {
     52     return resource;
     53 }
     54 const char* GetResource(const char* resource)
     55 {
     56     return resource;
     57 }
     58 #endif
     59 
     60 namespace webrtc
     61 {
     62 
     63 AudioEventObserver::AudioEventObserver(AudioDeviceModule* audioDevice)
     64 {
     65 }
     66 
     67 AudioEventObserver::~AudioEventObserver()
     68 {
     69 }
     70 
     71 void AudioEventObserver::OnErrorIsReported(const ErrorCode error)
     72 {
     73     TEST_LOG("\n[*** ERROR ***] => OnErrorIsReported(%d)\n \n", error);
     74     _error = error;
     75 }
     76 
     77 
     78 void AudioEventObserver::OnWarningIsReported(const WarningCode warning)
     79 {
     80     TEST_LOG("\n[*** WARNING ***] => OnWarningIsReported(%d)\n \n", warning);
     81     _warning = warning;
     82 }
     83 
     84 AudioTransportImpl::AudioTransportImpl(AudioDeviceModule* audioDevice) :
     85     _audioDevice(audioDevice),
     86     _playFromFile(false),
     87     _fullDuplex(false),
     88     _speakerVolume(false),
     89     _speakerMute(false),
     90     _microphoneVolume(false),
     91     _microphoneMute(false),
     92     _microphoneBoost(false),
     93     _microphoneAGC(false),
     94     _loopBackMeasurements(false),
     95     _playFile(*FileWrapper::Create()),
     96     _recCount(0),
     97     _playCount(0)
     98 {
     99     _resampler.Reset(48000, 48000, kResamplerSynchronousStereo);
    100 }
    101 
    102 AudioTransportImpl::~AudioTransportImpl()
    103 {
    104     _playFile.Flush();
    105     _playFile.CloseFile();
    106     delete &_playFile;
    107 
    108     for (AudioPacketList::iterator iter = _audioList.begin();
    109          iter != _audioList.end(); ++iter) {
    110             delete *iter;
    111     }
    112 }
    113 
    114 // ----------------------------------------------------------------------------
    115 //	AudioTransportImpl::SetFilePlayout
    116 // ----------------------------------------------------------------------------
    117 
    118 int32_t AudioTransportImpl::SetFilePlayout(bool enable, const char* fileName)
    119 {
    120     _playFromFile = enable;
    121     if (enable)
    122     {
    123         return (_playFile.OpenFile(fileName, true, true, false));
    124     } else
    125     {
    126         _playFile.Flush();
    127         return (_playFile.CloseFile());
    128     }
    129 }
    130 ;
    131 
    132 void AudioTransportImpl::SetFullDuplex(bool enable)
    133 {
    134     _fullDuplex = enable;
    135 
    136     for (AudioPacketList::iterator iter = _audioList.begin();
    137          iter != _audioList.end(); ++iter) {
    138             delete *iter;
    139     }
    140     _audioList.clear();
    141 }
    142 
    143 int32_t AudioTransportImpl::RecordedDataIsAvailable(
    144     const void* audioSamples,
    145     const uint32_t nSamples,
    146     const uint8_t nBytesPerSample,
    147     const uint8_t nChannels,
    148     const uint32_t samplesPerSec,
    149     const uint32_t totalDelayMS,
    150     const int32_t clockDrift,
    151     const uint32_t currentMicLevel,
    152     const bool keyPressed,
    153     uint32_t& newMicLevel)
    154 {
    155     if (_fullDuplex && _audioList.size() < 15)
    156     {
    157         AudioPacket* packet = new AudioPacket();
    158         memcpy(packet->dataBuffer, audioSamples, nSamples * nBytesPerSample);
    159         packet->nSamples = (uint16_t) nSamples;
    160         packet->nBytesPerSample = nBytesPerSample;
    161         packet->nChannels = nChannels;
    162         packet->samplesPerSec = samplesPerSec;
    163         _audioList.push_back(packet);
    164     }
    165 
    166     _recCount++;
    167     if (_recCount % 100 == 0)
    168     {
    169         bool addMarker(true);
    170 
    171         if (_loopBackMeasurements)
    172         {
    173             addMarker = false;
    174         }
    175 
    176         if (_microphoneVolume)
    177         {
    178             uint32_t maxVolume(0);
    179             uint32_t minVolume(0);
    180             uint32_t volume(0);
    181             uint16_t stepSize(0);
    182             EXPECT_EQ(0, _audioDevice->MaxMicrophoneVolume(&maxVolume));
    183             EXPECT_EQ(0, _audioDevice->MinMicrophoneVolume(&minVolume));
    184             EXPECT_EQ(0, _audioDevice->MicrophoneVolumeStepSize(&stepSize));
    185             EXPECT_EQ(0, _audioDevice->MicrophoneVolume(&volume));
    186             if (volume == 0)
    187             {
    188                 TEST_LOG("[0]");
    189                 addMarker = false;
    190             }
    191             int stepScale = (int) ((maxVolume - minVolume) / (stepSize * 10));
    192             volume += (stepScale * stepSize);
    193             if (volume > maxVolume)
    194             {
    195                 TEST_LOG("[MAX]");
    196                 volume = 0;
    197                 addMarker = false;
    198             }
    199             EXPECT_EQ(0, _audioDevice->SetMicrophoneVolume(volume));
    200         }
    201 
    202         if (_microphoneAGC)
    203         {
    204             uint32_t maxVolume(0);
    205             uint32_t minVolume(0);
    206             uint16_t stepSize(0);
    207             EXPECT_EQ(0, _audioDevice->MaxMicrophoneVolume(&maxVolume));
    208             EXPECT_EQ(0, _audioDevice->MinMicrophoneVolume(&minVolume));
    209             EXPECT_EQ(0, _audioDevice->MicrophoneVolumeStepSize(&stepSize));
    210             // emulate real AGC (min->max->min->max etc.)
    211             if (currentMicLevel <= 1)
    212             {
    213                 TEST_LOG("[MIN]");
    214                 addMarker = false;
    215             }
    216             int stepScale = (int) ((maxVolume - minVolume) / (stepSize * 10));
    217             newMicLevel = currentMicLevel + (stepScale * stepSize);
    218             if (newMicLevel > maxVolume)
    219             {
    220                 TEST_LOG("[MAX]");
    221                 newMicLevel = 1; // set lowest (non-zero) AGC level
    222                 addMarker = false;
    223             }
    224         }
    225 
    226         if (_microphoneMute && (_recCount % 500 == 0))
    227         {
    228             bool muted(false);
    229             EXPECT_EQ(0, _audioDevice->MicrophoneMute(&muted));
    230             muted = !muted;
    231             EXPECT_EQ(0, _audioDevice->SetMicrophoneMute(muted));
    232             if (muted)
    233             {
    234                 TEST_LOG("[MUTE ON]");
    235                 addMarker = false;
    236             } else
    237             {
    238                 TEST_LOG("[MUTE OFF]");
    239                 addMarker = false;
    240             }
    241         }
    242 
    243         if (_microphoneBoost && (_recCount % 500 == 0))
    244         {
    245             bool boosted(false);
    246             EXPECT_EQ(0, _audioDevice->MicrophoneBoost(&boosted));
    247             boosted = !boosted;
    248             EXPECT_EQ(0, _audioDevice->SetMicrophoneBoost(boosted));
    249             if (boosted)
    250             {
    251                 TEST_LOG("[BOOST ON]");
    252                 addMarker = false;
    253             } else
    254             {
    255                 TEST_LOG("[BOOST OFF]");
    256                 addMarker = false;
    257             }
    258         }
    259 
    260         if ((nChannels == 1) && addMarker)
    261         {
    262             // mono
    263             TEST_LOG("-");
    264         } else if ((nChannels == 2) && (nBytesPerSample == 2) && addMarker)
    265         {
    266             AudioDeviceModule::ChannelType
    267                 chType(AudioDeviceModule::kChannelLeft);
    268             EXPECT_EQ(0, _audioDevice->RecordingChannel(&chType));
    269             if (chType == AudioDeviceModule::kChannelLeft)
    270                 TEST_LOG("-|");
    271             else
    272                 TEST_LOG("|-");
    273         } else if (addMarker)
    274         {
    275             // stereo
    276             TEST_LOG("--");
    277         }
    278 
    279         if (nChannels == 2 && nBytesPerSample == 2)
    280         {
    281             // TEST_LOG("=> emulated mono (one channel exctracted from stereo input)\n");
    282         }
    283     }
    284 
    285     return 0;
    286 }
    287 
    288 
    289 int32_t AudioTransportImpl::NeedMorePlayData(
    290     const uint32_t nSamples,
    291     const uint8_t nBytesPerSample,
    292     const uint8_t nChannels,
    293     const uint32_t samplesPerSec,
    294     void* audioSamples,
    295     uint32_t& nSamplesOut,
    296     int64_t* elapsed_time_ms,
    297     int64_t* ntp_time_ms)
    298 {
    299     if (_fullDuplex)
    300     {
    301         if (_audioList.empty())
    302         {
    303             // use zero stuffing when not enough data
    304             memset(audioSamples, 0, nBytesPerSample * nSamples);
    305         } else
    306         {
    307             AudioPacket* packet = _audioList.front();
    308             _audioList.pop_front();
    309             if (packet)
    310             {
    311                 int ret(0);
    312                 int lenOut(0);
    313                 int16_t tmpBuf_96kHz[80 * 12];
    314                 int16_t* ptr16In = NULL;
    315                 int16_t* ptr16Out = NULL;
    316 
    317                 const uint16_t nSamplesIn = packet->nSamples;
    318                 const uint8_t nChannelsIn = packet->nChannels;
    319                 const uint32_t samplesPerSecIn = packet->samplesPerSec;
    320                 const uint16_t nBytesPerSampleIn =
    321                     packet->nBytesPerSample;
    322 
    323                 int32_t fsInHz(samplesPerSecIn);
    324                 int32_t fsOutHz(samplesPerSec);
    325 
    326                 if (fsInHz == 44100)
    327                     fsInHz = 44000;
    328 
    329                 if (fsOutHz == 44100)
    330                     fsOutHz = 44000;
    331 
    332                 if (nChannelsIn == 2 && nBytesPerSampleIn == 4)
    333                 {
    334                     // input is stereo => we will resample in stereo
    335                     ret = _resampler.ResetIfNeeded(fsInHz, fsOutHz,
    336                                                    kResamplerSynchronousStereo);
    337                     if (ret == 0)
    338                     {
    339                         if (nChannels == 2)
    340                         {
    341                             _resampler.Push(
    342                                 (const int16_t*) packet->dataBuffer,
    343                                 2 * nSamplesIn,
    344                                 (int16_t*) audioSamples, 2
    345                                 * nSamples, lenOut);
    346                         } else
    347                         {
    348                             _resampler.Push(
    349                                 (const int16_t*) packet->dataBuffer,
    350                                 2 * nSamplesIn, tmpBuf_96kHz, 2
    351                                 * nSamples, lenOut);
    352 
    353                             ptr16In = &tmpBuf_96kHz[0];
    354                             ptr16Out = (int16_t*) audioSamples;
    355 
    356                             // do stereo -> mono
    357                             for (unsigned int i = 0; i < nSamples; i++)
    358                             {
    359                                 *ptr16Out = *ptr16In; // use left channel
    360                                 ptr16Out++;
    361                                 ptr16In++;
    362                                 ptr16In++;
    363                             }
    364                         }
    365                         assert(2*nSamples == (uint32_t)lenOut);
    366                     } else
    367                     {
    368                         if (_playCount % 100 == 0)
    369                             TEST_LOG(
    370                                      "ERROR: unable to resample from %d to %d\n",
    371                                      samplesPerSecIn, samplesPerSec);
    372                     }
    373                 } else
    374                 {
    375                     // input is mono (can be "reduced from stereo" as well) =>
    376                     // we will resample in mono
    377                     ret = _resampler.ResetIfNeeded(fsInHz, fsOutHz,
    378                                                    kResamplerSynchronous);
    379                     if (ret == 0)
    380                     {
    381                         if (nChannels == 1)
    382                         {
    383                             _resampler.Push(
    384                                 (const int16_t*) packet->dataBuffer,
    385                                 nSamplesIn,
    386                                 (int16_t*) audioSamples,
    387                                 nSamples, lenOut);
    388                         } else
    389                         {
    390                             _resampler.Push(
    391                                 (const int16_t*) packet->dataBuffer,
    392                                 nSamplesIn, tmpBuf_96kHz, nSamples,
    393                                 lenOut);
    394 
    395                             ptr16In = &tmpBuf_96kHz[0];
    396                             ptr16Out = (int16_t*) audioSamples;
    397 
    398                             // do mono -> stereo
    399                             for (unsigned int i = 0; i < nSamples; i++)
    400                             {
    401                                 *ptr16Out = *ptr16In; // left
    402                                 ptr16Out++;
    403                                 *ptr16Out = *ptr16In; // right (same as left sample)
    404                                 ptr16Out++;
    405                                 ptr16In++;
    406                             }
    407                         }
    408                         assert(nSamples == (uint32_t)lenOut);
    409                     } else
    410                     {
    411                         if (_playCount % 100 == 0)
    412                             TEST_LOG("ERROR: unable to resample from %d to %d\n",
    413                                      samplesPerSecIn, samplesPerSec);
    414                     }
    415                 }
    416                 nSamplesOut = nSamples;
    417                 delete packet;
    418             }
    419         }
    420     }  // if (_fullDuplex)
    421 
    422     if (_playFromFile && _playFile.Open())
    423     {
    424         int16_t fileBuf[480];
    425 
    426         // read mono-file
    427         int32_t len = _playFile.Read((int8_t*) fileBuf, 2
    428             * nSamples);
    429         if (len != 2 * (int32_t) nSamples)
    430         {
    431             _playFile.Rewind();
    432             _playFile.Read((int8_t*) fileBuf, 2 * nSamples);
    433         }
    434 
    435         // convert to stero if required
    436         if (nChannels == 1)
    437         {
    438             memcpy(audioSamples, fileBuf, 2 * nSamples);
    439         } else
    440         {
    441             // mono sample from file is duplicated and sent to left and right
    442             // channels
    443             int16_t* audio16 = (int16_t*) audioSamples;
    444             for (unsigned int i = 0; i < nSamples; i++)
    445             {
    446                 (*audio16) = fileBuf[i]; // left
    447                 audio16++;
    448                 (*audio16) = fileBuf[i]; // right
    449                 audio16++;
    450             }
    451         }
    452     }  // if (_playFromFile && _playFile.Open())
    453 
    454     _playCount++;
    455 
    456     if (_playCount % 100 == 0)
    457     {
    458         bool addMarker(true);
    459 
    460         if (_speakerVolume)
    461         {
    462             uint32_t maxVolume(0);
    463             uint32_t minVolume(0);
    464             uint32_t volume(0);
    465             uint16_t stepSize(0);
    466             EXPECT_EQ(0, _audioDevice->MaxSpeakerVolume(&maxVolume));
    467             EXPECT_EQ(0, _audioDevice->MinSpeakerVolume(&minVolume));
    468             EXPECT_EQ(0, _audioDevice->SpeakerVolumeStepSize(&stepSize));
    469             EXPECT_EQ(0, _audioDevice->SpeakerVolume(&volume));
    470             if (volume == 0)
    471             {
    472                 TEST_LOG("[0]");
    473                 addMarker = false;
    474             }
    475             uint32_t step = (maxVolume - minVolume) / 10;
    476             step = (step < stepSize ? stepSize : step);
    477             volume += step;
    478             if (volume > maxVolume)
    479             {
    480                 TEST_LOG("[MAX]");
    481                 volume = 0;
    482                 addMarker = false;
    483             }
    484             EXPECT_EQ(0, _audioDevice->SetSpeakerVolume(volume));
    485         }
    486 
    487         if (_speakerMute && (_playCount % 500 == 0))
    488         {
    489             bool muted(false);
    490             EXPECT_EQ(0, _audioDevice->SpeakerMute(&muted));
    491             muted = !muted;
    492             EXPECT_EQ(0, _audioDevice->SetSpeakerMute(muted));
    493             if (muted)
    494             {
    495                 TEST_LOG("[MUTE ON]");
    496                 addMarker = false;
    497             } else
    498             {
    499                 TEST_LOG("[MUTE OFF]");
    500                 addMarker = false;
    501             }
    502         }
    503 
    504         if (_loopBackMeasurements)
    505         {
    506             uint16_t recDelayMS(0);
    507             uint16_t playDelayMS(0);
    508             size_t nItemsInList(0);
    509 
    510             nItemsInList = _audioList.size();
    511             EXPECT_EQ(0, _audioDevice->RecordingDelay(&recDelayMS));
    512             EXPECT_EQ(0, _audioDevice->PlayoutDelay(&playDelayMS));
    513             TEST_LOG("Delay (rec+play)+buf: %3zu (%3u+%3u)+%3zu [ms]\n",
    514                      recDelayMS + playDelayMS + 10 * (nItemsInList + 1),
    515                      recDelayMS, playDelayMS, 10 * (nItemsInList + 1));
    516 
    517             addMarker = false;
    518         }
    519 
    520         if ((nChannels == 1) && addMarker)
    521         {
    522             TEST_LOG("+");
    523         } else if ((nChannels == 2) && addMarker)
    524         {
    525             TEST_LOG("++");
    526         }
    527     }  // if (_playCount % 100 == 0)
    528 
    529     nSamplesOut = nSamples;
    530 
    531     return 0;
    532 }
    533 
    534 int AudioTransportImpl::OnDataAvailable(const int voe_channels[],
    535                                         int number_of_voe_channels,
    536                                         const int16_t* audio_data,
    537                                         int sample_rate,
    538                                         int number_of_channels,
    539                                         int number_of_frames,
    540                                         int audio_delay_milliseconds,
    541                                         int current_volume,
    542                                         bool key_pressed,
    543                                         bool need_audio_processing) {
    544   return 0;
    545 }
    546 
    547 void AudioTransportImpl::PushCaptureData(int voe_channel,
    548                                          const void* audio_data,
    549                                          int bits_per_sample, int sample_rate,
    550                                          int number_of_channels,
    551                                          int number_of_frames) {}
    552 
    553 void AudioTransportImpl::PullRenderData(int bits_per_sample, int sample_rate,
    554                                         int number_of_channels,
    555                                         int number_of_frames,
    556                                         void* audio_data,
    557                                         int64_t* elapsed_time_ms,
    558                                         int64_t* ntp_time_ms) {}
    559 
    560 FuncTestManager::FuncTestManager() :
    561     _processThread(NULL),
    562     _audioDevice(NULL),
    563     _audioEventObserver(NULL),
    564     _audioTransport(NULL)
    565 {
    566   _playoutFile48 = webrtc::test::ResourcePath("audio_device\\audio_short48",
    567                                               "pcm");
    568   _playoutFile44 = webrtc::test::ResourcePath("audio_device\\audio_short44",
    569                                               "pcm");
    570   _playoutFile16 = webrtc::test::ResourcePath("audio_device\\audio_short16",
    571                                               "pcm");
    572   _playoutFile8 = webrtc::test::ResourcePath("audio_device\\audio_short8",
    573                                              "pcm");
    574 }
    575 
    576 FuncTestManager::~FuncTestManager()
    577 {
    578 }
    579 
    580 int32_t FuncTestManager::Init()
    581 {
    582     EXPECT_TRUE((_processThread = ProcessThread::CreateProcessThread()) != NULL);
    583     if (_processThread == NULL)
    584     {
    585         return -1;
    586     }
    587     _processThread->Start();
    588 
    589     // create the Audio Device module
    590     EXPECT_TRUE((_audioDevice = AudioDeviceModuleImpl::Create(
    591         555, ADM_AUDIO_LAYER)) != NULL);
    592     if (_audioDevice == NULL)
    593     {
    594         return -1;
    595     }
    596     EXPECT_EQ(1, _audioDevice->AddRef());
    597 
    598     // register the Audio Device module
    599     _processThread->RegisterModule(_audioDevice);
    600 
    601     // register event observer
    602     _audioEventObserver = new AudioEventObserver(_audioDevice);
    603     EXPECT_EQ(0, _audioDevice->RegisterEventObserver(_audioEventObserver));
    604 
    605     // register audio transport
    606     _audioTransport = new AudioTransportImpl(_audioDevice);
    607     EXPECT_EQ(0, _audioDevice->RegisterAudioCallback(_audioTransport));
    608 
    609     return 0;
    610 }
    611 
    612 int32_t FuncTestManager::Close()
    613 {
    614     EXPECT_EQ(0, _audioDevice->RegisterEventObserver(NULL));
    615     EXPECT_EQ(0, _audioDevice->RegisterAudioCallback(NULL));
    616     EXPECT_EQ(0, _audioDevice->Terminate());
    617 
    618     // release the ProcessThread object
    619     if (_processThread)
    620     {
    621         _processThread->DeRegisterModule(_audioDevice);
    622         _processThread->Stop();
    623         ProcessThread::DestroyProcessThread(_processThread);
    624     }
    625 
    626     // delete the audio observer
    627     if (_audioEventObserver)
    628     {
    629         delete _audioEventObserver;
    630         _audioEventObserver = NULL;
    631     }
    632 
    633     // delete the audio transport
    634     if (_audioTransport)
    635     {
    636         delete _audioTransport;
    637         _audioTransport = NULL;
    638     }
    639 
    640     // release the AudioDeviceModule object
    641     if (_audioDevice)
    642     {
    643         EXPECT_EQ(0, _audioDevice->Release());
    644         _audioDevice = NULL;
    645     }
    646 
    647     // return the ThreadWrapper (singleton)
    648     Trace::ReturnTrace();
    649 
    650     // PRINT_TEST_RESULTS;
    651 
    652     return 0;
    653 }
    654 
    655 int32_t FuncTestManager::DoTest(const TestType testType)
    656 {
    657     switch (testType)
    658     {
    659         case TTAll:
    660             TestAudioLayerSelection();
    661             TestDeviceEnumeration();
    662             TestDeviceSelection();
    663             TestAudioTransport();
    664             TestSpeakerVolume();
    665             TestMicrophoneVolume();
    666             TestLoopback();
    667         case TTAudioLayerSelection:
    668             TestAudioLayerSelection();
    669             break;
    670         case TTDeviceEnumeration:
    671             TestDeviceEnumeration();
    672             break;
    673         case TTDeviceSelection:
    674             TestDeviceSelection();
    675             break;
    676         case TTAudioTransport:
    677             TestAudioTransport();
    678             break;
    679         case TTSpeakerVolume:
    680             TestSpeakerVolume();
    681             break;
    682         case TTMicrophoneVolume:
    683             TestMicrophoneVolume();
    684             break;
    685         case TTSpeakerMute:
    686             TestSpeakerMute();
    687             break;
    688         case TTMicrophoneMute:
    689             TestMicrophoneMute();
    690             break;
    691         case TTMicrophoneBoost:
    692             TestMicrophoneBoost();
    693             break;
    694         case TTMicrophoneAGC:
    695             TestMicrophoneAGC();
    696             break;
    697         case TTLoopback:
    698             TestLoopback();
    699             break;
    700         case TTDeviceRemoval:
    701             TestDeviceRemoval();
    702             break;
    703         case TTMobileAPI:
    704             TestAdvancedMBAPI();
    705         case TTTest:
    706             TestExtra();
    707             break;
    708         default:
    709             break;
    710     }
    711 
    712     return 0;
    713 }
    714 
    715 int32_t FuncTestManager::TestAudioLayerSelection()
    716 {
    717     TEST_LOG("\n=======================================\n");
    718     TEST_LOG(" Audio Layer test:\n");
    719     TEST_LOG("=======================================\n");
    720 
    721     if (_audioDevice == NULL)
    722     {
    723         return -1;
    724     }
    725 
    726     RESET_TEST;
    727 
    728     AudioDeviceModule* audioDevice = _audioDevice;
    729 
    730     AudioDeviceModule::AudioLayer audioLayer;
    731     EXPECT_EQ(0, audioDevice->ActiveAudioLayer(&audioLayer));
    732 
    733     if (audioLayer == AudioDeviceModule::kWindowsWaveAudio)
    734     {
    735         TEST_LOG("\nActiveAudioLayer: kWindowsWaveAudio\n \n");
    736     } else if (audioLayer == AudioDeviceModule::kWindowsCoreAudio)
    737     {
    738         TEST_LOG("\nActiveAudioLayer: kWindowsCoreAudio\n \n");
    739     } else if (audioLayer == AudioDeviceModule::kLinuxAlsaAudio)
    740     {
    741         TEST_LOG("\nActiveAudioLayer: kLinuxAlsaAudio\n \n");
    742     } else if (audioLayer == AudioDeviceModule::kLinuxPulseAudio)
    743     {
    744         TEST_LOG("\nActiveAudioLayer: kLinuxPulseAudio\n \n");
    745     } else
    746     {
    747         TEST_LOG("\nActiveAudioLayer: INVALID\n \n");
    748     }
    749 
    750     char ch;
    751     bool tryWinWave(false);
    752     bool tryWinCore(false);
    753 
    754     if (audioLayer == AudioDeviceModule::kWindowsWaveAudio)
    755     {
    756         TEST_LOG("Would you like to try kWindowsCoreAudio instead "
    757             "[requires Win Vista or Win 7] (Y/N)?\n: ");
    758         EXPECT_TRUE(scanf(" %c", &ch) > 0);
    759         ch = toupper(ch);
    760         if (ch == 'Y')
    761         {
    762             tryWinCore = true;
    763         }
    764     } else if (audioLayer == AudioDeviceModule::kWindowsCoreAudio)
    765     {
    766         TEST_LOG("Would you like to try kWindowsWaveAudio instead (Y/N)?\n: ");
    767         EXPECT_TRUE(scanf(" %c", &ch) > 0);
    768         ch = toupper(ch);
    769         if (ch == 'Y')
    770         {
    771             tryWinWave = true;
    772         }
    773     }
    774 
    775     if (tryWinWave || tryWinCore)
    776     {
    777         // =======================================
    778         // First, close down what we have started
    779 
    780         // terminate
    781         EXPECT_EQ(0, _audioDevice->RegisterEventObserver(NULL));
    782         EXPECT_EQ(0, _audioDevice->RegisterAudioCallback(NULL));
    783         EXPECT_EQ(0, _audioDevice->Terminate());
    784 
    785         // release the ProcessThread object
    786         if (_processThread)
    787         {
    788             _processThread->DeRegisterModule(_audioDevice);
    789             _processThread->Stop();
    790             ProcessThread::DestroyProcessThread(_processThread);
    791         }
    792 
    793         // delete the audio observer
    794         if (_audioEventObserver)
    795         {
    796             delete _audioEventObserver;
    797             _audioEventObserver = NULL;
    798         }
    799 
    800         // delete the audio transport
    801         if (_audioTransport)
    802         {
    803             delete _audioTransport;
    804             _audioTransport = NULL;
    805         }
    806 
    807         // release the AudioDeviceModule object
    808         if (_audioDevice)
    809         {
    810             EXPECT_EQ(0, _audioDevice->Release());
    811             _audioDevice = NULL;
    812         }
    813 
    814         // ==================================================
    815         // Next, try to make fresh start with new audio layer
    816 
    817         EXPECT_TRUE((_processThread = ProcessThread::CreateProcessThread()) != NULL);
    818         if (_processThread == NULL)
    819         {
    820             return -1;
    821         }
    822         _processThread->Start();
    823 
    824         // create the Audio Device module based on selected audio layer
    825         if (tryWinWave)
    826         {
    827             _audioDevice = AudioDeviceModuleImpl::Create(
    828                 555,
    829                 AudioDeviceModule::kWindowsWaveAudio);
    830         } else if (tryWinCore)
    831         {
    832             _audioDevice = AudioDeviceModuleImpl::Create(
    833                 555,
    834                 AudioDeviceModule::kWindowsCoreAudio);
    835         }
    836 
    837         if (_audioDevice == NULL)
    838         {
    839             TEST_LOG("\nERROR: Switch of audio layer failed!\n");
    840             // restore default audio layer instead
    841             EXPECT_TRUE((_audioDevice = AudioDeviceModuleImpl::Create(
    842                 555, AudioDeviceModule::kPlatformDefaultAudio)) != NULL);
    843         }
    844 
    845         if (_audioDevice == NULL)
    846         {
    847             TEST_LOG("\nERROR: Failed to revert back to default audio layer!\n");
    848             return -1;
    849         }
    850 
    851         EXPECT_EQ(1, _audioDevice->AddRef());
    852 
    853         // register the Audio Device module
    854         _processThread->RegisterModule(_audioDevice);
    855 
    856         // register event observer
    857         _audioEventObserver = new AudioEventObserver(_audioDevice);
    858         EXPECT_EQ(0, _audioDevice->RegisterEventObserver(_audioEventObserver));
    859 
    860         // register audio transport
    861         _audioTransport = new AudioTransportImpl(_audioDevice);
    862         EXPECT_EQ(0, _audioDevice->RegisterAudioCallback(_audioTransport));
    863 
    864         EXPECT_EQ(0, _audioDevice->ActiveAudioLayer(&audioLayer));
    865 
    866         if (audioLayer == AudioDeviceModule::kWindowsWaveAudio)
    867         {
    868             if (tryWinCore)
    869                 TEST_LOG("\nActiveAudioLayer: kWindowsWaveAudio <=> "
    870                     "switch was *not* possible\n \n");
    871             else
    872                 TEST_LOG("\nActiveAudioLayer: kWindowsWaveAudio <=> "
    873                     "switch was possible\n \n");
    874         } else if (audioLayer == AudioDeviceModule::kWindowsCoreAudio)
    875         {
    876             if (tryWinWave)
    877                 TEST_LOG("\nActiveAudioLayer: kWindowsCoreAudio <=> "
    878                     "switch was *not* possible\n \n");
    879             else
    880                 TEST_LOG("\nActiveAudioLayer: kWindowsCoreAudio <=> "
    881                     "switch was possible\n \n");
    882         }
    883     }  // if (tryWinWave || tryWinCore)
    884 
    885     PRINT_TEST_RESULTS;
    886 
    887     return 0;
    888 }
    889 
    890 int32_t FuncTestManager::TestDeviceEnumeration()
    891 {
    892     TEST_LOG("\n=======================================\n");
    893     TEST_LOG(" Device Enumeration test:\n");
    894     TEST_LOG("=======================================\n");
    895 
    896     if (_audioDevice == NULL)
    897     {
    898         return -1;
    899     }
    900 
    901     RESET_TEST;
    902 
    903     AudioDeviceModule* audioDevice = _audioDevice;
    904 
    905     EXPECT_EQ(0, audioDevice->Init());
    906     EXPECT_TRUE(audioDevice->Initialized());
    907 
    908     char name[kAdmMaxDeviceNameSize];
    909     char guid[kAdmMaxGuidSize];
    910 
    911     const int16_t nPlayoutDevices(audioDevice->PlayoutDevices());
    912     EXPECT_TRUE(nPlayoutDevices >= 0);
    913     TEST_LOG("\nPlayoutDevices: %u\n \n", nPlayoutDevices);
    914     for (int n = 0; n < nPlayoutDevices; n++)
    915     {
    916         EXPECT_EQ(0, audioDevice->PlayoutDeviceName(n, name, guid));
    917         TEST_LOG(
    918                  "PlayoutDeviceName(%d) :   name=%s \n \
    919 	                 guid=%s\n",
    920                  n, name, guid);
    921     }
    922 
    923 #ifdef _WIN32
    924     // default (-1)
    925     // TODO(henrika): fix below test.
    926 #if 0
    927     EXPECT_EQ(0, audioDevice->PlayoutDeviceName(-1, name, guid));
    928     TEST_LOG("PlayoutDeviceName(%d):   default name=%s \n \
    929 	                 default guid=%s\n", -1, name, guid);
    930 #endif  // 0
    931 #else
    932     // should fail
    933     EXPECT_EQ(-1, audioDevice->PlayoutDeviceName(-1, name, guid));
    934 #endif
    935 
    936     const int16_t nRecordingDevices(audioDevice->RecordingDevices());
    937     EXPECT_TRUE(nRecordingDevices >= 0);
    938     TEST_LOG("\nRecordingDevices: %u\n \n", nRecordingDevices);
    939     for (int n = 0; n < nRecordingDevices; n++)
    940     {
    941         EXPECT_EQ(0, audioDevice->RecordingDeviceName(n, name, guid));
    942         TEST_LOG(
    943                  "RecordingDeviceName(%d) : name=%s \n \
    944 	                 guid=%s\n",
    945                  n, name, guid);
    946     }
    947 
    948 #ifdef _WIN32
    949     // default (-1)
    950     // TODO(henrika): fix below test.
    951 #if 0
    952     EXPECT_EQ(0, audioDevice->RecordingDeviceName(-1, name, guid));
    953     TEST_LOG("RecordingDeviceName(%d): default name=%s \n \
    954 	                 default guid=%s\n", -1, name, guid);
    955 #endif
    956 #else
    957     // should fail
    958     EXPECT_EQ(-1, audioDevice->PlayoutDeviceName(-1, name, guid));
    959 #endif
    960 
    961     EXPECT_EQ(0, audioDevice->Terminate());
    962     EXPECT_FALSE(audioDevice->Initialized());
    963 
    964     PRINT_TEST_RESULTS;
    965 
    966     return 0;
    967 }
    968 
    969 int32_t FuncTestManager::TestDeviceSelection()
    970 {
    971     TEST_LOG("\n=======================================\n");
    972     TEST_LOG(" Device Selection test:\n");
    973     TEST_LOG("=======================================\n");
    974 
    975     if (_audioDevice == NULL)
    976     {
    977         return -1;
    978     }
    979 
    980     RESET_TEST;
    981 
    982 #define PRINT_HEADING(a, b) \
    983 	{ \
    984 		TEST_LOG("Set" #a "Device(" #b ") => \n"); \
    985 	} \
    986 
    987 #define PRINT_HEADING_IDX(a, b,c ) \
    988 	{ \
    989 		TEST_LOG("Set" #a "Device(%d) (%s) => \n", b, c); \
    990 	} \
    991 
    992 #define PRINT_STR(a, b) \
    993 	{ \
    994                 char str[128]; \
    995                 (b == true) ? (sprintf(str, "  %-17s: available\n", #a)) : (sprintf(str, "  %-17s: NA\n", #a)); \
    996                 TEST_LOG("%s", str); \
    997 	} \
    998 
    999     AudioDeviceModule* audioDevice = _audioDevice;
   1000 
   1001     EXPECT_EQ(0, audioDevice->Init());
   1002     EXPECT_TRUE(audioDevice->Initialized());
   1003 
   1004     bool available(false);
   1005     int16_t nDevices(-1);
   1006     char name[kAdmMaxDeviceNameSize];
   1007     char guid[kAdmMaxGuidSize];
   1008 
   1009     // =======
   1010     // Playout
   1011 
   1012     nDevices = audioDevice->PlayoutDevices();
   1013     EXPECT_TRUE(nDevices >= 0);
   1014 
   1015     TEST_LOG("\n");
   1016 #ifdef _WIN32
   1017     EXPECT_TRUE(audioDevice->SetPlayoutDevice(
   1018         AudioDeviceModule::kDefaultCommunicationDevice) == 0);
   1019     PRINT_HEADING(Playout, kDefaultCommunicationDevice);
   1020     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   1021     PRINT_STR(Playout, available);
   1022     if (available)
   1023     {
   1024         EXPECT_EQ(0, audioDevice->StereoPlayoutIsAvailable(&available));
   1025         PRINT_STR(Stereo Playout, available);
   1026     }
   1027     else
   1028     {
   1029         PRINT_STR(Stereo Playout, false);
   1030     }
   1031     EXPECT_EQ(0, audioDevice->SpeakerVolumeIsAvailable(&available));
   1032     PRINT_STR(Speaker Volume, available);
   1033     EXPECT_EQ(0, audioDevice->SpeakerMuteIsAvailable(&available));
   1034     PRINT_STR(Speaker Mute, available);
   1035 
   1036     EXPECT_EQ(0, audioDevice->SetPlayoutDevice(AudioDeviceModule::kDefaultDevice));
   1037     PRINT_HEADING(Playout, kDefaultDevice);
   1038     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   1039     PRINT_STR(Playout, available);
   1040     if (available)
   1041     {
   1042         EXPECT_EQ(0, audioDevice->StereoPlayoutIsAvailable(&available));
   1043         PRINT_STR(Stereo Playout, available);
   1044     }
   1045     else
   1046     {
   1047         PRINT_STR(Stereo Playout, false);
   1048     }
   1049     EXPECT_EQ(0, audioDevice->SpeakerVolumeIsAvailable(&available));
   1050     PRINT_STR(Speaker Volume, available);
   1051     EXPECT_EQ(0, audioDevice->SpeakerMuteIsAvailable(&available));
   1052     PRINT_STR(Speaker Mute, available);
   1053 #else
   1054     EXPECT_TRUE(audioDevice->SetPlayoutDevice(
   1055         AudioDeviceModule::kDefaultCommunicationDevice) == -1);
   1056     EXPECT_EQ(-1, audioDevice->SetPlayoutDevice(AudioDeviceModule::kDefaultDevice));
   1057 #endif
   1058 
   1059     for (int i = 0; i < nDevices; i++)
   1060     {
   1061         EXPECT_EQ(0, audioDevice->SetPlayoutDevice(i));
   1062         EXPECT_EQ(0, audioDevice->PlayoutDeviceName(i, name, guid));
   1063         PRINT_HEADING_IDX(Playout, i, name);
   1064         EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   1065         PRINT_STR(Playout, available);
   1066         if (available)
   1067         {
   1068             EXPECT_EQ(0, audioDevice->StereoPlayoutIsAvailable(&available));
   1069             PRINT_STR(Stereo Playout, available);
   1070         } else
   1071         {
   1072             PRINT_STR(Stereo Playout, false);
   1073         }
   1074         EXPECT_EQ(0, audioDevice->SpeakerVolumeIsAvailable(&available));
   1075         PRINT_STR(Speaker Volume, available);
   1076         EXPECT_EQ(0, audioDevice->SpeakerMuteIsAvailable(&available));
   1077         PRINT_STR(Speaker Mute, available);
   1078     }
   1079 
   1080     // =========
   1081     // Recording
   1082 
   1083     nDevices = audioDevice->RecordingDevices();
   1084     EXPECT_TRUE(nDevices >= 0);
   1085 
   1086     TEST_LOG("\n");
   1087 #ifdef _WIN32
   1088     EXPECT_TRUE(audioDevice->SetRecordingDevice(
   1089         AudioDeviceModule::kDefaultCommunicationDevice) == 0);
   1090     PRINT_HEADING(Recording, kDefaultCommunicationDevice);
   1091     EXPECT_EQ(0, audioDevice->RecordingIsAvailable(&available));
   1092     PRINT_STR(Recording, available);
   1093     if (available)
   1094     {
   1095         EXPECT_EQ(0, audioDevice->StereoRecordingIsAvailable(&available));
   1096         PRINT_STR(Stereo Recording, available);
   1097     }
   1098     else
   1099     {
   1100         // special fix to ensure that we don't log 'available' when recording is not OK
   1101         PRINT_STR(Stereo Recording, false);
   1102     }
   1103     EXPECT_EQ(0, audioDevice->MicrophoneVolumeIsAvailable(&available));
   1104     PRINT_STR(Microphone Volume, available);
   1105     EXPECT_EQ(0, audioDevice->MicrophoneMuteIsAvailable(&available));
   1106     PRINT_STR(Microphone Mute, available);
   1107     EXPECT_EQ(0, audioDevice->MicrophoneBoostIsAvailable(&available));
   1108     PRINT_STR(Microphone Boost, available);
   1109 
   1110     EXPECT_EQ(0, audioDevice->SetRecordingDevice(AudioDeviceModule::kDefaultDevice));
   1111     PRINT_HEADING(Recording, kDefaultDevice);
   1112     EXPECT_EQ(0, audioDevice->RecordingIsAvailable(&available));
   1113     PRINT_STR(Recording, available);
   1114     if (available)
   1115     {
   1116         EXPECT_EQ(0, audioDevice->StereoRecordingIsAvailable(&available));
   1117         PRINT_STR(Stereo Recording, available);
   1118     }
   1119     else
   1120     {
   1121         // special fix to ensure that we don't log 'available' when recording is not OK
   1122         PRINT_STR(Stereo Recording, false);
   1123     }
   1124     EXPECT_EQ(0, audioDevice->MicrophoneVolumeIsAvailable(&available));
   1125     PRINT_STR(Microphone Volume, available);
   1126     EXPECT_EQ(0, audioDevice->MicrophoneMuteIsAvailable(&available));
   1127     PRINT_STR(Microphone Mute, available);
   1128     EXPECT_EQ(0, audioDevice->MicrophoneBoostIsAvailable(&available));
   1129     PRINT_STR(Microphone Boost, available);
   1130 #else
   1131     EXPECT_TRUE(audioDevice->SetRecordingDevice(
   1132         AudioDeviceModule::kDefaultCommunicationDevice) == -1);
   1133     EXPECT_EQ(-1, audioDevice->SetRecordingDevice(AudioDeviceModule::kDefaultDevice));
   1134 #endif
   1135 
   1136     for (int i = 0; i < nDevices; i++)
   1137     {
   1138         EXPECT_EQ(0, audioDevice->SetRecordingDevice(i));
   1139         EXPECT_EQ(0, audioDevice->RecordingDeviceName(i, name, guid));
   1140         PRINT_HEADING_IDX(Recording, i, name);
   1141         EXPECT_EQ(0, audioDevice->RecordingIsAvailable(&available));
   1142         PRINT_STR(Recording, available);
   1143         if (available)
   1144         {
   1145             EXPECT_EQ(0, audioDevice->StereoRecordingIsAvailable(&available));
   1146             PRINT_STR(Stereo Recording, available);
   1147         } else
   1148         {
   1149             // special fix to ensure that we don't log 'available' when recording
   1150             // is not OK
   1151             PRINT_STR(Stereo Recording, false);
   1152         }
   1153         EXPECT_EQ(0, audioDevice->MicrophoneVolumeIsAvailable(&available));
   1154         PRINT_STR(Microphone Volume, available);
   1155         EXPECT_EQ(0, audioDevice->MicrophoneMuteIsAvailable(&available));
   1156         PRINT_STR(Microphone Mute, available);
   1157         EXPECT_EQ(0, audioDevice->MicrophoneBoostIsAvailable(&available));
   1158         PRINT_STR(Microphone Boost, available);
   1159     }
   1160 
   1161     EXPECT_EQ(0, audioDevice->Terminate());
   1162     EXPECT_FALSE(audioDevice->Initialized());
   1163 
   1164     PRINT_TEST_RESULTS;
   1165 
   1166     return 0;
   1167 }
   1168 
   1169 int32_t FuncTestManager::TestAudioTransport()
   1170 {
   1171     TEST_LOG("\n=======================================\n");
   1172     TEST_LOG(" Audio Transport test:\n");
   1173     TEST_LOG("=======================================\n");
   1174 
   1175     if (_audioDevice == NULL)
   1176     {
   1177         return -1;
   1178     }
   1179 
   1180     RESET_TEST;
   1181 
   1182     AudioDeviceModule* audioDevice = _audioDevice;
   1183 
   1184     EXPECT_EQ(0, audioDevice->Init());
   1185     EXPECT_TRUE(audioDevice->Initialized());
   1186 
   1187     bool recIsAvailable(false);
   1188     bool playIsAvailable(false);
   1189 
   1190     if (SelectRecordingDevice() == -1)
   1191     {
   1192         TEST_LOG("\nERROR: Device selection failed!\n \n");
   1193         return -1;
   1194     }
   1195 
   1196     EXPECT_EQ(0, audioDevice->RecordingIsAvailable(&recIsAvailable));
   1197     if (!recIsAvailable)
   1198     {
   1199         TEST_LOG(
   1200                  "\nWARNING: Recording is not available for the selected device!\n \n");
   1201     }
   1202 
   1203     if (SelectPlayoutDevice() == -1)
   1204     {
   1205         TEST_LOG("\nERROR: Device selection failed!\n \n");
   1206         return -1;
   1207     }
   1208 
   1209     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&playIsAvailable));
   1210     if (recIsAvailable && playIsAvailable)
   1211     {
   1212         _audioTransport->SetFullDuplex(true);
   1213     } else if (!playIsAvailable)
   1214     {
   1215         TEST_LOG(
   1216                  "\nWARNING: Playout is not available for the selected device!\n \n");
   1217     }
   1218 
   1219     bool available(false);
   1220     uint32_t samplesPerSec(0);
   1221 
   1222     if (playIsAvailable)
   1223     {
   1224         // =========================================
   1225         // Start by playing out an existing PCM file
   1226 
   1227         EXPECT_EQ(0, audioDevice->SpeakerVolumeIsAvailable(&available));
   1228         if (available)
   1229         {
   1230             uint32_t maxVolume(0);
   1231             EXPECT_EQ(0, audioDevice->MaxSpeakerVolume(&maxVolume));
   1232             EXPECT_EQ(0, audioDevice->SetSpeakerVolume(maxVolume/2));
   1233         }
   1234 
   1235         EXPECT_EQ(0, audioDevice->RegisterAudioCallback(_audioTransport));
   1236 
   1237         EXPECT_EQ(0, audioDevice->InitPlayout());
   1238         EXPECT_EQ(0, audioDevice->PlayoutSampleRate(&samplesPerSec));
   1239         if (samplesPerSec == 48000) {
   1240             _audioTransport->SetFilePlayout(
   1241                 true, GetResource(_playoutFile48.c_str()));
   1242         } else if (samplesPerSec == 44100 || samplesPerSec == 44000) {
   1243             _audioTransport->SetFilePlayout(
   1244                 true, GetResource(_playoutFile44.c_str()));
   1245         } else if (samplesPerSec == 16000) {
   1246             _audioTransport->SetFilePlayout(
   1247                 true, GetResource(_playoutFile16.c_str()));
   1248         } else if (samplesPerSec == 8000) {
   1249             _audioTransport->SetFilePlayout(
   1250                 true, GetResource(_playoutFile8.c_str()));
   1251         } else {
   1252             TEST_LOG("\nERROR: Sample rate (%u) is not supported!\n \n",
   1253                      samplesPerSec);
   1254             return -1;
   1255         }
   1256         EXPECT_EQ(0, audioDevice->StartPlayout());
   1257 
   1258         if (audioDevice->Playing())
   1259         {
   1260             TEST_LOG("\n> Listen to the file being played (fs=%d) out "
   1261                 "and verify that the audio quality is OK.\n"
   1262                 "> Press any key to stop playing...\n \n",
   1263                 samplesPerSec);
   1264             PAUSE(DEFAULT_PAUSE_TIME);
   1265         }
   1266 
   1267         EXPECT_EQ(0, audioDevice->StopPlayout());
   1268         EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   1269 
   1270         _audioTransport->SetFilePlayout(false);
   1271     }
   1272 
   1273     bool enabled(false);
   1274     if (recIsAvailable)
   1275     {
   1276         // ====================================
   1277         // Next, record from microphone to file
   1278 
   1279         EXPECT_EQ(0, audioDevice->MicrophoneVolumeIsAvailable(&available));
   1280         if (available)
   1281         {
   1282             uint32_t maxVolume(0);
   1283             EXPECT_EQ(0, audioDevice->MaxMicrophoneVolume(&maxVolume));
   1284             EXPECT_EQ(0, audioDevice->SetMicrophoneVolume(maxVolume));
   1285         }
   1286 
   1287         EXPECT_TRUE(audioDevice->StartRawInputFileRecording(
   1288             GetFilename(RecordedMicrophoneFile)) == 0);
   1289         EXPECT_EQ(0, audioDevice->RegisterAudioCallback(_audioTransport));
   1290 
   1291         EXPECT_EQ(0, audioDevice->InitRecording());
   1292         EXPECT_EQ(0, audioDevice->StereoRecording(&enabled));
   1293         if (enabled)
   1294         {
   1295             // ensure file recording in mono
   1296             EXPECT_EQ(0, audioDevice->SetRecordingChannel(AudioDeviceModule::kChannelLeft));
   1297         }
   1298         EXPECT_EQ(0, audioDevice->StartRecording());
   1299         SleepMs(100);
   1300 
   1301         EXPECT_TRUE(audioDevice->Recording());
   1302         if (audioDevice->Recording())
   1303         {
   1304             TEST_LOG("\n \n> The microphone input signal is now being recorded "
   1305                 "to a PCM file.\n"
   1306                 "> Speak into the microphone to ensure that your voice is"
   1307                 " recorded.\n> Press any key to stop recording...\n \n");
   1308             PAUSE(DEFAULT_PAUSE_TIME);
   1309         }
   1310 
   1311         EXPECT_EQ(0, audioDevice->StereoRecording(&enabled));
   1312         if (enabled)
   1313         {
   1314             EXPECT_EQ(0, audioDevice->SetRecordingChannel(AudioDeviceModule::kChannelBoth));
   1315         }
   1316         EXPECT_EQ(0, audioDevice->StopRecording());
   1317         EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   1318         EXPECT_EQ(0, audioDevice->StopRawInputFileRecording());
   1319     }
   1320 
   1321     if (recIsAvailable && playIsAvailable)
   1322     {
   1323         // ==========================
   1324         // Play out the recorded file
   1325 
   1326         _audioTransport->SetFilePlayout(true,
   1327                                         GetFilename(RecordedMicrophoneFile));
   1328 
   1329         EXPECT_EQ(0, audioDevice->RegisterAudioCallback(_audioTransport));
   1330         EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   1331         if (available)
   1332         {
   1333             EXPECT_EQ(0, audioDevice->InitPlayout());
   1334             EXPECT_EQ(0, audioDevice->StartPlayout());
   1335             SleepMs(100);
   1336         }
   1337 
   1338         EXPECT_TRUE(audioDevice->Playing());
   1339         if (audioDevice->Playing())
   1340         {
   1341             TEST_LOG("\n \n> Listen to the recorded file and verify that the "
   1342                 "audio quality is OK.\n"
   1343                 "> Press any key to stop listening...\n \n");
   1344             PAUSE(DEFAULT_PAUSE_TIME);
   1345         }
   1346 
   1347         EXPECT_EQ(0, audioDevice->StopPlayout());
   1348         EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   1349 
   1350         _audioTransport->SetFilePlayout(false);
   1351     }
   1352 
   1353     if (recIsAvailable && playIsAvailable)
   1354     {
   1355         // ==============================
   1356         // Finally, make full duplex test
   1357 
   1358         uint32_t playSamplesPerSec(0);
   1359         uint32_t recSamplesPerSecRec(0);
   1360 
   1361         EXPECT_EQ(0, audioDevice->RegisterAudioCallback(_audioTransport));
   1362 
   1363         _audioTransport->SetFullDuplex(true);
   1364 
   1365         EXPECT_EQ(0, audioDevice->MicrophoneVolumeIsAvailable(&available));
   1366         if (available)
   1367         {
   1368             uint32_t maxVolume(0);
   1369             EXPECT_EQ(0, audioDevice->MaxMicrophoneVolume(&maxVolume));
   1370             EXPECT_EQ(0, audioDevice->SetMicrophoneVolume(maxVolume));
   1371         }
   1372 
   1373         EXPECT_EQ(0, audioDevice->InitRecording());
   1374         EXPECT_EQ(0, audioDevice->InitPlayout());
   1375         EXPECT_EQ(0, audioDevice->PlayoutSampleRate(&playSamplesPerSec));
   1376         EXPECT_EQ(0, audioDevice->RecordingSampleRate(&recSamplesPerSecRec));
   1377         if (playSamplesPerSec != recSamplesPerSecRec)
   1378         {
   1379             TEST_LOG("\nERROR: sample rates does not match (fs_play=%u, fs_rec=%u)",
   1380                      playSamplesPerSec, recSamplesPerSecRec);
   1381             EXPECT_EQ(0, audioDevice->StopRecording());
   1382             EXPECT_EQ(0, audioDevice->StopPlayout());
   1383             EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   1384             _audioTransport->SetFullDuplex(false);
   1385             return -1;
   1386         }
   1387 
   1388         EXPECT_EQ(0, audioDevice->StartRecording());
   1389         EXPECT_EQ(0, audioDevice->StartPlayout());
   1390         SleepMs(100);
   1391 
   1392         if (audioDevice->Playing() && audioDevice->Recording())
   1393         {
   1394             TEST_LOG("\n \n> Full duplex audio (fs=%u) is now active.\n"
   1395                 "> Speak into the microphone and verify that your voice is "
   1396                 "played out in loopback.\n> Press any key to stop...\n \n",
   1397                      playSamplesPerSec);
   1398             PAUSE(DEFAULT_PAUSE_TIME);
   1399         }
   1400 
   1401         EXPECT_EQ(0, audioDevice->StopRecording());
   1402         EXPECT_EQ(0, audioDevice->StopPlayout());
   1403         EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   1404 
   1405         _audioTransport->SetFullDuplex(false);
   1406     }
   1407 
   1408     EXPECT_EQ(0, audioDevice->Terminate());
   1409     EXPECT_FALSE(audioDevice->Initialized());
   1410 
   1411     TEST_LOG("\n");
   1412     PRINT_TEST_RESULTS;
   1413 
   1414     return 0;
   1415 }
   1416 
   1417 int32_t FuncTestManager::TestSpeakerVolume()
   1418 {
   1419     TEST_LOG("\n=======================================\n");
   1420     TEST_LOG(" Speaker Volume test:\n");
   1421     TEST_LOG("=======================================\n");
   1422 
   1423     if (_audioDevice == NULL)
   1424     {
   1425         return -1;
   1426     }
   1427 
   1428     RESET_TEST;
   1429 
   1430     AudioDeviceModule* audioDevice = _audioDevice;
   1431 
   1432     EXPECT_EQ(0, audioDevice->Init());
   1433     EXPECT_TRUE(audioDevice->Initialized());
   1434 
   1435     if (SelectPlayoutDevice() == -1)
   1436     {
   1437         TEST_LOG("\nERROR: Device selection failed!\n \n");
   1438         return -1;
   1439     }
   1440 
   1441     bool available(false);
   1442     uint32_t startVolume(0);
   1443     uint32_t samplesPerSec(0);
   1444 
   1445     EXPECT_EQ(0, audioDevice->SpeakerVolumeIsAvailable(&available));
   1446     if (available)
   1447     {
   1448         _audioTransport->SetSpeakerVolume(true);
   1449     } else
   1450     {
   1451         TEST_LOG("\nERROR: Volume control is not available for the selected "
   1452             "device!\n \n");
   1453         return -1;
   1454     }
   1455 
   1456     // store initial volume setting
   1457     EXPECT_EQ(0, audioDevice->InitSpeaker());
   1458     EXPECT_EQ(0, audioDevice->SpeakerVolume(&startVolume));
   1459 
   1460     // start at volume 0
   1461     EXPECT_EQ(0, audioDevice->SetSpeakerVolume(0));
   1462 
   1463     // ======================================
   1464     // Start playing out an existing PCM file
   1465 
   1466     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(_audioTransport));
   1467     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   1468     if (available)
   1469     {
   1470         EXPECT_EQ(0, audioDevice->InitPlayout());
   1471         EXPECT_EQ(0, audioDevice->PlayoutSampleRate(&samplesPerSec));
   1472         if (48000 == samplesPerSec) {
   1473             _audioTransport->SetFilePlayout(
   1474                 true, GetResource(_playoutFile48.c_str()));
   1475         } else if (44100 == samplesPerSec || samplesPerSec == 44000) {
   1476             _audioTransport->SetFilePlayout(
   1477                 true, GetResource(_playoutFile44.c_str()));
   1478         } else if (samplesPerSec == 16000) {
   1479             _audioTransport->SetFilePlayout(
   1480                 true, GetResource(_playoutFile16.c_str()));
   1481         } else if (samplesPerSec == 8000) {
   1482             _audioTransport->SetFilePlayout(
   1483                 true, GetResource(_playoutFile8.c_str()));
   1484         } else {
   1485             TEST_LOG("\nERROR: Sample rate (%d) is not supported!\n \n",
   1486                      samplesPerSec);
   1487             return -1;
   1488         }
   1489         EXPECT_EQ(0, audioDevice->StartPlayout());
   1490     }
   1491 
   1492     EXPECT_TRUE(audioDevice->Playing());
   1493     if (audioDevice->Playing())
   1494     {
   1495         TEST_LOG("\n> Listen to the file being played out and verify that the "
   1496             "selected speaker volume is varied between [~0] and [~MAX].\n"
   1497             "> The file shall be played out with an increasing volume level "
   1498             "correlated to the speaker volume.\n"
   1499             "> Press any key to stop playing...\n \n");
   1500         PAUSE(10000);
   1501     }
   1502 
   1503     EXPECT_EQ(0, audioDevice->StopPlayout());
   1504     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   1505 
   1506     _audioTransport->SetSpeakerVolume(false);
   1507     _audioTransport->SetFilePlayout(false);
   1508 
   1509     // restore volume setting
   1510     EXPECT_EQ(0, audioDevice->SetSpeakerVolume(startVolume));
   1511 
   1512     TEST_LOG("\n");
   1513     PRINT_TEST_RESULTS;
   1514 
   1515     return 0;
   1516 }
   1517 
   1518 int32_t FuncTestManager::TestSpeakerMute()
   1519 {
   1520     TEST_LOG("\n=======================================\n");
   1521     TEST_LOG(" Speaker Mute test:\n");
   1522     TEST_LOG("=======================================\n");
   1523 
   1524     if (_audioDevice == NULL)
   1525     {
   1526         return -1;
   1527     }
   1528 
   1529     RESET_TEST;
   1530 
   1531     AudioDeviceModule* audioDevice = _audioDevice;
   1532 
   1533     EXPECT_EQ(0, audioDevice->Init());
   1534     EXPECT_TRUE(audioDevice->Initialized());
   1535 
   1536     if (SelectPlayoutDevice() == -1)
   1537     {
   1538         TEST_LOG("\nERROR: Device selection failed!\n \n");
   1539         return -1;
   1540     }
   1541 
   1542     bool available(false);
   1543     bool startMute(false);
   1544     uint32_t samplesPerSec(0);
   1545 
   1546     EXPECT_EQ(0, audioDevice->SpeakerMuteIsAvailable(&available));
   1547     if (available)
   1548     {
   1549         _audioTransport->SetSpeakerMute(true);
   1550     } else
   1551     {
   1552         TEST_LOG(
   1553                  "\nERROR: Mute control is not available for the selected"
   1554                  " device!\n \n");
   1555         return -1;
   1556     }
   1557 
   1558     // store initial mute setting
   1559     EXPECT_EQ(0, audioDevice->InitSpeaker());
   1560     EXPECT_EQ(0, audioDevice->SpeakerMute(&startMute));
   1561 
   1562     // start with no mute
   1563     EXPECT_EQ(0, audioDevice->SetSpeakerMute(false));
   1564 
   1565     // ======================================
   1566     // Start playing out an existing PCM file
   1567 
   1568     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(_audioTransport));
   1569     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   1570     if (available)
   1571     {
   1572         EXPECT_EQ(0, audioDevice->InitPlayout());
   1573         EXPECT_EQ(0, audioDevice->PlayoutSampleRate(&samplesPerSec));
   1574         if (48000 == samplesPerSec)
   1575             _audioTransport->SetFilePlayout(true, _playoutFile48.c_str());
   1576         else if (44100 == samplesPerSec || 44000 == samplesPerSec)
   1577             _audioTransport->SetFilePlayout(true, _playoutFile44.c_str());
   1578         else
   1579         {
   1580             TEST_LOG("\nERROR: Sample rate (%d) is not supported!\n \n",
   1581                      samplesPerSec);
   1582             return -1;
   1583         }
   1584         EXPECT_EQ(0, audioDevice->StartPlayout());
   1585     }
   1586 
   1587     EXPECT_TRUE(audioDevice->Playing());
   1588     if (audioDevice->Playing())
   1589     {
   1590         TEST_LOG("\n> Listen to the file being played out and verify that the"
   1591             " selected speaker mute control is toggled between [MUTE ON] and"
   1592             " [MUTE OFF].\n> You should only hear the file during the"
   1593             " 'MUTE OFF' periods.\n"
   1594             "> Press any key to stop playing...\n \n");
   1595         PAUSE(DEFAULT_PAUSE_TIME);
   1596     }
   1597 
   1598     EXPECT_EQ(0, audioDevice->StopPlayout());
   1599     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   1600 
   1601     _audioTransport->SetSpeakerMute(false);
   1602     _audioTransport->SetFilePlayout(false);
   1603 
   1604     // restore mute setting
   1605     EXPECT_EQ(0, audioDevice->SetSpeakerMute(startMute));
   1606 
   1607     TEST_LOG("\n");
   1608     PRINT_TEST_RESULTS;
   1609 
   1610     return 0;
   1611 }
   1612 
   1613 int32_t FuncTestManager::TestMicrophoneVolume()
   1614 {
   1615     TEST_LOG("\n=======================================\n");
   1616     TEST_LOG(" Microphone Volume test:\n");
   1617     TEST_LOG("=======================================\n");
   1618 
   1619     if (_audioDevice == NULL)
   1620     {
   1621         return -1;
   1622     }
   1623 
   1624     RESET_TEST;
   1625 
   1626     AudioDeviceModule* audioDevice = _audioDevice;
   1627 
   1628     EXPECT_EQ(0, audioDevice->Init());
   1629     EXPECT_TRUE(audioDevice->Initialized());
   1630 
   1631     if (SelectRecordingDevice() == -1)
   1632     {
   1633         TEST_LOG("\nERROR: Device selection failed!\n \n");
   1634         return -1;
   1635     }
   1636 
   1637     bool available(false);
   1638     EXPECT_EQ(0, audioDevice->MicrophoneVolumeIsAvailable(&available));
   1639     if (available)
   1640     {
   1641         _audioTransport->SetMicrophoneVolume(true);
   1642     } else
   1643     {
   1644         TEST_LOG("\nERROR: Volume control is not available for the selected "
   1645             "device!\n \n");
   1646         return -1;
   1647     }
   1648 
   1649     if (SelectPlayoutDevice() == -1)
   1650     {
   1651         TEST_LOG("\nERROR: Device selection failed!\n \n");
   1652         return -1;
   1653     }
   1654 
   1655     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   1656     if (available)
   1657     {
   1658         _audioTransport->SetFullDuplex(true);
   1659     } else
   1660     {
   1661         TEST_LOG("\nERROR: Playout is not available for the selected "
   1662             "device!\n \n");
   1663         return -1;
   1664     }
   1665 
   1666     TEST_LOG("\nEnable recording of microphone input to file (%s) during this"
   1667         " test (Y/N)?\n: ",
   1668              RecordedMicrophoneVolumeFile);
   1669     char ch;
   1670     bool fileRecording(false);
   1671     EXPECT_TRUE(scanf(" %c", &ch) > 0);
   1672     ch = toupper(ch);
   1673     if (ch == 'Y')
   1674     {
   1675         fileRecording = true;
   1676     }
   1677 
   1678     uint32_t startVolume(0);
   1679     bool enabled(false);
   1680 
   1681     // store initial volume setting
   1682     EXPECT_EQ(0, audioDevice->InitMicrophone());
   1683     EXPECT_EQ(0, audioDevice->MicrophoneVolume(&startVolume));
   1684 
   1685     // start at volume 0
   1686     EXPECT_EQ(0, audioDevice->SetMicrophoneVolume(0));
   1687 
   1688     // ======================================================================
   1689     // Start recording from the microphone while the mic volume is changed
   1690     // continuously.
   1691     // Also, start playing out the input to enable real-time verification.
   1692 
   1693     if (fileRecording)
   1694     {
   1695         EXPECT_EQ(0, audioDevice->StartRawInputFileRecording(RecordedMicrophoneVolumeFile));
   1696     }
   1697     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(_audioTransport));
   1698     EXPECT_EQ(0, audioDevice->RecordingIsAvailable(&available));
   1699     if (available)
   1700     {
   1701         EXPECT_EQ(0, audioDevice->InitRecording());
   1702         EXPECT_EQ(0, audioDevice->StereoRecording(&enabled));
   1703         if (enabled)
   1704         {
   1705             // ensures a mono file
   1706             EXPECT_EQ(0, audioDevice->SetRecordingChannel(AudioDeviceModule::kChannelRight));
   1707         }
   1708         EXPECT_EQ(0, audioDevice->StartRecording());
   1709     }
   1710     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   1711     if (available)
   1712     {
   1713         EXPECT_EQ(0, audioDevice->InitPlayout());
   1714         EXPECT_EQ(0, audioDevice->StartPlayout());
   1715     }
   1716 
   1717     EXPECT_TRUE(audioDevice->Recording());
   1718     EXPECT_TRUE(audioDevice->Playing());
   1719     if (audioDevice->Recording() && audioDevice->Playing())
   1720     {
   1721         TEST_LOG("\n> Speak into the microphone and verify that the selected "
   1722             "microphone volume is varied between [~0] and [~MAX].\n"
   1723             "> You should hear your own voice with an increasing volume level"
   1724             " correlated to the microphone volume.\n"
   1725             "> After a finalized test (and if file recording was enabled) "
   1726             "verify the recorded result off line.\n"
   1727             "> Press any key to stop...\n \n");
   1728         PAUSE(DEFAULT_PAUSE_TIME);
   1729     }
   1730 
   1731     if (fileRecording)
   1732     {
   1733         EXPECT_EQ(0, audioDevice->StopRawInputFileRecording());
   1734     }
   1735     EXPECT_EQ(0, audioDevice->StopRecording());
   1736     EXPECT_EQ(0, audioDevice->StopPlayout());
   1737     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   1738     EXPECT_EQ(0, audioDevice->StereoRecordingIsAvailable(&available));
   1739 
   1740     _audioTransport->SetMicrophoneVolume(false);
   1741     _audioTransport->SetFullDuplex(false);
   1742 
   1743     // restore volume setting
   1744     EXPECT_EQ(0, audioDevice->SetMicrophoneVolume(startVolume));
   1745 
   1746     TEST_LOG("\n");
   1747     PRINT_TEST_RESULTS;
   1748 
   1749     return 0;
   1750 }
   1751 
   1752 int32_t FuncTestManager::TestMicrophoneMute()
   1753 {
   1754     TEST_LOG("\n=======================================\n");
   1755     TEST_LOG(" Microphone Mute test:\n");
   1756     TEST_LOG("=======================================\n");
   1757 
   1758     if (_audioDevice == NULL)
   1759     {
   1760         return -1;
   1761     }
   1762 
   1763     RESET_TEST;
   1764 
   1765     AudioDeviceModule* audioDevice = _audioDevice;
   1766 
   1767     EXPECT_EQ(0, audioDevice->Init());
   1768     EXPECT_TRUE(audioDevice->Initialized());
   1769 
   1770     if (SelectRecordingDevice() == -1)
   1771     {
   1772         TEST_LOG("\nERROR: Device selection failed!\n \n");
   1773         return -1;
   1774     }
   1775 
   1776     bool available(false);
   1777     EXPECT_EQ(0, audioDevice->MicrophoneMuteIsAvailable(&available));
   1778     if (available)
   1779     {
   1780         _audioTransport->SetMicrophoneMute(true);
   1781     } else
   1782     {
   1783         TEST_LOG("\nERROR: Mute control is not available for the selected"
   1784             " device!\n \n");
   1785         return -1;
   1786     }
   1787 
   1788     if (SelectPlayoutDevice() == -1)
   1789     {
   1790         TEST_LOG("\nERROR: Device selection failed!\n \n");
   1791         return -1;
   1792     }
   1793 
   1794     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   1795     if (available)
   1796     {
   1797         _audioTransport->SetFullDuplex(true);
   1798     } else
   1799     {
   1800         TEST_LOG("\nERROR: Playout is not available for the selected "
   1801             "device!\n \n");
   1802         return -1;
   1803     }
   1804 
   1805     TEST_LOG("\nEnable recording of microphone input to file (%s) during this "
   1806         "test (Y/N)?\n: ",
   1807         RecordedMicrophoneMuteFile);
   1808     char ch;
   1809     bool fileRecording(false);
   1810     EXPECT_TRUE(scanf(" %c", &ch) > 0);
   1811     ch = toupper(ch);
   1812     if (ch == 'Y')
   1813     {
   1814         fileRecording = true;
   1815     }
   1816 
   1817     bool startMute(false);
   1818     bool enabled(false);
   1819 
   1820     // store initial volume setting
   1821     EXPECT_EQ(0, audioDevice->InitMicrophone());
   1822     EXPECT_EQ(0, audioDevice->MicrophoneMute(&startMute));
   1823 
   1824     // start at no mute
   1825     EXPECT_EQ(0, audioDevice->SetMicrophoneMute(false));
   1826 
   1827     // ==================================================================
   1828     // Start recording from the microphone while the mic mute is toggled
   1829     // continuously.
   1830     // Also, start playing out the input to enable real-time verification.
   1831 
   1832     if (fileRecording)
   1833     {
   1834         EXPECT_EQ(0, audioDevice->StartRawInputFileRecording(RecordedMicrophoneMuteFile));
   1835     }
   1836     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(_audioTransport));
   1837     EXPECT_EQ(0, audioDevice->RecordingIsAvailable(&available));
   1838     if (available)
   1839     {
   1840         EXPECT_EQ(0, audioDevice->InitRecording());
   1841         EXPECT_EQ(0, audioDevice->StereoRecording(&enabled));
   1842         if (enabled)
   1843         {
   1844             // ensure file recording in mono
   1845             EXPECT_EQ(0, audioDevice->SetRecordingChannel(AudioDeviceModule::kChannelLeft));
   1846         }
   1847         EXPECT_EQ(0, audioDevice->StartRecording());
   1848     }
   1849     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   1850     if (available)
   1851     {
   1852         EXPECT_EQ(0, audioDevice->InitPlayout());
   1853         EXPECT_EQ(0, audioDevice->StartPlayout());
   1854     }
   1855 
   1856     EXPECT_TRUE(audioDevice->Recording());
   1857     EXPECT_TRUE(audioDevice->Playing());
   1858     if (audioDevice->Recording() && audioDevice->Playing())
   1859     {
   1860         TEST_LOG("\n> Speak into the microphone and verify that the selected "
   1861             "microphone mute control is toggled between [MUTE ON] and [MUTE OFF]."
   1862             "\n> You should only hear your own voice in loopback during the"
   1863             " 'MUTE OFF' periods.\n> After a finalized test (and if file "
   1864             "recording was enabled) verify the recorded result off line.\n"
   1865             "> Press any key to stop...\n \n");
   1866         PAUSE(DEFAULT_PAUSE_TIME);
   1867     }
   1868 
   1869     if (fileRecording)
   1870     {
   1871         EXPECT_EQ(0, audioDevice->StopRawInputFileRecording());
   1872     }
   1873     EXPECT_EQ(0, audioDevice->StopRecording());
   1874     EXPECT_EQ(0, audioDevice->StopPlayout());
   1875     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   1876 
   1877     _audioTransport->SetMicrophoneMute(false);
   1878     _audioTransport->SetFullDuplex(false);
   1879 
   1880     // restore volume setting
   1881     EXPECT_EQ(0, audioDevice->SetMicrophoneMute(startMute));
   1882 
   1883     TEST_LOG("\n");
   1884     PRINT_TEST_RESULTS;
   1885 
   1886     return 0;
   1887 }
   1888 
   1889 int32_t FuncTestManager::TestMicrophoneBoost()
   1890 {
   1891     TEST_LOG("\n=======================================\n");
   1892     TEST_LOG(" Microphone Boost test:\n");
   1893     TEST_LOG("=======================================\n");
   1894 
   1895     if (_audioDevice == NULL)
   1896     {
   1897         return -1;
   1898     }
   1899 
   1900     RESET_TEST;
   1901 
   1902     AudioDeviceModule* audioDevice = _audioDevice;
   1903 
   1904     EXPECT_EQ(0, audioDevice->Init());
   1905     EXPECT_TRUE(audioDevice->Initialized());
   1906 
   1907     if (SelectRecordingDevice() == -1)
   1908     {
   1909         TEST_LOG("\nERROR: Device selection failed!\n \n");
   1910         return -1;
   1911     }
   1912 
   1913     bool available(false);
   1914     EXPECT_EQ(0, audioDevice->MicrophoneBoostIsAvailable(&available));
   1915     if (available)
   1916     {
   1917         _audioTransport->SetMicrophoneBoost(true);
   1918     } else
   1919     {
   1920         TEST_LOG(
   1921                  "\nERROR: Boost control is not available for the selected device!\n \n");
   1922         return -1;
   1923     }
   1924 
   1925     if (SelectPlayoutDevice() == -1)
   1926     {
   1927         TEST_LOG("\nERROR: Device selection failed!\n \n");
   1928         return -1;
   1929     }
   1930 
   1931     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   1932     if (available)
   1933     {
   1934         _audioTransport->SetFullDuplex(true);
   1935     } else
   1936     {
   1937         TEST_LOG("\nERROR: Playout is not available for the selected device!\n \n");
   1938         return -1;
   1939     }
   1940 
   1941     TEST_LOG("\nEnable recording of microphone input to file (%s) during this "
   1942         "test (Y/N)?\n: ",
   1943         RecordedMicrophoneBoostFile);
   1944     char ch;
   1945     bool fileRecording(false);
   1946     EXPECT_TRUE(scanf(" %c", &ch) > 0);
   1947     ch = toupper(ch);
   1948     if (ch == 'Y')
   1949     {
   1950         fileRecording = true;
   1951     }
   1952 
   1953     bool startBoost(false);
   1954     bool enabled(false);
   1955 
   1956     // store initial volume setting
   1957     EXPECT_EQ(0, audioDevice->InitMicrophone());
   1958     EXPECT_EQ(0, audioDevice->MicrophoneBoost(&startBoost));
   1959 
   1960     // start at no boost
   1961     EXPECT_EQ(0, audioDevice->SetMicrophoneBoost(false));
   1962 
   1963     // ==================================================================
   1964     // Start recording from the microphone while the mic boost is toggled
   1965     // continuously.
   1966     // Also, start playing out the input to enable real-time verification.
   1967 
   1968     if (fileRecording)
   1969     {
   1970         EXPECT_EQ(0, audioDevice->StartRawInputFileRecording(RecordedMicrophoneBoostFile));
   1971     }
   1972     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(_audioTransport));
   1973     EXPECT_EQ(0, audioDevice->RecordingIsAvailable(&available));
   1974     if (available)
   1975     {
   1976         EXPECT_EQ(0, audioDevice->InitRecording());
   1977         EXPECT_EQ(0, audioDevice->StereoRecording(&enabled));
   1978         if (enabled)
   1979         {
   1980             // ensure file recording in mono
   1981             EXPECT_EQ(0, audioDevice->SetRecordingChannel(AudioDeviceModule::kChannelLeft));
   1982         }
   1983         EXPECT_EQ(0, audioDevice->StartRecording());
   1984     }
   1985     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   1986     if (available)
   1987     {
   1988         EXPECT_EQ(0, audioDevice->InitPlayout());
   1989         EXPECT_EQ(0, audioDevice->StartPlayout());
   1990     }
   1991 
   1992     EXPECT_TRUE(audioDevice->Recording());
   1993     EXPECT_TRUE(audioDevice->Playing());
   1994     if (audioDevice->Recording() && audioDevice->Playing())
   1995     {
   1996         TEST_LOG("\n> Speak into the microphone and verify that the selected "
   1997             "microphone boost control is toggled between [BOOST ON] and [BOOST OFF].\n"
   1998             "> You should hear your own voice with an increased volume level "
   1999             "during the 'BOOST ON' periods.\n \n"
   2000             "> After a finalized test (and if file recording was enabled) verify"
   2001             " the recorded result off line.\n"
   2002         "> Press any key to stop...\n \n");
   2003         PAUSE(DEFAULT_PAUSE_TIME);
   2004     }
   2005 
   2006     if (fileRecording)
   2007     {
   2008         EXPECT_EQ(0, audioDevice->StopRawInputFileRecording());
   2009     }
   2010     EXPECT_EQ(0, audioDevice->StopRecording());
   2011     EXPECT_EQ(0, audioDevice->StopPlayout());
   2012     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   2013 
   2014     _audioTransport->SetMicrophoneBoost(false);
   2015     _audioTransport->SetFullDuplex(false);
   2016 
   2017     // restore boost setting
   2018     EXPECT_EQ(0, audioDevice->SetMicrophoneBoost(startBoost));
   2019 
   2020     TEST_LOG("\n");
   2021     PRINT_TEST_RESULTS;
   2022 
   2023     return 0;
   2024 }
   2025 
   2026 int32_t FuncTestManager::TestMicrophoneAGC()
   2027 {
   2028     TEST_LOG("\n=======================================\n");
   2029     TEST_LOG(" Microphone AGC test:\n");
   2030     TEST_LOG("=======================================\n");
   2031 
   2032     if (_audioDevice == NULL)
   2033     {
   2034         return -1;
   2035     }
   2036 
   2037     RESET_TEST;
   2038 
   2039     AudioDeviceModule* audioDevice = _audioDevice;
   2040 
   2041     EXPECT_EQ(0, audioDevice->Init());
   2042     EXPECT_TRUE(audioDevice->Initialized());
   2043 
   2044     if (SelectRecordingDevice() == -1)
   2045     {
   2046         TEST_LOG("\nERROR: Device selection failed!\n \n");
   2047         return -1;
   2048     }
   2049 
   2050     bool available(false);
   2051     EXPECT_EQ(0, audioDevice->MicrophoneVolumeIsAvailable(&available));
   2052     if (available)
   2053     {
   2054         _audioTransport->SetMicrophoneAGC(true);
   2055     } else
   2056     {
   2057         TEST_LOG("\nERROR: It is not possible to control the microphone volume"
   2058             " for the selected device!\n \n");
   2059         return -1;
   2060     }
   2061 
   2062     if (SelectPlayoutDevice() == -1)
   2063     {
   2064         TEST_LOG("\nERROR: Device selection failed!\n \n");
   2065         return -1;
   2066     }
   2067 
   2068     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   2069     if (available)
   2070     {
   2071         _audioTransport->SetFullDuplex(true);
   2072     } else
   2073     {
   2074         TEST_LOG("\nERROR: Playout is not available for the selected device!\n \n");
   2075         return -1;
   2076     }
   2077 
   2078     TEST_LOG("\nEnable recording of microphone input to file (%s) during "
   2079         "this test (Y/N)?\n: ",
   2080         RecordedMicrophoneAGCFile);
   2081     char ch;
   2082     bool fileRecording(false);
   2083     EXPECT_TRUE(scanf(" %c", &ch) > 0);
   2084     ch = toupper(ch);
   2085     if (ch == 'Y')
   2086     {
   2087         fileRecording = true;
   2088     }
   2089 
   2090     uint32_t startVolume(0);
   2091     bool enabled(false);
   2092 
   2093     // store initial volume setting
   2094     EXPECT_EQ(0, audioDevice->InitMicrophone());
   2095     EXPECT_EQ(0, audioDevice->MicrophoneVolume(&startVolume));
   2096 
   2097     // ====================================================================
   2098     // Start recording from the microphone while the mic volume is changed
   2099     // continuously
   2100     // by the emulated AGC (implemented by our audio transport).
   2101     // Also, start playing out the input to enable real-time verification.
   2102 
   2103     if (fileRecording)
   2104     {
   2105         EXPECT_EQ(0, audioDevice->StartRawInputFileRecording(RecordedMicrophoneAGCFile));
   2106     }
   2107     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(_audioTransport));
   2108     EXPECT_EQ(0, audioDevice->RecordingIsAvailable(&available));
   2109     if (available)
   2110     {
   2111         EXPECT_EQ(0, audioDevice->SetAGC(true));
   2112         EXPECT_EQ(0, audioDevice->InitRecording());
   2113         EXPECT_EQ(0, audioDevice->StereoRecording(&enabled));
   2114         if (enabled)
   2115         {
   2116             // ensures a mono file
   2117             EXPECT_EQ(0, audioDevice->SetRecordingChannel(AudioDeviceModule::kChannelRight));
   2118         }
   2119         EXPECT_EQ(0, audioDevice->StartRecording());
   2120     }
   2121     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
   2122     if (available)
   2123     {
   2124         EXPECT_EQ(0, audioDevice->InitPlayout());
   2125         EXPECT_EQ(0, audioDevice->StartPlayout());
   2126     }
   2127 
   2128     EXPECT_TRUE(audioDevice->AGC());
   2129     EXPECT_TRUE(audioDevice->Recording());
   2130     EXPECT_TRUE(audioDevice->Playing());
   2131     if (audioDevice->Recording() && audioDevice->Playing())
   2132     {
   2133         TEST_LOG("\n> Speak into the microphone and verify that the volume of"
   2134             " the selected microphone is varied between [~0] and [~MAX].\n"
   2135             "> You should hear your own voice with an increasing volume level"
   2136             " correlated to an emulated AGC setting.\n"
   2137             "> After a finalized test (and if file recording was enabled) verify"
   2138             " the recorded result off line.\n"
   2139             "> Press any key to stop...\n \n");
   2140         PAUSE(DEFAULT_PAUSE_TIME);
   2141     }
   2142 
   2143     if (fileRecording)
   2144     {
   2145         EXPECT_EQ(0, audioDevice->StopRawInputFileRecording());
   2146     }
   2147     EXPECT_EQ(0, audioDevice->SetAGC(false));
   2148     EXPECT_EQ(0, audioDevice->StopRecording());
   2149     EXPECT_EQ(0, audioDevice->StopPlayout());
   2150     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   2151     EXPECT_EQ(0, audioDevice->StereoRecordingIsAvailable(&available));
   2152 
   2153     _audioTransport->SetMicrophoneAGC(false);
   2154     _audioTransport->SetFullDuplex(false);
   2155 
   2156     // restore volume setting
   2157     EXPECT_EQ(0, audioDevice->SetMicrophoneVolume(startVolume));
   2158 
   2159     TEST_LOG("\n");
   2160     PRINT_TEST_RESULTS;
   2161 
   2162     return 0;
   2163 }
   2164 
   2165 int32_t FuncTestManager::TestLoopback()
   2166 {
   2167     TEST_LOG("\n=======================================\n");
   2168     TEST_LOG(" Loopback measurement test:\n");
   2169     TEST_LOG("=======================================\n");
   2170 
   2171     if (_audioDevice == NULL)
   2172     {
   2173         return -1;
   2174     }
   2175 
   2176     RESET_TEST;
   2177 
   2178     AudioDeviceModule* audioDevice = _audioDevice;
   2179 
   2180     EXPECT_EQ(0, audioDevice->Init());
   2181     EXPECT_TRUE(audioDevice->Initialized());
   2182 
   2183     bool recIsAvailable(false);
   2184     bool playIsAvailable(false);
   2185     uint8_t nPlayChannels(0);
   2186     uint8_t nRecChannels(0);
   2187 
   2188     if (SelectRecordingDevice() == -1)
   2189     {
   2190         TEST_LOG("\nERROR: Device selection failed!\n \n");
   2191         return -1;
   2192     }
   2193 
   2194     EXPECT_EQ(0, audioDevice->RecordingIsAvailable(&recIsAvailable));
   2195     if (!recIsAvailable)
   2196     {
   2197         TEST_LOG("\nERROR: Recording is not available for the selected device!\n \n");
   2198         return -1;
   2199     }
   2200 
   2201     if (SelectPlayoutDevice() == -1)
   2202     {
   2203         TEST_LOG("\nERROR: Device selection failed!\n \n");
   2204         return -1;
   2205     }
   2206 
   2207     EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&playIsAvailable));
   2208     if (recIsAvailable && playIsAvailable)
   2209     {
   2210         _audioTransport->SetFullDuplex(true);
   2211         _audioTransport->SetLoopbackMeasurements(true);
   2212     } else if (!playIsAvailable)
   2213     {
   2214         TEST_LOG("\nERROR: Playout is not available for the selected device!\n \n");
   2215         return -1;
   2216     }
   2217 
   2218     bool enabled(false);
   2219     bool available(false);
   2220 
   2221     if (recIsAvailable && playIsAvailable)
   2222     {
   2223         uint32_t playSamplesPerSec(0);
   2224         uint32_t recSamplesPerSecRec(0);
   2225 
   2226         EXPECT_EQ(0, audioDevice->RegisterAudioCallback(_audioTransport));
   2227 
   2228         _audioTransport->SetFullDuplex(true);
   2229 
   2230         EXPECT_EQ(0, audioDevice->StereoRecordingIsAvailable(&available));
   2231         if (available)
   2232         {
   2233             EXPECT_EQ(0, audioDevice->SetStereoRecording(true));
   2234         }
   2235 
   2236         EXPECT_EQ(0, audioDevice->StereoPlayoutIsAvailable(&available));
   2237         if (available)
   2238         {
   2239             EXPECT_EQ(0, audioDevice->SetStereoPlayout(true));
   2240         }
   2241 
   2242         EXPECT_EQ(0, audioDevice->MicrophoneVolumeIsAvailable(&available));
   2243         if (available)
   2244         {
   2245             uint32_t maxVolume(0);
   2246             EXPECT_EQ(0, audioDevice->MaxMicrophoneVolume(&maxVolume));
   2247             EXPECT_EQ(0, audioDevice->SetMicrophoneVolume(maxVolume));
   2248         }
   2249 
   2250         EXPECT_EQ(0, audioDevice->InitRecording());
   2251         EXPECT_EQ(0, audioDevice->InitPlayout());
   2252         EXPECT_EQ(0, audioDevice->PlayoutSampleRate(&playSamplesPerSec));
   2253         EXPECT_EQ(0, audioDevice->RecordingSampleRate(&recSamplesPerSecRec));
   2254         EXPECT_EQ(0, audioDevice->StereoPlayout(&enabled));
   2255         enabled ? nPlayChannels = 2 : nPlayChannels = 1;
   2256         EXPECT_EQ(0, audioDevice->StereoRecording(&enabled));
   2257         enabled ? nRecChannels = 2 : nRecChannels = 1;
   2258         EXPECT_EQ(0, audioDevice->StartRecording());
   2259         EXPECT_EQ(0, audioDevice->StartPlayout());
   2260 
   2261         if (audioDevice->Playing() && audioDevice->Recording())
   2262         {
   2263             TEST_LOG("\n \n> Loopback audio is now active.\n"
   2264                "> Rec : fs=%u, #channels=%u.\n"
   2265                 "> Play: fs=%u, #channels=%u.\n"
   2266                 "> Speak into the microphone and verify that your voice is"
   2267                 "  played out in loopback.\n"
   2268                 "> Press any key to stop...\n \n",
   2269                 recSamplesPerSecRec, nRecChannels, playSamplesPerSec,
   2270                 nPlayChannels);
   2271             PAUSE(30000);
   2272         }
   2273 
   2274         EXPECT_EQ(0, audioDevice->StopRecording());
   2275         EXPECT_EQ(0, audioDevice->StopPlayout());
   2276         EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   2277 
   2278         _audioTransport->SetFullDuplex(false);
   2279         _audioTransport->SetLoopbackMeasurements(false);
   2280     }
   2281 
   2282     EXPECT_EQ(0, audioDevice->Terminate());
   2283     EXPECT_FALSE(audioDevice->Initialized());
   2284 
   2285     TEST_LOG("\n");
   2286     PRINT_TEST_RESULTS;
   2287 
   2288     return 0;
   2289 }
   2290 
   2291 int32_t FuncTestManager::TestDeviceRemoval()
   2292 {
   2293     TEST_LOG("\n=======================================\n");
   2294     TEST_LOG(" Device removal test:\n");
   2295     TEST_LOG("=======================================\n");
   2296 
   2297     if (_audioDevice == NULL)
   2298     {
   2299         return -1;
   2300     }
   2301 
   2302     RESET_TEST;
   2303 
   2304     AudioDeviceModule* audioDevice = _audioDevice;
   2305 
   2306     EXPECT_EQ(0, audioDevice->Init());
   2307     EXPECT_TRUE(audioDevice->Initialized());
   2308 
   2309     bool recIsAvailable(false);
   2310     bool playIsAvailable(false);
   2311     uint8_t nPlayChannels(0);
   2312     uint8_t nRecChannels(0);
   2313     uint8_t loopCount(0);
   2314 
   2315     while (loopCount < 2)
   2316     {
   2317         if (SelectRecordingDevice() == -1)
   2318         {
   2319             TEST_LOG("\nERROR: Device selection failed!\n \n");
   2320             return -1;
   2321         }
   2322 
   2323         EXPECT_EQ(0, audioDevice->RecordingIsAvailable(&recIsAvailable));
   2324         if (!recIsAvailable)
   2325         {
   2326             TEST_LOG("\nERROR: Recording is not available for the selected device!\n \n");
   2327             return -1;
   2328         }
   2329 
   2330         if (SelectPlayoutDevice() == -1)
   2331         {
   2332             TEST_LOG("\nERROR: Device selection failed!\n \n");
   2333             return -1;
   2334         }
   2335 
   2336         EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&playIsAvailable));
   2337         if (recIsAvailable && playIsAvailable)
   2338         {
   2339             _audioTransport->SetFullDuplex(true);
   2340         } else if (!playIsAvailable)
   2341         {
   2342             TEST_LOG("\nERROR: Playout is not available for the selected device!\n \n");
   2343             return -1;
   2344         }
   2345 
   2346         bool available(false);
   2347         bool enabled(false);
   2348 
   2349         if (recIsAvailable && playIsAvailable)
   2350         {
   2351             uint32_t playSamplesPerSec(0);
   2352             uint32_t recSamplesPerSecRec(0);
   2353 
   2354             EXPECT_EQ(0, audioDevice->RegisterAudioCallback(_audioTransport));
   2355 
   2356             _audioTransport->SetFullDuplex(true);
   2357 
   2358             EXPECT_EQ(0, audioDevice->StereoRecordingIsAvailable(&available));
   2359             if (available)
   2360             {
   2361                 EXPECT_EQ(0, audioDevice->SetStereoRecording(true));
   2362             }
   2363 
   2364             EXPECT_EQ(0, audioDevice->StereoPlayoutIsAvailable(&available));
   2365             if (available)
   2366             {
   2367                 EXPECT_EQ(0, audioDevice->SetStereoPlayout(true));
   2368             }
   2369 
   2370             EXPECT_EQ(0, audioDevice->MicrophoneVolumeIsAvailable(&available));
   2371             if (available)
   2372             {
   2373                 uint32_t maxVolume(0);
   2374                 EXPECT_EQ(0, audioDevice->MaxMicrophoneVolume(&maxVolume));
   2375                 EXPECT_EQ(0, audioDevice->SetMicrophoneVolume(maxVolume));
   2376             }
   2377 
   2378             EXPECT_EQ(0, audioDevice->InitRecording());
   2379             EXPECT_EQ(0, audioDevice->InitPlayout());
   2380             EXPECT_EQ(0, audioDevice->PlayoutSampleRate(&playSamplesPerSec));
   2381             EXPECT_EQ(0, audioDevice->RecordingSampleRate(&recSamplesPerSecRec));
   2382             EXPECT_EQ(0, audioDevice->StereoPlayout(&enabled));
   2383             enabled ? nPlayChannels = 2 : nPlayChannels = 1;
   2384             EXPECT_EQ(0, audioDevice->StereoRecording(&enabled));
   2385             enabled ? nRecChannels = 2 : nRecChannels = 1;
   2386             EXPECT_EQ(0, audioDevice->StartRecording());
   2387             EXPECT_EQ(0, audioDevice->StartPlayout());
   2388 
   2389             AudioDeviceModule::AudioLayer audioLayer;
   2390             EXPECT_EQ(0, audioDevice->ActiveAudioLayer(&audioLayer));
   2391 
   2392             if (audioLayer == AudioDeviceModule::kLinuxPulseAudio)
   2393             {
   2394                 TEST_LOG("\n \n> PulseAudio loopback audio is now active.\n"
   2395                     "> Rec : fs=%u, #channels=%u.\n"
   2396                     "> Play: fs=%u, #channels=%u.\n"
   2397                     "> Speak into the microphone and verify that your voice is"
   2398                     " played out in loopback.\n"
   2399                     "> Unplug the device and make sure that your voice is played"
   2400                     " out in loop back on the built-in soundcard.\n"
   2401                     "> Then press any key...\n",
   2402                          recSamplesPerSecRec, nRecChannels, playSamplesPerSec,
   2403                          nPlayChannels);
   2404 
   2405                 PAUSE(DEFAULT_PAUSE_TIME);
   2406             } else if (audioDevice->Playing() && audioDevice->Recording())
   2407             {
   2408                 if (loopCount < 1)
   2409                 {
   2410                     TEST_LOG("\n \n> Loopback audio is now active.\n"
   2411                         "> Rec : fs=%u, #channels=%u.\n"
   2412                         "> Play: fs=%u, #channels=%u.\n"
   2413                         "> Speak into the microphone and verify that your voice"
   2414                         " is played out in loopback.\n"
   2415                         "> Unplug the device and wait for the error message...\n",
   2416                         recSamplesPerSecRec, nRecChannels,
   2417                         playSamplesPerSec, nPlayChannels);
   2418 
   2419                     _audioEventObserver->_error
   2420                         = (AudioDeviceObserver::ErrorCode) (-1);
   2421                     while (_audioEventObserver->_error
   2422                         == (AudioDeviceObserver::ErrorCode) (-1))
   2423                     {
   2424                         SleepMs(500);
   2425                     }
   2426                 } else
   2427                 {
   2428                     TEST_LOG("\n \n> Loopback audio is now active.\n"
   2429                         "> Rec : fs=%u, #channels=%u.\n"
   2430                         "> Play: fs=%u, #channels=%u.\n"
   2431                         "> Speak into the microphone and verify that your voice"
   2432                         " is played out in loopback.\n"
   2433                         "> Press any key to stop...\n",
   2434                              recSamplesPerSecRec, nRecChannels,
   2435                              playSamplesPerSec, nPlayChannels);
   2436 
   2437                     PAUSE(DEFAULT_PAUSE_TIME);
   2438                 }
   2439             }
   2440 
   2441             EXPECT_EQ(0, audioDevice->StopRecording());
   2442             EXPECT_EQ(0, audioDevice->StopPlayout());
   2443             EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   2444 
   2445             _audioTransport->SetFullDuplex(false);
   2446 
   2447             if (loopCount < 1)
   2448             {
   2449                 TEST_LOG("\n \n> Stopped!\n");
   2450                 TEST_LOG("> Now reinsert device if you want to enumerate it.\n");
   2451                 TEST_LOG("> Press any key when done.\n");
   2452                 PAUSE(DEFAULT_PAUSE_TIME);
   2453             }
   2454 
   2455             loopCount++;
   2456         }
   2457     }  // loopCount
   2458 
   2459     EXPECT_EQ(0, audioDevice->Terminate());
   2460     EXPECT_FALSE(audioDevice->Initialized());
   2461 
   2462     TEST_LOG("\n");
   2463     PRINT_TEST_RESULTS;
   2464 
   2465     return 0;
   2466 }
   2467 
   2468 int32_t FuncTestManager::TestExtra()
   2469 {
   2470     TEST_LOG("\n=======================================\n");
   2471     TEST_LOG(" Extra test:\n");
   2472     TEST_LOG("=======================================\n");
   2473 
   2474     if (_audioDevice == NULL)
   2475     {
   2476         return -1;
   2477     }
   2478 
   2479     RESET_TEST;
   2480 
   2481     AudioDeviceModule* audioDevice = _audioDevice;
   2482 
   2483     EXPECT_EQ(0, audioDevice->Init());
   2484     EXPECT_TRUE(audioDevice->Initialized());
   2485 
   2486     EXPECT_EQ(0, audioDevice->Terminate());
   2487     EXPECT_FALSE(audioDevice->Initialized());
   2488 
   2489     TEST_LOG("\n");
   2490     PRINT_TEST_RESULTS;
   2491 
   2492     return 0;
   2493 }
   2494 
   2495 int32_t FuncTestManager::SelectRecordingDevice()
   2496 {
   2497     int16_t nDevices = _audioDevice->RecordingDevices();
   2498     char name[kAdmMaxDeviceNameSize];
   2499     char guid[kAdmMaxGuidSize];
   2500     int32_t ret(-1);
   2501 
   2502 #ifdef _WIN32
   2503     TEST_LOG("\nSelect Recording Device\n \n");
   2504     TEST_LOG("  (%d) Default\n", 0);
   2505     TEST_LOG("  (%d) Default Communication [Win 7]\n", 1);
   2506     TEST_LOG("- - - - - - - - - - - - - - - - - - - -\n");
   2507     for (int i = 0; i < nDevices; i++)
   2508     {
   2509         EXPECT_EQ(0, _audioDevice->RecordingDeviceName(i, name, guid));
   2510         TEST_LOG(" (%d) Device %d (%s)\n", i+10, i, name);
   2511     }
   2512     TEST_LOG("\n: ");
   2513 
   2514     int sel(0);
   2515 
   2516     scanf("%u", &sel);
   2517 
   2518     if (sel == 0)
   2519     {
   2520         EXPECT_EQ(0, (ret = _audioDevice->SetRecordingDevice(AudioDeviceModule::kDefaultDevice)));
   2521     }
   2522     else if (sel == 1)
   2523     {
   2524         EXPECT_TRUE((ret = _audioDevice->SetRecordingDevice(
   2525             AudioDeviceModule::kDefaultCommunicationDevice)) == 0);
   2526     }
   2527     else if (sel < (nDevices+10))
   2528     {
   2529         EXPECT_EQ(0, (ret = _audioDevice->SetRecordingDevice(sel-10)));
   2530     }
   2531     else
   2532     {
   2533         return -1;
   2534     }
   2535 #else
   2536     TEST_LOG("\nSelect Recording Device\n \n");
   2537     for (int i = 0; i < nDevices; i++)
   2538     {
   2539         EXPECT_EQ(0, _audioDevice->RecordingDeviceName(i, name, guid));
   2540         TEST_LOG(" (%d) Device %d (%s)\n", i, i, name);
   2541     }
   2542     TEST_LOG("\n: ");
   2543     int sel(0);
   2544     EXPECT_TRUE(scanf("%u", &sel) > 0);
   2545     if (sel < (nDevices))
   2546     {
   2547         EXPECT_EQ(0, (ret = _audioDevice->SetRecordingDevice(sel)));
   2548     } else
   2549     {
   2550         return -1;
   2551     }
   2552 #endif
   2553 
   2554     return ret;
   2555 }
   2556 
   2557 int32_t FuncTestManager::SelectPlayoutDevice()
   2558 {
   2559     int16_t nDevices = _audioDevice->PlayoutDevices();
   2560     char name[kAdmMaxDeviceNameSize];
   2561     char guid[kAdmMaxGuidSize];
   2562 
   2563 #ifdef _WIN32
   2564     TEST_LOG("\nSelect Playout Device\n \n");
   2565     TEST_LOG("  (%d) Default\n", 0);
   2566     TEST_LOG("  (%d) Default Communication [Win 7]\n", 1);
   2567     TEST_LOG("- - - - - - - - - - - - - - - - - - - -\n");
   2568     for (int i = 0; i < nDevices; i++)
   2569     {
   2570         EXPECT_EQ(0, _audioDevice->PlayoutDeviceName(i, name, guid));
   2571         TEST_LOG(" (%d) Device %d (%s)\n", i+10, i, name);
   2572     }
   2573     TEST_LOG("\n: ");
   2574 
   2575     int sel(0);
   2576 
   2577     scanf("%u", &sel);
   2578 
   2579     int32_t ret(0);
   2580 
   2581     if (sel == 0)
   2582     {
   2583         EXPECT_TRUE((ret = _audioDevice->SetPlayoutDevice(
   2584             AudioDeviceModule::kDefaultDevice)) == 0);
   2585     }
   2586     else if (sel == 1)
   2587     {
   2588         EXPECT_TRUE((ret = _audioDevice->SetPlayoutDevice(
   2589             AudioDeviceModule::kDefaultCommunicationDevice)) == 0);
   2590     }
   2591     else if (sel < (nDevices+10))
   2592     {
   2593         EXPECT_EQ(0, (ret = _audioDevice->SetPlayoutDevice(sel-10)));
   2594     }
   2595     else
   2596     {
   2597         return -1;
   2598     }
   2599 #else
   2600     TEST_LOG("\nSelect Playout Device\n \n");
   2601     for (int i = 0; i < nDevices; i++)
   2602     {
   2603         EXPECT_EQ(0, _audioDevice->PlayoutDeviceName(i, name, guid));
   2604         TEST_LOG(" (%d) Device %d (%s)\n", i, i, name);
   2605     }
   2606     TEST_LOG("\n: ");
   2607     int sel(0);
   2608     EXPECT_TRUE(scanf("%u", &sel) > 0);
   2609     int32_t ret(0);
   2610     if (sel < (nDevices))
   2611     {
   2612         EXPECT_EQ(0, (ret = _audioDevice->SetPlayoutDevice(sel)));
   2613     } else
   2614     {
   2615         return -1;
   2616     }
   2617 #endif
   2618 
   2619     return ret;
   2620 }
   2621 
   2622 int32_t FuncTestManager::TestAdvancedMBAPI()
   2623 {
   2624     TEST_LOG("\n=======================================\n");
   2625     TEST_LOG(" Advanced mobile device API test:\n");
   2626     TEST_LOG("=======================================\n");
   2627 
   2628     if (_audioDevice == NULL)
   2629     {
   2630         return -1;
   2631     }
   2632 
   2633     RESET_TEST;
   2634 
   2635     AudioDeviceModule* audioDevice = _audioDevice;
   2636 
   2637     EXPECT_EQ(0, audioDevice->Init());
   2638     EXPECT_TRUE(audioDevice->Initialized());
   2639 
   2640     if (SelectRecordingDevice() == -1)
   2641     {
   2642         TEST_LOG("\nERROR: Device selection failed!\n \n");
   2643         return -1;
   2644     }
   2645     if (SelectPlayoutDevice() == -1)
   2646     {
   2647         TEST_LOG("\nERROR: Device selection failed!\n \n");
   2648         return -1;
   2649     }
   2650     _audioTransport->SetFullDuplex(true);
   2651     _audioTransport->SetLoopbackMeasurements(true);
   2652 
   2653     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(_audioTransport));
   2654     // Start recording
   2655     EXPECT_EQ(0, audioDevice->InitRecording());
   2656     EXPECT_EQ(0, audioDevice->StartRecording());
   2657     // Start playout
   2658     EXPECT_EQ(0, audioDevice->InitPlayout());
   2659     EXPECT_EQ(0, audioDevice->StartPlayout());
   2660 
   2661     EXPECT_TRUE(audioDevice->Recording());
   2662     EXPECT_TRUE(audioDevice->Playing());
   2663 
   2664 #if defined(_WIN32_WCE) || defined(WEBRTC_IOS)
   2665     TEST_LOG("\nResetAudioDevice\n \n");
   2666     if (audioDevice->Recording() && audioDevice->Playing())
   2667     {
   2668         TEST_LOG("\n> Speak into the microphone and verify that the audio is good.\n\
   2669 > Press any key to stop...\n \n");
   2670         PAUSE(DEFAULT_PAUSE_TIME);
   2671     }
   2672     for (int p=0; p<=60; p+=20)
   2673     {
   2674         TEST_LOG("Resetting sound device several time with pause %d ms\n", p);
   2675         for (int l=0; l<20; ++l)
   2676         {
   2677             EXPECT_EQ(0, audioDevice->ResetAudioDevice());
   2678             SleepMs(p);
   2679         }
   2680         TEST_LOG("\n> Speak into the microphone and verify that the audio is good.\n");
   2681         SleepMs(2000);
   2682     }
   2683 #endif
   2684 
   2685 #if defined(WEBRTC_IOS)
   2686     bool loudspeakerOn(false);
   2687     TEST_LOG("\nSet playout spaker\n \n");
   2688     if (audioDevice->Recording() && audioDevice->Playing())
   2689     {
   2690         TEST_LOG("\n> Speak into the microphone and verify that the audio is good.\n\
   2691 > Press any key to stop...\n \n");
   2692         PAUSE(DEFAULT_PAUSE_TIME);
   2693     }
   2694 
   2695     TEST_LOG("Set to use speaker\n");
   2696     EXPECT_EQ(0, audioDevice->SetLoudspeakerStatus(true));
   2697     TEST_LOG("\n> Speak into the microphone and verify that the audio is"
   2698         " from the loudspeaker.\n\
   2699 > Press any key to stop...\n \n");
   2700     PAUSE(DEFAULT_PAUSE_TIME);
   2701     EXPECT_EQ(0, audioDevice->GetLoudspeakerStatus(&loudspeakerOn));
   2702     EXPECT_TRUE(loudspeakerOn);
   2703 
   2704     TEST_LOG("Set to not use speaker\n");
   2705     EXPECT_EQ(0, audioDevice->SetLoudspeakerStatus(false));
   2706     TEST_LOG("\n> Speak into the microphone and verify that the audio is not"
   2707         " from the loudspeaker.\n\
   2708 > Press any key to stop...\n \n");
   2709     PAUSE(DEFAULT_PAUSE_TIME);
   2710     EXPECT_EQ(0, audioDevice->GetLoudspeakerStatus(&loudspeakerOn));
   2711     EXPECT_FALSE(loudspeakerOn);
   2712 #endif
   2713 
   2714     EXPECT_EQ(0, audioDevice->StopRecording());
   2715     EXPECT_EQ(0, audioDevice->StopPlayout());
   2716     EXPECT_EQ(0, audioDevice->RegisterAudioCallback(NULL));
   2717 
   2718     _audioTransport->SetFullDuplex(false);
   2719 
   2720     TEST_LOG("\n");
   2721     PRINT_TEST_RESULTS;
   2722 
   2723     return 0;
   2724 }
   2725 
   2726 }  // namespace webrtc
   2727 
   2728 // EOF
   2729