1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/filters/source_buffer_stream.h"
10 #include "base/bind_helpers.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "media/base/data_buffer.h"
16 #include "media/base/media_log.h"
17 #include "media/base/test_helpers.h"
18 #include "media/base/text_track_config.h"
19 #include "media/filters/webvtt_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
24 static const int kDefaultFramesPerSecond = 30;
25 static const int kDefaultKeyframesPerSecond = 6;
26 static const uint8 kDataA = 0x11;
27 static const uint8 kDataB = 0x33;
28 static const int kDataSize = 1;
30 class SourceBufferStreamTest : public testing::Test {
32 SourceBufferStreamTest() {
33 video_config_ = TestVideoConfig::Normal();
34 SetStreamInfo(kDefaultFramesPerSecond, kDefaultKeyframesPerSecond);
35 stream_.reset(new SourceBufferStream(video_config_, log_cb()));
38 void SetMemoryLimit(int buffers_of_data) {
39 stream_->set_memory_limit_for_testing(buffers_of_data * kDataSize);
42 void SetStreamInfo(int frames_per_second, int keyframes_per_second) {
43 frames_per_second_ = frames_per_second;
44 keyframes_per_second_ = keyframes_per_second;
45 frame_duration_ = ConvertToFrameDuration(frames_per_second);
48 void SetTextStream() {
49 video_config_ = TestVideoConfig::Invalid();
50 TextTrackConfig config(kTextSubtitles, "", "", "");
51 stream_.reset(new SourceBufferStream(config, LogCB()));
55 void NewSegmentAppend(int starting_position, int number_of_buffers) {
56 AppendBuffers(starting_position, number_of_buffers, true,
57 base::TimeDelta(), true, &kDataA, kDataSize);
60 void NewSegmentAppend(int starting_position, int number_of_buffers,
62 AppendBuffers(starting_position, number_of_buffers, true,
63 base::TimeDelta(), true, data, kDataSize);
66 void NewSegmentAppend_OffsetFirstBuffer(
67 int starting_position, int number_of_buffers,
68 base::TimeDelta first_buffer_offset) {
69 AppendBuffers(starting_position, number_of_buffers, true,
70 first_buffer_offset, true, &kDataA, kDataSize);
73 void NewSegmentAppend_ExpectFailure(
74 int starting_position, int number_of_buffers) {
75 AppendBuffers(starting_position, number_of_buffers, true,
76 base::TimeDelta(), false, &kDataA, kDataSize);
79 void AppendBuffers(int starting_position, int number_of_buffers) {
80 AppendBuffers(starting_position, number_of_buffers, false,
81 base::TimeDelta(), true, &kDataA, kDataSize);
84 void AppendBuffers(int starting_position, int number_of_buffers,
86 AppendBuffers(starting_position, number_of_buffers, false,
87 base::TimeDelta(), true, data, kDataSize);
90 void NewSegmentAppend(const std::string& buffers_to_append) {
91 AppendBuffers(buffers_to_append, true, kNoTimestamp(), false, true);
94 void NewSegmentAppend(base::TimeDelta start_timestamp,
95 const std::string& buffers_to_append) {
96 AppendBuffers(buffers_to_append, true, start_timestamp, false, true);
99 void AppendBuffers(const std::string& buffers_to_append) {
100 AppendBuffers(buffers_to_append, false, kNoTimestamp(), false, true);
103 void NewSegmentAppendOneByOne(const std::string& buffers_to_append) {
104 AppendBuffers(buffers_to_append, true, kNoTimestamp(), true, true);
107 void AppendBuffersOneByOne(const std::string& buffers_to_append) {
108 AppendBuffers(buffers_to_append, false, kNoTimestamp(), true, true);
111 void NewSegmentAppend_ExpectFailure(const std::string& buffers_to_append) {
112 AppendBuffers(buffers_to_append, true, kNoTimestamp(), false, false);
115 void AppendBuffers_ExpectFailure(const std::string& buffers_to_append) {
116 AppendBuffers(buffers_to_append, false, kNoTimestamp(), false, false);
119 void Seek(int position) {
120 stream_->Seek(position * frame_duration_);
123 void SeekToTimestamp(base::TimeDelta timestamp) {
124 stream_->Seek(timestamp);
127 void RemoveInMs(int start, int end, int duration) {
128 Remove(base::TimeDelta::FromMilliseconds(start),
129 base::TimeDelta::FromMilliseconds(end),
130 base::TimeDelta::FromMilliseconds(duration));
133 void Remove(base::TimeDelta start, base::TimeDelta end,
134 base::TimeDelta duration) {
135 stream_->Remove(start, end, duration);
138 int GetRemovalRangeInMs(int start, int end, int bytes_to_free,
140 base::TimeDelta removal_end_timestamp =
141 base::TimeDelta::FromMilliseconds(*removal_end);
142 int bytes_removed = stream_->GetRemovalRange(
143 base::TimeDelta::FromMilliseconds(start),
144 base::TimeDelta::FromMilliseconds(end), bytes_to_free,
145 &removal_end_timestamp);
146 *removal_end = removal_end_timestamp.InMilliseconds();
147 return bytes_removed;
150 void CheckExpectedRanges(const std::string& expected) {
151 Ranges<base::TimeDelta> r = stream_->GetBufferedTime();
153 std::stringstream ss;
155 for (size_t i = 0; i < r.size(); ++i) {
156 int64 start = (r.start(i) / frame_duration_);
157 int64 end = (r.end(i) / frame_duration_) - 1;
158 ss << "[" << start << "," << end << ") ";
161 EXPECT_EQ(expected, ss.str());
164 void CheckExpectedRangesByTimestamp(const std::string& expected) {
165 Ranges<base::TimeDelta> r = stream_->GetBufferedTime();
167 std::stringstream ss;
169 for (size_t i = 0; i < r.size(); ++i) {
170 int64 start = r.start(i).InMilliseconds();
171 int64 end = r.end(i).InMilliseconds();
172 ss << "[" << start << "," << end << ") ";
175 EXPECT_EQ(expected, ss.str());
178 void CheckExpectedBuffers(
179 int starting_position, int ending_position) {
180 CheckExpectedBuffers(starting_position, ending_position, false, NULL, 0);
183 void CheckExpectedBuffers(
184 int starting_position, int ending_position, bool expect_keyframe) {
185 CheckExpectedBuffers(starting_position, ending_position, expect_keyframe,
189 void CheckExpectedBuffers(
190 int starting_position, int ending_position, const uint8* data) {
191 CheckExpectedBuffers(starting_position, ending_position, false, data,
195 void CheckExpectedBuffers(
196 int starting_position, int ending_position, const uint8* data,
197 bool expect_keyframe) {
198 CheckExpectedBuffers(starting_position, ending_position, expect_keyframe,
202 void CheckExpectedBuffers(
203 int starting_position, int ending_position, bool expect_keyframe,
204 const uint8* expected_data, int expected_size) {
205 int current_position = starting_position;
206 for (; current_position <= ending_position; current_position++) {
207 scoped_refptr<StreamParserBuffer> buffer;
208 SourceBufferStream::Status status = stream_->GetNextBuffer(&buffer);
210 EXPECT_NE(status, SourceBufferStream::kConfigChange);
211 if (status != SourceBufferStream::kSuccess)
214 if (expect_keyframe && current_position == starting_position)
215 EXPECT_TRUE(buffer->IsKeyframe());
218 const uint8* actual_data = buffer->data();
219 const int actual_size = buffer->data_size();
220 EXPECT_EQ(expected_size, actual_size);
221 for (int i = 0; i < std::min(actual_size, expected_size); i++) {
222 EXPECT_EQ(expected_data[i], actual_data[i]);
226 EXPECT_EQ(buffer->GetDecodeTimestamp() / frame_duration_,
230 EXPECT_EQ(ending_position + 1, current_position);
233 void CheckExpectedBuffers(const std::string& expected) {
234 std::vector<std::string> timestamps;
235 base::SplitString(expected, ' ', ×tamps);
236 std::stringstream ss;
237 for (size_t i = 0; i < timestamps.size(); i++) {
238 scoped_refptr<StreamParserBuffer> buffer;
239 SourceBufferStream::Status status = stream_->GetNextBuffer(&buffer);
244 if (timestamps[i] == "C") {
245 EXPECT_EQ(SourceBufferStream::kConfigChange, status);
246 stream_->GetCurrentVideoDecoderConfig();
251 EXPECT_EQ(SourceBufferStream::kSuccess, status);
252 if (status != SourceBufferStream::kSuccess)
255 ss << buffer->GetDecodeTimestamp().InMilliseconds();
256 if (buffer->IsKeyframe())
259 EXPECT_EQ(expected, ss.str());
262 void CheckNoNextBuffer() {
263 scoped_refptr<StreamParserBuffer> buffer;
264 EXPECT_EQ(SourceBufferStream::kNeedBuffer, stream_->GetNextBuffer(&buffer));
267 void CheckVideoConfig(const VideoDecoderConfig& config) {
268 const VideoDecoderConfig& actual = stream_->GetCurrentVideoDecoderConfig();
269 EXPECT_TRUE(actual.Matches(config))
270 << "Expected: " << config.AsHumanReadableString()
271 << "\nActual: " << actual.AsHumanReadableString();
274 const LogCB log_cb() {
275 return base::Bind(&SourceBufferStreamTest::DebugMediaLog,
276 base::Unretained(this));
279 base::TimeDelta frame_duration() const { return frame_duration_; }
281 scoped_ptr<SourceBufferStream> stream_;
282 VideoDecoderConfig video_config_;
285 base::TimeDelta ConvertToFrameDuration(int frames_per_second) {
286 return base::TimeDelta::FromMicroseconds(
287 base::Time::kMicrosecondsPerSecond / frames_per_second);
290 void AppendBuffers(int starting_position,
291 int number_of_buffers,
292 bool begin_media_segment,
293 base::TimeDelta first_buffer_offset,
297 if (begin_media_segment)
298 stream_->OnNewMediaSegment(starting_position * frame_duration_);
300 int keyframe_interval = frames_per_second_ / keyframes_per_second_;
302 SourceBufferStream::BufferQueue queue;
303 for (int i = 0; i < number_of_buffers; i++) {
304 int position = starting_position + i;
305 bool is_keyframe = position % keyframe_interval == 0;
306 // Buffer type and track ID are meaningless to these tests.
307 scoped_refptr<StreamParserBuffer> buffer =
308 StreamParserBuffer::CopyFrom(data, size, is_keyframe,
309 DemuxerStream::AUDIO, 0);
310 base::TimeDelta timestamp = frame_duration_ * position;
313 timestamp += first_buffer_offset;
314 buffer->SetDecodeTimestamp(timestamp);
316 // Simulate an IBB...BBP pattern in which all B-frames reference both
317 // the I- and P-frames. For a GOP with playback order 12345, this would
318 // result in a decode timestamp order of 15234.
319 base::TimeDelta presentation_timestamp;
321 presentation_timestamp = timestamp;
322 } else if ((position - 1) % keyframe_interval == 0) {
323 // This is the P-frame (first frame following the I-frame)
324 presentation_timestamp =
325 (timestamp + frame_duration_ * (keyframe_interval - 2));
327 presentation_timestamp = timestamp - frame_duration_;
329 buffer->set_timestamp(presentation_timestamp);
331 queue.push_back(buffer);
334 EXPECT_EQ(expect_success, stream_->Append(queue));
337 // StringToBufferQueue() allows for the generation of StreamParserBuffers from
338 // coded strings of timestamps separated by spaces. Supported syntax:
341 // Generates a StreamParserBuffer with decode timestamp ##. E.g., "0 1 2 3".
344 // Indicates the buffer with timestamp ## reflects a keyframe. E.g., "0K 1".
347 // Indicates a splice frame buffer should be created with timestamp z#. The
348 // preceding timestamps a# ... y# will be treated as the fade out preroll for
349 // the splice frame. If a timestamp within the preroll ends with C the config
350 // id to use for that and subsequent preroll appends is incremented by one.
351 // The config id for non-splice frame appends will not be affected.
352 SourceBufferStream::BufferQueue StringToBufferQueue(
353 const std::string& buffers_to_append) {
354 std::vector<std::string> timestamps;
355 base::SplitString(buffers_to_append, ' ', ×tamps);
357 CHECK_GT(timestamps.size(), 0u);
359 bool splice_frame = false;
360 size_t splice_config_id = stream_->append_config_index_;
361 std::vector<scoped_refptr<StreamParserBuffer> > fade_out_preroll;
362 SourceBufferStream::BufferQueue buffers;
363 for (size_t i = 0; i < timestamps.size(); i++) {
364 bool is_keyframe = false;
365 bool last_splice_frame = false;
366 // Handle splice frame starts.
367 if (StartsWithASCII(timestamps[i], "S(", true)) {
368 CHECK(!splice_frame);
370 // Remove the "S(" off of the token.
371 timestamps[i] = timestamps[i].substr(2, timestamps[i].length());
373 if (splice_frame && EndsWith(timestamps[i], ")", true)) {
374 splice_frame = false;
375 last_splice_frame = true;
376 // Remove the ")" off of the token.
377 timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
379 // Handle config changes within the splice frame.
380 if (splice_frame && EndsWith(timestamps[i], "C", true)) {
382 CHECK(splice_config_id < stream_->audio_configs_.size() ||
383 splice_config_id < stream_->video_configs_.size());
384 // Remove the "C" off of the token.
385 timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
387 if (EndsWith(timestamps[i], "K", true)) {
389 // Remove the "K" off of the token.
390 timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
394 CHECK(base::StringToInt(timestamps[i], &time_in_ms));
396 // Create buffer. Buffer type and track ID are meaningless to these tests.
397 scoped_refptr<StreamParserBuffer> buffer =
398 StreamParserBuffer::CopyFrom(&kDataA, kDataSize, is_keyframe,
399 DemuxerStream::AUDIO, 0);
400 base::TimeDelta timestamp =
401 base::TimeDelta::FromMilliseconds(time_in_ms);
402 buffer->set_timestamp(timestamp);
403 buffer->SetDecodeTimestamp(timestamp);
406 if (!fade_out_preroll.empty()) {
407 // Enforce strictly monotonically increasing timestamps.
409 timestamp.InMicroseconds(),
410 fade_out_preroll.back()->GetDecodeTimestamp().InMicroseconds());
412 buffer->SetConfigId(splice_config_id);
413 fade_out_preroll.push_back(buffer);
417 if (last_splice_frame) {
418 // Forbid splice frames without preroll.
419 CHECK(!fade_out_preroll.empty());
420 buffer->SetFadeOutPreroll(fade_out_preroll);
421 fade_out_preroll.clear();
424 buffers.push_back(buffer);
429 void AppendBuffers(const std::string& buffers_to_append,
430 bool start_new_segment,
431 base::TimeDelta segment_start_timestamp,
433 bool expect_success) {
434 SourceBufferStream::BufferQueue buffers =
435 StringToBufferQueue(buffers_to_append);
437 if (start_new_segment) {
438 base::TimeDelta start_timestamp = segment_start_timestamp;
439 if (start_timestamp == kNoTimestamp())
440 start_timestamp = buffers[0]->GetDecodeTimestamp();
442 ASSERT_TRUE(start_timestamp <= buffers[0]->GetDecodeTimestamp());
444 stream_->OnNewMediaSegment(start_timestamp);
448 EXPECT_EQ(expect_success, stream_->Append(buffers));
452 // Append each buffer one by one.
453 for (size_t i = 0; i < buffers.size(); i++) {
454 SourceBufferStream::BufferQueue wrapper;
455 wrapper.push_back(buffers[i]);
456 EXPECT_TRUE(stream_->Append(wrapper));
460 void DebugMediaLog(const std::string& log) {
464 int frames_per_second_;
465 int keyframes_per_second_;
466 base::TimeDelta frame_duration_;
467 DISALLOW_COPY_AND_ASSIGN(SourceBufferStreamTest);
470 TEST_F(SourceBufferStreamTest, Append_SingleRange) {
471 // Append 15 buffers at positions 0 through 14.
472 NewSegmentAppend(0, 15);
474 // Check expected range.
475 CheckExpectedRanges("{ [0,14) }");
476 // Check buffers in range.
478 CheckExpectedBuffers(0, 14);
481 TEST_F(SourceBufferStreamTest, Append_SingleRange_OneBufferAtATime) {
482 // Append 15 buffers starting at position 0, one buffer at a time.
483 NewSegmentAppend(0, 1);
484 for (int i = 1; i < 15; i++)
487 // Check expected range.
488 CheckExpectedRanges("{ [0,14) }");
489 // Check buffers in range.
491 CheckExpectedBuffers(0, 14);
494 TEST_F(SourceBufferStreamTest, Append_DisjointRanges) {
495 // Append 5 buffers at positions 0 through 4.
496 NewSegmentAppend(0, 5);
498 // Append 10 buffers at positions 15 through 24.
499 NewSegmentAppend(15, 10);
501 // Check expected ranges.
502 CheckExpectedRanges("{ [0,4) [15,24) }");
503 // Check buffers in ranges.
505 CheckExpectedBuffers(0, 4);
507 CheckExpectedBuffers(15, 24);
510 TEST_F(SourceBufferStreamTest, Append_AdjacentRanges) {
511 // Append 10 buffers at positions 0 through 9.
512 NewSegmentAppend(0, 10);
514 // Append 11 buffers at positions 15 through 25.
515 NewSegmentAppend(15, 11);
517 // Append 5 buffers at positions 10 through 14 to bridge the gap.
518 NewSegmentAppend(10, 5);
520 // Check expected range.
521 CheckExpectedRanges("{ [0,25) }");
522 // Check buffers in range.
524 CheckExpectedBuffers(0, 25);
527 TEST_F(SourceBufferStreamTest, Append_DoesNotBeginWithKeyframe) {
528 // Append fails because the range doesn't begin with a keyframe.
529 NewSegmentAppend_ExpectFailure(3, 2);
531 // Append 10 buffers at positions 5 through 14.
532 NewSegmentAppend(5, 10);
534 // Check expected range.
535 CheckExpectedRanges("{ [5,14) }");
536 // Check buffers in range.
538 CheckExpectedBuffers(5, 14);
540 // Append fails because the range doesn't begin with a keyframe.
541 NewSegmentAppend_ExpectFailure(17, 3);
543 CheckExpectedRanges("{ [5,14) }");
545 CheckExpectedBuffers(5, 14);
548 TEST_F(SourceBufferStreamTest, Append_DoesNotBeginWithKeyframe_Adjacent) {
549 // Append 8 buffers at positions 0 through 7.
550 NewSegmentAppend(0, 8);
552 // Now start a new media segment at position 8. Append should fail because
553 // the media segment does not begin with a keyframe.
554 NewSegmentAppend_ExpectFailure(8, 2);
556 // Check expected range.
557 CheckExpectedRanges("{ [0,7) }");
558 // Check buffers in range.
560 CheckExpectedBuffers(0, 7);
563 TEST_F(SourceBufferStreamTest, Complete_Overlap) {
564 // Append 5 buffers at positions 5 through 9.
565 NewSegmentAppend(5, 5);
567 // Append 15 buffers at positions 0 through 14.
568 NewSegmentAppend(0, 15);
570 // Check expected range.
571 CheckExpectedRanges("{ [0,14) }");
572 // Check buffers in range.
574 CheckExpectedBuffers(0, 14);
577 TEST_F(SourceBufferStreamTest,
578 Complete_Overlap_AfterSegmentTimestampAndBeforeFirstBufferTimestamp) {
579 // Append a segment with a start timestamp of 0, but the first
580 // buffer starts at 30ms. This can happen in muxed content where the
581 // audio starts before the first frame.
582 NewSegmentAppend(base::TimeDelta::FromMilliseconds(0), "30K 60K 90K 120K");
584 CheckExpectedRangesByTimestamp("{ [0,150) }");
586 // Completely overlap the old buffers, with a segment that starts
587 // after the old segment start timestamp, but before the timestamp
588 // of the first buffer in the segment.
589 NewSegmentAppend("20K 50K 80K 110K");
591 // Verify that the buffered ranges are updated properly and we don't crash.
592 CheckExpectedRangesByTimestamp("{ [20,150) }");
594 SeekToTimestamp(base::TimeDelta::FromMilliseconds(20));
595 CheckExpectedBuffers("20K 50K 80K 110K 120K");
598 TEST_F(SourceBufferStreamTest, Complete_Overlap_EdgeCase) {
599 // Make each frame a keyframe so that it's okay to overlap frames at any point
600 // (instead of needing to respect keyframe boundaries).
601 SetStreamInfo(30, 30);
603 // Append 6 buffers at positions 6 through 11.
604 NewSegmentAppend(6, 6);
606 // Append 8 buffers at positions 5 through 12.
607 NewSegmentAppend(5, 8);
609 // Check expected range.
610 CheckExpectedRanges("{ [5,12) }");
611 // Check buffers in range.
613 CheckExpectedBuffers(5, 12);
616 TEST_F(SourceBufferStreamTest, Start_Overlap) {
617 // Append 10 buffers at positions 5 through 14.
618 NewSegmentAppend(5, 5);
620 // Append 6 buffers at positions 10 through 15.
621 NewSegmentAppend(10, 6);
623 // Check expected range.
624 CheckExpectedRanges("{ [5,15) }");
625 // Check buffers in range.
627 CheckExpectedBuffers(5, 15);
630 TEST_F(SourceBufferStreamTest, End_Overlap) {
631 // Append 10 buffers at positions 10 through 19.
632 NewSegmentAppend(10, 10);
634 // Append 10 buffers at positions 5 through 14.
635 NewSegmentAppend(5, 10);
637 // Check expected range.
638 CheckExpectedRanges("{ [5,19) }");
639 // Check buffers in range.
641 CheckExpectedBuffers(5, 19);
644 TEST_F(SourceBufferStreamTest, End_Overlap_Several) {
645 // Append 10 buffers at positions 10 through 19.
646 NewSegmentAppend(10, 10);
648 // Append 8 buffers at positions 5 through 12.
649 NewSegmentAppend(5, 8);
651 // Check expected ranges: stream should not have kept buffers 13 and 14
652 // because the keyframe on which they depended was overwritten.
653 CheckExpectedRanges("{ [5,12) [15,19) }");
655 // Check buffers in range.
657 CheckExpectedBuffers(5, 12);
661 CheckExpectedBuffers(15, 19);
664 // Test an end overlap edge case where a single buffer overlaps the
665 // beginning of a range.
666 // old : *0K* 30 60 90 120K 150
668 // after: *0K* *120K* 150K
670 TEST_F(SourceBufferStreamTest, End_Overlap_SingleBuffer) {
671 // Seek to start of stream.
672 SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
674 NewSegmentAppend("0K 30 60 90 120K 150");
675 CheckExpectedRangesByTimestamp("{ [0,180) }");
677 NewSegmentAppend("0K");
678 CheckExpectedRangesByTimestamp("{ [0,30) [120,180) }");
680 CheckExpectedBuffers("0K");
684 TEST_F(SourceBufferStreamTest, Complete_Overlap_Several) {
685 // Append 2 buffers at positions 5 through 6.
686 NewSegmentAppend(5, 2);
688 // Append 2 buffers at positions 10 through 11.
689 NewSegmentAppend(10, 2);
691 // Append 2 buffers at positions 15 through 16.
692 NewSegmentAppend(15, 2);
694 // Check expected ranges.
695 CheckExpectedRanges("{ [5,6) [10,11) [15,16) }");
697 // Append buffers at positions 0 through 19.
698 NewSegmentAppend(0, 20);
700 // Check expected range.
701 CheckExpectedRanges("{ [0,19) }");
702 // Check buffers in range.
704 CheckExpectedBuffers(0, 19);
707 TEST_F(SourceBufferStreamTest, Complete_Overlap_Several_Then_Merge) {
708 // Append 2 buffers at positions 5 through 6.
709 NewSegmentAppend(5, 2);
711 // Append 2 buffers at positions 10 through 11.
712 NewSegmentAppend(10, 2);
714 // Append 2 buffers at positions 15 through 16.
715 NewSegmentAppend(15, 2);
717 // Append 2 buffers at positions 20 through 21.
718 NewSegmentAppend(20, 2);
720 // Append buffers at positions 0 through 19.
721 NewSegmentAppend(0, 20);
723 // Check expected ranges.
724 CheckExpectedRanges("{ [0,21) }");
725 // Check buffers in range.
727 CheckExpectedBuffers(0, 21);
730 TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected) {
731 // Append 10 buffers at positions 5 through 14.
732 NewSegmentAppend(5, 10, &kDataA);
734 // Seek to buffer at position 5.
737 // Replace old data with new data.
738 NewSegmentAppend(5, 10, &kDataB);
740 // Check ranges are correct.
741 CheckExpectedRanges("{ [5,14) }");
743 // Check that data has been replaced with new data.
744 CheckExpectedBuffers(5, 14, &kDataB);
747 // This test is testing that a client can append data to SourceBufferStream that
748 // overlaps the range from which the client is currently grabbing buffers. We
749 // would expect that the SourceBufferStream would return old data until it hits
750 // the keyframe of the new data, after which it will return the new data.
751 TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected_TrackBuffer) {
752 // Append 10 buffers at positions 5 through 14.
753 NewSegmentAppend(5, 10, &kDataA);
755 // Seek to buffer at position 5 and get next buffer.
757 CheckExpectedBuffers(5, 5, &kDataA);
759 // Do a complete overlap by appending 20 buffers at positions 0 through 19.
760 NewSegmentAppend(0, 20, &kDataB);
762 // Check range is correct.
763 CheckExpectedRanges("{ [0,19) }");
765 // Expect old data up until next keyframe in new data.
766 CheckExpectedBuffers(6, 9, &kDataA);
767 CheckExpectedBuffers(10, 10, &kDataB, true);
769 // Expect rest of data to be new.
770 CheckExpectedBuffers(11, 19, &kDataB);
772 // Seek back to beginning; all data should be new.
774 CheckExpectedBuffers(0, 19, &kDataB);
776 // Check range continues to be correct.
777 CheckExpectedRanges("{ [0,19) }");
780 TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected_EdgeCase) {
781 // Append 10 buffers at positions 5 through 14.
782 NewSegmentAppend(5, 10, &kDataA);
784 // Seek to buffer at position 5 and get next buffer.
786 CheckExpectedBuffers(5, 5, &kDataA);
788 // Replace existing data with new data.
789 NewSegmentAppend(5, 10, &kDataB);
791 // Check ranges are correct.
792 CheckExpectedRanges("{ [5,14) }");
794 // Expect old data up until next keyframe in new data.
795 CheckExpectedBuffers(6, 9, &kDataA);
796 CheckExpectedBuffers(10, 10, &kDataB, true);
798 // Expect rest of data to be new.
799 CheckExpectedBuffers(11, 14, &kDataB);
801 // Seek back to beginning; all data should be new.
803 CheckExpectedBuffers(5, 14, &kDataB);
805 // Check range continues to be correct.
806 CheckExpectedRanges("{ [5,14) }");
809 TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected_Multiple) {
810 static const uint8 kDataC = 0x55;
811 static const uint8 kDataD = 0x77;
813 // Append 5 buffers at positions 5 through 9.
814 NewSegmentAppend(5, 5, &kDataA);
816 // Seek to buffer at position 5 and get next buffer.
818 CheckExpectedBuffers(5, 5, &kDataA);
820 // Replace existing data with new data.
821 NewSegmentAppend(5, 5, &kDataB);
823 // Then replace it again with different data.
824 NewSegmentAppend(5, 5, &kDataC);
826 // Now append 5 new buffers at positions 10 through 14.
827 NewSegmentAppend(10, 5, &kDataC);
829 // Now replace all the data entirely.
830 NewSegmentAppend(5, 10, &kDataD);
832 // Expect buffers 6 through 9 to be DataA, and the remaining
833 // buffers to be kDataD.
834 CheckExpectedBuffers(6, 9, &kDataA);
835 CheckExpectedBuffers(10, 14, &kDataD);
837 // At this point we cannot fulfill request.
840 // Seek back to beginning; all data should be new.
842 CheckExpectedBuffers(5, 14, &kDataD);
845 TEST_F(SourceBufferStreamTest, Start_Overlap_Selected) {
846 // Append 10 buffers at positions 0 through 9.
847 NewSegmentAppend(0, 10, &kDataA);
849 // Seek to position 5, then add buffers to overlap data at that position.
851 NewSegmentAppend(5, 10, &kDataB);
853 // Check expected range.
854 CheckExpectedRanges("{ [0,14) }");
856 // Because we seeked to a keyframe, the next buffers should all be new data.
857 CheckExpectedBuffers(5, 14, &kDataB);
859 // Make sure all data is correct.
861 CheckExpectedBuffers(0, 4, &kDataA);
862 CheckExpectedBuffers(5, 14, &kDataB);
865 TEST_F(SourceBufferStreamTest, Start_Overlap_Selected_TrackBuffer) {
866 // Append 15 buffers at positions 0 through 14.
867 NewSegmentAppend(0, 15, &kDataA);
869 // Seek to 10 and get buffer.
871 CheckExpectedBuffers(10, 10, &kDataA);
873 // Now append 10 buffers of new data at positions 10 through 19.
874 NewSegmentAppend(10, 10, &kDataB);
876 // Check expected range.
877 CheckExpectedRanges("{ [0,19) }");
879 // The next 4 buffers should be a from the old buffer, followed by a keyframe
880 // from the new data.
881 CheckExpectedBuffers(11, 14, &kDataA);
882 CheckExpectedBuffers(15, 15, &kDataB, true);
884 // The rest of the buffers should be new data.
885 CheckExpectedBuffers(16, 19, &kDataB);
887 // Now seek to the beginning; positions 0 through 9 should be the original
888 // data, positions 10 through 19 should be the new data.
890 CheckExpectedBuffers(0, 9, &kDataA);
891 CheckExpectedBuffers(10, 19, &kDataB);
893 // Make sure range is still correct.
894 CheckExpectedRanges("{ [0,19) }");
897 TEST_F(SourceBufferStreamTest, Start_Overlap_Selected_EdgeCase) {
898 // Append 10 buffers at positions 5 through 14.
899 NewSegmentAppend(5, 10, &kDataA);
902 CheckExpectedBuffers(10, 10, &kDataA);
904 // Now replace the last 5 buffers with new data.
905 NewSegmentAppend(10, 5, &kDataB);
907 // The next 4 buffers should be the origial data, held in the track buffer.
908 CheckExpectedBuffers(11, 14, &kDataA);
910 // The next buffer is at position 15, so we should fail to fulfill the
914 // Now append data at 15 through 19 and check to make sure it's correct.
915 NewSegmentAppend(15, 5, &kDataB);
916 CheckExpectedBuffers(15, 19, &kDataB);
918 // Seek to beginning of buffered range and check buffers.
920 CheckExpectedBuffers(5, 9, &kDataA);
921 CheckExpectedBuffers(10, 19, &kDataB);
923 // Check expected range.
924 CheckExpectedRanges("{ [5,19) }");
927 // This test covers the case where new buffers end-overlap an existing, selected
928 // range, and the next buffer is a keyframe that's being overlapped by new
930 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
931 // old  : *A*a a a a A a a a a
932 // new  : B b b b b B b b b b
933 // after: B b b b b*B*b b b b A a a a a
934 TEST_F(SourceBufferStreamTest, End_Overlap_Selected) {
935 // Append 10 buffers at positions 5 through 14.
936 NewSegmentAppend(5, 10, &kDataA);
938 // Seek to position 5.
941 // Now append 10 buffers at positions 0 through 9.
942 NewSegmentAppend(0, 10, &kDataB);
944 // Check expected range.
945 CheckExpectedRanges("{ [0,14) }");
947 // Because we seeked to a keyframe, the next buffers should be new.
948 CheckExpectedBuffers(5, 9, &kDataB);
950 // Make sure all data is correct.
952 CheckExpectedBuffers(0, 9, &kDataB);
953 CheckExpectedBuffers(10, 14, &kDataA);
956 // This test covers the case where new buffers end-overlap an existing, selected
957 // range, and the next buffer in the range is after the newly appended buffers.
958 // In this particular case, the end overlap does not require a split.
960 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
961 // old  : |A a a a a A a a*a*a|
962 // new  : B b b b b B b b b b
963 // after: |B b b b b B b b b b A a a*a*a|
964 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_AfterEndOfNew_1) {
965 // Append 10 buffers at positions 5 through 14.
966 NewSegmentAppend(5, 10, &kDataA);
968 // Seek to position 10, then move to position 13.
970 CheckExpectedBuffers(10, 12, &kDataA);
972 // Now append 10 buffers at positions 0 through 9.
973 NewSegmentAppend(0, 10, &kDataB);
975 // Check expected range.
976 CheckExpectedRanges("{ [0,14) }");
978 // Make sure rest of data is as expected.
979 CheckExpectedBuffers(13, 14, &kDataA);
981 // Make sure all data is correct.
983 CheckExpectedBuffers(0, 9, &kDataB);
984 CheckExpectedBuffers(10, 14, &kDataA);
987 // This test covers the case where new buffers end-overlap an existing, selected
988 // range, and the next buffer in the range is after the newly appended buffers.
989 // In this particular case, the end overlap requires a split, and the next
990 // buffer is in the split range.
992 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
993 // old  : |A a a a a A a a*a*a|
994 // new  : B b b b b B b b
995 // after: |B b b b b B b b| |A a a*a*a|
996 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_AfterEndOfNew_2) {
997 // Append 10 buffers at positions 5 through 14.
998 NewSegmentAppend(5, 10, &kDataA);
1000 // Seek to position 10, then move to position 13.
1002 CheckExpectedBuffers(10, 12, &kDataA);
1004 // Now append 8 buffers at positions 0 through 7.
1005 NewSegmentAppend(0, 8, &kDataB);
1007 // Check expected ranges.
1008 CheckExpectedRanges("{ [0,7) [10,14) }");
1010 // Make sure rest of data is as expected.
1011 CheckExpectedBuffers(13, 14, &kDataA);
1013 // Make sure all data is correct.
1015 CheckExpectedBuffers(0, 7, &kDataB);
1016 CheckNoNextBuffer();
1019 CheckExpectedBuffers(10, 14, &kDataA);
1022 // This test covers the case where new buffers end-overlap an existing, selected
1023 // range, and the next buffer in the range is after the newly appended buffers.
1024 // In this particular case, the end overlap requires a split, and the next
1025 // buffer was in between the end of the new data and the split range.
1027 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1028 // old  : |A a a*a*a A a a a a|
1029 // new  : B b b b b B b b
1030 // after: |B b b b b B b b| |A a a a a|
1032 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_AfterEndOfNew_3) {
1033 // Append 10 buffers at positions 5 through 14.
1034 NewSegmentAppend(5, 10, &kDataA);
1036 // Seek to position 5, then move to position 8.
1038 CheckExpectedBuffers(5, 7, &kDataA);
1040 // Now append 8 buffers at positions 0 through 7.
1041 NewSegmentAppend(0, 8, &kDataB);
1043 // Check expected ranges.
1044 CheckExpectedRanges("{ [0,7) [10,14) }");
1046 // Check for data in the track buffer.
1047 CheckExpectedBuffers(8, 9, &kDataA);
1048 // The buffer immediately after the track buffer should be a keyframe.
1049 CheckExpectedBuffers(10, 10, &kDataA, true);
1051 // Make sure all data is correct.
1053 CheckExpectedBuffers(0, 7, &kDataB);
1055 CheckExpectedBuffers(10, 14, &kDataA);
1058 // This test covers the case where new buffers end-overlap an existing, selected
1059 // range, and the next buffer in the range is overlapped by the new buffers.
1060 // In this particular case, the end overlap does not require a split.
1062 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1063 // old  : |A a a*a*a A a a a a|
1064 // new  : B b b b b B b b b b
1065 // after: |B b b b b B b b b b A a a a a|
1067 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_OverlappedByNew_1) {
1068 // Append 10 buffers at positions 5 through 14.
1069 NewSegmentAppend(5, 10, &kDataA);
1071 // Seek to position 5, then move to position 8.
1073 CheckExpectedBuffers(5, 7, &kDataA);
1075 // Now append 10 buffers at positions 0 through 9.
1076 NewSegmentAppend(0, 10, &kDataB);
1078 // Check expected range.
1079 CheckExpectedRanges("{ [0,14) }");
1081 // Check for data in the track buffer.
1082 CheckExpectedBuffers(8, 9, &kDataA);
1083 // The buffer immediately after the track buffer should be a keyframe.
1084 CheckExpectedBuffers(10, 10, &kDataA, true);
1086 // Make sure all data is correct.
1088 CheckExpectedBuffers(0, 9, &kDataB);
1089 CheckExpectedBuffers(10, 14, &kDataA);
1092 // This test covers the case where new buffers end-overlap an existing, selected
1093 // range, and the next buffer in the range is overlapped by the new buffers.
1094 // In this particular case, the end overlap requires a split, and the next
1095 // keyframe after the track buffer is in the split range.
1097 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1098 // old  : |A*a*a a a A a a a a|
1099 // new  : B b b b b B b
1100 // after: |B b b b b B b| |A a a a a|
1102 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_OverlappedByNew_2) {
1103 // Append 10 buffers at positions 5 through 14.
1104 NewSegmentAppend(5, 10, &kDataA);
1106 // Seek to position 5, then move to position 6.
1108 CheckExpectedBuffers(5, 5, &kDataA);
1110 // Now append 7 buffers at positions 0 through 6.
1111 NewSegmentAppend(0, 7, &kDataB);
1113 // Check expected ranges.
1114 CheckExpectedRanges("{ [0,6) [10,14) }");
1116 // Check for data in the track buffer.
1117 CheckExpectedBuffers(6, 9, &kDataA);
1118 // The buffer immediately after the track buffer should be a keyframe.
1119 CheckExpectedBuffers(10, 10, &kDataA, true);
1121 // Make sure all data is correct.
1123 CheckExpectedBuffers(0, 6, &kDataB);
1124 CheckNoNextBuffer();
1127 CheckExpectedBuffers(10, 14, &kDataA);
1130 // This test covers the case where new buffers end-overlap an existing, selected
1131 // range, and the next buffer in the range is overlapped by the new buffers.
1132 // In this particular case, the end overlap requires a split, and the next
1133 // keyframe after the track buffer is in the range with the new buffers.
1135 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1136 // old  : |A*a*a a a A a a a a A a a a a|
1137 // new  : B b b b b B b b b b B b b
1138 // after: |B b b b b B b b b b B b b| |A a a a a|
1140 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_OverlappedByNew_3) {
1141 // Append 15 buffers at positions 5 through 19.
1142 NewSegmentAppend(5, 15, &kDataA);
1144 // Seek to position 5, then move to position 6.
1146 CheckExpectedBuffers(5, 5, &kDataA);
1148 // Now append 13 buffers at positions 0 through 12.
1149 NewSegmentAppend(0, 13, &kDataB);
1151 // Check expected ranges.
1152 CheckExpectedRanges("{ [0,12) [15,19) }");
1154 // Check for data in the track buffer.
1155 CheckExpectedBuffers(6, 9, &kDataA);
1156 // The buffer immediately after the track buffer should be a keyframe
1157 // from the new data.
1158 CheckExpectedBuffers(10, 10, &kDataB, true);
1160 // Make sure all data is correct.
1162 CheckExpectedBuffers(0, 12, &kDataB);
1163 CheckNoNextBuffer();
1166 CheckExpectedBuffers(15, 19, &kDataA);
1169 // This test covers the case where new buffers end-overlap an existing, selected
1170 // range, and there is no keyframe after the end of the new buffers.
1171 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1172 // old  : |A*a*a a a|
1173 // new  : B b b b b B
1174 // after: |B b b b b B|
1176 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_NoKeyframeAfterNew) {
1177 // Append 5 buffers at positions 5 through 9.
1178 NewSegmentAppend(5, 5, &kDataA);
1180 // Seek to position 5, then move to position 6.
1182 CheckExpectedBuffers(5, 5, &kDataA);
1184 // Now append 6 buffers at positions 0 through 5.
1185 NewSegmentAppend(0, 6, &kDataB);
1187 // Check expected range.
1188 CheckExpectedRanges("{ [0,5) }");
1190 // Check for data in the track buffer.
1191 CheckExpectedBuffers(6, 9, &kDataA);
1193 // Now there's no data to fulfill the request.
1194 CheckNoNextBuffer();
1196 // Let's fill in the gap, buffers 6 through 10.
1197 AppendBuffers(6, 5, &kDataB);
1199 // We should be able to get the next buffer.
1200 CheckExpectedBuffers(10, 10, &kDataB);
1203 // This test covers the case where new buffers end-overlap an existing, selected
1204 // range, and there is no keyframe after the end of the new buffers, then the
1205 // range gets split.
1206 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1207 // old  : |A a a a a A*a*|
1208 // new  : B b b b b B b b b b B
1209 // after: |B b b b b B b b b b B|
1210 // new  : A a a a a A
1211 // after: |A a a a a A| |B b b b b B|
1213 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_NoKeyframeAfterNew2) {
1214 // Append 7 buffers at positions 10 through 16.
1215 NewSegmentAppend(10, 7, &kDataA);
1217 // Seek to position 15, then move to position 16.
1219 CheckExpectedBuffers(15, 15, &kDataA);
1221 // Now append 11 buffers at positions 5 through 15.
1222 NewSegmentAppend(5, 11, &kDataB);
1223 CheckExpectedRanges("{ [5,15) }");
1225 // Now do another end-overlap to split the range into two parts, where the
1226 // 2nd range should have the next buffer position.
1227 NewSegmentAppend(0, 6, &kDataA);
1228 CheckExpectedRanges("{ [0,5) [10,15) }");
1230 // Check for data in the track buffer.
1231 CheckExpectedBuffers(16, 16, &kDataA);
1233 // Now there's no data to fulfill the request.
1234 CheckNoNextBuffer();
1236 // Add data to the 2nd range, should not be able to fulfill the next read
1237 // until we've added a keyframe.
1238 NewSegmentAppend(15, 1, &kDataB);
1239 CheckNoNextBuffer();
1240 for (int i = 16; i <= 19; i++) {
1241 AppendBuffers(i, 1, &kDataB);
1242 CheckNoNextBuffer();
1245 // Now append a keyframe.
1246 AppendBuffers(20, 1, &kDataB);
1248 // We should be able to get the next buffer.
1249 CheckExpectedBuffers(20, 20, &kDataB, true);
1252 // This test covers the case where new buffers end-overlap an existing, selected
1253 // range, and the next keyframe in a separate range.
1254 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1255 // old  : |A*a*a a a| |A a a a a|
1256 // new  : B b b b b B
1257 // after: |B b b b b B| |A a a a a|
1259 TEST_F(SourceBufferStreamTest, End_Overlap_Selected_NoKeyframeAfterNew3) {
1260 // Append 5 buffers at positions 5 through 9.
1261 NewSegmentAppend(5, 5, &kDataA);
1263 // Append 5 buffers at positions 15 through 19.
1264 NewSegmentAppend(15, 5, &kDataA);
1266 // Check expected range.
1267 CheckExpectedRanges("{ [5,9) [15,19) }");
1269 // Seek to position 5, then move to position 6.
1271 CheckExpectedBuffers(5, 5, &kDataA);
1273 // Now append 6 buffers at positions 0 through 5.
1274 NewSegmentAppend(0, 6, &kDataB);
1276 // Check expected range.
1277 CheckExpectedRanges("{ [0,5) [15,19) }");
1279 // Check for data in the track buffer.
1280 CheckExpectedBuffers(6, 9, &kDataA);
1282 // Now there's no data to fulfill the request.
1283 CheckNoNextBuffer();
1285 // Let's fill in the gap, buffers 6 through 14.
1286 AppendBuffers(6, 9, &kDataB);
1288 // Check expected range.
1289 CheckExpectedRanges("{ [0,19) }");
1291 // We should be able to get the next buffer.
1292 CheckExpectedBuffers(10, 14, &kDataB);
1294 // We should be able to get the next buffer.
1295 CheckExpectedBuffers(15, 19, &kDataA);
1298 // This test covers the case when new buffers overlap the middle of a selected
1299 // range. This tests the case when there is no split and the next buffer is a
1301 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1302 // old  : A a a a a*A*a a a a A a a a a
1303 // new  : B b b b b
1304 // after: A a a a a*B*b b b b A a a a a
1305 TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_1) {
1306 // Append 15 buffers at positions 0 through 14.
1307 NewSegmentAppend(0, 15, &kDataA);
1309 // Seek to position 5.
1312 // Now append 5 buffers at positions 5 through 9.
1313 NewSegmentAppend(5, 5, &kDataB);
1315 // Check expected range.
1316 CheckExpectedRanges("{ [0,14) }");
1318 // Check for next data; should be new data.
1319 CheckExpectedBuffers(5, 9, &kDataB);
1321 // Make sure all data is correct.
1323 CheckExpectedBuffers(0, 4, &kDataA);
1324 CheckExpectedBuffers(5, 9, &kDataB);
1325 CheckExpectedBuffers(10, 14, &kDataA);
1328 // This test covers the case when new buffers overlap the middle of a selected
1329 // range. This tests the case when there is no split and the next buffer is
1330 // after the new buffers.
1331 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1332 // old  : A a a a a A a a a a A*a*a a a
1333 // new  : B b b b b
1334 // after: A a a a a B b b b b A*a*a a a
1335 TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_2) {
1336 // Append 15 buffers at positions 0 through 14.
1337 NewSegmentAppend(0, 15, &kDataA);
1339 // Seek to 10 then move to position 11.
1341 CheckExpectedBuffers(10, 10, &kDataA);
1343 // Now append 5 buffers at positions 5 through 9.
1344 NewSegmentAppend(5, 5, &kDataB);
1346 // Check expected range.
1347 CheckExpectedRanges("{ [0,14) }");
1349 // Make sure data is correct.
1350 CheckExpectedBuffers(11, 14, &kDataA);
1352 CheckExpectedBuffers(0, 4, &kDataA);
1353 CheckExpectedBuffers(5, 9, &kDataB);
1354 CheckExpectedBuffers(10, 14, &kDataA);
1357 // This test covers the case when new buffers overlap the middle of a selected
1358 // range. This tests the case when there is a split and the next buffer is
1359 // before the new buffers.
1360 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1361 // old  : A a*a*a a A a a a a A a a a a
1363 // after: A a*a*a a B b b| |A a a a a
1364 TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_3) {
1365 // Append 15 buffers at positions 0 through 14.
1366 NewSegmentAppend(0, 15, &kDataA);
1368 // Seek to beginning then move to position 2.
1370 CheckExpectedBuffers(0, 1, &kDataA);
1372 // Now append 3 buffers at positions 5 through 7.
1373 NewSegmentAppend(5, 3, &kDataB);
1375 // Check expected range.
1376 CheckExpectedRanges("{ [0,7) [10,14) }");
1378 // Make sure data is correct.
1379 CheckExpectedBuffers(2, 4, &kDataA);
1380 CheckExpectedBuffers(5, 7, &kDataB);
1382 CheckExpectedBuffers(10, 14, &kDataA);
1385 // This test covers the case when new buffers overlap the middle of a selected
1386 // range. This tests the case when there is a split and the next buffer is after
1387 // the new buffers but before the split range.
1388 // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1389 // old  : A a a a a A a a*a*a A a a a a
1391 // after: |A a a a a B b b| |A a a a a|
1393 TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_4) {
1394 // Append 15 buffers at positions 0 through 14.
1395 NewSegmentAppend(0, 15, &kDataA);
1397 // Seek to 5 then move to position 8.
1399 CheckExpectedBuffers(5, 7, &kDataA);
1401 // Now append 3 buffers at positions 5 through 7.
1402 NewSegmentAppend(5, 3, &kDataB);
1404 // Check expected range.
1405 CheckExpectedRanges("{ [0,7) [10,14) }");
1407 // Buffers 8 and 9 should be in the track buffer.
1408 CheckExpectedBuffers(8, 9, &kDataA);
1409 // The buffer immediately after the track buffer should be a keyframe.
1410 CheckExpectedBuffers(10, 10, &kDataA, true);
1412 // Make sure all data is correct.
1414 CheckExpectedBuffers(0, 4, &kDataA);
1415 CheckExpectedBuffers(5, 7, &kDataB);
1417 CheckExpectedBuffers(10, 14, &kDataA);
1420 TEST_F(SourceBufferStreamTest, Overlap_OneByOne) {
1421 // Append 5 buffers starting at 10ms, 30ms apart.
1422 NewSegmentAppendOneByOne("10K 40 70 100 130");
1424 // The range ends at 160, accounting for the last buffer's duration.
1425 CheckExpectedRangesByTimestamp("{ [10,160) }");
1427 // Overlap with 10 buffers starting at the beginning, appended one at a
1429 NewSegmentAppend(0, 1, &kDataB);
1430 for (int i = 1; i < 10; i++)
1431 AppendBuffers(i, 1, &kDataB);
1433 // All data should be replaced.
1435 CheckExpectedRanges("{ [0,9) }");
1436 CheckExpectedBuffers(0, 9, &kDataB);
1439 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_DeleteGroup) {
1440 NewSegmentAppendOneByOne("10K 40 70 100 130K");
1441 CheckExpectedRangesByTimestamp("{ [10,160) }");
1444 SeekToTimestamp(base::TimeDelta::FromMilliseconds(130));
1446 // Overlap with a new segment from 0 to 120ms.
1447 NewSegmentAppendOneByOne("0K 120");
1449 // Next buffer should still be 130ms.
1450 CheckExpectedBuffers("130K");
1452 // Check the final buffers is correct.
1453 SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
1454 CheckExpectedBuffers("0K 120 130K");
1457 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_BetweenMediaSegments) {
1458 // Append 5 buffers starting at 110ms, 30ms apart.
1459 NewSegmentAppendOneByOne("110K 140 170 200 230");
1460 CheckExpectedRangesByTimestamp("{ [110,260) }");
1462 // Now append 2 media segments from 0ms to 210ms, 30ms apart. Note that the
1463 // old keyframe 110ms falls in between these two segments.
1464 NewSegmentAppendOneByOne("0K 30 60 90");
1465 NewSegmentAppendOneByOne("120K 150 180 210");
1466 CheckExpectedRangesByTimestamp("{ [0,240) }");
1468 // Check the final buffers is correct; the keyframe at 110ms should be
1470 SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
1471 CheckExpectedBuffers("0K 30 60 90 120K 150 180 210");
1474 // old : 10K 40 *70* 100K 125 130K
1475 // new : 0K 30 60 90 120K
1476 // after: 0K 30 60 90 *120K* 130K
1477 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer) {
1478 NewSegmentAppendOneByOne("10K 40 70 100K 125 130K");
1479 CheckExpectedRangesByTimestamp("{ [10,160) }");
1482 SeekToTimestamp(base::TimeDelta::FromMilliseconds(70));
1483 CheckExpectedBuffers("10K 40");
1485 // Overlap with a new segment from 0 to 120ms.
1486 NewSegmentAppendOneByOne("0K 30 60 90 120K");
1487 CheckExpectedRangesByTimestamp("{ [0,160) }");
1489 // Should return frames 70ms and 100ms from the track buffer, then switch
1490 // to the new data at 120K, then switch back to the old data at 130K. The
1491 // frame at 125ms that depended on keyframe 100ms should have been deleted.
1492 CheckExpectedBuffers("70 100K 120K 130K");
1494 // Check the final result: should not include data from the track buffer.
1495 SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
1496 CheckExpectedBuffers("0K 30 60 90 120K 130K");
1499 // Overlap the next keyframe after the end of the track buffer with a new
1501 // old : 10K 40 *70* 100K 125 130K
1502 // new : 0K 30 60 90 120K
1503 // after: 0K 30 60 90 *120K* 130K
1506 // after: 0K 30 60 90 *110K* 130
1507 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer2) {
1508 NewSegmentAppendOneByOne("10K 40 70 100K 125 130K");
1509 CheckExpectedRangesByTimestamp("{ [10,160) }");
1512 SeekToTimestamp(base::TimeDelta::FromMilliseconds(70));
1513 CheckExpectedBuffers("10K 40");
1515 // Overlap with a new segment from 0 to 120ms; 70ms and 100ms go in track
1517 NewSegmentAppendOneByOne("0K 30 60 90 120K");
1518 CheckExpectedRangesByTimestamp("{ [0,160) }");
1520 // Now overlap the keyframe at 120ms.
1521 NewSegmentAppendOneByOne("110K 130");
1523 // Should expect buffers 70ms and 100ms from the track buffer. Then it should
1524 // return the keyframe after the track buffer, which is at 110ms.
1525 CheckExpectedBuffers("70 100K 110K 130");
1528 // Overlap the next keyframe after the end of the track buffer without a
1530 // old : 10K 40 *70* 100K 125 130K
1531 // new : 0K 30 60 90 120K
1532 // after: 0K 30 60 90 *120K* 130K
1534 // new : 50K 80 110 140
1535 // after: 0K 30 50K 80 110 140 * (waiting for keyframe)
1536 // track: 70 100K 120K 130K
1537 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer3) {
1538 NewSegmentAppendOneByOne("10K 40 70 100K 125 130K");
1539 CheckExpectedRangesByTimestamp("{ [10,160) }");
1542 SeekToTimestamp(base::TimeDelta::FromMilliseconds(70));
1543 CheckExpectedBuffers("10K 40");
1545 // Overlap with a new segment from 0 to 120ms; 70ms and 100ms go in track
1547 NewSegmentAppendOneByOne("0K 30 60 90 120K");
1548 CheckExpectedRangesByTimestamp("{ [0,160) }");
1550 // Now overlap the keyframe at 120ms. There's no keyframe after 70ms, so 120ms
1551 // and 130ms go into the track buffer.
1552 NewSegmentAppendOneByOne("50K 80 110 140");
1554 // Should have all the buffers from the track buffer, then stall.
1555 CheckExpectedBuffers("70 100K 120K 130K");
1556 CheckNoNextBuffer();
1558 // Appending a keyframe should fulfill the read.
1559 AppendBuffersOneByOne("150K");
1560 CheckExpectedBuffers("150K");
1561 CheckNoNextBuffer();
1564 // Overlap the next keyframe after the end of the track buffer with a keyframe
1565 // that comes before the end of the track buffer.
1566 // old : 10K 40 *70* 100K 125 130K
1567 // new : 0K 30 60 90 120K
1568 // after: 0K 30 60 90 *120K* 130K
1570 // new : 80K 110 140
1571 // after: 0K 30 60 *80K* 110 140
1573 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer4) {
1574 NewSegmentAppendOneByOne("10K 40 70 100K 125 130K");
1575 CheckExpectedRangesByTimestamp("{ [10,160) }");
1578 SeekToTimestamp(base::TimeDelta::FromMilliseconds(70));
1579 CheckExpectedBuffers("10K 40");
1581 // Overlap with a new segment from 0 to 120ms; 70ms and 100ms go in track
1583 NewSegmentAppendOneByOne("0K 30 60 90 120K");
1584 CheckExpectedRangesByTimestamp("{ [0,160) }");
1586 // Now append a keyframe at 80ms.
1587 NewSegmentAppendOneByOne("80K 110 140");
1589 CheckExpectedBuffers("70 80K 110 140");
1590 CheckNoNextBuffer();
1593 // Overlap the next keyframe after the end of the track buffer with a keyframe
1594 // that comes before the end of the track buffer, when the selected stream was
1595 // waiting for the next keyframe.
1596 // old : 10K 40 *70* 100K
1597 // new : 0K 30 60 90 120
1598 // after: 0K 30 60 90 120 * (waiting for keyframe)
1600 // new : 80K 110 140
1601 // after: 0K 30 60 *80K* 110 140
1603 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer5) {
1604 NewSegmentAppendOneByOne("10K 40 70 100K");
1605 CheckExpectedRangesByTimestamp("{ [10,130) }");
1608 SeekToTimestamp(base::TimeDelta::FromMilliseconds(70));
1609 CheckExpectedBuffers("10K 40");
1611 // Overlap with a new segment from 0 to 120ms; 70ms and 100ms go in track
1613 NewSegmentAppendOneByOne("0K 30 60 90 120");
1614 CheckExpectedRangesByTimestamp("{ [0,150) }");
1616 // Now append a keyframe at 80ms. The buffer at 100ms should be deleted from
1617 // the track buffer.
1618 NewSegmentAppendOneByOne("80K 110 140");
1620 CheckExpectedBuffers("70 80K 110 140");
1621 CheckNoNextBuffer();
1624 // Test that appending to a different range while there is data in
1625 // the track buffer doesn't affect the selected range or track buffer state.
1626 // old : 10K 40 *70* 100K 125 130K ... 200K 230
1627 // new : 0K 30 60 90 120K
1628 // after: 0K 30 60 90 *120K* 130K ... 200K 230
1630 // old : 0K 30 60 90 *120K* 130K ... 200K 230
1632 // after: 0K 30 60 90 *120K* 130K ... 200K 230 260K 290
1634 TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer6) {
1635 NewSegmentAppendOneByOne("10K 40 70 100K 125 130K");
1636 NewSegmentAppendOneByOne("200K 230");
1637 CheckExpectedRangesByTimestamp("{ [10,160) [200,260) }");
1640 SeekToTimestamp(base::TimeDelta::FromMilliseconds(70));
1641 CheckExpectedBuffers("10K 40");
1643 // Overlap with a new segment from 0 to 120ms.
1644 NewSegmentAppendOneByOne("0K 30 60 90 120K");
1645 CheckExpectedRangesByTimestamp("{ [0,160) [200,260) }");
1647 // Verify that 70 gets read out of the track buffer.
1648 CheckExpectedBuffers("70");
1650 // Append more data to the unselected range.
1651 NewSegmentAppendOneByOne("260K 290");
1652 CheckExpectedRangesByTimestamp("{ [0,160) [200,320) }");
1654 CheckExpectedBuffers("100K 120K 130K");
1655 CheckNoNextBuffer();
1657 // Check the final result: should not include data from the track buffer.
1658 SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
1659 CheckExpectedBuffers("0K 30 60 90 120K 130K");
1660 CheckNoNextBuffer();
1663 TEST_F(SourceBufferStreamTest, Seek_Keyframe) {
1664 // Append 6 buffers at positions 0 through 5.
1665 NewSegmentAppend(0, 6);
1667 // Seek to beginning.
1669 CheckExpectedBuffers(0, 5, true);
1672 TEST_F(SourceBufferStreamTest, Seek_NonKeyframe) {
1673 // Append 15 buffers at positions 0 through 14.
1674 NewSegmentAppend(0, 15);
1676 // Seek to buffer at position 13.
1679 // Expect seeking back to the nearest keyframe.
1680 CheckExpectedBuffers(10, 14, true);
1682 // Seek to buffer at position 3.
1685 // Expect seeking back to the nearest keyframe.
1686 CheckExpectedBuffers(0, 3, true);
1689 TEST_F(SourceBufferStreamTest, Seek_NotBuffered) {
1690 // Seek to beginning.
1693 // Try to get buffer; nothing's appended.
1694 CheckNoNextBuffer();
1696 // Append 2 buffers at positions 0.
1697 NewSegmentAppend(0, 2);
1699 CheckExpectedBuffers(0, 1);
1701 // Try to get buffer out of range.
1703 CheckNoNextBuffer();
1706 TEST_F(SourceBufferStreamTest, Seek_InBetweenTimestamps) {
1707 // Append 10 buffers at positions 0 through 9.
1708 NewSegmentAppend(0, 10);
1710 base::TimeDelta bump = frame_duration() / 4;
1711 CHECK(bump > base::TimeDelta());
1713 // Seek to buffer a little after position 5.
1714 stream_->Seek(5 * frame_duration() + bump);
1715 CheckExpectedBuffers(5, 5, true);
1717 // Seek to buffer a little before position 5.
1718 stream_->Seek(5 * frame_duration() - bump);
1719 CheckExpectedBuffers(0, 0, true);
1722 // This test will do a complete overlap of an existing range in order to add
1723 // buffers to the track buffers. Then the test does a seek to another part of
1724 // the stream. The SourceBufferStream should clear its internal track buffer in
1725 // response to the Seek().
1726 TEST_F(SourceBufferStreamTest, Seek_After_TrackBuffer_Filled) {
1727 // Append 10 buffers at positions 5 through 14.
1728 NewSegmentAppend(5, 10, &kDataA);
1730 // Seek to buffer at position 5 and get next buffer.
1732 CheckExpectedBuffers(5, 5, &kDataA);
1734 // Do a complete overlap by appending 20 buffers at positions 0 through 19.
1735 NewSegmentAppend(0, 20, &kDataB);
1737 // Check range is correct.
1738 CheckExpectedRanges("{ [0,19) }");
1740 // Seek to beginning; all data should be new.
1742 CheckExpectedBuffers(0, 19, &kDataB);
1744 // Check range continues to be correct.
1745 CheckExpectedRanges("{ [0,19) }");
1748 TEST_F(SourceBufferStreamTest, Seek_StartOfSegment) {
1749 base::TimeDelta bump = frame_duration() / 4;
1750 CHECK(bump > base::TimeDelta());
1752 // Append 5 buffers at position (5 + |bump|) through 9, where the media
1753 // segment begins at position 5.
1755 NewSegmentAppend_OffsetFirstBuffer(5, 5, bump);
1756 scoped_refptr<StreamParserBuffer> buffer;
1758 // GetNextBuffer() should return the next buffer at position (5 + |bump|).
1759 EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kSuccess);
1760 EXPECT_EQ(buffer->GetDecodeTimestamp(), 5 * frame_duration() + bump);
1762 // Check rest of buffers.
1763 CheckExpectedBuffers(6, 9);
1765 // Seek to position 15.
1768 // Append 5 buffers at positions (15 + |bump|) through 19, where the media
1769 // segment begins at 15.
1770 NewSegmentAppend_OffsetFirstBuffer(15, 5, bump);
1772 // GetNextBuffer() should return the next buffer at position (15 + |bump|).
1773 EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kSuccess);
1774 EXPECT_EQ(buffer->GetDecodeTimestamp(), 15 * frame_duration() + bump);
1776 // Check rest of buffers.
1777 CheckExpectedBuffers(16, 19);
1780 TEST_F(SourceBufferStreamTest, Seek_BeforeStartOfSegment) {
1781 // Append 10 buffers at positions 5 through 14.
1782 NewSegmentAppend(5, 10);
1784 // Seek to a time before the first buffer in the range.
1787 // Should return buffers from the beginning of the range.
1788 CheckExpectedBuffers(5, 14);
1791 TEST_F(SourceBufferStreamTest, OldSeekPoint_CompleteOverlap) {
1792 // Append 5 buffers at positions 0 through 4.
1793 NewSegmentAppend(0, 4);
1795 // Append 5 buffers at positions 10 through 14, and seek to the beginning of
1797 NewSegmentAppend(10, 5);
1800 // Now seek to the beginning of the first range.
1803 // Completely overlap the old seek point.
1804 NewSegmentAppend(5, 15);
1806 // The GetNextBuffer() call should respect the 2nd seek point.
1807 CheckExpectedBuffers(0, 0);
1810 TEST_F(SourceBufferStreamTest, OldSeekPoint_CompleteOverlap_Pending) {
1811 // Append 2 buffers at positions 0 through 1.
1812 NewSegmentAppend(0, 2);
1814 // Append 5 buffers at positions 15 through 19 and seek to beginning of the
1816 NewSegmentAppend(15, 5);
1819 // Now seek position 5.
1822 // Completely overlap the old seek point.
1823 NewSegmentAppend(10, 15);
1825 // The seek at position 5 should still be pending.
1826 CheckNoNextBuffer();
1829 TEST_F(SourceBufferStreamTest, OldSeekPoint_MiddleOverlap) {
1830 // Append 2 buffers at positions 0 through 1.
1831 NewSegmentAppend(0, 2);
1833 // Append 15 buffers at positions 5 through 19 and seek to position 15.
1834 NewSegmentAppend(5, 15);
1837 // Now seek to the beginning of the stream.
1840 // Overlap the middle of the range such that there are now three ranges.
1841 NewSegmentAppend(10, 3);
1842 CheckExpectedRanges("{ [0,1) [5,12) [15,19) }");
1844 // The GetNextBuffer() call should respect the 2nd seek point.
1845 CheckExpectedBuffers(0, 0);
1848 TEST_F(SourceBufferStreamTest, OldSeekPoint_MiddleOverlap_Pending) {
1849 // Append 2 buffers at positions 0 through 1.
1850 NewSegmentAppend(0, 2);
1852 // Append 15 buffers at positions 10 through 24 and seek to position 20.
1853 NewSegmentAppend(10, 15);
1856 // Now seek to position 5.
1859 // Overlap the middle of the range such that it is now split into two ranges.
1860 NewSegmentAppend(15, 3);
1861 CheckExpectedRanges("{ [0,1) [10,17) [20,24) }");
1863 // The seek at position 5 should still be pending.
1864 CheckNoNextBuffer();
1867 TEST_F(SourceBufferStreamTest, OldSeekPoint_StartOverlap) {
1868 // Append 2 buffers at positions 0 through 1.
1869 NewSegmentAppend(0, 2);
1871 // Append 15 buffers at positions 5 through 19 and seek to position 15.
1872 NewSegmentAppend(5, 15);
1875 // Now seek to the beginning of the stream.
1878 // Start overlap the old seek point.
1879 NewSegmentAppend(10, 10);
1881 // The GetNextBuffer() call should respect the 2nd seek point.
1882 CheckExpectedBuffers(0, 0);
1885 TEST_F(SourceBufferStreamTest, OldSeekPoint_StartOverlap_Pending) {
1886 // Append 2 buffers at positions 0 through 1.
1887 NewSegmentAppend(0, 2);
1889 // Append 15 buffers at positions 10 through 24 and seek to position 20.
1890 NewSegmentAppend(10, 15);
1893 // Now seek to position 5.
1896 // Start overlap the old seek point.
1897 NewSegmentAppend(15, 10);
1899 // The seek at time 0 should still be pending.
1900 CheckNoNextBuffer();
1903 TEST_F(SourceBufferStreamTest, OldSeekPoint_EndOverlap) {
1904 // Append 5 buffers at positions 0 through 4.
1905 NewSegmentAppend(0, 4);
1907 // Append 15 buffers at positions 10 through 24 and seek to start of range.
1908 NewSegmentAppend(10, 15);
1911 // Now seek to the beginning of the stream.
1914 // End overlap the old seek point.
1915 NewSegmentAppend(5, 10);
1917 // The GetNextBuffer() call should respect the 2nd seek point.
1918 CheckExpectedBuffers(0, 0);
1921 TEST_F(SourceBufferStreamTest, OldSeekPoint_EndOverlap_Pending) {
1922 // Append 2 buffers at positions 0 through 1.
1923 NewSegmentAppend(0, 2);
1925 // Append 15 buffers at positions 15 through 29 and seek to start of range.
1926 NewSegmentAppend(15, 15);
1929 // Now seek to position 5
1932 // End overlap the old seek point.
1933 NewSegmentAppend(10, 10);
1935 // The seek at time 0 should still be pending.
1936 CheckNoNextBuffer();
1939 TEST_F(SourceBufferStreamTest, GetNextBuffer_AfterMerges) {
1940 // Append 5 buffers at positions 10 through 14.
1941 NewSegmentAppend(10, 5);
1943 // Seek to buffer at position 12.
1946 // Append 5 buffers at positions 5 through 9.
1947 NewSegmentAppend(5, 5);
1949 // Make sure ranges are merged.
1950 CheckExpectedRanges("{ [5,14) }");
1952 // Make sure the next buffer is correct.
1953 CheckExpectedBuffers(10, 10);
1955 // Append 5 buffers at positions 15 through 19.
1956 NewSegmentAppend(15, 5);
1957 CheckExpectedRanges("{ [5,19) }");
1959 // Make sure the remaining next buffers are correct.
1960 CheckExpectedBuffers(11, 14);
1963 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenAppend) {
1964 // Append 4 buffers at positions 0 through 3.
1965 NewSegmentAppend(0, 4);
1967 // Seek to buffer at position 0 and get all buffers.
1969 CheckExpectedBuffers(0, 3);
1971 // Next buffer is at position 4, so should not be able to fulfill request.
1972 CheckNoNextBuffer();
1974 // Append 2 buffers at positions 4 through 5.
1975 AppendBuffers(4, 2);
1976 CheckExpectedBuffers(4, 5);
1979 // This test covers the case where new buffers start-overlap a range whose next
1980 // buffer is not buffered.
1981 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenStartOverlap) {
1982 // Append 10 buffers at positions 0 through 9 and exhaust the buffers.
1983 NewSegmentAppend(0, 10, &kDataA);
1985 CheckExpectedBuffers(0, 9, &kDataA);
1987 // Next buffer is at position 10, so should not be able to fulfill request.
1988 CheckNoNextBuffer();
1990 // Append 6 buffers at positons 5 through 10. This is to test that doing a
1991 // start-overlap successfully fulfills the read at position 10, even though
1992 // position 10 was unbuffered.
1993 NewSegmentAppend(5, 6, &kDataB);
1994 CheckExpectedBuffers(10, 10, &kDataB);
1996 // Then add 5 buffers from positions 11 though 15.
1997 AppendBuffers(11, 5, &kDataB);
1999 // Check the next 4 buffers are correct, which also effectively seeks to
2001 CheckExpectedBuffers(11, 14, &kDataB);
2003 // Replace the next buffer at position 15 with another start overlap.
2004 NewSegmentAppend(15, 2, &kDataA);
2005 CheckExpectedBuffers(15, 16, &kDataA);
2008 // Tests a start overlap that occurs right at the timestamp of the last output
2009 // buffer that was returned by GetNextBuffer(). This test verifies that
2010 // GetNextBuffer() skips to second GOP in the newly appended data instead
2011 // of returning two buffers with the same timestamp.
2012 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenStartOverlap2) {
2013 NewSegmentAppend("0K 30 60 90 120");
2016 CheckExpectedBuffers("0K 30 60 90 120");
2017 CheckNoNextBuffer();
2019 // Append a keyframe with the same timestamp as the last buffer output.
2020 NewSegmentAppend("120K");
2021 CheckNoNextBuffer();
2023 // Append the rest of the segment and make sure that buffers are returned
2024 // from the first GOP after 120.
2025 AppendBuffers("150 180 210K 240");
2026 CheckExpectedBuffers("210K 240");
2028 // Seek to the beginning and verify the contents of the source buffer.
2030 CheckExpectedBuffers("0K 30 60 90 120K 150 180 210K 240");
2031 CheckNoNextBuffer();
2034 // This test covers the case where new buffers completely overlap a range
2035 // whose next buffer is not buffered.
2036 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenCompleteOverlap) {
2037 // Append 5 buffers at positions 10 through 14 and exhaust the buffers.
2038 NewSegmentAppend(10, 5, &kDataA);
2040 CheckExpectedBuffers(10, 14, &kDataA);
2042 // Next buffer is at position 15, so should not be able to fulfill request.
2043 CheckNoNextBuffer();
2045 // Do a complete overlap and test that this successfully fulfills the read
2047 NewSegmentAppend(5, 11, &kDataB);
2048 CheckExpectedBuffers(15, 15, &kDataB);
2050 // Then add 5 buffers from positions 16 though 20.
2051 AppendBuffers(16, 5, &kDataB);
2053 // Check the next 4 buffers are correct, which also effectively seeks to
2055 CheckExpectedBuffers(16, 19, &kDataB);
2057 // Do a complete overlap and replace the buffer at position 20.
2058 NewSegmentAppend(0, 21, &kDataA);
2059 CheckExpectedBuffers(20, 20, &kDataA);
2062 // This test covers the case where a range is stalled waiting for its next
2063 // buffer, then an end-overlap causes the end of the range to be deleted.
2064 TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenEndOverlap) {
2065 // Append 5 buffers at positions 10 through 14 and exhaust the buffers.
2066 NewSegmentAppend(10, 5, &kDataA);
2068 CheckExpectedBuffers(10, 14, &kDataA);
2069 CheckExpectedRanges("{ [10,14) }");
2071 // Next buffer is at position 15, so should not be able to fulfill request.
2072 CheckNoNextBuffer();
2074 // Do an end overlap that causes the latter half of the range to be deleted.
2075 NewSegmentAppend(5, 6, &kDataB);
2076 CheckNoNextBuffer();
2077 CheckExpectedRanges("{ [5,10) }");
2079 // Fill in the gap. Getting the next buffer should still stall at position 15.
2080 for (int i = 11; i <= 14; i++) {
2081 AppendBuffers(i, 1, &kDataB);
2082 CheckNoNextBuffer();
2085 // Append the buffer at position 15 and check to make sure all is correct.
2086 AppendBuffers(15, 1);
2087 CheckExpectedBuffers(15, 15);
2088 CheckExpectedRanges("{ [5,15) }");
2091 // This test is testing the "next buffer" logic after a complete overlap. In
2092 // this scenario, when the track buffer is exhausted, there is no buffered data
2093 // to fulfill the request. The SourceBufferStream should be able to fulfill the
2094 // request when the data is later appended, and should not lose track of the
2095 // "next buffer" position.
2096 TEST_F(SourceBufferStreamTest, GetNextBuffer_Overlap_Selected_Complete) {
2097 // Append 5 buffers at positions 5 through 9.
2098 NewSegmentAppend(5, 5, &kDataA);
2100 // Seek to buffer at position 5 and get next buffer.
2102 CheckExpectedBuffers(5, 5, &kDataA);
2104 // Replace existing data with new data.
2105 NewSegmentAppend(5, 5, &kDataB);
2107 // Expect old data up until next keyframe in new data.
2108 CheckExpectedBuffers(6, 9, &kDataA);
2110 // Next buffer is at position 10, so should not be able to fulfill the
2112 CheckNoNextBuffer();
2114 // Now add 5 new buffers at positions 10 through 14.
2115 AppendBuffers(10, 5, &kDataB);
2116 CheckExpectedBuffers(10, 14, &kDataB);
2119 TEST_F(SourceBufferStreamTest, PresentationTimestampIndependence) {
2120 // Append 20 buffers at position 0.
2121 NewSegmentAppend(0, 20);
2124 int last_keyframe_idx = -1;
2125 base::TimeDelta last_keyframe_presentation_timestamp;
2126 base::TimeDelta last_p_frame_presentation_timestamp;
2128 // Check for IBB...BBP pattern.
2129 for (int i = 0; i < 20; i++) {
2130 scoped_refptr<StreamParserBuffer> buffer;
2131 ASSERT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kSuccess);
2133 if (buffer->IsKeyframe()) {
2134 EXPECT_EQ(buffer->timestamp(), buffer->GetDecodeTimestamp());
2135 last_keyframe_idx = i;
2136 last_keyframe_presentation_timestamp = buffer->timestamp();
2137 } else if (i == last_keyframe_idx + 1) {
2138 ASSERT_NE(last_keyframe_idx, -1);
2139 last_p_frame_presentation_timestamp = buffer->timestamp();
2140 EXPECT_LT(last_keyframe_presentation_timestamp,
2141 last_p_frame_presentation_timestamp);
2143 EXPECT_GT(buffer->timestamp(), last_keyframe_presentation_timestamp);
2144 EXPECT_LT(buffer->timestamp(), last_p_frame_presentation_timestamp);
2145 EXPECT_LT(buffer->timestamp(), buffer->GetDecodeTimestamp());
2150 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFront) {
2151 // Set memory limit to 20 buffers.
2154 // Append 20 buffers at positions 0 through 19.
2155 NewSegmentAppend(0, 1, &kDataA);
2156 for (int i = 1; i < 20; i++)
2157 AppendBuffers(i, 1, &kDataA);
2159 // None of the buffers should trigger garbage collection, so all data should
2160 // be there as expected.
2161 CheckExpectedRanges("{ [0,19) }");
2163 CheckExpectedBuffers(0, 19, &kDataA);
2165 // Seek to the middle of the stream.
2168 // Append 5 buffers to the end of the stream.
2169 AppendBuffers(20, 5, &kDataA);
2171 // GC should have deleted the first 5 buffers.
2172 CheckExpectedRanges("{ [5,24) }");
2173 CheckExpectedBuffers(10, 24, &kDataA);
2175 CheckExpectedBuffers(5, 9, &kDataA);
2178 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFrontGOPsAtATime) {
2179 // Set memory limit to 20 buffers.
2182 // Append 20 buffers at positions 0 through 19.
2183 NewSegmentAppend(0, 20, &kDataA);
2185 // Seek to position 10.
2188 // Add one buffer to put the memory over the cap.
2189 AppendBuffers(20, 1, &kDataA);
2191 // GC should have deleted the first 5 buffers so that the range still begins
2193 CheckExpectedRanges("{ [5,20) }");
2194 CheckExpectedBuffers(10, 20, &kDataA);
2196 CheckExpectedBuffers(5, 9, &kDataA);
2199 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteBack) {
2200 // Set memory limit to 5 buffers.
2203 // Seek to position 0.
2206 // Append 20 buffers at positions 0 through 19.
2207 NewSegmentAppend(0, 20, &kDataA);
2209 // Should leave the first 5 buffers from 0 to 4 and the last GOP appended.
2210 CheckExpectedRanges("{ [0,4) [15,19) }");
2211 CheckExpectedBuffers(0, 4, &kDataA);
2214 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFrontAndBack) {
2215 // Set memory limit to 3 buffers.
2218 // Seek to position 15.
2221 // Append 40 buffers at positions 0 through 39.
2222 NewSegmentAppend(0, 40, &kDataA);
2224 // Should leave the GOP containing the seek position and the last GOP
2226 CheckExpectedRanges("{ [15,19) [35,39) }");
2227 CheckExpectedBuffers(15, 19, &kDataA);
2228 CheckNoNextBuffer();
2231 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteSeveralRanges) {
2232 // Append 5 buffers at positions 0 through 4.
2233 NewSegmentAppend(0, 5);
2235 // Append 5 buffers at positions 10 through 14.
2236 NewSegmentAppend(10, 5);
2238 // Append 5 buffers at positions 20 through 24.
2239 NewSegmentAppend(20, 5);
2241 // Append 5 buffers at positions 30 through 34.
2242 NewSegmentAppend(30, 5);
2244 CheckExpectedRanges("{ [0,4) [10,14) [20,24) [30,34) }");
2246 // Seek to position 21.
2248 CheckExpectedBuffers(20, 20);
2250 // Set memory limit to 1 buffer.
2253 // Append 5 buffers at positions 40 through 44. This will trigger GC.
2254 NewSegmentAppend(40, 5);
2256 // Should delete everything except the GOP containing the current buffer and
2257 // the last GOP appended.
2258 CheckExpectedRanges("{ [20,24) [40,44) }");
2259 CheckExpectedBuffers(21, 24);
2260 CheckNoNextBuffer();
2262 // Continue appending into the last range to make sure it didn't break.
2263 AppendBuffers(45, 10);
2264 // Should only save last GOP appended.
2265 CheckExpectedRanges("{ [20,24) [50,54) }");
2267 // Make sure appending before and after the ranges didn't somehow break.
2268 SetMemoryLimit(100);
2269 NewSegmentAppend(0, 10);
2270 CheckExpectedRanges("{ [0,9) [20,24) [50,54) }");
2272 CheckExpectedBuffers(0, 9);
2274 NewSegmentAppend(90, 10);
2275 CheckExpectedRanges("{ [0,9) [20,24) [50,54) [90,99) }");
2277 CheckExpectedBuffers(50, 54);
2278 CheckNoNextBuffer();
2280 CheckExpectedBuffers(90, 99);
2281 CheckNoNextBuffer();
2284 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteAfterLastAppend) {
2285 // Set memory limit to 10 buffers.
2288 // Append 1 GOP starting at 310ms, 30ms apart.
2289 NewSegmentAppend("310K 340 370");
2291 // Append 2 GOPs starting at 490ms, 30ms apart.
2292 NewSegmentAppend("490K 520 550 580K 610 640");
2294 CheckExpectedRangesByTimestamp("{ [310,400) [490,670) }");
2296 // Seek to the GOP at 580ms.
2297 SeekToTimestamp(base::TimeDelta::FromMilliseconds(580));
2299 // Append 2 GOPs before the existing ranges.
2300 // So the ranges before GC are "{ [100,280) [310,400) [490,670) }".
2301 NewSegmentAppend("100K 130 160 190K 220 250K");
2303 // Should save the newly appended GOPs.
2304 CheckExpectedRangesByTimestamp("{ [100,280) [580,670) }");
2307 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteAfterLastAppendMerged) {
2308 // Set memory limit to 10 buffers.
2311 // Append 3 GOPs starting at 400ms, 30ms apart.
2312 NewSegmentAppend("400K 430 460 490K 520 550 580K 610 640");
2314 // Seek to the GOP at 580ms.
2315 SeekToTimestamp(base::TimeDelta::FromMilliseconds(580));
2317 // Append 2 GOPs starting at 220ms, and they will be merged with the existing
2318 // range. So the range before GC is "{ [220,670) }".
2319 NewSegmentAppend("220K 250 280 310K 340 370");
2321 // Should save the newly appended GOPs.
2322 CheckExpectedRangesByTimestamp("{ [220,400) [580,670) }");
2325 TEST_F(SourceBufferStreamTest, GarbageCollection_NoSeek) {
2326 // Set memory limit to 20 buffers.
2329 // Append 25 buffers at positions 0 through 24.
2330 NewSegmentAppend(0, 25, &kDataA);
2332 // GC deletes the first 5 buffers to keep the memory limit within cap.
2333 CheckExpectedRanges("{ [5,24) }");
2334 CheckNoNextBuffer();
2336 CheckExpectedBuffers(5, 24, &kDataA);
2339 TEST_F(SourceBufferStreamTest, GarbageCollection_PendingSeek) {
2340 // Append 10 buffers at positions 0 through 9.
2341 NewSegmentAppend(0, 10, &kDataA);
2343 // Append 5 buffers at positions 25 through 29.
2344 NewSegmentAppend(25, 5, &kDataA);
2346 // Seek to position 15.
2348 CheckNoNextBuffer();
2350 CheckExpectedRanges("{ [0,9) [25,29) }");
2352 // Set memory limit to 5 buffers.
2355 // Append 5 buffers as positions 30 to 34 to trigger GC.
2356 AppendBuffers(30, 5, &kDataA);
2358 // The current algorithm will delete from the beginning until the memory is
2360 CheckExpectedRanges("{ [30,34) }");
2362 // Expand memory limit again so that GC won't be triggered.
2363 SetMemoryLimit(100);
2365 // Append data to fulfill seek.
2366 NewSegmentAppend(15, 5, &kDataA);
2368 // Check to make sure all is well.
2369 CheckExpectedRanges("{ [15,19) [30,34) }");
2370 CheckExpectedBuffers(15, 19, &kDataA);
2372 CheckExpectedBuffers(30, 34, &kDataA);
2375 TEST_F(SourceBufferStreamTest, GarbageCollection_NeedsMoreData) {
2376 // Set memory limit to 15 buffers.
2379 // Append 10 buffers at positions 0 through 9.
2380 NewSegmentAppend(0, 10, &kDataA);
2382 // Advance next buffer position to 10.
2384 CheckExpectedBuffers(0, 9, &kDataA);
2385 CheckNoNextBuffer();
2387 // Append 20 buffers at positions 15 through 34.
2388 NewSegmentAppend(15, 20, &kDataA);
2390 // GC should have saved the keyframe before the current seek position and the
2391 // data closest to the current seek position. It will also save the last GOP
2393 CheckExpectedRanges("{ [5,9) [15,19) [30,34) }");
2395 // Now fulfill the seek at position 10. This will make GC delete the data
2396 // before position 10 to keep it within cap.
2397 NewSegmentAppend(10, 5, &kDataA);
2398 CheckExpectedRanges("{ [10,19) [30,34) }");
2399 CheckExpectedBuffers(10, 19, &kDataA);
2402 TEST_F(SourceBufferStreamTest, GarbageCollection_TrackBuffer) {
2403 // Set memory limit to 3 buffers.
2406 // Seek to position 15.
2409 // Append 18 buffers at positions 0 through 17.
2410 NewSegmentAppend(0, 18, &kDataA);
2412 // Should leave GOP containing seek position.
2413 CheckExpectedRanges("{ [15,17) }");
2415 // Seek ahead to position 16.
2416 CheckExpectedBuffers(15, 15, &kDataA);
2418 // Completely overlap the existing buffers.
2419 NewSegmentAppend(0, 20, &kDataB);
2421 // Because buffers 16 and 17 are not keyframes, they are moved to the track
2422 // buffer upon overlap. The source buffer (i.e. not the track buffer) is now
2423 // waiting for the next keyframe.
2424 CheckExpectedRanges("{ [15,19) }");
2425 CheckExpectedBuffers(16, 17, &kDataA);
2426 CheckNoNextBuffer();
2428 // Now add a keyframe at position 20.
2429 AppendBuffers(20, 5, &kDataB);
2431 // Should garbage collect such that there are 5 frames remaining, starting at
2433 CheckExpectedRanges("{ [20,24) }");
2434 CheckExpectedBuffers(20, 24, &kDataB);
2435 CheckNoNextBuffer();
2438 // Test saving the last GOP appended when this GOP is the only GOP in its range.
2439 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP) {
2440 // Set memory limit to 3 and make sure the 4-byte GOP is not garbage
2443 NewSegmentAppend("0K 30 60 90");
2444 CheckExpectedRangesByTimestamp("{ [0,120) }");
2446 // Make sure you can continue appending data to this GOP; again, GC should not
2447 // wipe out anything.
2448 AppendBuffers("120");
2449 CheckExpectedRangesByTimestamp("{ [0,150) }");
2451 // Set memory limit to 100 and append a 2nd range after this without
2453 SetMemoryLimit(100);
2454 NewSegmentAppend("200K 230 260 290K 320 350");
2455 CheckExpectedRangesByTimestamp("{ [0,150) [200,380) }");
2458 SeekToTimestamp(base::TimeDelta::FromMilliseconds(290));
2460 // Now set memory limit to 3 and append a GOP in a separate range after the
2461 // selected range. Because it is after 290ms, this tests that the GOP is saved
2462 // when deleting from the back.
2464 NewSegmentAppend("500K 530 560 590");
2466 // Should save GOP with 290ms and last GOP appended.
2467 CheckExpectedRangesByTimestamp("{ [290,380) [500,620) }");
2469 // Continue appending to this GOP after GC.
2470 AppendBuffers("620");
2471 CheckExpectedRangesByTimestamp("{ [290,380) [500,650) }");
2474 // Test saving the last GOP appended when this GOP is in the middle of a
2475 // non-selected range.
2476 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Middle) {
2477 // Append 3 GOPs starting at 0ms, 30ms apart.
2478 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240");
2479 CheckExpectedRangesByTimestamp("{ [0,270) }");
2481 // Now set the memory limit to 1 and overlap the middle of the range with a
2484 NewSegmentAppend("80K 110 140");
2486 // This whole GOP should be saved, and should be able to continue appending
2488 CheckExpectedRangesByTimestamp("{ [80,170) }");
2489 AppendBuffers("170");
2490 CheckExpectedRangesByTimestamp("{ [80,200) }");
2492 // Set memory limit to 100 and append a 2nd range after this without
2494 SetMemoryLimit(100);
2495 NewSegmentAppend("400K 430 460 490K 520 550 580K 610 640");
2496 CheckExpectedRangesByTimestamp("{ [80,200) [400,670) }");
2498 // Seek to 80ms to make the first range the selected range.
2499 SeekToTimestamp(base::TimeDelta::FromMilliseconds(80));
2501 // Now set memory limit to 3 and append a GOP in the middle of the second
2502 // range. Because it is after the selected range, this tests that the GOP is
2503 // saved when deleting from the back.
2505 NewSegmentAppend("500K 530 560 590");
2507 // Should save the GOP containing the seek point and GOP that was last
2509 CheckExpectedRangesByTimestamp("{ [80,200) [500,620) }");
2511 // Continue appending to this GOP after GC.
2512 AppendBuffers("620");
2513 CheckExpectedRangesByTimestamp("{ [80,200) [500,650) }");
2516 // Test saving the last GOP appended when the GOP containing the next buffer is
2517 // adjacent to the last GOP appended.
2518 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected1) {
2519 // Append 3 GOPs at 0ms, 90ms, and 180ms.
2520 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240");
2521 CheckExpectedRangesByTimestamp("{ [0,270) }");
2523 // Seek to the GOP at 90ms.
2524 SeekToTimestamp(base::TimeDelta::FromMilliseconds(90));
2526 // Set the memory limit to 1, then overlap the GOP at 0.
2528 NewSegmentAppend("0K 30 60");
2530 // Should save the GOP at 0ms and 90ms.
2531 CheckExpectedRangesByTimestamp("{ [0,180) }");
2533 // Seek to 0 and check all buffers.
2534 SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
2535 CheckExpectedBuffers("0K 30 60 90K 120 150");
2536 CheckNoNextBuffer();
2538 // Now seek back to 90ms and append a GOP at 180ms.
2539 SeekToTimestamp(base::TimeDelta::FromMilliseconds(90));
2540 NewSegmentAppend("180K 210 240");
2542 // Should save the GOP at 90ms and the GOP at 180ms.
2543 CheckExpectedRangesByTimestamp("{ [90,270) }");
2544 CheckExpectedBuffers("90K 120 150 180K 210 240");
2545 CheckNoNextBuffer();
2548 // Test saving the last GOP appended when it is at the beginning or end of the
2549 // selected range. This tests when the last GOP appended is before or after the
2550 // GOP containing the next buffer, but not directly adjacent to this GOP.
2551 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected2) {
2552 // Append 4 GOPs starting at positions 0ms, 90ms, 180ms, 270ms.
2553 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330");
2554 CheckExpectedRangesByTimestamp("{ [0,360) }");
2556 // Seek to the last GOP at 270ms.
2557 SeekToTimestamp(base::TimeDelta::FromMilliseconds(270));
2559 // Set the memory limit to 1, then overlap the GOP at 90ms.
2561 NewSegmentAppend("90K 120 150");
2563 // Should save the GOP at 90ms and the GOP at 270ms.
2564 CheckExpectedRangesByTimestamp("{ [90,180) [270,360) }");
2566 // Set memory limit to 100 and add 3 GOPs to the end of the selected range
2567 // at 360ms, 450ms, and 540ms.
2568 SetMemoryLimit(100);
2569 NewSegmentAppend("360K 390 420 450K 480 510 540K 570 600");
2570 CheckExpectedRangesByTimestamp("{ [90,180) [270,630) }");
2572 // Constrain the memory limit again and overlap the GOP at 450ms to test
2573 // deleting from the back.
2575 NewSegmentAppend("450K 480 510");
2577 // Should save GOP at 270ms and the GOP at 450ms.
2578 CheckExpectedRangesByTimestamp("{ [270,360) [450,540) }");
2581 // Test saving the last GOP appended when it is the same as the GOP containing
2583 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected3) {
2584 // Seek to start of stream.
2585 SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
2587 // Append 3 GOPs starting at 0ms, 90ms, 180ms.
2588 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240");
2589 CheckExpectedRangesByTimestamp("{ [0,270) }");
2591 // Set the memory limit to 1 then begin appending the start of a GOP starting
2594 NewSegmentAppend("0K 30");
2596 // Should save the newly appended GOP, which is also the next GOP that will be
2597 // returned from the seek request.
2598 CheckExpectedRangesByTimestamp("{ [0,60) }");
2600 // Check the buffers in the range.
2601 CheckExpectedBuffers("0K 30");
2602 CheckNoNextBuffer();
2604 // Continue appending to this buffer.
2605 AppendBuffers("60 90");
2607 // Should still save the rest of this GOP and should be able to fulfill the
2609 CheckExpectedRangesByTimestamp("{ [0,120) }");
2610 CheckExpectedBuffers("60 90");
2611 CheckNoNextBuffer();
2614 // Currently disabled because of bug: crbug.com/140875.
2615 TEST_F(SourceBufferStreamTest, DISABLED_GarbageCollection_WaitingForKeyframe) {
2616 // Set memory limit to 10 buffers.
2619 // Append 5 buffers at positions 10 through 14 and exhaust the buffers.
2620 NewSegmentAppend(10, 5, &kDataA);
2622 CheckExpectedBuffers(10, 14, &kDataA);
2623 CheckExpectedRanges("{ [10,14) }");
2625 // We are now stalled at position 15.
2626 CheckNoNextBuffer();
2628 // Do an end overlap that causes the latter half of the range to be deleted.
2629 NewSegmentAppend(5, 6, &kDataA);
2630 CheckNoNextBuffer();
2631 CheckExpectedRanges("{ [5,10) }");
2633 // Append buffers from position 20 to 29. This should trigger GC.
2634 NewSegmentAppend(20, 10, &kDataA);
2636 // GC should keep the keyframe before the seek position 15, and the next 9
2637 // buffers closest to the seek position.
2638 CheckNoNextBuffer();
2639 CheckExpectedRanges("{ [10,10) [20,28) }");
2641 // Fulfill the seek by appending one buffer at 15.
2642 NewSegmentAppend(15, 1, &kDataA);
2643 CheckExpectedBuffers(15, 15, &kDataA);
2644 CheckExpectedRanges("{ [15,15) [20,28) }");
2647 // Test the performance of garbage collection.
2648 TEST_F(SourceBufferStreamTest, GarbageCollection_Performance) {
2649 // Force |keyframes_per_second_| to be equal to kDefaultFramesPerSecond.
2650 SetStreamInfo(kDefaultFramesPerSecond, kDefaultFramesPerSecond);
2652 const int kBuffersToKeep = 1000;
2653 SetMemoryLimit(kBuffersToKeep);
2655 int buffers_appended = 0;
2657 NewSegmentAppend(0, kBuffersToKeep);
2658 buffers_appended += kBuffersToKeep;
2660 const int kBuffersToAppend = 1000;
2661 const int kGarbageCollections = 3;
2662 for (int i = 0; i < kGarbageCollections; ++i) {
2663 AppendBuffers(buffers_appended, kBuffersToAppend);
2664 buffers_appended += kBuffersToAppend;
2668 TEST_F(SourceBufferStreamTest, GetRemovalRange_BytesToFree) {
2669 // Append 2 GOPs starting at 300ms, 30ms apart.
2670 NewSegmentAppend("300K 330 360 390K 420 450");
2672 // Append 2 GOPs starting at 600ms, 30ms apart.
2673 NewSegmentAppend("600K 630 660 690K 720 750");
2675 // Append 2 GOPs starting at 900ms, 30ms apart.
2676 NewSegmentAppend("900K 930 960 990K 1020 1050");
2678 CheckExpectedRangesByTimestamp("{ [300,480) [600,780) [900,1080) }");
2680 int remove_range_end = -1;
2681 int bytes_removed = -1;
2684 bytes_removed = GetRemovalRangeInMs(300, 1080, 0, &remove_range_end);
2685 EXPECT_EQ(-1, remove_range_end);
2686 EXPECT_EQ(0, bytes_removed);
2688 // Smaller than the size of GOP.
2689 bytes_removed = GetRemovalRangeInMs(300, 1080, 1, &remove_range_end);
2690 EXPECT_EQ(390, remove_range_end);
2691 // Remove as the size of GOP.
2692 EXPECT_EQ(3, bytes_removed);
2694 // The same size with a GOP.
2695 bytes_removed = GetRemovalRangeInMs(300, 1080, 3, &remove_range_end);
2696 EXPECT_EQ(390, remove_range_end);
2697 EXPECT_EQ(3, bytes_removed);
2699 // The same size with a range.
2700 bytes_removed = GetRemovalRangeInMs(300, 1080, 6, &remove_range_end);
2701 EXPECT_EQ(480, remove_range_end);
2702 EXPECT_EQ(6, bytes_removed);
2704 // A frame larger than a range.
2705 bytes_removed = GetRemovalRangeInMs(300, 1080, 7, &remove_range_end);
2706 EXPECT_EQ(690, remove_range_end);
2707 EXPECT_EQ(9, bytes_removed);
2709 // The same size with two ranges.
2710 bytes_removed = GetRemovalRangeInMs(300, 1080, 12, &remove_range_end);
2711 EXPECT_EQ(780, remove_range_end);
2712 EXPECT_EQ(12, bytes_removed);
2714 // Larger than two ranges.
2715 bytes_removed = GetRemovalRangeInMs(300, 1080, 14, &remove_range_end);
2716 EXPECT_EQ(990, remove_range_end);
2717 EXPECT_EQ(15, bytes_removed);
2719 // The same size with the whole ranges.
2720 bytes_removed = GetRemovalRangeInMs(300, 1080, 18, &remove_range_end);
2721 EXPECT_EQ(1080, remove_range_end);
2722 EXPECT_EQ(18, bytes_removed);
2724 // Larger than the whole ranges.
2725 bytes_removed = GetRemovalRangeInMs(300, 1080, 20, &remove_range_end);
2726 EXPECT_EQ(1080, remove_range_end);
2727 EXPECT_EQ(18, bytes_removed);
2730 TEST_F(SourceBufferStreamTest, GetRemovalRange_Range) {
2731 // Append 2 GOPs starting at 300ms, 30ms apart.
2732 NewSegmentAppend("300K 330 360 390K 420 450");
2734 // Append 2 GOPs starting at 600ms, 30ms apart.
2735 NewSegmentAppend("600K 630 660 690K 720 750");
2737 // Append 2 GOPs starting at 900ms, 30ms apart.
2738 NewSegmentAppend("900K 930 960 990K 1020 1050");
2740 CheckExpectedRangesByTimestamp("{ [300,480) [600,780) [900,1080) }");
2742 int remove_range_end = -1;
2743 int bytes_removed = -1;
2745 // Within a GOP and no keyframe.
2746 bytes_removed = GetRemovalRangeInMs(630, 660, 20, &remove_range_end);
2747 EXPECT_EQ(-1, remove_range_end);
2748 EXPECT_EQ(0, bytes_removed);
2750 // Across a GOP and no keyframe.
2751 bytes_removed = GetRemovalRangeInMs(630, 750, 20, &remove_range_end);
2752 EXPECT_EQ(-1, remove_range_end);
2753 EXPECT_EQ(0, bytes_removed);
2755 // The same size with a range.
2756 bytes_removed = GetRemovalRangeInMs(600, 780, 20, &remove_range_end);
2757 EXPECT_EQ(780, remove_range_end);
2758 EXPECT_EQ(6, bytes_removed);
2760 // One frame larger than a range.
2761 bytes_removed = GetRemovalRangeInMs(570, 810, 20, &remove_range_end);
2762 EXPECT_EQ(780, remove_range_end);
2763 EXPECT_EQ(6, bytes_removed);
2765 // Facing the other ranges.
2766 bytes_removed = GetRemovalRangeInMs(480, 900, 20, &remove_range_end);
2767 EXPECT_EQ(780, remove_range_end);
2768 EXPECT_EQ(6, bytes_removed);
2770 // In the midle of the other ranges, but not including any GOP.
2771 bytes_removed = GetRemovalRangeInMs(420, 960, 20, &remove_range_end);
2772 EXPECT_EQ(780, remove_range_end);
2773 EXPECT_EQ(6, bytes_removed);
2775 // In the middle of the other ranges.
2776 bytes_removed = GetRemovalRangeInMs(390, 990, 20, &remove_range_end);
2777 EXPECT_EQ(990, remove_range_end);
2778 EXPECT_EQ(12, bytes_removed);
2780 // A frame smaller than the whole ranges.
2781 bytes_removed = GetRemovalRangeInMs(330, 1050, 20, &remove_range_end);
2782 EXPECT_EQ(990, remove_range_end);
2783 EXPECT_EQ(12, bytes_removed);
2785 // The same with the whole ranges.
2786 bytes_removed = GetRemovalRangeInMs(300, 1080, 20, &remove_range_end);
2787 EXPECT_EQ(1080, remove_range_end);
2788 EXPECT_EQ(18, bytes_removed);
2790 // Larger than the whole ranges.
2791 bytes_removed = GetRemovalRangeInMs(270, 1110, 20, &remove_range_end);
2792 EXPECT_EQ(1080, remove_range_end);
2793 EXPECT_EQ(18, bytes_removed);
2796 TEST_F(SourceBufferStreamTest, ConfigChange_Basic) {
2797 VideoDecoderConfig new_config = TestVideoConfig::Large();
2798 ASSERT_FALSE(new_config.Matches(video_config_));
2801 CheckVideoConfig(video_config_);
2803 // Append 5 buffers at positions 0 through 4
2804 NewSegmentAppend(0, 5, &kDataA);
2806 CheckVideoConfig(video_config_);
2808 // Signal a config change.
2809 stream_->UpdateVideoConfig(new_config);
2811 // Make sure updating the config doesn't change anything since new_config
2812 // should not be associated with the buffer GetNextBuffer() will return.
2813 CheckVideoConfig(video_config_);
2815 // Append 5 buffers at positions 5 through 9.
2816 NewSegmentAppend(5, 5, &kDataB);
2818 // Consume the buffers associated with the initial config.
2819 scoped_refptr<StreamParserBuffer> buffer;
2820 for (int i = 0; i < 5; i++) {
2821 EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kSuccess);
2822 CheckVideoConfig(video_config_);
2825 // Verify the next attempt to get a buffer will signal that a config change
2827 EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kConfigChange);
2829 // Verify that the new config is now returned.
2830 CheckVideoConfig(new_config);
2832 // Consume the remaining buffers associated with the new config.
2833 for (int i = 0; i < 5; i++) {
2834 CheckVideoConfig(new_config);
2835 EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kSuccess);
2839 TEST_F(SourceBufferStreamTest, ConfigChange_Seek) {
2840 scoped_refptr<StreamParserBuffer> buffer;
2841 VideoDecoderConfig new_config = TestVideoConfig::Large();
2844 NewSegmentAppend(0, 5, &kDataA);
2845 stream_->UpdateVideoConfig(new_config);
2846 NewSegmentAppend(5, 5, &kDataB);
2848 // Seek to the start of the buffers with the new config and make sure a
2849 // config change is signalled.
2850 CheckVideoConfig(video_config_);
2852 CheckVideoConfig(video_config_);
2853 EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kConfigChange);
2854 CheckVideoConfig(new_config);
2855 CheckExpectedBuffers(5, 9, &kDataB);
2858 // Seek to the start which has a different config. Don't fetch any buffers and
2859 // seek back to buffers with the current config. Make sure a config change
2860 // isn't signalled in this case.
2861 CheckVideoConfig(new_config);
2864 CheckExpectedBuffers(5, 9, &kDataB);
2867 // Seek to the start and make sure a config change is signalled.
2868 CheckVideoConfig(new_config);
2870 CheckVideoConfig(new_config);
2871 EXPECT_EQ(stream_->GetNextBuffer(&buffer), SourceBufferStream::kConfigChange);
2872 CheckVideoConfig(video_config_);
2873 CheckExpectedBuffers(0, 4, &kDataA);
2876 TEST_F(SourceBufferStreamTest, SetExplicitDuration) {
2877 // Append 2 buffers at positions 5 through 6.
2878 NewSegmentAppend(5, 2);
2880 // Append 2 buffers at positions 10 through 11.
2881 NewSegmentAppend(10, 2);
2883 // Append 2 buffers at positions 15 through 16.
2884 NewSegmentAppend(15, 2);
2886 // Check expected ranges.
2887 CheckExpectedRanges("{ [5,6) [10,11) [15,16) }");
2889 // Set duration to be between buffers 6 and 10.
2890 stream_->OnSetDuration(frame_duration() * 8);
2892 // Should truncate the data after 6.
2893 CheckExpectedRanges("{ [5,6) }");
2895 // Adding data past the previous duration should still work.
2896 NewSegmentAppend(0, 20);
2897 CheckExpectedRanges("{ [0,19) }");
2900 TEST_F(SourceBufferStreamTest, SetExplicitDuration_EdgeCase) {
2901 // Append 10 buffers at positions 10 through 19.
2902 NewSegmentAppend(10, 10);
2904 // Append 5 buffers at positions 25 through 29.
2905 NewSegmentAppend(25, 5);
2907 // Check expected ranges.
2908 CheckExpectedRanges("{ [10,19) [25,29) }");
2910 // Set duration to be right before buffer 25.
2911 stream_->OnSetDuration(frame_duration() * 25);
2913 // Should truncate the last range.
2914 CheckExpectedRanges("{ [10,19) }");
2917 TEST_F(SourceBufferStreamTest, SetExplicitDuration_DeletePartialRange) {
2918 // Append 5 buffers at positions 0 through 4.
2919 NewSegmentAppend(0, 5);
2921 // Append 10 buffers at positions 10 through 19.
2922 NewSegmentAppend(10, 10);
2924 // Append 5 buffers at positions 25 through 29.
2925 NewSegmentAppend(25, 5);
2927 // Check expected ranges.
2928 CheckExpectedRanges("{ [0,4) [10,19) [25,29) }");
2930 // Set duration to be between buffers 13 and 14.
2931 stream_->OnSetDuration(frame_duration() * 14);
2933 // Should truncate the data after 13.
2934 CheckExpectedRanges("{ [0,4) [10,13) }");
2937 TEST_F(SourceBufferStreamTest, SetExplicitDuration_DeleteSelectedRange) {
2938 // Append 2 buffers at positions 5 through 6.
2939 NewSegmentAppend(5, 2);
2941 // Append 2 buffers at positions 10 through 11.
2942 NewSegmentAppend(10, 2);
2944 // Append 2 buffers at positions 15 through 16.
2945 NewSegmentAppend(15, 2);
2947 // Check expected ranges.
2948 CheckExpectedRanges("{ [5,6) [10,11) [15,16) }");
2953 // Set duration to be after position 3.
2954 stream_->OnSetDuration(frame_duration() * 4);
2956 // Expect everything to be deleted, and should not have next buffer anymore.
2957 CheckNoNextBuffer();
2958 CheckExpectedRanges("{ }");
2960 // Appending data at position 10 should not fulfill the seek.
2961 // (If the duration is set to be something smaller than the current seek
2962 // point, then the seek point is reset and the SourceBufferStream waits
2963 // for a new seek request. Therefore even if the data is re-appended, it
2964 // should not fulfill the old seek.)
2965 NewSegmentAppend(0, 15);
2966 CheckNoNextBuffer();
2967 CheckExpectedRanges("{ [0,14) }");
2970 TEST_F(SourceBufferStreamTest, SetExplicitDuration_DeletePartialSelectedRange) {
2971 // Append 5 buffers at positions 0 through 4.
2972 NewSegmentAppend(0, 5);
2974 // Append 20 buffers at positions 10 through 29.
2975 NewSegmentAppend(10, 20);
2977 // Check expected ranges.
2978 CheckExpectedRanges("{ [0,4) [10,29) }");
2980 // Seek to position 10.
2983 // Set duration to be between buffers 24 and 25.
2984 stream_->OnSetDuration(frame_duration() * 25);
2986 // Should truncate the data after 24.
2987 CheckExpectedRanges("{ [0,4) [10,24) }");
2989 // The seek position should not be lost.
2990 CheckExpectedBuffers(10, 10);
2992 // Now set the duration immediately after buffer 10.
2993 stream_->OnSetDuration(frame_duration() * 11);
2995 // Seek position should be reset.
2996 CheckNoNextBuffer();
2997 CheckExpectedRanges("{ [0,4) [10,10) }");
3000 // Test the case where duration is set while the stream parser buffers
3001 // already start passing the data to decoding pipeline. Selected range,
3002 // when invalidated by getting truncated, should be updated to NULL
3003 // accordingly so that successive append operations keep working.
3004 TEST_F(SourceBufferStreamTest, SetExplicitDuration_UpdateSelectedRange) {
3005 // Seek to start of stream.
3006 SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
3008 NewSegmentAppend("0K 30 60 90");
3010 // Read out the first few buffers.
3011 CheckExpectedBuffers("0K 30");
3013 // Set duration to be right before buffer 1.
3014 stream_->OnSetDuration(base::TimeDelta::FromMilliseconds(60));
3016 // Verify that there is no next buffer.
3017 CheckNoNextBuffer();
3019 // We should be able to append new buffers at this point.
3020 NewSegmentAppend("120K 150");
3022 CheckExpectedRangesByTimestamp("{ [0,60) [120,180) }");
3025 TEST_F(SourceBufferStreamTest,
3026 SetExplicitDuration_AfterSegmentTimestampAndBeforeFirstBufferTimestamp) {
3028 NewSegmentAppend("0K 30K 60K");
3030 // Append a segment with a start timestamp of 200, but the first
3031 // buffer starts at 230ms. This can happen in muxed content where the
3032 // audio starts before the first frame.
3033 NewSegmentAppend(base::TimeDelta::FromMilliseconds(200),
3034 "230K 260K 290K 320K");
3036 NewSegmentAppend("400K 430K 460K");
3038 CheckExpectedRangesByTimestamp("{ [0,90) [200,350) [400,490) }");
3040 stream_->OnSetDuration(base::TimeDelta::FromMilliseconds(120));
3042 // Verify that the buffered ranges are updated properly and we don't crash.
3043 CheckExpectedRangesByTimestamp("{ [0,90) }");
3046 // Test the case were the current playback position is at the end of the
3047 // buffered data and several overlaps occur that causes the selected
3048 // range to get split and then merged back into a single range.
3049 TEST_F(SourceBufferStreamTest, OverlapSplitAndMergeWhileWaitingForMoreData) {
3050 // Seek to start of stream.
3051 SeekToTimestamp(base::TimeDelta::FromMilliseconds(0));
3053 NewSegmentAppend("0K 30 60 90 120K 150");
3054 CheckExpectedRangesByTimestamp("{ [0,180) }");
3056 // Read all the buffered data.
3057 CheckExpectedBuffers("0K 30 60 90 120K 150");
3058 CheckNoNextBuffer();
3060 // Append data over the current GOP so that a keyframe is needed before
3061 // playback can continue from the current position.
3062 NewSegmentAppend("120K 150");
3063 CheckExpectedRangesByTimestamp("{ [0,180) }");
3065 // Append buffers that cause the range to get split.
3066 NewSegmentAppend("0K 30");
3067 CheckExpectedRangesByTimestamp("{ [0,60) [120,180) }");
3069 // Append buffers that cause the ranges to get merged.
3070 AppendBuffers("60 90");
3072 CheckExpectedRangesByTimestamp("{ [0,180) }");
3074 // Verify that we still don't have a next buffer.
3075 CheckNoNextBuffer();
3077 // Add more data to the end and verify that this new data is read correctly.
3078 NewSegmentAppend("180K 210");
3079 CheckExpectedRangesByTimestamp("{ [0,240) }");
3080 CheckExpectedBuffers("180K 210");
3083 // Verify that non-keyframes with the same timestamp in the same
3084 // append are handled correctly.
3085 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_SingleAppend) {
3087 NewSegmentAppend("0K 30 30 60 90 120K 150");
3088 CheckExpectedBuffers("0K 30 30 60 90 120K 150");
3091 // Verify that non-keyframes with the same timestamp can occur
3092 // in different appends.
3093 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_TwoAppends) {
3095 NewSegmentAppend("0K 30");
3096 AppendBuffers("30 60 90 120K 150");
3097 CheckExpectedBuffers("0K 30 30 60 90 120K 150");
3100 // Verify that a non-keyframe followed by a keyframe with the same timestamp
3102 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Invalid_1) {
3104 NewSegmentAppend("0K 30");
3105 AppendBuffers_ExpectFailure("30K 60");
3108 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Invalid_2) {
3110 NewSegmentAppend_ExpectFailure("0K 30 30K 60");
3113 // Verify that a keyframe followed by a non-keyframe with the same timestamp
3115 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Invalid_3) {
3117 NewSegmentAppend("0K 30K");
3118 AppendBuffers_ExpectFailure("30 60");
3121 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Invalid_4) {
3123 NewSegmentAppend_ExpectFailure("0K 30K 30 60");
3126 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Overlap_1) {
3128 NewSegmentAppend("0K 30 60 60 90 120K 150");
3130 NewSegmentAppend("60K 91 121K 151");
3131 CheckExpectedBuffers("0K 30 60K 91 121K 151");
3134 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Overlap_2) {
3136 NewSegmentAppend("0K 30 60 60 90 120K 150");
3137 NewSegmentAppend("0K 30 61");
3138 CheckExpectedBuffers("0K 30 61 120K 150");
3141 TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Overlap_3) {
3143 NewSegmentAppend("0K 20 40 60 80 100K 101 102 103K");
3144 NewSegmentAppend("0K 20 40 60 80 90");
3145 CheckExpectedBuffers("0K 20 40 60 80 90 100K 101 102 103K");
3146 AppendBuffers("90 110K 150");
3148 CheckExpectedBuffers("0K 20 40 60 80 90 90 110K 150");
3149 CheckNoNextBuffer();
3150 CheckExpectedRangesByTimestamp("{ [0,190) }");
3153 // Test all the valid same timestamp cases for audio.
3154 TEST_F(SourceBufferStreamTest, SameTimestamp_Audio) {
3155 AudioDecoderConfig config(kCodecMP3, kSampleFormatF32, CHANNEL_LAYOUT_STEREO,
3156 44100, NULL, 0, false);
3157 stream_.reset(new SourceBufferStream(config, log_cb()));
3159 NewSegmentAppend("0K 0K 30K 30 60 60");
3160 CheckExpectedBuffers("0K 0K 30K 30 60 60");
3163 TEST_F(SourceBufferStreamTest, SameTimestamp_Audio_Invalid_1) {
3164 AudioDecoderConfig config(kCodecMP3, kSampleFormatF32, CHANNEL_LAYOUT_STEREO,
3165 44100, NULL, 0, false);
3166 stream_.reset(new SourceBufferStream(config, log_cb()));
3168 NewSegmentAppend_ExpectFailure("0K 30 30K 60");
3171 // If seeking past any existing range and the seek is pending
3172 // because no data has been provided for that position,
3173 // the stream position can be considered as the end of stream.
3174 TEST_F(SourceBufferStreamTest, EndSelected_During_PendingSeek) {
3175 // Append 15 buffers at positions 0 through 14.
3176 NewSegmentAppend(0, 15);
3179 EXPECT_TRUE(stream_->IsSeekPending());
3180 stream_->MarkEndOfStream();
3181 EXPECT_FALSE(stream_->IsSeekPending());
3184 // If there is a pending seek between 2 existing ranges,
3185 // the end of the stream has not been reached.
3186 TEST_F(SourceBufferStreamTest, EndNotSelected_During_PendingSeek) {
3188 // - 10 buffers at positions 0 through 9.
3189 // - 10 buffers at positions 30 through 39
3190 NewSegmentAppend(0, 10);
3191 NewSegmentAppend(30, 10);
3194 EXPECT_TRUE(stream_->IsSeekPending());
3195 stream_->MarkEndOfStream();
3196 EXPECT_TRUE(stream_->IsSeekPending());
3200 // Removing exact start & end of a range.
3201 TEST_F(SourceBufferStreamTest, Remove_WholeRange1) {
3203 NewSegmentAppend("10K 40 70K 100 130K");
3204 CheckExpectedRangesByTimestamp("{ [10,160) }");
3205 RemoveInMs(10, 160, 160);
3206 CheckExpectedRangesByTimestamp("{ }");
3209 // Removal range starts before range and ends exactly at end.
3210 TEST_F(SourceBufferStreamTest, Remove_WholeRange2) {
3212 NewSegmentAppend("10K 40 70K 100 130K");
3213 CheckExpectedRangesByTimestamp("{ [10,160) }");
3214 RemoveInMs(0, 160, 160);
3215 CheckExpectedRangesByTimestamp("{ }");
3218 // Removal range starts at the start of a range and ends beyond the
3220 TEST_F(SourceBufferStreamTest, Remove_WholeRange3) {
3222 NewSegmentAppend("10K 40 70K 100 130K");
3223 CheckExpectedRangesByTimestamp("{ [10,160) }");
3224 RemoveInMs(10, 200, 200);
3225 CheckExpectedRangesByTimestamp("{ }");
3228 // Removal range starts before range start and ends after the range end.
3229 TEST_F(SourceBufferStreamTest, Remove_WholeRange4) {
3231 NewSegmentAppend("10K 40 70K 100 130K");
3232 CheckExpectedRangesByTimestamp("{ [10,160) }");
3233 RemoveInMs(0, 200, 200);
3234 CheckExpectedRangesByTimestamp("{ }");
3237 // Removes multiple ranges.
3238 TEST_F(SourceBufferStreamTest, Remove_WholeRange5) {
3240 NewSegmentAppend("10K 40 70K 100 130K");
3241 NewSegmentAppend("1000K 1030 1060K 1090 1120K");
3242 NewSegmentAppend("2000K 2030 2060K 2090 2120K");
3243 CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) [2000,2150) }");
3244 RemoveInMs(10, 3000, 3000);
3245 CheckExpectedRangesByTimestamp("{ }");
3248 // Verifies a [0-infinity) range removes everything.
3249 TEST_F(SourceBufferStreamTest, Remove_ZeroToInfinity) {
3251 NewSegmentAppend("10K 40 70K 100 130K");
3252 NewSegmentAppend("1000K 1030 1060K 1090 1120K");
3253 NewSegmentAppend("2000K 2030 2060K 2090 2120K");
3254 CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) [2000,2150) }");
3255 Remove(base::TimeDelta(), kInfiniteDuration(), kInfiniteDuration());
3256 CheckExpectedRangesByTimestamp("{ }");
3259 // Removal range starts at the beginning of the range and ends in the
3260 // middle of the range. This test verifies that full GOPs are removed.
3261 TEST_F(SourceBufferStreamTest, Remove_Partial1) {
3263 NewSegmentAppend("10K 40 70K 100 130K");
3264 NewSegmentAppend("1000K 1030 1060K 1090 1120K");
3265 CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) }");
3266 RemoveInMs(0, 80, 2200);
3267 CheckExpectedRangesByTimestamp("{ [130,160) [1000,1150) }");
3270 // Removal range starts in the middle of a range and ends at the exact
3271 // end of the range.
3272 TEST_F(SourceBufferStreamTest, Remove_Partial2) {
3274 NewSegmentAppend("10K 40 70K 100 130K");
3275 NewSegmentAppend("1000K 1030 1060K 1090 1120K");
3276 CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) }");
3277 RemoveInMs(40, 160, 2200);
3278 CheckExpectedRangesByTimestamp("{ [10,40) [1000,1150) }");
3281 // Removal range starts and ends within a range.
3282 TEST_F(SourceBufferStreamTest, Remove_Partial3) {
3284 NewSegmentAppend("10K 40 70K 100 130K");
3285 NewSegmentAppend("1000K 1030 1060K 1090 1120K");
3286 CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) }");
3287 RemoveInMs(40, 120, 2200);
3288 CheckExpectedRangesByTimestamp("{ [10,40) [130,160) [1000,1150) }");
3291 // Removal range starts in the middle of one range and ends in the
3292 // middle of another range.
3293 TEST_F(SourceBufferStreamTest, Remove_Partial4) {
3295 NewSegmentAppend("10K 40 70K 100 130K");
3296 NewSegmentAppend("1000K 1030 1060K 1090 1120K");
3297 NewSegmentAppend("2000K 2030 2060K 2090 2120K");
3298 CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) [2000,2150) }");
3299 RemoveInMs(40, 2030, 2200);
3300 CheckExpectedRangesByTimestamp("{ [10,40) [2060,2150) }");
3303 // Test behavior when the current position is removed and new buffers
3304 // are appended over the removal range.
3305 TEST_F(SourceBufferStreamTest, Remove_CurrentPosition) {
3307 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330");
3308 CheckExpectedRangesByTimestamp("{ [0,360) }");
3309 CheckExpectedBuffers("0K 30 60 90K 120");
3311 // Remove a range that includes the next buffer (i.e., 150).
3312 RemoveInMs(150, 210, 360);
3313 CheckExpectedRangesByTimestamp("{ [0,150) [270,360) }");
3315 // Verify that no next buffer is returned.
3316 CheckNoNextBuffer();
3318 // Append some buffers to fill the gap that was created.
3319 NewSegmentAppend("120K 150 180 210K 240");
3320 CheckExpectedRangesByTimestamp("{ [0,360) }");
3322 // Verify that buffers resume at the next keyframe after the
3323 // current position.
3324 CheckExpectedBuffers("210K 240 270K 300 330");
3327 // Test behavior when buffers in the selected range before the current position
3329 TEST_F(SourceBufferStreamTest, Remove_BeforeCurrentPosition) {
3331 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330");
3332 CheckExpectedRangesByTimestamp("{ [0,360) }");
3333 CheckExpectedBuffers("0K 30 60 90K 120");
3335 // Remove a range that is before the current playback position.
3336 RemoveInMs(0, 90, 360);
3337 CheckExpectedRangesByTimestamp("{ [90,360) }");
3339 CheckExpectedBuffers("150 180K 210 240 270K 300 330");
3342 // Test removing the entire range for the current media segment
3344 TEST_F(SourceBufferStreamTest, Remove_MidSegment) {
3346 NewSegmentAppend("0K 30 60 90 120K 150 180 210");
3347 CheckExpectedRangesByTimestamp("{ [0,240) }");
3349 NewSegmentAppend("0K 30");
3351 CheckExpectedBuffers("0K");
3353 CheckExpectedRangesByTimestamp("{ [0,60) [120,240) }");
3355 // Remove the entire range that is being appended to.
3356 RemoveInMs(0, 60, 240);
3358 // Verify that there is no next buffer since it was removed.
3359 CheckNoNextBuffer();
3361 CheckExpectedRangesByTimestamp("{ [120,240) }");
3363 // Continue appending frames for the current GOP.
3364 AppendBuffers("60 90");
3366 // Verify that the non-keyframes are not added.
3367 CheckExpectedRangesByTimestamp("{ [120,240) }");
3369 // Finish the previous GOP and start the next one.
3370 AppendBuffers("120 150K 180");
3372 // Verify that new GOP replaces the existing range.
3373 CheckExpectedRangesByTimestamp("{ [150,210) }");
3376 SeekToTimestamp(base::TimeDelta::FromMilliseconds(150));
3377 CheckExpectedBuffers("150K 180");
3378 CheckNoNextBuffer();
3381 // Test removing the current GOP being appended, while not removing
3382 // the entire range the GOP belongs to.
3383 TEST_F(SourceBufferStreamTest, Remove_GOPBeingAppended) {
3385 NewSegmentAppend("0K 30 60 90 120K 150 180");
3386 CheckExpectedRangesByTimestamp("{ [0,210) }");
3388 // Remove the current GOP being appended.
3389 RemoveInMs(120, 150, 240);
3390 CheckExpectedRangesByTimestamp("{ [0,120) }");
3392 // Continue appending the current GOP and the next one.
3393 AppendBuffers("210 240K 270 300");
3395 // Verify that the non-keyframe in the previous GOP does
3396 // not effect any existing ranges and a new range is started at the
3397 // beginning of the next GOP.
3398 CheckExpectedRangesByTimestamp("{ [0,120) [240,330) }");
3400 // Verify the buffers in the ranges.
3401 CheckExpectedBuffers("0K 30 60 90");
3402 CheckNoNextBuffer();
3403 SeekToTimestamp(base::TimeDelta::FromMilliseconds(240));
3404 CheckExpectedBuffers("240K 270 300");
3407 TEST_F(SourceBufferStreamTest,
3408 Remove_PreviousAppendDestroyedAndOverwriteExistingRange) {
3409 SeekToTimestamp(base::TimeDelta::FromMilliseconds(90));
3411 NewSegmentAppend("90K 120 150");
3412 CheckExpectedRangesByTimestamp("{ [90,180) }");
3414 // Append a segment before the previously appended data.
3415 NewSegmentAppend("0K 30 60");
3417 // Verify that the ranges get merged.
3418 CheckExpectedRangesByTimestamp("{ [0,180) }");
3420 // Remove the data from the last append.
3421 RemoveInMs(0, 90, 360);
3422 CheckExpectedRangesByTimestamp("{ [90,180) }");
3424 // Append a new segment that follows the removed segment and
3425 // starts at the beginning of the range left over from the
3427 NewSegmentAppend("90K 121 151");
3428 CheckExpectedBuffers("90K 121 151");
3431 TEST_F(SourceBufferStreamTest, Text_Append_SingleRange) {
3433 NewSegmentAppend("0K 500K 1000K");
3434 CheckExpectedRangesByTimestamp("{ [0,1500) }");
3437 CheckExpectedBuffers("0K 500K 1000K");
3440 TEST_F(SourceBufferStreamTest, Text_Append_DisjointAfter) {
3442 NewSegmentAppend("0K 500K 1000K");
3443 CheckExpectedRangesByTimestamp("{ [0,1500) }");
3444 NewSegmentAppend("3000K 3500K 4000K");
3445 CheckExpectedRangesByTimestamp("{ [0,4500) }");
3448 CheckExpectedBuffers("0K 500K 1000K 3000K 3500K 4000K");
3451 TEST_F(SourceBufferStreamTest, Text_Append_DisjointBefore) {
3453 NewSegmentAppend("3000K 3500K 4000K");
3454 CheckExpectedRangesByTimestamp("{ [3000,4500) }");
3455 NewSegmentAppend("0K 500K 1000K");
3456 CheckExpectedRangesByTimestamp("{ [0,4500) }");
3459 CheckExpectedBuffers("0K 500K 1000K 3000K 3500K 4000K");
3462 TEST_F(SourceBufferStreamTest, Text_CompleteOverlap) {
3464 NewSegmentAppend("3000K 3500K 4000K");
3465 CheckExpectedRangesByTimestamp("{ [3000,4500) }");
3466 NewSegmentAppend("0K 501K 1001K 1501K 2001K 2501K "
3467 "3001K 3501K 4001K 4501K 5001K");
3468 CheckExpectedRangesByTimestamp("{ [0,5502) }");
3471 CheckExpectedBuffers("0K 501K 1001K 1501K 2001K 2501K "
3472 "3001K 3501K 4001K 4501K 5001K");
3475 TEST_F(SourceBufferStreamTest, Text_OverlapAfter) {
3477 NewSegmentAppend("0K 500K 1000K 1500K 2000K");
3478 CheckExpectedRangesByTimestamp("{ [0,2500) }");
3479 NewSegmentAppend("1499K 2001K 2501K 3001K");
3480 CheckExpectedRangesByTimestamp("{ [0,3503) }");
3483 CheckExpectedBuffers("0K 500K 1000K 1499K 2001K 2501K 3001K");
3486 TEST_F(SourceBufferStreamTest, Text_OverlapBefore) {
3488 NewSegmentAppend("1500K 2000K 2500K 3000K 3500K");
3489 CheckExpectedRangesByTimestamp("{ [1500,4000) }");
3490 NewSegmentAppend("0K 501K 1001K 1501K 2001K");
3491 CheckExpectedRangesByTimestamp("{ [0,4001) }");
3494 CheckExpectedBuffers("0K 501K 1001K 1501K 2001K 2500K 3000K 3500K");
3497 TEST_F(SourceBufferStreamTest, SpliceFrame_Basic) {
3499 NewSegmentAppend("0K S(3K 6 9 10) 15 20 S(25K 30 35) 40");
3500 CheckExpectedBuffers("0K 3K 6 9 C 10 15 20 25K 30 C 35 40");
3501 CheckNoNextBuffer();
3504 TEST_F(SourceBufferStreamTest, SpliceFrame_SeekClearsSplice) {
3506 NewSegmentAppend("0K S(3K 6 9 10) 15K 20");
3507 CheckExpectedBuffers("0K 3K 6");
3509 SeekToTimestamp(base::TimeDelta::FromMilliseconds(15));
3510 CheckExpectedBuffers("15K 20");
3511 CheckNoNextBuffer();
3514 TEST_F(SourceBufferStreamTest, SpliceFrame_SeekClearsSpliceFromTrackBuffer) {
3516 NewSegmentAppend("0K 2K S(3K 6 9 10) 15K 20");
3517 CheckExpectedBuffers("0K 2K");
3519 // Overlap the existing segment.
3520 NewSegmentAppend("5K 15K 20");
3521 CheckExpectedBuffers("3K 6");
3523 SeekToTimestamp(base::TimeDelta::FromMilliseconds(15));
3524 CheckExpectedBuffers("15K 20");
3525 CheckNoNextBuffer();
3528 TEST_F(SourceBufferStreamTest, SpliceFrame_ConfigChangeWithinSplice) {
3529 VideoDecoderConfig new_config = TestVideoConfig::Large();
3530 ASSERT_FALSE(new_config.Matches(video_config_));
3532 // Add a new video config, then reset the config index back to the original.
3533 stream_->UpdateVideoConfig(new_config);
3534 stream_->UpdateVideoConfig(video_config_);
3537 CheckVideoConfig(video_config_);
3538 NewSegmentAppend("0K S(3K 6C 9 10) 15");
3540 CheckExpectedBuffers("0K 3K C");
3541 CheckVideoConfig(new_config);
3542 CheckExpectedBuffers("6 9 C");
3543 CheckVideoConfig(video_config_);
3544 CheckExpectedBuffers("10 15");
3545 CheckNoNextBuffer();
3548 TEST_F(SourceBufferStreamTest, SpliceFrame_BasicFromTrackBuffer) {
3550 NewSegmentAppend("0K 5K S(8K 9 10) 20");
3551 CheckExpectedBuffers("0K 5K");
3553 // Overlap the existing segment.
3554 NewSegmentAppend("5K 20");
3555 CheckExpectedBuffers("8K 9 C 10 20");
3556 CheckNoNextBuffer();
3559 TEST_F(SourceBufferStreamTest,
3560 SpliceFrame_ConfigChangeWithinSpliceFromTrackBuffer) {
3561 VideoDecoderConfig new_config = TestVideoConfig::Large();
3562 ASSERT_FALSE(new_config.Matches(video_config_));
3564 // Add a new video config, then reset the config index back to the original.
3565 stream_->UpdateVideoConfig(new_config);
3566 stream_->UpdateVideoConfig(video_config_);
3569 CheckVideoConfig(video_config_);
3570 NewSegmentAppend("0K 5K S(7K 8C 9 10) 20");
3571 CheckExpectedBuffers("0K 5K");
3573 // Overlap the existing segment.
3574 NewSegmentAppend("5K 20");
3575 CheckExpectedBuffers("7K C");
3576 CheckVideoConfig(new_config);
3577 CheckExpectedBuffers("8 9 C");
3578 CheckVideoConfig(video_config_);
3579 CheckExpectedBuffers("10 20");
3580 CheckNoNextBuffer();
3583 // TODO(vrk): Add unit tests where keyframes are unaligned between streams.
3584 // (crbug.com/133557)
3586 // TODO(vrk): Add unit tests with end of stream being called at interesting
3589 } // namespace media