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