summaryrefslogtreecommitdiffstats
path: root/chromium/media/audio/android/audio_android_unittest.cc
blob: a356d9c25deb6f7e174a9177365c45a4c06c0f53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/android/build_info.h"
#include "base/basictypes.h"
#include "base/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_timeouts.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "media/audio/android/audio_manager_android.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_manager_base.h"
#include "media/audio/mock_audio_source_callback.h"
#include "media/base/decoder_buffer.h"
#include "media/base/seekable_buffer.h"
#include "media/base/test_data_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::AtLeast;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::NotNull;
using ::testing::Return;

namespace media {

ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop) {
  if (++*count >= limit) {
    loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
  }
}

static const char kSpeechFile_16b_s_48k[] = "speech_16b_stereo_48kHz.raw";
static const char kSpeechFile_16b_m_48k[] = "speech_16b_mono_48kHz.raw";
static const char kSpeechFile_16b_s_44k[] = "speech_16b_stereo_44kHz.raw";
static const char kSpeechFile_16b_m_44k[] = "speech_16b_mono_44kHz.raw";

static const float kCallbackTestTimeMs = 2000.0;
static const int kBitsPerSample = 16;
static const int kBytesPerSample = kBitsPerSample / 8;

// Converts AudioParameters::Format enumerator to readable string.
static std::string FormatToString(AudioParameters::Format format) {
  switch (format) {
    case AudioParameters::AUDIO_PCM_LINEAR:
      return std::string("AUDIO_PCM_LINEAR");
    case AudioParameters::AUDIO_PCM_LOW_LATENCY:
      return std::string("AUDIO_PCM_LOW_LATENCY");
    case AudioParameters::AUDIO_FAKE:
      return std::string("AUDIO_FAKE");
    case AudioParameters::AUDIO_LAST_FORMAT:
      return std::string("AUDIO_LAST_FORMAT");
    default:
      return std::string();
  }
}

// Converts ChannelLayout enumerator to readable string. Does not include
// multi-channel cases since these layouts are not supported on Android.
static std::string LayoutToString(ChannelLayout channel_layout) {
  switch (channel_layout) {
    case CHANNEL_LAYOUT_NONE:
      return std::string("CHANNEL_LAYOUT_NONE");
    case CHANNEL_LAYOUT_MONO:
      return std::string("CHANNEL_LAYOUT_MONO");
    case CHANNEL_LAYOUT_STEREO:
      return std::string("CHANNEL_LAYOUT_STEREO");
    case CHANNEL_LAYOUT_UNSUPPORTED:
    default:
      return std::string("CHANNEL_LAYOUT_UNSUPPORTED");
  }
}

static double ExpectedTimeBetweenCallbacks(AudioParameters params) {
  return (base::TimeDelta::FromMicroseconds(
              params.frames_per_buffer() * base::Time::kMicrosecondsPerSecond /
              static_cast<double>(params.sample_rate()))).InMillisecondsF();
}

// Helper method which verifies that the device list starts with a valid
// default device name followed by non-default device names.
static void CheckDeviceNames(const AudioDeviceNames& device_names) {
  VLOG(2) << "Got " << device_names.size() << " audio devices.";
  if (device_names.empty()) {
    // Log a warning so we can see the status on the build bots.  No need to
    // break the test though since this does successfully test the code and
    // some failure cases.
    LOG(WARNING) << "No input devices detected";
    return;
  }

  AudioDeviceNames::const_iterator it = device_names.begin();

  // The first device in the list should always be the default device.
  EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceName),
            it->device_name);
  EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceId), it->unique_id);
  ++it;

  // Other devices should have non-empty name and id and should not contain
  // default name or id.
  while (it != device_names.end()) {
    EXPECT_FALSE(it->device_name.empty());
    EXPECT_FALSE(it->unique_id.empty());
    VLOG(2) << "Device ID(" << it->unique_id
            << "), label: " << it->device_name;
    EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceName),
              it->device_name);
    EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceId),
              it->unique_id);
    ++it;
  }
}

// We clear the data bus to ensure that the test does not cause noise.
static int RealOnMoreData(AudioBus* dest, AudioBuffersState buffers_state) {
  dest->Zero();
  return dest->frames();
}

std::ostream& operator<<(std::ostream& os, const AudioParameters& params) {
  using namespace std;
  os << endl << "format: " << FormatToString(params.format()) << endl
     << "channel layout: " << LayoutToString(params.channel_layout()) << endl
     << "sample rate: " << params.sample_rate() << endl
     << "bits per sample: " << params.bits_per_sample() << endl
     << "frames per buffer: " << params.frames_per_buffer() << endl
     << "channels: " << params.channels() << endl
     << "bytes per buffer: " << params.GetBytesPerBuffer() << endl
     << "bytes per second: " << params.GetBytesPerSecond() << endl
     << "bytes per frame: " << params.GetBytesPerFrame() << endl
     << "chunk size in ms: " << ExpectedTimeBetweenCallbacks(params) << endl
     << "echo_canceller: "
     << (params.effects() & AudioParameters::ECHO_CANCELLER);
  return os;
}

