audio_file_reader_unittest.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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
  3. // found in the LICENSE file.
  4. #include "media/filters/audio_file_reader.h"
  5. #include <memory>
  6. #include "base/hash/md5.h"
  7. #include "base/time/time.h"
  8. #include "build/build_config.h"
  9. #include "media/base/audio_bus.h"
  10. #include "media/base/audio_hash.h"
  11. #include "media/base/decoder_buffer.h"
  12. #include "media/base/test_data_util.h"
  13. #include "media/ffmpeg/ffmpeg_common.h"
  14. #include "media/filters/in_memory_url_protocol.h"
  15. #include "media/media_buildflags.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. namespace media {
  18. class AudioFileReaderTest : public testing::Test {
  19. public:
  20. AudioFileReaderTest() : packet_verification_disabled_(false) {}
  21. AudioFileReaderTest(const AudioFileReaderTest&) = delete;
  22. AudioFileReaderTest& operator=(const AudioFileReaderTest&) = delete;
  23. ~AudioFileReaderTest() override = default;
  24. void Initialize(const char* filename) {
  25. data_ = ReadTestDataFile(filename);
  26. protocol_ = std::make_unique<InMemoryUrlProtocol>(
  27. data_->data(), data_->data_size(), false);
  28. reader_ = std::make_unique<AudioFileReader>(protocol_.get());
  29. }
  30. // Reads and the entire file provided to Initialize().
  31. void ReadAndVerify(const char* expected_audio_hash, int expected_frames) {
  32. std::vector<std::unique_ptr<AudioBus>> decoded_audio_packets;
  33. int actual_frames = reader_->Read(&decoded_audio_packets);
  34. std::unique_ptr<AudioBus> decoded_audio_data =
  35. AudioBus::Create(reader_->channels(), actual_frames);
  36. int dest_start_frame = 0;
  37. for (size_t k = 0; k < decoded_audio_packets.size(); ++k) {
  38. const AudioBus* packet = decoded_audio_packets[k].get();
  39. int frame_count = packet->frames();
  40. packet->CopyPartialFramesTo(0, frame_count, dest_start_frame,
  41. decoded_audio_data.get());
  42. dest_start_frame += frame_count;
  43. }
  44. ASSERT_LE(actual_frames, decoded_audio_data->frames());
  45. ASSERT_EQ(expected_frames, actual_frames);
  46. AudioHash audio_hash;
  47. audio_hash.Update(decoded_audio_data.get(), actual_frames);
  48. EXPECT_EQ(expected_audio_hash, audio_hash.ToString());
  49. }
  50. // Verify packets are consistent across demuxer runs. Reads the first few
  51. // packets and then seeks back to the start timestamp and verifies that the
  52. // hashes match on the packets just read.
  53. void VerifyPackets() {
  54. const int kReads = 3;
  55. const int kTestPasses = 2;
  56. AVPacket packet;
  57. base::TimeDelta start_timestamp;
  58. std::vector<std::string> packet_md5_hashes_;
  59. for (int i = 0; i < kTestPasses; ++i) {
  60. for (int j = 0; j < kReads; ++j) {
  61. ASSERT_TRUE(reader_->ReadPacketForTesting(&packet));
  62. // On the first pass save the MD5 hash of each packet, on subsequent
  63. // passes ensure it matches.
  64. const std::string md5_hash = base::MD5String(base::StringPiece(
  65. reinterpret_cast<char*>(packet.data), packet.size));
  66. if (i == 0) {
  67. packet_md5_hashes_.push_back(md5_hash);
  68. if (j == 0) {
  69. start_timestamp = ConvertFromTimeBase(
  70. reader_->codec_context_for_testing()->time_base, packet.pts);
  71. }
  72. } else {
  73. EXPECT_EQ(packet_md5_hashes_[j], md5_hash) << "j = " << j;
  74. }
  75. av_packet_unref(&packet);
  76. }
  77. ASSERT_TRUE(reader_->SeekForTesting(start_timestamp));
  78. }
  79. }
  80. void RunTest(const char* fn,
  81. const char* hash,
  82. int channels,
  83. int sample_rate,
  84. base::TimeDelta duration,
  85. int frames,
  86. int expected_frames) {
  87. Initialize(fn);
  88. ASSERT_TRUE(reader_->Open());
  89. EXPECT_EQ(channels, reader_->channels());
  90. EXPECT_EQ(sample_rate, reader_->sample_rate());
  91. if (frames >= 0) {
  92. EXPECT_EQ(duration.InMicroseconds(),
  93. reader_->GetDuration().InMicroseconds());
  94. EXPECT_EQ(frames, reader_->GetNumberOfFrames());
  95. EXPECT_EQ(reader_->HasKnownDuration(), true);
  96. } else {
  97. EXPECT_EQ(reader_->HasKnownDuration(), false);
  98. }
  99. if (!packet_verification_disabled_)
  100. ASSERT_NO_FATAL_FAILURE(VerifyPackets());
  101. ReadAndVerify(hash, expected_frames);
  102. }
  103. void RunTestFailingDemux(const char* fn) {
  104. Initialize(fn);
  105. EXPECT_FALSE(reader_->Open());
  106. }
  107. void RunTestFailingDecode(const char* fn, int expect_read = 0) {
  108. Initialize(fn);
  109. EXPECT_TRUE(reader_->Open());
  110. std::vector<std::unique_ptr<AudioBus>> decoded_audio_packets;
  111. EXPECT_EQ(reader_->Read(&decoded_audio_packets), expect_read);
  112. }
  113. void RunTestPartialDecode(const char* fn) {
  114. Initialize(fn);
  115. EXPECT_TRUE(reader_->Open());
  116. std::vector<std::unique_ptr<AudioBus>> decoded_audio_packets;
  117. constexpr int packets_to_read = 1;
  118. reader_->Read(&decoded_audio_packets, packets_to_read);
  119. EXPECT_EQ(static_cast<int>(decoded_audio_packets.size()), packets_to_read);
  120. }
  121. void disable_packet_verification() { packet_verification_disabled_ = true; }
  122. protected:
  123. scoped_refptr<DecoderBuffer> data_;
  124. std::unique_ptr<InMemoryUrlProtocol> protocol_;
  125. std::unique_ptr<AudioFileReader> reader_;
  126. bool packet_verification_disabled_;
  127. };
  128. TEST_F(AudioFileReaderTest, WithoutOpen) {
  129. Initialize("bear.ogv");
  130. }
  131. TEST_F(AudioFileReaderTest, InvalidFile) {
  132. RunTestFailingDemux("ten_byte_file");
  133. }
  134. TEST_F(AudioFileReaderTest, UnknownDuration) {
  135. RunTest("bear-320x240-live.webm", "-3.59,-2.06,-0.43,2.15,0.77,-0.95,", 2,
  136. 44100, base::Microseconds(-1), -1, 121024);
  137. }
  138. TEST_F(AudioFileReaderTest, WithVideo) {
  139. RunTest("bear.ogv", "-0.73,0.92,0.48,-0.07,-0.92,-0.88,", 2, 44100,
  140. base::Microseconds(1011520), 44609, 45632);
  141. }
  142. TEST_F(AudioFileReaderTest, Vorbis) {
  143. RunTest("sfx.ogg", "2.17,3.31,5.15,6.33,5.97,4.35,", 1, 44100,
  144. base::Microseconds(350001), 15436, 15936);
  145. }
  146. TEST_F(AudioFileReaderTest, WaveU8) {
  147. RunTest("sfx_u8.wav", "-1.23,-1.57,-1.14,-0.91,-0.87,-0.07,", 1, 44100,
  148. base::Microseconds(288414), 12720, 12719);
  149. }
  150. TEST_F(AudioFileReaderTest, WaveS16LE) {
  151. RunTest("sfx_s16le.wav", "3.05,2.87,3.00,3.32,3.58,4.08,", 1, 44100,
  152. base::Microseconds(288414), 12720, 12719);
  153. }
  154. TEST_F(AudioFileReaderTest, WaveS24LE) {
  155. RunTest("sfx_s24le.wav", "3.03,2.86,2.99,3.31,3.57,4.06,", 1, 44100,
  156. base::Microseconds(288414), 12720, 12719);
  157. }
  158. TEST_F(AudioFileReaderTest, WaveF32LE) {
  159. RunTest("sfx_f32le.wav", "3.03,2.86,2.99,3.31,3.57,4.06,", 1, 44100,
  160. base::Microseconds(288414), 12720, 12719);
  161. }
  162. TEST_F(AudioFileReaderTest, MP3) {
  163. RunTest("sfx.mp3", "1.30,2.72,4.56,5.08,3.74,2.03,", 1, 44100,
  164. base::Microseconds(313470), 13825, 11025);
  165. }
  166. TEST_F(AudioFileReaderTest, CorruptMP3) {
  167. // Disable packet verification since the file is corrupt and FFmpeg does not
  168. // make any guarantees on packet consistency in this case.
  169. disable_packet_verification();
  170. RunTest("corrupt.mp3", "-4.95,-2.95,-0.44,1.16,0.31,-2.21,", 1, 44100,
  171. base::Microseconds(1018801), 44930, 44928);
  172. }
  173. #if BUILDFLAG(USE_PROPRIETARY_CODECS)
  174. TEST_F(AudioFileReaderTest, AAC) {
  175. RunTest("sfx.m4a", "0.79,2.31,4.15,4.92,4.04,1.44,", 1, 44100,
  176. base::Microseconds(371660), 16391, 12701);
  177. }
  178. TEST_F(AudioFileReaderTest, AAC_SinglePacket) {
  179. RunTest("440hz-10ms.m4a", "3.77,4.53,4.75,3.48,3.67,3.76,", 1, 44100,
  180. base::Microseconds(69660), 3073, 441);
  181. }
  182. TEST_F(AudioFileReaderTest, AAC_ADTS) {
  183. RunTest("sfx.adts", "1.80,1.66,2.31,3.26,4.46,3.36,", 1, 44100,
  184. base::Microseconds(384733), 16967, 13312);
  185. }
  186. TEST_F(AudioFileReaderTest, MidStreamConfigChangesFail) {
  187. RunTestFailingDecode("midstream_config_change.mp3", 42624);
  188. }
  189. #endif
  190. TEST_F(AudioFileReaderTest, VorbisInvalidChannelLayout) {
  191. RunTestFailingDemux("9ch.ogg");
  192. }
  193. TEST_F(AudioFileReaderTest, WaveValidFourChannelLayout) {
  194. RunTest("4ch.wav", "131.71,38.02,130.31,44.89,135.98,42.52,", 4, 44100,
  195. base::Microseconds(100001), 4411, 4410);
  196. }
  197. TEST_F(AudioFileReaderTest, ReadPartialMP3) {
  198. RunTestPartialDecode("sfx.mp3");
  199. }
  200. } // namespace media