forked from google/lyra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_loss_handler_test.cc
289 lines (257 loc) · 13.2 KB
/
packet_loss_handler_test.cc
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "packet_loss_handler.h"
#include <memory>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "lyra_config.h"
#include "noise_estimator_interface.h"
#include "spectrogram_predictor_interface.h"
#include "testing/mock_noise_estimator.h"
#include "testing/mock_spectrogram_predictor.h"
namespace chromemedia {
namespace codec {
namespace {
using testing::Return;
static constexpr int kSampleRateHz = 16000;
static const int kNumSamplesPerFrame = GetNumSamplesPerFrame(kSampleRateHz);
static constexpr float kMaxLostSeconds = 0.1;
static const int kMaxConsecutiveLostSamples = kMaxLostSeconds * kSampleRateHz;
} // namespace
// Use a test peer to access the private constructor of PacketLossHandler and
// inject MockNoiseEstimator and MockSpectrogramPredictor.
class PacketLossHandlerPeer {
public:
explicit PacketLossHandlerPeer(
std::unique_ptr<MockNoiseEstimator> mock_noise_estimator,
std::unique_ptr<MockSpectrogramPredictor> mock_spectrogram_predictor)
: packet_loss_handler_(kSampleRateHz, std::move(mock_noise_estimator),
std::move(mock_spectrogram_predictor)) {}
bool SetReceivedFeatures(const std::vector<float>& features) {
return packet_loss_handler_.SetReceivedFeatures(features);
}
absl::optional<std::vector<float>> EstimateLostFeatures(int num_samples) {
return packet_loss_handler_.EstimateLostFeatures(num_samples);
}
bool is_comfort_noise() { return packet_loss_handler_.is_comfort_noise(); }
int FetchConsecutiveLostSamples() {
return packet_loss_handler_.consecutive_lost_samples_;
}
private:
PacketLossHandler packet_loss_handler_;
};
// Creates a PacketLossHandler with valid parameters and ensures that the
// returned handler is not nullptr.
TEST(PacketLossHandlerTest, ValidCreateReturnsHandler) {
EXPECT_NE(nullptr,
PacketLossHandler::Create(
kSampleRateHz, kNumFeatures,
static_cast<float>(kNumSamplesPerFrame) / kSampleRateHz));
}
// Creates a PacketLossHandler with parameters such that the contained
// NoiseEstimator will be invalid and ensures that the returned
// PacketLossHandler is nullptr as a result.
TEST(PacketLossHandlerTest, InvalidNoiseEstimatorCreatesNullHandler) {
// A seconds_per_frame value of 0.0 causes NoiseEstimator::Create to fail.
float seconds_per_frame = 0.0;
EXPECT_EQ(nullptr, PacketLossHandler::Create(kSampleRateHz, kNumFeatures,
seconds_per_frame));
}
// Calls SetReceivedFeatures on a valid PacketLossHandler with a valid feature
// vector and ensures |consecutive_lost_samples_| remains 0 and that the
// SpectrogramPredictor's FeedPacket method is invoked.
TEST(PacketLossHandlerTest, SetReceivedFeaturesWithValidFeatures) {
std::vector<float> mock_features(kNumFeatures, 1.0);
auto mock_noise_estimator = absl::make_unique<MockNoiseEstimator>();
auto mock_spectrogram_predictor =
absl::make_unique<MockSpectrogramPredictor>();
EXPECT_CALL(*mock_noise_estimator, Update(mock_features))
.WillOnce(Return(true));
EXPECT_CALL(*mock_spectrogram_predictor, FeedFrame(mock_features)).Times(1);
auto packet_loss_handler_peer = absl::make_unique<PacketLossHandlerPeer>(
std::move(mock_noise_estimator), std::move(mock_spectrogram_predictor));
EXPECT_EQ(0, packet_loss_handler_peer->FetchConsecutiveLostSamples());
EXPECT_FALSE(packet_loss_handler_peer->is_comfort_noise());
EXPECT_TRUE(packet_loss_handler_peer->SetReceivedFeatures(mock_features));
EXPECT_EQ(0, packet_loss_handler_peer->FetchConsecutiveLostSamples());
EXPECT_FALSE(packet_loss_handler_peer->is_comfort_noise());
}
// Calls SetReceivedFeatures on a valid PacketLossHandler with an invalid
// feature vector and ensures |consecutive_lost_samples_| remains 0 and that the
// SpectrogramPredictor's FeedPacket method is invoked.
TEST(PacketLossHandlerTest, SetReceivedFeaturesWithInvalidFeatures) {
float invalid_num_features = kNumFeatures - 1;
std::vector<float> mock_features(invalid_num_features, 1.0);
auto mock_noise_estimator = absl::make_unique<MockNoiseEstimator>();
auto mock_spectrogram_predictor =
absl::make_unique<MockSpectrogramPredictor>();
EXPECT_CALL(*mock_noise_estimator, Update(mock_features))
.WillOnce(Return(false));
EXPECT_CALL(*mock_spectrogram_predictor, FeedFrame(mock_features)).Times(0);
auto packet_loss_handler_peer = absl::make_unique<PacketLossHandlerPeer>(
std::move(mock_noise_estimator), std::move(mock_spectrogram_predictor));
EXPECT_FALSE(packet_loss_handler_peer->SetReceivedFeatures(mock_features));
EXPECT_EQ(0, packet_loss_handler_peer->FetchConsecutiveLostSamples());
}
// Calls EstimateLostFeatures on a PacketLossHandler with
// |consecutive_lost_samples_| less than kMaxConsecutiveLostSamples and makes
// sure that |consecutive_lost_samples_| is incremented and
// |spectrogram_predictor_|'s PredictFrame method is invoked.
TEST(PacketLossHandlerTest,
EstimateLostFeaturesWithConsecutiveLostSamplesBelowMax) {
static constexpr int kNumSamplesToRequest = 100;
std::vector<float> mock_prediction(kNumFeatures, 2.0);
auto mock_noise_estimator = absl::make_unique<MockNoiseEstimator>();
auto mock_spectrogram_predictor =
absl::make_unique<MockSpectrogramPredictor>();
EXPECT_CALL(*mock_spectrogram_predictor, PredictFrame())
.WillOnce(Return(mock_prediction));
auto packet_loss_handler_peer = absl::make_unique<PacketLossHandlerPeer>(
std::move(mock_noise_estimator), std::move(mock_spectrogram_predictor));
int consecutive_lost_samples_prior =
packet_loss_handler_peer->FetchConsecutiveLostSamples();
auto prediction =
packet_loss_handler_peer->EstimateLostFeatures(kNumSamplesToRequest);
EXPECT_EQ(consecutive_lost_samples_prior + kNumSamplesToRequest,
packet_loss_handler_peer->FetchConsecutiveLostSamples());
EXPECT_EQ(mock_prediction, prediction);
EXPECT_FALSE(packet_loss_handler_peer->is_comfort_noise());
}
// Calls EstimateLostFeatures on a PacketLossHandler and ensures that as a
// result |consecutive_lost_samples_| is nonzero, then calls SetReceivedFeatures
// with a valid feature vector and ensures |consecutive_lost_samples_| is reset
// to 0.
TEST(PacketLossHandlerTest, SetFeaturesResetsConsecutiveLostSamples) {
static constexpr int kNumSamplesToRequest = 100;
std::vector<float> mock_features(kNumFeatures, 1.0);
auto mock_noise_estimator = absl::make_unique<MockNoiseEstimator>();
auto mock_spectrogram_predictor =
absl::make_unique<MockSpectrogramPredictor>();
EXPECT_CALL(*mock_noise_estimator, Update(mock_features))
.WillOnce(Return(true));
auto packet_loss_handler_peer = absl::make_unique<PacketLossHandlerPeer>(
std::move(mock_noise_estimator), std::move(mock_spectrogram_predictor));
packet_loss_handler_peer->EstimateLostFeatures(kNumSamplesToRequest);
EXPECT_NE(0, packet_loss_handler_peer->FetchConsecutiveLostSamples());
EXPECT_TRUE(packet_loss_handler_peer->SetReceivedFeatures(mock_features));
EXPECT_EQ(0, packet_loss_handler_peer->FetchConsecutiveLostSamples());
EXPECT_FALSE(packet_loss_handler_peer->is_comfort_noise());
}
// Calls EstimateLostFeatures with out of bound values for |num_samples| and
// ensures that a nullopt is returned.
TEST(PacketLossHandlerTest, EstimateLostFeaturesWithInvalidNumSamples) {
auto mock_noise_estimator = absl::make_unique<MockNoiseEstimator>();
auto mock_spectrogram_predictor =
absl::make_unique<MockSpectrogramPredictor>();
EXPECT_CALL(*mock_spectrogram_predictor, PredictFrame()).Times(0);
EXPECT_CALL(*mock_spectrogram_predictor, FeedFrame(testing::_)).Times(0);
EXPECT_CALL(*mock_noise_estimator, NoiseEstimate()).Times(0);
auto packet_loss_handler_peer = absl::make_unique<PacketLossHandlerPeer>(
std::move(mock_noise_estimator), std::move(mock_spectrogram_predictor));
int num_samples_to_request = -1;
ASSERT_FALSE(
packet_loss_handler_peer->EstimateLostFeatures(num_samples_to_request)
.has_value());
num_samples_to_request = 0;
ASSERT_FALSE(
packet_loss_handler_peer->EstimateLostFeatures(num_samples_to_request)
.has_value());
}
// Calls EstimateLostFeatures on a PacketLossHandler to move the handler into a
// state in which it should return noise on the next invocation of
// EstimateLostFeatures. Then calls EstimateLostFeatures an additional time to
// verify noise is returned and fed back into |spectrogram_predictor_|.
TEST(PacketLossHandlerTest,
EstimateLostFeaturesReturnsNoiseAfterTooManyLostSamples) {
static const int kNumSamplesToRequest = 100;
std::vector<float> mock_prediction(kNumFeatures, 2.0);
std::vector<float> mock_noise(kNumFeatures, 3.0);
auto mock_noise_estimator = absl::make_unique<MockNoiseEstimator>();
auto mock_spectrogram_predictor =
absl::make_unique<MockSpectrogramPredictor>();
EXPECT_CALL(*mock_spectrogram_predictor, PredictFrame())
// The floor of the result is wanted, so integer division is fine.
.Times(kMaxConsecutiveLostSamples / kNumSamplesToRequest)
.WillRepeatedly(Return(mock_prediction));
EXPECT_CALL(*mock_spectrogram_predictor, FeedFrame(mock_noise)).Times(1);
EXPECT_CALL(*mock_noise_estimator, NoiseEstimate())
.WillOnce(Return(mock_noise));
auto packet_loss_handler_peer = absl::make_unique<PacketLossHandlerPeer>(
std::move(mock_noise_estimator), std::move(mock_spectrogram_predictor));
int num_samples_requested_aggregated = kNumSamplesToRequest;
while (num_samples_requested_aggregated <= kMaxConsecutiveLostSamples) {
auto prediction =
packet_loss_handler_peer->EstimateLostFeatures(kNumSamplesToRequest);
ASSERT_TRUE(prediction.has_value());
num_samples_requested_aggregated += kNumSamplesToRequest;
EXPECT_EQ(mock_prediction, prediction.value());
EXPECT_FALSE(packet_loss_handler_peer->is_comfort_noise());
}
auto prediction =
packet_loss_handler_peer->EstimateLostFeatures(kNumSamplesToRequest);
ASSERT_TRUE(prediction.has_value());
EXPECT_EQ(mock_noise, prediction.value());
EXPECT_TRUE(packet_loss_handler_peer->is_comfort_noise());
}
// Puts a PacketLossHandler into a state in which it should return noise upon
// invocation of EstimateLostFeatures and then calls SetReceivedFeatures on it
// with a valid feature vector and ensures that this puts the handler back into
// a state in which EstimateLostFeatures returns the result of calling
// PredictFrame on |spectrogram_predictor_|.
TEST(PacketLossHandlerTest,
SetReceivedFeaturesReturnsHandlerFromNoisePredictionState) {
static const int kNumSamplesToRequest = 100;
std::vector<float> mock_features(kNumFeatures, 1.0);
std::vector<float> mock_prediction(kNumFeatures, 2.0);
std::vector<float> mock_noise(kNumFeatures, 3.0);
auto mock_noise_estimator = absl::make_unique<MockNoiseEstimator>();
auto mock_spectrogram_predictor =
absl::make_unique<MockSpectrogramPredictor>();
EXPECT_CALL(*mock_spectrogram_predictor, PredictFrame())
// The floor of the result is wanted, so integer division is fine.
.Times(kMaxConsecutiveLostSamples / kNumSamplesToRequest + 1)
.WillRepeatedly(Return(mock_prediction));
EXPECT_CALL(*mock_spectrogram_predictor, FeedFrame(mock_noise)).Times(1);
EXPECT_CALL(*mock_spectrogram_predictor, FeedFrame(mock_features)).Times(1);
EXPECT_CALL(*mock_noise_estimator, Update(mock_features))
.WillOnce(Return(true));
EXPECT_CALL(*mock_noise_estimator, NoiseEstimate())
.WillOnce(Return(mock_noise));
auto packet_loss_handler_peer = absl::make_unique<PacketLossHandlerPeer>(
std::move(mock_noise_estimator), std::move(mock_spectrogram_predictor));
int num_samples_requested_aggregated = kNumSamplesToRequest;
while (num_samples_requested_aggregated <= kMaxConsecutiveLostSamples) {
auto prediction =
packet_loss_handler_peer->EstimateLostFeatures(kNumSamplesToRequest);
ASSERT_TRUE(prediction.has_value());
num_samples_requested_aggregated += kNumSamplesToRequest;
EXPECT_EQ(mock_prediction, prediction.value());
EXPECT_FALSE(packet_loss_handler_peer->is_comfort_noise());
}
auto prediction =
packet_loss_handler_peer->EstimateLostFeatures(kNumSamplesToRequest);
ASSERT_TRUE(prediction.has_value());
EXPECT_EQ(mock_noise, prediction.value());
EXPECT_TRUE(packet_loss_handler_peer->is_comfort_noise());
EXPECT_TRUE(packet_loss_handler_peer->SetReceivedFeatures(mock_features));
prediction =
packet_loss_handler_peer->EstimateLostFeatures(kNumSamplesToRequest);
ASSERT_TRUE(prediction.has_value());
EXPECT_EQ(mock_prediction, prediction.value());
EXPECT_FALSE(packet_loss_handler_peer->is_comfort_noise());
}
} // namespace codec
} // namespace chromemedia