<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>314814</bug_id>
          
          <creation_ts>2026-05-14 05:35:53 -0700</creation_ts>
          <short_desc>[MSE] SourceBuffer.remove() incorrectly removes one extra sample, and buffered trims coverage still backed by retained samples</short_desc>
          <delta_ts>2026-05-17 17:32:45 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Media</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Jean-Yves Avenard [:jya]">jean-yves.avenard</reporter>
          <assigned_to name="Jean-Yves Avenard [:jya]">jean-yves.avenard</assigned_to>
          <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2210872</commentid>
    <comment_count>0</comment_count>
    <who name="Jean-Yves Avenard [:jya]">jean-yves.avenard</who>
    <bug_when>2026-05-14 05:35:53 -0700</bug_when>
    <thetext>Consider the following:

Sample map m_samples.presentationOrder():

```
┌─────┬──────┬──────┬──────┬──────────────┬──────┐
│  #  │ PTS  │ DTS  │ Dur  │    Range     │ Sync │
├─────┼──────┼──────┼──────┼──────────────┼──────┤
│ 1   │ 0.00 │ 0.00 │ 1.00 │ [0.00, 1.00) │ ✓    │
├─────┼──────┼──────┼──────┼──────────────┼──────┤
│ 2   │ 1.00 │ 1.00 │ 1.00 │ [1.00, 2.00) │      │
├─────┼──────┼──────┼──────┼──────────────┼──────┤
│ 3   │ 2.00 │ 2.00 │ 1.00 │ [2.00, 3.00) │      │
├─────┼──────┼──────┼──────┼──────────────┼──────┤
│ 4   │ 3.00 │ 3.00 │ 1.00 │ [3.00, 4.00) │      │
├─────┼──────┼──────┼──────┼──────────────┼──────┤
│ 5   │ 3.96 │ 3.96 │ 0.33 │ [3.96, 4.29) │ ✓    │
├─────┼──────┼──────┼──────┼──────────────┼──────┤
│ 6   │ 4.29 │ 4.29 │ 0.33 │ [4.29, 4.62) │      │
├─────┼──────┼──────┼──────┼──────────────┼──────┤
│ 7   │ 4.62 │ 4.62 │ 0.33 │ [4.62, 4.95) │      │
├─────┼──────┼──────┼──────┼──────────────┼──────┤
│ 8   │ 4.95 │ 4.95 │ 0.33 │ [4.95, 5.28) │      │
└─────┴──────┴──────┴──────┴──────────────┴──────┘
```

m_buffered = [0.00, 5.28) (ranges merge via AddTimeRangeOption::EliminateSmallGaps in addSample at
TrackBuffer.cpp:150).

Samples #4 and #5 overlap in presentation time at [3.96, 4.00). (We established earlier this is a spec-legal state —
SourceBufferPrivate.cpp:1389 only removes existing samples whose PTS falls in the new sample&apos;s range, not whose range
falls in the new sample&apos;s range; with new PTS=3.96 and existing PTS=3.00 nothing is removed.)

---
Operation: sourceBuffer.remove(0, 3.96)

Entering TrackBuffer::removeCodedFrames(start=0, end=3.96) at TrackBuffer.cpp:408.

Lines 430-431 — split-at-time attempts. Both return empty (mock samples are non-divisible).

