Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: media/audio/alsa/alsa_output_unittest.cc

Issue 2101303004: Pass delay and timestamp to AudioSourceCallback::OnMoreData. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Fix CQ errors. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/memory/ptr_util.h"
8 #include "base/run_loop.h" 9 #include "base/run_loop.h"
9 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "base/test/simple_test_tick_clock.h"
10 #include "base/test/test_message_loop.h" 12 #include "base/test/test_message_loop.h"
11 #include "base/threading/thread_task_runner_handle.h" 13 #include "base/threading/thread_task_runner_handle.h"
14 #include "base/time/time.h"
12 #include "media/audio/alsa/alsa_output.h" 15 #include "media/audio/alsa/alsa_output.h"
13 #include "media/audio/alsa/alsa_wrapper.h" 16 #include "media/audio/alsa/alsa_wrapper.h"
14 #include "media/audio/alsa/audio_manager_alsa.h" 17 #include "media/audio/alsa/audio_manager_alsa.h"
15 #include "media/audio/fake_audio_log_factory.h" 18 #include "media/audio/fake_audio_log_factory.h"
16 #include "media/audio/mock_audio_source_callback.h" 19 #include "media/audio/mock_audio_source_callback.h"
17 #include "media/base/data_buffer.h" 20 #include "media/base/data_buffer.h"
18 #include "media/base/seekable_buffer.h" 21 #include "media/base/seekable_buffer.h"
19 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
21 24
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 char AlsaPcmOutputStreamTest::kSurround51[] = "surround51:CARD=foo,DEV=0"; 211 char AlsaPcmOutputStreamTest::kSurround51[] = "surround51:CARD=foo,DEV=0";
209 char AlsaPcmOutputStreamTest::kSurround70[] = "surround70:CARD=foo,DEV=0"; 212 char AlsaPcmOutputStreamTest::kSurround70[] = "surround70:CARD=foo,DEV=0";
210 char AlsaPcmOutputStreamTest::kSurround71[] = "surround71:CARD=foo,DEV=0"; 213 char AlsaPcmOutputStreamTest::kSurround71[] = "surround71:CARD=foo,DEV=0";
211 void* AlsaPcmOutputStreamTest::kFakeHints[] = { 214 void* AlsaPcmOutputStreamTest::kFakeHints[] = {
212 kSurround40, kSurround41, kSurround50, kSurround51, 215 kSurround40, kSurround41, kSurround50, kSurround51,
213 kSurround70, kSurround71, NULL }; 216 kSurround70, kSurround71, NULL };
214 char AlsaPcmOutputStreamTest::kGenericSurround50[] = "surround50"; 217 char AlsaPcmOutputStreamTest::kGenericSurround50[] = "surround50";
215 218
216 // Custom action to clear a memory buffer. 219 // Custom action to clear a memory buffer.
217 ACTION(ClearBuffer) { 220 ACTION(ClearBuffer) {
218 arg0->Zero(); 221 arg3->Zero();
219 } 222 }
220 223
221 TEST_F(AlsaPcmOutputStreamTest, ConstructedState) { 224 TEST_F(AlsaPcmOutputStreamTest, ConstructedState) {
222 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout); 225 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
223 EXPECT_EQ(AlsaPcmOutputStream::kCreated, test_stream->state()); 226 EXPECT_EQ(AlsaPcmOutputStream::kCreated, test_stream->state());
224 test_stream->Close(); 227 test_stream->Close();
225 228
226 // Should support mono. 229 // Should support mono.
227 test_stream = CreateStream(CHANNEL_LAYOUT_MONO); 230 test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
228 EXPECT_EQ(AlsaPcmOutputStream::kCreated, test_stream->state()); 231 EXPECT_EQ(AlsaPcmOutputStream::kCreated, test_stream->state());
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 EXPECT_CALL(mock_alsa_wrapper_, PcmSetParams(_, _, _, _, _, _, _)) 415 EXPECT_CALL(mock_alsa_wrapper_, PcmSetParams(_, _, _, _, _, _, _))
413 .WillOnce(Return(0)); 416 .WillOnce(Return(0));
414 EXPECT_CALL(mock_alsa_wrapper_, PcmGetParams(_, _, _)) 417 EXPECT_CALL(mock_alsa_wrapper_, PcmGetParams(_, _, _))
415 .WillOnce(DoAll(SetArgumentPointee<1>(kTestFramesPerPacket), 418 .WillOnce(DoAll(SetArgumentPointee<1>(kTestFramesPerPacket),
416 SetArgumentPointee<2>(kTestFramesPerPacket / 2), 419 SetArgumentPointee<2>(kTestFramesPerPacket / 2),
417 Return(0))); 420 Return(0)));
418 421
419 // Open the stream. 422 // Open the stream.
420 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout); 423 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
421 ASSERT_TRUE(test_stream->Open()); 424 ASSERT_TRUE(test_stream->Open());
425 base::SimpleTestTickClock* const tick_clock = new base::SimpleTestTickClock();
426 tick_clock->SetNowTicks(base::TimeTicks::Now());
427 test_stream->SetTickClockForTesting(base::WrapUnique(tick_clock));
422 428
423 // Expect Device setup. 429 // Expect Device setup.
424 EXPECT_CALL(mock_alsa_wrapper_, PcmDrop(kFakeHandle)) 430 EXPECT_CALL(mock_alsa_wrapper_, PcmDrop(kFakeHandle))
425 .WillOnce(Return(0)); 431 .WillOnce(Return(0));
426 EXPECT_CALL(mock_alsa_wrapper_, PcmPrepare(kFakeHandle)) 432 EXPECT_CALL(mock_alsa_wrapper_, PcmPrepare(kFakeHandle))
427 .WillOnce(Return(0)); 433 .WillOnce(Return(0));
428 434
429 // Expect the pre-roll. 435 // Expect the pre-roll.
430 MockAudioSourceCallback mock_callback; 436 MockAudioSourceCallback mock_callback;
431 EXPECT_CALL(mock_alsa_wrapper_, PcmState(kFakeHandle)) 437 EXPECT_CALL(mock_alsa_wrapper_, PcmState(kFakeHandle))
432 .WillRepeatedly(Return(SND_PCM_STATE_RUNNING)); 438 .WillRepeatedly(Return(SND_PCM_STATE_RUNNING));
433 EXPECT_CALL(mock_alsa_wrapper_, PcmDelay(kFakeHandle, _)) 439 EXPECT_CALL(mock_alsa_wrapper_, PcmDelay(kFakeHandle, _))
434 .WillRepeatedly(DoAll(SetArgumentPointee<1>(0), Return(0))); 440 .WillRepeatedly(DoAll(SetArgumentPointee<1>(0), Return(0)));
435 EXPECT_CALL(mock_callback, OnMoreData(_, _, 0)) 441 EXPECT_CALL(mock_callback,
442 OnMoreData(base::TimeDelta(), tick_clock->NowTicks(), 0, _))
436 .WillRepeatedly(DoAll(ClearBuffer(), Return(kTestFramesPerPacket))); 443 .WillRepeatedly(DoAll(ClearBuffer(), Return(kTestFramesPerPacket)));
437 EXPECT_CALL(mock_alsa_wrapper_, PcmWritei(kFakeHandle, _, _)) 444 EXPECT_CALL(mock_alsa_wrapper_, PcmWritei(kFakeHandle, _, _))
438 .WillRepeatedly(Return(kTestFramesPerPacket)); 445 .WillRepeatedly(Return(kTestFramesPerPacket));
439 446
440 // Expect scheduling. 447 // Expect scheduling.
441 EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(kFakeHandle)) 448 EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(kFakeHandle))
442 .Times(AtLeast(2)) 449 .Times(AtLeast(2))
443 .WillRepeatedly(Return(kTestFramesPerPacket)); 450 .WillRepeatedly(Return(kTestFramesPerPacket));
444 451
445 test_stream->Start(&mock_callback); 452 test_stream->Start(&mock_callback);
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 583
577 // No expectations set on the strict mock because nothing should be called. 584 // No expectations set on the strict mock because nothing should be called.
578 test_stream->stop_stream_ = true; 585 test_stream->stop_stream_ = true;
579 test_stream->WritePacket(); 586 test_stream->WritePacket();
580 EXPECT_EQ(0, test_stream->buffer_->forward_bytes()); 587 EXPECT_EQ(0, test_stream->buffer_->forward_bytes());
581 test_stream->Close(); 588 test_stream->Close();
582 } 589 }
583 590
584 TEST_F(AlsaPcmOutputStreamTest, BufferPacket) { 591 TEST_F(AlsaPcmOutputStreamTest, BufferPacket) {
585 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout); 592 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
593 base::SimpleTestTickClock* const tick_clock = new base::SimpleTestTickClock();
594 tick_clock->SetNowTicks(base::TimeTicks::Now());
595 test_stream->SetTickClockForTesting(base::WrapUnique(tick_clock));
586 InitBuffer(test_stream); 596 InitBuffer(test_stream);
587 test_stream->buffer_->Clear(); 597 test_stream->buffer_->Clear();
588 598
589 MockAudioSourceCallback mock_callback; 599 MockAudioSourceCallback mock_callback;
590 EXPECT_CALL(mock_alsa_wrapper_, PcmState(_)) 600 EXPECT_CALL(mock_alsa_wrapper_, PcmState(_))
591 .WillOnce(Return(SND_PCM_STATE_RUNNING)); 601 .WillOnce(Return(SND_PCM_STATE_RUNNING));
592 EXPECT_CALL(mock_alsa_wrapper_, PcmDelay(_, _)) 602 EXPECT_CALL(mock_alsa_wrapper_, PcmDelay(_, _))
593 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(0))); 603 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(0)));
594 EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(_)) 604 EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(_))
595 .WillRepeatedly(Return(0)); // Buffer is full. 605 .WillRepeatedly(Return(0)); // Buffer is full.
596 606
597 // Return a partially filled packet. 607 // Return a partially filled packet.
598 EXPECT_CALL(mock_callback, OnMoreData(_, _, 0)) 608 EXPECT_CALL(mock_callback,
609 OnMoreData(base::TimeDelta(), tick_clock->NowTicks(), 0, _))
599 .WillOnce(DoAll(ClearBuffer(), Return(kTestFramesPerPacket / 2))); 610 .WillOnce(DoAll(ClearBuffer(), Return(kTestFramesPerPacket / 2)));
600 611
601 bool source_exhausted; 612 bool source_exhausted;
602 test_stream->set_source_callback(&mock_callback); 613 test_stream->set_source_callback(&mock_callback);
603 test_stream->packet_size_ = kTestPacketSize; 614 test_stream->packet_size_ = kTestPacketSize;
604 test_stream->BufferPacket(&source_exhausted); 615 test_stream->BufferPacket(&source_exhausted);
605 616
606 EXPECT_EQ(kTestPacketSize / 2, test_stream->buffer_->forward_bytes()); 617 EXPECT_EQ(kTestPacketSize / 2, test_stream->buffer_->forward_bytes());
607 EXPECT_FALSE(source_exhausted); 618 EXPECT_FALSE(source_exhausted);
608 test_stream->Close(); 619 test_stream->Close();
609 } 620 }
610 621
611 TEST_F(AlsaPcmOutputStreamTest, BufferPacket_Negative) { 622 TEST_F(AlsaPcmOutputStreamTest, BufferPacket_Negative) {
612 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout); 623 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
624 base::SimpleTestTickClock* const tick_clock = new base::SimpleTestTickClock();
625 tick_clock->SetNowTicks(base::TimeTicks::Now());
626 test_stream->SetTickClockForTesting(base::WrapUnique(tick_clock));
613 InitBuffer(test_stream); 627 InitBuffer(test_stream);
614 test_stream->buffer_->Clear(); 628 test_stream->buffer_->Clear();
615 629
616 // Simulate where the underrun has occurred right after checking the delay. 630 // Simulate where the underrun has occurred right after checking the delay.
617 MockAudioSourceCallback mock_callback; 631 MockAudioSourceCallback mock_callback;
618 EXPECT_CALL(mock_alsa_wrapper_, PcmState(_)) 632 EXPECT_CALL(mock_alsa_wrapper_, PcmState(_))
619 .WillOnce(Return(SND_PCM_STATE_RUNNING)); 633 .WillOnce(Return(SND_PCM_STATE_RUNNING));
620 EXPECT_CALL(mock_alsa_wrapper_, PcmDelay(_, _)) 634 EXPECT_CALL(mock_alsa_wrapper_, PcmDelay(_, _))
621 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(0))); 635 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(0)));
622 EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(_)) 636 EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(_))
623 .WillRepeatedly(Return(0)); // Buffer is full. 637 .WillRepeatedly(Return(0)); // Buffer is full.
624 EXPECT_CALL(mock_callback, OnMoreData(_, _, 0)) 638 EXPECT_CALL(mock_callback,
639 OnMoreData(base::TimeDelta(), tick_clock->NowTicks(), 0, _))
625 .WillOnce(DoAll(ClearBuffer(), Return(kTestFramesPerPacket / 2))); 640 .WillOnce(DoAll(ClearBuffer(), Return(kTestFramesPerPacket / 2)));
626 641
627 bool source_exhausted; 642 bool source_exhausted;
628 test_stream->set_source_callback(&mock_callback); 643 test_stream->set_source_callback(&mock_callback);
629 test_stream->packet_size_ = kTestPacketSize; 644 test_stream->packet_size_ = kTestPacketSize;
630 test_stream->BufferPacket(&source_exhausted); 645 test_stream->BufferPacket(&source_exhausted);
631 646
632 EXPECT_EQ(kTestPacketSize / 2, test_stream->buffer_->forward_bytes()); 647 EXPECT_EQ(kTestPacketSize / 2, test_stream->buffer_->forward_bytes());
633 EXPECT_FALSE(source_exhausted); 648 EXPECT_FALSE(source_exhausted);
634 test_stream->Close(); 649 test_stream->Close();
635 } 650 }
636 651
637 TEST_F(AlsaPcmOutputStreamTest, BufferPacket_Underrun) { 652 TEST_F(AlsaPcmOutputStreamTest, BufferPacket_Underrun) {
638 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout); 653 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
654 base::SimpleTestTickClock* const tick_clock = new base::SimpleTestTickClock();
655 tick_clock->SetNowTicks(base::TimeTicks::Now());
656 test_stream->SetTickClockForTesting(base::WrapUnique(tick_clock));
639 InitBuffer(test_stream); 657 InitBuffer(test_stream);
640 test_stream->buffer_->Clear(); 658 test_stream->buffer_->Clear();
641 659
642 // If ALSA has underrun then we should assume a delay of zero. 660 // If ALSA has underrun then we should assume a delay of zero.
643 MockAudioSourceCallback mock_callback; 661 MockAudioSourceCallback mock_callback;
644 EXPECT_CALL(mock_alsa_wrapper_, PcmState(_)) 662 EXPECT_CALL(mock_alsa_wrapper_, PcmState(_))
645 .WillOnce(Return(SND_PCM_STATE_XRUN)); 663 .WillOnce(Return(SND_PCM_STATE_XRUN));
646 EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(_)) 664 EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(_))
647 .WillRepeatedly(Return(0)); // Buffer is full. 665 .WillRepeatedly(Return(0)); // Buffer is full.
648 EXPECT_CALL(mock_callback, OnMoreData(_, 0, 0)) 666 EXPECT_CALL(mock_callback,
667 OnMoreData(base::TimeDelta(), tick_clock->NowTicks(), 0, _))
649 .WillOnce(DoAll(ClearBuffer(), Return(kTestFramesPerPacket / 2))); 668 .WillOnce(DoAll(ClearBuffer(), Return(kTestFramesPerPacket / 2)));
650 669
651 bool source_exhausted; 670 bool source_exhausted;
652 test_stream->set_source_callback(&mock_callback); 671 test_stream->set_source_callback(&mock_callback);
653 test_stream->packet_size_ = kTestPacketSize; 672 test_stream->packet_size_ = kTestPacketSize;
654 test_stream->BufferPacket(&source_exhausted); 673 test_stream->BufferPacket(&source_exhausted);
655 674
656 EXPECT_EQ(kTestPacketSize / 2, test_stream->buffer_->forward_bytes()); 675 EXPECT_EQ(kTestPacketSize / 2, test_stream->buffer_->forward_bytes());
657 EXPECT_FALSE(source_exhausted); 676 EXPECT_FALSE(source_exhausted);
658 test_stream->Close(); 677 test_stream->Close();
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 877
859 // TODO(ajwong): Find a way to test whether or not another task has been 878 // TODO(ajwong): Find a way to test whether or not another task has been
860 // posted so we can verify that the Alsa code will indeed break the task 879 // posted so we can verify that the Alsa code will indeed break the task
861 // posting loop. 880 // posting loop.
862 881
863 test_stream->TransitionTo(AlsaPcmOutputStream::kIsClosed); 882 test_stream->TransitionTo(AlsaPcmOutputStream::kIsClosed);
864 test_stream->Close(); 883 test_stream->Close();
865 } 884 }
866 885
867 } // namespace media 886 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698