// Gmock implementation of AudioInputStream::AudioInputCallback.
class MockAudioInputCallback : public AudioInputStream::AudioInputCallback {
 public:
  MOCK_METHOD4(OnData,
               void(AudioInputStream* stream,
                    const AudioBus* src,
                    uint32 hardware_delay_bytes,
                    double volume));
  MOCK_METHOD1(OnError, void(AudioInputStream* stream));
};

// Implements AudioOutputStream::AudioSourceCallback and provides audio data
// by reading from a data file.
class FileAudioSource : public AudioOutputStream::AudioSourceCallback {
 public:
  explicit FileAudioSource(base::WaitableEvent* event, const std::string& name)
      : event_(event), pos_(0) {
    // Reads a test file from media/test/data directory and stores it in
    // a DecoderBuffer.
    file_ = ReadTestDataFile(name);

    // Log the name of the file which is used as input for this test.
    base::FilePath file_path = GetTestDataFilePath(name);
    VLOG(0) << "Reading from file: " << file_path.value().c_str();
  }

  virtual ~FileAudioSource() {}

  // AudioOutputStream::AudioSourceCallback implementation.

  // Use samples read from a data file and fill up the audio buffer
  // provided to us in the callback.
  virtual int OnMoreData(AudioBus* audio_bus,
                         AudioBuffersState buffers_state) OVERRIDE {
    bool stop_playing = false;
    int max_size =
        audio_bus->frames() * audio_bus->channels() * kBytesPerSample;

    // Adjust data size and prepare for end signal if file has ended.
    if (pos_ + max_size > file_size()) {
      stop_playing = true;
      max_size = file_size() - pos_;
    }

    // File data is stored as interleaved 16-bit values. Copy data samples from
    // the file and deinterleave to match the audio bus format.
    // FromInterleaved() will zero out any unfilled frames when there is not
    // sufficient data remaining in the file to fill up the complete frame.
    int frames = max_size / (audio_bus->channels() * kBytesPerSample);
    if (max_size) {
      audio_bus->FromInterleaved(file_->data() + pos_, frames, kBytesPerSample);
      pos_ += max_size;
    }

    // Set event to ensure that the test can stop when the file has ended.
    if (stop_playing)
      event_->Signal();

    return frames;
  }

  virtual void OnError(AudioOutputStream* stream) OVERRIDE {}

  int file_size() { return file_->data_size(); }

 private:
  base::WaitableEvent* event_;
  int pos_;
  scoped_refptr<DecoderBuffer> file_;

  DISALLOW_COPY_AND_ASSIGN(FileAudioSource);
};

// Implements AudioInputStream::AudioInputCallback and writes the recorded
// audio data to a local output file. Note that this implementation should
// only be used for manually invoked and evaluated tests, hence the created
// file will not be destroyed after the test is done since the intention is
// that it shall be available for off-line analysis.
class FileAudioSink : public AudioInputStream::AudioInputCallback {
 public:
  explicit FileAudioSink(base::WaitableEvent* event,
                         const AudioParameters& params,
                         const std::string& file_name)
      : event_(event), params_(params) {
    // Allocate space for ~10 seconds of data.
    const int kMaxBufferSize = 10 * params.GetBytesPerSecond();
    buffer_.reset(new media::SeekableBuffer(0, kMaxBufferSize));

    // Open up the binary file which will be written to in the destructor.
    base::FilePath file_path;
    EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &file_path));
    file_path = file_path.AppendASCII(file_name.c_str());
    binary_file_ = base::OpenFile(file_path, "wb");
    DLOG_IF(ERROR, !binary_file_) << "Failed to open binary PCM data file.";
    VLOG(0) << "Writing to file: " << file_path.value().c_str();
  }

  virtual ~FileAudioSink() {
    int bytes_written = 0;
    while (bytes_written < buffer_->forward_capacity()) {
      const uint8* chunk;
      int chunk_size;

      // Stop writing if no more data is available.
      if (!buffer_->GetCurrentChunk(&chunk, &chunk_size))
        break;

      // Write recorded data chunk to the file and prepare for next chunk.
      // TODO(henrika): use file_util:: instead.
      fwrite(chunk, 1, chunk_size, binary_file_);
      buffer_->Seek(chunk_size);
      bytes_written += chunk_size;
    }
    base::CloseFile(binary_file_);
  }

  // AudioInputStream::AudioInputCallback implementation.
  virtual void OnData(AudioInputStream* stream,
                      const AudioBus* src,
                      uint32 hardware_delay_bytes,
                      double volume) OVERRIDE {
    const int num_samples = src->frames() * src->channels();
    scoped_ptr<int16> interleaved(new int16[num_samples]);
    const int bytes_per_sample = sizeof(*interleaved);
    src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get());

    // Store data data in a temporary buffer to avoid making blocking
    // fwrite() calls in the audio callback. The complete buffer will be
    // written to file in the destructor.
    const int size = bytes_per_sample * num_samples;
    if (!buffer_->Append((const uint8*)interleaved.get(), size))
      event_->Signal();
  }

  virtual void OnError(AudioInputStream* stream) OVERRIDE {}

 private:
  base::WaitableEvent* event_;
  AudioParameters params_;
  scoped_ptr<media::SeekableBuffer> buffer_;
  FILE* binary_file_;

  DISALLOW_COPY_AND_ASSIGN(FileAudioSink);
};

