• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2011 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 #include <string.h>
12 
13 #include <list>
14 
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "webrtc/modules/video_coding/frame_buffer.h"
17 #include "webrtc/modules/video_coding/jitter_buffer.h"
18 #include "webrtc/modules/video_coding/media_opt_util.h"
19 #include "webrtc/modules/video_coding/packet.h"
20 #include "webrtc/modules/video_coding/test/stream_generator.h"
21 #include "webrtc/modules/video_coding/test/test_util.h"
22 #include "webrtc/system_wrappers/include/clock.h"
23 #include "webrtc/system_wrappers/include/metrics.h"
24 #include "webrtc/test/histogram.h"
25 
26 namespace webrtc {
27 
28 namespace {
29 const uint32_t kProcessIntervalSec = 60;
30 }  // namespace
31 
32 class Vp9SsMapTest : public ::testing::Test {
33  protected:
Vp9SsMapTest()34   Vp9SsMapTest() : packet_(data_, 1400, 1234, 1, true) {}
35 
SetUp()36   virtual void SetUp() {
37     packet_.isFirstPacket = true;
38     packet_.markerBit = true;
39     packet_.frameType = kVideoFrameKey;
40     packet_.codec = kVideoCodecVP9;
41     packet_.codecSpecificHeader.codec = kRtpVideoVp9;
42     packet_.codecSpecificHeader.codecHeader.VP9.flexible_mode = false;
43     packet_.codecSpecificHeader.codecHeader.VP9.gof_idx = 0;
44     packet_.codecSpecificHeader.codecHeader.VP9.temporal_idx = kNoTemporalIdx;
45     packet_.codecSpecificHeader.codecHeader.VP9.temporal_up_switch = false;
46     packet_.codecSpecificHeader.codecHeader.VP9.ss_data_available = true;
47     packet_.codecSpecificHeader.codecHeader.VP9.gof.SetGofInfoVP9(
48         kTemporalStructureMode3);  // kTemporalStructureMode3: 0-2-1-2..
49   }
50 
51   Vp9SsMap map_;
52   uint8_t data_[1500];
53   VCMPacket packet_;
54 };
55 
TEST_F(Vp9SsMapTest,Insert)56 TEST_F(Vp9SsMapTest, Insert) {
57   EXPECT_TRUE(map_.Insert(packet_));
58 }
59 
TEST_F(Vp9SsMapTest,Insert_NoSsData)60 TEST_F(Vp9SsMapTest, Insert_NoSsData) {
61   packet_.codecSpecificHeader.codecHeader.VP9.ss_data_available = false;
62   EXPECT_FALSE(map_.Insert(packet_));
63 }
64 
TEST_F(Vp9SsMapTest,Find)65 TEST_F(Vp9SsMapTest, Find) {
66   EXPECT_TRUE(map_.Insert(packet_));
67   Vp9SsMap::SsMap::iterator it;
68   EXPECT_TRUE(map_.Find(packet_.timestamp, &it));
69   EXPECT_EQ(packet_.timestamp, it->first);
70 }
71 
TEST_F(Vp9SsMapTest,Find_WithWrap)72 TEST_F(Vp9SsMapTest, Find_WithWrap) {
73   const uint32_t kSsTimestamp1 = 0xFFFFFFFF;
74   const uint32_t kSsTimestamp2 = 100;
75   packet_.timestamp = kSsTimestamp1;
76   EXPECT_TRUE(map_.Insert(packet_));
77   packet_.timestamp = kSsTimestamp2;
78   EXPECT_TRUE(map_.Insert(packet_));
79   Vp9SsMap::SsMap::iterator it;
80   EXPECT_FALSE(map_.Find(kSsTimestamp1 - 1, &it));
81   EXPECT_TRUE(map_.Find(kSsTimestamp1, &it));
82   EXPECT_EQ(kSsTimestamp1, it->first);
83   EXPECT_TRUE(map_.Find(0, &it));
84   EXPECT_EQ(kSsTimestamp1, it->first);
85   EXPECT_TRUE(map_.Find(kSsTimestamp2 - 1, &it));
86   EXPECT_EQ(kSsTimestamp1, it->first);
87   EXPECT_TRUE(map_.Find(kSsTimestamp2, &it));
88   EXPECT_EQ(kSsTimestamp2, it->first);
89   EXPECT_TRUE(map_.Find(kSsTimestamp2 + 1, &it));
90   EXPECT_EQ(kSsTimestamp2, it->first);
91 }
92 
TEST_F(Vp9SsMapTest,Reset)93 TEST_F(Vp9SsMapTest, Reset) {
94   EXPECT_TRUE(map_.Insert(packet_));
95   Vp9SsMap::SsMap::iterator it;
96   EXPECT_TRUE(map_.Find(packet_.timestamp, &it));
97   EXPECT_EQ(packet_.timestamp, it->first);
98 
99   map_.Reset();
100   EXPECT_FALSE(map_.Find(packet_.timestamp, &it));
101 }
102 
TEST_F(Vp9SsMapTest,RemoveOld)103 TEST_F(Vp9SsMapTest, RemoveOld) {
104   Vp9SsMap::SsMap::iterator it;
105   const uint32_t kSsTimestamp1 = 10000;
106   packet_.timestamp = kSsTimestamp1;
107   EXPECT_TRUE(map_.Insert(packet_));
108 
109   const uint32_t kTimestamp = kSsTimestamp1 + kProcessIntervalSec * 90000;
110   map_.RemoveOld(kTimestamp - 1);              // Interval not passed.
111   EXPECT_TRUE(map_.Find(kSsTimestamp1, &it));  // Should not been removed.
112 
113   map_.RemoveOld(kTimestamp);
114   EXPECT_FALSE(map_.Find(kSsTimestamp1, &it));
115   EXPECT_TRUE(map_.Find(kTimestamp, &it));
116   EXPECT_EQ(kTimestamp, it->first);
117 }
118 
TEST_F(Vp9SsMapTest,RemoveOld_WithWrap)119 TEST_F(Vp9SsMapTest, RemoveOld_WithWrap) {
120   Vp9SsMap::SsMap::iterator it;
121   const uint32_t kSsTimestamp1 = 0xFFFFFFFF - kProcessIntervalSec * 90000;
122   const uint32_t kSsTimestamp2 = 10;
123   const uint32_t kSsTimestamp3 = 1000;
124   packet_.timestamp = kSsTimestamp1;
125   EXPECT_TRUE(map_.Insert(packet_));
126   packet_.timestamp = kSsTimestamp2;
127   EXPECT_TRUE(map_.Insert(packet_));
128   packet_.timestamp = kSsTimestamp3;
129   EXPECT_TRUE(map_.Insert(packet_));
130 
131   map_.RemoveOld(kSsTimestamp3);
132   EXPECT_FALSE(map_.Find(kSsTimestamp1, &it));
133   EXPECT_FALSE(map_.Find(kSsTimestamp2, &it));
134   EXPECT_TRUE(map_.Find(kSsTimestamp3, &it));
135 }
136 
TEST_F(Vp9SsMapTest,UpdatePacket_NoSsData)137 TEST_F(Vp9SsMapTest, UpdatePacket_NoSsData) {
138   packet_.codecSpecificHeader.codecHeader.VP9.gof_idx = 0;
139   EXPECT_FALSE(map_.UpdatePacket(&packet_));
140 }
141 
TEST_F(Vp9SsMapTest,UpdatePacket_NoGofIdx)142 TEST_F(Vp9SsMapTest, UpdatePacket_NoGofIdx) {
143   EXPECT_TRUE(map_.Insert(packet_));
144   packet_.codecSpecificHeader.codecHeader.VP9.gof_idx = kNoGofIdx;
145   EXPECT_FALSE(map_.UpdatePacket(&packet_));
146 }
147 
TEST_F(Vp9SsMapTest,UpdatePacket_InvalidGofIdx)148 TEST_F(Vp9SsMapTest, UpdatePacket_InvalidGofIdx) {
149   EXPECT_TRUE(map_.Insert(packet_));
150   packet_.codecSpecificHeader.codecHeader.VP9.gof_idx = 4;
151   EXPECT_FALSE(map_.UpdatePacket(&packet_));
152 }
153 
TEST_F(Vp9SsMapTest,UpdatePacket)154 TEST_F(Vp9SsMapTest, UpdatePacket) {
155   EXPECT_TRUE(map_.Insert(packet_));  // kTemporalStructureMode3: 0-2-1-2..
156 
157   packet_.codecSpecificHeader.codecHeader.VP9.gof_idx = 0;
158   EXPECT_TRUE(map_.UpdatePacket(&packet_));
159   EXPECT_EQ(0, packet_.codecSpecificHeader.codecHeader.VP9.temporal_idx);
160   EXPECT_FALSE(packet_.codecSpecificHeader.codecHeader.VP9.temporal_up_switch);
161   EXPECT_EQ(1U, packet_.codecSpecificHeader.codecHeader.VP9.num_ref_pics);
162   EXPECT_EQ(4, packet_.codecSpecificHeader.codecHeader.VP9.pid_diff[0]);
163 
164   packet_.codecSpecificHeader.codecHeader.VP9.gof_idx = 1;
165   EXPECT_TRUE(map_.UpdatePacket(&packet_));
166   EXPECT_EQ(2, packet_.codecSpecificHeader.codecHeader.VP9.temporal_idx);
167   EXPECT_TRUE(packet_.codecSpecificHeader.codecHeader.VP9.temporal_up_switch);
168   EXPECT_EQ(1U, packet_.codecSpecificHeader.codecHeader.VP9.num_ref_pics);
169   EXPECT_EQ(1, packet_.codecSpecificHeader.codecHeader.VP9.pid_diff[0]);
170 
171   packet_.codecSpecificHeader.codecHeader.VP9.gof_idx = 2;
172   EXPECT_TRUE(map_.UpdatePacket(&packet_));
173   EXPECT_EQ(1, packet_.codecSpecificHeader.codecHeader.VP9.temporal_idx);
174   EXPECT_TRUE(packet_.codecSpecificHeader.codecHeader.VP9.temporal_up_switch);
175   EXPECT_EQ(1U, packet_.codecSpecificHeader.codecHeader.VP9.num_ref_pics);
176   EXPECT_EQ(2, packet_.codecSpecificHeader.codecHeader.VP9.pid_diff[0]);
177 
178   packet_.codecSpecificHeader.codecHeader.VP9.gof_idx = 3;
179   EXPECT_TRUE(map_.UpdatePacket(&packet_));
180   EXPECT_EQ(2, packet_.codecSpecificHeader.codecHeader.VP9.temporal_idx);
181   EXPECT_FALSE(packet_.codecSpecificHeader.codecHeader.VP9.temporal_up_switch);
182   EXPECT_EQ(2U, packet_.codecSpecificHeader.codecHeader.VP9.num_ref_pics);
183   EXPECT_EQ(1, packet_.codecSpecificHeader.codecHeader.VP9.pid_diff[0]);
184   EXPECT_EQ(2, packet_.codecSpecificHeader.codecHeader.VP9.pid_diff[1]);
185 }
186 
187 class TestBasicJitterBuffer : public ::testing::Test {
188  protected:
SetUp()189   virtual void SetUp() {
190     clock_.reset(new SimulatedClock(0));
191     jitter_buffer_.reset(new VCMJitterBuffer(
192         clock_.get(),
193         rtc::scoped_ptr<EventWrapper>(event_factory_.CreateEvent())));
194     jitter_buffer_->Start();
195     seq_num_ = 1234;
196     timestamp_ = 0;
197     size_ = 1400;
198     // Data vector -  0, 0, 0x80, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0x80, 3....
199     data_[0] = 0;
200     data_[1] = 0;
201     data_[2] = 0x80;
202     int count = 3;
203     for (unsigned int i = 3; i < sizeof(data_) - 3; ++i) {
204       data_[i] = count;
205       count++;
206       if (count == 10) {
207         data_[i + 1] = 0;
208         data_[i + 2] = 0;
209         data_[i + 3] = 0x80;
210         count = 3;
211         i += 3;
212       }
213     }
214     packet_.reset(new VCMPacket(data_, size_, seq_num_, timestamp_, true));
215   }
216 
DecodeCompleteFrame()217   VCMEncodedFrame* DecodeCompleteFrame() {
218     uint32_t timestamp = 0;
219     bool found_frame = jitter_buffer_->NextCompleteTimestamp(10, &timestamp);
220     if (!found_frame)
221       return NULL;
222     VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(timestamp);
223     return frame;
224   }
225 
DecodeIncompleteFrame()226   VCMEncodedFrame* DecodeIncompleteFrame() {
227     uint32_t timestamp = 0;
228     bool found_frame = jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp);
229     if (!found_frame)
230       return NULL;
231     VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(timestamp);
232     return frame;
233   }
234 
CheckOutFrame(VCMEncodedFrame * frame_out,unsigned int size,bool startCode)235   void CheckOutFrame(VCMEncodedFrame* frame_out,
236                      unsigned int size,
237                      bool startCode) {
238     ASSERT_TRUE(frame_out);
239 
240     const uint8_t* outData = frame_out->Buffer();
241     unsigned int i = 0;
242 
243     if (startCode) {
244       EXPECT_EQ(0, outData[0]);
245       EXPECT_EQ(0, outData[1]);
246       EXPECT_EQ(0, outData[2]);
247       EXPECT_EQ(1, outData[3]);
248       i += 4;
249     }
250 
251     EXPECT_EQ(size, frame_out->Length());
252     int count = 3;
253     for (; i < size; i++) {
254       if (outData[i] == 0 && outData[i + 1] == 0 && outData[i + 2] == 0x80) {
255         i += 2;
256       } else if (startCode && outData[i] == 0 && outData[i + 1] == 0) {
257         EXPECT_EQ(0, outData[0]);
258         EXPECT_EQ(0, outData[1]);
259         EXPECT_EQ(0, outData[2]);
260         EXPECT_EQ(1, outData[3]);
261         i += 3;
262       } else {
263         EXPECT_EQ(count, outData[i]);
264         count++;
265         if (count == 10) {
266           count = 3;
267         }
268       }
269     }
270   }
271 
272   uint16_t seq_num_;
273   uint32_t timestamp_;
274   int size_;
275   uint8_t data_[1500];
276   rtc::scoped_ptr<VCMPacket> packet_;
277   rtc::scoped_ptr<SimulatedClock> clock_;
278   NullEventFactory event_factory_;
279   rtc::scoped_ptr<VCMJitterBuffer> jitter_buffer_;
280 };
281 
282 class TestRunningJitterBuffer : public ::testing::Test {
283  protected:
284   enum { kDataBufferSize = 10 };
285 
SetUp()286   virtual void SetUp() {
287     clock_.reset(new SimulatedClock(0));
288     max_nack_list_size_ = 150;
289     oldest_packet_to_nack_ = 250;
290     jitter_buffer_ = new VCMJitterBuffer(
291         clock_.get(),
292         rtc::scoped_ptr<EventWrapper>(event_factory_.CreateEvent()));
293     stream_generator_ = new StreamGenerator(0, clock_->TimeInMilliseconds());
294     jitter_buffer_->Start();
295     jitter_buffer_->SetNackSettings(max_nack_list_size_, oldest_packet_to_nack_,
296                                     0);
297     memset(data_buffer_, 0, kDataBufferSize);
298   }
299 
TearDown()300   virtual void TearDown() {
301     jitter_buffer_->Stop();
302     delete stream_generator_;
303     delete jitter_buffer_;
304   }
305 
InsertPacketAndPop(int index)306   VCMFrameBufferEnum InsertPacketAndPop(int index) {
307     VCMPacket packet;
308     packet.dataPtr = data_buffer_;
309     bool packet_available = stream_generator_->PopPacket(&packet, index);
310     EXPECT_TRUE(packet_available);
311     if (!packet_available)
312       return kGeneralError;  // Return here to avoid crashes below.
313     bool retransmitted = false;
314     return jitter_buffer_->InsertPacket(packet, &retransmitted);
315   }
316 
InsertPacket(int index)317   VCMFrameBufferEnum InsertPacket(int index) {
318     VCMPacket packet;
319     packet.dataPtr = data_buffer_;
320     bool packet_available = stream_generator_->GetPacket(&packet, index);
321     EXPECT_TRUE(packet_available);
322     if (!packet_available)
323       return kGeneralError;  // Return here to avoid crashes below.
324     bool retransmitted = false;
325     return jitter_buffer_->InsertPacket(packet, &retransmitted);
326   }
327 
InsertFrame(FrameType frame_type)328   VCMFrameBufferEnum InsertFrame(FrameType frame_type) {
329     stream_generator_->GenerateFrame(
330         frame_type, (frame_type != kEmptyFrame) ? 1 : 0,
331         (frame_type == kEmptyFrame) ? 1 : 0, clock_->TimeInMilliseconds());
332     VCMFrameBufferEnum ret = InsertPacketAndPop(0);
333     clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
334     return ret;
335   }
336 
InsertFrames(int num_frames,FrameType frame_type)337   VCMFrameBufferEnum InsertFrames(int num_frames, FrameType frame_type) {
338     VCMFrameBufferEnum ret_for_all = kNoError;
339     for (int i = 0; i < num_frames; ++i) {
340       VCMFrameBufferEnum ret = InsertFrame(frame_type);
341       if (ret < kNoError) {
342         ret_for_all = ret;
343       } else if (ret_for_all >= kNoError) {
344         ret_for_all = ret;
345       }
346     }
347     return ret_for_all;
348   }
349 
DropFrame(int num_packets)350   void DropFrame(int num_packets) {
351     stream_generator_->GenerateFrame(kVideoFrameDelta, num_packets, 0,
352                                      clock_->TimeInMilliseconds());
353     for (int i = 0; i < num_packets; ++i)
354       stream_generator_->DropLastPacket();
355     clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
356   }
357 
DecodeCompleteFrame()358   bool DecodeCompleteFrame() {
359     uint32_t timestamp = 0;
360     bool found_frame = jitter_buffer_->NextCompleteTimestamp(0, &timestamp);
361     if (!found_frame)
362       return false;
363 
364     VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(timestamp);
365     bool ret = (frame != NULL);
366     jitter_buffer_->ReleaseFrame(frame);
367     return ret;
368   }
369 
DecodeIncompleteFrame()370   bool DecodeIncompleteFrame() {
371     uint32_t timestamp = 0;
372     bool found_frame = jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp);
373     if (!found_frame)
374       return false;
375     VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(timestamp);
376     bool ret = (frame != NULL);
377     jitter_buffer_->ReleaseFrame(frame);
378     return ret;
379   }
380 
381   VCMJitterBuffer* jitter_buffer_;
382   StreamGenerator* stream_generator_;
383   rtc::scoped_ptr<SimulatedClock> clock_;
384   NullEventFactory event_factory_;
385   size_t max_nack_list_size_;
386   int oldest_packet_to_nack_;
387   uint8_t data_buffer_[kDataBufferSize];
388 };
389 
390 class TestJitterBufferNack : public TestRunningJitterBuffer {
391  protected:
SetUp()392   virtual void SetUp() {
393     TestRunningJitterBuffer::SetUp();
394     jitter_buffer_->SetNackMode(kNack, -1, -1);
395   }
396 
TearDown()397   virtual void TearDown() { TestRunningJitterBuffer::TearDown(); }
398 };
399 
TEST_F(TestBasicJitterBuffer,StopRunning)400 TEST_F(TestBasicJitterBuffer, StopRunning) {
401   jitter_buffer_->Stop();
402   EXPECT_TRUE(NULL == DecodeCompleteFrame());
403   EXPECT_TRUE(NULL == DecodeIncompleteFrame());
404   jitter_buffer_->Start();
405   // Allow selective errors.
406   jitter_buffer_->SetDecodeErrorMode(kSelectiveErrors);
407 
408   // No packets inserted.
409   EXPECT_TRUE(NULL == DecodeCompleteFrame());
410   EXPECT_TRUE(NULL == DecodeIncompleteFrame());
411 
412   // Allow decoding with errors.
413   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
414 
415   // No packets inserted.
416   EXPECT_TRUE(NULL == DecodeCompleteFrame());
417   EXPECT_TRUE(NULL == DecodeIncompleteFrame());
418 }
419 
TEST_F(TestBasicJitterBuffer,SinglePacketFrame)420 TEST_F(TestBasicJitterBuffer, SinglePacketFrame) {
421   // Always start with a complete key frame when not allowing errors.
422   jitter_buffer_->SetDecodeErrorMode(kNoErrors);
423   packet_->frameType = kVideoFrameKey;
424   packet_->isFirstPacket = true;
425   packet_->markerBit = true;
426   packet_->timestamp += 123 * 90;
427 
428   // Insert the packet to the jitter buffer and get a frame.
429   bool retransmitted = false;
430   EXPECT_EQ(kCompleteSession,
431             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
432   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
433   CheckOutFrame(frame_out, size_, false);
434   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
435   jitter_buffer_->ReleaseFrame(frame_out);
436 }
437 
TEST_F(TestBasicJitterBuffer,VerifyHistogramStats)438 TEST_F(TestBasicJitterBuffer, VerifyHistogramStats) {
439   test::ClearHistograms();
440   // Always start with a complete key frame when not allowing errors.
441   jitter_buffer_->SetDecodeErrorMode(kNoErrors);
442   packet_->frameType = kVideoFrameKey;
443   packet_->isFirstPacket = true;
444   packet_->markerBit = true;
445   packet_->timestamp += 123 * 90;
446 
447   // Insert single packet frame to the jitter buffer and get a frame.
448   bool retransmitted = false;
449   EXPECT_EQ(kCompleteSession,
450             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
451   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
452   CheckOutFrame(frame_out, size_, false);
453   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
454   jitter_buffer_->ReleaseFrame(frame_out);
455 
456   // Verify that histograms are updated when the jitter buffer is stopped.
457   clock_->AdvanceTimeMilliseconds(metrics::kMinRunTimeInSeconds * 1000);
458   jitter_buffer_->Stop();
459   EXPECT_EQ(
460       0, test::LastHistogramSample("WebRTC.Video.DiscardedPacketsInPercent"));
461   EXPECT_EQ(
462       0, test::LastHistogramSample("WebRTC.Video.DuplicatedPacketsInPercent"));
463   EXPECT_NE(-1, test::LastHistogramSample(
464                     "WebRTC.Video.CompleteFramesReceivedPerSecond"));
465   EXPECT_EQ(1000, test::LastHistogramSample(
466                       "WebRTC.Video.KeyFramesReceivedInPermille"));
467 
468   // Verify that histograms are not updated if stop is called again.
469   jitter_buffer_->Stop();
470   EXPECT_EQ(
471       1, test::NumHistogramSamples("WebRTC.Video.DiscardedPacketsInPercent"));
472   EXPECT_EQ(
473       1, test::NumHistogramSamples("WebRTC.Video.DuplicatedPacketsInPercent"));
474   EXPECT_EQ(1, test::NumHistogramSamples(
475                    "WebRTC.Video.CompleteFramesReceivedPerSecond"));
476   EXPECT_EQ(
477       1, test::NumHistogramSamples("WebRTC.Video.KeyFramesReceivedInPermille"));
478 }
479 
TEST_F(TestBasicJitterBuffer,DualPacketFrame)480 TEST_F(TestBasicJitterBuffer, DualPacketFrame) {
481   packet_->frameType = kVideoFrameKey;
482   packet_->isFirstPacket = true;
483   packet_->markerBit = false;
484 
485   bool retransmitted = false;
486   EXPECT_EQ(kIncomplete,
487             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
488   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
489   // Should not be complete.
490   EXPECT_TRUE(frame_out == NULL);
491 
492   ++seq_num_;
493   packet_->isFirstPacket = false;
494   packet_->markerBit = true;
495   packet_->seqNum = seq_num_;
496 
497   EXPECT_EQ(kCompleteSession,
498             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
499 
500   frame_out = DecodeCompleteFrame();
501   CheckOutFrame(frame_out, 2 * size_, false);
502 
503   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
504   jitter_buffer_->ReleaseFrame(frame_out);
505 }
506 
507 TEST_F(TestBasicJitterBuffer, 100PacketKeyFrame) {
508   packet_->frameType = kVideoFrameKey;
509   packet_->isFirstPacket = true;
510   packet_->markerBit = false;
511 
512   bool retransmitted = false;
513   EXPECT_EQ(kIncomplete,
514             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
515 
516   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
517 
518   // Frame should not be complete.
519   EXPECT_TRUE(frame_out == NULL);
520 
521   // Insert 98 frames.
522   int loop = 0;
523   do {
524     seq_num_++;
525     packet_->isFirstPacket = false;
526     packet_->markerBit = false;
527     packet_->seqNum = seq_num_;
528 
529     EXPECT_EQ(kIncomplete,
530               jitter_buffer_->InsertPacket(*packet_, &retransmitted));
531     loop++;
532   } while (loop < 98);
533 
534   // Insert last packet.
535   ++seq_num_;
536   packet_->isFirstPacket = false;
537   packet_->markerBit = true;
538   packet_->seqNum = seq_num_;
539 
540   EXPECT_EQ(kCompleteSession,
541             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
542 
543   frame_out = DecodeCompleteFrame();
544 
545   CheckOutFrame(frame_out, 100 * size_, false);
546   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
547   jitter_buffer_->ReleaseFrame(frame_out);
548 }
549 
550 TEST_F(TestBasicJitterBuffer, 100PacketDeltaFrame) {
551   // Always start with a complete key frame.
552   packet_->frameType = kVideoFrameKey;
553   packet_->isFirstPacket = true;
554   packet_->markerBit = true;
555 
556   bool retransmitted = false;
557   EXPECT_EQ(kCompleteSession,
558             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
559   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
560   EXPECT_FALSE(frame_out == NULL);
561   jitter_buffer_->ReleaseFrame(frame_out);
562 
563   ++seq_num_;
564   packet_->seqNum = seq_num_;
565   packet_->markerBit = false;
566   packet_->frameType = kVideoFrameDelta;
567   packet_->timestamp += 33 * 90;
568 
569   EXPECT_EQ(kIncomplete,
570             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
571 
572   frame_out = DecodeCompleteFrame();
573 
574   // Frame should not be complete.
575   EXPECT_TRUE(frame_out == NULL);
576 
577   packet_->isFirstPacket = false;
578   // Insert 98 frames.
579   int loop = 0;
580   do {
581     ++seq_num_;
582     packet_->seqNum = seq_num_;
583 
584     // Insert a packet into a frame.
585     EXPECT_EQ(kIncomplete,
586               jitter_buffer_->InsertPacket(*packet_, &retransmitted));
587     loop++;
588   } while (loop < 98);
589 
590   // Insert the last packet.
591   ++seq_num_;
592   packet_->isFirstPacket = false;
593   packet_->markerBit = true;
594   packet_->seqNum = seq_num_;
595 
596   EXPECT_EQ(kCompleteSession,
597             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
598 
599   frame_out = DecodeCompleteFrame();
600 
601   CheckOutFrame(frame_out, 100 * size_, false);
602   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
603   jitter_buffer_->ReleaseFrame(frame_out);
604 }
605 
TEST_F(TestBasicJitterBuffer,PacketReorderingReverseOrder)606 TEST_F(TestBasicJitterBuffer, PacketReorderingReverseOrder) {
607   // Insert the "first" packet last.
608   seq_num_ += 100;
609   packet_->frameType = kVideoFrameKey;
610   packet_->isFirstPacket = false;
611   packet_->markerBit = true;
612   packet_->seqNum = seq_num_;
613   packet_->timestamp = timestamp_;
614 
615   bool retransmitted = false;
616   EXPECT_EQ(kIncomplete,
617             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
618 
619   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
620 
621   EXPECT_TRUE(frame_out == NULL);
622 
623   // Insert 98 packets.
624   int loop = 0;
625   do {
626     seq_num_--;
627     packet_->isFirstPacket = false;
628     packet_->markerBit = false;
629     packet_->seqNum = seq_num_;
630 
631     EXPECT_EQ(kIncomplete,
632               jitter_buffer_->InsertPacket(*packet_, &retransmitted));
633     loop++;
634   } while (loop < 98);
635 
636   // Insert the last packet.
637   seq_num_--;
638   packet_->isFirstPacket = true;
639   packet_->markerBit = false;
640   packet_->seqNum = seq_num_;
641 
642   EXPECT_EQ(kCompleteSession,
643             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
644 
645   frame_out = DecodeCompleteFrame();
646 
647   CheckOutFrame(frame_out, 100 * size_, false);
648 
649   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
650   jitter_buffer_->ReleaseFrame(frame_out);
651 }
652 
TEST_F(TestBasicJitterBuffer,FrameReordering2Frames2PacketsEach)653 TEST_F(TestBasicJitterBuffer, FrameReordering2Frames2PacketsEach) {
654   packet_->frameType = kVideoFrameDelta;
655   packet_->isFirstPacket = true;
656   packet_->markerBit = false;
657 
658   bool retransmitted = false;
659   EXPECT_EQ(kIncomplete,
660             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
661 
662   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
663 
664   EXPECT_TRUE(frame_out == NULL);
665 
666   seq_num_++;
667   packet_->isFirstPacket = false;
668   packet_->markerBit = true;
669   packet_->seqNum = seq_num_;
670 
671   EXPECT_EQ(kCompleteSession,
672             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
673 
674   // check that we fail to get frame since seqnum is not continuous
675   frame_out = DecodeCompleteFrame();
676   EXPECT_TRUE(frame_out == NULL);
677 
678   seq_num_ -= 3;
679   timestamp_ -= 33 * 90;
680   packet_->frameType = kVideoFrameKey;
681   packet_->isFirstPacket = true;
682   packet_->markerBit = false;
683   packet_->seqNum = seq_num_;
684   packet_->timestamp = timestamp_;
685 
686   EXPECT_EQ(kIncomplete,
687             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
688 
689   frame_out = DecodeCompleteFrame();
690 
691   // It should not be complete.
692   EXPECT_TRUE(frame_out == NULL);
693 
694   seq_num_++;
695   packet_->isFirstPacket = false;
696   packet_->markerBit = true;
697   packet_->seqNum = seq_num_;
698 
699   EXPECT_EQ(kCompleteSession,
700             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
701 
702   frame_out = DecodeCompleteFrame();
703   CheckOutFrame(frame_out, 2 * size_, false);
704   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
705   jitter_buffer_->ReleaseFrame(frame_out);
706 
707   frame_out = DecodeCompleteFrame();
708   CheckOutFrame(frame_out, 2 * size_, false);
709   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
710   jitter_buffer_->ReleaseFrame(frame_out);
711 }
712 
TEST_F(TestBasicJitterBuffer,TestReorderingWithPadding)713 TEST_F(TestBasicJitterBuffer, TestReorderingWithPadding) {
714   packet_->frameType = kVideoFrameKey;
715   packet_->isFirstPacket = true;
716   packet_->markerBit = true;
717 
718   // Send in an initial good packet/frame (Frame A) to start things off.
719   bool retransmitted = false;
720   EXPECT_EQ(kCompleteSession,
721             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
722   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
723   EXPECT_TRUE(frame_out != NULL);
724   jitter_buffer_->ReleaseFrame(frame_out);
725 
726   // Now send in a complete delta frame (Frame C), but with a sequence number
727   // gap. No pic index either, so no temporal scalability cheating :)
728   packet_->frameType = kVideoFrameDelta;
729   // Leave a gap of 2 sequence numbers and two frames.
730   packet_->seqNum = seq_num_ + 3;
731   packet_->timestamp = timestamp_ + (66 * 90);
732   // Still isFirst = marker = true.
733   // Session should be complete (frame is complete), but there's nothing to
734   // decode yet.
735   EXPECT_EQ(kCompleteSession,
736             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
737   frame_out = DecodeCompleteFrame();
738   EXPECT_TRUE(frame_out == NULL);
739 
740   // Now send in a complete delta frame (Frame B) that is continuous from A, but
741   // doesn't fill the full gap to C. The rest of the gap is going to be padding.
742   packet_->seqNum = seq_num_ + 1;
743   packet_->timestamp = timestamp_ + (33 * 90);
744   // Still isFirst = marker = true.
745   EXPECT_EQ(kCompleteSession,
746             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
747   frame_out = DecodeCompleteFrame();
748   EXPECT_TRUE(frame_out != NULL);
749   jitter_buffer_->ReleaseFrame(frame_out);
750 
751   // But Frame C isn't continuous yet.
752   frame_out = DecodeCompleteFrame();
753   EXPECT_TRUE(frame_out == NULL);
754 
755   // Add in the padding. These are empty packets (data length is 0) with no
756   // marker bit and matching the timestamp of Frame B.
757   VCMPacket empty_packet(data_, 0, seq_num_ + 2, timestamp_ + (33 * 90), false);
758   EXPECT_EQ(kOldPacket,
759             jitter_buffer_->InsertPacket(empty_packet, &retransmitted));
760   empty_packet.seqNum += 1;
761   EXPECT_EQ(kOldPacket,
762             jitter_buffer_->InsertPacket(empty_packet, &retransmitted));
763 
764   // But now Frame C should be ready!
765   frame_out = DecodeCompleteFrame();
766   EXPECT_TRUE(frame_out != NULL);
767   jitter_buffer_->ReleaseFrame(frame_out);
768 }
769 
TEST_F(TestBasicJitterBuffer,DuplicatePackets)770 TEST_F(TestBasicJitterBuffer, DuplicatePackets) {
771   packet_->frameType = kVideoFrameKey;
772   packet_->isFirstPacket = true;
773   packet_->markerBit = false;
774   packet_->seqNum = seq_num_;
775   packet_->timestamp = timestamp_;
776   EXPECT_EQ(0, jitter_buffer_->num_packets());
777   EXPECT_EQ(0, jitter_buffer_->num_duplicated_packets());
778 
779   bool retransmitted = false;
780   EXPECT_EQ(kIncomplete,
781             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
782 
783   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
784 
785   EXPECT_TRUE(frame_out == NULL);
786   EXPECT_EQ(1, jitter_buffer_->num_packets());
787   EXPECT_EQ(0, jitter_buffer_->num_duplicated_packets());
788 
789   // Insert a packet into a frame.
790   EXPECT_EQ(kDuplicatePacket,
791             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
792   EXPECT_EQ(2, jitter_buffer_->num_packets());
793   EXPECT_EQ(1, jitter_buffer_->num_duplicated_packets());
794 
795   seq_num_++;
796   packet_->seqNum = seq_num_;
797   packet_->markerBit = true;
798   packet_->isFirstPacket = false;
799 
800   EXPECT_EQ(kCompleteSession,
801             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
802 
803   frame_out = DecodeCompleteFrame();
804   ASSERT_TRUE(frame_out != NULL);
805   CheckOutFrame(frame_out, 2 * size_, false);
806 
807   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
808   EXPECT_EQ(3, jitter_buffer_->num_packets());
809   EXPECT_EQ(1, jitter_buffer_->num_duplicated_packets());
810   jitter_buffer_->ReleaseFrame(frame_out);
811 }
812 
TEST_F(TestBasicJitterBuffer,DuplicatePreviousDeltaFramePacket)813 TEST_F(TestBasicJitterBuffer, DuplicatePreviousDeltaFramePacket) {
814   packet_->frameType = kVideoFrameKey;
815   packet_->isFirstPacket = true;
816   packet_->markerBit = true;
817   packet_->seqNum = seq_num_;
818   packet_->timestamp = timestamp_;
819   jitter_buffer_->SetDecodeErrorMode(kNoErrors);
820   EXPECT_EQ(0, jitter_buffer_->num_packets());
821   EXPECT_EQ(0, jitter_buffer_->num_duplicated_packets());
822 
823   bool retransmitted = false;
824   // Insert first complete frame.
825   EXPECT_EQ(kCompleteSession,
826             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
827 
828   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
829   ASSERT_TRUE(frame_out != NULL);
830   CheckOutFrame(frame_out, size_, false);
831   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
832   jitter_buffer_->ReleaseFrame(frame_out);
833 
834   // Insert 3 delta frames.
835   for (uint16_t i = 1; i <= 3; ++i) {
836     packet_->seqNum = seq_num_ + i;
837     packet_->timestamp = timestamp_ + (i * 33) * 90;
838     packet_->frameType = kVideoFrameDelta;
839     EXPECT_EQ(kCompleteSession,
840               jitter_buffer_->InsertPacket(*packet_, &retransmitted));
841     EXPECT_EQ(i + 1, jitter_buffer_->num_packets());
842     EXPECT_EQ(0, jitter_buffer_->num_duplicated_packets());
843   }
844 
845   // Retransmit second delta frame.
846   packet_->seqNum = seq_num_ + 2;
847   packet_->timestamp = timestamp_ + 66 * 90;
848 
849   EXPECT_EQ(kDuplicatePacket,
850             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
851 
852   EXPECT_EQ(5, jitter_buffer_->num_packets());
853   EXPECT_EQ(1, jitter_buffer_->num_duplicated_packets());
854 
855   // Should be able to decode 3 delta frames, key frame already decoded.
856   for (size_t i = 0; i < 3; ++i) {
857     frame_out = DecodeCompleteFrame();
858     ASSERT_TRUE(frame_out != NULL);
859     CheckOutFrame(frame_out, size_, false);
860     EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
861     jitter_buffer_->ReleaseFrame(frame_out);
862   }
863 }
864 
TEST_F(TestBasicJitterBuffer,TestSkipForwardVp9)865 TEST_F(TestBasicJitterBuffer, TestSkipForwardVp9) {
866   // Verify that JB skips forward to next base layer frame.
867   //  -------------------------------------------------
868   // | 65485 | 65486 | 65487 | 65488 | 65489 | ...
869   // | pid:5 | pid:6 | pid:7 | pid:8 | pid:9 | ...
870   // | tid:0 | tid:2 | tid:1 | tid:2 | tid:0 | ...
871   // |  ss   |   x   |   x   |   x   |       |
872   //  -------------------------------------------------
873   // |<----------tl0idx:200--------->|<---tl0idx:201---
874 
875   bool re = false;
876   packet_->codec = kVideoCodecVP9;
877   packet_->codecSpecificHeader.codec = kRtpVideoVp9;
878   packet_->isFirstPacket = true;
879   packet_->markerBit = true;
880   packet_->codecSpecificHeader.codecHeader.VP9.flexible_mode = false;
881   packet_->codecSpecificHeader.codecHeader.VP9.spatial_idx = 0;
882   packet_->codecSpecificHeader.codecHeader.VP9.beginning_of_frame = true;
883   packet_->codecSpecificHeader.codecHeader.VP9.end_of_frame = true;
884   packet_->codecSpecificHeader.codecHeader.VP9.temporal_up_switch = false;
885 
886   packet_->seqNum = 65485;
887   packet_->timestamp = 1000;
888   packet_->frameType = kVideoFrameKey;
889   packet_->codecSpecificHeader.codecHeader.VP9.picture_id = 5;
890   packet_->codecSpecificHeader.codecHeader.VP9.tl0_pic_idx = 200;
891   packet_->codecSpecificHeader.codecHeader.VP9.temporal_idx = 0;
892   packet_->codecSpecificHeader.codecHeader.VP9.ss_data_available = true;
893   packet_->codecSpecificHeader.codecHeader.VP9.gof.SetGofInfoVP9(
894       kTemporalStructureMode3);  // kTemporalStructureMode3: 0-2-1-2..
895   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, &re));
896 
897   // Insert next temporal layer 0.
898   packet_->seqNum = 65489;
899   packet_->timestamp = 13000;
900   packet_->frameType = kVideoFrameDelta;
901   packet_->codecSpecificHeader.codecHeader.VP9.picture_id = 9;
902   packet_->codecSpecificHeader.codecHeader.VP9.tl0_pic_idx = 201;
903   packet_->codecSpecificHeader.codecHeader.VP9.temporal_idx = 0;
904   packet_->codecSpecificHeader.codecHeader.VP9.ss_data_available = false;
905   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, &re));
906 
907   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
908   EXPECT_EQ(1000U, frame_out->TimeStamp());
909   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
910   jitter_buffer_->ReleaseFrame(frame_out);
911 
912   frame_out = DecodeCompleteFrame();
913   EXPECT_EQ(13000U, frame_out->TimeStamp());
914   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
915   jitter_buffer_->ReleaseFrame(frame_out);
916 }
917 
TEST_F(TestBasicJitterBuffer,ReorderedVp9SsData_3TlLayers)918 TEST_F(TestBasicJitterBuffer, ReorderedVp9SsData_3TlLayers) {
919   // Verify that frames are updated with SS data when SS packet is reordered.
920   //  --------------------------------
921   // | 65486 | 65487 | 65485 |...
922   // | pid:6 | pid:7 | pid:5 |...
923   // | tid:2 | tid:1 | tid:0 |...
924   // |       |       |  ss   |
925   //  --------------------------------
926   // |<--------tl0idx:200--------->|
927 
928   bool re = false;
929   packet_->codec = kVideoCodecVP9;
930   packet_->codecSpecificHeader.codec = kRtpVideoVp9;
931   packet_->isFirstPacket = true;
932   packet_->markerBit = true;
933   packet_->codecSpecificHeader.codecHeader.VP9.flexible_mode = false;
934   packet_->codecSpecificHeader.codecHeader.VP9.spatial_idx = 0;
935   packet_->codecSpecificHeader.codecHeader.VP9.beginning_of_frame = true;
936   packet_->codecSpecificHeader.codecHeader.VP9.end_of_frame = true;
937   packet_->codecSpecificHeader.codecHeader.VP9.tl0_pic_idx = 200;
938 
939   packet_->seqNum = 65486;
940   packet_->timestamp = 6000;
941   packet_->frameType = kVideoFrameDelta;
942   packet_->codecSpecificHeader.codecHeader.VP9.picture_id = 6;
943   packet_->codecSpecificHeader.codecHeader.VP9.temporal_idx = 2;
944   packet_->codecSpecificHeader.codecHeader.VP9.temporal_up_switch = true;
945   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, &re));
946 
947   packet_->seqNum = 65487;
948   packet_->timestamp = 9000;
949   packet_->frameType = kVideoFrameDelta;
950   packet_->codecSpecificHeader.codecHeader.VP9.picture_id = 7;
951   packet_->codecSpecificHeader.codecHeader.VP9.temporal_idx = 1;
952   packet_->codecSpecificHeader.codecHeader.VP9.temporal_up_switch = true;
953   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, &re));
954 
955   // Insert first frame with SS data.
956   packet_->seqNum = 65485;
957   packet_->timestamp = 3000;
958   packet_->frameType = kVideoFrameKey;
959   packet_->width = 352;
960   packet_->height = 288;
961   packet_->codecSpecificHeader.codecHeader.VP9.picture_id = 5;
962   packet_->codecSpecificHeader.codecHeader.VP9.temporal_idx = 0;
963   packet_->codecSpecificHeader.codecHeader.VP9.temporal_up_switch = false;
964   packet_->codecSpecificHeader.codecHeader.VP9.ss_data_available = true;
965   packet_->codecSpecificHeader.codecHeader.VP9.gof.SetGofInfoVP9(
966       kTemporalStructureMode3);  // kTemporalStructureMode3: 0-2-1-2..
967   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, &re));
968 
969   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
970   EXPECT_EQ(3000U, frame_out->TimeStamp());
971   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
972   EXPECT_EQ(0, frame_out->CodecSpecific()->codecSpecific.VP9.temporal_idx);
973   EXPECT_FALSE(
974       frame_out->CodecSpecific()->codecSpecific.VP9.temporal_up_switch);
975   jitter_buffer_->ReleaseFrame(frame_out);
976 
977   frame_out = DecodeCompleteFrame();
978   EXPECT_EQ(6000U, frame_out->TimeStamp());
979   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
980   EXPECT_EQ(2, frame_out->CodecSpecific()->codecSpecific.VP9.temporal_idx);
981   EXPECT_TRUE(frame_out->CodecSpecific()->codecSpecific.VP9.temporal_up_switch);
982   jitter_buffer_->ReleaseFrame(frame_out);
983 
984   frame_out = DecodeCompleteFrame();
985   EXPECT_EQ(9000U, frame_out->TimeStamp());
986   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
987   EXPECT_EQ(1, frame_out->CodecSpecific()->codecSpecific.VP9.temporal_idx);
988   EXPECT_TRUE(frame_out->CodecSpecific()->codecSpecific.VP9.temporal_up_switch);
989   jitter_buffer_->ReleaseFrame(frame_out);
990 }
991 
TEST_F(TestBasicJitterBuffer,ReorderedVp9SsData_2Tl2SLayers)992 TEST_F(TestBasicJitterBuffer, ReorderedVp9SsData_2Tl2SLayers) {
993   // Verify that frames are updated with SS data when SS packet is reordered.
994   //  -----------------------------------------
995   // | 65486  | 65487  | 65485  | 65484  |...
996   // | pid:6  | pid:6  | pid:5  | pid:5  |...
997   // | tid:1  | tid:1  | tid:0  | tid:0  |...
998   // | sid:0  | sid:1  | sid:1  | sid:0  |...
999   // | t:6000 | t:6000 | t:3000 | t:3000 |
1000   // |        |        |        |  ss    |
1001   //  -----------------------------------------
1002   // |<-----------tl0idx:200------------>|
1003 
1004   bool re = false;
1005   packet_->codec = kVideoCodecVP9;
1006   packet_->codecSpecificHeader.codec = kRtpVideoVp9;
1007   packet_->codecSpecificHeader.codecHeader.VP9.flexible_mode = false;
1008   packet_->codecSpecificHeader.codecHeader.VP9.beginning_of_frame = true;
1009   packet_->codecSpecificHeader.codecHeader.VP9.end_of_frame = true;
1010   packet_->codecSpecificHeader.codecHeader.VP9.tl0_pic_idx = 200;
1011 
1012   packet_->isFirstPacket = true;
1013   packet_->markerBit = false;
1014   packet_->seqNum = 65486;
1015   packet_->timestamp = 6000;
1016   packet_->frameType = kVideoFrameDelta;
1017   packet_->codecSpecificHeader.codecHeader.VP9.spatial_idx = 0;
1018   packet_->codecSpecificHeader.codecHeader.VP9.picture_id = 6;
1019   packet_->codecSpecificHeader.codecHeader.VP9.temporal_idx = 1;
1020   packet_->codecSpecificHeader.codecHeader.VP9.temporal_up_switch = true;
1021   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, &re));
1022 
1023   packet_->isFirstPacket = false;
1024   packet_->markerBit = true;
1025   packet_->seqNum = 65487;
1026   packet_->frameType = kVideoFrameDelta;
1027   packet_->codecSpecificHeader.codecHeader.VP9.spatial_idx = 1;
1028   packet_->codecSpecificHeader.codecHeader.VP9.picture_id = 6;
1029   packet_->codecSpecificHeader.codecHeader.VP9.temporal_idx = 1;
1030   packet_->codecSpecificHeader.codecHeader.VP9.temporal_up_switch = true;
1031   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, &re));
1032 
1033   packet_->isFirstPacket = false;
1034   packet_->markerBit = true;
1035   packet_->seqNum = 65485;
1036   packet_->timestamp = 3000;
1037   packet_->frameType = kVideoFrameKey;
1038   packet_->codecSpecificHeader.codecHeader.VP9.spatial_idx = 1;
1039   packet_->codecSpecificHeader.codecHeader.VP9.picture_id = 5;
1040   packet_->codecSpecificHeader.codecHeader.VP9.temporal_idx = 0;
1041   packet_->codecSpecificHeader.codecHeader.VP9.temporal_up_switch = false;
1042   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, &re));
1043 
1044   // Insert first frame with SS data.
1045   packet_->isFirstPacket = true;
1046   packet_->markerBit = false;
1047   packet_->seqNum = 65484;
1048   packet_->frameType = kVideoFrameKey;
1049   packet_->width = 352;
1050   packet_->height = 288;
1051   packet_->codecSpecificHeader.codecHeader.VP9.spatial_idx = 0;
1052   packet_->codecSpecificHeader.codecHeader.VP9.picture_id = 5;
1053   packet_->codecSpecificHeader.codecHeader.VP9.temporal_idx = 0;
1054   packet_->codecSpecificHeader.codecHeader.VP9.temporal_up_switch = false;
1055   packet_->codecSpecificHeader.codecHeader.VP9.ss_data_available = true;
1056   packet_->codecSpecificHeader.codecHeader.VP9.gof.SetGofInfoVP9(
1057       kTemporalStructureMode2);  // kTemporalStructureMode3: 0-1-0-1..
1058   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, &re));
1059 
1060   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1061   EXPECT_EQ(3000U, frame_out->TimeStamp());
1062   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1063   EXPECT_EQ(0, frame_out->CodecSpecific()->codecSpecific.VP9.temporal_idx);
1064   EXPECT_FALSE(
1065       frame_out->CodecSpecific()->codecSpecific.VP9.temporal_up_switch);
1066   jitter_buffer_->ReleaseFrame(frame_out);
1067 
1068   frame_out = DecodeCompleteFrame();
1069   EXPECT_EQ(6000U, frame_out->TimeStamp());
1070   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
1071   EXPECT_EQ(1, frame_out->CodecSpecific()->codecSpecific.VP9.temporal_idx);
1072   EXPECT_TRUE(frame_out->CodecSpecific()->codecSpecific.VP9.temporal_up_switch);
1073   jitter_buffer_->ReleaseFrame(frame_out);
1074 }
1075 
TEST_F(TestBasicJitterBuffer,H264InsertStartCode)1076 TEST_F(TestBasicJitterBuffer, H264InsertStartCode) {
1077   packet_->frameType = kVideoFrameKey;
1078   packet_->isFirstPacket = true;
1079   packet_->markerBit = false;
1080   packet_->seqNum = seq_num_;
1081   packet_->timestamp = timestamp_;
1082   packet_->insertStartCode = true;
1083 
1084   bool retransmitted = false;
1085   EXPECT_EQ(kIncomplete,
1086             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1087 
1088   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1089 
1090   // Frame should not be complete.
1091   EXPECT_TRUE(frame_out == NULL);
1092 
1093   seq_num_++;
1094   packet_->isFirstPacket = false;
1095   packet_->markerBit = true;
1096   packet_->seqNum = seq_num_;
1097 
1098   EXPECT_EQ(kCompleteSession,
1099             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1100 
1101   frame_out = DecodeCompleteFrame();
1102   CheckOutFrame(frame_out, size_ * 2 + 4 * 2, true);
1103   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1104   jitter_buffer_->ReleaseFrame(frame_out);
1105 }
1106 
1107 // Test threshold conditions of decodable state.
TEST_F(TestBasicJitterBuffer,PacketLossWithSelectiveErrorsThresholdCheck)1108 TEST_F(TestBasicJitterBuffer, PacketLossWithSelectiveErrorsThresholdCheck) {
1109   jitter_buffer_->SetDecodeErrorMode(kSelectiveErrors);
1110   // Always start with a key frame. Use 10 packets to test Decodable State
1111   // boundaries.
1112   packet_->frameType = kVideoFrameKey;
1113   packet_->isFirstPacket = true;
1114   packet_->markerBit = false;
1115   packet_->seqNum = seq_num_;
1116   packet_->timestamp = timestamp_;
1117 
1118   bool retransmitted = false;
1119   EXPECT_EQ(kIncomplete,
1120             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1121   uint32_t timestamp = 0;
1122   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
1123   EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
1124 
1125   packet_->isFirstPacket = false;
1126   for (int i = 1; i < 9; ++i) {
1127     packet_->seqNum++;
1128     EXPECT_EQ(kIncomplete,
1129               jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1130     EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
1131     EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
1132   }
1133 
1134   // last packet
1135   packet_->markerBit = true;
1136   packet_->seqNum++;
1137 
1138   EXPECT_EQ(kCompleteSession,
1139             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1140   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1141   CheckOutFrame(frame_out, 10 * size_, false);
1142   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1143   jitter_buffer_->ReleaseFrame(frame_out);
1144 
1145   // An incomplete frame can only be decoded once a subsequent frame has begun
1146   // to arrive. Insert packet in distant frame for this purpose.
1147   packet_->frameType = kVideoFrameDelta;
1148   packet_->isFirstPacket = true;
1149   packet_->markerBit = false;
1150   packet_->seqNum += 100;
1151   packet_->timestamp += 33 * 90 * 8;
1152 
1153   EXPECT_EQ(kDecodableSession,
1154             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1155   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
1156   EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
1157 
1158   // Insert second frame
1159   packet_->seqNum -= 99;
1160   packet_->timestamp -= 33 * 90 * 7;
1161 
1162   EXPECT_EQ(kDecodableSession,
1163             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1164   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
1165   EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
1166 
1167   packet_->isFirstPacket = false;
1168   for (int i = 1; i < 8; ++i) {
1169     packet_->seqNum++;
1170     EXPECT_EQ(kDecodableSession,
1171               jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1172     EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
1173     EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
1174   }
1175 
1176   packet_->seqNum++;
1177   EXPECT_EQ(kDecodableSession,
1178             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1179   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
1180   EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
1181 
1182   frame_out = DecodeIncompleteFrame();
1183   ASSERT_FALSE(NULL == frame_out);
1184   CheckOutFrame(frame_out, 9 * size_, false);
1185   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
1186   jitter_buffer_->ReleaseFrame(frame_out);
1187 
1188   packet_->markerBit = true;
1189   packet_->seqNum++;
1190   EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1191 }
1192 
1193 // Make sure first packet is present before a frame can be decoded.
TEST_F(TestBasicJitterBuffer,PacketLossWithSelectiveErrorsIncompleteKey)1194 TEST_F(TestBasicJitterBuffer, PacketLossWithSelectiveErrorsIncompleteKey) {
1195   jitter_buffer_->SetDecodeErrorMode(kSelectiveErrors);
1196   // Always start with a key frame.
1197   packet_->frameType = kVideoFrameKey;
1198   packet_->isFirstPacket = true;
1199   packet_->markerBit = true;
1200   packet_->seqNum = seq_num_;
1201   packet_->timestamp = timestamp_;
1202 
1203   bool retransmitted = false;
1204   EXPECT_EQ(kCompleteSession,
1205             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1206   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1207   CheckOutFrame(frame_out, size_, false);
1208   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1209   jitter_buffer_->ReleaseFrame(frame_out);
1210 
1211   // An incomplete frame can only be decoded once a subsequent frame has begun
1212   // to arrive. Insert packet in distant frame for this purpose.
1213   packet_->frameType = kVideoFrameDelta;
1214   packet_->isFirstPacket = false;
1215   packet_->markerBit = false;
1216   packet_->seqNum += 100;
1217   packet_->timestamp += 33 * 90 * 8;
1218   EXPECT_EQ(kIncomplete,
1219             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1220   uint32_t timestamp;
1221   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
1222   EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
1223 
1224   // Insert second frame - an incomplete key frame.
1225   packet_->frameType = kVideoFrameKey;
1226   packet_->isFirstPacket = true;
1227   packet_->seqNum -= 99;
1228   packet_->timestamp -= 33 * 90 * 7;
1229 
1230   EXPECT_EQ(kIncomplete,
1231             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1232   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
1233   EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
1234 
1235   // Insert a few more packets. Make sure we're waiting for the key frame to be
1236   // complete.
1237   packet_->isFirstPacket = false;
1238   for (int i = 1; i < 5; ++i) {
1239     packet_->seqNum++;
1240     EXPECT_EQ(kIncomplete,
1241               jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1242     EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
1243     EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
1244   }
1245 
1246   // Complete key frame.
1247   packet_->markerBit = true;
1248   packet_->seqNum++;
1249   EXPECT_EQ(kCompleteSession,
1250             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1251   frame_out = DecodeCompleteFrame();
1252   CheckOutFrame(frame_out, 6 * size_, false);
1253   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1254   jitter_buffer_->ReleaseFrame(frame_out);
1255 }
1256 
1257 // Make sure first packet is present before a frame can be decoded.
TEST_F(TestBasicJitterBuffer,PacketLossWithSelectiveErrorsMissingFirstPacket)1258 TEST_F(TestBasicJitterBuffer, PacketLossWithSelectiveErrorsMissingFirstPacket) {
1259   jitter_buffer_->SetDecodeErrorMode(kSelectiveErrors);
1260   // Always start with a key frame.
1261   packet_->frameType = kVideoFrameKey;
1262   packet_->isFirstPacket = true;
1263   packet_->markerBit = true;
1264   packet_->seqNum = seq_num_;
1265   packet_->timestamp = timestamp_;
1266 
1267   bool retransmitted = false;
1268   EXPECT_EQ(kCompleteSession,
1269             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1270   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1271   CheckOutFrame(frame_out, size_, false);
1272   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1273   jitter_buffer_->ReleaseFrame(frame_out);
1274 
1275   // An incomplete frame can only be decoded once a subsequent frame has begun
1276   // to arrive. Insert packet in distant frame for this purpose.
1277   packet_->frameType = kVideoFrameDelta;
1278   packet_->isFirstPacket = false;
1279   packet_->markerBit = false;
1280   packet_->seqNum += 100;
1281   packet_->timestamp += 33 * 90 * 8;
1282   EXPECT_EQ(kIncomplete,
1283             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1284   uint32_t timestamp;
1285   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
1286   EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
1287 
1288   // Insert second frame with the first packet missing. Make sure we're waiting
1289   // for the key frame to be complete.
1290   packet_->seqNum -= 98;
1291   packet_->timestamp -= 33 * 90 * 7;
1292 
1293   EXPECT_EQ(kIncomplete,
1294             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1295   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
1296   EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
1297 
1298   for (int i = 0; i < 5; ++i) {
1299     packet_->seqNum++;
1300     EXPECT_EQ(kIncomplete,
1301               jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1302     EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
1303     EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
1304   }
1305 
1306   // Add first packet. Frame should now be decodable, but incomplete.
1307   packet_->isFirstPacket = true;
1308   packet_->seqNum -= 6;
1309   EXPECT_EQ(kDecodableSession,
1310             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1311   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
1312   EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
1313 
1314   frame_out = DecodeIncompleteFrame();
1315   CheckOutFrame(frame_out, 7 * size_, false);
1316   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
1317   jitter_buffer_->ReleaseFrame(frame_out);
1318 }
1319 
TEST_F(TestBasicJitterBuffer,DiscontinuousStreamWhenDecodingWithErrors)1320 TEST_F(TestBasicJitterBuffer, DiscontinuousStreamWhenDecodingWithErrors) {
1321   // Will use one packet per frame.
1322   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
1323   packet_->frameType = kVideoFrameKey;
1324   packet_->isFirstPacket = true;
1325   packet_->markerBit = true;
1326   packet_->seqNum = seq_num_;
1327   packet_->timestamp = timestamp_;
1328   bool retransmitted = false;
1329   EXPECT_EQ(kCompleteSession,
1330             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1331   uint32_t next_timestamp;
1332   EXPECT_TRUE(jitter_buffer_->NextCompleteTimestamp(0, &next_timestamp));
1333   EXPECT_EQ(packet_->timestamp, next_timestamp);
1334   VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(next_timestamp);
1335   EXPECT_TRUE(frame != NULL);
1336   jitter_buffer_->ReleaseFrame(frame);
1337 
1338   // Drop a complete frame.
1339   timestamp_ += 2 * 33 * 90;
1340   seq_num_ += 2;
1341   packet_->frameType = kVideoFrameDelta;
1342   packet_->isFirstPacket = true;
1343   packet_->markerBit = false;
1344   packet_->seqNum = seq_num_;
1345   packet_->timestamp = timestamp_;
1346   EXPECT_EQ(kDecodableSession,
1347             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1348   // Insert a packet (so the previous one will be released).
1349   timestamp_ += 33 * 90;
1350   seq_num_ += 2;
1351   packet_->frameType = kVideoFrameDelta;
1352   packet_->isFirstPacket = true;
1353   packet_->markerBit = false;
1354   packet_->seqNum = seq_num_;
1355   packet_->timestamp = timestamp_;
1356   EXPECT_EQ(kDecodableSession,
1357             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1358   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &next_timestamp));
1359   EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(&next_timestamp));
1360   EXPECT_EQ(packet_->timestamp - 33 * 90, next_timestamp);
1361 }
1362 
TEST_F(TestBasicJitterBuffer,PacketLoss)1363 TEST_F(TestBasicJitterBuffer, PacketLoss) {
1364   // Verify missing packets statistics and not decodable packets statistics.
1365   // Insert 10 frames consisting of 4 packets and remove one from all of them.
1366   // The last packet is an empty (non-media) packet.
1367 
1368   // Select a start seqNum which triggers a difficult wrap situation
1369   // The JB will only output (incomplete)frames if the next one has started
1370   // to arrive. Start by inserting one frame (key).
1371   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
1372   seq_num_ = 0xffff - 4;
1373   seq_num_++;
1374   packet_->frameType = kVideoFrameKey;
1375   packet_->isFirstPacket = true;
1376   packet_->markerBit = false;
1377   packet_->seqNum = seq_num_;
1378   packet_->timestamp = timestamp_;
1379   packet_->completeNALU = kNaluStart;
1380 
1381   bool retransmitted = false;
1382   EXPECT_EQ(kDecodableSession,
1383             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1384   for (int i = 0; i < 11; ++i) {
1385     webrtc::FrameType frametype = kVideoFrameDelta;
1386     seq_num_++;
1387     timestamp_ += 33 * 90;
1388     packet_->frameType = frametype;
1389     packet_->isFirstPacket = true;
1390     packet_->markerBit = false;
1391     packet_->seqNum = seq_num_;
1392     packet_->timestamp = timestamp_;
1393     packet_->completeNALU = kNaluStart;
1394 
1395     EXPECT_EQ(kDecodableSession,
1396               jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1397 
1398     VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1399 
1400     // Should not be complete.
1401     EXPECT_TRUE(frame_out == NULL);
1402 
1403     seq_num_ += 2;
1404     packet_->isFirstPacket = false;
1405     packet_->markerBit = true;
1406     packet_->seqNum = seq_num_;
1407     packet_->completeNALU = kNaluEnd;
1408 
1409     EXPECT_EQ(jitter_buffer_->InsertPacket(*packet_, &retransmitted),
1410               kDecodableSession);
1411 
1412     // Insert an empty (non-media) packet.
1413     seq_num_++;
1414     packet_->isFirstPacket = false;
1415     packet_->markerBit = false;
1416     packet_->seqNum = seq_num_;
1417     packet_->completeNALU = kNaluEnd;
1418     packet_->frameType = kEmptyFrame;
1419 
1420     EXPECT_EQ(jitter_buffer_->InsertPacket(*packet_, &retransmitted),
1421               kDecodableSession);
1422     frame_out = DecodeIncompleteFrame();
1423 
1424     // One of the packets has been discarded by the jitter buffer.
1425     // Last frame can't be extracted yet.
1426     if (i < 10) {
1427       CheckOutFrame(frame_out, size_, false);
1428 
1429       if (i == 0) {
1430         EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1431       } else {
1432         EXPECT_EQ(frametype, frame_out->FrameType());
1433       }
1434       EXPECT_FALSE(frame_out->Complete());
1435       EXPECT_FALSE(frame_out->MissingFrame());
1436     }
1437 
1438     jitter_buffer_->ReleaseFrame(frame_out);
1439   }
1440 
1441   // Insert 3 old packets and verify that we have 3 discarded packets
1442   // Match value to actual latest timestamp decoded.
1443   timestamp_ -= 33 * 90;
1444   packet_->timestamp = timestamp_ - 1000;
1445 
1446   EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1447 
1448   packet_->timestamp = timestamp_ - 500;
1449 
1450   EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1451 
1452   packet_->timestamp = timestamp_ - 100;
1453 
1454   EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1455 
1456   EXPECT_EQ(3, jitter_buffer_->num_discarded_packets());
1457 
1458   jitter_buffer_->Flush();
1459 
1460   // This statistic shouldn't be reset by a flush.
1461   EXPECT_EQ(3, jitter_buffer_->num_discarded_packets());
1462 }
1463 
TEST_F(TestBasicJitterBuffer,DeltaFrame100PacketsWithSeqNumWrap)1464 TEST_F(TestBasicJitterBuffer, DeltaFrame100PacketsWithSeqNumWrap) {
1465   seq_num_ = 0xfff0;
1466   packet_->frameType = kVideoFrameKey;
1467   packet_->isFirstPacket = true;
1468   packet_->markerBit = false;
1469   packet_->seqNum = seq_num_;
1470   packet_->timestamp = timestamp_;
1471 
1472   bool retransmitted = false;
1473   EXPECT_EQ(kIncomplete,
1474             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1475 
1476   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1477 
1478   EXPECT_TRUE(frame_out == NULL);
1479 
1480   int loop = 0;
1481   do {
1482     seq_num_++;
1483     packet_->isFirstPacket = false;
1484     packet_->markerBit = false;
1485     packet_->seqNum = seq_num_;
1486 
1487     EXPECT_EQ(kIncomplete,
1488               jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1489 
1490     frame_out = DecodeCompleteFrame();
1491 
1492     EXPECT_TRUE(frame_out == NULL);
1493 
1494     loop++;
1495   } while (loop < 98);
1496 
1497   seq_num_++;
1498   packet_->isFirstPacket = false;
1499   packet_->markerBit = true;
1500   packet_->seqNum = seq_num_;
1501 
1502   EXPECT_EQ(kCompleteSession,
1503             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1504 
1505   frame_out = DecodeCompleteFrame();
1506 
1507   CheckOutFrame(frame_out, 100 * size_, false);
1508 
1509   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1510   jitter_buffer_->ReleaseFrame(frame_out);
1511 }
1512 
TEST_F(TestBasicJitterBuffer,PacketReorderingReverseWithNegSeqNumWrap)1513 TEST_F(TestBasicJitterBuffer, PacketReorderingReverseWithNegSeqNumWrap) {
1514   // Insert "first" packet last seqnum.
1515   seq_num_ = 10;
1516   packet_->frameType = kVideoFrameKey;
1517   packet_->isFirstPacket = false;
1518   packet_->markerBit = true;
1519   packet_->seqNum = seq_num_;
1520 
1521   bool retransmitted = false;
1522   EXPECT_EQ(kIncomplete,
1523             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1524   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1525 
1526   // Should not be complete.
1527   EXPECT_TRUE(frame_out == NULL);
1528 
1529   // Insert 98 frames.
1530   int loop = 0;
1531   do {
1532     seq_num_--;
1533     packet_->isFirstPacket = false;
1534     packet_->markerBit = false;
1535     packet_->seqNum = seq_num_;
1536 
1537     EXPECT_EQ(kIncomplete,
1538               jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1539 
1540     frame_out = DecodeCompleteFrame();
1541 
1542     EXPECT_TRUE(frame_out == NULL);
1543 
1544     loop++;
1545   } while (loop < 98);
1546 
1547   // Insert last packet.
1548   seq_num_--;
1549   packet_->isFirstPacket = true;
1550   packet_->markerBit = false;
1551   packet_->seqNum = seq_num_;
1552 
1553   EXPECT_EQ(kCompleteSession,
1554             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1555 
1556   frame_out = DecodeCompleteFrame();
1557   CheckOutFrame(frame_out, 100 * size_, false);
1558   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1559   jitter_buffer_->ReleaseFrame(frame_out);
1560 }
1561 
TEST_F(TestBasicJitterBuffer,TestInsertOldFrame)1562 TEST_F(TestBasicJitterBuffer, TestInsertOldFrame) {
1563   //   -------      -------
1564   //  |   2   |    |   1   |
1565   //   -------      -------
1566   //  t = 3000     t = 2000
1567   seq_num_ = 2;
1568   timestamp_ = 3000;
1569   packet_->frameType = kVideoFrameKey;
1570   packet_->isFirstPacket = true;
1571   packet_->markerBit = true;
1572   packet_->timestamp = timestamp_;
1573   packet_->seqNum = seq_num_;
1574 
1575   bool retransmitted = false;
1576   EXPECT_EQ(kCompleteSession,
1577             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1578 
1579   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1580   EXPECT_EQ(3000u, frame_out->TimeStamp());
1581   CheckOutFrame(frame_out, size_, false);
1582   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1583   jitter_buffer_->ReleaseFrame(frame_out);
1584 
1585   seq_num_--;
1586   timestamp_ = 2000;
1587   packet_->frameType = kVideoFrameDelta;
1588   packet_->isFirstPacket = true;
1589   packet_->markerBit = true;
1590   packet_->seqNum = seq_num_;
1591   packet_->timestamp = timestamp_;
1592 
1593   EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1594 }
1595 
TEST_F(TestBasicJitterBuffer,TestInsertOldFrameWithSeqNumWrap)1596 TEST_F(TestBasicJitterBuffer, TestInsertOldFrameWithSeqNumWrap) {
1597   //   -------      -------
1598   //  |   2   |    |   1   |
1599   //   -------      -------
1600   //  t = 3000     t = 0xffffff00
1601 
1602   seq_num_ = 2;
1603   timestamp_ = 3000;
1604   packet_->frameType = kVideoFrameKey;
1605   packet_->isFirstPacket = true;
1606   packet_->markerBit = true;
1607   packet_->seqNum = seq_num_;
1608   packet_->timestamp = timestamp_;
1609 
1610   bool retransmitted = false;
1611   EXPECT_EQ(kCompleteSession,
1612             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1613 
1614   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1615   EXPECT_EQ(timestamp_, frame_out->TimeStamp());
1616 
1617   CheckOutFrame(frame_out, size_, false);
1618 
1619   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1620 
1621   jitter_buffer_->ReleaseFrame(frame_out);
1622 
1623   seq_num_--;
1624   timestamp_ = 0xffffff00;
1625   packet_->frameType = kVideoFrameDelta;
1626   packet_->isFirstPacket = true;
1627   packet_->markerBit = true;
1628   packet_->seqNum = seq_num_;
1629   packet_->timestamp = timestamp_;
1630 
1631   // This timestamp is old.
1632   EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1633 }
1634 
TEST_F(TestBasicJitterBuffer,TimestampWrap)1635 TEST_F(TestBasicJitterBuffer, TimestampWrap) {
1636   //  ---------------     ---------------
1637   // |   1   |   2   |   |   3   |   4   |
1638   //  ---------------     ---------------
1639   //  t = 0xffffff00        t = 33*90
1640 
1641   timestamp_ = 0xffffff00;
1642   packet_->frameType = kVideoFrameKey;
1643   packet_->isFirstPacket = true;
1644   packet_->markerBit = false;
1645   packet_->seqNum = seq_num_;
1646   packet_->timestamp = timestamp_;
1647 
1648   bool retransmitted = false;
1649   EXPECT_EQ(kIncomplete,
1650             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1651 
1652   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1653   EXPECT_TRUE(frame_out == NULL);
1654 
1655   seq_num_++;
1656   packet_->isFirstPacket = false;
1657   packet_->markerBit = true;
1658   packet_->seqNum = seq_num_;
1659 
1660   EXPECT_EQ(kCompleteSession,
1661             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1662 
1663   frame_out = DecodeCompleteFrame();
1664   CheckOutFrame(frame_out, 2 * size_, false);
1665   jitter_buffer_->ReleaseFrame(frame_out);
1666 
1667   seq_num_++;
1668   timestamp_ += 33 * 90;
1669   packet_->frameType = kVideoFrameDelta;
1670   packet_->isFirstPacket = true;
1671   packet_->markerBit = false;
1672   packet_->seqNum = seq_num_;
1673   packet_->timestamp = timestamp_;
1674 
1675   EXPECT_EQ(kIncomplete,
1676             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1677 
1678   frame_out = DecodeCompleteFrame();
1679   EXPECT_TRUE(frame_out == NULL);
1680 
1681   seq_num_++;
1682   packet_->isFirstPacket = false;
1683   packet_->markerBit = true;
1684   packet_->seqNum = seq_num_;
1685 
1686   EXPECT_EQ(kCompleteSession,
1687             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1688 
1689   frame_out = DecodeCompleteFrame();
1690   CheckOutFrame(frame_out, 2 * size_, false);
1691   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
1692   jitter_buffer_->ReleaseFrame(frame_out);
1693 }
1694 
1695 TEST_F(TestBasicJitterBuffer, 2FrameWithTimestampWrap) {
1696   //   -------          -------
1697   //  |   1   |        |   2   |
1698   //   -------          -------
1699   // t = 0xffffff00    t = 2700
1700 
1701   timestamp_ = 0xffffff00;
1702   packet_->frameType = kVideoFrameKey;
1703   packet_->isFirstPacket = true;
1704   packet_->markerBit = true;
1705   packet_->timestamp = timestamp_;
1706 
1707   bool retransmitted = false;
1708   // Insert first frame (session will be complete).
1709   EXPECT_EQ(kCompleteSession,
1710             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1711 
1712   // Insert next frame.
1713   seq_num_++;
1714   timestamp_ = 2700;
1715   packet_->frameType = kVideoFrameDelta;
1716   packet_->isFirstPacket = true;
1717   packet_->markerBit = true;
1718   packet_->seqNum = seq_num_;
1719   packet_->timestamp = timestamp_;
1720 
1721   EXPECT_EQ(kCompleteSession,
1722             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1723 
1724   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1725   EXPECT_EQ(0xffffff00, frame_out->TimeStamp());
1726   CheckOutFrame(frame_out, size_, false);
1727   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1728   jitter_buffer_->ReleaseFrame(frame_out);
1729 
1730   VCMEncodedFrame* frame_out2 = DecodeCompleteFrame();
1731   EXPECT_EQ(2700u, frame_out2->TimeStamp());
1732   CheckOutFrame(frame_out2, size_, false);
1733   EXPECT_EQ(kVideoFrameDelta, frame_out2->FrameType());
1734   jitter_buffer_->ReleaseFrame(frame_out2);
1735 }
1736 
TEST_F(TestBasicJitterBuffer,Insert2FramesReOrderedWithTimestampWrap)1737 TEST_F(TestBasicJitterBuffer, Insert2FramesReOrderedWithTimestampWrap) {
1738   //   -------          -------
1739   //  |   2   |        |   1   |
1740   //   -------          -------
1741   //  t = 2700        t = 0xffffff00
1742 
1743   seq_num_ = 2;
1744   timestamp_ = 2700;
1745   packet_->frameType = kVideoFrameDelta;
1746   packet_->isFirstPacket = true;
1747   packet_->markerBit = true;
1748   packet_->seqNum = seq_num_;
1749   packet_->timestamp = timestamp_;
1750 
1751   bool retransmitted = false;
1752   EXPECT_EQ(kCompleteSession,
1753             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1754 
1755   // Insert second frame
1756   seq_num_--;
1757   timestamp_ = 0xffffff00;
1758   packet_->frameType = kVideoFrameKey;
1759   packet_->isFirstPacket = true;
1760   packet_->markerBit = true;
1761   packet_->seqNum = seq_num_;
1762   packet_->timestamp = timestamp_;
1763 
1764   EXPECT_EQ(kCompleteSession,
1765             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1766 
1767   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1768   EXPECT_EQ(0xffffff00, frame_out->TimeStamp());
1769   CheckOutFrame(frame_out, size_, false);
1770   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1771   jitter_buffer_->ReleaseFrame(frame_out);
1772 
1773   VCMEncodedFrame* frame_out2 = DecodeCompleteFrame();
1774   EXPECT_EQ(2700u, frame_out2->TimeStamp());
1775   CheckOutFrame(frame_out2, size_, false);
1776   EXPECT_EQ(kVideoFrameDelta, frame_out2->FrameType());
1777   jitter_buffer_->ReleaseFrame(frame_out2);
1778 }
1779 
TEST_F(TestBasicJitterBuffer,DeltaFrameWithMoreThanMaxNumberOfPackets)1780 TEST_F(TestBasicJitterBuffer, DeltaFrameWithMoreThanMaxNumberOfPackets) {
1781   int loop = 0;
1782   bool firstPacket = true;
1783   bool retransmitted = false;
1784   // Insert kMaxPacketsInJitterBuffer into frame.
1785   do {
1786     seq_num_++;
1787     packet_->isFirstPacket = false;
1788     packet_->markerBit = false;
1789     packet_->seqNum = seq_num_;
1790 
1791     if (firstPacket) {
1792       EXPECT_EQ(kIncomplete,
1793                 jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1794       firstPacket = false;
1795     } else {
1796       EXPECT_EQ(kIncomplete,
1797                 jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1798     }
1799 
1800     loop++;
1801   } while (loop < kMaxPacketsInSession);
1802 
1803   // Max number of packets inserted.
1804   // Insert one more packet.
1805   seq_num_++;
1806   packet_->isFirstPacket = false;
1807   packet_->markerBit = true;
1808   packet_->seqNum = seq_num_;
1809 
1810   // Insert the packet -> frame recycled.
1811   EXPECT_EQ(kSizeError, jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1812   EXPECT_TRUE(NULL == DecodeCompleteFrame());
1813 }
1814 
TEST_F(TestBasicJitterBuffer,ExceedNumOfFrameWithSeqNumWrap)1815 TEST_F(TestBasicJitterBuffer, ExceedNumOfFrameWithSeqNumWrap) {
1816   // TEST fill JB with more than max number of frame (50 delta frames +
1817   // 51 key frames) with wrap in seq_num_
1818   //
1819   //  --------------------------------------------------------------
1820   // | 65485 | 65486 | 65487 | .... | 65535 | 0 | 1 | 2 | .....| 50 |
1821   //  --------------------------------------------------------------
1822   // |<-----------delta frames------------->|<------key frames----->|
1823 
1824   // Make sure the jitter doesn't request a keyframe after too much non-
1825   // decodable frames.
1826   jitter_buffer_->SetNackMode(kNack, -1, -1);
1827   jitter_buffer_->SetNackSettings(kMaxNumberOfFrames, kMaxNumberOfFrames, 0);
1828 
1829   int loop = 0;
1830   seq_num_ = 65485;
1831   uint32_t first_key_frame_timestamp = 0;
1832   bool retransmitted = false;
1833   // Insert MAX_NUMBER_OF_FRAMES frames.
1834   do {
1835     timestamp_ += 33 * 90;
1836     seq_num_++;
1837     packet_->isFirstPacket = true;
1838     packet_->markerBit = true;
1839     packet_->seqNum = seq_num_;
1840     packet_->timestamp = timestamp_;
1841 
1842     if (loop == 50) {
1843       first_key_frame_timestamp = packet_->timestamp;
1844       packet_->frameType = kVideoFrameKey;
1845     }
1846 
1847     // Insert frame.
1848     EXPECT_EQ(kCompleteSession,
1849               jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1850 
1851     loop++;
1852   } while (loop < kMaxNumberOfFrames);
1853 
1854   // Max number of frames inserted.
1855 
1856   // Insert one more frame.
1857   timestamp_ += 33 * 90;
1858   seq_num_++;
1859   packet_->isFirstPacket = true;
1860   packet_->markerBit = true;
1861   packet_->seqNum = seq_num_;
1862   packet_->timestamp = timestamp_;
1863 
1864   // Now, no free frame - frames will be recycled until first key frame.
1865   EXPECT_EQ(kFlushIndicator,
1866             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1867 
1868   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1869   EXPECT_EQ(first_key_frame_timestamp, frame_out->TimeStamp());
1870   CheckOutFrame(frame_out, size_, false);
1871   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1872   jitter_buffer_->ReleaseFrame(frame_out);
1873 }
1874 
TEST_F(TestBasicJitterBuffer,EmptyLastFrame)1875 TEST_F(TestBasicJitterBuffer, EmptyLastFrame) {
1876   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
1877   seq_num_ = 3;
1878   // Insert one empty packet per frame, should never return the last timestamp
1879   // inserted. Only return empty frames in the presence of subsequent frames.
1880   int maxSize = 1000;
1881   bool retransmitted = false;
1882   for (int i = 0; i < maxSize + 10; i++) {
1883     timestamp_ += 33 * 90;
1884     seq_num_++;
1885     packet_->isFirstPacket = false;
1886     packet_->markerBit = false;
1887     packet_->seqNum = seq_num_;
1888     packet_->timestamp = timestamp_;
1889     packet_->frameType = kEmptyFrame;
1890 
1891     EXPECT_EQ(kNoError, jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1892     VCMEncodedFrame* testFrame = DecodeIncompleteFrame();
1893     // Timestamp should never be the last TS inserted.
1894     if (testFrame != NULL) {
1895       EXPECT_TRUE(testFrame->TimeStamp() < timestamp_);
1896       jitter_buffer_->ReleaseFrame(testFrame);
1897     }
1898   }
1899 }
1900 
TEST_F(TestBasicJitterBuffer,H264IncompleteNalu)1901 TEST_F(TestBasicJitterBuffer, H264IncompleteNalu) {
1902   jitter_buffer_->SetNackMode(kNoNack, -1, -1);
1903   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
1904   ++seq_num_;
1905   timestamp_ += 33 * 90;
1906   int insertedLength = 0;
1907   packet_->seqNum = seq_num_;
1908   packet_->timestamp = timestamp_;
1909   packet_->frameType = kVideoFrameKey;
1910   packet_->isFirstPacket = true;
1911   packet_->completeNALU = kNaluStart;
1912   packet_->markerBit = false;
1913   bool retransmitted = false;
1914 
1915   EXPECT_EQ(kDecodableSession,
1916             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1917 
1918   seq_num_ += 2;  // Skip one packet.
1919   packet_->seqNum = seq_num_;
1920   packet_->frameType = kVideoFrameKey;
1921   packet_->isFirstPacket = false;
1922   packet_->completeNALU = kNaluIncomplete;
1923   packet_->markerBit = false;
1924 
1925   EXPECT_EQ(kDecodableSession,
1926             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1927 
1928   seq_num_++;
1929   packet_->seqNum = seq_num_;
1930   packet_->frameType = kVideoFrameKey;
1931   packet_->isFirstPacket = false;
1932   packet_->completeNALU = kNaluEnd;
1933   packet_->markerBit = false;
1934 
1935   EXPECT_EQ(kDecodableSession,
1936             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1937 
1938   seq_num_++;
1939   packet_->seqNum = seq_num_;
1940   packet_->completeNALU = kNaluComplete;
1941   packet_->markerBit = true;  // Last packet.
1942   EXPECT_EQ(kDecodableSession,
1943             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1944   // The JB will only output (incomplete) frames if a packet belonging to a
1945   // subsequent frame was already inserted. Insert one packet of a subsequent
1946   // frame. place high timestamp so the JB would always have a next frame
1947   // (otherwise, for every inserted frame we need to take care of the next
1948   // frame as well).
1949   packet_->seqNum = 1;
1950   packet_->timestamp = timestamp_ + 33 * 90 * 10;
1951   packet_->frameType = kVideoFrameDelta;
1952   packet_->isFirstPacket = false;
1953   packet_->completeNALU = kNaluStart;
1954   packet_->markerBit = false;
1955 
1956   EXPECT_EQ(kDecodableSession,
1957             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1958 
1959   VCMEncodedFrame* frame_out = DecodeIncompleteFrame();
1960 
1961   // We can decode everything from a NALU until a packet has been lost.
1962   // Thus we can decode the first packet of the first NALU and the second NALU
1963   // which consists of one packet.
1964   CheckOutFrame(frame_out, packet_->sizeBytes * 2, false);
1965   jitter_buffer_->ReleaseFrame(frame_out);
1966 
1967   // Test reordered start frame + 1 lost.
1968   seq_num_ += 2;  // Re-order 1 frame.
1969   timestamp_ += 33 * 90;
1970   insertedLength = 0;
1971 
1972   packet_->seqNum = seq_num_;
1973   packet_->timestamp = timestamp_;
1974   packet_->frameType = kVideoFrameKey;
1975   packet_->isFirstPacket = false;
1976   packet_->completeNALU = kNaluEnd;
1977   packet_->markerBit = false;
1978   EXPECT_EQ(kDecodableSession,
1979             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1980   insertedLength += packet_->sizeBytes;  // This packet should be decoded.
1981   seq_num_--;
1982   packet_->seqNum = seq_num_;
1983   packet_->timestamp = timestamp_;
1984   packet_->frameType = kVideoFrameKey;
1985   packet_->isFirstPacket = true;
1986   packet_->completeNALU = kNaluStart;
1987   packet_->markerBit = false;
1988 
1989   EXPECT_EQ(kDecodableSession,
1990             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
1991   insertedLength += packet_->sizeBytes;  // This packet should be decoded.
1992 
1993   seq_num_ += 3;  // One packet drop.
1994   packet_->seqNum = seq_num_;
1995   packet_->timestamp = timestamp_;
1996   packet_->frameType = kVideoFrameKey;
1997   packet_->isFirstPacket = false;
1998   packet_->completeNALU = kNaluComplete;
1999   packet_->markerBit = false;
2000   EXPECT_EQ(kDecodableSession,
2001             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
2002   insertedLength += packet_->sizeBytes;  // This packet should be decoded.
2003   seq_num_++;
2004   packet_->seqNum = seq_num_;
2005   packet_->timestamp = timestamp_;
2006   packet_->frameType = kVideoFrameKey;
2007   packet_->isFirstPacket = false;
2008   packet_->completeNALU = kNaluStart;
2009   packet_->markerBit = false;
2010   EXPECT_EQ(kDecodableSession,
2011             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
2012   // This packet should be decoded since it's the beginning of a NAL.
2013   insertedLength += packet_->sizeBytes;
2014 
2015   seq_num_ += 2;
2016   packet_->seqNum = seq_num_;
2017   packet_->timestamp = timestamp_;
2018   packet_->frameType = kVideoFrameKey;
2019   packet_->isFirstPacket = false;
2020   packet_->completeNALU = kNaluEnd;
2021   packet_->markerBit = true;
2022   EXPECT_EQ(kDecodableSession,
2023             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
2024   // This packet should not be decoded because it is an incomplete NAL if it
2025   // is the last.
2026   frame_out = DecodeIncompleteFrame();
2027   // Only last NALU is complete.
2028   CheckOutFrame(frame_out, insertedLength, false);
2029   jitter_buffer_->ReleaseFrame(frame_out);
2030 
2031   // Test to insert empty packet.
2032   seq_num_++;
2033   timestamp_ += 33 * 90;
2034   VCMPacket emptypacket(data_, 0, seq_num_, timestamp_, true);
2035   emptypacket.seqNum = seq_num_;
2036   emptypacket.timestamp = timestamp_;
2037   emptypacket.frameType = kVideoFrameKey;
2038   emptypacket.isFirstPacket = true;
2039   emptypacket.completeNALU = kNaluComplete;
2040   emptypacket.markerBit = true;
2041   EXPECT_EQ(kCompleteSession,
2042             jitter_buffer_->InsertPacket(emptypacket, &retransmitted));
2043   // This packet should not be decoded because it is an incomplete NAL if it
2044   // is the last.
2045 
2046   // Will be sent to the decoder, as a packet belonging to a subsequent frame
2047   // has arrived.
2048   frame_out = DecodeIncompleteFrame();
2049   EXPECT_TRUE(frame_out != NULL);
2050   jitter_buffer_->ReleaseFrame(frame_out);
2051 
2052   // Test that a frame can include an empty packet.
2053   seq_num_++;
2054   timestamp_ += 33 * 90;
2055 
2056   packet_->seqNum = seq_num_;
2057   packet_->timestamp = timestamp_;
2058   packet_->frameType = kVideoFrameKey;
2059   packet_->isFirstPacket = true;
2060   packet_->completeNALU = kNaluComplete;
2061   packet_->markerBit = false;
2062 
2063   EXPECT_EQ(kDecodableSession,
2064             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
2065 
2066   seq_num_++;
2067   emptypacket.seqNum = seq_num_;
2068   emptypacket.timestamp = timestamp_;
2069   emptypacket.frameType = kVideoFrameKey;
2070   emptypacket.isFirstPacket = true;
2071   emptypacket.completeNALU = kNaluComplete;
2072   emptypacket.markerBit = true;
2073   EXPECT_EQ(kCompleteSession,
2074             jitter_buffer_->InsertPacket(emptypacket, &retransmitted));
2075 
2076   frame_out = DecodeCompleteFrame();
2077   // Only last NALU is complete
2078   CheckOutFrame(frame_out, packet_->sizeBytes, false);
2079   jitter_buffer_->ReleaseFrame(frame_out);
2080 }
2081 
TEST_F(TestBasicJitterBuffer,NextFrameWhenIncomplete)2082 TEST_F(TestBasicJitterBuffer, NextFrameWhenIncomplete) {
2083   // Test that a we cannot get incomplete frames from the JB if we haven't
2084   // received the marker bit, unless we have received a packet from a later
2085   // timestamp.
2086   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
2087   // Start with a complete key frame - insert and decode.
2088   packet_->frameType = kVideoFrameKey;
2089   packet_->isFirstPacket = true;
2090   packet_->markerBit = true;
2091   bool retransmitted = false;
2092 
2093   EXPECT_EQ(kCompleteSession,
2094             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
2095   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
2096   EXPECT_TRUE(frame_out != NULL);
2097   jitter_buffer_->ReleaseFrame(frame_out);
2098 
2099   packet_->seqNum += 2;
2100   packet_->timestamp += 33 * 90;
2101   packet_->frameType = kVideoFrameDelta;
2102   packet_->isFirstPacket = false;
2103   packet_->markerBit = false;
2104 
2105   EXPECT_EQ(kDecodableSession,
2106             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
2107 
2108   frame_out = DecodeIncompleteFrame();
2109   EXPECT_TRUE(frame_out == NULL);
2110 
2111   packet_->seqNum += 2;
2112   packet_->timestamp += 33 * 90;
2113   packet_->isFirstPacket = true;
2114 
2115   EXPECT_EQ(kDecodableSession,
2116             jitter_buffer_->InsertPacket(*packet_, &retransmitted));
2117 
2118   frame_out = DecodeIncompleteFrame();
2119   CheckOutFrame(frame_out, packet_->sizeBytes, false);
2120   jitter_buffer_->ReleaseFrame(frame_out);
2121 }
2122 
TEST_F(TestRunningJitterBuffer,Full)2123 TEST_F(TestRunningJitterBuffer, Full) {
2124   // Make sure the jitter doesn't request a keyframe after too much non-
2125   // decodable frames.
2126   jitter_buffer_->SetNackMode(kNack, -1, -1);
2127   jitter_buffer_->SetNackSettings(kMaxNumberOfFrames, kMaxNumberOfFrames, 0);
2128   // Insert a key frame and decode it.
2129   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
2130   EXPECT_TRUE(DecodeCompleteFrame());
2131   DropFrame(1);
2132   // Fill the jitter buffer.
2133   EXPECT_GE(InsertFrames(kMaxNumberOfFrames, kVideoFrameDelta), kNoError);
2134   // Make sure we can't decode these frames.
2135   EXPECT_FALSE(DecodeCompleteFrame());
2136   // This frame will make the jitter buffer recycle frames until a key frame.
2137   // Since none is found it will have to wait until the next key frame before
2138   // decoding.
2139   EXPECT_EQ(kFlushIndicator, InsertFrame(kVideoFrameDelta));
2140   EXPECT_FALSE(DecodeCompleteFrame());
2141 }
2142 
TEST_F(TestRunningJitterBuffer,EmptyPackets)2143 TEST_F(TestRunningJitterBuffer, EmptyPackets) {
2144   // Make sure a frame can get complete even though empty packets are missing.
2145   stream_generator_->GenerateFrame(kVideoFrameKey, 3, 3,
2146                                    clock_->TimeInMilliseconds());
2147   bool request_key_frame = false;
2148   // Insert empty packet.
2149   EXPECT_EQ(kNoError, InsertPacketAndPop(4));
2150   EXPECT_FALSE(request_key_frame);
2151   // Insert 3 media packets.
2152   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
2153   EXPECT_FALSE(request_key_frame);
2154   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
2155   EXPECT_FALSE(request_key_frame);
2156   EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0));
2157   EXPECT_FALSE(request_key_frame);
2158   // Insert empty packet.
2159   EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0));
2160   EXPECT_FALSE(request_key_frame);
2161 }
2162 
TEST_F(TestRunningJitterBuffer,StatisticsTest)2163 TEST_F(TestRunningJitterBuffer, StatisticsTest) {
2164   FrameCounts frame_stats(jitter_buffer_->FrameStatistics());
2165   EXPECT_EQ(0, frame_stats.delta_frames);
2166   EXPECT_EQ(0, frame_stats.key_frames);
2167 
2168   uint32_t framerate = 0;
2169   uint32_t bitrate = 0;
2170   jitter_buffer_->IncomingRateStatistics(&framerate, &bitrate);
2171   EXPECT_EQ(0u, framerate);
2172   EXPECT_EQ(0u, bitrate);
2173 
2174   // Insert a couple of key and delta frames.
2175   InsertFrame(kVideoFrameKey);
2176   InsertFrame(kVideoFrameDelta);
2177   InsertFrame(kVideoFrameDelta);
2178   InsertFrame(kVideoFrameKey);
2179   InsertFrame(kVideoFrameDelta);
2180   // Decode some of them to make sure the statistics doesn't depend on frames
2181   // being decoded.
2182   EXPECT_TRUE(DecodeCompleteFrame());
2183   EXPECT_TRUE(DecodeCompleteFrame());
2184   frame_stats = jitter_buffer_->FrameStatistics();
2185   EXPECT_EQ(3, frame_stats.delta_frames);
2186   EXPECT_EQ(2, frame_stats.key_frames);
2187 
2188   // Insert 20 more frames to get estimates of bitrate and framerate over
2189   // 1 second.
2190   for (int i = 0; i < 20; ++i) {
2191     InsertFrame(kVideoFrameDelta);
2192   }
2193   jitter_buffer_->IncomingRateStatistics(&framerate, &bitrate);
2194   // TODO(holmer): The current implementation returns the average of the last
2195   // two framerate calculations, which is why it takes two calls to reach the
2196   // actual framerate. This should be fixed.
2197   EXPECT_EQ(kDefaultFrameRate / 2u, framerate);
2198   EXPECT_EQ(kDefaultBitrateKbps, bitrate);
2199   // Insert 25 more frames to get estimates of bitrate and framerate over
2200   // 2 seconds.
2201   for (int i = 0; i < 25; ++i) {
2202     InsertFrame(kVideoFrameDelta);
2203   }
2204   jitter_buffer_->IncomingRateStatistics(&framerate, &bitrate);
2205   EXPECT_EQ(kDefaultFrameRate, framerate);
2206   EXPECT_EQ(kDefaultBitrateKbps, bitrate);
2207 }
2208 
TEST_F(TestRunningJitterBuffer,SkipToKeyFrame)2209 TEST_F(TestRunningJitterBuffer, SkipToKeyFrame) {
2210   // Insert delta frames.
2211   EXPECT_GE(InsertFrames(5, kVideoFrameDelta), kNoError);
2212   // Can't decode without a key frame.
2213   EXPECT_FALSE(DecodeCompleteFrame());
2214   InsertFrame(kVideoFrameKey);
2215   // Skip to the next key frame.
2216   EXPECT_TRUE(DecodeCompleteFrame());
2217 }
2218 
TEST_F(TestRunningJitterBuffer,DontSkipToKeyFrameIfDecodable)2219 TEST_F(TestRunningJitterBuffer, DontSkipToKeyFrameIfDecodable) {
2220   InsertFrame(kVideoFrameKey);
2221   EXPECT_TRUE(DecodeCompleteFrame());
2222   const int kNumDeltaFrames = 5;
2223   EXPECT_GE(InsertFrames(kNumDeltaFrames, kVideoFrameDelta), kNoError);
2224   InsertFrame(kVideoFrameKey);
2225   for (int i = 0; i < kNumDeltaFrames + 1; ++i) {
2226     EXPECT_TRUE(DecodeCompleteFrame());
2227   }
2228 }
2229 
TEST_F(TestRunningJitterBuffer,KeyDeltaKeyDelta)2230 TEST_F(TestRunningJitterBuffer, KeyDeltaKeyDelta) {
2231   InsertFrame(kVideoFrameKey);
2232   EXPECT_TRUE(DecodeCompleteFrame());
2233   const int kNumDeltaFrames = 5;
2234   EXPECT_GE(InsertFrames(kNumDeltaFrames, kVideoFrameDelta), kNoError);
2235   InsertFrame(kVideoFrameKey);
2236   EXPECT_GE(InsertFrames(kNumDeltaFrames, kVideoFrameDelta), kNoError);
2237   InsertFrame(kVideoFrameKey);
2238   for (int i = 0; i < 2 * (kNumDeltaFrames + 1); ++i) {
2239     EXPECT_TRUE(DecodeCompleteFrame());
2240   }
2241 }
2242 
TEST_F(TestRunningJitterBuffer,TwoPacketsNonContinuous)2243 TEST_F(TestRunningJitterBuffer, TwoPacketsNonContinuous) {
2244   InsertFrame(kVideoFrameKey);
2245   EXPECT_TRUE(DecodeCompleteFrame());
2246   stream_generator_->GenerateFrame(kVideoFrameDelta, 1, 0,
2247                                    clock_->TimeInMilliseconds());
2248   clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
2249   stream_generator_->GenerateFrame(kVideoFrameDelta, 2, 0,
2250                                    clock_->TimeInMilliseconds());
2251   EXPECT_EQ(kIncomplete, InsertPacketAndPop(1));
2252   EXPECT_EQ(kCompleteSession, InsertPacketAndPop(1));
2253   EXPECT_FALSE(DecodeCompleteFrame());
2254   EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0));
2255   EXPECT_TRUE(DecodeCompleteFrame());
2256   EXPECT_TRUE(DecodeCompleteFrame());
2257 }
2258 
TEST_F(TestJitterBufferNack,EmptyPackets)2259 TEST_F(TestJitterBufferNack, EmptyPackets) {
2260   // Make sure empty packets doesn't clog the jitter buffer.
2261   jitter_buffer_->SetNackMode(kNack, media_optimization::kLowRttNackMs, -1);
2262   EXPECT_GE(InsertFrames(kMaxNumberOfFrames, kEmptyFrame), kNoError);
2263   InsertFrame(kVideoFrameKey);
2264   EXPECT_TRUE(DecodeCompleteFrame());
2265 }
2266 
TEST_F(TestJitterBufferNack,NackTooOldPackets)2267 TEST_F(TestJitterBufferNack, NackTooOldPackets) {
2268   // Insert a key frame and decode it.
2269   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
2270   EXPECT_TRUE(DecodeCompleteFrame());
2271 
2272   // Drop one frame and insert |kNackHistoryLength| to trigger NACKing a too
2273   // old packet.
2274   DropFrame(1);
2275   // Insert a frame which should trigger a recycle until the next key frame.
2276   EXPECT_EQ(kFlushIndicator,
2277             InsertFrames(oldest_packet_to_nack_ + 1, kVideoFrameDelta));
2278   EXPECT_FALSE(DecodeCompleteFrame());
2279 
2280   bool request_key_frame = false;
2281   std::vector<uint16_t> nack_list =
2282       jitter_buffer_->GetNackList(&request_key_frame);
2283   // No key frame will be requested since the jitter buffer is empty.
2284   EXPECT_FALSE(request_key_frame);
2285   EXPECT_EQ(0u, nack_list.size());
2286 
2287   EXPECT_GE(InsertFrame(kVideoFrameDelta), kNoError);
2288   // Waiting for a key frame.
2289   EXPECT_FALSE(DecodeCompleteFrame());
2290   EXPECT_FALSE(DecodeIncompleteFrame());
2291 
2292   // The next complete continuous frame isn't a key frame, but we're waiting
2293   // for one.
2294   EXPECT_FALSE(DecodeCompleteFrame());
2295   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
2296   // Skipping ahead to the key frame.
2297   EXPECT_TRUE(DecodeCompleteFrame());
2298 }
2299 
TEST_F(TestJitterBufferNack,NackLargeJitterBuffer)2300 TEST_F(TestJitterBufferNack, NackLargeJitterBuffer) {
2301   // Insert a key frame and decode it.
2302   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
2303   EXPECT_TRUE(DecodeCompleteFrame());
2304 
2305   // Insert a frame which should trigger a recycle until the next key frame.
2306   EXPECT_GE(InsertFrames(oldest_packet_to_nack_, kVideoFrameDelta), kNoError);
2307 
2308   bool request_key_frame = false;
2309   std::vector<uint16_t> nack_list =
2310       jitter_buffer_->GetNackList(&request_key_frame);
2311   // Verify that the jitter buffer does not request a key frame.
2312   EXPECT_FALSE(request_key_frame);
2313   // Verify that no packets are NACKed.
2314   EXPECT_EQ(0u, nack_list.size());
2315   // Verify that we can decode the next frame.
2316   EXPECT_TRUE(DecodeCompleteFrame());
2317 }
2318 
TEST_F(TestJitterBufferNack,NackListFull)2319 TEST_F(TestJitterBufferNack, NackListFull) {
2320   // Insert a key frame and decode it.
2321   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
2322   EXPECT_TRUE(DecodeCompleteFrame());
2323 
2324   // Generate and drop |kNackHistoryLength| packets to fill the NACK list.
2325   DropFrame(max_nack_list_size_ + 1);
2326   // Insert a frame which should trigger a recycle until the next key frame.
2327   EXPECT_EQ(kFlushIndicator, InsertFrame(kVideoFrameDelta));
2328   EXPECT_FALSE(DecodeCompleteFrame());
2329 
2330   bool request_key_frame = false;
2331   jitter_buffer_->GetNackList(&request_key_frame);
2332   // The jitter buffer is empty, so we won't request key frames until we get a
2333   // packet.
2334   EXPECT_FALSE(request_key_frame);
2335 
2336   EXPECT_GE(InsertFrame(kVideoFrameDelta), kNoError);
2337   // Now we have a packet in the jitter buffer, a key frame will be requested
2338   // since it's not a key frame.
2339   jitter_buffer_->GetNackList(&request_key_frame);
2340   // The jitter buffer is empty, so we won't request key frames until we get a
2341   // packet.
2342   EXPECT_TRUE(request_key_frame);
2343   // The next complete continuous frame isn't a key frame, but we're waiting
2344   // for one.
2345   EXPECT_FALSE(DecodeCompleteFrame());
2346   EXPECT_FALSE(DecodeIncompleteFrame());
2347   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
2348   // Skipping ahead to the key frame.
2349   EXPECT_TRUE(DecodeCompleteFrame());
2350 }
2351 
TEST_F(TestJitterBufferNack,NoNackListReturnedBeforeFirstDecode)2352 TEST_F(TestJitterBufferNack, NoNackListReturnedBeforeFirstDecode) {
2353   DropFrame(10);
2354   // Insert a frame and try to generate a NACK list. Shouldn't get one.
2355   EXPECT_GE(InsertFrame(kVideoFrameDelta), kNoError);
2356   bool request_key_frame = false;
2357   std::vector<uint16_t> nack_list =
2358       jitter_buffer_->GetNackList(&request_key_frame);
2359   // No list generated, and a key frame request is signaled.
2360   EXPECT_EQ(0u, nack_list.size());
2361   EXPECT_TRUE(request_key_frame);
2362 }
2363 
TEST_F(TestJitterBufferNack,NackListBuiltBeforeFirstDecode)2364 TEST_F(TestJitterBufferNack, NackListBuiltBeforeFirstDecode) {
2365   stream_generator_->Init(0, clock_->TimeInMilliseconds());
2366   InsertFrame(kVideoFrameKey);
2367   stream_generator_->GenerateFrame(kVideoFrameDelta, 2, 0,
2368                                    clock_->TimeInMilliseconds());
2369   stream_generator_->NextPacket(NULL);  // Drop packet.
2370   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
2371   EXPECT_TRUE(DecodeCompleteFrame());
2372   bool extended = false;
2373   std::vector<uint16_t> nack_list = jitter_buffer_->GetNackList(&extended);
2374   EXPECT_EQ(1u, nack_list.size());
2375 }
2376 
TEST_F(TestJitterBufferNack,VerifyRetransmittedFlag)2377 TEST_F(TestJitterBufferNack, VerifyRetransmittedFlag) {
2378   stream_generator_->Init(0, clock_->TimeInMilliseconds());
2379   stream_generator_->GenerateFrame(kVideoFrameKey, 3, 0,
2380                                    clock_->TimeInMilliseconds());
2381   VCMPacket packet;
2382   stream_generator_->PopPacket(&packet, 0);
2383   bool retransmitted = false;
2384   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(packet, &retransmitted));
2385   EXPECT_FALSE(retransmitted);
2386   // Drop second packet.
2387   stream_generator_->PopPacket(&packet, 1);
2388   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(packet, &retransmitted));
2389   EXPECT_FALSE(retransmitted);
2390   EXPECT_FALSE(DecodeCompleteFrame());
2391   bool extended = false;
2392   std::vector<uint16_t> nack_list = jitter_buffer_->GetNackList(&extended);
2393   EXPECT_EQ(1u, nack_list.size());
2394   stream_generator_->PopPacket(&packet, 0);
2395   EXPECT_EQ(packet.seqNum, nack_list[0]);
2396   EXPECT_EQ(kCompleteSession,
2397             jitter_buffer_->InsertPacket(packet, &retransmitted));
2398   EXPECT_TRUE(retransmitted);
2399   EXPECT_TRUE(DecodeCompleteFrame());
2400 }
2401 
TEST_F(TestJitterBufferNack,UseNackToRecoverFirstKeyFrame)2402 TEST_F(TestJitterBufferNack, UseNackToRecoverFirstKeyFrame) {
2403   stream_generator_->Init(0, clock_->TimeInMilliseconds());
2404   stream_generator_->GenerateFrame(kVideoFrameKey, 3, 0,
2405                                    clock_->TimeInMilliseconds());
2406   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
2407   // Drop second packet.
2408   EXPECT_EQ(kIncomplete, InsertPacketAndPop(1));
2409   EXPECT_FALSE(DecodeCompleteFrame());
2410   bool extended = false;
2411   std::vector<uint16_t> nack_list = jitter_buffer_->GetNackList(&extended);
2412   EXPECT_EQ(1u, nack_list.size());
2413   VCMPacket packet;
2414   stream_generator_->GetPacket(&packet, 0);
2415   EXPECT_EQ(packet.seqNum, nack_list[0]);
2416 }
2417 
TEST_F(TestJitterBufferNack,UseNackToRecoverFirstKeyFrameSecondInQueue)2418 TEST_F(TestJitterBufferNack, UseNackToRecoverFirstKeyFrameSecondInQueue) {
2419   VCMPacket packet;
2420   stream_generator_->Init(0, clock_->TimeInMilliseconds());
2421   // First frame is delta.
2422   stream_generator_->GenerateFrame(kVideoFrameDelta, 3, 0,
2423                                    clock_->TimeInMilliseconds());
2424   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
2425   // Drop second packet in frame.
2426   ASSERT_TRUE(stream_generator_->PopPacket(&packet, 0));
2427   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
2428   // Second frame is key.
2429   stream_generator_->GenerateFrame(kVideoFrameKey, 3, 0,
2430                                    clock_->TimeInMilliseconds() + 10);
2431   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
2432   // Drop second packet in frame.
2433   EXPECT_EQ(kIncomplete, InsertPacketAndPop(1));
2434   EXPECT_FALSE(DecodeCompleteFrame());
2435   bool extended = false;
2436   std::vector<uint16_t> nack_list = jitter_buffer_->GetNackList(&extended);
2437   EXPECT_EQ(1u, nack_list.size());
2438   stream_generator_->GetPacket(&packet, 0);
2439   EXPECT_EQ(packet.seqNum, nack_list[0]);
2440 }
2441 
TEST_F(TestJitterBufferNack,NormalOperation)2442 TEST_F(TestJitterBufferNack, NormalOperation) {
2443   EXPECT_EQ(kNack, jitter_buffer_->nack_mode());
2444   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
2445 
2446   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
2447   EXPECT_TRUE(DecodeIncompleteFrame());
2448 
2449   //  ----------------------------------------------------------------
2450   // | 1 | 2 | .. | 8 | 9 | x | 11 | 12 | .. | 19 | x | 21 | .. | 100 |
2451   //  ----------------------------------------------------------------
2452   stream_generator_->GenerateFrame(kVideoFrameKey, 100, 0,
2453                                    clock_->TimeInMilliseconds());
2454   clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
2455   EXPECT_EQ(kDecodableSession, InsertPacketAndPop(0));
2456   // Verify that the frame is incomplete.
2457   EXPECT_FALSE(DecodeCompleteFrame());
2458   while (stream_generator_->PacketsRemaining() > 1) {
2459     if (stream_generator_->NextSequenceNumber() % 10 != 0) {
2460       EXPECT_EQ(kDecodableSession, InsertPacketAndPop(0));
2461     } else {
2462       stream_generator_->NextPacket(NULL);  // Drop packet
2463     }
2464   }
2465   EXPECT_EQ(kDecodableSession, InsertPacketAndPop(0));
2466   EXPECT_EQ(0, stream_generator_->PacketsRemaining());
2467   EXPECT_FALSE(DecodeCompleteFrame());
2468   EXPECT_FALSE(DecodeIncompleteFrame());
2469   bool request_key_frame = false;
2470   std::vector<uint16_t> nack_list =
2471       jitter_buffer_->GetNackList(&request_key_frame);
2472   // Verify the NACK list.
2473   const size_t kExpectedNackSize = 9;
2474   ASSERT_EQ(kExpectedNackSize, nack_list.size());
2475   for (size_t i = 0; i < nack_list.size(); ++i)
2476     EXPECT_EQ((1 + i) * 10, nack_list[i]);
2477 }
2478 
TEST_F(TestJitterBufferNack,NormalOperationWrap)2479 TEST_F(TestJitterBufferNack, NormalOperationWrap) {
2480   bool request_key_frame = false;
2481   //  -------   ------------------------------------------------------------
2482   // | 65532 | | 65533 | 65534 | 65535 | x | 1 | .. | 9 | x | 11 |.....| 96 |
2483   //  -------   ------------------------------------------------------------
2484   stream_generator_->Init(65532, clock_->TimeInMilliseconds());
2485   InsertFrame(kVideoFrameKey);
2486   EXPECT_FALSE(request_key_frame);
2487   EXPECT_TRUE(DecodeCompleteFrame());
2488   stream_generator_->GenerateFrame(kVideoFrameDelta, 100, 0,
2489                                    clock_->TimeInMilliseconds());
2490   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
2491   while (stream_generator_->PacketsRemaining() > 1) {
2492     if (stream_generator_->NextSequenceNumber() % 10 != 0) {
2493       EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
2494       EXPECT_FALSE(request_key_frame);
2495     } else {
2496       stream_generator_->NextPacket(NULL);  // Drop packet
2497     }
2498   }
2499   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
2500   EXPECT_FALSE(request_key_frame);
2501   EXPECT_EQ(0, stream_generator_->PacketsRemaining());
2502   EXPECT_FALSE(DecodeCompleteFrame());
2503   EXPECT_FALSE(DecodeCompleteFrame());
2504   bool extended = false;
2505   std::vector<uint16_t> nack_list = jitter_buffer_->GetNackList(&extended);
2506   // Verify the NACK list.
2507   const size_t kExpectedNackSize = 10;
2508   ASSERT_EQ(kExpectedNackSize, nack_list.size());
2509   for (size_t i = 0; i < nack_list.size(); ++i)
2510     EXPECT_EQ(i * 10, nack_list[i]);
2511 }
2512 
TEST_F(TestJitterBufferNack,NormalOperationWrap2)2513 TEST_F(TestJitterBufferNack, NormalOperationWrap2) {
2514   bool request_key_frame = false;
2515   //  -----------------------------------
2516   // | 65532 | 65533 | 65534 | x | 0 | 1 |
2517   //  -----------------------------------
2518   stream_generator_->Init(65532, clock_->TimeInMilliseconds());
2519   InsertFrame(kVideoFrameKey);
2520   EXPECT_FALSE(request_key_frame);
2521   EXPECT_TRUE(DecodeCompleteFrame());
2522   stream_generator_->GenerateFrame(kVideoFrameDelta, 1, 0,
2523                                    clock_->TimeInMilliseconds());
2524   clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
2525   for (int i = 0; i < 5; ++i) {
2526     if (stream_generator_->NextSequenceNumber() != 65535) {
2527       EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0));
2528       EXPECT_FALSE(request_key_frame);
2529     } else {
2530       stream_generator_->NextPacket(NULL);  // Drop packet
2531     }
2532     stream_generator_->GenerateFrame(kVideoFrameDelta, 1, 0,
2533                                      clock_->TimeInMilliseconds());
2534     clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
2535   }
2536   EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0));
2537   EXPECT_FALSE(request_key_frame);
2538   bool extended = false;
2539   std::vector<uint16_t> nack_list = jitter_buffer_->GetNackList(&extended);
2540   // Verify the NACK list.
2541   ASSERT_EQ(1u, nack_list.size());
2542   EXPECT_EQ(65535, nack_list[0]);
2543 }
2544 
TEST_F(TestJitterBufferNack,ResetByFutureKeyFrameDoesntError)2545 TEST_F(TestJitterBufferNack, ResetByFutureKeyFrameDoesntError) {
2546   stream_generator_->Init(0, clock_->TimeInMilliseconds());
2547   InsertFrame(kVideoFrameKey);
2548   EXPECT_TRUE(DecodeCompleteFrame());
2549   bool extended = false;
2550   std::vector<uint16_t> nack_list = jitter_buffer_->GetNackList(&extended);
2551   EXPECT_EQ(0u, nack_list.size());
2552 
2553   // Far-into-the-future video frame, could be caused by resetting the encoder
2554   // or otherwise restarting. This should not fail when error when the packet is
2555   // a keyframe, even if all of the nack list needs to be flushed.
2556   stream_generator_->Init(10000, clock_->TimeInMilliseconds());
2557   clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
2558   InsertFrame(kVideoFrameKey);
2559   EXPECT_TRUE(DecodeCompleteFrame());
2560   nack_list = jitter_buffer_->GetNackList(&extended);
2561   EXPECT_EQ(0u, nack_list.size());
2562 
2563   // Stream should be decodable from this point.
2564   clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
2565   InsertFrame(kVideoFrameDelta);
2566   EXPECT_TRUE(DecodeCompleteFrame());
2567   nack_list = jitter_buffer_->GetNackList(&extended);
2568   EXPECT_EQ(0u, nack_list.size());
2569 }
2570 
2571 }  // namespace webrtc
2572