File: inter_arrival_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (422 lines) | stat: -rw-r--r-- 14,801 bytes parent folder | download
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "testing/gtest/include/gtest/gtest.h"

#include "webrtc/common_types.h"
#include "webrtc/modules/remote_bitrate_estimator/inter_arrival.h"
#include "webrtc/system_wrappers/interface/scoped_ptr.h"

namespace webrtc {
namespace testing {

enum {
  kTimestampGroupLengthUs = 5000,
  kMinStep = 20,
  kTriggerNewGroupUs = kTimestampGroupLengthUs + kMinStep,
  kBurstThresholdMs = 5,
  kAbsSendTimeFraction = 18,
  kAbsSendTimeInterArrivalUpshift = 8,
  kInterArrivalShift = kAbsSendTimeFraction + kAbsSendTimeInterArrivalUpshift,
};

const double kRtpTimestampToMs = 1.0 / 90.0;
const double kAstToMs = 1000.0 / static_cast<double>(1 << kInterArrivalShift);

class InterArrivalTest : public ::testing::Test {
 protected:
  virtual void SetUp() {
    inter_arrival_rtp_.reset(new InterArrival(
        MakeRtpTimestamp(kTimestampGroupLengthUs),
        kRtpTimestampToMs,
        true));
    inter_arrival_ast_.reset(new InterArrival(
        MakeAbsSendTime(kTimestampGroupLengthUs),
        kAstToMs,
        true));
  }

  // Test that neither inter_arrival instance complete the timestamp group from
  // the given data.
  void ExpectFalse(int64_t timestamp_us, int64_t arrival_time_ms,
                   size_t packet_size) {
    InternalExpectFalse(inter_arrival_rtp_.get(),
                        MakeRtpTimestamp(timestamp_us), arrival_time_ms,
                        packet_size);
    InternalExpectFalse(inter_arrival_ast_.get(), MakeAbsSendTime(timestamp_us),
                        arrival_time_ms, packet_size);
  }

  // Test that both inter_arrival instances complete the timestamp group from
  // the given data and that all returned deltas are as expected (except
  // timestamp delta, which is rounded from us to different ranges and must
  // match within an interval, given in |timestamp_near].
  void ExpectTrue(int64_t timestamp_us, int64_t arrival_time_ms,
                  size_t packet_size, int64_t expected_timestamp_delta_us,
                  int64_t expected_arrival_time_delta_ms,
                  int expected_packet_size_delta,
                  uint32_t timestamp_near) {
    InternalExpectTrue(inter_arrival_rtp_.get(), MakeRtpTimestamp(timestamp_us),
                       arrival_time_ms, packet_size,
                       MakeRtpTimestamp(expected_timestamp_delta_us),
                       expected_arrival_time_delta_ms,
                       expected_packet_size_delta, timestamp_near);
    InternalExpectTrue(inter_arrival_ast_.get(), MakeAbsSendTime(timestamp_us),
                       arrival_time_ms, packet_size,
                       MakeAbsSendTime(expected_timestamp_delta_us),
                       expected_arrival_time_delta_ms,
                       expected_packet_size_delta, timestamp_near << 8);
  }

