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 19 matching lines...) Expand all Loading... |
30 using ::testing::NotNull; | 30 using ::testing::NotNull; |
31 using ::testing::Return; | 31 using ::testing::Return; |
32 | 32 |
33 using base::win::ScopedCOMInitializer; | 33 using base::win::ScopedCOMInitializer; |
34 | 34 |
35 namespace media { | 35 namespace media { |
36 | 36 |
37 static const wchar_t kAudioFile1_16b_m_16K[] | 37 static const wchar_t kAudioFile1_16b_m_16K[] |
38 = L"media\\test\\data\\sweep02_16b_mono_16KHz.raw"; | 38 = L"media\\test\\data\\sweep02_16b_mono_16KHz.raw"; |
39 | 39 |
40 static int ClearData(AudioBus* audio_bus, int total_bytes_delay) { | 40 static int ClearData(AudioBus* audio_bus, AudioBuffersState buffers_state) { |
41 audio_bus->Zero(); | 41 audio_bus->Zero(); |
42 return audio_bus->frames(); | 42 return audio_bus->frames(); |
43 } | 43 } |
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 virtual int OnMoreData(AudioBus* audio_bus, |
55 int total_bytes_delay) { | 55 AudioBuffersState buffers_state) { |
56 ++callback_count_; | 56 ++callback_count_; |
57 // Touch the channel memory value to make sure memory is good. | 57 // Touch the channel memory value to make sure memory is good. |
58 audio_bus->Zero(); | 58 audio_bus->Zero(); |
59 return audio_bus->frames(); | 59 return audio_bus->frames(); |
60 } | 60 } |
61 // AudioSourceCallback::OnError implementation: | 61 // AudioSourceCallback::OnError implementation: |
62 virtual void OnError(AudioOutputStream* stream) { | 62 virtual void OnError(AudioOutputStream* stream) { |
63 ++had_error_; | 63 ++had_error_; |
64 } | 64 } |
65 // Returns how many times OnMoreData() has been called. | 65 // Returns how many times OnMoreData() has been called. |
(...skipping 16 matching lines...) Expand all Loading... |
82 | 82 |
83 const int kMaxNumBuffers = 3; | 83 const int kMaxNumBuffers = 3; |
84 // Specializes TestSourceBasic to simulate a source that blocks for some time | 84 // Specializes TestSourceBasic to simulate a source that blocks for some time |
85 // in the OnMoreData callback. | 85 // in the OnMoreData callback. |
86 class TestSourceLaggy : public TestSourceBasic { | 86 class TestSourceLaggy : public TestSourceBasic { |
87 public: | 87 public: |
88 TestSourceLaggy(int laggy_after_buffer, int lag_in_ms) | 88 TestSourceLaggy(int laggy_after_buffer, int lag_in_ms) |
89 : laggy_after_buffer_(laggy_after_buffer), lag_in_ms_(lag_in_ms) { | 89 : laggy_after_buffer_(laggy_after_buffer), lag_in_ms_(lag_in_ms) { |
90 } | 90 } |
91 virtual int OnMoreData(AudioBus* audio_bus, | 91 virtual int OnMoreData(AudioBus* audio_bus, |
92 int total_bytes_delay) { | 92 AudioBuffersState buffers_state) { |
93 // Call the base, which increments the callback_count_. | 93 // Call the base, which increments the callback_count_. |
94 TestSourceBasic::OnMoreData(audio_bus, total_bytes_delay); | 94 TestSourceBasic::OnMoreData(audio_bus, buffers_state); |
95 if (callback_count() > kMaxNumBuffers) { | 95 if (callback_count() > kMaxNumBuffers) { |
96 ::Sleep(lag_in_ms_); | 96 ::Sleep(lag_in_ms_); |
97 } | 97 } |
98 return audio_bus->frames(); | 98 return audio_bus->frames(); |
99 } | 99 } |
100 private: | 100 private: |
101 int laggy_after_buffer_; | 101 int laggy_after_buffer_; |
102 int lag_in_ms_; | 102 int lag_in_ms_; |
103 }; | 103 }; |
104 | 104 |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 uint32 bytes_100_ms = samples_100_ms * 2; | 513 uint32 bytes_100_ms = samples_100_ms * 2; |
514 | 514 |
515 // Audio output stream has either a double or triple buffer scheme. | 515 // Audio output stream has either a double or triple buffer scheme. |
516 // We expect the amount of pending bytes will reaching up to 2 times of | 516 // We expect the amount of pending bytes will reaching up to 2 times of |
517 // |bytes_100_ms| depending on number of buffers used. | 517 // |bytes_100_ms| depending on number of buffers used. |
518 // From that it would decrease as we are playing the data but not providing | 518 // From that it would decrease as we are playing the data but not providing |
519 // new one. And then we will try to provide zero data so the amount of | 519 // new one. And then we will try to provide zero data so the amount of |
520 // pending bytes will go down and eventually read zero. | 520 // pending bytes will go down and eventually read zero. |
521 InSequence s; | 521 InSequence s; |
522 | 522 |
523 EXPECT_CALL(source, OnMoreData(NotNull(), 0)) | 523 EXPECT_CALL(source, OnMoreData(NotNull(), |
| 524 Field(&AudioBuffersState::pending_bytes, 0))) |
524 .WillOnce(Invoke(ClearData)); | 525 .WillOnce(Invoke(ClearData)); |
525 | 526 |
526 // Note: If AudioManagerWin::NumberOfWaveOutBuffers() ever changes, or if this | 527 // Note: If AudioManagerWin::NumberOfWaveOutBuffers() ever changes, or if this |
527 // test is run on Vista, these expectations will fail. | 528 // test is run on Vista, these expectations will fail. |
528 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms)) | 529 EXPECT_CALL(source, OnMoreData(NotNull(), |
| 530 Field(&AudioBuffersState::pending_bytes, |
| 531 bytes_100_ms))) |
529 .WillOnce(Invoke(ClearData)); | 532 .WillOnce(Invoke(ClearData)); |
530 EXPECT_CALL(source, OnMoreData(NotNull(), 2 * bytes_100_ms)) | 533 EXPECT_CALL(source, OnMoreData(NotNull(), |
| 534 Field(&AudioBuffersState::pending_bytes, |
| 535 2 * bytes_100_ms))) |
531 .WillOnce(Invoke(ClearData)); | 536 .WillOnce(Invoke(ClearData)); |
532 EXPECT_CALL(source, OnMoreData(NotNull(), 2 * bytes_100_ms)) | 537 EXPECT_CALL(source, OnMoreData(NotNull(), |
| 538 Field(&AudioBuffersState::pending_bytes, |
| 539 2 * bytes_100_ms))) |
533 .Times(AnyNumber()) | 540 .Times(AnyNumber()) |
534 .WillRepeatedly(Return(0)); | 541 .WillRepeatedly(Return(0)); |
535 EXPECT_CALL(source, OnMoreData(NotNull(), bytes_100_ms)) | 542 EXPECT_CALL(source, OnMoreData(NotNull(), |
| 543 Field(&AudioBuffersState::pending_bytes, |
| 544 bytes_100_ms))) |
536 .Times(AnyNumber()) | 545 .Times(AnyNumber()) |
537 .WillRepeatedly(Return(0)); | 546 .WillRepeatedly(Return(0)); |
538 EXPECT_CALL(source, OnMoreData(NotNull(), 0)) | 547 EXPECT_CALL(source, OnMoreData(NotNull(), |
| 548 Field(&AudioBuffersState::pending_bytes, 0))) |
539 .Times(AnyNumber()) | 549 .Times(AnyNumber()) |
540 .WillRepeatedly(Return(0)); | 550 .WillRepeatedly(Return(0)); |
541 | 551 |
542 oas->Start(&source); | 552 oas->Start(&source); |
543 ::Sleep(500); | 553 ::Sleep(500); |
544 oas->Stop(); | 554 oas->Stop(); |
545 oas->Close(); | 555 oas->Close(); |
546 } | 556 } |
547 | 557 |
548 // Simple source that uses a SyncSocket to retrieve the audio data | 558 // Simple source that uses a SyncSocket to retrieve the audio data |
549 // from a potentially remote thread. | 559 // from a potentially remote thread. |
550 class SyncSocketSource : public AudioOutputStream::AudioSourceCallback { | 560 class SyncSocketSource : public AudioOutputStream::AudioSourceCallback { |
551 public: | 561 public: |
552 SyncSocketSource(base::SyncSocket* socket, const AudioParameters& params) | 562 SyncSocketSource(base::SyncSocket* socket, const AudioParameters& params) |
553 : socket_(socket) { | 563 : socket_(socket) { |
554 // Setup AudioBus wrapping data we'll receive over the sync socket. | 564 // Setup AudioBus wrapping data we'll receive over the sync socket. |
555 data_size_ = AudioBus::CalculateMemorySize(params); | 565 data_size_ = AudioBus::CalculateMemorySize(params); |
556 data_.reset(static_cast<float*>( | 566 data_.reset(static_cast<float*>( |
557 base::AlignedAlloc(data_size_, AudioBus::kChannelAlignment))); | 567 base::AlignedAlloc(data_size_, AudioBus::kChannelAlignment))); |
558 audio_bus_ = AudioBus::WrapMemory(params, data_.get()); | 568 audio_bus_ = AudioBus::WrapMemory(params, data_.get()); |
559 } | 569 } |
560 ~SyncSocketSource() {} | 570 ~SyncSocketSource() {} |
561 | 571 |
562 // AudioSourceCallback::OnMoreData implementation: | 572 // AudioSourceCallback::OnMoreData implementation: |
563 virtual int OnMoreData(AudioBus* audio_bus, | 573 virtual int OnMoreData(AudioBus* audio_bus, |
564 int total_bytes_delay) { | 574 AudioBuffersState buffers_state) { |
565 socket_->Send(&total_bytes_delay, sizeof(total_bytes_delay)); | 575 socket_->Send(&buffers_state, sizeof(buffers_state)); |
566 uint32 size = socket_->Receive(data_.get(), data_size_); | 576 uint32 size = socket_->Receive(data_.get(), data_size_); |
567 DCHECK_EQ(static_cast<size_t>(size) % sizeof(*audio_bus_->channel(0)), 0U); | 577 DCHECK_EQ(static_cast<size_t>(size) % sizeof(*audio_bus_->channel(0)), 0U); |
568 audio_bus_->CopyTo(audio_bus); | 578 audio_bus_->CopyTo(audio_bus); |
569 return audio_bus_->frames(); | 579 return audio_bus_->frames(); |
570 } | 580 } |
571 virtual int OnMoreIOData(AudioBus* source, | 581 virtual int OnMoreIOData(AudioBus* source, |
572 AudioBus* dest, | 582 AudioBus* dest, |
573 int total_bytes_delay) { | 583 AudioBuffersState buffers_state) { |
574 NOTREACHED(); | 584 NOTREACHED(); |
575 return 0; | 585 return 0; |
576 } | 586 } |
577 // AudioSourceCallback::OnError implementation: | 587 // AudioSourceCallback::OnError implementation: |
578 virtual void OnError(AudioOutputStream* stream) { | 588 virtual void OnError(AudioOutputStream* stream) { |
579 } | 589 } |
580 | 590 |
581 private: | 591 private: |
582 base::SyncSocket* socket_; | 592 base::SyncSocket* socket_; |
583 int data_size_; | 593 int data_size_; |
(...skipping 21 matching lines...) Expand all Loading... |
605 | 615 |
606 // Setup AudioBus wrapping data we'll pass over the sync socket. | 616 // Setup AudioBus wrapping data we'll pass over the sync socket. |
607 scoped_ptr<float, base::AlignedFreeDeleter> data(static_cast<float*>( | 617 scoped_ptr<float, base::AlignedFreeDeleter> data(static_cast<float*>( |
608 base::AlignedAlloc(ctx.packet_size_bytes, AudioBus::kChannelAlignment))); | 618 base::AlignedAlloc(ctx.packet_size_bytes, AudioBus::kChannelAlignment))); |
609 scoped_ptr<AudioBus> audio_bus = AudioBus::WrapMemory( | 619 scoped_ptr<AudioBus> audio_bus = AudioBus::WrapMemory( |
610 ctx.channels, ctx.frames, data.get()); | 620 ctx.channels, ctx.frames, data.get()); |
611 | 621 |
612 SineWaveAudioSource sine(1, ctx.sine_freq, ctx.sample_rate); | 622 SineWaveAudioSource sine(1, ctx.sine_freq, ctx.sample_rate); |
613 const int kTwoSecFrames = ctx.sample_rate * 2; | 623 const int kTwoSecFrames = ctx.sample_rate * 2; |
614 | 624 |
615 int total_bytes_delay = 0; | 625 AudioBuffersState buffers_state; |
616 int times = 0; | 626 int times = 0; |
617 for (int ix = 0; ix < kTwoSecFrames; ix += ctx.frames) { | 627 for (int ix = 0; ix < kTwoSecFrames; ix += ctx.frames) { |
618 if (ctx.socket->Receive(&total_bytes_delay, sizeof(total_bytes_delay)) == 0) | 628 if (ctx.socket->Receive(&buffers_state, sizeof(buffers_state)) == 0) |
619 break; | 629 break; |
620 if ((times > 0) && (total_bytes_delay < 1000)) __debugbreak(); | 630 if ((times > 0) && (buffers_state.pending_bytes < 1000)) __debugbreak(); |
621 sine.OnMoreData(audio_bus.get(), total_bytes_delay); | 631 sine.OnMoreData(audio_bus.get(), buffers_state); |
622 ctx.socket->Send(data.get(), ctx.packet_size_bytes); | 632 ctx.socket->Send(data.get(), ctx.packet_size_bytes); |
623 ++times; | 633 ++times; |
624 } | 634 } |
625 | 635 |
626 return 0; | 636 return 0; |
627 } | 637 } |
628 | 638 |
629 // Test the basic operation of AudioOutputStream used with a SyncSocket. | 639 // Test the basic operation of AudioOutputStream used with a SyncSocket. |
630 // The emphasis is to verify that it is possible to feed data to the audio | 640 // The emphasis is to verify that it is possible to feed data to the audio |
631 // layer using a source based on SyncSocket. In a real situation we would | 641 // layer using a source based on SyncSocket. In a real situation we would |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 oas->Start(&source); | 682 oas->Start(&source); |
673 | 683 |
674 ::WaitForSingleObject(thread, INFINITE); | 684 ::WaitForSingleObject(thread, INFINITE); |
675 ::CloseHandle(thread); | 685 ::CloseHandle(thread); |
676 | 686 |
677 oas->Stop(); | 687 oas->Stop(); |
678 oas->Close(); | 688 oas->Close(); |
679 } | 689 } |
680 | 690 |
681 } // namespace media | 691 } // namespace media |
OLD | NEW |