OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "media/base/audio_discard_helper.h" | 5 #include "media/base/audio_discard_helper.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 static scoped_refptr<DecoderBuffer> CreateEncodedBuffer( | 23 static scoped_refptr<DecoderBuffer> CreateEncodedBuffer( |
24 base::TimeDelta timestamp, | 24 base::TimeDelta timestamp, |
25 base::TimeDelta duration) { | 25 base::TimeDelta duration) { |
26 scoped_refptr<DecoderBuffer> result(new DecoderBuffer(1)); | 26 scoped_refptr<DecoderBuffer> result(new DecoderBuffer(1)); |
27 result->set_timestamp(timestamp); | 27 result->set_timestamp(timestamp); |
28 result->set_duration(duration); | 28 result->set_duration(duration); |
29 return result; | 29 return result; |
30 } | 30 } |
31 | 31 |
32 static scoped_refptr<AudioBuffer> CreateDecodedBuffer(int frames) { | 32 static scoped_refptr<AudioBuffer> CreateDecodedBuffer(int frames) { |
33 return MakeAudioBuffer(kSampleFormatPlanarF32, | 33 return MakeAudioBuffer(kSampleFormatPlanarF32, CHANNEL_LAYOUT_MONO, 1, |
34 CHANNEL_LAYOUT_MONO, | 34 kSampleRate, 0.0f, kDataStep, frames, kNoTimestamp); |
35 1, | |
36 kSampleRate, | |
37 0.0f, | |
38 kDataStep, | |
39 frames, | |
40 kNoTimestamp()); | |
41 } | 35 } |
42 | 36 |
43 static float ExtractDecodedData(const scoped_refptr<AudioBuffer>& buffer, | 37 static float ExtractDecodedData(const scoped_refptr<AudioBuffer>& buffer, |
44 int index) { | 38 int index) { |
45 // This is really inefficient, but we can't access the raw AudioBuffer if any | 39 // This is really inefficient, but we can't access the raw AudioBuffer if any |
46 // start trimming has been applied. | 40 // start trimming has been applied. |
47 std::unique_ptr<AudioBus> temp_bus = | 41 std::unique_ptr<AudioBus> temp_bus = |
48 AudioBus::Create(buffer->channel_count(), 1); | 42 AudioBus::Create(buffer->channel_count(), 1); |
49 buffer->ReadFrames(1, index, 0, temp_bus.get()); | 43 buffer->ReadFrames(1, index, 0, temp_bus.get()); |
50 return temp_bus->channel(0)[0]; | 44 return temp_bus->channel(0)[0]; |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 ASSERT_FALSE(discard_helper.initialized()); | 455 ASSERT_FALSE(discard_helper.initialized()); |
462 | 456 |
463 const base::TimeDelta kTimestamp = base::TimeDelta(); | 457 const base::TimeDelta kTimestamp = base::TimeDelta(); |
464 const base::TimeDelta kDuration = base::TimeDelta::FromMilliseconds(10); | 458 const base::TimeDelta kDuration = base::TimeDelta::FromMilliseconds(10); |
465 const int kTestFrames = discard_helper.TimeDeltaToFrames(kDuration); | 459 const int kTestFrames = discard_helper.TimeDeltaToFrames(kDuration); |
466 discard_helper.Reset(0); | 460 discard_helper.Reset(0); |
467 | 461 |
468 scoped_refptr<DecoderBuffer> encoded_buffer = | 462 scoped_refptr<DecoderBuffer> encoded_buffer = |
469 CreateEncodedBuffer(kTimestamp, kDuration); | 463 CreateEncodedBuffer(kTimestamp, kDuration); |
470 encoded_buffer->set_discard_padding( | 464 encoded_buffer->set_discard_padding( |
471 std::make_pair(kInfiniteDuration(), base::TimeDelta())); | 465 std::make_pair(kInfiniteDuration, base::TimeDelta())); |
472 scoped_refptr<AudioBuffer> decoded_buffer = CreateDecodedBuffer(kTestFrames); | 466 scoped_refptr<AudioBuffer> decoded_buffer = CreateDecodedBuffer(kTestFrames); |
473 | 467 |
474 // Verify all of the first buffer is discarded. | 468 // Verify all of the first buffer is discarded. |
475 ASSERT_FALSE(discard_helper.ProcessBuffers(encoded_buffer, decoded_buffer)); | 469 ASSERT_FALSE(discard_helper.ProcessBuffers(encoded_buffer, decoded_buffer)); |
476 ASSERT_TRUE(discard_helper.initialized()); | 470 ASSERT_TRUE(discard_helper.initialized()); |
477 encoded_buffer->set_timestamp(kTimestamp + kDuration); | 471 encoded_buffer->set_timestamp(kTimestamp + kDuration); |
478 encoded_buffer->set_discard_padding(DecoderBuffer::DiscardPadding()); | 472 encoded_buffer->set_discard_padding(DecoderBuffer::DiscardPadding()); |
479 | 473 |
480 // Verify a second buffer goes through untouched. | 474 // Verify a second buffer goes through untouched. |
481 decoded_buffer = CreateDecodedBuffer(kTestFrames / 2); | 475 decoded_buffer = CreateDecodedBuffer(kTestFrames / 2); |
482 ASSERT_TRUE(discard_helper.ProcessBuffers(encoded_buffer, decoded_buffer)); | 476 ASSERT_TRUE(discard_helper.ProcessBuffers(encoded_buffer, decoded_buffer)); |
483 EXPECT_EQ(kTimestamp, decoded_buffer->timestamp()); | 477 EXPECT_EQ(kTimestamp, decoded_buffer->timestamp()); |
484 EXPECT_EQ(kDuration / 2, decoded_buffer->duration()); | 478 EXPECT_EQ(kDuration / 2, decoded_buffer->duration()); |
485 EXPECT_EQ(kTestFrames / 2, decoded_buffer->frame_count()); | 479 EXPECT_EQ(kTestFrames / 2, decoded_buffer->frame_count()); |
486 ASSERT_FLOAT_EQ(0.0f, ExtractDecodedData(decoded_buffer, 0)); | 480 ASSERT_FLOAT_EQ(0.0f, ExtractDecodedData(decoded_buffer, 0)); |
487 } | 481 } |
488 | 482 |
489 TEST(AudioDiscardHelperTest, CompleteDiscardWithDelayedDiscard) { | 483 TEST(AudioDiscardHelperTest, CompleteDiscardWithDelayedDiscard) { |
490 AudioDiscardHelper discard_helper(kSampleRate, 0); | 484 AudioDiscardHelper discard_helper(kSampleRate, 0); |
491 ASSERT_FALSE(discard_helper.initialized()); | 485 ASSERT_FALSE(discard_helper.initialized()); |
492 | 486 |
493 const base::TimeDelta kTimestamp = base::TimeDelta(); | 487 const base::TimeDelta kTimestamp = base::TimeDelta(); |
494 const base::TimeDelta kDuration = base::TimeDelta::FromMilliseconds(10); | 488 const base::TimeDelta kDuration = base::TimeDelta::FromMilliseconds(10); |
495 const int kTestFrames = discard_helper.TimeDeltaToFrames(kDuration); | 489 const int kTestFrames = discard_helper.TimeDeltaToFrames(kDuration); |
496 discard_helper.Reset(0); | 490 discard_helper.Reset(0); |
497 | 491 |
498 scoped_refptr<DecoderBuffer> encoded_buffer = | 492 scoped_refptr<DecoderBuffer> encoded_buffer = |
499 CreateEncodedBuffer(kTimestamp, kDuration); | 493 CreateEncodedBuffer(kTimestamp, kDuration); |
500 encoded_buffer->set_discard_padding( | 494 encoded_buffer->set_discard_padding( |
501 std::make_pair(kInfiniteDuration(), base::TimeDelta())); | 495 std::make_pair(kInfiniteDuration, base::TimeDelta())); |
502 scoped_refptr<AudioBuffer> decoded_buffer = CreateDecodedBuffer(kTestFrames); | 496 scoped_refptr<AudioBuffer> decoded_buffer = CreateDecodedBuffer(kTestFrames); |
503 | 497 |
504 // Setup a delayed discard. | 498 // Setup a delayed discard. |
505 ASSERT_FALSE(discard_helper.ProcessBuffers(encoded_buffer, NULL)); | 499 ASSERT_FALSE(discard_helper.ProcessBuffers(encoded_buffer, NULL)); |
506 ASSERT_TRUE(discard_helper.initialized()); | 500 ASSERT_TRUE(discard_helper.initialized()); |
507 | 501 |
508 // Verify the first output buffer is dropped. | 502 // Verify the first output buffer is dropped. |
509 encoded_buffer->set_timestamp(kTimestamp + kDuration); | 503 encoded_buffer->set_timestamp(kTimestamp + kDuration); |
510 encoded_buffer->set_discard_padding(DecoderBuffer::DiscardPadding()); | 504 encoded_buffer->set_discard_padding(DecoderBuffer::DiscardPadding()); |
511 ASSERT_FALSE(discard_helper.ProcessBuffers(encoded_buffer, decoded_buffer)); | 505 ASSERT_FALSE(discard_helper.ProcessBuffers(encoded_buffer, decoded_buffer)); |
(...skipping 15 matching lines...) Expand all Loading... |
527 ASSERT_FALSE(discard_helper.initialized()); | 521 ASSERT_FALSE(discard_helper.initialized()); |
528 discard_helper.Reset(kDecoderDelay); | 522 discard_helper.Reset(kDecoderDelay); |
529 | 523 |
530 const base::TimeDelta kTimestamp = base::TimeDelta(); | 524 const base::TimeDelta kTimestamp = base::TimeDelta(); |
531 const base::TimeDelta kDuration = base::TimeDelta::FromMilliseconds(10); | 525 const base::TimeDelta kDuration = base::TimeDelta::FromMilliseconds(10); |
532 const int kTestFrames = discard_helper.TimeDeltaToFrames(kDuration); | 526 const int kTestFrames = discard_helper.TimeDeltaToFrames(kDuration); |
533 | 527 |
534 scoped_refptr<DecoderBuffer> encoded_buffer = | 528 scoped_refptr<DecoderBuffer> encoded_buffer = |
535 CreateEncodedBuffer(kTimestamp, kDuration); | 529 CreateEncodedBuffer(kTimestamp, kDuration); |
536 encoded_buffer->set_discard_padding( | 530 encoded_buffer->set_discard_padding( |
537 std::make_pair(kInfiniteDuration(), base::TimeDelta())); | 531 std::make_pair(kInfiniteDuration, base::TimeDelta())); |
538 scoped_refptr<AudioBuffer> decoded_buffer = CreateDecodedBuffer(kTestFrames); | 532 scoped_refptr<AudioBuffer> decoded_buffer = CreateDecodedBuffer(kTestFrames); |
539 | 533 |
540 // Verify all of the first buffer is discarded. | 534 // Verify all of the first buffer is discarded. |
541 ASSERT_FALSE(discard_helper.ProcessBuffers(encoded_buffer, decoded_buffer)); | 535 ASSERT_FALSE(discard_helper.ProcessBuffers(encoded_buffer, decoded_buffer)); |
542 ASSERT_TRUE(discard_helper.initialized()); | 536 ASSERT_TRUE(discard_helper.initialized()); |
543 encoded_buffer->set_timestamp(kTimestamp + kDuration); | 537 encoded_buffer->set_timestamp(kTimestamp + kDuration); |
544 encoded_buffer->set_discard_padding(DecoderBuffer::DiscardPadding()); | 538 encoded_buffer->set_discard_padding(DecoderBuffer::DiscardPadding()); |
545 | 539 |
546 // Verify 5ms off the front of the second buffer is discarded. | 540 // Verify 5ms off the front of the second buffer is discarded. |
547 decoded_buffer = CreateDecodedBuffer(kTestFrames * 2); | 541 decoded_buffer = CreateDecodedBuffer(kTestFrames * 2); |
548 ASSERT_TRUE(discard_helper.ProcessBuffers(encoded_buffer, decoded_buffer)); | 542 ASSERT_TRUE(discard_helper.ProcessBuffers(encoded_buffer, decoded_buffer)); |
549 EXPECT_EQ(kTimestamp, decoded_buffer->timestamp()); | 543 EXPECT_EQ(kTimestamp, decoded_buffer->timestamp()); |
550 EXPECT_EQ(kDuration * 2 - kDuration / 2, decoded_buffer->duration()); | 544 EXPECT_EQ(kDuration * 2 - kDuration / 2, decoded_buffer->duration()); |
551 EXPECT_EQ(kTestFrames * 2 - kDecoderDelay, decoded_buffer->frame_count()); | 545 EXPECT_EQ(kTestFrames * 2 - kDecoderDelay, decoded_buffer->frame_count()); |
552 ASSERT_FLOAT_EQ(kDecoderDelay * kDataStep, | 546 ASSERT_FLOAT_EQ(kDecoderDelay * kDataStep, |
553 ExtractDecodedData(decoded_buffer, 0)); | 547 ExtractDecodedData(decoded_buffer, 0)); |
554 } | 548 } |
555 | 549 |
556 } // namespace media | 550 } // namespace media |
OLD | NEW |