  void WrapTestHelper(int64_t wrap_start_us, uint32_t timestamp_near,
                      bool unorderly_within_group) {
    // Step through the range of a 32 bit int, 1/4 at a time to not cause
    // packets close to wraparound to be judged as out of order.

    // G1
    int64_t arrival_time = 17;
    ExpectFalse(0, arrival_time, 1);

    // G2
    arrival_time += kBurstThresholdMs + 1;
    ExpectFalse(wrap_start_us / 4, arrival_time, 1);

    // G3
    arrival_time += kBurstThresholdMs + 1;
    ExpectTrue(wrap_start_us / 2, arrival_time, 1,
               wrap_start_us / 4, 6, 0,   // Delta G2-G1
               0);

    // G4
    arrival_time += kBurstThresholdMs + 1;
    int64_t g4_arrival_time = arrival_time;
    ExpectTrue(wrap_start_us / 2 + wrap_start_us / 4, arrival_time, 1,
               wrap_start_us / 4, 6, 0,   // Delta G3-G2
               timestamp_near);

    // G5
    arrival_time += kBurstThresholdMs + 1;
    ExpectTrue(wrap_start_us, arrival_time, 2,
               wrap_start_us / 4, 6, 0,   // Delta G4-G3
               timestamp_near);
    for (int i = 0; i < 10; ++i) {
      // Slowly step across the wrap point.
      arrival_time += kBurstThresholdMs + 1;
      if (unorderly_within_group) {
        // These packets arrive with timestamps in decreasing order but are
        // nevertheless accumulated to group because their timestamps are higher
        // than the initial timestamp of the group.
        ExpectFalse(wrap_start_us + kMinStep * (9 - i), arrival_time, 1);
      } else {
        ExpectFalse(wrap_start_us + kMinStep * i, arrival_time, 1);
      }
    }
    int64_t g5_arrival_time = arrival_time;

    // This packet is out of order and should be dropped.
    arrival_time += kBurstThresholdMs + 1;
    ExpectFalse(wrap_start_us - 100, arrival_time, 100);

    // G6
    arrival_time += kBurstThresholdMs + 1;
    int64_t g6_arrival_time = arrival_time;
    ExpectTrue(wrap_start_us + kTriggerNewGroupUs, arrival_time, 10,
               wrap_start_us / 4 + 9 * kMinStep,
               g5_arrival_time - g4_arrival_time,
               (2 + 10) - 1,  // Delta G5-G4
               timestamp_near);

    // This packet is out of order and should be dropped.
    arrival_time += kBurstThresholdMs + 1;
    ExpectFalse(wrap_start_us + kTimestampGroupLengthUs, arrival_time, 100);

    // G7
    arrival_time += kBurstThresholdMs + 1;
    ExpectTrue(wrap_start_us + 2 * kTriggerNewGroupUs,
               arrival_time, 100,
               // Delta G6-G5
               kTriggerNewGroupUs - 9 * kMinStep,
               g6_arrival_time - g5_arrival_time,
               10 - (2 + 10),
               timestamp_near);
  }

 private:
  static uint32_t MakeRtpTimestamp(int64_t us) {
    return static_cast<uint32_t>(static_cast<uint64_t>(us * 90 + 500) / 1000);
  }

  static uint32_t MakeAbsSendTime(int64_t us) {
    uint32_t absolute_send_time = static_cast<uint32_t>(
        ((static_cast<uint64_t>(us) << 18) + 500000) / 1000000) & 0x00FFFFFFul;
    return absolute_send_time << 8;
  }

  static void InternalExpectFalse(InterArrival* inter_arrival,
                                  uint32_t timestamp, int64_t arrival_time_ms,
                                  size_t packet_size) {
    uint32_t dummy_timestamp = 101;
    int64_t dummy_arrival_time_ms = 303;
    int dummy_packet_size = 909;
    bool computed = inter_arrival->ComputeDeltas(timestamp,
                                                 arrival_time_ms,
                                                 packet_size,
                                                 &dummy_timestamp,
                                                 &dummy_arrival_time_ms,
                                                 &dummy_packet_size);
    EXPECT_EQ(computed, false);
    EXPECT_EQ(101ul, dummy_timestamp);
    EXPECT_EQ(303, dummy_arrival_time_ms);
    EXPECT_EQ(909, dummy_packet_size);
  }

  static void InternalExpectTrue(InterArrival* inter_arrival,
                                 uint32_t timestamp, int64_t arrival_time_ms,
                                 size_t packet_size,
                                 uint32_t expected_timestamp_delta,
                                 int64_t expected_arrival_time_delta_ms,
                                 int expected_packet_size_delta,
                                 uint32_t timestamp_near) {
    uint32_t delta_timestamp = 101;
    int64_t delta_arrival_time_ms = 303;
    int delta_packet_size = 909;
    bool computed = inter_arrival->ComputeDeltas(timestamp,
                                                 arrival_time_ms,
                                                 packet_size,
                                                 &delta_timestamp,
                                                 &delta_arrival_time_ms,
                                                 &delta_packet_size);
    EXPECT_EQ(true, computed);
    EXPECT_NEAR(expected_timestamp_delta, delta_timestamp, timestamp_near);
    EXPECT_EQ(expected_arrival_time_delta_ms, delta_arrival_time_ms);
    EXPECT_EQ(expected_packet_size_delta, delta_packet_size);
  }

