47
47
import androidx .media3 .extractor .Ac4Util ;
48
48
import androidx .media3 .extractor .CeaUtil ;
49
49
import androidx .media3 .extractor .ChunkIndex ;
50
+ import androidx .media3 .extractor .ChunkIndexMerger ;
50
51
import androidx .media3 .extractor .Extractor ;
51
52
import androidx .media3 .extractor .ExtractorInput ;
52
53
import androidx .media3 .extractor .ExtractorOutput ;
53
54
import androidx .media3 .extractor .ExtractorsFactory ;
54
55
import androidx .media3 .extractor .GaplessInfoHolder ;
55
- import androidx .media3 .extractor .MergedChunkIndex ;
56
56
import androidx .media3 .extractor .PositionHolder ;
57
57
import androidx .media3 .extractor .SeekMap ;
58
58
import androidx .media3 .extractor .SniffFailure ;
@@ -224,7 +224,7 @@ public static ExtractorsFactory newFactory(SubtitleParser.Factory subtitleParser
224
224
private final ReorderingSeiMessageQueue reorderingSeiMessageQueue ;
225
225
@ Nullable private final TrackOutput additionalEmsgTrackOutput ;
226
226
227
- private final MergedChunkIndex mergedChunkIndex ;
227
+ private final ChunkIndexMerger chunkIndexMerger ;
228
228
229
229
private ImmutableList <SniffFailure > lastSniffFailures ;
230
230
private int parserState ;
@@ -439,7 +439,7 @@ public FragmentedMp4Extractor(
439
439
new ReorderingSeiMessageQueue (
440
440
(presentationTimeUs , seiBuffer ) ->
441
441
CeaUtil .consume (presentationTimeUs , seiBuffer , ceaTrackOutputs ));
442
- mergedChunkIndex = new MergedChunkIndex ();
442
+ chunkIndexMerger = new ChunkIndexMerger ();
443
443
seekPositionBeforeSidxProcessing = C .INDEX_UNSET ;
444
444
}
445
445
@@ -524,30 +524,37 @@ public void release() {
524
524
525
525
@ Override
526
526
public int read (ExtractorInput input , PositionHolder seekPosition ) throws IOException {
527
- while (true ) {
528
- switch (parserState ) {
529
- case STATE_READING_ATOM_HEADER :
530
- if (!readAtomHeader (input , /* skipPayloadParsing= */ false )) {
531
- if (seekPositionBeforeSidxProcessing != C .INDEX_UNSET ) {
532
- seekPosition .position = seekPositionBeforeSidxProcessing ;
533
- seekPositionBeforeSidxProcessing = C .INDEX_UNSET ;
534
- return Extractor .RESULT_SEEK ;
535
- } else {
536
- reorderingSeiMessageQueue .flush ();
537
- return Extractor .RESULT_END_OF_INPUT ;
527
+ try {
528
+ while (true ) {
529
+ switch (parserState ) {
530
+ case STATE_READING_ATOM_HEADER :
531
+ if (!readAtomHeader (input , /* skipPayloadParsing= */ false )) {
532
+ if (seekPositionBeforeSidxProcessing != C .INDEX_UNSET ) {
533
+ seekPosition .position = seekPositionBeforeSidxProcessing ;
534
+ seekPositionBeforeSidxProcessing = C .INDEX_UNSET ;
535
+ return Extractor .RESULT_SEEK ;
536
+ } else {
537
+ reorderingSeiMessageQueue .flush ();
538
+ return Extractor .RESULT_END_OF_INPUT ;
539
+ }
538
540
}
539
- }
540
- break ;
541
- case STATE_READING_ATOM_PAYLOAD :
542
- readAtomPayload (input );
543
- break ;
544
- case STATE_READING_ENCRYPTION_DATA :
545
- readEncryptionData (input );
546
- break ;
547
- default :
548
- if (readSample (input )) {
549
- return RESULT_CONTINUE ;
550
- }
541
+ break ;
542
+ case STATE_READING_ATOM_PAYLOAD :
543
+ readAtomPayload (input );
544
+ break ;
545
+ case STATE_READING_ENCRYPTION_DATA :
546
+ readEncryptionData (input );
547
+ break ;
548
+ default :
549
+ if (readSample (input )) {
550
+ return RESULT_CONTINUE ;
551
+ }
552
+ }
553
+ }
554
+ } finally {
555
+ if (seekPositionBeforeSidxProcessing != C .INDEX_UNSET ) {
556
+ seekPosition .position = seekPositionBeforeSidxProcessing ;
557
+ seekPositionBeforeSidxProcessing = C .INDEX_UNSET ;
551
558
}
552
559
}
553
560
}
@@ -683,16 +690,21 @@ private void onLeafAtomRead(LeafBox leaf, ExtractorInput input) throws IOExcepti
683
690
} else if (leaf .type == Mp4Box .TYPE_sidx ) {
684
691
long inputPosition = input .getPosition ();
685
692
Pair <Long , ChunkIndex > result = parseSidx (leaf .data , inputPosition );
686
- mergedChunkIndex . merge (result .second );
693
+ chunkIndexMerger . add (result .second );
687
694
if (!haveOutputSeekMap ) {
688
695
segmentIndexEarliestPresentationTimeUs = result .first ;
689
696
extractorOutput .seekMap (result .second );
690
697
haveOutputSeekMap = true ;
691
- } else if (!haveOutputCompleteSeekMap && mergedChunkIndex .size () > 1 ) {
698
+ } else if ((flags & FLAG_MERGE_FRAGMENTED_SIDX ) != 0
699
+ && !haveOutputCompleteSeekMap
700
+ && chunkIndexMerger .size () > 1 ) {
692
701
seekPositionBeforeSidxProcessing = inputPosition ;
693
- processRemainingSidxAtoms (input );
694
- extractorOutput .seekMap (mergedChunkIndex .toChunkIndex ());
695
- haveOutputCompleteSeekMap = true ;
702
+ try {
703
+ processRemainingSidxAtoms (input );
704
+ haveOutputCompleteSeekMap = true ;
705
+ } finally {
706
+ extractorOutput .seekMap (chunkIndexMerger .merge ());
707
+ }
696
708
}
697
709
} else if (leaf .type == Mp4Box .TYPE_emsg ) {
698
710
onEmsgLeafAtomRead (leaf .data );
@@ -703,14 +715,14 @@ private void processRemainingSidxAtoms(ExtractorInput input) throws IOException
703
715
enterReadingAtomHeaderState ();
704
716
while (readAtomHeader (input , /* skipPayloadParsing= */ true )) {
705
717
if (atomType == Mp4Box .TYPE_sidx ) {
706
- ParsableByteArray inputArray = new ParsableByteArray ((int ) atomSize );
707
- System .arraycopy (atomHeader .getData (), 0 , inputArray .getData (), 0 , Mp4Box .HEADER_SIZE );
718
+ scratch . reset ((int ) atomSize );
719
+ System .arraycopy (atomHeader .getData (), 0 , scratch .getData (), 0 , Mp4Box .HEADER_SIZE );
708
720
input .readFully (
709
- inputArray .getData (), Mp4Box .HEADER_SIZE , (int ) (atomSize - atomHeaderBytesRead ));
721
+ scratch .getData (), Mp4Box .HEADER_SIZE , (int ) (atomSize - atomHeaderBytesRead ));
710
722
711
- LeafBox sidxBox = new LeafBox (Mp4Box .TYPE_sidx , inputArray );
723
+ LeafBox sidxBox = new LeafBox (Mp4Box .TYPE_sidx , scratch );
712
724
Pair <Long , ChunkIndex > result = parseSidx (sidxBox .data , input .getPeekPosition ());
713
- mergedChunkIndex . merge (result .second );
725
+ chunkIndexMerger . add (result .second );
714
726
} else {
715
727
input .skipFully ((int ) (atomSize - atomHeaderBytesRead ), /* allowEndOfInput= */ true );
716
728
}
0 commit comments