// Implements AudioInputCallback and AudioSourceCallback to support full
// duplex audio where captured samples are played out in loopback after
// reading from a temporary FIFO storage.
class FullDuplexAudioSinkSource
    : public AudioInputStream::AudioInputCallback,
      public AudioOutputStream::AudioSourceCallback {
 public:
  explicit FullDuplexAudioSinkSource(const AudioParameters& params)
      : params_(params),
        previous_time_(base::TimeTicks::Now()),
        started_(false) {
    // Start with a reasonably small FIFO size. It will be increased
    // dynamically during the test if required.
    fifo_.reset(new media::SeekableBuffer(0, 2 * params.GetBytesPerBuffer()));
    buffer_.reset(new uint8[params_.GetBytesPerBuffer()]);
  }

  virtual ~FullDuplexAudioSinkSource() {}

  // AudioInputStream::AudioInputCallback implementation
  virtual void OnData(AudioInputStream* stream,
                      const AudioBus* src,
                      uint32 hardware_delay_bytes,
                      double volume) OVERRIDE {
    const base::TimeTicks now_time = base::TimeTicks::Now();
    const int diff = (now_time - previous_time_).InMilliseconds();

    EXPECT_EQ(params_.bits_per_sample(), 16);
    const int num_samples = src->frames() * src->channels();
    scoped_ptr<int16> interleaved(new int16[num_samples]);
    const int bytes_per_sample = sizeof(*interleaved);
    src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get());
    const int size = bytes_per_sample * num_samples;

    base::AutoLock lock(lock_);
    if (diff > 1000) {
      started_ = true;
      previous_time_ = now_time;

      // Log out the extra delay added by the FIFO. This is a best effort
      // estimate. We might be +- 10ms off here.
      int extra_fifo_delay =
          static_cast<int>(BytesToMilliseconds(fifo_->forward_bytes() + size));
      DVLOG(1) << extra_fifo_delay;
    }

    // We add an initial delay of ~1 second before loopback starts to ensure
    // a stable callback sequence and to avoid initial bursts which might add
    // to the extra FIFO delay.
    if (!started_)
      return;

    // Append new data to the FIFO and extend the size if the max capacity
    // was exceeded. Flush the FIFO when extended just in case.
    if (!fifo_->Append((const uint8*)interleaved.get(), size)) {
      fifo_->set_forward_capacity(2 * fifo_->forward_capacity());
      fifo_->Clear();
    }
  }

  virtual void OnError(AudioInputStream* stream) OVERRIDE {}

  // AudioOutputStream::AudioSourceCallback implementation
  virtual int OnMoreData(AudioBus* dest,
                         AudioBuffersState buffers_state) OVERRIDE {
    const int size_in_bytes =
        (params_.bits_per_sample() / 8) * dest->frames() * dest->channels();
    EXPECT_EQ(size_in_bytes, params_.GetBytesPerBuffer());

    base::AutoLock lock(lock_);

    // We add an initial delay of ~1 second before loopback starts to ensure
    // a stable callback sequences and to avoid initial bursts which might add
    // to the extra FIFO delay.
    if (!started_) {
      dest->Zero();
      return dest->frames();
    }

    // Fill up destination with zeros if the FIFO does not contain enough
    // data to fulfill the request.
    if (fifo_->forward_bytes() < size_in_bytes) {
      dest->Zero();
    } else {
      fifo_->Read(buffer_.get(), size_in_bytes);
      dest->FromInterleaved(
          buffer_.get(), dest->frames(), params_.bits_per_sample() / 8);
    }

    return dest->frames();
  }

  virtual void OnError(AudioOutputStream* stream) OVERRIDE {}

 private:
  // Converts from bytes to milliseconds given number of bytes and existing
  // audio parameters.
  double BytesToMilliseconds(int bytes) const {
    const int frames = bytes / params_.GetBytesPerFrame();
    return (base::TimeDelta::FromMicroseconds(
                frames * base::Time::kMicrosecondsPerSecond /
                static_cast<double>(params_.sample_rate()))).InMillisecondsF();
  }

  AudioParameters params_;
  base::TimeTicks previous_time_;
  base::Lock lock_;
  scoped_ptr<media::SeekableBuffer> fifo_;
  scoped_ptr<uint8[]> buffer_;
  bool started_;

  DISALLOW_COPY_AND_ASSIGN(FullDuplexAudioSinkSource);
};

