Search in sources :

Example 11 with Extractor

use of androidx.media3.extractor.Extractor in project media by androidx.

the class ContainerMediaChunk method load.

@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public final void load() throws IOException {
    if (nextLoadPosition == 0) {
        // Configure the output and set it as the target for the extractor wrapper.
        BaseMediaChunkOutput output = getOutput();
        output.setSampleOffsetUs(sampleOffsetUs);
        chunkExtractor.init(getTrackOutputProvider(output), clippedStartTimeUs == C.TIME_UNSET ? C.TIME_UNSET : (clippedStartTimeUs - sampleOffsetUs), clippedEndTimeUs == C.TIME_UNSET ? C.TIME_UNSET : (clippedEndTimeUs - sampleOffsetUs));
    }
    try {
        // Create and open the input.
        DataSpec loadDataSpec = dataSpec.subrange(nextLoadPosition);
        ExtractorInput input = new DefaultExtractorInput(dataSource, loadDataSpec.position, dataSource.open(loadDataSpec));
        // Load and decode the sample data.
        try {
            while (!loadCanceled && chunkExtractor.read(input)) {
            }
        } finally {
            nextLoadPosition = input.getPosition() - dataSpec.position;
        }
    } finally {
        DataSourceUtil.closeQuietly(dataSource);
    }
    loadCompleted = !loadCanceled;
}
Also used : DefaultExtractorInput(androidx.media3.extractor.DefaultExtractorInput) ExtractorInput(androidx.media3.extractor.ExtractorInput) DataSpec(androidx.media3.datasource.DataSpec) DefaultExtractorInput(androidx.media3.extractor.DefaultExtractorInput)

Example 12 with Extractor

use of androidx.media3.extractor.Extractor in project media by androidx.

the class DefaultHlsExtractorFactory method createExtractor.

@Override
public BundledHlsMediaChunkExtractor createExtractor(Uri uri, Format format, @Nullable List<Format> muxedCaptionFormats, TimestampAdjuster timestampAdjuster, Map<String, List<String>> responseHeaders, ExtractorInput sniffingExtractorInput, PlayerId playerId) throws IOException {
    @FileTypes.Type int formatInferredFileType = FileTypes.inferFileTypeFromMimeType(format.sampleMimeType);
    @FileTypes.Type int responseHeadersInferredFileType = FileTypes.inferFileTypeFromResponseHeaders(responseHeaders);
    @FileTypes.Type int uriInferredFileType = FileTypes.inferFileTypeFromUri(uri);
    // Defines the order in which to try the extractors.
    List<Integer> fileTypeOrder = new ArrayList<>(/* initialCapacity= */
    DEFAULT_EXTRACTOR_ORDER.length);
    addFileTypeIfValidAndNotPresent(formatInferredFileType, fileTypeOrder);
    addFileTypeIfValidAndNotPresent(responseHeadersInferredFileType, fileTypeOrder);
    addFileTypeIfValidAndNotPresent(uriInferredFileType, fileTypeOrder);
    for (int fileType : DEFAULT_EXTRACTOR_ORDER) {
        addFileTypeIfValidAndNotPresent(fileType, fileTypeOrder);
    }
    // Extractor to be used if the type is not recognized.
    @Nullable Extractor fallBackExtractor = null;
    sniffingExtractorInput.resetPeekPosition();
    for (int i = 0; i < fileTypeOrder.size(); i++) {
        int fileType = fileTypeOrder.get(i);
        Extractor extractor = checkNotNull(createExtractorByFileType(fileType, format, muxedCaptionFormats, timestampAdjuster));
        if (sniffQuietly(extractor, sniffingExtractorInput)) {
            return new BundledHlsMediaChunkExtractor(extractor, format, timestampAdjuster);
        }
        if (fallBackExtractor == null && (fileType == formatInferredFileType || fileType == responseHeadersInferredFileType || fileType == uriInferredFileType || fileType == FileTypes.TS)) {
            // If sniffing fails, fallback to the file types inferred from context. If all else fails,
            // fallback to Transport Stream. See https://github.com/google/ExoPlayer/issues/8219.
            fallBackExtractor = extractor;
        }
    }
    return new BundledHlsMediaChunkExtractor(checkNotNull(fallBackExtractor), format, timestampAdjuster);
}
Also used : ArrayList(java.util.ArrayList) Ac4Extractor(androidx.media3.extractor.ts.Ac4Extractor) Extractor(androidx.media3.extractor.Extractor) TsExtractor(androidx.media3.extractor.ts.TsExtractor) AdtsExtractor(androidx.media3.extractor.ts.AdtsExtractor) Mp3Extractor(androidx.media3.extractor.mp3.Mp3Extractor) Ac3Extractor(androidx.media3.extractor.ts.Ac3Extractor) FragmentedMp4Extractor(androidx.media3.extractor.mp4.FragmentedMp4Extractor) SuppressLint(android.annotation.SuppressLint) Nullable(androidx.annotation.Nullable)

