aboutsummaryrefslogtreecommitdiff
path: root/cast/streaming/compound_rtcp_parser_unittest.cc
blob: f09224f9a82966ed17f7c58e87f53aa594a4a3fb (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cast/streaming/compound_rtcp_parser.h"

#include <chrono>
#include <cmath>

#include "cast/streaming/mock_compound_rtcp_parser_client.h"
#include "cast/streaming/rtcp_session.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "platform/api/time.h"

using testing::_;
using testing::Mock;
using testing::SaveArg;
using testing::StrictMock;

namespace cast {
namespace streaming {
namespace {

constexpr Ssrc kSenderSsrc{1};
constexpr Ssrc kReceiverSsrc{2};

class CompoundRtcpParserTest : public testing::Test {
 public:
  RtcpSession* session() { return &session_; }
  StrictMock<MockCompoundRtcpParserClient>* client() { return &client_; }
  CompoundRtcpParser* parser() { return &parser_; }

 private:
  RtcpSession session_{kSenderSsrc, kReceiverSsrc, openscreen::Clock::now()};
  StrictMock<MockCompoundRtcpParserClient> client_;
  CompoundRtcpParser parser_{&session_, &client_};
};

TEST_F(CompoundRtcpParserTest, ProcessesEmptyPacket) {
  const uint8_t kEmpty[0] = {};
  // Expect NO calls to mock client.
  EXPECT_TRUE(
      parser()->Parse(absl::Span<const uint8_t>(kEmpty, 0), FrameId::first()));
}

TEST_F(CompoundRtcpParserTest, ReturnsErrorForGarbage) {
  const uint8_t kGarbage[] = {
      0x42, 0x61, 0x16, 0x17, 0x26, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x69,
      0x6e, 0x67, 0x2f, 0x63, 0x61, 0x73, 0x74, 0x2f, 0x63, 0x6f, 0x6d, 0x70,
      0x6f, 0x75, 0x6e, 0x64, 0x5f, 0x72, 0x74, 0x63, 0x70, 0x5f};
  // Expect NO calls to mock client.
  EXPECT_FALSE(parser()->Parse(kGarbage, FrameId::first()));
}

TEST_F(CompoundRtcpParserTest, ParsesReceiverReportWithoutReportBlock) {
  // clang-format off
  const uint8_t kReceiverReportWithoutReportBlock[] = {
      0b10000000,  // Version=2, Padding=no, ReportCount=0.
      201,  // RTCP Packet type byte.
      0x00, 0x01,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x02,  // Receiver SSRC.
  };
  // clang-format on

  // Expect NO calls to mock client.
  EXPECT_TRUE(
      parser()->Parse(kReceiverReportWithoutReportBlock, FrameId::first()));
}

TEST_F(CompoundRtcpParserTest, ParsesReceiverReportWithReportBlock) {
  // clang-format off
  const uint8_t kReceiverReportWithReportBlock[] = {
      0b10000001,  // Version=2, Padding=no, ReportCount=1.
      201,  // RTCP Packet type byte.
      0x00, 0x07,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x02,  // Receiver SSRC.

      // Report block:
      0x00, 0x00, 0x00, 0x01,  // Sender SSRC.
      0x05,  // Fraction Lost.
      0x01, 0x02, 0x03,  // Cumulative # packets lost.
      0x09, 0x09, 0x09, 0x02,  // Highest sequence number.
      0x00, 0x00, 0x00, 0xaa,  // Interarrival Jitter.
      0x0b, 0x0c, 0x8f, 0xed,  // Sender Report ID.
      0x00, 0x01, 0x00, 0x00,  // Delay since last sender report.
  };
  // clang-format on

  RtcpReportBlock block;
  EXPECT_CALL(*(client()), OnReceiverReport(_)).WillOnce(SaveArg<0>(&block));
  EXPECT_TRUE(
      parser()->Parse(kReceiverReportWithReportBlock, FrameId::first()));
  Mock::VerifyAndClearExpectations(client());
  EXPECT_EQ(kSenderSsrc, block.ssrc);
  EXPECT_EQ(uint8_t{5}, block.packet_fraction_lost_numerator);
  EXPECT_EQ(0x010203, block.cumulative_packets_lost);
  EXPECT_EQ(uint32_t{0x09090902}, block.extended_high_sequence_number);
  EXPECT_EQ(RtpTimeDelta::FromTicks(170), block.jitter);
  EXPECT_EQ(StatusReportId{0x0b0c8fed}, block.last_status_report_id);
  EXPECT_EQ(RtcpReportBlock::Delay(65536), block.delay_since_last_report);
}

TEST_F(CompoundRtcpParserTest, ParsesPictureLossIndicatorMessage) {
  // clang-format off
  const uint8_t kPictureLossIndicatorPacket[] = {
      0b10000000 | 1,  // Version=2, Padding=no, Subtype=PLI.
      206,  // RTCP Packet type byte.
      0x00, 0x02,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x02,  // Receiver SSRC.
      0x00, 0x00, 0x00, 0x01,  // Sender SSRC.
  };

  const uint8_t kPictureLossIndicatorPacketWithWrongReceiverSsrc[] = {
      0b10000000 | 1,  // Version=2, Padding=no, Subtype=PLI.
      206,  // RTCP Packet type byte.
      0x00, 0x02,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x03,  // WRONG Receiver SSRC.
      0x00, 0x00, 0x00, 0x01,  // Sender SSRC.
  };

  const uint8_t kPictureLossIndicatorPacketWithWrongSenderSsrc[] = {
      0b10000000 | 1,  // Version=2, Padding=no, Subtype=PLI.
      206,  // RTCP Packet type byte.
      0x00, 0x02,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x02,  // Receiver SSRC.
      0x00, 0x00, 0x00, 0x03,  // WRONG Sender SSRC.
  };
  // clang-format on

  // The mock client should get a PLI notification when the packet is valid and
  // contains the correct SSRCs.
  EXPECT_CALL(*(client()), OnReceiverIndicatesPictureLoss());
  EXPECT_TRUE(parser()->Parse(kPictureLossIndicatorPacket, FrameId::first()));
  Mock::VerifyAndClearExpectations(client());

  // The mock client should get no PLI notifications when either of the SSRCs is
  // incorrect.
  EXPECT_CALL(*(client()), OnReceiverIndicatesPictureLoss()).Times(0);
  EXPECT_TRUE(parser()->Parse(kPictureLossIndicatorPacketWithWrongReceiverSsrc,
                              FrameId::first()));
  Mock::VerifyAndClearExpectations(client());
  EXPECT_CALL(*(client()), OnReceiverIndicatesPictureLoss()).Times(0);
  EXPECT_TRUE(parser()->Parse(kPictureLossIndicatorPacketWithWrongSenderSsrc,
                              FrameId::first()));
  Mock::VerifyAndClearExpectations(client());
}

// Tests that RTCP packets containing chronologically-old data are ignored. This
// test's methodology simulates a real-world possibility: A receiver sends a
// "Picture Loss Indicator" in one RTCP packet, and then it sends another packet
// ~1 second later without the PLI, indicating the problem has been resolved.
// However, the packets are delivered out-of-order by the network. In this case,
// the CompoundRtcpParser should ignore the stale packet containing the PLI.
TEST_F(CompoundRtcpParserTest, IgnoresStalePackets) {
  // clang-format off
  const uint8_t kNotStaleCompoundPacket[] = {
      // Receiver report:
      0b10000000,  // Version=2, Padding=no, ReportCount=0.
      201,  // RTCP Packet type byte.
      0x00, 0x01,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x02,  // Receiver SSRC.

      // Receiver reference time report:
      0b10000000,  // Version=2, Padding=no.
      207,  // RTCP Packet type byte.
      0x00, 0x04,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x02,  // Receiver SSRC.
      0x04,  // Block type = Receiver Reference Time Report
      0x00,  // Reserved byte.
      0x00, 0x02,  // Block length = 2.
      0xe0, 0x73, 0x2e, 0x54,  // NTP Timestamp (late evening on 2019-04-30).
          0x80, 0x00, 0x00, 0x00,
  };

  const uint8_t kStaleCompoundPacketWithPli[] = {
      // Picture loss indicator:
      0b10000000 | 1,  // Version=2, Padding=no, Subtype=PLI.
      206,  // RTCP Packet type byte.
      0x00, 0x02,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x02,  // Receiver SSRC.
      0x00, 0x00, 0x00, 0x01,  // Sender SSRC.

      // Receiver reference time report:
      0b10000000,  // Version=2, Padding=no.
      207,  // RTCP Packet type byte.
      0x00, 0x04,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x02,  // Receiver SSRC.
      0x04,  // Block type = Receiver Reference Time Report
      0x00,  // Reserved byte.
      0x00, 0x02,  // Block length = 2.
      0xe0, 0x73, 0x2e, 0x53,  // NTP Timestamp (late evening on 2019-04-30).
          0x42, 0x31, 0x20, 0x00,
  };
  // clang-format on

  const auto expected_timestamp =
      session()->ntp_converter().ToLocalTime(NtpTimestamp{0xe0732e5480000000});
  EXPECT_CALL(*(client()), OnReceiverReferenceTimeAdvanced(expected_timestamp));
  EXPECT_CALL(*(client()), OnReceiverIndicatesPictureLoss()).Times(0);
  EXPECT_TRUE(parser()->Parse(kNotStaleCompoundPacket, FrameId::first()));
  EXPECT_TRUE(parser()->Parse(kStaleCompoundPacketWithPli, FrameId::first()));
}

// Tests that unknown RTCP extended reports are ignored, but known ones are
// still parsed when sent alongside the unknown ones.
TEST_F(CompoundRtcpParserTest, IgnoresUnknownExtendedReports) {
  // clang-format off
  const uint8_t kPacketWithThreeExtendedReports[] = {
      0b10000000,  // Version=2, Padding=no.
      207,  // RTCP Packet type byte.
      0x00, 0x0c,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x02,  // Receiver SSRC.

      // Unknown extended report:
      0x02,  // Block type = unknown (2)
      0x00,  // Reserved byte.
      0x00, 0x06,  // Block length = 6 words.
      0x01, 0x01, 0x01, 0x01,
      0x02, 0x02, 0x02, 0x02,
      0x03, 0x03, 0x03, 0x03,
      0x04, 0x04, 0x04, 0x04,
      0x05, 0x05, 0x05, 0x05,
      0x06, 0x06, 0x06, 0x06,

      // Receiver Reference Time Report:
      0x04,  // Block type = RRTR
      0x00,  // Reserved byte.
      0x00, 0x02,  // Block length = 2 words.
      0xe0, 0x73, 0x2e, 0x55,  // NTP Timestamp (late evening on 2019-04-30).
          0x00, 0x00, 0x00, 0x00,

      // Another unknown extended report:
      0x00,  // Block type = unknown (0)
      0x00,  // Reserved byte.
      0x00, 0x00,  // Block length = 0 words.
  };
  // clang-format on

  const auto expected_timestamp =
      session()->ntp_converter().ToLocalTime(NtpTimestamp{0xe0732e5500000000});
  EXPECT_CALL(*(client()), OnReceiverReferenceTimeAdvanced(expected_timestamp));
  EXPECT_TRUE(
      parser()->Parse(kPacketWithThreeExtendedReports, FrameId::first()));
}

// Tests that a simple Cast Feedback packet is parsed, and the checkpoint frame
// ID is properly bit-extended, based on the current state of the Sender.
TEST_F(CompoundRtcpParserTest, ParsesSimpleFeedback) {
  // clang-format off
  const uint8_t kFeedbackPacket[] = {
      0b10000000 | 15,  // Version=2, Padding=no, Subtype=Feedback.
      206,  // RTCP Packet type byte.
      0x00, 0x04,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x02,  // Receiver SSRC.
      0x00, 0x00, 0x00, 0x01,  // Sender SSRC.
      'C', 'A', 'S', 'T',
      0x0a,  // Checkpoint Frame ID = 10.
      0x00,  // No NACKs.
      0x02, 0x26,  // Playout delay = 550 ms.
  };
  // clang-format on

  // First scenario: Valid range of FrameIds is [0,42].
  const auto kMaxFeedbackFrameId0 = FrameId::first() + 42;
  const auto expected_frame_id0 = FrameId::first() + 10;
  const auto expected_playout_delay = std::chrono::milliseconds(550);
  EXPECT_CALL(*(client()),
              OnReceiverCheckpoint(expected_frame_id0, expected_playout_delay));
  EXPECT_TRUE(parser()->Parse(kFeedbackPacket, kMaxFeedbackFrameId0));
  Mock::VerifyAndClearExpectations(client());

  // Second scenario: Valid range of FrameIds is [299,554]. Note: 544 == 0x22a.
  const auto kMaxFeedbackFrameId1 = FrameId::first() + 0x22a;
  const auto expected_frame_id1 = FrameId::first() + 0x20a;
  EXPECT_CALL(*(client()),
              OnReceiverCheckpoint(expected_frame_id1, expected_playout_delay));
  EXPECT_TRUE(parser()->Parse(kFeedbackPacket, kMaxFeedbackFrameId1));
  Mock::VerifyAndClearExpectations(client());
}

// Tests NACK feedback parsing, and that redundant NACKs are de-duped, and that
// the results are delivered to the client sorted.
TEST_F(CompoundRtcpParserTest, ParsesFeedbackWithNacks) {
  // clang-format off
  const uint8_t kFeedbackPacket[] = {
      0b10000000 | 15,  // Version=2, Padding=no, Subtype=Feedback.
      206,  // RTCP Packet type byte.
      0x00, 0x0b,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x02,  // Receiver SSRC.
      0x00, 0x00, 0x00, 0x01,  // Sender SSRC.
      'C', 'A', 'S', 'T',
      0x0a,  // Checkpoint Frame ID = 10.
      0x07,  // Seven NACKs.
      0x02, 0x28,  // Playout delay = 552 ms.
      0x0b, 0x00, 0x03, 0b00000000,  // NACK Packet 3 in Frame 11.
      0x0b, 0x00, 0x07, 0b10001101,  // NACK Packet 7-8, 10-11, 15 in Frame 11.
      0x0d, 0xff, 0xff, 0b00000000,  // NACK all packets in Frame 13.
      0x0b, 0x00, 0x0b, 0b00000000,  // Redundant: NACK packet 11 in Frame 11.
      0x0c, 0xff, 0xff, 0b00000000,  // NACK all packets in Frame 12.
      0x0d, 0x00, 0x01, 0b00000000,  // Redundant: NACK packet 1 in Frame 13.
      0x0e, 0x00, 0x00, 0b01000010,  // NACK packets 0, 2, 7 in Frame 14.
  };
  // clang-format on

  // The de-duped and sorted list of the frame/packet NACKs expected when
  // parsing kFeedbackPacket:
  const std::vector<PacketNack> kMissingPackets = {
      {FrameId::first() + 11, 3},
      {FrameId::first() + 11, 7},
      {FrameId::first() + 11, 8},
      {FrameId::first() + 11, 10},
      {FrameId::first() + 11, 11},
      {FrameId::first() + 11, 15},
      {FrameId::first() + 12, kAllPacketsLost},
      {FrameId::first() + 13, kAllPacketsLost},
      {FrameId::first() + 14, 0},
      {FrameId::first() + 14, 2},
      {FrameId::first() + 14, 7},
  };

  const auto kMaxFeedbackFrameId = FrameId::first() + 42;
  const auto expected_frame_id = FrameId::first() + 10;
  const auto expected_playout_delay = std::chrono::milliseconds(552);
  EXPECT_CALL(*(client()),
              OnReceiverCheckpoint(expected_frame_id, expected_playout_delay));
  EXPECT_CALL(*(client()), OnReceiverIsMissingPackets(kMissingPackets));
  EXPECT_TRUE(parser()->Parse(kFeedbackPacket, kMaxFeedbackFrameId));
}

// Tests the CST2 "later frame ACK" parsing: Both the common "2 bytes of bit
// vector" case, and a "multiple words of bit vector" case.
TEST_F(CompoundRtcpParserTest, ParsesFeedbackWithAcks) {
  // clang-format off
  const uint8_t kSmallerFeedbackPacket[] = {
      0b10000000 | 15,  // Version=2, Padding=no, Subtype=Feedback.
      206,  // RTCP Packet type byte.
      0x00, 0x07,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x02,  // Receiver SSRC.
      0x00, 0x00, 0x00, 0x01,  // Sender SSRC.
      'C', 'A', 'S', 'T',
      0x0a,  // Checkpoint Frame ID = 10.
      0x01,  // One NACK.
      0x01, 0x26,  // Playout delay = 294 ms.
      0x0b, 0x00, 0x03, 0b00000000,  // NACK Packet 3 in Frame 11.
      'C', 'S', 'T', '2',
      0x99,  // Feedback counter.
      0x02,  // 2 bytes of ACK bit vector.
      0b00000010, 0b00000000,  // ACK only frame 13.
  };

  const uint8_t kLargerFeedbackPacket[] = {
      0b10000000 | 15,  // Version=2, Padding=no, Subtype=Feedback.
      206,  // RTCP Packet type byte.
      0x00, 0x08,  // Length of remainder of packet, in 32-bit words.
      0x00, 0x00, 0x00, 0x02,  // Receiver SSRC.
      0x00, 0x00, 0x00, 0x01,  // Sender SSRC.
      'C', 'A', 'S', 'T',
      0x0a,  // Checkpoint Frame ID = 10.
      0x00,  // Zero NACKs.
      0x01, 0x26,  // Playout delay = 294 ms.
      'C', 'S', 'T', '2',
      0x99,  // Feedback counter.
      0x0a,  // 10 bytes of ACK bit vector.
      0b11111111, 0b11111111,  // ACK frames 12-27.
      0b00000000, 0b00000001, 0b00000000, 0b00000000,  // ACK frame 36.
      0b00000000, 0b00000000, 0b00000000, 0b10000000,  // ACK frame 91.
  };
  // clang-format on

  // From the smaller packet: The single frame ACK and single packet NACK.
  const std::vector<FrameId> kFrame13Only = {FrameId::first() + 13};
  const std::vector<PacketNack> kFrame11Packet3Only = {
      {FrameId::first() + 11, 3}};

  // From the larger packet: Many frame ACKs.
  const std::vector<FrameId> kManyFrames = {
      FrameId::first() + 12, FrameId::first() + 13, FrameId::first() + 14,
      FrameId::first() + 15, FrameId::first() + 16, FrameId::first() + 17,
      FrameId::first() + 18, FrameId::first() + 19, FrameId::first() + 20,
      FrameId::first() + 21, FrameId::first() + 22, FrameId::first() + 23,
      FrameId::first() + 24, FrameId::first() + 25, FrameId::first() + 26,
      FrameId::first() + 27, FrameId::first() + 36, FrameId::first() + 91,
  };

  // Test the smaller packet.
  const auto kMaxFeedbackFrameId = FrameId::first() + 100;
  const auto expected_frame_id = FrameId::first() + 10;
  const auto expected_playout_delay = std::chrono::milliseconds(294);
  EXPECT_CALL(*(client()),
              OnReceiverCheckpoint(expected_frame_id, expected_playout_delay));
  EXPECT_CALL(*(client()), OnReceiverHasFrames(kFrame13Only));
  EXPECT_CALL(*(client()), OnReceiverIsMissingPackets(kFrame11Packet3Only));
  EXPECT_TRUE(parser()->Parse(kSmallerFeedbackPacket, kMaxFeedbackFrameId));
  Mock::VerifyAndClearExpectations(client());

  // Test the larger ACK packet.
  EXPECT_CALL(*(client()),
              OnReceiverCheckpoint(expected_frame_id, expected_playout_delay));
  EXPECT_CALL(*(client()), OnReceiverHasFrames(kManyFrames));
  EXPECT_TRUE(parser()->Parse(kLargerFeedbackPacket, kMaxFeedbackFrameId));
  Mock::VerifyAndClearExpectations(client());
}

}  // namespace
}  // namespace streaming
}  // namespace cast