// Test fixture class for tests which only exercise the output path.
class AudioAndroidOutputTest : public testing::Test {
 public:
  AudioAndroidOutputTest()
      : loop_(new base::MessageLoopForUI()),
        audio_manager_(AudioManager::CreateForTesting()),
        audio_output_stream_(NULL) {
  }

  virtual ~AudioAndroidOutputTest() {
  }

 protected:
  AudioManager* audio_manager() { return audio_manager_.get(); }
  base::MessageLoopForUI* loop() { return loop_.get(); }
  const AudioParameters& audio_output_parameters() {
    return audio_output_parameters_;
  }

  // Synchronously runs the provided callback/closure on the audio thread.
  void RunOnAudioThread(const base::Closure& closure) {
    if (!audio_manager()->GetTaskRunner()->BelongsToCurrentThread()) {
      base::WaitableEvent event(false, false);
      audio_manager()->GetTaskRunner()->PostTask(
          FROM_HERE,
          base::Bind(&AudioAndroidOutputTest::RunOnAudioThreadImpl,
                     base::Unretained(this),
                     closure,
                     &event));
      event.Wait();
    } else {
      closure.Run();
    }
  }

  void RunOnAudioThreadImpl(const base::Closure& closure,
                            base::WaitableEvent* event) {
    DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
    closure.Run();
    event->Signal();
  }

  void GetDefaultOutputStreamParametersOnAudioThread() {
    RunOnAudioThread(
        base::Bind(&AudioAndroidOutputTest::GetDefaultOutputStreamParameters,
                   base::Unretained(this)));
  }

  void MakeAudioOutputStreamOnAudioThread(const AudioParameters& params) {
    RunOnAudioThread(
        base::Bind(&AudioAndroidOutputTest::MakeOutputStream,
                   base::Unretained(this),
                   params));
  }

  void OpenAndCloseAudioOutputStreamOnAudioThread() {
    RunOnAudioThread(
        base::Bind(&AudioAndroidOutputTest::OpenAndClose,
                   base::Unretained(this)));
  }

  void OpenAndStartAudioOutputStreamOnAudioThread(
      AudioOutputStream::AudioSourceCallback* source) {
    RunOnAudioThread(
        base::Bind(&AudioAndroidOutputTest::OpenAndStart,
                   base::Unretained(this),
                   source));
  }

  void StopAndCloseAudioOutputStreamOnAudioThread() {
    RunOnAudioThread(
        base::Bind(&AudioAndroidOutputTest::StopAndClose,
                   base::Unretained(this)));
  }

  double AverageTimeBetweenCallbacks(int num_callbacks) const {
    return ((end_time_ - start_time_) / static_cast<double>(num_callbacks - 1))
        .InMillisecondsF();
  }

  void StartOutputStreamCallbacks(const AudioParameters& params) {
    double expected_time_between_callbacks_ms =
        ExpectedTimeBetweenCallbacks(params);
    const int num_callbacks =
        (kCallbackTestTimeMs / expected_time_between_callbacks_ms);
    MakeAudioOutputStreamOnAudioThread(params);

    int count = 0;
    MockAudioSourceCallback source;

    EXPECT_CALL(source, OnMoreData(NotNull(), _))
        .Times(AtLeast(num_callbacks))
        .WillRepeatedly(
             DoAll(CheckCountAndPostQuitTask(&count, num_callbacks, loop()),
                   Invoke(RealOnMoreData)));
    EXPECT_CALL(source, OnError(audio_output_stream_)).Times(0);

    OpenAndStartAudioOutputStreamOnAudioThread(&source);

    start_time_ = base::TimeTicks::Now();
    loop()->Run();
    end_time_ = base::TimeTicks::Now();

    StopAndCloseAudioOutputStreamOnAudioThread();

    double average_time_between_callbacks_ms =
        AverageTimeBetweenCallbacks(num_callbacks);
    VLOG(0) << "expected time between callbacks: "
            << expected_time_between_callbacks_ms << " ms";
    VLOG(0) << "average time between callbacks: "
            << average_time_between_callbacks_ms << " ms";
    EXPECT_GE(average_time_between_callbacks_ms,
              0.70 * expected_time_between_callbacks_ms);
    EXPECT_LE(average_time_between_callbacks_ms,
              1.35 * expected_time_between_callbacks_ms);
  }

