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