| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/environment.h" | 5 #include "base/environment.h" |
| 6 #include "base/test/test_timeouts.h" | 6 #include "base/test/test_timeouts.h" |
| 7 #include "content/renderer/media/audio_hardware.h" | 7 #include "content/renderer/media/audio_hardware.h" |
| 8 #include "content/renderer/media/webrtc_audio_capturer.h" | 8 #include "content/renderer/media/webrtc_audio_capturer.h" |
| 9 #include "content/renderer/media/webrtc_audio_device_impl.h" | 9 #include "content/renderer/media/webrtc_audio_device_impl.h" |
| 10 #include "content/renderer/media/webrtc_audio_renderer.h" | 10 #include "content/renderer/media/webrtc_audio_renderer.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 using testing::InvokeWithoutArgs; | 23 using testing::InvokeWithoutArgs; |
| 24 using testing::Return; | 24 using testing::Return; |
| 25 using testing::StrEq; | 25 using testing::StrEq; |
| 26 | 26 |
| 27 namespace content { | 27 namespace content { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 const int kRenderViewId = 1; | 31 const int kRenderViewId = 1; |
| 32 | 32 |
| 33 class AudioUtil : public AudioUtilInterface { | 33 class RealAudioHardwareConfig : public media::AudioHardwareConfig { |
| 34 public: | 34 public: |
| 35 AudioUtil() {} | 35 RealAudioHardwareConfig() {} |
| 36 virtual ~RealAudioHardwareConfig() {} |
| 36 | 37 |
| 37 virtual int GetAudioHardwareSampleRate() OVERRIDE { | 38 virtual int GetOutputBufferSize() OVERRIDE { |
| 39 return media::GetAudioHardwareBufferSize(); |
| 40 } |
| 41 |
| 42 virtual int GetOutputSampleRate() OVERRIDE { |
| 38 return media::GetAudioHardwareSampleRate(); | 43 return media::GetAudioHardwareSampleRate(); |
| 39 } | 44 } |
| 40 virtual int GetAudioInputHardwareSampleRate( | 45 |
| 41 const std::string& device_id) OVERRIDE { | 46 virtual int GetInputSampleRate() OVERRIDE { |
| 42 return media::GetAudioInputHardwareSampleRate(device_id); | 47 return media::GetAudioInputHardwareSampleRate( |
| 48 media::AudioManagerBase::kDefaultDeviceId); |
| 43 } | 49 } |
| 44 virtual media::ChannelLayout GetAudioInputHardwareChannelLayout( | 50 |
| 45 const std::string& device_id) OVERRIDE { | 51 virtual media::ChannelLayout GetInputChannelLayout() OVERRIDE { |
| 46 return media::GetAudioInputHardwareChannelLayout(device_id); | 52 return media::GetAudioInputHardwareChannelLayout( |
| 53 media::AudioManagerBase::kDefaultDeviceId); |
| 47 } | 54 } |
| 55 |
| 48 private: | 56 private: |
| 49 DISALLOW_COPY_AND_ASSIGN(AudioUtil); | 57 DISALLOW_COPY_AND_ASSIGN(RealAudioHardwareConfig); |
| 50 }; | 58 }; |
| 51 | 59 |
| 52 class AudioUtilNoHardware : public AudioUtilInterface { | 60 class FakeAudioHardwareConfig : public media::AudioHardwareConfig { |
| 53 public: | 61 public: |
| 54 AudioUtilNoHardware(int output_rate, int input_rate, | 62 FakeAudioHardwareConfig(int output_size, int output_rate, int input_rate, |
| 55 media::ChannelLayout input_channel_layout) | 63 media::ChannelLayout input_channel_layout) |
| 56 : output_rate_(output_rate), | 64 : output_size_(output_size), |
| 65 output_rate_(output_rate), |
| 57 input_rate_(input_rate), | 66 input_rate_(input_rate), |
| 58 input_channel_layout_(input_channel_layout) { | 67 input_channel_layout_(input_channel_layout) { |
| 59 } | 68 } |
| 69 virtual ~FakeAudioHardwareConfig() {} |
| 60 | 70 |
| 61 virtual int GetAudioHardwareSampleRate() OVERRIDE { | 71 virtual int GetOutputBufferSize() OVERRIDE { |
| 72 return output_size_; |
| 73 } |
| 74 |
| 75 virtual int GetOutputSampleRate() OVERRIDE { |
| 62 return output_rate_; | 76 return output_rate_; |
| 63 } | 77 } |
| 64 virtual int GetAudioInputHardwareSampleRate( | 78 |
| 65 const std::string& device_id) OVERRIDE { | 79 virtual int GetInputSampleRate() OVERRIDE { |
| 66 return input_rate_; | 80 return input_rate_; |
| 67 } | 81 } |
| 68 virtual media::ChannelLayout GetAudioInputHardwareChannelLayout( | 82 |
| 69 const std::string& device_id) OVERRIDE { | 83 virtual media::ChannelLayout GetInputChannelLayout() OVERRIDE { |
| 70 return input_channel_layout_; | 84 return input_channel_layout_; |
| 71 } | 85 } |
| 72 | 86 |
| 73 private: | 87 private: |
| 88 int output_size_; |
| 74 int output_rate_; | 89 int output_rate_; |
| 75 int input_rate_; | 90 int input_rate_; |
| 76 media::ChannelLayout input_channel_layout_; | 91 media::ChannelLayout input_channel_layout_; |
| 77 DISALLOW_COPY_AND_ASSIGN(AudioUtilNoHardware); | 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(FakeAudioHardwareConfig); |
| 78 }; | 94 }; |
| 79 | 95 |
| 80 // Return true if at least one element in the array matches |value|. | 96 // Return true if at least one element in the array matches |value|. |
| 81 bool FindElementInArray(int* array, int size, int value) { | 97 bool FindElementInArray(int* array, int size, int value) { |
| 82 return (std::find(&array[0], &array[0] + size, value) != &array[size]); | 98 return (std::find(&array[0], &array[0] + size, value) != &array[size]); |
| 83 } | 99 } |
| 84 | 100 |
| 85 // This method returns false if a non-supported rate is detected on the | 101 // This method returns false if a non-supported rate is detected on the |
| 86 // input or output side. | 102 // input or output side. |
| 87 // TODO(henrika): add support for automatic fallback to Windows Wave audio | 103 // TODO(henrika): add support for automatic fallback to Windows Wave audio |
| 88 // if a non-supported rate is detected. It is probably better to detect | 104 // if a non-supported rate is detected. It is probably better to detect |
| 89 // invalid audio settings by actually trying to open the audio streams instead | 105 // invalid audio settings by actually trying to open the audio streams instead |
| 90 // of relying on hard coded conditions. | 106 // of relying on hard coded conditions. |
| 91 bool HardwareSampleRatesAreValid() { | 107 bool HardwareSampleRatesAreValid() { |
| 92 // These are the currently supported hardware sample rates in both directions. | 108 // These are the currently supported hardware sample rates in both directions. |
| 93 // The actual WebRTC client can limit these ranges further depending on | 109 // The actual WebRTC client can limit these ranges further depending on |
| 94 // platform but this is the maximum range we support today. | 110 // platform but this is the maximum range we support today. |
| 95 int valid_input_rates[] = {16000, 32000, 44100, 48000, 96000}; | 111 int valid_input_rates[] = {16000, 32000, 44100, 48000, 96000}; |
| 96 int valid_output_rates[] = {44100, 48000, 96000}; | 112 int valid_output_rates[] = {44100, 48000, 96000}; |
| 97 | 113 |
| 114 RendererAudioHardwareConfig* hardware_config = |
| 115 RenderThreadImpl::current()->GetAudioHardwareConfig(); |
| 116 |
| 98 // Verify the input sample rate. | 117 // Verify the input sample rate. |
| 99 int input_sample_rate = GetAudioInputSampleRate(); | 118 int input_sample_rate = hardware_config->GetInputSampleRate(); |
| 100 | 119 |
| 101 if (!FindElementInArray(valid_input_rates, arraysize(valid_input_rates), | 120 if (!FindElementInArray(valid_input_rates, arraysize(valid_input_rates), |
| 102 input_sample_rate)) { | 121 input_sample_rate)) { |
| 103 LOG(WARNING) << "Non-supported input sample rate detected."; | 122 LOG(WARNING) << "Non-supported input sample rate detected."; |
| 104 return false; | 123 return false; |
| 105 } | 124 } |
| 106 | 125 |
| 107 // Given that the input rate was OK, verify the output rate as well. | 126 // Given that the input rate was OK, verify the output rate as well. |
| 108 int output_sample_rate = GetAudioOutputSampleRate(); | 127 int output_sample_rate = hardware_config->GetOutputSampleRate(); |
| 109 if (!FindElementInArray(valid_output_rates, arraysize(valid_output_rates), | 128 if (!FindElementInArray(valid_output_rates, arraysize(valid_output_rates), |
| 110 output_sample_rate)) { | 129 output_sample_rate)) { |
| 111 LOG(WARNING) << "Non-supported output sample rate detected."; | 130 LOG(WARNING) << "Non-supported output sample rate detected."; |
| 112 return false; | 131 return false; |
| 113 } | 132 } |
| 114 | 133 |
| 115 return true; | 134 return true; |
| 116 } | 135 } |
| 117 | 136 |
| 118 // Utility method which initializes the audio capturer contained in the | 137 // Utility method which initializes the audio capturer contained in the |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 int invalid_rates[] = {-1, 0, 8000, 11025, 22050, 32000, 192000}; | 258 int invalid_rates[] = {-1, 0, 8000, 11025, 22050, 32000, 192000}; |
| 240 for (size_t i = 0; i < arraysize(invalid_rates); ++i) { | 259 for (size_t i = 0; i < arraysize(invalid_rates); ++i) { |
| 241 EXPECT_FALSE(FindElementInArray(valid_rates, arraysize(valid_rates), | 260 EXPECT_FALSE(FindElementInArray(valid_rates, arraysize(valid_rates), |
| 242 invalid_rates[i])); | 261 invalid_rates[i])); |
| 243 } | 262 } |
| 244 } | 263 } |
| 245 | 264 |
| 246 // Basic test that instantiates and initializes an instance of | 265 // Basic test that instantiates and initializes an instance of |
| 247 // WebRtcAudioDeviceImpl. | 266 // WebRtcAudioDeviceImpl. |
| 248 TEST_F(WebRTCAudioDeviceTest, Construct) { | 267 TEST_F(WebRTCAudioDeviceTest, Construct) { |
| 249 AudioUtilNoHardware audio_util(48000, 48000, media::CHANNEL_LAYOUT_MONO); | 268 FakeAudioHardwareConfig audio_config( |
| 250 SetAudioUtilCallback(&audio_util); | 269 480, 48000, 48000, media::CHANNEL_LAYOUT_MONO); |
| 270 SetAudioHardwareConfig(&audio_config); |
| 251 | 271 |
| 252 scoped_refptr<WebRtcAudioDeviceImpl> webrtc_audio_device( | 272 scoped_refptr<WebRtcAudioDeviceImpl> webrtc_audio_device( |
| 253 new WebRtcAudioDeviceImpl()); | 273 new WebRtcAudioDeviceImpl()); |
| 254 | 274 |
| 255 // The capturer is not created until after the WebRtcAudioDeviceImpl has | 275 // The capturer is not created until after the WebRtcAudioDeviceImpl has |
| 256 // been initialized. | 276 // been initialized. |
| 257 EXPECT_FALSE(InitializeCapturer(webrtc_audio_device.get())); | 277 EXPECT_FALSE(InitializeCapturer(webrtc_audio_device.get())); |
| 258 | 278 |
| 259 WebRTCAutoDelete<webrtc::VoiceEngine> engine(webrtc::VoiceEngine::Create()); | 279 WebRTCAutoDelete<webrtc::VoiceEngine> engine(webrtc::VoiceEngine::Create()); |
| 260 ASSERT_TRUE(engine.valid()); | 280 ASSERT_TRUE(engine.valid()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 272 // webrtc::VoEExternalMedia implementation to hijack the output audio and | 292 // webrtc::VoEExternalMedia implementation to hijack the output audio and |
| 273 // verify that streaming starts correctly. | 293 // verify that streaming starts correctly. |
| 274 // Disabled when running headless since the bots don't have the required config. | 294 // Disabled when running headless since the bots don't have the required config. |
| 275 // Flaky, http://crbug.com/167299 . | 295 // Flaky, http://crbug.com/167299 . |
| 276 TEST_F(WebRTCAudioDeviceTest, DISABLED_StartPlayout) { | 296 TEST_F(WebRTCAudioDeviceTest, DISABLED_StartPlayout) { |
| 277 if (!has_output_devices_) { | 297 if (!has_output_devices_) { |
| 278 LOG(WARNING) << "No output device detected."; | 298 LOG(WARNING) << "No output device detected."; |
| 279 return; | 299 return; |
| 280 } | 300 } |
| 281 | 301 |
| 282 AudioUtil audio_util; | 302 RealAudioHardwareConfig audio_config; |
| 283 SetAudioUtilCallback(&audio_util); | 303 SetAudioHardwareConfig(&audio_config); |
| 284 | 304 |
| 285 if (!HardwareSampleRatesAreValid()) | 305 if (!HardwareSampleRatesAreValid()) |
| 286 return; | 306 return; |
| 287 | 307 |
| 288 EXPECT_CALL(media_observer(), | 308 EXPECT_CALL(media_observer(), |
| 289 OnSetAudioStreamStatus(_, 1, StrEq("created"))).Times(1); | 309 OnSetAudioStreamStatus(_, 1, StrEq("created"))).Times(1); |
| 290 EXPECT_CALL(media_observer(), | 310 EXPECT_CALL(media_observer(), |
| 291 OnSetAudioStreamPlaying(_, 1, true)).Times(1); | 311 OnSetAudioStreamPlaying(_, 1, true)).Times(1); |
| 292 EXPECT_CALL(media_observer(), | 312 EXPECT_CALL(media_observer(), |
| 293 OnSetAudioStreamStatus(_, 1, StrEq("closed"))).Times(1); | 313 OnSetAudioStreamStatus(_, 1, StrEq("closed"))).Times(1); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 // is also required to ensure that "sending" can start without actually trying | 370 // is also required to ensure that "sending" can start without actually trying |
| 351 // to send encoded packets to the network. Our main interest here is to ensure | 371 // to send encoded packets to the network. Our main interest here is to ensure |
| 352 // that the audio capturing starts as it should. | 372 // that the audio capturing starts as it should. |
| 353 // Disabled when running headless since the bots don't have the required config. | 373 // Disabled when running headless since the bots don't have the required config. |
| 354 TEST_F(WebRTCAudioDeviceTest, StartRecording) { | 374 TEST_F(WebRTCAudioDeviceTest, StartRecording) { |
| 355 if (!has_input_devices_ || !has_output_devices_) { | 375 if (!has_input_devices_ || !has_output_devices_) { |
| 356 LOG(WARNING) << "Missing audio devices."; | 376 LOG(WARNING) << "Missing audio devices."; |
| 357 return; | 377 return; |
| 358 } | 378 } |
| 359 | 379 |
| 360 AudioUtil audio_util; | 380 RealAudioHardwareConfig audio_config; |
| 361 SetAudioUtilCallback(&audio_util); | 381 SetAudioHardwareConfig(&audio_config); |
| 362 | 382 |
| 363 if (!HardwareSampleRatesAreValid()) | 383 if (!HardwareSampleRatesAreValid()) |
| 364 return; | 384 return; |
| 365 | 385 |
| 366 // TODO(tommi): extend MediaObserver and MockMediaObserver with support | 386 // TODO(tommi): extend MediaObserver and MockMediaObserver with support |
| 367 // for new interfaces, like OnSetAudioStreamRecording(). When done, add | 387 // for new interfaces, like OnSetAudioStreamRecording(). When done, add |
| 368 // EXPECT_CALL() macros here. | 388 // EXPECT_CALL() macros here. |
| 369 scoped_refptr<WebRtcAudioDeviceImpl> webrtc_audio_device( | 389 scoped_refptr<WebRtcAudioDeviceImpl> webrtc_audio_device( |
| 370 new WebRtcAudioDeviceImpl()); | 390 new WebRtcAudioDeviceImpl()); |
| 371 | 391 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 // Flaky, http://crbug.com/167298 . | 443 // Flaky, http://crbug.com/167298 . |
| 424 TEST_F(WebRTCAudioDeviceTest, DISABLED_PlayLocalFile) { | 444 TEST_F(WebRTCAudioDeviceTest, DISABLED_PlayLocalFile) { |
| 425 if (!has_output_devices_) { | 445 if (!has_output_devices_) { |
| 426 LOG(WARNING) << "No output device detected."; | 446 LOG(WARNING) << "No output device detected."; |
| 427 return; | 447 return; |
| 428 } | 448 } |
| 429 | 449 |
| 430 std::string file_path( | 450 std::string file_path( |
| 431 GetTestDataPath(FILE_PATH_LITERAL("speechmusic_mono_16kHz.pcm"))); | 451 GetTestDataPath(FILE_PATH_LITERAL("speechmusic_mono_16kHz.pcm"))); |
| 432 | 452 |
| 433 AudioUtil audio_util; | 453 RealAudioHardwareConfig audio_config; |
| 434 SetAudioUtilCallback(&audio_util); | 454 SetAudioHardwareConfig(&audio_config); |
| 435 | 455 |
| 436 if (!HardwareSampleRatesAreValid()) | 456 if (!HardwareSampleRatesAreValid()) |
| 437 return; | 457 return; |
| 438 | 458 |
| 439 EXPECT_CALL(media_observer(), | 459 EXPECT_CALL(media_observer(), |
| 440 OnSetAudioStreamStatus(_, 1, StrEq("created"))).Times(1); | 460 OnSetAudioStreamStatus(_, 1, StrEq("created"))).Times(1); |
| 441 EXPECT_CALL(media_observer(), | 461 EXPECT_CALL(media_observer(), |
| 442 OnSetAudioStreamPlaying(_, 1, true)).Times(1); | 462 OnSetAudioStreamPlaying(_, 1, true)).Times(1); |
| 443 EXPECT_CALL(media_observer(), | 463 EXPECT_CALL(media_observer(), |
| 444 OnSetAudioStreamStatus(_, 1, StrEq("closed"))).Times(1); | 464 OnSetAudioStreamStatus(_, 1, StrEq("closed"))).Times(1); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 // where they are decoded and played out on the default audio output device. | 514 // where they are decoded and played out on the default audio output device. |
| 495 // Disabled when running headless since the bots don't have the required config. | 515 // Disabled when running headless since the bots don't have the required config. |
| 496 // TODO(henrika): improve quality by using a wideband codec, enabling noise- | 516 // TODO(henrika): improve quality by using a wideband codec, enabling noise- |
| 497 // suppressions etc. | 517 // suppressions etc. |
| 498 TEST_F(WebRTCAudioDeviceTest, FullDuplexAudioWithAGC) { | 518 TEST_F(WebRTCAudioDeviceTest, FullDuplexAudioWithAGC) { |
| 499 if (!has_output_devices_ || !has_input_devices_) { | 519 if (!has_output_devices_ || !has_input_devices_) { |
| 500 LOG(WARNING) << "Missing audio devices."; | 520 LOG(WARNING) << "Missing audio devices."; |
| 501 return; | 521 return; |
| 502 } | 522 } |
| 503 | 523 |
| 504 AudioUtil audio_util; | 524 RealAudioHardwareConfig audio_config; |
| 505 SetAudioUtilCallback(&audio_util); | 525 SetAudioHardwareConfig(&audio_config); |
| 506 | 526 |
| 507 if (!HardwareSampleRatesAreValid()) | 527 if (!HardwareSampleRatesAreValid()) |
| 508 return; | 528 return; |
| 509 | 529 |
| 510 EXPECT_CALL(media_observer(), | 530 EXPECT_CALL(media_observer(), |
| 511 OnSetAudioStreamStatus(_, 1, StrEq("created"))); | 531 OnSetAudioStreamStatus(_, 1, StrEq("created"))); |
| 512 EXPECT_CALL(media_observer(), | 532 EXPECT_CALL(media_observer(), |
| 513 OnSetAudioStreamPlaying(_, 1, true)); | 533 OnSetAudioStreamPlaying(_, 1, true)); |
| 514 EXPECT_CALL(media_observer(), | 534 EXPECT_CALL(media_observer(), |
| 515 OnSetAudioStreamStatus(_, 1, StrEq("closed"))); | 535 OnSetAudioStreamStatus(_, 1, StrEq("closed"))); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 | 580 |
| 561 renderer->Stop(); | 581 renderer->Stop(); |
| 562 EXPECT_EQ(0, base->StopSend(ch)); | 582 EXPECT_EQ(0, base->StopSend(ch)); |
| 563 EXPECT_EQ(0, base->StopPlayout(ch)); | 583 EXPECT_EQ(0, base->StopPlayout(ch)); |
| 564 | 584 |
| 565 EXPECT_EQ(0, base->DeleteChannel(ch)); | 585 EXPECT_EQ(0, base->DeleteChannel(ch)); |
| 566 EXPECT_EQ(0, base->Terminate()); | 586 EXPECT_EQ(0, base->Terminate()); |
| 567 } | 587 } |
| 568 | 588 |
| 569 } // namespace content | 589 } // namespace content |
| OLD | NEW |