| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "modules/webaudio/AudioContext.h" | 5 #include "modules/webaudio/AudioContext.h" |
| 6 | 6 |
| 7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
| 8 #include "core/testing/DummyPageHolder.h" | 8 #include "core/testing/DummyPageHolder.h" |
| 9 #include "platform/testing/TestingPlatformSupport.h" | 9 #include "platform/testing/TestingPlatformSupport.h" |
| 10 #include "platform/wtf/PtrUtil.h" | |
| 11 #include "public/platform/WebAudioDevice.h" | 10 #include "public/platform/WebAudioDevice.h" |
| 12 #include "public/platform/WebAudioLatencyHint.h" | 11 #include "public/platform/WebAudioLatencyHint.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 13 |
| 15 namespace blink { | 14 namespace blink { |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 class MockWebAudioDevice : public WebAudioDevice { | 18 class MockWebAudioDevice : public WebAudioDevice { |
| 20 public: | 19 public: |
| 21 explicit MockWebAudioDevice(double sample_rate, int frames_per_buffer) | 20 explicit MockWebAudioDevice(double sample_rate, int frames_per_buffer) |
| 22 : sample_rate_(sample_rate), frames_per_buffer_(frames_per_buffer) {} | 21 : sample_rate_(sample_rate), frames_per_buffer_(frames_per_buffer) {} |
| 23 ~MockWebAudioDevice() override = default; | 22 ~MockWebAudioDevice() override = default; |
| 24 | 23 |
| 25 void Start() override {} | 24 void Start() override {} |
| 26 void Stop() override {} | 25 void Stop() override {} |
| 27 double SampleRate() override { return sample_rate_; } | 26 double SampleRate() override { return sample_rate_; } |
| 28 int FramesPerBuffer() override { return frames_per_buffer_; } | 27 int FramesPerBuffer() override { return frames_per_buffer_; } |
| 29 | 28 |
| 30 private: | 29 private: |
| 31 double sample_rate_; | 30 double sample_rate_; |
| 32 int frames_per_buffer_; | 31 int frames_per_buffer_; |
| 33 }; | 32 }; |
| 34 | 33 |
| 35 class AudioContextTestPlatform : public TestingPlatformSupport { | 34 class AudioContextTestPlatform : public TestingPlatformSupport { |
| 36 public: | 35 public: |
| 37 std::unique_ptr<WebAudioDevice> CreateAudioDevice( | 36 WebAudioDevice* CreateAudioDevice(unsigned number_of_input_channels, |
| 38 unsigned number_of_input_channels, | 37 unsigned number_of_channels, |
| 39 unsigned number_of_channels, | 38 const WebAudioLatencyHint& latency_hint, |
| 40 const WebAudioLatencyHint& latency_hint, | 39 WebAudioDevice::RenderCallback*, |
| 41 WebAudioDevice::RenderCallback*, | 40 const WebString& device_id, |
| 42 const WebString& device_id, | 41 const WebSecurityOrigin&) override { |
| 43 const WebSecurityOrigin&) override { | |
| 44 double buffer_size = 0; | 42 double buffer_size = 0; |
| 45 const double interactive_size = AudioHardwareBufferSize(); | 43 const double interactive_size = AudioHardwareBufferSize(); |
| 46 const double balanced_size = AudioHardwareBufferSize() * 2; | 44 const double balanced_size = AudioHardwareBufferSize() * 2; |
| 47 const double playback_size = AudioHardwareBufferSize() * 4; | 45 const double playback_size = AudioHardwareBufferSize() * 4; |
| 48 switch (latency_hint.Category()) { | 46 switch (latency_hint.Category()) { |
| 49 case WebAudioLatencyHint::kCategoryInteractive: | 47 case WebAudioLatencyHint::kCategoryInteractive: |
| 50 buffer_size = interactive_size; | 48 buffer_size = interactive_size; |
| 51 break; | 49 break; |
| 52 case WebAudioLatencyHint::kCategoryBalanced: | 50 case WebAudioLatencyHint::kCategoryBalanced: |
| 53 buffer_size = balanced_size; | 51 buffer_size = balanced_size; |
| 54 break; | 52 break; |
| 55 case WebAudioLatencyHint::kCategoryPlayback: | 53 case WebAudioLatencyHint::kCategoryPlayback: |
| 56 buffer_size = playback_size; | 54 buffer_size = playback_size; |
| 57 break; | 55 break; |
| 58 case WebAudioLatencyHint::kCategoryExact: | 56 case WebAudioLatencyHint::kCategoryExact: |
| 59 buffer_size = | 57 buffer_size = |
| 60 clampTo(latency_hint.Seconds() * AudioHardwareSampleRate(), | 58 clampTo(latency_hint.Seconds() * AudioHardwareSampleRate(), |
| 61 static_cast<double>(AudioHardwareBufferSize()), | 59 static_cast<double>(AudioHardwareBufferSize()), |
| 62 static_cast<double>(playback_size)); | 60 static_cast<double>(playback_size)); |
| 63 break; | 61 break; |
| 64 default: | 62 default: |
| 65 NOTREACHED(); | 63 NOTREACHED(); |
| 66 break; | 64 break; |
| 67 } | 65 } |
| 68 | 66 |
| 69 return WTF::MakeUnique<MockWebAudioDevice>(AudioHardwareSampleRate(), | 67 return new MockWebAudioDevice(AudioHardwareSampleRate(), buffer_size); |
| 70 buffer_size); | |
| 71 } | 68 } |
| 72 | 69 |
| 73 double AudioHardwareSampleRate() override { return 44100; } | 70 double AudioHardwareSampleRate() override { return 44100; } |
| 74 size_t AudioHardwareBufferSize() override { return 128; } | 71 size_t AudioHardwareBufferSize() override { return 128; } |
| 75 }; | 72 }; |
| 76 | 73 |
| 77 } // anonymous namespace | 74 } // anonymous namespace |
| 78 | 75 |
| 79 class AudioContextTest : public ::testing::Test { | 76 class AudioContextTest : public ::testing::Test { |
| 80 protected: | 77 protected: |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 exact_too_big_options.setLatencyHint( | 133 exact_too_big_options.setLatencyHint( |
| 137 AudioContextLatencyCategoryOrDouble::fromDouble( | 134 AudioContextLatencyCategoryOrDouble::fromDouble( |
| 138 playback_context->baseLatency() * 2)); | 135 playback_context->baseLatency() * 2)); |
| 139 AudioContext* exact_too_big_context = AudioContext::Create( | 136 AudioContext* exact_too_big_context = AudioContext::Create( |
| 140 GetDocument(), exact_too_big_options, ASSERT_NO_EXCEPTION); | 137 GetDocument(), exact_too_big_options, ASSERT_NO_EXCEPTION); |
| 141 EXPECT_EQ(exact_too_big_context->baseLatency(), | 138 EXPECT_EQ(exact_too_big_context->baseLatency(), |
| 142 playback_context->baseLatency()); | 139 playback_context->baseLatency()); |
| 143 } | 140 } |
| 144 | 141 |
| 145 } // namespace blink | 142 } // namespace blink |
| OLD | NEW |