| 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 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
| 10 #include "base/memory/aligned_memory.h" | 10 #include "base/memory/aligned_memory.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 | 44 |
| 45 // This class allows to find out if the callbacks are occurring as | 45 // This class allows to find out if the callbacks are occurring as |
| 46 // expected and if any error has been reported. | 46 // expected and if any error has been reported. |
| 47 class TestSourceBasic : public AudioOutputStream::AudioSourceCallback { | 47 class TestSourceBasic : public AudioOutputStream::AudioSourceCallback { |
| 48 public: | 48 public: |
| 49 explicit TestSourceBasic() | 49 explicit TestSourceBasic() |
| 50 : callback_count_(0), | 50 : callback_count_(0), |
| 51 had_error_(0) { | 51 had_error_(0) { |
| 52 } | 52 } |
| 53 // AudioSourceCallback::OnMoreData implementation: | 53 // AudioSourceCallback::OnMoreData implementation: |
| 54 virtual int OnMoreData(AudioBus* audio_bus, | 54 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override { |
| 55 uint32 total_bytes_delay) { | |
| 56 ++callback_count_; | 55 ++callback_count_; |
| 57 // Touch the channel memory value to make sure memory is good. | 56 // Touch the channel memory value to make sure memory is good. |
| 58 audio_bus->Zero(); | 57 audio_bus->Zero(); |
| 59 return audio_bus->frames(); | 58 return audio_bus->frames(); |
| 60 } | 59 } |
| 61 // AudioSourceCallback::OnError implementation: | 60 // AudioSourceCallback::OnError implementation: |
| 62 virtual void OnError(AudioOutputStream* stream) { | 61 void OnError(AudioOutputStream* stream) override { ++had_error_; } |
| 63 ++had_error_; | |
| 64 } | |
| 65 // Returns how many times OnMoreData() has been called. | 62 // Returns how many times OnMoreData() has been called. |
| 66 int callback_count() const { | 63 int callback_count() const { |
| 67 return callback_count_; | 64 return callback_count_; |
| 68 } | 65 } |
| 69 // Returns how many times the OnError callback was called. | 66 // Returns how many times the OnError callback was called. |
| 70 int had_error() const { | 67 int had_error() const { |
| 71 return had_error_; | 68 return had_error_; |
| 72 } | 69 } |
| 73 | 70 |
| 74 void set_error(bool error) { | 71 void set_error(bool error) { |
| 75 had_error_ += error ? 1 : 0; | 72 had_error_ += error ? 1 : 0; |
| 76 } | 73 } |
| 77 | 74 |
| 78 private: | 75 private: |
| 79 int callback_count_; | 76 int callback_count_; |
| 80 int had_error_; | 77 int had_error_; |
| 81 }; | 78 }; |
| 82 | 79 |
| 83 const int kMaxNumBuffers = 3; | 80 const int kMaxNumBuffers = 3; |
| 84 // Specializes TestSourceBasic to simulate a source that blocks for some time | 81 // Specializes TestSourceBasic to simulate a source that blocks for some time |
| 85 // in the OnMoreData callback. | 82 // in the OnMoreData callback. |
| 86 class TestSourceLaggy : public TestSourceBasic { | 83 class TestSourceLaggy : public TestSourceBasic { |
| 87 public: | 84 public: |
| 88 TestSourceLaggy(int laggy_after_buffer, int lag_in_ms) | 85 TestSourceLaggy(int laggy_after_buffer, int lag_in_ms) |
| 89 : laggy_after_buffer_(laggy_after_buffer), lag_in_ms_(lag_in_ms) { | 86 : laggy_after_buffer_(laggy_after_buffer), lag_in_ms_(lag_in_ms) { |
| 90 } | 87 } |
| 91 virtual int OnMoreData(AudioBus* audio_bus, | 88 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override { |
| 92 uint32 total_bytes_delay) { | |
| 93 // Call the base, which increments the callback_count_. | 89 // Call the base, which increments the callback_count_. |
| 94 TestSourceBasic::OnMoreData(audio_bus, total_bytes_delay); | 90 TestSourceBasic::OnMoreData(audio_bus, total_bytes_delay); |
| 95 if (callback_count() > kMaxNumBuffers) { | 91 if (callback_count() > kMaxNumBuffers) { |
| 96 ::Sleep(lag_in_ms_); | 92 ::Sleep(lag_in_ms_); |
| 97 } | 93 } |
| 98 return audio_bus->frames(); | 94 return audio_bus->frames(); |
| 99 } | 95 } |
| 100 private: | 96 private: |
| 101 int laggy_after_buffer_; | 97 int laggy_after_buffer_; |
| 102 int lag_in_ms_; | 98 int lag_in_ms_; |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 class SyncSocketSource : public AudioOutputStream::AudioSourceCallback { | 513 class SyncSocketSource : public AudioOutputStream::AudioSourceCallback { |
| 518 public: | 514 public: |
| 519 SyncSocketSource(base::SyncSocket* socket, const AudioParameters& params) | 515 SyncSocketSource(base::SyncSocket* socket, const AudioParameters& params) |
| 520 : socket_(socket) { | 516 : socket_(socket) { |
| 521 // Setup AudioBus wrapping data we'll receive over the sync socket. | 517 // Setup AudioBus wrapping data we'll receive over the sync socket. |
| 522 data_size_ = AudioBus::CalculateMemorySize(params); | 518 data_size_ = AudioBus::CalculateMemorySize(params); |
| 523 data_.reset(static_cast<float*>( | 519 data_.reset(static_cast<float*>( |
| 524 base::AlignedAlloc(data_size_, AudioBus::kChannelAlignment))); | 520 base::AlignedAlloc(data_size_, AudioBus::kChannelAlignment))); |
| 525 audio_bus_ = AudioBus::WrapMemory(params, data_.get()); | 521 audio_bus_ = AudioBus::WrapMemory(params, data_.get()); |
| 526 } | 522 } |
| 527 ~SyncSocketSource() {} | 523 ~SyncSocketSource() override {} |
| 528 | 524 |
| 529 // AudioSourceCallback::OnMoreData implementation: | 525 // AudioSourceCallback::OnMoreData implementation: |
| 530 virtual int OnMoreData(AudioBus* audio_bus, | 526 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override { |
| 531 uint32 total_bytes_delay) { | |
| 532 socket_->Send(&total_bytes_delay, sizeof(total_bytes_delay)); | 527 socket_->Send(&total_bytes_delay, sizeof(total_bytes_delay)); |
| 533 uint32 size = socket_->Receive(data_.get(), data_size_); | 528 uint32 size = socket_->Receive(data_.get(), data_size_); |
| 534 DCHECK_EQ(static_cast<size_t>(size) % sizeof(*audio_bus_->channel(0)), 0U); | 529 DCHECK_EQ(static_cast<size_t>(size) % sizeof(*audio_bus_->channel(0)), 0U); |
| 535 audio_bus_->CopyTo(audio_bus); | 530 audio_bus_->CopyTo(audio_bus); |
| 536 return audio_bus_->frames(); | 531 return audio_bus_->frames(); |
| 537 } | 532 } |
| 538 | 533 |
| 539 // AudioSourceCallback::OnError implementation: | 534 // AudioSourceCallback::OnError implementation: |
| 540 virtual void OnError(AudioOutputStream* stream) { | 535 void OnError(AudioOutputStream* stream) override {} |
| 541 } | |
| 542 | 536 |
| 543 private: | 537 private: |
| 544 base::SyncSocket* socket_; | 538 base::SyncSocket* socket_; |
| 545 int data_size_; | 539 int data_size_; |
| 546 scoped_ptr<float, base::AlignedFreeDeleter> data_; | 540 scoped_ptr<float, base::AlignedFreeDeleter> data_; |
| 547 scoped_ptr<AudioBus> audio_bus_; | 541 scoped_ptr<AudioBus> audio_bus_; |
| 548 }; | 542 }; |
| 549 | 543 |
| 550 struct SyncThreadContext { | 544 struct SyncThreadContext { |
| 551 base::SyncSocket* socket; | 545 base::SyncSocket* socket; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 oas->Start(&source); | 625 oas->Start(&source); |
| 632 | 626 |
| 633 ::WaitForSingleObject(thread, INFINITE); | 627 ::WaitForSingleObject(thread, INFINITE); |
| 634 ::CloseHandle(thread); | 628 ::CloseHandle(thread); |
| 635 | 629 |
| 636 oas->Stop(); | 630 oas->Stop(); |
| 637 oas->Close(); | 631 oas->Close(); |
| 638 } | 632 } |
| 639 | 633 |
| 640 } // namespace media | 634 } // namespace media |
| OLD | NEW |