-
Notifications
You must be signed in to change notification settings - Fork 527
Adds support for fragmented mp4 with multiple sidx boxes #2186
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Open
anthonybajoua
wants to merge
6
commits into
androidx:main
Choose a base branch
from
anthonybajoua:meta/fragmented-mp4-multiple-sidx-seek-squashed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ef35943
Adds support for fragmented mp4 with multiple sidx boxes
anthonybajoua 0d3227c
Merge remote-tracking branch 'origin/main' into meta/fragmented-mp4-m…
anthonybajoua a728a36
Refactored and added Javadoc
rohitjoins 8b681a8
Changes based on internal review
rohitjoins 2b9d7dc
More changes based on the internal review
rohitjoins 0d55dd3
Merge branch 'main' into meta/fragmented-mp4-multiple-sidx-seek-squashed
rohitjoins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
libraries/extractor/src/main/java/androidx/media3/extractor/ChunkIndexMerger.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2025 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package androidx.media3.extractor; | ||
|
||
import androidx.media3.common.util.UnstableApi; | ||
import com.google.common.primitives.Ints; | ||
import com.google.common.primitives.Longs; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* A utility class for merging multiple {@link ChunkIndex} instances into a single {@link | ||
* ChunkIndex}. | ||
* | ||
* <p>This is useful in scenarios where media is split across multiple segments or sources, and a | ||
* unified index is needed for seeking or playback. | ||
*/ | ||
@UnstableApi | ||
public final class ChunkIndexMerger { | ||
|
||
/** Start time in microseconds to {@link ChunkIndex} mapping. Maintains insertion order. */ | ||
private final Map<Long, ChunkIndex> chunkMap; | ||
|
||
/** Creates an instance. */ | ||
public ChunkIndexMerger() { | ||
this.chunkMap = new LinkedHashMap<>(); | ||
} | ||
|
||
/** | ||
* Adds a {@link ChunkIndex} to be merged. | ||
* | ||
* <p>Chunk indices with duplicate starting timestamps are ignored to avoid redundant data. | ||
* | ||
* @param chunk The {@link ChunkIndex} to add. | ||
*/ | ||
public void add(ChunkIndex chunk) { | ||
if (chunk.timesUs.length > 0 && !chunkMap.containsKey(chunk.timesUs[0])) { | ||
chunkMap.put(chunk.timesUs[0], chunk); | ||
} | ||
} | ||
|
||
/** Returns a single {@link ChunkIndex} that merges all added chunk indices. */ | ||
public ChunkIndex merge() { | ||
List<int[]> sizesList = new ArrayList<>(); | ||
List<long[]> offsetsList = new ArrayList<>(); | ||
List<long[]> durationsList = new ArrayList<>(); | ||
List<long[]> timesList = new ArrayList<>(); | ||
|
||
for (ChunkIndex chunk : chunkMap.values()) { | ||
sizesList.add(chunk.sizes); | ||
offsetsList.add(chunk.offsets); | ||
durationsList.add(chunk.durationsUs); | ||
timesList.add(chunk.timesUs); | ||
} | ||
|
||
return new ChunkIndex( | ||
Ints.concat(sizesList.toArray(new int[sizesList.size()][])), | ||
Longs.concat(offsetsList.toArray(new long[offsetsList.size()][])), | ||
Longs.concat(durationsList.toArray(new long[durationsList.size()][])), | ||
Longs.concat(timesList.toArray(new long[timesList.size()][]))); | ||
} | ||
|
||
/** Clears all added chunk indices and internal state. */ | ||
public void clear() { | ||
chunkMap.clear(); | ||
} | ||
|
||
/** Returns the number of chunk indices added so far. */ | ||
public int size() { | ||
return chunkMap.size(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're still investigating the use of
finally
block here. Extractor should be able to restart from an IOException without having this. Removing this block results in test failures with IO Errors simulation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is due to the reset of the seek position being set and not cleared during an IO Error.
https://gist.github.com/anthonybajoua/c85a786a30f9876c82849544f848f190
See the above revision for a much cleaner way to achieve this.