| 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 <windows.h> | 5 #include <windows.h> |
| 6 #include <mmsystem.h> | 6 #include <mmsystem.h> |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 using ::testing::Invoke; | 31 using ::testing::Invoke; |
| 32 using ::testing::InSequence; | 32 using ::testing::InSequence; |
| 33 using ::testing::NiceMock; | 33 using ::testing::NiceMock; |
| 34 using ::testing::NotNull; | 34 using ::testing::NotNull; |
| 35 using ::testing::Return; | 35 using ::testing::Return; |
| 36 | 36 |
| 37 namespace media { | 37 namespace media { |
| 38 | 38 |
| 39 static int ClearData(AudioBus* audio_bus, | 39 static int ClearData(AudioBus* audio_bus, |
| 40 uint32_t total_bytes_delay, | 40 uint32_t total_bytes_delay, |
| 41 uint32_t frames_skipped) { | 41 uint32_t frames_skipped, |
| 42 const AudioTimestamp& timestamp) { |
| 42 audio_bus->Zero(); | 43 audio_bus->Zero(); |
| 43 return audio_bus->frames(); | 44 return audio_bus->frames(); |
| 44 } | 45 } |
| 45 | 46 |
| 46 // This class allows to find out if the callbacks are occurring as | 47 // This class allows to find out if the callbacks are occurring as |
| 47 // expected and if any error has been reported. | 48 // expected and if any error has been reported. |
| 48 class TestSourceBasic : public AudioOutputStream::AudioSourceCallback { | 49 class TestSourceBasic : public AudioOutputStream::AudioSourceCallback { |
| 49 public: | 50 public: |
| 50 TestSourceBasic() | 51 TestSourceBasic() |
| 51 : callback_count_(0), | 52 : callback_count_(0), |
| 52 had_error_(0) { | 53 had_error_(0) { |
| 53 } | 54 } |
| 54 // AudioSourceCallback::OnMoreData implementation: | 55 // AudioSourceCallback::OnMoreData implementation: |
| 55 int OnMoreData(AudioBus* audio_bus, | 56 int OnMoreData(AudioBus* audio_bus, |
| 56 uint32_t total_bytes_delay, | 57 uint32_t total_bytes_delay, |
| 57 uint32_t frames_skipped) override { | 58 uint32_t frames_skipped, |
| 59 const AudioTimestamp& timestamp) override { |
| 58 ++callback_count_; | 60 ++callback_count_; |
| 59 // Touch the channel memory value to make sure memory is good. | 61 // Touch the channel memory value to make sure memory is good. |
| 60 audio_bus->Zero(); | 62 audio_bus->Zero(); |
| 61 return audio_bus->frames(); | 63 return audio_bus->frames(); |
| 62 } | 64 } |
| 63 // AudioSourceCallback::OnError implementation: | 65 // AudioSourceCallback::OnError implementation: |
| 64 void OnError(AudioOutputStream* stream) override { ++had_error_; } | 66 void OnError(AudioOutputStream* stream) override { ++had_error_; } |
| 65 // Returns how many times OnMoreData() has been called. | 67 // Returns how many times OnMoreData() has been called. |
| 66 int callback_count() const { | 68 int callback_count() const { |
| 67 return callback_count_; | 69 return callback_count_; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 83 const int kMaxNumBuffers = 3; | 85 const int kMaxNumBuffers = 3; |
| 84 // Specializes TestSourceBasic to simulate a source that blocks for some time | 86 // Specializes TestSourceBasic to simulate a source that blocks for some time |
| 85 // in the OnMoreData callback. | 87 // in the OnMoreData callback. |
| 86 class TestSourceLaggy : public TestSourceBasic { | 88 class TestSourceLaggy : public TestSourceBasic { |
| 87 public: | 89 public: |
| 88 explicit TestSourceLaggy(int lag_in_ms) | 90 explicit TestSourceLaggy(int lag_in_ms) |
| 89 : lag_in_ms_(lag_in_ms) { | 91 : lag_in_ms_(lag_in_ms) { |
| 90 } | 92 } |
| 91 int OnMoreData(AudioBus* audio_bus, | 93 int OnMoreData(AudioBus* audio_bus, |
| 92 uint32_t total_bytes_delay, | 94 uint32_t total_bytes_delay, |
| 93 uint32_t frames_skipped) override { | 95 uint32_t frames_skipped, |
| 96 const AudioTimestamp& timestamp) override { |
| 94 // Call the base, which increments the callback_count_. | 97 // Call the base, which increments the callback_count_. |
| 95 TestSourceBasic::OnMoreData(audio_bus, total_bytes_delay, frames_skipped); | 98 TestSourceBasic::OnMoreData(audio_bus, total_bytes_delay, frames_skipped, |
| 99 timestamp); |
| 96 if (callback_count() > kMaxNumBuffers) { | 100 if (callback_count() > kMaxNumBuffers) { |
| 97 ::Sleep(lag_in_ms_); | 101 ::Sleep(lag_in_ms_); |
| 98 } | 102 } |
| 99 return audio_bus->frames(); | 103 return audio_bus->frames(); |
| 100 } | 104 } |
| 101 private: | 105 private: |
| 102 int lag_in_ms_; | 106 int lag_in_ms_; |
| 103 }; | 107 }; |
| 104 | 108 |
| 105 // Helper class to memory map an entire file. The mapping is read-only. Don't | 109 // Helper class to memory map an entire file. The mapping is read-only. Don't |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 uint32_t bytes_100_ms = samples_100_ms * 2; | 493 uint32_t bytes_100_ms = samples_100_ms * 2; |
| 490 | 494 |
| 491 // Audio output stream has either a double or triple buffer scheme. | 495 // Audio output stream has either a double or triple buffer scheme. |
| 492 // We expect the amount of pending bytes will reaching up to 2 times of | 496 // We expect the amount of pending bytes will reaching up to 2 times of |
| 493 // |bytes_100_ms| depending on number of buffers used. | 497 // |bytes_100_ms| depending on number of buffers used. |
| 494 // From that it would decrease as we are playing the data but not providing | 498 // From that it would decrease as we are playing the data but not providing |
| 495 // new one. And then we will try to provide zero data so the amount of | 499 // new one. And then we will try to provide zero data so the amount of |
| 496 // pending bytes will go down and eventually read zero. | 500 // pending bytes will go down and eventually read zero. |
| 497 InSequence s; | 501 InSequence s; |
| 498 | 502 |
| 499 EXPECT_CALL(source, OnMoreData(NotNull(), 0, 0)).WillOnce(Invoke(ClearData)); | 503 EXPECT_CALL(source, OnMoreData(NotNull(), 0, 0, AudioTimestamp())) |
| 504 .WillOnce(Invoke(ClearData)); |
| 500 | 505 |
| 501 // Note: If AudioManagerWin::NumberOfWaveOutBuffers() ever changes, or if this | 506 // Note: If AudioManagerWin::NumberOfWaveOutBuffers() ever changes, or if this |
| 502 // test is run on Vista, these expectations will fail. | 507 // test is run on Vista, these expectations will fail. |
| 503 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms, 0)) | 508 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms, 0, AudioTimestamp())) |
| 504 .WillOnce(Invoke(ClearData)); | 509 .WillOnce(Invoke(ClearData)); |
| 505 EXPECT_CALL(source, OnMoreData(NotNull(), 2 * bytes_100_ms, 0)) | 510 EXPECT_CALL(source, |
| 511 OnMoreData(NotNull(), 2 * bytes_100_ms, 0, AudioTimestamp())) |
| 506 .WillOnce(Invoke(ClearData)); | 512 .WillOnce(Invoke(ClearData)); |
| 507 EXPECT_CALL(source, OnMoreData(NotNull(), 2 * bytes_100_ms, 0)) | 513 EXPECT_CALL(source, |
| 514 OnMoreData(NotNull(), 2 * bytes_100_ms, 0, AudioTimestamp())) |
| 508 .Times(AnyNumber()) | 515 .Times(AnyNumber()) |
| 509 .WillRepeatedly(Return(0)); | 516 .WillRepeatedly(Return(0)); |
| 510 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms, 0)) | 517 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms, 0, AudioTimestamp())) |
| 511 .Times(AnyNumber()) | 518 .Times(AnyNumber()) |
| 512 .WillRepeatedly(Return(0)); | 519 .WillRepeatedly(Return(0)); |
| 513 EXPECT_CALL(source, OnMoreData(NotNull(), 0, 0)) | 520 EXPECT_CALL(source, OnMoreData(NotNull(), 0, 0, AudioTimestamp())) |
| 514 .Times(AnyNumber()) | 521 .Times(AnyNumber()) |
| 515 .WillRepeatedly(Return(0)); | 522 .WillRepeatedly(Return(0)); |
| 516 | 523 |
| 517 oas->Start(&source); | 524 oas->Start(&source); |
| 518 ::Sleep(500); | 525 ::Sleep(500); |
| 519 oas->Stop(); | 526 oas->Stop(); |
| 520 oas->Close(); | 527 oas->Close(); |
| 521 } | 528 } |
| 522 | 529 |
| 523 // Simple source that uses a SyncSocket to retrieve the audio data | 530 // Simple source that uses a SyncSocket to retrieve the audio data |
| 524 // from a potentially remote thread. | 531 // from a potentially remote thread. |
| 525 class SyncSocketSource : public AudioOutputStream::AudioSourceCallback { | 532 class SyncSocketSource : public AudioOutputStream::AudioSourceCallback { |
| 526 public: | 533 public: |
| 527 SyncSocketSource(base::SyncSocket* socket, const AudioParameters& params) | 534 SyncSocketSource(base::SyncSocket* socket, const AudioParameters& params) |
| 528 : socket_(socket) { | 535 : socket_(socket) { |
| 529 // Setup AudioBus wrapping data we'll receive over the sync socket. | 536 // Setup AudioBus wrapping data we'll receive over the sync socket. |
| 530 data_size_ = AudioBus::CalculateMemorySize(params); | 537 data_size_ = AudioBus::CalculateMemorySize(params); |
| 531 data_.reset(static_cast<float*>( | 538 data_.reset(static_cast<float*>( |
| 532 base::AlignedAlloc(data_size_, AudioBus::kChannelAlignment))); | 539 base::AlignedAlloc(data_size_, AudioBus::kChannelAlignment))); |
| 533 audio_bus_ = AudioBus::WrapMemory(params, data_.get()); | 540 audio_bus_ = AudioBus::WrapMemory(params, data_.get()); |
| 534 } | 541 } |
| 535 ~SyncSocketSource() override {} | 542 ~SyncSocketSource() override {} |
| 536 | 543 |
| 537 // AudioSourceCallback::OnMoreData implementation: | 544 // AudioSourceCallback::OnMoreData implementation: |
| 538 int OnMoreData(AudioBus* audio_bus, | 545 int OnMoreData(AudioBus* audio_bus, |
| 539 uint32_t total_bytes_delay, | 546 uint32_t total_bytes_delay, |
| 540 uint32_t frames_skipped) override { | 547 uint32_t frames_skipped, |
| 548 const AudioTimestamp& timestamp) override { |
| 541 socket_->Send(&total_bytes_delay, sizeof(total_bytes_delay)); | 549 socket_->Send(&total_bytes_delay, sizeof(total_bytes_delay)); |
| 542 uint32_t size = socket_->Receive(data_.get(), data_size_); | 550 uint32_t size = socket_->Receive(data_.get(), data_size_); |
| 543 DCHECK_EQ(static_cast<size_t>(size) % sizeof(*audio_bus_->channel(0)), 0U); | 551 DCHECK_EQ(static_cast<size_t>(size) % sizeof(*audio_bus_->channel(0)), 0U); |
| 544 audio_bus_->CopyTo(audio_bus); | 552 audio_bus_->CopyTo(audio_bus); |
| 545 return audio_bus_->frames(); | 553 return audio_bus_->frames(); |
| 546 } | 554 } |
| 547 | 555 |
| 548 // AudioSourceCallback::OnError implementation: | 556 // AudioSourceCallback::OnError implementation: |
| 549 void OnError(AudioOutputStream* stream) override {} | 557 void OnError(AudioOutputStream* stream) override {} |
| 550 | 558 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 637 oas->Start(&source); | 645 oas->Start(&source); |
| 638 | 646 |
| 639 ::WaitForSingleObject(thread, INFINITE); | 647 ::WaitForSingleObject(thread, INFINITE); |
| 640 ::CloseHandle(thread); | 648 ::CloseHandle(thread); |
| 641 | 649 |
| 642 oas->Stop(); | 650 oas->Stop(); |
| 643 oas->Close(); | 651 oas->Close(); |
| 644 } | 652 } |
| 645 | 653 |
| 646 } // namespace media | 654 } // namespace media |
| OLD | NEW |