Revert "[M120 Migration]Fix for crash during chrome exit"
[platform/framework/web/chromium-efl.git] / media / filters / source_buffer_stream_unittest.cc
1 // Copyright 2012 The Chromium Authors
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 <stddef.h>
8 #include <stdint.h>
9
10 #include <memory>
11 #include <string>
12 #include <vector>
13
14 #include "base/functional/bind.h"
15 #include "base/functional/callback_helpers.h"
16 #include "base/logging.h"
17 #include "base/numerics/safe_conversions.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_split.h"
20 #include "base/strings/string_util.h"
21 #include "base/test/scoped_feature_list.h"
22 #include "base/time/time.h"
23 #include "media/base/data_buffer.h"
24 #include "media/base/decoder_buffer.h"
25 #include "media/base/media_log.h"
26 #include "media/base/media_switches.h"
27 #include "media/base/media_util.h"
28 #include "media/base/mock_media_log.h"
29 #include "media/base/test_helpers.h"
30 #include "media/base/timestamp_constants.h"
31 #include "media/base/webvtt_util.h"
32 #include "media/filters/source_buffer_range.h"
33 #include "testing/gtest/include/gtest/gtest.h"
34
35 using ::testing::HasSubstr;
36 using ::testing::InSequence;
37 using ::testing::StrictMock;
38
39 namespace {
40
41 enum class TimeGranularity { kMicrosecond, kMillisecond };
42
43 }  // namespace
44
45 namespace media {
46
47 typedef StreamParser::BufferQueue BufferQueue;
48
49 static const int kDefaultFramesPerSecond = 30;
50 static const int kDefaultKeyframesPerSecond = 6;
51 static const uint8_t kDataA = 0x11;
52 static const uint8_t kDataB = 0x33;
53 static const int kDataSize = 1;
54
55 // Matchers for verifying common media log entry strings.
56 MATCHER_P(ContainsTrackBufferExhaustionSkipLog, skip_milliseconds, "") {
57   return CONTAINS_STRING(arg,
58                          "Media append that overlapped current playback "
59                          "position may cause time gap in playing VIDEO stream "
60                          "because the next keyframe is " +
61                              base::NumberToString(skip_milliseconds) +
62                              "ms beyond last overlapped frame. Media may "
63                              "appear temporarily frozen.");
64 }
65
66 #define EXPECT_STATUS_FOR_STREAM_OP(status_suffix, operation) \
67   { EXPECT_EQ(SourceBufferStreamStatus::status_suffix, stream_->operation); }
68
69 class SourceBufferStreamTest : public testing::Test {
70  public:
71   SourceBufferStreamTest(const SourceBufferStreamTest&) = delete;
72   SourceBufferStreamTest& operator=(const SourceBufferStreamTest&) = delete;
73
74  protected:
75   SourceBufferStreamTest() {
76     video_config_ = TestVideoConfig::Normal();
77     SetStreamInfo(kDefaultFramesPerSecond, kDefaultKeyframesPerSecond);
78     ResetStream<>(video_config_);
79   }
80
81   template <typename ConfigT>
82   void ResetStream(const ConfigT& config) {
83     stream_ = std::make_unique<SourceBufferStream>(config, &media_log_);
84   }
85
86   void SetMemoryLimit(size_t buffers_of_data) {
87     stream_->set_memory_limit(buffers_of_data * kDataSize);
88   }
89
90   void SetStreamInfo(int frames_per_second, int keyframes_per_second) {
91     frames_per_second_ = frames_per_second;
92     keyframes_per_second_ = keyframes_per_second;
93     frame_duration_ = ConvertToFrameDuration(frames_per_second);
94   }
95
96   void SetAudioStream() {
97     video_config_ = TestVideoConfig::Invalid();
98     audio_config_.Initialize(AudioCodec::kVorbis, kSampleFormatPlanarF32,
99                              CHANNEL_LAYOUT_STEREO, 1000, EmptyExtraData(),
100                              EncryptionScheme::kUnencrypted, base::TimeDelta(),
101                              0);
102     ResetStream<>(audio_config_);
103
104     // Equivalent to 2ms per frame.
105     SetStreamInfo(500, 500);
106   }
107
108   void NewCodedFrameGroupAppend(int starting_position, int number_of_buffers) {
109     AppendBuffers(starting_position, number_of_buffers, true, base::TimeDelta(),
110                   &kDataA, kDataSize);
111   }
112
113   void NewCodedFrameGroupAppend(int starting_position,
114                                 int number_of_buffers,
115                                 const uint8_t* data) {
116     AppendBuffers(starting_position, number_of_buffers, true, base::TimeDelta(),
117                   data, kDataSize);
118   }
119
120   void NewCodedFrameGroupAppend_OffsetFirstBuffer(
121       int starting_position,
122       int number_of_buffers,
123       base::TimeDelta first_buffer_offset) {
124     AppendBuffers(starting_position, number_of_buffers, true,
125                   first_buffer_offset, &kDataA, kDataSize);
126   }
127
128   void AppendBuffers(int starting_position, int number_of_buffers) {
129     AppendBuffers(starting_position, number_of_buffers, false,
130                   base::TimeDelta(), &kDataA, kDataSize);
131   }
132
133   void AppendBuffers(int starting_position,
134                      int number_of_buffers,
135                      const uint8_t* data) {
136     AppendBuffers(starting_position, number_of_buffers, false,
137                   base::TimeDelta(), data, kDataSize);
138   }
139
140   void NewCodedFrameGroupAppend(const std::string& buffers_to_append) {
141     AppendBuffers(buffers_to_append, true, kNoTimestamp, false);
142   }
143
144   void NewCodedFrameGroupAppend(base::TimeDelta start_timestamp,
145                                 const std::string& buffers_to_append) {
146     AppendBuffers(buffers_to_append, true, start_timestamp, false);
147   }
148
149   void AppendBuffers(const std::string& buffers_to_append) {
150     AppendBuffers(buffers_to_append, false, kNoTimestamp, false);
151   }
152
153   void NewCodedFrameGroupAppendOneByOne(const std::string& buffers_to_append) {
154     AppendBuffers(buffers_to_append, true, kNoTimestamp, true);
155   }
156
157   void AppendBuffersOneByOne(const std::string& buffers_to_append) {
158     AppendBuffers(buffers_to_append, false, kNoTimestamp, true);
159   }
160
161   void Seek(int position) { stream_->Seek(position * frame_duration_); }
162
163   void SeekToTimestampMs(int64_t timestamp_ms) {
164     stream_->Seek(base::Milliseconds(timestamp_ms));
165   }
166
167   bool GarbageCollect(base::TimeDelta media_time, int new_data_size) {
168     return stream_->GarbageCollectIfNeeded(media_time, new_data_size);
169   }
170
171   bool GarbageCollectWithPlaybackAtBuffer(int position, int new_data_buffers) {
172     return GarbageCollect(position * frame_duration_,
173                           new_data_buffers * kDataSize);
174   }
175
176   void RemoveInMs(int start, int end, int duration) {
177     Remove(base::Milliseconds(start), base::Milliseconds(end),
178            base::Milliseconds(duration));
179   }
180
181   void Remove(base::TimeDelta start, base::TimeDelta end,
182               base::TimeDelta duration) {
183     stream_->Remove(start, end, duration);
184   }
185
186   void SignalStartOfCodedFrameGroup(base::TimeDelta start_timestamp) {
187     stream_->OnStartOfCodedFrameGroup(start_timestamp);
188   }
189
190   int GetRemovalRangeInMs(int start, int end, int bytes_to_free,
191                           int* removal_end) {
192     base::TimeDelta removal_end_timestamp = base::Milliseconds(*removal_end);
193     int bytes_removed = stream_->GetRemovalRange(
194         base::Milliseconds(start), base::Milliseconds(end), bytes_to_free,
195         &removal_end_timestamp);
196     *removal_end = removal_end_timestamp.InMilliseconds();
197     return bytes_removed;
198   }
199
200   void CheckExpectedRanges(const std::string& expected) {
201     Ranges<base::TimeDelta> r = stream_->GetBufferedTime();
202
203     std::stringstream ss;
204     ss << "{ ";
205     for (size_t i = 0; i < r.size(); ++i) {
206       int64_t start = r.start(i).IntDiv(frame_duration_);
207       int64_t end = r.end(i).IntDiv(frame_duration_) - 1;
208       ss << "[" << start << "," << end << ") ";
209     }
210     ss << "}";
211     EXPECT_EQ(expected, ss.str());
212   }
213
214   void CheckExpectedRangesByTimestamp(
215       const std::string& expected,
216       TimeGranularity granularity = TimeGranularity::kMillisecond) {
217     Ranges<base::TimeDelta> r = stream_->GetBufferedTime();
218
219     std::stringstream ss;
220     ss << "{ ";
221     for (size_t i = 0; i < r.size(); ++i) {
222       auto conversion = (granularity == TimeGranularity::kMillisecond)
223                             ? &base::TimeDelta::InMilliseconds
224                             : &base::TimeDelta::InMicroseconds;
225       int64_t start = (r.start(i).*conversion)();
226       int64_t end = (r.end(i).*conversion)();
227       ss << "[" << start << "," << end << ") ";
228     }
229     ss << "}";
230     EXPECT_EQ(expected, ss.str());
231   }
232
233   void CheckExpectedRangeEndTimes(const std::string& expected) {
234     std::stringstream ss;
235     ss << "{ ";
236     for (const auto& r : stream_->ranges_) {
237       base::TimeDelta highest_pts = r->GetEndTimestamp();
238       base::TimeDelta end_time = r->GetBufferedEndTimestamp();
239       ss << "<" << highest_pts.InMilliseconds() << ","
240          << end_time.InMilliseconds() << "> ";
241     }
242     ss << "}";
243     EXPECT_EQ(expected, ss.str());
244   }
245
246   void CheckIsNextInPTSSequenceWithFirstRange(int64_t pts_in_ms,
247                                               bool expectation) {
248     ASSERT_GE(stream_->ranges_.size(), 1u);
249     const auto& range_ptr = *(stream_->ranges_.begin());
250     EXPECT_EQ(expectation, range_ptr->IsNextInPresentationSequence(
251                                base::Milliseconds(pts_in_ms)));
252   }
253
254   void CheckExpectedBuffers(
255       int starting_position, int ending_position) {
256     CheckExpectedBuffers(starting_position, ending_position, false, NULL, 0);
257   }
258
259   void CheckExpectedBuffers(
260       int starting_position, int ending_position, bool expect_keyframe) {
261     CheckExpectedBuffers(starting_position, ending_position, expect_keyframe,
262                          NULL, 0);
263   }
264
265   void CheckExpectedBuffers(int starting_position,
266                             int ending_position,
267                             const uint8_t* data) {
268     CheckExpectedBuffers(starting_position, ending_position, false, data,
269                          kDataSize);
270   }
271
272   void CheckExpectedBuffers(int starting_position,
273                             int ending_position,
274                             const uint8_t* data,
275                             bool expect_keyframe) {
276     CheckExpectedBuffers(starting_position, ending_position, expect_keyframe,
277                          data, kDataSize);
278   }
279
280   void CheckExpectedBuffers(int starting_position,
281                             int ending_position,
282                             bool expect_keyframe,
283                             const uint8_t* expected_data,
284                             int expected_size) {
285     int current_position = starting_position;
286     for (; current_position <= ending_position; current_position++) {
287       scoped_refptr<StreamParserBuffer> buffer;
288       SourceBufferStreamStatus status = stream_->GetNextBuffer(&buffer);
289       EXPECT_NE(status, SourceBufferStreamStatus::kConfigChange);
290       if (status != SourceBufferStreamStatus::kSuccess)
291         break;
292
293       if (expect_keyframe && current_position == starting_position)
294         EXPECT_TRUE(buffer->is_key_frame());
295
296       if (expected_data) {
297         const uint8_t* actual_data = buffer->data();
298         const int actual_size = buffer->data_size();
299         EXPECT_EQ(expected_size, actual_size);
300         for (int i = 0; i < std::min(actual_size, expected_size); i++) {
301           EXPECT_EQ(expected_data[i], actual_data[i]);
302         }
303       }
304
305       EXPECT_EQ(
306           base::ClampFloor(buffer->GetDecodeTimestamp() / frame_duration_),
307           current_position);
308     }
309
310     EXPECT_EQ(ending_position + 1, current_position);
311   }
312
313   void CheckExpectedBuffers(
314       const std::string& expected,
315       TimeGranularity granularity = TimeGranularity::kMillisecond) {
316     std::vector<std::string> timestamps = base::SplitString(
317         expected, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
318     std::stringstream ss;
319     const SourceBufferStreamType type = stream_->GetType();
320     for (size_t i = 0; i < timestamps.size(); i++) {
321       scoped_refptr<StreamParserBuffer> buffer;
322       SourceBufferStreamStatus status = stream_->GetNextBuffer(&buffer);
323
324       if (i > 0)
325         ss << " ";
326
327       if (status == SourceBufferStreamStatus::kConfigChange) {
328         switch (type) {
329           case SourceBufferStreamType::kVideo:
330             stream_->GetCurrentVideoDecoderConfig();
331             break;
332           case SourceBufferStreamType::kAudio:
333             stream_->GetCurrentAudioDecoderConfig();
334             break;
335         }
336
337         EXPECT_EQ("C", timestamps[i]);
338         ss << "C";
339         continue;
340       }
341
342       EXPECT_EQ(SourceBufferStreamStatus::kSuccess, status);
343       if (status != SourceBufferStreamStatus::kSuccess)
344         break;
345
346       if (granularity == TimeGranularity::kMillisecond)
347         ss << buffer->timestamp().InMilliseconds();
348       else
349         ss << buffer->timestamp().InMicroseconds();
350
351       if (buffer->GetDecodeTimestamp() !=
352           DecodeTimestamp::FromPresentationTime(buffer->timestamp())) {
353         if (granularity == TimeGranularity::kMillisecond)
354           ss << "|" << buffer->GetDecodeTimestamp().InMilliseconds();
355         else
356           ss << "|" << buffer->GetDecodeTimestamp().InMicroseconds();
357       }
358
359       // Check duration if expected timestamp contains it.
360       if (timestamps[i].find('D') != std::string::npos) {
361         ss << "D" << buffer->duration().InMilliseconds();
362       }
363
364       // Check duration estimation if expected timestamp contains it.
365       if (timestamps[i].find('E') != std::string::npos &&
366           buffer->is_duration_estimated()) {
367         ss << "E";
368       }
369
370       // Handle preroll buffers.
371       if (base::EndsWith(timestamps[i], "P", base::CompareCase::SENSITIVE)) {
372         ASSERT_TRUE(buffer->is_key_frame());
373         scoped_refptr<StreamParserBuffer> preroll_buffer;
374         preroll_buffer.swap(buffer);
375
376         // When a preroll buffer is encountered we should be able to request one
377         // more buffer.  The first buffer should match the timestamp and config
378         // of the second buffer, except that its discard_padding() should be its
379         // duration.
380         EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
381         ASSERT_EQ(buffer->GetConfigId(), preroll_buffer->GetConfigId());
382         ASSERT_EQ(buffer->track_id(), preroll_buffer->track_id());
383         ASSERT_EQ(buffer->timestamp(), preroll_buffer->timestamp());
384         ASSERT_EQ(buffer->GetDecodeTimestamp(),
385                   preroll_buffer->GetDecodeTimestamp());
386         ASSERT_EQ(kInfiniteDuration, preroll_buffer->discard_padding().first);
387         ASSERT_EQ(base::TimeDelta(), preroll_buffer->discard_padding().second);
388         ASSERT_TRUE(buffer->is_key_frame());
389
390         ss << "P";
391       } else if (buffer->is_key_frame()) {
392         ss << "K";
393       }
394     }
395     EXPECT_EQ(expected, ss.str());
396   }
397
398   void CheckNoNextBuffer() {
399     scoped_refptr<StreamParserBuffer> buffer;
400     EXPECT_STATUS_FOR_STREAM_OP(kNeedBuffer, GetNextBuffer(&buffer));
401   }
402
403   void CheckEOSReached() {
404     scoped_refptr<StreamParserBuffer> buffer;
405     EXPECT_STATUS_FOR_STREAM_OP(kEndOfStream, GetNextBuffer(&buffer));
406   }
407
408   void CheckVideoConfig(const VideoDecoderConfig& config) {
409     const VideoDecoderConfig& actual = stream_->GetCurrentVideoDecoderConfig();
410     EXPECT_TRUE(actual.Matches(config))
411         << "Expected: " << config.AsHumanReadableString()
412         << "\nActual: " << actual.AsHumanReadableString();
413   }
414
415   void CheckAudioConfig(const AudioDecoderConfig& config) {
416     const AudioDecoderConfig& actual = stream_->GetCurrentAudioDecoderConfig();
417     EXPECT_TRUE(actual.Matches(config))
418         << "Expected: " << config.AsHumanReadableString()
419         << "\nActual: " << actual.AsHumanReadableString();
420   }
421
422   base::TimeDelta frame_duration() const { return frame_duration_; }
423
424   StrictMock<MockMediaLog> media_log_;
425   std::unique_ptr<SourceBufferStream> stream_;
426
427   VideoDecoderConfig video_config_;
428   AudioDecoderConfig audio_config_;
429
430  private:
431   DemuxerStream::Type GetStreamType() {
432     switch (stream_->GetType()) {
433       case SourceBufferStreamType::kAudio:
434         return DemuxerStream::AUDIO;
435       case SourceBufferStreamType::kVideo:
436         return DemuxerStream::VIDEO;
437     }
438     NOTREACHED_NORETURN();
439   }
440
441   base::TimeDelta ConvertToFrameDuration(int frames_per_second) {
442     return base::Seconds(1) / frames_per_second;
443   }
444
445   void AppendBuffers(int starting_position,
446                      int number_of_buffers,
447                      bool begin_coded_frame_group,
448                      base::TimeDelta first_buffer_offset,
449                      const uint8_t* data,
450                      int size) {
451     if (begin_coded_frame_group) {
452       stream_->OnStartOfCodedFrameGroup(starting_position * frame_duration_);
453     }
454
455     int keyframe_interval = frames_per_second_ / keyframes_per_second_;
456
457     BufferQueue queue;
458     for (int i = 0; i < number_of_buffers; i++) {
459       int position = starting_position + i;
460       bool is_keyframe = position % keyframe_interval == 0;
461       // Track ID is meaningless to these tests.
462       scoped_refptr<StreamParserBuffer> buffer = StreamParserBuffer::CopyFrom(
463           data, size, is_keyframe, GetStreamType(), 0);
464       base::TimeDelta timestamp = frame_duration_ * position;
465
466       if (i == 0)
467         timestamp += first_buffer_offset;
468       buffer->SetDecodeTimestamp(
469           DecodeTimestamp::FromPresentationTime(timestamp));
470
471       // Simulate an IBB...BBP pattern in which all B-frames reference both
472       // the I- and P-frames. For a GOP with playback order 12345, this would
473       // result in a decode timestamp order of 15234.
474       base::TimeDelta presentation_timestamp;
475       if (is_keyframe) {
476         presentation_timestamp = timestamp;
477       } else if ((position - 1) % keyframe_interval == 0) {
478         // This is the P-frame (first frame following the I-frame)
479         presentation_timestamp =
480             (timestamp + frame_duration_ * (keyframe_interval - 2));
481       } else {
482         presentation_timestamp = timestamp - frame_duration_;
483       }
484       buffer->set_timestamp(presentation_timestamp);
485       buffer->set_duration(frame_duration_);
486
487       queue.push_back(buffer);
488     }
489     if (!queue.empty())
490       stream_->Append(queue);
491   }
492
493   void UpdateLastBufferDuration(DecodeTimestamp current_dts,
494                                 BufferQueue* buffers) {
495     if (buffers->empty() || buffers->back()->duration().is_positive())
496       return;
497
498     DecodeTimestamp last_dts = buffers->back()->GetDecodeTimestamp();
499     DCHECK(current_dts >= last_dts);
500     buffers->back()->set_duration(current_dts - last_dts);
501   }
502
503   // StringToBufferQueue() allows for the generation of StreamParserBuffers from
504   // coded strings of timestamps separated by spaces.
505   //
506   // Supported syntax (options must be in this order):
507   // pp[u][|dd[u]][Dzz][E][P][K]
508   //
509   // pp:
510   // Generates a StreamParserBuffer with decode and presentation timestamp xx.
511   // E.g., "0 1 2 3".
512   // pp is interpreted as milliseconds, unless suffixed with "u", in which case
513   // pp is interpreted as microseconds.
514   //
515   // pp|dd:
516   // Generates a StreamParserBuffer with presentation timestamp pp and decode
517   // timestamp dd. E.g., "0|0 3|1 1|2 2|3". dd is interpreted as milliseconds,
518   // unless suffixed with "u", in which case dd is interpreted as microseconds.
519   //
520   // Dzz[u]
521   // Explicitly describe the duration of the buffer. zz specifies the duration
522   // in milliseconds (or in microseconds if suffixed with "u"). If the duration
523   // isn't specified with this syntax, the duration is derived using the
524   // timestamp delta between this buffer and the next buffer. If not specified,
525   // the final buffer will simply copy the duration of the previous buffer. If
526   // the queue only contains 1 buffer then the duration must be explicitly
527   // specified with this format.
528   // E.g. "0D10 10D20"
529   //
530   // E:
531   // Indicates that the buffer should be marked as containing an *estimated*
532   // duration. E.g., "0D20E 20 25E 30"
533   //
534   // P:
535   // Indicates the buffer with will also have a preroll buffer
536   // associated with it. The preroll buffer will just be dummy data.
537   // E.g. "0P 5 10"
538   //
539   // K:
540   // Indicates the buffer is a keyframe. E.g., "0K 1|2K 2|4D2K 6 8".
541   BufferQueue StringToBufferQueue(const std::string& buffers_to_append) {
542     std::vector<std::string> timestamps = base::SplitString(
543         buffers_to_append, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
544
545     CHECK_GT(timestamps.size(), 0u);
546
547     BufferQueue buffers;
548     for (size_t i = 0; i < timestamps.size(); i++) {
549       bool is_keyframe = false;
550       bool has_preroll = false;
551       bool is_duration_estimated = false;
552
553       if (base::EndsWith(timestamps[i], "K", base::CompareCase::SENSITIVE)) {
554         is_keyframe = true;
555         // Remove the "K" off of the token.
556         timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
557       }
558       // Handle preroll buffers.
559       if (base::EndsWith(timestamps[i], "P", base::CompareCase::SENSITIVE)) {
560         is_keyframe = true;
561         has_preroll = true;
562         // Remove the "P" off of the token.
563         timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
564       }
565
566       if (base::EndsWith(timestamps[i], "E", base::CompareCase::SENSITIVE)) {
567         is_duration_estimated = true;
568         // Remove the "E" off of the token.
569         timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
570       }
571
572       int duration_in_us = -1;
573       size_t duration_pos = timestamps[i].find('D');
574       if (duration_pos != std::string::npos) {
575         bool is_duration_us = false;  // Default to millisecond interpretation.
576         if (base::EndsWith(timestamps[i], "u", base::CompareCase::SENSITIVE)) {
577           is_duration_us = true;
578           timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
579         }
580         CHECK(base::StringToInt(timestamps[i].substr(duration_pos + 1),
581                                 &duration_in_us));
582         if (!is_duration_us)
583           duration_in_us *= base::Time::kMicrosecondsPerMillisecond;
584         timestamps[i] = timestamps[i].substr(0, duration_pos);
585       }
586
587       std::vector<std::string> buffer_timestamp_strings = base::SplitString(
588           timestamps[i], "|", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
589
590       if (buffer_timestamp_strings.size() == 1)
591         buffer_timestamp_strings.push_back(buffer_timestamp_strings[0]);
592
593       CHECK_EQ(2u, buffer_timestamp_strings.size());
594
595       std::vector<base::TimeDelta> buffer_timestamps;
596
597       // Parse PTS, then DTS into TimeDeltas.
598       for (size_t j = 0; j < 2; ++j) {
599         int us = 0;
600         bool is_us = false;  // Default to millisecond interpretation.
601
602         if (base::EndsWith(buffer_timestamp_strings[j], "u",
603                            base::CompareCase::SENSITIVE)) {
604           is_us = true;
605           buffer_timestamp_strings[j] = buffer_timestamp_strings[j].substr(
606               0, buffer_timestamp_strings[j].length() - 1);
607         }
608         CHECK(base::StringToInt(buffer_timestamp_strings[j], &us));
609         if (!is_us)
610           us *= base::Time::kMicrosecondsPerMillisecond;
611
612         buffer_timestamps.push_back(base::Microseconds(us));
613       }
614
615       // Create buffer. Track ID is meaningless to these tests
616       scoped_refptr<StreamParserBuffer> buffer = StreamParserBuffer::CopyFrom(
617           &kDataA, kDataSize, is_keyframe, GetStreamType(), 0);
618       buffer->set_timestamp(buffer_timestamps[0]);
619       if (is_duration_estimated)
620         buffer->set_is_duration_estimated(true);
621
622       if (buffer_timestamps[1] != buffer_timestamps[0]) {
623         buffer->SetDecodeTimestamp(
624             DecodeTimestamp::FromPresentationTime(buffer_timestamps[1]));
625       }
626
627       if (duration_in_us >= 0)
628         buffer->set_duration(base::Microseconds(duration_in_us));
629
630       // Simulate preroll buffers by just generating another buffer and sticking
631       // it as the preroll.
632       if (has_preroll) {
633         scoped_refptr<StreamParserBuffer> preroll_buffer =
634             StreamParserBuffer::CopyFrom(&kDataA, kDataSize, is_keyframe,
635                                          GetStreamType(), 0);
636         preroll_buffer->set_duration(frame_duration_);
637         buffer->SetPrerollBuffer(preroll_buffer);
638       }
639
640       UpdateLastBufferDuration(buffer->GetDecodeTimestamp(), &buffers);
641       buffers.push_back(buffer);
642     }
643
644     // If the last buffer doesn't have a duration, assume it is the
645     // same as the second to last buffer.
646     if (buffers.size() >= 2 && buffers.back()->duration() == kNoTimestamp) {
647       buffers.back()->set_duration(
648           buffers[buffers.size() - 2]->duration());
649     }
650
651     return buffers;
652   }
653
654   void AppendBuffers(const std::string& buffers_to_append,
655                      bool start_new_coded_frame_group,
656                      base::TimeDelta coded_frame_group_start_timestamp,
657                      bool one_by_one) {
658     BufferQueue buffers = StringToBufferQueue(buffers_to_append);
659
660     if (start_new_coded_frame_group) {
661       base::TimeDelta start_timestamp = coded_frame_group_start_timestamp;
662
663       base::TimeDelta buffers_start_timestamp = buffers[0]->timestamp();
664
665       if (start_timestamp == kNoTimestamp)
666         start_timestamp = buffers_start_timestamp;
667       else
668         ASSERT_TRUE(start_timestamp <= buffers_start_timestamp);
669
670       stream_->OnStartOfCodedFrameGroup(start_timestamp);
671     }
672
673     if (!one_by_one) {
674       stream_->Append(buffers);
675       return;
676     }
677
678     // Append each buffer one by one.
679     for (size_t i = 0; i < buffers.size(); i++) {
680       BufferQueue wrapper;
681       wrapper.push_back(buffers[i]);
682       stream_->Append(wrapper);
683     }
684   }
685
686   int frames_per_second_;
687   int keyframes_per_second_;
688   base::TimeDelta frame_duration_;
689 };
690
691 TEST_F(SourceBufferStreamTest, Append_SingleRange) {
692   // Append 15 buffers at positions 0 through 14.
693   NewCodedFrameGroupAppend(0, 15);
694
695   // Check expected range.
696   CheckExpectedRanges("{ [0,14) }");
697   // Check buffers in range.
698   Seek(0);
699   CheckExpectedBuffers(0, 14);
700 }
701
702 TEST_F(SourceBufferStreamTest, Append_SingleRange_OneBufferAtATime) {
703   // Append 15 buffers starting at position 0, one buffer at a time.
704   NewCodedFrameGroupAppend(0, 1);
705   for (int i = 1; i < 15; i++)
706     AppendBuffers(i, 1);
707
708   // Check expected range.
709   CheckExpectedRanges("{ [0,14) }");
710   // Check buffers in range.
711   Seek(0);
712   CheckExpectedBuffers(0, 14);
713 }
714
715 TEST_F(SourceBufferStreamTest, Append_DisjointRanges) {
716   // Append 5 buffers at positions 0 through 4.
717   NewCodedFrameGroupAppend(0, 5);
718
719   // Append 10 buffers at positions 15 through 24.
720   NewCodedFrameGroupAppend(15, 10);
721
722   // Check expected ranges.
723   CheckExpectedRanges("{ [0,4) [15,24) }");
724   // Check buffers in ranges.
725   Seek(0);
726   CheckExpectedBuffers(0, 4);
727   Seek(15);
728   CheckExpectedBuffers(15, 24);
729 }
730
731 TEST_F(SourceBufferStreamTest, Append_AdjacentRanges) {
732   // Append 10 buffers at positions 0 through 9.
733   NewCodedFrameGroupAppend(0, 10);
734
735   // Append 11 buffers at positions 15 through 25.
736   NewCodedFrameGroupAppend(15, 11);
737
738   // Append 5 buffers at positions 10 through 14 to bridge the gap.
739   NewCodedFrameGroupAppend(10, 5);
740
741   // Check expected range.
742   CheckExpectedRanges("{ [0,25) }");
743   // Check buffers in range.
744   Seek(0);
745   CheckExpectedBuffers(0, 25);
746 }
747
748 TEST_F(SourceBufferStreamTest, Complete_Overlap) {
749   // Append 5 buffers at positions 5 through 9.
750   NewCodedFrameGroupAppend(5, 5);
751
752   // Append 15 buffers at positions 0 through 14.
753   NewCodedFrameGroupAppend(0, 15);
754
755   // Check expected range.
756   CheckExpectedRanges("{ [0,14) }");
757   // Check buffers in range.
758   Seek(0);
759   CheckExpectedBuffers(0, 14);
760 }
761
762 TEST_F(SourceBufferStreamTest,
763        Complete_Overlap_AfterGroupTimestampAndBeforeFirstBufferTimestamp) {
764   // Append a coded frame group with a start timestamp of 0, but the first
765   // buffer starts at 30ms. This can happen in muxed content where the
766   // audio starts before the first frame.
767   NewCodedFrameGroupAppend(base::Milliseconds(0), "30K 60K 90K 120K");
768
769   CheckExpectedRangesByTimestamp("{ [0,150) }");
770
771   // Completely overlap the old buffers, with a coded frame group that starts
772   // after the old coded frame group start timestamp, but before the timestamp
773   // of the first buffer in the coded frame group.
774   NewCodedFrameGroupAppend("20K 50K 80K 110D10K");
775
776   // Verify that the buffered ranges are updated properly and we don't crash.
777   CheckExpectedRangesByTimestamp("{ [0,150) }");
778
779   SeekToTimestampMs(0);
780   CheckExpectedBuffers("20K 50K 80K 110K 120K");
781 }
782
783 TEST_F(SourceBufferStreamTest, Complete_Overlap_EdgeCase) {
784   // Make each frame a keyframe so that it's okay to overlap frames at any point
785   // (instead of needing to respect keyframe boundaries).
786   SetStreamInfo(30, 30);
787
788   // Append 6 buffers at positions 6 through 11.
789   NewCodedFrameGroupAppend(6, 6);
790
791   // Append 8 buffers at positions 5 through 12.
792   NewCodedFrameGroupAppend(5, 8);
793
794   // Check expected range.
795   CheckExpectedRanges("{ [5,12) }");
796   // Check buffers in range.
797   Seek(5);
798   CheckExpectedBuffers(5, 12);
799 }
800
801 TEST_F(SourceBufferStreamTest, Start_Overlap) {
802   // Append 10 buffers at positions 5 through 14.
803   NewCodedFrameGroupAppend(5, 5);
804
805   // Append 6 buffers at positions 10 through 15.
806   NewCodedFrameGroupAppend(10, 6);
807
808   // Check expected range.
809   CheckExpectedRanges("{ [5,15) }");
810   // Check buffers in range.
811   Seek(5);
812   CheckExpectedBuffers(5, 15);
813 }
814
815 TEST_F(SourceBufferStreamTest, End_Overlap) {
816   // Append 10 buffers at positions 10 through 19.
817   NewCodedFrameGroupAppend(10, 10);
818
819   // Append 10 buffers at positions 5 through 14.
820   NewCodedFrameGroupAppend(5, 10);
821
822   // Check expected range.
823   CheckExpectedRanges("{ [5,19) }");
824   // Check buffers in range.
825   Seek(5);
826   CheckExpectedBuffers(5, 19);
827 }
828
829 // Using position based test API:
830 // DTS  :  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
831 // PTS  :  0  4  1  2  3  5  9  6  7  8 10 14 11 12 13 15 19 16 17 18 20
832 // old Â :                                A  a  a  a  a  A  a  a  a  a
833 // new  :                 B  b  b  b  b  B  b  b
834 // after:                 B  b  b  b  b  B  b  b        A  a  a  a  a
835 TEST_F(SourceBufferStreamTest, End_Overlap_Several) {
836   // Append 10 buffers at positions 10 through 19 (DTS and PTS).
837   NewCodedFrameGroupAppend(10, 10, &kDataA);
838
839   // Append 8 buffers at positions 5 through 12 (DTS); 5 through 14 (PTS) with
840   // partial second GOP.
841   NewCodedFrameGroupAppend(5, 8, &kDataB);
842
843   // Check expected ranges: stream should not have kept buffers at DTS 13,14;
844   // PTS 12,13 because the keyframe on which they depended (10, PTS=DTS) was
845   // overwritten. Note that partial second GOP of B includes PTS [10,14), DTS
846   // [10,12). These are continuous with the overlapped original range's next GOP
847   // at (15, PTS=DTS).
848   // Unlike the rest of the position based test API used in this case, these
849   // range expectation strings are the actual timestamps (divided by
850   // frame_duration_).
851   CheckExpectedRanges("{ [5,19) }");
852
853   // Check buffers in range.
854   Seek(5);
855   CheckExpectedBuffers(5, 12, &kDataB);
856   // No seek is necessary (1 continuous range).
857   CheckExpectedBuffers(15, 19, &kDataA);
858   CheckNoNextBuffer();
859 }
860
861 // Test an end overlap edge case where a single buffer overlaps the
862 // beginning of a range.
863 // old  : 0K   30   60   90   120K  150
864 // new  : 0K
865 // after: 0K                  120K  150
866 // track:
867 TEST_F(SourceBufferStreamTest, End_Overlap_SingleBuffer) {
868   // Seek to start of stream.
869   SeekToTimestampMs(0);
870
871   NewCodedFrameGroupAppend("0K 30 60 90 120K 150");
872   CheckExpectedRangesByTimestamp("{ [0,180) }");
873
874   NewCodedFrameGroupAppend("0D30K");
875   CheckExpectedRangesByTimestamp("{ [0,180) }");
876
877   CheckExpectedBuffers("0K 120K 150");
878   CheckNoNextBuffer();
879 }
880
881 // Using position based test API:
882 // DTS  :  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
883 // PTS  :  0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0
884 // old Â :            A a                 A a                 A a
885 // new  :  B b b b b B b b b b B b b b b B b b b b B b b b b B b b b b
886 // after == new
887 TEST_F(SourceBufferStreamTest, Complete_Overlap_Several) {
888   // Append 2 buffers at positions 5 through 6 (DTS); 5 through 9 (PTS) partial
889   // GOP.
890   NewCodedFrameGroupAppend(5, 2, &kDataA);
891
892   // Append 2 buffers at positions 15 through 16 (DTS); 15 through 19 (PTS)
893   // partial GOP.
894   NewCodedFrameGroupAppend(15, 2, &kDataA);
895
896   // Append 2 buffers at positions 25 through 26 (DTS); 25 through 29 (PTS)
897   // partial GOP.
898   NewCodedFrameGroupAppend(25, 2, &kDataA);
899
900   // Check expected ranges.  Unlike the rest of the position based test API used
901   // in this case, these range expectation strings are the actual timestamps
902   // (divided by frame_duration_).
903   CheckExpectedRanges("{ [5,9) [15,19) [25,29) }");
904
905   // Append buffers at positions 0 through 29 (DTS and PTS).
906   NewCodedFrameGroupAppend(0, 30, &kDataB);
907
908   // Check expected range.
909   CheckExpectedRanges("{ [0,29) }");
910   // Check buffers in range.
911   Seek(0);
912   CheckExpectedBuffers(0, 29, &kDataB);
913 }
914
915 // Using position based test API:
916 // DTS:0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
917 // PTS:0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9
918 // old:          A a                 A a                 A a                 A a
919 // new:B b b b b B b b b b B b b b b B b b b b B b b b b B b b b b B b b b b
920 TEST_F(SourceBufferStreamTest, Complete_Overlap_Several_Then_Merge) {
921   // Append 2 buffers at positions 5 through 6 (DTS); 5 through 9 (PTS) partial
922   // GOP.
923   NewCodedFrameGroupAppend(5, 2, &kDataA);
924
925   // Append 2 buffers at positions 10 through 11 (DTS); 15 through 19 (PTS)
926   // partial GOP.
927   NewCodedFrameGroupAppend(15, 2, &kDataA);
928
929   // Append 2 buffers at positions 25 through 26 (DTS); 25 through 29 (PTS)
930   // partial GOP.
931   NewCodedFrameGroupAppend(25, 2, &kDataA);
932
933   // Append 2 buffers at positions 35 through 36 (DTS); 35 through 39 (PTS)
934   // partial GOP.
935   NewCodedFrameGroupAppend(35, 2, &kDataA);
936
937   // Append buffers at positions 0 through 34 (DTS and PTS).
938   NewCodedFrameGroupAppend(0, 35, &kDataB);
939
940   // Check expected ranges.  Unlike the rest of the position based test API used
941   // in this case, these range expectation strings are the actual timestamps
942   // (divided by frame_duration_).
943   CheckExpectedRanges("{ [0,39) }");
944
945   // Check buffers in range.
946   Seek(0);
947   CheckExpectedBuffers(0, 34, &kDataB);
948   CheckExpectedBuffers(35, 36, &kDataA);
949 }
950
951 TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected) {
952   // Append 10 buffers at positions 5 through 14.
953   NewCodedFrameGroupAppend(5, 10, &kDataA);
954
955   // Seek to buffer at position 5.
956   Seek(5);
957
958   // Replace old data with new data.
959   NewCodedFrameGroupAppend(5, 10, &kDataB);
960
961   // Check ranges are correct.
962   CheckExpectedRanges("{ [5,14) }");
963
964   // Check that data has been replaced with new data.
965   CheckExpectedBuffers(5, 14, &kDataB);
966 }
967
968 // This test is testing that a client can append data to SourceBufferStream that
969 // overlaps the range from which the client is currently grabbing buffers. We
970 // would expect that the SourceBufferStream would return old data until it hits
971 // the keyframe of the new data, after which it will return the new data.
972 TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected_TrackBuffer) {
973   // Append 10 buffers at positions 5 through 14.
974   NewCodedFrameGroupAppend(5, 10, &kDataA);
975
976   // Seek to buffer at position 5 and get next buffer.
977   Seek(5);
978   CheckExpectedBuffers(5, 5, &kDataA);
979
980   // Do a complete overlap by appending 20 buffers at positions 0 through 19.
981   NewCodedFrameGroupAppend(0, 20, &kDataB);
982
983   // Check range is correct.
984   CheckExpectedRanges("{ [0,19) }");
985
986   // Expect old data up until next keyframe in new data.
987   CheckExpectedBuffers(6, 9, &kDataA);
988   CheckExpectedBuffers(10, 10, &kDataB, true);
989
990   // Expect rest of data to be new.
991   CheckExpectedBuffers(11, 19, &kDataB);
992
993   // Seek back to beginning; all data should be new.
994   Seek(0);
995   CheckExpectedBuffers(0, 19, &kDataB);
996
997   // Check range continues to be correct.
998   CheckExpectedRanges("{ [0,19) }");
999 }
1000
1001 TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected_EdgeCase) {
1002   // Append 10 buffers at positions 5 through 14.
1003   NewCodedFrameGroupAppend(5, 10, &kDataA);
1004
1005   // Seek to buffer at position 5 and get next buffer.
1006   Seek(5);
1007   CheckExpectedBuffers(5, 5, &kDataA);
1008
1009   // Replace existing data with new data.
1010   NewCodedFrameGroupAppend(5, 10, &kDataB);
1011
1012   // Check ranges are correct.
1013   CheckExpectedRanges("{ [5,14) }");
1014
1015   // Expect old data up until next keyframe in new data.
1016   CheckExpectedBuffers(6, 9, &kDataA);
1017   CheckExpectedBuffers(10, 10, &kDataB, true);
1018
1019   // Expect rest of data to be new.
1020   CheckExpectedBuffers(11, 14, &kDataB);
1021
1022   // Seek back to beginning; all data should be new.
1023   Seek(5);
1024   CheckExpectedBuffers(5, 14, &kDataB);
1025
1026   // Check range continues to be correct.
1027   CheckExpectedRanges("{ [5,14) }");
1028 }
1029
1030 TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected_Multiple) {
1031   static const uint8_t kDataC = 0x55;
1032   static const uint8_t kDataD = 0x77;
1033
1034   // Append 5 buffers at positions 5 through 9.
1035   NewCodedFrameGroupAppend(5, 5, &kDataA);
1036
1037   // Seek to buffer at position 5 and get next buffer.
1038   Seek(5);
1039   CheckExpectedBuffers(5, 5, &kDataA);
1040
1041   // Replace existing data with new data.
1042   NewCodedFrameGroupAppend(5, 5, &kDataB);
1043
1044   // Then replace it again with different data.
1045   NewCodedFrameGroupAppend(5, 5, &kDataC);
1046
1047   // Now append 5 new buffers at positions 10 through 14.
1048   NewCodedFrameGroupAppend(10, 5, &kDataC);
1049
1050   // Now replace all the data entirely.
1051   NewCodedFrameGroupAppend(5, 10, &kDataD);
1052
1053   // Expect buffers 6 through 9 to be DataA, and the remaining
1054   // buffers to be kDataD.
1055   CheckExpectedBuffers(6, 9, &kDataA);
1056   CheckExpectedBuffers(10, 14, &kDataD);
1057
1058   // At this point we cannot fulfill request.
1059   CheckNoNextBuffer();
1060
1061   // Seek back to beginning; all data should be new.
1062   Seek(5);
1063   CheckExpectedBuffers(5, 14, &kDataD);
1064 }
1065
1066 TEST_F(SourceBufferStreamTest, Start_Overlap_Selected) {
1067   // Append 10 buffers at positions 0 through 9.
1068   NewCodedFrameGroupAppend(0, 10, &kDataA);
1069
1070   // Seek to position 5, then add buffers to overlap data at that position.
1071   Seek(5);
1072   NewCodedFrameGroupAppend(5, 10, &kDataB);
1073
1074   // Check expected range.
1075   CheckExpectedRanges("{ [0,14) }");
1076
1077   // Because we seeked to a keyframe, the next buffers should all be new data.
1078   CheckExpectedBuffers(5, 14, &kDataB);
1079
1080   // Make sure all data is correct.
1081   Seek(0);
1082   CheckExpectedBuffers(0, 4, &kDataA);
1083   CheckExpectedBuffers(5, 14, &kDataB);
1084 }
1085
1086 TEST_F(SourceBufferStreamTest, Start_Overlap_Selected_TrackBuffer) {
1087   // Append 15 buffers at positions 0 through 14.
1088   NewCodedFrameGroupAppend(0, 15, &kDataA);
1089
1090   // Seek to 10 and get buffer.
1091   Seek(10);
1092   CheckExpectedBuffers(10, 10, &kDataA);
1093
1094   // Now append 10 buffers of new data at positions 10 through 19.
1095   NewCodedFrameGroupAppend(10, 10, &kDataB);
1096
1097   // Check expected range.
1098   CheckExpectedRanges("{ [0,19) }");
1099
1100   // The next 4 buffers should be a from the old buffer, followed by a keyframe
1101   // from the new data.
1102   CheckExpectedBuffers(11, 14, &kDataA);
1103   CheckExpectedBuffers(15, 15, &kDataB, true);
1104
1105   // The rest of the buffers should be new data.
1106   CheckExpectedBuffers(16, 19, &kDataB);
1107
1108   // Now seek to the beginning; positions 0 through 9 should be the original
1109   // data, positions 10 through 19 should be the new data.
1110   Seek(0);
1111   CheckExpectedBuffers(0, 9, &kDataA);
1112   CheckExpectedBuffers(10, 19, &kDataB);
1113
1114   // Make sure range is still correct.
1115   CheckExpectedRanges("{ [0,19) }");
1116 }
1117
1118 TEST_F(SourceBufferStreamTest, Start_Overlap_Selected_EdgeCase) {
1119   // Append 10 buffers at positions 5 through 14.
1120   NewCodedFrameGroupAppend(5, 10, &kDataA);
1121
1122   Seek(10);
1123   CheckExpectedBuffers(10, 10, &kDataA);
1124
1125   // Now replace the last 5 buffers with new data.
1126   NewCodedFrameGroupAppend(10, 5, &kDataB);
1127
1128   // The next 4 buffers should be the origial data, held in the track buffer.
1129   CheckExpectedBuffers(11, 14, &kDataA);
1130
1131   // The next buffer is at position 15, so we should fail to fulfill the
1132   // request.
1133   CheckNoNextBuffer();
1134
1135   // Now append data at 15 through 19 and check to make sure it's correct.
1136   NewCodedFrameGroupAppend(15, 5, &kDataB);
1137   CheckExpectedBuffers(15, 19, &kDataB);
1138
1139   // Seek to beginning of buffered range and check buffers.
1140   Seek(5);
1141   CheckExpectedBuffers(5, 9, &kDataA);
1142   CheckExpectedBuffers(10, 19, &kDataB);
1143
1144   // Check expected range.
1145   CheckExpectedRanges("{ [5,19) }");
1146 }
1147
1148 // This test covers the case where new buffers end-overlap an existing, selected
1149 // range, and the next buffer is a keyframe that's being overlapped by new
1150 // buffers.
1151 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1152 // old Â :           *A*a a a a A a a a a
1153 // new Â :  B b b b b B b b b b
1154 // after:  B b b b b*B*b b b b A a a a a
1155 TEST_F(SourceBufferStreamTest, End_Overlap_Selected) {
1156   // Append 10 buffers at positions 5 through 14.
1157   NewCodedFrameGroupAppend(5, 10, &kDataA);
1158
1159   // Seek to position 5.
1160   Seek(5);
1161
1162   // Now append 10 buffers at positions 0 through 9.
1163   NewCodedFrameGroupAppend(0, 10, &kDataB);
1164
1165   // Check expected range.
1166   CheckExpectedRanges("{ [0,14) }");
1167
1168   // Because we seeked to a keyframe, the next buffers should be new.
1169   CheckExpectedBuffers(5, 9, &kDataB);
1170
1171   // Make sure all data is correct.
1172   Seek(0);
1173   CheckExpectedBuffers(0, 9, &kDataB);
1174   CheckExpectedBuffers(10, 14, &kDataA);
1175 }
1176
1177 // This test covers the case where new buffers end-overlap an existing, selected
1178 // range, and the next buffer in the range is after the newly appended buffers.
1179 //
1180 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1181 // old Â :           |A a a a a A a a*a*a|
1182 // new Â :  B b b b b B b b b b
1183 // after: |B b b b b B b b b b A a a*a*a|
1184 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_AfterEndOfNew_1) {
1185   // Append 10 buffers at positions 5 through 14.
1186   NewCodedFrameGroupAppend(5, 10, &kDataA);
1187
1188   // Seek to position 10, then move to position 13.
1189   Seek(10);
1190   CheckExpectedBuffers(10, 12, &kDataA);
1191
1192   // Now append 10 buffers at positions 0 through 9.
1193   NewCodedFrameGroupAppend(0, 10, &kDataB);
1194
1195   // Check expected range.
1196   CheckExpectedRanges("{ [0,14) }");
1197
1198   // Make sure rest of data is as expected.
1199   CheckExpectedBuffers(13, 14, &kDataA);
1200
1201   // Make sure all data is correct.
1202   Seek(0);
1203   CheckExpectedBuffers(0, 9, &kDataB);
1204   CheckExpectedBuffers(10, 14, &kDataA);
1205 }
1206
1207 // Using position based test API:
1208 // This test covers the case where new buffers end-overlap an existing, selected
1209 // range, and the next buffer in the range is after the newly appended buffers.
1210 //
1211 // DTS  :  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1212 // PTS  :  0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0
1213 // old Â :            A a a a a A a a*a*a
1214 // new Â :  B b b b b B b b
1215 // after:  B b b b b B b b     A a a*a*a
1216 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_AfterEndOfNew_2) {
1217   // Append 10 buffers at positions 5 through 14 (DTS and PTS, 2 full GOPs)
1218   NewCodedFrameGroupAppend(5, 10, &kDataA);
1219
1220   // Seek to position 10, then move to position 13.
1221   Seek(10);
1222   CheckExpectedBuffers(10, 12, &kDataA);
1223
1224   // Now append 8 buffers at positions 0 through 7 (DTS); 0 through 9 (PTS) with
1225   // partial second GOP.
1226   NewCodedFrameGroupAppend(0, 8, &kDataB);
1227
1228   // Check expected ranges: stream should not have kept buffers at DTS 8,9;
1229   // PTS 7,8 because the keyframe on which they depended (5, PTS=DTS) was
1230   // overwritten. Note that partial second GOP of B includes PTS [5,9), DTS
1231   // [5,7). These are continuous with the overlapped original range's next GOP
1232   // at (10, PTS=DTS).
1233   // Unlike the rest of the position based test API used in this case, these
1234   // range expectation strings are the actual timestamps (divided by
1235   // frame_duration_).
1236   CheckExpectedRanges("{ [0,14) }");
1237
1238   // Make sure rest of data is as expected.
1239   CheckExpectedBuffers(13, 14, &kDataA);
1240
1241   // Make sure all data is correct.
1242   Seek(0);
1243   CheckExpectedBuffers(0, 7, &kDataB);
1244   // No seek should be necessary (1 continuous range).
1245   CheckExpectedBuffers(10, 14, &kDataA);
1246   CheckNoNextBuffer();
1247 }
1248
1249 // Using position based test API:
1250 // This test covers the case where new buffers end-overlap an existing, selected
1251 // range, and the next buffer in the range is after the newly appended buffers.
1252 //
1253 // DTS  :  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1254 // PTS  :  0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0
1255 // old Â :            A a a*a*a A a a a a
1256 // new Â :  B b b b b B b b
1257 // after:  B b b b b B b b     A a a a a
1258 // track:                  a a
1259 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_AfterEndOfNew_3) {
1260   // Append 10 buffers at positions 5 through 14 (DTS and PTS, 2 full GOPs)
1261   NewCodedFrameGroupAppend(5, 10, &kDataA);
1262
1263   // Seek to position 5, then move to position 8.
1264   Seek(5);
1265   CheckExpectedBuffers(5, 7, &kDataA);
1266
1267   // Now append 8 buffers at positions 0 through 7 (DTS); 0 through 9 (PTS) with
1268   // partial second GOP.
1269   NewCodedFrameGroupAppend(0, 8, &kDataB);
1270
1271   // Check expected ranges: stream should not have kept buffers at DTS 8,9;
1272   // PTS 7,8 because the keyframe on which they depended (5, PTS=DTS) was
1273   // overwritten. However, they were in the GOP being read from, so were put
1274   // into the track buffer. Note that partial second GOP of B includes PTS
1275   // [5,9), DTS [5,7). These are continuous with the overlapped original range's
1276   // next GOP at (10, PTS=DTS).
1277   // Unlike the rest of the position based test API used in this case, these
1278   // range expectation strings are the actual timestamps (divided by
1279   // frame_duration_).
1280   CheckExpectedRanges("{ [0,14) }");
1281
1282   // Check for data in the track buffer.
1283   CheckExpectedBuffers(8, 9, &kDataA);
1284   // The buffer immediately after the track buffer should be a keyframe.
1285   CheckExpectedBuffers(10, 10, &kDataA, true);
1286
1287   // Make sure all data is correct.
1288   Seek(0);
1289   CheckExpectedBuffers(0, 7, &kDataB);
1290   // No seek should be necessary (1 continuous range).
1291   CheckExpectedBuffers(10, 14, &kDataA);
1292   CheckNoNextBuffer();
1293 }
1294
1295 // This test covers the case where new buffers end-overlap an existing, selected
1296 // range, and the next buffer in the range is overlapped by the new buffers.
1297 //
1298 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1299 // old Â :           |A a a*a*a A a a a a|
1300 // new Â :  B b b b b B b b b b
1301 // after: |B b b b b B b b b b A a a a a|
1302 // track:                 |a a|
1303 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_OverlappedByNew_1) {
1304   // Append 10 buffers at positions 5 through 14.
1305   NewCodedFrameGroupAppend(5, 10, &kDataA);
1306
1307   // Seek to position 5, then move to position 8.
1308   Seek(5);
1309   CheckExpectedBuffers(5, 7, &kDataA);
1310
1311   // Now append 10 buffers at positions 0 through 9.
1312   NewCodedFrameGroupAppend(0, 10, &kDataB);
1313
1314   // Check expected range.
1315   CheckExpectedRanges("{ [0,14) }");
1316
1317   // Check for data in the track buffer.
1318   CheckExpectedBuffers(8, 9, &kDataA);
1319   // The buffer immediately after the track buffer should be a keyframe.
1320   CheckExpectedBuffers(10, 10, &kDataA, true);
1321
1322   // Make sure all data is correct.
1323   Seek(0);
1324   CheckExpectedBuffers(0, 9, &kDataB);
1325   CheckExpectedBuffers(10, 14, &kDataA);
1326 }
1327
1328 // Using position based test API:
1329 // This test covers the case where new buffers end-overlap an existing, selected
1330 // range, and the next buffer in the range is overlapped by the new buffers.
1331 //
1332 // DTS  :  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1333 // PTS  :  0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0
1334 // old Â :            A*a*a a a A a a a a
1335 // new Â :  B b b b b B b
1336 // after:  B b b b b B b       A a a a a
1337 // track:              a a a a
1338 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_OverlappedByNew_2) {
1339   // Append 10 buffers at positions 5 through 14 (PTS and DTS, 2 full GOPs).
1340   NewCodedFrameGroupAppend(5, 10, &kDataA);
1341
1342   // Seek to position 5, then move to position 6.
1343   Seek(5);
1344   CheckExpectedBuffers(5, 5, &kDataA);
1345
1346   // Now append 7 buffers at positions 0 through 6 (DTS); 0 through 9 (PTS) with
1347   // partial second GOP.
1348   NewCodedFrameGroupAppend(0, 7, &kDataB);
1349
1350   // Check expected ranges: stream should not have kept buffers at DTS 7,8,9;
1351   // PTS 6,7,8 because the keyframe on which they depended (5, PTS=DTS) was
1352   // overwritten. However, they were in the GOP being read from, so were put
1353   // into the track buffer. Note that partial second GOP of B includes PTS
1354   // [5,9), DTS [5,6). These are continuous with the overlapped original range's
1355   // next GOP at (10, PTS=DTS).
1356   // Unlike the rest of the position based test API used in this case, these
1357   // range expectation strings are the actual timestamps (divided by
1358   // frame_duration_).
1359   CheckExpectedRanges("{ [0,14) }");
1360
1361   // Check for data in the track buffer.
1362   CheckExpectedBuffers(6, 9, &kDataA);
1363   // The buffer immediately after the track buffer should be a keyframe.
1364   CheckExpectedBuffers(10, 10, &kDataA, true);
1365
1366   // Make sure all data is correct.
1367   Seek(0);
1368   CheckExpectedBuffers(0, 6, &kDataB);
1369   // No seek should be necessary (1 continuous range).
1370   CheckExpectedBuffers(10, 14, &kDataA);
1371   CheckNoNextBuffer();
1372 }
1373
1374 // Using position based test API:
1375 // This test covers the case where new buffers end-overlap an existing, selected
1376 // range, and the next buffer in the range is overlapped by the new buffers.
1377 // In this particular case, the next keyframe after the track buffer is in the
1378 // range with the new buffers.
1379 //
1380 // DTS  :  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1381 // PTS  :  0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0
1382 // old Â :            A*a*a a a A a a a a A a a a a
1383 // new Â :  B b b b b B b b b b B
1384 // after:  B b b b b B b b b b B         A a a a a
1385 // track:              a a a a
1386 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_OverlappedByNew_3) {
1387   // Append 15 buffers at positions 5 through 19 (PTS and DTS, 3 full GOPs).
1388   NewCodedFrameGroupAppend(5, 15, &kDataA);
1389
1390   // Seek to position 5, then move to position 6.
1391   Seek(5);
1392   CheckExpectedBuffers(5, 5, &kDataA);
1393
1394   // Now append 11 buffers at positions 0 through 10 (PTS and DTS, 2 full GOPs
1395   // and just the keyframe of a third GOP).
1396   NewCodedFrameGroupAppend(0, 11, &kDataB);
1397
1398   // Check expected ranges: stream should not have kept buffers at 11-14 (DTS
1399   // and PTS) because the keyframe on which they depended (10, PTS=DTS) was
1400   // overwritten. The GOP being read from was overwritten, so track buffer
1401   // should contain DTS 6-9 (PTS 9,6,7,8). Note that the partial third GOP of B
1402   // includes (10, PTS=DTS). This partial GOP is continuous with the overlapped
1403   // original range's next GOP at (15, PTS=DTS).
1404   // Unlike the rest of the position based test API used in this case, these
1405   // range expectation strings are the actual timestamps (divided by
1406   // frame_duration_).
1407   CheckExpectedRanges("{ [0,19) }");
1408
1409   // Check for data in the track buffer.
1410   CheckExpectedBuffers(6, 9, &kDataA);
1411   // The buffer immediately after the track buffer should be a keyframe
1412   // from the new data.
1413   CheckExpectedBuffers(10, 10, &kDataB, true);
1414
1415   // Make sure all data is correct.
1416   Seek(0);
1417   CheckExpectedBuffers(0, 10, &kDataB);
1418   // No seek should be necessary (1 continuous range).
1419   CheckExpectedBuffers(15, 19, &kDataA);
1420 }
1421
1422 // This test covers the case where new buffers end-overlap an existing, selected
1423 // range, and there is no keyframe after the end of the new buffers.
1424 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1425 // old Â :           |A*a*a a a|
1426 // new Â :  B b b b b B
1427 // after: |B b b b b B|
1428 // track:             |a a a a|
1429 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_NoKeyframeAfterNew) {
1430   // Append 5 buffers at positions 5 through 9.
1431   NewCodedFrameGroupAppend(5, 5, &kDataA);
1432
1433   // Seek to position 5, then move to position 6.
1434   Seek(5);
1435   CheckExpectedBuffers(5, 5, &kDataA);
1436
1437   // Now append 6 buffers at positions 0 through 5.
1438   NewCodedFrameGroupAppend(0, 6, &kDataB);
1439
1440   // Check expected range.
1441   CheckExpectedRanges("{ [0,5) }");
1442
1443   // Check for data in the track buffer.
1444   CheckExpectedBuffers(6, 9, &kDataA);
1445
1446   // Now there's no data to fulfill the request.
1447   CheckNoNextBuffer();
1448
1449   // Let's fill in the gap, buffers 6 through 10.
1450   AppendBuffers(6, 5, &kDataB);
1451
1452   // We should be able to get the next buffer.
1453   CheckExpectedBuffers(10, 10, &kDataB);
1454 }
1455
1456 // Using position based test API:
1457 // This test covers the case where new buffers end-overlap an existing, selected
1458 // range, and there is no keyframe after the end of the new buffers, then more
1459 // buffers end-overlap the beginning.
1460 // DTS  :  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1461 // PTS :   0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0
1462 // old Â :                      A a a a a A*a*
1463 // new Â :            B b b b b B b b b b B
1464 // after:            B b b b b B b b b b B
1465 // new Â :  A a a a a A
1466 // after:  A a a a a A         B b b b b B
1467 // track:                                  a
1468 // new  :                                B b b b b B
1469 // after:  A a a a a A         B b b b b B b b b b B
1470 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_NoKeyframeAfterNew2) {
1471   // Append 7 buffers at positions 10 through 16 (DTS); 10 through 19 (PTS) with
1472   // a partial second GOP.
1473   NewCodedFrameGroupAppend(10, 7, &kDataA);
1474
1475   // Seek to position 15, then move to position 16.
1476   Seek(15);
1477   CheckExpectedBuffers(15, 15, &kDataA);
1478
1479   // Now append 11 buffers at positions 5 through 15 (PTS and DTS), 2 full GOPs
1480   // and just the keyframe of a third GOP.
1481   NewCodedFrameGroupAppend(5, 11, &kDataB);
1482
1483   // Check expected ranges: stream should not have kept buffer at DTS 16, PTS 19
1484   // because the keyframe it depended on (15, PTS=DTS) was overwritten.
1485   // The GOP being read from was overwritten, so track buffer
1486   // should contain DTS 16, PTS 19.
1487   // Unlike the rest of the position based test API used in this case,
1488   // CheckExpectedRanges() uses expectation strings containing actual timestamps
1489   // (divided by frame_duration_).
1490   CheckExpectedRanges("{ [5,15) }");
1491
1492   // Now do another end-overlap. Append one full GOP plus keyframe of 2nd. Note
1493   // that this new keyframe at (5, PTS=DTS) is continuous with the overlapped
1494   // range's next GOP (B) at (10, PTS=DTS).
1495   NewCodedFrameGroupAppend(0, 6, &kDataA);
1496   CheckExpectedRanges("{ [0,15) }");
1497
1498   // Check for data in the track buffer.
1499   CheckExpectedBuffers(16, 16, &kDataA);
1500
1501   // Now there's no data to fulfill the request.
1502   CheckNoNextBuffer();
1503
1504   // Add data to the end of the range in the position just read from the track
1505   // buffer. The stream should not be able to fulfill the next read
1506   // until we've added a keyframe continuous beyond this point.
1507   NewCodedFrameGroupAppend(15, 1, &kDataB);
1508   CheckNoNextBuffer();
1509   for (int i = 16; i <= 19; i++) {
1510     AppendBuffers(i, 1, &kDataB);
1511     CheckNoNextBuffer();
1512   }
1513
1514   // Now append a keyframe at PTS=DTS=20.
1515   AppendBuffers(20, 1, &kDataB);
1516
1517   // The buffer at position 16 (PTS 19) in track buffer is adjacent to the next
1518   // keyframe, so no warning should be emitted on that track buffer exhaustion.
1519   // We should be able to get the next buffer (no longer from the track buffer).
1520   CheckExpectedBuffers(20, 20, &kDataB, true);
1521   CheckNoNextBuffer();
1522
1523   // Make sure all data is correct.
1524   CheckExpectedRanges("{ [0,20) }");
1525   Seek(0);
1526   CheckExpectedBuffers(0, 5, &kDataA);
1527   // No seek should be necessary (1 continuous range).
1528   CheckExpectedBuffers(10, 20, &kDataB);
1529   CheckNoNextBuffer();
1530 }
1531
1532 // This test covers the case where new buffers end-overlap an existing, selected
1533 // range, and the next keyframe in a separate range.
1534 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1535 // old Â :           |A*a*a a a|         |A a a a a|
1536 // new Â :  B b b b b B
1537 // after: |B b b b b B|                 |A a a a a|
1538 // track:             |a a a a|
1539 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_NoKeyframeAfterNew3) {
1540   // Append 5 buffers at positions 5 through 9.
1541   NewCodedFrameGroupAppend(5, 5, &kDataA);
1542
1543   // Append 5 buffers at positions 15 through 19.
1544   NewCodedFrameGroupAppend(15, 5, &kDataA);
1545
1546   // Check expected range.
1547   CheckExpectedRanges("{ [5,9) [15,19) }");
1548
1549   // Seek to position 5, then move to position 6.
1550   Seek(5);
1551   CheckExpectedBuffers(5, 5, &kDataA);
1552
1553   // Now append 6 buffers at positions 0 through 5.
1554   NewCodedFrameGroupAppend(0, 6, &kDataB);
1555
1556   // Check expected range.
1557   CheckExpectedRanges("{ [0,5) [15,19) }");
1558
1559   // Check for data in the track buffer.
1560   CheckExpectedBuffers(6, 9, &kDataA);
1561
1562   // Now there's no data to fulfill the request.
1563   CheckNoNextBuffer();
1564
1565   // Let's fill in the gap, buffers 6 through 14.
1566   AppendBuffers(6, 9, &kDataB);
1567
1568   // Check expected range.
1569   CheckExpectedRanges("{ [0,19) }");
1570
1571   // We should be able to get the next buffer.
1572   CheckExpectedBuffers(10, 14, &kDataB);
1573
1574   // We should be able to get the next buffer.
1575   CheckExpectedBuffers(15, 19, &kDataA);
1576 }
1577
1578 // This test covers the case when new buffers overlap the middle of a selected
1579 // range. This tests the case when there is precise overlap of an existing GOP,
1580 // and the next buffer is a keyframe.
1581 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1582 // old Â :  A a a a a*A*a a a a A a a a a
1583 // new Â :            B b b b b
1584 // after:  A a a a a*B*b b b b A a a a a
1585 TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_1) {
1586   // Append 15 buffers at positions 0 through 14.
1587   NewCodedFrameGroupAppend(0, 15, &kDataA);
1588
1589   // Seek to position 5.
1590   Seek(5);
1591
1592   // Now append 5 buffers at positions 5 through 9.
1593   NewCodedFrameGroupAppend(5, 5, &kDataB);
1594
1595   // Check expected range.
1596   CheckExpectedRanges("{ [0,14) }");
1597
1598   // Check for next data; should be new data.
1599   CheckExpectedBuffers(5, 9, &kDataB);
1600
1601   // Make sure all data is correct.
1602   Seek(0);
1603   CheckExpectedBuffers(0, 4, &kDataA);
1604   CheckExpectedBuffers(5, 9, &kDataB);
1605   CheckExpectedBuffers(10, 14, &kDataA);
1606   CheckNoNextBuffer();
1607 }
1608
1609 // This test covers the case when new buffers overlap the middle of a selected
1610 // range. This tests the case when there is precise overlap of an existing GOP,
1611 // and the next buffer is a non-keyframe in a GOP after the new buffers.
1612 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1613 // old Â :  A a a a a A a a a a A*a*a a a
1614 // new Â :            B b b b b
1615 // after:  A a a a a B b b b b A*a*a a a
1616 TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_2) {
1617   // Append 15 buffers at positions 0 through 14.
1618   NewCodedFrameGroupAppend(0, 15, &kDataA);
1619
1620   // Seek to 10 then move to position 11.
1621   Seek(10);
1622   CheckExpectedBuffers(10, 10, &kDataA);
1623
1624   // Now append 5 buffers at positions 5 through 9.
1625   NewCodedFrameGroupAppend(5, 5, &kDataB);
1626
1627   // Check expected range.
1628   CheckExpectedRanges("{ [0,14) }");
1629
1630   // Make sure data is correct.
1631   CheckExpectedBuffers(11, 14, &kDataA);
1632   CheckNoNextBuffer();
1633   Seek(0);
1634   CheckExpectedBuffers(0, 4, &kDataA);
1635   CheckExpectedBuffers(5, 9, &kDataB);
1636   CheckExpectedBuffers(10, 14, &kDataA);
1637   CheckNoNextBuffer();
1638 }
1639
1640 // This test covers the case when new buffers overlap the middle of a selected
1641 // range. This tests the case when only a partial GOP is appended, that append
1642 // is merged into the overlapped range, and the next buffer is before the new
1643 // buffers.
1644 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1645 // old Â :  A a*a*a a A a a a a A a a a a
1646 // new Â :            B
1647 // after:  A a*a*a a B         A a a a a
1648 TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_3) {
1649   // Append 15 buffers at positions 0 through 14.
1650   NewCodedFrameGroupAppend(0, 15, &kDataA);
1651
1652   // Seek to beginning then move to position 2.
1653   Seek(0);
1654   CheckExpectedBuffers(0, 1, &kDataA);
1655
1656   // Now append 1 buffer at position 5 (just the keyframe of a GOP).
1657   NewCodedFrameGroupAppend(5, 1, &kDataB);
1658
1659   // Check expected range.
1660   CheckExpectedRanges("{ [0,14) }");
1661
1662   // Make sure data is correct.
1663   CheckExpectedBuffers(2, 4, &kDataA);
1664   CheckExpectedBuffers(5, 5, &kDataB);
1665   // No seek should be necessary (1 continuous range).
1666   CheckExpectedBuffers(10, 14, &kDataA);
1667   CheckNoNextBuffer();
1668
1669   // Seek to the beginning and recheck data in case track buffer erroneously
1670   // became involved.
1671   Seek(0);
1672   CheckExpectedBuffers(0, 4, &kDataA);
1673   CheckExpectedBuffers(5, 5, &kDataB);
1674   CheckExpectedBuffers(10, 14, &kDataA);
1675   CheckNoNextBuffer();
1676 }
1677
1678 // This test covers the case when new buffers overlap the middle of a selected
1679 // range. This tests the case when only a partial GOP is appended, and the next
1680 // buffer is after the new buffers, and comes from the track buffer until the
1681 // next GOP in the original buffers.
1682 // index:  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1683 // old Â :  A a a a a A a a*a*a A a a a a
1684 // new Â :            B
1685 // after:  A a a a a B         A a a a a
1686 // track:                  a a
1687 TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_4) {
1688   // Append 15 buffers at positions 0 through 14.
1689   NewCodedFrameGroupAppend(0, 15, &kDataA);
1690
1691   // Seek to 5 then move to position 8.
1692   Seek(5);
1693   CheckExpectedBuffers(5, 7, &kDataA);
1694
1695   // Now append 1 buffer at position 5.
1696   NewCodedFrameGroupAppend(5, 1, &kDataB);
1697
1698   // Check expected range.
1699   CheckExpectedRanges("{ [0,14) }");
1700
1701   // Buffers 8 and 9 should be in the track buffer.
1702   CheckExpectedBuffers(8, 9, &kDataA);
1703
1704   // The buffer immediately after the track buffer should be a keyframe.
1705   CheckExpectedBuffers(10, 10, &kDataA, true);
1706
1707   // Make sure all data is correct.
1708   Seek(0);
1709   CheckExpectedBuffers(0, 4, &kDataA);
1710   CheckExpectedBuffers(5, 5, &kDataB);
1711   // No seek should be necessary (1 continuous range).
1712   CheckExpectedBuffers(10, 14, &kDataA);
1713   CheckNoNextBuffer();
1714 }
1715
1716 TEST_F(SourceBufferStreamTest, Overlap_OneByOne) {
1717   // Append 5 buffers starting at 10ms, 30ms apart.
1718   NewCodedFrameGroupAppendOneByOne("10K 40 70 100 130");
1719
1720   // The range ends at 160, accounting for the last buffer's duration.
1721   CheckExpectedRangesByTimestamp("{ [10,160) }");
1722
1723   // Overlap with 10 buffers starting at the beginning, appended one at a
1724   // time.
1725   NewCodedFrameGroupAppend(0, 1, &kDataB);
1726   for (int i = 1; i < 10; i++)
1727     AppendBuffers(i, 1, &kDataB);
1728
1729   // All data should be replaced.
1730   Seek(0);
1731   CheckExpectedRanges("{ [0,9) }");
1732   CheckExpectedBuffers(0, 9, &kDataB);
1733 }
1734
1735 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_DeleteGroup) {
1736   NewCodedFrameGroupAppendOneByOne("10K 40 70 100 130K");
1737   CheckExpectedRangesByTimestamp("{ [10,160) }");
1738
1739   // Seek to 130ms.
1740   SeekToTimestampMs(130);
1741
1742   // Overlap with a new coded frame group from 0 to 130ms.
1743   NewCodedFrameGroupAppendOneByOne("0K 120D10");
1744
1745   // Next buffer should still be 130ms.
1746   CheckExpectedBuffers("130K");
1747
1748   // Check the final buffers is correct.
1749   SeekToTimestampMs(0);
1750   CheckExpectedBuffers("0K 120 130K");
1751 }
1752
1753 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_BetweenCodedFrameGroups) {
1754   // Append 5 buffers starting at 110ms, 30ms apart.
1755   NewCodedFrameGroupAppendOneByOne("110K 140 170 200 230");
1756   CheckExpectedRangesByTimestamp("{ [110,260) }");
1757
1758   // Now append 2 coded frame groups from 0ms to 210ms, 30ms apart. Note that
1759   // the
1760   // old keyframe 110ms falls in between these two groups.
1761   NewCodedFrameGroupAppendOneByOne("0K 30 60 90");
1762   NewCodedFrameGroupAppendOneByOne("120K 150 180 210");
1763   CheckExpectedRangesByTimestamp("{ [0,240) }");
1764
1765   // Check the final buffers is correct; the keyframe at 110ms should be
1766   // deleted.
1767   SeekToTimestampMs(0);
1768   CheckExpectedBuffers("0K 30 60 90 120K 150 180 210");
1769 }
1770
1771 // old  :   10K  40  *70*  100K  125  130K
1772 // new  : 0K   30   60   90   120K
1773 // after: 0K   30   60   90  *120K*   130K
1774 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer) {
1775   EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(50));
1776
1777   NewCodedFrameGroupAppendOneByOne("10K 40 70 100K 125 130D30K");
1778   CheckExpectedRangesByTimestamp("{ [10,160) }");
1779
1780   // Seek to 70ms.
1781   SeekToTimestampMs(70);
1782   CheckExpectedBuffers("10K 40");
1783
1784   // Overlap with a new coded frame group from 0 to 130ms.
1785   NewCodedFrameGroupAppendOneByOne("0K 30 60 90 120D10K");
1786   CheckExpectedRangesByTimestamp("{ [0,160) }");
1787
1788   // Should return frame 70ms from the track buffer, then switch
1789   // to the new data at 120K, then switch back to the old data at 130K. The
1790   // frame at 125ms that depended on keyframe 100ms should have been deleted.
1791   CheckExpectedBuffers("70 120K 130K");
1792
1793   // Check the final result: should not include data from the track buffer.
1794   SeekToTimestampMs(0);
1795   CheckExpectedBuffers("0K 30 60 90 120K 130K");
1796 }
1797
1798 // Overlap the next keyframe after the end of the track buffer with a new
1799 // keyframe.
1800 // old  :   10K  40  *70*  100K  125  130K
1801 // new  : 0K   30   60   90   120K
1802 // after: 0K   30   60   90  *120K*   130K
1803 // track:             70
1804 // new  :                     110K    130
1805 // after: 0K   30   60   90  *110K*   130
1806 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer2) {
1807   EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(40));
1808
1809   NewCodedFrameGroupAppendOneByOne("10K 40 70 100K 125 130D30K");
1810   CheckExpectedRangesByTimestamp("{ [10,160) }");
1811
1812   // Seek to 70ms.
1813   SeekToTimestampMs(70);
1814   CheckExpectedBuffers("10K 40");
1815
1816   // Overlap with a new coded frame group from 0 to 120ms; 70ms and 100ms go in
1817   // track
1818   // buffer.
1819   NewCodedFrameGroupAppendOneByOne("0K 30 60 90 120D10K");
1820   CheckExpectedRangesByTimestamp("{ [0,160) }");
1821
1822   // Now overlap the keyframe at 120ms.
1823   NewCodedFrameGroupAppendOneByOne("110K 130");
1824
1825   // Should return frame 70ms from the track buffer. Then it should
1826   // return the keyframe after the track buffer, which is at 110ms.
1827   CheckExpectedBuffers("70 110K 130");
1828 }
1829
1830 // Overlap the next keyframe after the end of the track buffer without a
1831 // new keyframe.
1832 // old  :   10K  40  *70*  100K  125  130K
1833 // new  : 0K   30   60   90   120K
1834 // after: 0K   30   60   90  *120K*   130K
1835 // track:             70
1836 // new  :        50K   80   110          140
1837 // after: 0K   30   50K   80   110   140 * (waiting for keyframe)
1838 // track:             70
1839 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer3) {
1840   EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(80));
1841
1842   NewCodedFrameGroupAppendOneByOne("10K 40 70 100K 125 130D30K");
1843   CheckExpectedRangesByTimestamp("{ [10,160) }");
1844
1845   // Seek to 70ms.
1846   SeekToTimestampMs(70);
1847   CheckExpectedBuffers("10K 40");
1848
1849   // Overlap with a new coded frame group from 0 to 120ms; 70ms goes in track
1850   // buffer.
1851   NewCodedFrameGroupAppendOneByOne("0K 30 60 90 120D10K");
1852   CheckExpectedRangesByTimestamp("{ [0,160) }");
1853
1854   // Now overlap the keyframe at 120ms and 130ms.
1855   NewCodedFrameGroupAppendOneByOne("50K 80 110 140");
1856   CheckExpectedRangesByTimestamp("{ [0,170) }");
1857
1858   // Should have all the buffers from the track buffer, then stall.
1859   CheckExpectedBuffers("70");
1860   CheckNoNextBuffer();
1861
1862   // Appending a keyframe should fulfill the read.
1863   AppendBuffersOneByOne("150D30K");
1864   CheckExpectedBuffers("150K");
1865   CheckNoNextBuffer();
1866 }
1867
1868 // Overlap the next keyframe after the end of the track buffer with a keyframe
1869 // that comes before the end of the track buffer.
1870 // old  :   10K  40  *70*  100K  125  130K
1871 // new  : 0K   30   60   90   120K
1872 // after: 0K   30   60   90  *120K*   130K
1873 // track:             70
1874 // new  :              80K  110          140
1875 // after: 0K   30   60   *80K*  110   140
1876 // track:               70
1877 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer4) {
1878   NewCodedFrameGroupAppendOneByOne("10K 40 70 100K 125 130D30K");
1879   CheckExpectedRangesByTimestamp("{ [10,160) }");
1880
1881   // Seek to 70ms.
1882   SeekToTimestampMs(70);
1883   CheckExpectedBuffers("10K 40");
1884
1885   // Overlap with a new coded frame group from 0 to 120ms; 70ms and 100ms go in
1886   // track
1887   // buffer.
1888   NewCodedFrameGroupAppendOneByOne("0K 30 60 90 120D10K");
1889   CheckExpectedRangesByTimestamp("{ [0,160) }");
1890
1891   // Now append a keyframe at 80ms.
1892   NewCodedFrameGroupAppendOneByOne("80K 110 140");
1893
1894   CheckExpectedBuffers("70 80K 110 140");
1895   CheckNoNextBuffer();
1896 }
1897
1898 // Overlap the next keyframe after the end of the track buffer with a keyframe
1899 // that comes before the end of the track buffer, when the selected stream was
1900 // waiting for the next keyframe.
1901 // old  :   10K  40  *70*  100K
1902 // new  : 0K   30   60   90   120
1903 // after: 0K   30   60   90   120 * (waiting for keyframe)
1904 // track:             70
1905 // new  :              80K  110          140
1906 // after: 0K   30   60   *80K*  110   140
1907 // track:               70
1908 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer5) {
1909   NewCodedFrameGroupAppendOneByOne("10K 40 70 100K");
1910   CheckExpectedRangesByTimestamp("{ [10,130) }");
1911
1912   // Seek to 70ms.
1913   SeekToTimestampMs(70);
1914   CheckExpectedBuffers("10K 40");
1915
1916   // Overlap with a new coded frame group from 0 to 120ms; 70ms goes in track
1917   // buffer.
1918   NewCodedFrameGroupAppendOneByOne("0K 30 60 90 120");
1919   CheckExpectedRangesByTimestamp("{ [0,150) }");
1920
1921   // Now append a keyframe at 80ms.
1922   NewCodedFrameGroupAppendOneByOne("80K 110 140");
1923
1924   CheckExpectedBuffers("70 80K 110 140");
1925   CheckNoNextBuffer();
1926 }
1927
1928 // Test that appending to a different range while there is data in
1929 // the track buffer doesn't affect the selected range or track buffer state.
1930 // old  :   10K  40  *70*  100K  125  130K ... 200K 230
1931 // new  : 0K   30   60   90   120K
1932 // after: 0K   30   60   90  *120K*   130K ... 200K 230
1933 // track:             70
1934 // old  : 0K   30   60   90  *120K*   130K ... 200K 230
1935 // new  :                                               260K 290
1936 // after: 0K   30   60   90  *120K*   130K ... 200K 230 260K 290
1937 // track:             70
1938 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer6) {
1939   EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(50));
1940
1941   NewCodedFrameGroupAppendOneByOne("10K 40 70 100K 125 130D30K");
1942   NewCodedFrameGroupAppendOneByOne("200K 230");
1943   CheckExpectedRangesByTimestamp("{ [10,160) [200,260) }");
1944
1945   // Seek to 70ms.
1946   SeekToTimestampMs(70);
1947   CheckExpectedBuffers("10K 40");
1948
1949   // Overlap with a new coded frame group from 0 to 120ms.
1950   NewCodedFrameGroupAppendOneByOne("0K 30 60 90 120D10K");
1951   CheckExpectedRangesByTimestamp("{ [0,160) [200,260) }");
1952
1953   // Verify that 70 gets read out of the track buffer.
1954   CheckExpectedBuffers("70");
1955
1956   // Append more data to the unselected range.
1957   NewCodedFrameGroupAppendOneByOne("260K 290");
1958   CheckExpectedRangesByTimestamp("{ [0,160) [200,320) }");
1959
1960   CheckExpectedBuffers("120K 130K");
1961   CheckNoNextBuffer();
1962
1963   // Check the final result: should not include data from the track buffer.
1964   SeekToTimestampMs(0);
1965   CheckExpectedBuffers("0K 30 60 90 120K 130K");
1966   CheckNoNextBuffer();
1967 }
1968
1969 // Test that overlap-appending with a GOP that begins with time of next track
1970 // buffer frame drops that track buffer frame and buffers the new GOP correctly.
1971 // append :    10K   40    70     100
1972 // read the first two buffers
1973 // after  :    10K   40   *70*    100
1974 //
1975 // append : 0K    30    60    90    120
1976 // after  : 0K    30    60    90    120
1977 // track  :               *70*    100
1978 //
1979 // read the buffer at 70ms from track
1980 // after  : 0K    30    60    90    120
1981 // track  :                      *100*
1982 //
1983 // append :                       100K   130
1984 // after  : 0K    30    60    90 *100K*  130
1985 // track  : (empty)
1986 // 100K, not 100, should be the next buffer read.
1987 TEST_F(SourceBufferStreamTest,
1988        Overlap_That_Prunes_All_of_Previous_TrackBuffer) {
1989   NewCodedFrameGroupAppend("10K 40 70 100");
1990   CheckExpectedRangesByTimestamp("{ [10,130) }");
1991
1992   // Seek to 70ms.
1993   SeekToTimestampMs(70);
1994   CheckExpectedBuffers("10K 40");
1995
1996   // Overlap with a new coded frame group from 0 to 120ms, leaving the original
1997   // nonkeyframes at 70ms and 100ms in the track buffer.
1998   NewCodedFrameGroupAppend("0K 30 60 90 120");
1999   CheckExpectedRangesByTimestamp("{ [0,150) }");
2000
2001   // Verify that 70 gets read out of the track buffer, leaving the nonkeyframe
2002   // at 100ms in the track buffer.
2003   CheckExpectedBuffers("70");
2004
2005   // Overlap with a coded frame group having a keyframe at 100ms. This should
2006   // clear the track buffer and serve that keyframe, not the original
2007   // nonkeyframe at time 100ms on the next read call.
2008   NewCodedFrameGroupAppend("100K 130");
2009   CheckExpectedRangesByTimestamp("{ [0,160) }");
2010   CheckExpectedBuffers("100K 130");
2011   CheckNoNextBuffer();
2012
2013   // Check the final result: should not include data from the track buffer.
2014   SeekToTimestampMs(0);
2015   CheckExpectedBuffers("0K 30 60 90 100K 130");
2016   CheckNoNextBuffer();
2017 }
2018
2019 TEST_F(SourceBufferStreamTest, Seek_Keyframe) {
2020   // Append 6 buffers at positions 0 through 5.
2021   NewCodedFrameGroupAppend(0, 6);
2022
2023   // Seek to beginning.
2024   Seek(0);
2025   CheckExpectedBuffers(0, 5, true);
2026 }
2027
2028 TEST_F(SourceBufferStreamTest, Seek_NonKeyframe) {
2029   // Append 15 buffers at positions 0 through 14.
2030   NewCodedFrameGroupAppend(0, 15);
2031
2032   // Seek to buffer at position 13.
2033   Seek(13);
2034
2035   // Expect seeking back to the nearest keyframe.
2036   CheckExpectedBuffers(10, 14, true);
2037
2038   // Seek to buffer at position 3.
2039   Seek(3);
2040
2041   // Expect seeking back to the nearest keyframe.
2042   CheckExpectedBuffers(0, 3, true);
2043 }
2044
2045 TEST_F(SourceBufferStreamTest, Seek_NotBuffered) {
2046   // Seek to beginning.
2047   SeekToTimestampMs(0);
2048
2049   // Try to get buffer; nothing's appended.
2050   CheckNoNextBuffer();
2051
2052   // Append 1 buffer at time 0, duration 10ms.
2053   NewCodedFrameGroupAppend("0D10K");
2054
2055   // Confirm we can read it back.
2056   SeekToTimestampMs(0);
2057   CheckExpectedBuffers("0K");
2058
2059   // Try to get buffer out of range.
2060   SeekToTimestampMs(10);
2061   CheckNoNextBuffer();
2062 }
2063
2064 TEST_F(SourceBufferStreamTest, Seek_InBetweenTimestamps) {
2065   // Append 10 buffers at positions 0 through 9.
2066   NewCodedFrameGroupAppend(0, 10);
2067
2068   base::TimeDelta bump = frame_duration() / 4;
2069   CHECK(bump.is_positive());
2070
2071   // Seek to buffer a little after position 5.
2072   stream_->Seek(5 * frame_duration() + bump);
2073   CheckExpectedBuffers(5, 5, true);
2074
2075   // Seek to buffer a little before position 5.
2076   stream_->Seek(5 * frame_duration() - bump);
2077   CheckExpectedBuffers(0, 0, true);
2078 }
2079
2080 // This test will do a complete overlap of an existing range in order to add
2081 // buffers to the track buffers. Then the test does a seek to another part of
2082 // the stream. The SourceBufferStream should clear its internal track buffer in
2083 // response to the Seek().
2084 TEST_F(SourceBufferStreamTest, Seek_After_TrackBuffer_Filled) {
2085   // Append 10 buffers at positions 5 through 14.
2086   NewCodedFrameGroupAppend(5, 10, &kDataA);
2087
2088   // Seek to buffer at position 5 and get next buffer.
2089   Seek(5);
2090   CheckExpectedBuffers(5, 5, &kDataA);
2091
2092   // Do a complete overlap by appending 20 buffers at positions 0 through 19.
2093   NewCodedFrameGroupAppend(0, 20, &kDataB);
2094
2095   // Check range is correct.
2096   CheckExpectedRanges("{ [0,19) }");
2097
2098   // Seek to beginning; all data should be new.
2099   Seek(0);
2100   CheckExpectedBuffers(0, 19, &kDataB);
2101
2102   // Check range continues to be correct.
2103   CheckExpectedRanges("{ [0,19) }");
2104 }
2105
2106 TEST_F(SourceBufferStreamTest, Seek_StartOfGroup) {
2107   base::TimeDelta bump = frame_duration() / 4;
2108   CHECK(bump.is_positive());
2109
2110   // Append 5 buffers at position (5 + |bump|) through 9, where the coded frame
2111   // group begins at position 5.
2112   Seek(5);
2113   NewCodedFrameGroupAppend_OffsetFirstBuffer(5, 5, bump);
2114   scoped_refptr<StreamParserBuffer> buffer;
2115
2116   // GetNextBuffer() should return the next buffer at position (5 + |bump|).
2117   EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
2118   EXPECT_EQ(buffer->GetDecodeTimestamp(),
2119             DecodeTimestamp::FromPresentationTime(5 * frame_duration() + bump));
2120
2121   // Check rest of buffers.
2122   CheckExpectedBuffers(6, 9);
2123
2124   // Seek to position 15.
2125   Seek(15);
2126
2127   // Append 5 buffers at positions (15 + |bump|) through 19, where the coded
2128   // frame group begins at 15.
2129   NewCodedFrameGroupAppend_OffsetFirstBuffer(15, 5, bump);
2130
2131   // GetNextBuffer() should return the next buffer at position (15 + |bump|).
2132   EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
2133   EXPECT_EQ(buffer->GetDecodeTimestamp(), DecodeTimestamp::FromPresentationTime(
2134       15 * frame_duration() + bump));
2135
2136   // Check rest of buffers.
2137   CheckExpectedBuffers(16, 19);
2138 }
2139
2140 TEST_F(SourceBufferStreamTest, Seek_BeforeStartOfGroup) {
2141   // Append 10 buffers at positions 5 through 14.
2142   NewCodedFrameGroupAppend(5, 10);
2143
2144   // Seek to a time before the first buffer in the range.
2145   Seek(0);
2146
2147   // Should return buffers from the beginning of the range.
2148   CheckExpectedBuffers(5, 14);
2149 }
2150
2151 TEST_F(SourceBufferStreamTest, OldSeekPoint_CompleteOverlap) {
2152   // Append 5 buffers at positions 0 through 4.
2153   NewCodedFrameGroupAppend(0, 4);
2154
2155   // Append 5 buffers at positions 10 through 14, and seek to the beginning of
2156   // this range.
2157   NewCodedFrameGroupAppend(10, 5);
2158   Seek(10);
2159
2160   // Now seek to the beginning of the first range.
2161   Seek(0);
2162
2163   // Completely overlap the old seek point.
2164   NewCodedFrameGroupAppend(5, 15);
2165
2166   // The GetNextBuffer() call should respect the 2nd seek point.
2167   CheckExpectedBuffers(0, 0);
2168 }
2169
2170 TEST_F(SourceBufferStreamTest, OldSeekPoint_CompleteOverlap_Pending) {
2171   // Append 2 buffers at positions 0 through 1.
2172   NewCodedFrameGroupAppend(0, 2);
2173
2174   // Append 5 buffers at positions 15 through 19 and seek to beginning of the
2175   // range.
2176   NewCodedFrameGroupAppend(15, 5);
2177   Seek(15);
2178
2179   // Now seek position 5.
2180   Seek(5);
2181
2182   // Completely overlap the old seek point.
2183   NewCodedFrameGroupAppend(10, 15);
2184
2185   // The seek at position 5 should still be pending.
2186   CheckNoNextBuffer();
2187 }
2188
2189 TEST_F(SourceBufferStreamTest, OldSeekPoint_MiddleOverlap) {
2190   // Append 1 buffer at position 0, duration 10ms.
2191   NewCodedFrameGroupAppend("0D10K");
2192
2193   // Append 3 IPBBB GOPs starting at 50ms.
2194   NewCodedFrameGroupAppend(
2195       "50K 90|60 60|70 70|80 80|90 "
2196       "100K 140|110 110|120 120|130 130|140 "
2197       "150K 190|160 160|170 170|180 180|190");
2198   SeekToTimestampMs(150);
2199
2200   // Now seek to the beginning of the stream.
2201   SeekToTimestampMs(0);
2202
2203   // Overlap the middle of the last range with a partial GOP, just a keyframe.
2204   NewCodedFrameGroupAppend("100D10K");
2205   CheckExpectedRangesByTimestamp("{ [0,10) [50,200) }");
2206
2207   // The GetNextBuffer() call should respect the 2nd seek point.
2208   CheckExpectedBuffers("0K");
2209   CheckNoNextBuffer();
2210
2211   // Check the data in the second range.
2212   SeekToTimestampMs(50);
2213   CheckExpectedBuffers(
2214       "50K 90|60 60|70 70|80 80|90 100K 150K 190|160 160|170 170|180 180|190");
2215   CheckNoNextBuffer();
2216 }
2217
2218 TEST_F(SourceBufferStreamTest, OldSeekPoint_MiddleOverlap_Pending) {
2219   // Append 1 buffer at position 0, duration 10ms.
2220   NewCodedFrameGroupAppend("0D10K");
2221
2222   // Append 3 IPBBB GOPs starting at 50ms. Then seek to 150ms.
2223   NewCodedFrameGroupAppend(
2224       "50K 90|60 60|70 70|80 80|90 "
2225       "100K 140|110 110|120 120|130 130|140 "
2226       "150K 190|160 160|170 170|180 180|190");
2227   SeekToTimestampMs(150);
2228
2229   // Now seek to unbuffered time 20ms.
2230   SeekToTimestampMs(20);
2231
2232   // Overlap the middle of the last range with a partial GOP, just a keyframe.
2233   NewCodedFrameGroupAppend("100D10K");
2234   CheckExpectedRangesByTimestamp("{ [0,10) [50,200) }");
2235
2236   // The seek to 20ms should still be pending.
2237   CheckNoNextBuffer();
2238
2239   // Check the data in both ranges.
2240   SeekToTimestampMs(0);
2241   CheckExpectedBuffers("0K");
2242   CheckNoNextBuffer();
2243   SeekToTimestampMs(50);
2244   CheckExpectedBuffers(
2245       "50K 90|60 60|70 70|80 80|90 100K 150K 190|160 160|170 170|180 180|190");
2246   CheckNoNextBuffer();
2247 }
2248
2249 TEST_F(SourceBufferStreamTest, OldSeekPoint_StartOverlap) {
2250   // Append 2 buffers at positions 0 through 1.
2251   NewCodedFrameGroupAppend(0, 2);
2252
2253   // Append 15 buffers at positions 5 through 19 and seek to position 15.
2254   NewCodedFrameGroupAppend(5, 15);
2255   Seek(15);
2256
2257   // Now seek to the beginning of the stream.
2258   Seek(0);
2259
2260   // Start overlap the old seek point.
2261   NewCodedFrameGroupAppend(10, 10);
2262
2263   // The GetNextBuffer() call should respect the 2nd seek point.
2264   CheckExpectedBuffers(0, 0);
2265 }
2266
2267 TEST_F(SourceBufferStreamTest, OldSeekPoint_StartOverlap_Pending) {
2268   // Append 2 buffers at positions 0 through 1.
2269   NewCodedFrameGroupAppend(0, 2);
2270
2271   // Append 15 buffers at positions 10 through 24 and seek to position 20.
2272   NewCodedFrameGroupAppend(10, 15);
2273   Seek(20);
2274
2275   // Now seek to position 5.
2276   Seek(5);
2277
2278   // Start overlap the old seek point.
2279   NewCodedFrameGroupAppend(15, 10);
2280
2281   // The seek at time 0 should still be pending.
2282   CheckNoNextBuffer();
2283 }
2284
2285 TEST_F(SourceBufferStreamTest, OldSeekPoint_EndOverlap) {
2286   // Append 5 buffers at positions 0 through 4.
2287   NewCodedFrameGroupAppend(0, 4);
2288
2289   // Append 15 buffers at positions 10 through 24 and seek to start of range.
2290   NewCodedFrameGroupAppend(10, 15);
2291   Seek(10);
2292
2293   // Now seek to the beginning of the stream.
2294   Seek(0);
2295
2296   // End overlap the old seek point.
2297   NewCodedFrameGroupAppend(5, 10);
2298
2299   // The GetNextBuffer() call should respect the 2nd seek point.
2300   CheckExpectedBuffers(0, 0);
2301 }
2302
2303 TEST_F(SourceBufferStreamTest, OldSeekPoint_EndOverlap_Pending) {
2304   // Append 2 buffers at positions 0 through 1.
2305   NewCodedFrameGroupAppend(0, 2);
2306
2307   // Append 15 buffers at positions 15 through 29 and seek to start of range.
2308   NewCodedFrameGroupAppend(15, 15);
2309   Seek(15);
2310
2311   // Now seek to position 5
2312   Seek(5);
2313
2314   // End overlap the old seek point.
2315   NewCodedFrameGroupAppend(10, 10);
2316
2317   // The seek at time 5 should still be pending.
2318   CheckNoNextBuffer();
2319 }
2320
2321 TEST_F(SourceBufferStreamTest, GetNextBuffer_AfterMerges) {
2322   // Append 5 buffers at positions 10 through 14.
2323   NewCodedFrameGroupAppend(10, 5);
2324
2325   // Seek to buffer at position 12.
2326   Seek(12);
2327
2328   // Append 5 buffers at positions 5 through 9.
2329   NewCodedFrameGroupAppend(5, 5);
2330
2331   // Make sure ranges are merged.
2332   CheckExpectedRanges("{ [5,14) }");
2333
2334   // Make sure the next buffer is correct.
2335   CheckExpectedBuffers(10, 10);
2336
2337   // Append 5 buffers at positions 15 through 19.
2338   NewCodedFrameGroupAppend(15, 5);
2339   CheckExpectedRanges("{ [5,19) }");
2340
2341   // Make sure the remaining next buffers are correct.
2342   CheckExpectedBuffers(11, 14);
2343 }
2344
2345 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenAppend) {
2346   // Append 4 buffers at positions 0 through 3.
2347   NewCodedFrameGroupAppend(0, 4);
2348
2349   // Seek to buffer at position 0 and get all buffers.
2350   Seek(0);
2351   CheckExpectedBuffers(0, 3);
2352
2353   // Next buffer is at position 4, so should not be able to fulfill request.
2354   CheckNoNextBuffer();
2355
2356   // Append 2 buffers at positions 4 through 5.
2357   AppendBuffers(4, 2);
2358   CheckExpectedBuffers(4, 5);
2359 }
2360
2361 // This test covers the case where new buffers start-overlap a range whose next
2362 // buffer is not buffered.
2363 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenStartOverlap) {
2364   // Append 10 buffers at positions 0 through 9 and exhaust the buffers.
2365   NewCodedFrameGroupAppend(0, 10, &kDataA);
2366   Seek(0);
2367   CheckExpectedBuffers(0, 9, &kDataA);
2368
2369   // Next buffer is at position 10, so should not be able to fulfill request.
2370   CheckNoNextBuffer();
2371
2372   // Append 6 buffers at positons 5 through 10. This is to test that doing a
2373   // start-overlap successfully fulfills the read at position 10, even though
2374   // position 10 was unbuffered.
2375   NewCodedFrameGroupAppend(5, 6, &kDataB);
2376   CheckExpectedBuffers(10, 10, &kDataB);
2377
2378   // Then add 5 buffers from positions 11 though 15.
2379   AppendBuffers(11, 5, &kDataB);
2380
2381   // Check the next 4 buffers are correct, which also effectively seeks to
2382   // position 15.
2383   CheckExpectedBuffers(11, 14, &kDataB);
2384
2385   // Replace the next buffer at position 15 with another start overlap.
2386   NewCodedFrameGroupAppend(15, 2, &kDataA);
2387   CheckExpectedBuffers(15, 16, &kDataA);
2388 }
2389
2390 // Tests a start overlap that occurs right at the timestamp of the last output
2391 // buffer that was returned by GetNextBuffer(). This test verifies that
2392 // GetNextBuffer() skips to second GOP in the newly appended data instead
2393 // of returning two buffers with the same timestamp.
2394 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenStartOverlap2) {
2395   NewCodedFrameGroupAppend("0K 30 60 90 120");
2396
2397   Seek(0);
2398   CheckExpectedBuffers("0K 30 60 90 120");
2399   CheckNoNextBuffer();
2400
2401   // Append a keyframe with the same timestamp as the last buffer output.
2402   NewCodedFrameGroupAppend("120D30K");
2403   CheckNoNextBuffer();
2404
2405   // Append the rest of the coded frame group and make sure that buffers are
2406   // returned from the first GOP after 120.
2407   AppendBuffers("150 180 210K 240");
2408   CheckExpectedBuffers("210K 240");
2409
2410   // Seek to the beginning and verify the contents of the source buffer.
2411   Seek(0);
2412   CheckExpectedBuffers("0K 30 60 90 120K 150 180 210K 240");
2413   CheckNoNextBuffer();
2414 }
2415
2416 // This test covers the case where new buffers completely overlap a range
2417 // whose next buffer is not buffered.
2418 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenCompleteOverlap) {
2419   // Append 5 buffers at positions 10 through 14 and exhaust the buffers.
2420   NewCodedFrameGroupAppend(10, 5, &kDataA);
2421   Seek(10);
2422   CheckExpectedBuffers(10, 14, &kDataA);
2423
2424   // Next buffer is at position 15, so should not be able to fulfill request.
2425   CheckNoNextBuffer();
2426
2427   // Do a complete overlap and test that this successfully fulfills the read
2428   // at position 15.
2429   NewCodedFrameGroupAppend(5, 11, &kDataB);
2430   CheckExpectedBuffers(15, 15, &kDataB);
2431
2432   // Then add 5 buffers from positions 16 though 20.
2433   AppendBuffers(16, 5, &kDataB);
2434
2435   // Check the next 4 buffers are correct, which also effectively seeks to
2436   // position 20.
2437   CheckExpectedBuffers(16, 19, &kDataB);
2438
2439   // Do a complete overlap and replace the buffer at position 20.
2440   NewCodedFrameGroupAppend(0, 21, &kDataA);
2441   CheckExpectedBuffers(20, 20, &kDataA);
2442 }
2443
2444 // This test covers the case where a range is stalled waiting for its next
2445 // buffer, then an end-overlap causes the end of the range to be deleted.
2446 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenEndOverlap) {
2447   // Append 5 buffers at positions 10 through 14 and exhaust the buffers.
2448   NewCodedFrameGroupAppend(10, 5, &kDataA);
2449   Seek(10);
2450   CheckExpectedBuffers(10, 14, &kDataA);
2451   CheckExpectedRanges("{ [10,14) }");
2452
2453   // Next buffer is at position 15, so should not be able to fulfill request.
2454   CheckNoNextBuffer();
2455
2456   // Do an end overlap that causes the latter half of the range to be deleted.
2457   NewCodedFrameGroupAppend(5, 6, &kDataB);
2458   CheckNoNextBuffer();
2459   CheckExpectedRanges("{ [5,10) }");
2460
2461   // Fill in the gap. Getting the next buffer should still stall at position 15.
2462   for (int i = 11; i <= 14; i++) {
2463     AppendBuffers(i, 1, &kDataB);
2464     CheckNoNextBuffer();
2465   }
2466
2467   // Append the buffer at position 15 and check to make sure all is correct.
2468   AppendBuffers(15, 1);
2469   CheckExpectedBuffers(15, 15);
2470   CheckExpectedRanges("{ [5,15) }");
2471 }
2472
2473 // This test is testing the "next buffer" logic after a complete overlap. In
2474 // this scenario, when the track buffer is exhausted, there is no buffered data
2475 // to fulfill the request. The SourceBufferStream should be able to fulfill the
2476 // request when the data is later appended, and should not lose track of the
2477 // "next buffer" position.
2478 TEST_F(SourceBufferStreamTest, GetNextBuffer_Overlap_Selected_Complete) {
2479   // Append 5 buffers at positions 5 through 9.
2480   NewCodedFrameGroupAppend(5, 5, &kDataA);
2481
2482   // Seek to buffer at position 5 and get next buffer.
2483   Seek(5);
2484   CheckExpectedBuffers(5, 5, &kDataA);
2485
2486   // Replace existing data with new data.
2487   NewCodedFrameGroupAppend(5, 5, &kDataB);
2488
2489   // Expect old data up until next keyframe in new data.
2490   CheckExpectedBuffers(6, 9, &kDataA);
2491
2492   // Next buffer is at position 10, so should not be able to fulfill the
2493   // request.
2494   CheckNoNextBuffer();
2495
2496   // Now add 5 new buffers at positions 10 through 14.
2497   AppendBuffers(10, 5, &kDataB);
2498   CheckExpectedBuffers(10, 14, &kDataB);
2499 }
2500
2501 TEST_F(SourceBufferStreamTest, PresentationTimestampIndependence) {
2502   // Append 20 buffers at position 0.
2503   NewCodedFrameGroupAppend(0, 20);
2504   Seek(0);
2505
2506   int last_keyframe_idx = -1;
2507   base::TimeDelta last_keyframe_presentation_timestamp;
2508   base::TimeDelta last_p_frame_presentation_timestamp;
2509
2510   // Check for IBB...BBP pattern.
2511   for (int i = 0; i < 20; i++) {
2512     scoped_refptr<StreamParserBuffer> buffer;
2513     EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
2514
2515     if (buffer->is_key_frame()) {
2516       EXPECT_EQ(DecodeTimestamp::FromPresentationTime(buffer->timestamp()),
2517                 buffer->GetDecodeTimestamp());
2518       last_keyframe_idx = i;
2519       last_keyframe_presentation_timestamp = buffer->timestamp();
2520     } else if (i == last_keyframe_idx + 1) {
2521       ASSERT_NE(last_keyframe_idx, -1);
2522       last_p_frame_presentation_timestamp = buffer->timestamp();
2523       EXPECT_LT(last_keyframe_presentation_timestamp,
2524                 last_p_frame_presentation_timestamp);
2525     } else {
2526       EXPECT_GT(buffer->timestamp(), last_keyframe_presentation_timestamp);
2527       EXPECT_LT(buffer->timestamp(), last_p_frame_presentation_timestamp);
2528       EXPECT_LT(DecodeTimestamp::FromPresentationTime(buffer->timestamp()),
2529                 buffer->GetDecodeTimestamp());
2530     }
2531   }
2532 }
2533
2534 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFront) {
2535   // Set memory limit to 20 buffers.
2536   SetMemoryLimit(20);
2537
2538   // Append 20 buffers at positions 0 through 19.
2539   NewCodedFrameGroupAppend(0, 1, &kDataA);
2540   for (int i = 1; i < 20; i++)
2541     AppendBuffers(i, 1, &kDataA);
2542
2543   // GC should be a no-op, since we are just under memory limit.
2544   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0));
2545   CheckExpectedRanges("{ [0,19) }");
2546   Seek(0);
2547   CheckExpectedBuffers(0, 19, &kDataA);
2548
2549   // Seek to the middle of the stream.
2550   Seek(10);
2551
2552   // We are about to append 5 new buffers and current playback position is 10,
2553   // so the GC algorithm should be able to delete some old data from the front.
2554   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(10, 5));
2555   CheckExpectedRanges("{ [5,19) }");
2556
2557   // Append 5 buffers to the end of the stream.
2558   AppendBuffers(20, 5, &kDataA);
2559   CheckExpectedRanges("{ [5,24) }");
2560
2561   CheckExpectedBuffers(10, 24, &kDataA);
2562   Seek(5);
2563   CheckExpectedBuffers(5, 9, &kDataA);
2564 }
2565
2566 TEST_F(SourceBufferStreamTest,
2567        GarbageCollection_DeleteFront_PreserveSeekedGOP) {
2568   // Set memory limit to 15 buffers.
2569   SetMemoryLimit(15);
2570
2571   NewCodedFrameGroupAppend("0K 10 20 30 40 50K 60 70 80 90");
2572   NewCodedFrameGroupAppend("1000K 1010 1020 1030 1040");
2573
2574   // GC should be a no-op, since we are just under memory limit.
2575   EXPECT_TRUE(GarbageCollect(base::TimeDelta(), 0));
2576   CheckExpectedRangesByTimestamp("{ [0,100) [1000,1050) }");
2577
2578   // Seek to the near the end of the first range
2579   SeekToTimestampMs(95);
2580
2581   // We are about to append 7 new buffers and current playback position is at
2582   // the end of the last GOP in the first range, so the GC algorithm should be
2583   // able to delete some old data from the front, but must not collect the last
2584   // GOP in that first range. Neither can it collect the last appended GOP
2585   // (which is the entire second range), so GC should return false since it
2586   // couldn't collect enough.
2587   EXPECT_FALSE(GarbageCollect(base::Milliseconds(95), 7));
2588   CheckExpectedRangesByTimestamp("{ [50,100) [1000,1050) }");
2589 }
2590
2591 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFrontGOPsAtATime) {
2592   // Set memory limit to 20 buffers.
2593   SetMemoryLimit(20);
2594
2595   // Append 20 buffers at positions 0 through 19.
2596   NewCodedFrameGroupAppend(0, 20, &kDataA);
2597
2598   // Seek to position 10.
2599   Seek(10);
2600   CheckExpectedRanges("{ [0,19) }");
2601
2602   // Add one buffer to put the memory over the cap.
2603   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(10, 1));
2604   AppendBuffers(20, 1, &kDataA);
2605
2606   // GC should have deleted the first 5 buffers so that the range still begins
2607   // with a keyframe.
2608   CheckExpectedRanges("{ [5,20) }");
2609   CheckExpectedBuffers(10, 20, &kDataA);
2610   Seek(5);
2611   CheckExpectedBuffers(5, 9, &kDataA);
2612 }
2613
2614 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteBack) {
2615   // Set memory limit to 5 buffers.
2616   SetMemoryLimit(5);
2617
2618   // Append 5 buffers at positions 15 through 19.
2619   NewCodedFrameGroupAppend(15, 5, &kDataA);
2620   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0));
2621
2622   // Append 5 buffers at positions 0 through 4.
2623   NewCodedFrameGroupAppend(0, 5, &kDataA);
2624   CheckExpectedRanges("{ [0,4) [15,19) }");
2625
2626   // Seek to position 0.
2627   Seek(0);
2628   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0));
2629   // Should leave the first 5 buffers from 0 to 4.
2630   CheckExpectedRanges("{ [0,4) }");
2631   CheckExpectedBuffers(0, 4, &kDataA);
2632 }
2633
2634 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFrontAndBack) {
2635   // Set memory limit to 3 buffers.
2636   SetMemoryLimit(3);
2637
2638   // Seek to position 15.
2639   Seek(15);
2640
2641   // Append 40 buffers at positions 0 through 39.
2642   NewCodedFrameGroupAppend(0, 40, &kDataA);
2643   // GC will try to keep data between current playback position and last append
2644   // position. This will ensure that the last append position is 19 and will
2645   // allow GC algorithm to collect data outside of the range [15,19)
2646   NewCodedFrameGroupAppend(15, 5, &kDataA);
2647   CheckExpectedRanges("{ [0,39) }");
2648
2649   // Should leave the GOP containing the current playback position 15 and the
2650   // last append position 19. GC returns false, since we are still above limit.
2651   EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(15, 0));
2652   CheckExpectedRanges("{ [15,19) }");
2653   CheckExpectedBuffers(15, 19, &kDataA);
2654   CheckNoNextBuffer();
2655 }
2656
2657 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteSeveralRanges) {
2658   // Append 5 buffers at positions 0 through 4.
2659   NewCodedFrameGroupAppend(0, 5);
2660
2661   // Append 5 buffers at positions 10 through 14.
2662   NewCodedFrameGroupAppend(10, 5);
2663
2664   // Append 5 buffers at positions 20 through 24.
2665   NewCodedFrameGroupAppend(20, 5);
2666
2667   // Append 5 buffers at positions 40 through 44.
2668   NewCodedFrameGroupAppend(40, 5);
2669
2670   CheckExpectedRanges("{ [0,4) [10,14) [20,24) [40,44) }");
2671
2672   // Seek to position 20.
2673   Seek(20);
2674   CheckExpectedBuffers(20, 20);
2675
2676   // Set memory limit to 1 buffer.
2677   SetMemoryLimit(1);
2678
2679   // Append 5 buffers at positions 30 through 34.
2680   NewCodedFrameGroupAppend(30, 5);
2681
2682   // We will have more than 1 buffer left, GC will fail
2683   EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(20, 0));
2684
2685   // Should have deleted all buffer ranges before the current buffer and after
2686   // last GOP
2687   CheckExpectedRanges("{ [20,24) [30,34) }");
2688   CheckExpectedBuffers(21, 24);
2689   CheckNoNextBuffer();
2690
2691   // Continue appending into the last range to make sure it didn't break.
2692   AppendBuffers(35, 10);
2693   EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(20, 0));
2694   // Should save everything between read head and last appended
2695   CheckExpectedRanges("{ [20,24) [30,44) }");
2696
2697   // Make sure appending before and after the ranges didn't somehow break.
2698   SetMemoryLimit(100);
2699   NewCodedFrameGroupAppend(0, 10);
2700   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(20, 0));
2701   CheckExpectedRanges("{ [0,9) [20,24) [30,44) }");
2702   Seek(0);
2703   CheckExpectedBuffers(0, 9);
2704
2705   NewCodedFrameGroupAppend(90, 10);
2706   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0));
2707   CheckExpectedRanges("{ [0,9) [20,24) [30,44) [90,99) }");
2708   Seek(30);
2709   CheckExpectedBuffers(30, 44);
2710   CheckNoNextBuffer();
2711   Seek(90);
2712   CheckExpectedBuffers(90, 99);
2713   CheckNoNextBuffer();
2714 }
2715
2716 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteAfterLastAppend) {
2717   // Set memory limit to 10 buffers.
2718   SetMemoryLimit(10);
2719
2720   // Append 1 GOP starting at 310ms, 30ms apart.
2721   NewCodedFrameGroupAppend("310K 340 370");
2722
2723   // Append 2 GOPs starting at 490ms, 30ms apart.
2724   NewCodedFrameGroupAppend("490K 520 550 580K 610 640");
2725
2726   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0));
2727
2728   CheckExpectedRangesByTimestamp("{ [310,400) [490,670) }");
2729
2730   // Seek to the GOP at 580ms.
2731   SeekToTimestampMs(580);
2732
2733   // Append 2 GOPs before the existing ranges.
2734   // So the ranges before GC are "{ [100,280) [310,400) [490,670) }".
2735   NewCodedFrameGroupAppend("100K 130 160 190K 220 250K");
2736
2737   EXPECT_TRUE(GarbageCollect(base::Milliseconds(580), 0));
2738
2739   // Should save the newly appended GOPs.
2740   CheckExpectedRangesByTimestamp("{ [100,280) [580,670) }");
2741 }
2742
2743 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteAfterLastAppendMerged) {
2744   // Set memory limit to 10 buffers.
2745   SetMemoryLimit(10);
2746
2747   // Append 3 GOPs starting at 400ms, 30ms apart.
2748   NewCodedFrameGroupAppend("400K 430 460 490K 520 550 580K 610 640");
2749
2750   // Seek to the GOP at 580ms.
2751   SeekToTimestampMs(580);
2752
2753   // Append 2 GOPs starting at 220ms, and they will be merged with the existing
2754   // range.  So the range before GC is "{ [220,670) }".
2755   NewCodedFrameGroupAppend("220K 250 280 310K 340 370");
2756
2757   EXPECT_TRUE(GarbageCollect(base::Milliseconds(580), 0));
2758
2759   // Should save the newly appended GOPs.
2760   CheckExpectedRangesByTimestamp("{ [220,400) [580,670) }");
2761 }
2762
2763 TEST_F(SourceBufferStreamTest, GarbageCollection_NoSeek) {
2764   // Set memory limit to 20 buffers.
2765   SetMemoryLimit(20);
2766
2767   // Append 25 buffers at positions 0 through 24.
2768   NewCodedFrameGroupAppend(0, 25, &kDataA);
2769
2770   // If playback is still in the first GOP (starting at 0), GC should fail.
2771   EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(2, 0));
2772   CheckExpectedRanges("{ [0,24) }");
2773
2774   // As soon as playback position moves past the first GOP, it should be removed
2775   // and after removing the first GOP we are under memory limit.
2776   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(5, 0));
2777   CheckExpectedRanges("{ [5,24) }");
2778   CheckNoNextBuffer();
2779   Seek(5);
2780   CheckExpectedBuffers(5, 24, &kDataA);
2781 }
2782
2783 TEST_F(SourceBufferStreamTest, GarbageCollection_PendingSeek) {
2784   // Append 10 buffers at positions 0 through 9.
2785   NewCodedFrameGroupAppend(0, 10, &kDataA);
2786
2787   // Append 5 buffers at positions 25 through 29.
2788   NewCodedFrameGroupAppend(25, 5, &kDataA);
2789
2790   // Seek to position 15.
2791   Seek(15);
2792   CheckNoNextBuffer();
2793   CheckExpectedRanges("{ [0,9) [25,29) }");
2794
2795   // Set memory limit to 5 buffers.
2796   SetMemoryLimit(5);
2797
2798   // Append 5 buffers as positions 30 to 34 to trigger GC.
2799   AppendBuffers(30, 5, &kDataA);
2800
2801   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(30, 0));
2802
2803   // The current algorithm will delete from the beginning until the memory is
2804   // under cap.
2805   CheckExpectedRanges("{ [30,34) }");
2806
2807   // Expand memory limit again so that GC won't be triggered.
2808   SetMemoryLimit(100);
2809
2810   // Append data to fulfill seek.
2811   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(30, 5));
2812   NewCodedFrameGroupAppend(15, 5, &kDataA);
2813
2814   // Check to make sure all is well.
2815   CheckExpectedRanges("{ [15,19) [30,34) }");
2816   CheckExpectedBuffers(15, 19, &kDataA);
2817   Seek(30);
2818   CheckExpectedBuffers(30, 34, &kDataA);
2819 }
2820
2821 TEST_F(SourceBufferStreamTest, GarbageCollection_NeedsMoreData) {
2822   // Set memory limit to 15 buffers.
2823   SetMemoryLimit(15);
2824
2825   // Append 10 buffers at positions 0 through 9.
2826   NewCodedFrameGroupAppend(0, 10, &kDataA);
2827
2828   // Advance next buffer position to 10.
2829   Seek(0);
2830   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0));
2831   CheckExpectedBuffers(0, 9, &kDataA);
2832   CheckNoNextBuffer();
2833
2834   // Append 20 buffers at positions 15 through 34.
2835   NewCodedFrameGroupAppend(15, 20, &kDataA);
2836   CheckExpectedRanges("{ [0,9) [15,34) }");
2837
2838   // GC should save the keyframe before the next buffer position and the data
2839   // closest to the next buffer position. It will also save all buffers from
2840   // next buffer to the last GOP appended, which overflows limit and leads to
2841   // failure.
2842   EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(5, 0));
2843   CheckExpectedRanges("{ [5,9) [15,34) }");
2844
2845   // Now fulfill the seek at position 10. This will make GC delete the data
2846   // before position 10 to keep it within cap.
2847   NewCodedFrameGroupAppend(10, 5, &kDataA);
2848   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(10, 0));
2849   CheckExpectedRanges("{ [10,24) }");
2850   CheckExpectedBuffers(10, 24, &kDataA);
2851 }
2852
2853 // Using position based test API:
2854 // DTS  :  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
2855 // PTS  :  0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3
2856 // old Â :  A a a a a A a a a a A a a a a*A*a a
2857 //       -- Garbage Collect --
2858 // after:                                A a a
2859 //       -- Read one buffer --
2860 // after:                                A*a*a
2861 // new  :  B b b b b B b b b b B b b b b B b b b b
2862 // track:                                 *a*a
2863 //       -- Garbage Collect --
2864 // after:                                B b b b b
2865 // track:                                 *a*a
2866 //       -- Read 2 buffers -> exhausts track buffer
2867 // after:                                B b b b b
2868 //       (awaiting next keyframe after GOP at position 15)
2869 // new  :                                         *B*b b b b
2870 // after:                                B b b b b*B*b b b b
2871 //       -- Garbage Collect --
2872 // after:                                         *B*b b b b
2873 TEST_F(SourceBufferStreamTest, GarbageCollection_TrackBuffer) {
2874   // Set memory limit to 3 buffers.
2875   SetMemoryLimit(3);
2876
2877   // Seek to position 15.
2878   Seek(15);
2879
2880   // Append 18 buffers at positions 0 through 17 (DTS), 0 through 19 (PTS) with
2881   // partial 4th GOP.
2882   NewCodedFrameGroupAppend(0, 18, &kDataA);
2883
2884   EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(15, 0));
2885
2886   // GC should leave GOP containing seek position (15,16,17 DTS; 15,19,16 PTS).
2887   // Unlike the rest of the position based test API used in this case,
2888   // CheckExpectedRanges() uses expectation strings containing actual timestamps
2889   // (divided by frame_duration_).
2890   CheckExpectedRanges("{ [15,19) }");
2891
2892   // Move next buffer position to 16.
2893   CheckExpectedBuffers(15, 15, &kDataA);
2894
2895   // Completely overlap the existing buffers with 4 full GOPs (0-19, PTS and
2896   // DTS).
2897   NewCodedFrameGroupAppend(0, 20, &kDataB);
2898
2899   // Final GOP [15,19) contains 5 buffers, which is more than memory limit of
2900   // 3 buffers set at the beginning of the test, so GC will fail.
2901   EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(15, 0));
2902
2903   // Because buffers 16,17 (DTS), 19,16 (PTS) are not keyframes, they are moved
2904   // to the track buffer upon overlap. The source buffer (i.e. not the track
2905   // buffer) is now waiting for the next keyframe beyond GOP that survived GC.
2906   CheckExpectedRanges("{ [15,19) }");     // Source buffer
2907   CheckExpectedBuffers(16, 17, &kDataA);  // Exhaust the track buffer
2908   CheckNoNextBuffer();  // Confirms the source buffer is awaiting next keyframe.
2909
2910   // Now add a 5-frame GOP  at position 20-24 (PTS and DTS).
2911   AppendBuffers(20, 5, &kDataB);
2912
2913   // 5 buffers in final GOP, GC will fail
2914   EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(20, 0));
2915
2916   // Should garbage collect such that there are 5 frames remaining, starting at
2917   // the keyframe.
2918   CheckExpectedRanges("{ [20,24) }");
2919
2920   // The buffer at position 16 (PTS 19) in track buffer was adjacent
2921   // to the next keyframe (PTS=DTS=20), so no warning should be emitted on that
2922   // track buffer exhaustion even though the last frame read out of track buffer
2923   // before exhaustion was position 17 (PTS 16).
2924   CheckExpectedBuffers(20, 24, &kDataB);
2925   CheckNoNextBuffer();
2926 }
2927
2928 // Test GC preserves data starting at first GOP containing playback position.
2929 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveDataAtPlaybackPosition) {
2930   // Set memory limit to 30 buffers = 1 second of data.
2931   SetMemoryLimit(30);
2932   // And append 300 buffers = 10 seconds of data.
2933   NewCodedFrameGroupAppend(0, 300, &kDataA);
2934   CheckExpectedRanges("{ [0,299) }");
2935
2936   // Playback position at 0, all data must be preserved.
2937   EXPECT_FALSE(GarbageCollect(base::Milliseconds(0), 0));
2938   CheckExpectedRanges("{ [0,299) }");
2939
2940   // Playback position at 1 sec, the first second of data [0,29) should be
2941   // collected, since we are way over memory limit.
2942   EXPECT_FALSE(GarbageCollect(base::Milliseconds(1000), 0));
2943   CheckExpectedRanges("{ [30,299) }");
2944
2945   // Playback position at 1.1 sec, no new data can be collected, since the
2946   // playback position is still in the first GOP of buffered data.
2947   EXPECT_FALSE(GarbageCollect(base::Milliseconds(1100), 0));
2948   CheckExpectedRanges("{ [30,299) }");
2949
2950   // Playback position at 5.166 sec, just at the very end of GOP corresponding
2951   // to buffer range 150-155, which should be preserved.
2952   EXPECT_FALSE(GarbageCollect(base::Milliseconds(5166), 0));
2953   CheckExpectedRanges("{ [150,299) }");
2954
2955   // Playback position at 5.167 sec, just past the end of GOP corresponding to
2956   // buffer range 150-155, it should be garbage collected now.
2957   EXPECT_FALSE(GarbageCollect(base::Milliseconds(5167), 0));
2958   CheckExpectedRanges("{ [155,299) }");
2959
2960   // Playback at 9.0 sec, we can now successfully collect all data except the
2961   // last second and we are back under memory limit of 30 buffers, so GCIfNeeded
2962   // should return true.
2963   EXPECT_TRUE(GarbageCollect(base::Milliseconds(9000), 0));
2964   CheckExpectedRanges("{ [270,299) }");
2965
2966   // Playback at 9.999 sec, GC succeeds, since we are under memory limit even
2967   // without removing any data.
2968   EXPECT_TRUE(GarbageCollect(base::Milliseconds(9999), 0));
2969   CheckExpectedRanges("{ [270,299) }");
2970
2971   // Playback at 15 sec, this should never happen during regular playback in
2972   // browser, since this position has no data buffered, but it should still
2973   // cause no problems to GC algorithm, so test it just in case.
2974   EXPECT_TRUE(GarbageCollect(base::Milliseconds(15000), 0));
2975   CheckExpectedRanges("{ [270,299) }");
2976 }
2977
2978 // Test saving the last GOP appended when this GOP is the only GOP in its range.
2979 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP) {
2980   // Set memory limit to 3 and make sure the 4-byte GOP is not garbage
2981   // collected.
2982   SetMemoryLimit(3);
2983   NewCodedFrameGroupAppend("0K 30 60 90");
2984   EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(0, 0));
2985   CheckExpectedRangesByTimestamp("{ [0,120) }");
2986
2987   // Make sure you can continue appending data to this GOP; again, GC should not
2988   // wipe out anything.
2989   AppendBuffers("120D30");
2990   EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(0, 0));
2991   CheckExpectedRangesByTimestamp("{ [0,150) }");
2992
2993   // Append a 2nd range after this without triggering GC.
2994   NewCodedFrameGroupAppend("200K 230 260 290K 320 350");
2995   CheckExpectedRangesByTimestamp("{ [0,150) [200,380) }");
2996
2997   // Seek to 290ms.
2998   SeekToTimestampMs(290);
2999
3000   // Now append a GOP in a separate range after the selected range and trigger
3001   // GC. Because it is after 290ms, this tests that the GOP is saved when
3002   // deleting from the back.
3003   NewCodedFrameGroupAppend("500K 530 560 590");
3004   EXPECT_FALSE(GarbageCollect(base::Milliseconds(290), 0));
3005
3006   // Should save GOPs between 290ms and the last GOP appended.
3007   CheckExpectedRangesByTimestamp("{ [290,380) [500,620) }");
3008
3009   // Continue appending to this GOP after GC.
3010   AppendBuffers("620D30");
3011   EXPECT_FALSE(GarbageCollect(base::Milliseconds(290), 0));
3012   CheckExpectedRangesByTimestamp("{ [290,380) [500,650) }");
3013 }
3014
3015 // Test saving the last GOP appended when this GOP is in the middle of a
3016 // non-selected range.
3017 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Middle) {
3018   // Append 3 GOPs starting at 0ms, 30ms apart.
3019   NewCodedFrameGroupAppend("0K 30 60 90K 120 150 180K 210 240");
3020   CheckExpectedRangesByTimestamp("{ [0,270) }");
3021
3022   // Now set the memory limit to 1 and overlap the middle of the range with a
3023   // new GOP.
3024   SetMemoryLimit(1);
3025   NewCodedFrameGroupAppend("80K 110 140");
3026
3027   // This whole GOP should be saved after GC, which will fail due to GOP being
3028   // larger than 1 buffer
3029   EXPECT_FALSE(GarbageCollect(base::Milliseconds(80), 0));
3030   CheckExpectedRangesByTimestamp("{ [80,170) }");
3031   // We should still be able to continue appending data to GOP
3032   AppendBuffers("170D30");
3033   EXPECT_FALSE(GarbageCollect(base::Milliseconds(80), 0));
3034   CheckExpectedRangesByTimestamp("{ [80,200) }");
3035
3036   // Append a 2nd range after this range, without triggering GC.
3037   NewCodedFrameGroupAppend("400K 430 460 490K 520 550 580K 610 640");
3038   CheckExpectedRangesByTimestamp("{ [80,200) [400,670) }");
3039
3040   // Seek to 80ms to make the first range the selected range.
3041   SeekToTimestampMs(80);
3042
3043   // Now append a GOP in the middle of the second range and trigger GC. Because
3044   // it is after the selected range, this tests that the GOP is saved when
3045   // deleting from the back.
3046   NewCodedFrameGroupAppend("500K 530 560 590");
3047   EXPECT_FALSE(GarbageCollect(base::Milliseconds(80), 0));
3048
3049   // Should save the GOPs between the seek point and GOP that was last appended
3050   CheckExpectedRangesByTimestamp("{ [80,200) [400,620) }");
3051
3052   // Continue appending to this GOP after GC.
3053   AppendBuffers("620D30");
3054   EXPECT_FALSE(GarbageCollect(base::Milliseconds(80), 0));
3055   CheckExpectedRangesByTimestamp("{ [80,200) [400,650) }");
3056 }
3057
3058 // Test saving the last GOP appended when the GOP containing the next buffer is
3059 // adjacent to the last GOP appended.
3060 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected1) {
3061   // Append 3 GOPs at 0ms, 90ms, and 180ms.
3062   NewCodedFrameGroupAppend("0K 30 60 90K 120 150 180K 210 240");
3063   CheckExpectedRangesByTimestamp("{ [0,270) }");
3064
3065   // Seek to the GOP at 90ms.
3066   SeekToTimestampMs(90);
3067
3068   // Set the memory limit to 1, then overlap the GOP at 0.
3069   SetMemoryLimit(1);
3070   NewCodedFrameGroupAppend("0K 30 60");
3071
3072   // GC should save the GOP at 0ms and 90ms, and will fail since GOP larger
3073   // than 1 buffer
3074   EXPECT_FALSE(GarbageCollect(base::Milliseconds(90), 0));
3075   CheckExpectedRangesByTimestamp("{ [0,180) }");
3076
3077   // Seek to 0 and check all buffers.
3078   SeekToTimestampMs(0);
3079   CheckExpectedBuffers("0K 30 60 90K 120 150");
3080   CheckNoNextBuffer();
3081
3082   // Now seek back to 90ms and append a GOP at 180ms.
3083   SeekToTimestampMs(90);
3084   NewCodedFrameGroupAppend("180K 210 240");
3085
3086   // Should save the GOP at 90ms and the GOP at 180ms.
3087   EXPECT_FALSE(GarbageCollect(base::Milliseconds(90), 0));
3088   CheckExpectedRangesByTimestamp("{ [90,270) }");
3089   CheckExpectedBuffers("90K 120 150 180K 210 240");
3090   CheckNoNextBuffer();
3091 }
3092
3093 // Test saving the last GOP appended when it is at the beginning or end of the
3094 // selected range. This tests when the last GOP appended is before or after the
3095 // GOP containing the next buffer, but not directly adjacent to this GOP.
3096 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected2) {
3097   // Append 4 GOPs starting at positions 0ms, 90ms, 180ms, 270ms.
3098   NewCodedFrameGroupAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330");
3099   CheckExpectedRangesByTimestamp("{ [0,360) }");
3100
3101   // Seek to the last GOP at 270ms.
3102   SeekToTimestampMs(270);
3103
3104   // Set the memory limit to 1, then overlap the GOP at 90ms.
3105   SetMemoryLimit(1);
3106   NewCodedFrameGroupAppend("90K 120 150");
3107
3108   // GC will save data in the range where the most recent append has happened
3109   // [0; 180) and the range where the next read position is [270;360)
3110   EXPECT_FALSE(GarbageCollect(base::Milliseconds(270), 0));
3111   CheckExpectedRangesByTimestamp("{ [0,180) [270,360) }");
3112
3113   // Add 3 GOPs to the end of the selected range at 360ms, 450ms, and 540ms.
3114   NewCodedFrameGroupAppend("360K 390 420 450K 480 510 540K 570 600");
3115   CheckExpectedRangesByTimestamp("{ [0,180) [270,630) }");
3116
3117   // Overlap the GOP at 450ms and garbage collect to test deleting from the
3118   // back.
3119   NewCodedFrameGroupAppend("450K 480 510");
3120   EXPECT_FALSE(GarbageCollect(base::Milliseconds(270), 0));
3121
3122   // Should save GOPs from GOP at 270ms to GOP at 450ms.
3123   CheckExpectedRangesByTimestamp("{ [270,540) }");
3124 }
3125
3126 // Test saving the last GOP appended when it is the same as the GOP containing
3127 // the next buffer.
3128 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected3) {
3129   // Seek to start of stream.
3130   SeekToTimestampMs(0);
3131
3132   // Append 3 GOPs starting at 0ms, 90ms, 180ms.
3133   NewCodedFrameGroupAppend("0K 30 60 90K 120 150 180K 210 240");
3134   CheckExpectedRangesByTimestamp("{ [0,270) }");
3135
3136   // Set the memory limit to 1 then begin appending the start of a GOP starting
3137   // at 0ms.
3138   SetMemoryLimit(1);
3139   NewCodedFrameGroupAppend("0K 30");
3140
3141   // GC should save the newly appended GOP, which is also the next GOP that
3142   // will be returned from the seek request.
3143   EXPECT_FALSE(GarbageCollect(base::Milliseconds(0), 0));
3144   CheckExpectedRangesByTimestamp("{ [0,60) }");
3145
3146   // Check the buffers in the range.
3147   CheckExpectedBuffers("0K 30");
3148   CheckNoNextBuffer();
3149
3150   // Continue appending to this buffer.
3151   AppendBuffers("60 90");
3152
3153   // GC should still save the rest of this GOP and should be able to fulfill
3154   // the read.
3155   EXPECT_FALSE(GarbageCollect(base::Milliseconds(0), 0));
3156   CheckExpectedRangesByTimestamp("{ [0,120) }");
3157   CheckExpectedBuffers("60 90");
3158   CheckNoNextBuffer();
3159 }
3160
3161 // Test the performance of garbage collection.
3162 TEST_F(SourceBufferStreamTest, GarbageCollection_Performance) {
3163   // Force |keyframes_per_second_| to be equal to kDefaultFramesPerSecond.
3164   SetStreamInfo(kDefaultFramesPerSecond, kDefaultFramesPerSecond);
3165
3166   const int kBuffersToKeep = 1000;
3167   SetMemoryLimit(kBuffersToKeep);
3168
3169   int buffers_appended = 0;
3170
3171   NewCodedFrameGroupAppend(0, kBuffersToKeep);
3172   buffers_appended += kBuffersToKeep;
3173
3174   const int kBuffersToAppend = 1000;
3175   const int kGarbageCollections = 3;
3176   for (int i = 0; i < kGarbageCollections; ++i) {
3177     AppendBuffers(buffers_appended, kBuffersToAppend);
3178     buffers_appended += kBuffersToAppend;
3179   }
3180 }
3181
3182 TEST_F(SourceBufferStreamTest, GarbageCollection_MediaTimeAfterLastAppendTime) {
3183   // Set memory limit to 10 buffers.
3184   SetMemoryLimit(10);
3185
3186   // Append 12 buffers. The duration of the last buffer is 30
3187   NewCodedFrameGroupAppend("0K 30 60 90 120K 150 180 210K 240 270 300K 330D30");
3188   CheckExpectedRangesByTimestamp("{ [0,360) }");
3189
3190   // Do a garbage collection with the media time higher than the timestamp of
3191   // the last appended buffer (330), but still within buffered ranges, taking
3192   // into account the duration of the last frame (timestamp of the last frame is
3193   // 330, duration is 30, so the latest valid buffered position is 330+30=360).
3194   EXPECT_TRUE(GarbageCollect(base::Milliseconds(360), 0));
3195
3196   // GC should collect one GOP from the front to bring us back under memory
3197   // limit of 10 buffers.
3198   CheckExpectedRangesByTimestamp("{ [120,360) }");
3199 }
3200
3201 TEST_F(SourceBufferStreamTest,
3202        GarbageCollection_MediaTimeOutsideOfStreamBufferedRange) {
3203   // Set memory limit to 10 buffers.
3204   SetMemoryLimit(10);
3205
3206   // Append 12 buffers.
3207   NewCodedFrameGroupAppend("0K 30 60 90 120K 150 180 210K 240 270 300K 330");
3208   CheckExpectedRangesByTimestamp("{ [0,360) }");
3209
3210   // Seek in order to set the stream read position to 330 an ensure that the
3211   // stream selects the buffered range.
3212   SeekToTimestampMs(330);
3213
3214   // Do a garbage collection with the media time outside the buffered ranges
3215   // (this might happen when there's both audio and video streams, audio stream
3216   // buffered range is longer than the video stream buffered range, since
3217   // media::Pipeline uses audio stream as a time source in that case, it might
3218   // return a media_time that is slightly outside of video buffered range). In
3219   // those cases the GC algorithm should clamp the media_time value to the
3220   // buffered ranges to work correctly (see crbug.com/563292).
3221   EXPECT_TRUE(GarbageCollect(base::Milliseconds(361), 0));
3222
3223   // GC should collect one GOP from the front to bring us back under memory
3224   // limit of 10 buffers.
3225   CheckExpectedRangesByTimestamp("{ [120,360) }");
3226 }
3227
3228 TEST_F(SourceBufferStreamTest, GetRemovalRange_BytesToFree) {
3229   // Append 2 GOPs starting at 300ms, 30ms apart.
3230   NewCodedFrameGroupAppend("300K 330 360 390K 420 450");
3231
3232   // Append 2 GOPs starting at 600ms, 30ms apart.
3233   NewCodedFrameGroupAppend("600K 630 660 690K 720 750");
3234
3235   // Append 2 GOPs starting at 900ms, 30ms apart.
3236   NewCodedFrameGroupAppend("900K 930 960 990K 1020 1050");
3237
3238   CheckExpectedRangesByTimestamp("{ [300,480) [600,780) [900,1080) }");
3239
3240   int remove_range_end = -1;
3241   int bytes_removed = -1;
3242
3243   // Size 0.
3244   bytes_removed = GetRemovalRangeInMs(300, 1080, 0, &remove_range_end);
3245   EXPECT_EQ(-1, remove_range_end);
3246   EXPECT_EQ(0, bytes_removed);
3247
3248   // Smaller than the size of GOP.
3249   bytes_removed = GetRemovalRangeInMs(300, 1080, 1, &remove_range_end);
3250   EXPECT_EQ(390, remove_range_end);
3251   // Remove as the size of GOP.
3252   EXPECT_EQ(3, bytes_removed);
3253
3254   // The same size with a GOP.
3255   bytes_removed = GetRemovalRangeInMs(300, 1080, 3, &remove_range_end);
3256   EXPECT_EQ(390, remove_range_end);
3257   EXPECT_EQ(3, bytes_removed);
3258
3259   // The same size with a range.
3260   bytes_removed = GetRemovalRangeInMs(300, 1080, 6, &remove_range_end);
3261   EXPECT_EQ(480, remove_range_end);
3262   EXPECT_EQ(6, bytes_removed);
3263
3264   // A frame larger than a range.
3265   bytes_removed = GetRemovalRangeInMs(300, 1080, 7, &remove_range_end);
3266   EXPECT_EQ(690, remove_range_end);
3267   EXPECT_EQ(9, bytes_removed);
3268
3269   // The same size with two ranges.
3270   bytes_removed = GetRemovalRangeInMs(300, 1080, 12, &remove_range_end);
3271   EXPECT_EQ(780, remove_range_end);
3272   EXPECT_EQ(12, bytes_removed);
3273
3274   // Larger than two ranges.
3275   bytes_removed = GetRemovalRangeInMs(300, 1080, 14, &remove_range_end);
3276   EXPECT_EQ(990, remove_range_end);
3277   EXPECT_EQ(15, bytes_removed);
3278
3279   // The same size with the whole ranges.
3280   bytes_removed = GetRemovalRangeInMs(300, 1080, 18, &remove_range_end);
3281   EXPECT_EQ(1080, remove_range_end);
3282   EXPECT_EQ(18, bytes_removed);
3283
3284   // Larger than the whole ranges.
3285   bytes_removed = GetRemovalRangeInMs(300, 1080, 20, &remove_range_end);
3286   EXPECT_EQ(1080, remove_range_end);
3287   EXPECT_EQ(18, bytes_removed);
3288 }
3289
3290 TEST_F(SourceBufferStreamTest, GetRemovalRange_Range) {
3291   // Append 2 GOPs starting at 300ms, 30ms apart.
3292   NewCodedFrameGroupAppend("300K 330 360 390K 420 450");
3293
3294   // Append 2 GOPs starting at 600ms, 30ms apart.
3295   NewCodedFrameGroupAppend("600K 630 660 690K 720 750");
3296
3297   // Append 2 GOPs starting at 900ms, 30ms apart.
3298   NewCodedFrameGroupAppend("900K 930 960 990K 1020 1050");
3299
3300   CheckExpectedRangesByTimestamp("{ [300,480) [600,780) [900,1080) }");
3301
3302   int remove_range_end = -1;
3303   int bytes_removed = -1;
3304
3305   // Within a GOP and no keyframe.
3306   bytes_removed = GetRemovalRangeInMs(630, 660, 20, &remove_range_end);
3307   EXPECT_EQ(-1, remove_range_end);
3308   EXPECT_EQ(0, bytes_removed);
3309
3310   // Across a GOP and no keyframe.
3311   bytes_removed = GetRemovalRangeInMs(630, 750, 20, &remove_range_end);
3312   EXPECT_EQ(-1, remove_range_end);
3313   EXPECT_EQ(0, bytes_removed);
3314
3315   // The same size with a range.
3316   bytes_removed = GetRemovalRangeInMs(600, 780, 20, &remove_range_end);
3317   EXPECT_EQ(780, remove_range_end);
3318   EXPECT_EQ(6, bytes_removed);
3319
3320   // One frame larger than a range.
3321   bytes_removed = GetRemovalRangeInMs(570, 810, 20, &remove_range_end);
3322   EXPECT_EQ(780, remove_range_end);
3323   EXPECT_EQ(6, bytes_removed);
3324
3325   // Facing the other ranges.
3326   bytes_removed = GetRemovalRangeInMs(480, 900, 20, &remove_range_end);
3327   EXPECT_EQ(780, remove_range_end);
3328   EXPECT_EQ(6, bytes_removed);
3329
3330   // In the midle of the other ranges, but not including any GOP.
3331   bytes_removed = GetRemovalRangeInMs(420, 960, 20, &remove_range_end);
3332   EXPECT_EQ(780, remove_range_end);
3333   EXPECT_EQ(6, bytes_removed);
3334
3335   // In the middle of the other ranges.
3336   bytes_removed = GetRemovalRangeInMs(390, 990, 20, &remove_range_end);
3337   EXPECT_EQ(990, remove_range_end);
3338   EXPECT_EQ(12, bytes_removed);
3339
3340   // A frame smaller than the whole ranges.
3341   bytes_removed = GetRemovalRangeInMs(330, 1050, 20, &remove_range_end);
3342   EXPECT_EQ(990, remove_range_end);
3343   EXPECT_EQ(12, bytes_removed);
3344
3345   // The same with the whole ranges.
3346   bytes_removed = GetRemovalRangeInMs(300, 1080, 20, &remove_range_end);
3347   EXPECT_EQ(1080, remove_range_end);
3348   EXPECT_EQ(18, bytes_removed);
3349
3350   // Larger than the whole ranges.
3351   bytes_removed = GetRemovalRangeInMs(270, 1110, 20, &remove_range_end);
3352   EXPECT_EQ(1080, remove_range_end);
3353   EXPECT_EQ(18, bytes_removed);
3354 }
3355
3356 TEST_F(SourceBufferStreamTest, IsNextBufferConfigChanged) {
3357   // selected_range_ is nullptr, so return false
3358   EXPECT_FALSE(stream_->IsNextBufferConfigChanged());
3359   VideoDecoderConfig new_config = TestVideoConfig::Large();
3360   ASSERT_FALSE(new_config.Matches(video_config_));
3361
3362   // read all buffers
3363   NewCodedFrameGroupAppend("0K 10 20");
3364   Seek(0);
3365   CheckExpectedRangesByTimestamp("{ [0,30) }");
3366   CheckExpectedBuffers("0K 10 20");
3367   EXPECT_FALSE(stream_->IsNextBufferConfigChanged());
3368
3369   // Signal a config change.
3370   stream_->UpdateVideoConfig(new_config, false);
3371   NewCodedFrameGroupAppend("30K 40");
3372   EXPECT_TRUE(stream_->IsNextBufferConfigChanged());
3373
3374   scoped_refptr<StreamParserBuffer> buffer;
3375   EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
3376   CheckVideoConfig(new_config);
3377
3378   // Overlap-append
3379   NewCodedFrameGroupAppend(
3380       "21K 41 51 61 71 81 91 101 111 121 "
3381       "131K 141");
3382   CheckExpectedRangesByTimestamp("{ [0,151) }");
3383
3384   // track_buffer has the buffers with timestamp 30 and 40
3385   EXPECT_FALSE(stream_->IsNextBufferConfigChanged());
3386 }
3387
3388 TEST_F(SourceBufferStreamTest, ConfigChange_Basic) {
3389   VideoDecoderConfig new_config = TestVideoConfig::Large();
3390   ASSERT_FALSE(new_config.Matches(video_config_));
3391   Seek(0);
3392   CheckVideoConfig(video_config_);
3393
3394   // Append 5 buffers at positions 0 through 4
3395   NewCodedFrameGroupAppend(0, 5, &kDataA);
3396
3397   CheckVideoConfig(video_config_);
3398
3399   // Signal a config change.
3400   stream_->UpdateVideoConfig(new_config, false);
3401
3402   // Make sure updating the config doesn't change anything since new_config
3403   // should not be associated with the buffer GetNextBuffer() will return.
3404   CheckVideoConfig(video_config_);
3405
3406   // Append 5 buffers at positions 5 through 9.
3407   NewCodedFrameGroupAppend(5, 5, &kDataB);
3408
3409   // Consume the buffers associated with the initial config.
3410   scoped_refptr<StreamParserBuffer> buffer;
3411   for (int i = 0; i < 5; i++) {
3412     EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
3413     CheckVideoConfig(video_config_);
3414   }
3415
3416   // Verify the next attempt to get a buffer will signal that a config change
3417   // has happened.
3418   EXPECT_TRUE(stream_->IsNextBufferConfigChanged());
3419   EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
3420
3421   // Verify that the new config is now returned.
3422   CheckVideoConfig(new_config);
3423
3424   // Consume the remaining buffers associated with the new config.
3425   for (int i = 0; i < 5; i++) {
3426     CheckVideoConfig(new_config);
3427     EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
3428   }
3429 }
3430
3431 TEST_F(SourceBufferStreamTest, ConfigChange_Seek) {
3432   scoped_refptr<StreamParserBuffer> buffer;
3433   VideoDecoderConfig new_config = TestVideoConfig::Large();
3434
3435   Seek(0);
3436   NewCodedFrameGroupAppend(0, 5, &kDataA);
3437   stream_->UpdateVideoConfig(new_config, false);
3438   NewCodedFrameGroupAppend(5, 5, &kDataB);
3439
3440   // Seek to the start of the buffers with the new config and make sure a
3441   // config change is signalled.
3442   CheckVideoConfig(video_config_);
3443   Seek(5);
3444   CheckVideoConfig(video_config_);
3445   EXPECT_TRUE(stream_->IsNextBufferConfigChanged());
3446   EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
3447   CheckVideoConfig(new_config);
3448   CheckExpectedBuffers(5, 9, &kDataB);
3449
3450
3451   // Seek to the start which has a different config. Don't fetch any buffers and
3452   // seek back to buffers with the current config. Make sure a config change
3453   // isn't signalled in this case.
3454   CheckVideoConfig(new_config);
3455   Seek(0);
3456   Seek(7);
3457   CheckExpectedBuffers(5, 9, &kDataB);
3458
3459
3460   // Seek to the start and make sure a config change is signalled.
3461   CheckVideoConfig(new_config);
3462   Seek(0);
3463   CheckVideoConfig(new_config);
3464   EXPECT_TRUE(stream_->IsNextBufferConfigChanged());
3465   EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
3466   CheckVideoConfig(video_config_);
3467   CheckExpectedBuffers(0, 4, &kDataA);
3468 }
3469
3470 TEST_F(SourceBufferStreamTest, SetExplicitDuration) {
3471   // Append 3 discontinuous partial GOPs.
3472   NewCodedFrameGroupAppend("50K 90|60");
3473   NewCodedFrameGroupAppend("150K 190|160");
3474   NewCodedFrameGroupAppend("250K 290|260");
3475
3476   CheckExpectedRangesByTimestamp("{ [50,100) [150,200) [250,300) }");
3477
3478   // Set duration to be 80ms. Truncates the buffered data after 80ms.
3479   stream_->OnSetDuration(base::Milliseconds(80));
3480
3481   // The simulated P-frame at PTS 90ms should have been
3482   // removed by the duration truncation. Only the frame at PTS 50ms should
3483   // remain.
3484   CheckExpectedRangesByTimestamp("{ [50,60) }");
3485
3486   // Adding data past the previous duration should still work.
3487   NewCodedFrameGroupAppend("0D50K 50 100K");
3488   CheckExpectedRangesByTimestamp("{ [0,150) }");
3489 }
3490
3491 TEST_F(SourceBufferStreamTest, SetExplicitDuration_EdgeCase) {
3492   // Append 10 buffers at positions 10 through 19.
3493   NewCodedFrameGroupAppend(10, 10);
3494
3495   // Append 5 buffers at positions 25 through 29.
3496   NewCodedFrameGroupAppend(25, 5);
3497
3498   // Check expected ranges.
3499   CheckExpectedRanges("{ [10,19) [25,29) }");
3500
3501   // Set duration to be right before buffer 25.
3502   stream_->OnSetDuration(frame_duration() * 25);
3503
3504   // Should truncate the last range.
3505   CheckExpectedRanges("{ [10,19) }");
3506 }
3507
3508 TEST_F(SourceBufferStreamTest, SetExplicitDuration_EdgeCase2) {
3509   // This test requires specific relative proportions for fudge room, append
3510   // size, and duration truncation amounts. See details at:
3511   // https://codereview.chromium.org/2385423002
3512
3513   // Append buffers with first buffer establishing max_inter_buffer_distance
3514   // of 5 ms. This translates to a fudge room (2 x max_interbuffer_distance) of
3515   // 10 ms.
3516   NewCodedFrameGroupAppend("0K 5K 9D4K");
3517   CheckExpectedRangesByTimestamp("{ [0,13) }");
3518
3519   // Trim off last 2 buffers, totaling 8 ms. Notably less than the current fudge
3520   // room of 10 ms.
3521   stream_->OnSetDuration(base::Milliseconds(5));
3522
3523   // Verify truncation.
3524   CheckExpectedRangesByTimestamp("{ [0,5) }");
3525
3526   // Append new buffers just beyond the fudge-room allowance of 10ms.
3527   AppendBuffers("11K 15K");
3528
3529   // Verify new append creates a gap.
3530   CheckExpectedRangesByTimestamp("{ [0,5) [11,19) }");
3531 }
3532
3533 TEST_F(SourceBufferStreamTest, RemoveWithinFudgeRoom) {
3534   // This test requires specific relative proportions for fudge room, append
3535   // size, and removal amounts. See details at:
3536   // https://codereview.chromium.org/2385423002
3537
3538   // Append buffers with first buffer establishing max_inter_buffer_distance
3539   // of 5 ms. This translates to a fudge room (2 x max_interbuffer_distance) of
3540   // 10 ms.
3541   NewCodedFrameGroupAppend("0K 5K 9D4K");
3542   CheckExpectedRangesByTimestamp("{ [0,13) }");
3543
3544   // Trim off last 2 buffers, totaling 8 ms. Notably less than the current fudge
3545   // room of 10 ms.
3546   RemoveInMs(5, 13, 13);
3547
3548   // Verify removal.
3549   CheckExpectedRangesByTimestamp("{ [0,5) }");
3550
3551   // Append new buffers just beyond the fudge-room allowance of 10ms.
3552   AppendBuffers("11K 15K");
3553
3554   // Verify new append creates a gap.
3555   CheckExpectedRangesByTimestamp("{ [0,5) [11,19) }");
3556 }
3557
3558 TEST_F(SourceBufferStreamTest, SetExplicitDuration_DeletePartialRange) {
3559   // Append IPBBB GOPs into 3 discontinuous ranges.
3560   NewCodedFrameGroupAppend("0K 40|10 10|20 20|30 30|40");
3561   NewCodedFrameGroupAppend(
3562       "100K 140|110 110|120 120|130 130|140 "
3563       "150K 190|160 160|170 170|180 180|190");
3564   NewCodedFrameGroupAppend("250K 290|260 260|270 270|280 280|290");
3565
3566   // Check expected ranges.
3567   CheckExpectedRangesByTimestamp("{ [0,50) [100,200) [250,300) }");
3568
3569   stream_->OnSetDuration(base::Milliseconds(140));
3570
3571   // The B-frames at PTS 110-130 were in the GOP in decode order after
3572   // the simulated P-frame at PTS 140 which was truncated, so those B-frames
3573   // are also removed.
3574   CheckExpectedRangesByTimestamp("{ [0,50) [100,110) }");
3575 }
3576
3577 TEST_F(SourceBufferStreamTest, SetExplicitDuration_DeleteSelectedRange) {
3578   // Append 3 discontinuous partial GOPs.
3579   NewCodedFrameGroupAppend("50K 90|60");
3580   NewCodedFrameGroupAppend("150K 190|160");
3581   NewCodedFrameGroupAppend("250K 290|260");
3582
3583   CheckExpectedRangesByTimestamp("{ [50,100) [150,200) [250,300) }");
3584
3585   SeekToTimestampMs(150);
3586
3587   // Set duration to 50ms.
3588   stream_->OnSetDuration(base::Milliseconds(50));
3589
3590   // Expect everything to be deleted, and should not have next buffer anymore.
3591   CheckNoNextBuffer();
3592   CheckExpectedRangesByTimestamp("{ }");
3593
3594   // Appending data 0ms through 250ms should not fulfill the seek.
3595   // (If the duration is set to be something smaller than the current seek
3596   // point, which had been 150ms, then the seek point is reset and the
3597   // SourceBufferStream waits for a new seek request. Therefore even if the data
3598   // is re-appended, it should not fulfill the old seek.)
3599   NewCodedFrameGroupAppend("0K 50K 100K 150K 200K");
3600   CheckNoNextBuffer();
3601   CheckExpectedRangesByTimestamp("{ [0,250) }");
3602 }
3603
3604 TEST_F(SourceBufferStreamTest, SetExplicitDuration_DeletePartialSelectedRange) {
3605   // Append 5 buffers at positions 0 through 4.
3606   NewCodedFrameGroupAppend(0, 5);
3607
3608   // Append 20 buffers at positions 10 through 29.
3609   NewCodedFrameGroupAppend(10, 20);
3610
3611   // Check expected ranges.
3612   CheckExpectedRanges("{ [0,4) [10,29) }");
3613
3614   // Seek to position 10.
3615   Seek(10);
3616
3617   // Set duration to be between buffers 24 and 25.
3618   stream_->OnSetDuration(frame_duration() * 25);
3619
3620   // Should truncate the data after 24.
3621   CheckExpectedRanges("{ [0,4) [10,24) }");
3622
3623   // The seek position should not be lost.
3624   CheckExpectedBuffers(10, 10);
3625
3626   // Now set the duration immediately after buffer 10.
3627   stream_->OnSetDuration(frame_duration() * 11);
3628
3629   // Seek position should be reset.
3630   CheckNoNextBuffer();
3631   CheckExpectedRanges("{ [0,4) [10,10) }");
3632 }
3633
3634 // Test the case where duration is set while the stream parser buffers
3635 // already start passing the data to decoding pipeline. Selected range,
3636 // when invalidated by getting truncated, should be updated to NULL
3637 // accordingly so that successive append operations keep working.
3638 TEST_F(SourceBufferStreamTest, SetExplicitDuration_UpdateSelectedRange) {
3639   // Seek to start of stream.
3640   SeekToTimestampMs(0);
3641
3642   NewCodedFrameGroupAppend("0K 30 60 90");
3643
3644   // Read out the first few buffers.
3645   CheckExpectedBuffers("0K 30");
3646
3647   // Set duration to be right before buffer 1.
3648   stream_->OnSetDuration(base::Milliseconds(60));
3649
3650   // Verify that there is no next buffer.
3651   CheckNoNextBuffer();
3652
3653   // We should be able to append new buffers at this point.
3654   NewCodedFrameGroupAppend("120K 150");
3655
3656   CheckExpectedRangesByTimestamp("{ [0,60) [120,180) }");
3657 }
3658
3659 TEST_F(SourceBufferStreamTest,
3660        SetExplicitDuration_AfterGroupTimestampAndBeforeFirstBufferTimestamp) {
3661   NewCodedFrameGroupAppend("0K 30K 60K");
3662
3663   // Append a coded frame group with a start timestamp of 200, but the first
3664   // buffer starts at 230ms. This can happen in muxed content where the
3665   // audio starts before the first frame.
3666   NewCodedFrameGroupAppend(base::Milliseconds(200), "230K 260K 290K 320K");
3667
3668   NewCodedFrameGroupAppend("400K 430K 460K");
3669
3670   CheckExpectedRangesByTimestamp("{ [0,90) [200,350) [400,490) }");
3671
3672   stream_->OnSetDuration(base::Milliseconds(120));
3673
3674   // Verify that the buffered ranges are updated properly and we don't crash.
3675   CheckExpectedRangesByTimestamp("{ [0,90) }");
3676 }
3677
3678 TEST_F(SourceBufferStreamTest, SetExplicitDuration_MarkEOS) {
3679   // Append 1 full and 1 partial GOP: IPBBBIPBB
3680   NewCodedFrameGroupAppend(
3681       "0K 40|10 10|20 20|30 30|40 "
3682       "50K 90|60 60|70 70|80");
3683
3684   CheckExpectedRangesByTimestamp("{ [0,100) }");
3685
3686   SeekToTimestampMs(50);
3687
3688   // Set duration to be before the seeked to position.
3689   // This will result in truncation of the selected range and a reset
3690   // of NextBufferPosition.
3691   stream_->OnSetDuration(base::Milliseconds(40));
3692
3693   // The P-frame at PTS 40ms was removed, so its dependent B-frames at PTS 10-30
3694   // were also removed.
3695   CheckExpectedRangesByTimestamp("{ [0,10) }");
3696
3697   // Mark EOS reached.
3698   stream_->MarkEndOfStream();
3699
3700   // Expect EOS to be reached.
3701   CheckEOSReached();
3702 }
3703
3704 TEST_F(SourceBufferStreamTest, SetExplicitDuration_MarkEOS_IsSeekPending) {
3705   // Append 1 full and 1 partial GOP: IPBBBIPBB
3706   NewCodedFrameGroupAppend(
3707       "0K 40|10 10|20 20|30 30|40 "
3708       "50K 90|60 60|70 70|80");
3709
3710   CheckExpectedRangesByTimestamp("{ [0,100) }");
3711
3712   // Seek to 100ms will result in a pending seek.
3713   SeekToTimestampMs(100);
3714
3715   // Set duration to be before the seeked to position.
3716   // This will result in truncation of the selected range and a reset
3717   // of NextBufferPosition.
3718   stream_->OnSetDuration(base::Milliseconds(40));
3719
3720   // The P-frame at PTS 40ms was removed, so its dependent B-frames at PTS 10-30
3721   // were also removed.
3722   CheckExpectedRangesByTimestamp("{ [0,10) }");
3723
3724   EXPECT_TRUE(stream_->IsSeekPending());
3725   // Mark EOS reached.
3726   stream_->MarkEndOfStream();
3727   EXPECT_FALSE(stream_->IsSeekPending());
3728 }
3729
3730 // Test the case were the current playback position is at the end of the
3731 // buffered data and several overlaps occur.
3732 TEST_F(SourceBufferStreamTest, OverlapWhileWaitingForMoreData) {
3733   // Seek to start of stream.
3734   SeekToTimestampMs(0);
3735
3736   NewCodedFrameGroupAppend("0K 30 60 90 120K 150");
3737   CheckExpectedRangesByTimestamp("{ [0,180) }");
3738
3739   // Read all the buffered data.
3740   CheckExpectedBuffers("0K 30 60 90 120K 150");
3741   CheckNoNextBuffer();
3742
3743   // Append data over the current GOP so that a keyframe is needed before
3744   // playback can continue from the current position.
3745   NewCodedFrameGroupAppend("120K 150");
3746   CheckExpectedRangesByTimestamp("{ [0,180) }");
3747
3748   // Append buffers that replace the first GOP with a partial GOP.
3749   NewCodedFrameGroupAppend("0K 30");
3750   CheckExpectedRangesByTimestamp("{ [0,180) }");
3751
3752   // Append buffers that complete that partial GOP.
3753   AppendBuffers("60 90");
3754   CheckExpectedRangesByTimestamp("{ [0,180) }");
3755
3756   // Verify that we still don't have a next buffer.
3757   CheckNoNextBuffer();
3758
3759   // Add more data to the end and verify that this new data is read correctly.
3760   NewCodedFrameGroupAppend("180K 210");
3761   CheckExpectedRangesByTimestamp("{ [0,240) }");
3762   CheckExpectedBuffers("180K 210");
3763   CheckNoNextBuffer();
3764 }
3765
3766 // Verify that a single coded frame at the current read position unblocks the
3767 // read even if the frame is buffered after the previously read position is
3768 // removed.
3769 TEST_F(SourceBufferStreamTest, AfterRemove_SingleFrameRange_Unblocks_Read) {
3770   Seek(0);
3771   NewCodedFrameGroupAppend("0K 30 60 90D30");
3772   CheckExpectedRangesByTimestamp("{ [0,120) }");
3773   CheckExpectedBuffers("0K 30 60 90");
3774   CheckNoNextBuffer();
3775
3776   RemoveInMs(0, 120, 120);
3777   CheckExpectedRangesByTimestamp("{ }");
3778   NewCodedFrameGroupAppend("120D30K");
3779   CheckExpectedRangesByTimestamp("{ [120,150) }");
3780   CheckExpectedBuffers("120K");
3781   CheckNoNextBuffer();
3782 }
3783
3784 // Verify that multiple short (relative to max-inter-buffer-distance * 2) coded
3785 // frames at the current read position unblock the read even if the frames are
3786 // buffered after the previously read position is removed.
3787 TEST_F(SourceBufferStreamTest, AfterRemove_TinyFrames_Unblock_Read_1) {
3788   Seek(0);
3789   NewCodedFrameGroupAppend("0K 30 60 90D30");
3790   CheckExpectedRangesByTimestamp("{ [0,120) }");
3791   CheckExpectedBuffers("0K 30 60 90");
3792   CheckNoNextBuffer();
3793
3794   RemoveInMs(0, 120, 120);
3795   CheckExpectedRangesByTimestamp("{ }");
3796   NewCodedFrameGroupAppend("120D1K 121D1");
3797   CheckExpectedRangesByTimestamp("{ [120,122) }");
3798   CheckExpectedBuffers("120K 121");
3799   CheckNoNextBuffer();
3800 }
3801
3802 // Verify that multiple short (relative to max-inter-buffer-distance * 2) coded
3803 // frames starting at the fudge room boundary unblock the read even if the
3804 // frames are buffered after the previously read position is removed.
3805 TEST_F(SourceBufferStreamTest, AfterRemove_TinyFrames_Unblock_Read_2) {
3806   Seek(0);
3807   NewCodedFrameGroupAppend("0K 30 60 90D30");
3808   CheckExpectedRangesByTimestamp("{ [0,120) }");
3809   CheckExpectedBuffers("0K 30 60 90");
3810   CheckNoNextBuffer();
3811
3812   RemoveInMs(0, 120, 120);
3813   CheckExpectedRangesByTimestamp("{ }");
3814   NewCodedFrameGroupAppend("150D1K 151D1");
3815   CheckExpectedRangesByTimestamp("{ [150,152) }");
3816   CheckExpectedBuffers("150K 151");
3817   CheckNoNextBuffer();
3818 }
3819
3820 // Verify that coded frames starting after the fudge room boundary do not
3821 // unblock the read when buffered after the previously read position is removed.
3822 TEST_F(SourceBufferStreamTest, AfterRemove_BeyondFudge_Stalled) {
3823   Seek(0);
3824   NewCodedFrameGroupAppend("0K 30 60 90D30");
3825   CheckExpectedRangesByTimestamp("{ [0,120) }");
3826   CheckExpectedBuffers("0K 30 60 90");
3827   CheckNoNextBuffer();
3828
3829   RemoveInMs(0, 120, 120);
3830   CheckExpectedRangesByTimestamp("{ }");
3831   NewCodedFrameGroupAppend("151D1K 152D1");
3832   CheckExpectedRangesByTimestamp("{ [151,153) }");
3833   CheckNoNextBuffer();
3834 }
3835
3836 // Verify that non-keyframes with the same timestamp in the same
3837 // append are handled correctly.
3838 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_SingleAppend) {
3839   Seek(0);
3840   NewCodedFrameGroupAppend("0K 30 30 60 90 120K 150");
3841   CheckExpectedBuffers("0K 30 30 60 90 120K 150");
3842 }
3843
3844 // Verify that a non-keyframe followed by a keyframe with the same timestamp
3845 // is allowed.
3846 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_SingleAppend2) {
3847   Seek(0);
3848   NewCodedFrameGroupAppend("0K 30 30K 60");
3849   CheckExpectedBuffers("0K 30 30K 60");
3850 }
3851
3852 // Verify that non-keyframes with the same timestamp can occur
3853 // in different appends.
3854 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_TwoAppends) {
3855   Seek(0);
3856   NewCodedFrameGroupAppend("0K 30D0");
3857   AppendBuffers("30 60 90 120K 150");
3858   CheckExpectedBuffers("0K 30 30 60 90 120K 150");
3859 }
3860
3861 // Verify that a non-keyframe followed by a keyframe with the same timestamp
3862 // is allowed.
3863 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_TwoAppends2) {
3864   Seek(0);
3865   NewCodedFrameGroupAppend("0K 30D0");
3866   AppendBuffers("30K 60");
3867   CheckExpectedBuffers("0K 30 30K 60");
3868 }
3869
3870 // Verify that a keyframe followed by a non-keyframe with the same timestamp
3871 // is allowed.
3872 TEST_F(SourceBufferStreamTest, SameTimestamp_VideoKeyFrame_TwoAppends) {
3873   Seek(0);
3874   NewCodedFrameGroupAppend("0K 30D0K");
3875   AppendBuffers("30 60");
3876   CheckExpectedBuffers("0K 30K 30 60");
3877 }
3878
3879 TEST_F(SourceBufferStreamTest, SameTimestamp_VideoKeyFrame_SingleAppend) {
3880   Seek(0);
3881   NewCodedFrameGroupAppend("0K 30K 30 60");
3882   CheckExpectedBuffers("0K 30K 30 60");
3883 }
3884
3885 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Overlap_1) {
3886   Seek(0);
3887   NewCodedFrameGroupAppend("0K 30 60 60 90 120K 150");
3888
3889   NewCodedFrameGroupAppend("60K 91 121K 151");
3890   CheckExpectedBuffers("0K 30 60K 91 121K 151");
3891 }
3892
3893 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Overlap_2) {
3894   Seek(0);
3895   NewCodedFrameGroupAppend("0K 30 60 60 90 120K 150");
3896   NewCodedFrameGroupAppend("0K 30 61");
3897   CheckExpectedBuffers("0K 30 61 120K 150");
3898 }
3899
3900 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Overlap_3) {
3901   Seek(0);
3902   NewCodedFrameGroupAppend("0K 20 40 60 80 100K 101 102 103K");
3903   NewCodedFrameGroupAppend("0K 20 40 60 80 90D0");
3904   CheckExpectedBuffers("0K 20 40 60 80 90 100K 101 102 103K");
3905   AppendBuffers("90 110K 150");
3906   Seek(0);
3907   CheckExpectedBuffers("0K 20 40 60 80 90 90 110K 150");
3908   CheckNoNextBuffer();
3909   CheckExpectedRangesByTimestamp("{ [0,190) }");
3910 }
3911
3912 // Test all the valid same timestamp cases for audio.
3913 TEST_F(SourceBufferStreamTest, SameTimestamp_Audio) {
3914   AudioDecoderConfig config(AudioCodec::kMP3, kSampleFormatF32,
3915                             CHANNEL_LAYOUT_STEREO, 44100, EmptyExtraData(),
3916                             EncryptionScheme::kUnencrypted);
3917   ResetStream<>(config);
3918   Seek(0);
3919   NewCodedFrameGroupAppend("0K 0K 30K 30K");
3920   CheckExpectedBuffers("0K 0K 30K 30K");
3921 }
3922
3923 // If seeking past any existing range and the seek is pending
3924 // because no data has been provided for that position,
3925 // the stream position can be considered as the end of stream.
3926 TEST_F(SourceBufferStreamTest, EndSelected_During_PendingSeek) {
3927   // Append 15 buffers at positions 0 through 14.
3928   NewCodedFrameGroupAppend(0, 15);
3929
3930   Seek(20);
3931   EXPECT_TRUE(stream_->IsSeekPending());
3932   stream_->MarkEndOfStream();
3933   EXPECT_FALSE(stream_->IsSeekPending());
3934 }
3935
3936 // If there is a pending seek between 2 existing ranges,
3937 // the end of the stream has not been reached.
3938 TEST_F(SourceBufferStreamTest, EndNotSelected_During_PendingSeek) {
3939   // Append:
3940   // - 10 buffers at positions 0 through 9.
3941   // - 10 buffers at positions 30 through 39
3942   NewCodedFrameGroupAppend(0, 10);
3943   NewCodedFrameGroupAppend(30, 10);
3944
3945   Seek(20);
3946   EXPECT_TRUE(stream_->IsSeekPending());
3947   stream_->MarkEndOfStream();
3948   EXPECT_TRUE(stream_->IsSeekPending());
3949 }
3950
3951 // Removing exact start & end of a range.
3952 TEST_F(SourceBufferStreamTest, Remove_WholeRange1) {
3953   Seek(0);
3954   NewCodedFrameGroupAppend("10K 40 70K 100 130K");
3955   CheckExpectedRangesByTimestamp("{ [10,160) }");
3956   RemoveInMs(10, 160, 160);
3957   CheckExpectedRangesByTimestamp("{ }");
3958 }
3959
3960 // Removal range starts before range and ends exactly at end.
3961 TEST_F(SourceBufferStreamTest, Remove_WholeRange2) {
3962   Seek(0);
3963   NewCodedFrameGroupAppend("10K 40 70K 100 130K");
3964   CheckExpectedRangesByTimestamp("{ [10,160) }");
3965   RemoveInMs(0, 160, 160);
3966   CheckExpectedRangesByTimestamp("{ }");
3967 }
3968
3969 // Removal range starts at the start of a range and ends beyond the
3970 // range end.
3971 TEST_F(SourceBufferStreamTest, Remove_WholeRange3) {
3972   Seek(0);
3973   NewCodedFrameGroupAppend("10K 40 70K 100 130K");
3974   CheckExpectedRangesByTimestamp("{ [10,160) }");
3975   RemoveInMs(10, 200, 200);
3976   CheckExpectedRangesByTimestamp("{ }");
3977 }
3978
3979 // Removal range starts before range start and ends after the range end.
3980 TEST_F(SourceBufferStreamTest, Remove_WholeRange4) {
3981   Seek(0);
3982   NewCodedFrameGroupAppend("10K 40 70K 100 130K");
3983   CheckExpectedRangesByTimestamp("{ [10,160) }");
3984   RemoveInMs(0, 200, 200);
3985   CheckExpectedRangesByTimestamp("{ }");
3986 }
3987
3988 // Removes multiple ranges.
3989 TEST_F(SourceBufferStreamTest, Remove_WholeRange5) {
3990   Seek(0);
3991   NewCodedFrameGroupAppend("10K 40 70K 100 130K");
3992   NewCodedFrameGroupAppend("1000K 1030 1060K 1090 1120K");
3993   NewCodedFrameGroupAppend("2000K 2030 2060K 2090 2120K");
3994   CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) [2000,2150) }");
3995   RemoveInMs(10, 3000, 3000);
3996   CheckExpectedRangesByTimestamp("{ }");
3997 }
3998
3999 // Verifies a [0-infinity) range removes everything.
4000 TEST_F(SourceBufferStreamTest, Remove_ZeroToInfinity) {
4001   Seek(0);
4002   NewCodedFrameGroupAppend("10K 40 70K 100 130K");
4003   NewCodedFrameGroupAppend("1000K 1030 1060K 1090 1120K");
4004   NewCodedFrameGroupAppend("2000K 2030 2060K 2090 2120K");
4005   CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) [2000,2150) }");
4006   Remove(base::TimeDelta(), kInfiniteDuration, kInfiniteDuration);
4007   CheckExpectedRangesByTimestamp("{ }");
4008 }
4009
4010 // Removal range starts at the beginning of the range and ends in the
4011 // middle of the range. This test verifies that full GOPs are removed.
4012 TEST_F(SourceBufferStreamTest, Remove_Partial1) {
4013   Seek(0);
4014   NewCodedFrameGroupAppend("10K 40 70K 100 130K");
4015   NewCodedFrameGroupAppend("1000K 1030 1060K 1090 1120K");
4016   CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) }");
4017   RemoveInMs(0, 80, 2200);
4018   CheckExpectedRangesByTimestamp("{ [130,160) [1000,1150) }");
4019 }
4020
4021 // Removal range starts in the middle of a range and ends at the exact
4022 // end of the range.
4023 TEST_F(SourceBufferStreamTest, Remove_Partial2) {
4024   Seek(0);
4025   NewCodedFrameGroupAppend("10K 40 70K 100 130K");
4026   NewCodedFrameGroupAppend("1000K 1030 1060K 1090 1120K");
4027   CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) }");
4028   RemoveInMs(40, 160, 2200);
4029   CheckExpectedRangesByTimestamp("{ [10,40) [1000,1150) }");
4030 }
4031
4032 // Removal range starts and ends within a range.
4033 TEST_F(SourceBufferStreamTest, Remove_Partial3) {
4034   Seek(0);
4035   NewCodedFrameGroupAppend("10K 40 70K 100 130K");
4036   NewCodedFrameGroupAppend("1000K 1030 1060K 1090 1120K");
4037   CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) }");
4038   RemoveInMs(40, 120, 2200);
4039   CheckExpectedRangesByTimestamp("{ [10,40) [130,160) [1000,1150) }");
4040 }
4041
4042 // Removal range starts in the middle of one range and ends in the
4043 // middle of another range.
4044 TEST_F(SourceBufferStreamTest, Remove_Partial4) {
4045   Seek(0);
4046   NewCodedFrameGroupAppend("10K 40 70K 100 130K");
4047   NewCodedFrameGroupAppend("1000K 1030 1060K 1090 1120K");
4048   NewCodedFrameGroupAppend("2000K 2030 2060K 2090 2120K");
4049   CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) [2000,2150) }");
4050   RemoveInMs(40, 2030, 2200);
4051   CheckExpectedRangesByTimestamp("{ [10,40) [2060,2150) }");
4052 }
4053
4054 // Test behavior when the current position is removed and new buffers
4055 // are appended over the removal range.
4056 TEST_F(SourceBufferStreamTest, Remove_CurrentPosition) {
4057   Seek(0);
4058   NewCodedFrameGroupAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330");
4059   CheckExpectedRangesByTimestamp("{ [0,360) }");
4060   CheckExpectedBuffers("0K 30 60 90K 120");
4061
4062   // Remove a range that includes the next buffer (i.e., 150).
4063   RemoveInMs(150, 210, 360);
4064   CheckExpectedRangesByTimestamp("{ [0,150) [270,360) }");
4065
4066   // Verify that no next buffer is returned.
4067   CheckNoNextBuffer();
4068
4069   // Append some buffers to fill the gap that was created.
4070   NewCodedFrameGroupAppend("120K 150 180 210K 240");
4071   CheckExpectedRangesByTimestamp("{ [0,360) }");
4072
4073   // Verify that buffers resume at the next keyframe after the
4074   // current position.
4075   CheckExpectedBuffers("210K 240 270K 300 330");
4076 }
4077
4078 // Test behavior when buffers in the selected range before the current position
4079 // are removed.
4080 TEST_F(SourceBufferStreamTest, Remove_BeforeCurrentPosition) {
4081   Seek(0);
4082   NewCodedFrameGroupAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330");
4083   CheckExpectedRangesByTimestamp("{ [0,360) }");
4084   CheckExpectedBuffers("0K 30 60 90K 120");
4085
4086   // Remove a range that is before the current playback position.
4087   RemoveInMs(0, 90, 360);
4088   CheckExpectedRangesByTimestamp("{ [90,360) }");
4089
4090   CheckExpectedBuffers("150 180K 210 240 270K 300 330");
4091 }
4092
4093 // Test removing the preliminary portion for the current coded frame group being
4094 // appended.
4095 TEST_F(SourceBufferStreamTest, Remove_MidGroup) {
4096   Seek(0);
4097   NewCodedFrameGroupAppend("0K 30 60 90 120K 150 180 210");
4098   CheckExpectedRangesByTimestamp("{ [0,240) }");
4099
4100   // Partially replace the first GOP, then read its keyframe.
4101   NewCodedFrameGroupAppend("0K 30");
4102   CheckExpectedBuffers("0K");
4103
4104   CheckExpectedRangesByTimestamp("{ [0,240) }");
4105
4106   // Remove the partial GOP that we're in the middle of reading.
4107   RemoveInMs(0, 60, 240);
4108
4109   // Verify that there is no next buffer since it was removed and the remaining
4110   // buffered range is beyond the current position.
4111   CheckNoNextBuffer();
4112   CheckExpectedRangesByTimestamp("{ [120,240) }");
4113
4114   // Continue appending frames for the current GOP.
4115   AppendBuffers("60 90");
4116
4117   // Verify that the non-keyframes are not added.
4118   CheckExpectedRangesByTimestamp("{ [120,240) }");
4119
4120   // Finish the previous GOP and start the next one.
4121   AppendBuffers("120 150K 180");
4122
4123   // Verify that new GOP replaces the existing GOP.
4124   CheckExpectedRangesByTimestamp("{ [150,210) }");
4125   SeekToTimestampMs(150);
4126   CheckExpectedBuffers("150K 180");
4127   CheckNoNextBuffer();
4128 }
4129
4130 // Test removing the current GOP being appended, while not removing
4131 // the entire range the GOP belongs to.
4132 TEST_F(SourceBufferStreamTest, Remove_GOPBeingAppended) {
4133   Seek(0);
4134   NewCodedFrameGroupAppend("0K 30 60 90 120K 150 180");
4135   CheckExpectedRangesByTimestamp("{ [0,210) }");
4136
4137   // Remove the current GOP being appended.
4138   RemoveInMs(120, 150, 240);
4139   CheckExpectedRangesByTimestamp("{ [0,120) }");
4140
4141   // Continue appending the current GOP and the next one.
4142   AppendBuffers("210 240K 270 300");
4143
4144   // Verify that the non-keyframe in the previous GOP does
4145   // not effect any existing ranges and a new range is started at the
4146   // beginning of the next GOP.
4147   CheckExpectedRangesByTimestamp("{ [0,120) [240,330) }");
4148
4149   // Verify the buffers in the ranges.
4150   CheckExpectedBuffers("0K 30 60 90");
4151   CheckNoNextBuffer();
4152   SeekToTimestampMs(240);
4153   CheckExpectedBuffers("240K 270 300");
4154 }
4155
4156 TEST_F(SourceBufferStreamTest, Remove_WholeGOPBeingAppended) {
4157   SeekToTimestampMs(1000);
4158   NewCodedFrameGroupAppend("1000K 1030 1060 1090");
4159   CheckExpectedRangesByTimestamp("{ [1000,1120) }");
4160
4161   // Remove the keyframe of the current GOP being appended.
4162   RemoveInMs(1000, 1030, 1120);
4163   CheckExpectedRangesByTimestamp("{ }");
4164
4165   // Continue appending the current GOP.
4166   AppendBuffers("1210 1240");
4167
4168   CheckExpectedRangesByTimestamp("{ }");
4169
4170   // Append the beginning of the next GOP.
4171   AppendBuffers("1270K 1300");
4172
4173   // Verify that the new range is started at the
4174   // beginning of the next GOP.
4175   CheckExpectedRangesByTimestamp("{ [1270,1330) }");
4176
4177   // Verify the buffers in the ranges.
4178   CheckNoNextBuffer();
4179   SeekToTimestampMs(1270);
4180   CheckExpectedBuffers("1270K 1300");
4181 }
4182
4183 TEST_F(SourceBufferStreamTest,
4184        Remove_PreviousAppendDestroyedAndOverwriteExistingRange) {
4185   SeekToTimestampMs(90);
4186
4187   NewCodedFrameGroupAppend("90K 120 150");
4188   CheckExpectedRangesByTimestamp("{ [90,180) }");
4189
4190   // Append a coded frame group before the previously appended data.
4191   NewCodedFrameGroupAppend("0K 30 60");
4192
4193   // Verify that the ranges get merged.
4194   CheckExpectedRangesByTimestamp("{ [0,180) }");
4195
4196   // Remove the data from the last append.
4197   RemoveInMs(0, 90, 360);
4198   CheckExpectedRangesByTimestamp("{ [90,180) }");
4199
4200   // Append a new coded frame group that follows the removed group and
4201   // starts at the beginning of the range left over from the
4202   // remove.
4203   NewCodedFrameGroupAppend("90K 121 151");
4204   CheckExpectedBuffers("90K 121 151");
4205 }
4206
4207 TEST_F(SourceBufferStreamTest, Remove_GapAtBeginningOfGroup) {
4208   Seek(0);
4209
4210   // Append a coded frame group that has a gap at the beginning of it.
4211   NewCodedFrameGroupAppend(base::Milliseconds(0), "30K 60 90 120K 150");
4212   CheckExpectedRangesByTimestamp("{ [0,180) }");
4213
4214   // Remove the gap that doesn't contain any buffers.
4215   RemoveInMs(0, 10, 180);
4216   CheckExpectedRangesByTimestamp("{ [10,180) }");
4217
4218   // Verify we still get the first buffer still since only part of
4219   // the gap was removed.
4220   // TODO(acolwell/wolenetz): Consider not returning a buffer at this
4221   // point since the current seek position has been explicitly
4222   // removed but didn't happen to remove any buffers.
4223   // http://crbug.com/384016
4224   CheckExpectedBuffers("30K");
4225
4226   // Remove a range that includes the first GOP.
4227   RemoveInMs(0, 60, 180);
4228
4229   // Verify that no buffer is returned because the current buffer
4230   // position has been removed.
4231   CheckNoNextBuffer();
4232
4233   CheckExpectedRangesByTimestamp("{ [120,180) }");
4234 }
4235
4236 TEST_F(SourceBufferStreamTest,
4237        OverlappingAppendRangeMembership_OneMicrosecond_Video) {
4238   NewCodedFrameGroupAppend("10D20K");
4239   CheckExpectedRangesByTimestamp("{ [10000,30000) }",
4240                                  TimeGranularity::kMicrosecond);
4241
4242   // Append a buffer 1 microsecond earlier, with estimated duration.
4243   NewCodedFrameGroupAppend("9999uD20EK");
4244   CheckExpectedRangesByTimestamp("{ [9999,30000) }",
4245                                  TimeGranularity::kMicrosecond);
4246
4247   // Append that same buffer again, but without any discontinuity signalled / no
4248   // new coded frame group.
4249   AppendBuffers("9999uD20EK");
4250   CheckExpectedRangesByTimestamp("{ [9999,30000) }",
4251                                  TimeGranularity::kMicrosecond);
4252
4253   Seek(0);
4254   CheckExpectedBuffers("9999K 9999K 10000K", TimeGranularity::kMicrosecond);
4255   CheckNoNextBuffer();
4256 }
4257
4258 TEST_F(SourceBufferStreamTest,
4259        OverlappingAppendRangeMembership_TwoMicroseconds_Video) {
4260   NewCodedFrameGroupAppend("10D20K");
4261   CheckExpectedRangesByTimestamp("{ [10000,30000) }",
4262                                  TimeGranularity::kMicrosecond);
4263
4264   // Append an exactly abutting buffer 2us earlier.
4265   NewCodedFrameGroupAppend("9998uD20EK");
4266   CheckExpectedRangesByTimestamp("{ [9998,30000) }",
4267                                  TimeGranularity::kMicrosecond);
4268
4269   // Append that same buffer again, but without any discontinuity signalled / no
4270   // new coded frame group.
4271   AppendBuffers("9998uD20EK");
4272   CheckExpectedRangesByTimestamp("{ [9998,30000) }",
4273                                  TimeGranularity::kMicrosecond);
4274
4275   Seek(0);
4276   CheckExpectedBuffers("9998K 9998K 10000K", TimeGranularity::kMicrosecond);
4277   CheckNoNextBuffer();
4278 }
4279
4280 TEST_F(SourceBufferStreamTest, Audio_SpliceTrimmingForOverlap) {
4281   SetAudioStream();
4282   Seek(0);
4283   NewCodedFrameGroupAppend("0K 2K 4K 6K 8K 10K 12K");
4284   CheckExpectedRangesByTimestamp("{ [0,14) }");
4285   // Note that duration  of frame at time 10 is verified to be 2 ms.
4286   CheckExpectedBuffers("0K 2K 4K 6K 8K 10D2K 12K");
4287   CheckNoNextBuffer();
4288
4289   // Append new group with front slightly overlapping existing buffer at 10ms.
4290   EXPECT_MEDIA_LOG(TrimmedSpliceOverlap(11000, 10000, 1000));
4291   NewCodedFrameGroupAppend("11K 13K 15K 17K");
4292
4293   // Cross-fade splicing is no longer implemented. Instead we should expect
4294   // wholly overlapped buffers to be removed (12K). If a buffer is partially
4295   // overlapped (e.g. last millisecond of 10K), the existing buffer should be
4296   // trimmed to perfectly abut the newly appended buffers.
4297   Seek(0);
4298
4299   CheckExpectedRangesByTimestamp("{ [0,19) }");
4300   CheckExpectedBuffers("0K 2K 4K 6K 8K 10D1K 11D2K 13K 15K 17K");
4301   CheckNoNextBuffer();
4302 }
4303
4304 // Test that a splice is not created if an end timestamp and start timestamp
4305 // perfectly overlap.
4306 TEST_F(SourceBufferStreamTest, Audio_SpliceFrame_NoSplice) {
4307   SetAudioStream();
4308   Seek(0);
4309
4310   // Add 10 frames across 2 *non-overlapping* appends.
4311   NewCodedFrameGroupAppend("0K 2K 4K 6K 8K 10K");
4312   NewCodedFrameGroupAppend("12K 14K 16K 18K");
4313
4314   // Manually inspect the buffers at the no-splice boundary to verify duration
4315   // and lack of discard padding (set when splicing).
4316   scoped_refptr<StreamParserBuffer> buffer;
4317   const DecoderBuffer::DiscardPadding kEmptyDiscardPadding;
4318   for (int i = 0; i < 10; i++) {
4319     // Verify buffer timestamps and durations are preserved and no buffers have
4320     // discard padding (indicating no splice trimming).
4321     EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
4322     EXPECT_EQ(base::Milliseconds(i * 2), buffer->timestamp());
4323     EXPECT_EQ(base::Milliseconds(2), buffer->duration());
4324     EXPECT_EQ(kEmptyDiscardPadding, buffer->discard_padding());
4325   }
4326
4327   CheckNoNextBuffer();
4328 }
4329
4330 TEST_F(SourceBufferStreamTest, Audio_NoSpliceForBadOverlap) {
4331   SetAudioStream();
4332   Seek(0);
4333
4334   // Add 2 frames with matching PTS and ov, where the duration of the first
4335   // frame suggests that it overlaps the second frame. The overlap is within a
4336   // coded frame group (bad content), so no splicing is expected.
4337   NewCodedFrameGroupAppend("0D10K 0D10K");
4338   CheckExpectedRangesByTimestamp("{ [0,10) }");
4339   CheckExpectedBuffers("0D10K 0D10K");
4340   CheckNoNextBuffer();
4341
4342   Seek(0);
4343
4344   // Add a new frame in a separate coded frame group that falls into the
4345   // overlap of the two existing frames. Splicing should not be performed since
4346   // the content is poorly muxed. We can't know which frame to splice when the
4347   // content is already messed up.
4348   EXPECT_MEDIA_LOG(NoSpliceForBadMux(2, 2000));
4349   NewCodedFrameGroupAppend("2D10K");
4350   CheckExpectedRangesByTimestamp("{ [0,12) }");
4351   CheckExpectedBuffers("0D10K 0D10K 2D10K");
4352   CheckNoNextBuffer();
4353 }
4354
4355 TEST_F(SourceBufferStreamTest, Audio_NoSpliceForEstimatedDuration) {
4356   SetAudioStream();
4357   Seek(0);
4358
4359   // Append two buffers, the latter having estimated duration.
4360   NewCodedFrameGroupAppend("0D10K 10D10EK");
4361   CheckExpectedRangesByTimestamp("{ [0,20) }");
4362   CheckExpectedBuffers("0D10K 10D10EK");
4363   CheckNoNextBuffer();
4364
4365   Seek(0);
4366
4367   // Add a new frame in a separate coded frame group that falls in the middle of
4368   // the second buffer. In spite of the overlap, no splice should be performed
4369   // due to the overlapped buffer having estimated duration.
4370   NewCodedFrameGroupAppend("15D10K");
4371   CheckExpectedRangesByTimestamp("{ [0,25) }");
4372   CheckExpectedBuffers("0D10K 10D10EK 15D10K");
4373   CheckNoNextBuffer();
4374 }
4375
4376 TEST_F(SourceBufferStreamTest, Audio_SpliceTrimming_ExistingTrimming) {
4377   const base::TimeDelta kDuration = base::Milliseconds(4);
4378   const base::TimeDelta kNoDiscard = base::TimeDelta();
4379   const bool is_keyframe = true;
4380
4381   SetAudioStream();
4382   Seek(0);
4383
4384   // Make two BufferQueues with a mix of buffers containing start/end discard.
4385   // Buffer PTS and duration have been adjusted to reflect discard. A_buffers
4386   // will be appended first, then B_buffers. The start of B will overlap A
4387   // to generate a splice.
4388   BufferQueue A_buffers;
4389   BufferQueue B_buffers;
4390
4391   // Buffer A1: PTS = 0, front discard = 2ms, duration = 2ms.
4392   scoped_refptr<StreamParserBuffer> bufferA1 = StreamParserBuffer::CopyFrom(
4393       &kDataA, kDataSize, is_keyframe, DemuxerStream::AUDIO, 0);
4394   bufferA1->set_timestamp(base::Milliseconds(0));
4395   bufferA1->set_duration(kDuration / 2);
4396   const DecoderBuffer::DiscardPadding discardA1 =
4397       std::make_pair(kDuration / 2, kNoDiscard);
4398   bufferA1->set_discard_padding(discardA1);
4399   A_buffers.push_back(bufferA1);
4400
4401   // Buffer A2: PTS = 2, end discard = 2ms, duration = 2ms.
4402   scoped_refptr<StreamParserBuffer> bufferA2 = StreamParserBuffer::CopyFrom(
4403       &kDataA, kDataSize, is_keyframe, DemuxerStream::AUDIO, 0);
4404   bufferA2->set_timestamp(base::Milliseconds(2));
4405   bufferA2->set_duration(kDuration / 2);
4406   const DecoderBuffer::DiscardPadding discardA2 =
4407       std::make_pair(kNoDiscard, kDuration / 2);
4408   bufferA2->set_discard_padding(discardA2);
4409   A_buffers.push_back(bufferA2);
4410
4411   // Buffer B1: PTS = 3, front discard = 2ms, duration = 2ms.
4412   scoped_refptr<StreamParserBuffer> bufferB1 = StreamParserBuffer::CopyFrom(
4413       &kDataA, kDataSize, is_keyframe, DemuxerStream::AUDIO, 0);
4414   bufferB1->set_timestamp(base::Milliseconds(3));
4415   bufferB1->set_duration(kDuration / 2);
4416   const DecoderBuffer::DiscardPadding discardB1 =
4417       std::make_pair(kDuration / 2, kNoDiscard);
4418   bufferB1->set_discard_padding(discardB1);
4419   B_buffers.push_back(bufferB1);
4420
4421   // Buffer B2: PTS = 5, no discard padding, duration = 4ms.
4422   scoped_refptr<StreamParserBuffer> bufferB2 = StreamParserBuffer::CopyFrom(
4423       &kDataA, kDataSize, is_keyframe, DemuxerStream::AUDIO, 0);
4424   bufferB2->set_timestamp(base::Milliseconds(5));
4425   bufferB2->set_duration(kDuration);
4426   B_buffers.push_back(bufferB2);
4427
4428   // Append buffers, trigger splice trimming.
4429   stream_->OnStartOfCodedFrameGroup(bufferA1->timestamp());
4430   stream_->Append(A_buffers);
4431   EXPECT_MEDIA_LOG(TrimmedSpliceOverlap(3000, 2000, 1000));
4432   stream_->Append(B_buffers);
4433
4434   // Verify buffers.
4435   scoped_refptr<StreamParserBuffer> read_buffer;
4436
4437   // Buffer A1 was not spliced, should be unchanged.
4438   EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&read_buffer));
4439   EXPECT_EQ(base::Milliseconds(0), read_buffer->timestamp());
4440   EXPECT_EQ(kDuration / 2, read_buffer->duration());
4441   EXPECT_EQ(discardA1, read_buffer->discard_padding());
4442
4443   // Buffer A2 was overlapped by buffer B1 1ms. Splice trimming should trim A2's
4444   // duration and increase its discard padding by 1ms.
4445   const base::TimeDelta overlap = base::Milliseconds(1);
4446   EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&read_buffer));
4447   EXPECT_EQ(base::Milliseconds(2), read_buffer->timestamp());
4448   EXPECT_EQ((kDuration / 2) - overlap, read_buffer->duration());
4449   const DecoderBuffer::DiscardPadding overlap_discard =
4450       std::make_pair(discardA2.first, discardA2.second + overlap);
4451   EXPECT_EQ(overlap_discard, read_buffer->discard_padding());
4452
4453   // Buffer B1 is overlapping A2, but B1 should be unchanged - splice trimming
4454   // only modifies the earlier buffer (A1).
4455   EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&read_buffer));
4456   EXPECT_EQ(base::Milliseconds(3), read_buffer->timestamp());
4457   EXPECT_EQ(kDuration / 2, read_buffer->duration());
4458   EXPECT_EQ(discardB1, read_buffer->discard_padding());
4459
4460   // Buffer B2 is not spliced, should be unchanged.
4461   EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&read_buffer));
4462   EXPECT_EQ(base::Milliseconds(5), read_buffer->timestamp());
4463   EXPECT_EQ(kDuration, read_buffer->duration());
4464   EXPECT_EQ(std::make_pair(kNoDiscard, kNoDiscard),
4465             read_buffer->discard_padding());
4466
4467   CheckNoNextBuffer();
4468 }
4469
4470 TEST_F(SourceBufferStreamTest, Audio_SpliceFrame_NoMillisecondSplices) {
4471   EXPECT_MEDIA_LOG(SkippingSpliceTooLittleOverlap(1250, 250));
4472
4473   video_config_ = TestVideoConfig::Invalid();
4474   audio_config_.Initialize(
4475       AudioCodec::kVorbis, kSampleFormatPlanarF32, CHANNEL_LAYOUT_STEREO, 4000,
4476       EmptyExtraData(), EncryptionScheme::kUnencrypted, base::TimeDelta(), 0);
4477   ResetStream<>(audio_config_);
4478   // Equivalent to 0.5ms per frame.
4479   SetStreamInfo(2000, 2000);
4480   Seek(0);
4481
4482   // Append four buffers with a 0.5ms duration each.
4483   NewCodedFrameGroupAppend(0, 4);
4484   CheckExpectedRangesByTimestamp("{ [0,2) }");
4485
4486   // Overlap the range [0, 2) with [1.25, 2); this results in an overlap of
4487   // 0.25ms between the original buffer at time 1.0 and the new buffer at time
4488   // 1.25.
4489   NewCodedFrameGroupAppend_OffsetFirstBuffer(2, 2, base::Milliseconds(0.25));
4490   CheckExpectedRangesByTimestamp("{ [0,2) }");
4491
4492   // A splice frame should not be generated since it requires at least 1ms of
4493   // data to crossfade.
4494   CheckExpectedBuffers("0K 0K 1K 1K");
4495   CheckNoNextBuffer();
4496 }
4497
4498 TEST_F(SourceBufferStreamTest, Audio_PrerollFrame) {
4499   Seek(0);
4500   NewCodedFrameGroupAppend("0K 3P 6K");
4501   CheckExpectedBuffers("0K 3P 6K");
4502   CheckNoNextBuffer();
4503 }
4504
4505 TEST_F(SourceBufferStreamTest, Audio_ConfigChangeWithPreroll) {
4506   AudioDecoderConfig new_config(AudioCodec::kVorbis, kSampleFormatPlanarF32,
4507                                 CHANNEL_LAYOUT_MONO, 2000, EmptyExtraData(),
4508                                 EncryptionScheme::kUnencrypted);
4509   SetAudioStream();
4510   Seek(0);
4511
4512   // Append some audio using the default configuration.
4513   CheckAudioConfig(audio_config_);
4514   NewCodedFrameGroupAppend("0K 3K 6K");
4515
4516   // Update the configuration.
4517   stream_->UpdateAudioConfig(new_config, false);
4518
4519   // We haven't read any buffers at this point, so the config for the next
4520   // buffer at time 0 should still be the original config.
4521   CheckAudioConfig(audio_config_);
4522
4523   // Append new audio containing preroll and using the new config.
4524   EXPECT_MEDIA_LOG(TrimmedSpliceOverlap(7000, 6000, 2000));
4525   NewCodedFrameGroupAppend("7P 8K");
4526
4527   // Check buffers from the first append.
4528   CheckExpectedBuffers("0K 3K 6K");
4529
4530   // Verify the next attempt to get a buffer will signal that a config change
4531   // has happened.
4532   scoped_refptr<StreamParserBuffer> buffer;
4533   EXPECT_TRUE(stream_->IsNextBufferConfigChanged());
4534   EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
4535
4536   // Verify upcoming buffers will use the new config.
4537   CheckAudioConfig(new_config);
4538
4539   // Check buffers from the second append, including preroll.
4540   // CheckExpectedBuffers("6P 7K 8K");
4541   CheckExpectedBuffers("7P 8K");
4542
4543   CheckNoNextBuffer();
4544 }
4545
4546 TEST_F(SourceBufferStreamTest, Audio_Opus_SeekToJustBeforeRangeStart) {
4547   // Seek to a time within the fudge room of seekability to a buffered Opus
4548   // audio frame's range, but before the range's start. Use small seek_preroll
4549   // in case the associated logic to check same config in the preroll time
4550   // interval requires a nonzero seek_preroll value.
4551   video_config_ = TestVideoConfig::Invalid();
4552   audio_config_.Initialize(AudioCodec::kOpus, kSampleFormatPlanarF32,
4553                            CHANNEL_LAYOUT_STEREO, 1000, EmptyExtraData(),
4554                            EncryptionScheme::kUnencrypted,
4555                            base::Milliseconds(10), 0);
4556   ResetStream<>(audio_config_);
4557
4558   // Equivalent to 1s per frame.
4559   SetStreamInfo(1, 1);
4560   Seek(0);
4561
4562   // Append a buffer at 1.5 seconds, with duration 1 second, increasing the
4563   // fudge room to 2 * 1 seconds. The pending seek to time 0 should be satisfied
4564   // with this buffer's range, because that seek time is within the fudge room
4565   // of 2.
4566   NewCodedFrameGroupAppend("1500D1000K");
4567   CheckExpectedRangesByTimestamp("{ [1500,2500) }");
4568   CheckExpectedBuffers("1500K");
4569   CheckNoNextBuffer();
4570 }
4571
4572 TEST_F(SourceBufferStreamTest, BFrames) {
4573   Seek(0);
4574   NewCodedFrameGroupAppend("0K 120|30 30|60 60|90 90|120");
4575   CheckExpectedRangesByTimestamp("{ [0,150) }");
4576
4577   CheckExpectedBuffers("0K 120|30 30|60 60|90 90|120");
4578   CheckNoNextBuffer();
4579 }
4580
4581 TEST_F(SourceBufferStreamTest, RemoveShouldAlwaysExcludeEnd) {
4582   NewCodedFrameGroupAppend("10D2K 12D2 14D2");
4583   CheckExpectedRangesByTimestamp("{ [10,16) }");
4584
4585   // Start new coded frame group, appending KF to abut the start of previous
4586   // group.
4587   NewCodedFrameGroupAppend("0D10K");
4588   Seek(0);
4589   CheckExpectedRangesByTimestamp("{ [0,16) }");
4590   CheckExpectedBuffers("0K 10K 12 14");
4591   CheckNoNextBuffer();
4592
4593   // Append another buffer with the same timestamp as the last KF. This triggers
4594   // special logic that allows two buffers to have the same timestamp. When
4595   // preparing for this new append, there is no reason to remove the later GOP
4596   // starting at timestamp 10. This verifies the fix for http://crbug.com/469325
4597   // where the decision *not* to remove the start of the overlapped range was
4598   // erroneously triggering buffers with a timestamp matching the end
4599   // of the append (and any later dependent frames) to be removed.
4600   AppendBuffers("0D10");
4601   Seek(0);
4602   CheckExpectedRangesByTimestamp("{ [0,16) }");
4603   CheckExpectedBuffers("0K 0 10K 12 14");
4604   CheckNoNextBuffer();
4605 }
4606
4607 TEST_F(SourceBufferStreamTest, RefinedDurationEstimates_BackOverlap) {
4608   // Append a few buffers, the last one having estimated duration.
4609   NewCodedFrameGroupAppend("0K 5 10 20D10E");
4610   CheckExpectedRangesByTimestamp("{ [0,30) }");
4611   Seek(0);
4612   CheckExpectedBuffers("0K 5 10 20D10E");
4613   CheckNoNextBuffer();
4614
4615   // Append a buffer to the end that overlaps the *back* of the existing range.
4616   // This should trigger the estimated duration to be recomputed as a timestamp
4617   // delta.
4618   AppendBuffers("25D10");
4619   CheckExpectedRangesByTimestamp("{ [0,35) }");
4620   Seek(0);
4621   // The duration of the buffer at time 20 has changed from 10ms to 5ms.
4622   CheckExpectedBuffers("0K 5 10 20D5E 25");
4623   CheckNoNextBuffer();
4624
4625   // If the last buffer is removed, the adjusted duration should remain at 5ms.
4626   RemoveInMs(25, 35, 35);
4627   CheckExpectedRangesByTimestamp("{ [0,25) }");
4628   Seek(0);
4629   CheckExpectedBuffers("0K 5 10 20D5E");
4630   CheckNoNextBuffer();
4631 }
4632
4633 TEST_F(SourceBufferStreamTest, RefinedDurationEstimates_FrontOverlap) {
4634   // Append a few buffers.
4635   NewCodedFrameGroupAppend("10K 15 20D5");
4636   CheckExpectedRangesByTimestamp("{ [10,25) }");
4637   SeekToTimestampMs(10);
4638   CheckExpectedBuffers("10K 15 20");
4639   CheckNoNextBuffer();
4640
4641   // Append new buffers, where the last has estimated duration that overlaps the
4642   // *front* of the existing range. The overlap should trigger refinement of the
4643   // estimated duration from 7ms to 5ms.
4644   NewCodedFrameGroupAppend("0K 5D7E");
4645   CheckExpectedRangesByTimestamp("{ [0,25) }");
4646   Seek(0);
4647   CheckExpectedBuffers("0K 5D5E 10K 15 20");
4648   CheckNoNextBuffer();
4649
4650   // If the overlapped buffer at timestamp 10 is removed, the adjusted duration
4651   // should remain adjusted.
4652   RemoveInMs(10, 20, 25);
4653   CheckExpectedRangesByTimestamp("{ [0,10) }");
4654   Seek(0);
4655   CheckExpectedBuffers("0K 5D5E");
4656   CheckNoNextBuffer();
4657 }
4658
4659 TEST_F(SourceBufferStreamTest, SeekToStartSatisfiedUpToThreshold) {
4660   NewCodedFrameGroupAppend("999K 1010 1020D10");
4661   CheckExpectedRangesByTimestamp("{ [999,1030) }");
4662
4663   SeekToTimestampMs(0);
4664   CheckExpectedBuffers("999K 1010 1020D10");
4665   CheckNoNextBuffer();
4666 }
4667
4668 TEST_F(SourceBufferStreamTest, SeekToStartUnsatisfiedBeyondThreshold) {
4669   NewCodedFrameGroupAppend("1000K 1010 1020D10");
4670   CheckExpectedRangesByTimestamp("{ [1000,1030) }");
4671
4672   SeekToTimestampMs(0);
4673   CheckNoNextBuffer();
4674 }
4675
4676 TEST_F(SourceBufferStreamTest,
4677        ReSeekToStartSatisfiedUpToThreshold_SameTimestamps) {
4678   // Append a few buffers.
4679   NewCodedFrameGroupAppend("999K 1010 1020D10");
4680   CheckExpectedRangesByTimestamp("{ [999,1030) }");
4681
4682   // Don't read any buffers between Seek and Remove.
4683   SeekToTimestampMs(0);
4684   RemoveInMs(999, 1030, 1030);
4685   CheckExpectedRangesByTimestamp("{ }");
4686   CheckNoNextBuffer();
4687
4688   // Append buffers at the original timestamps and verify no stall.
4689   NewCodedFrameGroupAppend("999K 1010 1020D10");
4690   CheckExpectedRangesByTimestamp("{ [999,1030) }");
4691   CheckExpectedBuffers("999K 1010 1020D10");
4692   CheckNoNextBuffer();
4693 }
4694
4695 TEST_F(SourceBufferStreamTest,
4696        ReSeekToStartSatisfiedUpToThreshold_EarlierTimestamps) {
4697   // Append a few buffers.
4698   NewCodedFrameGroupAppend("999K 1010 1020D10");
4699   CheckExpectedRangesByTimestamp("{ [999,1030) }");
4700
4701   // Don't read any buffers between Seek and Remove.
4702   SeekToTimestampMs(0);
4703   RemoveInMs(999, 1030, 1030);
4704   CheckExpectedRangesByTimestamp("{ }");
4705   CheckNoNextBuffer();
4706
4707   // Append buffers before the original timestamps and verify no stall (the
4708   // re-seek to time 0 should still be satisfied with the new buffers).
4709   NewCodedFrameGroupAppend("500K 510 520D10");
4710   CheckExpectedRangesByTimestamp("{ [500,530) }");
4711   CheckExpectedBuffers("500K 510 520D10");
4712   CheckNoNextBuffer();
4713 }
4714
4715 TEST_F(SourceBufferStreamTest,
4716        ReSeekToStartSatisfiedUpToThreshold_LaterTimestamps) {
4717   // Append a few buffers.
4718   NewCodedFrameGroupAppend("500K 510 520D10");
4719   CheckExpectedRangesByTimestamp("{ [500,530) }");
4720
4721   // Don't read any buffers between Seek and Remove.
4722   SeekToTimestampMs(0);
4723   RemoveInMs(500, 530, 530);
4724   CheckExpectedRangesByTimestamp("{ }");
4725   CheckNoNextBuffer();
4726
4727   // Append buffers beginning after original timestamps, but still below the
4728   // start threshold, and verify no stall (the re-seek to time 0 should still be
4729   // satisfied with the new buffers).
4730   NewCodedFrameGroupAppend("999K 1010 1020D10");
4731   CheckExpectedRangesByTimestamp("{ [999,1030) }");
4732   CheckExpectedBuffers("999K 1010 1020D10");
4733   CheckNoNextBuffer();
4734 }
4735
4736 TEST_F(SourceBufferStreamTest, ReSeekBeyondStartThreshold_SameTimestamps) {
4737   // Append a few buffers.
4738   NewCodedFrameGroupAppend("1000K 1010 1020D10");
4739   CheckExpectedRangesByTimestamp("{ [1000,1030) }");
4740
4741   // Don't read any buffers between Seek and Remove.
4742   SeekToTimestampMs(1000);
4743   RemoveInMs(1000, 1030, 1030);
4744   CheckExpectedRangesByTimestamp("{ }");
4745   CheckNoNextBuffer();
4746
4747   // Append buffers at the original timestamps and verify no stall.
4748   NewCodedFrameGroupAppend("1000K 1010 1020D10");
4749   CheckExpectedRangesByTimestamp("{ [1000,1030) }");
4750   CheckExpectedBuffers("1000K 1010 1020D10");
4751   CheckNoNextBuffer();
4752 }
4753
4754 TEST_F(SourceBufferStreamTest, ReSeekBeyondThreshold_EarlierTimestamps) {
4755   // Append a few buffers.
4756   NewCodedFrameGroupAppend("2000K 2010 2020D10");
4757   CheckExpectedRangesByTimestamp("{ [2000,2030) }");
4758
4759   // Don't read any buffers between Seek and Remove.
4760   SeekToTimestampMs(2000);
4761   RemoveInMs(2000, 2030, 2030);
4762   CheckExpectedRangesByTimestamp("{ }");
4763   CheckNoNextBuffer();
4764
4765   // Append buffers before the original timestamps and verify no stall (the
4766   // re-seek to time 2 seconds should still be satisfied with the new buffers
4767   // and should emit preroll from last keyframe).
4768   NewCodedFrameGroupAppend("1080K 1090 2000D10");
4769   CheckExpectedRangesByTimestamp("{ [1080,2010) }");
4770   CheckExpectedBuffers("1080K 1090 2000D10");
4771   CheckNoNextBuffer();
4772 }
4773
4774 TEST_F(SourceBufferStreamTest, ConfigChange_ReSeek) {
4775   // Append a few buffers, with a config change in the middle.
4776   VideoDecoderConfig new_config = TestVideoConfig::Large();
4777   NewCodedFrameGroupAppend("2000K 2010 2020D10");
4778   stream_->UpdateVideoConfig(new_config, false);
4779   NewCodedFrameGroupAppend("2030K 2040 2050D10");
4780   CheckExpectedRangesByTimestamp("{ [2000,2060) }");
4781
4782   // Read the config change, but don't read any non-config-change buffer between
4783   // Seek and Remove.
4784   scoped_refptr<StreamParserBuffer> buffer;
4785   CheckVideoConfig(video_config_);
4786   SeekToTimestampMs(2030);
4787   CheckVideoConfig(video_config_);
4788   EXPECT_TRUE(stream_->IsNextBufferConfigChanged());
4789   EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
4790   CheckVideoConfig(new_config);
4791
4792   // Trigger the re-seek.
4793   RemoveInMs(2030, 2060, 2060);
4794   CheckExpectedRangesByTimestamp("{ [2000,2030) }");
4795   CheckNoNextBuffer();
4796
4797   // Append buffers at the original timestamps and verify no stall or redundant
4798   // signalling of config change.
4799   NewCodedFrameGroupAppend("2030K 2040 2050D10");
4800   CheckVideoConfig(new_config);
4801   CheckExpectedRangesByTimestamp("{ [2000,2060) }");
4802   CheckExpectedBuffers("2030K 2040 2050D10");
4803   CheckNoNextBuffer();
4804   CheckVideoConfig(new_config);
4805
4806   // Seek to the start of buffered and verify config changes and buffers.
4807   SeekToTimestampMs(2000);
4808   CheckVideoConfig(new_config);
4809   ASSERT_FALSE(new_config.Matches(video_config_));
4810   EXPECT_TRUE(stream_->IsNextBufferConfigChanged());
4811   EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
4812   CheckVideoConfig(video_config_);
4813   CheckExpectedBuffers("2000K 2010 2020D10");
4814   CheckVideoConfig(video_config_);
4815   EXPECT_TRUE(stream_->IsNextBufferConfigChanged());
4816   EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
4817   CheckVideoConfig(new_config);
4818   CheckExpectedBuffers("2030K 2040 2050D10");
4819   CheckNoNextBuffer();
4820   CheckVideoConfig(new_config);
4821 }
4822
4823 TEST_F(SourceBufferStreamTest, TrackBuffer_ExhaustionWithSkipForward) {
4824   NewCodedFrameGroupAppend("0K 10 20 30 40");
4825
4826   // Read the first 4 buffers, so next buffer is at time 40.
4827   Seek(0);
4828   CheckExpectedRangesByTimestamp("{ [0,50) }");
4829   CheckExpectedBuffers("0K 10 20 30");
4830
4831   // Overlap-append, populating track buffer with timestamp 40 from original
4832   // append. Confirm there could be a large jump in time until the next key
4833   // frame after exhausting the track buffer.
4834   NewCodedFrameGroupAppend(
4835       "31K 41 51 61 71 81 91 101 111 121 "
4836       "131K 141");
4837   CheckExpectedRangesByTimestamp("{ [0,151) }");
4838
4839   // Confirm the large jump occurs and warning log is generated.
4840   // If this test is changed, update
4841   // TrackBufferExhaustion_ImmediateNewTrackBuffer accordingly.
4842   EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(91));
4843
4844   CheckExpectedBuffers("40 131K 141");
4845   CheckNoNextBuffer();
4846 }
4847
4848 TEST_F(SourceBufferStreamTest,
4849        TrackBuffer_ExhaustionAndImmediateNewTrackBuffer) {
4850   NewCodedFrameGroupAppend("0K 10 20 30 40");
4851
4852   // Read the first 4 buffers, so next buffer is at time 40.
4853   Seek(0);
4854   CheckExpectedRangesByTimestamp("{ [0,50) }");
4855   CheckExpectedBuffers("0K 10 20 30");
4856
4857   // Overlap-append
4858   NewCodedFrameGroupAppend(
4859       "31K 41 51 61 71 81 91 101 111 121 "
4860       "131K 141");
4861   CheckExpectedRangesByTimestamp("{ [0,151) }");
4862
4863   // Exhaust the track buffer, but don't read any of the overlapping append yet.
4864   CheckExpectedBuffers("40");
4865
4866   // Selected range's next buffer is now the 131K buffer from the overlapping
4867   // append. (See TrackBuffer_ExhaustionWithSkipForward for that verification.)
4868   // Do another overlap-append to immediately create another track buffer and
4869   // verify both track buffer exhaustions skip forward and emit log warnings.
4870   NewCodedFrameGroupAppend(
4871       "22K 32 42 52 62 72 82 92 102 112 122K 132 142 152K 162");
4872   CheckExpectedRangesByTimestamp("{ [0,172) }");
4873
4874   InSequence s;
4875   EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(91));
4876   EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(11));
4877
4878   CheckExpectedBuffers("131K 141 152K 162");
4879   CheckNoNextBuffer();
4880 }
4881
4882 TEST_F(
4883     SourceBufferStreamTest,
4884     AdjacentCodedFrameGroupContinuation_NoGapCreatedByTinyGapInGroupContinuation) {
4885   NewCodedFrameGroupAppend("0K 10 20K 30 40K 50D10");
4886   CheckExpectedRangesByTimestamp("{ [0,60) }");
4887
4888   // Continue appending to the previously started coded frame group, albeit with
4889   // a tiny (1ms) gap. This gap should *NOT* produce a buffered range gap.
4890   AppendBuffers("61K 71D10");
4891   CheckExpectedRangesByTimestamp("{ [0,81) }");
4892 }
4893
4894 TEST_F(SourceBufferStreamTest,
4895        AdjacentCodedFrameGroupContinuation_NoGapCreatedPrefixRemoved) {
4896   NewCodedFrameGroupAppend("0K 10 20K 30 40K 50D10");
4897   CheckExpectedRangesByTimestamp("{ [0,60) }");
4898
4899   RemoveInMs(0, 35, 60);
4900   CheckExpectedRangesByTimestamp("{ [40,60) }");
4901
4902   // Continue appending to the previously started coded frame group, albeit with
4903   // a tiny (1ms) gap. This gap should *NOT* produce a buffered range gap.
4904   AppendBuffers("61K 71D10");
4905   CheckExpectedRangesByTimestamp("{ [40,81) }");
4906 }
4907
4908 TEST_F(SourceBufferStreamTest,
4909        AdjacentNewCodedFrameGroupContinuation_NoGapCreatedPrefixRemoved) {
4910   NewCodedFrameGroupAppend("0K 10 20K 30 40K 50D10");
4911   CheckExpectedRangesByTimestamp("{ [0,60) }");
4912
4913   RemoveInMs(0, 35, 60);
4914   CheckExpectedRangesByTimestamp("{ [40,60) }");
4915
4916   // Continue appending, with a new coded frame group, albeit with
4917   // a tiny (1ms) gap. This gap should *NOT* produce a buffered range gap.
4918   // This test demonstrates the "pre-relaxation" behavior, where a new "media
4919   // segment" (now a new "coded frame group") was signaled at every media
4920   // segment boundary.
4921   NewCodedFrameGroupAppend("61K 71D10");
4922   CheckExpectedRangesByTimestamp("{ [40,81) }");
4923 }
4924
4925 TEST_F(SourceBufferStreamTest,
4926        StartCodedFrameGroup_RemoveThenAppendMoreMuchLater) {
4927   NewCodedFrameGroupAppend("1000K 1010 1020 1030K 1040 1050 1060K 1070 1080");
4928   NewCodedFrameGroupAppend("0K 10 20");
4929   CheckExpectedRangesByTimestamp("{ [0,30) [1000,1090) }");
4930
4931   SignalStartOfCodedFrameGroup(base::Milliseconds(1070));
4932   CheckExpectedRangesByTimestamp("{ [0,30) [1000,1090) }");
4933
4934   RemoveInMs(1030, 1050, 1090);
4935   CheckExpectedRangesByTimestamp("{ [0,30) [1000,1030) [1060,1090) }");
4936
4937   // We've signalled that we're about to do some appends to a coded frame group
4938   // which starts at time 1070ms. Note that the first frame, if any ever,
4939   // appended to this SourceBufferStream for that coded frame group must have a
4940   // decode timestamp >= 1070ms (it can be significantly in the future).
4941   // Regardless, that appended frame must be buffered into the same existing
4942   // range as current [1060,1090), since the new coded frame group's start of
4943   // 1070ms is within that range.
4944   AppendBuffers("2000K 2010");
4945   CheckExpectedRangesByTimestamp("{ [0,30) [1000,1030) [1060,2020) }");
4946   SeekToTimestampMs(1060);
4947   CheckExpectedBuffers("1060K 2000K 2010");
4948   CheckNoNextBuffer();
4949 }
4950
4951 TEST_F(SourceBufferStreamTest,
4952        StartCodedFrameGroup_InExisting_AppendMuchLater) {
4953   NewCodedFrameGroupAppend("0K 10 20 30K 40 50");
4954   SignalStartOfCodedFrameGroup(base::Milliseconds(45));
4955   CheckExpectedRangesByTimestamp("{ [0,60) }");
4956
4957   AppendBuffers("2000K 2010");
4958   CheckExpectedRangesByTimestamp("{ [0,2020) }");
4959   Seek(0);
4960   CheckExpectedBuffers("0K 10 20 30K 40 2000K 2010");
4961   CheckNoNextBuffer();
4962 }
4963
4964 TEST_F(SourceBufferStreamTest,
4965        StartCodedFrameGroup_InExisting_RemoveGOP_ThenAppend_1) {
4966   NewCodedFrameGroupAppend("0K 10 20 30K 40 50");
4967   SignalStartOfCodedFrameGroup(base::Milliseconds(30));
4968   RemoveInMs(30, 60, 60);
4969   CheckExpectedRangesByTimestamp("{ [0,30) }");
4970
4971   AppendBuffers("2000K 2010");
4972   CheckExpectedRangesByTimestamp("{ [0,2020) }");
4973   Seek(0);
4974   CheckExpectedBuffers("0K 10 20 2000K 2010");
4975   CheckNoNextBuffer();
4976 }
4977
4978 TEST_F(SourceBufferStreamTest,
4979        StartCodedFrameGroup_InExisting_RemoveGOP_ThenAppend_2) {
4980   NewCodedFrameGroupAppend("0K 10 20 30K 40 50");
4981   // Though we signal 45ms, it's adjusted internally (due to detected overlap)
4982   // to be 40.001ms (which is just beyond the highest buffered timestamp at or
4983   // before 45ms) to help prevent potential discontinuity across the front of
4984   // the overlapping append.
4985   SignalStartOfCodedFrameGroup(base::Milliseconds(45));
4986   RemoveInMs(30, 60, 60);
4987   CheckExpectedRangesByTimestamp("{ [0,30) }");
4988
4989   AppendBuffers("2000K 2010");
4990   CheckExpectedRangesByTimestamp("{ [0,30) [40,2020) }");
4991   Seek(0);
4992   CheckExpectedBuffers("0K 10 20");
4993   CheckNoNextBuffer();
4994   SeekToTimestampMs(40);
4995   CheckExpectedBuffers("2000K 2010");
4996   CheckNoNextBuffer();
4997   SeekToTimestampMs(1000);
4998   CheckExpectedBuffers("2000K 2010");
4999   CheckNoNextBuffer();
5000 }
5001
5002 TEST_F(SourceBufferStreamTest,
5003        StartCodedFrameGroup_InExisting_RemoveMostRecentAppend_ThenAppend_1) {
5004   NewCodedFrameGroupAppend("0K 10 20 30K 40 50");
5005   SignalStartOfCodedFrameGroup(base::Milliseconds(45));
5006   RemoveInMs(50, 60, 60);
5007   CheckExpectedRangesByTimestamp("{ [0,50) }");
5008
5009   AppendBuffers("2000K 2010");
5010   CheckExpectedRangesByTimestamp("{ [0,2020) }");
5011   Seek(0);
5012   CheckExpectedBuffers("0K 10 20 30K 40 2000K 2010");
5013   CheckNoNextBuffer();
5014 }
5015
5016 TEST_F(SourceBufferStreamTest,
5017        StartCodedFrameGroup_InExisting_RemoveMostRecentAppend_ThenAppend_2) {
5018   NewCodedFrameGroupAppend("0K 10 20 30K 40 50");
5019   SignalStartOfCodedFrameGroup(base::Milliseconds(50));
5020   RemoveInMs(50, 60, 60);
5021   CheckExpectedRangesByTimestamp("{ [0,50) }");
5022
5023   AppendBuffers("2000K 2010");
5024   CheckExpectedRangesByTimestamp("{ [0,2020) }");
5025   Seek(0);
5026   CheckExpectedBuffers("0K 10 20 30K 40 2000K 2010");
5027   CheckNoNextBuffer();
5028 }
5029
5030 TEST_F(SourceBufferStreamTest, GetLowestPresentationTimestamp_NonMuxed) {
5031   EXPECT_EQ(base::TimeDelta(), stream_->GetLowestPresentationTimestamp());
5032
5033   NewCodedFrameGroupAppend("100K 110K");
5034   EXPECT_EQ(base::Milliseconds(100), stream_->GetLowestPresentationTimestamp());
5035
5036   RemoveInMs(110, 120, 120);
5037   EXPECT_EQ(base::Milliseconds(100), stream_->GetLowestPresentationTimestamp());
5038
5039   RemoveInMs(100, 110, 120);
5040   EXPECT_EQ(base::TimeDelta(), stream_->GetLowestPresentationTimestamp());
5041
5042   NewCodedFrameGroupAppend("100K 110K");
5043   EXPECT_EQ(base::Milliseconds(100), stream_->GetLowestPresentationTimestamp());
5044
5045   RemoveInMs(100, 110, 120);
5046   EXPECT_EQ(base::Milliseconds(110), stream_->GetLowestPresentationTimestamp());
5047
5048   RemoveInMs(110, 120, 120);
5049   EXPECT_EQ(base::TimeDelta(), stream_->GetLowestPresentationTimestamp());
5050 }
5051
5052 TEST_F(SourceBufferStreamTest, GetLowestPresentationTimestamp_Muxed) {
5053   // Simulate `stream_` being one of multiple resulting from parsing and
5054   // buffering a muxed bytestream. In this case, it is common for range start
5055   // times across the streams in the same muxed segment to not precisely align.
5056   // The frame processing algorithm indicates the segment's "coded frame group
5057   // start time" to the SourceBufferStream, and the underlying range remembers
5058   // this even if the corresponding actual start time in the underlying range is
5059   // later than that start time. However, if the start of that range is removed,
5060   // then the underlying range no longer attempts to maintain the original
5061   // "coded frame group start time" as the lowest timestamp. This impacts
5062   // GetLowestPresentationTimestamp(), since the underlying range start time of
5063   // the first range is involved and is conditional. See also
5064   // SourceBufferRange::GetStartTimestamp().
5065   EXPECT_EQ(base::TimeDelta(), stream_->GetLowestPresentationTimestamp());
5066
5067   NewCodedFrameGroupAppend(base::Milliseconds(50), "100K 110K");
5068   EXPECT_EQ(base::Milliseconds(50), stream_->GetLowestPresentationTimestamp());
5069
5070   RemoveInMs(110, 120, 120);
5071   EXPECT_EQ(base::Milliseconds(50), stream_->GetLowestPresentationTimestamp());
5072
5073   RemoveInMs(100, 110, 120);
5074   EXPECT_EQ(base::TimeDelta(), stream_->GetLowestPresentationTimestamp());
5075
5076   NewCodedFrameGroupAppend(base::Milliseconds(50), "100K 110K");
5077   EXPECT_EQ(base::Milliseconds(50), stream_->GetLowestPresentationTimestamp());
5078
5079   RemoveInMs(100, 110, 120);
5080   EXPECT_EQ(base::Milliseconds(110), stream_->GetLowestPresentationTimestamp());
5081
5082   RemoveInMs(110, 120, 120);
5083   EXPECT_EQ(base::TimeDelta(), stream_->GetLowestPresentationTimestamp());
5084 }
5085
5086 TEST_F(SourceBufferStreamTest, GetHighestPresentationTimestamp) {
5087   EXPECT_EQ(base::TimeDelta(), stream_->GetHighestPresentationTimestamp());
5088
5089   NewCodedFrameGroupAppend("0K 10K");
5090   EXPECT_EQ(base::Milliseconds(10), stream_->GetHighestPresentationTimestamp());
5091
5092   RemoveInMs(0, 10, 20);
5093   EXPECT_EQ(base::Milliseconds(10), stream_->GetHighestPresentationTimestamp());
5094
5095   RemoveInMs(10, 20, 20);
5096   EXPECT_EQ(base::TimeDelta(), stream_->GetHighestPresentationTimestamp());
5097
5098   NewCodedFrameGroupAppend("0K 10K");
5099   EXPECT_EQ(base::Milliseconds(10), stream_->GetHighestPresentationTimestamp());
5100
5101   RemoveInMs(10, 20, 20);
5102   EXPECT_EQ(base::TimeDelta(), stream_->GetHighestPresentationTimestamp());
5103 }
5104
5105 TEST_F(SourceBufferStreamTest, GarbageCollectionUnderMemoryPressure) {
5106   SetMemoryLimit(16);
5107   NewCodedFrameGroupAppend("0K 1 2 3K 4 5 6K 7 8 9K 10 11 12K 13 14 15K");
5108   CheckExpectedRangesByTimestamp("{ [0,16) }");
5109
5110   // This feature is disabled by default, so by default memory pressure
5111   // notification takes no effect and the memory limits and won't remove
5112   // anything from buffered ranges, since we are under the limit of 20 bytes.
5113   stream_->OnMemoryPressure(
5114       base::Milliseconds(0),
5115       base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, false);
5116   EXPECT_TRUE(GarbageCollect(base::Milliseconds(8), 0));
5117   CheckExpectedRangesByTimestamp("{ [0,16) }");
5118
5119   // Now enable the feature (on top of any overrides already in
5120   // |scoped_feature_list_|.)
5121   base::test::ScopedFeatureList scoped_feature_list;
5122   scoped_feature_list.InitAndEnableFeature(kMemoryPressureBasedSourceBufferGC);
5123
5124   // Verify that effective MSE memory limit is reduced under memory pressure.
5125   stream_->OnMemoryPressure(
5126       base::Milliseconds(0),
5127       base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, false);
5128
5129   // Effective memory limit is now 8 buffers, but we still will not collect any
5130   // data between the current playback position 3 and last append position 15.
5131   EXPECT_TRUE(GarbageCollect(base::Milliseconds(4), 0));
5132   CheckExpectedRangesByTimestamp("{ [3,16) }");
5133
5134   // As playback proceeds further to time 9 we should be able to collect
5135   // enough data to bring us back under memory limit of 8 buffers.
5136   EXPECT_TRUE(GarbageCollect(base::Milliseconds(9), 0));
5137   CheckExpectedRangesByTimestamp("{ [9,16) }");
5138
5139   // If memory pressure becomes critical, the garbage collection algorithm
5140   // becomes even more aggressive and collects everything up to the current
5141   // playback position.
5142   stream_->OnMemoryPressure(
5143       base::Milliseconds(0),
5144       base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, false);
5145   EXPECT_TRUE(GarbageCollect(base::Milliseconds(13), 0));
5146   CheckExpectedRangesByTimestamp("{ [12,16) }");
5147
5148   // But even under critical memory pressure the MSE memory limit imposed by the
5149   // memory pressure is soft, i.e. we should be able to append more data
5150   // successfully up to the hard limit of 16 bytes.
5151   NewCodedFrameGroupAppend("16K 17 18 19 20 21 22 23 24 25 26 27");
5152   CheckExpectedRangesByTimestamp("{ [12,28) }");
5153   EXPECT_TRUE(GarbageCollect(base::Milliseconds(13), 0));
5154   CheckExpectedRangesByTimestamp("{ [12,28) }");
5155 }
5156
5157 TEST_F(SourceBufferStreamTest, InstantGarbageCollectionUnderMemoryPressure) {
5158   SetMemoryLimit(16);
5159   NewCodedFrameGroupAppend("0K 1 2 3K 4 5 6K 7 8 9K 10 11 12K 13 14 15K");
5160   CheckExpectedRangesByTimestamp("{ [0,16) }");
5161
5162   // Verify that garbage collection happens immediately on critical memory
5163   // pressure notification, even without explicit GarbageCollect invocation,
5164   // when the immediate GC is allowed.
5165   // First, enable the feature (on top of any overrides already in
5166   // |scoped_feature_list_|.)
5167   base::test::ScopedFeatureList scoped_feature_list;
5168   scoped_feature_list.InitAndEnableFeature(kMemoryPressureBasedSourceBufferGC);
5169   stream_->OnMemoryPressure(
5170       base::Milliseconds(7),
5171       base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, true);
5172   CheckExpectedRangesByTimestamp("{ [6,16) }");
5173   stream_->OnMemoryPressure(
5174       base::Milliseconds(9),
5175       base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, true);
5176   CheckExpectedRangesByTimestamp("{ [9,16) }");
5177 }
5178
5179 TEST_F(SourceBufferStreamTest, GCFromFrontThenExplicitRemoveFromMiddleToEnd) {
5180   // Attempts to exercise SourceBufferRange::GetBufferIndexAt() after its
5181   // |keyframe_map_index_base_| has been increased, and when there is a GOP
5182   // following the search timestamp.  GC followed by an explicit remove may
5183   // trigger that code path.
5184   SetMemoryLimit(10);
5185
5186   // Append 3 IBPPP GOPs in one continuous range.
5187   NewCodedFrameGroupAppend(
5188       "0K 40|10 10|20 20|30 30|40 "
5189       "50K 90|60 60|70 70|80 80|90 "
5190       "100K 140|110 110|120 120|130 130|140");
5191
5192   CheckExpectedRangesByTimestamp("{ [0,150) }");
5193
5194   // Seek to the second GOP's keyframe to allow GC to collect all of the first
5195   // GOP (ostensibly increasing SourceBufferRange's |keyframe_map_index_base_|).
5196   SeekToTimestampMs(50);
5197   GarbageCollect(base::Milliseconds(50), 0);
5198   CheckExpectedRangesByTimestamp("{ [50,150) }");
5199
5200   // Remove from the middle of the first remaining GOP to the end of the range.
5201   RemoveInMs(60, 150, 150);
5202   CheckExpectedRangesByTimestamp("{ [50,60) }");
5203 }
5204
5205 TEST_F(SourceBufferStreamTest, BFrames_WithoutEditList) {
5206   // Simulates B-frame content where MP4 edit lists are not used to shift PTS so
5207   // it matches DTS. From acolwell@chromium.org in https://crbug.com/398130
5208   Seek(0);
5209   NewCodedFrameGroupAppend(base::Milliseconds(60),
5210                            "60|0K 180|30 90|60 120|90 150|120");
5211   CheckExpectedRangesByTimestamp("{ [60,210) }");
5212   CheckExpectedBuffers("60|0K 180|30 90|60 120|90 150|120");
5213   CheckNoNextBuffer();
5214 }
5215
5216 TEST_F(SourceBufferStreamTest, OverlapSameTimestampWithinSameGOP) {
5217   // We use distinct appends here to make sure the intended frame durations
5218   // are respected by the test helpers (which the OneByOne helper doesn't
5219   // respect always). We need granular appends of this GOP for at least the
5220   // append of PTS=DTS=30, below.
5221   NewCodedFrameGroupAppend("0|0D10K");
5222   AppendBuffers("30|10D0");
5223   AppendBuffers("20|20D10");
5224
5225   // The following should *not* remove the PTS frame 30, above.
5226   AppendBuffers("30|30 40|40");
5227   Seek(0);
5228   CheckExpectedRangesByTimestamp("{ [0,50) }");
5229   CheckExpectedBuffers("0K 30|10 20 30 40");
5230 }
5231
5232 struct VideoEndTimeCase {
5233   // Times in Milliseconds
5234   int64_t new_frame_pts;
5235   int64_t new_frame_duration;
5236   int64_t expected_highest_pts;
5237   int64_t expected_end_time;
5238 };
5239
5240 TEST_F(SourceBufferStreamTest, VideoRangeEndTimeCases) {
5241   // With a basic range containing just a single keyframe [10,20), verify
5242   // various keyframe overlap append cases' results on the range end time.
5243   const VideoEndTimeCase kCases[] = {
5244       {0, 10, 10, 20},
5245       {20, 1, 20, 21},
5246       {15, 3, 15, 18},
5247       {15, 5, 15, 20},
5248       {15, 8, 15, 23},
5249
5250       // Cases where the new frame removes the previous frame:
5251       {10, 3, 10, 13},
5252       {10, 10, 10, 20},
5253       {10, 13, 10, 23},
5254       {5, 8, 5, 13},
5255       {5, 15, 5, 20},
5256       {5, 20, 5, 25}};
5257
5258   for (const auto& c : kCases) {
5259     RemoveInMs(0, 100, 100);
5260     NewCodedFrameGroupAppend("10D10K");
5261     CheckExpectedRangesByTimestamp("{ [10,20) }");
5262     CheckExpectedRangeEndTimes("{ <10,20> }");
5263
5264     std::stringstream ss;
5265     ss << c.new_frame_pts << "D" << c.new_frame_duration << "K";
5266     DVLOG(1) << "Appending " << ss.str();
5267     NewCodedFrameGroupAppend(ss.str());
5268
5269     std::stringstream expected;
5270     expected << "{ <" << c.expected_highest_pts << "," << c.expected_end_time
5271              << "> }";
5272     CheckExpectedRangeEndTimes(expected.str());
5273   }
5274 }
5275
5276 struct AudioEndTimeCase {
5277   // Times in Milliseconds
5278   int64_t new_frame_pts;
5279   int64_t new_frame_duration;
5280   int64_t expected_highest_pts;
5281   int64_t expected_end_time;
5282   bool expect_splice;
5283 };
5284
5285 TEST_F(SourceBufferStreamTest, AudioRangeEndTimeCases) {
5286   // With a basic range containing just a single keyframe [10,20), verify
5287   // various keyframe overlap append cases' results on the range end time.
5288   const AudioEndTimeCase kCases[] = {
5289       {0, 10, 10, 20, false},
5290       {20, 1, 20, 21, false},
5291       {15, 3, 15, 18, true},
5292       {15, 5, 15, 20, true},
5293       {15, 8, 15, 23, true},
5294
5295       // Cases where the new frame removes the previous frame:
5296       {10, 3, 10, 13, false},
5297       {10, 10, 10, 20, false},
5298       {10, 13, 10, 23, false},
5299       {5, 8, 5, 13, false},
5300       {5, 15, 5, 20, false},
5301       {5, 20, 5, 25, false}};
5302
5303   SetAudioStream();
5304   for (const auto& c : kCases) {
5305     InSequence s;
5306
5307     RemoveInMs(0, 100, 100);
5308     NewCodedFrameGroupAppend("10D10K");
5309     CheckExpectedRangesByTimestamp("{ [10,20) }");
5310     CheckExpectedRangeEndTimes("{ <10,20> }");
5311
5312     std::stringstream ss;
5313     ss << c.new_frame_pts << "D" << c.new_frame_duration << "K";
5314     if (c.expect_splice) {
5315       EXPECT_MEDIA_LOG(TrimmedSpliceOverlap(c.new_frame_pts * 1000, 10000,
5316                                             (20 - c.new_frame_pts) * 1000));
5317     }
5318     DVLOG(1) << "Appending " << ss.str();
5319     NewCodedFrameGroupAppend(ss.str());
5320
5321     std::stringstream expected;
5322     expected << "{ <" << c.expected_highest_pts << "," << c.expected_end_time
5323              << "> }";
5324     CheckExpectedRangeEndTimes(expected.str());
5325   }
5326 }
5327
5328 TEST_F(SourceBufferStreamTest, SameTimestampEstimatedDurations_Video) {
5329   // Start a coded frame group with a frame having a non-estimated duration.
5330   NewCodedFrameGroupAppend("10D10K");
5331
5332   // In the same coded frame group, append a same-timestamp frame with estimated
5333   // duration smaller than the first frame. (This can happen at least if there
5334   // was an intervening init segment resetting the estimation logic.) This
5335   // second frame need not be a keyframe. We use a non-keyframe here to
5336   // differentiate the buffers in CheckExpectedBuffers(), below.
5337   AppendBuffers("10D9E");
5338
5339   // The next append, which triggered https://crbug.com/761567, didn't need to
5340   // be with same timestamp as the earlier ones; it just needs to be in the same
5341   // buffered range.  Also, it doesn't need to be a keyframe, have an estimated
5342   // duration, nor be in the same coded frame group to trigger that issue.
5343   NewCodedFrameGroupAppend("11D10K");
5344
5345   Seek(0);
5346   CheckExpectedRangesByTimestamp("{ [10,21) }");
5347   CheckExpectedRangeEndTimes("{ <11,21> }");
5348   CheckExpectedBuffers("10K 10 11K");
5349   CheckNoNextBuffer();
5350 }
5351
5352 TEST_F(SourceBufferStreamTest, RangeIsNextInPTS_Simple) {
5353   // Append a simple GOP where DTS==PTS, perform basic PTS continuity checks.
5354   NewCodedFrameGroupAppend("10D10K");
5355   CheckIsNextInPTSSequenceWithFirstRange(9, false);
5356   CheckIsNextInPTSSequenceWithFirstRange(10, true);
5357   CheckIsNextInPTSSequenceWithFirstRange(20, true);
5358   CheckIsNextInPTSSequenceWithFirstRange(30, true);
5359   CheckIsNextInPTSSequenceWithFirstRange(31, false);
5360 }
5361
5362 TEST_F(SourceBufferStreamTest, RangeIsNextInPTS_OutOfOrder) {
5363   // Append a GOP where DTS != PTS such that a timestamp used as DTS would not
5364   // be continuous, but used as PTS is, and verify PTS continuity.
5365   NewCodedFrameGroupAppend("1000|0K 1120|30 1030|60 1060|90 1090|120");
5366   CheckIsNextInPTSSequenceWithFirstRange(0, false);
5367   CheckIsNextInPTSSequenceWithFirstRange(30, false);
5368   CheckIsNextInPTSSequenceWithFirstRange(60, false);
5369   CheckIsNextInPTSSequenceWithFirstRange(90, false);
5370   CheckIsNextInPTSSequenceWithFirstRange(120, false);
5371   CheckIsNextInPTSSequenceWithFirstRange(150, false);
5372   CheckIsNextInPTSSequenceWithFirstRange(1000, false);
5373   CheckIsNextInPTSSequenceWithFirstRange(1030, false);
5374   CheckIsNextInPTSSequenceWithFirstRange(1060, false);
5375   CheckIsNextInPTSSequenceWithFirstRange(1090, false);
5376   CheckIsNextInPTSSequenceWithFirstRange(1119, false);
5377   CheckIsNextInPTSSequenceWithFirstRange(1120, true);
5378   CheckIsNextInPTSSequenceWithFirstRange(1150, true);
5379   CheckIsNextInPTSSequenceWithFirstRange(1180, true);
5380   CheckIsNextInPTSSequenceWithFirstRange(1181, false);
5381 }
5382
5383 TEST_F(SourceBufferStreamTest, RangeCoalescenceOnFudgeRoomIncrease_1) {
5384   // Change the fudge room (by increasing frame duration) and verify coalescence
5385   // behavior.
5386   NewCodedFrameGroupAppend("0K 10K");
5387   NewCodedFrameGroupAppend("100K 110K");
5388   NewCodedFrameGroupAppend("500K 510K");
5389   CheckExpectedRangesByTimestamp("{ [0,20) [100,120) [500,520) }");
5390
5391   // Increase the fudge room almost enough to merge the first two buffered
5392   // ranges.
5393   NewCodedFrameGroupAppend("1000D44K");
5394   CheckExpectedRangesByTimestamp("{ [0,20) [100,120) [500,520) [1000,1044) }");
5395
5396   // Increase the fudge room again to merge the first two buffered ranges.
5397   NewCodedFrameGroupAppend("2000D45K");
5398   CheckExpectedRangesByTimestamp(
5399       "{ [0,120) [500,520) [1000,1044) [2000,2045) }");
5400
5401   SeekToTimestampMs(0);
5402   CheckExpectedBuffers("0K 10K 100K 110K");
5403   CheckNoNextBuffer();
5404   SeekToTimestampMs(500);
5405   CheckExpectedBuffers("500K 510K");
5406   CheckNoNextBuffer();
5407   SeekToTimestampMs(1000);
5408   CheckExpectedBuffers("1000K");
5409   CheckNoNextBuffer();
5410   SeekToTimestampMs(2000);
5411   CheckExpectedBuffers("2000K");
5412   CheckNoNextBuffer();
5413 }
5414
5415 TEST_F(SourceBufferStreamTest, RangeCoalescenceOnFudgeRoomIncrease_2) {
5416   // Change the fudge room (by increasing frame duration) and verify coalescence
5417   // behavior.
5418   NewCodedFrameGroupAppend("0K 10K");
5419   NewCodedFrameGroupAppend("40K 50K 60K");
5420   CheckExpectedRangesByTimestamp("{ [0,20) [40,70) }");
5421
5422   // Increase the fudge room to merge the first two buffered ranges.
5423   NewCodedFrameGroupAppend("1000D20K");
5424   CheckExpectedRangesByTimestamp("{ [0,70) [1000,1020) }");
5425
5426   // Try to trigger unsorted ranges, as might occur if the first two buffered
5427   // ranges were not correctly coalesced.
5428   NewCodedFrameGroupAppend("45D10K");
5429
5430   CheckExpectedRangesByTimestamp("{ [0,70) [1000,1020) }");
5431   SeekToTimestampMs(0);
5432   CheckExpectedBuffers("0K 10K 40K 45K 60K");
5433   CheckNoNextBuffer();
5434   SeekToTimestampMs(1000);
5435   CheckExpectedBuffers("1000K");
5436   CheckNoNextBuffer();
5437 }
5438
5439 TEST_F(SourceBufferStreamTest, NoRangeGapWhenIncrementallyOverlapped) {
5440   // Append 2 SAP-Type-1 GOPs continuous in DTS and PTS interval and with frame
5441   // durations and number of frames per GOP such that the first keyframe by
5442   // itself would not be considered "adjacent" to the second GOP by our fudge
5443   // room logic alone, but we now adjust the range start times occurring during
5444   // an overlap to enable overlap appends to remain continuous with the
5445   // remainder of the overlapped range, if any.  Then incrementally reappend
5446   // each frame of the first GOP.
5447   NewCodedFrameGroupAppend("0K 10 20 30 40 50K 60 70 80 90");
5448   Seek(0);
5449   CheckExpectedRangesByTimestamp("{ [0,100) }");
5450   CheckExpectedRangeEndTimes("{ <90,100> }");
5451   CheckExpectedBuffers("0K 10 20 30 40 50K 60 70 80 90");
5452   CheckNoNextBuffer();
5453
5454   NewCodedFrameGroupAppend("0D10K");  // Replaces first GOP with 1 frame.
5455   Seek(0);
5456   CheckExpectedRangesByTimestamp("{ [0,100) }");
5457   CheckExpectedRangeEndTimes("{ <90,100> }");
5458   CheckExpectedBuffers("0K 50K 60 70 80 90");
5459   CheckNoNextBuffer();
5460
5461   // Add more of the replacement GOP.
5462   AppendBuffers("10 20");
5463   Seek(0);
5464   CheckExpectedRangesByTimestamp("{ [0,100) }");
5465   CheckExpectedRangeEndTimes("{ <90,100> }");
5466   CheckExpectedBuffers("0K 10 20 50K 60 70 80 90");
5467   CheckNoNextBuffer();
5468
5469   // Add more of the replacement GOP.
5470   AppendBuffers("30D10");
5471   Seek(0);
5472   CheckExpectedRangesByTimestamp("{ [0,100) }");
5473   CheckExpectedRangeEndTimes("{ <90,100> }");
5474   CheckExpectedBuffers("0K 10 20 30 50K 60 70 80 90");
5475   CheckNoNextBuffer();
5476
5477   // Complete the replacement GOP.
5478   AppendBuffers("40D10");
5479   Seek(0);
5480   CheckExpectedRangesByTimestamp("{ [0,100) }");
5481   CheckExpectedRangeEndTimes("{ <90,100> }");
5482   CheckExpectedBuffers("0K 10 20 30 40 50K 60 70 80 90");
5483   CheckNoNextBuffer();
5484 }
5485
5486 TEST_F(SourceBufferStreamTest, AllowIncrementalAppendsToCoalesceRangeGap) {
5487   // Append a SAP-Type-1 GOP with a coded frame group start time far before the
5488   // timestamp of the first GOP (beyond any fudge room possible in this test).
5489   // This simulates one of multiple muxed tracks with jagged start times
5490   // following a discontinuity.
5491   // Then incrementally append a preceding SAP-Type-1 GOP with frames that
5492   // eventually are adjacent within fudge room of the first appended GOP's group
5493   // start time and observe the buffered range and demux gap coalesces. Finally,
5494   // incrementally append more frames of that preceding GOP to fill in the
5495   // timeline to abut the first appended GOP's keyframe timestamp and observe no
5496   // further buffered range change or discontinuity.
5497   NewCodedFrameGroupAppend(base::Milliseconds(100), "150K 160");
5498   SeekToTimestampMs(100);
5499   CheckExpectedRangesByTimestamp("{ [100,170) }");
5500   CheckExpectedRangeEndTimes("{ <160,170> }");
5501   CheckExpectedBuffers("150K 160");
5502   CheckNoNextBuffer();
5503
5504   NewCodedFrameGroupAppend("70D10K");
5505   SeekToTimestampMs(70);
5506   CheckExpectedRangesByTimestamp("{ [70,80) [100,170) }");
5507   CheckExpectedRangeEndTimes("{ <70,80> <160,170> }");
5508   CheckExpectedBuffers("70K");
5509   CheckNoNextBuffer();
5510   SeekToTimestampMs(100);
5511   CheckExpectedBuffers("150K 160");
5512   CheckNoNextBuffer();
5513
5514   AppendBuffers("80D10");  // 80ms is just close enough to 100ms to coalesce.
5515   SeekToTimestampMs(70);
5516   CheckExpectedRangesByTimestamp("{ [70,170) }");
5517   CheckExpectedRangeEndTimes("{ <160,170> }");
5518   CheckExpectedBuffers("70K 80 150K 160");
5519   CheckNoNextBuffer();
5520
5521   AppendBuffers("90D10");
5522   SeekToTimestampMs(70);
5523   CheckExpectedRangesByTimestamp("{ [70,170) }");
5524   CheckExpectedRangeEndTimes("{ <160,170> }");
5525   CheckExpectedBuffers("70K 80 90 150K 160");
5526   CheckNoNextBuffer();
5527
5528   AppendBuffers("100 110 120");
5529   SeekToTimestampMs(70);
5530   CheckExpectedRangesByTimestamp("{ [70,170) }");
5531   CheckExpectedRangeEndTimes("{ <160,170> }");
5532   CheckExpectedBuffers("70K 80 90 100 110 120 150K 160");
5533   CheckNoNextBuffer();
5534
5535   AppendBuffers("130D10");
5536   SeekToTimestampMs(70);
5537   CheckExpectedRangesByTimestamp("{ [70,170) }");
5538   CheckExpectedRangeEndTimes("{ <160,170> }");
5539   CheckExpectedBuffers("70K 80 90 100 110 120 130 150K 160");
5540   CheckNoNextBuffer();
5541
5542   AppendBuffers("140D10");
5543   SeekToTimestampMs(70);
5544   CheckExpectedRangesByTimestamp("{ [70,170) }");
5545   CheckExpectedRangeEndTimes("{ <160,170> }");
5546   CheckExpectedBuffers("70K 80 90 100 110 120 130 140 150K 160");
5547   CheckNoNextBuffer();
5548 }
5549
5550 TEST_F(SourceBufferStreamTest, PreciselyOverlapLastAudioFrameAppended_1) {
5551   // Appends an audio frame, A, which is then immediately followed by a
5552   // subsequent frame, B. Then appends a new frame, C, which precisely overlaps
5553   // frame B, and verifies that there is exactly 1 buffered range resulting.
5554   SetAudioStream();
5555
5556   // Frame A
5557   NewCodedFrameGroupAppend("0D10K");
5558   SeekToTimestampMs(0);
5559   CheckExpectedRangesByTimestamp("{ [0,10) }");
5560   CheckExpectedRangeEndTimes("{ <0,10> }");
5561   CheckExpectedBuffers("0K");
5562   CheckNoNextBuffer();
5563
5564   // Frame B
5565   NewCodedFrameGroupAppend("10D10K");
5566   SeekToTimestampMs(0);
5567   CheckExpectedRangesByTimestamp("{ [0,20) }");
5568   CheckExpectedRangeEndTimes("{ <10,20> }");
5569   CheckExpectedBuffers("0K 10K");
5570   CheckNoNextBuffer();
5571
5572   // Frame C.
5573   // Though DTS is continuous per MSE spec, FrameProcessor signals new CFG more
5574   // granularly, including in this case.
5575   NewCodedFrameGroupAppend("10D10K");
5576
5577   SeekToTimestampMs(0);
5578   CheckExpectedRangesByTimestamp("{ [0,20) }");
5579   CheckExpectedRangeEndTimes("{ <10,20> }");
5580   CheckExpectedBuffers("0K 10K");
5581   CheckNoNextBuffer();
5582 }
5583
5584 TEST_F(SourceBufferStreamTest, PreciselyOverlapLastAudioFrameAppended_2) {
5585   // Appends an audio frame, A, which is then splice-trim-truncated by a
5586   // subsequent frame, B. Then appends a new frame, C, which precisely overlaps
5587   // frame B, and verifies that there is exactly 1 buffered range resulting.
5588   SetAudioStream();
5589
5590   // Frame A
5591   NewCodedFrameGroupAppend("0D100K");
5592   SeekToTimestampMs(0);
5593   CheckExpectedRangesByTimestamp("{ [0,100) }");
5594   CheckExpectedRangeEndTimes("{ <0,100> }");
5595   CheckExpectedBuffers("0K");
5596   CheckNoNextBuffer();
5597
5598   // Frame B
5599   EXPECT_MEDIA_LOG(TrimmedSpliceOverlap(60000, 0, 40000));
5600   NewCodedFrameGroupAppend("60D10K");
5601   SeekToTimestampMs(0);
5602   CheckExpectedRangesByTimestamp("{ [0,70) }");
5603   CheckExpectedRangeEndTimes("{ <60,70> }");
5604   CheckExpectedBuffers("0K 60K");
5605   CheckNoNextBuffer();
5606
5607   // Frame C.
5608   // Though DTS is continuous per MSE spec, FrameProcessor signals new CFG more
5609   // granularly, including in this case.
5610   NewCodedFrameGroupAppend("60D10K");
5611
5612   SeekToTimestampMs(0);
5613   CheckExpectedRangesByTimestamp("{ [0,70) }");
5614   CheckExpectedRangeEndTimes("{ <60,70> }");
5615   CheckExpectedBuffers("0K 60K");
5616   CheckNoNextBuffer();
5617 }
5618
5619 TEST_F(SourceBufferStreamTest, ZeroDurationBuffersThenIncreasingFudgeRoom) {
5620   // Appends some zero duration buffers to result in disjoint buffered ranges.
5621   // Verifies that increasing the fudge room allows those that become within
5622   // adjacency threshold to merge, including those for which the new fudge room
5623   // is well more than sufficient to let them be adjacent.
5624   SetAudioStream();
5625
5626   NewCodedFrameGroupAppend("0uD0K");
5627   CheckExpectedRangesByTimestamp("{ [0,1) }", TimeGranularity::kMicrosecond);
5628
5629   NewCodedFrameGroupAppend("1uD0K");
5630   CheckExpectedRangesByTimestamp("{ [0,2) }", TimeGranularity::kMicrosecond);
5631
5632   // Initial fudge room allows for up to 2ms gap to coalesce.
5633   NewCodedFrameGroupAppend("5000uD0K");
5634   CheckExpectedRangesByTimestamp("{ [0,2) [5000,5001) }",
5635                                  TimeGranularity::kMicrosecond);
5636   NewCodedFrameGroupAppend("2002uD0K");
5637   CheckExpectedRangesByTimestamp("{ [0,2) [2002,2003) [5000,5001) }",
5638                                  TimeGranularity::kMicrosecond);
5639
5640   // Grow the fudge room enough to coalesce the first two ranges.
5641   NewCodedFrameGroupAppend("8000uD1001uK");
5642   CheckExpectedRangesByTimestamp("{ [0,2003) [5000,5001) [8000,9001) }",
5643                                  TimeGranularity::kMicrosecond);
5644
5645   // Append a buffer with duration 4ms, much larger than previous buffers. This
5646   // grows the fudge room to 8ms (2 * 4ms). Expect that the first three ranges
5647   // are retroactively merged due to being adjacent per the new, larger fudge
5648   // room.
5649   NewCodedFrameGroupAppend("100D4K");
5650   CheckExpectedRangesByTimestamp("{ [0,9001) [100000,104000) }",
5651                                  TimeGranularity::kMicrosecond);
5652   SeekToTimestampMs(0);
5653   CheckExpectedBuffers("0K 1K 2002K 5000K 8000K",
5654                        TimeGranularity::kMicrosecond);
5655   CheckNoNextBuffer();
5656   SeekToTimestampMs(100);
5657   CheckExpectedBuffers("100K");
5658   CheckNoNextBuffer();
5659 }
5660
5661 TEST_F(SourceBufferStreamTest, NonZeroDurationBuffersThenIncreasingFudgeRoom) {
5662   // Verifies that a single fudge room increase which merges more than 2
5663   // previously disjoint ranges in a row performs the merging correctly.
5664   NewCodedFrameGroupAppend("0D10K");
5665   NewCodedFrameGroupAppend("50D10K");
5666   NewCodedFrameGroupAppend("100D10K");
5667   NewCodedFrameGroupAppend("150D10K");
5668   NewCodedFrameGroupAppend("500D10K");
5669   CheckExpectedRangesByTimestamp(
5670       "{ [0,10) [50,60) [100,110) [150,160) [500,510) }");
5671
5672   NewCodedFrameGroupAppend("600D30K");
5673   CheckExpectedRangesByTimestamp("{ [0,160) [500,510) [600,630) }");
5674   SeekToTimestampMs(0);
5675   CheckExpectedBuffers("0K 50K 100K 150K");
5676   CheckNoNextBuffer();
5677   SeekToTimestampMs(500);
5678   CheckExpectedBuffers("500K");
5679   CheckNoNextBuffer();
5680   SeekToTimestampMs(600);
5681   CheckExpectedBuffers("600K");
5682   CheckNoNextBuffer();
5683 }
5684
5685 TEST_F(SourceBufferStreamTest, SapType2WithNonkeyframePtsInEarlierRange) {
5686   // Buffer a standalone GOP [0,10).
5687   NewCodedFrameGroupAppend("0D10K");
5688   CheckExpectedRangesByTimestamp("{ [0,10) }");
5689
5690   // Following discontinuity (simulated by DTS gap, signalled by new coded frame
5691   // group with time beyond fudge room of [0,10)), buffer 2 new GOPs in a later
5692   // range: a SAP-2 GOP with a nonkeyframe with PTS belonging to the first
5693   // range, and a subsequent minimal GOP.
5694   NewCodedFrameGroupAppend("30D10K 1|40D10");
5695   CheckExpectedRangesByTimestamp("{ [0,10) [30,40) }");
5696   NewCodedFrameGroupAppend("40|50D10K");
5697
5698   // Verify that there are two distinct ranges, and that the SAP-2 nonkeyframe
5699   // is buffered as part of the second range's first GOP.
5700   CheckExpectedRangesByTimestamp("{ [0,10) [30,50) }");
5701
5702   SeekToTimestampMs(0);
5703   CheckExpectedBuffers("0K");
5704   CheckNoNextBuffer();
5705   SeekToTimestampMs(30);
5706   CheckExpectedBuffers("30K 1|40 40|50K");
5707   CheckNoNextBuffer();
5708 }
5709
5710 TEST_F(SourceBufferStreamTest,
5711        MergeAllowedIfRangeEndTimeWithEstimatedDurationMatchesNextRangeStart) {
5712   // Tests the edge case where fudge room is not increased when an estimated
5713   // duration is increased due to overlap appends, causing two ranges to not be
5714   // within fudge room of each other (nor merged), yet abutting each other.
5715   // Append a GOP that has fudge room as its interval (e.g. 2 frames of same
5716   // duration >= minimum 1ms).
5717   NewCodedFrameGroupAppend("0D10K 10D10");
5718   CheckExpectedRangesByTimestamp("{ [0,20) }");
5719
5720   // Trigger a DTS discontinuity so later 21ms append also is discontinuous and
5721   // retains 10ms*2 fudge room.
5722   NewCodedFrameGroupAppend("100D10K");
5723   CheckExpectedRangesByTimestamp("{ [0,20) [100,110) }");
5724
5725   // Append another keyframe that starts within fudge room distance of the
5726   // non-keyframe in the GOP appended, above.
5727   NewCodedFrameGroupAppend("21D10K");
5728   CheckExpectedRangesByTimestamp("{ [0,31) [100,110) }");
5729
5730   // Overlap-append the original GOP with a single estimated-duration keyframe.
5731   // Though its timestamp is not within fudge room of the next keyframe, that
5732   // next keyframe at time 21ms was in the overlapped range and is retained in
5733   // the result of the overlap append's range.
5734   NewCodedFrameGroupAppend("0D10EK");
5735   CheckExpectedRangesByTimestamp("{ [0,31) [100,110) }");
5736
5737   // That new keyframe at time 0 now has derived estimated duration 21ms.  That
5738   // increased estimated duration did *not* increase the fudge room (which is
5739   // still 2 * 10ms = 20ms.) So the next line, which splices in a new frame at
5740   // time 21 causes the estimated keyframe at time 0 to not have a timestamp
5741   // within fudge room of the new range that starts right at 21ms, the same time
5742   // that ends the first buffered range, requiring CanAppendBuffersToEnd to
5743   // handle this scenario specifically.
5744   NewCodedFrameGroupAppend("21D10K");
5745   CheckExpectedRangesByTimestamp("{ [0,31) [100,110) }");
5746
5747   SeekToTimestampMs(0);
5748   CheckExpectedBuffers("0D21EK 21D10K");
5749   CheckNoNextBuffer();
5750   SeekToTimestampMs(100);
5751   CheckExpectedBuffers("100D10K");
5752   CheckNoNextBuffer();
5753 }
5754
5755 }  // namespace media