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