  scoped_ptr<InterArrival> inter_arrival_rtp_;
  scoped_ptr<InterArrival> inter_arrival_ast_;
};

TEST_F(InterArrivalTest, FirstPacket) {
  ExpectFalse(0, 17, 1);
}

TEST_F(InterArrivalTest, FirstGroup) {
  // G1
  int64_t arrival_time = 17;
  int64_t g1_arrival_time = arrival_time;
  ExpectFalse(0, arrival_time, 1);

  // G2
  arrival_time += kBurstThresholdMs + 1;
  int64_t g2_arrival_time = arrival_time;
  ExpectFalse(kTriggerNewGroupUs, arrival_time, 2);

  // G3
  // Only once the first packet of the third group arrives, do we see the deltas
  // between the first two.
  arrival_time += kBurstThresholdMs + 1;
  ExpectTrue(2 * kTriggerNewGroupUs, arrival_time, 1,
             // Delta G2-G1
             kTriggerNewGroupUs, g2_arrival_time - g1_arrival_time, 1,
             0);
}

TEST_F(InterArrivalTest, SecondGroup) {
  // G1
  int64_t arrival_time = 17;
  int64_t g1_arrival_time = arrival_time;
  ExpectFalse(0, arrival_time, 1);

  // G2
  arrival_time += kBurstThresholdMs + 1;
  int64_t g2_arrival_time = arrival_time;
  ExpectFalse(kTriggerNewGroupUs, arrival_time, 2);

  // G3
  arrival_time += kBurstThresholdMs + 1;
  int64_t g3_arrival_time = arrival_time;
  ExpectTrue(2 * kTriggerNewGroupUs, arrival_time, 1,
             // Delta G2-G1
             kTriggerNewGroupUs, g2_arrival_time - g1_arrival_time, 1,
             0);

  // G4
  // First packet of 4th group yields deltas between group 2 and 3.
  arrival_time += kBurstThresholdMs + 1;
  ExpectTrue(3 * kTriggerNewGroupUs, arrival_time, 2,
             // Delta G3-G2
             kTriggerNewGroupUs, g3_arrival_time - g2_arrival_time, -1,
             0);
}

TEST_F(InterArrivalTest, AccumulatedGroup) {
  // G1
  int64_t arrival_time = 17;
  int64_t g1_arrival_time = arrival_time;
  ExpectFalse(0, arrival_time, 1);

  // G2
  arrival_time += kBurstThresholdMs + 1;
  ExpectFalse(kTriggerNewGroupUs, 28, 2);
  int64_t timestamp = kTriggerNewGroupUs;
  for (int i = 0; i < 10; ++i) {
    // A bunch of packets arriving within the same group.
    arrival_time += kBurstThresholdMs + 1;
    timestamp += kMinStep;
    ExpectFalse(timestamp, arrival_time, 1);
  }
  int64_t g2_arrival_time = arrival_time;
  int64_t g2_timestamp = timestamp;

  // G3
  arrival_time = 500;
  ExpectTrue(2 * kTriggerNewGroupUs, arrival_time, 100,
             g2_timestamp, g2_arrival_time - g1_arrival_time,
             (2 + 10) - 1,   // Delta G2-G1
             0);
}

TEST_F(InterArrivalTest, OutOfOrderPacket) {
  // G1
  int64_t arrival_time = 17;
  int64_t timestamp = 0;
  ExpectFalse(timestamp, arrival_time, 1);
  int64_t g1_timestamp = timestamp;
  int64_t g1_arrival_time = arrival_time;

  // G2
  arrival_time += 11;
  timestamp += kTriggerNewGroupUs;
  ExpectFalse(timestamp, 28, 2);
  for (int i = 0; i < 10; ++i) {
    arrival_time += kBurstThresholdMs + 1;
    timestamp += kMinStep;
    ExpectFalse(timestamp, arrival_time, 1);
  }
  int64_t g2_timestamp = timestamp;
  int64_t g2_arrival_time = arrival_time;

  // This packet is out of order and should be dropped.
  arrival_time = 281;
  ExpectFalse(g1_timestamp, arrival_time, 100);

  // G3
  arrival_time = 500;
  timestamp = 2 * kTriggerNewGroupUs;
  ExpectTrue(timestamp, arrival_time, 100,
             // Delta G2-G1
             g2_timestamp - g1_timestamp, g2_arrival_time - g1_arrival_time,
             (2 + 10) - 1,
             0);
}

TEST_F(InterArrivalTest, OutOfOrderWithinGroup) {
  // G1
  int64_t arrival_time = 17;
  int64_t timestamp = 0;
  ExpectFalse(timestamp, arrival_time, 1);
  int64_t g1_timestamp = timestamp;
  int64_t g1_arrival_time = arrival_time;

  // G2
  timestamp += kTriggerNewGroupUs;
  arrival_time += 11;
  ExpectFalse(kTriggerNewGroupUs, 28, 2);
  timestamp += 10 * kMinStep;
  int64_t g2_timestamp = timestamp;
  for (int i = 0; i < 10; ++i) {
    // These packets arrive with timestamps in decreasing order but are
    // nevertheless accumulated to group because their timestamps are higher
    // than the initial timestamp of the group.
    arrival_time += kBurstThresholdMs + 1;
    ExpectFalse(timestamp, arrival_time, 1);
    timestamp -= kMinStep;
  }
  int64_t g2_arrival_time = arrival_time;

  // However, this packet is deemed out of order and should be dropped.
  arrival_time = 281;
  timestamp = g1_timestamp;
  ExpectFalse(timestamp, arrival_time, 100);

  // G3
  timestamp = 2 * kTriggerNewGroupUs;
  arrival_time = 500;
  ExpectTrue(timestamp, arrival_time, 100,
             g2_timestamp - g1_timestamp, g2_arrival_time - g1_arrival_time,
             (2 + 10) - 1,
             0);
}

TEST_F(InterArrivalTest, TwoBursts) {
  // G1
  int64_t g1_arrival_time = 17;
  ExpectFalse(0, g1_arrival_time, 1);

  // G2
  int64_t timestamp = kTriggerNewGroupUs;
  int64_t arrival_time = 100;  // Simulate no packets arriving for 100 ms.
  for (int i = 0; i < 10; ++i) {
    // A bunch of packets arriving in one burst (within 5 ms apart).
    timestamp += 30000;
    arrival_time += kBurstThresholdMs;
    ExpectFalse(timestamp, arrival_time, 1);
  }
  int64_t g2_arrival_time = arrival_time;
  int64_t g2_timestamp = timestamp;

  // G3
  timestamp += 30000;
  arrival_time += kBurstThresholdMs + 1;
  ExpectTrue(timestamp, arrival_time, 100,
             g2_timestamp, g2_arrival_time - g1_arrival_time,
             10 - 1,  // Delta G2-G1
             0);
}


TEST_F(InterArrivalTest, NoBursts) {
  // G1
  ExpectFalse(0, 17, 1);

  // G2
  int64_t timestamp = kTriggerNewGroupUs;
  int64_t arrival_time = 28;
  ExpectFalse(timestamp, arrival_time, 2);

  // G3
  ExpectTrue(kTriggerNewGroupUs + 30000, arrival_time + kBurstThresholdMs + 1,
             100, timestamp - 0, arrival_time - 17,
             2 - 1,  // Delta G2-G1
             0);
}

// Yields 0xfffffffe when converted to internal representation in
// inter_arrival_rtp_ and inter_arrival_ast_ respectively.
static const int64_t kStartRtpTimestampWrapUs = 47721858827;
static const int64_t kStartAbsSendTimeWrapUs = 63999995;

TEST_F(InterArrivalTest, RtpTimestampWrap) {
  WrapTestHelper(kStartRtpTimestampWrapUs, 1, false);
}

TEST_F(InterArrivalTest, AbsSendTimeWrap) {
  WrapTestHelper(kStartAbsSendTimeWrapUs, 1, false);
}

TEST_F(InterArrivalTest, RtpTimestampWrapOutOfOrderWithinGroup) {
  WrapTestHelper(kStartRtpTimestampWrapUs, 1, true);
}

TEST_F(InterArrivalTest, AbsSendTimeWrapOutOfOrderWithinGroup) {
  WrapTestHelper(kStartAbsSendTimeWrapUs, 1, true);
}
}  // namespace testing
}  // namespace webrtc