Lines 433-436 — iterator selection:
auto removePresentationStart = m_samples.presentationOrder().findSampleStartingOnOrAfterPresentationTime(start);  //
lower_bound(0) → #1
auto removePresentationEnd   = m_samples.presentationOrder().findSampleStartingOnOrAfterPresentationTime(end);    //
lower_bound(3.96) → #5
Presentation range [#1, #5) = {#1, #2, #3, #4}. Sample #5 is NOT selected (PTS=3.96 is not &lt; 3.96). Spec-correct.

Lines 445-452 — decode-order extraction:
- Min DTS in selection = 0 (#1), Max DTS = 3 (#4).
- findSyncSampleAfterDecodeIterator(#4) → next sync after DTS=3 is DTS=3.96 → #5.
- erasedSamples = decode-order [#1, #5) = {#1, #2, #3, #4}.

Line 454 — call removeSamples(erasedSamples, ...) at TrackBuffer.cpp:319.

Inside removeSamples:

Lines 333-359 — sample removal + erasedRanges build:
- Remove #1: erasedRanges.add(0.00, 1.00, EliminateSmallGaps) → erasedRanges = [0, 1).
- Remove #2: add → [0, 2).
- Remove #3: add → [0, 3).
- Remove #4: add [3.00, 4.00) → erasedRanges = [0.00, 4.00).

After removal, sample map = {#5, #6, #7, #8}.

Lines 365-391 — padding loop, one iteration for the single range [0, 4):

auto erasedStart = 0.00;
auto erasedEnd   = 4.00;

// Front
auto startIterator = m_samples.presentationOrder().reverseFindSampleBeforePresentationTime(0);
if (startIterator == rend())   // true — no retained sample has PTS &lt; 0
    additionalErasedRanges.add(MediaTime::zeroTime(), 0);   // add(0, 0) — no-op

// Back
auto endIterator = m_samples.presentationOrder().findSampleStartingAfterPresentationTime(erasedStart);  //
upper_bound(0) = #5
// endIterator != end()
Ref nextSample = endIterator-&gt;second.get();   // #5
if (nextSample-&gt;presentationTime() &gt; erasedEnd)         // 3.96 &gt; 4 ? NO — branch skipped
    additionalErasedRanges.add(erasedEnd, nextSample-&gt;presentationTime());

Neither branch in the back-side else fires — the &gt; check doesn&apos;t cover the &lt; case. additionalErasedRanges stays empty.

erasedRanges returned unchanged: [0.00, 4.00).

Lines 467-468 back in removeCodedFrames:
erasedRanges.invert();                       // (-∞, 0) ∪ [4.00, +∞)
m_buffered.intersectWith(erasedRanges);      // [0, 5.28) ∩ above = [4.00, 5.28)

---
Final state (current code)

- m_samples = {#5, #6, #7, #8} (samples #5&apos;s range is still [3.96, 4.29)).
- m_buffered = [4.00, 5.28).

Why this is incorrect

Union of retained samples&apos; ranges:
#5 [3.96, 4.29) ∪ #6 [4.29, 4.62) ∪ #7 [4.62, 4.95) ∪ #8 [4.95, 5.28) = [3.96, 5.28)

m_buffered should equal this union, but instead reports [4.00, 5.28). The [3.96, 4.00) region is missing from
m_buffered even though sample #5 still covers it.

The root cause: erasedRanges is the union of removed samples&apos; full [PT, PT+dur) intervals = [0, 4). That union
includes [3.96, 4.00) (from removed #4&apos;s range extending to 4.00), but [3.96, 4.00) is also covered by retained #5 —
so subtracting erasedRanges from m_buffered trims buffered coverage that is still backed by a sample.

The padding loop at lines 381-388 already consults endIterator (the next retained sample after erasedStart) and
handles one case — when its PTS is greater than erasedEnd (gap). The other side of the comparison — PTS less than
erasedEnd (overlap) — never appears. That&apos;s the missing code.

This creates gaps that will cause playback to stall sometimes should the JS player remove + re-append samples from [0-3.96] as the buffered range would now be [0, 3.96] [4, 5.28]</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2210873</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2026-05-14 05:36:01 -0700</bug_when>
    <thetext>&lt;rdar://problem/177065364&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2211026</commentid>
    <comment_count>2</comment_count>
    <who name="Jean-Yves Avenard [:jya]">jean-yves.avenard</who>
    <bug_when>2026-05-14 15:29:59 -0700</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/64959</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2211585</commentid>
    <comment_count>3</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2026-05-17 17:32:44 -0700</bug_when>
    <thetext>Committed 313383@main (21a3e6b45c05): &lt;https://commits.webkit.org/313383@main&gt;

Reviewed commits have been landed. Closing PR #64959 and removing active labels.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>