Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / filters / source_buffer_stream_unittest.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/filters/source_buffer_stream.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "media/base/data_buffer.h"
16 #include "media/base/media_log.h"
17 #include "media/base/test_helpers.h"
18 #include "media/base/text_track_config.h"
19 #include "media/filters/webvtt_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace media {
23
24 static const int kDefaultFramesPerSecond = 30;
25 static const int kDefaultKeyframesPerSecond = 6;
26 static const uint8 kDataA = 0x11;
27 static const uint8 kDataB = 0x33;
28 static const int kDataSize = 1;
29
30 class SourceBufferStreamTest : public testing::Test {
31  protected:
32   SourceBufferStreamTest() {
33     video_config_ = TestVideoConfig::Normal();
34     SetStreamInfo(kDefaultFramesPerSecond, kDefaultKeyframesPerSecond);
35     stream_.reset(new SourceBufferStream(video_config_, log_cb()));
36   }
37
38   void SetMemoryLimit(int buffers_of_data) {
39     stream_->set_memory_limit_for_testing(buffers_of_data * kDataSize);
40   }
41
42   void SetStreamInfo(int frames_per_second, int keyframes_per_second) {
43     frames_per_second_ = frames_per_second;
44     keyframes_per_second_ = keyframes_per_second;
45     frame_duration_ = ConvertToFrameDuration(frames_per_second);
46   }
47
48   void SetTextStream() {
49     video_config_ = TestVideoConfig::Invalid();
50     TextTrackConfig config(kTextSubtitles, "", "", "");
51     stream_.reset(new SourceBufferStream(config, LogCB()));
52     SetStreamInfo(2, 2);
53   }
54
55   void NewSegmentAppend(int starting_position, int number_of_buffers) {
56     AppendBuffers(starting_position, number_of_buffers, true,
57                   base::TimeDelta(), true, &kDataA, kDataSize);
58   }
59
60   void NewSegmentAppend(int starting_position, int number_of_buffers,
61                         const uint8* data) {
62     AppendBuffers(starting_position, number_of_buffers, true,
63                   base::TimeDelta(), true, data, kDataSize);
64   }
65
66   void NewSegmentAppend_OffsetFirstBuffer(
67       int starting_position, int number_of_buffers,
68       base::TimeDelta first_buffer_offset) {
69     AppendBuffers(starting_position, number_of_buffers, true,
70                   first_buffer_offset, true, &kDataA, kDataSize);
71   }
72
73   void NewSegmentAppend_ExpectFailure(
74       int starting_position, int number_of_buffers) {
75     AppendBuffers(starting_position, number_of_buffers, true,
76                   base::TimeDelta(), false, &kDataA, kDataSize);
77   }
78
79   void AppendBuffers(int starting_position, int number_of_buffers) {
80     AppendBuffers(starting_position, number_of_buffers, false,
81                   base::TimeDelta(), true, &kDataA, kDataSize);
82   }
83
84   void AppendBuffers(int starting_position, int number_of_buffers,
85                      const uint8* data) {
86     AppendBuffers(starting_position, number_of_buffers, false,
87                   base::TimeDelta(), true, data, kDataSize);
88   }
89
90   void NewSegmentAppend(const std::string& buffers_to_append) {
91     AppendBuffers(buffers_to_append, true, kNoTimestamp(), false, true);
92   }
93
94   void NewSegmentAppend(base::TimeDelta start_timestamp,
95                         const std::string& buffers_to_append) {
96     AppendBuffers(buffers_to_append, true, start_timestamp, false, true);
97   }
98
99   void AppendBuffers(const std::string& buffers_to_append) {
100     AppendBuffers(buffers_to_append, false, kNoTimestamp(), false, true);
101   }
102
103   void NewSegmentAppendOneByOne(const std::string& buffers_to_append) {
104     AppendBuffers(buffers_to_append, true, kNoTimestamp(), true, true);
105   }
106
107   void AppendBuffersOneByOne(const std::string& buffers_to_append) {
108     AppendBuffers(buffers_to_append, false, kNoTimestamp(), true, true);
109   }
110
111   void NewSegmentAppend_ExpectFailure(const std::string& buffers_to_append) {
112     AppendBuffers(buffers_to_append, true, kNoTimestamp(), false, false);
113   }
114
115   void AppendBuffers_ExpectFailure(const std::string& buffers_to_append) {
116     AppendBuffers(buffers_to_append, false, kNoTimestamp(), false, false);
117   }
118
119   void Seek(int position) {
120     stream_->Seek(position * frame_duration_);
121   }
122
123   void SeekToTimestamp(base::TimeDelta timestamp) {
124     stream_->Seek(timestamp);
125   }
126
127   void RemoveInMs(int start, int end, int duration) {
128     Remove(base::TimeDelta::FromMilliseconds(start),
129            base::TimeDelta::FromMilliseconds(end),
130            base::TimeDelta::FromMilliseconds(duration));
131   }
132
133   void Remove(base::TimeDelta start, base::TimeDelta end,
134               base::TimeDelta duration) {
135     stream_->Remove(start, end, duration);
136   }
137
138   int GetRemovalRangeInMs(int start, int end, int bytes_to_free,
139                           int* removal_end) {
140     base::TimeDelta removal_end_timestamp =
141         base::TimeDelta::FromMilliseconds(*removal_end);
142     int bytes_removed = stream_->GetRemovalRange(
143         base::TimeDelta::FromMilliseconds(start),
144         base::TimeDelta::FromMilliseconds(end), bytes_to_free,
145         &removal_end_timestamp);
146     *removal_end = removal_end_timestamp.InMilliseconds();
147     return bytes_removed;
148   }
149
150   void CheckExpectedRanges(const std::string& expected) {
151     Ranges<base::TimeDelta> r = stream_->GetBufferedTime();
152
153     std::stringstream ss;
154     ss << "{ ";
155     for (size_t i = 0; i < r.size(); ++i) {
156       int64 start = (r.start(i) / frame_duration_);
157       int64 end = (r.end(i) / frame_duration_) - 1;
158       ss << "[" << start << "," << end << ") ";
159     }
160     ss << "}";
161     EXPECT_EQ(expected, ss.str());
162   }
163
164   void CheckExpectedRangesByTimestamp(const std::string& expected) {
165     Ranges<base::TimeDelta> r = stream_->GetBufferedTime();
166
167     std::stringstream ss;
168     ss << "{ ";
169     for (size_t i = 0; i < r.size(); ++i) {
170       int64 start = r.start(i).InMilliseconds();
171       int64 end = r.end(i).InMilliseconds();
172       ss << "[" << start << "," << end << ") ";
173     }
174     ss << "}";
175     EXPECT_EQ(expected, ss.str());
176   }
177
178   void CheckExpectedBuffers(
179       int starting_position, int ending_position) {
180     CheckExpectedBuffers(starting_position, ending_position, false, NULL, 0);
181   }
182
183   void CheckExpectedBuffers(
184       int starting_position, int ending_position, bool expect_keyframe) {
185     CheckExpectedBuffers(starting_position, ending_position, expect_keyframe,
186                          NULL, 0);
187   }
188
189   void CheckExpectedBuffers(
190       int starting_position, int ending_position, const uint8* data) {
191     CheckExpectedBuffers(starting_position, ending_position, false, data,
192                          kDataSize);
193   }
194
195   void CheckExpectedBuffers(
196       int starting_position, int ending_position, const uint8* data,
197       bool expect_keyframe) {
198     CheckExpectedBuffers(starting_position, ending_position, expect_keyframe,
199                          data, kDataSize);
200   }
201
202   void CheckExpectedBuffers(
203       int starting_position, int ending_position, bool expect_keyframe,
204       const uint8* expected_data, int expected_size) {
205     int current_position = starting_position;
206     for (; current_position <= ending_position; current_position++) {
207       scoped_refptr<StreamParserBuffer> buffer;
208       SourceBufferStream::Status status = stream_->GetNextBuffer(&buffer);
209
210       EXPECT_NE(status, SourceBufferStream::kConfigChange);
211       if (status != SourceBufferStream::kSuccess)
212         break;
213
214       if (expect_keyframe && current_position == starting_position)
215         EXPECT_TRUE(buffer->IsKeyframe());
216
217       if (expected_data) {
218         const uint8* actual_data = buffer->data();
219         const int actual_size = buffer->data_size();
220         EXPECT_EQ(expected_size, actual_size);
221         for (int i = 0; i < std::min(actual_size, expected_size); i++) {
222           EXPECT_EQ(expected_data[i], actual_data[i]);
223         }
224       }
225
226       EXPECT_EQ(buffer->GetDecodeTimestamp() / frame_duration_,
227                 current_position);
228     }
229
230     EXPECT_EQ(ending_position + 1, current_position);
231   }
232
233   void CheckExpectedBuffers(const std::string& expected) {
234     std::vector<std::string> timestamps;
235     base::SplitString(expected, ' ', &timestamps);
236     std::stringstream ss;
237     for (size_t i = 0; i < timestamps.size(); i++) {
238       scoped_refptr<StreamParserBuffer> buffer;
239       SourceBufferStream::Status status = stream_->GetNextBuffer(&buffer);
240
241       if (i > 0)
242         ss << " ";
243
244       if (timestamps[i] == "C") {
245         EXPECT_EQ(SourceBufferStream::kConfigChange, status);
246         stream_->GetCurrentVideoDecoderConfig();
247         ss << timestamps[i];
248         continue;
249       }
250
251       EXPECT_EQ(SourceBufferStream::kSuccess, status);
252       if (status != SourceBufferStream::kSuccess)
253         break;
254
255       ss << buffer->GetDecodeTimestamp().InMilliseconds();
256       if (buffer->IsKeyframe())
257         ss << "K";
258     }
259     EXPECT_EQ(expected, ss.str());
260   }
261
262   void CheckNoNextBuffer() {
263     scoped_refptr<StreamParserBuffer> buffer;
264     EXPECT_EQ(SourceBufferStream::kNeedBuffer, stream_->GetNextBuffer(&buffer));
265   }
266
267   void CheckVideoConfig(const VideoDecoderConfig& config) {
268     const VideoDecoderConfig& actual = stream_->GetCurrentVideoDecoderConfig();
269     EXPECT_TRUE(actual.Matches(config))
270         << "Expected: " << config.AsHumanReadableString()
271         << "\nActual: " << actual.AsHumanReadableString();
272   }
273
274   const LogCB log_cb() {
275     return base::Bind(&SourceBufferStreamTest::DebugMediaLog,
276                       base::Unretained(this));
277   }
278
279   base::TimeDelta frame_duration() const { return frame_duration_; }
280
281   scoped_ptr<SourceBufferStream> stream_;
282   VideoDecoderConfig video_config_;
283
284  private:
285   base::TimeDelta ConvertToFrameDuration(int frames_per_second) {
286     return base::TimeDelta::FromMicroseconds(
287         base::Time::kMicrosecondsPerSecond / frames_per_second);
288   }
289
290   void AppendBuffers(int starting_position,
291                      int number_of_buffers,
292                      bool begin_media_segment,
293                      base::TimeDelta first_buffer_offset,
294                      bool expect_success,
295                      const uint8* data,
296                      int size) {
297     if (begin_media_segment)
298       stream_->OnNewMediaSegment(starting_position * frame_duration_);
299
300     int keyframe_interval = frames_per_second_ / keyframes_per_second_;
301
302     SourceBufferStream::BufferQueue queue;
303     for (int i = 0; i < number_of_buffers; i++) {
304       int position = starting_position + i;
305       bool is_keyframe = position % keyframe_interval == 0;
306       // Buffer type and track ID are meaningless to these tests.
307       scoped_refptr<StreamParserBuffer> buffer =
308           StreamParserBuffer::CopyFrom(data, size, is_keyframe,
309                                        DemuxerStream::AUDIO, 0);
310       base::TimeDelta timestamp = frame_duration_ * position;
311
312       if (i == 0)
313         timestamp += first_buffer_offset;
314       buffer->SetDecodeTimestamp(timestamp);
315
316       // Simulate an IBB...BBP pattern in which all B-frames reference both
317       // the I- and P-frames. For a GOP with playback order 12345, this would
318       // result in a decode timestamp order of 15234.
319       base::TimeDelta presentation_timestamp;
320       if (is_keyframe) {
321         presentation_timestamp = timestamp;
322       } else if ((position - 1) % keyframe_interval == 0) {
323         // This is the P-frame (first frame following the I-frame)
324         presentation_timestamp =
325             (timestamp + frame_duration_ * (keyframe_interval - 2));
326       } else {
327         presentation_timestamp = timestamp - frame_duration_;
328       }
329       buffer->set_timestamp(presentation_timestamp);
330
331       queue.push_back(buffer);
332     }
333     if (!queue.empty())
334       EXPECT_EQ(expect_success, stream_->Append(queue));
335   }
336
337   // StringToBufferQueue() allows for the generation of StreamParserBuffers from
338   // coded strings of timestamps separated by spaces.  Supported syntax:
339   //
340   // ##:
341   // Generates a StreamParserBuffer with decode timestamp ##.  E.g., "0 1 2 3".
342   //
343   // ##K:
344   // Indicates the buffer with timestamp ## reflects a keyframe.  E.g., "0K 1".
345   //
346   // S(a# ... y# z#)
347   // Indicates a splice frame buffer should be created with timestamp z#.  The
348   // preceding timestamps a# ... y# will be treated as the fade out preroll for
349   // the splice frame.  If a timestamp within the preroll ends with C the config
350   // id to use for that and subsequent preroll appends is incremented by one.
351   // The config id for non-splice frame appends will not be affected.
352   SourceBufferStream::BufferQueue StringToBufferQueue(
353       const std::string& buffers_to_append) {
354     std::vector<std::string> timestamps;
355     base::SplitString(buffers_to_append, ' ', &timestamps);
356
357     CHECK_GT(timestamps.size(), 0u);
358
359     bool splice_frame = false;
360     size_t splice_config_id = stream_->append_config_index_;
361     std::vector<scoped_refptr<StreamParserBuffer> > fade_out_preroll;
362     SourceBufferStream::BufferQueue buffers;
363     for (size_t i = 0; i < timestamps.size(); i++) {
364       bool is_keyframe = false;
365       bool last_splice_frame = false;
366       // Handle splice frame starts.
367       if (StartsWithASCII(timestamps[i], "S(", true)) {
368         CHECK(!splice_frame);
369         splice_frame = true;
370         // Remove the "S(" off of the token.
371         timestamps[i] = timestamps[i].substr(2, timestamps[i].length());
372       }
373       if (splice_frame && EndsWith(timestamps[i], ")", true)) {
374         splice_frame = false;
375         last_splice_frame = true;
376         // Remove the ")" off of the token.
377         timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
378       }
379       // Handle config changes within the splice frame.
380       if (splice_frame && EndsWith(timestamps[i], "C", true)) {
381         splice_config_id++;
382         CHECK(splice_config_id < stream_->audio_configs_.size() ||
383               splice_config_id < stream_->video_configs_.size());
384         // Remove the "C" off of the token.
385         timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
386       }
387       if (EndsWith(timestamps[i], "K", true)) {
388         is_keyframe = true;
389         // Remove the "K" off of the token.
390         timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
391       }
392
393       int time_in_ms;
394       CHECK(base::StringToInt(timestamps[i], &time_in_ms));
395
396       // Create buffer. Buffer type and track ID are meaningless to these tests.
397       scoped_refptr<StreamParserBuffer> buffer =
398           StreamParserBuffer::CopyFrom(&kDataA, kDataSize, is_keyframe,
399                                        DemuxerStream::AUDIO, 0);
400       base::TimeDelta timestamp =
401           base::TimeDelta::FromMilliseconds(time_in_ms);
402       buffer->set_timestamp(timestamp);
403       buffer->SetDecodeTimestamp(timestamp);
404
405       if (splice_frame) {
406         if (!fade_out_preroll.empty()) {
407           // Enforce strictly monotonically increasing timestamps.
408           CHECK_GT(
409               timestamp.InMicroseconds(),
410               fade_out_preroll.back()->GetDecodeTimestamp().InMicroseconds());
411         }
412         buffer->SetConfigId(splice_config_id);
413         fade_out_preroll.push_back(buffer);
414         continue;
415       }
416
417       if (last_splice_frame) {
418         // Forbid splice frames without preroll.
419         CHECK(!fade_out_preroll.empty());
420         buffer->SetFadeOutPreroll(fade_out_preroll);
421         fade_out_preroll.clear();
422       }
423
424       buffers.push_back(buffer);
425     }
426     return buffers;
427   }
428
429   void AppendBuffers(const std::string& buffers_to_append,
430                      bool start_new_segment,
431                      base::TimeDelta segment_start_timestamp,
432                      bool one_by_one,
433                      bool expect_success) {
434     SourceBufferStream::BufferQueue buffers =
435         StringToBufferQueue(buffers_to_append);
436
437     if (start_new_segment) {
438       base::TimeDelta start_timestamp = segment_start_timestamp;
439       if (start_timestamp == kNoTimestamp())
440         start_timestamp = buffers[0]->GetDecodeTimestamp();
441
442       ASSERT_TRUE(start_timestamp <= buffers[0]->GetDecodeTimestamp());
443
444       stream_->OnNewMediaSegment(start_timestamp);
445     }
446
447     if (!one_by_one) {
448       EXPECT_EQ(expect_success, stream_->Append(buffers));
449       return;
450     }
451
452     // Append each buffer one by one.
453     for (size_t i = 0; i < buffers.size(); i++) {
454       SourceBufferStream::BufferQueue wrapper;
455       wrapper.push_back(buffers[i]);
456       EXPECT_TRUE(stream_->Append(wrapper));
457     }
458   }
459
460   void DebugMediaLog(const std::string& log) {
461     DVLOG(1) << log;
462   }
463
464   int frames_per_second_;
465   int keyframes_per_second_;
466   base::TimeDelta frame_duration_;
467   DISALLOW_COPY_AND_ASSIGN(SourceBufferStreamTest);
468 };
469
470 TEST_F(SourceBufferStreamTest, Append_SingleRange) {
471   // Append 15 buffers at positions 0 through 14.
472   NewSegmentAppend(0, 15);
473
474   // Check expected range.
475   CheckExpectedRanges("{ [0,14) }");
476   // Check buffers in range.
477   Seek(0);
478   CheckExpectedBuffers(0, 14);
479 }
480
481 TEST_F(SourceBufferStreamTest, Append_SingleRange_OneBufferAtATime) {
482   // Append 15 buffers starting at position 0, one buffer at a time.
483   NewSegmentAppend(0, 1);
484   for (int i = 1; i < 15; i++)
485     AppendBuffers(i, 1);
486
487   // Check expected range.
488   CheckExpectedRanges("{ [0,14) }");
489   // Check buffers in range.
490   Seek(0);
491   CheckExpectedBuffers(0, 14);
492 }
493
494 TEST_F(SourceBufferStreamTest, Append_DisjointRanges) {
495   // Append 5 buffers at positions 0 through 4.
496   NewSegmentAppend(0, 5);
497
498   // Append 10 buffers at positions 15 through 24.
499   NewSegmentAppend(15, 10);
500
501   // Check expected ranges.
502   CheckExpectedRanges("{ [0,4) [15,24) }");
503   // Check buffers in ranges.
504   Seek(0);
505   CheckExpectedBuffers(0, 4);
506   Seek(15);
507   CheckExpectedBuffers(15, 24);
508 }
509
510 TEST_F(SourceBufferStreamTest, Append_AdjacentRanges) {
511   // Append 10 buffers at positions 0 through 9.
512   NewSegmentAppend(0, 10);
513
514   // Append 11 buffers at positions 15 through 25.
515   NewSegmentAppend(15, 11);
516
517   // Append 5 buffers at positions 10 through 14 to bridge the gap.
518   NewSegmentAppend(10, 5);
519
520   // Check expected range.
521   CheckExpectedRanges("{ [0,25) }");
522   // Check buffers in range.
523   Seek(0);
524   CheckExpectedBuffers(0, 25);
525 }
526
527 TEST_F(SourceBufferStreamTest, Append_DoesNotBeginWithKeyframe) {
528   // Append fails because the range doesn't begin with a keyframe.
529   NewSegmentAppend_ExpectFailure(3, 2);
530
531   // Append 10 buffers at positions 5 through 14.
532   NewSegmentAppend(5, 10);
533
534   // Check expected range.
535   CheckExpectedRanges("{ [5,14) }");
536   // Check buffers in range.
537   Seek(5);
538   CheckExpectedBuffers(5, 14);
539
540   // Append fails because the range doesn't begin with a keyframe.
541   NewSegmentAppend_ExpectFailure(17, 3);
542
543   CheckExpectedRanges("{ [5,14) }");
544   Seek(5);
545   CheckExpectedBuffers(5, 14);
546 }
547
548 TEST_F(SourceBufferStreamTest, Append_DoesNotBeginWithKeyframe_Adjacent) {
549   // Append 8 buffers at positions 0 through 7.
550   NewSegmentAppend(0, 8);
551
552   // Now start a new media segment at position 8. Append should fail because
553   // the media segment does not begin with a keyframe.
554   NewSegmentAppend_ExpectFailure(8, 2);
555
556   // Check expected range.
557   CheckExpectedRanges("{ [0,7) }");
558   // Check buffers in range.
559   Seek(0);
560   CheckExpectedBuffers(0, 7);
561 }
562
563 TEST_F(SourceBufferStreamTest, Complete_Overlap) {
564   // Append 5 buffers at positions 5 through 9.
565   NewSegmentAppend(5, 5);
566
567   // Append 15 buffers at positions 0 through 14.
568   NewSegmentAppend(0, 15);
569
570   // Check expected range.
571   CheckExpectedRanges("{ [0,14) }");
572   // Check buffers in range.
573   Seek(0);
574   CheckExpectedBuffers(0, 14);
575 }
576
577 TEST_F(SourceBufferStreamTest,
578        Complete_Overlap_AfterSegmentTimestampAndBeforeFirstBufferTimestamp) {
579   // Append a segment with a start timestamp of 0, but the first
580   // buffer starts at 30ms. This can happen in muxed content where the
581   // audio starts before the first frame.
582   NewSegmentAppend(base::TimeDelta::FromMilliseconds(0), "30K 60K 90K 120K");
583
584   CheckExpectedRangesByTimestamp("{ [0,150) }");
585
586   // Completely overlap the old buffers, with a segment that starts
587   // after the old segment start timestamp, but before the timestamp
588   // of the first buffer in the segment.
589   NewSegmentAppend("20K 50K 80K 110K");
590
591   // Verify that the buffered ranges are updated properly and we don't crash.
592   CheckExpectedRangesByTimestamp("{ [20,150) }");
593
594   SeekToTimestamp(base::TimeDelta::FromMilliseconds(20));
595   CheckExpectedBuffers("20K 50K 80K 110K 120K");
596 }
597
598 TEST_F(SourceBufferStreamTest, Complete_Overlap_EdgeCase) {
599   // Make each frame a keyframe so that it's okay to overlap frames at any point
600   // (instead of needing to respect keyframe boundaries).
601   SetStreamInfo(30, 30);
602
603   // Append 6 buffers at positions 6 through 11.
604   NewSegmentAppend(6, 6);
605
606   // Append 8 buffers at positions 5 through 12.
607   NewSegmentAppend(5, 8);
608
609   // Check expected range.
610   CheckExpectedRanges("{ [5,12) }");
611   // Check buffers in range.
612   Seek(5);
613   CheckExpectedBuffers(5, 12);
614 }
615
616 TEST_F(SourceBufferStreamTest, Start_Overlap) {
617   // Append 10 buffers at positions 5 through 14.
618   NewSegmentAppend(5, 5);
619
620   // Append 6 buffers at positions 10 through 15.
621   NewSegmentAppend(10, 6);
622
623   // Check expected range.
624   CheckExpectedRanges("{ [5,15) }");
625   // Check buffers in range.
626   Seek(5);
627   CheckExpectedBuffers(5, 15);
628 }
629
630 TEST_F(SourceBufferStreamTest, End_Overlap) {
631   // Append 10 buffers at positions 10 through 19.
632   NewSegmentAppend(10, 10);
633
634   // Append 10 buffers at positions 5 through 14.
635   NewSegmentAppend(5, 10);
636
637   // Check expected range.
638   CheckExpectedRanges("{ [5,19) }");
639   // Check buffers in range.
640   Seek(5);
641   CheckExpectedBuffers(5, 19);
642 }
643
644 TEST_F(SourceBufferStreamTest, End_Overlap_Several) {
645   // Append 10 buffers at positions 10 through 19.
646   NewSegmentAppend(10, 10);
647
648   // Append 8 buffers at positions 5 through 12.
649   NewSegmentAppend(5, 8);
650
651   // Check expected ranges: stream should not have kept buffers 13 and 14
652   // because the keyframe on which they depended was overwritten.
653   CheckExpectedRanges("{ [5,12) [15,19) }");
654
655   // Check buffers in range.
656   Seek(5);
657   CheckExpectedBuffers(5, 12);
658   CheckNoNextBuffer();
659
660   Seek(19);
661   CheckExpectedBuffers(15, 19);
662 }
663
664 // Test an end overlap edge case where a single buffer overlaps the
665 // beginning of a range.
666 // old  : *0K*   30   60   90   120K  150
667 // new  : *0K*
668 // after: *0K*                 *120K* 150K
669 // track:
670 TEST_F(SourceBufferStreamTest, End_Overlap_SingleBuffer) {
671   // Seek to start of stream.
672   SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
673
674   NewSegmentAppend("0K 30 60 90 120K 150");
675   CheckExpectedRangesByTimestamp("{ [0,180) }");
676
677   NewSegmentAppend("0K");
678   CheckExpectedRangesByTimestamp("{ [0,30) [120,180) }");
679
680   CheckExpectedBuffers("0K");
681   CheckNoNextBuffer();
682 }
683
684 TEST_F(SourceBufferStreamTest, Complete_Overlap_Several) {
685   // Append 2 buffers at positions 5 through 6.
686   NewSegmentAppend(5, 2);
687
688   // Append 2 buffers at positions 10 through 11.
689   NewSegmentAppend(10, 2);
690
691   // Append 2 buffers at positions 15 through 16.
692   NewSegmentAppend(15, 2);
693
694   // Check expected ranges.
695   CheckExpectedRanges("{ [5,6) [10,11) [15,16) }");
696
697   // Append buffers at positions 0 through 19.
698   NewSegmentAppend(0, 20);
699
700   // Check expected range.
701   CheckExpectedRanges("{ [0,19) }");
702   // Check buffers in range.
703   Seek(0);
704   CheckExpectedBuffers(0, 19);
705 }
706
707 TEST_F(SourceBufferStreamTest, Complete_Overlap_Several_Then_Merge) {
708   // Append 2 buffers at positions 5 through 6.
709   NewSegmentAppend(5, 2);
710
711   // Append 2 buffers at positions 10 through 11.
712   NewSegmentAppend(10, 2);
713
714   // Append 2 buffers at positions 15 through 16.
715   NewSegmentAppend(15, 2);
716
717   // Append 2 buffers at positions 20 through 21.
718   NewSegmentAppend(20, 2);
719
720   // Append buffers at positions 0 through 19.
721   NewSegmentAppend(0, 20);
722
723   // Check expected ranges.
724   CheckExpectedRanges("{ [0,21) }");
725   // Check buffers in range.
726   Seek(0);
727   CheckExpectedBuffers(0, 21);
728 }
729
730 TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected) {
731   // Append 10 buffers at positions 5 through 14.
732   NewSegmentAppend(5, 10, &kDataA);
733
734   // Seek to buffer at position 5.
735   Seek(5);
736
737   // Replace old data with new data.
738   NewSegmentAppend(5, 10, &kDataB);
739
740   // Check ranges are correct.
741   CheckExpectedRanges("{ [5,14) }");
742
743   // Check that data has been replaced with new data.
744   CheckExpectedBuffers(5, 14, &kDataB);
745 }
746
747 // This test is testing that a client can append data to SourceBufferStream that
748 // overlaps the range from which the client is currently grabbing buffers. We
749 // would expect that the SourceBufferStream would return old data until it hits
750 // the keyframe of the new data, after which it will return the new data.
751 TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected_TrackBuffer) {
752   // Append 10 buffers at positions 5 through 14.
753   NewSegmentAppend(5, 10, &kDataA);
754
755   // Seek to buffer at position 5 and get next buffer.
756   Seek(5);
757   CheckExpectedBuffers(5, 5, &kDataA);
758
759   // Do a complete overlap by appending 20 buffers at positions 0 through 19.
760   NewSegmentAppend(0, 20, &kDataB);
761
762   // Check range is correct.
763   CheckExpectedRanges("{ [0,19) }");
764
765   // Expect old data up until next keyframe in new data.
766   CheckExpectedBuffers(6, 9, &kDataA);
767   CheckExpectedBuffers(10, 10, &kDataB, true);
768
769   // Expect rest of data to be new.
770   CheckExpectedBuffers(11, 19, &kDataB);
771
772   // Seek back to beginning; all data should be new.
773   Seek(0);
774   CheckExpectedBuffers(0, 19, &kDataB);
775
776   // Check range continues to be correct.
777   CheckExpectedRanges("{ [0,19) }");
778 }
779
780 TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected_EdgeCase) {
781   // Append 10 buffers at positions 5 through 14.
782   NewSegmentAppend(5, 10, &kDataA);
783
784   // Seek to buffer at position 5 and get next buffer.
785   Seek(5);
786   CheckExpectedBuffers(5, 5, &kDataA);
787
788   // Replace existing data with new data.
789   NewSegmentAppend(5, 10, &kDataB);
790
791   // Check ranges are correct.
792   CheckExpectedRanges("{ [5,14) }");
793
794   // Expect old data up until next keyframe in new data.
795   CheckExpectedBuffers(6, 9, &kDataA);
796   CheckExpectedBuffers(10, 10, &kDataB, true);
797
798   // Expect rest of data to be new.
799   CheckExpectedBuffers(11, 14, &kDataB);
800
801   // Seek back to beginning; all data should be new.
802   Seek(5);
803   CheckExpectedBuffers(5, 14, &kDataB);
804
805   // Check range continues to be correct.
806   CheckExpectedRanges("{ [5,14) }");
807 }
808
809 TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected_Multiple) {
810   static const uint8 kDataC = 0x55;
811   static const uint8 kDataD = 0x77;
812
813   // Append 5 buffers at positions 5 through 9.
814   NewSegmentAppend(5, 5, &kDataA);
815
816   // Seek to buffer at position 5 and get next buffer.
817   Seek(5);
818   CheckExpectedBuffers(5, 5, &kDataA);
819
820   // Replace existing data with new data.
821   NewSegmentAppend(5, 5, &kDataB);
822
823   // Then replace it again with different data.
824   NewSegmentAppend(5, 5, &kDataC);
825
826   // Now append 5 new buffers at positions 10 through 14.
827   NewSegmentAppend(10, 5, &kDataC);
828
829   // Now replace all the data entirely.
830   NewSegmentAppend(5, 10, &kDataD);
831
832   // Expect buffers 6 through 9 to be DataA, and the remaining
833   // buffers to be kDataD.
834   CheckExpectedBuffers(6, 9, &kDataA);
835   CheckExpectedBuffers(10, 14, &kDataD);
836
837   // At this point we cannot fulfill request.
838   CheckNoNextBuffer();
839
840   // Seek back to beginning; all data should be new.
841   Seek(5);
842   CheckExpectedBuffers(5, 14, &kDataD);
843 }
844
845 TEST_F(SourceBufferStreamTest, Start_Overlap_Selected) {
846   // Append 10 buffers at positions 0 through 9.
847   NewSegmentAppend(0, 10, &kDataA);
848
849   // Seek to position 5, then add buffers to overlap data at that position.
850   Seek(5);
851   NewSegmentAppend(5, 10, &kDataB);
852
853   // Check expected range.
854   CheckExpectedRanges("{ [0,14) }");
855
856   // Because we seeked to a keyframe, the next buffers should all be new data.
857   CheckExpectedBuffers(5, 14, &kDataB);
858
859   // Make sure all data is correct.
860   Seek(0);
861   CheckExpectedBuffers(0, 4, &kDataA);
862   CheckExpectedBuffers(5, 14, &kDataB);
863 }
864
865 TEST_F(SourceBufferStreamTest, Start_Overlap_Selected_TrackBuffer) {
866   // Append 15 buffers at positions 0 through 14.
867   NewSegmentAppend(0, 15, &kDataA);
868
869   // Seek to 10 and get buffer.
870   Seek(10);
871   CheckExpectedBuffers(10, 10, &kDataA);
872
873   // Now append 10 buffers of new data at positions 10 through 19.
874   NewSegmentAppend(10, 10, &kDataB);
875
876   // Check expected range.
877   CheckExpectedRanges("{ [0,19) }");
878
879   // The next 4 buffers should be a from the old buffer, followed by a keyframe
880   // from the new data.
881   CheckExpectedBuffers(11, 14, &kDataA);
882   CheckExpectedBuffers(15, 15, &kDataB, true);
883
884   // The rest of the buffers should be new data.
885   CheckExpectedBuffers(16, 19, &kDataB);
886
887   // Now seek to the beginning; positions 0 through 9 should be the original
888   // data, positions 10 through 19 should be the new data.
889   Seek(0);
890   CheckExpectedBuffers(0, 9, &kDataA);
891   CheckExpectedBuffers(10, 19, &kDataB);
892
893   // Make sure range is still correct.
894   CheckExpectedRanges("{ [0,19) }");
895 }
896
897 TEST_F(SourceBufferStreamTest, Start_Overlap_Selected_EdgeCase) {
898   // Append 10 buffers at positions 5 through 14.
899   NewSegmentAppend(5, 10, &kDataA);
900
901   Seek(10);
902   CheckExpectedBuffers(10, 10, &kDataA);
903
904   // Now replace the last 5 buffers with new data.
905   NewSegmentAppend(10, 5, &kDataB);
906
907   // The next 4 buffers should be the origial data, held in the track buffer.
908   CheckExpectedBuffers(11, 14, &kDataA);
909
910   // The next buffer is at position 15, so we should fail to fulfill the
911   // request.
912   CheckNoNextBuffer();
913
914   // Now append data at 15 through 19 and check to make sure it's correct.
915   NewSegmentAppend(15, 5, &kDataB);
916   CheckExpectedBuffers(15, 19, &kDataB);
917
918   // Seek to beginning of buffered range and check buffers.
919   Seek(5);
920   CheckExpectedBuffers(5, 9, &kDataA);
921   CheckExpectedBuffers(10, 19, &kDataB);
922
923   // Check expected range.
924   CheckExpectedRanges("{ [5,19) }");
925 }
926
927 // This test covers the case where new buffers end-overlap an existing, selected
928 // range, and the next buffer is a keyframe that's being overlapped by new
929 // buffers.
930 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
931 // old Â :           *A*a a a a A a a a a
932 // new Â :  B b b b b B b b b b
933 // after:  B b b b b*B*b b b b A a a a a
934 TEST_F(SourceBufferStreamTest, End_Overlap_Selected) {
935   // Append 10 buffers at positions 5 through 14.
936   NewSegmentAppend(5, 10, &kDataA);
937
938   // Seek to position 5.
939   Seek(5);
940
941   // Now append 10 buffers at positions 0 through 9.
942   NewSegmentAppend(0, 10, &kDataB);
943
944   // Check expected range.
945   CheckExpectedRanges("{ [0,14) }");
946
947   // Because we seeked to a keyframe, the next buffers should be new.
948   CheckExpectedBuffers(5, 9, &kDataB);
949
950   // Make sure all data is correct.
951   Seek(0);
952   CheckExpectedBuffers(0, 9, &kDataB);
953   CheckExpectedBuffers(10, 14, &kDataA);
954 }
955
956 // This test covers the case where new buffers end-overlap an existing, selected
957 // range, and the next buffer in the range is after the newly appended buffers.
958 // In this particular case, the end overlap does not require a split.
959 //
960 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
961 // old Â :           |A a a a a A a a*a*a|
962 // new Â :  B b b b b B b b b b
963 // after: |B b b b b B b b b b A a a*a*a|
964 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_AfterEndOfNew_1) {
965   // Append 10 buffers at positions 5 through 14.
966   NewSegmentAppend(5, 10, &kDataA);
967
968   // Seek to position 10, then move to position 13.
969   Seek(10);
970   CheckExpectedBuffers(10, 12, &kDataA);
971
972   // Now append 10 buffers at positions 0 through 9.
973   NewSegmentAppend(0, 10, &kDataB);
974
975   // Check expected range.
976   CheckExpectedRanges("{ [0,14) }");
977
978   // Make sure rest of data is as expected.
979   CheckExpectedBuffers(13, 14, &kDataA);
980
981   // Make sure all data is correct.
982   Seek(0);
983   CheckExpectedBuffers(0, 9, &kDataB);
984   CheckExpectedBuffers(10, 14, &kDataA);
985 }
986
987 // This test covers the case where new buffers end-overlap an existing, selected
988 // range, and the next buffer in the range is after the newly appended buffers.
989 // In this particular case, the end overlap requires a split, and the next
990 // buffer is in the split range.
991 //
992 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
993 // old Â :           |A a a a a A a a*a*a|
994 // new Â :  B b b b b B b b
995 // after: |B b b b b B b b|   |A a a*a*a|
996 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_AfterEndOfNew_2) {
997   // Append 10 buffers at positions 5 through 14.
998   NewSegmentAppend(5, 10, &kDataA);
999
1000   // Seek to position 10, then move to position 13.
1001   Seek(10);
1002   CheckExpectedBuffers(10, 12, &kDataA);
1003
1004   // Now append 8 buffers at positions 0 through 7.
1005   NewSegmentAppend(0, 8, &kDataB);
1006
1007   // Check expected ranges.
1008   CheckExpectedRanges("{ [0,7) [10,14) }");
1009
1010   // Make sure rest of data is as expected.
1011   CheckExpectedBuffers(13, 14, &kDataA);
1012
1013   // Make sure all data is correct.
1014   Seek(0);
1015   CheckExpectedBuffers(0, 7, &kDataB);
1016   CheckNoNextBuffer();
1017
1018   Seek(10);
1019   CheckExpectedBuffers(10, 14, &kDataA);
1020 }
1021
1022 // This test covers the case where new buffers end-overlap an existing, selected
1023 // range, and the next buffer in the range is after the newly appended buffers.
1024 // In this particular case, the end overlap requires a split, and the next
1025 // buffer was in between the end of the new data and the split range.
1026 //
1027 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1028 // old Â :           |A a a*a*a A a a a a|
1029 // new Â :  B b b b b B b b
1030 // after: |B b b b b B b b|   |A a a a a|
1031 // track:                 |a a|
1032 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_AfterEndOfNew_3) {
1033   // Append 10 buffers at positions 5 through 14.
1034   NewSegmentAppend(5, 10, &kDataA);
1035
1036   // Seek to position 5, then move to position 8.
1037   Seek(5);
1038   CheckExpectedBuffers(5, 7, &kDataA);
1039
1040   // Now append 8 buffers at positions 0 through 7.
1041   NewSegmentAppend(0, 8, &kDataB);
1042
1043   // Check expected ranges.
1044   CheckExpectedRanges("{ [0,7) [10,14) }");
1045
1046   // Check for data in the track buffer.
1047   CheckExpectedBuffers(8, 9, &kDataA);
1048   // The buffer immediately after the track buffer should be a keyframe.
1049   CheckExpectedBuffers(10, 10, &kDataA, true);
1050
1051   // Make sure all data is correct.
1052   Seek(0);
1053   CheckExpectedBuffers(0, 7, &kDataB);
1054   Seek(10);
1055   CheckExpectedBuffers(10, 14, &kDataA);
1056 }
1057
1058 // This test covers the case where new buffers end-overlap an existing, selected
1059 // range, and the next buffer in the range is overlapped by the new buffers.
1060 // In this particular case, the end overlap does not require a split.
1061 //
1062 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1063 // old Â :           |A a a*a*a A a a a a|
1064 // new Â :  B b b b b B b b b b
1065 // after: |B b b b b B b b b b A a a a a|
1066 // track:                 |a a|
1067 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_OverlappedByNew_1) {
1068   // Append 10 buffers at positions 5 through 14.
1069   NewSegmentAppend(5, 10, &kDataA);
1070
1071   // Seek to position 5, then move to position 8.
1072   Seek(5);
1073   CheckExpectedBuffers(5, 7, &kDataA);
1074
1075   // Now append 10 buffers at positions 0 through 9.
1076   NewSegmentAppend(0, 10, &kDataB);
1077
1078   // Check expected range.
1079   CheckExpectedRanges("{ [0,14) }");
1080
1081   // Check for data in the track buffer.
1082   CheckExpectedBuffers(8, 9, &kDataA);
1083   // The buffer immediately after the track buffer should be a keyframe.
1084   CheckExpectedBuffers(10, 10, &kDataA, true);
1085
1086   // Make sure all data is correct.
1087   Seek(0);
1088   CheckExpectedBuffers(0, 9, &kDataB);
1089   CheckExpectedBuffers(10, 14, &kDataA);
1090 }
1091
1092 // This test covers the case where new buffers end-overlap an existing, selected
1093 // range, and the next buffer in the range is overlapped by the new buffers.
1094 // In this particular case, the end overlap requires a split, and the next
1095 // keyframe after the track buffer is in the split range.
1096 //
1097 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1098 // old Â :           |A*a*a a a A a a a a|
1099 // new Â :  B b b b b B b
1100 // after: |B b b b b B b|     |A a a a a|
1101 // track:             |a a a a|
1102 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_OverlappedByNew_2) {
1103   // Append 10 buffers at positions 5 through 14.
1104   NewSegmentAppend(5, 10, &kDataA);
1105
1106   // Seek to position 5, then move to position 6.
1107   Seek(5);
1108   CheckExpectedBuffers(5, 5, &kDataA);
1109
1110   // Now append 7 buffers at positions 0 through 6.
1111   NewSegmentAppend(0, 7, &kDataB);
1112
1113   // Check expected ranges.
1114   CheckExpectedRanges("{ [0,6) [10,14) }");
1115
1116   // Check for data in the track buffer.
1117   CheckExpectedBuffers(6, 9, &kDataA);
1118   // The buffer immediately after the track buffer should be a keyframe.
1119   CheckExpectedBuffers(10, 10, &kDataA, true);
1120
1121   // Make sure all data is correct.
1122   Seek(0);
1123   CheckExpectedBuffers(0, 6, &kDataB);
1124   CheckNoNextBuffer();
1125
1126   Seek(10);
1127   CheckExpectedBuffers(10, 14, &kDataA);
1128 }
1129
1130 // This test covers the case where new buffers end-overlap an existing, selected
1131 // range, and the next buffer in the range is overlapped by the new buffers.
1132 // In this particular case, the end overlap requires a split, and the next
1133 // keyframe after the track buffer is in the range with the new buffers.
1134 //
1135 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1136 // old Â :           |A*a*a a a A a a a a A a a a a|
1137 // new Â :  B b b b b B b b b b B b b
1138 // after: |B b b b b B b b b b B b b|   |A a a a a|
1139 // track:             |a a a a|
1140 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_OverlappedByNew_3) {
1141   // Append 15 buffers at positions 5 through 19.
1142   NewSegmentAppend(5, 15, &kDataA);
1143
1144   // Seek to position 5, then move to position 6.
1145   Seek(5);
1146   CheckExpectedBuffers(5, 5, &kDataA);
1147
1148   // Now append 13 buffers at positions 0 through 12.
1149   NewSegmentAppend(0, 13, &kDataB);
1150
1151   // Check expected ranges.
1152   CheckExpectedRanges("{ [0,12) [15,19) }");
1153
1154   // Check for data in the track buffer.
1155   CheckExpectedBuffers(6, 9, &kDataA);
1156   // The buffer immediately after the track buffer should be a keyframe
1157   // from the new data.
1158   CheckExpectedBuffers(10, 10, &kDataB, true);
1159
1160   // Make sure all data is correct.
1161   Seek(0);
1162   CheckExpectedBuffers(0, 12, &kDataB);
1163   CheckNoNextBuffer();
1164
1165   Seek(15);
1166   CheckExpectedBuffers(15, 19, &kDataA);
1167 }
1168
1169 // This test covers the case where new buffers end-overlap an existing, selected
1170 // range, and there is no keyframe after the end of the new buffers.
1171 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1172 // old Â :           |A*a*a a a|
1173 // new Â :  B b b b b B
1174 // after: |B b b b b B|
1175 // track:             |a a a a|
1176 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_NoKeyframeAfterNew) {
1177   // Append 5 buffers at positions 5 through 9.
1178   NewSegmentAppend(5, 5, &kDataA);
1179
1180   // Seek to position 5, then move to position 6.
1181   Seek(5);
1182   CheckExpectedBuffers(5, 5, &kDataA);
1183
1184   // Now append 6 buffers at positions 0 through 5.
1185   NewSegmentAppend(0, 6, &kDataB);
1186
1187   // Check expected range.
1188   CheckExpectedRanges("{ [0,5) }");
1189
1190   // Check for data in the track buffer.
1191   CheckExpectedBuffers(6, 9, &kDataA);
1192
1193   // Now there's no data to fulfill the request.
1194   CheckNoNextBuffer();
1195
1196   // Let's fill in the gap, buffers 6 through 10.
1197   AppendBuffers(6, 5, &kDataB);
1198
1199   // We should be able to get the next buffer.
1200   CheckExpectedBuffers(10, 10, &kDataB);
1201 }
1202
1203 // This test covers the case where new buffers end-overlap an existing, selected
1204 // range, and there is no keyframe after the end of the new buffers, then the
1205 // range gets split.
1206 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1207 // old Â :                     |A a a a a A*a*|
1208 // new Â :            B b b b b B b b b b B
1209 // after:           |B b b b b B b b b b B|
1210 // new Â :  A a a a a A
1211 // after: |A a a a a A|       |B b b b b B|
1212 // track:                                 |a|
1213 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_NoKeyframeAfterNew2) {
1214   // Append 7 buffers at positions 10 through 16.
1215   NewSegmentAppend(10, 7, &kDataA);
1216
1217   // Seek to position 15, then move to position 16.
1218   Seek(15);
1219   CheckExpectedBuffers(15, 15, &kDataA);
1220
1221   // Now append 11 buffers at positions 5 through 15.
1222   NewSegmentAppend(5, 11, &kDataB);
1223   CheckExpectedRanges("{ [5,15) }");
1224
1225   // Now do another end-overlap to split the range into two parts, where the
1226   // 2nd range should have the next buffer position.
1227   NewSegmentAppend(0, 6, &kDataA);
1228   CheckExpectedRanges("{ [0,5) [10,15) }");
1229
1230   // Check for data in the track buffer.
1231   CheckExpectedBuffers(16, 16, &kDataA);
1232
1233   // Now there's no data to fulfill the request.
1234   CheckNoNextBuffer();
1235
1236   // Add data to the 2nd range, should not be able to fulfill the next read
1237   // until we've added a keyframe.
1238   NewSegmentAppend(15, 1, &kDataB);
1239   CheckNoNextBuffer();
1240   for (int i = 16; i <= 19; i++) {
1241     AppendBuffers(i, 1, &kDataB);
1242     CheckNoNextBuffer();
1243   }
1244
1245   // Now append a keyframe.
1246   AppendBuffers(20, 1, &kDataB);
1247
1248   // We should be able to get the next buffer.
1249   CheckExpectedBuffers(20, 20, &kDataB, true);
1250 }
1251
1252 // This test covers the case where new buffers end-overlap an existing, selected
1253 // range, and the next keyframe in a separate range.
1254 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1255 // old Â :           |A*a*a a a|          |A a a a a|
1256 // new Â :  B b b b b B
1257 // after: |B b b b b B|                  |A a a a a|
1258 // track:             |a a a a|
1259 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_NoKeyframeAfterNew3) {
1260   // Append 5 buffers at positions 5 through 9.
1261   NewSegmentAppend(5, 5, &kDataA);
1262
1263   // Append 5 buffers at positions 15 through 19.
1264   NewSegmentAppend(15, 5, &kDataA);
1265
1266   // Check expected range.
1267   CheckExpectedRanges("{ [5,9) [15,19) }");
1268
1269   // Seek to position 5, then move to position 6.
1270   Seek(5);
1271   CheckExpectedBuffers(5, 5, &kDataA);
1272
1273   // Now append 6 buffers at positions 0 through 5.
1274   NewSegmentAppend(0, 6, &kDataB);
1275
1276   // Check expected range.
1277   CheckExpectedRanges("{ [0,5) [15,19) }");
1278
1279   // Check for data in the track buffer.
1280   CheckExpectedBuffers(6, 9, &kDataA);
1281
1282   // Now there's no data to fulfill the request.
1283   CheckNoNextBuffer();
1284
1285   // Let's fill in the gap, buffers 6 through 14.
1286   AppendBuffers(6, 9, &kDataB);
1287
1288   // Check expected range.
1289   CheckExpectedRanges("{ [0,19) }");
1290
1291   // We should be able to get the next buffer.
1292   CheckExpectedBuffers(10, 14, &kDataB);
1293
1294   // We should be able to get the next buffer.
1295   CheckExpectedBuffers(15, 19, &kDataA);
1296 }
1297
1298 // This test covers the case when new buffers overlap the middle of a selected
1299 // range. This tests the case when there is no split and the next buffer is a
1300 // keyframe.
1301 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1302 // old Â :  A a a a a*A*a a a a A a a a a
1303 // new Â :            B b b b b
1304 // after:  A a a a a*B*b b b b A a a a a
1305 TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_1) {
1306   // Append 15 buffers at positions 0 through 14.
1307   NewSegmentAppend(0, 15, &kDataA);
1308
1309   // Seek to position 5.
1310   Seek(5);
1311
1312   // Now append 5 buffers at positions 5 through 9.
1313   NewSegmentAppend(5, 5, &kDataB);
1314
1315   // Check expected range.
1316   CheckExpectedRanges("{ [0,14) }");
1317
1318   // Check for next data; should be new data.
1319   CheckExpectedBuffers(5, 9, &kDataB);
1320
1321   // Make sure all data is correct.
1322   Seek(0);
1323   CheckExpectedBuffers(0, 4, &kDataA);
1324   CheckExpectedBuffers(5, 9, &kDataB);
1325   CheckExpectedBuffers(10, 14, &kDataA);
1326 }
1327
1328 // This test covers the case when new buffers overlap the middle of a selected
1329 // range. This tests the case when there is no split and the next buffer is
1330 // after the new buffers.
1331 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1332 // old Â :  A a a a a A a a a a A*a*a a a
1333 // new Â :            B b b b b
1334 // after:  A a a a a B b b b b A*a*a a a
1335 TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_2) {
1336   // Append 15 buffers at positions 0 through 14.
1337   NewSegmentAppend(0, 15, &kDataA);
1338
1339   // Seek to 10 then move to position 11.
1340   Seek(10);
1341   CheckExpectedBuffers(10, 10, &kDataA);
1342
1343   // Now append 5 buffers at positions 5 through 9.
1344   NewSegmentAppend(5, 5, &kDataB);
1345
1346   // Check expected range.
1347   CheckExpectedRanges("{ [0,14) }");
1348
1349   // Make sure data is correct.
1350   CheckExpectedBuffers(11, 14, &kDataA);
1351   Seek(0);
1352   CheckExpectedBuffers(0, 4, &kDataA);
1353   CheckExpectedBuffers(5, 9, &kDataB);
1354   CheckExpectedBuffers(10, 14, &kDataA);
1355 }
1356
1357 // This test covers the case when new buffers overlap the middle of a selected
1358 // range. This tests the case when there is a split and the next buffer is
1359 // before the new buffers.
1360 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1361 // old Â :  A a*a*a a A a a a a A a a a a
1362 // new Â :            B b b
1363 // after:  A a*a*a a B b b|   |A a a a a
1364 TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_3) {
1365   // Append 15 buffers at positions 0 through 14.
1366   NewSegmentAppend(0, 15, &kDataA);
1367
1368   // Seek to beginning then move to position 2.
1369   Seek(0);
1370   CheckExpectedBuffers(0, 1, &kDataA);
1371
1372   // Now append 3 buffers at positions 5 through 7.
1373   NewSegmentAppend(5, 3, &kDataB);
1374
1375   // Check expected range.
1376   CheckExpectedRanges("{ [0,7) [10,14) }");
1377
1378   // Make sure data is correct.
1379   CheckExpectedBuffers(2, 4, &kDataA);
1380   CheckExpectedBuffers(5, 7, &kDataB);
1381   Seek(10);
1382   CheckExpectedBuffers(10, 14, &kDataA);
1383 }
1384
1385 // This test covers the case when new buffers overlap the middle of a selected
1386 // range. This tests the case when there is a split and the next buffer is after
1387 // the new buffers but before the split range.
1388 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1389 // old Â :  A a a a a A a a*a*a A a a a a
1390 // new Â :            B b b
1391 // after: |A a a a a B b b|   |A a a a a|
1392 // track:                 |a a|
1393 TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_4) {
1394   // Append 15 buffers at positions 0 through 14.
1395   NewSegmentAppend(0, 15, &kDataA);
1396
1397   // Seek to 5 then move to position 8.
1398   Seek(5);
1399   CheckExpectedBuffers(5, 7, &kDataA);
1400
1401   // Now append 3 buffers at positions 5 through 7.
1402   NewSegmentAppend(5, 3, &kDataB);
1403
1404   // Check expected range.
1405   CheckExpectedRanges("{ [0,7) [10,14) }");
1406
1407   // Buffers 8 and 9 should be in the track buffer.
1408   CheckExpectedBuffers(8, 9, &kDataA);
1409   // The buffer immediately after the track buffer should be a keyframe.
1410   CheckExpectedBuffers(10, 10, &kDataA, true);
1411
1412   // Make sure all data is correct.
1413   Seek(0);
1414   CheckExpectedBuffers(0, 4, &kDataA);
1415   CheckExpectedBuffers(5, 7, &kDataB);
1416   Seek(10);
1417   CheckExpectedBuffers(10, 14, &kDataA);
1418 }
1419
1420 TEST_F(SourceBufferStreamTest, Overlap_OneByOne) {
1421   // Append 5 buffers starting at 10ms, 30ms apart.
1422   NewSegmentAppendOneByOne("10K 40 70 100 130");
1423
1424   // The range ends at 160, accounting for the last buffer's duration.
1425   CheckExpectedRangesByTimestamp("{ [10,160) }");
1426
1427   // Overlap with 10 buffers starting at the beginning, appended one at a
1428   // time.
1429   NewSegmentAppend(0, 1, &kDataB);
1430   for (int i = 1; i < 10; i++)
1431     AppendBuffers(i, 1, &kDataB);
1432
1433   // All data should be replaced.
1434   Seek(0);
1435   CheckExpectedRanges("{ [0,9) }");
1436   CheckExpectedBuffers(0, 9, &kDataB);
1437 }
1438
1439 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_DeleteGroup) {
1440   NewSegmentAppendOneByOne("10K 40 70 100 130K");
1441   CheckExpectedRangesByTimestamp("{ [10,160) }");
1442
1443   // Seek to 130ms.
1444   SeekToTimestamp(base::TimeDelta::FromMilliseconds(130));
1445
1446   // Overlap with a new segment from 0 to 120ms.
1447   NewSegmentAppendOneByOne("0K 120");
1448
1449   // Next buffer should still be 130ms.
1450   CheckExpectedBuffers("130K");
1451
1452   // Check the final buffers is correct.
1453   SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
1454   CheckExpectedBuffers("0K 120 130K");
1455 }
1456
1457 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_BetweenMediaSegments) {
1458   // Append 5 buffers starting at 110ms, 30ms apart.
1459   NewSegmentAppendOneByOne("110K 140 170 200 230");
1460   CheckExpectedRangesByTimestamp("{ [110,260) }");
1461
1462   // Now append 2 media segments from 0ms to 210ms, 30ms apart. Note that the
1463   // old keyframe 110ms falls in between these two segments.
1464   NewSegmentAppendOneByOne("0K 30 60 90");
1465   NewSegmentAppendOneByOne("120K 150 180 210");
1466   CheckExpectedRangesByTimestamp("{ [0,240) }");
1467
1468   // Check the final buffers is correct; the keyframe at 110ms should be
1469   // deleted.
1470   SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
1471   CheckExpectedBuffers("0K 30 60 90 120K 150 180 210");
1472 }
1473
1474 // old  :   10K  40  *70*  100K  125  130K
1475 // new  : 0K   30   60   90   120K
1476 // after: 0K   30   60   90  *120K*   130K
1477 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer) {
1478   NewSegmentAppendOneByOne("10K 40 70 100K 125 130K");
1479   CheckExpectedRangesByTimestamp("{ [10,160) }");
1480
1481   // Seek to 70ms.
1482   SeekToTimestamp(base::TimeDelta::FromMilliseconds(70));
1483   CheckExpectedBuffers("10K 40");
1484
1485   // Overlap with a new segment from 0 to 120ms.
1486   NewSegmentAppendOneByOne("0K 30 60 90 120K");
1487   CheckExpectedRangesByTimestamp("{ [0,160) }");
1488
1489   // Should return frames 70ms and 100ms from the track buffer, then switch
1490   // to the new data at 120K, then switch back to the old data at 130K. The
1491   // frame at 125ms that depended on keyframe 100ms should have been deleted.
1492   CheckExpectedBuffers("70 100K 120K 130K");
1493
1494   // Check the final result: should not include data from the track buffer.
1495   SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
1496   CheckExpectedBuffers("0K 30 60 90 120K 130K");
1497 }
1498
1499 // Overlap the next keyframe after the end of the track buffer with a new
1500 // keyframe.
1501 // old  :   10K  40  *70*  100K  125  130K
1502 // new  : 0K   30   60   90   120K
1503 // after: 0K   30   60   90  *120K*   130K
1504 // track:             70   100K
1505 // new  :                     110K    130
1506 // after: 0K   30   60   90  *110K*   130
1507 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer2) {
1508   NewSegmentAppendOneByOne("10K 40 70 100K 125 130K");
1509   CheckExpectedRangesByTimestamp("{ [10,160) }");
1510
1511   // Seek to 70ms.
1512   SeekToTimestamp(base::TimeDelta::FromMilliseconds(70));
1513   CheckExpectedBuffers("10K 40");
1514
1515   // Overlap with a new segment from 0 to 120ms; 70ms and 100ms go in track
1516   // buffer.
1517   NewSegmentAppendOneByOne("0K 30 60 90 120K");
1518   CheckExpectedRangesByTimestamp("{ [0,160) }");
1519
1520   // Now overlap the keyframe at 120ms.
1521   NewSegmentAppendOneByOne("110K 130");
1522
1523   // Should expect buffers 70ms and 100ms from the track buffer. Then it should
1524   // return the keyframe after the track buffer, which is at 110ms.
1525   CheckExpectedBuffers("70 100K 110K 130");
1526 }
1527
1528 // Overlap the next keyframe after the end of the track buffer without a
1529 // new keyframe.
1530 // old  :   10K  40  *70*  100K  125  130K
1531 // new  : 0K   30   60   90   120K
1532 // after: 0K   30   60   90  *120K*   130K
1533 // track:             70   100K
1534 // new  :        50K   80   110          140
1535 // after: 0K   30   50K   80   110   140 * (waiting for keyframe)
1536 // track:               70   100K   120K   130K
1537 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer3) {
1538   NewSegmentAppendOneByOne("10K 40 70 100K 125 130K");
1539   CheckExpectedRangesByTimestamp("{ [10,160) }");
1540
1541   // Seek to 70ms.
1542   SeekToTimestamp(base::TimeDelta::FromMilliseconds(70));
1543   CheckExpectedBuffers("10K 40");
1544
1545   // Overlap with a new segment from 0 to 120ms; 70ms and 100ms go in track
1546   // buffer.
1547   NewSegmentAppendOneByOne("0K 30 60 90 120K");
1548   CheckExpectedRangesByTimestamp("{ [0,160) }");
1549
1550   // Now overlap the keyframe at 120ms. There's no keyframe after 70ms, so 120ms
1551   // and 130ms go into the track buffer.
1552   NewSegmentAppendOneByOne("50K 80 110 140");
1553
1554   // Should have all the buffers from the track buffer, then stall.
1555   CheckExpectedBuffers("70 100K 120K 130K");
1556   CheckNoNextBuffer();
1557
1558   // Appending a keyframe should fulfill the read.
1559   AppendBuffersOneByOne("150K");
1560   CheckExpectedBuffers("150K");
1561   CheckNoNextBuffer();
1562 }
1563
1564 // Overlap the next keyframe after the end of the track buffer with a keyframe
1565 // that comes before the end of the track buffer.
1566 // old  :   10K  40  *70*  100K  125  130K
1567 // new  : 0K   30   60   90   120K
1568 // after: 0K   30   60   90  *120K*   130K
1569 // track:             70   100K
1570 // new  :              80K  110          140
1571 // after: 0K   30   60   *80K*  110   140
1572 // track:               70
1573 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer4) {
1574   NewSegmentAppendOneByOne("10K 40 70 100K 125 130K");
1575   CheckExpectedRangesByTimestamp("{ [10,160) }");
1576
1577   // Seek to 70ms.
1578   SeekToTimestamp(base::TimeDelta::FromMilliseconds(70));
1579   CheckExpectedBuffers("10K 40");
1580
1581   // Overlap with a new segment from 0 to 120ms; 70ms and 100ms go in track
1582   // buffer.
1583   NewSegmentAppendOneByOne("0K 30 60 90 120K");
1584   CheckExpectedRangesByTimestamp("{ [0,160) }");
1585
1586   // Now append a keyframe at 80ms.
1587   NewSegmentAppendOneByOne("80K 110 140");
1588
1589   CheckExpectedBuffers("70 80K 110 140");
1590   CheckNoNextBuffer();
1591 }
1592
1593 // Overlap the next keyframe after the end of the track buffer with a keyframe
1594 // that comes before the end of the track buffer, when the selected stream was
1595 // waiting for the next keyframe.
1596 // old  :   10K  40  *70*  100K
1597 // new  : 0K   30   60   90   120
1598 // after: 0K   30   60   90   120 * (waiting for keyframe)
1599 // track:             70   100K
1600 // new  :              80K  110          140
1601 // after: 0K   30   60   *80K*  110   140
1602 // track:               70
1603 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer5) {
1604   NewSegmentAppendOneByOne("10K 40 70 100K");
1605   CheckExpectedRangesByTimestamp("{ [10,130) }");
1606
1607   // Seek to 70ms.
1608   SeekToTimestamp(base::TimeDelta::FromMilliseconds(70));
1609   CheckExpectedBuffers("10K 40");
1610
1611   // Overlap with a new segment from 0 to 120ms; 70ms and 100ms go in track
1612   // buffer.
1613   NewSegmentAppendOneByOne("0K 30 60 90 120");
1614   CheckExpectedRangesByTimestamp("{ [0,150) }");
1615
1616   // Now append a keyframe at 80ms. The buffer at 100ms should be deleted from
1617   // the track buffer.
1618   NewSegmentAppendOneByOne("80K 110 140");
1619
1620   CheckExpectedBuffers("70 80K 110 140");
1621   CheckNoNextBuffer();
1622 }
1623
1624 // Test that appending to a different range while there is data in
1625 // the track buffer doesn't affect the selected range or track buffer state.
1626 // old  :   10K  40  *70*  100K  125  130K ... 200K 230
1627 // new  : 0K   30   60   90   120K
1628 // after: 0K   30   60   90  *120K*   130K ... 200K 230
1629 // track:             70   100K
1630 // old  : 0K   30   60   90  *120K*   130K ... 200K 230
1631 // new  :                                               260K 290
1632 // after: 0K   30   60   90  *120K*   130K ... 200K 230 260K 290
1633 // track:             70   100K
1634 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer6) {
1635   NewSegmentAppendOneByOne("10K 40 70 100K 125 130K");
1636   NewSegmentAppendOneByOne("200K 230");
1637   CheckExpectedRangesByTimestamp("{ [10,160) [200,260) }");
1638
1639   // Seek to 70ms.
1640   SeekToTimestamp(base::TimeDelta::FromMilliseconds(70));
1641   CheckExpectedBuffers("10K 40");
1642
1643   // Overlap with a new segment from 0 to 120ms.
1644   NewSegmentAppendOneByOne("0K 30 60 90 120K");
1645   CheckExpectedRangesByTimestamp("{ [0,160) [200,260) }");
1646
1647   // Verify that 70 gets read out of the track buffer.
1648   CheckExpectedBuffers("70");
1649
1650   // Append more data to the unselected range.
1651   NewSegmentAppendOneByOne("260K 290");
1652   CheckExpectedRangesByTimestamp("{ [0,160) [200,320) }");
1653
1654   CheckExpectedBuffers("100K 120K 130K");
1655   CheckNoNextBuffer();
1656
1657   // Check the final result: should not include data from the track buffer.
1658   SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
1659   CheckExpectedBuffers("0K 30 60 90 120K 130K");
1660   CheckNoNextBuffer();
1661 }
1662
1663 TEST_F(SourceBufferStreamTest, Seek_Keyframe) {
1664   // Append 6 buffers at positions 0 through 5.
1665   NewSegmentAppend(0, 6);
1666
1667   // Seek to beginning.
1668   Seek(0);
1669   CheckExpectedBuffers(0, 5, true);
1670 }
1671
1672 TEST_F(SourceBufferStreamTest, Seek_NonKeyframe) {
1673   // Append 15 buffers at positions 0 through 14.
1674   NewSegmentAppend(0, 15);
1675
1676   // Seek to buffer at position 13.
1677   Seek(13);
1678
1679   // Expect seeking back to the nearest keyframe.
1680   CheckExpectedBuffers(10, 14, true);
1681
1682   // Seek to buffer at position 3.
1683   Seek(3);
1684
1685   // Expect seeking back to the nearest keyframe.
1686   CheckExpectedBuffers(0, 3, true);
1687 }
1688
1689 TEST_F(SourceBufferStreamTest, Seek_NotBuffered) {
1690   // Seek to beginning.
1691   Seek(0);
1692
1693   // Try to get buffer; nothing's appended.
1694   CheckNoNextBuffer();
1695
1696   // Append 2 buffers at positions 0.
1697   NewSegmentAppend(0, 2);
1698   Seek(0);
1699   CheckExpectedBuffers(0, 1);
1700
1701   // Try to get buffer out of range.
1702   Seek(2);
1703   CheckNoNextBuffer();
1704 }
1705
1706 TEST_F(SourceBufferStreamTest, Seek_InBetweenTimestamps) {
1707   // Append 10 buffers at positions 0 through 9.
1708   NewSegmentAppend(0, 10);
1709
1710   base::TimeDelta bump = frame_duration() / 4;
1711   CHECK(bump > base::TimeDelta());
1712
1713   // Seek to buffer a little after position 5.
1714   stream_->Seek(5 * frame_duration() + bump);
1715   CheckExpectedBuffers(5, 5, true);
1716
1717   // Seek to buffer a little before position 5.
1718   stream_->Seek(5 * frame_duration() - bump);
1719   CheckExpectedBuffers(0, 0, true);
1720 }
1721
1722 // This test will do a complete overlap of an existing range in order to add
1723 // buffers to the track buffers. Then the test does a seek to another part of
1724 // the stream. The SourceBufferStream should clear its internal track buffer in
1725 // response to the Seek().
1726 TEST_F(SourceBufferStreamTest, Seek_After_TrackBuffer_Filled) {
1727   // Append 10 buffers at positions 5 through 14.
1728   NewSegmentAppend(5, 10, &kDataA);
1729
1730   // Seek to buffer at position 5 and get next buffer.
1731   Seek(5);
1732   CheckExpectedBuffers(5, 5, &kDataA);
1733
1734   // Do a complete overlap by appending 20 buffers at positions 0 through 19.
1735   NewSegmentAppend(0, 20, &kDataB);
1736
1737   // Check range is correct.
1738   CheckExpectedRanges("{ [0,19) }");
1739
1740   // Seek to beginning; all data should be new.
1741   Seek(0);
1742   CheckExpectedBuffers(0, 19, &kDataB);
1743
1744   // Check range continues to be correct.
1745   CheckExpectedRanges("{ [0,19) }");
1746 }
1747
1748 TEST_F(SourceBufferStreamTest, Seek_StartOfSegment) {
1749   base::TimeDelta bump = frame_duration() / 4;
1750   CHECK(bump > base::TimeDelta());
1751
1752   // Append 5 buffers at position (5 + |bump|) through 9, where the media
1753   // segment begins at position 5.
1754   Seek(5);
1755   NewSegmentAppend_OffsetFirstBuffer(5, 5, bump);
1756   scoped_refptr<StreamParserBuffer> buffer;
1757
1758   // GetNextBuffer() should return the next buffer at position (5 + |bump|).
1759   EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kSuccess);
1760   EXPECT_EQ(buffer->GetDecodeTimestamp(), 5 * frame_duration() + bump);
1761
1762   // Check rest of buffers.
1763   CheckExpectedBuffers(6, 9);
1764
1765   // Seek to position 15.
1766   Seek(15);
1767
1768   // Append 5 buffers at positions (15 + |bump|) through 19, where the media
1769   // segment begins at 15.
1770   NewSegmentAppend_OffsetFirstBuffer(15, 5, bump);
1771
1772   // GetNextBuffer() should return the next buffer at position (15 + |bump|).
1773   EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kSuccess);
1774   EXPECT_EQ(buffer->GetDecodeTimestamp(), 15 * frame_duration() + bump);
1775
1776   // Check rest of buffers.
1777   CheckExpectedBuffers(16, 19);
1778 }
1779
1780 TEST_F(SourceBufferStreamTest, Seek_BeforeStartOfSegment) {
1781   // Append 10 buffers at positions 5 through 14.
1782   NewSegmentAppend(5, 10);
1783
1784   // Seek to a time before the first buffer in the range.
1785   Seek(0);
1786
1787   // Should return buffers from the beginning of the range.
1788   CheckExpectedBuffers(5, 14);
1789 }
1790
1791 TEST_F(SourceBufferStreamTest, OldSeekPoint_CompleteOverlap) {
1792   // Append 5 buffers at positions 0 through 4.
1793   NewSegmentAppend(0, 4);
1794
1795   // Append 5 buffers at positions 10 through 14, and seek to the beginning of
1796   // this range.
1797   NewSegmentAppend(10, 5);
1798   Seek(10);
1799
1800   // Now seek to the beginning of the first range.
1801   Seek(0);
1802
1803   // Completely overlap the old seek point.
1804   NewSegmentAppend(5, 15);
1805
1806   // The GetNextBuffer() call should respect the 2nd seek point.
1807   CheckExpectedBuffers(0, 0);
1808 }
1809
1810 TEST_F(SourceBufferStreamTest, OldSeekPoint_CompleteOverlap_Pending) {
1811   // Append 2 buffers at positions 0 through 1.
1812   NewSegmentAppend(0, 2);
1813
1814   // Append 5 buffers at positions 15 through 19 and seek to beginning of the
1815   // range.
1816   NewSegmentAppend(15, 5);
1817   Seek(15);
1818
1819   // Now seek position 5.
1820   Seek(5);
1821
1822   // Completely overlap the old seek point.
1823   NewSegmentAppend(10, 15);
1824
1825   // The seek at position 5 should still be pending.
1826   CheckNoNextBuffer();
1827 }
1828
1829 TEST_F(SourceBufferStreamTest, OldSeekPoint_MiddleOverlap) {
1830   // Append 2 buffers at positions 0 through 1.
1831   NewSegmentAppend(0, 2);
1832
1833   // Append 15 buffers at positions 5 through 19 and seek to position 15.
1834   NewSegmentAppend(5, 15);
1835   Seek(15);
1836
1837   // Now seek to the beginning of the stream.
1838   Seek(0);
1839
1840   // Overlap the middle of the range such that there are now three ranges.
1841   NewSegmentAppend(10, 3);
1842   CheckExpectedRanges("{ [0,1) [5,12) [15,19) }");
1843
1844   // The GetNextBuffer() call should respect the 2nd seek point.
1845   CheckExpectedBuffers(0, 0);
1846 }
1847
1848 TEST_F(SourceBufferStreamTest, OldSeekPoint_MiddleOverlap_Pending) {
1849   // Append 2 buffers at positions 0 through 1.
1850   NewSegmentAppend(0, 2);
1851
1852   // Append 15 buffers at positions 10 through 24 and seek to position 20.
1853   NewSegmentAppend(10, 15);
1854   Seek(20);
1855
1856   // Now seek to position 5.
1857   Seek(5);
1858
1859   // Overlap the middle of the range such that it is now split into two ranges.
1860   NewSegmentAppend(15, 3);
1861   CheckExpectedRanges("{ [0,1) [10,17) [20,24) }");
1862
1863   // The seek at position 5 should still be pending.
1864   CheckNoNextBuffer();
1865 }
1866
1867 TEST_F(SourceBufferStreamTest, OldSeekPoint_StartOverlap) {
1868   // Append 2 buffers at positions 0 through 1.
1869   NewSegmentAppend(0, 2);
1870
1871   // Append 15 buffers at positions 5 through 19 and seek to position 15.
1872   NewSegmentAppend(5, 15);
1873   Seek(15);
1874
1875   // Now seek to the beginning of the stream.
1876   Seek(0);
1877
1878   // Start overlap the old seek point.
1879   NewSegmentAppend(10, 10);
1880
1881   // The GetNextBuffer() call should respect the 2nd seek point.
1882   CheckExpectedBuffers(0, 0);
1883 }
1884
1885 TEST_F(SourceBufferStreamTest, OldSeekPoint_StartOverlap_Pending) {
1886   // Append 2 buffers at positions 0 through 1.
1887   NewSegmentAppend(0, 2);
1888
1889   // Append 15 buffers at positions 10 through 24 and seek to position 20.
1890   NewSegmentAppend(10, 15);
1891   Seek(20);
1892
1893   // Now seek to position 5.
1894   Seek(5);
1895
1896   // Start overlap the old seek point.
1897   NewSegmentAppend(15, 10);
1898
1899   // The seek at time 0 should still be pending.
1900   CheckNoNextBuffer();
1901 }
1902
1903 TEST_F(SourceBufferStreamTest, OldSeekPoint_EndOverlap) {
1904   // Append 5 buffers at positions 0 through 4.
1905   NewSegmentAppend(0, 4);
1906
1907   // Append 15 buffers at positions 10 through 24 and seek to start of range.
1908   NewSegmentAppend(10, 15);
1909   Seek(10);
1910
1911   // Now seek to the beginning of the stream.
1912   Seek(0);
1913
1914   // End overlap the old seek point.
1915   NewSegmentAppend(5, 10);
1916
1917   // The GetNextBuffer() call should respect the 2nd seek point.
1918   CheckExpectedBuffers(0, 0);
1919 }
1920
1921 TEST_F(SourceBufferStreamTest, OldSeekPoint_EndOverlap_Pending) {
1922   // Append 2 buffers at positions 0 through 1.
1923   NewSegmentAppend(0, 2);
1924
1925   // Append 15 buffers at positions 15 through 29 and seek to start of range.
1926   NewSegmentAppend(15, 15);
1927   Seek(15);
1928
1929   // Now seek to position 5
1930   Seek(5);
1931
1932   // End overlap the old seek point.
1933   NewSegmentAppend(10, 10);
1934
1935   // The seek at time 0 should still be pending.
1936   CheckNoNextBuffer();
1937 }
1938
1939 TEST_F(SourceBufferStreamTest, GetNextBuffer_AfterMerges) {
1940   // Append 5 buffers at positions 10 through 14.
1941   NewSegmentAppend(10, 5);
1942
1943   // Seek to buffer at position 12.
1944   Seek(12);
1945
1946   // Append 5 buffers at positions 5 through 9.
1947   NewSegmentAppend(5, 5);
1948
1949   // Make sure ranges are merged.
1950   CheckExpectedRanges("{ [5,14) }");
1951
1952   // Make sure the next buffer is correct.
1953   CheckExpectedBuffers(10, 10);
1954
1955   // Append 5 buffers at positions 15 through 19.
1956   NewSegmentAppend(15, 5);
1957   CheckExpectedRanges("{ [5,19) }");
1958
1959   // Make sure the remaining next buffers are correct.
1960   CheckExpectedBuffers(11, 14);
1961 }
1962
1963 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenAppend) {
1964   // Append 4 buffers at positions 0 through 3.
1965   NewSegmentAppend(0, 4);
1966
1967   // Seek to buffer at position 0 and get all buffers.
1968   Seek(0);
1969   CheckExpectedBuffers(0, 3);
1970
1971   // Next buffer is at position 4, so should not be able to fulfill request.
1972   CheckNoNextBuffer();
1973
1974   // Append 2 buffers at positions 4 through 5.
1975   AppendBuffers(4, 2);
1976   CheckExpectedBuffers(4, 5);
1977 }
1978
1979 // This test covers the case where new buffers start-overlap a range whose next
1980 // buffer is not buffered.
1981 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenStartOverlap) {
1982   // Append 10 buffers at positions 0 through 9 and exhaust the buffers.
1983   NewSegmentAppend(0, 10, &kDataA);
1984   Seek(0);
1985   CheckExpectedBuffers(0, 9, &kDataA);
1986
1987   // Next buffer is at position 10, so should not be able to fulfill request.
1988   CheckNoNextBuffer();
1989
1990   // Append 6 buffers at positons 5 through 10. This is to test that doing a
1991   // start-overlap successfully fulfills the read at position 10, even though
1992   // position 10 was unbuffered.
1993   NewSegmentAppend(5, 6, &kDataB);
1994   CheckExpectedBuffers(10, 10, &kDataB);
1995
1996   // Then add 5 buffers from positions 11 though 15.
1997   AppendBuffers(11, 5, &kDataB);
1998
1999   // Check the next 4 buffers are correct, which also effectively seeks to
2000   // position 15.
2001   CheckExpectedBuffers(11, 14, &kDataB);
2002
2003   // Replace the next buffer at position 15 with another start overlap.
2004   NewSegmentAppend(15, 2, &kDataA);
2005   CheckExpectedBuffers(15, 16, &kDataA);
2006 }
2007
2008 // Tests a start overlap that occurs right at the timestamp of the last output
2009 // buffer that was returned by GetNextBuffer(). This test verifies that
2010 // GetNextBuffer() skips to second GOP in the newly appended data instead
2011 // of returning two buffers with the same timestamp.
2012 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenStartOverlap2) {
2013   NewSegmentAppend("0K 30 60 90 120");
2014
2015   Seek(0);
2016   CheckExpectedBuffers("0K 30 60 90 120");
2017   CheckNoNextBuffer();
2018
2019   // Append a keyframe with the same timestamp as the last buffer output.
2020   NewSegmentAppend("120K");
2021   CheckNoNextBuffer();
2022
2023   // Append the rest of the segment and make sure that buffers are returned
2024   // from the first GOP after 120.
2025   AppendBuffers("150 180 210K 240");
2026   CheckExpectedBuffers("210K 240");
2027
2028   // Seek to the beginning and verify the contents of the source buffer.
2029   Seek(0);
2030   CheckExpectedBuffers("0K 30 60 90 120K 150 180 210K 240");
2031   CheckNoNextBuffer();
2032 }
2033
2034 // This test covers the case where new buffers completely overlap a range
2035 // whose next buffer is not buffered.
2036 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenCompleteOverlap) {
2037   // Append 5 buffers at positions 10 through 14 and exhaust the buffers.
2038   NewSegmentAppend(10, 5, &kDataA);
2039   Seek(10);
2040   CheckExpectedBuffers(10, 14, &kDataA);
2041
2042   // Next buffer is at position 15, so should not be able to fulfill request.
2043   CheckNoNextBuffer();
2044
2045   // Do a complete overlap and test that this successfully fulfills the read
2046   // at position 15.
2047   NewSegmentAppend(5, 11, &kDataB);
2048   CheckExpectedBuffers(15, 15, &kDataB);
2049
2050   // Then add 5 buffers from positions 16 though 20.
2051   AppendBuffers(16, 5, &kDataB);
2052
2053   // Check the next 4 buffers are correct, which also effectively seeks to
2054   // position 20.
2055   CheckExpectedBuffers(16, 19, &kDataB);
2056
2057   // Do a complete overlap and replace the buffer at position 20.
2058   NewSegmentAppend(0, 21, &kDataA);
2059   CheckExpectedBuffers(20, 20, &kDataA);
2060 }
2061
2062 // This test covers the case where a range is stalled waiting for its next
2063 // buffer, then an end-overlap causes the end of the range to be deleted.
2064 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenEndOverlap) {
2065   // Append 5 buffers at positions 10 through 14 and exhaust the buffers.
2066   NewSegmentAppend(10, 5, &kDataA);
2067   Seek(10);
2068   CheckExpectedBuffers(10, 14, &kDataA);
2069   CheckExpectedRanges("{ [10,14) }");
2070
2071   // Next buffer is at position 15, so should not be able to fulfill request.
2072   CheckNoNextBuffer();
2073
2074   // Do an end overlap that causes the latter half of the range to be deleted.
2075   NewSegmentAppend(5, 6, &kDataB);
2076   CheckNoNextBuffer();
2077   CheckExpectedRanges("{ [5,10) }");
2078
2079   // Fill in the gap. Getting the next buffer should still stall at position 15.
2080   for (int i = 11; i <= 14; i++) {
2081     AppendBuffers(i, 1, &kDataB);
2082     CheckNoNextBuffer();
2083   }
2084
2085   // Append the buffer at position 15 and check to make sure all is correct.
2086   AppendBuffers(15, 1);
2087   CheckExpectedBuffers(15, 15);
2088   CheckExpectedRanges("{ [5,15) }");
2089 }
2090
2091 // This test is testing the "next buffer" logic after a complete overlap. In
2092 // this scenario, when the track buffer is exhausted, there is no buffered data
2093 // to fulfill the request. The SourceBufferStream should be able to fulfill the
2094 // request when the data is later appended, and should not lose track of the
2095 // "next buffer" position.
2096 TEST_F(SourceBufferStreamTest, GetNextBuffer_Overlap_Selected_Complete) {
2097   // Append 5 buffers at positions 5 through 9.
2098   NewSegmentAppend(5, 5, &kDataA);
2099
2100   // Seek to buffer at position 5 and get next buffer.
2101   Seek(5);
2102   CheckExpectedBuffers(5, 5, &kDataA);
2103
2104   // Replace existing data with new data.
2105   NewSegmentAppend(5, 5, &kDataB);
2106
2107   // Expect old data up until next keyframe in new data.
2108   CheckExpectedBuffers(6, 9, &kDataA);
2109
2110   // Next buffer is at position 10, so should not be able to fulfill the
2111   // request.
2112   CheckNoNextBuffer();
2113
2114   // Now add 5 new buffers at positions 10 through 14.
2115   AppendBuffers(10, 5, &kDataB);
2116   CheckExpectedBuffers(10, 14, &kDataB);
2117 }
2118
2119 TEST_F(SourceBufferStreamTest, PresentationTimestampIndependence) {
2120   // Append 20 buffers at position 0.
2121   NewSegmentAppend(0, 20);
2122   Seek(0);
2123
2124   int last_keyframe_idx = -1;
2125   base::TimeDelta last_keyframe_presentation_timestamp;
2126   base::TimeDelta last_p_frame_presentation_timestamp;
2127
2128   // Check for IBB...BBP pattern.
2129   for (int i = 0; i < 20; i++) {
2130     scoped_refptr<StreamParserBuffer> buffer;
2131     ASSERT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kSuccess);
2132
2133     if (buffer->IsKeyframe()) {
2134       EXPECT_EQ(buffer->timestamp(), buffer->GetDecodeTimestamp());
2135       last_keyframe_idx = i;
2136       last_keyframe_presentation_timestamp = buffer->timestamp();
2137     } else if (i == last_keyframe_idx + 1) {
2138       ASSERT_NE(last_keyframe_idx, -1);
2139       last_p_frame_presentation_timestamp = buffer->timestamp();
2140       EXPECT_LT(last_keyframe_presentation_timestamp,
2141                 last_p_frame_presentation_timestamp);
2142     } else {
2143       EXPECT_GT(buffer->timestamp(), last_keyframe_presentation_timestamp);
2144       EXPECT_LT(buffer->timestamp(), last_p_frame_presentation_timestamp);
2145       EXPECT_LT(buffer->timestamp(), buffer->GetDecodeTimestamp());
2146     }
2147   }
2148 }
2149
2150 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFront) {
2151   // Set memory limit to 20 buffers.
2152   SetMemoryLimit(20);
2153
2154   // Append 20 buffers at positions 0 through 19.
2155   NewSegmentAppend(0, 1, &kDataA);
2156   for (int i = 1; i < 20; i++)
2157     AppendBuffers(i, 1, &kDataA);
2158
2159   // None of the buffers should trigger garbage collection, so all data should
2160   // be there as expected.
2161   CheckExpectedRanges("{ [0,19) }");
2162   Seek(0);
2163   CheckExpectedBuffers(0, 19, &kDataA);
2164
2165   // Seek to the middle of the stream.
2166   Seek(10);
2167
2168   // Append 5 buffers to the end of the stream.
2169   AppendBuffers(20, 5, &kDataA);
2170
2171   // GC should have deleted the first 5 buffers.
2172   CheckExpectedRanges("{ [5,24) }");
2173   CheckExpectedBuffers(10, 24, &kDataA);
2174   Seek(5);
2175   CheckExpectedBuffers(5, 9, &kDataA);
2176 }
2177
2178 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFrontGOPsAtATime) {
2179   // Set memory limit to 20 buffers.
2180   SetMemoryLimit(20);
2181
2182   // Append 20 buffers at positions 0 through 19.
2183   NewSegmentAppend(0, 20, &kDataA);
2184
2185   // Seek to position 10.
2186   Seek(10);
2187
2188   // Add one buffer to put the memory over the cap.
2189   AppendBuffers(20, 1, &kDataA);
2190
2191   // GC should have deleted the first 5 buffers so that the range still begins
2192   // with a keyframe.
2193   CheckExpectedRanges("{ [5,20) }");
2194   CheckExpectedBuffers(10, 20, &kDataA);
2195   Seek(5);
2196   CheckExpectedBuffers(5, 9, &kDataA);
2197 }
2198
2199 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteBack) {
2200   // Set memory limit to 5 buffers.
2201   SetMemoryLimit(5);
2202
2203   // Seek to position 0.
2204   Seek(0);
2205
2206   // Append 20 buffers at positions 0 through 19.
2207   NewSegmentAppend(0, 20, &kDataA);
2208
2209   // Should leave the first 5 buffers from 0 to 4 and the last GOP appended.
2210   CheckExpectedRanges("{ [0,4) [15,19) }");
2211   CheckExpectedBuffers(0, 4, &kDataA);
2212 }
2213
2214 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFrontAndBack) {
2215   // Set memory limit to 3 buffers.
2216   SetMemoryLimit(3);
2217
2218   // Seek to position 15.
2219   Seek(15);
2220
2221   // Append 40 buffers at positions 0 through 39.
2222   NewSegmentAppend(0, 40, &kDataA);
2223
2224   // Should leave the GOP containing the seek position and the last GOP
2225   // appended.
2226   CheckExpectedRanges("{ [15,19) [35,39) }");
2227   CheckExpectedBuffers(15, 19, &kDataA);
2228   CheckNoNextBuffer();
2229 }
2230
2231 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteSeveralRanges) {
2232   // Append 5 buffers at positions 0 through 4.
2233   NewSegmentAppend(0, 5);
2234
2235   // Append 5 buffers at positions 10 through 14.
2236   NewSegmentAppend(10, 5);
2237
2238   // Append 5 buffers at positions 20 through 24.
2239   NewSegmentAppend(20, 5);
2240
2241   // Append 5 buffers at positions 30 through 34.
2242   NewSegmentAppend(30, 5);
2243
2244   CheckExpectedRanges("{ [0,4) [10,14) [20,24) [30,34) }");
2245
2246   // Seek to position 21.
2247   Seek(20);
2248   CheckExpectedBuffers(20, 20);
2249
2250   // Set memory limit to 1 buffer.
2251   SetMemoryLimit(1);
2252
2253   // Append 5 buffers at positions 40 through 44. This will trigger GC.
2254   NewSegmentAppend(40, 5);
2255
2256   // Should delete everything except the GOP containing the current buffer and
2257   // the last GOP appended.
2258   CheckExpectedRanges("{ [20,24) [40,44) }");
2259   CheckExpectedBuffers(21, 24);
2260   CheckNoNextBuffer();
2261
2262   // Continue appending into the last range to make sure it didn't break.
2263   AppendBuffers(45, 10);
2264   // Should only save last GOP appended.
2265   CheckExpectedRanges("{ [20,24) [50,54) }");
2266
2267   // Make sure appending before and after the ranges didn't somehow break.
2268   SetMemoryLimit(100);
2269   NewSegmentAppend(0, 10);
2270   CheckExpectedRanges("{ [0,9) [20,24) [50,54) }");
2271   Seek(0);
2272   CheckExpectedBuffers(0, 9);
2273
2274   NewSegmentAppend(90, 10);
2275   CheckExpectedRanges("{ [0,9) [20,24) [50,54) [90,99) }");
2276   Seek(50);
2277   CheckExpectedBuffers(50, 54);
2278   CheckNoNextBuffer();
2279   Seek(90);
2280   CheckExpectedBuffers(90, 99);
2281   CheckNoNextBuffer();
2282 }
2283
2284 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteAfterLastAppend) {
2285   // Set memory limit to 10 buffers.
2286   SetMemoryLimit(10);
2287
2288   // Append 1 GOP starting at 310ms, 30ms apart.
2289   NewSegmentAppend("310K 340 370");
2290
2291   // Append 2 GOPs starting at 490ms, 30ms apart.
2292   NewSegmentAppend("490K 520 550 580K 610 640");
2293
2294   CheckExpectedRangesByTimestamp("{ [310,400) [490,670) }");
2295
2296   // Seek to the GOP at 580ms.
2297   SeekToTimestamp(base::TimeDelta::FromMilliseconds(580));
2298
2299   // Append 2 GOPs before the existing ranges.
2300   // So the ranges before GC are "{ [100,280) [310,400) [490,670) }".
2301   NewSegmentAppend("100K 130 160 190K 220 250K");
2302
2303   // Should save the newly appended GOPs.
2304   CheckExpectedRangesByTimestamp("{ [100,280) [580,670) }");
2305 }
2306
2307 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteAfterLastAppendMerged) {
2308   // Set memory limit to 10 buffers.
2309   SetMemoryLimit(10);
2310
2311   // Append 3 GOPs starting at 400ms, 30ms apart.
2312   NewSegmentAppend("400K 430 460 490K 520 550 580K 610 640");
2313
2314   // Seek to the GOP at 580ms.
2315   SeekToTimestamp(base::TimeDelta::FromMilliseconds(580));
2316
2317   // Append 2 GOPs starting at 220ms, and they will be merged with the existing
2318   // range.  So the range before GC is "{ [220,670) }".
2319   NewSegmentAppend("220K 250 280 310K 340 370");
2320
2321   // Should save the newly appended GOPs.
2322   CheckExpectedRangesByTimestamp("{ [220,400) [580,670) }");
2323 }
2324
2325 TEST_F(SourceBufferStreamTest, GarbageCollection_NoSeek) {
2326   // Set memory limit to 20 buffers.
2327   SetMemoryLimit(20);
2328
2329   // Append 25 buffers at positions 0 through 24.
2330   NewSegmentAppend(0, 25, &kDataA);
2331
2332   // GC deletes the first 5 buffers to keep the memory limit within cap.
2333   CheckExpectedRanges("{ [5,24) }");
2334   CheckNoNextBuffer();
2335   Seek(5);
2336   CheckExpectedBuffers(5, 24, &kDataA);
2337 }
2338
2339 TEST_F(SourceBufferStreamTest, GarbageCollection_PendingSeek) {
2340   // Append 10 buffers at positions 0 through 9.
2341   NewSegmentAppend(0, 10, &kDataA);
2342
2343   // Append 5 buffers at positions 25 through 29.
2344   NewSegmentAppend(25, 5, &kDataA);
2345
2346   // Seek to position 15.
2347   Seek(15);
2348   CheckNoNextBuffer();
2349
2350   CheckExpectedRanges("{ [0,9) [25,29) }");
2351
2352   // Set memory limit to 5 buffers.
2353   SetMemoryLimit(5);
2354
2355   // Append 5 buffers as positions 30 to 34 to trigger GC.
2356   AppendBuffers(30, 5, &kDataA);
2357
2358   // The current algorithm will delete from the beginning until the memory is
2359   // under cap.
2360   CheckExpectedRanges("{ [30,34) }");
2361
2362   // Expand memory limit again so that GC won't be triggered.
2363   SetMemoryLimit(100);
2364
2365   // Append data to fulfill seek.
2366   NewSegmentAppend(15, 5, &kDataA);
2367
2368   // Check to make sure all is well.
2369   CheckExpectedRanges("{ [15,19) [30,34) }");
2370   CheckExpectedBuffers(15, 19, &kDataA);
2371   Seek(30);
2372   CheckExpectedBuffers(30, 34, &kDataA);
2373 }
2374
2375 TEST_F(SourceBufferStreamTest, GarbageCollection_NeedsMoreData) {
2376   // Set memory limit to 15 buffers.
2377   SetMemoryLimit(15);
2378
2379   // Append 10 buffers at positions 0 through 9.
2380   NewSegmentAppend(0, 10, &kDataA);
2381
2382   // Advance next buffer position to 10.
2383   Seek(0);
2384   CheckExpectedBuffers(0, 9, &kDataA);
2385   CheckNoNextBuffer();
2386
2387   // Append 20 buffers at positions 15 through 34.
2388   NewSegmentAppend(15, 20, &kDataA);
2389
2390   // GC should have saved the keyframe before the current seek position and the
2391   // data closest to the current seek position. It will also save the last GOP
2392   // appended.
2393   CheckExpectedRanges("{ [5,9) [15,19) [30,34) }");
2394
2395   // Now fulfill the seek at position 10. This will make GC delete the data
2396   // before position 10 to keep it within cap.
2397   NewSegmentAppend(10, 5, &kDataA);
2398   CheckExpectedRanges("{ [10,19) [30,34) }");
2399   CheckExpectedBuffers(10, 19, &kDataA);
2400 }
2401
2402 TEST_F(SourceBufferStreamTest, GarbageCollection_TrackBuffer) {
2403   // Set memory limit to 3 buffers.
2404   SetMemoryLimit(3);
2405
2406   // Seek to position 15.
2407   Seek(15);
2408
2409   // Append 18 buffers at positions 0 through 17.
2410   NewSegmentAppend(0, 18, &kDataA);
2411
2412   // Should leave GOP containing seek position.
2413   CheckExpectedRanges("{ [15,17) }");
2414
2415   // Seek ahead to position 16.
2416   CheckExpectedBuffers(15, 15, &kDataA);
2417
2418   // Completely overlap the existing buffers.
2419   NewSegmentAppend(0, 20, &kDataB);
2420
2421   // Because buffers 16 and 17 are not keyframes, they are moved to the track
2422   // buffer upon overlap. The source buffer (i.e. not the track buffer) is now
2423   // waiting for the next keyframe.
2424   CheckExpectedRanges("{ [15,19) }");
2425   CheckExpectedBuffers(16, 17, &kDataA);
2426   CheckNoNextBuffer();
2427
2428   // Now add a keyframe at position 20.
2429   AppendBuffers(20, 5, &kDataB);
2430
2431   // Should garbage collect such that there are 5 frames remaining, starting at
2432   // the keyframe.
2433   CheckExpectedRanges("{ [20,24) }");
2434   CheckExpectedBuffers(20, 24, &kDataB);
2435   CheckNoNextBuffer();
2436 }
2437
2438 // Test saving the last GOP appended when this GOP is the only GOP in its range.
2439 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP) {
2440   // Set memory limit to 3 and make sure the 4-byte GOP is not garbage
2441   // collected.
2442   SetMemoryLimit(3);
2443   NewSegmentAppend("0K 30 60 90");
2444   CheckExpectedRangesByTimestamp("{ [0,120) }");
2445
2446   // Make sure you can continue appending data to this GOP; again, GC should not
2447   // wipe out anything.
2448   AppendBuffers("120");
2449   CheckExpectedRangesByTimestamp("{ [0,150) }");
2450
2451   // Set memory limit to 100 and append a 2nd range after this without
2452   // triggering GC.
2453   SetMemoryLimit(100);
2454   NewSegmentAppend("200K 230 260 290K 320 350");
2455   CheckExpectedRangesByTimestamp("{ [0,150) [200,380) }");
2456
2457   // Seek to 290ms.
2458   SeekToTimestamp(base::TimeDelta::FromMilliseconds(290));
2459
2460   // Now set memory limit to 3 and append a GOP in a separate range after the
2461   // selected range. Because it is after 290ms, this tests that the GOP is saved
2462   // when deleting from the back.
2463   SetMemoryLimit(3);
2464   NewSegmentAppend("500K 530 560 590");
2465
2466   // Should save GOP with 290ms and last GOP appended.
2467   CheckExpectedRangesByTimestamp("{ [290,380) [500,620) }");
2468
2469   // Continue appending to this GOP after GC.
2470   AppendBuffers("620");
2471   CheckExpectedRangesByTimestamp("{ [290,380) [500,650) }");
2472 }
2473
2474 // Test saving the last GOP appended when this GOP is in the middle of a
2475 // non-selected range.
2476 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Middle) {
2477   // Append 3 GOPs starting at 0ms, 30ms apart.
2478   NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240");
2479   CheckExpectedRangesByTimestamp("{ [0,270) }");
2480
2481   // Now set the memory limit to 1 and overlap the middle of the range with a
2482   // new GOP.
2483   SetMemoryLimit(1);
2484   NewSegmentAppend("80K 110 140");
2485
2486   // This whole GOP should be saved, and should be able to continue appending
2487   // data to it.
2488   CheckExpectedRangesByTimestamp("{ [80,170) }");
2489   AppendBuffers("170");
2490   CheckExpectedRangesByTimestamp("{ [80,200) }");
2491
2492   // Set memory limit to 100 and append a 2nd range after this without
2493   // triggering GC.
2494   SetMemoryLimit(100);
2495   NewSegmentAppend("400K 430 460 490K 520 550 580K 610 640");
2496   CheckExpectedRangesByTimestamp("{ [80,200) [400,670) }");
2497
2498   // Seek to 80ms to make the first range the selected range.
2499   SeekToTimestamp(base::TimeDelta::FromMilliseconds(80));
2500
2501   // Now set memory limit to 3 and append a GOP in the middle of the second
2502   // range. Because it is after the selected range, this tests that the GOP is
2503   // saved when deleting from the back.
2504   SetMemoryLimit(3);
2505   NewSegmentAppend("500K 530 560 590");
2506
2507   // Should save the GOP containing the seek point and GOP that was last
2508   // appended.
2509   CheckExpectedRangesByTimestamp("{ [80,200) [500,620) }");
2510
2511   // Continue appending to this GOP after GC.
2512   AppendBuffers("620");
2513   CheckExpectedRangesByTimestamp("{ [80,200) [500,650) }");
2514 }
2515
2516 // Test saving the last GOP appended when the GOP containing the next buffer is
2517 // adjacent to the last GOP appended.
2518 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected1) {
2519   // Append 3 GOPs at 0ms, 90ms, and 180ms.
2520   NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240");
2521   CheckExpectedRangesByTimestamp("{ [0,270) }");
2522
2523   // Seek to the GOP at 90ms.
2524   SeekToTimestamp(base::TimeDelta::FromMilliseconds(90));
2525
2526   // Set the memory limit to 1, then overlap the GOP at 0.
2527   SetMemoryLimit(1);
2528   NewSegmentAppend("0K 30 60");
2529
2530   // Should save the GOP at 0ms and 90ms.
2531   CheckExpectedRangesByTimestamp("{ [0,180) }");
2532
2533   // Seek to 0 and check all buffers.
2534   SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
2535   CheckExpectedBuffers("0K 30 60 90K 120 150");
2536   CheckNoNextBuffer();
2537
2538   // Now seek back to 90ms and append a GOP at 180ms.
2539   SeekToTimestamp(base::TimeDelta::FromMilliseconds(90));
2540   NewSegmentAppend("180K 210 240");
2541
2542   // Should save the GOP at 90ms and the GOP at 180ms.
2543   CheckExpectedRangesByTimestamp("{ [90,270) }");
2544   CheckExpectedBuffers("90K 120 150 180K 210 240");
2545   CheckNoNextBuffer();
2546 }
2547
2548 // Test saving the last GOP appended when it is at the beginning or end of the
2549 // selected range. This tests when the last GOP appended is before or after the
2550 // GOP containing the next buffer, but not directly adjacent to this GOP.
2551 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected2) {
2552   // Append 4 GOPs starting at positions 0ms, 90ms, 180ms, 270ms.
2553   NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330");
2554   CheckExpectedRangesByTimestamp("{ [0,360) }");
2555
2556   // Seek to the last GOP at 270ms.
2557   SeekToTimestamp(base::TimeDelta::FromMilliseconds(270));
2558
2559   // Set the memory limit to 1, then overlap the GOP at 90ms.
2560   SetMemoryLimit(1);
2561   NewSegmentAppend("90K 120 150");
2562
2563   // Should save the GOP at 90ms and the GOP at 270ms.
2564   CheckExpectedRangesByTimestamp("{ [90,180) [270,360) }");
2565
2566   // Set memory limit to 100 and add 3 GOPs to the end of the selected range
2567   // at 360ms, 450ms, and 540ms.
2568   SetMemoryLimit(100);
2569   NewSegmentAppend("360K 390 420 450K 480 510 540K 570 600");
2570   CheckExpectedRangesByTimestamp("{ [90,180) [270,630) }");
2571
2572   // Constrain the memory limit again and overlap the GOP at 450ms to test
2573   // deleting from the back.
2574   SetMemoryLimit(1);
2575   NewSegmentAppend("450K 480 510");
2576
2577   // Should save GOP at 270ms and the GOP at 450ms.
2578   CheckExpectedRangesByTimestamp("{ [270,360) [450,540) }");
2579 }
2580
2581 // Test saving the last GOP appended when it is the same as the GOP containing
2582 // the next buffer.
2583 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected3) {
2584   // Seek to start of stream.
2585   SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
2586
2587   // Append 3 GOPs starting at 0ms, 90ms, 180ms.
2588   NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240");
2589   CheckExpectedRangesByTimestamp("{ [0,270) }");
2590
2591   // Set the memory limit to 1 then begin appending the start of a GOP starting
2592   // at 0ms.
2593   SetMemoryLimit(1);
2594   NewSegmentAppend("0K 30");
2595
2596   // Should save the newly appended GOP, which is also the next GOP that will be
2597   // returned from the seek request.
2598   CheckExpectedRangesByTimestamp("{ [0,60) }");
2599
2600   // Check the buffers in the range.
2601   CheckExpectedBuffers("0K 30");
2602   CheckNoNextBuffer();
2603
2604   // Continue appending to this buffer.
2605   AppendBuffers("60 90");
2606
2607   // Should still save the rest of this GOP and should be able to fulfill the
2608   // read.
2609   CheckExpectedRangesByTimestamp("{ [0,120) }");
2610   CheckExpectedBuffers("60 90");
2611   CheckNoNextBuffer();
2612 }
2613
2614 // Currently disabled because of bug: crbug.com/140875.
2615 TEST_F(SourceBufferStreamTest, DISABLED_GarbageCollection_WaitingForKeyframe) {
2616   // Set memory limit to 10 buffers.
2617   SetMemoryLimit(10);
2618
2619   // Append 5 buffers at positions 10 through 14 and exhaust the buffers.
2620   NewSegmentAppend(10, 5, &kDataA);
2621   Seek(10);
2622   CheckExpectedBuffers(10, 14, &kDataA);
2623   CheckExpectedRanges("{ [10,14) }");
2624
2625   // We are now stalled at position 15.
2626   CheckNoNextBuffer();
2627
2628   // Do an end overlap that causes the latter half of the range to be deleted.
2629   NewSegmentAppend(5, 6, &kDataA);
2630   CheckNoNextBuffer();
2631   CheckExpectedRanges("{ [5,10) }");
2632
2633   // Append buffers from position 20 to 29. This should trigger GC.
2634   NewSegmentAppend(20, 10, &kDataA);
2635
2636   // GC should keep the keyframe before the seek position 15, and the next 9
2637   // buffers closest to the seek position.
2638   CheckNoNextBuffer();
2639   CheckExpectedRanges("{ [10,10) [20,28) }");
2640
2641   // Fulfill the seek by appending one buffer at 15.
2642   NewSegmentAppend(15, 1, &kDataA);
2643   CheckExpectedBuffers(15, 15, &kDataA);
2644   CheckExpectedRanges("{ [15,15) [20,28) }");
2645 }
2646
2647 // Test the performance of garbage collection.
2648 TEST_F(SourceBufferStreamTest, GarbageCollection_Performance) {
2649   // Force |keyframes_per_second_| to be equal to kDefaultFramesPerSecond.
2650   SetStreamInfo(kDefaultFramesPerSecond, kDefaultFramesPerSecond);
2651
2652   const int kBuffersToKeep = 1000;
2653   SetMemoryLimit(kBuffersToKeep);
2654
2655   int buffers_appended = 0;
2656
2657   NewSegmentAppend(0, kBuffersToKeep);
2658   buffers_appended += kBuffersToKeep;
2659
2660   const int kBuffersToAppend = 1000;
2661   const int kGarbageCollections = 3;
2662   for (int i = 0; i < kGarbageCollections; ++i) {
2663     AppendBuffers(buffers_appended, kBuffersToAppend);
2664     buffers_appended += kBuffersToAppend;
2665   }
2666 }
2667
2668 TEST_F(SourceBufferStreamTest, GetRemovalRange_BytesToFree) {
2669   // Append 2 GOPs starting at 300ms, 30ms apart.
2670   NewSegmentAppend("300K 330 360 390K 420 450");
2671
2672   // Append 2 GOPs starting at 600ms, 30ms apart.
2673   NewSegmentAppend("600K 630 660 690K 720 750");
2674
2675   // Append 2 GOPs starting at 900ms, 30ms apart.
2676   NewSegmentAppend("900K 930 960 990K 1020 1050");
2677
2678   CheckExpectedRangesByTimestamp("{ [300,480) [600,780) [900,1080) }");
2679
2680   int remove_range_end = -1;
2681   int bytes_removed = -1;
2682
2683   // Size 0.
2684   bytes_removed = GetRemovalRangeInMs(300, 1080, 0, &remove_range_end);
2685   EXPECT_EQ(-1, remove_range_end);
2686   EXPECT_EQ(0, bytes_removed);
2687
2688   // Smaller than the size of GOP.
2689   bytes_removed = GetRemovalRangeInMs(300, 1080, 1, &remove_range_end);
2690   EXPECT_EQ(390, remove_range_end);
2691   // Remove as the size of GOP.
2692   EXPECT_EQ(3, bytes_removed);
2693
2694   // The same size with a GOP.
2695   bytes_removed = GetRemovalRangeInMs(300, 1080, 3, &remove_range_end);
2696   EXPECT_EQ(390, remove_range_end);
2697   EXPECT_EQ(3, bytes_removed);
2698
2699   // The same size with a range.
2700   bytes_removed = GetRemovalRangeInMs(300, 1080, 6, &remove_range_end);
2701   EXPECT_EQ(480, remove_range_end);
2702   EXPECT_EQ(6, bytes_removed);
2703
2704   // A frame larger than a range.
2705   bytes_removed = GetRemovalRangeInMs(300, 1080, 7, &remove_range_end);
2706   EXPECT_EQ(690, remove_range_end);
2707   EXPECT_EQ(9, bytes_removed);
2708
2709   // The same size with two ranges.
2710   bytes_removed = GetRemovalRangeInMs(300, 1080, 12, &remove_range_end);
2711   EXPECT_EQ(780, remove_range_end);
2712   EXPECT_EQ(12, bytes_removed);
2713
2714   // Larger than two ranges.
2715   bytes_removed = GetRemovalRangeInMs(300, 1080, 14, &remove_range_end);
2716   EXPECT_EQ(990, remove_range_end);
2717   EXPECT_EQ(15, bytes_removed);
2718
2719   // The same size with the whole ranges.
2720   bytes_removed = GetRemovalRangeInMs(300, 1080, 18, &remove_range_end);
2721   EXPECT_EQ(1080, remove_range_end);
2722   EXPECT_EQ(18, bytes_removed);
2723
2724   // Larger than the whole ranges.
2725   bytes_removed = GetRemovalRangeInMs(300, 1080, 20, &remove_range_end);
2726   EXPECT_EQ(1080, remove_range_end);
2727   EXPECT_EQ(18, bytes_removed);
2728 }
2729
2730 TEST_F(SourceBufferStreamTest, GetRemovalRange_Range) {
2731   // Append 2 GOPs starting at 300ms, 30ms apart.
2732   NewSegmentAppend("300K 330 360 390K 420 450");
2733
2734   // Append 2 GOPs starting at 600ms, 30ms apart.
2735   NewSegmentAppend("600K 630 660 690K 720 750");
2736
2737   // Append 2 GOPs starting at 900ms, 30ms apart.
2738   NewSegmentAppend("900K 930 960 990K 1020 1050");
2739
2740   CheckExpectedRangesByTimestamp("{ [300,480) [600,780) [900,1080) }");
2741
2742   int remove_range_end = -1;
2743   int bytes_removed = -1;
2744
2745   // Within a GOP and no keyframe.
2746   bytes_removed = GetRemovalRangeInMs(630, 660, 20, &remove_range_end);
2747   EXPECT_EQ(-1, remove_range_end);
2748   EXPECT_EQ(0, bytes_removed);
2749
2750   // Across a GOP and no keyframe.
2751   bytes_removed = GetRemovalRangeInMs(630, 750, 20, &remove_range_end);
2752   EXPECT_EQ(-1, remove_range_end);
2753   EXPECT_EQ(0, bytes_removed);
2754
2755   // The same size with a range.
2756   bytes_removed = GetRemovalRangeInMs(600, 780, 20, &remove_range_end);
2757   EXPECT_EQ(780, remove_range_end);
2758   EXPECT_EQ(6, bytes_removed);
2759
2760   // One frame larger than a range.
2761   bytes_removed = GetRemovalRangeInMs(570, 810, 20, &remove_range_end);
2762   EXPECT_EQ(780, remove_range_end);
2763   EXPECT_EQ(6, bytes_removed);
2764
2765   // Facing the other ranges.
2766   bytes_removed = GetRemovalRangeInMs(480, 900, 20, &remove_range_end);
2767   EXPECT_EQ(780, remove_range_end);
2768   EXPECT_EQ(6, bytes_removed);
2769
2770   // In the midle of the other ranges, but not including any GOP.
2771   bytes_removed = GetRemovalRangeInMs(420, 960, 20, &remove_range_end);
2772   EXPECT_EQ(780, remove_range_end);
2773   EXPECT_EQ(6, bytes_removed);
2774
2775   // In the middle of the other ranges.
2776   bytes_removed = GetRemovalRangeInMs(390, 990, 20, &remove_range_end);
2777   EXPECT_EQ(990, remove_range_end);
2778   EXPECT_EQ(12, bytes_removed);
2779
2780   // A frame smaller than the whole ranges.
2781   bytes_removed = GetRemovalRangeInMs(330, 1050, 20, &remove_range_end);
2782   EXPECT_EQ(990, remove_range_end);
2783   EXPECT_EQ(12, bytes_removed);
2784
2785   // The same with the whole ranges.
2786   bytes_removed = GetRemovalRangeInMs(300, 1080, 20, &remove_range_end);
2787   EXPECT_EQ(1080, remove_range_end);
2788   EXPECT_EQ(18, bytes_removed);
2789
2790   // Larger than the whole ranges.
2791   bytes_removed = GetRemovalRangeInMs(270, 1110, 20, &remove_range_end);
2792   EXPECT_EQ(1080, remove_range_end);
2793   EXPECT_EQ(18, bytes_removed);
2794 }
2795
2796 TEST_F(SourceBufferStreamTest, ConfigChange_Basic) {
2797   VideoDecoderConfig new_config = TestVideoConfig::Large();
2798   ASSERT_FALSE(new_config.Matches(video_config_));
2799
2800   Seek(0);
2801   CheckVideoConfig(video_config_);
2802
2803   // Append 5 buffers at positions 0 through 4
2804   NewSegmentAppend(0, 5, &kDataA);
2805
2806   CheckVideoConfig(video_config_);
2807
2808   // Signal a config change.
2809   stream_->UpdateVideoConfig(new_config);
2810
2811   // Make sure updating the config doesn't change anything since new_config
2812   // should not be associated with the buffer GetNextBuffer() will return.
2813   CheckVideoConfig(video_config_);
2814
2815   // Append 5 buffers at positions 5 through 9.
2816   NewSegmentAppend(5, 5, &kDataB);
2817
2818   // Consume the buffers associated with the initial config.
2819   scoped_refptr<StreamParserBuffer> buffer;
2820   for (int i = 0; i < 5; i++) {
2821     EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kSuccess);
2822     CheckVideoConfig(video_config_);
2823   }
2824
2825   // Verify the next attempt to get a buffer will signal that a config change
2826   // has happened.
2827   EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kConfigChange);
2828
2829   // Verify that the new config is now returned.
2830   CheckVideoConfig(new_config);
2831
2832   // Consume the remaining buffers associated with the new config.
2833   for (int i = 0; i < 5; i++) {
2834     CheckVideoConfig(new_config);
2835     EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kSuccess);
2836   }
2837 }
2838
2839 TEST_F(SourceBufferStreamTest, ConfigChange_Seek) {
2840   scoped_refptr<StreamParserBuffer> buffer;
2841   VideoDecoderConfig new_config = TestVideoConfig::Large();
2842
2843   Seek(0);
2844   NewSegmentAppend(0, 5, &kDataA);
2845   stream_->UpdateVideoConfig(new_config);
2846   NewSegmentAppend(5, 5, &kDataB);
2847
2848   // Seek to the start of the buffers with the new config and make sure a
2849   // config change is signalled.
2850   CheckVideoConfig(video_config_);
2851   Seek(5);
2852   CheckVideoConfig(video_config_);
2853   EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kConfigChange);
2854   CheckVideoConfig(new_config);
2855   CheckExpectedBuffers(5, 9, &kDataB);
2856
2857
2858   // Seek to the start which has a different config. Don't fetch any buffers and
2859   // seek back to buffers with the current config. Make sure a config change
2860   // isn't signalled in this case.
2861   CheckVideoConfig(new_config);
2862   Seek(0);
2863   Seek(7);
2864   CheckExpectedBuffers(5, 9, &kDataB);
2865
2866
2867   // Seek to the start and make sure a config change is signalled.
2868   CheckVideoConfig(new_config);
2869   Seek(0);
2870   CheckVideoConfig(new_config);
2871   EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kConfigChange);
2872   CheckVideoConfig(video_config_);
2873   CheckExpectedBuffers(0, 4, &kDataA);
2874 }
2875
2876 TEST_F(SourceBufferStreamTest, SetExplicitDuration) {
2877   // Append 2 buffers at positions 5 through 6.
2878   NewSegmentAppend(5, 2);
2879
2880   // Append 2 buffers at positions 10 through 11.
2881   NewSegmentAppend(10, 2);
2882
2883   // Append 2 buffers at positions 15 through 16.
2884   NewSegmentAppend(15, 2);
2885
2886   // Check expected ranges.
2887   CheckExpectedRanges("{ [5,6) [10,11) [15,16) }");
2888
2889   // Set duration to be between buffers 6 and 10.
2890   stream_->OnSetDuration(frame_duration() * 8);
2891
2892   // Should truncate the data after 6.
2893   CheckExpectedRanges("{ [5,6) }");
2894
2895   // Adding data past the previous duration should still work.
2896   NewSegmentAppend(0, 20);
2897   CheckExpectedRanges("{ [0,19) }");
2898 }
2899
2900 TEST_F(SourceBufferStreamTest, SetExplicitDuration_EdgeCase) {
2901   // Append 10 buffers at positions 10 through 19.
2902   NewSegmentAppend(10, 10);
2903
2904   // Append 5 buffers at positions 25 through 29.
2905   NewSegmentAppend(25, 5);
2906
2907   // Check expected ranges.
2908   CheckExpectedRanges("{ [10,19) [25,29) }");
2909
2910   // Set duration to be right before buffer 25.
2911   stream_->OnSetDuration(frame_duration() * 25);
2912
2913   // Should truncate the last range.
2914   CheckExpectedRanges("{ [10,19) }");
2915 }
2916
2917 TEST_F(SourceBufferStreamTest, SetExplicitDuration_DeletePartialRange) {
2918   // Append 5 buffers at positions 0 through 4.
2919   NewSegmentAppend(0, 5);
2920
2921   // Append 10 buffers at positions 10 through 19.
2922   NewSegmentAppend(10, 10);
2923
2924   // Append 5 buffers at positions 25 through 29.
2925   NewSegmentAppend(25, 5);
2926
2927   // Check expected ranges.
2928   CheckExpectedRanges("{ [0,4) [10,19) [25,29) }");
2929
2930   // Set duration to be between buffers 13 and 14.
2931   stream_->OnSetDuration(frame_duration() * 14);
2932
2933   // Should truncate the data after 13.
2934   CheckExpectedRanges("{ [0,4) [10,13) }");
2935 }
2936
2937 TEST_F(SourceBufferStreamTest, SetExplicitDuration_DeleteSelectedRange) {
2938   // Append 2 buffers at positions 5 through 6.
2939   NewSegmentAppend(5, 2);
2940
2941   // Append 2 buffers at positions 10 through 11.
2942   NewSegmentAppend(10, 2);
2943
2944   // Append 2 buffers at positions 15 through 16.
2945   NewSegmentAppend(15, 2);
2946
2947   // Check expected ranges.
2948   CheckExpectedRanges("{ [5,6) [10,11) [15,16) }");
2949
2950   // Seek to 10.
2951   Seek(10);
2952
2953   // Set duration to be after position 3.
2954   stream_->OnSetDuration(frame_duration() * 4);
2955
2956   // Expect everything to be deleted, and should not have next buffer anymore.
2957   CheckNoNextBuffer();
2958   CheckExpectedRanges("{ }");
2959
2960   // Appending data at position 10 should not fulfill the seek.
2961   // (If the duration is set to be something smaller than the current seek
2962   // point, then the seek point is reset and the SourceBufferStream waits
2963   // for a new seek request. Therefore even if the data is re-appended, it
2964   // should not fulfill the old seek.)
2965   NewSegmentAppend(0, 15);
2966   CheckNoNextBuffer();
2967   CheckExpectedRanges("{ [0,14) }");
2968 }
2969
2970 TEST_F(SourceBufferStreamTest, SetExplicitDuration_DeletePartialSelectedRange) {
2971   // Append 5 buffers at positions 0 through 4.
2972   NewSegmentAppend(0, 5);
2973
2974   // Append 20 buffers at positions 10 through 29.
2975   NewSegmentAppend(10, 20);
2976
2977   // Check expected ranges.
2978   CheckExpectedRanges("{ [0,4) [10,29) }");
2979
2980   // Seek to position 10.
2981   Seek(10);
2982
2983   // Set duration to be between buffers 24 and 25.
2984   stream_->OnSetDuration(frame_duration() * 25);
2985
2986   // Should truncate the data after 24.
2987   CheckExpectedRanges("{ [0,4) [10,24) }");
2988
2989   // The seek position should not be lost.
2990   CheckExpectedBuffers(10, 10);
2991
2992   // Now set the duration immediately after buffer 10.
2993   stream_->OnSetDuration(frame_duration() * 11);
2994
2995   // Seek position should be reset.
2996   CheckNoNextBuffer();
2997   CheckExpectedRanges("{ [0,4) [10,10) }");
2998 }
2999
3000 // Test the case where duration is set while the stream parser buffers
3001 // already start passing the data to decoding pipeline. Selected range,
3002 // when invalidated by getting truncated, should be updated to NULL
3003 // accordingly so that successive append operations keep working.
3004 TEST_F(SourceBufferStreamTest, SetExplicitDuration_UpdateSelectedRange) {
3005   // Seek to start of stream.
3006   SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
3007
3008   NewSegmentAppend("0K 30 60 90");
3009
3010   // Read out the first few buffers.
3011   CheckExpectedBuffers("0K 30");
3012
3013   // Set duration to be right before buffer 1.
3014   stream_->OnSetDuration(base::TimeDelta::FromMilliseconds(60));
3015
3016   // Verify that there is no next buffer.
3017   CheckNoNextBuffer();
3018
3019   // We should be able to append new buffers at this point.
3020   NewSegmentAppend("120K 150");
3021
3022   CheckExpectedRangesByTimestamp("{ [0,60) [120,180) }");
3023 }
3024
3025 TEST_F(SourceBufferStreamTest,
3026        SetExplicitDuration_AfterSegmentTimestampAndBeforeFirstBufferTimestamp) {
3027
3028   NewSegmentAppend("0K 30K 60K");
3029
3030   // Append a segment with a start timestamp of 200, but the first
3031   // buffer starts at 230ms. This can happen in muxed content where the
3032   // audio starts before the first frame.
3033   NewSegmentAppend(base::TimeDelta::FromMilliseconds(200),
3034                    "230K 260K 290K 320K");
3035
3036   NewSegmentAppend("400K 430K 460K");
3037
3038   CheckExpectedRangesByTimestamp("{ [0,90) [200,350) [400,490) }");
3039
3040   stream_->OnSetDuration(base::TimeDelta::FromMilliseconds(120));
3041
3042   // Verify that the buffered ranges are updated properly and we don't crash.
3043   CheckExpectedRangesByTimestamp("{ [0,90) }");
3044 }
3045
3046 // Test the case were the current playback position is at the end of the
3047 // buffered data and several overlaps occur that causes the selected
3048 // range to get split and then merged back into a single range.
3049 TEST_F(SourceBufferStreamTest, OverlapSplitAndMergeWhileWaitingForMoreData) {
3050   // Seek to start of stream.
3051   SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
3052
3053   NewSegmentAppend("0K 30 60 90 120K 150");
3054   CheckExpectedRangesByTimestamp("{ [0,180) }");
3055
3056   // Read all the buffered data.
3057   CheckExpectedBuffers("0K 30 60 90 120K 150");
3058   CheckNoNextBuffer();
3059
3060   // Append data over the current GOP so that a keyframe is needed before
3061   // playback can continue from the current position.
3062   NewSegmentAppend("120K 150");
3063   CheckExpectedRangesByTimestamp("{ [0,180) }");
3064
3065   // Append buffers that cause the range to get split.
3066   NewSegmentAppend("0K 30");
3067   CheckExpectedRangesByTimestamp("{ [0,60) [120,180) }");
3068
3069   // Append buffers that cause the ranges to get merged.
3070   AppendBuffers("60 90");
3071
3072   CheckExpectedRangesByTimestamp("{ [0,180) }");
3073
3074   // Verify that we still don't have a next buffer.
3075   CheckNoNextBuffer();
3076
3077   // Add more data to the end and verify that this new data is read correctly.
3078   NewSegmentAppend("180K 210");
3079   CheckExpectedRangesByTimestamp("{ [0,240) }");
3080   CheckExpectedBuffers("180K 210");
3081 }
3082
3083 // Verify that non-keyframes with the same timestamp in the same
3084 // append are handled correctly.
3085 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_SingleAppend) {
3086   Seek(0);
3087   NewSegmentAppend("0K 30 30 60 90 120K 150");
3088   CheckExpectedBuffers("0K 30 30 60 90 120K 150");
3089 }
3090
3091 // Verify that non-keyframes with the same timestamp can occur
3092 // in different appends.
3093 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_TwoAppends) {
3094   Seek(0);
3095   NewSegmentAppend("0K 30");
3096   AppendBuffers("30 60 90 120K 150");
3097   CheckExpectedBuffers("0K 30 30 60 90 120K 150");
3098 }
3099
3100 // Verify that a non-keyframe followed by a keyframe with the same timestamp
3101 // is not allowed.
3102 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Invalid_1) {
3103   Seek(0);
3104   NewSegmentAppend("0K 30");
3105   AppendBuffers_ExpectFailure("30K 60");
3106 }
3107
3108 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Invalid_2) {
3109   Seek(0);
3110   NewSegmentAppend_ExpectFailure("0K 30 30K 60");
3111 }
3112
3113 // Verify that a keyframe followed by a non-keyframe with the same timestamp
3114 // is not allowed.
3115 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Invalid_3) {
3116   Seek(0);
3117   NewSegmentAppend("0K 30K");
3118   AppendBuffers_ExpectFailure("30 60");
3119 }
3120
3121 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Invalid_4) {
3122   Seek(0);
3123   NewSegmentAppend_ExpectFailure("0K 30K 30 60");
3124 }
3125
3126 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Overlap_1) {
3127   Seek(0);
3128   NewSegmentAppend("0K 30 60 60 90 120K 150");
3129
3130   NewSegmentAppend("60K 91 121K 151");
3131   CheckExpectedBuffers("0K 30 60K 91 121K 151");
3132 }
3133
3134 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Overlap_2) {
3135   Seek(0);
3136   NewSegmentAppend("0K 30 60 60 90 120K 150");
3137   NewSegmentAppend("0K 30 61");
3138   CheckExpectedBuffers("0K 30 61 120K 150");
3139 }
3140
3141 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Overlap_3) {
3142   Seek(0);
3143   NewSegmentAppend("0K 20 40 60 80 100K 101 102 103K");
3144   NewSegmentAppend("0K 20 40 60 80 90");
3145   CheckExpectedBuffers("0K 20 40 60 80 90 100K 101 102 103K");
3146   AppendBuffers("90 110K 150");
3147   Seek(0);
3148   CheckExpectedBuffers("0K 20 40 60 80 90 90 110K 150");
3149   CheckNoNextBuffer();
3150   CheckExpectedRangesByTimestamp("{ [0,190) }");
3151 }
3152
3153 // Test all the valid same timestamp cases for audio.
3154 TEST_F(SourceBufferStreamTest, SameTimestamp_Audio) {
3155   AudioDecoderConfig config(kCodecMP3, kSampleFormatF32, CHANNEL_LAYOUT_STEREO,
3156                             44100, NULL, 0, false);
3157   stream_.reset(new SourceBufferStream(config, log_cb()));
3158   Seek(0);
3159   NewSegmentAppend("0K 0K 30K 30 60 60");
3160   CheckExpectedBuffers("0K 0K 30K 30 60 60");
3161 }
3162
3163 TEST_F(SourceBufferStreamTest, SameTimestamp_Audio_Invalid_1) {
3164   AudioDecoderConfig config(kCodecMP3, kSampleFormatF32, CHANNEL_LAYOUT_STEREO,
3165                             44100, NULL, 0, false);
3166   stream_.reset(new SourceBufferStream(config, log_cb()));
3167   Seek(0);
3168   NewSegmentAppend_ExpectFailure("0K 30 30K 60");
3169 }
3170
3171 // If seeking past any existing range and the seek is pending
3172 // because no data has been provided for that position,
3173 // the stream position can be considered as the end of stream.
3174 TEST_F(SourceBufferStreamTest, EndSelected_During_PendingSeek) {
3175   // Append 15 buffers at positions 0 through 14.
3176   NewSegmentAppend(0, 15);
3177
3178   Seek(20);
3179   EXPECT_TRUE(stream_->IsSeekPending());
3180   stream_->MarkEndOfStream();
3181   EXPECT_FALSE(stream_->IsSeekPending());
3182 }
3183
3184 // If there is a pending seek between 2 existing ranges,
3185 // the end of the stream has not been reached.
3186 TEST_F(SourceBufferStreamTest, EndNotSelected_During_PendingSeek) {
3187   // Append:
3188   // - 10 buffers at positions 0 through 9.
3189   // - 10 buffers at positions 30 through 39
3190   NewSegmentAppend(0, 10);
3191   NewSegmentAppend(30, 10);
3192
3193   Seek(20);
3194   EXPECT_TRUE(stream_->IsSeekPending());
3195   stream_->MarkEndOfStream();
3196   EXPECT_TRUE(stream_->IsSeekPending());
3197 }
3198
3199
3200 // Removing exact start & end of a range.
3201 TEST_F(SourceBufferStreamTest, Remove_WholeRange1) {
3202   Seek(0);
3203   NewSegmentAppend("10K 40 70K 100 130K");
3204   CheckExpectedRangesByTimestamp("{ [10,160) }");
3205   RemoveInMs(10, 160, 160);
3206   CheckExpectedRangesByTimestamp("{ }");
3207 }
3208
3209 // Removal range starts before range and ends exactly at end.
3210 TEST_F(SourceBufferStreamTest, Remove_WholeRange2) {
3211   Seek(0);
3212   NewSegmentAppend("10K 40 70K 100 130K");
3213   CheckExpectedRangesByTimestamp("{ [10,160) }");
3214   RemoveInMs(0, 160, 160);
3215   CheckExpectedRangesByTimestamp("{ }");
3216 }
3217
3218 // Removal range starts at the start of a range and ends beyond the
3219 // range end.
3220 TEST_F(SourceBufferStreamTest, Remove_WholeRange3) {
3221   Seek(0);
3222   NewSegmentAppend("10K 40 70K 100 130K");
3223   CheckExpectedRangesByTimestamp("{ [10,160) }");
3224   RemoveInMs(10, 200, 200);
3225   CheckExpectedRangesByTimestamp("{ }");
3226 }
3227
3228 // Removal range starts before range start and ends after the range end.
3229 TEST_F(SourceBufferStreamTest, Remove_WholeRange4) {
3230   Seek(0);
3231   NewSegmentAppend("10K 40 70K 100 130K");
3232   CheckExpectedRangesByTimestamp("{ [10,160) }");
3233   RemoveInMs(0, 200, 200);
3234   CheckExpectedRangesByTimestamp("{ }");
3235 }
3236
3237 // Removes multiple ranges.
3238 TEST_F(SourceBufferStreamTest, Remove_WholeRange5) {
3239   Seek(0);
3240   NewSegmentAppend("10K 40 70K 100 130K");
3241   NewSegmentAppend("1000K 1030 1060K 1090 1120K");
3242   NewSegmentAppend("2000K 2030 2060K 2090 2120K");
3243   CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) [2000,2150) }");
3244   RemoveInMs(10, 3000, 3000);
3245   CheckExpectedRangesByTimestamp("{ }");
3246 }
3247
3248 // Verifies a [0-infinity) range removes everything.
3249 TEST_F(SourceBufferStreamTest, Remove_ZeroToInfinity) {
3250   Seek(0);
3251   NewSegmentAppend("10K 40 70K 100 130K");
3252   NewSegmentAppend("1000K 1030 1060K 1090 1120K");
3253   NewSegmentAppend("2000K 2030 2060K 2090 2120K");
3254   CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) [2000,2150) }");
3255   Remove(base::TimeDelta(), kInfiniteDuration(), kInfiniteDuration());
3256   CheckExpectedRangesByTimestamp("{ }");
3257 }
3258
3259 // Removal range starts at the beginning of the range and ends in the
3260 // middle of the range. This test verifies that full GOPs are removed.
3261 TEST_F(SourceBufferStreamTest, Remove_Partial1) {
3262   Seek(0);
3263   NewSegmentAppend("10K 40 70K 100 130K");
3264   NewSegmentAppend("1000K 1030 1060K 1090 1120K");
3265   CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) }");
3266   RemoveInMs(0, 80, 2200);
3267   CheckExpectedRangesByTimestamp("{ [130,160) [1000,1150) }");
3268 }
3269
3270 // Removal range starts in the middle of a range and ends at the exact
3271 // end of the range.
3272 TEST_F(SourceBufferStreamTest, Remove_Partial2) {
3273   Seek(0);
3274   NewSegmentAppend("10K 40 70K 100 130K");
3275   NewSegmentAppend("1000K 1030 1060K 1090 1120K");
3276   CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) }");
3277   RemoveInMs(40, 160, 2200);
3278   CheckExpectedRangesByTimestamp("{ [10,40) [1000,1150) }");
3279 }
3280
3281 // Removal range starts and ends within a range.
3282 TEST_F(SourceBufferStreamTest, Remove_Partial3) {
3283   Seek(0);
3284   NewSegmentAppend("10K 40 70K 100 130K");
3285   NewSegmentAppend("1000K 1030 1060K 1090 1120K");
3286   CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) }");
3287   RemoveInMs(40, 120, 2200);
3288   CheckExpectedRangesByTimestamp("{ [10,40) [130,160) [1000,1150) }");
3289 }
3290
3291 // Removal range starts in the middle of one range and ends in the
3292 // middle of another range.
3293 TEST_F(SourceBufferStreamTest, Remove_Partial4) {
3294   Seek(0);
3295   NewSegmentAppend("10K 40 70K 100 130K");
3296   NewSegmentAppend("1000K 1030 1060K 1090 1120K");
3297   NewSegmentAppend("2000K 2030 2060K 2090 2120K");
3298   CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) [2000,2150) }");
3299   RemoveInMs(40, 2030, 2200);
3300   CheckExpectedRangesByTimestamp("{ [10,40) [2060,2150) }");
3301 }
3302
3303 // Test behavior when the current position is removed and new buffers
3304 // are appended over the removal range.
3305 TEST_F(SourceBufferStreamTest, Remove_CurrentPosition) {
3306   Seek(0);
3307   NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330");
3308   CheckExpectedRangesByTimestamp("{ [0,360) }");
3309   CheckExpectedBuffers("0K 30 60 90K 120");
3310
3311   // Remove a range that includes the next buffer (i.e., 150).
3312   RemoveInMs(150, 210, 360);
3313   CheckExpectedRangesByTimestamp("{ [0,150) [270,360) }");
3314
3315   // Verify that no next buffer is returned.
3316   CheckNoNextBuffer();
3317
3318   // Append some buffers to fill the gap that was created.
3319   NewSegmentAppend("120K 150 180 210K 240");
3320   CheckExpectedRangesByTimestamp("{ [0,360) }");
3321
3322   // Verify that buffers resume at the next keyframe after the
3323   // current position.
3324   CheckExpectedBuffers("210K 240 270K 300 330");
3325 }
3326
3327 // Test behavior when buffers in the selected range before the current position
3328 // are removed.
3329 TEST_F(SourceBufferStreamTest, Remove_BeforeCurrentPosition) {
3330   Seek(0);
3331   NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330");
3332   CheckExpectedRangesByTimestamp("{ [0,360) }");
3333   CheckExpectedBuffers("0K 30 60 90K 120");
3334
3335   // Remove a range that is before the current playback position.
3336   RemoveInMs(0, 90, 360);
3337   CheckExpectedRangesByTimestamp("{ [90,360) }");
3338
3339   CheckExpectedBuffers("150 180K 210 240 270K 300 330");
3340 }
3341
3342 // Test removing the entire range for the current media segment
3343 // being appended.
3344 TEST_F(SourceBufferStreamTest, Remove_MidSegment) {
3345   Seek(0);
3346   NewSegmentAppend("0K 30 60 90 120K 150 180 210");
3347   CheckExpectedRangesByTimestamp("{ [0,240) }");
3348
3349   NewSegmentAppend("0K 30");
3350
3351   CheckExpectedBuffers("0K");
3352
3353   CheckExpectedRangesByTimestamp("{ [0,60) [120,240) }");
3354
3355   // Remove the entire range that is being appended to.
3356   RemoveInMs(0, 60, 240);
3357
3358   // Verify that there is no next buffer since it was removed.
3359   CheckNoNextBuffer();
3360
3361   CheckExpectedRangesByTimestamp("{ [120,240) }");
3362
3363   // Continue appending frames for the current GOP.
3364   AppendBuffers("60 90");
3365
3366   // Verify that the non-keyframes are not added.
3367   CheckExpectedRangesByTimestamp("{ [120,240) }");
3368
3369   // Finish the previous GOP and start the next one.
3370   AppendBuffers("120 150K 180");
3371
3372   // Verify that new GOP replaces the existing range.
3373   CheckExpectedRangesByTimestamp("{ [150,210) }");
3374
3375
3376   SeekToTimestamp(base::TimeDelta::FromMilliseconds(150));
3377   CheckExpectedBuffers("150K 180");
3378   CheckNoNextBuffer();
3379 }
3380
3381 // Test removing the current GOP being appended, while not removing
3382 // the entire range the GOP belongs to.
3383 TEST_F(SourceBufferStreamTest, Remove_GOPBeingAppended) {
3384   Seek(0);
3385   NewSegmentAppend("0K 30 60 90 120K 150 180");
3386   CheckExpectedRangesByTimestamp("{ [0,210) }");
3387
3388   // Remove the current GOP being appended.
3389   RemoveInMs(120, 150, 240);
3390   CheckExpectedRangesByTimestamp("{ [0,120) }");
3391
3392   // Continue appending the current GOP and the next one.
3393   AppendBuffers("210 240K 270 300");
3394
3395   // Verify that the non-keyframe in the previous GOP does
3396   // not effect any existing ranges and a new range is started at the
3397   // beginning of the next GOP.
3398   CheckExpectedRangesByTimestamp("{ [0,120) [240,330) }");
3399
3400   // Verify the buffers in the ranges.
3401   CheckExpectedBuffers("0K 30 60 90");
3402   CheckNoNextBuffer();
3403   SeekToTimestamp(base::TimeDelta::FromMilliseconds(240));
3404   CheckExpectedBuffers("240K 270 300");
3405 }
3406
3407 TEST_F(SourceBufferStreamTest,
3408        Remove_PreviousAppendDestroyedAndOverwriteExistingRange) {
3409   SeekToTimestamp(base::TimeDelta::FromMilliseconds(90));
3410
3411   NewSegmentAppend("90K 120 150");
3412   CheckExpectedRangesByTimestamp("{ [90,180) }");
3413
3414   // Append a segment before the previously appended data.
3415   NewSegmentAppend("0K 30 60");
3416
3417   // Verify that the ranges get merged.
3418   CheckExpectedRangesByTimestamp("{ [0,180) }");
3419
3420   // Remove the data from the last append.
3421   RemoveInMs(0, 90, 360);
3422   CheckExpectedRangesByTimestamp("{ [90,180) }");
3423
3424   // Append a new segment that follows the removed segment and
3425   // starts at the beginning of the range left over from the
3426   // remove.
3427   NewSegmentAppend("90K 121 151");
3428   CheckExpectedBuffers("90K 121 151");
3429 }
3430
3431 TEST_F(SourceBufferStreamTest, Text_Append_SingleRange) {
3432   SetTextStream();
3433   NewSegmentAppend("0K 500K 1000K");
3434   CheckExpectedRangesByTimestamp("{ [0,1500) }");
3435
3436   Seek(0);
3437   CheckExpectedBuffers("0K 500K 1000K");
3438 }
3439
3440 TEST_F(SourceBufferStreamTest, Text_Append_DisjointAfter) {
3441   SetTextStream();
3442   NewSegmentAppend("0K 500K 1000K");
3443   CheckExpectedRangesByTimestamp("{ [0,1500) }");
3444   NewSegmentAppend("3000K 3500K 4000K");
3445   CheckExpectedRangesByTimestamp("{ [0,4500) }");
3446
3447   Seek(0);
3448   CheckExpectedBuffers("0K 500K 1000K 3000K 3500K 4000K");
3449 }
3450
3451 TEST_F(SourceBufferStreamTest, Text_Append_DisjointBefore) {
3452   SetTextStream();
3453   NewSegmentAppend("3000K 3500K 4000K");
3454   CheckExpectedRangesByTimestamp("{ [3000,4500) }");
3455   NewSegmentAppend("0K 500K 1000K");
3456   CheckExpectedRangesByTimestamp("{ [0,4500) }");
3457
3458   Seek(0);
3459   CheckExpectedBuffers("0K 500K 1000K 3000K 3500K 4000K");
3460 }
3461
3462 TEST_F(SourceBufferStreamTest, Text_CompleteOverlap) {
3463   SetTextStream();
3464   NewSegmentAppend("3000K 3500K 4000K");
3465   CheckExpectedRangesByTimestamp("{ [3000,4500) }");
3466   NewSegmentAppend("0K 501K 1001K 1501K 2001K 2501K "
3467                    "3001K 3501K 4001K 4501K 5001K");
3468   CheckExpectedRangesByTimestamp("{ [0,5502) }");
3469
3470   Seek(0);
3471   CheckExpectedBuffers("0K 501K 1001K 1501K 2001K 2501K "
3472                        "3001K 3501K 4001K 4501K 5001K");
3473 }
3474
3475 TEST_F(SourceBufferStreamTest, Text_OverlapAfter) {
3476   SetTextStream();
3477   NewSegmentAppend("0K 500K 1000K 1500K 2000K");
3478   CheckExpectedRangesByTimestamp("{ [0,2500) }");
3479   NewSegmentAppend("1499K 2001K 2501K 3001K");
3480   CheckExpectedRangesByTimestamp("{ [0,3503) }");
3481
3482   Seek(0);
3483   CheckExpectedBuffers("0K 500K 1000K 1499K 2001K 2501K 3001K");
3484 }
3485
3486 TEST_F(SourceBufferStreamTest, Text_OverlapBefore) {
3487   SetTextStream();
3488   NewSegmentAppend("1500K 2000K 2500K 3000K 3500K");
3489   CheckExpectedRangesByTimestamp("{ [1500,4000) }");
3490   NewSegmentAppend("0K 501K 1001K 1501K 2001K");
3491   CheckExpectedRangesByTimestamp("{ [0,4001) }");
3492
3493   Seek(0);
3494   CheckExpectedBuffers("0K 501K 1001K 1501K 2001K 2500K 3000K 3500K");
3495 }
3496
3497 TEST_F(SourceBufferStreamTest, SpliceFrame_Basic) {
3498   Seek(0);
3499   NewSegmentAppend("0K S(3K 6 9 10) 15 20 S(25K 30 35) 40");
3500   CheckExpectedBuffers("0K 3K 6 9 C 10 15 20 25K 30 C 35 40");
3501   CheckNoNextBuffer();
3502 }
3503
3504 TEST_F(SourceBufferStreamTest, SpliceFrame_SeekClearsSplice) {
3505   Seek(0);
3506   NewSegmentAppend("0K S(3K 6 9 10) 15K 20");
3507   CheckExpectedBuffers("0K 3K 6");
3508
3509   SeekToTimestamp(base::TimeDelta::FromMilliseconds(15));
3510   CheckExpectedBuffers("15K 20");
3511   CheckNoNextBuffer();
3512 }
3513
3514 TEST_F(SourceBufferStreamTest, SpliceFrame_SeekClearsSpliceFromTrackBuffer) {
3515   Seek(0);
3516   NewSegmentAppend("0K 2K S(3K 6 9 10) 15K 20");
3517   CheckExpectedBuffers("0K 2K");
3518
3519   // Overlap the existing segment.
3520   NewSegmentAppend("5K 15K 20");
3521   CheckExpectedBuffers("3K 6");
3522
3523   SeekToTimestamp(base::TimeDelta::FromMilliseconds(15));
3524   CheckExpectedBuffers("15K 20");
3525   CheckNoNextBuffer();
3526 }
3527
3528 TEST_F(SourceBufferStreamTest, SpliceFrame_ConfigChangeWithinSplice) {
3529   VideoDecoderConfig new_config = TestVideoConfig::Large();
3530   ASSERT_FALSE(new_config.Matches(video_config_));
3531
3532   // Add a new video config, then reset the config index back to the original.
3533   stream_->UpdateVideoConfig(new_config);
3534   stream_->UpdateVideoConfig(video_config_);
3535
3536   Seek(0);
3537   CheckVideoConfig(video_config_);
3538   NewSegmentAppend("0K S(3K 6C 9 10) 15");
3539
3540   CheckExpectedBuffers("0K 3K C");
3541   CheckVideoConfig(new_config);
3542   CheckExpectedBuffers("6 9 C");
3543   CheckVideoConfig(video_config_);
3544   CheckExpectedBuffers("10 15");
3545   CheckNoNextBuffer();
3546 }
3547
3548 TEST_F(SourceBufferStreamTest, SpliceFrame_BasicFromTrackBuffer) {
3549   Seek(0);
3550   NewSegmentAppend("0K 5K S(8K 9 10) 20");
3551   CheckExpectedBuffers("0K 5K");
3552
3553   // Overlap the existing segment.
3554   NewSegmentAppend("5K 20");
3555   CheckExpectedBuffers("8K 9 C 10 20");
3556   CheckNoNextBuffer();
3557 }
3558
3559 TEST_F(SourceBufferStreamTest,
3560        SpliceFrame_ConfigChangeWithinSpliceFromTrackBuffer) {
3561   VideoDecoderConfig new_config = TestVideoConfig::Large();
3562   ASSERT_FALSE(new_config.Matches(video_config_));
3563
3564   // Add a new video config, then reset the config index back to the original.
3565   stream_->UpdateVideoConfig(new_config);
3566   stream_->UpdateVideoConfig(video_config_);
3567
3568   Seek(0);
3569   CheckVideoConfig(video_config_);
3570   NewSegmentAppend("0K 5K S(7K 8C 9 10) 20");
3571   CheckExpectedBuffers("0K 5K");
3572
3573   // Overlap the existing segment.
3574   NewSegmentAppend("5K 20");
3575   CheckExpectedBuffers("7K C");
3576   CheckVideoConfig(new_config);
3577   CheckExpectedBuffers("8 9 C");
3578   CheckVideoConfig(video_config_);
3579   CheckExpectedBuffers("10 20");
3580   CheckNoNextBuffer();
3581 }
3582
3583 // TODO(vrk): Add unit tests where keyframes are unaligned between streams.
3584 // (crbug.com/133557)
3585
3586 // TODO(vrk): Add unit tests with end of stream being called at interesting
3587 // times.
3588
3589 }  // namespace media