Example 13 with Extractor

use of androidx.media3.extractor.Extractor in project media by androidx.

the class HlsSampleStreamWrapper method buildTracksFromSampleStreams.

/**
 * Builds tracks that are exposed by this {@link HlsSampleStreamWrapper} instance, as well as
 * internal data-structures required for operation.
 *
 * <p>Tracks in HLS are complicated. A HLS multivariant playlist contains a number of "variants".
 * Each variant stream typically contains muxed video, audio and (possibly) additional audio,
 * metadata and caption tracks. We wish to allow the user to select between an adaptive track that
 * spans all variants, as well as each individual variant. If multiple audio tracks are present
 * within each variant then we wish to allow the user to select between those also.
 *
 * <p>To do this, tracks are constructed as follows. The {@link HlsChunkSource} exposes (N+1)
 * tracks, where N is the number of variants defined in the HLS multivariant playlist. These
 * consist of one adaptive track defined to span all variants and a track for each individual
 * variant. The adaptive track is initially selected. The extractor is then prepared to discover
 * the tracks inside of each variant stream. The two sets of tracks are then combined by this
 * method to create a third set, which is the set exposed by this {@link HlsSampleStreamWrapper}:
 *
 * <ul>
 *   <li>The extractor tracks are inspected to infer a "primary" track type. If a video track is
 *       present then it is always the primary type. If not, audio is the primary type if present.
 *       Else text is the primary type if present. Else there is no primary type.
 *   <li>If there is exactly one extractor track of the primary type, it's expanded into (N+1)
 *       exposed tracks, all of which correspond to the primary extractor track and each of which
 *       corresponds to a different chunk source track. Selecting one of these tracks has the
 *       effect of switching the selected track on the chunk source.
 *   <li>All other extractor tracks are exposed directly. Selecting one of these tracks has the
 *       effect of selecting an extractor track, leaving the selected track on the chunk source
 *       unchanged.
 * </ul>
 */
