OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/filters/source_buffer_stream.h" | 5 #include "media/filters/source_buffer_stream.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 } | 182 } |
183 | 183 |
184 void Seek(int position) { | 184 void Seek(int position) { |
185 stream_->Seek(position * frame_duration_); | 185 stream_->Seek(position * frame_duration_); |
186 } | 186 } |
187 | 187 |
188 void SeekToTimestampMs(int64_t timestamp_ms) { | 188 void SeekToTimestampMs(int64_t timestamp_ms) { |
189 stream_->Seek(base::TimeDelta::FromMilliseconds(timestamp_ms)); | 189 stream_->Seek(base::TimeDelta::FromMilliseconds(timestamp_ms)); |
190 } | 190 } |
191 | 191 |
| 192 bool GarbageCollectWithPlaybackAtBuffer(int position, int newDataBuffers) { |
| 193 return stream_->GarbageCollectIfNeeded( |
| 194 DecodeTimestamp::FromPresentationTime(position * frame_duration_), |
| 195 newDataBuffers * kDataSize); |
| 196 } |
| 197 |
192 void RemoveInMs(int start, int end, int duration) { | 198 void RemoveInMs(int start, int end, int duration) { |
193 Remove(base::TimeDelta::FromMilliseconds(start), | 199 Remove(base::TimeDelta::FromMilliseconds(start), |
194 base::TimeDelta::FromMilliseconds(end), | 200 base::TimeDelta::FromMilliseconds(end), |
195 base::TimeDelta::FromMilliseconds(duration)); | 201 base::TimeDelta::FromMilliseconds(duration)); |
196 } | 202 } |
197 | 203 |
198 void Remove(base::TimeDelta start, base::TimeDelta end, | 204 void Remove(base::TimeDelta start, base::TimeDelta end, |
199 base::TimeDelta duration) { | 205 base::TimeDelta duration) { |
200 stream_->Remove(start, end, duration); | 206 stream_->Remove(start, end, duration); |
201 } | 207 } |
(...skipping 2209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2411 | 2417 |
2412 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFront) { | 2418 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFront) { |
2413 // Set memory limit to 20 buffers. | 2419 // Set memory limit to 20 buffers. |
2414 SetMemoryLimit(20); | 2420 SetMemoryLimit(20); |
2415 | 2421 |
2416 // Append 20 buffers at positions 0 through 19. | 2422 // Append 20 buffers at positions 0 through 19. |
2417 NewSegmentAppend(0, 1, &kDataA); | 2423 NewSegmentAppend(0, 1, &kDataA); |
2418 for (int i = 1; i < 20; i++) | 2424 for (int i = 1; i < 20; i++) |
2419 AppendBuffers(i, 1, &kDataA); | 2425 AppendBuffers(i, 1, &kDataA); |
2420 | 2426 |
2421 // None of the buffers should trigger garbage collection, so all data should | 2427 // GC should be a no-op, since we are just under memory limit. |
2422 // be there as expected. | 2428 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0)); |
2423 CheckExpectedRanges("{ [0,19) }"); | 2429 CheckExpectedRanges("{ [0,19) }"); |
2424 Seek(0); | 2430 Seek(0); |
2425 CheckExpectedBuffers(0, 19, &kDataA); | 2431 CheckExpectedBuffers(0, 19, &kDataA); |
2426 | 2432 |
2427 // Seek to the middle of the stream. | 2433 // Seek to the middle of the stream. |
2428 Seek(10); | 2434 Seek(10); |
2429 | 2435 |
| 2436 // We are about to append 5 new buffers and current playback position is 10, |
| 2437 // so the GC algorithm should be able to delete some old data from the front. |
| 2438 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(10, 5)); |
| 2439 CheckExpectedRanges("{ [5,19) }"); |
| 2440 |
2430 // Append 5 buffers to the end of the stream. | 2441 // Append 5 buffers to the end of the stream. |
2431 AppendBuffers(20, 5, &kDataA); | 2442 AppendBuffers(20, 5, &kDataA); |
| 2443 CheckExpectedRanges("{ [5,24) }"); |
2432 | 2444 |
2433 // GC should have deleted the first 5 buffers. | |
2434 CheckExpectedRanges("{ [5,24) }"); | |
2435 CheckExpectedBuffers(10, 24, &kDataA); | 2445 CheckExpectedBuffers(10, 24, &kDataA); |
2436 Seek(5); | 2446 Seek(5); |
2437 CheckExpectedBuffers(5, 9, &kDataA); | 2447 CheckExpectedBuffers(5, 9, &kDataA); |
2438 } | 2448 } |
2439 | 2449 |
2440 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFrontGOPsAtATime) { | 2450 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFrontGOPsAtATime) { |
2441 // Set memory limit to 20 buffers. | 2451 // Set memory limit to 20 buffers. |
2442 SetMemoryLimit(20); | 2452 SetMemoryLimit(20); |
2443 | 2453 |
2444 // Append 20 buffers at positions 0 through 19. | 2454 // Append 20 buffers at positions 0 through 19. |
2445 NewSegmentAppend(0, 20, &kDataA); | 2455 NewSegmentAppend(0, 20, &kDataA); |
2446 | 2456 |
2447 // Seek to position 10. | 2457 // Seek to position 10. |
2448 Seek(10); | 2458 Seek(10); |
| 2459 CheckExpectedRanges("{ [0,19) }"); |
2449 | 2460 |
2450 // Add one buffer to put the memory over the cap. | 2461 // Add one buffer to put the memory over the cap. |
| 2462 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(10, 1)); |
2451 AppendBuffers(20, 1, &kDataA); | 2463 AppendBuffers(20, 1, &kDataA); |
2452 | 2464 |
2453 // GC should have deleted the first 5 buffers so that the range still begins | 2465 // GC should have deleted the first 5 buffers so that the range still begins |
2454 // with a keyframe. | 2466 // with a keyframe. |
2455 CheckExpectedRanges("{ [5,20) }"); | 2467 CheckExpectedRanges("{ [5,20) }"); |
2456 CheckExpectedBuffers(10, 20, &kDataA); | 2468 CheckExpectedBuffers(10, 20, &kDataA); |
2457 Seek(5); | 2469 Seek(5); |
2458 CheckExpectedBuffers(5, 9, &kDataA); | 2470 CheckExpectedBuffers(5, 9, &kDataA); |
2459 } | 2471 } |
2460 | 2472 |
2461 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteBack) { | 2473 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteBack) { |
2462 // Set memory limit to 5 buffers. | 2474 // Set memory limit to 5 buffers. |
2463 SetMemoryLimit(5); | 2475 SetMemoryLimit(5); |
2464 | 2476 |
| 2477 // Append 5 buffers at positions 15 through 19. |
| 2478 NewSegmentAppend(15, 5, &kDataA); |
| 2479 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0)); |
| 2480 |
| 2481 // Append 5 buffers at positions 0 through 4. |
| 2482 NewSegmentAppend(0, 5, &kDataA); |
| 2483 CheckExpectedRanges("{ [0,4) [15,19) }"); |
| 2484 |
2465 // Seek to position 0. | 2485 // Seek to position 0. |
2466 Seek(0); | 2486 Seek(0); |
2467 | 2487 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0)); |
2468 // Append 20 buffers at positions 0 through 19. | 2488 // Should leave the first 5 buffers from 0 to 4. |
2469 NewSegmentAppend(0, 20, &kDataA); | 2489 CheckExpectedRanges("{ [0,4) }"); |
2470 | |
2471 // Should leave the first 5 buffers from 0 to 4 and the last GOP appended. | |
2472 CheckExpectedRanges("{ [0,4) [15,19) }"); | |
2473 CheckExpectedBuffers(0, 4, &kDataA); | 2490 CheckExpectedBuffers(0, 4, &kDataA); |
2474 } | 2491 } |
2475 | 2492 |
2476 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFrontAndBack) { | 2493 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFrontAndBack) { |
2477 // Set memory limit to 3 buffers. | 2494 // Set memory limit to 3 buffers. |
2478 SetMemoryLimit(3); | 2495 SetMemoryLimit(3); |
2479 | 2496 |
2480 // Seek to position 15. | 2497 // Seek to position 15. |
2481 Seek(15); | 2498 Seek(15); |
2482 | 2499 |
2483 // Append 40 buffers at positions 0 through 39. | 2500 // Append 40 buffers at positions 0 through 39. |
2484 NewSegmentAppend(0, 40, &kDataA); | 2501 NewSegmentAppend(0, 40, &kDataA); |
| 2502 // GC will try to keep data between current playback position and last append |
| 2503 // position. This will ensure that the last append position is 19 and will |
| 2504 // allow GC algorithm to collect data outside of the range [15,19) |
| 2505 NewSegmentAppend(15, 5, &kDataA); |
| 2506 CheckExpectedRanges("{ [0,39) }"); |
2485 | 2507 |
2486 // Should leave the GOP containing the seek position and the last GOP | 2508 // Should leave the GOP containing the current playback position 15 and the |
2487 // appended. | 2509 // last append position 19. GC returns false, since we are still above limit. |
2488 CheckExpectedRanges("{ [15,19) [35,39) }"); | 2510 EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(15, 0)); |
| 2511 CheckExpectedRanges("{ [15,19) }"); |
2489 CheckExpectedBuffers(15, 19, &kDataA); | 2512 CheckExpectedBuffers(15, 19, &kDataA); |
2490 CheckNoNextBuffer(); | 2513 CheckNoNextBuffer(); |
2491 } | 2514 } |
2492 | 2515 |
2493 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteSeveralRanges) { | 2516 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteSeveralRanges) { |
2494 // Append 5 buffers at positions 0 through 4. | 2517 // Append 5 buffers at positions 0 through 4. |
2495 NewSegmentAppend(0, 5); | 2518 NewSegmentAppend(0, 5); |
2496 | 2519 |
2497 // Append 5 buffers at positions 10 through 14. | 2520 // Append 5 buffers at positions 10 through 14. |
2498 NewSegmentAppend(10, 5); | 2521 NewSegmentAppend(10, 5); |
2499 | 2522 |
2500 // Append 5 buffers at positions 20 through 24. | 2523 // Append 5 buffers at positions 20 through 24. |
2501 NewSegmentAppend(20, 5); | 2524 NewSegmentAppend(20, 5); |
2502 | 2525 |
2503 // Append 5 buffers at positions 30 through 34. | 2526 // Append 5 buffers at positions 40 through 44. |
2504 NewSegmentAppend(30, 5); | 2527 NewSegmentAppend(40, 5); |
2505 | 2528 |
2506 CheckExpectedRanges("{ [0,4) [10,14) [20,24) [30,34) }"); | 2529 CheckExpectedRanges("{ [0,4) [10,14) [20,24) [40,44) }"); |
2507 | 2530 |
2508 // Seek to position 21. | 2531 // Seek to position 20. |
2509 Seek(20); | 2532 Seek(20); |
2510 CheckExpectedBuffers(20, 20); | 2533 CheckExpectedBuffers(20, 20); |
2511 | 2534 |
2512 // Set memory limit to 1 buffer. | 2535 // Set memory limit to 1 buffer. |
2513 SetMemoryLimit(1); | 2536 SetMemoryLimit(1); |
2514 | 2537 |
2515 // Append 5 buffers at positions 40 through 44. This will trigger GC. | 2538 // Append 5 buffers at positions 30 through 34. |
2516 NewSegmentAppend(40, 5); | 2539 NewSegmentAppend(30, 5); |
2517 | 2540 |
2518 // Should delete everything except the GOP containing the current buffer and | 2541 // We will have more than 1 buffer left, GC will fail |
2519 // the last GOP appended. | 2542 EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(20, 0)); |
2520 CheckExpectedRanges("{ [20,24) [40,44) }"); | 2543 |
| 2544 // Should have deleted all buffer ranges before the current buffer and after |
| 2545 // last GOP |
| 2546 CheckExpectedRanges("{ [20,24) [30,34) }"); |
2521 CheckExpectedBuffers(21, 24); | 2547 CheckExpectedBuffers(21, 24); |
2522 CheckNoNextBuffer(); | 2548 CheckNoNextBuffer(); |
2523 | 2549 |
2524 // Continue appending into the last range to make sure it didn't break. | 2550 // Continue appending into the last range to make sure it didn't break. |
2525 AppendBuffers(45, 10); | 2551 AppendBuffers(35, 10); |
2526 // Should only save last GOP appended. | 2552 EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(20, 0)); |
2527 CheckExpectedRanges("{ [20,24) [50,54) }"); | 2553 // Should save everything between read head and last appended |
| 2554 CheckExpectedRanges("{ [20,24) [30,44) }"); |
2528 | 2555 |
2529 // Make sure appending before and after the ranges didn't somehow break. | 2556 // Make sure appending before and after the ranges didn't somehow break. |
2530 SetMemoryLimit(100); | 2557 SetMemoryLimit(100); |
2531 NewSegmentAppend(0, 10); | 2558 NewSegmentAppend(0, 10); |
2532 CheckExpectedRanges("{ [0,9) [20,24) [50,54) }"); | 2559 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(20, 0)); |
| 2560 CheckExpectedRanges("{ [0,9) [20,24) [30,44) }"); |
2533 Seek(0); | 2561 Seek(0); |
2534 CheckExpectedBuffers(0, 9); | 2562 CheckExpectedBuffers(0, 9); |
2535 | 2563 |
2536 NewSegmentAppend(90, 10); | 2564 NewSegmentAppend(90, 10); |
2537 CheckExpectedRanges("{ [0,9) [20,24) [50,54) [90,99) }"); | 2565 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0)); |
2538 Seek(50); | 2566 CheckExpectedRanges("{ [0,9) [20,24) [30,44) [90,99) }"); |
2539 CheckExpectedBuffers(50, 54); | 2567 Seek(30); |
| 2568 CheckExpectedBuffers(30, 44); |
2540 CheckNoNextBuffer(); | 2569 CheckNoNextBuffer(); |
2541 Seek(90); | 2570 Seek(90); |
2542 CheckExpectedBuffers(90, 99); | 2571 CheckExpectedBuffers(90, 99); |
2543 CheckNoNextBuffer(); | 2572 CheckNoNextBuffer(); |
2544 } | 2573 } |
2545 | 2574 |
2546 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteAfterLastAppend) { | 2575 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteAfterLastAppend) { |
2547 // Set memory limit to 10 buffers. | 2576 // Set memory limit to 10 buffers. |
2548 SetMemoryLimit(10); | 2577 SetMemoryLimit(10); |
2549 | 2578 |
2550 // Append 1 GOP starting at 310ms, 30ms apart. | 2579 // Append 1 GOP starting at 310ms, 30ms apart. |
2551 NewSegmentAppend("310K 340 370"); | 2580 NewSegmentAppend("310K 340 370"); |
2552 | 2581 |
2553 // Append 2 GOPs starting at 490ms, 30ms apart. | 2582 // Append 2 GOPs starting at 490ms, 30ms apart. |
2554 NewSegmentAppend("490K 520 550 580K 610 640"); | 2583 NewSegmentAppend("490K 520 550 580K 610 640"); |
2555 | 2584 |
| 2585 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0)); |
| 2586 |
2556 CheckExpectedRangesByTimestamp("{ [310,400) [490,670) }"); | 2587 CheckExpectedRangesByTimestamp("{ [310,400) [490,670) }"); |
2557 | 2588 |
2558 // Seek to the GOP at 580ms. | 2589 // Seek to the GOP at 580ms. |
2559 SeekToTimestampMs(580); | 2590 SeekToTimestampMs(580); |
2560 | 2591 |
2561 // Append 2 GOPs before the existing ranges. | 2592 // Append 2 GOPs before the existing ranges. |
2562 // So the ranges before GC are "{ [100,280) [310,400) [490,670) }". | 2593 // So the ranges before GC are "{ [100,280) [310,400) [490,670) }". |
2563 NewSegmentAppend("100K 130 160 190K 220 250K"); | 2594 NewSegmentAppend("100K 130 160 190K 220 250K"); |
2564 | 2595 |
| 2596 EXPECT_TRUE(stream_->GarbageCollectIfNeeded( |
| 2597 DecodeTimestamp::FromMilliseconds(580), 0)); |
| 2598 |
2565 // Should save the newly appended GOPs. | 2599 // Should save the newly appended GOPs. |
2566 CheckExpectedRangesByTimestamp("{ [100,280) [580,670) }"); | 2600 CheckExpectedRangesByTimestamp("{ [100,280) [580,670) }"); |
2567 } | 2601 } |
2568 | 2602 |
2569 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteAfterLastAppendMerged) { | 2603 TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteAfterLastAppendMerged) { |
2570 // Set memory limit to 10 buffers. | 2604 // Set memory limit to 10 buffers. |
2571 SetMemoryLimit(10); | 2605 SetMemoryLimit(10); |
2572 | 2606 |
2573 // Append 3 GOPs starting at 400ms, 30ms apart. | 2607 // Append 3 GOPs starting at 400ms, 30ms apart. |
2574 NewSegmentAppend("400K 430 460 490K 520 550 580K 610 640"); | 2608 NewSegmentAppend("400K 430 460 490K 520 550 580K 610 640"); |
2575 | 2609 |
2576 // Seek to the GOP at 580ms. | 2610 // Seek to the GOP at 580ms. |
2577 SeekToTimestampMs(580); | 2611 SeekToTimestampMs(580); |
2578 | 2612 |
2579 // Append 2 GOPs starting at 220ms, and they will be merged with the existing | 2613 // Append 2 GOPs starting at 220ms, and they will be merged with the existing |
2580 // range. So the range before GC is "{ [220,670) }". | 2614 // range. So the range before GC is "{ [220,670) }". |
2581 NewSegmentAppend("220K 250 280 310K 340 370"); | 2615 NewSegmentAppend("220K 250 280 310K 340 370"); |
2582 | 2616 |
| 2617 EXPECT_TRUE(stream_->GarbageCollectIfNeeded( |
| 2618 DecodeTimestamp::FromMilliseconds(580), 0)); |
| 2619 |
2583 // Should save the newly appended GOPs. | 2620 // Should save the newly appended GOPs. |
2584 CheckExpectedRangesByTimestamp("{ [220,400) [580,670) }"); | 2621 CheckExpectedRangesByTimestamp("{ [220,400) [580,670) }"); |
2585 } | 2622 } |
2586 | 2623 |
2587 TEST_F(SourceBufferStreamTest, GarbageCollection_NoSeek) { | 2624 TEST_F(SourceBufferStreamTest, GarbageCollection_NoSeek) { |
2588 // Set memory limit to 20 buffers. | 2625 // Set memory limit to 20 buffers. |
2589 SetMemoryLimit(20); | 2626 SetMemoryLimit(20); |
2590 | 2627 |
2591 // Append 25 buffers at positions 0 through 24. | 2628 // Append 25 buffers at positions 0 through 24. |
2592 NewSegmentAppend(0, 25, &kDataA); | 2629 NewSegmentAppend(0, 25, &kDataA); |
2593 | 2630 |
2594 // GC deletes the first 5 buffers to keep the memory limit within cap. | 2631 // If playback is still in the first GOP (starting at 0), GC should fail. |
| 2632 EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(2, 0)); |
| 2633 CheckExpectedRanges("{ [0,24) }"); |
| 2634 |
| 2635 // As soon as playback position moves past the first GOP, it should be removed |
| 2636 // and after removing the first GOP we are under memory limit. |
| 2637 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(5, 0)); |
2595 CheckExpectedRanges("{ [5,24) }"); | 2638 CheckExpectedRanges("{ [5,24) }"); |
2596 CheckNoNextBuffer(); | 2639 CheckNoNextBuffer(); |
2597 Seek(5); | 2640 Seek(5); |
2598 CheckExpectedBuffers(5, 24, &kDataA); | 2641 CheckExpectedBuffers(5, 24, &kDataA); |
2599 } | 2642 } |
2600 | 2643 |
2601 TEST_F(SourceBufferStreamTest, GarbageCollection_PendingSeek) { | 2644 TEST_F(SourceBufferStreamTest, GarbageCollection_PendingSeek) { |
2602 // Append 10 buffers at positions 0 through 9. | 2645 // Append 10 buffers at positions 0 through 9. |
2603 NewSegmentAppend(0, 10, &kDataA); | 2646 NewSegmentAppend(0, 10, &kDataA); |
2604 | 2647 |
2605 // Append 5 buffers at positions 25 through 29. | 2648 // Append 5 buffers at positions 25 through 29. |
2606 NewSegmentAppend(25, 5, &kDataA); | 2649 NewSegmentAppend(25, 5, &kDataA); |
2607 | 2650 |
2608 // Seek to position 15. | 2651 // Seek to position 15. |
2609 Seek(15); | 2652 Seek(15); |
2610 CheckNoNextBuffer(); | 2653 CheckNoNextBuffer(); |
2611 | |
2612 CheckExpectedRanges("{ [0,9) [25,29) }"); | 2654 CheckExpectedRanges("{ [0,9) [25,29) }"); |
2613 | 2655 |
2614 // Set memory limit to 5 buffers. | 2656 // Set memory limit to 5 buffers. |
2615 SetMemoryLimit(5); | 2657 SetMemoryLimit(5); |
2616 | 2658 |
2617 // Append 5 buffers as positions 30 to 34 to trigger GC. | 2659 // Append 5 buffers as positions 30 to 34 to trigger GC. |
2618 AppendBuffers(30, 5, &kDataA); | 2660 AppendBuffers(30, 5, &kDataA); |
2619 | 2661 |
| 2662 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(30, 0)); |
| 2663 |
2620 // The current algorithm will delete from the beginning until the memory is | 2664 // The current algorithm will delete from the beginning until the memory is |
2621 // under cap. | 2665 // under cap. |
2622 CheckExpectedRanges("{ [30,34) }"); | 2666 CheckExpectedRanges("{ [30,34) }"); |
2623 | 2667 |
2624 // Expand memory limit again so that GC won't be triggered. | 2668 // Expand memory limit again so that GC won't be triggered. |
2625 SetMemoryLimit(100); | 2669 SetMemoryLimit(100); |
2626 | 2670 |
2627 // Append data to fulfill seek. | 2671 // Append data to fulfill seek. |
| 2672 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(30, 5)); |
2628 NewSegmentAppend(15, 5, &kDataA); | 2673 NewSegmentAppend(15, 5, &kDataA); |
2629 | 2674 |
2630 // Check to make sure all is well. | 2675 // Check to make sure all is well. |
2631 CheckExpectedRanges("{ [15,19) [30,34) }"); | 2676 CheckExpectedRanges("{ [15,19) [30,34) }"); |
2632 CheckExpectedBuffers(15, 19, &kDataA); | 2677 CheckExpectedBuffers(15, 19, &kDataA); |
2633 Seek(30); | 2678 Seek(30); |
2634 CheckExpectedBuffers(30, 34, &kDataA); | 2679 CheckExpectedBuffers(30, 34, &kDataA); |
2635 } | 2680 } |
2636 | 2681 |
2637 TEST_F(SourceBufferStreamTest, GarbageCollection_NeedsMoreData) { | 2682 TEST_F(SourceBufferStreamTest, GarbageCollection_NeedsMoreData) { |
2638 // Set memory limit to 15 buffers. | 2683 // Set memory limit to 15 buffers. |
2639 SetMemoryLimit(15); | 2684 SetMemoryLimit(15); |
2640 | 2685 |
2641 // Append 10 buffers at positions 0 through 9. | 2686 // Append 10 buffers at positions 0 through 9. |
2642 NewSegmentAppend(0, 10, &kDataA); | 2687 NewSegmentAppend(0, 10, &kDataA); |
2643 | 2688 |
2644 // Advance next buffer position to 10. | 2689 // Advance next buffer position to 10. |
2645 Seek(0); | 2690 Seek(0); |
| 2691 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0)); |
2646 CheckExpectedBuffers(0, 9, &kDataA); | 2692 CheckExpectedBuffers(0, 9, &kDataA); |
2647 CheckNoNextBuffer(); | 2693 CheckNoNextBuffer(); |
2648 | 2694 |
2649 // Append 20 buffers at positions 15 through 34. | 2695 // Append 20 buffers at positions 15 through 34. |
2650 NewSegmentAppend(15, 20, &kDataA); | 2696 NewSegmentAppend(15, 20, &kDataA); |
| 2697 CheckExpectedRanges("{ [0,9) [15,34) }"); |
2651 | 2698 |
2652 // GC should have saved the keyframe before the current seek position and the | 2699 // GC should save the keyframe before the next buffer position and the data |
2653 // data closest to the current seek position. It will also save the last GOP | 2700 // closest to the next buffer position. It will also save all buffers from |
2654 // appended. | 2701 // next buffer to the last GOP appended, which overflows limit and leads to |
2655 CheckExpectedRanges("{ [5,9) [15,19) [30,34) }"); | 2702 // failure. |
| 2703 EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(5, 0)); |
| 2704 CheckExpectedRanges("{ [5,9) [15,34) }"); |
2656 | 2705 |
2657 // Now fulfill the seek at position 10. This will make GC delete the data | 2706 // Now fulfill the seek at position 10. This will make GC delete the data |
2658 // before position 10 to keep it within cap. | 2707 // before position 10 to keep it within cap. |
2659 NewSegmentAppend(10, 5, &kDataA); | 2708 NewSegmentAppend(10, 5, &kDataA); |
2660 CheckExpectedRanges("{ [10,19) [30,34) }"); | 2709 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(10, 0)); |
2661 CheckExpectedBuffers(10, 19, &kDataA); | 2710 CheckExpectedRanges("{ [10,24) }"); |
| 2711 CheckExpectedBuffers(10, 24, &kDataA); |
2662 } | 2712 } |
2663 | 2713 |
2664 TEST_F(SourceBufferStreamTest, GarbageCollection_TrackBuffer) { | 2714 TEST_F(SourceBufferStreamTest, GarbageCollection_TrackBuffer) { |
2665 EXPECT_MEDIA_LOG_STRING(ContainsTrackBufferExhaustionSkipLog(99)); | 2715 EXPECT_MEDIA_LOG_STRING(ContainsTrackBufferExhaustionSkipLog(99)); |
2666 | 2716 |
2667 // Set memory limit to 3 buffers. | 2717 // Set memory limit to 3 buffers. |
2668 SetMemoryLimit(3); | 2718 SetMemoryLimit(3); |
2669 | 2719 |
2670 // Seek to position 15. | 2720 // Seek to position 15. |
2671 Seek(15); | 2721 Seek(15); |
2672 | 2722 |
2673 // Append 18 buffers at positions 0 through 17. | 2723 // Append 18 buffers at positions 0 through 17. |
2674 NewSegmentAppend(0, 18, &kDataA); | 2724 NewSegmentAppend(0, 18, &kDataA); |
2675 | 2725 |
| 2726 EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(15, 0)); |
| 2727 |
2676 // Should leave GOP containing seek position. | 2728 // Should leave GOP containing seek position. |
2677 CheckExpectedRanges("{ [15,17) }"); | 2729 CheckExpectedRanges("{ [15,17) }"); |
2678 | 2730 |
2679 // Seek ahead to position 16. | 2731 // Move next buffer position to 16. |
2680 CheckExpectedBuffers(15, 15, &kDataA); | 2732 CheckExpectedBuffers(15, 15, &kDataA); |
2681 | 2733 |
2682 // Completely overlap the existing buffers. | 2734 // Completely overlap the existing buffers. |
2683 NewSegmentAppend(0, 20, &kDataB); | 2735 NewSegmentAppend(0, 20, &kDataB); |
2684 | 2736 |
| 2737 // Final GOP [15,19) contains 5 buffers, which is more than memory limit of |
| 2738 // 3 buffers set at the beginning of the test, so GC will fail. |
| 2739 EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(15, 0)); |
| 2740 |
2685 // Because buffers 16 and 17 are not keyframes, they are moved to the track | 2741 // Because buffers 16 and 17 are not keyframes, they are moved to the track |
2686 // buffer upon overlap. The source buffer (i.e. not the track buffer) is now | 2742 // buffer upon overlap. The source buffer (i.e. not the track buffer) is now |
2687 // waiting for the next keyframe. | 2743 // waiting for the next keyframe. |
2688 CheckExpectedRanges("{ [15,19) }"); | 2744 CheckExpectedRanges("{ [15,19) }"); |
2689 CheckExpectedBuffers(16, 17, &kDataA); | 2745 CheckExpectedBuffers(16, 17, &kDataA); |
2690 CheckNoNextBuffer(); | 2746 CheckNoNextBuffer(); |
2691 | 2747 |
2692 // Now add a keyframe at position 20. | 2748 // Now add a keyframe at position 20. |
2693 AppendBuffers(20, 5, &kDataB); | 2749 AppendBuffers(20, 5, &kDataB); |
2694 | 2750 |
| 2751 // 5 buffers in final GOP, GC will fail |
| 2752 EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(20, 0)); |
| 2753 |
2695 // Should garbage collect such that there are 5 frames remaining, starting at | 2754 // Should garbage collect such that there are 5 frames remaining, starting at |
2696 // the keyframe. | 2755 // the keyframe. |
2697 CheckExpectedRanges("{ [20,24) }"); | 2756 CheckExpectedRanges("{ [20,24) }"); |
2698 CheckExpectedBuffers(20, 24, &kDataB); | 2757 CheckExpectedBuffers(20, 24, &kDataB); |
2699 CheckNoNextBuffer(); | 2758 CheckNoNextBuffer(); |
2700 } | 2759 } |
2701 | 2760 |
| 2761 // Test GC preserves data starting at first GOP containing playback position. |
| 2762 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveDataAtPlaybackPosition) { |
| 2763 // Set memory limit to 30 buffers = 1 second of data. |
| 2764 SetMemoryLimit(30); |
| 2765 // And append 300 buffers = 10 seconds of data. |
| 2766 NewSegmentAppend(0, 300, &kDataA); |
| 2767 CheckExpectedRanges("{ [0,299) }"); |
| 2768 |
| 2769 // Playback position at 0, all data must be preserved. |
| 2770 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2771 DecodeTimestamp::FromMilliseconds(0), 0)); |
| 2772 CheckExpectedRanges("{ [0,299) }"); |
| 2773 |
| 2774 // Playback position at 1 sec, the first second of data [0,29) should be |
| 2775 // collected, since we are way over memory limit. |
| 2776 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2777 DecodeTimestamp::FromMilliseconds(1000), 0)); |
| 2778 CheckExpectedRanges("{ [30,299) }"); |
| 2779 |
| 2780 // Playback position at 1.1 sec, no new data can be collected, since the |
| 2781 // playback position is still in the first GOP of buffered data. |
| 2782 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2783 DecodeTimestamp::FromMilliseconds(1100), 0)); |
| 2784 CheckExpectedRanges("{ [30,299) }"); |
| 2785 |
| 2786 // Playback position at 5.166 sec, just at the very end of GOP corresponding |
| 2787 // to buffer range 150-155, which should be preserved. |
| 2788 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2789 DecodeTimestamp::FromMilliseconds(5166), 0)); |
| 2790 CheckExpectedRanges("{ [150,299) }"); |
| 2791 |
| 2792 // Playback position at 5.167 sec, just past the end of GOP corresponding to |
| 2793 // buffer range 150-155, it should be garbage collected now. |
| 2794 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2795 DecodeTimestamp::FromMilliseconds(5167), 0)); |
| 2796 CheckExpectedRanges("{ [155,299) }"); |
| 2797 |
| 2798 // Playback at 9.0 sec, we can now successfully collect all data except the |
| 2799 // last second and we are back under memory limit of 30 buffers, so GCIfNeeded |
| 2800 // should return true. |
| 2801 EXPECT_TRUE(stream_->GarbageCollectIfNeeded( |
| 2802 DecodeTimestamp::FromMilliseconds(9000), 0)); |
| 2803 CheckExpectedRanges("{ [270,299) }"); |
| 2804 |
| 2805 // Playback at 9.999 sec, GC succeeds, since we are under memory limit even |
| 2806 // without removing any data. |
| 2807 EXPECT_TRUE(stream_->GarbageCollectIfNeeded( |
| 2808 DecodeTimestamp::FromMilliseconds(9999), 0)); |
| 2809 CheckExpectedRanges("{ [270,299) }"); |
| 2810 |
| 2811 // Playback at 15 sec, this should never happen during regular playback in |
| 2812 // browser, since this position has no data buffered, but it should still |
| 2813 // cause no problems to GC algorithm, so test it just in case. |
| 2814 EXPECT_TRUE(stream_->GarbageCollectIfNeeded( |
| 2815 DecodeTimestamp::FromMilliseconds(15000), 0)); |
| 2816 CheckExpectedRanges("{ [270,299) }"); |
| 2817 } |
| 2818 |
2702 // Test saving the last GOP appended when this GOP is the only GOP in its range. | 2819 // Test saving the last GOP appended when this GOP is the only GOP in its range. |
2703 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP) { | 2820 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP) { |
2704 // Set memory limit to 3 and make sure the 4-byte GOP is not garbage | 2821 // Set memory limit to 3 and make sure the 4-byte GOP is not garbage |
2705 // collected. | 2822 // collected. |
2706 SetMemoryLimit(3); | 2823 SetMemoryLimit(3); |
2707 NewSegmentAppend("0K 30 60 90"); | 2824 NewSegmentAppend("0K 30 60 90"); |
| 2825 EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(0, 0)); |
2708 CheckExpectedRangesByTimestamp("{ [0,120) }"); | 2826 CheckExpectedRangesByTimestamp("{ [0,120) }"); |
2709 | 2827 |
2710 // Make sure you can continue appending data to this GOP; again, GC should not | 2828 // Make sure you can continue appending data to this GOP; again, GC should not |
2711 // wipe out anything. | 2829 // wipe out anything. |
2712 AppendBuffers("120D30"); | 2830 AppendBuffers("120D30"); |
| 2831 EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(0, 0)); |
2713 CheckExpectedRangesByTimestamp("{ [0,150) }"); | 2832 CheckExpectedRangesByTimestamp("{ [0,150) }"); |
2714 | 2833 |
2715 // Set memory limit to 100 and append a 2nd range after this without | 2834 // Append a 2nd range after this without triggering GC. |
2716 // triggering GC. | |
2717 SetMemoryLimit(100); | |
2718 NewSegmentAppend("200K 230 260 290K 320 350"); | 2835 NewSegmentAppend("200K 230 260 290K 320 350"); |
2719 CheckExpectedRangesByTimestamp("{ [0,150) [200,380) }"); | 2836 CheckExpectedRangesByTimestamp("{ [0,150) [200,380) }"); |
2720 | 2837 |
2721 // Seek to 290ms. | 2838 // Seek to 290ms. |
2722 SeekToTimestampMs(290); | 2839 SeekToTimestampMs(290); |
2723 | 2840 |
2724 // Now set memory limit to 3 and append a GOP in a separate range after the | 2841 // Now append a GOP in a separate range after the selected range and trigger |
2725 // selected range. Because it is after 290ms, this tests that the GOP is saved | 2842 // GC. Because it is after 290ms, this tests that the GOP is saved when |
2726 // when deleting from the back. | 2843 // deleting from the back. |
2727 SetMemoryLimit(3); | |
2728 NewSegmentAppend("500K 530 560 590"); | 2844 NewSegmentAppend("500K 530 560 590"); |
| 2845 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2846 DecodeTimestamp::FromMilliseconds(290), 0)); |
2729 | 2847 |
2730 // Should save GOP with 290ms and last GOP appended. | 2848 // Should save GOPs between 290ms and the last GOP appended. |
2731 CheckExpectedRangesByTimestamp("{ [290,380) [500,620) }"); | 2849 CheckExpectedRangesByTimestamp("{ [290,380) [500,620) }"); |
2732 | 2850 |
2733 // Continue appending to this GOP after GC. | 2851 // Continue appending to this GOP after GC. |
2734 AppendBuffers("620D30"); | 2852 AppendBuffers("620D30"); |
| 2853 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2854 DecodeTimestamp::FromMilliseconds(290), 0)); |
2735 CheckExpectedRangesByTimestamp("{ [290,380) [500,650) }"); | 2855 CheckExpectedRangesByTimestamp("{ [290,380) [500,650) }"); |
2736 } | 2856 } |
2737 | 2857 |
2738 // Test saving the last GOP appended when this GOP is in the middle of a | 2858 // Test saving the last GOP appended when this GOP is in the middle of a |
2739 // non-selected range. | 2859 // non-selected range. |
2740 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Middle) { | 2860 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Middle) { |
2741 // Append 3 GOPs starting at 0ms, 30ms apart. | 2861 // Append 3 GOPs starting at 0ms, 30ms apart. |
2742 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240"); | 2862 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240"); |
2743 CheckExpectedRangesByTimestamp("{ [0,270) }"); | 2863 CheckExpectedRangesByTimestamp("{ [0,270) }"); |
2744 | 2864 |
2745 // Now set the memory limit to 1 and overlap the middle of the range with a | 2865 // Now set the memory limit to 1 and overlap the middle of the range with a |
2746 // new GOP. | 2866 // new GOP. |
2747 SetMemoryLimit(1); | 2867 SetMemoryLimit(1); |
2748 NewSegmentAppend("80K 110 140"); | 2868 NewSegmentAppend("80K 110 140"); |
2749 | 2869 |
2750 // This whole GOP should be saved, and should be able to continue appending | 2870 // This whole GOP should be saved after GC, which will fail due to GOP being |
2751 // data to it. | 2871 // larger than 1 buffer |
| 2872 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2873 DecodeTimestamp::FromMilliseconds(80), 0)); |
2752 CheckExpectedRangesByTimestamp("{ [80,170) }"); | 2874 CheckExpectedRangesByTimestamp("{ [80,170) }"); |
| 2875 // We should still be able to continue appending data to GOP |
2753 AppendBuffers("170D30"); | 2876 AppendBuffers("170D30"); |
| 2877 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2878 DecodeTimestamp::FromMilliseconds(80), 0)); |
2754 CheckExpectedRangesByTimestamp("{ [80,200) }"); | 2879 CheckExpectedRangesByTimestamp("{ [80,200) }"); |
2755 | 2880 |
2756 // Set memory limit to 100 and append a 2nd range after this without | 2881 // Append a 2nd range after this range, without triggering GC. |
2757 // triggering GC. | |
2758 SetMemoryLimit(100); | |
2759 NewSegmentAppend("400K 430 460 490K 520 550 580K 610 640"); | 2882 NewSegmentAppend("400K 430 460 490K 520 550 580K 610 640"); |
2760 CheckExpectedRangesByTimestamp("{ [80,200) [400,670) }"); | 2883 CheckExpectedRangesByTimestamp("{ [80,200) [400,670) }"); |
2761 | 2884 |
2762 // Seek to 80ms to make the first range the selected range. | 2885 // Seek to 80ms to make the first range the selected range. |
2763 SeekToTimestampMs(80); | 2886 SeekToTimestampMs(80); |
2764 | 2887 |
2765 // Now set memory limit to 3 and append a GOP in the middle of the second | 2888 // Now append a GOP in the middle of the second range and trigger GC. Because |
2766 // range. Because it is after the selected range, this tests that the GOP is | 2889 // it is after the selected range, this tests that the GOP is saved when |
2767 // saved when deleting from the back. | 2890 // deleting from the back. |
2768 SetMemoryLimit(3); | |
2769 NewSegmentAppend("500K 530 560 590"); | 2891 NewSegmentAppend("500K 530 560 590"); |
| 2892 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2893 DecodeTimestamp::FromMilliseconds(80), 0)); |
2770 | 2894 |
2771 // Should save the GOP containing the seek point and GOP that was last | 2895 // Should save the GOPs between the seek point and GOP that was last appended |
2772 // appended. | 2896 CheckExpectedRangesByTimestamp("{ [80,200) [400,620) }"); |
2773 CheckExpectedRangesByTimestamp("{ [80,200) [500,620) }"); | |
2774 | 2897 |
2775 // Continue appending to this GOP after GC. | 2898 // Continue appending to this GOP after GC. |
2776 AppendBuffers("620D30"); | 2899 AppendBuffers("620D30"); |
2777 CheckExpectedRangesByTimestamp("{ [80,200) [500,650) }"); | 2900 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2901 DecodeTimestamp::FromMilliseconds(80), 0)); |
| 2902 CheckExpectedRangesByTimestamp("{ [80,200) [400,650) }"); |
2778 } | 2903 } |
2779 | 2904 |
2780 // Test saving the last GOP appended when the GOP containing the next buffer is | 2905 // Test saving the last GOP appended when the GOP containing the next buffer is |
2781 // adjacent to the last GOP appended. | 2906 // adjacent to the last GOP appended. |
2782 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected1) { | 2907 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected1) { |
2783 // Append 3 GOPs at 0ms, 90ms, and 180ms. | 2908 // Append 3 GOPs at 0ms, 90ms, and 180ms. |
2784 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240"); | 2909 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240"); |
2785 CheckExpectedRangesByTimestamp("{ [0,270) }"); | 2910 CheckExpectedRangesByTimestamp("{ [0,270) }"); |
2786 | 2911 |
2787 // Seek to the GOP at 90ms. | 2912 // Seek to the GOP at 90ms. |
2788 SeekToTimestampMs(90); | 2913 SeekToTimestampMs(90); |
2789 | 2914 |
2790 // Set the memory limit to 1, then overlap the GOP at 0. | 2915 // Set the memory limit to 1, then overlap the GOP at 0. |
2791 SetMemoryLimit(1); | 2916 SetMemoryLimit(1); |
2792 NewSegmentAppend("0K 30 60"); | 2917 NewSegmentAppend("0K 30 60"); |
2793 | 2918 |
2794 // Should save the GOP at 0ms and 90ms. | 2919 // GC should save the GOP at 0ms and 90ms, and will fail since GOP larger |
| 2920 // than 1 buffer |
| 2921 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2922 DecodeTimestamp::FromMilliseconds(90), 0)); |
2795 CheckExpectedRangesByTimestamp("{ [0,180) }"); | 2923 CheckExpectedRangesByTimestamp("{ [0,180) }"); |
2796 | 2924 |
2797 // Seek to 0 and check all buffers. | 2925 // Seek to 0 and check all buffers. |
2798 SeekToTimestampMs(0); | 2926 SeekToTimestampMs(0); |
2799 CheckExpectedBuffers("0K 30 60 90K 120 150"); | 2927 CheckExpectedBuffers("0K 30 60 90K 120 150"); |
2800 CheckNoNextBuffer(); | 2928 CheckNoNextBuffer(); |
2801 | 2929 |
2802 // Now seek back to 90ms and append a GOP at 180ms. | 2930 // Now seek back to 90ms and append a GOP at 180ms. |
2803 SeekToTimestampMs(90); | 2931 SeekToTimestampMs(90); |
2804 NewSegmentAppend("180K 210 240"); | 2932 NewSegmentAppend("180K 210 240"); |
2805 | 2933 |
2806 // Should save the GOP at 90ms and the GOP at 180ms. | 2934 // Should save the GOP at 90ms and the GOP at 180ms. |
| 2935 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2936 DecodeTimestamp::FromMilliseconds(90), 0)); |
2807 CheckExpectedRangesByTimestamp("{ [90,270) }"); | 2937 CheckExpectedRangesByTimestamp("{ [90,270) }"); |
2808 CheckExpectedBuffers("90K 120 150 180K 210 240"); | 2938 CheckExpectedBuffers("90K 120 150 180K 210 240"); |
2809 CheckNoNextBuffer(); | 2939 CheckNoNextBuffer(); |
2810 } | 2940 } |
2811 | 2941 |
2812 // Test saving the last GOP appended when it is at the beginning or end of the | 2942 // Test saving the last GOP appended when it is at the beginning or end of the |
2813 // selected range. This tests when the last GOP appended is before or after the | 2943 // selected range. This tests when the last GOP appended is before or after the |
2814 // GOP containing the next buffer, but not directly adjacent to this GOP. | 2944 // GOP containing the next buffer, but not directly adjacent to this GOP. |
2815 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected2) { | 2945 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected2) { |
2816 // Append 4 GOPs starting at positions 0ms, 90ms, 180ms, 270ms. | 2946 // Append 4 GOPs starting at positions 0ms, 90ms, 180ms, 270ms. |
2817 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330"); | 2947 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330"); |
2818 CheckExpectedRangesByTimestamp("{ [0,360) }"); | 2948 CheckExpectedRangesByTimestamp("{ [0,360) }"); |
2819 | 2949 |
2820 // Seek to the last GOP at 270ms. | 2950 // Seek to the last GOP at 270ms. |
2821 SeekToTimestampMs(270); | 2951 SeekToTimestampMs(270); |
2822 | 2952 |
2823 // Set the memory limit to 1, then overlap the GOP at 90ms. | 2953 // Set the memory limit to 1, then overlap the GOP at 90ms. |
2824 SetMemoryLimit(1); | 2954 SetMemoryLimit(1); |
2825 NewSegmentAppend("90K 120 150"); | 2955 NewSegmentAppend("90K 120 150"); |
2826 | 2956 |
2827 // Should save the GOP at 90ms and the GOP at 270ms. | 2957 // GC will save data in the range where the most recent append has happened |
2828 CheckExpectedRangesByTimestamp("{ [90,180) [270,360) }"); | 2958 // [0; 180) and the range where the next read position is [270;360) |
| 2959 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2960 DecodeTimestamp::FromMilliseconds(270), 0)); |
| 2961 CheckExpectedRangesByTimestamp("{ [0,180) [270,360) }"); |
2829 | 2962 |
2830 // Set memory limit to 100 and add 3 GOPs to the end of the selected range | 2963 // Add 3 GOPs to the end of the selected range at 360ms, 450ms, and 540ms. |
2831 // at 360ms, 450ms, and 540ms. | |
2832 SetMemoryLimit(100); | |
2833 NewSegmentAppend("360K 390 420 450K 480 510 540K 570 600"); | 2964 NewSegmentAppend("360K 390 420 450K 480 510 540K 570 600"); |
2834 CheckExpectedRangesByTimestamp("{ [90,180) [270,630) }"); | 2965 CheckExpectedRangesByTimestamp("{ [0,180) [270,630) }"); |
2835 | 2966 |
2836 // Constrain the memory limit again and overlap the GOP at 450ms to test | 2967 // Overlap the GOP at 450ms and garbage collect to test deleting from the |
2837 // deleting from the back. | 2968 // back. |
2838 SetMemoryLimit(1); | |
2839 NewSegmentAppend("450K 480 510"); | 2969 NewSegmentAppend("450K 480 510"); |
| 2970 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2971 DecodeTimestamp::FromMilliseconds(270), 0)); |
2840 | 2972 |
2841 // Should save GOP at 270ms and the GOP at 450ms. | 2973 // Should save GOPs from GOP at 270ms to GOP at 450ms. |
2842 CheckExpectedRangesByTimestamp("{ [270,360) [450,540) }"); | 2974 CheckExpectedRangesByTimestamp("{ [270,540) }"); |
2843 } | 2975 } |
2844 | 2976 |
2845 // Test saving the last GOP appended when it is the same as the GOP containing | 2977 // Test saving the last GOP appended when it is the same as the GOP containing |
2846 // the next buffer. | 2978 // the next buffer. |
2847 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected3) { | 2979 TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected3) { |
2848 // Seek to start of stream. | 2980 // Seek to start of stream. |
2849 SeekToTimestampMs(0); | 2981 SeekToTimestampMs(0); |
2850 | 2982 |
2851 // Append 3 GOPs starting at 0ms, 90ms, 180ms. | 2983 // Append 3 GOPs starting at 0ms, 90ms, 180ms. |
2852 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240"); | 2984 NewSegmentAppend("0K 30 60 90K 120 150 180K 210 240"); |
2853 CheckExpectedRangesByTimestamp("{ [0,270) }"); | 2985 CheckExpectedRangesByTimestamp("{ [0,270) }"); |
2854 | 2986 |
2855 // Set the memory limit to 1 then begin appending the start of a GOP starting | 2987 // Set the memory limit to 1 then begin appending the start of a GOP starting |
2856 // at 0ms. | 2988 // at 0ms. |
2857 SetMemoryLimit(1); | 2989 SetMemoryLimit(1); |
2858 NewSegmentAppend("0K 30"); | 2990 NewSegmentAppend("0K 30"); |
2859 | 2991 |
2860 // Should save the newly appended GOP, which is also the next GOP that will be | 2992 // GC should save the newly appended GOP, which is also the next GOP that |
2861 // returned from the seek request. | 2993 // will be returned from the seek request. |
| 2994 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 2995 DecodeTimestamp::FromMilliseconds(0), 0)); |
2862 CheckExpectedRangesByTimestamp("{ [0,60) }"); | 2996 CheckExpectedRangesByTimestamp("{ [0,60) }"); |
2863 | 2997 |
2864 // Check the buffers in the range. | 2998 // Check the buffers in the range. |
2865 CheckExpectedBuffers("0K 30"); | 2999 CheckExpectedBuffers("0K 30"); |
2866 CheckNoNextBuffer(); | 3000 CheckNoNextBuffer(); |
2867 | 3001 |
2868 // Continue appending to this buffer. | 3002 // Continue appending to this buffer. |
2869 AppendBuffers("60 90"); | 3003 AppendBuffers("60 90"); |
2870 | 3004 |
2871 // Should still save the rest of this GOP and should be able to fulfill the | 3005 // GC should still save the rest of this GOP and should be able to fulfill |
2872 // read. | 3006 // the read. |
| 3007 EXPECT_FALSE(stream_->GarbageCollectIfNeeded( |
| 3008 DecodeTimestamp::FromMilliseconds(0), 0)); |
2873 CheckExpectedRangesByTimestamp("{ [0,120) }"); | 3009 CheckExpectedRangesByTimestamp("{ [0,120) }"); |
2874 CheckExpectedBuffers("60 90"); | 3010 CheckExpectedBuffers("60 90"); |
2875 CheckNoNextBuffer(); | 3011 CheckNoNextBuffer(); |
2876 } | 3012 } |
2877 | 3013 |
2878 // Currently disabled because of bug: crbug.com/140875. | 3014 // Currently disabled because of bug: crbug.com/140875. |
2879 TEST_F(SourceBufferStreamTest, DISABLED_GarbageCollection_WaitingForKeyframe) { | 3015 TEST_F(SourceBufferStreamTest, DISABLED_GarbageCollection_WaitingForKeyframe) { |
2880 // Set memory limit to 10 buffers. | 3016 // Set memory limit to 10 buffers. |
2881 SetMemoryLimit(10); | 3017 SetMemoryLimit(10); |
2882 | 3018 |
(...skipping 1553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4436 CheckNoNextBuffer(); | 4572 CheckNoNextBuffer(); |
4437 } | 4573 } |
4438 | 4574 |
4439 // TODO(vrk): Add unit tests where keyframes are unaligned between streams. | 4575 // TODO(vrk): Add unit tests where keyframes are unaligned between streams. |
4440 // (crbug.com/133557) | 4576 // (crbug.com/133557) |
4441 | 4577 |
4442 // TODO(vrk): Add unit tests with end of stream being called at interesting | 4578 // TODO(vrk): Add unit tests with end of stream being called at interesting |
4443 // times. | 4579 // times. |
4444 | 4580 |
4445 } // namespace media | 4581 } // namespace media |
OLD | NEW |