  void GetDefaultOutputStreamParameters() {
    DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
    audio_output_parameters_ =
        audio_manager()->GetDefaultOutputStreamParameters();
    EXPECT_TRUE(audio_output_parameters_.IsValid());
  }

  void MakeOutputStream(const AudioParameters& params) {
    DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
    audio_output_stream_ = audio_manager()->MakeAudioOutputStream(
        params, std::string());
    EXPECT_TRUE(audio_output_stream_);
  }

  void OpenAndClose() {
    DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
    EXPECT_TRUE(audio_output_stream_->Open());
    audio_output_stream_->Close();
    audio_output_stream_ = NULL;
  }

  void OpenAndStart(AudioOutputStream::AudioSourceCallback* source) {
    DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
    EXPECT_TRUE(audio_output_stream_->Open());
    audio_output_stream_->Start(source);
  }

  void StopAndClose() {
    DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
    audio_output_stream_->Stop();
    audio_output_stream_->Close();
    audio_output_stream_ = NULL;
  }

  scoped_ptr<base::MessageLoopForUI> loop_;
  scoped_ptr<AudioManager> audio_manager_;
  AudioParameters audio_output_parameters_;
  AudioOutputStream* audio_output_stream_;
  base::TimeTicks start_time_;
  base::TimeTicks end_time_;

 private:
  DISALLOW_COPY_AND_ASSIGN(AudioAndroidOutputTest);
};

// AudioRecordInputStream should only be created on Jelly Bean and higher. This
// ensures we only test against the AudioRecord path when that is satisfied.
std::vector<bool> RunAudioRecordInputPathTests() {
  std::vector<bool> tests;
  tests.push_back(false);
  if (base::android::BuildInfo::GetInstance()->sdk_int() >= 16)
    tests.push_back(true);
  return tests;
}