@EnsuresNonNull({ "trackGroups", "optionalTrackGroups", "trackGroupToSampleQueueIndex" })
private void buildTracksFromSampleStreams() {
    // Iterate through the extractor tracks to discover the "primary" track type, and the index
    // of the single track of this type.
    int primaryExtractorTrackType = C.TRACK_TYPE_NONE;
    int primaryExtractorTrackIndex = C.INDEX_UNSET;
    int extractorTrackCount = sampleQueues.length;
    for (int i = 0; i < extractorTrackCount; i++) {
        @Nullable String sampleMimeType = Assertions.checkStateNotNull(sampleQueues[i].getUpstreamFormat()).sampleMimeType;
        int trackType;
        if (MimeTypes.isVideo(sampleMimeType)) {
            trackType = C.TRACK_TYPE_VIDEO;
        } else if (MimeTypes.isAudio(sampleMimeType)) {
            trackType = C.TRACK_TYPE_AUDIO;
        } else if (MimeTypes.isText(sampleMimeType)) {
            trackType = C.TRACK_TYPE_TEXT;
        } else {
            trackType = C.TRACK_TYPE_NONE;
        }
        if (getTrackTypeScore(trackType) > getTrackTypeScore(primaryExtractorTrackType)) {
            primaryExtractorTrackType = trackType;
            primaryExtractorTrackIndex = i;
        } else if (trackType == primaryExtractorTrackType && primaryExtractorTrackIndex != C.INDEX_UNSET) {
            // We have multiple tracks of the primary type. We only want an index if there only exists a
            // single track of the primary type, so unset the index again.
            primaryExtractorTrackIndex = C.INDEX_UNSET;
        }
    }
    TrackGroup chunkSourceTrackGroup = chunkSource.getTrackGroup();
    int chunkSourceTrackCount = chunkSourceTrackGroup.length;
    // Instantiate the necessary internal data-structures.
    primaryTrackGroupIndex = C.INDEX_UNSET;
    trackGroupToSampleQueueIndex = new int[extractorTrackCount];
    for (int i = 0; i < extractorTrackCount; i++) {
        trackGroupToSampleQueueIndex[i] = i;
    }
    // Construct the set of exposed track groups.
    TrackGroup[] trackGroups = new TrackGroup[extractorTrackCount];
    for (int i = 0; i < extractorTrackCount; i++) {
        Format sampleFormat = Assertions.checkStateNotNull(sampleQueues[i].getUpstreamFormat());
        if (i == primaryExtractorTrackIndex) {
            Format[] formats = new Format[chunkSourceTrackCount];
            for (int j = 0; j < chunkSourceTrackCount; j++) {
                Format playlistFormat = chunkSourceTrackGroup.getFormat(j);
                if (primaryExtractorTrackType == C.TRACK_TYPE_AUDIO && muxedAudioFormat != null) {
                    playlistFormat = playlistFormat.withManifestFormatInfo(muxedAudioFormat);
                }
                // If there's only a single variant (chunkSourceTrackCount == 1) then we can safely
                // retain all fields from sampleFormat. Else we need to use deriveFormat to retain only
                // the fields that will be the same for all variants.
                formats[j] = chunkSourceTrackCount == 1 ? sampleFormat.withManifestFormatInfo(playlistFormat) : deriveFormat(playlistFormat, sampleFormat, /* propagateBitrates= */
                true);
            }
            trackGroups[i] = new TrackGroup(uid, formats);
            primaryTrackGroupIndex = i;
        } else {
            @Nullable Format playlistFormat = primaryExtractorTrackType == C.TRACK_TYPE_VIDEO && MimeTypes.isAudio(sampleFormat.sampleMimeType) ? muxedAudioFormat : null;
            String muxedTrackGroupId = uid + ":muxed:" + (i < primaryExtractorTrackIndex ? i : i - 1);
            trackGroups[i] = new TrackGroup(muxedTrackGroupId, deriveFormat(playlistFormat, sampleFormat, /* propagateBitrates= */
            false));
        }
    }
    this.trackGroups = createTrackGroupArrayWithDrmInfo(trackGroups);
    Assertions.checkState(optionalTrackGroups == null);
    optionalTrackGroups = Collections.emptySet();
}
Also used : Format(androidx.media3.common.Format) TrackGroup(androidx.media3.common.TrackGroup) Nullable(androidx.annotation.Nullable) EnsuresNonNull(org.checkerframework.checker.nullness.qual.EnsuresNonNull)

Example 14 with Extractor

use of androidx.media3.extractor.Extractor in project media by androidx.

the class HlsMediaChunk method prepareExtraction.

