aboutsummaryrefslogtreecommitdiff
path: root/cast/streaming/sender_unittest.cc
blob: 1296e4a179ac265a974a28fc58be590eb8cea601 (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
// Copyright 2020 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/sender.h"

#include <stdint.h>

#include <algorithm>
#include <array>
#include <chrono>
#include <limits>
#include <map>
#include <set>
#include <utility>
#include <vector>

#include "absl/types/optional.h"
#include "absl/types/span.h"
#include "cast/streaming/compound_rtcp_builder.h"
#include "cast/streaming/constants.h"
#include "cast/streaming/encoded_frame.h"
#include "cast/streaming/frame_collector.h"
#include "cast/streaming/frame_crypto.h"
#include "cast/streaming/frame_id.h"
#include "cast/streaming/mock_environment.h"
#include "cast/streaming/packet_util.h"
#include "cast/streaming/rtcp_session.h"
#include "cast/streaming/rtp_defines.h"
#include "cast/streaming/rtp_packet_parser.h"
#include "cast/streaming/sender_packet_router.h"
#include "cast/streaming/sender_report_parser.h"
#include "cast/streaming/session_config.h"
#include "cast/streaming/ssrc.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "platform/test/fake_clock.h"
#include "platform/test/fake_task_runner.h"
#include "util/alarm.h"
#include "util/chrono_helpers.h"
#include "util/yet_another_bit_vector.h"

using testing::_;
using testing::AtLeast;
using testing::Invoke;
using testing::InvokeWithoutArgs;
using testing::Mock;
using testing::NiceMock;
using testing::Return;
using testing::Sequence;

namespace openscreen {
namespace cast {
namespace {

// Sender configuration.
constexpr Ssrc kSenderSsrc = 1;
constexpr Ssrc kReceiverSsrc = 2;
constexpr int kRtpTimebase = 48000;
constexpr milliseconds kTargetPlayoutDelay{400};
constexpr auto kAesKey =
    std::array<uint8_t, 16>{{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                             0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}};
constexpr auto kCastIvMask =
    std::array<uint8_t, 16>{{0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0, 0x90, 0x80,
                             0x70, 0x60, 0x50, 0x40, 0x30, 0x20, 0x10, 0x00}};
constexpr RtpPayloadType kRtpPayloadType = RtpPayloadType::kVideoVp8;

// The number of RTP ticks advanced per frame, for 100 FPS media.
constexpr int kRtpTicksPerFrame = kRtpTimebase / 100;

// The number of milliseconds advanced per frame, for 100 FPS media.
constexpr milliseconds kFrameDuration{1000 / 100};
static_assert(kFrameDuration < (kTargetPlayoutDelay / 10),
              "Kickstart test assumes frame duration is far less than the "
              "playout delay.");

// An Encoded frame that also holds onto its own copy of data.
struct EncodedFrameWithBuffer : public EncodedFrame {
  // |EncodedFrame::data| always points inside buffer.begin()...buffer.end().
  std::vector<uint8_t> buffer;
};

// SenderPacketRouter configuration for these tests.
constexpr int kNumPacketsPerBurst = 20;
constexpr milliseconds kBurstInterval{10};

// An arbitrary value, subtracted from "now," to specify the reference_time on
// frames that are about to be enqueued. This simulates that capture+encode
// happened in the past, before Sender::EnqueueFrame() is called.
constexpr milliseconds kCaptureDelay{11};

// In some tests, the computed time values could be off a little bit due to
// imprecision in certain wire-format timestamp types. The following macro
// behaves just like Gtest's EXPECT_NEAR(), but works with all the time types
// too.
#define EXPECT_NEARLY_EQUAL(duration_a, duration_b, epsilon) \
  if ((duration_a) >= (duration_b)) {                        \
    EXPECT_LE((duration_a), (duration_b) + (epsilon));       \
  } else {                                                   \
    EXPECT_GE((duration_a), (duration_b) - (epsilon));       \
  }

void OverrideRtpTimestamp(int frame_count, EncodedFrame* frame, int fps) {
  const int ticks = frame_count * kRtpTimebase / fps;
  frame->rtp_timestamp = RtpTimeTicks() + RtpTimeDelta::FromTicks(ticks);
}

// Simulates UDP/IPv6 traffic in one direction (from Sender→Receiver, or
// Receiver→Sender), with a settable amount of delay.
class SimulatedNetworkPipe {
 public:
  SimulatedNetworkPipe(TaskRunner* task_runner,
                       Environment::PacketConsumer* remote)
      : task_runner_(task_runner), remote_(remote) {
    // Create a fake IPv6 address using the "documentative purposes" prefix
    // concatenated with the |this| pointer.
    std::array<uint16_t, 8> hextets{};
    hextets[0] = 0x2001;
    hextets[1] = 0x0db8;
    auto* const this_pointer = this;
    static_assert(sizeof(this_pointer) <= (6 * sizeof(uint16_t)), "");
    memcpy(&hextets[2], &this_pointer, sizeof(this_pointer));
    local_endpoint_ = IPEndpoint{IPAddress(hextets), 2344};
  }

  const IPEndpoint& local_endpoint() const { return local_endpoint_; }

  Clock::duration network_delay() const { return network_delay_; }
  void set_network_delay(Clock::duration delay) { network_delay_ = delay; }

  // The caller needs to spin the task runner before |packet| will reach the
  // other side.
  void StartPacketTransmission(std::vector<uint8_t> packet) {
    task_runner_->PostTaskWithDelay(
        [this, packet = std::move(packet)]() mutable {
          remote_->OnReceivedPacket(local_endpoint_, FakeClock::now(),
                                    std::move(packet));
        },
        network_delay_);
  }

 private:
  TaskRunner* const task_runner_;
  Environment::PacketConsumer* const remote_;

  IPEndpoint local_endpoint_;

  // The amount of time for the packet to transmit over this simulated network
  // pipe. Defaults to zero to simplify the tests that don't care about delays.
  Clock::duration network_delay_{};
};

// Processes packets from the Sender under test, allowing unit tests to set
// expectations for parsed RTP or RTCP packets, to confirm proper behavior of
// the Sender.
class MockReceiver : public Environment::PacketConsumer {
 public:
  explicit MockReceiver(SimulatedNetworkPipe* pipe_to_sender)
      : pipe_to_sender_(pipe_to_sender),
        rtcp_session_(kSenderSsrc, kReceiverSsrc, FakeClock::now()),
        sender_report_parser_(&rtcp_session_),
        rtcp_builder_(&rtcp_session_),
        rtp_parser_(kSenderSsrc),
        crypto_(kAesKey, kCastIvMask) {
    rtcp_builder_.SetPlayoutDelay(kTargetPlayoutDelay);
  }

  ~MockReceiver() override = default;

  // Simulate the Receiver ACK'ing all frames up to and including the
  // |new_checkpoint|.
  void SetCheckpointFrame(FrameId new_checkpoint) {
    OSP_CHECK_GE(new_checkpoint, rtcp_builder_.checkpoint_frame());
    rtcp_builder_.SetCheckpointFrame(new_checkpoint);
  }

  // Automatically advances the checkpoint based on what is found in
  // |complete_frames_|, returning true if the checkpoint moved forward.
  bool AutoAdvanceCheckpoint() {
    const FrameId old_checkpoint = rtcp_builder_.checkpoint_frame();
    FrameId new_checkpoint = old_checkpoint;
    for (auto it = complete_frames_.upper_bound(old_checkpoint);
         it != complete_frames_.end(); ++it) {
      if (it->first != new_checkpoint + 1) {
        break;
      }
      ++new_checkpoint;
    }
    if (new_checkpoint > old_checkpoint) {
      rtcp_builder_.SetCheckpointFrame(new_checkpoint);
      return true;
    }
    return false;
  }

  void SetPictureLossIndicator(bool picture_is_lost) {
    rtcp_builder_.SetPictureLossIndicator(picture_is_lost);
  }

  void SetReceiverReport(StatusReportId reply_for,
                         RtcpReportBlock::Delay processing_delay) {
    RtcpReportBlock receiver_report;
    receiver_report.ssrc = kSenderSsrc;
    receiver_report.last_status_report_id = reply_for;
    receiver_report.delay_since_last_report = processing_delay;
    rtcp_builder_.IncludeReceiverReportInNextPacket(receiver_report);
  }

  void SetNacksAndAcks(std::vector<PacketNack> packet_nacks,
                       std::vector<FrameId> frame_acks) {
    rtcp_builder_.IncludeFeedbackInNextPacket(std::move(packet_nacks),
                                              std::move(frame_acks));
  }

  // Builds and sends a RTCP packet containing one or more of: checkpoint, PLI,
  // Receiver Report, NACKs, ACKs.
  void TransmitRtcpFeedbackPacket() {
    uint8_t buffer[kMaxRtpPacketSizeForIpv6UdpOnEthernet];
    const absl::Span<uint8_t> packet =
        rtcp_builder_.BuildPacket(FakeClock::now(), buffer);
    pipe_to_sender_->StartPacketTransmission(
        std::vector<uint8_t>(packet.begin(), packet.end()));
  }

  // Used by tests to simulate the Receiver not seeing specific packets come in
  // from the network (e.g., because the network dropped the packets).
  void SetIgnoreList(std::vector<PacketNack> ignore_list) {
    ignore_list_ = ignore_list;
  }

  // Environment::PacketConsumer implementation.
  //
  // Called to process a packet from the Sender, simulating basic RTP frame
  // collection and Sender Report parsing/handling.
  void OnReceivedPacket(const IPEndpoint& source,
                        Clock::time_point arrival_time,
                        std::vector<uint8_t> packet) override {
    const auto type_and_ssrc = InspectPacketForRouting(packet);
    EXPECT_NE(ApparentPacketType::UNKNOWN, type_and_ssrc.first);
    EXPECT_EQ(kSenderSsrc, type_and_ssrc.second);
    if (type_and_ssrc.first == ApparentPacketType::RTP) {
      const absl::optional<RtpPacketParser::ParseResult> part_of_frame =
          rtp_parser_.Parse(packet);
      ASSERT_TRUE(part_of_frame);

      // Return early if simulating packet drops over the network.
      if (std::find_if(ignore_list_.begin(), ignore_list_.end(),
                       [&](const PacketNack& baddie) {
                         return (
                             baddie.frame_id == part_of_frame->frame_id &&
                             (baddie.packet_id == kAllPacketsLost ||
                              baddie.packet_id == part_of_frame->packet_id));
                       }) != ignore_list_.end()) {
        return;
      }

      OnRtpPacket(*part_of_frame);
      CollectRtpPacket(*part_of_frame, std::move(packet));
    } else if (type_and_ssrc.first == ApparentPacketType::RTCP) {
      absl::optional<SenderReportParser::SenderReportWithId> report =
          sender_report_parser_.Parse(packet);
      ASSERT_TRUE(report);
      OnSenderReport(*report);
    }
  }

  std::map<FrameId, EncodedFrameWithBuffer> TakeCompleteFrames() {
    std::map<FrameId, EncodedFrameWithBuffer> result;
    result.swap(complete_frames_);
    return result;
  }

  // Tests set expectations on these mocks to monitor events of interest, and/or
  // invoke additional behaviors.
  MOCK_METHOD1(OnRtpPacket,
               void(const RtpPacketParser::ParseResult& parsed_packet));
  MOCK_METHOD1(OnFrameComplete, void(FrameId frame_id));
  MOCK_METHOD1(OnSenderReport,
               void(const SenderReportParser::SenderReportWithId& report));

 private:
  // Collects the individual RTP packets until a whole frame can be formed, then
  // calls OnFrameComplete(). Ignores extra RTP packets that are no longer
  // needed.
  void CollectRtpPacket(const RtpPacketParser::ParseResult& part_of_frame,
                        std::vector<uint8_t> packet) {
    const FrameId frame_id = part_of_frame.frame_id;
    if (complete_frames_.find(frame_id) != complete_frames_.end()) {
      return;
    }
    FrameCollector& collector = incomplete_frames_[frame_id];
    collector.set_frame_id(frame_id);
    EXPECT_TRUE(collector.CollectRtpPacket(part_of_frame, &packet));
    if (!collector.is_complete()) {
      return;
    }
    const EncryptedFrame& encrypted = collector.PeekAtAssembledFrame();
    EncodedFrameWithBuffer* const decrypted = &complete_frames_[frame_id];
    // Note: Not setting decrypted->reference_time here since the logic around
    // calculating the playout time is rather complex, and is definitely outside
    // the scope of the testing being done in this module. Instead, end-to-end
    // testing should exist elsewhere to confirm frame play-out times with real
    // Receivers.
    decrypted->buffer.resize(FrameCrypto::GetPlaintextSize(encrypted));
    decrypted->data = absl::Span<uint8_t>(decrypted->buffer);
    crypto_.Decrypt(encrypted, decrypted);
    incomplete_frames_.erase(frame_id);
    OnFrameComplete(frame_id);
  }

  SimulatedNetworkPipe* const pipe_to_sender_;
  RtcpSession rtcp_session_;
  SenderReportParser sender_report_parser_;
  CompoundRtcpBuilder rtcp_builder_;
  RtpPacketParser rtp_parser_;
  FrameCrypto crypto_;

  std::vector<PacketNack> ignore_list_;
  std::map<FrameId, FrameCollector> incomplete_frames_;
  std::map<FrameId, EncodedFrameWithBuffer> complete_frames_;
};

class MockObserver : public Sender::Observer {
 public:
  MOCK_METHOD1(OnFrameCanceled, void(FrameId frame_id));
  MOCK_METHOD0(OnPictureLost, void());
};

class SenderTest : public testing::Test {
 public:
  SenderTest()
      : fake_clock_(Clock::now()),
        task_runner_(&fake_clock_),
        sender_environment_(&FakeClock::now, &task_runner_),
        sender_packet_router_(&sender_environment_,
                              kNumPacketsPerBurst,
                              kBurstInterval),
        sender_(&sender_environment_,
                &sender_packet_router_,
                {/* .sender_ssrc = */ kSenderSsrc,
                 /* .receiver_ssrc = */ kReceiverSsrc,
                 /* .rtp_timebase = */ kRtpTimebase,
                 /* .channels = */ 2,
                 /* .target_playout_delay = */ kTargetPlayoutDelay,
                 /* .aes_secret_key = */ kAesKey,
                 /* .aes_iv_mask = */ kCastIvMask,
                 /* .is_pli_enabled = */ true},
                kRtpPayloadType),
        receiver_to_sender_pipe_(&task_runner_, &sender_packet_router_),
        receiver_(&receiver_to_sender_pipe_),
        sender_to_receiver_pipe_(&task_runner_, &receiver_) {
    sender_environment_.set_socket_error_handler(
        [](Error error) { ASSERT_TRUE(error.ok()) << error; });
    sender_environment_.set_remote_endpoint(
        receiver_to_sender_pipe_.local_endpoint());
    ON_CALL(sender_environment_, SendPacket(_))
        .WillByDefault(Invoke([this](absl::Span<const uint8_t> packet) {
          sender_to_receiver_pipe_.StartPacketTransmission(
              std::vector<uint8_t>(packet.begin(), packet.end()));
        }));
  }

  ~SenderTest() override = default;

  Sender* sender() { return &sender_; }
  MockReceiver* receiver() { return &receiver_; }

  void SetReceiverToSenderNetworkDelay(Clock::duration delay) {
    receiver_to_sender_pipe_.set_network_delay(delay);
  }

  void SetSenderToReceiverNetworkDelay(Clock::duration delay) {
    sender_to_receiver_pipe_.set_network_delay(delay);
  }

  void SimulateExecution(Clock::duration how_long = Clock::duration::zero()) {
    fake_clock_.Advance(how_long);
  }

  static void PopulateFramePayloadBuffer(int seed,
                                         int num_bytes,
                                         std::vector<uint8_t>* payload) {
    payload->clear();
    payload->reserve(num_bytes);
    for (int i = 0; i < num_bytes; ++i) {
      payload->push_back(static_cast<uint8_t>(seed + i));
    }
  }

  static void PopulateFrameWithDefaults(FrameId frame_id,
                                        Clock::time_point reference_time,
                                        int seed,
                                        int num_payload_bytes,
                                        EncodedFrameWithBuffer* frame) {
    frame->dependency = (frame_id == FrameId::first())
                            ? EncodedFrame::KEY_FRAME
                            : EncodedFrame::DEPENDS_ON_ANOTHER;
    frame->frame_id = frame_id;
    frame->referenced_frame_id = frame->frame_id;
    if (frame_id != FrameId::first()) {
      --frame->referenced_frame_id;
    }
    frame->rtp_timestamp =
        RtpTimeTicks() + (RtpTimeDelta::FromTicks(kRtpTicksPerFrame) *
                          (frame_id - FrameId::first()));
    frame->reference_time = reference_time;
    PopulateFramePayloadBuffer(seed, num_payload_bytes, &frame->buffer);
    frame->data = absl::Span<uint8_t>(frame->buffer);
  }

  // Confirms that all |sent_frames| exist in |received_frames|, with identical
  // data and metadata.
  static void ExpectFramesReceivedCorrectly(
      absl::Span<EncodedFrameWithBuffer> sent_frames,
      const std::map<FrameId, EncodedFrameWithBuffer> received_frames) {
    ASSERT_EQ(sent_frames.size(), received_frames.size());

    for (const EncodedFrameWithBuffer& sent_frame : sent_frames) {
      SCOPED_TRACE(testing::Message()
                   << "Checking sent frame " << sent_frame.frame_id);
      const auto received_it = received_frames.find(sent_frame.frame_id);
      if (received_it == received_frames.end()) {
        ADD_FAILURE() << "Did not receive frame.";
        continue;
      }
      const EncodedFrame& received_frame = received_it->second;
      EXPECT_EQ(sent_frame.dependency, received_frame.dependency);
      EXPECT_EQ(sent_frame.referenced_frame_id,
                received_frame.referenced_frame_id);
      EXPECT_EQ(sent_frame.rtp_timestamp, received_frame.rtp_timestamp);
      EXPECT_TRUE(sent_frame.data == received_frame.data);
    }
  }

 private:
  FakeClock fake_clock_;
  FakeTaskRunner task_runner_;
  NiceMock<MockEnvironment> sender_environment_;
  SenderPacketRouter sender_packet_router_;
  Sender sender_;
  SimulatedNetworkPipe receiver_to_sender_pipe_;
  NiceMock<MockReceiver> receiver_;
  SimulatedNetworkPipe sender_to_receiver_pipe_;
};

// Tests that the Sender can send EncodedFrames over an ideal network (i.e., low
// latency, no loss), and does so without having to transmit the same packet
// twice.
TEST_F(SenderTest, SendsFramesEfficiently) {
  constexpr milliseconds kOneWayNetworkDelay{1};
  SetSenderToReceiverNetworkDelay(kOneWayNetworkDelay);
  SetReceiverToSenderNetworkDelay(kOneWayNetworkDelay);

  // Expect that each packet is only sent once.
  std::set<std::pair<FrameId, FramePacketId>> received_packets;
  EXPECT_CALL(*receiver(), OnRtpPacket(_))
      .WillRepeatedly(
          Invoke([&](const RtpPacketParser::ParseResult& parsed_packet) {
            std::pair<FrameId, FramePacketId> id(parsed_packet.frame_id,
                                                 parsed_packet.packet_id);
            const auto insert_result = received_packets.insert(id);
            EXPECT_TRUE(insert_result.second)
                << "Received duplicate packet: " << id.first << ':'
                << static_cast<int>(id.second);
          }));

  // Simulate normal frame ACK'ing behavior.
  ON_CALL(*receiver(), OnFrameComplete(_)).WillByDefault(InvokeWithoutArgs([&] {
    if (receiver()->AutoAdvanceCheckpoint()) {
      receiver()->TransmitRtcpFeedbackPacket();
    }
  }));

  NiceMock<MockObserver> observer;
  EXPECT_CALL(observer, OnFrameCanceled(FrameId::first())).Times(1);
  EXPECT_CALL(observer, OnFrameCanceled(FrameId::first() + 1)).Times(1);
  EXPECT_CALL(observer, OnFrameCanceled(FrameId::first() + 2)).Times(1);
  sender()->SetObserver(&observer);

  EncodedFrameWithBuffer frames[3];
  constexpr int kFrameDataSizes[] = {8196, 12, 1900};
  for (int i = 0; i < 3; ++i) {
    if (i == 0) {
      EXPECT_TRUE(sender()->NeedsKeyFrame());
    } else {
      EXPECT_FALSE(sender()->NeedsKeyFrame());
    }
    PopulateFrameWithDefaults(FrameId::first() + i,
                              FakeClock::now() - kCaptureDelay, 0xbf - i,
                              kFrameDataSizes[i], &frames[i]);
    ASSERT_EQ(Sender::OK, sender()->EnqueueFrame(frames[i]));
    SimulateExecution(kFrameDuration);
  }
  SimulateExecution(kTargetPlayoutDelay);

  ExpectFramesReceivedCorrectly(frames, receiver()->TakeCompleteFrames());
}

// Tests that the Sender correctly computes the current in-flight media
// duration, a backlog signal for clients.
TEST_F(SenderTest, ComputesInFlightMediaDuration) {
  // With no frames enqueued, the in-flight media duration should be zero.
  EXPECT_EQ(Clock::duration::zero(),
            sender()->GetInFlightMediaDuration(RtpTimeTicks()));
  EXPECT_EQ(Clock::duration::zero(),
            sender()->GetInFlightMediaDuration(
                RtpTimeTicks() + RtpTimeDelta::FromTicks(kRtpTicksPerFrame)));

  // Enqueue a frame.
  EncodedFrameWithBuffer frame;
  PopulateFrameWithDefaults(FrameId::first(), FakeClock::now(), 0,
                            13 /* bytes */, &frame);
  ASSERT_EQ(Sender::OK, sender()->EnqueueFrame(frame));

  // Now, the in-flight media duration should depend on the RTP timestamp of the
  // next frame.
  EXPECT_EQ(kFrameDuration, sender()->GetInFlightMediaDuration(
                                frame.rtp_timestamp +
                                RtpTimeDelta::FromTicks(kRtpTicksPerFrame)));
  EXPECT_EQ(10 * kFrameDuration,
            sender()->GetInFlightMediaDuration(
                frame.rtp_timestamp +
                RtpTimeDelta::FromTicks(10 * kRtpTicksPerFrame)));
}

// Tests that the Sender computes the maximum in-flight media duration based on
// its analysis of current network conditions. By implication, this demonstrates
// that the Sender is also measuring the network round-trip time.
TEST_F(SenderTest, RespondsToNetworkLatencyChanges) {
  // The expected maximum error in time calculations is one tick of the RTCP
  // report block's delay type.
  constexpr auto kEpsilon = to_nanoseconds(RtcpReportBlock::Delay(1));

  // Before the Sender has the necessary information to compute the network
  // round-trip time, GetMaxInFlightMediaDuration() will return half the target
  // playout delay.
  EXPECT_NEARLY_EQUAL(kTargetPlayoutDelay / 2,
                      sender()->GetMaxInFlightMediaDuration(), kEpsilon);

  // No network is perfect. Simulate different one-way network delays.
  constexpr milliseconds kOutboundDelay{2};
  constexpr milliseconds kInboundDelay{4};
  constexpr milliseconds kRoundTripDelay = kOutboundDelay + kInboundDelay;
  SetSenderToReceiverNetworkDelay(kOutboundDelay);
  SetReceiverToSenderNetworkDelay(kInboundDelay);

  // Enqueue a frame in the Sender to start emitting periodic RTCP reports.
  {
    EncodedFrameWithBuffer frame;
    PopulateFrameWithDefaults(FrameId::first(), FakeClock::now(), 0,
                              1 /* byte */, &frame);
    ASSERT_EQ(Sender::OK, sender()->EnqueueFrame(frame));
  }

  // Run one network round-trip from Sender→Receiver→Sender.
  StatusReportId sender_report_id{};
  EXPECT_CALL(*receiver(), OnSenderReport(_))
      .WillOnce(Invoke(
          [&](const SenderReportParser::SenderReportWithId& sender_report) {
            sender_report_id = sender_report.report_id;
          }));
  // Simulate the passage of time for the Sender Report to reach the Receiver.
  SimulateExecution(kOutboundDelay);
  // The Receiver should have received the Sender Report at this point.
  Mock::VerifyAndClearExpectations(receiver());
  ASSERT_NE(StatusReportId{}, sender_report_id);
  // Simulate the passage of time in the Receiver doing "other tasks" before
  // replying back to the Sender. This delay is included in the Receiver Report
  // so that the Sender can isolate the delays caused by the network.
  constexpr milliseconds kReceiverProcessingDelay{2};
  SimulateExecution(kReceiverProcessingDelay);
  // Create the Receiver Report "reply," and simulate it being sent across the
  // network, back to the Sender.
  receiver()->SetReceiverReport(
      sender_report_id, std::chrono::duration_cast<RtcpReportBlock::Delay>(
                            kReceiverProcessingDelay));
  receiver()->TransmitRtcpFeedbackPacket();
  SimulateExecution(kInboundDelay);

  // At this point, the Sender should have computed the network round-trip time,
  // and so GetMaxInFlightMediaDuration() will return half the target playout
  // delay PLUS half the network round-trip time.
  EXPECT_NEARLY_EQUAL(kTargetPlayoutDelay / 2 + kRoundTripDelay / 2,
                      sender()->GetMaxInFlightMediaDuration(), kEpsilon);

  // Increase the outbound delay, which will increase the total round-trip time.
  constexpr milliseconds kIncreasedOutboundDelay{6};
  constexpr milliseconds kIncreasedRoundTripDelay =
      kIncreasedOutboundDelay + kInboundDelay;
  SetSenderToReceiverNetworkDelay(kIncreasedOutboundDelay);

  // With increased network delay, run several more network round-trips. Expect
  // the Sender to gradually converge towards the new network round-trip time.
  constexpr int kNumReportIntervals = 50;
  EXPECT_CALL(*receiver(), OnSenderReport(_))
      .Times(kNumReportIntervals)
      .WillRepeatedly(Invoke(
          [&](const SenderReportParser::SenderReportWithId& sender_report) {
            receiver()->SetReceiverReport(sender_report.report_id,
                                          RtcpReportBlock::Delay::zero());
            receiver()->TransmitRtcpFeedbackPacket();
          }));
  Clock::duration last_max = sender()->GetMaxInFlightMediaDuration();
  for (int i = 0; i < kNumReportIntervals; ++i) {
    SimulateExecution(kRtcpReportInterval);
    const Clock::duration updated_value =
        sender()->GetMaxInFlightMediaDuration();
    EXPECT_LE(last_max, updated_value);
    last_max = updated_value;
  }
  EXPECT_NEARLY_EQUAL(kTargetPlayoutDelay / 2 + kIncreasedRoundTripDelay / 2,
                      sender()->GetMaxInFlightMediaDuration(), kEpsilon);
}

// Tests that the Sender rejects frames if too large a span of FrameIds would be
// in-flight at once.
TEST_F(SenderTest, RejectsEnqueuingBeforeProtocolDesignLimit) {
  // For this test, use 1000 FPS. This makes the frames all one millisecond
  // apart to avoid triggering the media-duration rejection logic.
  constexpr int kFramesPerSecond = 1000;
  constexpr milliseconds kFrameDuration{1};

  // Send the absolute design-limit maximum number of frames.
  int frame_count = 0;
  for (; frame_count < kMaxUnackedFrames; ++frame_count) {
    EncodedFrameWithBuffer frame;
    PopulateFrameWithDefaults(sender()->GetNextFrameId(), FakeClock::now(), 0,
                              13 /* bytes */, &frame);
    OverrideRtpTimestamp(frame_count, &frame, kFramesPerSecond);
    ASSERT_EQ(Sender::OK, sender()->EnqueueFrame(frame));
    SimulateExecution(kFrameDuration);
  }

  // Now, attempting to enqueue just one more frame should fail.
  EncodedFrameWithBuffer one_frame_too_much;
  PopulateFrameWithDefaults(sender()->GetNextFrameId(), FakeClock::now(), 0,
                            13 /* bytes */, &one_frame_too_much);
  OverrideRtpTimestamp(frame_count++, &one_frame_too_much, kFramesPerSecond);
  EXPECT_EQ(Sender::REACHED_ID_SPAN_LIMIT,
            sender()->EnqueueFrame(one_frame_too_much));
  SimulateExecution(kFrameDuration);

  // Now, simulate the Receiver ACKing the first frame, and enqueuing should
  // then succeed again.
  receiver()->SetCheckpointFrame(FrameId::first());
  receiver()->TransmitRtcpFeedbackPacket();
  SimulateExecution();  // RTCP transmitted to Sender.
  EXPECT_EQ(Sender::OK, sender()->EnqueueFrame(one_frame_too_much));
  SimulateExecution(kFrameDuration);

  // Finally, attempting to enqueue another frame should fail again.
  EncodedFrameWithBuffer another_frame_too_much;
  PopulateFrameWithDefaults(sender()->GetNextFrameId(), FakeClock::now(), 0,
                            13 /* bytes */, &another_frame_too_much);
  OverrideRtpTimestamp(frame_count++, &another_frame_too_much,
                       kFramesPerSecond);
  EXPECT_EQ(Sender::REACHED_ID_SPAN_LIMIT,
            sender()->EnqueueFrame(another_frame_too_much));
  SimulateExecution(kFrameDuration);
}

TEST_F(SenderTest, CanCancelAllInFlightFrames) {
  NiceMock<MockObserver> observer;
  sender()->SetObserver(&observer);

  // Send the absolute design-limit maximum number of frames.
  for (int i = 0; i < kMaxUnackedFrames; ++i) {
    EncodedFrameWithBuffer frame;
    PopulateFrameWithDefaults(sender()->GetNextFrameId(), FakeClock::now(), 0,
                              13 /* bytes */, &frame);
    OverrideRtpTimestamp(i, &frame, 1000 /* fps */);
    ASSERT_EQ(Sender::OK, sender()->EnqueueFrame(frame));
    SimulateExecution(kFrameDuration);
  }

  EXPECT_CALL(observer, OnFrameCanceled(_)).Times(kMaxUnackedFrames);
  sender()->CancelInFlightData();
}

// Tests that the Sender rejects frames if too-long a media duration is
// in-flight. This is the Sender's primary flow control mechanism.
TEST_F(SenderTest, RejectsEnqueuingIfTooLongMediaDurationIsInFlight) {
  // For this test, use 20 FPS. This makes all frames 50 ms apart, which should
  // make it easy to trigger the media-duration rejection logic.
  constexpr int kFramesPerSecond = 20;
  constexpr milliseconds kFrameDuration{50};

  // Enqueue frames until one is rejected because the in-flight duration would
  // be too high.
  EncodedFrameWithBuffer frame;
  int frame_count = 0;
  for (; frame_count < kMaxUnackedFrames; ++frame_count) {
    PopulateFrameWithDefaults(sender()->GetNextFrameId(), FakeClock::now(), 0,
                              13 /* bytes */, &frame);
    OverrideRtpTimestamp(frame_count, &frame, kFramesPerSecond);
    const auto result = sender()->EnqueueFrame(frame);
    SimulateExecution(kFrameDuration);
    if (result == Sender::MAX_DURATION_IN_FLIGHT) {
      break;
    }
    ASSERT_EQ(Sender::OK, result);
  }

  // Now, simulate the Receiver ACKing the first frame, and enqueuing should
  // then succeed again.
  receiver()->SetCheckpointFrame(FrameId::first());
  receiver()->TransmitRtcpFeedbackPacket();
  SimulateExecution();  // RTCP transmitted to Sender.
  EXPECT_EQ(Sender::OK, sender()->EnqueueFrame(frame));
  SimulateExecution(kFrameDuration);

  // However, attempting to enqueue another frame should fail again.
  EncodedFrameWithBuffer one_frame_too_much;
  PopulateFrameWithDefaults(sender()->GetNextFrameId(), FakeClock::now(), 0,
                            13 /* bytes */, &one_frame_too_much);
  OverrideRtpTimestamp(++frame_count, &one_frame_too_much, kFramesPerSecond);
  EXPECT_EQ(Sender::MAX_DURATION_IN_FLIGHT,
            sender()->EnqueueFrame(one_frame_too_much));
  SimulateExecution(kFrameDuration);
}

// Tests that the Sender propagates the Receiver's picture loss indicator to the
// Observer::OnPictureLost(), and via calls to NeedsKeyFrame(); but only when
// producing a key frame is absolutely necessary.
TEST_F(SenderTest, ManagesReceiverPictureLossWorkflow) {
  NiceMock<MockObserver> observer;
  sender()->SetObserver(&observer);

  // Send three frames...
  EncodedFrameWithBuffer frames[6];
  for (int i = 0; i < 3; ++i) {
    if (i == 0) {
      EXPECT_TRUE(sender()->NeedsKeyFrame());
    } else {
      EXPECT_FALSE(sender()->NeedsKeyFrame());
    }
    PopulateFrameWithDefaults(FrameId::first() + i,
                              FakeClock::now() - kCaptureDelay, 0,
                              24 /* bytes */, &frames[i]);
    ASSERT_EQ(Sender::OK, sender()->EnqueueFrame(frames[i]));
    SimulateExecution(kFrameDuration);
  }
  SimulateExecution(kTargetPlayoutDelay);

  // Simulate the Receiver ACK'ing the first three frames.
  EXPECT_CALL(observer, OnFrameCanceled(FrameId::first())).Times(1);
  EXPECT_CALL(observer, OnFrameCanceled(FrameId::first() + 1)).Times(1);
  EXPECT_CALL(observer, OnFrameCanceled(FrameId::first() + 2)).Times(1);
  EXPECT_CALL(observer, OnPictureLost()).Times(0);
  receiver()->SetCheckpointFrame(frames[2].frame_id);
  receiver()->TransmitRtcpFeedbackPacket();
  SimulateExecution();  // RTCP transmitted to Sender.
  Mock::VerifyAndClearExpectations(&observer);

  // Simulate something going wrong in the Receiver, and have it report picture
  // loss to the Sender. The Sender should then propagate this to its Observer
  // and return true when NeedsKeyFrame() is called.
  EXPECT_CALL(observer, OnFrameCanceled(_)).Times(0);
  EXPECT_CALL(observer, OnPictureLost()).Times(1);
  EXPECT_FALSE(sender()->NeedsKeyFrame());
  receiver()->SetPictureLossIndicator(true);
  receiver()->TransmitRtcpFeedbackPacket();
  SimulateExecution();  // RTCP transmitted to Sender.
  Mock::VerifyAndClearExpectations(&observer);
  EXPECT_TRUE(sender()->NeedsKeyFrame());

  // Send a non-key frame, and expect NeedsKeyFrame() still returns true. The
  // Observer is not re-notified. This accounts for the case where a client's
  // media encoder had frames in its processing pipeline before NeedsKeyFrame()
  // began returning true.
  EXPECT_CALL(observer, OnFrameCanceled(_)).Times(0);
  EXPECT_CALL(observer, OnPictureLost()).Times(0);
  EncodedFrameWithBuffer& nonkey_frame = frames[3];
  PopulateFrameWithDefaults(FrameId::first() + 3,
                            FakeClock::now() - kCaptureDelay, 0, 24 /* bytes */,
                            &nonkey_frame);
  ASSERT_EQ(Sender::OK, sender()->EnqueueFrame(nonkey_frame));
  SimulateExecution(kFrameDuration);
  Mock::VerifyAndClearExpectations(&observer);
  EXPECT_TRUE(sender()->NeedsKeyFrame());

  // Now send a key frame, and expect NeedsKeyFrame() returns false. Note that
  // the Receiver hasn't cleared the PLI condition, but the Sender knows more
  // key frames won't be needed.
  EXPECT_CALL(observer, OnFrameCanceled(_)).Times(0);
  EXPECT_CALL(observer, OnPictureLost()).Times(0);
  EncodedFrameWithBuffer& recovery_frame = frames[4];
  PopulateFrameWithDefaults(FrameId::first() + 4,
                            FakeClock::now() - kCaptureDelay, 0, 24 /* bytes */,
                            &recovery_frame);
  recovery_frame.dependency = EncodedFrame::KEY_FRAME;
  recovery_frame.referenced_frame_id = recovery_frame.frame_id;
  ASSERT_EQ(Sender::OK, sender()->EnqueueFrame(recovery_frame));
  SimulateExecution(kFrameDuration);
  Mock::VerifyAndClearExpectations(&observer);
  EXPECT_FALSE(sender()->NeedsKeyFrame());

  // Let's say the Receiver hasn't received the key frame yet, and it reports
  // its picture loss again to the Sender. Observer::OnPictureLost() should not
  // be called, and NeedsKeyFrame() should NOT return true, because the Sender
  // knows the Receiver hasn't acknowledged the key frame (just sent) yet.
  EXPECT_CALL(observer, OnFrameCanceled(nonkey_frame.frame_id)).Times(1);
  EXPECT_CALL(observer, OnPictureLost()).Times(0);
  receiver()->SetCheckpointFrame(nonkey_frame.frame_id);
  receiver()->SetPictureLossIndicator(true);
  receiver()->TransmitRtcpFeedbackPacket();
  SimulateExecution();  // RTCP transmitted to Sender.
  Mock::VerifyAndClearExpectations(&observer);
  EXPECT_FALSE(sender()->NeedsKeyFrame());

  // Now, simulate the Receiver getting the key frame, but NOT recovering. This
  // should cause Observer::OnPictureLost() to be called, and cause
  // NeedsKeyFrame() to return true again.
  EXPECT_CALL(observer, OnFrameCanceled(recovery_frame.frame_id)).Times(1);
  EXPECT_CALL(observer, OnPictureLost()).Times(1);
  receiver()->SetCheckpointFrame(recovery_frame.frame_id);
  receiver()->SetPictureLossIndicator(true);
  receiver()->TransmitRtcpFeedbackPacket();
  SimulateExecution();  // RTCP transmitted to Sender.
  Mock::VerifyAndClearExpectations(&observer);
  EXPECT_TRUE(sender()->NeedsKeyFrame());

  // Send another key frame, and expect NeedsKeyFrame() returns false.
  EXPECT_CALL(observer, OnFrameCanceled(_)).Times(0);
  EXPECT_CALL(observer, OnPictureLost()).Times(0);
  EncodedFrameWithBuffer& another_recovery_frame = frames[5];
  PopulateFrameWithDefaults(FrameId::first() + 5,
                            FakeClock::now() - kCaptureDelay, 0, 24 /* bytes */,
                            &another_recovery_frame);
  another_recovery_frame.dependency = EncodedFrame::KEY_FRAME;
  another_recovery_frame.referenced_frame_id = another_recovery_frame.frame_id;
  ASSERT_EQ(Sender::OK, sender()->EnqueueFrame(another_recovery_frame));
  SimulateExecution(kFrameDuration);
  Mock::VerifyAndClearExpectations(&observer);
  EXPECT_FALSE(sender()->NeedsKeyFrame());

  // Now, simulate the Receiver recovering. It will report this to the Sender,
  // and NeedsKeyFrame() will still return false.
  EXPECT_CALL(observer, OnFrameCanceled(another_recovery_frame.frame_id))
      .Times(1);
  EXPECT_CALL(observer, OnPictureLost()).Times(0);
  receiver()->SetCheckpointFrame(another_recovery_frame.frame_id);
  receiver()->SetPictureLossIndicator(false);
  receiver()->TransmitRtcpFeedbackPacket();
  SimulateExecution();  // RTCP transmitted to Sender.
  Mock::VerifyAndClearExpectations(&observer);
  EXPECT_FALSE(sender()->NeedsKeyFrame());

  ExpectFramesReceivedCorrectly(frames, receiver()->TakeCompleteFrames());
}

// Tests that the Receiver should get a Sender Report just before the first RTP
// packet, and at regular intervals thereafter. The Sender Report contains the
// lip-sync information necessary for play-out timing.
TEST_F(SenderTest, ProvidesSenderReports) {
  std::vector<SenderReportParser::SenderReportWithId> sender_reports;
  Sequence packet_sequence;
  EXPECT_CALL(*receiver(), OnSenderReport(_))
      .InSequence(packet_sequence)
      .WillOnce(
          Invoke([&](const SenderReportParser::SenderReportWithId& report) {
            sender_reports.push_back(report);
          }))
      .RetiresOnSaturation();
  EXPECT_CALL(*receiver(), OnRtpPacket(_)).Times(1).InSequence(packet_sequence);
  EXPECT_CALL(*receiver(), OnSenderReport(_))
      .Times(3)
      .InSequence(packet_sequence)
      .WillRepeatedly(
          Invoke([&](const SenderReportParser::SenderReportWithId& report) {
            sender_reports.push_back(report);
          }));

  EncodedFrameWithBuffer frame;
  constexpr int kFrameDataSize = 250;
  PopulateFrameWithDefaults(FrameId::first(), FakeClock::now(), 0,
                            kFrameDataSize, &frame);
  ASSERT_EQ(Sender::OK, sender()->EnqueueFrame(frame));
  SimulateExecution();  // Should send one Sender Report + one RTP packet.
  EXPECT_EQ(size_t{1}, sender_reports.size());

  // Have the Receiver ACK the frame to prevent retransmitting the RTP packet.
  receiver()->SetCheckpointFrame(FrameId::first());
  receiver()->TransmitRtcpFeedbackPacket();
  SimulateExecution();  // RTCP transmitted to Sender.

  // Advance through three more reporting intervals. One Sender Report should be
  // sent each interval, making a total of 4 reports sent.
  constexpr auto kThreeReportIntervals = 3 * kRtcpReportInterval;
  SimulateExecution(kThreeReportIntervals);  // Three more Sender Reports.
  ASSERT_EQ(size_t{4}, sender_reports.size());

  // The first report should contain the same timestamps as the frame because
  // the Clock did not advance. Also, its packet count and octet count fields
  // should be zero since the report was sent before the RTP packet.
  EXPECT_EQ(frame.reference_time, sender_reports.front().reference_time);
  EXPECT_EQ(frame.rtp_timestamp, sender_reports.front().rtp_timestamp);
  EXPECT_EQ(uint32_t{0}, sender_reports.front().send_packet_count);
  EXPECT_EQ(uint32_t{0}, sender_reports.front().send_octet_count);

  // The last report should contain the timestamps extrapolated into the future
  // because the Clock did move forward. Also, the packet count and octet fields
  // should now be non-zero because the report was sent after the RTP packet.
  EXPECT_EQ(frame.reference_time + kThreeReportIntervals,
            sender_reports.back().reference_time);
  EXPECT_EQ(frame.rtp_timestamp +
                RtpTimeDelta::FromDuration(kThreeReportIntervals, kRtpTimebase),
            sender_reports.back().rtp_timestamp);
  EXPECT_EQ(uint32_t{1}, sender_reports.back().send_packet_count);
  EXPECT_EQ(uint32_t{kFrameDataSize}, sender_reports.back().send_octet_count);
}

// Tests that the Sender provides Kickstart packets whenever the Receiver may
// not know about new frames.
TEST_F(SenderTest, ProvidesKickstartPacketsIfReceiverDoesNotACK) {
  // Have the Receiver move the checkpoint forward only for the first frame, and
  // none of the later frames. This will force the Sender to eventually send a
  // Kickstart packet.
  ON_CALL(*receiver(), OnFrameComplete(_))
      .WillByDefault(Invoke([&](FrameId frame_id) {
        if (frame_id == FrameId::first()) {
          receiver()->SetCheckpointFrame(FrameId::first());
          receiver()->TransmitRtcpFeedbackPacket();
        }
      }));

  // Send three frames, paced to the media.
  EncodedFrameWithBuffer frames[3];
  for (int i = 0; i < 3; ++i) {
    PopulateFrameWithDefaults(FrameId::first() + i,
                              FakeClock::now() - kCaptureDelay, i,
                              48 /* bytes */, &frames[i]);
    ASSERT_EQ(Sender::OK, sender()->EnqueueFrame(frames[i]));
    SimulateExecution(kFrameDuration);
  }

  // Now, do nothing for a while. Because the Receiver isn't moving the
  // checkpoint forward, the Sender will have sent all the RTP packets at least
  // once, and then will start sending just Kickstart packets.
  SimulateExecution(kTargetPlayoutDelay);

  // Keep doing nothing for a while, and confirm the Sender is just sending the
  // same Kickstart packet over and over. The Kickstart packet is supposed to be
  // the last packet of the latest frame.
  std::set<std::pair<FrameId, FramePacketId>> unique_received_packet_ids;
  EXPECT_CALL(*receiver(), OnRtpPacket(_))
      .WillRepeatedly(
          Invoke([&](const RtpPacketParser::ParseResult& parsed_packet) {
            unique_received_packet_ids.emplace(parsed_packet.frame_id,
                                               parsed_packet.packet_id);
          }));
  SimulateExecution(kTargetPlayoutDelay);
  Mock::VerifyAndClearExpectations(receiver());
  EXPECT_EQ(size_t{1}, unique_received_packet_ids.size());
  EXPECT_EQ(frames[2].frame_id, unique_received_packet_ids.begin()->first);

  // Now, simulate the Receiver ACKing all the frames.
  receiver()->SetCheckpointFrame(frames[2].frame_id);
  receiver()->TransmitRtcpFeedbackPacket();
  SimulateExecution();  // RTCP transmitted to Sender.

  // With all the frames sent, the Sender should not be transmitting anything.
  EXPECT_CALL(*receiver(), OnRtpPacket(_)).Times(0);
  SimulateExecution(10 * kTargetPlayoutDelay);

  ExpectFramesReceivedCorrectly(frames, receiver()->TakeCompleteFrames());
}

// Tests that the Sender only retransmits packets specifically NACK'ed by the
// Receiver.
TEST_F(SenderTest, ResendsIndividuallyNackedPackets) {
  // Populate the frame data in each frame with enough bytes to force at least
  // three RTP packets per frame.
  constexpr int kFrameDataSize = 3 * kMaxRtpPacketSizeForIpv6UdpOnEthernet;

  // Use a 1ms network delay in each direction to make the sequence of events
  // clearer in this test.
  constexpr milliseconds kOneWayNetworkDelay{1};
  SetSenderToReceiverNetworkDelay(kOneWayNetworkDelay);
  SetReceiverToSenderNetworkDelay(kOneWayNetworkDelay);

  // Simulate that three specific packets will be dropped by the network, one
  // from each frame (about to be sent).
  const std::vector<PacketNack> dropped_packets{
      {FrameId::first(), FramePacketId{2}},
      {FrameId::first() + 1, FramePacketId{1}},
      {FrameId::first() + 2, FramePacketId{0}},
  };
  receiver()->SetIgnoreList(dropped_packets);

  // Send three frames, paced to the media. The Receiver won't completely
  // receive any of these frames due to dropped packets.
  EXPECT_CALL(*receiver(), OnFrameComplete(_)).Times(0);
  EncodedFrameWithBuffer frames[3];
  for (int i = 0; i < 3; ++i) {
    PopulateFrameWithDefaults(FrameId::first() + i,
                              FakeClock::now() - kCaptureDelay, i,
                              kFrameDataSize, &frames[i]);
    ASSERT_EQ(Sender::OK, sender()->EnqueueFrame(frames[i]));
    SimulateExecution(kFrameDuration);
  }
  SimulateExecution(kTargetPlayoutDelay);
  Mock::VerifyAndClearExpectations(receiver());
  EXPECT_EQ(3, sender()->GetInFlightFrameCount());

  // The Receiver NACKs the three dropped packets...
  receiver()->SetNacksAndAcks(dropped_packets, {});
  receiver()->TransmitRtcpFeedbackPacket();

  // In the meantime, the network recovers (i.e., no more dropped packets)...
  receiver()->SetIgnoreList({});

  // The NACKs reach the Sender, and it acts on them by retransmitting.
  SimulateExecution(kOneWayNetworkDelay);

  // As each retransmitted packet arrives at the Receiver, advance the
  // checkpoint forward to notify the Sender of frames that are now completely
  // received. Also, confirm that only the three specifically-NACK'ed packets
  // were retransmitted.
  EXPECT_CALL(*receiver(), OnFrameComplete(_))
      .Times(3)
      .WillRepeatedly(InvokeWithoutArgs([&] {
        if (receiver()->AutoAdvanceCheckpoint()) {
          receiver()->TransmitRtcpFeedbackPacket();
        }
      }));
  EXPECT_CALL(*receiver(), OnRtpPacket(_))
      .Times(3)
      .WillRepeatedly(Invoke([&](const RtpPacketParser::ParseResult& packet) {
        EXPECT_FALSE(std::find(dropped_packets.begin(), dropped_packets.end(),
                               PacketNack{packet.frame_id, packet.packet_id}) ==
                     dropped_packets.end());
      }));
  SimulateExecution(kOneWayNetworkDelay);
  Mock::VerifyAndClearExpectations(receiver());

  // The Receiver checkpoint feedback(s) travel back to the Sender, and there
  // should no longer be any frames in-flight.
  SimulateExecution(kOneWayNetworkDelay);
  EXPECT_EQ(0, sender()->GetInFlightFrameCount());

  // The Sender should not be transmitting anything from now on since all frames
  // are known to have been completely received.
  EXPECT_CALL(*receiver(), OnRtpPacket(_)).Times(0);
  SimulateExecution(10 * kTargetPlayoutDelay);

  ExpectFramesReceivedCorrectly(frames, receiver()->TakeCompleteFrames());
}

// Tests that the Sender retransmits an entire frame if the Receiver requests it
// (i.e., a full frame NACK), but does not retransmit any packets for frames
// (before or after) that have been acknowledged.
TEST_F(SenderTest, ResendsMissingFrames) {
  // Populate the frame data in each frame with enough bytes to force at least
  // three RTP packets per frame.
  constexpr int kFrameDataSize = 3 * kMaxRtpPacketSizeForIpv6UdpOnEthernet;

  // Use a 1ms network delay in each direction to make the sequence of events
  // clearer in this test.
  constexpr milliseconds kOneWayNetworkDelay{1};
  SetSenderToReceiverNetworkDelay(kOneWayNetworkDelay);
  SetReceiverToSenderNetworkDelay(kOneWayNetworkDelay);

  // Simulate that all of the packets for the second frame will be dropped by
  // the network, but only the packets for that frame.
  const std::vector<PacketNack> dropped_packets{
      {FrameId::first() + 1, kAllPacketsLost},
  };
  receiver()->SetIgnoreList(dropped_packets);

  NiceMock<MockObserver> observer;
  sender()->SetObserver(&observer);

  // The expectations below track the story and execute simulated Receiver
  // responses. The Sender will have three frames enqueued by its client, and
  // then...
  //
  // The first frame is received and the Receiver ACKs it by moving the
  // checkpoint forward.
  Sequence completion_sequence;
  EXPECT_CALL(*receiver(), OnFrameComplete(FrameId::first()))
      .InSequence(completion_sequence)
      .WillOnce(InvokeWithoutArgs([&] {
        receiver()->SetCheckpointFrame(FrameId::first());
        receiver()->TransmitRtcpFeedbackPacket();
      }));
  // Since all of the packets for the second frame are being dropped, the third
  // frame will finish next. The Receiver responds by NACKing the second frame
  // and ACKing the third frame. The checkpoint does not move forward because
  // the second frame has not been received yet.
  //
  // NETWORK CHANGE: After the third frame is received, stop dropping packets.
  EXPECT_CALL(*receiver(), OnFrameComplete(FrameId::first() + 2))
      .InSequence(completion_sequence)
      .WillOnce(InvokeWithoutArgs([&] {
        receiver()->SetNacksAndAcks(dropped_packets,
                                    std::vector<FrameId>{FrameId::first() + 2});
        receiver()->TransmitRtcpFeedbackPacket();
        receiver()->SetIgnoreList({});
      }));
  // Finally, the Sender should respond to the whole-frame NACK by re-sending
  // all of the packets for the second frame, and so the Receiver should
  // completely receive the frame.
  EXPECT_CALL(*receiver(), OnFrameComplete(FrameId::first() + 1))
      .InSequence(completion_sequence)
      .WillOnce(InvokeWithoutArgs([&] {
        receiver()->SetCheckpointFrame(FrameId::first() + 2);
        receiver()->TransmitRtcpFeedbackPacket();
      }));

  // From the Sender's perspective, the Receiver will ACK the first frame, then
  // the third frame, then the second frame.
  Sequence cancel_sequence;
  EXPECT_CALL(observer, OnFrameCanceled(FrameId::first()))
      .Times(1)
      .InSequence(cancel_sequence);
  EXPECT_CALL(observer, OnFrameCanceled(FrameId::first() + 2))
      .Times(1)
      .InSequence(cancel_sequence);
  EXPECT_CALL(observer, OnFrameCanceled(FrameId::first() + 1))
      .Times(1)
      .InSequence(cancel_sequence);

  // With all the expectations/sequences in-place, let 'er rip!
  EncodedFrameWithBuffer frames[3];
  for (int i = 0; i < 3; ++i) {
    PopulateFrameWithDefaults(FrameId::first() + i,
                              FakeClock::now() - kCaptureDelay, i,
                              kFrameDataSize, &frames[i]);
    ASSERT_EQ(Sender::OK, sender()->EnqueueFrame(frames[i]));
    SimulateExecution(kFrameDuration);
  }
  SimulateExecution(kTargetPlayoutDelay);
  Mock::VerifyAndClearExpectations(receiver());
  EXPECT_EQ(0, sender()->GetInFlightFrameCount());

  // The Sender should not be transmitting anything from now on since all frames
  // are known to have been completely received.
  EXPECT_CALL(*receiver(), OnRtpPacket(_)).Times(0);
  SimulateExecution(10 * kTargetPlayoutDelay);

  ExpectFramesReceivedCorrectly(frames, receiver()->TakeCompleteFrames());
}

}  // namespace
}  // namespace cast
}  // namespace openscreen