// Test fixture class for tests which exercise the input path, or both input and
// output paths. It is value-parameterized to test against both the Java
// AudioRecord (when true) and native OpenSLES (when false) input paths.
class AudioAndroidInputTest : public AudioAndroidOutputTest,
                              public testing::WithParamInterface<bool> {
 public:
  AudioAndroidInputTest() : audio_input_stream_(NULL) {}

 protected:
  const AudioParameters& audio_input_parameters() {
    return audio_input_parameters_;
  }

  AudioParameters GetInputStreamParameters() {
    GetDefaultInputStreamParametersOnAudioThread();

    // Override the platform effects setting to use the AudioRecord or OpenSLES
    // path as requested.
    int effects = GetParam() ? AudioParameters::ECHO_CANCELLER :
                               AudioParameters::NO_EFFECTS;
    AudioParameters params(audio_input_parameters().format(),
                           audio_input_parameters().channel_layout(),
                           audio_input_parameters().input_channels(),
                           audio_input_parameters().sample_rate(),
                           audio_input_parameters().bits_per_sample(),
                           audio_input_parameters().frames_per_buffer(),
                           effects);
    return params;
  }

  void GetDefaultInputStreamParametersOnAudioThread() {
     RunOnAudioThread(
        base::Bind(&AudioAndroidInputTest::GetDefaultInputStreamParameters,
                   base::Unretained(this)));
  }

  void MakeAudioInputStreamOnAudioThread(const AudioParameters& params) {
    RunOnAudioThread(
        base::Bind(&AudioAndroidInputTest::MakeInputStream,
                   base::Unretained(this),
                   params));
  }

  void OpenAndCloseAudioInputStreamOnAudioThread() {
    RunOnAudioThread(
        base::Bind(&AudioAndroidInputTest::OpenAndClose,
                   base::Unretained(this)));
  }

  void OpenAndStartAudioInputStreamOnAudioThread(
      AudioInputStream::AudioInputCallback* sink) {
    RunOnAudioThread(
        base::Bind(&AudioAndroidInputTest::OpenAndStart,
                   base::Unretained(this),
                   sink));
  }

  void StopAndCloseAudioInputStreamOnAudioThread() {
    RunOnAudioThread(
        base::Bind(&AudioAndroidInputTest::StopAndClose,
                   base::Unretained(this)));
  }

  void StartInputStreamCallbacks(const AudioParameters& params) {
    double expected_time_between_callbacks_ms =
        ExpectedTimeBetweenCallbacks(params);
    const int num_callbacks =
        (kCallbackTestTimeMs / expected_time_between_callbacks_ms);

    MakeAudioInputStreamOnAudioThread(params);

    int count = 0;
    MockAudioInputCallback sink;

    EXPECT_CALL(sink, OnData(audio_input_stream_, NotNull(), _, _))
        .Times(AtLeast(num_callbacks))
        .WillRepeatedly(
            CheckCountAndPostQuitTask(&count, num_callbacks, loop()));
    EXPECT_CALL(sink, OnError(audio_input_stream_)).Times(0);

    OpenAndStartAudioInputStreamOnAudioThread(&sink);

    start_time_ = base::TimeTicks::Now();
    loop()->Run();
    end_time_ = base::TimeTicks::Now();

    StopAndCloseAudioInputStreamOnAudioThread();

    double average_time_between_callbacks_ms =
        AverageTimeBetweenCallbacks(num_callbacks);
    VLOG(0) << "expected time between callbacks: "
            << expected_time_between_callbacks_ms << " ms";
    VLOG(0) << "average time between callbacks: "
            << average_time_between_callbacks_ms << " ms";
    EXPECT_GE(average_time_between_callbacks_ms,
              0.70 * expected_time_between_callbacks_ms);
    EXPECT_LE(average_time_between_callbacks_ms,
              1.30 * expected_time_between_callbacks_ms);
  }

  void GetDefaultInputStreamParameters() {
    DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
    audio_input_parameters_ = audio_manager()->GetInputStreamParameters(
        AudioManagerBase::kDefaultDeviceId);
  }

  void MakeInputStream(const AudioParameters& params) {
    DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
    audio_input_stream_ = audio_manager()->MakeAudioInputStream(
        params, AudioManagerBase::kDefaultDeviceId);
    EXPECT_TRUE(audio_input_stream_);
  }

  void OpenAndClose() {
    DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
    EXPECT_TRUE(audio_input_stream_->Open());
    audio_input_stream_->Close();
    audio_input_stream_ = NULL;
  }

  void OpenAndStart(AudioInputStream::AudioInputCallback* sink) {
    DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
    EXPECT_TRUE(audio_input_stream_->Open());
    audio_input_stream_->Start(sink);
  }

  void StopAndClose() {
    DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
    audio_input_stream_->Stop();
    audio_input_stream_->Close();
    audio_input_stream_ = NULL;
  }

  AudioInputStream* audio_input_stream_;
  AudioParameters audio_input_parameters_;

 private:
  DISALLOW_COPY_AND_ASSIGN(AudioAndroidInputTest);
};

// Get the default audio input parameters and log the result.
TEST_P(AudioAndroidInputTest, GetDefaultInputStreamParameters) {
  // We don't go through AudioAndroidInputTest::GetInputStreamParameters() here
  // so that we can log the real (non-overridden) values of the effects.
  GetDefaultInputStreamParametersOnAudioThread();
  EXPECT_TRUE(audio_input_parameters().IsValid());
  VLOG(1) << audio_input_parameters();
}

// Get the default audio output parameters and log the result.
TEST_F(AudioAndroidOutputTest, GetDefaultOutputStreamParameters) {
  GetDefaultOutputStreamParametersOnAudioThread();
  VLOG(1) << audio_output_parameters();
}

// Verify input device enumeration.
TEST_F(AudioAndroidInputTest, GetAudioInputDeviceNames) {
  if (!audio_manager()->HasAudioInputDevices())
    return;
  AudioDeviceNames devices;
  RunOnAudioThread(
      base::Bind(&AudioManager::GetAudioInputDeviceNames,
                 base::Unretained(audio_manager()),
                 &devices));
  CheckDeviceNames(devices);
}

// Verify output device enumeration.
TEST_F(AudioAndroidOutputTest, GetAudioOutputDeviceNames) {
  if (!audio_manager()->HasAudioOutputDevices())
    return;
  AudioDeviceNames devices;
  RunOnAudioThread(
      base::Bind(&AudioManager::GetAudioOutputDeviceNames,
                 base::Unretained(audio_manager()),
                 &devices));
  CheckDeviceNames(devices);
}

// Ensure that a default input stream can be created and closed.
TEST_P(AudioAndroidInputTest, CreateAndCloseInputStream) {
  AudioParameters params = GetInputStreamParameters();
  MakeAudioInputStreamOnAudioThread(params);
  RunOnAudioThread(
      base::Bind(&AudioInputStream::Close,
                 base::Unretained(audio_input_stream_)));
}

