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