mpeg1_audio_stream_parser_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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
  3. // found in the LICENSE file.
  4. #include "media/formats/mpeg/mpeg1_audio_stream_parser.h"
  5. #include <stdint.h>
  6. #include <memory>
  7. #include "media/base/test_data_util.h"
  8. #include "media/formats/common/stream_parser_test_base.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace media {
  11. class MPEG1AudioStreamParserTest
  12. : public StreamParserTestBase, public testing::Test {
  13. public:
  14. MPEG1AudioStreamParserTest()
  15. : StreamParserTestBase(std::make_unique<MPEG1AudioStreamParser>()) {}
  16. };
  17. // Test parsing with small prime sized chunks to smoke out "power of
  18. // 2" field size assumptions.
  19. TEST_F(MPEG1AudioStreamParserTest, UnalignedAppend) {
  20. const std::string expected =
  21. "NewSegment"
  22. "{ 0K }"
  23. "{ 0K }"
  24. "{ 0K }"
  25. "{ 0K }"
  26. "{ 0K }"
  27. "{ 0K }"
  28. "{ 0K }"
  29. "EndOfSegment"
  30. "NewSegment"
  31. "{ 0K }"
  32. "{ 0K }"
  33. "{ 0K }"
  34. "EndOfSegment"
  35. "NewSegment"
  36. "{ 0K }"
  37. "{ 0K }"
  38. "EndOfSegment";
  39. EXPECT_EQ(expected, ParseFile("sfx.mp3", 17));
  40. EXPECT_GT(last_audio_config().codec_delay(), 0);
  41. }
  42. TEST_F(MPEG1AudioStreamParserTest, UnalignedAppendMP2) {
  43. const std::string expected =
  44. "NewSegment"
  45. "{ 0K }"
  46. "{ 0K }"
  47. "{ 0K }"
  48. "{ 0K }"
  49. "EndOfSegment"
  50. "NewSegment"
  51. "{ 0K }"
  52. "{ 0K }"
  53. "{ 0K }"
  54. "{ 0K }"
  55. "{ 0K }"
  56. "{ 0K }"
  57. "EndOfSegment";
  58. EXPECT_EQ(expected, ParseFile("sfx.mp2", 17));
  59. EXPECT_GT(last_audio_config().codec_delay(), 0);
  60. }
  61. // Test parsing with a larger piece size to verify that multiple buffers
  62. // are passed to |new_buffer_cb_|.
  63. TEST_F(MPEG1AudioStreamParserTest, UnalignedAppend512) {
  64. const std::string expected =
  65. "NewSegment"
  66. "{ 0K 26K 52K 78K }"
  67. "EndOfSegment"
  68. "NewSegment"
  69. "{ 0K 26K 52K }"
  70. "{ 0K 26K 52K 78K }"
  71. "{ 0K }"
  72. "EndOfSegment";
  73. EXPECT_EQ(expected, ParseFile("sfx.mp3", 512));
  74. EXPECT_GT(last_audio_config().codec_delay(), 0);
  75. }
  76. TEST_F(MPEG1AudioStreamParserTest, MetadataParsing) {
  77. scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile("sfx.mp3");
  78. const uint8_t* buffer_ptr = buffer->data();
  79. // The first 32 bytes of sfx.mp3 are an ID3 tag, so no segments should be
  80. // extracted after appending those bytes.
  81. const int kId3TagSize = 32;
  82. EXPECT_EQ("", ParseData(buffer_ptr, kId3TagSize));
  83. EXPECT_FALSE(last_audio_config().IsValidConfig());
  84. buffer_ptr += kId3TagSize;
  85. // The next 417 bytes are a Xing frame; with the identifier 21 bytes into
  86. // the frame. Appending less than 21 bytes, should result in no segments
  87. // nor an AudioDecoderConfig being created.
  88. const int kXingTagPosition = 21;
  89. EXPECT_EQ("", ParseData(buffer_ptr, kXingTagPosition));
  90. EXPECT_FALSE(last_audio_config().IsValidConfig());
  91. buffer_ptr += kXingTagPosition;
  92. // Appending the rests of the Xing frame should result in no segments, but
  93. // should generate a valid AudioDecoderConfig.
  94. const int kXingRemainingSize = 417 - kXingTagPosition;
  95. EXPECT_EQ("", ParseData(buffer_ptr, kXingRemainingSize));
  96. EXPECT_TRUE(last_audio_config().IsValidConfig());
  97. buffer_ptr += kXingRemainingSize;
  98. // Append the first real frame and ensure we get a segment.
  99. const int kFirstRealFrameSize = 182;
  100. EXPECT_EQ("NewSegment{ 0K }EndOfSegment",
  101. ParseData(buffer_ptr, kFirstRealFrameSize));
  102. EXPECT_TRUE(last_audio_config().IsValidConfig());
  103. }
  104. } // namespace media