// Ensure that a default output stream can be created and closed.
// TODO(henrika): should we also verify that this API changes the audio mode
// to communication mode, and calls RegisterHeadsetReceiver, the first time
// it is called?
TEST_F(AudioAndroidOutputTest, CreateAndCloseOutputStream) {
  GetDefaultOutputStreamParametersOnAudioThread();
  MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
  RunOnAudioThread(
      base::Bind(&AudioOutputStream::Close,
                 base::Unretained(audio_output_stream_)));
}

// Ensure that a default input stream can be opened and closed.
TEST_P(AudioAndroidInputTest, OpenAndCloseInputStream) {
  AudioParameters params = GetInputStreamParameters();
  MakeAudioInputStreamOnAudioThread(params);
  OpenAndCloseAudioInputStreamOnAudioThread();
}

// Ensure that a default output stream can be opened and closed.
TEST_F(AudioAndroidOutputTest, OpenAndCloseOutputStream) {
  GetDefaultOutputStreamParametersOnAudioThread();
  MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
  OpenAndCloseAudioOutputStreamOnAudioThread();
}

// Start input streaming using default input parameters and ensure that the
// callback sequence is sane.
TEST_P(AudioAndroidInputTest, DISABLED_StartInputStreamCallbacks) {
  AudioParameters native_params = GetInputStreamParameters();
  StartInputStreamCallbacks(native_params);
}

// Start input streaming using non default input parameters and ensure that the
// callback sequence is sane. The only change we make in this test is to select
// a 10ms buffer size instead of the default size.
TEST_P(AudioAndroidInputTest,
       DISABLED_StartInputStreamCallbacksNonDefaultParameters) {
  AudioParameters native_params = GetInputStreamParameters();
  AudioParameters params(native_params.format(),
                         native_params.channel_layout(),
                         native_params.input_channels(),
                         native_params.sample_rate(),
                         native_params.bits_per_sample(),
                         native_params.sample_rate() / 100,
                         native_params.effects());
  StartInputStreamCallbacks(params);
}

// Start output streaming using default output parameters and ensure that the
// callback sequence is sane.
TEST_F(AudioAndroidOutputTest, StartOutputStreamCallbacks) {
  GetDefaultOutputStreamParametersOnAudioThread();
  StartOutputStreamCallbacks(audio_output_parameters());
}

// Start output streaming using non default output parameters and ensure that
// the callback sequence is sane. The only change we make in this test is to
// select a 10ms buffer size instead of the default size and to open up the
// device in mono.
// TODO(henrika): possibly add support for more variations.
TEST_F(AudioAndroidOutputTest, DISABLED_StartOutputStreamCallbacksNonDefaultParameters) {
  GetDefaultOutputStreamParametersOnAudioThread();
  AudioParameters params(audio_output_parameters().format(),
                         CHANNEL_LAYOUT_MONO,
                         audio_output_parameters().sample_rate(),
                         audio_output_parameters().bits_per_sample(),
                         audio_output_parameters().sample_rate() / 100);
  StartOutputStreamCallbacks(params);
}

// Play out a PCM file segment in real time and allow the user to verify that
// the rendered audio sounds OK.
// NOTE: this test requires user interaction and is not designed to run as an
// automatized test on bots.
TEST_F(AudioAndroidOutputTest, DISABLED_RunOutputStreamWithFileAsSource) {
  GetDefaultOutputStreamParametersOnAudioThread();
  VLOG(1) << audio_output_parameters();
  MakeAudioOutputStreamOnAudioThread(audio_output_parameters());

  std::string file_name;
  const AudioParameters params = audio_output_parameters();
  if (params.sample_rate() == 48000 && params.channels() == 2) {
    file_name = kSpeechFile_16b_s_48k;
  } else if (params.sample_rate() == 48000 && params.channels() == 1) {
    file_name = kSpeechFile_16b_m_48k;
  } else if (params.sample_rate() == 44100 && params.channels() == 2) {
    file_name = kSpeechFile_16b_s_44k;
  } else if (params.sample_rate() == 44100 && params.channels() == 1) {
    file_name = kSpeechFile_16b_m_44k;
  } else {
    FAIL() << "This test supports 44.1kHz and 48kHz mono/stereo only.";
    return;
  }

  base::WaitableEvent event(false, false);
  FileAudioSource source(&event, file_name);

  OpenAndStartAudioOutputStreamOnAudioThread(&source);
  VLOG(0) << ">> Verify that the file is played out correctly...";
  EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
  StopAndCloseAudioOutputStreamOnAudioThread();
}

