OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 #include <limits> | 12 #include <limits> |
13 #include <list> | 13 #include <list> |
14 #include <memory> | 14 #include <memory> |
15 #include <numeric> | 15 #include <numeric> |
16 #include <string> | 16 #include <string> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "webrtc/base/arraysize.h" | 19 #include "webrtc/base/arraysize.h" |
20 #include "webrtc/base/criticalsection.h" | 20 #include "webrtc/base/criticalsection.h" |
21 #include "webrtc/base/format_macros.h" | 21 #include "webrtc/base/format_macros.h" |
22 #include "webrtc/base/scoped_ref_ptr.h" | 22 #include "webrtc/base/scoped_ref_ptr.h" |
23 #include "webrtc/modules/audio_device/android/audio_common.h" | 23 #include "webrtc/modules/audio_device/android/audio_common.h" |
24 #include "webrtc/modules/audio_device/android/audio_manager.h" | 24 #include "webrtc/modules/audio_device/android/audio_manager.h" |
25 #include "webrtc/modules/audio_device/android/build_info.h" | 25 #include "webrtc/modules/audio_device/android/build_info.h" |
26 #include "webrtc/modules/audio_device/android/ensure_initialized.h" | 26 #include "webrtc/modules/audio_device/android/ensure_initialized.h" |
27 #include "webrtc/modules/audio_device/audio_device_impl.h" | 27 #include "webrtc/modules/audio_device/audio_device_impl.h" |
28 #include "webrtc/modules/audio_device/include/audio_device.h" | 28 #include "webrtc/modules/audio_device/include/audio_device.h" |
| 29 #include "webrtc/modules/audio_device/include/mock_audio_transport.h" |
29 #include "webrtc/system_wrappers/include/clock.h" | 30 #include "webrtc/system_wrappers/include/clock.h" |
30 #include "webrtc/system_wrappers/include/event_wrapper.h" | 31 #include "webrtc/system_wrappers/include/event_wrapper.h" |
31 #include "webrtc/system_wrappers/include/sleep.h" | 32 #include "webrtc/system_wrappers/include/sleep.h" |
32 #include "webrtc/test/gmock.h" | 33 #include "webrtc/test/gmock.h" |
33 #include "webrtc/test/gtest.h" | 34 #include "webrtc/test/gtest.h" |
34 #include "webrtc/test/testsupport/fileutils.h" | 35 #include "webrtc/test/testsupport/fileutils.h" |
35 | 36 |
36 using std::cout; | 37 using std::cout; |
37 using std::endl; | 38 using std::endl; |
38 using ::testing::_; | 39 using ::testing::_; |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 const size_t bytes_per_buffer_; | 361 const size_t bytes_per_buffer_; |
361 size_t play_count_; | 362 size_t play_count_; |
362 size_t rec_count_; | 363 size_t rec_count_; |
363 int64_t pulse_time_; | 364 int64_t pulse_time_; |
364 std::vector<int> latencies_; | 365 std::vector<int> latencies_; |
365 }; | 366 }; |
366 | 367 |
367 // Mocks the AudioTransport object and proxies actions for the two callbacks | 368 // Mocks the AudioTransport object and proxies actions for the two callbacks |
368 // (RecordedDataIsAvailable and NeedMorePlayData) to different implementations | 369 // (RecordedDataIsAvailable and NeedMorePlayData) to different implementations |
369 // of AudioStreamInterface. | 370 // of AudioStreamInterface. |
370 class MockAudioTransport : public AudioTransport { | 371 class MockAudioTransportAndroid : public test::MockAudioTransport { |
371 public: | 372 public: |
372 explicit MockAudioTransport(int type) | 373 explicit MockAudioTransportAndroid(int type) |
373 : num_callbacks_(0), | 374 : num_callbacks_(0), |
374 type_(type), | 375 type_(type), |
375 play_count_(0), | 376 play_count_(0), |
376 rec_count_(0), | 377 rec_count_(0), |
377 audio_stream_(nullptr) {} | 378 audio_stream_(nullptr) {} |
378 | 379 |
379 virtual ~MockAudioTransport() {} | 380 virtual ~MockAudioTransportAndroid() {} |
380 | |
381 MOCK_METHOD10(RecordedDataIsAvailable, | |
382 int32_t(const void* audioSamples, | |
383 const size_t nSamples, | |
384 const size_t nBytesPerSample, | |
385 const size_t nChannels, | |
386 const uint32_t samplesPerSec, | |
387 const uint32_t totalDelayMS, | |
388 const int32_t clockDrift, | |
389 const uint32_t currentMicLevel, | |
390 const bool keyPressed, | |
391 uint32_t& newMicLevel)); | |
392 | |
393 MOCK_METHOD8(NeedMorePlayData, | |
394 int32_t(const size_t nSamples, | |
395 const size_t nBytesPerSample, | |
396 const size_t nChannels, | |
397 const uint32_t samplesPerSec, | |
398 void* audioSamples, | |
399 size_t& nSamplesOut, | |
400 int64_t* elapsed_time_ms, | |
401 int64_t* ntp_time_ms)); | |
402 | |
403 MOCK_METHOD6(PushCaptureData, | |
404 void(int voe_channel, | |
405 const void* audio_data, | |
406 int bits_per_sample, | |
407 int sample_rate, | |
408 size_t number_of_channels, | |
409 size_t number_of_frames)); | |
410 | |
411 MOCK_METHOD7(PullRenderData, | |
412 void(int bits_per_sample, | |
413 int sample_rate, | |
414 size_t number_of_channels, | |
415 size_t number_of_frames, | |
416 void* audio_data, | |
417 int64_t* elapsed_time_ms, | |
418 int64_t* ntp_time_ms)); | |
419 | 381 |
420 // Set default actions of the mock object. We are delegating to fake | 382 // Set default actions of the mock object. We are delegating to fake |
421 // implementations (of AudioStreamInterface) here. | 383 // implementations (of AudioStreamInterface) here. |
422 void HandleCallbacks(EventWrapper* test_is_done, | 384 void HandleCallbacks(EventWrapper* test_is_done, |
423 AudioStreamInterface* audio_stream, | 385 AudioStreamInterface* audio_stream, |
424 int num_callbacks) { | 386 int num_callbacks) { |
425 test_is_done_ = test_is_done; | 387 test_is_done_ = test_is_done; |
426 audio_stream_ = audio_stream; | 388 audio_stream_ = audio_stream; |
427 num_callbacks_ = num_callbacks; | 389 num_callbacks_ = num_callbacks; |
428 if (play_mode()) { | 390 if (play_mode()) { |
429 ON_CALL(*this, NeedMorePlayData(_, _, _, _, _, _, _, _)) | 391 ON_CALL(*this, NeedMorePlayData(_, _, _, _, _, _, _, _)) |
430 .WillByDefault( | 392 .WillByDefault( |
431 Invoke(this, &MockAudioTransport::RealNeedMorePlayData)); | 393 Invoke(this, &MockAudioTransportAndroid::RealNeedMorePlayData)); |
432 } | 394 } |
433 if (rec_mode()) { | 395 if (rec_mode()) { |
434 ON_CALL(*this, RecordedDataIsAvailable(_, _, _, _, _, _, _, _, _, _)) | 396 ON_CALL(*this, RecordedDataIsAvailable(_, _, _, _, _, _, _, _, _, _)) |
435 .WillByDefault( | 397 .WillByDefault(Invoke( |
436 Invoke(this, &MockAudioTransport::RealRecordedDataIsAvailable)); | 398 this, &MockAudioTransportAndroid::RealRecordedDataIsAvailable)); |
437 } | 399 } |
438 } | 400 } |
439 | 401 |
440 int32_t RealRecordedDataIsAvailable(const void* audioSamples, | 402 int32_t RealRecordedDataIsAvailable(const void* audioSamples, |
441 const size_t nSamples, | 403 const size_t nSamples, |
442 const size_t nBytesPerSample, | 404 const size_t nBytesPerSample, |
443 const size_t nChannels, | 405 const size_t nChannels, |
444 const uint32_t samplesPerSec, | 406 const uint32_t samplesPerSec, |
445 const uint32_t totalDelayMS, | 407 const uint32_t totalDelayMS, |
446 const int32_t clockDrift, | 408 const int32_t clockDrift, |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
892 TEST_F(AudioDeviceTest, StopRecordingRequiresInitToRestart) { | 854 TEST_F(AudioDeviceTest, StopRecordingRequiresInitToRestart) { |
893 EXPECT_EQ(0, audio_device()->InitRecording()); | 855 EXPECT_EQ(0, audio_device()->InitRecording()); |
894 EXPECT_EQ(0, audio_device()->StartRecording()); | 856 EXPECT_EQ(0, audio_device()->StartRecording()); |
895 EXPECT_EQ(0, audio_device()->StopRecording()); | 857 EXPECT_EQ(0, audio_device()->StopRecording()); |
896 EXPECT_FALSE(audio_device()->RecordingIsInitialized()); | 858 EXPECT_FALSE(audio_device()->RecordingIsInitialized()); |
897 } | 859 } |
898 | 860 |
899 // Start playout and verify that the native audio layer starts asking for real | 861 // Start playout and verify that the native audio layer starts asking for real |
900 // audio samples to play out using the NeedMorePlayData callback. | 862 // audio samples to play out using the NeedMorePlayData callback. |
901 TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) { | 863 TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) { |
902 MockAudioTransport mock(kPlayout); | 864 MockAudioTransportAndroid mock(kPlayout); |
903 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks); | 865 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks); |
904 EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(), | 866 EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(), |
905 kBytesPerSample, | 867 kBytesPerSample, |
906 playout_channels(), | 868 playout_channels(), |
907 playout_sample_rate(), | 869 playout_sample_rate(), |
908 NotNull(), | 870 NotNull(), |
909 _, _, _)) | 871 _, _, _)) |
910 .Times(AtLeast(kNumCallbacks)); | 872 .Times(AtLeast(kNumCallbacks)); |
911 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); | 873 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
912 StartPlayout(); | 874 StartPlayout(); |
913 test_is_done_->Wait(kTestTimeOutInMilliseconds); | 875 test_is_done_->Wait(kTestTimeOutInMilliseconds); |
914 StopPlayout(); | 876 StopPlayout(); |
915 } | 877 } |
916 | 878 |
917 // Start recording and verify that the native audio layer starts feeding real | 879 // Start recording and verify that the native audio layer starts feeding real |
918 // audio samples via the RecordedDataIsAvailable callback. | 880 // audio samples via the RecordedDataIsAvailable callback. |
919 TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) { | 881 TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) { |
920 MockAudioTransport mock(kRecording); | 882 MockAudioTransportAndroid mock(kRecording); |
921 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks); | 883 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks); |
922 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), | 884 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), |
923 record_frames_per_10ms_buffer(), | 885 record_frames_per_10ms_buffer(), |
924 kBytesPerSample, | 886 kBytesPerSample, |
925 record_channels(), | 887 record_channels(), |
926 record_sample_rate(), | 888 record_sample_rate(), |
927 total_delay_ms(), | 889 total_delay_ms(), |
928 0, | 890 0, |
929 0, | 891 0, |
930 false, | 892 false, |
931 _)) | 893 _)) |
932 .Times(AtLeast(kNumCallbacks)); | 894 .Times(AtLeast(kNumCallbacks)); |
933 | 895 |
934 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); | 896 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
935 StartRecording(); | 897 StartRecording(); |
936 test_is_done_->Wait(kTestTimeOutInMilliseconds); | 898 test_is_done_->Wait(kTestTimeOutInMilliseconds); |
937 StopRecording(); | 899 StopRecording(); |
938 } | 900 } |
939 | 901 |
940 | 902 |
941 // Start playout and recording (full-duplex audio) and verify that audio is | 903 // Start playout and recording (full-duplex audio) and verify that audio is |
942 // active in both directions. | 904 // active in both directions. |
943 TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) { | 905 TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) { |
944 MockAudioTransport mock(kPlayout | kRecording); | 906 MockAudioTransportAndroid mock(kPlayout | kRecording); |
945 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks); | 907 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks); |
946 EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(), | 908 EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(), |
947 kBytesPerSample, | 909 kBytesPerSample, |
948 playout_channels(), | 910 playout_channels(), |
949 playout_sample_rate(), | 911 playout_sample_rate(), |
950 NotNull(), | 912 NotNull(), |
951 _, _, _)) | 913 _, _, _)) |
952 .Times(AtLeast(kNumCallbacks)); | 914 .Times(AtLeast(kNumCallbacks)); |
953 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), | 915 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), |
954 record_frames_per_10ms_buffer(), | 916 record_frames_per_10ms_buffer(), |
(...skipping 13 matching lines...) Expand all Loading... |
968 StopRecording(); | 930 StopRecording(); |
969 StopPlayout(); | 931 StopPlayout(); |
970 } | 932 } |
971 | 933 |
972 // Start playout and read audio from an external PCM file when the audio layer | 934 // Start playout and read audio from an external PCM file when the audio layer |
973 // asks for data to play out. Real audio is played out in this test but it does | 935 // asks for data to play out. Real audio is played out in this test but it does |
974 // not contain any explicit verification that the audio quality is perfect. | 936 // not contain any explicit verification that the audio quality is perfect. |
975 TEST_F(AudioDeviceTest, RunPlayoutWithFileAsSource) { | 937 TEST_F(AudioDeviceTest, RunPlayoutWithFileAsSource) { |
976 // TODO(henrika): extend test when mono output is supported. | 938 // TODO(henrika): extend test when mono output is supported. |
977 EXPECT_EQ(1u, playout_channels()); | 939 EXPECT_EQ(1u, playout_channels()); |
978 NiceMock<MockAudioTransport> mock(kPlayout); | 940 NiceMock<MockAudioTransportAndroid> mock(kPlayout); |
979 const int num_callbacks = kFilePlayTimeInSec * kNumCallbacksPerSecond; | 941 const int num_callbacks = kFilePlayTimeInSec * kNumCallbacksPerSecond; |
980 std::string file_name = GetFileName(playout_sample_rate()); | 942 std::string file_name = GetFileName(playout_sample_rate()); |
981 std::unique_ptr<FileAudioStream> file_audio_stream( | 943 std::unique_ptr<FileAudioStream> file_audio_stream( |
982 new FileAudioStream(num_callbacks, file_name, playout_sample_rate())); | 944 new FileAudioStream(num_callbacks, file_name, playout_sample_rate())); |
983 mock.HandleCallbacks(test_is_done_.get(), | 945 mock.HandleCallbacks(test_is_done_.get(), |
984 file_audio_stream.get(), | 946 file_audio_stream.get(), |
985 num_callbacks); | 947 num_callbacks); |
986 // SetMaxPlayoutVolume(); | 948 // SetMaxPlayoutVolume(); |
987 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); | 949 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
988 StartPlayout(); | 950 StartPlayout(); |
(...skipping 10 matching lines...) Expand all Loading... |
999 // of the FIFO will vary more due to an unbalance between the two sides. | 961 // of the FIFO will vary more due to an unbalance between the two sides. |
1000 // This test tries to verify that the device maintains a balanced callback- | 962 // This test tries to verify that the device maintains a balanced callback- |
1001 // sequence by running in loopback for ten seconds while measuring the size | 963 // sequence by running in loopback for ten seconds while measuring the size |
1002 // (max and average) of the FIFO. The size of the FIFO is increased by the | 964 // (max and average) of the FIFO. The size of the FIFO is increased by the |
1003 // recording side and decreased by the playout side. | 965 // recording side and decreased by the playout side. |
1004 // TODO(henrika): tune the final test parameters after running tests on several | 966 // TODO(henrika): tune the final test parameters after running tests on several |
1005 // different devices. | 967 // different devices. |
1006 TEST_F(AudioDeviceTest, RunPlayoutAndRecordingInFullDuplex) { | 968 TEST_F(AudioDeviceTest, RunPlayoutAndRecordingInFullDuplex) { |
1007 EXPECT_EQ(record_channels(), playout_channels()); | 969 EXPECT_EQ(record_channels(), playout_channels()); |
1008 EXPECT_EQ(record_sample_rate(), playout_sample_rate()); | 970 EXPECT_EQ(record_sample_rate(), playout_sample_rate()); |
1009 NiceMock<MockAudioTransport> mock(kPlayout | kRecording); | 971 NiceMock<MockAudioTransportAndroid> mock(kPlayout | kRecording); |
1010 std::unique_ptr<FifoAudioStream> fifo_audio_stream( | 972 std::unique_ptr<FifoAudioStream> fifo_audio_stream( |
1011 new FifoAudioStream(playout_frames_per_10ms_buffer())); | 973 new FifoAudioStream(playout_frames_per_10ms_buffer())); |
1012 mock.HandleCallbacks(test_is_done_.get(), | 974 mock.HandleCallbacks(test_is_done_.get(), |
1013 fifo_audio_stream.get(), | 975 fifo_audio_stream.get(), |
1014 kFullDuplexTimeInSec * kNumCallbacksPerSecond); | 976 kFullDuplexTimeInSec * kNumCallbacksPerSecond); |
1015 SetMaxPlayoutVolume(); | 977 SetMaxPlayoutVolume(); |
1016 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); | 978 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
1017 StartRecording(); | 979 StartRecording(); |
1018 StartPlayout(); | 980 StartPlayout(); |
1019 test_is_done_->Wait(std::max(kTestTimeOutInMilliseconds, | 981 test_is_done_->Wait(std::max(kTestTimeOutInMilliseconds, |
(...skipping 13 matching lines...) Expand all Loading... |
1033 // The latency is measured like so: | 995 // The latency is measured like so: |
1034 // - Insert impulses periodically on the output side. | 996 // - Insert impulses periodically on the output side. |
1035 // - Detect the impulses on the input side. | 997 // - Detect the impulses on the input side. |
1036 // - Measure the time difference between the transmit time and receive time. | 998 // - Measure the time difference between the transmit time and receive time. |
1037 // - Store time differences in a vector and calculate min, max and average. | 999 // - Store time differences in a vector and calculate min, max and average. |
1038 // This test requires a special hardware called Audio Loopback Dongle. | 1000 // This test requires a special hardware called Audio Loopback Dongle. |
1039 // See http://source.android.com/devices/audio/loopback.html for details. | 1001 // See http://source.android.com/devices/audio/loopback.html for details. |
1040 TEST_F(AudioDeviceTest, DISABLED_MeasureLoopbackLatency) { | 1002 TEST_F(AudioDeviceTest, DISABLED_MeasureLoopbackLatency) { |
1041 EXPECT_EQ(record_channels(), playout_channels()); | 1003 EXPECT_EQ(record_channels(), playout_channels()); |
1042 EXPECT_EQ(record_sample_rate(), playout_sample_rate()); | 1004 EXPECT_EQ(record_sample_rate(), playout_sample_rate()); |
1043 NiceMock<MockAudioTransport> mock(kPlayout | kRecording); | 1005 NiceMock<MockAudioTransportAndroid> mock(kPlayout | kRecording); |
1044 std::unique_ptr<LatencyMeasuringAudioStream> latency_audio_stream( | 1006 std::unique_ptr<LatencyMeasuringAudioStream> latency_audio_stream( |
1045 new LatencyMeasuringAudioStream(playout_frames_per_10ms_buffer())); | 1007 new LatencyMeasuringAudioStream(playout_frames_per_10ms_buffer())); |
1046 mock.HandleCallbacks(test_is_done_.get(), | 1008 mock.HandleCallbacks(test_is_done_.get(), |
1047 latency_audio_stream.get(), | 1009 latency_audio_stream.get(), |
1048 kMeasureLatencyTimeInSec * kNumCallbacksPerSecond); | 1010 kMeasureLatencyTimeInSec * kNumCallbacksPerSecond); |
1049 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); | 1011 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
1050 SetMaxPlayoutVolume(); | 1012 SetMaxPlayoutVolume(); |
1051 DisableBuiltInAECIfAvailable(); | 1013 DisableBuiltInAECIfAvailable(); |
1052 StartRecording(); | 1014 StartRecording(); |
1053 StartPlayout(); | 1015 StartPlayout(); |
1054 test_is_done_->Wait(std::max(kTestTimeOutInMilliseconds, | 1016 test_is_done_->Wait(std::max(kTestTimeOutInMilliseconds, |
1055 1000 * kMeasureLatencyTimeInSec)); | 1017 1000 * kMeasureLatencyTimeInSec)); |
1056 StopPlayout(); | 1018 StopPlayout(); |
1057 StopRecording(); | 1019 StopRecording(); |
1058 // Verify that the correct number of transmitted impulses are detected. | 1020 // Verify that the correct number of transmitted impulses are detected. |
1059 EXPECT_EQ(latency_audio_stream->num_latency_values(), | 1021 EXPECT_EQ(latency_audio_stream->num_latency_values(), |
1060 static_cast<size_t>( | 1022 static_cast<size_t>( |
1061 kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1)); | 1023 kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1)); |
1062 latency_audio_stream->PrintResults(); | 1024 latency_audio_stream->PrintResults(); |
1063 } | 1025 } |
1064 | 1026 |
1065 } // namespace webrtc | 1027 } // namespace webrtc |
OLD | NEW |