1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11/*
12 * This file includes unit tests for the RTPSender.
13 */
14
15#include <list>
16#include <vector>
17
18#include "testing/gmock/include/gmock/gmock.h"
19#include "testing/gtest/include/gtest/gtest.h"
20#include "webrtc/base/buffer.h"
21#include "webrtc/base/scoped_ptr.h"
22#include "webrtc/modules/rtp_rtcp/include/rtp_cvo.h"
23#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
24#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
25#include "webrtc/modules/rtp_rtcp/source/rtp_format_video_generic.h"
26#include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h"
27#include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
28#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
29#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
30#include "webrtc/system_wrappers/include/stl_util.h"
31#include "webrtc/test/mock_transport.h"
32#include "webrtc/typedefs.h"
33
34namespace webrtc {
35
36namespace {
37const int kTransmissionTimeOffsetExtensionId = 1;
38const int kAbsoluteSendTimeExtensionId = 14;
39const int kTransportSequenceNumberExtensionId = 13;
40const int kPayload = 100;
41const int kRtxPayload = 98;
42const uint32_t kTimestamp = 10;
43const uint16_t kSeqNum = 33;
44const int kTimeOffset = 22222;
45const int kMaxPacketLength = 1500;
46const uint32_t kAbsoluteSendTime = 0x00aabbcc;
47const uint8_t kAudioLevel = 0x5a;
48const uint16_t kTransportSequenceNumber = 0xaabbu;
49const uint8_t kAudioLevelExtensionId = 9;
50const int kAudioPayload = 103;
51const uint64_t kStartTime = 123456789;
52const size_t kMaxPaddingSize = 224u;
53const int kVideoRotationExtensionId = 5;
54const VideoRotation kRotation = kVideoRotation_270;
55
56using testing::_;
57
58const uint8_t* GetPayloadData(const RTPHeader& rtp_header,
59                              const uint8_t* packet) {
60  return packet + rtp_header.headerLength;
61}
62
63size_t GetPayloadDataLength(const RTPHeader& rtp_header,
64                            const size_t packet_length) {
65  return packet_length - rtp_header.headerLength - rtp_header.paddingLength;
66}
67
68uint64_t ConvertMsToAbsSendTime(int64_t time_ms) {
69  return (((time_ms << 18) + 500) / 1000) & 0x00ffffff;
70}
71
72class LoopbackTransportTest : public webrtc::Transport {
73 public:
74  LoopbackTransportTest()
75      : packets_sent_(0),
76        last_sent_packet_len_(0),
77        total_bytes_sent_(0),
78        last_sent_packet_(nullptr) {}
79
80  ~LoopbackTransportTest() {
81    STLDeleteContainerPointers(sent_packets_.begin(), sent_packets_.end());
82  }
83  bool SendRtp(const uint8_t* data,
84               size_t len,
85               const PacketOptions& options) override {
86    packets_sent_++;
87    rtc::Buffer* buffer =
88        new rtc::Buffer(reinterpret_cast<const uint8_t*>(data), len);
89    last_sent_packet_ = buffer->data();
90    last_sent_packet_len_ = len;
91    total_bytes_sent_ += len;
92    sent_packets_.push_back(buffer);
93    return true;
94  }
95  bool SendRtcp(const uint8_t* data, size_t len) override { return false; }
96  int packets_sent_;
97  size_t last_sent_packet_len_;
98  size_t total_bytes_sent_;
99  uint8_t* last_sent_packet_;
100  std::vector<rtc::Buffer*> sent_packets_;
101};
102
103}  // namespace
104
105class MockRtpPacketSender : public RtpPacketSender {
106 public:
107  MockRtpPacketSender() {}
108  virtual ~MockRtpPacketSender() {}
109
110  MOCK_METHOD6(InsertPacket,
111               void(Priority priority,
112                    uint32_t ssrc,
113                    uint16_t sequence_number,
114                    int64_t capture_time_ms,
115                    size_t bytes,
116                    bool retransmission));
117};
118
119class RtpSenderTest : public ::testing::Test {
120 protected:
121  RtpSenderTest()
122      : fake_clock_(kStartTime),
123        mock_paced_sender_(),
124        rtp_sender_(),
125        payload_(kPayload),
126        transport_(),
127        kMarkerBit(true) {
128    EXPECT_CALL(mock_paced_sender_, InsertPacket(_, _, _, _, _, _))
129        .WillRepeatedly(testing::Return());
130  }
131
132  void SetUp() override { SetUpRtpSender(true); }
133
134  void SetUpRtpSender(bool pacer) {
135    rtp_sender_.reset(new RTPSender(false, &fake_clock_, &transport_, nullptr,
136                                    pacer ? &mock_paced_sender_ : nullptr,
137                                    nullptr, nullptr, nullptr, nullptr,
138                                    nullptr));
139    rtp_sender_->SetSequenceNumber(kSeqNum);
140  }
141
142  SimulatedClock fake_clock_;
143  MockRtpPacketSender mock_paced_sender_;
144  rtc::scoped_ptr<RTPSender> rtp_sender_;
145  int payload_;
146  LoopbackTransportTest transport_;
147  const bool kMarkerBit;
148  uint8_t packet_[kMaxPacketLength];
149
150  void VerifyRTPHeaderCommon(const RTPHeader& rtp_header) {
151    VerifyRTPHeaderCommon(rtp_header, kMarkerBit);
152  }
153
154  void VerifyRTPHeaderCommon(const RTPHeader& rtp_header, bool marker_bit) {
155    EXPECT_EQ(marker_bit, rtp_header.markerBit);
156    EXPECT_EQ(payload_, rtp_header.payloadType);
157    EXPECT_EQ(kSeqNum, rtp_header.sequenceNumber);
158    EXPECT_EQ(kTimestamp, rtp_header.timestamp);
159    EXPECT_EQ(rtp_sender_->SSRC(), rtp_header.ssrc);
160    EXPECT_EQ(0, rtp_header.numCSRCs);
161    EXPECT_EQ(0U, rtp_header.paddingLength);
162  }
163
164  void SendPacket(int64_t capture_time_ms, int payload_length) {
165    uint32_t timestamp = capture_time_ms * 90;
166    int32_t rtp_length = rtp_sender_->BuildRTPheader(
167        packet_, kPayload, kMarkerBit, timestamp, capture_time_ms);
168    ASSERT_GE(rtp_length, 0);
169
170    // Packet should be stored in a send bucket.
171    EXPECT_EQ(0, rtp_sender_->SendToNetwork(
172                     packet_, payload_length, rtp_length, capture_time_ms,
173                     kAllowRetransmission, RtpPacketSender::kNormalPriority));
174  }
175};
176
177// TODO(pbos): Move tests over from WithoutPacer to RtpSenderTest as this is our
178// default code path.
179class RtpSenderTestWithoutPacer : public RtpSenderTest {
180 public:
181  void SetUp() override { SetUpRtpSender(false); }
182};
183
184class RtpSenderVideoTest : public RtpSenderTest {
185 protected:
186  void SetUp() override {
187    // TODO(pbos): Set up to use pacer.
188    SetUpRtpSender(false);
189    rtp_sender_video_.reset(
190        new RTPSenderVideo(&fake_clock_, rtp_sender_.get()));
191  }
192  rtc::scoped_ptr<RTPSenderVideo> rtp_sender_video_;
193
194  void VerifyCVOPacket(uint8_t* data,
195                       size_t len,
196                       bool expect_cvo,
197                       RtpHeaderExtensionMap* map,
198                       uint16_t seq_num,
199                       VideoRotation rotation) {
200    webrtc::RtpUtility::RtpHeaderParser rtp_parser(data, len);
201
202    webrtc::RTPHeader rtp_header;
203    size_t length = static_cast<size_t>(rtp_sender_->BuildRTPheader(
204        packet_, kPayload, expect_cvo /* marker_bit */, kTimestamp, 0));
205    if (expect_cvo) {
206      ASSERT_EQ(kRtpHeaderSize + rtp_sender_->RtpHeaderExtensionTotalLength(),
207                length);
208    } else {
209      ASSERT_EQ(kRtpHeaderSize, length);
210    }
211    ASSERT_TRUE(rtp_parser.Parse(&rtp_header, map));
212    ASSERT_FALSE(rtp_parser.RTCP());
213    EXPECT_EQ(payload_, rtp_header.payloadType);
214    EXPECT_EQ(seq_num, rtp_header.sequenceNumber);
215    EXPECT_EQ(kTimestamp, rtp_header.timestamp);
216    EXPECT_EQ(rtp_sender_->SSRC(), rtp_header.ssrc);
217    EXPECT_EQ(0, rtp_header.numCSRCs);
218    EXPECT_EQ(0U, rtp_header.paddingLength);
219    EXPECT_EQ(ConvertVideoRotationToCVOByte(rotation),
220              rtp_header.extension.videoRotation);
221  }
222};
223
224TEST_F(RtpSenderTestWithoutPacer,
225       RegisterRtpTransmissionTimeOffsetHeaderExtension) {
226  EXPECT_EQ(0u, rtp_sender_->RtpHeaderExtensionTotalLength());
227  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
228                   kRtpExtensionTransmissionTimeOffset,
229                   kTransmissionTimeOffsetExtensionId));
230  EXPECT_EQ(kRtpOneByteHeaderLength + kTransmissionTimeOffsetLength,
231            rtp_sender_->RtpHeaderExtensionTotalLength());
232  EXPECT_EQ(0, rtp_sender_->DeregisterRtpHeaderExtension(
233                   kRtpExtensionTransmissionTimeOffset));
234  EXPECT_EQ(0u, rtp_sender_->RtpHeaderExtensionTotalLength());
235}
236
237TEST_F(RtpSenderTestWithoutPacer, RegisterRtpAbsoluteSendTimeHeaderExtension) {
238  EXPECT_EQ(0u, rtp_sender_->RtpHeaderExtensionTotalLength());
239  EXPECT_EQ(
240      0, rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
241                                                 kAbsoluteSendTimeExtensionId));
242  EXPECT_EQ(RtpUtility::Word32Align(kRtpOneByteHeaderLength +
243                                    kAbsoluteSendTimeLength),
244            rtp_sender_->RtpHeaderExtensionTotalLength());
245  EXPECT_EQ(0, rtp_sender_->DeregisterRtpHeaderExtension(
246                   kRtpExtensionAbsoluteSendTime));
247  EXPECT_EQ(0u, rtp_sender_->RtpHeaderExtensionTotalLength());
248}
249
250TEST_F(RtpSenderTestWithoutPacer, RegisterRtpAudioLevelHeaderExtension) {
251  EXPECT_EQ(0u, rtp_sender_->RtpHeaderExtensionTotalLength());
252  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
253                                                       kAudioLevelExtensionId));
254  EXPECT_EQ(
255      RtpUtility::Word32Align(kRtpOneByteHeaderLength + kAudioLevelLength),
256      rtp_sender_->RtpHeaderExtensionTotalLength());
257  EXPECT_EQ(0,
258            rtp_sender_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel));
259  EXPECT_EQ(0u, rtp_sender_->RtpHeaderExtensionTotalLength());
260}
261
262TEST_F(RtpSenderTestWithoutPacer, RegisterRtpHeaderExtensions) {
263  EXPECT_EQ(0u, rtp_sender_->RtpHeaderExtensionTotalLength());
264  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
265                   kRtpExtensionTransmissionTimeOffset,
266                   kTransmissionTimeOffsetExtensionId));
267  EXPECT_EQ(RtpUtility::Word32Align(kRtpOneByteHeaderLength +
268                                    kTransmissionTimeOffsetLength),
269            rtp_sender_->RtpHeaderExtensionTotalLength());
270  EXPECT_EQ(
271      0, rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
272                                                 kAbsoluteSendTimeExtensionId));
273  EXPECT_EQ(RtpUtility::Word32Align(kRtpOneByteHeaderLength +
274                                    kTransmissionTimeOffsetLength +
275                                    kAbsoluteSendTimeLength),
276            rtp_sender_->RtpHeaderExtensionTotalLength());
277  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
278                                                       kAudioLevelExtensionId));
279  EXPECT_EQ(RtpUtility::Word32Align(
280                kRtpOneByteHeaderLength + kTransmissionTimeOffsetLength +
281                kAbsoluteSendTimeLength + kAudioLevelLength),
282            rtp_sender_->RtpHeaderExtensionTotalLength());
283  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
284                   kRtpExtensionVideoRotation, kVideoRotationExtensionId));
285  EXPECT_TRUE(rtp_sender_->ActivateCVORtpHeaderExtension());
286  EXPECT_EQ(RtpUtility::Word32Align(kRtpOneByteHeaderLength +
287                                    kTransmissionTimeOffsetLength +
288                                    kAbsoluteSendTimeLength +
289                                    kAudioLevelLength + kVideoRotationLength),
290            rtp_sender_->RtpHeaderExtensionTotalLength());
291
292  // Deregister starts.
293  EXPECT_EQ(0, rtp_sender_->DeregisterRtpHeaderExtension(
294                   kRtpExtensionTransmissionTimeOffset));
295  EXPECT_EQ(RtpUtility::Word32Align(kRtpOneByteHeaderLength +
296                                    kAbsoluteSendTimeLength +
297                                    kAudioLevelLength + kVideoRotationLength),
298            rtp_sender_->RtpHeaderExtensionTotalLength());
299  EXPECT_EQ(0, rtp_sender_->DeregisterRtpHeaderExtension(
300                   kRtpExtensionAbsoluteSendTime));
301  EXPECT_EQ(RtpUtility::Word32Align(kRtpOneByteHeaderLength +
302                                    kAudioLevelLength + kVideoRotationLength),
303            rtp_sender_->RtpHeaderExtensionTotalLength());
304  EXPECT_EQ(0,
305            rtp_sender_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel));
306  EXPECT_EQ(
307      RtpUtility::Word32Align(kRtpOneByteHeaderLength + kVideoRotationLength),
308      rtp_sender_->RtpHeaderExtensionTotalLength());
309  EXPECT_EQ(
310      0, rtp_sender_->DeregisterRtpHeaderExtension(kRtpExtensionVideoRotation));
311  EXPECT_EQ(0u, rtp_sender_->RtpHeaderExtensionTotalLength());
312}
313
314TEST_F(RtpSenderTestWithoutPacer, RegisterRtpVideoRotationHeaderExtension) {
315  EXPECT_EQ(0u, rtp_sender_->RtpHeaderExtensionTotalLength());
316  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
317                   kRtpExtensionVideoRotation, kVideoRotationExtensionId));
318  EXPECT_EQ(0u, rtp_sender_->RtpHeaderExtensionTotalLength());
319
320  EXPECT_TRUE(rtp_sender_->ActivateCVORtpHeaderExtension());
321  EXPECT_EQ(
322      RtpUtility::Word32Align(kRtpOneByteHeaderLength + kVideoRotationLength),
323      rtp_sender_->RtpHeaderExtensionTotalLength());
324  EXPECT_EQ(
325      0, rtp_sender_->DeregisterRtpHeaderExtension(kRtpExtensionVideoRotation));
326  EXPECT_EQ(0u, rtp_sender_->RtpHeaderExtensionTotalLength());
327}
328
329TEST_F(RtpSenderTestWithoutPacer, BuildRTPPacket) {
330  size_t length = static_cast<size_t>(rtp_sender_->BuildRTPheader(
331      packet_, kPayload, kMarkerBit, kTimestamp, 0));
332  ASSERT_EQ(kRtpHeaderSize, length);
333
334  // Verify
335  webrtc::RtpUtility::RtpHeaderParser rtp_parser(packet_, length);
336  webrtc::RTPHeader rtp_header;
337
338  const bool valid_rtp_header = rtp_parser.Parse(&rtp_header, nullptr);
339
340  ASSERT_TRUE(valid_rtp_header);
341  ASSERT_FALSE(rtp_parser.RTCP());
342  VerifyRTPHeaderCommon(rtp_header);
343  EXPECT_EQ(length, rtp_header.headerLength);
344  EXPECT_FALSE(rtp_header.extension.hasTransmissionTimeOffset);
345  EXPECT_FALSE(rtp_header.extension.hasAbsoluteSendTime);
346  EXPECT_FALSE(rtp_header.extension.hasAudioLevel);
347  EXPECT_EQ(0, rtp_header.extension.transmissionTimeOffset);
348  EXPECT_EQ(0u, rtp_header.extension.absoluteSendTime);
349  EXPECT_FALSE(rtp_header.extension.voiceActivity);
350  EXPECT_EQ(0u, rtp_header.extension.audioLevel);
351  EXPECT_EQ(0u, rtp_header.extension.videoRotation);
352}
353
354TEST_F(RtpSenderTestWithoutPacer,
355       BuildRTPPacketWithTransmissionOffsetExtension) {
356  EXPECT_EQ(0, rtp_sender_->SetTransmissionTimeOffset(kTimeOffset));
357  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
358                   kRtpExtensionTransmissionTimeOffset,
359                   kTransmissionTimeOffsetExtensionId));
360
361  size_t length = static_cast<size_t>(rtp_sender_->BuildRTPheader(
362      packet_, kPayload, kMarkerBit, kTimestamp, 0));
363  ASSERT_EQ(kRtpHeaderSize + rtp_sender_->RtpHeaderExtensionTotalLength(),
364            length);
365
366  // Verify
367  webrtc::RtpUtility::RtpHeaderParser rtp_parser(packet_, length);
368  webrtc::RTPHeader rtp_header;
369
370  RtpHeaderExtensionMap map;
371  map.Register(kRtpExtensionTransmissionTimeOffset,
372               kTransmissionTimeOffsetExtensionId);
373  const bool valid_rtp_header = rtp_parser.Parse(&rtp_header, &map);
374
375  ASSERT_TRUE(valid_rtp_header);
376  ASSERT_FALSE(rtp_parser.RTCP());
377  VerifyRTPHeaderCommon(rtp_header);
378  EXPECT_EQ(length, rtp_header.headerLength);
379  EXPECT_TRUE(rtp_header.extension.hasTransmissionTimeOffset);
380  EXPECT_EQ(kTimeOffset, rtp_header.extension.transmissionTimeOffset);
381
382  // Parse without map extension
383  webrtc::RTPHeader rtp_header2;
384  const bool valid_rtp_header2 = rtp_parser.Parse(&rtp_header2, nullptr);
385
386  ASSERT_TRUE(valid_rtp_header2);
387  VerifyRTPHeaderCommon(rtp_header2);
388  EXPECT_EQ(length, rtp_header2.headerLength);
389  EXPECT_FALSE(rtp_header2.extension.hasTransmissionTimeOffset);
390  EXPECT_EQ(0, rtp_header2.extension.transmissionTimeOffset);
391}
392
393TEST_F(RtpSenderTestWithoutPacer,
394       BuildRTPPacketWithNegativeTransmissionOffsetExtension) {
395  const int kNegTimeOffset = -500;
396  EXPECT_EQ(0, rtp_sender_->SetTransmissionTimeOffset(kNegTimeOffset));
397  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
398                   kRtpExtensionTransmissionTimeOffset,
399                   kTransmissionTimeOffsetExtensionId));
400
401  size_t length = static_cast<size_t>(rtp_sender_->BuildRTPheader(
402      packet_, kPayload, kMarkerBit, kTimestamp, 0));
403  ASSERT_EQ(kRtpHeaderSize + rtp_sender_->RtpHeaderExtensionTotalLength(),
404            length);
405
406  // Verify
407  webrtc::RtpUtility::RtpHeaderParser rtp_parser(packet_, length);
408  webrtc::RTPHeader rtp_header;
409
410  RtpHeaderExtensionMap map;
411  map.Register(kRtpExtensionTransmissionTimeOffset,
412               kTransmissionTimeOffsetExtensionId);
413  const bool valid_rtp_header = rtp_parser.Parse(&rtp_header, &map);
414
415  ASSERT_TRUE(valid_rtp_header);
416  ASSERT_FALSE(rtp_parser.RTCP());
417  VerifyRTPHeaderCommon(rtp_header);
418  EXPECT_EQ(length, rtp_header.headerLength);
419  EXPECT_TRUE(rtp_header.extension.hasTransmissionTimeOffset);
420  EXPECT_EQ(kNegTimeOffset, rtp_header.extension.transmissionTimeOffset);
421}
422
423TEST_F(RtpSenderTestWithoutPacer, BuildRTPPacketWithAbsoluteSendTimeExtension) {
424  EXPECT_EQ(0, rtp_sender_->SetAbsoluteSendTime(kAbsoluteSendTime));
425  EXPECT_EQ(
426      0, rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
427                                                 kAbsoluteSendTimeExtensionId));
428
429  size_t length = static_cast<size_t>(rtp_sender_->BuildRTPheader(
430      packet_, kPayload, kMarkerBit, kTimestamp, 0));
431  ASSERT_EQ(kRtpHeaderSize + rtp_sender_->RtpHeaderExtensionTotalLength(),
432            length);
433
434  // Verify
435  webrtc::RtpUtility::RtpHeaderParser rtp_parser(packet_, length);
436  webrtc::RTPHeader rtp_header;
437
438  RtpHeaderExtensionMap map;
439  map.Register(kRtpExtensionAbsoluteSendTime, kAbsoluteSendTimeExtensionId);
440  const bool valid_rtp_header = rtp_parser.Parse(&rtp_header, &map);
441
442  ASSERT_TRUE(valid_rtp_header);
443  ASSERT_FALSE(rtp_parser.RTCP());
444  VerifyRTPHeaderCommon(rtp_header);
445  EXPECT_EQ(length, rtp_header.headerLength);
446  EXPECT_TRUE(rtp_header.extension.hasAbsoluteSendTime);
447  EXPECT_EQ(kAbsoluteSendTime, rtp_header.extension.absoluteSendTime);
448
449  // Parse without map extension
450  webrtc::RTPHeader rtp_header2;
451  const bool valid_rtp_header2 = rtp_parser.Parse(&rtp_header2, nullptr);
452
453  ASSERT_TRUE(valid_rtp_header2);
454  VerifyRTPHeaderCommon(rtp_header2);
455  EXPECT_EQ(length, rtp_header2.headerLength);
456  EXPECT_FALSE(rtp_header2.extension.hasAbsoluteSendTime);
457  EXPECT_EQ(0u, rtp_header2.extension.absoluteSendTime);
458}
459
460// Test CVO header extension is only set when marker bit is true.
461TEST_F(RtpSenderTestWithoutPacer, BuildRTPPacketWithVideoRotation_MarkerBit) {
462  rtp_sender_->SetVideoRotation(kRotation);
463  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
464                   kRtpExtensionVideoRotation, kVideoRotationExtensionId));
465  EXPECT_TRUE(rtp_sender_->ActivateCVORtpHeaderExtension());
466
467  RtpHeaderExtensionMap map;
468  map.Register(kRtpExtensionVideoRotation, kVideoRotationExtensionId);
469
470  size_t length = static_cast<size_t>(
471      rtp_sender_->BuildRTPheader(packet_, kPayload, true, kTimestamp, 0));
472  ASSERT_EQ(kRtpHeaderSize + rtp_sender_->RtpHeaderExtensionTotalLength(),
473            length);
474
475  // Verify
476  webrtc::RtpUtility::RtpHeaderParser rtp_parser(packet_, length);
477  webrtc::RTPHeader rtp_header;
478
479  ASSERT_TRUE(rtp_parser.Parse(&rtp_header, &map));
480  ASSERT_FALSE(rtp_parser.RTCP());
481  VerifyRTPHeaderCommon(rtp_header);
482  EXPECT_EQ(length, rtp_header.headerLength);
483  EXPECT_TRUE(rtp_header.extension.hasVideoRotation);
484  EXPECT_EQ(ConvertVideoRotationToCVOByte(kRotation),
485            rtp_header.extension.videoRotation);
486}
487
488// Test CVO header extension is not set when marker bit is false.
489TEST_F(RtpSenderTestWithoutPacer,
490       DISABLED_BuildRTPPacketWithVideoRotation_NoMarkerBit) {
491  rtp_sender_->SetVideoRotation(kRotation);
492  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
493                   kRtpExtensionVideoRotation, kVideoRotationExtensionId));
494  EXPECT_TRUE(rtp_sender_->ActivateCVORtpHeaderExtension());
495
496  RtpHeaderExtensionMap map;
497  map.Register(kRtpExtensionVideoRotation, kVideoRotationExtensionId);
498
499  size_t length = static_cast<size_t>(
500      rtp_sender_->BuildRTPheader(packet_, kPayload, false, kTimestamp, 0));
501  ASSERT_EQ(kRtpHeaderSize, length);
502
503  // Verify
504  webrtc::RtpUtility::RtpHeaderParser rtp_parser(packet_, length);
505  webrtc::RTPHeader rtp_header;
506
507  ASSERT_TRUE(rtp_parser.Parse(&rtp_header, &map));
508  ASSERT_FALSE(rtp_parser.RTCP());
509  VerifyRTPHeaderCommon(rtp_header, false);
510  EXPECT_EQ(length, rtp_header.headerLength);
511  EXPECT_FALSE(rtp_header.extension.hasVideoRotation);
512}
513
514TEST_F(RtpSenderTestWithoutPacer, BuildRTPPacketWithAudioLevelExtension) {
515  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
516                                                       kAudioLevelExtensionId));
517
518  size_t length = static_cast<size_t>(rtp_sender_->BuildRTPheader(
519      packet_, kPayload, kMarkerBit, kTimestamp, 0));
520  ASSERT_EQ(kRtpHeaderSize + rtp_sender_->RtpHeaderExtensionTotalLength(),
521            length);
522
523  // Verify
524  webrtc::RtpUtility::RtpHeaderParser rtp_parser(packet_, length);
525  webrtc::RTPHeader rtp_header;
526
527  // Updating audio level is done in RTPSenderAudio, so simulate it here.
528  rtp_parser.Parse(&rtp_header);
529  rtp_sender_->UpdateAudioLevel(packet_, length, rtp_header, true, kAudioLevel);
530
531  RtpHeaderExtensionMap map;
532  map.Register(kRtpExtensionAudioLevel, kAudioLevelExtensionId);
533  const bool valid_rtp_header = rtp_parser.Parse(&rtp_header, &map);
534
535  ASSERT_TRUE(valid_rtp_header);
536  ASSERT_FALSE(rtp_parser.RTCP());
537  VerifyRTPHeaderCommon(rtp_header);
538  EXPECT_EQ(length, rtp_header.headerLength);
539  EXPECT_TRUE(rtp_header.extension.hasAudioLevel);
540  EXPECT_TRUE(rtp_header.extension.voiceActivity);
541  EXPECT_EQ(kAudioLevel, rtp_header.extension.audioLevel);
542
543  // Parse without map extension
544  webrtc::RTPHeader rtp_header2;
545  const bool valid_rtp_header2 = rtp_parser.Parse(&rtp_header2, nullptr);
546
547  ASSERT_TRUE(valid_rtp_header2);
548  VerifyRTPHeaderCommon(rtp_header2);
549  EXPECT_EQ(length, rtp_header2.headerLength);
550  EXPECT_FALSE(rtp_header2.extension.hasAudioLevel);
551  EXPECT_FALSE(rtp_header2.extension.voiceActivity);
552  EXPECT_EQ(0u, rtp_header2.extension.audioLevel);
553}
554
555TEST_F(RtpSenderTestWithoutPacer, BuildRTPPacketWithHeaderExtensions) {
556  EXPECT_EQ(0, rtp_sender_->SetTransmissionTimeOffset(kTimeOffset));
557  EXPECT_EQ(0, rtp_sender_->SetAbsoluteSendTime(kAbsoluteSendTime));
558  EXPECT_EQ(0,
559            rtp_sender_->SetTransportSequenceNumber(kTransportSequenceNumber));
560  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
561                   kRtpExtensionTransmissionTimeOffset,
562                   kTransmissionTimeOffsetExtensionId));
563  EXPECT_EQ(
564      0, rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
565                                                 kAbsoluteSendTimeExtensionId));
566  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
567                                                       kAudioLevelExtensionId));
568  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
569                   kRtpExtensionTransportSequenceNumber,
570                   kTransportSequenceNumberExtensionId));
571
572  size_t length = static_cast<size_t>(rtp_sender_->BuildRTPheader(
573      packet_, kPayload, kMarkerBit, kTimestamp, 0));
574  ASSERT_EQ(kRtpHeaderSize + rtp_sender_->RtpHeaderExtensionTotalLength(),
575            length);
576
577  // Verify
578  webrtc::RtpUtility::RtpHeaderParser rtp_parser(packet_, length);
579  webrtc::RTPHeader rtp_header;
580
581  // Updating audio level is done in RTPSenderAudio, so simulate it here.
582  rtp_parser.Parse(&rtp_header);
583  rtp_sender_->UpdateAudioLevel(packet_, length, rtp_header, true, kAudioLevel);
584
585  RtpHeaderExtensionMap map;
586  map.Register(kRtpExtensionTransmissionTimeOffset,
587               kTransmissionTimeOffsetExtensionId);
588  map.Register(kRtpExtensionAbsoluteSendTime, kAbsoluteSendTimeExtensionId);
589  map.Register(kRtpExtensionAudioLevel, kAudioLevelExtensionId);
590  map.Register(kRtpExtensionTransportSequenceNumber,
591               kTransportSequenceNumberExtensionId);
592  const bool valid_rtp_header = rtp_parser.Parse(&rtp_header, &map);
593
594  ASSERT_TRUE(valid_rtp_header);
595  ASSERT_FALSE(rtp_parser.RTCP());
596  VerifyRTPHeaderCommon(rtp_header);
597  EXPECT_EQ(length, rtp_header.headerLength);
598  EXPECT_TRUE(rtp_header.extension.hasTransmissionTimeOffset);
599  EXPECT_TRUE(rtp_header.extension.hasAbsoluteSendTime);
600  EXPECT_TRUE(rtp_header.extension.hasAudioLevel);
601  EXPECT_TRUE(rtp_header.extension.hasTransportSequenceNumber);
602  EXPECT_EQ(kTimeOffset, rtp_header.extension.transmissionTimeOffset);
603  EXPECT_EQ(kAbsoluteSendTime, rtp_header.extension.absoluteSendTime);
604  EXPECT_TRUE(rtp_header.extension.voiceActivity);
605  EXPECT_EQ(kAudioLevel, rtp_header.extension.audioLevel);
606  EXPECT_EQ(kTransportSequenceNumber,
607            rtp_header.extension.transportSequenceNumber);
608
609  // Parse without map extension
610  webrtc::RTPHeader rtp_header2;
611  const bool valid_rtp_header2 = rtp_parser.Parse(&rtp_header2, nullptr);
612
613  ASSERT_TRUE(valid_rtp_header2);
614  VerifyRTPHeaderCommon(rtp_header2);
615  EXPECT_EQ(length, rtp_header2.headerLength);
616  EXPECT_FALSE(rtp_header2.extension.hasTransmissionTimeOffset);
617  EXPECT_FALSE(rtp_header2.extension.hasAbsoluteSendTime);
618  EXPECT_FALSE(rtp_header2.extension.hasAudioLevel);
619  EXPECT_FALSE(rtp_header2.extension.hasTransportSequenceNumber);
620
621  EXPECT_EQ(0, rtp_header2.extension.transmissionTimeOffset);
622  EXPECT_EQ(0u, rtp_header2.extension.absoluteSendTime);
623  EXPECT_FALSE(rtp_header2.extension.voiceActivity);
624  EXPECT_EQ(0u, rtp_header2.extension.audioLevel);
625  EXPECT_EQ(0u, rtp_header2.extension.transportSequenceNumber);
626}
627
628TEST_F(RtpSenderTest, TrafficSmoothingWithExtensions) {
629  EXPECT_CALL(mock_paced_sender_, InsertPacket(RtpPacketSender::kNormalPriority,
630                                               _, kSeqNum, _, _, _))
631      .WillRepeatedly(testing::Return());
632
633  rtp_sender_->SetStorePacketsStatus(true, 10);
634  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
635                   kRtpExtensionTransmissionTimeOffset,
636                   kTransmissionTimeOffsetExtensionId));
637  EXPECT_EQ(
638      0, rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
639                                                 kAbsoluteSendTimeExtensionId));
640  rtp_sender_->SetTargetBitrate(300000);
641  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
642  int rtp_length_int = rtp_sender_->BuildRTPheader(
643      packet_, kPayload, kMarkerBit, kTimestamp, capture_time_ms);
644  ASSERT_NE(-1, rtp_length_int);
645  size_t rtp_length = static_cast<size_t>(rtp_length_int);
646
647  // Packet should be stored in a send bucket.
648  EXPECT_EQ(0, rtp_sender_->SendToNetwork(packet_, 0, rtp_length,
649                                          capture_time_ms, kAllowRetransmission,
650                                          RtpPacketSender::kNormalPriority));
651
652  EXPECT_EQ(0, transport_.packets_sent_);
653
654  const int kStoredTimeInMs = 100;
655  fake_clock_.AdvanceTimeMilliseconds(kStoredTimeInMs);
656
657  rtp_sender_->TimeToSendPacket(kSeqNum, capture_time_ms, false);
658
659  // Process send bucket. Packet should now be sent.
660  EXPECT_EQ(1, transport_.packets_sent_);
661  EXPECT_EQ(rtp_length, transport_.last_sent_packet_len_);
662  // Parse sent packet.
663  webrtc::RtpUtility::RtpHeaderParser rtp_parser(transport_.last_sent_packet_,
664                                                 rtp_length);
665  webrtc::RTPHeader rtp_header;
666  RtpHeaderExtensionMap map;
667  map.Register(kRtpExtensionTransmissionTimeOffset,
668               kTransmissionTimeOffsetExtensionId);
669  map.Register(kRtpExtensionAbsoluteSendTime, kAbsoluteSendTimeExtensionId);
670  const bool valid_rtp_header = rtp_parser.Parse(&rtp_header, &map);
671  ASSERT_TRUE(valid_rtp_header);
672
673  // Verify transmission time offset.
674  EXPECT_EQ(kStoredTimeInMs * 90, rtp_header.extension.transmissionTimeOffset);
675  uint64_t expected_send_time =
676      ConvertMsToAbsSendTime(fake_clock_.TimeInMilliseconds());
677  EXPECT_EQ(expected_send_time, rtp_header.extension.absoluteSendTime);
678}
679
680TEST_F(RtpSenderTest, TrafficSmoothingRetransmits) {
681  EXPECT_CALL(mock_paced_sender_, InsertPacket(RtpPacketSender::kNormalPriority,
682                                               _, kSeqNum, _, _, _))
683      .WillRepeatedly(testing::Return());
684
685  rtp_sender_->SetStorePacketsStatus(true, 10);
686  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
687                   kRtpExtensionTransmissionTimeOffset,
688                   kTransmissionTimeOffsetExtensionId));
689  EXPECT_EQ(
690      0, rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
691                                                 kAbsoluteSendTimeExtensionId));
692  rtp_sender_->SetTargetBitrate(300000);
693  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
694  int rtp_length_int = rtp_sender_->BuildRTPheader(
695      packet_, kPayload, kMarkerBit, kTimestamp, capture_time_ms);
696  ASSERT_NE(-1, rtp_length_int);
697  size_t rtp_length = static_cast<size_t>(rtp_length_int);
698
699  // Packet should be stored in a send bucket.
700  EXPECT_EQ(0, rtp_sender_->SendToNetwork(packet_, 0, rtp_length,
701                                          capture_time_ms, kAllowRetransmission,
702                                          RtpPacketSender::kNormalPriority));
703
704  EXPECT_EQ(0, transport_.packets_sent_);
705
706  EXPECT_CALL(mock_paced_sender_,
707              InsertPacket(RtpPacketSender::kHighPriority, _, kSeqNum, _, _, _))
708      .WillRepeatedly(testing::Return());
709
710  const int kStoredTimeInMs = 100;
711  fake_clock_.AdvanceTimeMilliseconds(kStoredTimeInMs);
712
713  EXPECT_EQ(rtp_length_int, rtp_sender_->ReSendPacket(kSeqNum));
714  EXPECT_EQ(0, transport_.packets_sent_);
715
716  rtp_sender_->TimeToSendPacket(kSeqNum, capture_time_ms, false);
717
718  // Process send bucket. Packet should now be sent.
719  EXPECT_EQ(1, transport_.packets_sent_);
720  EXPECT_EQ(rtp_length, transport_.last_sent_packet_len_);
721
722  // Parse sent packet.
723  webrtc::RtpUtility::RtpHeaderParser rtp_parser(transport_.last_sent_packet_,
724                                                 rtp_length);
725  webrtc::RTPHeader rtp_header;
726  RtpHeaderExtensionMap map;
727  map.Register(kRtpExtensionTransmissionTimeOffset,
728               kTransmissionTimeOffsetExtensionId);
729  map.Register(kRtpExtensionAbsoluteSendTime, kAbsoluteSendTimeExtensionId);
730  const bool valid_rtp_header = rtp_parser.Parse(&rtp_header, &map);
731  ASSERT_TRUE(valid_rtp_header);
732
733  // Verify transmission time offset.
734  EXPECT_EQ(kStoredTimeInMs * 90, rtp_header.extension.transmissionTimeOffset);
735  uint64_t expected_send_time =
736      ConvertMsToAbsSendTime(fake_clock_.TimeInMilliseconds());
737  EXPECT_EQ(expected_send_time, rtp_header.extension.absoluteSendTime);
738}
739
740// This test sends 1 regular video packet, then 4 padding packets, and then
741// 1 more regular packet.
742TEST_F(RtpSenderTest, SendPadding) {
743  // Make all (non-padding) packets go to send queue.
744  EXPECT_CALL(mock_paced_sender_,
745              InsertPacket(RtpPacketSender::kNormalPriority, _, _, _, _, _))
746      .WillRepeatedly(testing::Return());
747
748  uint16_t seq_num = kSeqNum;
749  uint32_t timestamp = kTimestamp;
750  rtp_sender_->SetStorePacketsStatus(true, 10);
751  size_t rtp_header_len = kRtpHeaderSize;
752  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
753                   kRtpExtensionTransmissionTimeOffset,
754                   kTransmissionTimeOffsetExtensionId));
755  rtp_header_len += 4;  // 4 bytes extension.
756  EXPECT_EQ(
757      0, rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
758                                                 kAbsoluteSendTimeExtensionId));
759  rtp_header_len += 4;  // 4 bytes extension.
760  rtp_header_len += 4;  // 4 extra bytes common to all extension headers.
761
762  // Create and set up parser.
763  rtc::scoped_ptr<webrtc::RtpHeaderParser> rtp_parser(
764      webrtc::RtpHeaderParser::Create());
765  ASSERT_TRUE(rtp_parser.get() != nullptr);
766  rtp_parser->RegisterRtpHeaderExtension(kRtpExtensionTransmissionTimeOffset,
767                                         kTransmissionTimeOffsetExtensionId);
768  rtp_parser->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
769                                         kAbsoluteSendTimeExtensionId);
770  webrtc::RTPHeader rtp_header;
771
772  rtp_sender_->SetTargetBitrate(300000);
773  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
774  int rtp_length_int = rtp_sender_->BuildRTPheader(
775      packet_, kPayload, kMarkerBit, timestamp, capture_time_ms);
776  const uint32_t media_packet_timestamp = timestamp;
777  ASSERT_NE(-1, rtp_length_int);
778  size_t rtp_length = static_cast<size_t>(rtp_length_int);
779
780  // Packet should be stored in a send bucket.
781  EXPECT_EQ(0, rtp_sender_->SendToNetwork(packet_, 0, rtp_length,
782                                          capture_time_ms, kAllowRetransmission,
783                                          RtpPacketSender::kNormalPriority));
784
785  int total_packets_sent = 0;
786  EXPECT_EQ(total_packets_sent, transport_.packets_sent_);
787
788  const int kStoredTimeInMs = 100;
789  fake_clock_.AdvanceTimeMilliseconds(kStoredTimeInMs);
790  rtp_sender_->TimeToSendPacket(seq_num++, capture_time_ms, false);
791  // Packet should now be sent. This test doesn't verify the regular video
792  // packet, since it is tested in another test.
793  EXPECT_EQ(++total_packets_sent, transport_.packets_sent_);
794  timestamp += 90 * kStoredTimeInMs;
795
796  // Send padding 4 times, waiting 50 ms between each.
797  for (int i = 0; i < 4; ++i) {
798    const int kPaddingPeriodMs = 50;
799    const size_t kPaddingBytes = 100;
800    const size_t kMaxPaddingLength = 224;  // Value taken from rtp_sender.cc.
801    // Padding will be forced to full packets.
802    EXPECT_EQ(kMaxPaddingLength, rtp_sender_->TimeToSendPadding(kPaddingBytes));
803
804    // Process send bucket. Padding should now be sent.
805    EXPECT_EQ(++total_packets_sent, transport_.packets_sent_);
806    EXPECT_EQ(kMaxPaddingLength + rtp_header_len,
807              transport_.last_sent_packet_len_);
808    // Parse sent packet.
809    ASSERT_TRUE(rtp_parser->Parse(transport_.last_sent_packet_,
810                                  transport_.last_sent_packet_len_,
811                                  &rtp_header));
812    EXPECT_EQ(kMaxPaddingLength, rtp_header.paddingLength);
813
814    // Verify sequence number and timestamp. The timestamp should be the same
815    // as the last media packet.
816    EXPECT_EQ(seq_num++, rtp_header.sequenceNumber);
817    EXPECT_EQ(media_packet_timestamp, rtp_header.timestamp);
818    // Verify transmission time offset.
819    int offset = timestamp - media_packet_timestamp;
820    EXPECT_EQ(offset, rtp_header.extension.transmissionTimeOffset);
821    uint64_t expected_send_time =
822        ConvertMsToAbsSendTime(fake_clock_.TimeInMilliseconds());
823    EXPECT_EQ(expected_send_time, rtp_header.extension.absoluteSendTime);
824    fake_clock_.AdvanceTimeMilliseconds(kPaddingPeriodMs);
825    timestamp += 90 * kPaddingPeriodMs;
826  }
827
828  // Send a regular video packet again.
829  capture_time_ms = fake_clock_.TimeInMilliseconds();
830  rtp_length_int = rtp_sender_->BuildRTPheader(packet_, kPayload, kMarkerBit,
831                                               timestamp, capture_time_ms);
832  ASSERT_NE(-1, rtp_length_int);
833  rtp_length = static_cast<size_t>(rtp_length_int);
834
835  // Packet should be stored in a send bucket.
836  EXPECT_EQ(0, rtp_sender_->SendToNetwork(packet_, 0, rtp_length,
837                                          capture_time_ms, kAllowRetransmission,
838                                          RtpPacketSender::kNormalPriority));
839
840  rtp_sender_->TimeToSendPacket(seq_num, capture_time_ms, false);
841  // Process send bucket.
842  EXPECT_EQ(++total_packets_sent, transport_.packets_sent_);
843  EXPECT_EQ(rtp_length, transport_.last_sent_packet_len_);
844  // Parse sent packet.
845  ASSERT_TRUE(
846      rtp_parser->Parse(transport_.last_sent_packet_, rtp_length, &rtp_header));
847
848  // Verify sequence number and timestamp.
849  EXPECT_EQ(seq_num, rtp_header.sequenceNumber);
850  EXPECT_EQ(timestamp, rtp_header.timestamp);
851  // Verify transmission time offset. This packet is sent without delay.
852  EXPECT_EQ(0, rtp_header.extension.transmissionTimeOffset);
853  uint64_t expected_send_time =
854      ConvertMsToAbsSendTime(fake_clock_.TimeInMilliseconds());
855  EXPECT_EQ(expected_send_time, rtp_header.extension.absoluteSendTime);
856}
857
858TEST_F(RtpSenderTest, SendRedundantPayloads) {
859  MockTransport transport;
860  rtp_sender_.reset(new RTPSender(false, &fake_clock_, &transport, nullptr,
861                                  &mock_paced_sender_, nullptr, nullptr,
862                                  nullptr, nullptr, nullptr));
863  rtp_sender_->SetSequenceNumber(kSeqNum);
864  rtp_sender_->SetRtxPayloadType(kRtxPayload, kPayload);
865  // Make all packets go through the pacer.
866  EXPECT_CALL(mock_paced_sender_,
867              InsertPacket(RtpPacketSender::kNormalPriority, _, _, _, _, _))
868      .WillRepeatedly(testing::Return());
869
870  uint16_t seq_num = kSeqNum;
871  rtp_sender_->SetStorePacketsStatus(true, 10);
872  int32_t rtp_header_len = kRtpHeaderSize;
873  EXPECT_EQ(
874      0, rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
875                                                 kAbsoluteSendTimeExtensionId));
876  rtp_header_len += 4;  // 4 bytes extension.
877  rtp_header_len += 4;  // 4 extra bytes common to all extension headers.
878
879  rtp_sender_->SetRtxStatus(kRtxRetransmitted | kRtxRedundantPayloads);
880  rtp_sender_->SetRtxSsrc(1234);
881
882  // Create and set up parser.
883  rtc::scoped_ptr<webrtc::RtpHeaderParser> rtp_parser(
884      webrtc::RtpHeaderParser::Create());
885  ASSERT_TRUE(rtp_parser.get() != nullptr);
886  rtp_parser->RegisterRtpHeaderExtension(kRtpExtensionTransmissionTimeOffset,
887                                         kTransmissionTimeOffsetExtensionId);
888  rtp_parser->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
889                                         kAbsoluteSendTimeExtensionId);
890  rtp_sender_->SetTargetBitrate(300000);
891  const size_t kNumPayloadSizes = 10;
892  const size_t kPayloadSizes[kNumPayloadSizes] = {500, 550, 600, 650, 700,
893                                                  750, 800, 850, 900, 950};
894  // Send 10 packets of increasing size.
895  for (size_t i = 0; i < kNumPayloadSizes; ++i) {
896    int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
897    EXPECT_CALL(transport, SendRtp(_, _, _)).WillOnce(testing::Return(true));
898    SendPacket(capture_time_ms, kPayloadSizes[i]);
899    rtp_sender_->TimeToSendPacket(seq_num++, capture_time_ms, false);
900    fake_clock_.AdvanceTimeMilliseconds(33);
901  }
902  // The amount of padding to send it too small to send a payload packet.
903  EXPECT_CALL(transport, SendRtp(_, kMaxPaddingSize + rtp_header_len, _))
904      .WillOnce(testing::Return(true));
905  EXPECT_EQ(kMaxPaddingSize, rtp_sender_->TimeToSendPadding(49));
906
907  EXPECT_CALL(transport,
908              SendRtp(_, kPayloadSizes[0] + rtp_header_len + kRtxHeaderSize, _))
909      .WillOnce(testing::Return(true));
910  EXPECT_EQ(kPayloadSizes[0], rtp_sender_->TimeToSendPadding(500));
911
912  EXPECT_CALL(transport, SendRtp(_, kPayloadSizes[kNumPayloadSizes - 1] +
913                                        rtp_header_len + kRtxHeaderSize,
914                                 _))
915      .WillOnce(testing::Return(true));
916  EXPECT_CALL(transport, SendRtp(_, kMaxPaddingSize + rtp_header_len, _))
917      .WillOnce(testing::Return(true));
918  EXPECT_EQ(kPayloadSizes[kNumPayloadSizes - 1] + kMaxPaddingSize,
919            rtp_sender_->TimeToSendPadding(999));
920}
921
922TEST_F(RtpSenderTestWithoutPacer, SendGenericVideo) {
923  char payload_name[RTP_PAYLOAD_NAME_SIZE] = "GENERIC";
924  const uint8_t payload_type = 127;
925  ASSERT_EQ(0, rtp_sender_->RegisterPayload(payload_name, payload_type, 90000,
926                                            0, 1500));
927  uint8_t payload[] = {47, 11, 32, 93, 89};
928
929  // Send keyframe
930  ASSERT_EQ(
931      0, rtp_sender_->SendOutgoingData(kVideoFrameKey, payload_type, 1234, 4321,
932                                       payload, sizeof(payload), nullptr));
933
934  RtpUtility::RtpHeaderParser rtp_parser(transport_.last_sent_packet_,
935                                         transport_.last_sent_packet_len_);
936  webrtc::RTPHeader rtp_header;
937  ASSERT_TRUE(rtp_parser.Parse(&rtp_header));
938
939  const uint8_t* payload_data =
940      GetPayloadData(rtp_header, transport_.last_sent_packet_);
941  uint8_t generic_header = *payload_data++;
942
943  ASSERT_EQ(sizeof(payload) + sizeof(generic_header),
944            GetPayloadDataLength(rtp_header, transport_.last_sent_packet_len_));
945
946  EXPECT_TRUE(generic_header & RtpFormatVideoGeneric::kKeyFrameBit);
947  EXPECT_TRUE(generic_header & RtpFormatVideoGeneric::kFirstPacketBit);
948
949  EXPECT_EQ(0, memcmp(payload, payload_data, sizeof(payload)));
950
951  // Send delta frame
952  payload[0] = 13;
953  payload[1] = 42;
954  payload[4] = 13;
955
956  ASSERT_EQ(0, rtp_sender_->SendOutgoingData(kVideoFrameDelta, payload_type,
957                                             1234, 4321, payload,
958                                             sizeof(payload), nullptr));
959
960  RtpUtility::RtpHeaderParser rtp_parser2(transport_.last_sent_packet_,
961                                          transport_.last_sent_packet_len_);
962  ASSERT_TRUE(rtp_parser.Parse(&rtp_header));
963
964  payload_data = GetPayloadData(rtp_header, transport_.last_sent_packet_);
965  generic_header = *payload_data++;
966
967  EXPECT_FALSE(generic_header & RtpFormatVideoGeneric::kKeyFrameBit);
968  EXPECT_TRUE(generic_header & RtpFormatVideoGeneric::kFirstPacketBit);
969
970  ASSERT_EQ(sizeof(payload) + sizeof(generic_header),
971            GetPayloadDataLength(rtp_header, transport_.last_sent_packet_len_));
972
973  EXPECT_EQ(0, memcmp(payload, payload_data, sizeof(payload)));
974}
975
976TEST_F(RtpSenderTest, FrameCountCallbacks) {
977  class TestCallback : public FrameCountObserver {
978   public:
979    TestCallback() : FrameCountObserver(), num_calls_(0), ssrc_(0) {}
980    virtual ~TestCallback() {}
981
982    void FrameCountUpdated(const FrameCounts& frame_counts,
983                           uint32_t ssrc) override {
984      ++num_calls_;
985      ssrc_ = ssrc;
986      frame_counts_ = frame_counts;
987    }
988
989    uint32_t num_calls_;
990    uint32_t ssrc_;
991    FrameCounts frame_counts_;
992  } callback;
993
994  rtp_sender_.reset(new RTPSender(false, &fake_clock_, &transport_, nullptr,
995                                  &mock_paced_sender_, nullptr, nullptr,
996                                  nullptr, &callback, nullptr));
997
998  char payload_name[RTP_PAYLOAD_NAME_SIZE] = "GENERIC";
999  const uint8_t payload_type = 127;
1000  ASSERT_EQ(0, rtp_sender_->RegisterPayload(payload_name, payload_type, 90000,
1001                                            0, 1500));
1002  uint8_t payload[] = {47, 11, 32, 93, 89};
1003  rtp_sender_->SetStorePacketsStatus(true, 1);
1004  uint32_t ssrc = rtp_sender_->SSRC();
1005
1006  ASSERT_EQ(
1007      0, rtp_sender_->SendOutgoingData(kVideoFrameKey, payload_type, 1234, 4321,
1008                                       payload, sizeof(payload), nullptr));
1009
1010  EXPECT_EQ(1U, callback.num_calls_);
1011  EXPECT_EQ(ssrc, callback.ssrc_);
1012  EXPECT_EQ(1, callback.frame_counts_.key_frames);
1013  EXPECT_EQ(0, callback.frame_counts_.delta_frames);
1014
1015  ASSERT_EQ(0, rtp_sender_->SendOutgoingData(kVideoFrameDelta, payload_type,
1016                                             1234, 4321, payload,
1017                                             sizeof(payload), nullptr));
1018
1019  EXPECT_EQ(2U, callback.num_calls_);
1020  EXPECT_EQ(ssrc, callback.ssrc_);
1021  EXPECT_EQ(1, callback.frame_counts_.key_frames);
1022  EXPECT_EQ(1, callback.frame_counts_.delta_frames);
1023
1024  rtp_sender_.reset();
1025}
1026
1027TEST_F(RtpSenderTest, BitrateCallbacks) {
1028  class TestCallback : public BitrateStatisticsObserver {
1029   public:
1030    TestCallback() : BitrateStatisticsObserver(), num_calls_(0), ssrc_(0) {}
1031    virtual ~TestCallback() {}
1032
1033    void Notify(const BitrateStatistics& total_stats,
1034                const BitrateStatistics& retransmit_stats,
1035                uint32_t ssrc) override {
1036      ++num_calls_;
1037      ssrc_ = ssrc;
1038      total_stats_ = total_stats;
1039      retransmit_stats_ = retransmit_stats;
1040    }
1041
1042    uint32_t num_calls_;
1043    uint32_t ssrc_;
1044    BitrateStatistics total_stats_;
1045    BitrateStatistics retransmit_stats_;
1046  } callback;
1047  rtp_sender_.reset(new RTPSender(false, &fake_clock_, &transport_, nullptr,
1048                                  nullptr, nullptr, nullptr, &callback, nullptr,
1049                                  nullptr));
1050
1051  // Simulate kNumPackets sent with kPacketInterval ms intervals.
1052  const uint32_t kNumPackets = 15;
1053  const uint32_t kPacketInterval = 20;
1054  // Overhead = 12 bytes RTP header + 1 byte generic header.
1055  const uint32_t kPacketOverhead = 13;
1056
1057  char payload_name[RTP_PAYLOAD_NAME_SIZE] = "GENERIC";
1058  const uint8_t payload_type = 127;
1059  ASSERT_EQ(0, rtp_sender_->RegisterPayload(payload_name, payload_type, 90000,
1060                                            0, 1500));
1061  uint8_t payload[] = {47, 11, 32, 93, 89};
1062  rtp_sender_->SetStorePacketsStatus(true, 1);
1063  uint32_t ssrc = rtp_sender_->SSRC();
1064
1065  // Initial process call so we get a new time window.
1066  rtp_sender_->ProcessBitrate();
1067  uint64_t start_time = fake_clock_.CurrentNtpInMilliseconds();
1068
1069  // Send a few frames.
1070  for (uint32_t i = 0; i < kNumPackets; ++i) {
1071    ASSERT_EQ(0,
1072              rtp_sender_->SendOutgoingData(kVideoFrameKey, payload_type, 1234,
1073                                            4321, payload, sizeof(payload), 0));
1074    fake_clock_.AdvanceTimeMilliseconds(kPacketInterval);
1075  }
1076
1077  rtp_sender_->ProcessBitrate();
1078
1079  const uint32_t expected_packet_rate = 1000 / kPacketInterval;
1080
1081  // We get one call for every stats updated, thus two calls since both the
1082  // stream stats and the retransmit stats are updated once.
1083  EXPECT_EQ(2u, callback.num_calls_);
1084  EXPECT_EQ(ssrc, callback.ssrc_);
1085  EXPECT_EQ(start_time + (kNumPackets * kPacketInterval),
1086            callback.total_stats_.timestamp_ms);
1087  EXPECT_EQ(expected_packet_rate, callback.total_stats_.packet_rate);
1088  EXPECT_EQ((kPacketOverhead + sizeof(payload)) * 8 * expected_packet_rate,
1089            callback.total_stats_.bitrate_bps);
1090
1091  rtp_sender_.reset();
1092}
1093
1094class RtpSenderAudioTest : public RtpSenderTest {
1095 protected:
1096  RtpSenderAudioTest() {}
1097
1098  void SetUp() override {
1099    payload_ = kAudioPayload;
1100    rtp_sender_.reset(new RTPSender(true, &fake_clock_, &transport_, nullptr,
1101                                    nullptr, nullptr, nullptr, nullptr, nullptr,
1102                                    nullptr));
1103    rtp_sender_->SetSequenceNumber(kSeqNum);
1104  }
1105};
1106
1107TEST_F(RtpSenderTestWithoutPacer, StreamDataCountersCallbacks) {
1108  class TestCallback : public StreamDataCountersCallback {
1109   public:
1110    TestCallback() : StreamDataCountersCallback(), ssrc_(0), counters_() {}
1111    virtual ~TestCallback() {}
1112
1113    void DataCountersUpdated(const StreamDataCounters& counters,
1114                             uint32_t ssrc) override {
1115      ssrc_ = ssrc;
1116      counters_ = counters;
1117    }
1118
1119    uint32_t ssrc_;
1120    StreamDataCounters counters_;
1121
1122    void MatchPacketCounter(const RtpPacketCounter& expected,
1123                            const RtpPacketCounter& actual) {
1124      EXPECT_EQ(expected.payload_bytes, actual.payload_bytes);
1125      EXPECT_EQ(expected.header_bytes, actual.header_bytes);
1126      EXPECT_EQ(expected.padding_bytes, actual.padding_bytes);
1127      EXPECT_EQ(expected.packets, actual.packets);
1128    }
1129
1130    void Matches(uint32_t ssrc, const StreamDataCounters& counters) {
1131      EXPECT_EQ(ssrc, ssrc_);
1132      MatchPacketCounter(counters.transmitted, counters_.transmitted);
1133      MatchPacketCounter(counters.retransmitted, counters_.retransmitted);
1134      EXPECT_EQ(counters.fec.packets, counters_.fec.packets);
1135    }
1136  } callback;
1137
1138  const uint8_t kRedPayloadType = 96;
1139  const uint8_t kUlpfecPayloadType = 97;
1140  char payload_name[RTP_PAYLOAD_NAME_SIZE] = "GENERIC";
1141  const uint8_t payload_type = 127;
1142  ASSERT_EQ(0, rtp_sender_->RegisterPayload(payload_name, payload_type, 90000,
1143                                            0, 1500));
1144  uint8_t payload[] = {47, 11, 32, 93, 89};
1145  rtp_sender_->SetStorePacketsStatus(true, 1);
1146  uint32_t ssrc = rtp_sender_->SSRC();
1147
1148  rtp_sender_->RegisterRtpStatisticsCallback(&callback);
1149
1150  // Send a frame.
1151  ASSERT_EQ(
1152      0, rtp_sender_->SendOutgoingData(kVideoFrameKey, payload_type, 1234, 4321,
1153                                       payload, sizeof(payload), nullptr));
1154  StreamDataCounters expected;
1155  expected.transmitted.payload_bytes = 6;
1156  expected.transmitted.header_bytes = 12;
1157  expected.transmitted.padding_bytes = 0;
1158  expected.transmitted.packets = 1;
1159  expected.retransmitted.payload_bytes = 0;
1160  expected.retransmitted.header_bytes = 0;
1161  expected.retransmitted.padding_bytes = 0;
1162  expected.retransmitted.packets = 0;
1163  expected.fec.packets = 0;
1164  callback.Matches(ssrc, expected);
1165
1166  // Retransmit a frame.
1167  uint16_t seqno = rtp_sender_->SequenceNumber() - 1;
1168  rtp_sender_->ReSendPacket(seqno, 0);
1169  expected.transmitted.payload_bytes = 12;
1170  expected.transmitted.header_bytes = 24;
1171  expected.transmitted.packets = 2;
1172  expected.retransmitted.payload_bytes = 6;
1173  expected.retransmitted.header_bytes = 12;
1174  expected.retransmitted.padding_bytes = 0;
1175  expected.retransmitted.packets = 1;
1176  callback.Matches(ssrc, expected);
1177
1178  // Send padding.
1179  rtp_sender_->TimeToSendPadding(kMaxPaddingSize);
1180  expected.transmitted.payload_bytes = 12;
1181  expected.transmitted.header_bytes = 36;
1182  expected.transmitted.padding_bytes = kMaxPaddingSize;
1183  expected.transmitted.packets = 3;
1184  callback.Matches(ssrc, expected);
1185
1186  // Send FEC.
1187  rtp_sender_->SetGenericFECStatus(true, kRedPayloadType, kUlpfecPayloadType);
1188  FecProtectionParams fec_params;
1189  fec_params.fec_mask_type = kFecMaskRandom;
1190  fec_params.fec_rate = 1;
1191  fec_params.max_fec_frames = 1;
1192  fec_params.use_uep_protection = false;
1193  rtp_sender_->SetFecParameters(&fec_params, &fec_params);
1194  ASSERT_EQ(0, rtp_sender_->SendOutgoingData(kVideoFrameDelta, payload_type,
1195                                             1234, 4321, payload,
1196                                             sizeof(payload), nullptr));
1197  expected.transmitted.payload_bytes = 40;
1198  expected.transmitted.header_bytes = 60;
1199  expected.transmitted.packets = 5;
1200  expected.fec.packets = 1;
1201  callback.Matches(ssrc, expected);
1202
1203  rtp_sender_->RegisterRtpStatisticsCallback(nullptr);
1204}
1205
1206TEST_F(RtpSenderAudioTest, SendAudio) {
1207  char payload_name[RTP_PAYLOAD_NAME_SIZE] = "PAYLOAD_NAME";
1208  const uint8_t payload_type = 127;
1209  ASSERT_EQ(0, rtp_sender_->RegisterPayload(payload_name, payload_type, 48000,
1210                                            0, 1500));
1211  uint8_t payload[] = {47, 11, 32, 93, 89};
1212
1213  ASSERT_EQ(
1214      0, rtp_sender_->SendOutgoingData(kAudioFrameCN, payload_type, 1234, 4321,
1215                                       payload, sizeof(payload), nullptr));
1216
1217  RtpUtility::RtpHeaderParser rtp_parser(transport_.last_sent_packet_,
1218                                         transport_.last_sent_packet_len_);
1219  webrtc::RTPHeader rtp_header;
1220  ASSERT_TRUE(rtp_parser.Parse(&rtp_header));
1221
1222  const uint8_t* payload_data =
1223      GetPayloadData(rtp_header, transport_.last_sent_packet_);
1224
1225  ASSERT_EQ(sizeof(payload),
1226            GetPayloadDataLength(rtp_header, transport_.last_sent_packet_len_));
1227
1228  EXPECT_EQ(0, memcmp(payload, payload_data, sizeof(payload)));
1229}
1230
1231TEST_F(RtpSenderAudioTest, SendAudioWithAudioLevelExtension) {
1232  EXPECT_EQ(0, rtp_sender_->SetAudioLevel(kAudioLevel));
1233  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
1234                                                       kAudioLevelExtensionId));
1235
1236  char payload_name[RTP_PAYLOAD_NAME_SIZE] = "PAYLOAD_NAME";
1237  const uint8_t payload_type = 127;
1238  ASSERT_EQ(0, rtp_sender_->RegisterPayload(payload_name, payload_type, 48000,
1239                                            0, 1500));
1240  uint8_t payload[] = {47, 11, 32, 93, 89};
1241
1242  ASSERT_EQ(
1243      0, rtp_sender_->SendOutgoingData(kAudioFrameCN, payload_type, 1234, 4321,
1244                                       payload, sizeof(payload), nullptr));
1245
1246  RtpUtility::RtpHeaderParser rtp_parser(transport_.last_sent_packet_,
1247                                         transport_.last_sent_packet_len_);
1248  webrtc::RTPHeader rtp_header;
1249  ASSERT_TRUE(rtp_parser.Parse(&rtp_header));
1250
1251  const uint8_t* payload_data =
1252      GetPayloadData(rtp_header, transport_.last_sent_packet_);
1253
1254  ASSERT_EQ(sizeof(payload),
1255            GetPayloadDataLength(rtp_header, transport_.last_sent_packet_len_));
1256
1257  EXPECT_EQ(0, memcmp(payload, payload_data, sizeof(payload)));
1258
1259  uint8_t extension[] = {
1260      0xbe, 0xde, 0x00, 0x01,
1261      (kAudioLevelExtensionId << 4) + 0,  // ID + length.
1262      kAudioLevel,                        // Data.
1263      0x00, 0x00                          // Padding.
1264  };
1265
1266  EXPECT_EQ(0, memcmp(extension, payload_data - sizeof(extension),
1267                      sizeof(extension)));
1268}
1269
1270// As RFC4733, named telephone events are carried as part of the audio stream
1271// and must use the same sequence number and timestamp base as the regular
1272// audio channel.
1273// This test checks the marker bit for the first packet and the consequent
1274// packets of the same telephone event. Since it is specifically for DTMF
1275// events, ignoring audio packets and sending kEmptyFrame instead of those.
1276TEST_F(RtpSenderAudioTest, CheckMarkerBitForTelephoneEvents) {
1277  char payload_name[RTP_PAYLOAD_NAME_SIZE] = "telephone-event";
1278  uint8_t payload_type = 126;
1279  ASSERT_EQ(0,
1280            rtp_sender_->RegisterPayload(payload_name, payload_type, 0, 0, 0));
1281  // For Telephone events, payload is not added to the registered payload list,
1282  // it will register only the payload used for audio stream.
1283  // Registering the payload again for audio stream with different payload name.
1284  const char kPayloadName[] = "payload_name";
1285  ASSERT_EQ(
1286      0, rtp_sender_->RegisterPayload(kPayloadName, payload_type, 8000, 1, 0));
1287  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
1288  // DTMF event key=9, duration=500 and attenuationdB=10
1289  rtp_sender_->SendTelephoneEvent(9, 500, 10);
1290  // During start, it takes the starting timestamp as last sent timestamp.
1291  // The duration is calculated as the difference of current and last sent
1292  // timestamp. So for first call it will skip since the duration is zero.
1293  ASSERT_EQ(0, rtp_sender_->SendOutgoingData(kEmptyFrame, payload_type,
1294                                             capture_time_ms, 0, nullptr, 0,
1295                                             nullptr));
1296  // DTMF Sample Length is (Frequency/1000) * Duration.
1297  // So in this case, it is (8000/1000) * 500 = 4000.
1298  // Sending it as two packets.
1299  ASSERT_EQ(0, rtp_sender_->SendOutgoingData(kEmptyFrame, payload_type,
1300                                             capture_time_ms + 2000, 0, nullptr,
1301                                             0, nullptr));
1302  rtc::scoped_ptr<webrtc::RtpHeaderParser> rtp_parser(
1303      webrtc::RtpHeaderParser::Create());
1304  ASSERT_TRUE(rtp_parser.get() != nullptr);
1305  webrtc::RTPHeader rtp_header;
1306  ASSERT_TRUE(rtp_parser->Parse(transport_.last_sent_packet_,
1307                                transport_.last_sent_packet_len_, &rtp_header));
1308  // Marker Bit should be set to 1 for first packet.
1309  EXPECT_TRUE(rtp_header.markerBit);
1310
1311  ASSERT_EQ(0, rtp_sender_->SendOutgoingData(kEmptyFrame, payload_type,
1312                                             capture_time_ms + 4000, 0, nullptr,
1313                                             0, nullptr));
1314  ASSERT_TRUE(rtp_parser->Parse(transport_.last_sent_packet_,
1315                                transport_.last_sent_packet_len_, &rtp_header));
1316  // Marker Bit should be set to 0 for rest of the packets.
1317  EXPECT_FALSE(rtp_header.markerBit);
1318}
1319
1320TEST_F(RtpSenderTestWithoutPacer, BytesReportedCorrectly) {
1321  const char* kPayloadName = "GENERIC";
1322  const uint8_t kPayloadType = 127;
1323  rtp_sender_->SetSSRC(1234);
1324  rtp_sender_->SetRtxSsrc(4321);
1325  rtp_sender_->SetRtxPayloadType(kPayloadType - 1, kPayloadType);
1326  rtp_sender_->SetRtxStatus(kRtxRetransmitted | kRtxRedundantPayloads);
1327
1328  ASSERT_EQ(0, rtp_sender_->RegisterPayload(kPayloadName, kPayloadType, 90000,
1329                                            0, 1500));
1330  uint8_t payload[] = {47, 11, 32, 93, 89};
1331
1332  ASSERT_EQ(
1333      0, rtp_sender_->SendOutgoingData(kVideoFrameKey, kPayloadType, 1234, 4321,
1334                                       payload, sizeof(payload), 0));
1335
1336  // Will send 2 full-size padding packets.
1337  rtp_sender_->TimeToSendPadding(1);
1338  rtp_sender_->TimeToSendPadding(1);
1339
1340  StreamDataCounters rtp_stats;
1341  StreamDataCounters rtx_stats;
1342  rtp_sender_->GetDataCounters(&rtp_stats, &rtx_stats);
1343
1344  // Payload + 1-byte generic header.
1345  EXPECT_GT(rtp_stats.first_packet_time_ms, -1);
1346  EXPECT_EQ(rtp_stats.transmitted.payload_bytes, sizeof(payload) + 1);
1347  EXPECT_EQ(rtp_stats.transmitted.header_bytes, 12u);
1348  EXPECT_EQ(rtp_stats.transmitted.padding_bytes, 0u);
1349  EXPECT_EQ(rtx_stats.transmitted.payload_bytes, 0u);
1350  EXPECT_EQ(rtx_stats.transmitted.header_bytes, 24u);
1351  EXPECT_EQ(rtx_stats.transmitted.padding_bytes, 2 * kMaxPaddingSize);
1352
1353  EXPECT_EQ(rtp_stats.transmitted.TotalBytes(),
1354            rtp_stats.transmitted.payload_bytes +
1355                rtp_stats.transmitted.header_bytes +
1356                rtp_stats.transmitted.padding_bytes);
1357  EXPECT_EQ(rtx_stats.transmitted.TotalBytes(),
1358            rtx_stats.transmitted.payload_bytes +
1359                rtx_stats.transmitted.header_bytes +
1360                rtx_stats.transmitted.padding_bytes);
1361
1362  EXPECT_EQ(
1363      transport_.total_bytes_sent_,
1364      rtp_stats.transmitted.TotalBytes() + rtx_stats.transmitted.TotalBytes());
1365}
1366
1367TEST_F(RtpSenderTestWithoutPacer, RespectsNackBitrateLimit) {
1368  const int32_t kPacketSize = 1400;
1369  const int32_t kNumPackets = 30;
1370
1371  rtp_sender_->SetStorePacketsStatus(true, kNumPackets);
1372  // Set bitrate (in kbps) to fit kNumPackets á kPacketSize bytes in one second.
1373  rtp_sender_->SetTargetBitrate(kNumPackets * kPacketSize * 8);
1374  const uint16_t kStartSequenceNumber = rtp_sender_->SequenceNumber();
1375  std::list<uint16_t> sequence_numbers;
1376  for (int32_t i = 0; i < kNumPackets; ++i) {
1377    sequence_numbers.push_back(kStartSequenceNumber + i);
1378    fake_clock_.AdvanceTimeMilliseconds(1);
1379    SendPacket(fake_clock_.TimeInMilliseconds(), kPacketSize);
1380  }
1381  EXPECT_EQ(kNumPackets, transport_.packets_sent_);
1382
1383  fake_clock_.AdvanceTimeMilliseconds(1000 - kNumPackets);
1384
1385  // Resending should work - brings the bandwidth up to the limit.
1386  // NACK bitrate is capped to the same bitrate as the encoder, since the max
1387  // protection overhead is 50% (see MediaOptimization::SetTargetRates).
1388  rtp_sender_->OnReceivedNACK(sequence_numbers, 0);
1389  EXPECT_EQ(kNumPackets * 2, transport_.packets_sent_);
1390
1391  // Resending should not work, bandwidth exceeded.
1392  rtp_sender_->OnReceivedNACK(sequence_numbers, 0);
1393  EXPECT_EQ(kNumPackets * 2, transport_.packets_sent_);
1394}
1395
1396// Verify that all packets of a frame have CVO byte set.
1397TEST_F(RtpSenderVideoTest, SendVideoWithCVO) {
1398  RTPVideoHeader hdr = {0};
1399  hdr.rotation = kVideoRotation_90;
1400
1401  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
1402                   kRtpExtensionVideoRotation, kVideoRotationExtensionId));
1403  EXPECT_TRUE(rtp_sender_->ActivateCVORtpHeaderExtension());
1404
1405  EXPECT_EQ(
1406      RtpUtility::Word32Align(kRtpOneByteHeaderLength + kVideoRotationLength),
1407      rtp_sender_->RtpHeaderExtensionTotalLength());
1408
1409  rtp_sender_video_->SendVideo(kRtpVideoGeneric, kVideoFrameKey, kPayload,
1410                               kTimestamp, 0, packet_, sizeof(packet_), nullptr,
1411                               &hdr);
1412
1413  RtpHeaderExtensionMap map;
1414  map.Register(kRtpExtensionVideoRotation, kVideoRotationExtensionId);
1415
1416  // Verify that this packet does have CVO byte.
1417  VerifyCVOPacket(
1418      reinterpret_cast<uint8_t*>(transport_.sent_packets_[0]->data()),
1419      transport_.sent_packets_[0]->size(), true, &map, kSeqNum, hdr.rotation);
1420
1421  // Verify that this packet does have CVO byte.
1422  VerifyCVOPacket(
1423      reinterpret_cast<uint8_t*>(transport_.sent_packets_[1]->data()),
1424      transport_.sent_packets_[1]->size(), true, &map, kSeqNum + 1,
1425      hdr.rotation);
1426}
1427}  // namespace webrtc
1428