// Start input streaming and run it for ten seconds while recording to a
// local audio file.
// NOTE: this test requires user interaction and is not designed to run as an
// automatized test on bots.
TEST_P(AudioAndroidInputTest, DISABLED_RunSimplexInputStreamWithFileAsSink) {
  AudioParameters params = GetInputStreamParameters();
  VLOG(1) << params;
  MakeAudioInputStreamOnAudioThread(params);

  std::string file_name = base::StringPrintf("out_simplex_%d_%d_%d.pcm",
                                             params.sample_rate(),
                                             params.frames_per_buffer(),
                                             params.channels());

  base::WaitableEvent event(false, false);
  FileAudioSink sink(&event, params, file_name);

  OpenAndStartAudioInputStreamOnAudioThread(&sink);
  VLOG(0) << ">> Speak into the microphone to record audio...";
  EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
  StopAndCloseAudioInputStreamOnAudioThread();
}

// Same test as RunSimplexInputStreamWithFileAsSink but this time output
// streaming is active as well (reads zeros only).
// NOTE: this test requires user interaction and is not designed to run as an
// automatized test on bots.
TEST_P(AudioAndroidInputTest, DISABLED_RunDuplexInputStreamWithFileAsSink) {
  AudioParameters in_params = GetInputStreamParameters();
  VLOG(1) << in_params;
  MakeAudioInputStreamOnAudioThread(in_params);

  GetDefaultOutputStreamParametersOnAudioThread();
  VLOG(1) << audio_output_parameters();
  MakeAudioOutputStreamOnAudioThread(audio_output_parameters());

  std::string file_name = base::StringPrintf("out_duplex_%d_%d_%d.pcm",
                                             in_params.sample_rate(),
                                             in_params.frames_per_buffer(),
                                             in_params.channels());

  base::WaitableEvent event(false, false);
  FileAudioSink sink(&event, in_params, file_name);
  MockAudioSourceCallback source;

  EXPECT_CALL(source, OnMoreData(NotNull(), _))
      .WillRepeatedly(Invoke(RealOnMoreData));
  EXPECT_CALL(source, OnError(audio_output_stream_)).Times(0);

  OpenAndStartAudioInputStreamOnAudioThread(&sink);
  OpenAndStartAudioOutputStreamOnAudioThread(&source);
  VLOG(0) << ">> Speak into the microphone to record audio";
  EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
  StopAndCloseAudioOutputStreamOnAudioThread();
  StopAndCloseAudioInputStreamOnAudioThread();
}

// Start audio in both directions while feeding captured data into a FIFO so
// it can be read directly (in loopback) by the render side. A small extra
// delay will be added by the FIFO and an estimate of this delay will be
// printed out during the test.
// NOTE: this test requires user interaction and is not designed to run as an
// automatized test on bots.
TEST_P(AudioAndroidInputTest,
       DISABLED_RunSymmetricInputAndOutputStreamsInFullDuplex) {
  // Get native audio parameters for the input side.
  AudioParameters default_input_params = GetInputStreamParameters();

  // Modify the parameters so that both input and output can use the same
  // parameters by selecting 10ms as buffer size. This will also ensure that
  // the output stream will be a mono stream since mono is default for input
  // audio on Android.
  AudioParameters io_params(default_input_params.format(),
                            default_input_params.channel_layout(),
                            ChannelLayoutToChannelCount(
                                default_input_params.channel_layout()),
                            default_input_params.sample_rate(),
                            default_input_params.bits_per_sample(),
                            default_input_params.sample_rate() / 100,
                            default_input_params.effects());
  VLOG(1) << io_params;

  // Create input and output streams using the common audio parameters.
  MakeAudioInputStreamOnAudioThread(io_params);
  MakeAudioOutputStreamOnAudioThread(io_params);

  FullDuplexAudioSinkSource full_duplex(io_params);

  // Start a full duplex audio session and print out estimates of the extra
  // delay we should expect from the FIFO. If real-time delay measurements are
  // performed, the result should be reduced by this extra delay since it is
  // something that has been added by the test.
  OpenAndStartAudioInputStreamOnAudioThread(&full_duplex);
  OpenAndStartAudioOutputStreamOnAudioThread(&full_duplex);
  VLOG(1) << "HINT: an estimate of the extra FIFO delay will be updated "
          << "once per second during this test.";
  VLOG(0) << ">> Speak into the mic and listen to the audio in loopback...";
  fflush(stdout);
  base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(20));
  printf("\n");
  StopAndCloseAudioOutputStreamOnAudioThread();
  StopAndCloseAudioInputStreamOnAudioThread();
}

INSTANTIATE_TEST_CASE_P(AudioAndroidInputTest, AudioAndroidInputTest,
    testing::ValuesIn(RunAudioRecordInputPathTests()));

}  // namespace media