Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: webrtc/webrtc/modules/audio_device/test/func_test_manager.cc @ 0:4bda6873e34c

pub_scrub_3792 tip
Last change on this file since 0:4bda6873e34c was 0:4bda6873e34c, checked in by Michael Prittie <mprittie@…>, 6 years ago

Scrubbed password for publication.

File size: 84.0 KB
Line 
1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <assert.h>
12#include <ctype.h>
13#include <stdio.h>
14#include <string.h>
15
16#include "testing/gtest/include/gtest/gtest.h"
17#include "webrtc/modules/audio_device/test/func_test_manager.h"
18#include "webrtc/system_wrappers/interface/sleep.h"
19#include "webrtc/test/testsupport/fileutils.h"
20
21#include "webrtc/modules/audio_device/audio_device_config.h"
22#include "webrtc/modules/audio_device/audio_device_impl.h"
23
24#ifndef __GNUC__
25// Disable warning message ('sprintf': name was marked as #pragma deprecated)
26#pragma warning( disable : 4995 )
27// Disable warning message 4996 ('scanf': This function or variable may be unsafe)
28#pragma warning( disable : 4996 )
29#endif
30
31const char* RecordedMicrophoneFile = "recorded_microphone_mono_48.pcm";
32const char* RecordedMicrophoneVolumeFile =
33"recorded_microphone_volume_mono_48.pcm";
34const char* RecordedMicrophoneMuteFile = "recorded_microphone_mute_mono_48.pcm";
35const char* RecordedMicrophoneBoostFile =
36"recorded_microphone_boost_mono_48.pcm";
37const char* RecordedMicrophoneAGCFile = "recorded_microphone_AGC_mono_48.pcm";
38const char* RecordedSpeakerFile = "recorded_speaker_48.pcm";
39
40struct AudioPacket
41{
42    uint8_t dataBuffer[4 * 960];
43    uint16_t nSamples;
44    uint16_t nBytesPerSample;
45    uint8_t nChannels;
46    uint32_t samplesPerSec;
47};
48
49// Helper functions
50#if !defined(WEBRTC_IOS)
51char* GetFilename(char* filename)
52{
53    return filename;
54}
55const char* GetFilename(const char* filename)
56{
57    return filename;
58}
59char* GetResource(char* resource)
60{
61    return resource;
62}
63const char* GetResource(const char* resource)
64{
65    return resource;
66}
67#endif
68
69namespace webrtc
70{
71
72AudioEventObserver::AudioEventObserver(AudioDeviceModule* audioDevice)
73{
74}
75
76AudioEventObserver::~AudioEventObserver()
77{
78}
79
80void AudioEventObserver::OnErrorIsReported(const ErrorCode error)
81{
82    TEST_LOG("\n[*** ERROR ***] => OnErrorIsReported(%d)\n \n", error);
83    _error = error;
84}
85
86
87void AudioEventObserver::OnWarningIsReported(const WarningCode warning)
88{
89    TEST_LOG("\n[*** WARNING ***] => OnWarningIsReported(%d)\n \n", warning);
90    _warning = warning;
91}
92
93AudioTransportImpl::AudioTransportImpl(AudioDeviceModule* audioDevice) :
94    _audioDevice(audioDevice),
95    _playFromFile(false),
96    _fullDuplex(false),
97    _speakerVolume(false),
98    _speakerMute(false),
99    _microphoneVolume(false),
100    _microphoneMute(false),
101    _microphoneBoost(false),
102    _microphoneAGC(false),
103    _loopBackMeasurements(false),
104    _playFile(*FileWrapper::Create()),
105    _recCount(0),
106    _playCount(0),
107    _audioList()
108{
109    _resampler.Reset(48000, 48000, kResamplerSynchronousStereo);
110}
111
112AudioTransportImpl::~AudioTransportImpl()
113{
114    _playFile.Flush();
115    _playFile.CloseFile();
116    delete &_playFile;
117
118    while (!_audioList.Empty())
119    {
120        ListItem* item = _audioList.First();
121        if (item)
122        {
123            AudioPacket* packet = static_cast<AudioPacket*> (item->GetItem());
124            if (packet)
125            {
126                delete packet;
127            }
128        }
129        _audioList.PopFront();
130    }
131}
132
133// ----------------------------------------------------------------------------
134//      AudioTransportImpl::SetFilePlayout
135// ----------------------------------------------------------------------------
136
137int32_t AudioTransportImpl::SetFilePlayout(bool enable, const char* fileName)
138{
139    _playFromFile = enable;
140    if (enable)
141    {
142        return (_playFile.OpenFile(fileName, true, true, false));
143    } else
144    {
145        _playFile.Flush();
146        return (_playFile.CloseFile());
147    }
148}
149;
150
151void AudioTransportImpl::SetFullDuplex(bool enable)
152{
153    _fullDuplex = enable;
154
155    while (!_audioList.Empty())
156    {
157        ListItem* item = _audioList.First();
158        if (item)
159        {
160            AudioPacket* packet = static_cast<AudioPacket*> (item->GetItem());
161            if (packet)
162            {
163                delete packet;
164            }
165        }
166        _audioList.PopFront();
167    }
168}
169
170int32_t AudioTransportImpl::RecordedDataIsAvailable(
171    const void* audioSamples,
172    const uint32_t nSamples,
173    const uint8_t nBytesPerSample,
174    const uint8_t nChannels,
175    const uint32_t samplesPerSec,
176    const uint32_t totalDelayMS,
177    const int32_t clockDrift,
178    const uint32_t currentMicLevel,
179    const bool keyPressed,
180    uint32_t& newMicLevel)
181{
182    if (_fullDuplex && _audioList.GetSize() < 15)
183    {
184        AudioPacket* packet = new AudioPacket();
185        memcpy(packet->dataBuffer, audioSamples, nSamples * nBytesPerSample);
186        packet->nSamples = (uint16_t) nSamples;
187        packet->nBytesPerSample = nBytesPerSample;
188        packet->nChannels = nChannels;
189        packet->samplesPerSec = samplesPerSec;
190        _audioList.PushBack(packet);
191    }
192
193    _recCount++;
194    if (_recCount % 100 == 0)
195    {
196        bool addMarker(true);
197
198        if (_loopBackMeasurements)
199        {
200            addMarker = false;
201        }
202
203        if (_microphoneVolume)
204        {
205            uint32_t maxVolume(0);
206            uint32_t minVolume(0);
207            uint32_t volume(0);
208            uint16_t stepSize(0);
209            EXPECT_EQ(0, _audioDevice->MaxMicrophoneVolume(&maxVolume));
210            EXPECT_EQ(0, _audioDevice->MinMicrophoneVolume(&minVolume));
211            EXPECT_EQ(0, _audioDevice->MicrophoneVolumeStepSize(&stepSize));
212            EXPECT_EQ(0, _audioDevice->MicrophoneVolume(&volume));
213            if (volume == 0)
214            {
215                TEST_LOG("[0]");
216                addMarker = false;
217            }
218            int stepScale = (int) ((maxVolume - minVolume) / (stepSize * 10));
219            volume += (stepScale * stepSize);
220            if (volume > maxVolume)
221            {
222                TEST_LOG("[MAX]");
223                volume = 0;
224                addMarker = false;
225            }
226            EXPECT_EQ(0, _audioDevice->SetMicrophoneVolume(volume));
227        }
228
229        if (_microphoneAGC)
230        {
231            uint32_t maxVolume(0);
232            uint32_t minVolume(0);
233            uint16_t stepSize(0);
234            EXPECT_EQ(0, _audioDevice->MaxMicrophoneVolume(&maxVolume));
235            EXPECT_EQ(0, _audioDevice->MinMicrophoneVolume(&minVolume));
236            EXPECT_EQ(0, _audioDevice->MicrophoneVolumeStepSize(&stepSize));
237            // emulate real AGC (min->max->min->max etc.)
238            if (currentMicLevel <= 1)
239            {
240                TEST_LOG("[MIN]");
241                addMarker = false;
242            }
243            int stepScale = (int) ((maxVolume - minVolume) / (stepSize * 10));
244            newMicLevel = currentMicLevel + (stepScale * stepSize);
245            if (newMicLevel > maxVolume)
246            {
247                TEST_LOG("[MAX]");
248                newMicLevel = 1; // set lowest (non-zero) AGC level
249                addMarker = false;
250            }
251        }
252
253        if (_microphoneMute && (_recCount % 500 == 0))
254        {
255            bool muted(false);
256            EXPECT_EQ(0, _audioDevice->MicrophoneMute(&muted));
257            muted = !muted;
258            EXPECT_EQ(0, _audioDevice->SetMicrophoneMute(muted));
259            if (muted)
260            {
261                TEST_LOG("[MUTE ON]");
262                addMarker = false;
263            } else
264            {
265                TEST_LOG("[MUTE OFF]");
266                addMarker = false;
267            }
268        }
269
270        if (_microphoneBoost && (_recCount % 500 == 0))
271        {
272            bool boosted(false);
273            EXPECT_EQ(0, _audioDevice->MicrophoneBoost(&boosted));
274            boosted = !boosted;
275            EXPECT_EQ(0, _audioDevice->SetMicrophoneBoost(boosted));
276            if (boosted)
277            {
278                TEST_LOG("[BOOST ON]");
279                addMarker = false;
280            } else
281            {
282                TEST_LOG("[BOOST OFF]");
283                addMarker = false;
284            }
285        }
286
287        if ((nChannels == 1) && addMarker)
288        {
289            // mono
290            TEST_LOG("-");
291        } else if ((nChannels == 2) && (nBytesPerSample == 2) && addMarker)
292        {
293            AudioDeviceModule::ChannelType
294                chType(AudioDeviceModule::kChannelLeft);
295            EXPECT_EQ(0, _audioDevice->RecordingChannel(&chType));
296            if (chType == AudioDeviceModule::kChannelLeft)
297                TEST_LOG("-|");
298            else
299                TEST_LOG("|-");
300        } else if (addMarker)
301        {
302            // stereo
303            TEST_LOG("--");
304        }
305
306        if (nChannels == 2 && nBytesPerSample == 2)
307        {
308            // TEST_LOG("=> emulated mono (one channel exctracted from stereo input)\n");
309        }
310    }
311
312    return 0;
313}
314
315
316int32_t AudioTransportImpl::NeedMorePlayData(
317    const uint32_t nSamples,
318    const uint8_t nBytesPerSample,
319    const uint8_t nChannels,
320    const uint32_t samplesPerSec,
321    void* audioSamples,
322    uint32_t& nSamplesOut)
323{
324    if (_fullDuplex)
325    {
326        if (_audioList.Empty())
327        {
328            // use zero stuffing when not enough data
329            memset(audioSamples, 0, nBytesPerSample * nSamples);
330        } else
331        {
332            ListItem* item = _audioList.First();
333            AudioPacket* packet = static_cast<AudioPacket*> (item->GetItem());
334            if (packet)
335            {
336                int ret(0);
337                int lenOut(0);
338                int16_t tmpBuf_96kHz[80 * 12];
339                int16_t* ptr16In = NULL;
340                int16_t* ptr16Out = NULL;
341
342                const uint16_t nSamplesIn = packet->nSamples;
343                const uint8_t nChannelsIn = packet->nChannels;
344                const uint32_t samplesPerSecIn = packet->samplesPerSec;
345                const uint16_t nBytesPerSampleIn =
346                    packet->nBytesPerSample;
347
348                int32_t fsInHz(samplesPerSecIn);
349                int32_t fsOutHz(samplesPerSec);
350
351                if (fsInHz == 44100)
352                    fsInHz = 44000;
353
354                if (fsOutHz == 44100)
355                    fsOutHz = 44000;
356
357                if (nChannelsIn == 2 && nBytesPerSampleIn == 4)
358                {
359                    // input is stereo => we will resample in stereo
360                    ret = _resampler.ResetIfNeeded(fsInHz, fsOutHz,
361                                                   kResamplerSynchronousStereo);
362                    if (ret == 0)
363                    {
364                        if (nChannels == 2)
365                        {
366                            _resampler.Push(
367                                (const int16_t*) packet->dataBuffer,
368                                2 * nSamplesIn,
369                                (int16_t*) audioSamples, 2
370                                * nSamples, lenOut);
371                        } else
372                        {
373                            _resampler.Push(
374                                (const int16_t*) packet->dataBuffer,
375                                2 * nSamplesIn, tmpBuf_96kHz, 2
376                                * nSamples, lenOut);
377
378                            ptr16In = &tmpBuf_96kHz[0];
379                            ptr16Out = (int16_t*) audioSamples;
380
381                            // do stereo -> mono
382                            for (unsigned int i = 0; i < nSamples; i++)
383                            {
384                                *ptr16Out = *ptr16In; // use left channel
385                                ptr16Out++;
386                                ptr16In++;
387                                ptr16In++;
388                            }
389                        }
390                        assert(2*nSamples == (uint32_t)lenOut);
391                    } else
392                    {
393                        if (_playCount % 100 == 0)
394                            TEST_LOG(
395                                     "ERROR: unable to resample from %d to %d\n",
396                                     samplesPerSecIn, samplesPerSec);
397                    }
398                } else
399                {
400                    // input is mono (can be "reduced from stereo" as well) =>
401                    // we will resample in mono
402                    ret = _resampler.ResetIfNeeded(fsInHz, fsOutHz,
403                                                   kResamplerSynchronous);
404                    if (ret == 0)
405                    {
406                        if (nChannels == 1)
407                        {
408                            _resampler.Push(
409                                (const int16_t*) packet->dataBuffer,
410                                nSamplesIn,
411                                (int16_t*) audioSamples,
412                                nSamples, lenOut);
413                        } else
414                        {
415                            _resampler.Push(
416                                (const int16_t*) packet->dataBuffer,
417                                nSamplesIn, tmpBuf_96kHz, nSamples,
418                                lenOut);
419
420                            ptr16In = &tmpBuf_96kHz[0];
421                            ptr16Out = (int16_t*) audioSamples;
422
423                            // do mono -> stereo
424                            for (unsigned int i = 0; i < nSamples; i++)
425                            {
426                                *ptr16Out = *ptr16In; // left
427                                ptr16Out++;
428                                *ptr16Out = *ptr16In; // right (same as left sample)
429                                ptr16Out++;
430                                ptr16In++;
431                            }
432                        }
433                        assert(nSamples == (uint32_t)lenOut);
434                    } else
435                    {
436                        if (_playCount % 100 == 0)
437                            TEST_LOG("ERROR: unable to resample from %d to %d\n",
438                                     samplesPerSecIn, samplesPerSec);
439                    }
440                }
441                nSamplesOut = nSamples;
442                delete packet;
443            }
444            _audioList.PopFront();
445        }
446    }  // if (_fullDuplex)
447
448    if (_playFromFile && _playFile.Open())
449    {
450        int16_t fileBuf[480];
451
452        // read mono-file
453        int32_t len = _playFile.Read((int8_t*) fileBuf, 2
454            * nSamples);
455        if (len != 2 * (int32_t) nSamples)
456        {
457            _playFile.Rewind();
458            _playFile.Read((int8_t*) fileBuf, 2 * nSamples);
459        }
460
461        // convert to stero if required
462        if (nChannels == 1)
463        {
464            memcpy(audioSamples, fileBuf, 2 * nSamples);
465        } else
466        {
467            // mono sample from file is duplicated and sent to left and right
468            // channels
469            int16_t* audio16 = (int16_t*) audioSamples;
470            for (unsigned int i = 0; i < nSamples; i++)
471            {
472                (*audio16) = fileBuf[i]; // left
473                audio16++;
474                (*audio16) = fileBuf[i]; // right
475                audio16++;
476            }
477        }
478    }  // if (_playFromFile && _playFile.Open())
479
480    _playCount++;
481
482    if (_playCount % 100 == 0)
483    {
484        bool addMarker(true);
485
486        if (_speakerVolume)
487        {
488            uint32_t maxVolume(0);
489            uint32_t minVolume(0);
490            uint32_t volume(0);
491            uint16_t stepSize(0);
492            EXPECT_EQ(0, _audioDevice->MaxSpeakerVolume(&maxVolume));
493            EXPECT_EQ(0, _audioDevice->MinSpeakerVolume(&minVolume));
494            EXPECT_EQ(0, _audioDevice->SpeakerVolumeStepSize(&stepSize));
495            EXPECT_EQ(0, _audioDevice->SpeakerVolume(&volume));
496            if (volume == 0)
497            {
498                TEST_LOG("[0]");
499                addMarker = false;
500            }
501            uint32_t step = (maxVolume - minVolume) / 10;
502            step = (step < stepSize ? stepSize : step);
503            volume += step;
504            if (volume > maxVolume)
505            {
506                TEST_LOG("[MAX]");
507                volume = 0;
508                addMarker = false;
509            }
510            EXPECT_EQ(0, _audioDevice->SetSpeakerVolume(volume));
511        }
512
513        if (_speakerMute && (_playCount % 500 == 0))
514        {
515            bool muted(false);
516            EXPECT_EQ(0, _audioDevice->SpeakerMute(&muted));
517            muted = !muted;
518            EXPECT_EQ(0, _audioDevice->SetSpeakerMute(muted));
519            if (muted)
520            {
521                TEST_LOG("[MUTE ON]");
522                addMarker = false;
523            } else
524            {
525                TEST_LOG("[MUTE OFF]");
526                addMarker = false;
527            }
528        }
529
530        if (_loopBackMeasurements)
531        {
532            uint16_t recDelayMS(0);
533            uint16_t playDelayMS(0);
534            uint32_t nItemsInList(0);
535
536            nItemsInList = _audioList.GetSize();
537            EXPECT_EQ(0, _audioDevice->RecordingDelay(&recDelayMS));
538            EXPECT_EQ(0, _audioDevice->PlayoutDelay(&playDelayMS));
539            TEST_LOG("Delay (rec+play)+buf: %3u (%3u+%3u)+%3u [ms]\n",
540                     recDelayMS + playDelayMS + 10 * (nItemsInList + 1),
541                     recDelayMS, playDelayMS, 10 * (nItemsInList + 1));
542
543            addMarker = false;
544        }
545
546        if ((nChannels == 1) && addMarker)
547        {
548            TEST_LOG("+");
549        } else if ((nChannels == 2) && addMarker)
550        {
551            TEST_LOG("++");
552        }
553    }  // if (_playCount % 100 == 0)
554
555    nSamplesOut = nSamples;
556
557    return 0;
558}
559
560int AudioTransportImpl::OnDataAvailable(const int voe_channels[],
561                                        int number_of_voe_channels,
562                                        const int16_t* audio_data,
563                                        int sample_rate,
564                                        int number_of_channels,
565                                        int number_of_frames,
566                                        int audio_delay_milliseconds,
567                                        int current_volume,
568                                        bool key_pressed,
569                                        bool need_audio_processing) {
570  return 0;
571}
572
573FuncTestManager::FuncTestManager() :
574    _processThread(NULL),
575    _audioDevice(NULL),
576    _audioEventObserver(NULL),
577    _audioTransport(NULL)
578{
579  _playoutFile48 = webrtc::test::ResourcePath("audio_device\\audio_short48",
580                                              "pcm");
581  _playoutFile44 = webrtc::test::ResourcePath("audio_device\\audio_short44",
582                                              "pcm");
583  _playoutFile16 = webrtc::test::ResourcePath("audio_device\\audio_short16",
584                                              "pcm");
585  _playoutFile8 = webrtc::test::ResourcePath("audio_device\\audio_short8",
586                                             "pcm");
587}
588
589FuncTestManager::~FuncTestManager()
590{
591}
592
593int32_t FuncTestManager::Init()
594{
595    EXPECT_TRUE((_processThread = ProcessThread::CreateProcessThread()) != NULL);
596    if (_processThread == NULL)
597    {
598        return -1;
599    }
600    _processThread->Start();
601
602    // create the Audio Device module
603    EXPECT_TRUE((_audioDevice = AudioDeviceModuleImpl::Create(
604        555, ADM_AUDIO_LAYER)) != NULL);
605    if (_audioDevice == NULL)
606    {
607        return -1;
608    }
609    EXPECT_EQ(1, _audioDevice->AddRef());
610
611    // register the Audio Device module
612    _processThread->RegisterModule(_audioDevice);
613
614    // register event observer
615    _audioEventObserver = new AudioEventObserver(_audioDevice);
616    EXPECT_EQ(0, _audioDevice->RegisterEventObserver(_audioEventObserver));
617
618    // register audio transport
619    _audioTransport = new AudioTransportImpl(_audioDevice);
620    EXPECT_EQ(0, _audioDevice->RegisterAudioCallback(_audioTransport));
621
622    return 0;
623}
624
625int32_t FuncTestManager::Close()
626{
627    EXPECT_EQ(0, _audioDevice->RegisterEventObserver(NULL));
628    EXPECT_EQ(0, _audioDevice->RegisterAudioCallback(NULL));
629    EXPECT_EQ(0, _audioDevice->Terminate());
630
631    // release the ProcessThread object
632    if (_processThread)
633    {
634        _processThread->DeRegisterModule(_audioDevice);
635        _processThread->Stop();
636        ProcessThread::DestroyProcessThread(_processThread);
637    }
638
639    // delete the audio observer
640    if (_audioEventObserver)
641    {
642        delete _audioEventObserver;
643        _audioEventObserver = NULL;
644    }
645
646    // delete the audio transport
647    if (_audioTransport)
648    {
649        delete _audioTransport;
650        _audioTransport = NULL;
651    }
652
653    // release the AudioDeviceModule object
654    if (_audioDevice)
655    {
656        EXPECT_EQ(0, _audioDevice->Release());
657        _audioDevice = NULL;
658    }
659
660    // return the ThreadWrapper (singleton)
661    Trace::ReturnTrace();
662
663    // PRINT_TEST_RESULTS;
664
665    return 0;
666}
667
668int32_t FuncTestManager::DoTest(const TestType testType)
669{
670    switch (testType)
671    {
672        case TTAll:
673            TestAudioLayerSelection();
674            TestDeviceEnumeration();
675            TestDeviceSelection();
676            TestAudioTransport();
677            TestSpeakerVolume();
678            TestMicrophoneVolume();
679            TestLoopback();
680        case TTAudioLayerSelection:
681            TestAudioLayerSelection();
682            break;
683        case TTDeviceEnumeration:
684            TestDeviceEnumeration();
685            break;
686        case TTDeviceSelection:
687            TestDeviceSelection();
688            break;
689        case TTAudioTransport:
690            TestAudioTransport();
691            break;
692        case TTSpeakerVolume:
693            TestSpeakerVolume();
694            break;
695        case TTMicrophoneVolume:
696            TestMicrophoneVolume();
697            break;
698        case TTSpeakerMute:
699            TestSpeakerMute();
700            break;
701        case TTMicrophoneMute:
702            TestMicrophoneMute();
703            break;
704        case TTMicrophoneBoost:
705            TestMicrophoneBoost();
706            break;
707        case TTMicrophoneAGC:
708            TestMicrophoneAGC();
709            break;
710        case TTLoopback:
711            TestLoopback();
712            break;
713        case TTDeviceRemoval:
714            TestDeviceRemoval();
715            break;
716        case TTMobileAPI:
717            TestAdvancedMBAPI();
718        case TTTest:
719            TestExtra();
720            break;
721        default:
722            break;
723    }
724
725    return 0;
726}
727
728int32_t FuncTestManager::TestAudioLayerSelection()
729{
730    TEST_LOG("\n=======================================\n");
731    TEST_LOG(" Audio Layer test:\n");
732    TEST_LOG("=======================================\n");
733
734    if (_audioDevice == NULL)
735    {
736        return -1;
737    }
738
739    RESET_TEST;
740
741    AudioDeviceModule* audioDevice = _audioDevice;
742
743    AudioDeviceModule::AudioLayer audioLayer;
744    EXPECT_EQ(0, audioDevice->ActiveAudioLayer(&audioLayer));
745
746    if (audioLayer == AudioDeviceModule::kWindowsWaveAudio)
747    {
748        TEST_LOG("\nActiveAudioLayer: kWindowsWaveAudio\n \n");
749    } else if (audioLayer == AudioDeviceModule::kWindowsCoreAudio)
750    {
751        TEST_LOG("\nActiveAudioLayer: kWindowsCoreAudio\n \n");
752    } else if (audioLayer == AudioDeviceModule::kLinuxAlsaAudio)
753    {
754        TEST_LOG("\nActiveAudioLayer: kLinuxAlsaAudio\n \n");
755    } else if (audioLayer == AudioDeviceModule::kLinuxPulseAudio)
756    {
757        TEST_LOG("\nActiveAudioLayer: kLinuxPulseAudio\n \n");
758    } else
759    {
760        TEST_LOG("\nActiveAudioLayer: INVALID\n \n");
761    }
762
763    char ch;
764    bool tryWinWave(false);
765    bool tryWinCore(false);
766
767    if (audioLayer == AudioDeviceModule::kWindowsWaveAudio)
768    {
769        TEST_LOG("Would you like to try kWindowsCoreAudio instead "
770            "[requires Win Vista or Win 7] (Y/N)?\n: ");
771        EXPECT_TRUE(scanf(" %c", &ch) > 0);
772        ch = toupper(ch);
773        if (ch == 'Y')
774        {
775            tryWinCore = true;
776        }
777    } else if (audioLayer == AudioDeviceModule::kWindowsCoreAudio)
778    {
779        TEST_LOG("Would you like to try kWindowsWaveAudio instead (Y/N)?\n: ");
780        EXPECT_TRUE(scanf(" %c", &ch) > 0);
781        ch = toupper(ch);
782        if (ch == 'Y')
783        {
784            tryWinWave = true;
785        }
786    }
787
788    if (tryWinWave || tryWinCore)
789    {
790        // =======================================
791        // First, close down what we have started
792
793        // terminate
794        EXPECT_EQ(0, _audioDevice->RegisterEventObserver(NULL));
795        EXPECT_EQ(0, _audioDevice->RegisterAudioCallback(NULL));
796        EXPECT_EQ(0, _audioDevice->Terminate());
797
798        // release the ProcessThread object
799        if (_processThread)
800        {
801            _processThread->DeRegisterModule(_audioDevice);
802            _processThread->Stop();
803            ProcessThread::DestroyProcessThread(_processThread);
804        }
805
806        // delete the audio observer
807        if (_audioEventObserver)
808        {
809            delete _audioEventObserver;
810            _audioEventObserver = NULL;
811        }
812
813        // delete the audio transport
814        if (_audioTransport)
815        {
816            delete _audioTransport;
817            _audioTransport = NULL;
818        }
819
820        // release the AudioDeviceModule object
821        if (_audioDevice)
822        {
823            EXPECT_EQ(0, _audioDevice->Release());
824            _audioDevice = NULL;
825        }
826
827        // ==================================================
828        // Next, try to make fresh start with new audio layer
829
830        EXPECT_TRUE((_processThread = ProcessThread::CreateProcessThread()) != NULL);
831        if (_processThread == NULL)
832        {
833            return -1;
834        }
835        _processThread->Start();
836
837        // create the Audio Device module based on selected audio layer
838        if (tryWinWave)
839        {
840            _audioDevice = AudioDeviceModuleImpl::Create(
841                555,
842                AudioDeviceModule::kWindowsWaveAudio);
843        } else if (tryWinCore)
844        {
845            _audioDevice = AudioDeviceModuleImpl::Create(
846                555,
847                AudioDeviceModule::kWindowsCoreAudio);
848        }
849
850        if (_audioDevice == NULL)
851        {
852            TEST_LOG("\nERROR: Switch of audio layer failed!\n");
853            // restore default audio layer instead
854            EXPECT_TRUE((_audioDevice = AudioDeviceModuleImpl::Create(
855                555, AudioDeviceModule::kPlatformDefaultAudio)) != NULL);
856        }
857
858        if (_audioDevice == NULL)
859        {
860            TEST_LOG("\nERROR: Failed to revert back to default audio layer!\n");
861            return -1;
862        }
863
864        EXPECT_EQ(1, _audioDevice->AddRef());
865
866        // register the Audio Device module
867        _processThread->RegisterModule(_audioDevice);
868
869        // register event observer
870        _audioEventObserver = new AudioEventObserver(_audioDevice);
871        EXPECT_EQ(0, _audioDevice->RegisterEventObserver(_audioEventObserver));
872
873        // register audio transport
874        _audioTransport = new AudioTransportImpl(_audioDevice);
875        EXPECT_EQ(0, _audioDevice->RegisterAudioCallback(_audioTransport));
876
877        EXPECT_EQ(0, _audioDevice->ActiveAudioLayer(&audioLayer));
878
879        if (audioLayer == AudioDeviceModule::kWindowsWaveAudio)
880        {
881            if (tryWinCore)
882                TEST_LOG("\nActiveAudioLayer: kWindowsWaveAudio <=> "
883                    "switch was *not* possible\n \n");
884            else
885                TEST_LOG("\nActiveAudioLayer: kWindowsWaveAudio <=> "
886                    "switch was possible\n \n");
887        } else if (audioLayer == AudioDeviceModule::kWindowsCoreAudio)
888        {
889            if (tryWinWave)
890                TEST_LOG("\nActiveAudioLayer: kWindowsCoreAudio <=> "
891                    "switch was *not* possible\n \n");
892            else
893                TEST_LOG("\nActiveAudioLayer: kWindowsCoreAudio <=> "
894                    "switch was possible\n \n");
895        }
896    }  // if (tryWinWave || tryWinCore)
897
898    PRINT_TEST_RESULTS;
899
900    return 0;
901}
902
903int32_t FuncTestManager::TestDeviceEnumeration()
904{
905    TEST_LOG("\n=======================================\n");
906    TEST_LOG(" Device Enumeration test:\n");
907    TEST_LOG("=======================================\n");
908
909    if (_audioDevice == NULL)
910    {
911        return -1;
912    }
913
914    RESET_TEST;
915
916    AudioDeviceModule* audioDevice = _audioDevice;
917
918    EXPECT_EQ(0, audioDevice->Init());
919    EXPECT_TRUE(audioDevice->Initialized());
920
921    char name[kAdmMaxDeviceNameSize];
922    char guid[kAdmMaxGuidSize];
923
924    const int16_t nPlayoutDevices(audioDevice->PlayoutDevices());
925    EXPECT_TRUE(nPlayoutDevices >= 0);
926    TEST_LOG("\nPlayoutDevices: %u\n \n", nPlayoutDevices);
927    for (int n = 0; n < nPlayoutDevices; n++)
928    {
929        EXPECT_EQ(0, audioDevice->PlayoutDeviceName(n, name, guid));
930        TEST_LOG(
931                 "PlayoutDeviceName(%d) :   name=%s \n \
932                         guid=%s\n",
933                 n, name, guid);
934    }
935
936#ifdef _WIN32
937    // default (-1)
938    EXPECT_EQ(0, audioDevice->PlayoutDeviceName(-1, name, guid));
939    TEST_LOG("PlayoutDeviceName(%d):   default name=%s \n \
940                         default guid=%s\n", -1, name, guid);
941#else
942    // should fail
943    EXPECT_EQ(-1, audioDevice->PlayoutDeviceName(-1, name, guid));
944#endif
945
946    const int16_t nRecordingDevices(audioDevice->RecordingDevices());
947    EXPECT_TRUE(nRecordingDevices >= 0);
948    TEST_LOG("\nRecordingDevices: %u\n \n", nRecordingDevices);
949    for (int n = 0; n < nRecordingDevices; n++)
950    {
951        EXPECT_EQ(0, audioDevice->RecordingDeviceName(n, name, guid));
952        TEST_LOG(
953                 "RecordingDeviceName(%d) : name=%s \n \
954                         guid=%s\n",
955                 n, name, guid);
956    }
957
958#ifdef _WIN32
959    // default (-1)
960    EXPECT_EQ(0, audioDevice->RecordingDeviceName(-1, name, guid));
961    TEST_LOG("RecordingDeviceName(%d): default name=%s \n \
962                         default guid=%s\n", -1, name, guid);
963#else
964    // should fail
965    EXPECT_EQ(-1, audioDevice->PlayoutDeviceName(-1, name, guid));
966#endif
967
968    EXPECT_EQ(0, audioDevice->Terminate());
969    EXPECT_FALSE(audioDevice->Initialized());
970
971    PRINT_TEST_RESULTS;
972
973    return 0;
974}
975
976int32_t FuncTestManager::TestDeviceSelection()
977{
978    TEST_LOG("\n=======================================\n");
979    TEST_LOG(" Device Selection test:\n");
980    TEST_LOG("=======================================\n");
981
982    if (_audioDevice == NULL)
983    {
984        return -1;
985    }
986
987    RESET_TEST;
988
989#define PRINT_HEADING(a, b) \
990        { \
991                TEST_LOG("Set" #a "Device(" #b ") => \n"); \
992        } \
993
994#define PRINT_HEADING_IDX(a, b,c ) \
995        { \
996                TEST_LOG("Set" #a "Device(%d) (%s) => \n", b, c); \
997        } \
998
999#define PRINT_STR(a, b) \
1000        { \
1001                char str[128]; \
1002                (b == true) ? (sprintf(str, "  %-17s: available\n", #a)) : (sprintf(str, "  %-17s: NA\n", #a)); \
1003                TEST_LOG("%s", str); \
1004        } \
1005
1006    AudioDeviceModule* audioDevice = _audioDevice;
1007
1008    EXPECT_EQ(0, audioDevice->Init());
1009    EXPECT_TRUE(audioDevice->Initialized());
1010
1011    bool available(false);
1012    int16_t nDevices(-1);
1013    char name[kAdmMaxDeviceNameSize];
1014    char guid[kAdmMaxGuidSize];
1015
1016    // =======
1017    // Playout
1018
1019    nDevices = audioDevice->PlayoutDevices();
1020    EXPECT_TRUE(nDevices >= 0);
1021
1022    TEST_LOG("\n");
1023#ifdef _WIN32
1024    EXPECT_TRUE(audioDevice->SetPlayoutDevice(
1025        AudioDeviceModule::kDefaultCommunicationDevice) == 0);
1026    PRINT_HEADING(Playout, kDefaultCommunicationDevice);
1027    EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
1028    PRINT_STR(Playout, available);
1029    if (available)
1030    {
1031        EXPECT_EQ(0, audioDevice->StereoPlayoutIsAvailable(&available));
1032        PRINT_STR(Stereo Playout, available);
1033    }
1034    else
1035    {
1036        PRINT_STR(Stereo Playout, false);
1037    }
1038    EXPECT_EQ(0, audioDevice->SpeakerIsAvailable(&available));
1039    PRINT_STR(Speaker, available);
1040    EXPECT_EQ(0, audioDevice->SpeakerVolumeIsAvailable(&available));
1041    PRINT_STR(Speaker Volume, available);
1042    EXPECT_EQ(0, audioDevice->SpeakerMuteIsAvailable(&available));
1043    PRINT_STR(Speaker Mute, available);
1044
1045    EXPECT_EQ(0, audioDevice->SetPlayoutDevice(AudioDeviceModule::kDefaultDevice));
1046    PRINT_HEADING(Playout, kDefaultDevice);
1047    EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
1048    PRINT_STR(Playout, available);
1049    if (available)
1050    {
1051        EXPECT_EQ(0, audioDevice->StereoPlayoutIsAvailable(&available));
1052        PRINT_STR(Stereo Playout, available);
1053    }
1054    else
1055    {
1056        PRINT_STR(Stereo Playout, false);
1057    }
1058    EXPECT_EQ(0, audioDevice->SpeakerIsAvailable(&available));
1059    PRINT_STR(Speaker, available);
1060    EXPECT_EQ(0, audioDevice->SpeakerVolumeIsAvailable(&available));
1061    PRINT_STR(Speaker Volume, available);
1062    EXPECT_EQ(0, audioDevice->SpeakerMuteIsAvailable(&available));
1063    PRINT_STR(Speaker Mute, available);
1064#else
1065    EXPECT_TRUE(audioDevice->SetPlayoutDevice(
1066        AudioDeviceModule::kDefaultCommunicationDevice) == -1);
1067    EXPECT_EQ(-1, audioDevice->SetPlayoutDevice(AudioDeviceModule::kDefaultDevice));
1068#endif
1069
1070    for (int i = 0; i < nDevices; i++)
1071    {
1072        EXPECT_EQ(0, audioDevice->SetPlayoutDevice(i));
1073        EXPECT_EQ(0, audioDevice->PlayoutDeviceName(i, name, guid));
1074        PRINT_HEADING_IDX(Playout, i, name);
1075        EXPECT_EQ(0, audioDevice->PlayoutIsAvailable(&available));
1076        PRINT_STR(Playout, available);
1077        if (available)
1078        {
1079            EXPECT_EQ(0, audioDevice->StereoPlayoutIsAvailable(&available));
1080            PRINT_STR(Stereo Playout, available);
1081        } else
1082        {
1083            PRINT_STR(Stereo Playout, false);
1084        }
1085        EXPECT_EQ(0, audioDevice->SpeakerIsAvailable(&available));
1086        PRINT_STR(Speaker, available);
1087        EXPECT_EQ(0, audioDevice->SpeakerVolumeIsAvailable(&available));
1088        PRINT_STR(Speaker Volume, available);
1089        EXPECT_EQ(0, audioDevice->SpeakerMuteIsAvailable(&available));
1090        PRINT_STR(Speaker Mute, available);
1091    }
1092
1093    // =========
1094    // Recording
1095
1096    nDevices = audioDevice->RecordingDevices();
1097    EXPECT_TRUE(nDevices >= 0);
1098
1099    TEST_LOG("\n");
1100#ifdef _WIN32
1101    EXPECT_TRUE(audioDevice->SetRecordingDevice(
1102        AudioDeviceModule::kDefaultCommunicationDevice) == 0);
1103    PRINT_HEADING(Recording, kDefaultCommunicationDevice);
1104    EXPECT_EQ(0, audioDevice->RecordingIsAvailable(&available));
1105    PRINT_STR(Recording, available);
1106    if (available)
1107    {
1108        EXPECT_EQ(0, audioDevice->StereoRecordingIsAvailable(&available));
1109        PRINT_STR(Stereo Recording, available);
1110    }
1111    else
1112    {
1113        // special fix to ensure that we don't log 'available' when recording is not OK
1114        PRINT_STR(Stereo Recording, false);
1115    }
1116    EXPECT_EQ(0, audioDevice->MicrophoneIsAvailable(&available));
1117    PRINT_STR(Microphone, available);
1118    EXPECT_EQ(0, audioDevice->MicrophoneVolumeIsAvailable(&available));
1119    PRINT_STR(Microphone Volume, available);
1120    EXPECT_EQ(0, audioDevice->MicrophoneMuteIsAvailable(&available));
1121    PRINT_STR(Microphone Mute, available);
1122    EXPECT_EQ(0, audioDevice->MicrophoneBoostIsAvailable(&available));
1123    PRINT_STR(Microphone Boost, available);
1124
1125    EXPECT_EQ(0, audioDevice->SetRecordingDevice(AudioDeviceModule::kDefaultDevice));
1126    PRINT_HEADING(Recording, kDefaultDevice);
1127    EXPECT_EQ(0, audioDevice->RecordingIsAvailable(&available));
1128    PRINT_STR(Recording, available);
1129    if (available)
1130    {
1131        EXPECT_EQ(0, audioDevice->StereoRecordingIsAvailable(&available));
1132        PRINT_STR(Stereo Recording, available);
1133    }
1134    else
1135    {
1136        // special fix to ensure that we don't log 'available' when recording is not OK
1137        PRINT_STR(Stereo Recording, false);
1138    }
1139    EXPECT_EQ(0, audioDevice->MicrophoneIsAvailable(&available));
1140    PRINT_STR(Microphone, available);
1141    EXPECT_EQ(0, audioDevice->MicrophoneVolumeIsAvailable(&available));
1142    PRINT_STR(Microphone Volume, available);
1143    EXPECT_EQ(0, audioDevice->MicrophoneMuteIsAvailable(&available));
1144    PRINT_STR(Microphone Mute, available);
1145    EXPECT_EQ(0, audioDevice->MicrophoneBoostIsAvailable(&available));
1146    PRINT_STR(Microphone Boost, available);
1147#else
1148    EXPECT_TRUE(audioDevice->SetRecordingDevice(
1149        AudioDeviceModule::kDefaultCommunicationDevice) == -1);
1150    EXPECT_EQ(-1, audioDevice->SetRecordingDevice(AudioDeviceModule::kDefaultDevice));
1151#endif
1152
1153    for (int i = 0; i < nDevices; i++)
1154    {
1155        EXPECT_EQ(0, audioDevice->SetRecordingDevice(i));
1156        EXPECT_EQ(0, audioDevice->RecordingDeviceName(i, name, guid));
1157        PRINT_HEADING_IDX(Recording, i, name);
1158        EXPECT_EQ(0, audioDevice->RecordingIsAvailable(&available));
1159        PRINT_STR(Recording, available);
1160        if (available)
1161        {
1162            EXPECT_EQ(0, audioDevice->StereoRecordingIsAvailable(&available));
1163            PRINT_STR(Stereo Recording, available);
1164        } else
1165        {
1166            // special fix to ensure that we don't log 'available' when recording
1167            // is not OK
1168            PRINT_STR(Stereo Recording, false);
1169        }
1170        EXPECT_EQ(0, audioDevice->MicrophoneIsAvailable(&available));
1171        PRINT_STR(Microphone, available);
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
1188int32_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
1436int32_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
1537int32_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
1632int32_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
1771int32_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
1908int32_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
2045int32_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
2184int32_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
2310int32_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
2487int32_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
2514int32_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
2576int32_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
2641int32_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
Note: See TracBrowser for help on using the repository browser.