1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
media / base / audio_hash_unittest.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include <memory>
#include "media/base/audio_bus.h"
#include "media/base/audio_hash.h"
#include "media/base/fake_audio_render_callback.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
static const int kChannelCount = 2;
static const int kFrameCount = 1024;
static const int kSampleRate = 48000;
class AudioHashTest : public testing::Test {
public:
AudioHashTest()
: bus_one_(AudioBus::Create(kChannelCount, kFrameCount)),
bus_two_(AudioBus::Create(kChannelCount, kFrameCount)),
fake_callback_(0.01, kSampleRate) {
// Fill each channel in each bus with unique data.
GenerateUniqueChannels(bus_one_.get());
GenerateUniqueChannels(bus_two_.get());
}
void GenerateUniqueChannels(AudioBus* audio_bus) {
// Use an AudioBus wrapper to avoid an extra memcpy when filling channels.
std::unique_ptr<AudioBus> wrapped_bus = AudioBus::CreateWrapper(1);
wrapped_bus->set_frames(audio_bus->frames());
// Since FakeAudioRenderCallback generates only a single channel of unique
// audio data, we need to fill each channel manually.
for (int ch = 0; ch < audio_bus->channels(); ++ch) {
wrapped_bus->SetChannelData(0, audio_bus->channel_span(ch));
fake_callback_.Render(base::TimeDelta(), base::TimeTicks::Now(), {},
wrapped_bus.get());
}
}
AudioHashTest(const AudioHashTest&) = delete;
AudioHashTest& operator=(const AudioHashTest&) = delete;
~AudioHashTest() override = default;
protected:
std::unique_ptr<AudioBus> bus_one_;
std::unique_ptr<AudioBus> bus_two_;
FakeAudioRenderCallback fake_callback_;
};
// Ensure the same data hashes the same.
TEST_F(AudioHashTest, Equivalence) {
AudioHash hash_one;
hash_one.Update(bus_one_.get(), bus_one_->frames());
AudioHash hash_two;
hash_two.Update(bus_one_.get(), bus_one_->frames());
EXPECT_EQ(hash_one.ToString(), hash_two.ToString());
}
// Ensure sample order matters to the hash.
TEST_F(AudioHashTest, SampleOrder) {
AudioHash original_hash;
original_hash.Update(bus_one_.get(), bus_one_->frames());
// Swap a sample in the bus.
std::swap(bus_one_->channel(0)[0], bus_one_->channel(0)[1]);
AudioHash swapped_hash;
swapped_hash.Update(bus_one_.get(), bus_one_->frames());
EXPECT_NE(original_hash.ToString(), swapped_hash.ToString());
}
// Ensure channel order matters to the hash.
TEST_F(AudioHashTest, ChannelOrder) {
AudioHash original_hash;
original_hash.Update(bus_one_.get(), bus_one_->frames());
// Reverse channel order for the same sample data.
const int channels = bus_one_->channels();
std::unique_ptr<AudioBus> swapped_ch_bus = AudioBus::CreateWrapper(channels);
swapped_ch_bus->set_frames(bus_one_->frames());
for (int i = channels - 1; i >= 0; --i)
swapped_ch_bus->SetChannelData(channels - (i + 1),
bus_one_->channel_span(i));
AudioHash swapped_hash;
swapped_hash.Update(swapped_ch_bus.get(), swapped_ch_bus->frames());
EXPECT_NE(original_hash.ToString(), swapped_hash.ToString());
}
// Ensure bus order matters to the hash.
TEST_F(AudioHashTest, BusOrder) {
AudioHash original_hash;
original_hash.Update(bus_one_.get(), bus_one_->frames());
original_hash.Update(bus_two_.get(), bus_two_->frames());
AudioHash reordered_hash;
reordered_hash.Update(bus_two_.get(), bus_two_->frames());
reordered_hash.Update(bus_one_.get(), bus_one_->frames());
EXPECT_NE(original_hash.ToString(), reordered_hash.ToString());
}
// Ensure bus order matters to the hash even with empty buses.
TEST_F(AudioHashTest, EmptyBusOrder) {
bus_one_->Zero();
bus_two_->Zero();
AudioHash one_bus_hash;
one_bus_hash.Update(bus_one_.get(), bus_one_->frames());
AudioHash two_bus_hash;
two_bus_hash.Update(bus_one_.get(), bus_one_->frames());
two_bus_hash.Update(bus_two_.get(), bus_two_->frames());
EXPECT_NE(one_bus_hash.ToString(), two_bus_hash.ToString());
}
// Where A = [0, n], ensure hash(A[0:n/2]), hash(A[n/2:n]) and hash(A) result
// in the same value.
TEST_F(AudioHashTest, HashIgnoresUpdateOrder) {
AudioHash full_hash;
full_hash.Update(bus_one_.get(), bus_one_->frames());
AudioHash half_hash;
half_hash.Update(bus_one_.get(), bus_one_->frames() / 2);
// Create a new bus representing the second half of |bus_one_|.
const int half_frames = bus_one_->frames() / 2;
const int channels = bus_one_->channels();
std::unique_ptr<AudioBus> half_bus = AudioBus::CreateWrapper(channels);
half_bus->set_frames(half_frames);
half_bus->SetAllChannels(
bus_one_->AllChannelsSubspan(half_frames, half_frames));
half_hash.Update(half_bus.get(), half_bus->frames());
EXPECT_EQ(full_hash.ToString(), half_hash.ToString());
}
// Ensure approximate hashes pass verification.
TEST_F(AudioHashTest, VerifySimilarHash) {
AudioHash hash_one;
hash_one.Update(bus_one_.get(), bus_one_->frames());
// Twiddle the values inside the first bus.
float* channel = bus_one_->channel(0);
for (int i = 0; i < bus_one_->frames(); i += bus_one_->frames() / 64)
channel[i] += 0.0001f;
AudioHash hash_two;
hash_two.Update(bus_one_.get(), bus_one_->frames());
EXPECT_EQ(hash_one.ToString(), hash_two.ToString());
// Twiddle the values too much...
for (int i = 0; i < bus_one_->frames(); ++i)
channel[i] += 0.0001f;
AudioHash hash_three;
hash_three.Update(bus_one_.get(), bus_one_->frames());
EXPECT_NE(hash_one.ToString(), hash_three.ToString());
}
} // namespace media