@RequiresNonNull("output")
@EnsuresNonNull("extractor")
private DefaultExtractorInput prepareExtraction(DataSource dataSource, DataSpec dataSpec, boolean initializeTimestampAdjuster) throws IOException {
    long bytesToRead = dataSource.open(dataSpec);
    if (initializeTimestampAdjuster) {
        try {
            timestampAdjuster.sharedInitializeOrWait(isMasterTimestampSource, startTimeUs);
        } catch (InterruptedException e) {
            throw new InterruptedIOException();
        }
    }
    DefaultExtractorInput extractorInput = new DefaultExtractorInput(dataSource, dataSpec.position, bytesToRead);
    if (extractor == null) {
        long id3Timestamp = peekId3PrivTimestamp(extractorInput);
        extractorInput.resetPeekPosition();
        extractor = previousExtractor != null ? previousExtractor.recreate() : extractorFactory.createExtractor(dataSpec.uri, trackFormat, muxedCaptionFormats, timestampAdjuster, dataSource.getResponseHeaders(), extractorInput, playerId);
        if (extractor.isPackedAudioExtractor()) {
            output.setSampleOffsetUs(id3Timestamp != C.TIME_UNSET ? timestampAdjuster.adjustTsTimestamp(id3Timestamp) : startTimeUs);
        } else {
            // In case the container format changes mid-stream to non-packed-audio, we need to reset
            // the timestamp offset.
            output.setSampleOffsetUs(/* sampleOffsetUs= */
            0L);
        }
        output.onNewExtractor();
        extractor.init(output);
    }
    output.setDrmInitData(drmInitData);
    return extractorInput;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) DefaultExtractorInput(androidx.media3.extractor.DefaultExtractorInput) EnsuresNonNull(org.checkerframework.checker.nullness.qual.EnsuresNonNull) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Example 15 with Extractor

use of androidx.media3.extractor.Extractor in project media by androidx.

the class DefaultExtractorsFactory method addExtractorsForFileType.

private void addExtractorsForFileType(@FileTypes.Type int fileType, List<Extractor> extractors) {
    switch(fileType) {
        case FileTypes.AC3:
            extractors.add(new Ac3Extractor());
            break;
        case FileTypes.AC4:
            extractors.add(new Ac4Extractor());
            break;
        case FileTypes.ADTS:
            extractors.add(new AdtsExtractor(adtsFlags | (constantBitrateSeekingEnabled ? AdtsExtractor.FLAG_ENABLE_CONSTANT_BITRATE_SEEKING : 0) | (constantBitrateSeekingAlwaysEnabled ? AdtsExtractor.FLAG_ENABLE_CONSTANT_BITRATE_SEEKING_ALWAYS : 0)));
            break;
        case FileTypes.AMR:
            extractors.add(new AmrExtractor(amrFlags | (constantBitrateSeekingEnabled ? AmrExtractor.FLAG_ENABLE_CONSTANT_BITRATE_SEEKING : 0) | (constantBitrateSeekingAlwaysEnabled ? AmrExtractor.FLAG_ENABLE_CONSTANT_BITRATE_SEEKING_ALWAYS : 0)));
            break;
        case FileTypes.FLAC:
            @Nullable Extractor flacExtractor = FLAC_EXTENSION_LOADER.getExtractor(flacFlags);
            if (flacExtractor != null) {
                extractors.add(flacExtractor);
            } else {
                extractors.add(new FlacExtractor(flacFlags));
            }
            break;
        case FileTypes.FLV:
            extractors.add(new FlvExtractor());
            break;
        case FileTypes.MATROSKA:
            extractors.add(new MatroskaExtractor(matroskaFlags));
            break;
        case FileTypes.MP3:
            extractors.add(new Mp3Extractor(mp3Flags | (constantBitrateSeekingEnabled ? Mp3Extractor.FLAG_ENABLE_CONSTANT_BITRATE_SEEKING : 0) | (constantBitrateSeekingAlwaysEnabled ? Mp3Extractor.FLAG_ENABLE_CONSTANT_BITRATE_SEEKING_ALWAYS : 0)));
            break;
        case FileTypes.MP4:
            extractors.add(new FragmentedMp4Extractor(fragmentedMp4Flags));
            extractors.add(new Mp4Extractor(mp4Flags));
            break;
        case FileTypes.OGG:
            extractors.add(new OggExtractor());
            break;
        case FileTypes.PS:
            extractors.add(new PsExtractor());
            break;
        case FileTypes.TS:
            extractors.add(new TsExtractor(tsMode, tsFlags, tsTimestampSearchBytes));
            break;
        case FileTypes.WAV:
            extractors.add(new WavExtractor());
            break;
        case FileTypes.JPEG:
            extractors.add(new JpegExtractor());
            break;
        case FileTypes.WEBVTT:
        case FileTypes.UNKNOWN:
        default:
            break;
    }
}
Also used : FragmentedMp4Extractor(androidx.media3.extractor.mp4.FragmentedMp4Extractor) JpegExtractor(androidx.media3.extractor.jpeg.JpegExtractor) Ac4Extractor(androidx.media3.extractor.ts.Ac4Extractor) AdtsExtractor(androidx.media3.extractor.ts.AdtsExtractor) Mp3Extractor(androidx.media3.extractor.mp3.Mp3Extractor) MatroskaExtractor(androidx.media3.extractor.mkv.MatroskaExtractor) TsExtractor(androidx.media3.extractor.ts.TsExtractor) FlacExtractor(androidx.media3.extractor.flac.FlacExtractor) AmrExtractor(androidx.media3.extractor.amr.AmrExtractor) Mp4Extractor(androidx.media3.extractor.mp4.Mp4Extractor) FragmentedMp4Extractor(androidx.media3.extractor.mp4.FragmentedMp4Extractor) PsExtractor(androidx.media3.extractor.ts.PsExtractor) OggExtractor(androidx.media3.extractor.ogg.OggExtractor) WavExtractor(androidx.media3.extractor.wav.WavExtractor) WavExtractor(androidx.media3.extractor.wav.WavExtractor) Ac4Extractor(androidx.media3.extractor.ts.Ac4Extractor) TsExtractor(androidx.media3.extractor.ts.TsExtractor) AmrExtractor(androidx.media3.extractor.amr.AmrExtractor) FlacExtractor(androidx.media3.extractor.flac.FlacExtractor) AdtsExtractor(androidx.media3.extractor.ts.AdtsExtractor) PsExtractor(androidx.media3.extractor.ts.PsExtractor) Mp3Extractor(androidx.media3.extractor.mp3.Mp3Extractor) OggExtractor(androidx.media3.extractor.ogg.OggExtractor) JpegExtractor(androidx.media3.extractor.jpeg.JpegExtractor) Ac3Extractor(androidx.media3.extractor.ts.Ac3Extractor) FlvExtractor(androidx.media3.extractor.flv.FlvExtractor) Mp4Extractor(androidx.media3.extractor.mp4.Mp4Extractor) MatroskaExtractor(androidx.media3.extractor.mkv.MatroskaExtractor) FragmentedMp4Extractor(androidx.media3.extractor.mp4.FragmentedMp4Extractor) FlvExtractor(androidx.media3.extractor.flv.FlvExtractor) Ac3Extractor(androidx.media3.extractor.ts.Ac3Extractor) Nullable(androidx.annotation.Nullable)

