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