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/audio_device_test_api.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: 68.4 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 <math.h>
12#include <stdio.h>
13#include <string.h>
14
15#include "webrtc/modules/audio_device/test/audio_device_test_defines.h"
16
17#include "testing/gtest/include/gtest/gtest.h"
18#include "webrtc/test/testsupport/fileutils.h"
19
20#include "webrtc/modules/audio_device/audio_device_config.h"
21#include "webrtc/modules/audio_device/audio_device_impl.h"
22#include "webrtc/modules/audio_device/audio_device_utility.h"
23#include "webrtc/system_wrappers/interface/sleep.h"
24
25// Helper functions
26#if defined(ANDROID)
27char filenameStr[2][256] =
28{ {0},
29  {0},
30}; // Allow two buffers for those API calls taking two filenames
31int currentStr = 0;
32
33const char* GetFilename(const char* filename)
34{
35  currentStr = !currentStr;
36  sprintf(filenameStr[currentStr], "/sdcard/admtest/%s", filename);
37  return filenameStr[currentStr];
38}
39#elif !defined(WEBRTC_IOS)
40const char* GetFilename(const char* filename) {
41  std::string full_path_filename = webrtc::test::OutputPath() + filename;
42  return full_path_filename.c_str();
43}
44#endif
45
46using namespace webrtc;
47
48class AudioEventObserverAPI: public AudioDeviceObserver {
49 public:
50  AudioEventObserverAPI(AudioDeviceModule* audioDevice)
51      : error_(kRecordingError),
52        warning_(kRecordingWarning),
53        audio_device_(audioDevice) {
54  }
55
56  ~AudioEventObserverAPI() {}
57
58  virtual void OnErrorIsReported(const ErrorCode error) {
59    TEST_LOG("\n[*** ERROR ***] => OnErrorIsReported(%d)\n\n", error);
60    error_ = error;
61  }
62
63  virtual void OnWarningIsReported(const WarningCode warning) {
64    TEST_LOG("\n[*** WARNING ***] => OnWarningIsReported(%d)\n\n", warning);
65    warning_ = warning;
66    EXPECT_EQ(0, audio_device_->StopRecording());
67    EXPECT_EQ(0, audio_device_->StopPlayout());
68  }
69
70 public:
71  ErrorCode error_;
72  WarningCode warning_;
73 private:
74  AudioDeviceModule* audio_device_;
75};
76
77class AudioTransportAPI: public AudioTransport {
78 public:
79  AudioTransportAPI(AudioDeviceModule* audioDevice)
80      : rec_count_(0),
81        play_count_(0) {
82  }
83
84  ~AudioTransportAPI() {}
85
86  virtual int32_t RecordedDataIsAvailable(
87      const void* audioSamples,
88      const uint32_t nSamples,
89      const uint8_t nBytesPerSample,
90      const uint8_t nChannels,
91      const uint32_t sampleRate,
92      const uint32_t totalDelay,
93      const int32_t clockSkew,
94      const uint32_t currentMicLevel,
95      const bool keyPressed,
96      uint32_t& newMicLevel) {
97    rec_count_++;
98    if (rec_count_ % 100 == 0) {
99      if (nChannels == 1) {
100        // mono
101        TEST_LOG("-");
102      } else if ((nChannels == 2) && (nBytesPerSample == 2)) {
103        // stereo but only using one channel
104        TEST_LOG("-|");
105      } else {
106        // stereo
107        TEST_LOG("--");
108      }
109    }
110    return 0;
111  }
112
113  virtual int32_t NeedMorePlayData(
114      const uint32_t nSamples,
115      const uint8_t nBytesPerSample,
116      const uint8_t nChannels,
117      const uint32_t sampleRate,
118      void* audioSamples,
119      uint32_t& nSamplesOut) {
120    play_count_++;
121    if (play_count_ % 100 == 0) {
122      if (nChannels == 1) {
123        TEST_LOG("+");
124      } else {
125        TEST_LOG("++");
126      }
127    }
128    nSamplesOut = 480;
129    return 0;
130  }
131
132  virtual int OnDataAvailable(const int voe_channels[],
133                              int number_of_voe_channels,
134                              const int16_t* audio_data,
135                              int sample_rate,
136                              int number_of_channels,
137                              int number_of_frames,
138                              int audio_delay_milliseconds,
139                              int current_volume,
140                              bool key_pressed,
141                              bool need_audio_processing) {
142    return 0;
143  }
144
145 private:
146  uint32_t rec_count_;
147  uint32_t play_count_;
148};
149
150class AudioDeviceAPITest: public testing::Test {
151 protected:
152  AudioDeviceAPITest() {}
153
154  virtual ~AudioDeviceAPITest() {}
155
156  static void SetUpTestCase() {
157    process_thread_ = ProcessThread::CreateProcessThread();
158    process_thread_->Start();
159
160    // Windows:
161    //      if (WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
162    //          user can select between default (Core) or Wave
163    //      else
164    //          user can select between default (Wave) or Wave
165    const int32_t kId = 444;
166
167#if defined(_WIN32)
168    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
169                kId, AudioDeviceModule::kLinuxAlsaAudio)) == NULL);
170#if defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
171    TEST_LOG("WEBRTC_WINDOWS_CORE_AUDIO_BUILD is defined!\n\n");
172    // create default implementation (=Core Audio) instance
173    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
174                kId, AudioDeviceModule::kPlatformDefaultAudio)) != NULL);
175    audio_device_->AddRef();
176    EXPECT_EQ(0, audio_device_->Release());
177    // create non-default (=Wave Audio) instance
178    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
179                kId, AudioDeviceModule::kWindowsWaveAudio)) != NULL);
180    audio_device_->AddRef();
181    EXPECT_EQ(0, audio_device_->Release());
182    // explicitly specify usage of Core Audio (same as default)
183    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
184                kId, AudioDeviceModule::kWindowsCoreAudio)) != NULL);
185#else
186    TEST_LOG("WEBRTC_WINDOWS_CORE_AUDIO_BUILD is *not* defined!\n");
187    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
188                kId, AudioDeviceModule::kWindowsCoreAudio)) == NULL);
189    // create default implementation (=Wave Audio) instance
190    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
191                kId, AudioDeviceModule::kPlatformDefaultAudio)) != NULL);
192    audio_device_->AddRef();
193    EXPECT_EQ(0, audio_device_->Release());
194    // explicitly specify usage of Wave Audio (same as default)
195    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
196                kId, AudioDeviceModule::kWindowsWaveAudio)) != NULL);
197#endif
198#endif
199
200#if defined(ANDROID)
201    // Fails tests
202    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
203                kId, AudioDeviceModule::kWindowsWaveAudio)) == NULL);
204    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
205                kId, AudioDeviceModule::kWindowsCoreAudio)) == NULL);
206    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
207                kId, AudioDeviceModule::kLinuxAlsaAudio)) == NULL);
208    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
209                kId, AudioDeviceModule::kLinuxPulseAudio)) == NULL);
210    // Create default implementation instance
211    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
212                kId, AudioDeviceModule::kPlatformDefaultAudio)) != NULL);
213#elif defined(WEBRTC_LINUX)
214    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
215                kId, AudioDeviceModule::kWindowsWaveAudio)) == NULL);
216    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
217                kId, AudioDeviceModule::kWindowsCoreAudio)) == NULL);
218    // create default implementation instance
219    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
220                kId, AudioDeviceModule::kPlatformDefaultAudio)) != NULL);
221    audio_device_->AddRef();
222    EXPECT_EQ(0, audio_device_->Terminate());
223    EXPECT_EQ(0, audio_device_->Release());
224    // explicitly specify usage of Pulse Audio (same as default)
225    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
226                kId, AudioDeviceModule::kLinuxPulseAudio)) != NULL);
227#endif
228
229#if defined(WEBRTC_MAC)
230    // Fails tests
231    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
232                kId, AudioDeviceModule::kWindowsWaveAudio)) == NULL);
233    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
234                kId, AudioDeviceModule::kWindowsCoreAudio)) == NULL);
235    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
236                kId, AudioDeviceModule::kLinuxAlsaAudio)) == NULL);
237    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
238                kId, AudioDeviceModule::kLinuxPulseAudio)) == NULL);
239    // Create default implementation instance
240    EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create(
241                kId, AudioDeviceModule::kPlatformDefaultAudio)) != NULL);
242#endif
243
244    if (audio_device_ == NULL) {
245      FAIL() << "Failed creating audio device object!";
246    }
247
248    // The ADM is reference counted.
249    audio_device_->AddRef();
250
251    process_thread_->RegisterModule(audio_device_);
252
253    AudioDeviceModule::AudioLayer audio_layer =
254        AudioDeviceModule::kPlatformDefaultAudio;
255    EXPECT_EQ(0, audio_device_->ActiveAudioLayer(&audio_layer));
256    if (audio_layer == AudioDeviceModule::kLinuxAlsaAudio) {
257      linux_alsa_ = true;
258    }
259  }
260
261  static void TearDownTestCase() {
262    if (process_thread_) {
263      process_thread_->DeRegisterModule(audio_device_);
264      process_thread_->Stop();
265      ProcessThread::DestroyProcessThread(process_thread_);
266    }
267    if (event_observer_) {
268      delete event_observer_;
269      event_observer_ = NULL;
270    }
271    if (audio_transport_) {
272      delete audio_transport_;
273      audio_transport_ = NULL;
274    }
275    if (audio_device_) {
276      EXPECT_EQ(0, audio_device_->Release());
277    }
278    PRINT_TEST_RESULTS;
279  }
280
281  void SetUp() {
282    if (linux_alsa_) {
283      FAIL() << "API Test is not available on ALSA on Linux!";
284    }
285    EXPECT_EQ(0, audio_device_->Init());
286    EXPECT_TRUE(audio_device_->Initialized());
287  }
288
289  void TearDown() {
290    EXPECT_EQ(0, audio_device_->Terminate());
291  }
292
293  void CheckVolume(uint32_t expected, uint32_t actual) {
294    // Mac and Windows have lower resolution on the volume settings.
295#if defined(WEBRTC_MAC) || defined(_WIN32)
296    int diff = abs(static_cast<int>(expected - actual));
297    EXPECT_LE(diff, 5);
298#else
299    EXPECT_TRUE((actual == expected) || (actual == expected-1));
300#endif
301  }
302
303  void CheckInitialPlayoutStates() {
304    EXPECT_FALSE(audio_device_->PlayoutIsInitialized());
305    EXPECT_FALSE(audio_device_->Playing());
306    EXPECT_FALSE(audio_device_->SpeakerIsInitialized());
307  }
308
309  void CheckInitialRecordingStates() {
310    EXPECT_FALSE(audio_device_->RecordingIsInitialized());
311    EXPECT_FALSE(audio_device_->Recording());
312    EXPECT_FALSE(audio_device_->MicrophoneIsInitialized());
313  }
314
315  static bool linux_alsa_;
316  static ProcessThread* process_thread_;
317  static AudioDeviceModule* audio_device_;
318  static AudioTransportAPI* audio_transport_;
319  static AudioEventObserverAPI* event_observer_;
320};
321
322// Must be initialized like this to handle static SetUpTestCase() above.
323bool AudioDeviceAPITest::linux_alsa_ = false;
324ProcessThread* AudioDeviceAPITest::process_thread_ = NULL;
325AudioDeviceModule* AudioDeviceAPITest::audio_device_ = NULL;
326AudioTransportAPI* AudioDeviceAPITest::audio_transport_ = NULL;
327AudioEventObserverAPI* AudioDeviceAPITest::event_observer_ = NULL;
328
329TEST_F(AudioDeviceAPITest, RegisterEventObserver) {
330  event_observer_ = new AudioEventObserverAPI(audio_device_);
331  EXPECT_EQ(0, audio_device_->RegisterEventObserver(NULL));
332  EXPECT_EQ(0, audio_device_->RegisterEventObserver(event_observer_));
333  EXPECT_EQ(0, audio_device_->RegisterEventObserver(NULL));
334}
335
336TEST_F(AudioDeviceAPITest, RegisterAudioCallback) {
337  audio_transport_ = new AudioTransportAPI(audio_device_);
338  EXPECT_EQ(0, audio_device_->RegisterAudioCallback(NULL));
339  EXPECT_EQ(0, audio_device_->RegisterAudioCallback(audio_transport_));
340  EXPECT_EQ(0, audio_device_->RegisterAudioCallback(NULL));
341}
342
343TEST_F(AudioDeviceAPITest, Init) {
344  EXPECT_TRUE(audio_device_->Initialized());
345  EXPECT_EQ(0, audio_device_->Init());
346  EXPECT_TRUE(audio_device_->Initialized());
347  EXPECT_EQ(0, audio_device_->Terminate());
348  EXPECT_FALSE(audio_device_->Initialized());
349  EXPECT_EQ(0, audio_device_->Init());
350  EXPECT_TRUE(audio_device_->Initialized());
351  EXPECT_EQ(0, audio_device_->Terminate());
352  EXPECT_FALSE(audio_device_->Initialized());
353}
354
355TEST_F(AudioDeviceAPITest, Terminate) {
356  EXPECT_TRUE(audio_device_->Initialized());
357  EXPECT_EQ(0, audio_device_->Terminate());
358  EXPECT_FALSE(audio_device_->Initialized());
359  EXPECT_EQ(0, audio_device_->Terminate());
360  EXPECT_FALSE(audio_device_->Initialized());
361  EXPECT_EQ(0, audio_device_->Init());
362  EXPECT_TRUE(audio_device_->Initialized());
363  EXPECT_EQ(0, audio_device_->Terminate());
364  EXPECT_FALSE(audio_device_->Initialized());
365}
366
367TEST_F(AudioDeviceAPITest, PlayoutDevices) {
368  EXPECT_GT(audio_device_->PlayoutDevices(), 0);
369  EXPECT_GT(audio_device_->PlayoutDevices(), 0);
370}
371
372TEST_F(AudioDeviceAPITest, RecordingDevices) {
373  EXPECT_GT(audio_device_->RecordingDevices(), 0);
374  EXPECT_GT(audio_device_->RecordingDevices(), 0);
375}
376
377TEST_F(AudioDeviceAPITest, PlayoutDeviceName) {
378  char name[kAdmMaxDeviceNameSize];
379  char guid[kAdmMaxGuidSize];
380  int16_t no_devices = audio_device_->PlayoutDevices();
381
382  // fail tests
383  EXPECT_EQ(-1, audio_device_->PlayoutDeviceName(-2, name, guid));
384  EXPECT_EQ(-1, audio_device_->PlayoutDeviceName(no_devices, name, guid));
385  EXPECT_EQ(-1, audio_device_->PlayoutDeviceName(0, NULL, guid));
386
387  // bulk tests
388  EXPECT_EQ(0, audio_device_->PlayoutDeviceName(0, name, NULL));
389#ifdef _WIN32
390  // shall be mapped to 0.
391  EXPECT_EQ(0, audio_device_->PlayoutDeviceName(-1, name, NULL));
392#else
393  EXPECT_EQ(-1, audio_device_->PlayoutDeviceName(-1, name, NULL));
394#endif
395  for (int i = 0; i < no_devices; i++) {
396    EXPECT_EQ(0, audio_device_->PlayoutDeviceName(i, name, guid));
397    EXPECT_EQ(0, audio_device_->PlayoutDeviceName(i, name, NULL));
398  }
399}
400
401TEST_F(AudioDeviceAPITest, RecordingDeviceName) {
402  char name[kAdmMaxDeviceNameSize];
403  char guid[kAdmMaxGuidSize];
404  int16_t no_devices = audio_device_->RecordingDevices();
405
406  // fail tests
407  EXPECT_EQ(-1, audio_device_->RecordingDeviceName(-2, name, guid));
408  EXPECT_EQ(-1, audio_device_->RecordingDeviceName(no_devices, name, guid));
409  EXPECT_EQ(-1, audio_device_->RecordingDeviceName(0, NULL, guid));
410
411  // bulk tests
412  EXPECT_EQ(0, audio_device_->RecordingDeviceName(0, name, NULL));
413#ifdef _WIN32
414  // shall me mapped to 0
415  EXPECT_EQ(0, audio_device_->RecordingDeviceName(-1, name, NULL));
416#else
417  EXPECT_EQ(-1, audio_device_->RecordingDeviceName(-1, name, NULL));
418#endif
419  for (int i = 0; i < no_devices; i++) {
420    EXPECT_EQ(0, audio_device_->RecordingDeviceName(i, name, guid));
421    EXPECT_EQ(0, audio_device_->RecordingDeviceName(i, name, NULL));
422  }
423}
424
425TEST_F(AudioDeviceAPITest, SetPlayoutDevice) {
426  int16_t no_devices = audio_device_->PlayoutDevices();
427
428  // fail tests
429  EXPECT_EQ(-1, audio_device_->SetPlayoutDevice(-1));
430  EXPECT_EQ(-1, audio_device_->SetPlayoutDevice(no_devices));
431
432  // bulk tests
433#ifdef _WIN32
434  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(
435      AudioDeviceModule::kDefaultCommunicationDevice));
436  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(
437      AudioDeviceModule::kDefaultDevice));
438#else
439  EXPECT_EQ(-1, audio_device_->SetPlayoutDevice(
440      AudioDeviceModule::kDefaultCommunicationDevice));
441  EXPECT_EQ(-1, audio_device_->SetPlayoutDevice(
442      AudioDeviceModule::kDefaultDevice));
443#endif
444  for (int i = 0; i < no_devices; i++) {
445    EXPECT_EQ(0, audio_device_->SetPlayoutDevice(i));
446  }
447}
448
449TEST_F(AudioDeviceAPITest, SetRecordingDevice) {
450  EXPECT_EQ(0, audio_device_->Init());
451  int16_t no_devices = audio_device_->RecordingDevices();
452
453  // fail tests
454  EXPECT_EQ(-1, audio_device_->SetRecordingDevice(-1));
455  EXPECT_EQ(-1, audio_device_->SetRecordingDevice(no_devices));
456
457  // bulk tests
458#ifdef _WIN32
459  EXPECT_TRUE(audio_device_->SetRecordingDevice(
460          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
461  EXPECT_EQ(0, audio_device_->SetRecordingDevice(
462      AudioDeviceModule::kDefaultDevice));
463#else
464  EXPECT_TRUE(audio_device_->SetRecordingDevice(
465      AudioDeviceModule::kDefaultCommunicationDevice) == -1);
466  EXPECT_TRUE(audio_device_->SetRecordingDevice(
467      AudioDeviceModule::kDefaultDevice) == -1);
468#endif
469  for (int i = 0; i < no_devices; i++) {
470    EXPECT_EQ(0, audio_device_->SetRecordingDevice(i));
471  }
472}
473
474TEST_F(AudioDeviceAPITest, PlayoutIsAvailable) {
475  bool available;
476#ifdef _WIN32
477  EXPECT_TRUE(audio_device_->SetPlayoutDevice(
478          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
479  EXPECT_EQ(0, audio_device_->PlayoutIsAvailable(&available));
480  // Availability check should not initialize.
481  EXPECT_FALSE(audio_device_->PlayoutIsInitialized());
482
483  EXPECT_EQ(0,
484            audio_device_->SetPlayoutDevice(AudioDeviceModule::kDefaultDevice));
485  EXPECT_EQ(0, audio_device_->PlayoutIsAvailable(&available));
486  EXPECT_FALSE(audio_device_->PlayoutIsInitialized());
487#endif
488
489  int16_t no_devices = audio_device_->PlayoutDevices();
490  for (int i = 0; i < no_devices; i++) {
491    EXPECT_EQ(0, audio_device_->SetPlayoutDevice(i));
492    EXPECT_EQ(0, audio_device_->PlayoutIsAvailable(&available));
493    EXPECT_FALSE(audio_device_->PlayoutIsInitialized());
494  }
495}
496
497TEST_F(AudioDeviceAPITest, RecordingIsAvailable) {
498  bool available;
499#ifdef _WIN32
500  EXPECT_EQ(0, audio_device_->SetRecordingDevice(
501      AudioDeviceModule::kDefaultCommunicationDevice));
502  EXPECT_EQ(0, audio_device_->RecordingIsAvailable(&available));
503  EXPECT_FALSE(audio_device_->RecordingIsInitialized());
504
505  EXPECT_EQ(0, audio_device_->SetRecordingDevice(
506      AudioDeviceModule::kDefaultDevice));
507  EXPECT_EQ(0, audio_device_->RecordingIsAvailable(&available));
508  EXPECT_FALSE(audio_device_->RecordingIsInitialized());
509#endif
510
511  int16_t no_devices = audio_device_->RecordingDevices();
512  for (int i = 0; i < no_devices; i++) {
513    EXPECT_EQ(0, audio_device_->SetRecordingDevice(i));
514    EXPECT_EQ(0, audio_device_->RecordingIsAvailable(&available));
515    EXPECT_FALSE(audio_device_->RecordingIsInitialized());
516  }
517}
518
519TEST_F(AudioDeviceAPITest, InitPlayout) {
520  // check initial state
521  EXPECT_FALSE(audio_device_->PlayoutIsInitialized());
522
523  // ensure that device must be set before we can initialize
524  EXPECT_EQ(-1, audio_device_->InitPlayout());
525  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(MACRO_DEFAULT_DEVICE));
526  EXPECT_EQ(0, audio_device_->InitPlayout());
527  EXPECT_TRUE(audio_device_->PlayoutIsInitialized());
528
529  // bulk tests
530  bool available;
531  EXPECT_EQ(0, audio_device_->PlayoutIsAvailable(&available));
532  if (available) {
533    EXPECT_EQ(0, audio_device_->InitPlayout());
534    EXPECT_TRUE(audio_device_->PlayoutIsInitialized());
535    EXPECT_EQ(0, audio_device_->InitPlayout());
536    EXPECT_EQ(-1, audio_device_->SetPlayoutDevice(
537        MACRO_DEFAULT_COMMUNICATION_DEVICE));
538    EXPECT_EQ(0, audio_device_->StopPlayout());
539    EXPECT_FALSE(audio_device_->PlayoutIsInitialized());
540  }
541
542  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(
543      MACRO_DEFAULT_COMMUNICATION_DEVICE));
544  EXPECT_EQ(0, audio_device_->PlayoutIsAvailable(&available));
545  if (available) {
546    EXPECT_EQ(0, audio_device_->InitPlayout());
547    // Sleep is needed for e.g. iPhone since we after stopping then starting may
548    // have a hangover time of a couple of ms before initialized.
549    SleepMs(50);
550    EXPECT_TRUE(audio_device_->PlayoutIsInitialized());
551  }
552
553  int16_t no_devices = audio_device_->PlayoutDevices();
554  for (int i = 0; i < no_devices; i++) {
555    EXPECT_EQ(0, audio_device_->PlayoutIsAvailable(&available));
556    if (available) {
557      EXPECT_EQ(0, audio_device_->StopPlayout());
558      EXPECT_FALSE(audio_device_->PlayoutIsInitialized());
559      EXPECT_EQ(0, audio_device_->SetPlayoutDevice(i));
560      EXPECT_EQ(0, audio_device_->PlayoutIsAvailable(&available));
561      if (available) {
562        EXPECT_EQ(0, audio_device_->InitPlayout());
563        EXPECT_TRUE(audio_device_->PlayoutIsInitialized());
564      }
565    }
566  }
567  EXPECT_EQ(0, audio_device_->StopPlayout());
568}
569
570TEST_F(AudioDeviceAPITest, InitRecording) {
571  // check initial state
572  EXPECT_FALSE(audio_device_->RecordingIsInitialized());
573
574  // ensure that device must be set before we can initialize
575  EXPECT_EQ(-1, audio_device_->InitRecording());
576  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
577  EXPECT_EQ(0, audio_device_->InitRecording());
578  EXPECT_TRUE(audio_device_->RecordingIsInitialized());
579
580  // bulk tests
581  bool available;
582  EXPECT_EQ(0, audio_device_->RecordingIsAvailable(&available));
583  if (available) {
584    EXPECT_EQ(0, audio_device_->InitRecording());
585    EXPECT_TRUE(audio_device_->RecordingIsInitialized());
586    EXPECT_EQ(0, audio_device_->InitRecording());
587    EXPECT_EQ(-1,
588        audio_device_->SetRecordingDevice(MACRO_DEFAULT_COMMUNICATION_DEVICE));
589    EXPECT_EQ(0, audio_device_->StopRecording());
590    EXPECT_FALSE(audio_device_->RecordingIsInitialized());
591  }
592
593  EXPECT_EQ(0,
594      audio_device_->SetRecordingDevice(MACRO_DEFAULT_COMMUNICATION_DEVICE));
595  EXPECT_EQ(0, audio_device_->RecordingIsAvailable(&available));
596  if (available) {
597    EXPECT_EQ(0, audio_device_->InitRecording());
598    SleepMs(50);
599    EXPECT_TRUE(audio_device_->RecordingIsInitialized());
600  }
601
602  int16_t no_devices = audio_device_->RecordingDevices();
603  for (int i = 0; i < no_devices; i++) {
604    EXPECT_EQ(0, audio_device_->RecordingIsAvailable(&available));
605    if (available) {
606      EXPECT_EQ(0, audio_device_->StopRecording());
607      EXPECT_FALSE(audio_device_->RecordingIsInitialized());
608      EXPECT_EQ(0, audio_device_->SetRecordingDevice(i));
609      EXPECT_EQ(0, audio_device_->RecordingIsAvailable(&available));
610      if (available) {
611        EXPECT_EQ(0, audio_device_->InitRecording());
612        EXPECT_TRUE(audio_device_->RecordingIsInitialized());
613      }
614    }
615  }
616  EXPECT_EQ(0, audio_device_->StopRecording());
617}
618
619TEST_F(AudioDeviceAPITest, StartAndStopPlayout) {
620  bool available;
621  EXPECT_EQ(0, audio_device_->RegisterAudioCallback(NULL));
622
623  CheckInitialPlayoutStates();
624
625  EXPECT_EQ(-1, audio_device_->StartPlayout());
626  EXPECT_EQ(0, audio_device_->StopPlayout());
627
628#ifdef _WIN32
629  // kDefaultCommunicationDevice
630  EXPECT_TRUE(audio_device_->SetPlayoutDevice(
631          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
632  EXPECT_EQ(0, audio_device_->PlayoutIsAvailable(&available));
633  if (available)
634  {
635    EXPECT_FALSE(audio_device_->PlayoutIsInitialized());
636    EXPECT_EQ(0, audio_device_->InitPlayout());
637    EXPECT_EQ(0, audio_device_->StartPlayout());
638    EXPECT_TRUE(audio_device_->Playing());
639    EXPECT_EQ(0, audio_device_->RegisterAudioCallback(audio_transport_));
640    EXPECT_EQ(0, audio_device_->StopPlayout());
641    EXPECT_FALSE(audio_device_->Playing());
642    EXPECT_EQ(0, audio_device_->RegisterAudioCallback(NULL));
643  }
644#endif
645
646  // repeat test but for kDefaultDevice
647  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(MACRO_DEFAULT_DEVICE));
648  EXPECT_EQ(0, audio_device_->PlayoutIsAvailable(&available));
649  if (available) {
650    EXPECT_FALSE(audio_device_->PlayoutIsInitialized());
651    EXPECT_EQ(0, audio_device_->InitPlayout());
652    EXPECT_EQ(0, audio_device_->StartPlayout());
653    EXPECT_TRUE(audio_device_->Playing());
654    EXPECT_EQ(0, audio_device_->RegisterAudioCallback(audio_transport_));
655    EXPECT_EQ(0, audio_device_->StopPlayout());
656    EXPECT_FALSE(audio_device_->Playing());
657  }
658
659  // repeat test for all devices
660  int16_t no_devices = audio_device_->PlayoutDevices();
661  for (int i = 0; i < no_devices; i++) {
662    EXPECT_EQ(0, audio_device_->SetPlayoutDevice(i));
663    EXPECT_EQ(0, audio_device_->PlayoutIsAvailable(&available));
664    if (available) {
665      EXPECT_FALSE(audio_device_->PlayoutIsInitialized());
666      EXPECT_EQ(0, audio_device_->InitPlayout());
667      EXPECT_EQ(0, audio_device_->StartPlayout());
668      EXPECT_TRUE(audio_device_->Playing());
669      EXPECT_EQ(0, audio_device_->RegisterAudioCallback(audio_transport_));
670      EXPECT_EQ(0, audio_device_->StopPlayout());
671      EXPECT_FALSE(audio_device_->Playing());
672    }
673  }
674}
675
676TEST_F(AudioDeviceAPITest, StartAndStopRecording) {
677  bool available;
678  EXPECT_EQ(0, audio_device_->RegisterAudioCallback(NULL));
679
680  CheckInitialRecordingStates();
681
682  EXPECT_EQ(-1, audio_device_->StartRecording());
683  EXPECT_EQ(0, audio_device_->StopRecording());
684
685#ifdef _WIN32
686  // kDefaultCommunicationDevice
687  EXPECT_TRUE(audio_device_->SetRecordingDevice(
688          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
689  EXPECT_EQ(0, audio_device_->RecordingIsAvailable(&available));
690  if (available)
691  {
692    EXPECT_FALSE(audio_device_->RecordingIsInitialized());
693    EXPECT_EQ(0, audio_device_->InitRecording());
694    EXPECT_EQ(0, audio_device_->StartRecording());
695    EXPECT_TRUE(audio_device_->Recording());
696    EXPECT_EQ(0, audio_device_->RegisterAudioCallback(audio_transport_));
697    EXPECT_EQ(0, audio_device_->StopRecording());
698    EXPECT_FALSE(audio_device_->Recording());
699    EXPECT_EQ(0, audio_device_->RegisterAudioCallback(NULL));
700  }
701#endif
702
703  // repeat test but for kDefaultDevice
704  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
705  EXPECT_EQ(0, audio_device_->RecordingIsAvailable(&available));
706  if (available) {
707    EXPECT_FALSE(audio_device_->RecordingIsInitialized());
708    EXPECT_EQ(0, audio_device_->InitRecording());
709    EXPECT_EQ(0, audio_device_->StartRecording());
710    EXPECT_TRUE(audio_device_->Recording());
711    EXPECT_EQ(0, audio_device_->RegisterAudioCallback(audio_transport_));
712    EXPECT_EQ(0, audio_device_->StopRecording());
713    EXPECT_FALSE(audio_device_->Recording());
714  }
715
716  // repeat test for all devices
717  int16_t no_devices = audio_device_->RecordingDevices();
718  for (int i = 0; i < no_devices; i++) {
719    EXPECT_EQ(0, audio_device_->SetRecordingDevice(i));
720    EXPECT_EQ(0, audio_device_->RecordingIsAvailable(&available));
721    if (available) {
722      EXPECT_FALSE(audio_device_->RecordingIsInitialized());
723      EXPECT_EQ(0, audio_device_->InitRecording());
724      EXPECT_EQ(0, audio_device_->StartRecording());
725      EXPECT_TRUE(audio_device_->Recording());
726      EXPECT_EQ(0, audio_device_->RegisterAudioCallback(audio_transport_));
727      EXPECT_EQ(0, audio_device_->StopRecording());
728      EXPECT_FALSE(audio_device_->Recording());
729    }
730  }
731}
732
733#if defined(_WIN32) && !defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
734TEST_F(AudioDeviceAPITest, SetAndGetWaveOutVolume) {
735  uint32_t vol(0);
736  // NOTE 1: Windows Wave only!
737  // NOTE 2: It seems like the waveOutSetVolume API returns
738  // MMSYSERR_NOTSUPPORTED on some Vista machines!
739  const uint16_t maxVol(0xFFFF);
740  uint16_t volL, volR;
741
742  CheckInitialPlayoutStates();
743
744  // make dummy test to see if this API is supported
745  int32_t works = audio_device_->SetWaveOutVolume(vol, vol);
746  WARNING(works == 0);
747
748  if (works == 0)
749  {
750    // set volume without open playout device
751    for (vol = 0; vol <= maxVol; vol += (maxVol/5))
752    {
753      EXPECT_EQ(0, audio_device_->SetWaveOutVolume(vol, vol));
754      EXPECT_EQ(0, audio_device_->WaveOutVolume(volL, volR));
755      EXPECT_TRUE((volL == vol) && (volR == vol));
756    }
757
758    // repeat test but this time with an open (default) output device
759    EXPECT_EQ(0, audio_device_->SetPlayoutDevice(
760        AudioDeviceModule::kDefaultDevice));
761    EXPECT_EQ(0, audio_device_->InitPlayout());
762    EXPECT_TRUE(audio_device_->PlayoutIsInitialized());
763    for (vol = 0; vol <= maxVol; vol += (maxVol/5))
764    {
765      EXPECT_EQ(0, audio_device_->SetWaveOutVolume(vol, vol));
766      EXPECT_EQ(0, audio_device_->WaveOutVolume(volL, volR));
767      EXPECT_TRUE((volL == vol) && (volR == vol));
768    }
769
770    // as above but while playout is active
771    EXPECT_EQ(0, audio_device_->StartPlayout());
772    EXPECT_TRUE(audio_device_->Playing());
773    for (vol = 0; vol <= maxVol; vol += (maxVol/5))
774    {
775      EXPECT_EQ(0, audio_device_->SetWaveOutVolume(vol, vol));
776      EXPECT_EQ(0, audio_device_->WaveOutVolume(volL, volR));
777      EXPECT_TRUE((volL == vol) && (volR == vol));
778    }
779  }
780
781  EXPECT_EQ(0, audio_device_->StopPlayout());
782  EXPECT_FALSE(audio_device_->Playing());
783}
784#endif  // defined(_WIN32) && !defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
785
786TEST_F(AudioDeviceAPITest, SpeakerIsAvailable) {
787  bool available;
788  CheckInitialPlayoutStates();
789
790#ifdef _WIN32
791  // check the kDefaultCommunicationDevice
792  EXPECT_TRUE(audio_device_->SetPlayoutDevice(
793          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
794  EXPECT_EQ(0, audio_device_->SpeakerIsAvailable(&available));
795  // check for availability should not lead to initialization
796  EXPECT_FALSE(audio_device_->SpeakerIsInitialized());
797#endif
798
799  // check the kDefaultDevice
800  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(MACRO_DEFAULT_DEVICE));
801  EXPECT_EQ(0, audio_device_->SpeakerIsAvailable(&available));
802  EXPECT_FALSE(audio_device_->SpeakerIsInitialized());
803
804  // check all availiable devices
805  int16_t no_devices = audio_device_->PlayoutDevices();
806  for (int i = 0; i < no_devices; i++) {
807    EXPECT_EQ(0, audio_device_->SetPlayoutDevice(i));
808    EXPECT_EQ(0, audio_device_->SpeakerIsAvailable(&available));
809    EXPECT_FALSE(audio_device_->SpeakerIsInitialized());
810  }
811}
812
813TEST_F(AudioDeviceAPITest, InitSpeaker) {
814  // NOTE: By calling Terminate (in TearDown) followed by Init (in SetUp) we
815  // ensure that any existing output mixer handle is set to NULL.
816  // The mixer handle is closed and reopened again for each call to
817  // SetPlayoutDevice.
818  CheckInitialPlayoutStates();
819
820  // kDefaultCommunicationDevice
821  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(
822      MACRO_DEFAULT_COMMUNICATION_DEVICE));
823  bool available;
824  EXPECT_EQ(0, audio_device_->SpeakerIsAvailable(&available));
825  if (available) {
826    EXPECT_EQ(0, audio_device_->InitSpeaker());
827  }
828
829  // fail tests
830  EXPECT_EQ(0, audio_device_->PlayoutIsAvailable(&available));
831  if (available) {
832    EXPECT_EQ(0, audio_device_->InitPlayout());
833    EXPECT_EQ(0, audio_device_->StartPlayout());
834    EXPECT_EQ(-1, audio_device_->InitSpeaker());
835    EXPECT_EQ(0, audio_device_->StopPlayout());
836  }
837
838  // kDefaultDevice
839  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(MACRO_DEFAULT_DEVICE));
840  EXPECT_EQ(0, audio_device_->SpeakerIsAvailable(&available));
841  if (available) {
842    EXPECT_EQ(0, audio_device_->InitSpeaker());
843  }
844
845  // repeat test for all devices
846  int16_t no_devices = audio_device_->PlayoutDevices();
847  for (int i = 0; i < no_devices; i++) {
848    EXPECT_EQ(0, audio_device_->SetPlayoutDevice(i));
849    EXPECT_EQ(0, audio_device_->SpeakerIsAvailable(&available));
850    if (available) {
851      EXPECT_EQ(0, audio_device_->InitSpeaker());
852    }
853  }
854}
855
856TEST_F(AudioDeviceAPITest, MicrophoneIsAvailable) {
857  CheckInitialRecordingStates();
858  bool available;
859#ifdef _WIN32
860  // check the kDefaultCommunicationDevice
861  EXPECT_TRUE(audio_device_->SetRecordingDevice(
862          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
863  EXPECT_EQ(0, audio_device_->MicrophoneIsAvailable(&available));
864  // check for availability should not lead to initialization
865  EXPECT_FALSE(audio_device_->MicrophoneIsInitialized());
866#endif
867
868  // check the kDefaultDevice
869  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
870  EXPECT_EQ(0, audio_device_->MicrophoneIsAvailable(&available));
871  EXPECT_FALSE(audio_device_->MicrophoneIsInitialized());
872
873  // check all availiable devices
874  int16_t no_devices = audio_device_->RecordingDevices();
875  for (int i = 0; i < no_devices; i++) {
876    EXPECT_EQ(0, audio_device_->SetRecordingDevice(i));
877    EXPECT_EQ(0, audio_device_->MicrophoneIsAvailable(&available));
878    EXPECT_FALSE(audio_device_->MicrophoneIsInitialized());
879  }
880}
881
882TEST_F(AudioDeviceAPITest, InitMicrophone) {
883  // NOTE: By calling Terminate (in TearDown) followed by Init (in SetUp) we
884  // ensure that any existing output mixer handle is set to NULL.
885  // The mixer handle is closed and reopened again for each call to
886  // SetRecordingDevice.
887  CheckInitialRecordingStates();
888
889  // kDefaultCommunicationDevice
890  EXPECT_EQ(0,
891      audio_device_->SetRecordingDevice(MACRO_DEFAULT_COMMUNICATION_DEVICE));
892  bool available;
893  EXPECT_EQ(0, audio_device_->MicrophoneIsAvailable(&available));
894  if (available) {
895    EXPECT_EQ(0, audio_device_->InitMicrophone());
896  }
897
898  // fail tests
899  EXPECT_EQ(0, audio_device_->RecordingIsAvailable(&available));
900  if (available) {
901    EXPECT_EQ(0, audio_device_->InitRecording());
902    EXPECT_EQ(0, audio_device_->StartRecording());
903    EXPECT_EQ(-1, audio_device_->InitMicrophone());
904    EXPECT_EQ(0, audio_device_->StopRecording());
905  }
906
907  // kDefaultDevice
908  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
909  EXPECT_EQ(0, audio_device_->MicrophoneIsAvailable(&available));
910  if (available) {
911    EXPECT_EQ(0, audio_device_->InitMicrophone());
912  }
913
914  // repeat test for all devices
915  int16_t no_devices = audio_device_->RecordingDevices();
916  for (int i = 0; i < no_devices; i++) {
917    EXPECT_EQ(0, audio_device_->SetRecordingDevice(i));
918    EXPECT_EQ(0, audio_device_->MicrophoneIsAvailable(&available));
919    if (available) {
920      EXPECT_EQ(0, audio_device_->InitMicrophone());
921    }
922  }
923}
924
925TEST_F(AudioDeviceAPITest, SpeakerVolumeIsAvailable) {
926  CheckInitialPlayoutStates();
927  bool available;
928
929#ifdef _WIN32
930  // check the kDefaultCommunicationDevice
931  EXPECT_TRUE(audio_device_->SetPlayoutDevice(
932          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
933  EXPECT_EQ(0, audio_device_->SpeakerVolumeIsAvailable(&available));
934  // check for availability should not lead to initialization
935  EXPECT_FALSE(audio_device_->SpeakerIsInitialized());
936#endif
937
938  // check the kDefaultDevice
939  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(MACRO_DEFAULT_DEVICE));
940  EXPECT_EQ(0, audio_device_->SpeakerVolumeIsAvailable(&available));
941  EXPECT_FALSE(audio_device_->SpeakerIsInitialized());
942
943  // check all availiable devices
944  int16_t no_devices = audio_device_->PlayoutDevices();
945  for (int i = 0; i < no_devices; i++) {
946    EXPECT_EQ(0, audio_device_->SetPlayoutDevice(i));
947    EXPECT_EQ(0, audio_device_->SpeakerVolumeIsAvailable(&available));
948    EXPECT_FALSE(audio_device_->SpeakerIsInitialized());
949  }
950}
951
952// Tests the following methods:
953// SetSpeakerVolume
954// SpeakerVolume
955// MaxSpeakerVolume
956// MinSpeakerVolume
957// NOTE: Disabled on mac due to issue 257.
958#ifndef WEBRTC_MAC
959TEST_F(AudioDeviceAPITest, SpeakerVolumeTests) {
960  uint32_t vol(0);
961  uint32_t volume(0);
962  uint32_t maxVolume(0);
963  uint32_t minVolume(0);
964  uint16_t stepSize(0);
965  bool available;
966  CheckInitialPlayoutStates();
967
968  // fail tests
969  EXPECT_EQ(-1, audio_device_->SetSpeakerVolume(0));
970  // speaker must be initialized first
971  EXPECT_EQ(-1, audio_device_->SpeakerVolume(&volume));
972  EXPECT_EQ(-1, audio_device_->MaxSpeakerVolume(&maxVolume));
973  EXPECT_EQ(-1, audio_device_->MinSpeakerVolume(&minVolume));
974  EXPECT_EQ(-1, audio_device_->SpeakerVolumeStepSize(&stepSize));
975
976#if defined(_WIN32) && !defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
977  // test for warning (can e.g. happen on Vista with Wave API)
978  EXPECT_EQ(0,
979            audio_device_->SetPlayoutDevice(AudioDeviceModule::kDefaultDevice));
980  EXPECT_EQ(0, audio_device_->SpeakerVolumeIsAvailable(&available));
981  if (available) {
982    EXPECT_EQ(0, audio_device_->InitSpeaker());
983    EXPECT_EQ(0, audio_device_->SetSpeakerVolume(19001));
984    EXPECT_EQ(0, audio_device_->SpeakerVolume(&volume));
985    WARNING(volume == 19001);
986  }
987#endif
988
989#ifdef _WIN32
990  // use kDefaultCommunicationDevice and modify/retrieve the volume
991  EXPECT_TRUE(audio_device_->SetPlayoutDevice(
992          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
993  EXPECT_EQ(0, audio_device_->SpeakerVolumeIsAvailable(&available));
994  if (available) {
995    EXPECT_EQ(0, audio_device_->InitSpeaker());
996    EXPECT_EQ(0, audio_device_->MaxSpeakerVolume(&maxVolume));
997    EXPECT_EQ(0, audio_device_->MinSpeakerVolume(&minVolume));
998    EXPECT_EQ(0, audio_device_->SpeakerVolumeStepSize(&stepSize));
999    for (vol = minVolume; vol < (int)maxVolume; vol += 20*stepSize) {
1000      EXPECT_EQ(0, audio_device_->SetSpeakerVolume(vol));
1001      EXPECT_EQ(0, audio_device_->SpeakerVolume(&volume));
1002      CheckVolume(volume, vol);
1003    }
1004  }
1005#endif
1006
1007  // use kDefaultDevice and modify/retrieve the volume
1008  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(MACRO_DEFAULT_DEVICE));
1009  EXPECT_EQ(0, audio_device_->SpeakerVolumeIsAvailable(&available));
1010  if (available) {
1011    EXPECT_EQ(0, audio_device_->InitSpeaker());
1012    EXPECT_EQ(0, audio_device_->MaxSpeakerVolume(&maxVolume));
1013    EXPECT_EQ(0, audio_device_->MinSpeakerVolume(&minVolume));
1014    EXPECT_EQ(0, audio_device_->SpeakerVolumeStepSize(&stepSize));
1015    uint32_t step = (maxVolume - minVolume) / 10;
1016    step = (step < stepSize ? stepSize : step);
1017    for (vol = minVolume; vol <= maxVolume; vol += step) {
1018      EXPECT_EQ(0, audio_device_->SetSpeakerVolume(vol));
1019      EXPECT_EQ(0, audio_device_->SpeakerVolume(&volume));
1020      CheckVolume(volume, vol);
1021    }
1022  }
1023
1024  // use all (indexed) devices and modify/retrieve the volume
1025  int16_t no_devices = audio_device_->PlayoutDevices();
1026  for (int i = 0; i < no_devices; i++) {
1027    EXPECT_EQ(0, audio_device_->SetPlayoutDevice(i));
1028    EXPECT_EQ(0, audio_device_->SpeakerVolumeIsAvailable(&available));
1029    if (available) {
1030      EXPECT_EQ(0, audio_device_->InitSpeaker());
1031      EXPECT_EQ(0, audio_device_->MaxSpeakerVolume(&maxVolume));
1032      EXPECT_EQ(0, audio_device_->MinSpeakerVolume(&minVolume));
1033      EXPECT_EQ(0, audio_device_->SpeakerVolumeStepSize(&stepSize));
1034      uint32_t step = (maxVolume - minVolume) / 10;
1035      step = (step < stepSize ? stepSize : step);
1036      for (vol = minVolume; vol <= maxVolume; vol += step) {
1037        EXPECT_EQ(0, audio_device_->SetSpeakerVolume(vol));
1038        EXPECT_EQ(0, audio_device_->SpeakerVolume(&volume));
1039        CheckVolume(volume, vol);
1040      }
1041    }
1042  }
1043
1044  // restore reasonable level
1045  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(MACRO_DEFAULT_DEVICE));
1046  EXPECT_EQ(0, audio_device_->SpeakerVolumeIsAvailable(&available));
1047  if (available) {
1048    EXPECT_EQ(0, audio_device_->InitSpeaker());
1049    EXPECT_EQ(0, audio_device_->MaxSpeakerVolume(&maxVolume));
1050    EXPECT_TRUE(audio_device_->SetSpeakerVolume(maxVolume < 10 ?
1051        maxVolume/3 : maxVolume/10) == 0);
1052  }
1053}
1054#endif  // !WEBRTC_MAC
1055
1056TEST_F(AudioDeviceAPITest, AGC) {
1057  // NOTE: The AGC API only enables/disables the AGC. To ensure that it will
1058  // have an effect, use it in combination with MicrophoneVolumeIsAvailable.
1059  CheckInitialRecordingStates();
1060  EXPECT_FALSE(audio_device_->AGC());
1061
1062  // set/get tests
1063  EXPECT_EQ(0, audio_device_->SetAGC(true));
1064  EXPECT_TRUE(audio_device_->AGC());
1065  EXPECT_EQ(0, audio_device_->SetAGC(false));
1066  EXPECT_FALSE(audio_device_->AGC());
1067}
1068
1069TEST_F(AudioDeviceAPITest, MicrophoneVolumeIsAvailable) {
1070  CheckInitialRecordingStates();
1071  bool available;
1072
1073#ifdef _WIN32
1074  // check the kDefaultCommunicationDevice
1075  EXPECT_TRUE(audio_device_->SetRecordingDevice(
1076          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
1077  EXPECT_EQ(0, audio_device_->MicrophoneVolumeIsAvailable(&available));
1078  // check for availability should not lead to initialization
1079  EXPECT_FALSE(audio_device_->MicrophoneIsInitialized());
1080#endif
1081
1082  // check the kDefaultDevice
1083  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
1084  EXPECT_EQ(0, audio_device_->MicrophoneVolumeIsAvailable(&available));
1085  EXPECT_FALSE(audio_device_->MicrophoneIsInitialized());
1086
1087  // check all availiable devices
1088  int16_t no_devices = audio_device_->RecordingDevices();
1089  for (int i = 0; i < no_devices; i++) {
1090    EXPECT_EQ(0, audio_device_->SetRecordingDevice(i));
1091    EXPECT_EQ(0, audio_device_->MicrophoneVolumeIsAvailable(&available));
1092    EXPECT_FALSE(audio_device_->MicrophoneIsInitialized());
1093  }
1094}
1095
1096// Tests the methods:
1097// SetMicrophoneVolume
1098// MicrophoneVolume
1099// MaxMicrophoneVolume
1100// MinMicrophoneVolume
1101// NOTE: Disabled on mac due to issue 257.
1102#ifndef WEBRTC_MAC
1103TEST_F(AudioDeviceAPITest, MicrophoneVolumeTests) {
1104  uint32_t vol(0);
1105  uint32_t volume(0);
1106  uint32_t maxVolume(0);
1107  uint32_t minVolume(0);
1108  uint16_t stepSize(0);
1109  bool available;
1110  CheckInitialRecordingStates();
1111
1112  // fail tests
1113  EXPECT_EQ(-1, audio_device_->SetMicrophoneVolume(0));
1114  // must be initialized first
1115  EXPECT_EQ(-1, audio_device_->MicrophoneVolume(&volume));
1116  EXPECT_EQ(-1, audio_device_->MaxMicrophoneVolume(&maxVolume));
1117  EXPECT_EQ(-1, audio_device_->MinMicrophoneVolume(&minVolume));
1118  EXPECT_EQ(-1, audio_device_->MicrophoneVolumeStepSize(&stepSize));
1119
1120#if defined(_WIN32) && !defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
1121  // test for warning (can e.g. happen on Vista with Wave API)
1122  EXPECT_EQ(0, audio_device_->SetRecordingDevice(
1123      AudioDeviceModule::kDefaultDevice));
1124  EXPECT_EQ(0, audio_device_->MicrophoneVolumeIsAvailable(&available));
1125  if (available)
1126  {
1127    EXPECT_EQ(0, audio_device_->InitMicrophone());
1128    EXPECT_EQ(0, audio_device_->SetMicrophoneVolume(19001));
1129    EXPECT_EQ(0, audio_device_->MicrophoneVolume(&volume));
1130    WARNING(volume == 19001);
1131  }
1132#endif
1133
1134#ifdef _WIN32
1135  // initialize kDefaultCommunicationDevice and modify/retrieve the volume
1136  EXPECT_TRUE(audio_device_->SetRecordingDevice(
1137          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
1138  EXPECT_EQ(0, audio_device_->MicrophoneVolumeIsAvailable(&available));
1139  if (available)
1140  {
1141    EXPECT_EQ(0, audio_device_->InitMicrophone());
1142    EXPECT_EQ(0, audio_device_->MaxMicrophoneVolume(&maxVolume));
1143    EXPECT_EQ(0, audio_device_->MinMicrophoneVolume(&minVolume));
1144    EXPECT_EQ(0, audio_device_->MicrophoneVolumeStepSize(&stepSize));
1145    for (vol = minVolume; vol < (int)maxVolume; vol += 10*stepSize)
1146    {
1147      EXPECT_EQ(0, audio_device_->SetMicrophoneVolume(vol));
1148      EXPECT_EQ(0, audio_device_->MicrophoneVolume(&volume));
1149      CheckVolume(volume, vol);
1150    }
1151  }
1152#endif
1153
1154  // reinitialize kDefaultDevice and modify/retrieve the volume
1155  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
1156  EXPECT_EQ(0, audio_device_->MicrophoneVolumeIsAvailable(&available));
1157  if (available) {
1158    EXPECT_EQ(0, audio_device_->InitMicrophone());
1159    EXPECT_EQ(0, audio_device_->MaxMicrophoneVolume(&maxVolume));
1160    EXPECT_EQ(0, audio_device_->MinMicrophoneVolume(&minVolume));
1161    EXPECT_EQ(0, audio_device_->MicrophoneVolumeStepSize(&stepSize));
1162    for (vol = minVolume; vol < maxVolume; vol += 10 * stepSize) {
1163      EXPECT_EQ(0, audio_device_->SetMicrophoneVolume(vol));
1164      EXPECT_EQ(0, audio_device_->MicrophoneVolume(&volume));
1165      CheckVolume(volume, vol);
1166    }
1167  }
1168
1169  // use all (indexed) devices and modify/retrieve the volume
1170  int16_t no_devices = audio_device_->RecordingDevices();
1171  for (int i = 0; i < no_devices; i++) {
1172    EXPECT_EQ(0, audio_device_->SetRecordingDevice(i));
1173    EXPECT_EQ(0, audio_device_->MicrophoneVolumeIsAvailable(&available));
1174    if (available) {
1175      EXPECT_EQ(0, audio_device_->InitMicrophone());
1176      EXPECT_EQ(0, audio_device_->MaxMicrophoneVolume(&maxVolume));
1177      EXPECT_EQ(0, audio_device_->MinMicrophoneVolume(&minVolume));
1178      EXPECT_EQ(0, audio_device_->MicrophoneVolumeStepSize(&stepSize));
1179      for (vol = minVolume; vol < maxVolume; vol += 20 * stepSize) {
1180        EXPECT_EQ(0, audio_device_->SetMicrophoneVolume(vol));
1181        EXPECT_EQ(0, audio_device_->MicrophoneVolume(&volume));
1182        CheckVolume(volume, vol);
1183      }
1184    }
1185  }
1186
1187  // restore reasonable level
1188  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
1189  EXPECT_EQ(0, audio_device_->MicrophoneVolumeIsAvailable(&available));
1190  if (available) {
1191    EXPECT_EQ(0, audio_device_->InitMicrophone());
1192    EXPECT_EQ(0, audio_device_->MaxMicrophoneVolume(&maxVolume));
1193    EXPECT_EQ(0, audio_device_->SetMicrophoneVolume(maxVolume/10));
1194  }
1195}
1196#endif  // !WEBRTC_MAC
1197
1198TEST_F(AudioDeviceAPITest, SpeakerMuteIsAvailable) {
1199  bool available;
1200  CheckInitialPlayoutStates();
1201#ifdef _WIN32
1202  // check the kDefaultCommunicationDevice
1203  EXPECT_TRUE(audio_device_->SetPlayoutDevice(
1204          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
1205  EXPECT_EQ(0, audio_device_->SpeakerMuteIsAvailable(&available));
1206  // check for availability should not lead to initialization
1207  EXPECT_FALSE(audio_device_->SpeakerIsInitialized());
1208#endif
1209
1210  // check the kDefaultDevice
1211  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(MACRO_DEFAULT_DEVICE));
1212  EXPECT_EQ(0, audio_device_->SpeakerMuteIsAvailable(&available));
1213  EXPECT_FALSE(audio_device_->SpeakerIsInitialized());
1214
1215  // check all availiable devices
1216  int16_t no_devices = audio_device_->PlayoutDevices();
1217  for (int i = 0; i < no_devices; i++) {
1218    EXPECT_EQ(0, audio_device_->SetPlayoutDevice(i));
1219    EXPECT_EQ(0, audio_device_->SpeakerMuteIsAvailable(&available));
1220    EXPECT_FALSE(audio_device_->SpeakerIsInitialized());
1221  }
1222}
1223
1224TEST_F(AudioDeviceAPITest, MicrophoneMuteIsAvailable) {
1225  bool available;
1226  CheckInitialRecordingStates();
1227#ifdef _WIN32
1228  // check the kDefaultCommunicationDevice
1229  EXPECT_TRUE(audio_device_->SetRecordingDevice(
1230          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
1231  EXPECT_EQ(0, audio_device_->MicrophoneMuteIsAvailable(&available));
1232  // check for availability should not lead to initialization
1233#endif
1234  EXPECT_FALSE(audio_device_->MicrophoneIsInitialized());
1235
1236  // check the kDefaultDevice
1237  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
1238  EXPECT_EQ(0, audio_device_->MicrophoneMuteIsAvailable(&available));
1239  EXPECT_FALSE(audio_device_->MicrophoneIsInitialized());
1240
1241  // check all availiable devices
1242  int16_t no_devices = audio_device_->RecordingDevices();
1243  for (int i = 0; i < no_devices; i++) {
1244    EXPECT_EQ(0, audio_device_->SetRecordingDevice(i));
1245    EXPECT_EQ(0, audio_device_->MicrophoneMuteIsAvailable(&available));
1246    EXPECT_FALSE(audio_device_->MicrophoneIsInitialized());
1247  }
1248}
1249
1250TEST_F(AudioDeviceAPITest, MicrophoneBoostIsAvailable) {
1251  bool available;
1252  CheckInitialRecordingStates();
1253#ifdef _WIN32
1254  // check the kDefaultCommunicationDevice
1255  EXPECT_TRUE(audio_device_->SetRecordingDevice(
1256          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
1257  EXPECT_EQ(0, audio_device_->MicrophoneBoostIsAvailable(&available));
1258  // check for availability should not lead to initialization
1259  EXPECT_FALSE(audio_device_->MicrophoneIsInitialized());
1260#endif
1261
1262  // check the kDefaultDevice
1263  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
1264  EXPECT_EQ(0, audio_device_->MicrophoneBoostIsAvailable(&available));
1265  EXPECT_FALSE(audio_device_->MicrophoneIsInitialized());
1266
1267  // check all availiable devices
1268  int16_t no_devices = audio_device_->RecordingDevices();
1269  for (int i = 0; i < no_devices; i++) {
1270    EXPECT_EQ(0, audio_device_->SetRecordingDevice(i));
1271    EXPECT_EQ(0, audio_device_->MicrophoneBoostIsAvailable(&available));
1272    EXPECT_FALSE(audio_device_->MicrophoneIsInitialized());
1273  }
1274}
1275
1276TEST_F(AudioDeviceAPITest, SpeakerMuteTests) {
1277  bool available;
1278  bool enabled;
1279  CheckInitialPlayoutStates();
1280  // fail tests
1281  EXPECT_EQ(-1, audio_device_->SetSpeakerMute(true));
1282  // requires initialization
1283  EXPECT_EQ(-1, audio_device_->SpeakerMute(&enabled));
1284
1285#ifdef _WIN32
1286  // initialize kDefaultCommunicationDevice and modify/retrieve the mute state
1287  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(
1288      AudioDeviceModule::kDefaultCommunicationDevice));
1289  EXPECT_EQ(0, audio_device_->SpeakerMuteIsAvailable(&available));
1290  if (available)
1291  {
1292    EXPECT_EQ(0, audio_device_->InitSpeaker());
1293    EXPECT_EQ(0, audio_device_->SetSpeakerMute(true));
1294    EXPECT_EQ(0, audio_device_->SpeakerMute(&enabled));
1295    EXPECT_TRUE(enabled);
1296    EXPECT_EQ(0, audio_device_->SetSpeakerMute(false));
1297    EXPECT_EQ(0, audio_device_->SpeakerMute(&enabled));
1298    EXPECT_FALSE(enabled);
1299  }
1300#endif
1301
1302  // reinitialize kDefaultDevice and modify/retrieve the mute state
1303  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(MACRO_DEFAULT_DEVICE));
1304  EXPECT_EQ(0, audio_device_->SpeakerMuteIsAvailable(&available));
1305  if (available) {
1306    EXPECT_EQ(0, audio_device_->InitSpeaker());
1307    EXPECT_EQ(0, audio_device_->SetSpeakerMute(true));
1308    EXPECT_EQ(0, audio_device_->SpeakerMute(&enabled));
1309    EXPECT_TRUE(enabled);
1310    EXPECT_EQ(0, audio_device_->SetSpeakerMute(false));
1311    EXPECT_EQ(0, audio_device_->SpeakerMute(&enabled));
1312    EXPECT_FALSE(enabled);
1313  }
1314
1315  // reinitialize the default device (0) and modify/retrieve the mute state
1316  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(0));
1317  EXPECT_EQ(0, audio_device_->SpeakerMuteIsAvailable(&available));
1318  if (available) {
1319    EXPECT_EQ(0, audio_device_->InitSpeaker());
1320    EXPECT_EQ(0, audio_device_->SetSpeakerMute(true));
1321    EXPECT_EQ(0, audio_device_->SpeakerMute(&enabled));
1322    EXPECT_TRUE(enabled);
1323    EXPECT_EQ(0, audio_device_->SetSpeakerMute(false));
1324    EXPECT_EQ(0, audio_device_->SpeakerMute(&enabled));
1325    EXPECT_FALSE(enabled);
1326  }
1327}
1328
1329TEST_F(AudioDeviceAPITest, MicrophoneMuteTests) {
1330  CheckInitialRecordingStates();
1331
1332  // fail tests
1333  EXPECT_EQ(-1, audio_device_->SetMicrophoneMute(true));
1334  // requires initialization
1335  bool available;
1336  bool enabled;
1337  EXPECT_EQ(-1, audio_device_->MicrophoneMute(&enabled));
1338
1339#ifdef _WIN32
1340  // initialize kDefaultCommunicationDevice and modify/retrieve the mute
1341  EXPECT_TRUE(audio_device_->SetRecordingDevice(
1342          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
1343  EXPECT_EQ(0, audio_device_->MicrophoneMuteIsAvailable(&available));
1344  if (available)
1345  {
1346    EXPECT_EQ(0, audio_device_->InitMicrophone());
1347    EXPECT_EQ(0, audio_device_->SetMicrophoneMute(true));
1348    EXPECT_EQ(0, audio_device_->MicrophoneMute(&enabled));
1349    EXPECT_TRUE(enabled);
1350    EXPECT_EQ(0, audio_device_->SetMicrophoneMute(false));
1351    EXPECT_EQ(0, audio_device_->MicrophoneMute(&enabled));
1352    EXPECT_FALSE(enabled);
1353  }
1354#endif
1355
1356  // reinitialize kDefaultDevice and modify/retrieve the mute
1357  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
1358  EXPECT_EQ(0, audio_device_->MicrophoneMuteIsAvailable(&available));
1359  if (available) {
1360    EXPECT_EQ(0, audio_device_->InitMicrophone());
1361    EXPECT_EQ(0, audio_device_->SetMicrophoneMute(true));
1362    EXPECT_EQ(0, audio_device_->MicrophoneMute(&enabled));
1363    EXPECT_TRUE(enabled);
1364    EXPECT_EQ(0, audio_device_->SetMicrophoneMute(false));
1365    EXPECT_EQ(0, audio_device_->MicrophoneMute(&enabled));
1366    EXPECT_FALSE(enabled);
1367  }
1368
1369  // reinitialize the default device (0) and modify/retrieve the Mute
1370  EXPECT_EQ(0, audio_device_->SetRecordingDevice(0));
1371  EXPECT_EQ(0, audio_device_->MicrophoneMuteIsAvailable(&available));
1372  if (available) {
1373    EXPECT_EQ(0, audio_device_->InitMicrophone());
1374    EXPECT_EQ(0, audio_device_->SetMicrophoneMute(true));
1375    EXPECT_EQ(0, audio_device_->MicrophoneMute(&enabled));
1376    EXPECT_TRUE(enabled);
1377    EXPECT_EQ(0, audio_device_->SetMicrophoneMute(false));
1378    EXPECT_EQ(0, audio_device_->MicrophoneMute(&enabled));
1379    EXPECT_FALSE(enabled);
1380  }
1381}
1382
1383TEST_F(AudioDeviceAPITest, MicrophoneBoostTests) {
1384  bool available;
1385  bool enabled;
1386  CheckInitialRecordingStates();
1387
1388  // fail tests
1389  EXPECT_EQ(-1, audio_device_->SetMicrophoneBoost(true));
1390  // requires initialization
1391  EXPECT_EQ(-1, audio_device_->MicrophoneBoost(&enabled));
1392
1393#ifdef _WIN32
1394  // initialize kDefaultCommunicationDevice and modify/retrieve the boost
1395  EXPECT_TRUE(audio_device_->SetRecordingDevice(
1396          AudioDeviceModule::kDefaultCommunicationDevice) == 0);
1397  EXPECT_EQ(0, audio_device_->MicrophoneBoostIsAvailable(&available));
1398  if (available)
1399  {
1400    EXPECT_EQ(0, audio_device_->InitMicrophone());
1401    EXPECT_EQ(0, audio_device_->SetMicrophoneBoost(true));
1402    EXPECT_EQ(0, audio_device_->MicrophoneBoost(&enabled));
1403    EXPECT_TRUE(enabled);
1404    EXPECT_EQ(0, audio_device_->SetMicrophoneBoost(false));
1405    EXPECT_EQ(0, audio_device_->MicrophoneBoost(&enabled));
1406    EXPECT_FALSE(enabled);
1407  }
1408#endif
1409
1410  // reinitialize kDefaultDevice and modify/retrieve the boost
1411  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
1412  EXPECT_EQ(0, audio_device_->MicrophoneBoostIsAvailable(&available));
1413  if (available) {
1414    EXPECT_EQ(0, audio_device_->InitMicrophone());
1415    EXPECT_EQ(0, audio_device_->SetMicrophoneBoost(true));
1416    EXPECT_EQ(0, audio_device_->MicrophoneBoost(&enabled));
1417    EXPECT_TRUE(enabled);
1418    EXPECT_EQ(0, audio_device_->SetMicrophoneBoost(false));
1419    EXPECT_EQ(0, audio_device_->MicrophoneBoost(&enabled));
1420    EXPECT_FALSE(enabled);
1421  }
1422
1423  // reinitialize the default device (0) and modify/retrieve the boost
1424  EXPECT_EQ(0, audio_device_->SetRecordingDevice(0));
1425  EXPECT_EQ(0, audio_device_->MicrophoneBoostIsAvailable(&available));
1426  if (available) {
1427    EXPECT_EQ(0, audio_device_->InitMicrophone());
1428    EXPECT_EQ(0, audio_device_->SetMicrophoneBoost(true));
1429    EXPECT_EQ(0, audio_device_->MicrophoneBoost(&enabled));
1430    EXPECT_TRUE(enabled);
1431    EXPECT_EQ(0, audio_device_->SetMicrophoneBoost(false));
1432    EXPECT_EQ(0, audio_device_->MicrophoneBoost(&enabled));
1433    EXPECT_FALSE(enabled);
1434  }
1435}
1436
1437TEST_F(AudioDeviceAPITest, StereoPlayoutTests) {
1438  CheckInitialPlayoutStates();
1439
1440  // fail tests
1441  EXPECT_EQ(-1, audio_device_->InitPlayout());
1442  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(
1443      MACRO_DEFAULT_COMMUNICATION_DEVICE));
1444
1445  // TODO(kjellander): Fix so these tests pass on Mac.
1446#if !defined(WEBRTC_MAC)
1447  EXPECT_EQ(0, audio_device_->InitPlayout());
1448  EXPECT_TRUE(audio_device_->PlayoutIsInitialized());
1449  // must be performed before initialization
1450  EXPECT_EQ(-1, audio_device_->SetStereoPlayout(true));
1451#endif
1452
1453  // ensure that we can set the stereo mode for playout
1454  EXPECT_EQ(0, audio_device_->StopPlayout());
1455  EXPECT_FALSE(audio_device_->PlayoutIsInitialized());
1456
1457  // initialize kDefaultCommunicationDevice and modify/retrieve stereo support
1458  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(
1459      MACRO_DEFAULT_COMMUNICATION_DEVICE));
1460  bool available;
1461  bool enabled;
1462  EXPECT_EQ(0, audio_device_->StereoPlayoutIsAvailable(&available));
1463  if (available) {
1464    EXPECT_EQ(0, audio_device_->SetStereoPlayout(true));
1465    EXPECT_EQ(0, audio_device_->StereoPlayout(&enabled));
1466    EXPECT_TRUE(enabled);
1467    EXPECT_EQ(0, audio_device_->SetStereoPlayout(false));
1468    EXPECT_EQ(0, audio_device_->StereoPlayout(&enabled));
1469    EXPECT_FALSE(enabled);
1470    EXPECT_EQ(0, audio_device_->SetStereoPlayout(true));
1471    EXPECT_EQ(0, audio_device_->StereoPlayout(&enabled));
1472    EXPECT_TRUE(enabled);
1473  }
1474
1475  // initialize kDefaultDevice and modify/retrieve stereo support
1476  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(MACRO_DEFAULT_DEVICE));
1477  EXPECT_EQ(0, audio_device_->StereoPlayoutIsAvailable(&available));
1478  if (available) {
1479    EXPECT_EQ(0, audio_device_->SetStereoPlayout(true));
1480    EXPECT_EQ(0, audio_device_->StereoPlayout(&enabled));
1481    EXPECT_TRUE(enabled);
1482    EXPECT_EQ(0, audio_device_->SetStereoPlayout(false));
1483    EXPECT_EQ(0, audio_device_->StereoPlayout(&enabled));
1484    EXPECT_FALSE(enabled);
1485    EXPECT_EQ(0, audio_device_->SetStereoPlayout(true));
1486    EXPECT_EQ(0, audio_device_->StereoPlayout(&enabled));
1487    EXPECT_TRUE(enabled);
1488  }
1489
1490  // initialize default device (0) and modify/retrieve stereo support
1491  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(0));
1492  EXPECT_EQ(0, audio_device_->StereoPlayoutIsAvailable(&available));
1493  if (available) {
1494    EXPECT_EQ(0, audio_device_->SetStereoPlayout(true));
1495    EXPECT_EQ(0, audio_device_->StereoPlayout(&enabled));
1496    EXPECT_TRUE(enabled);
1497    EXPECT_EQ(0, audio_device_->SetStereoPlayout(false));
1498    EXPECT_EQ(0, audio_device_->StereoPlayout(&enabled));
1499    EXPECT_FALSE(enabled);
1500    EXPECT_EQ(0, audio_device_->SetStereoPlayout(true));
1501    EXPECT_EQ(0, audio_device_->StereoPlayout(&enabled));
1502    EXPECT_TRUE(enabled);
1503  }
1504}
1505
1506TEST_F(AudioDeviceAPITest, StereoRecordingTests) {
1507  CheckInitialRecordingStates();
1508  EXPECT_FALSE(audio_device_->Playing());
1509
1510  // fail tests
1511  EXPECT_EQ(-1, audio_device_->InitRecording());
1512  EXPECT_EQ(0, audio_device_->SetRecordingDevice(
1513      MACRO_DEFAULT_COMMUNICATION_DEVICE));
1514
1515  // TODO(kjellander): Fix so these tests pass on Mac.
1516#if !defined(WEBRTC_MAC)
1517  EXPECT_EQ(0, audio_device_->InitRecording());
1518  EXPECT_TRUE(audio_device_->RecordingIsInitialized());
1519  // must be performed before initialization
1520  EXPECT_EQ(-1, audio_device_->SetStereoRecording(true));
1521#endif
1522  // ensures that we can set the stereo mode for recording
1523  EXPECT_EQ(0, audio_device_->StopRecording());
1524  EXPECT_FALSE(audio_device_->RecordingIsInitialized());
1525
1526  // initialize kDefaultCommunicationDevice and modify/retrieve stereo support
1527  EXPECT_EQ(0, audio_device_->SetRecordingDevice(
1528      MACRO_DEFAULT_COMMUNICATION_DEVICE));
1529  bool available;
1530  bool enabled;
1531  EXPECT_EQ(0, audio_device_->StereoRecordingIsAvailable(&available));
1532  if (available) {
1533    EXPECT_EQ(0, audio_device_->SetStereoRecording(true));
1534    EXPECT_EQ(0, audio_device_->StereoRecording(&enabled));
1535    EXPECT_TRUE(enabled);
1536    EXPECT_EQ(0, audio_device_->SetStereoRecording(false));
1537    EXPECT_EQ(0, audio_device_->StereoRecording(&enabled));
1538    EXPECT_FALSE(enabled);
1539  }
1540
1541  // initialize kDefaultDevice and modify/retrieve stereo support
1542  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
1543  EXPECT_EQ(0, audio_device_->StereoRecordingIsAvailable(&available));
1544  if (available) {
1545    EXPECT_EQ(0, audio_device_->SetStereoRecording(true));
1546    EXPECT_EQ(0, audio_device_->StereoRecording(&enabled));
1547    EXPECT_TRUE(enabled);
1548    EXPECT_EQ(0, audio_device_->SetStereoRecording(false));
1549    EXPECT_EQ(0, audio_device_->StereoRecording(&enabled));
1550    EXPECT_FALSE(enabled);
1551  }
1552
1553  // initialize default device (0) and modify/retrieve stereo support
1554  EXPECT_EQ(0, audio_device_->SetRecordingDevice(0));
1555  EXPECT_EQ(0, audio_device_->StereoRecordingIsAvailable(&available));
1556  if (available) {
1557    EXPECT_EQ(0, audio_device_->SetStereoRecording(true));
1558    EXPECT_EQ(0, audio_device_->StereoRecording(&enabled));
1559    EXPECT_TRUE(enabled);
1560    EXPECT_EQ(0, audio_device_->SetStereoRecording(false));
1561    EXPECT_EQ(0, audio_device_->StereoRecording(&enabled));
1562    EXPECT_FALSE(enabled);
1563  }
1564}
1565
1566TEST_F(AudioDeviceAPITest, RecordingChannelTests) {
1567  // the user in Win Core Audio
1568  AudioDeviceModule::ChannelType channelType(AudioDeviceModule::kChannelBoth);
1569  CheckInitialRecordingStates();
1570  EXPECT_FALSE(audio_device_->Playing());
1571
1572  // fail tests
1573  EXPECT_EQ(0, audio_device_->SetStereoRecording(false));
1574  EXPECT_EQ(-1, audio_device_->SetRecordingChannel(
1575      AudioDeviceModule::kChannelBoth));
1576
1577  // initialize kDefaultCommunicationDevice and modify/retrieve stereo support
1578  EXPECT_EQ(0, audio_device_->SetRecordingDevice(
1579      MACRO_DEFAULT_COMMUNICATION_DEVICE));
1580  bool available;
1581  EXPECT_EQ(0, audio_device_->StereoRecordingIsAvailable(&available));
1582  if (available) {
1583    EXPECT_EQ(0, audio_device_->SetStereoRecording(true));
1584    EXPECT_EQ(0, audio_device_->SetRecordingChannel(
1585        AudioDeviceModule::kChannelBoth));
1586    EXPECT_EQ(0, audio_device_->RecordingChannel(&channelType));
1587    EXPECT_EQ(AudioDeviceModule::kChannelBoth, channelType);
1588    EXPECT_EQ(0, audio_device_->SetRecordingChannel(
1589        AudioDeviceModule::kChannelLeft));
1590    EXPECT_EQ(0, audio_device_->RecordingChannel(&channelType));
1591    EXPECT_EQ(AudioDeviceModule::kChannelLeft, channelType);
1592    EXPECT_EQ(0, audio_device_->SetRecordingChannel(
1593        AudioDeviceModule::kChannelRight));
1594    EXPECT_EQ(0, audio_device_->RecordingChannel(&channelType));
1595    EXPECT_EQ(AudioDeviceModule::kChannelRight, channelType);
1596  }
1597}
1598
1599TEST_F(AudioDeviceAPITest, PlayoutBufferTests) {
1600  AudioDeviceModule::BufferType bufferType;
1601  uint16_t sizeMS(0);
1602
1603  CheckInitialPlayoutStates();
1604  EXPECT_EQ(0, audio_device_->PlayoutBuffer(&bufferType, &sizeMS));
1605#if defined(_WIN32) || defined(ANDROID) || defined(WEBRTC_IOS)
1606  EXPECT_EQ(AudioDeviceModule::kAdaptiveBufferSize, bufferType);
1607#else
1608  EXPECT_EQ(AudioDeviceModule::kFixedBufferSize, bufferType);
1609#endif
1610
1611  // fail tests
1612  EXPECT_EQ(-1, audio_device_->InitPlayout());
1613  // must set device first
1614  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(
1615      MACRO_DEFAULT_COMMUNICATION_DEVICE));
1616
1617  // TODO(kjellander): Fix so these tests pass on Mac.
1618#if !defined(WEBRTC_MAC)
1619  EXPECT_EQ(0, audio_device_->InitPlayout());
1620  EXPECT_TRUE(audio_device_->PlayoutIsInitialized());
1621#endif
1622  EXPECT_TRUE(audio_device_->SetPlayoutBuffer(
1623      AudioDeviceModule::kAdaptiveBufferSize, 100) == -1);
1624  EXPECT_EQ(0, audio_device_->StopPlayout());
1625  EXPECT_TRUE(audio_device_->SetPlayoutBuffer(
1626      AudioDeviceModule::kFixedBufferSize, kAdmMinPlayoutBufferSizeMs-1) == -1);
1627  EXPECT_TRUE(audio_device_->SetPlayoutBuffer(
1628      AudioDeviceModule::kFixedBufferSize, kAdmMaxPlayoutBufferSizeMs+1) == -1);
1629
1630  // bulk tests (all should be successful)
1631  EXPECT_FALSE(audio_device_->PlayoutIsInitialized());
1632#ifdef _WIN32
1633  EXPECT_EQ(0, audio_device_->SetPlayoutBuffer(
1634      AudioDeviceModule::kAdaptiveBufferSize, 0));
1635  EXPECT_EQ(0, audio_device_->PlayoutBuffer(&bufferType, &sizeMS));
1636  EXPECT_EQ(AudioDeviceModule::kAdaptiveBufferSize, bufferType);
1637  EXPECT_EQ(0, audio_device_->SetPlayoutBuffer(
1638      AudioDeviceModule::kAdaptiveBufferSize, 10000));
1639  EXPECT_EQ(0, audio_device_->PlayoutBuffer(&bufferType, &sizeMS));
1640  EXPECT_EQ(AudioDeviceModule::kAdaptiveBufferSize, bufferType);
1641#endif
1642#if defined(ANDROID) || defined(WEBRTC_IOS)
1643  EXPECT_EQ(-1,
1644            audio_device_->SetPlayoutBuffer(AudioDeviceModule::kFixedBufferSize,
1645                                          kAdmMinPlayoutBufferSizeMs));
1646#else
1647  EXPECT_EQ(0, audio_device_->SetPlayoutBuffer(
1648      AudioDeviceModule::kFixedBufferSize, kAdmMinPlayoutBufferSizeMs));
1649  EXPECT_EQ(0, audio_device_->PlayoutBuffer(&bufferType, &sizeMS));
1650  EXPECT_EQ(AudioDeviceModule::kFixedBufferSize, bufferType);
1651  EXPECT_EQ(kAdmMinPlayoutBufferSizeMs, sizeMS);
1652  EXPECT_EQ(0, audio_device_->SetPlayoutBuffer(
1653      AudioDeviceModule::kFixedBufferSize, kAdmMaxPlayoutBufferSizeMs));
1654  EXPECT_EQ(0, audio_device_->PlayoutBuffer(&bufferType, &sizeMS));
1655  EXPECT_EQ(AudioDeviceModule::kFixedBufferSize, bufferType);
1656  EXPECT_EQ(kAdmMaxPlayoutBufferSizeMs, sizeMS);
1657  EXPECT_EQ(0, audio_device_->SetPlayoutBuffer(
1658      AudioDeviceModule::kFixedBufferSize, 100));
1659  EXPECT_EQ(0, audio_device_->PlayoutBuffer(&bufferType, &sizeMS));
1660  EXPECT_EQ(AudioDeviceModule::kFixedBufferSize, bufferType);
1661  EXPECT_EQ(100, sizeMS);
1662#endif
1663
1664#ifdef _WIN32
1665  // restore default
1666  EXPECT_EQ(0, audio_device_->SetPlayoutBuffer(
1667      AudioDeviceModule::kAdaptiveBufferSize, 0));
1668  EXPECT_EQ(0, audio_device_->PlayoutBuffer(&bufferType, &sizeMS));
1669#endif
1670}
1671
1672TEST_F(AudioDeviceAPITest, PlayoutDelay) {
1673  // NOTE: this API is better tested in a functional test
1674  uint16_t sizeMS(0);
1675  CheckInitialPlayoutStates();
1676  // bulk tests
1677  EXPECT_EQ(0, audio_device_->PlayoutDelay(&sizeMS));
1678  EXPECT_EQ(0, audio_device_->PlayoutDelay(&sizeMS));
1679}
1680
1681TEST_F(AudioDeviceAPITest, RecordingDelay) {
1682  // NOTE: this API is better tested in a functional test
1683  uint16_t sizeMS(0);
1684  CheckInitialRecordingStates();
1685
1686  // bulk tests
1687  EXPECT_EQ(0, audio_device_->RecordingDelay(&sizeMS));
1688  EXPECT_EQ(0, audio_device_->RecordingDelay(&sizeMS));
1689}
1690
1691TEST_F(AudioDeviceAPITest, CPULoad) {
1692  // NOTE: this API is better tested in a functional test
1693  uint16_t load(0);
1694
1695  // bulk tests
1696#ifdef _WIN32
1697  EXPECT_EQ(0, audio_device_->CPULoad(&load));
1698  EXPECT_EQ(0, load);
1699#else
1700  EXPECT_EQ(-1, audio_device_->CPULoad(&load));
1701#endif
1702}
1703
1704// TODO(kjellander): Fix flakiness causing failures on Windows.
1705// TODO(phoglund):  Fix flakiness causing failures on Linux.
1706#if !defined(_WIN32) && !defined(WEBRTC_LINUX)
1707TEST_F(AudioDeviceAPITest, StartAndStopRawOutputFileRecording) {
1708  // NOTE: this API is better tested in a functional test
1709  CheckInitialPlayoutStates();
1710
1711  // fail tests
1712  EXPECT_EQ(-1, audio_device_->StartRawOutputFileRecording(NULL));
1713
1714  // bulk tests
1715  EXPECT_EQ(0, audio_device_->StartRawOutputFileRecording(
1716      GetFilename("raw_output_not_playing.pcm")));
1717  EXPECT_EQ(0, audio_device_->StopRawOutputFileRecording());
1718  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(
1719      MACRO_DEFAULT_COMMUNICATION_DEVICE));
1720
1721  // TODO(kjellander): Fix so these tests pass on Mac.
1722#if !defined(WEBRTC_MAC)
1723  EXPECT_EQ(0, audio_device_->InitPlayout());
1724  EXPECT_EQ(0, audio_device_->StartPlayout());
1725#endif
1726
1727  EXPECT_EQ(0, audio_device_->StartRawOutputFileRecording(
1728      GetFilename("raw_output_playing.pcm")));
1729  SleepMs(100);
1730  EXPECT_EQ(0, audio_device_->StopRawOutputFileRecording());
1731  EXPECT_EQ(0, audio_device_->StopPlayout());
1732  EXPECT_EQ(0, audio_device_->StartRawOutputFileRecording(
1733      GetFilename("raw_output_not_playing.pcm")));
1734  EXPECT_EQ(0, audio_device_->StopRawOutputFileRecording());
1735
1736  // results after this test:
1737  //
1738  // - size of raw_output_not_playing.pcm shall be 0
1739  // - size of raw_output_playing.pcm shall be > 0
1740}
1741
1742TEST_F(AudioDeviceAPITest, StartAndStopRawInputFileRecording) {
1743  // NOTE: this API is better tested in a functional test
1744  CheckInitialRecordingStates();
1745  EXPECT_FALSE(audio_device_->Playing());
1746
1747  // fail tests
1748  EXPECT_EQ(-1, audio_device_->StartRawInputFileRecording(NULL));
1749
1750  // bulk tests
1751  EXPECT_EQ(0, audio_device_->StartRawInputFileRecording(
1752      GetFilename("raw_input_not_recording.pcm")));
1753  EXPECT_EQ(0, audio_device_->StopRawInputFileRecording());
1754  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
1755
1756  // TODO(kjellander): Fix so these tests pass on Mac.
1757#if !defined(WEBRTC_MAC)
1758  EXPECT_EQ(0, audio_device_->InitRecording());
1759  EXPECT_EQ(0, audio_device_->StartRecording());
1760#endif
1761  EXPECT_EQ(0, audio_device_->StartRawInputFileRecording(
1762      GetFilename("raw_input_recording.pcm")));
1763  SleepMs(100);
1764  EXPECT_EQ(0, audio_device_->StopRawInputFileRecording());
1765  EXPECT_EQ(0, audio_device_->StopRecording());
1766  EXPECT_EQ(0, audio_device_->StartRawInputFileRecording(
1767      GetFilename("raw_input_not_recording.pcm")));
1768  EXPECT_EQ(0, audio_device_->StopRawInputFileRecording());
1769
1770  // results after this test:
1771  //
1772  // - size of raw_input_not_recording.pcm shall be 0
1773  // - size of raw_input_not_recording.pcm shall be > 0
1774}
1775#endif  // !WIN32 && !WEBRTC_LINUX
1776
1777TEST_F(AudioDeviceAPITest, RecordingSampleRate) {
1778  uint32_t sampleRate(0);
1779
1780  // bulk tests
1781  EXPECT_EQ(0, audio_device_->RecordingSampleRate(&sampleRate));
1782#if defined(_WIN32) && !defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
1783  EXPECT_EQ(48000, sampleRate);
1784#elif defined(ANDROID)
1785  TEST_LOG("Recording sample rate is %u\n\n", sampleRate);
1786  EXPECT_TRUE((sampleRate == 44000) || (sampleRate == 16000));
1787#elif defined(WEBRTC_IOS)
1788  TEST_LOG("Recording sample rate is %u\n\n", sampleRate);
1789  EXPECT_TRUE((sampleRate == 44000) || (sampleRate == 16000) ||
1790              (sampleRate == 8000));
1791#endif
1792
1793  // @TODO(xians) - add tests for all platforms here...
1794}
1795
1796TEST_F(AudioDeviceAPITest, PlayoutSampleRate) {
1797  uint32_t sampleRate(0);
1798
1799  // bulk tests
1800  EXPECT_EQ(0, audio_device_->PlayoutSampleRate(&sampleRate));
1801#if defined(_WIN32) && !defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
1802  EXPECT_EQ(48000, sampleRate);
1803#elif defined(ANDROID)
1804  TEST_LOG("Playout sample rate is %u\n\n", sampleRate);
1805  EXPECT_TRUE((sampleRate == 44000) || (sampleRate == 16000));
1806#elif defined(WEBRTC_IOS)
1807  TEST_LOG("Playout sample rate is %u\n\n", sampleRate);
1808  EXPECT_TRUE((sampleRate == 44000) || (sampleRate == 16000) ||
1809              (sampleRate == 8000));
1810#endif
1811}
1812
1813TEST_F(AudioDeviceAPITest, ResetAudioDevice) {
1814  CheckInitialPlayoutStates();
1815  CheckInitialRecordingStates();
1816  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(MACRO_DEFAULT_DEVICE));
1817  EXPECT_EQ(0, audio_device_->SetRecordingDevice(MACRO_DEFAULT_DEVICE));
1818
1819#if defined(WEBRTC_IOS)
1820  // Not playing or recording, should just return 0
1821  EXPECT_EQ(0, audio_device_->ResetAudioDevice());
1822
1823  EXPECT_EQ(0, audio_device_->InitRecording());
1824  EXPECT_EQ(0, audio_device_->StartRecording());
1825  EXPECT_EQ(0, audio_device_->InitPlayout());
1826  EXPECT_EQ(0, audio_device_->StartPlayout());
1827  for (int l=0; l<20; ++l)
1828  {
1829    TEST_LOG("Resetting sound device several time with pause %d ms\n", l);
1830    EXPECT_EQ(0, audio_device_->ResetAudioDevice());
1831    SleepMs(l);
1832  }
1833#else
1834  // Fail tests
1835  EXPECT_EQ(-1, audio_device_->ResetAudioDevice());
1836
1837  // TODO(kjellander): Fix so these tests pass on Mac.
1838#if !defined(WEBRTC_MAC)
1839  EXPECT_EQ(0, audio_device_->InitRecording());
1840  EXPECT_EQ(0, audio_device_->StartRecording());
1841  EXPECT_EQ(0, audio_device_->InitPlayout());
1842  EXPECT_EQ(0, audio_device_->StartPlayout());
1843#endif
1844  EXPECT_EQ(-1, audio_device_->ResetAudioDevice());
1845#endif
1846  EXPECT_EQ(0, audio_device_->StopRecording());
1847  EXPECT_EQ(0, audio_device_->StopPlayout());
1848}
1849
1850TEST_F(AudioDeviceAPITest, SetPlayoutSpeaker) {
1851  CheckInitialPlayoutStates();
1852  EXPECT_EQ(0, audio_device_->SetPlayoutDevice(MACRO_DEFAULT_DEVICE));
1853
1854  bool loudspeakerOn(false);
1855#if defined(WEBRTC_IOS)
1856  // Not playing or recording, should just return a success
1857  EXPECT_EQ(0, audio_device_->SetLoudspeakerStatus(true));
1858  EXPECT_EQ(0, audio_device_->GetLoudspeakerStatus(loudspeakerOn));
1859  EXPECT_TRUE(loudspeakerOn);
1860  EXPECT_EQ(0, audio_device_->SetLoudspeakerStatus(false));
1861  EXPECT_EQ(0, audio_device_->GetLoudspeakerStatus(loudspeakerOn));
1862  EXPECT_FALSE(loudspeakerOn);
1863
1864  EXPECT_EQ(0, audio_device_->InitPlayout());
1865  EXPECT_EQ(0, audio_device_->StartPlayout());
1866  EXPECT_EQ(0, audio_device_->SetLoudspeakerStatus(true));
1867  EXPECT_EQ(0, audio_device_->GetLoudspeakerStatus(loudspeakerOn));
1868  EXPECT_TRUE(loudspeakerOn);
1869  EXPECT_EQ(0, audio_device_->SetLoudspeakerStatus(false));
1870  EXPECT_EQ(0, audio_device_->GetLoudspeakerStatus(loudspeakerOn));
1871  EXPECT_FALSE(loudspeakerOn);
1872
1873#else
1874  // Fail tests
1875  EXPECT_EQ(-1, audio_device_->SetLoudspeakerStatus(true));
1876  EXPECT_EQ(-1, audio_device_->SetLoudspeakerStatus(false));
1877  EXPECT_EQ(-1, audio_device_->SetLoudspeakerStatus(true));
1878  EXPECT_EQ(-1, audio_device_->SetLoudspeakerStatus(false));
1879
1880  // TODO(kjellander): Fix so these tests pass on Mac.
1881#if !defined(WEBRTC_MAC)
1882  EXPECT_EQ(0, audio_device_->InitPlayout());
1883  EXPECT_EQ(0, audio_device_->StartPlayout());
1884#endif
1885
1886  EXPECT_EQ(-1, audio_device_->GetLoudspeakerStatus(&loudspeakerOn));
1887#endif
1888  EXPECT_EQ(0, audio_device_->StopPlayout());
1889}
Note: See TracBrowser for help on using the repository browser.