Aggregations

Test (org.junit.Test)87 SeekMap (androidx.media3.extractor.SeekMap)73 Uri (android.net.Uri)67 FakeTrackOutput (androidx.media3.test.utils.FakeTrackOutput)59 FakeExtractorOutput (androidx.media3.test.utils.FakeExtractorOutput)44 Extractor (com.google.android.exoplayer2.extractor.Extractor)18 Nullable (androidx.annotation.Nullable)12 ExtractorInput (androidx.media3.extractor.ExtractorInput)12 FakeExtractorInput (androidx.media3.test.utils.FakeExtractorInput)12 FragmentedMp4Extractor (com.google.android.exoplayer2.extractor.mp4.FragmentedMp4Extractor)10 DefaultExtractorInput (androidx.media3.extractor.DefaultExtractorInput)9 WebvttDecoder (androidx.media3.extractor.text.webvtt.WebvttDecoder)8 Mp3Extractor (com.google.android.exoplayer2.extractor.mp3.Mp3Extractor)8 Extractor (androidx.media3.extractor.Extractor)7 Ac3Extractor (com.google.android.exoplayer2.extractor.ts.Ac3Extractor)7 AdtsExtractor (com.google.android.exoplayer2.extractor.ts.AdtsExtractor)7 TsExtractor (com.google.android.exoplayer2.extractor.ts.TsExtractor)7 Format (androidx.media3.common.Format)6 Mp3Extractor (androidx.media3.extractor.mp3.Mp3Extractor)6 FragmentedMp4Extractor (androidx.media3.extractor.mp4.FragmentedMp4Extractor)6