899a3eecaa874a1d9429541c5458d5bc09451bee
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / video_coding / main / source / jitter_buffer_unittest.cc
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/main/source/frame_buffer.h"
17 #include "webrtc/modules/video_coding/main/source/jitter_buffer.h"
18 #include "webrtc/modules/video_coding/main/source/media_opt_util.h"
19 #include "webrtc/modules/video_coding/main/source/packet.h"
20 #include "webrtc/modules/video_coding/main/source/test/stream_generator.h"
21 #include "webrtc/modules/video_coding/main/test/test_util.h"
22 #include "webrtc/system_wrappers/interface/clock.h"
23
24 namespace webrtc {
25
26 class TestBasicJitterBuffer : public ::testing::Test {
27  protected:
28   virtual void SetUp() {
29     clock_.reset(new SimulatedClock(0));
30     jitter_buffer_.reset(
31         new VCMJitterBuffer(clock_.get(), &event_factory_));
32     jitter_buffer_->Start();
33     seq_num_ = 1234;
34     timestamp_ = 0;
35     size_ = 1400;
36     // Data vector -  0, 0, 0x80, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0x80, 3....
37     data_[0] = 0;
38     data_[1] = 0;
39     data_[2] = 0x80;
40     int count = 3;
41     for (unsigned int i = 3; i < sizeof(data_) - 3; ++i) {
42       data_[i] = count;
43       count++;
44       if (count == 10) {
45         data_[i + 1] = 0;
46         data_[i + 2] = 0;
47         data_[i + 3] = 0x80;
48         count = 3;
49         i += 3;
50       }
51     }
52     packet_.reset(new VCMPacket(data_, size_, seq_num_, timestamp_, true));
53   }
54
55   VCMEncodedFrame* DecodeCompleteFrame() {
56     uint32_t timestamp = 0;
57     bool found_frame = jitter_buffer_->NextCompleteTimestamp(10, &timestamp);
58     if (!found_frame)
59       return NULL;
60     VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(timestamp);
61     return frame;
62   }
63
64   VCMEncodedFrame* DecodeIncompleteFrame() {
65     uint32_t timestamp = 0;
66     bool found_frame = jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp);
67     if (!found_frame)
68       return NULL;
69     VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(timestamp);
70     return frame;
71   }
72
73   void CheckOutFrame(VCMEncodedFrame* frame_out,
74                     unsigned int size,
75                     bool startCode) {
76     ASSERT_TRUE(frame_out);
77
78     const uint8_t* outData = frame_out->Buffer();
79     unsigned int i = 0;
80
81     if (startCode) {
82       EXPECT_EQ(0, outData[0]);
83       EXPECT_EQ(0, outData[1]);
84       EXPECT_EQ(0, outData[2]);
85       EXPECT_EQ(1, outData[3]);
86       i += 4;
87     }
88
89     EXPECT_EQ(size, frame_out->Length());
90     int count = 3;
91     for (; i < size; i++) {
92       if (outData[i] == 0 && outData[i + 1] == 0 && outData[i + 2] == 0x80) {
93         i += 2;
94       } else if (startCode && outData[i] == 0 && outData[i + 1] == 0) {
95         EXPECT_EQ(0, outData[0]);
96         EXPECT_EQ(0, outData[1]);
97         EXPECT_EQ(0, outData[2]);
98         EXPECT_EQ(1, outData[3]);
99         i += 3;
100       } else {
101         EXPECT_EQ(count, outData[i]);
102         count++;
103         if (count == 10) {
104           count = 3;
105         }
106       }
107     }
108   }
109
110   uint16_t seq_num_;
111   uint32_t timestamp_;
112   int size_;
113   uint8_t data_[1500];
114   scoped_ptr<VCMJitterBuffer> jitter_buffer_;
115   scoped_ptr<VCMPacket> packet_;
116   scoped_ptr<SimulatedClock> clock_;
117   NullEventFactory event_factory_;
118 };
119
120
121 class TestRunningJitterBuffer : public ::testing::Test {
122  protected:
123   enum { kDataBufferSize = 10 };
124
125   virtual void SetUp() {
126     clock_.reset(new SimulatedClock(0));
127     max_nack_list_size_ = 150;
128     oldest_packet_to_nack_ = 250;
129     jitter_buffer_ = new VCMJitterBuffer(clock_.get(), &event_factory_);
130     stream_generator_ = new StreamGenerator(0, 0, clock_->TimeInMilliseconds());
131     jitter_buffer_->Start();
132     jitter_buffer_->SetNackSettings(max_nack_list_size_,
133                                     oldest_packet_to_nack_, 0);
134     memset(data_buffer_, 0, kDataBufferSize);
135   }
136
137   virtual void TearDown() {
138     jitter_buffer_->Stop();
139     delete stream_generator_;
140     delete jitter_buffer_;
141   }
142
143   VCMFrameBufferEnum InsertPacketAndPop(int index) {
144     VCMPacket packet;
145     packet.dataPtr = data_buffer_;
146     bool packet_available = stream_generator_->PopPacket(&packet, index);
147     EXPECT_TRUE(packet_available);
148     if (!packet_available)
149       return kGeneralError;  // Return here to avoid crashes below.
150     bool retransmitted = false;
151     return jitter_buffer_->InsertPacket(packet, &retransmitted);
152   }
153
154   VCMFrameBufferEnum InsertPacket(int index) {
155     VCMPacket packet;
156     packet.dataPtr = data_buffer_;
157     bool packet_available = stream_generator_->GetPacket(&packet, index);
158     EXPECT_TRUE(packet_available);
159     if (!packet_available)
160       return kGeneralError;  // Return here to avoid crashes below.
161     bool retransmitted = false;
162     return jitter_buffer_->InsertPacket(packet, &retransmitted);
163   }
164
165   VCMFrameBufferEnum InsertFrame(FrameType frame_type) {
166     stream_generator_->GenerateFrame(frame_type,
167                                     (frame_type != kFrameEmpty) ? 1 : 0,
168                                     (frame_type == kFrameEmpty) ? 1 : 0,
169                                     clock_->TimeInMilliseconds());
170     VCMFrameBufferEnum ret = InsertPacketAndPop(0);
171     clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
172     return ret;
173   }
174
175   VCMFrameBufferEnum InsertFrames(int num_frames, FrameType frame_type) {
176     VCMFrameBufferEnum ret_for_all = kNoError;
177     for (int i = 0; i < num_frames; ++i) {
178       VCMFrameBufferEnum ret = InsertFrame(frame_type);
179       if (ret < kNoError) {
180         ret_for_all = ret;
181       } else if (ret_for_all >= kNoError) {
182         ret_for_all = ret;
183       }
184     }
185     return ret_for_all;
186   }
187
188   void DropFrame(int num_packets) {
189     stream_generator_->GenerateFrame(kVideoFrameDelta, num_packets, 0,
190                                      clock_->TimeInMilliseconds());
191     for (int i = 0; i < num_packets; ++i)
192       stream_generator_->DropLastPacket();
193     clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
194   }
195
196   bool DecodeCompleteFrame() {
197     uint32_t timestamp = 0;
198     bool found_frame = jitter_buffer_->NextCompleteTimestamp(0, &timestamp);
199     if (!found_frame)
200       return false;
201
202     VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(timestamp);
203     bool ret = (frame != NULL);
204     jitter_buffer_->ReleaseFrame(frame);
205     return ret;
206   }
207
208   bool DecodeIncompleteFrame() {
209     uint32_t timestamp = 0;
210     bool found_frame = jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp);
211     if (!found_frame)
212       return false;
213     VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(timestamp);
214     bool ret = (frame != NULL);
215     jitter_buffer_->ReleaseFrame(frame);
216     return ret;
217   }
218
219   VCMJitterBuffer* jitter_buffer_;
220   StreamGenerator* stream_generator_;
221   scoped_ptr<SimulatedClock> clock_;
222   NullEventFactory event_factory_;
223   size_t max_nack_list_size_;
224   int oldest_packet_to_nack_;
225   uint8_t data_buffer_[kDataBufferSize];
226 };
227
228 class TestJitterBufferNack : public TestRunningJitterBuffer {
229  protected:
230   virtual void SetUp() {
231     TestRunningJitterBuffer::SetUp();
232     jitter_buffer_->SetNackMode(kNack, -1, -1);
233   }
234
235   virtual void TearDown() {
236     TestRunningJitterBuffer::TearDown();
237   }
238 };
239
240 TEST_F(TestBasicJitterBuffer, StopRunning) {
241   jitter_buffer_->Stop();
242   EXPECT_TRUE(NULL == DecodeCompleteFrame());
243   EXPECT_TRUE(NULL == DecodeIncompleteFrame());
244   jitter_buffer_->Start();
245   // Allow selective errors.
246   jitter_buffer_->SetDecodeErrorMode(kSelectiveErrors);
247
248   // No packets inserted.
249   EXPECT_TRUE(NULL == DecodeCompleteFrame());
250   EXPECT_TRUE(NULL == DecodeIncompleteFrame());
251
252   // Allow decoding with errors.
253   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
254
255   // No packets inserted.
256   EXPECT_TRUE(NULL == DecodeCompleteFrame());
257   EXPECT_TRUE(NULL == DecodeIncompleteFrame());
258 }
259
260 TEST_F(TestBasicJitterBuffer, SinglePacketFrame) {
261   // Always start with a complete key frame when not allowing errors.
262   jitter_buffer_->SetDecodeErrorMode(kNoErrors);
263   packet_->frameType = kVideoFrameKey;
264   packet_->isFirstPacket = true;
265   packet_->markerBit = true;
266   packet_->timestamp += 123 * 90;
267
268   // Insert the packet to the jitter buffer and get a frame.
269   bool retransmitted = false;
270   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
271                                                            &retransmitted));
272   VCMEncodedFrame* frame_out =  DecodeCompleteFrame();
273   CheckOutFrame(frame_out, size_, false);
274   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
275 }
276
277 TEST_F(TestBasicJitterBuffer, DualPacketFrame) {
278   packet_->frameType = kVideoFrameKey;
279   packet_->isFirstPacket = true;
280   packet_->markerBit = false;
281
282   bool retransmitted = false;
283   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
284                                                        &retransmitted));
285   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
286   // Should not be complete.
287   EXPECT_TRUE(frame_out == NULL);
288
289   ++seq_num_;
290   packet_->isFirstPacket = false;
291   packet_->markerBit = true;
292   packet_->seqNum = seq_num_;
293
294   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
295                                                            &retransmitted));
296
297   frame_out = DecodeCompleteFrame();
298   CheckOutFrame(frame_out, 2 * size_, false);
299
300   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
301 }
302
303 TEST_F(TestBasicJitterBuffer, 100PacketKeyFrame) {
304   packet_->frameType = kVideoFrameKey;
305   packet_->isFirstPacket = true;
306   packet_->markerBit = false;
307
308   bool retransmitted = false;
309   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
310                                                        &retransmitted));
311
312   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
313
314   // Frame should not be complete.
315   EXPECT_TRUE(frame_out == NULL);
316
317   // Insert 98 frames.
318   int loop = 0;
319   do {
320     seq_num_++;
321     packet_->isFirstPacket = false;
322     packet_->markerBit = false;
323     packet_->seqNum = seq_num_;
324
325     EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
326                                                         &retransmitted));
327     loop++;
328   } while (loop < 98);
329
330   // Insert last packet.
331   ++seq_num_;
332   packet_->isFirstPacket = false;
333   packet_->markerBit = true;
334   packet_->seqNum = seq_num_;
335
336   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
337                                                            &retransmitted));
338
339   frame_out = DecodeCompleteFrame();
340
341   CheckOutFrame(frame_out, 100 * size_, false);
342   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
343 }
344
345 TEST_F(TestBasicJitterBuffer, 100PacketDeltaFrame) {
346   // Always start with a complete key frame.
347   packet_->frameType = kVideoFrameKey;
348   packet_->isFirstPacket = true;
349   packet_->markerBit = true;
350
351   bool retransmitted = false;
352   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
353                                                            &retransmitted));
354   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
355   EXPECT_FALSE(frame_out == NULL);
356
357   ++seq_num_;
358   packet_->seqNum = seq_num_;
359   packet_->markerBit = false;
360   packet_->frameType = kVideoFrameDelta;
361   packet_->timestamp += 33 * 90;
362
363   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
364                                                        &retransmitted));
365
366   frame_out = DecodeCompleteFrame();
367
368   // Frame should not be complete.
369   EXPECT_TRUE(frame_out == NULL);
370
371   packet_->isFirstPacket = false;
372   // Insert 98 frames.
373   int loop = 0;
374   do {
375     ++seq_num_;
376     packet_->seqNum = seq_num_;
377
378     // Insert a packet into a frame.
379     EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
380                                                         &retransmitted));
381     loop++;
382   } while (loop < 98);
383
384   // Insert the last packet.
385   ++seq_num_;
386   packet_->isFirstPacket = false;
387   packet_->markerBit = true;
388   packet_->seqNum = seq_num_;
389
390   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
391                                                            &retransmitted));
392
393   frame_out = DecodeCompleteFrame();
394
395   CheckOutFrame(frame_out, 100 * size_, false);
396   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
397 }
398
399 TEST_F(TestBasicJitterBuffer, PacketReorderingReverseOrder) {
400   // Insert the "first" packet last.
401   seq_num_ += 100;
402   packet_->frameType = kVideoFrameKey;
403   packet_->isFirstPacket = false;
404   packet_->markerBit = true;
405   packet_->seqNum = seq_num_;
406   packet_->timestamp = timestamp_;
407
408   bool retransmitted = false;
409   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
410                                                        &retransmitted));
411
412   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
413
414   EXPECT_TRUE(frame_out == NULL);
415
416   // Insert 98 packets.
417   int loop = 0;
418   do {
419     seq_num_--;
420     packet_->isFirstPacket = false;
421     packet_->markerBit = false;
422     packet_->seqNum = seq_num_;
423
424     EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
425                                                         &retransmitted));
426     loop++;
427   } while (loop < 98);
428
429   // Insert the last packet.
430   seq_num_--;
431   packet_->isFirstPacket = true;
432   packet_->markerBit = false;
433   packet_->seqNum = seq_num_;
434
435   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
436                                                            &retransmitted));
437
438   frame_out = DecodeCompleteFrame();;
439
440   CheckOutFrame(frame_out, 100 * size_, false);
441
442   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
443 }
444
445 TEST_F(TestBasicJitterBuffer, FrameReordering2Frames2PacketsEach) {
446   packet_->frameType = kVideoFrameDelta;
447   packet_->isFirstPacket = true;
448   packet_->markerBit = false;
449
450   bool retransmitted = false;
451   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
452                                                        &retransmitted));
453
454   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
455
456   EXPECT_TRUE(frame_out == NULL);
457
458   seq_num_++;
459   packet_->isFirstPacket = false;
460   packet_->markerBit = true;
461   packet_->seqNum = seq_num_;
462
463   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
464                                                            &retransmitted));
465
466   // check that we fail to get frame since seqnum is not continuous
467   frame_out = DecodeCompleteFrame();
468   EXPECT_TRUE(frame_out == NULL);
469
470   seq_num_ -= 3;
471   timestamp_ -= 33*90;
472   packet_->frameType = kVideoFrameKey;
473   packet_->isFirstPacket = true;
474   packet_->markerBit = false;
475   packet_->seqNum = seq_num_;
476   packet_->timestamp = timestamp_;
477
478   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
479                                                        &retransmitted));
480
481   frame_out = DecodeCompleteFrame();
482
483   // It should not be complete.
484   EXPECT_TRUE(frame_out == NULL);
485
486   seq_num_++;
487   packet_->isFirstPacket = false;
488   packet_->markerBit = true;
489   packet_->seqNum = seq_num_;
490
491   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
492                                                            &retransmitted));
493
494   frame_out = DecodeCompleteFrame();
495
496   CheckOutFrame(frame_out, 2 * size_, false);
497
498   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
499
500   jitter_buffer_->ReleaseFrame(frame_out);
501
502   frame_out = DecodeCompleteFrame();
503
504   CheckOutFrame(frame_out, 2 * size_, false);
505
506   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
507 }
508
509 TEST_F(TestBasicJitterBuffer, DuplicatePackets) {
510   packet_->frameType = kVideoFrameKey;
511   packet_->isFirstPacket = true;
512   packet_->markerBit = false;
513   packet_->seqNum = seq_num_;
514   packet_->timestamp = timestamp_;
515   EXPECT_EQ(0, jitter_buffer_->num_packets());
516   EXPECT_EQ(0, jitter_buffer_->num_duplicated_packets());
517
518   bool retransmitted = false;
519   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
520                                                        &retransmitted));
521
522   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
523
524   EXPECT_TRUE(frame_out == NULL);
525   EXPECT_EQ(1, jitter_buffer_->num_packets());
526   EXPECT_EQ(0, jitter_buffer_->num_duplicated_packets());
527
528   packet_->isFirstPacket = false;
529   packet_->markerBit = true;
530
531   // Insert a packet into a frame.
532   EXPECT_EQ(kDuplicatePacket, jitter_buffer_->InsertPacket(*packet_,
533                                                            &retransmitted));
534   EXPECT_EQ(2, jitter_buffer_->num_packets());
535   EXPECT_EQ(1, jitter_buffer_->num_duplicated_packets());
536
537   seq_num_++;
538   packet_->seqNum = seq_num_;
539
540   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
541                                                            &retransmitted));
542
543   frame_out = DecodeCompleteFrame();
544
545   CheckOutFrame(frame_out, 2 * size_, false);
546
547   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
548   EXPECT_EQ(3, jitter_buffer_->num_packets());
549   EXPECT_EQ(1, jitter_buffer_->num_duplicated_packets());
550 }
551
552 TEST_F(TestBasicJitterBuffer, H264InsertStartCode) {
553   packet_->frameType = kVideoFrameKey;
554   packet_->isFirstPacket = true;
555   packet_->markerBit = false;
556   packet_->seqNum = seq_num_;
557   packet_->timestamp = timestamp_;
558   packet_->insertStartCode = true;
559
560   bool retransmitted = false;
561   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
562                                                        &retransmitted));
563
564   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
565
566   // Frame should not be complete.
567   EXPECT_TRUE(frame_out == NULL);
568
569   seq_num_++;
570   packet_->isFirstPacket = false;
571   packet_->markerBit = true;
572   packet_->seqNum = seq_num_;
573
574   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
575                                                            &retransmitted));
576
577   frame_out = DecodeCompleteFrame();
578   CheckOutFrame(frame_out, size_ * 2 + 4 * 2, true);
579   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
580 }
581
582 // Test threshold conditions of decodable state.
583 TEST_F(TestBasicJitterBuffer, PacketLossWithSelectiveErrorsThresholdCheck) {
584   jitter_buffer_->SetDecodeErrorMode(kSelectiveErrors);
585   // Always start with a key frame. Use 10 packets to test Decodable State
586   // boundaries.
587   packet_->frameType = kVideoFrameKey;
588   packet_->isFirstPacket = true;
589   packet_->markerBit = false;
590   packet_->seqNum = seq_num_;
591   packet_->timestamp = timestamp_;
592
593   bool retransmitted = false;
594   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
595                                                        &retransmitted));
596   uint32_t timestamp = 0;
597   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
598   EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
599
600   packet_->isFirstPacket = false;
601   for (int i = 1; i < 9; ++i) {
602     packet_->seqNum++;
603     EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
604         &retransmitted));
605     EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
606     EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
607   }
608
609   // last packet
610   packet_->markerBit = true;
611   packet_->seqNum++;
612
613   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
614                                                            &retransmitted));
615   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
616   CheckOutFrame(frame_out, 10 * size_, false);
617   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
618
619   // An incomplete frame can only be decoded once a subsequent frame has begun
620   // to arrive. Insert packet in distant frame for this purpose.
621   packet_->frameType = kVideoFrameDelta;
622   packet_->isFirstPacket = true;
623   packet_->markerBit = false;
624   packet_->seqNum += 100;
625   packet_->timestamp += 33 * 90 * 8;
626
627   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
628                                                        &retransmitted));
629   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
630   EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
631
632   // Insert second frame
633   packet_->seqNum -= 99;
634   packet_->timestamp -= 33 * 90 * 7;
635
636   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
637                                                             &retransmitted));
638   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
639   EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
640
641   packet_->isFirstPacket = false;
642   for (int i = 1; i < 8; ++i) {
643     packet_->seqNum++;
644     EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
645                                                               &retransmitted));
646     EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
647     EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
648   }
649
650   packet_->seqNum++;
651   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
652                                                             &retransmitted));
653   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
654   EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
655
656   frame_out = DecodeIncompleteFrame();
657   ASSERT_FALSE(NULL == frame_out);
658   CheckOutFrame(frame_out, 9 * size_, false);
659   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
660
661   packet_->markerBit = true;
662   packet_->seqNum++;
663   EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_,
664                                                      &retransmitted));
665 }
666
667 // Make sure first packet is present before a frame can be decoded.
668 TEST_F(TestBasicJitterBuffer, PacketLossWithSelectiveErrorsIncompleteKey) {
669   jitter_buffer_->SetDecodeErrorMode(kSelectiveErrors);
670   // Always start with a key frame.
671   packet_->frameType = kVideoFrameKey;
672   packet_->isFirstPacket = true;
673   packet_->markerBit = true;
674   packet_->seqNum = seq_num_;
675   packet_->timestamp = timestamp_;
676
677   bool retransmitted = false;
678   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
679                                                        &retransmitted));
680   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
681   CheckOutFrame(frame_out, size_, false);
682   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
683
684   // An incomplete frame can only be decoded once a subsequent frame has begun
685   // to arrive. Insert packet in distant frame for this purpose.
686   packet_->frameType = kVideoFrameDelta;
687   packet_->isFirstPacket = false;
688   packet_->markerBit = false;
689   packet_->seqNum += 100;
690   packet_->timestamp += 33*90*8;
691   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
692                                                        &retransmitted));
693   uint32_t timestamp;
694   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
695   EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
696
697   // Insert second frame - an incomplete key frame.
698   packet_->frameType = kVideoFrameKey;
699   packet_->isFirstPacket = true;
700   packet_->seqNum -= 99;
701   packet_->timestamp -= 33*90*7;
702
703   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
704                                                             &retransmitted));
705   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
706   EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
707
708   // Insert a few more packets. Make sure we're waiting for the key frame to be
709   // complete.
710   packet_->isFirstPacket = false;
711   for (int i = 1; i < 5; ++i) {
712     packet_->seqNum++;
713     EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
714                                                               &retransmitted));
715     EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
716     EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
717   }
718
719   // Complete key frame.
720   packet_->markerBit = true;
721   packet_->seqNum++;
722   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
723                                                            &retransmitted));
724   frame_out = DecodeCompleteFrame();
725   CheckOutFrame(frame_out, 6 * size_, false);
726   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
727 }
728
729 // Make sure first packet is present before a frame can be decoded.
730 TEST_F(TestBasicJitterBuffer, PacketLossWithSelectiveErrorsMissingFirstPacket) {
731   jitter_buffer_->SetDecodeErrorMode(kSelectiveErrors);
732   // Always start with a key frame.
733   packet_->frameType = kVideoFrameKey;
734   packet_->isFirstPacket = true;
735   packet_->markerBit = true;
736   packet_->seqNum = seq_num_;
737   packet_->timestamp = timestamp_;
738
739   bool retransmitted = false;
740   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
741                                                        &retransmitted));
742   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
743   CheckOutFrame(frame_out, size_, false);
744   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
745
746   // An incomplete frame can only be decoded once a subsequent frame has begun
747   // to arrive. Insert packet in distant frame for this purpose.
748   packet_->frameType = kVideoFrameDelta;
749   packet_->isFirstPacket = false;
750   packet_->markerBit = false;
751   packet_->seqNum += 100;
752   packet_->timestamp += 33*90*8;
753   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
754                                                        &retransmitted));
755   uint32_t timestamp;
756   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
757   EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
758
759   // Insert second frame with the first packet missing. Make sure we're waiting
760   // for the key frame to be complete.
761   packet_->seqNum -= 98;
762   packet_->timestamp -= 33*90*7;
763
764   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
765                                                             &retransmitted));
766   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
767   EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
768
769   for (int i = 0; i < 5; ++i) {
770     packet_->seqNum++;
771     EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
772                                                               &retransmitted));
773     EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
774     EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
775   }
776
777   // Add first packet. Frame should now be decodable, but incomplete.
778   packet_->isFirstPacket = true;
779   packet_->seqNum -= 6;
780   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
781                                                            &retransmitted));
782   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &timestamp));
783   EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(&timestamp));
784
785   frame_out = DecodeIncompleteFrame();
786   CheckOutFrame(frame_out, 7 * size_, false);
787   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
788 }
789
790 TEST_F(TestBasicJitterBuffer, DiscontinuousStreamWhenDecodingWithErrors) {
791   // Will use one packet per frame.
792   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
793   packet_->frameType = kVideoFrameKey;
794   packet_->isFirstPacket = true;
795   packet_->markerBit = true;
796   packet_->seqNum = seq_num_;
797   packet_->timestamp = timestamp_;
798   bool retransmitted = false;
799   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
800                                                            &retransmitted));
801   uint32_t next_timestamp;
802   EXPECT_TRUE(jitter_buffer_->NextCompleteTimestamp(0, &next_timestamp));
803   EXPECT_EQ(packet_->timestamp, next_timestamp);
804   VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(next_timestamp);
805   EXPECT_TRUE(frame != NULL);
806   jitter_buffer_->ReleaseFrame(frame);
807
808   // Drop a complete frame.
809   timestamp_ += 2 * 33 * 90;
810   seq_num_ += 2;
811   packet_->frameType = kVideoFrameDelta;
812   packet_->isFirstPacket = true;
813   packet_->markerBit = false;
814   packet_->seqNum = seq_num_;
815   packet_->timestamp = timestamp_;
816   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
817                                                             &retransmitted));
818   // Insert a packet (so the previous one will be released).
819   timestamp_ += 33 * 90;
820   seq_num_ += 2;
821   packet_->frameType = kVideoFrameDelta;
822   packet_->isFirstPacket = true;
823   packet_->markerBit = false;
824   packet_->seqNum = seq_num_;
825   packet_->timestamp = timestamp_;
826   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
827                                                             &retransmitted));
828   EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &next_timestamp));
829   EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(&next_timestamp));
830   EXPECT_EQ(packet_->timestamp - 33 * 90, next_timestamp);
831 }
832
833 TEST_F(TestBasicJitterBuffer, PacketLoss) {
834   // Verify missing packets statistics and not decodable packets statistics.
835   // Insert 10 frames consisting of 4 packets and remove one from all of them.
836   // The last packet is an empty (non-media) packet.
837
838   // Select a start seqNum which triggers a difficult wrap situation
839   // The JB will only output (incomplete)frames if the next one has started
840   // to arrive. Start by inserting one frame (key).
841   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
842   seq_num_ = 0xffff - 4;
843   seq_num_++;
844   packet_->frameType = kVideoFrameKey;
845   packet_->isFirstPacket = true;
846   packet_->markerBit = false;
847   packet_->seqNum = seq_num_;
848   packet_->timestamp = timestamp_;
849   packet_->completeNALU = kNaluStart;
850
851   bool retransmitted = false;
852   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
853                                                             &retransmitted));
854   for (int i = 0; i < 11; ++i) {
855     webrtc::FrameType frametype = kVideoFrameDelta;
856     seq_num_++;
857     timestamp_ += 33*90;
858     packet_->frameType = frametype;
859     packet_->isFirstPacket = true;
860     packet_->markerBit = false;
861     packet_->seqNum = seq_num_;
862     packet_->timestamp = timestamp_;
863     packet_->completeNALU = kNaluStart;
864
865     EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
866                                                               &retransmitted));
867
868     VCMEncodedFrame* frame_out = DecodeCompleteFrame();
869
870     // Should not be complete.
871     EXPECT_TRUE(frame_out == NULL);
872
873     seq_num_ += 2;
874     packet_->isFirstPacket = false;
875     packet_->markerBit = true;
876     packet_->seqNum = seq_num_;
877     packet_->completeNALU = kNaluEnd;
878
879     EXPECT_EQ(jitter_buffer_->InsertPacket(*packet_, &retransmitted),
880               kDecodableSession);
881
882     // Insert an empty (non-media) packet.
883     seq_num_++;
884     packet_->isFirstPacket = false;
885     packet_->markerBit = false;
886     packet_->seqNum = seq_num_;
887     packet_->completeNALU = kNaluEnd;
888     packet_->frameType = kFrameEmpty;
889
890     EXPECT_EQ(jitter_buffer_->InsertPacket(*packet_, &retransmitted),
891               kDecodableSession);
892     frame_out = DecodeIncompleteFrame();
893
894     // One of the packets has been discarded by the jitter buffer.
895     // Last frame can't be extracted yet.
896     if (i < 10) {
897       CheckOutFrame(frame_out, size_, false);
898
899       if (i == 0) {
900           EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
901       } else {
902          EXPECT_EQ(frametype, frame_out->FrameType());
903       }
904       EXPECT_FALSE(frame_out->Complete());
905       EXPECT_FALSE(frame_out->MissingFrame());
906     }
907
908     jitter_buffer_->ReleaseFrame(frame_out);
909   }
910
911   // Insert 3 old packets and verify that we have 3 discarded packets
912   // Match value to actual latest timestamp decoded.
913   timestamp_ -= 33 * 90;
914   packet_->timestamp = timestamp_ - 1000;
915
916   EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_,
917                                                      &retransmitted));
918
919   packet_->timestamp = timestamp_ - 500;
920
921   EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_,
922                                                      &retransmitted));
923
924   packet_->timestamp = timestamp_ - 100;
925
926   EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_,
927                                                      &retransmitted));
928
929   EXPECT_EQ(3, jitter_buffer_->num_discarded_packets());
930
931   jitter_buffer_->Flush();
932
933   // This statistic shouldn't be reset by a flush.
934   EXPECT_EQ(3, jitter_buffer_->num_discarded_packets());
935 }
936
937 TEST_F(TestBasicJitterBuffer, DeltaFrame100PacketsWithSeqNumWrap) {
938   seq_num_ = 0xfff0;
939   packet_->frameType = kVideoFrameKey;
940   packet_->isFirstPacket = true;
941   packet_->markerBit = false;
942   packet_->seqNum = seq_num_;
943   packet_->timestamp = timestamp_;
944
945   bool retransmitted = false;
946   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
947                                                        &retransmitted));
948
949   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
950
951   EXPECT_TRUE(frame_out == NULL);
952
953   int loop = 0;
954   do {
955     seq_num_++;
956     packet_->isFirstPacket = false;
957     packet_->markerBit = false;
958     packet_->seqNum = seq_num_;
959
960     EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
961                                                         &retransmitted));
962
963     frame_out = DecodeCompleteFrame();
964
965     EXPECT_TRUE(frame_out == NULL);
966
967     loop++;
968   } while (loop < 98);
969
970   seq_num_++;
971   packet_->isFirstPacket = false;
972   packet_->markerBit = true;
973   packet_->seqNum = seq_num_;
974
975   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
976                                                            &retransmitted));
977
978   frame_out = DecodeCompleteFrame();
979
980   CheckOutFrame(frame_out, 100 * size_, false);
981
982   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
983 }
984
985 TEST_F(TestBasicJitterBuffer, PacketReorderingReverseWithNegSeqNumWrap) {
986   // Insert "first" packet last seqnum.
987   seq_num_ = 10;
988   packet_->frameType = kVideoFrameKey;
989   packet_->isFirstPacket = false;
990   packet_->markerBit = true;
991   packet_->seqNum = seq_num_;
992
993   bool retransmitted = false;
994   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
995                                                        &retransmitted));
996   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
997
998   // Should not be complete.
999   EXPECT_TRUE(frame_out == NULL);
1000
1001   // Insert 98 frames.
1002   int loop = 0;
1003   do {
1004     seq_num_--;
1005     packet_->isFirstPacket = false;
1006     packet_->markerBit = false;
1007     packet_->seqNum = seq_num_;
1008
1009     EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
1010                                                         &retransmitted));
1011
1012     frame_out = DecodeCompleteFrame();
1013
1014     EXPECT_TRUE(frame_out == NULL);
1015
1016     loop++;
1017   } while (loop < 98);
1018
1019   // Insert last packet.
1020   seq_num_--;
1021   packet_->isFirstPacket = true;
1022   packet_->markerBit = false;
1023   packet_->seqNum = seq_num_;
1024
1025   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
1026                                                            &retransmitted));
1027
1028   frame_out = DecodeCompleteFrame();
1029   CheckOutFrame(frame_out, 100 * size_, false);
1030   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1031 }
1032
1033 TEST_F(TestBasicJitterBuffer, TestInsertOldFrame) {
1034   //   -------      -------
1035   //  |   2   |    |   1   |
1036   //   -------      -------
1037   //  t = 3000     t = 2000
1038   seq_num_ = 2;
1039   timestamp_ = 3000;
1040   packet_->frameType = kVideoFrameKey;
1041   packet_->isFirstPacket = true;
1042   packet_->markerBit = true;
1043   packet_->timestamp = timestamp_;
1044   packet_->seqNum = seq_num_;
1045
1046   bool retransmitted = false;
1047   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
1048                                                            &retransmitted));
1049
1050   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1051   EXPECT_EQ(3000u, frame_out->TimeStamp());
1052
1053   CheckOutFrame(frame_out, size_, false);
1054
1055   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1056
1057   jitter_buffer_->ReleaseFrame(frame_out);
1058
1059   seq_num_--;
1060   timestamp_ = 2000;
1061   packet_->frameType = kVideoFrameDelta;
1062   packet_->isFirstPacket = true;
1063   packet_->markerBit = true;
1064   packet_->seqNum = seq_num_;
1065   packet_->timestamp = timestamp_;
1066
1067   EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_,
1068                                                      &retransmitted));
1069 }
1070
1071 TEST_F(TestBasicJitterBuffer, TestInsertOldFrameWithSeqNumWrap) {
1072   //   -------      -------
1073   //  |   2   |    |   1   |
1074   //   -------      -------
1075   //  t = 3000     t = 0xffffff00
1076
1077   seq_num_ = 2;
1078   timestamp_ = 3000;
1079   packet_->frameType = kVideoFrameKey;
1080   packet_->isFirstPacket = true;
1081   packet_->markerBit = true;
1082   packet_->seqNum = seq_num_;
1083   packet_->timestamp = timestamp_;
1084
1085   bool retransmitted = false;
1086   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
1087                                                            &retransmitted));
1088
1089   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1090   EXPECT_EQ(timestamp_, frame_out->TimeStamp());
1091
1092   CheckOutFrame(frame_out, size_, false);
1093
1094   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1095
1096   jitter_buffer_->ReleaseFrame(frame_out);
1097
1098   seq_num_--;
1099   timestamp_ = 0xffffff00;
1100   packet_->frameType = kVideoFrameDelta;
1101   packet_->isFirstPacket = true;
1102   packet_->markerBit = true;
1103   packet_->seqNum = seq_num_;
1104   packet_->timestamp = timestamp_;
1105
1106
1107   // This timestamp is old.
1108   EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_,
1109                                                      &retransmitted));
1110 }
1111
1112 TEST_F(TestBasicJitterBuffer, TimestampWrap) {
1113   //  ---------------     ---------------
1114   // |   1   |   2   |   |   3   |   4   |
1115   //  ---------------     ---------------
1116   //  t = 0xffffff00        t = 33*90
1117
1118   timestamp_ = 0xffffff00;
1119   packet_->frameType = kVideoFrameKey;
1120   packet_->isFirstPacket = true;
1121   packet_->markerBit = false;
1122   packet_->seqNum = seq_num_;
1123   packet_->timestamp = timestamp_;
1124
1125   bool retransmitted = false;
1126   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
1127                                                        &retransmitted));
1128
1129   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1130
1131   EXPECT_TRUE(frame_out == NULL);
1132
1133   seq_num_++;
1134   packet_->isFirstPacket = false;
1135   packet_->markerBit = true;
1136   packet_->seqNum = seq_num_;
1137
1138   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
1139                                                            &retransmitted));
1140
1141   frame_out = DecodeCompleteFrame();
1142
1143   CheckOutFrame(frame_out, 2 * size_, false);
1144
1145   jitter_buffer_->ReleaseFrame(frame_out);
1146
1147   seq_num_++;
1148   timestamp_ += 33*90;
1149   packet_->frameType = kVideoFrameDelta;
1150   packet_->isFirstPacket = true;
1151   packet_->markerBit = false;
1152   packet_->seqNum = seq_num_;
1153   packet_->timestamp = timestamp_;
1154
1155   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
1156                                                        &retransmitted));
1157
1158   frame_out = DecodeCompleteFrame();
1159
1160   EXPECT_TRUE(frame_out == NULL);
1161
1162   seq_num_++;
1163   packet_->isFirstPacket = false;
1164   packet_->markerBit = true;
1165   packet_->seqNum = seq_num_;
1166
1167   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
1168                                                            &retransmitted));
1169
1170   frame_out = DecodeCompleteFrame();
1171
1172   CheckOutFrame(frame_out, 2 * size_, false);
1173
1174   EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType());
1175 }
1176
1177 TEST_F(TestBasicJitterBuffer, 2FrameWithTimestampWrap) {
1178   //   -------          -------
1179   //  |   1   |        |   2   |
1180   //   -------          -------
1181   // t = 0xffffff00    t = 2700
1182
1183   timestamp_ = 0xffffff00;
1184   packet_->frameType = kVideoFrameKey;
1185   packet_->isFirstPacket = true;
1186   packet_->markerBit = true;
1187   packet_->timestamp = timestamp_;
1188
1189   bool retransmitted = false;
1190   // Insert first frame (session will be complete).
1191   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
1192                                                            &retransmitted));
1193
1194   // Insert next frame.
1195   seq_num_++;
1196   timestamp_ = 2700;
1197   packet_->frameType = kVideoFrameDelta;
1198   packet_->isFirstPacket = true;
1199   packet_->markerBit = true;
1200   packet_->seqNum = seq_num_;
1201   packet_->timestamp = timestamp_;
1202
1203   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
1204                                                            &retransmitted));
1205
1206   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1207   EXPECT_EQ(0xffffff00, frame_out->TimeStamp());
1208
1209   CheckOutFrame(frame_out, size_, false);
1210
1211   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1212
1213   VCMEncodedFrame* frame_out2 = DecodeCompleteFrame();
1214   EXPECT_EQ(2700u, frame_out2->TimeStamp());
1215
1216   CheckOutFrame(frame_out2, size_, false);
1217
1218   EXPECT_EQ(kVideoFrameDelta, frame_out2->FrameType());
1219 }
1220
1221 TEST_F(TestBasicJitterBuffer, Insert2FramesReOrderedWithTimestampWrap) {
1222   //   -------          -------
1223   //  |   2   |        |   1   |
1224   //   -------          -------
1225   //  t = 2700        t = 0xffffff00
1226
1227   seq_num_ = 2;
1228   timestamp_ = 2700;
1229   packet_->frameType = kVideoFrameDelta;
1230   packet_->isFirstPacket = true;
1231   packet_->markerBit = true;
1232   packet_->seqNum = seq_num_;
1233   packet_->timestamp = timestamp_;
1234
1235   bool retransmitted = false;
1236   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
1237                                                            &retransmitted));
1238
1239   // Insert second frame
1240   seq_num_--;
1241   timestamp_ = 0xffffff00;
1242   packet_->frameType = kVideoFrameKey;
1243   packet_->isFirstPacket = true;
1244   packet_->markerBit = true;
1245   packet_->seqNum = seq_num_;
1246   packet_->timestamp = timestamp_;
1247
1248   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
1249                                                            &retransmitted));
1250
1251   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1252   EXPECT_EQ(0xffffff00, frame_out->TimeStamp());
1253
1254   CheckOutFrame(frame_out, size_, false);
1255
1256   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1257
1258   VCMEncodedFrame* frame_out2 = DecodeCompleteFrame();
1259   EXPECT_EQ(2700u, frame_out2->TimeStamp());
1260
1261   CheckOutFrame(frame_out2, size_, false);
1262
1263   EXPECT_EQ(kVideoFrameDelta, frame_out2->FrameType());
1264 }
1265
1266 TEST_F(TestBasicJitterBuffer, DeltaFrameWithMoreThanMaxNumberOfPackets) {
1267   int loop = 0;
1268   bool firstPacket = true;
1269   bool retransmitted = false;
1270   // Insert kMaxPacketsInJitterBuffer into frame.
1271   do {
1272     seq_num_++;
1273     packet_->isFirstPacket = false;
1274     packet_->markerBit = false;
1275     packet_->seqNum = seq_num_;
1276
1277     if (firstPacket) {
1278       EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
1279                                                            &retransmitted));
1280       firstPacket = false;
1281     } else {
1282       EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_,
1283                                                           &retransmitted));
1284     }
1285
1286     loop++;
1287   } while (loop < kMaxPacketsInSession);
1288
1289   // Max number of packets inserted.
1290   // Insert one more packet.
1291   seq_num_++;
1292   packet_->isFirstPacket = false;
1293   packet_->markerBit = true;
1294   packet_->seqNum = seq_num_;
1295
1296   // Insert the packet -> frame recycled.
1297   EXPECT_EQ(kSizeError, jitter_buffer_->InsertPacket(*packet_,
1298                                                      &retransmitted));
1299   EXPECT_TRUE(NULL == DecodeCompleteFrame());
1300
1301 }
1302
1303 TEST_F(TestBasicJitterBuffer, ExceedNumOfFrameWithSeqNumWrap) {
1304   // TEST fill JB with more than max number of frame (50 delta frames +
1305   // 51 key frames) with wrap in seq_num_
1306   //
1307   //  --------------------------------------------------------------
1308   // | 65485 | 65486 | 65487 | .... | 65535 | 0 | 1 | 2 | .....| 50 |
1309   //  --------------------------------------------------------------
1310   // |<-----------delta frames------------->|<------key frames----->|
1311
1312   int loop = 0;
1313   seq_num_ = 65485;
1314   uint32_t first_key_frame_timestamp = 0;
1315   bool retransmitted = false;
1316   // Insert MAX_NUMBER_OF_FRAMES frames.
1317   do {
1318     timestamp_ += 33*90;
1319     seq_num_++;
1320     packet_->isFirstPacket = true;
1321     packet_->markerBit = true;
1322     packet_->seqNum = seq_num_;
1323     packet_->timestamp = timestamp_;
1324
1325     if (loop == 50) {
1326       first_key_frame_timestamp = packet_->timestamp;
1327       packet_->frameType = kVideoFrameKey;
1328     }
1329
1330     // Insert frame.
1331     EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
1332                                                              &retransmitted));
1333
1334     loop++;
1335   } while (loop < kMaxNumberOfFrames);
1336
1337   // Max number of frames inserted.
1338
1339   // Insert one more frame.
1340   timestamp_ += 33*90;
1341   seq_num_++;
1342   packet_->isFirstPacket = true;
1343   packet_->markerBit = true;
1344   packet_->seqNum = seq_num_;
1345   packet_->timestamp = timestamp_;
1346
1347   // Now, no free frame - frames will be recycled until first key frame.
1348   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
1349                                                            &retransmitted));
1350
1351   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1352   EXPECT_EQ(first_key_frame_timestamp, frame_out->TimeStamp());
1353
1354   CheckOutFrame(frame_out, size_, false);
1355
1356   EXPECT_EQ(kVideoFrameKey, frame_out->FrameType());
1357 }
1358
1359 TEST_F(TestBasicJitterBuffer, EmptyLastFrame) {
1360   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
1361   seq_num_ = 3;
1362   // Insert one empty packet per frame, should never return the last timestamp
1363   // inserted. Only return empty frames in the presence of subsequent frames.
1364   int maxSize = 1000;
1365   bool retransmitted = false;
1366   for (int i = 0; i < maxSize + 10; i++) {
1367     timestamp_ += 33 * 90;
1368     seq_num_++;
1369     packet_->isFirstPacket = false;
1370     packet_->markerBit = false;
1371     packet_->seqNum = seq_num_;
1372     packet_->timestamp = timestamp_;
1373     packet_->frameType = kFrameEmpty;
1374
1375     EXPECT_EQ(kNoError, jitter_buffer_->InsertPacket(*packet_,
1376                                                      &retransmitted));
1377     VCMEncodedFrame* testFrame = DecodeIncompleteFrame();
1378     // Timestamp should never be the last TS inserted.
1379     if (testFrame != NULL) {
1380       EXPECT_TRUE(testFrame->TimeStamp() < timestamp_);
1381     }
1382   }
1383 }
1384
1385 TEST_F(TestBasicJitterBuffer, H264IncompleteNalu) {
1386   jitter_buffer_->SetNackMode(kNoNack, -1, -1);
1387   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
1388   ++seq_num_;
1389   timestamp_ += 33 * 90;
1390   int insertedLength = 0;
1391   packet_->seqNum = seq_num_;
1392   packet_->timestamp = timestamp_;
1393   packet_->frameType = kVideoFrameKey;
1394   packet_->isFirstPacket = true;
1395   packet_->completeNALU = kNaluStart;
1396   packet_->markerBit = false;
1397   bool retransmitted = false;
1398
1399   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
1400                                                             &retransmitted));
1401
1402   seq_num_ += 2;  // Skip one packet.
1403   packet_->seqNum = seq_num_;
1404   packet_->frameType = kVideoFrameKey;
1405   packet_->isFirstPacket = false;
1406   packet_->completeNALU = kNaluIncomplete;
1407   packet_->markerBit = false;
1408
1409   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
1410                                                             &retransmitted));
1411
1412   seq_num_++;
1413   packet_->seqNum = seq_num_;
1414   packet_->frameType = kVideoFrameKey;
1415   packet_->isFirstPacket = false;
1416   packet_->completeNALU = kNaluEnd;
1417   packet_->markerBit = false;
1418
1419   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
1420                                                             &retransmitted));
1421
1422   seq_num_++;
1423   packet_->seqNum = seq_num_;
1424   packet_->completeNALU = kNaluComplete;
1425   packet_->markerBit = true;  // Last packet.
1426   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
1427                                                             &retransmitted));
1428   // The JB will only output (incomplete) frames if a packet belonging to a
1429   // subsequent frame was already inserted. Insert one packet of a subsequent
1430   // frame. place high timestamp so the JB would always have a next frame
1431   // (otherwise, for every inserted frame we need to take care of the next
1432   // frame as well).
1433   packet_->seqNum = 1;
1434   packet_->timestamp = timestamp_ + 33 * 90 * 10;
1435   packet_->frameType = kVideoFrameDelta;
1436   packet_->isFirstPacket = false;
1437   packet_->completeNALU = kNaluStart;
1438   packet_->markerBit = false;
1439
1440   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
1441                                                             &retransmitted));
1442
1443   VCMEncodedFrame* frame_out = DecodeIncompleteFrame();
1444
1445   // We can decode everything from a NALU until a packet has been lost.
1446   // Thus we can decode the first packet of the first NALU and the second NALU
1447   // which consists of one packet.
1448   CheckOutFrame(frame_out, packet_->sizeBytes * 2, false);
1449   jitter_buffer_->ReleaseFrame(frame_out);
1450
1451   // Test reordered start frame + 1 lost.
1452   seq_num_ += 2;  // Re-order 1 frame.
1453   timestamp_ += 33*90;
1454   insertedLength = 0;
1455
1456   packet_->seqNum = seq_num_;
1457   packet_->timestamp = timestamp_;
1458   packet_->frameType = kVideoFrameKey;
1459   packet_->isFirstPacket = false;
1460   packet_->completeNALU = kNaluEnd;
1461   packet_->markerBit = false;
1462   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
1463                                                             &retransmitted));
1464   insertedLength += packet_->sizeBytes; // This packet should be decoded.
1465   seq_num_--;
1466   packet_->seqNum = seq_num_;
1467   packet_->timestamp = timestamp_;
1468   packet_->frameType = kVideoFrameKey;
1469   packet_->isFirstPacket = true;
1470   packet_->completeNALU = kNaluStart;
1471   packet_->markerBit = false;
1472
1473   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
1474                                                             &retransmitted));
1475   insertedLength += packet_->sizeBytes;  // This packet should be decoded.
1476
1477   seq_num_ += 3;  // One packet drop.
1478   packet_->seqNum = seq_num_;
1479   packet_->timestamp = timestamp_;
1480   packet_->frameType = kVideoFrameKey;
1481   packet_->isFirstPacket = false;
1482   packet_->completeNALU = kNaluComplete;
1483   packet_->markerBit = false;
1484   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
1485                                                             &retransmitted));
1486   insertedLength += packet_->sizeBytes;  // This packet should be decoded.
1487   seq_num_++;
1488   packet_->seqNum = seq_num_;
1489   packet_->timestamp = timestamp_;
1490   packet_->frameType = kVideoFrameKey;
1491   packet_->isFirstPacket = false;
1492   packet_->completeNALU = kNaluStart;
1493   packet_->markerBit = false;
1494   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
1495                                                             &retransmitted));
1496   // This packet should be decoded since it's the beginning of a NAL.
1497   insertedLength += packet_->sizeBytes;
1498
1499   seq_num_ += 2;
1500   packet_->seqNum = seq_num_;
1501   packet_->timestamp = timestamp_;
1502   packet_->frameType = kVideoFrameKey;
1503   packet_->isFirstPacket = false;
1504   packet_->completeNALU = kNaluEnd;
1505   packet_->markerBit = true;
1506   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
1507                                                             &retransmitted));
1508   // This packet should not be decoded because it is an incomplete NAL if it
1509   // is the last.
1510   frame_out = DecodeIncompleteFrame();
1511   // Only last NALU is complete.
1512   CheckOutFrame(frame_out, insertedLength, false);
1513   jitter_buffer_->ReleaseFrame(frame_out);
1514
1515   // Test to insert empty packet.
1516   seq_num_++;
1517   timestamp_ += 33 * 90;
1518   VCMPacket emptypacket(data_, 0, seq_num_, timestamp_, true);
1519   emptypacket.seqNum = seq_num_;
1520   emptypacket.timestamp = timestamp_;
1521   emptypacket.frameType = kVideoFrameKey;
1522   emptypacket.isFirstPacket = true;
1523   emptypacket.completeNALU = kNaluComplete;
1524   emptypacket.markerBit = true;
1525   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(emptypacket,
1526                                                            &retransmitted));
1527   // This packet should not be decoded because it is an incomplete NAL if it
1528   // is the last.
1529
1530   // Will be sent to the decoder, as a packet belonging to a subsequent frame
1531   // has arrived.
1532   frame_out = DecodeIncompleteFrame();
1533
1534
1535   // Test that a frame can include an empty packet.
1536   seq_num_++;
1537   timestamp_ += 33 * 90;
1538
1539   packet_->seqNum = seq_num_;
1540   packet_->timestamp = timestamp_;
1541   packet_->frameType = kVideoFrameKey;
1542   packet_->isFirstPacket = true;
1543   packet_->completeNALU = kNaluComplete;
1544   packet_->markerBit = false;
1545
1546   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
1547                                                             &retransmitted));
1548
1549   seq_num_++;
1550   emptypacket.seqNum = seq_num_;
1551   emptypacket.timestamp = timestamp_;
1552   emptypacket.frameType = kVideoFrameKey;
1553   emptypacket.isFirstPacket = true;
1554   emptypacket.completeNALU = kNaluComplete;
1555   emptypacket.markerBit = true;
1556   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(emptypacket,
1557                                                            &retransmitted));
1558
1559   frame_out = DecodeCompleteFrame();
1560   // Only last NALU is complete
1561   CheckOutFrame(frame_out, packet_->sizeBytes, false);
1562 }
1563
1564 TEST_F(TestBasicJitterBuffer, NextFrameWhenIncomplete) {
1565   // Test that a we cannot get incomplete frames from the JB if we haven't
1566   // received the marker bit, unless we have received a packet from a later
1567   // timestamp.
1568   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
1569   // Start with a complete key frame - insert and decode.
1570   packet_->frameType = kVideoFrameKey;
1571   packet_->isFirstPacket = true;
1572   packet_->markerBit = true;
1573   bool retransmitted = false;
1574
1575   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_,
1576                                                            &retransmitted));
1577   VCMEncodedFrame* frame_out = DecodeCompleteFrame();
1578   EXPECT_TRUE(frame_out != NULL);
1579
1580   packet_->seqNum += 2;
1581   packet_->timestamp += 33 * 90;
1582   packet_->frameType = kVideoFrameDelta;
1583   packet_->isFirstPacket = false;
1584   packet_->markerBit = false;
1585
1586
1587   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
1588                                                             &retransmitted));
1589
1590   frame_out = DecodeIncompleteFrame();
1591   EXPECT_TRUE(frame_out == NULL);
1592
1593   packet_->seqNum += 2;
1594   packet_->timestamp += 33 * 90;
1595   packet_->isFirstPacket = true;
1596
1597   EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_,
1598                                                             &retransmitted));
1599
1600   frame_out = DecodeIncompleteFrame();
1601
1602   CheckOutFrame(frame_out, packet_->sizeBytes, false);
1603 }
1604
1605 TEST_F(TestRunningJitterBuffer, Full) {
1606   // Insert a key frame and decode it.
1607   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
1608   EXPECT_TRUE(DecodeCompleteFrame());
1609   DropFrame(1);
1610   // Fill the jitter buffer.
1611   EXPECT_GE(InsertFrames(kMaxNumberOfFrames, kVideoFrameDelta), kNoError);
1612   // Make sure we can't decode these frames.
1613   EXPECT_FALSE(DecodeCompleteFrame());
1614   // This frame will make the jitter buffer recycle frames until a key frame.
1615   // Since none is found it will have to wait until the next key frame before
1616   // decoding.
1617   EXPECT_GE(InsertFrame(kVideoFrameDelta), kNoError);
1618   EXPECT_FALSE(DecodeCompleteFrame());
1619 }
1620
1621 TEST_F(TestRunningJitterBuffer, EmptyPackets) {
1622   // Make sure a frame can get complete even though empty packets are missing.
1623   stream_generator_->GenerateFrame(kVideoFrameKey, 3, 3,
1624                                    clock_->TimeInMilliseconds());
1625   bool request_key_frame = false;
1626   // Insert empty packet.
1627   EXPECT_EQ(kNoError, InsertPacketAndPop(4));
1628   EXPECT_FALSE(request_key_frame);
1629   // Insert 3 media packets.
1630   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
1631   EXPECT_FALSE(request_key_frame);
1632   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
1633   EXPECT_FALSE(request_key_frame);
1634   EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0));
1635   EXPECT_FALSE(request_key_frame);
1636   // Insert empty packet.
1637   EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0));
1638   EXPECT_FALSE(request_key_frame);
1639 }
1640
1641 TEST_F(TestRunningJitterBuffer, StatisticsTest) {
1642   std::map<FrameType, uint32_t> frame_stats(jitter_buffer_->FrameStatistics());
1643   EXPECT_EQ(0u, frame_stats[kVideoFrameDelta]);
1644   EXPECT_EQ(0u, frame_stats[kVideoFrameKey]);
1645
1646   uint32_t framerate = 0;
1647   uint32_t bitrate = 0;
1648   jitter_buffer_->IncomingRateStatistics(&framerate, &bitrate);
1649   EXPECT_EQ(0u, framerate);
1650   EXPECT_EQ(0u, bitrate);
1651
1652   // Insert a couple of key and delta frames.
1653   InsertFrame(kVideoFrameKey);
1654   InsertFrame(kVideoFrameDelta);
1655   InsertFrame(kVideoFrameDelta);
1656   InsertFrame(kVideoFrameKey);
1657   InsertFrame(kVideoFrameDelta);
1658   // Decode some of them to make sure the statistics doesn't depend on frames
1659   // being decoded.
1660   EXPECT_TRUE(DecodeCompleteFrame());
1661   EXPECT_TRUE(DecodeCompleteFrame());
1662   frame_stats = jitter_buffer_->FrameStatistics();
1663   EXPECT_EQ(3u, frame_stats[kVideoFrameDelta]);
1664   EXPECT_EQ(2u, frame_stats[kVideoFrameKey]);
1665
1666   // Insert 20 more frames to get estimates of bitrate and framerate over
1667   // 1 second.
1668   for (int i = 0; i < 20; ++i) {
1669     InsertFrame(kVideoFrameDelta);
1670   }
1671   jitter_buffer_->IncomingRateStatistics(&framerate, &bitrate);
1672   // TODO(holmer): The current implementation returns the average of the last
1673   // two framerate calculations, which is why it takes two calls to reach the
1674   // actual framerate. This should be fixed.
1675   EXPECT_EQ(kDefaultFrameRate / 2u, framerate);
1676   EXPECT_EQ(kDefaultBitrateKbps, bitrate);
1677   // Insert 25 more frames to get estimates of bitrate and framerate over
1678   // 2 seconds.
1679   for (int i = 0; i < 25; ++i) {
1680     InsertFrame(kVideoFrameDelta);
1681   }
1682   jitter_buffer_->IncomingRateStatistics(&framerate, &bitrate);
1683   EXPECT_EQ(kDefaultFrameRate, framerate);
1684   EXPECT_EQ(kDefaultBitrateKbps, bitrate);
1685 }
1686
1687 TEST_F(TestRunningJitterBuffer, SkipToKeyFrame) {
1688   // Insert delta frames.
1689   EXPECT_GE(InsertFrames(5, kVideoFrameDelta), kNoError);
1690   // Can't decode without a key frame.
1691   EXPECT_FALSE(DecodeCompleteFrame());
1692   InsertFrame(kVideoFrameKey);
1693   // Skip to the next key frame.
1694   EXPECT_TRUE(DecodeCompleteFrame());
1695 }
1696
1697 TEST_F(TestRunningJitterBuffer, DontSkipToKeyFrameIfDecodable) {
1698   InsertFrame(kVideoFrameKey);
1699   EXPECT_TRUE(DecodeCompleteFrame());
1700   const int kNumDeltaFrames = 5;
1701   EXPECT_GE(InsertFrames(kNumDeltaFrames, kVideoFrameDelta), kNoError);
1702   InsertFrame(kVideoFrameKey);
1703   for (int i = 0; i < kNumDeltaFrames + 1; ++i) {
1704     EXPECT_TRUE(DecodeCompleteFrame());
1705   }
1706 }
1707
1708 TEST_F(TestRunningJitterBuffer, KeyDeltaKeyDelta) {
1709   InsertFrame(kVideoFrameKey);
1710   EXPECT_TRUE(DecodeCompleteFrame());
1711   const int kNumDeltaFrames = 5;
1712   EXPECT_GE(InsertFrames(kNumDeltaFrames, kVideoFrameDelta), kNoError);
1713   InsertFrame(kVideoFrameKey);
1714   EXPECT_GE(InsertFrames(kNumDeltaFrames, kVideoFrameDelta), kNoError);
1715   InsertFrame(kVideoFrameKey);
1716   for (int i = 0; i < 2 * (kNumDeltaFrames + 1); ++i) {
1717     EXPECT_TRUE(DecodeCompleteFrame());
1718   }
1719 }
1720
1721 TEST_F(TestRunningJitterBuffer, TwoPacketsNonContinuous) {
1722   InsertFrame(kVideoFrameKey);
1723   EXPECT_TRUE(DecodeCompleteFrame());
1724   stream_generator_->GenerateFrame(kVideoFrameDelta, 1, 0,
1725                                    clock_->TimeInMilliseconds());
1726   clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
1727   stream_generator_->GenerateFrame(kVideoFrameDelta, 2, 0,
1728                                    clock_->TimeInMilliseconds());
1729   EXPECT_EQ(kIncomplete, InsertPacketAndPop(1));
1730   EXPECT_EQ(kCompleteSession, InsertPacketAndPop(1));
1731   EXPECT_FALSE(DecodeCompleteFrame());
1732   EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0));
1733   EXPECT_TRUE(DecodeCompleteFrame());
1734   EXPECT_TRUE(DecodeCompleteFrame());
1735 }
1736
1737 TEST_F(TestJitterBufferNack, EmptyPackets) {
1738   // Make sure empty packets doesn't clog the jitter buffer.
1739   jitter_buffer_->SetNackMode(kNack, media_optimization::kLowRttNackMs, -1);
1740   EXPECT_GE(InsertFrames(kMaxNumberOfFrames, kFrameEmpty), kNoError);
1741   InsertFrame(kVideoFrameKey);
1742   EXPECT_TRUE(DecodeCompleteFrame());
1743 }
1744
1745 TEST_F(TestJitterBufferNack, NackTooOldPackets) {
1746   // Insert a key frame and decode it.
1747   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
1748   EXPECT_TRUE(DecodeCompleteFrame());
1749
1750   // Drop one frame and insert |kNackHistoryLength| to trigger NACKing a too
1751   // old packet.
1752   DropFrame(1);
1753   // Insert a frame which should trigger a recycle until the next key frame.
1754   EXPECT_EQ(kFlushIndicator, InsertFrames(oldest_packet_to_nack_ + 1,
1755                                           kVideoFrameDelta));
1756   EXPECT_FALSE(DecodeCompleteFrame());
1757
1758   uint16_t nack_list_length = max_nack_list_size_;
1759   bool request_key_frame = false;
1760   uint16_t* nack_list = jitter_buffer_->GetNackList(&nack_list_length,
1761                                                     &request_key_frame);
1762   // No key frame will be requested since the jitter buffer is empty.
1763   EXPECT_FALSE(request_key_frame);
1764   EXPECT_TRUE(nack_list == NULL);
1765   EXPECT_EQ(0, nack_list_length);
1766
1767   EXPECT_GE(InsertFrame(kVideoFrameDelta), kNoError);
1768   // Verify that the jitter buffer requests a key frame since we need one to
1769   // start decoding.
1770   EXPECT_FALSE(request_key_frame);
1771   EXPECT_TRUE(nack_list == NULL);
1772   EXPECT_EQ(0, nack_list_length);
1773   // Waiting for a key frame.
1774   EXPECT_FALSE(DecodeCompleteFrame());
1775   EXPECT_FALSE(DecodeIncompleteFrame());
1776
1777   // The next complete continuous frame isn't a key frame, but we're waiting
1778   // for one.
1779   EXPECT_FALSE(DecodeCompleteFrame());
1780   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
1781   // Skipping ahead to the key frame.
1782   EXPECT_TRUE(DecodeCompleteFrame());
1783 }
1784
1785 TEST_F(TestJitterBufferNack, NackLargeJitterBuffer) {
1786   // Insert a key frame and decode it.
1787   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
1788   EXPECT_TRUE(DecodeCompleteFrame());
1789
1790   // Insert a frame which should trigger a recycle until the next key frame.
1791   EXPECT_GE(InsertFrames(oldest_packet_to_nack_, kVideoFrameDelta), kNoError);
1792
1793   uint16_t nack_list_length = max_nack_list_size_;
1794   bool request_key_frame = false;
1795   jitter_buffer_->GetNackList(&nack_list_length, &request_key_frame);
1796   // Verify that the jitter buffer does not request a key frame.
1797   EXPECT_FALSE(request_key_frame);
1798   // Verify that no packets are NACKed.
1799   EXPECT_EQ(0, nack_list_length);
1800   // Verify that we can decode the next frame.
1801   EXPECT_TRUE(DecodeCompleteFrame());
1802 }
1803
1804 TEST_F(TestJitterBufferNack, NackListFull) {
1805   // Insert a key frame and decode it.
1806   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
1807   EXPECT_TRUE(DecodeCompleteFrame());
1808
1809   // Generate and drop |kNackHistoryLength| packets to fill the NACK list.
1810   DropFrame(max_nack_list_size_ + 1);
1811   // Insert a frame which should trigger a recycle until the next key frame.
1812   EXPECT_EQ(kFlushIndicator, InsertFrame(kVideoFrameDelta));
1813   EXPECT_FALSE(DecodeCompleteFrame());
1814
1815   uint16_t nack_list_length = max_nack_list_size_;
1816   bool request_key_frame = false;
1817   jitter_buffer_->GetNackList(&nack_list_length, &request_key_frame);
1818   // The jitter buffer is empty, so we won't request key frames until we get a
1819   // packet.
1820   EXPECT_FALSE(request_key_frame);
1821
1822   EXPECT_GE(InsertFrame(kVideoFrameDelta), kNoError);
1823   // Now we have a packet in the jitter buffer, a key frame will be requested
1824   // since it's not a key frame.
1825   jitter_buffer_->GetNackList(&nack_list_length, &request_key_frame);
1826   // The jitter buffer is empty, so we won't request key frames until we get a
1827   // packet.
1828   EXPECT_TRUE(request_key_frame);
1829   // The next complete continuous frame isn't a key frame, but we're waiting
1830   // for one.
1831   EXPECT_FALSE(DecodeCompleteFrame());
1832   EXPECT_FALSE(DecodeIncompleteFrame());
1833   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
1834   // Skipping ahead to the key frame.
1835   EXPECT_TRUE(DecodeCompleteFrame());
1836 }
1837
1838 TEST_F(TestJitterBufferNack, NoNackListReturnedBeforeFirstDecode) {
1839   DropFrame(10);
1840   // Insert a frame and try to generate a NACK list. Shouldn't get one.
1841   EXPECT_GE(InsertFrame(kVideoFrameDelta), kNoError);
1842   uint16_t nack_list_size = 0;
1843   bool request_key_frame = false;
1844   uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size,
1845                                                   &request_key_frame);
1846   // No list generated, and a key frame request is signaled.
1847   EXPECT_TRUE(list == NULL);
1848   EXPECT_EQ(0, nack_list_size);
1849   EXPECT_TRUE(request_key_frame);
1850 }
1851
1852 TEST_F(TestJitterBufferNack, NackListBuiltBeforeFirstDecode) {
1853   stream_generator_->Init(0, 0, clock_->TimeInMilliseconds());
1854   InsertFrame(kVideoFrameKey);
1855   stream_generator_->GenerateFrame(kVideoFrameDelta, 2, 0,
1856                                   clock_->TimeInMilliseconds());
1857   stream_generator_->NextPacket(NULL);  // Drop packet.
1858   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
1859   EXPECT_TRUE(DecodeCompleteFrame());
1860   uint16_t nack_list_size = 0;
1861   bool extended = false;
1862   uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, &extended);
1863   EXPECT_EQ(1, nack_list_size);
1864   EXPECT_TRUE(list != NULL);
1865 }
1866
1867 TEST_F(TestJitterBufferNack, VerifyRetransmittedFlag) {
1868   stream_generator_->Init(0, 0, clock_->TimeInMilliseconds());
1869   stream_generator_->GenerateFrame(kVideoFrameKey, 3, 0,
1870                                    clock_->TimeInMilliseconds());
1871   VCMPacket packet;
1872   stream_generator_->PopPacket(&packet, 0);
1873   bool retransmitted = false;
1874   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(packet, &retransmitted));
1875   EXPECT_FALSE(retransmitted);
1876   // Drop second packet.
1877   stream_generator_->PopPacket(&packet, 1);
1878   EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(packet, &retransmitted));
1879   EXPECT_FALSE(retransmitted);
1880   EXPECT_FALSE(DecodeCompleteFrame());
1881   uint16_t nack_list_size = 0;
1882   bool extended = false;
1883   uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, &extended);
1884   EXPECT_EQ(1, nack_list_size);
1885   ASSERT_TRUE(list != NULL);
1886   stream_generator_->PopPacket(&packet, 0);
1887   EXPECT_EQ(packet.seqNum, list[0]);
1888   EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(packet,
1889                                                            &retransmitted));
1890   EXPECT_TRUE(retransmitted);
1891   EXPECT_TRUE(DecodeCompleteFrame());
1892 }
1893
1894 TEST_F(TestJitterBufferNack, UseNackToRecoverFirstKeyFrame) {
1895   stream_generator_->Init(0, 0, clock_->TimeInMilliseconds());
1896   stream_generator_->GenerateFrame(kVideoFrameKey, 3, 0,
1897                                   clock_->TimeInMilliseconds());
1898   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
1899   // Drop second packet.
1900   EXPECT_EQ(kIncomplete, InsertPacketAndPop(1));
1901   EXPECT_FALSE(DecodeCompleteFrame());
1902   uint16_t nack_list_size = 0;
1903   bool extended = false;
1904   uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, &extended);
1905   EXPECT_EQ(1, nack_list_size);
1906   ASSERT_TRUE(list != NULL);
1907   VCMPacket packet;
1908   stream_generator_->GetPacket(&packet, 0);
1909   EXPECT_EQ(packet.seqNum, list[0]);
1910 }
1911
1912 TEST_F(TestJitterBufferNack, UseNackToRecoverFirstKeyFrameSecondInQueue) {
1913   VCMPacket packet;
1914   stream_generator_->Init(0, 0, clock_->TimeInMilliseconds());
1915   // First frame is delta.
1916   stream_generator_->GenerateFrame(kVideoFrameDelta, 3, 0,
1917                                    clock_->TimeInMilliseconds());
1918   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
1919   // Drop second packet in frame.
1920   ASSERT_TRUE(stream_generator_->PopPacket(&packet, 0));
1921   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
1922   // Second frame is key.
1923   stream_generator_->GenerateFrame(kVideoFrameKey, 3, 0,
1924                                    clock_->TimeInMilliseconds() + 10);
1925   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
1926   // Drop second packet in frame.
1927   EXPECT_EQ(kIncomplete, InsertPacketAndPop(1));
1928   EXPECT_FALSE(DecodeCompleteFrame());
1929   uint16_t nack_list_size = 0;
1930   bool extended = false;
1931   uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, &extended);
1932   EXPECT_EQ(1, nack_list_size);
1933   ASSERT_TRUE(list != NULL);
1934   stream_generator_->GetPacket(&packet, 0);
1935   EXPECT_EQ(packet.seqNum, list[0]);
1936 }
1937
1938 TEST_F(TestJitterBufferNack, NormalOperation) {
1939   EXPECT_EQ(kNack, jitter_buffer_->nack_mode());
1940   jitter_buffer_->SetDecodeErrorMode(kWithErrors);
1941
1942   EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError);
1943   EXPECT_TRUE(DecodeIncompleteFrame());
1944
1945   //  ----------------------------------------------------------------
1946   // | 1 | 2 | .. | 8 | 9 | x | 11 | 12 | .. | 19 | x | 21 | .. | 100 |
1947   //  ----------------------------------------------------------------
1948   stream_generator_->GenerateFrame(kVideoFrameKey, 100, 0,
1949                                   clock_->TimeInMilliseconds());
1950   clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
1951   EXPECT_EQ(kDecodableSession, InsertPacketAndPop(0));
1952   // Verify that the frame is incomplete.
1953   EXPECT_FALSE(DecodeCompleteFrame());
1954   while (stream_generator_->PacketsRemaining() > 1) {
1955     if (stream_generator_->NextSequenceNumber() % 10 != 0) {
1956       EXPECT_EQ(kDecodableSession, InsertPacketAndPop(0));
1957     } else {
1958       stream_generator_->NextPacket(NULL);  // Drop packet
1959     }
1960   }
1961   EXPECT_EQ(kDecodableSession, InsertPacketAndPop(0));
1962   EXPECT_EQ(0, stream_generator_->PacketsRemaining());
1963   EXPECT_FALSE(DecodeCompleteFrame());
1964   EXPECT_FALSE(DecodeIncompleteFrame());
1965   uint16_t nack_list_size = 0;
1966   bool request_key_frame = false;
1967   uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size,
1968                                                &request_key_frame);
1969   // Verify the NACK list.
1970   const int kExpectedNackSize = 9;
1971   ASSERT_EQ(kExpectedNackSize, nack_list_size);
1972   for (int i = 0; i < nack_list_size; ++i)
1973     EXPECT_EQ((1 + i) * 10, list[i]);
1974 }
1975
1976 TEST_F(TestJitterBufferNack, NormalOperationWrap) {
1977   bool request_key_frame = false;
1978   //  -------   ------------------------------------------------------------
1979   // | 65532 | | 65533 | 65534 | 65535 | x | 1 | .. | 9 | x | 11 |.....| 96 |
1980   //  -------   ------------------------------------------------------------
1981   stream_generator_->Init(65532, 0, clock_->TimeInMilliseconds());
1982   InsertFrame(kVideoFrameKey);
1983   EXPECT_FALSE(request_key_frame);
1984   EXPECT_TRUE(DecodeCompleteFrame());
1985   stream_generator_->GenerateFrame(kVideoFrameDelta, 100, 0,
1986                                   clock_->TimeInMilliseconds());
1987   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
1988   while (stream_generator_->PacketsRemaining() > 1) {
1989     if (stream_generator_->NextSequenceNumber() % 10 != 0) {
1990       EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
1991       EXPECT_FALSE(request_key_frame);
1992     } else {
1993       stream_generator_->NextPacket(NULL);  // Drop packet
1994     }
1995   }
1996   EXPECT_EQ(kIncomplete, InsertPacketAndPop(0));
1997   EXPECT_FALSE(request_key_frame);
1998   EXPECT_EQ(0, stream_generator_->PacketsRemaining());
1999   EXPECT_FALSE(DecodeCompleteFrame());
2000   EXPECT_FALSE(DecodeCompleteFrame());
2001   uint16_t nack_list_size = 0;
2002   bool extended = false;
2003   uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, &extended);
2004   // Verify the NACK list.
2005   const int kExpectedNackSize = 10;
2006   ASSERT_EQ(kExpectedNackSize, nack_list_size);
2007   for (int i = 0; i < nack_list_size; ++i)
2008     EXPECT_EQ(i * 10, list[i]);
2009 }
2010
2011 TEST_F(TestJitterBufferNack, NormalOperationWrap2) {
2012   bool request_key_frame = false;
2013   //  -----------------------------------
2014   // | 65532 | 65533 | 65534 | x | 0 | 1 |
2015   //  -----------------------------------
2016   stream_generator_->Init(65532, 0, clock_->TimeInMilliseconds());
2017   InsertFrame(kVideoFrameKey);
2018   EXPECT_FALSE(request_key_frame);
2019   EXPECT_TRUE(DecodeCompleteFrame());
2020   stream_generator_->GenerateFrame(kVideoFrameDelta, 1, 0,
2021                                    clock_->TimeInMilliseconds());
2022   clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
2023   for (int i = 0; i < 5; ++i) {
2024     if (stream_generator_->NextSequenceNumber()  != 65535) {
2025       EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0));
2026       EXPECT_FALSE(request_key_frame);
2027     } else {
2028       stream_generator_->NextPacket(NULL);  // Drop packet
2029     }
2030     stream_generator_->GenerateFrame(kVideoFrameDelta, 1, 0,
2031                                      clock_->TimeInMilliseconds());
2032     clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
2033   }
2034   EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0));
2035   EXPECT_FALSE(request_key_frame);
2036   uint16_t nack_list_size = 0;
2037   bool extended = false;
2038   uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, &extended);
2039   // Verify the NACK list.
2040   ASSERT_EQ(1, nack_list_size);
2041   EXPECT_EQ(65535, list[0]);
2042 }
2043
2044 TEST_F(TestJitterBufferNack, ResetByFutureKeyFrameDoesntError) {
2045   stream_generator_->Init(0, 0, clock_->TimeInMilliseconds());
2046   InsertFrame(kVideoFrameKey);
2047   EXPECT_TRUE(DecodeCompleteFrame());
2048   uint16_t nack_list_size = 0;
2049   bool extended = false;
2050   jitter_buffer_->GetNackList(&nack_list_size, &extended);
2051   EXPECT_EQ(0, nack_list_size);
2052
2053   // Far-into-the-future video frame, could be caused by resetting the encoder
2054   // or otherwise restarting. This should not fail when error when the packet is
2055   // a keyframe, even if all of the nack list needs to be flushed.
2056   stream_generator_->Init(10000, 0, clock_->TimeInMilliseconds());
2057   clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
2058   InsertFrame(kVideoFrameKey);
2059   EXPECT_TRUE(DecodeCompleteFrame());
2060   jitter_buffer_->GetNackList(&nack_list_size, &extended);
2061   EXPECT_EQ(0, nack_list_size);
2062
2063   // Stream should be decodable from this point.
2064   clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs);
2065   InsertFrame(kVideoFrameDelta);
2066   EXPECT_TRUE(DecodeCompleteFrame());
2067   jitter_buffer_->GetNackList(&nack_list_size, &extended);
2068   EXPECT_EQ(0, nack_list_size);
2069 }
2070
2071 }  // namespace webrtc