Search in sources :

Example 6 with Representation

use of androidx.media3.exoplayer.dash.manifest.Representation in project media by androidx.

the class DashUtil method loadInitializationData.

private static void loadInitializationData(DataSource dataSource, Representation representation, int baseUrlIndex, ChunkExtractor chunkExtractor, RangedUri requestUri) throws IOException {
    DataSpec dataSpec = DashUtil.buildDataSpec(representation, representation.baseUrls.get(baseUrlIndex).url, requestUri, /* flags= */
    0);
    InitializationChunk initializationChunk = new InitializationChunk(dataSource, dataSpec, representation.format, C.SELECTION_REASON_UNKNOWN, null, /* trackSelectionData */
    chunkExtractor);
    initializationChunk.load();
}
Also used : InitializationChunk(androidx.media3.exoplayer.source.chunk.InitializationChunk) DataSpec(androidx.media3.datasource.DataSpec)

Example 7 with Representation

use of androidx.media3.exoplayer.dash.manifest.Representation in project media by androidx.

the class DefaultDashChunkSource method getNextChunk.

@Override
public void getNextChunk(long playbackPositionUs, long loadPositionUs, List<? extends MediaChunk> queue, ChunkHolder out) {
    if (fatalError != null) {
        return;
    }
    long bufferedDurationUs = loadPositionUs - playbackPositionUs;
    long presentationPositionUs = Util.msToUs(manifest.availabilityStartTimeMs) + Util.msToUs(manifest.getPeriod(periodIndex).startMs) + loadPositionUs;
    if (playerTrackEmsgHandler != null && playerTrackEmsgHandler.maybeRefreshManifestBeforeLoadingNextChunk(presentationPositionUs)) {
        return;
    }
    long nowUnixTimeUs = Util.msToUs(Util.getNowUnixTimeMs(elapsedRealtimeOffsetMs));
    long nowPeriodTimeUs = getNowPeriodTimeUs(nowUnixTimeUs);
    MediaChunk previous = queue.isEmpty() ? null : queue.get(queue.size() - 1);
    MediaChunkIterator[] chunkIterators = new MediaChunkIterator[trackSelection.length()];
    for (int i = 0; i < chunkIterators.length; i++) {
        RepresentationHolder representationHolder = representationHolders[i];
        if (representationHolder.segmentIndex == null) {
            chunkIterators[i] = MediaChunkIterator.EMPTY;
        } else {
            long firstAvailableSegmentNum = representationHolder.getFirstAvailableSegmentNum(nowUnixTimeUs);
            long lastAvailableSegmentNum = representationHolder.getLastAvailableSegmentNum(nowUnixTimeUs);
            long segmentNum = getSegmentNum(representationHolder, previous, loadPositionUs, firstAvailableSegmentNum, lastAvailableSegmentNum);
            if (segmentNum < firstAvailableSegmentNum) {
                chunkIterators[i] = MediaChunkIterator.EMPTY;
            } else {
                representationHolder = updateSelectedBaseUrl(/* trackIndex= */
                i);
                chunkIterators[i] = new RepresentationSegmentIterator(representationHolder, segmentNum, lastAvailableSegmentNum, nowPeriodTimeUs);
            }
        }
    }
    long availableLiveDurationUs = getAvailableLiveDurationUs(nowUnixTimeUs, playbackPositionUs);
    trackSelection.updateSelectedTrack(playbackPositionUs, bufferedDurationUs, availableLiveDurationUs, queue, chunkIterators);
    RepresentationHolder representationHolder = updateSelectedBaseUrl(trackSelection.getSelectedIndex());
    if (representationHolder.chunkExtractor != null) {
        Representation selectedRepresentation = representationHolder.representation;
        @Nullable RangedUri pendingInitializationUri = null;
        @Nullable RangedUri pendingIndexUri = null;
        if (representationHolder.chunkExtractor.getSampleFormats() == null) {
            pendingInitializationUri = selectedRepresentation.getInitializationUri();
        }
        if (representationHolder.segmentIndex == null) {
            pendingIndexUri = selectedRepresentation.getIndexUri();
        }
        if (pendingInitializationUri != null || pendingIndexUri != null) {
            // We have initialization and/or index requests to make.
            out.chunk = newInitializationChunk(representationHolder, dataSource, trackSelection.getSelectedFormat(), trackSelection.getSelectionReason(), trackSelection.getSelectionData(), pendingInitializationUri, pendingIndexUri);
            return;
        }
    }
    long periodDurationUs = representationHolder.periodDurationUs;
    boolean periodEnded = periodDurationUs != C.TIME_UNSET;
    if (representationHolder.getSegmentCount() == 0) {
        // The index doesn't define any segments.
        out.endOfStream = periodEnded;
        return;
    }
    long firstAvailableSegmentNum = representationHolder.getFirstAvailableSegmentNum(nowUnixTimeUs);
    long lastAvailableSegmentNum = representationHolder.getLastAvailableSegmentNum(nowUnixTimeUs);
    long segmentNum = getSegmentNum(representationHolder, previous, loadPositionUs, firstAvailableSegmentNum, lastAvailableSegmentNum);
    if (segmentNum < firstAvailableSegmentNum) {
        // This is before the first chunk in the current manifest.
        fatalError = new BehindLiveWindowException();
        return;
    }
    if (segmentNum > lastAvailableSegmentNum || (missingLastSegment && segmentNum >= lastAvailableSegmentNum)) {
        // The segment is beyond the end of the period.
        out.endOfStream = periodEnded;
        return;
    }
    if (periodEnded && representationHolder.getSegmentStartTimeUs(segmentNum) >= periodDurationUs) {
        // The period duration clips the period to a position before the segment.
        out.endOfStream = true;
        return;
    }
    int maxSegmentCount = (int) min(maxSegmentsPerLoad, lastAvailableSegmentNum - segmentNum + 1);
    if (periodDurationUs != C.TIME_UNSET) {
        while (maxSegmentCount > 1 && representationHolder.getSegmentStartTimeUs(segmentNum + maxSegmentCount - 1) >= periodDurationUs) {
            // The period duration clips the period to a position before the last segment in the range
            // [segmentNum, segmentNum + maxSegmentCount - 1]. Reduce maxSegmentCount.
            maxSegmentCount--;
        }
    }
    long seekTimeUs = queue.isEmpty() ? loadPositionUs : C.TIME_UNSET;
    out.chunk = newMediaChunk(representationHolder, dataSource, trackType, trackSelection.getSelectedFormat(), trackSelection.getSelectionReason(), trackSelection.getSelectionData(), segmentNum, maxSegmentCount, seekTimeUs, nowPeriodTimeUs);
}
Also used : BaseMediaChunkIterator(androidx.media3.exoplayer.source.chunk.BaseMediaChunkIterator) MediaChunkIterator(androidx.media3.exoplayer.source.chunk.MediaChunkIterator) BehindLiveWindowException(androidx.media3.exoplayer.source.BehindLiveWindowException) SingleSampleMediaChunk(androidx.media3.exoplayer.source.chunk.SingleSampleMediaChunk) ContainerMediaChunk(androidx.media3.exoplayer.source.chunk.ContainerMediaChunk) MediaChunk(androidx.media3.exoplayer.source.chunk.MediaChunk) RangedUri(androidx.media3.exoplayer.dash.manifest.RangedUri) Representation(androidx.media3.exoplayer.dash.manifest.Representation) Nullable(androidx.annotation.Nullable)

Example 8 with Representation

use of androidx.media3.exoplayer.dash.manifest.Representation in project media by androidx.

the class DefaultDashChunkSource method newMediaChunk.

protected Chunk newMediaChunk(RepresentationHolder representationHolder, DataSource dataSource, @C.TrackType int trackType, Format trackFormat, @C.SelectionReason int trackSelectionReason, Object trackSelectionData, long firstSegmentNum, int maxSegmentCount, long seekTimeUs, long nowPeriodTimeUs) {
    Representation representation = representationHolder.representation;
    long startTimeUs = representationHolder.getSegmentStartTimeUs(firstSegmentNum);
    RangedUri segmentUri = representationHolder.getSegmentUrl(firstSegmentNum);
    if (representationHolder.chunkExtractor == null) {
        long endTimeUs = representationHolder.getSegmentEndTimeUs(firstSegmentNum);
        int flags = representationHolder.isSegmentAvailableAtFullNetworkSpeed(firstSegmentNum, nowPeriodTimeUs) ? 0 : DataSpec.FLAG_MIGHT_NOT_USE_FULL_NETWORK_SPEED;
        DataSpec dataSpec = DashUtil.buildDataSpec(representation, representationHolder.selectedBaseUrl.url, segmentUri, flags);
        return new SingleSampleMediaChunk(dataSource, dataSpec, trackFormat, trackSelectionReason, trackSelectionData, startTimeUs, endTimeUs, firstSegmentNum, trackType, trackFormat);
    } else {
        int segmentCount = 1;
        for (int i = 1; i < maxSegmentCount; i++) {
            RangedUri nextSegmentUri = representationHolder.getSegmentUrl(firstSegmentNum + i);
            @Nullable RangedUri mergedSegmentUri = segmentUri.attemptMerge(nextSegmentUri, representationHolder.selectedBaseUrl.url);
            if (mergedSegmentUri == null) {
                // Unable to merge segment fetches because the URIs do not merge.
                break;
            }
            segmentUri = mergedSegmentUri;
            segmentCount++;
        }
        long segmentNum = firstSegmentNum + segmentCount - 1;
        long endTimeUs = representationHolder.getSegmentEndTimeUs(segmentNum);
        long periodDurationUs = representationHolder.periodDurationUs;
        long clippedEndTimeUs = periodDurationUs != C.TIME_UNSET && periodDurationUs <= endTimeUs ? periodDurationUs : C.TIME_UNSET;
        int flags = representationHolder.isSegmentAvailableAtFullNetworkSpeed(segmentNum, nowPeriodTimeUs) ? 0 : DataSpec.FLAG_MIGHT_NOT_USE_FULL_NETWORK_SPEED;
        DataSpec dataSpec = DashUtil.buildDataSpec(representation, representationHolder.selectedBaseUrl.url, segmentUri, flags);
        long sampleOffsetUs = -representation.presentationTimeOffsetUs;
        return new ContainerMediaChunk(dataSource, dataSpec, trackFormat, trackSelectionReason, trackSelectionData, startTimeUs, endTimeUs, seekTimeUs, clippedEndTimeUs, firstSegmentNum, segmentCount, sampleOffsetUs, representationHolder.chunkExtractor);
    }
}
Also used : SingleSampleMediaChunk(androidx.media3.exoplayer.source.chunk.SingleSampleMediaChunk) RangedUri(androidx.media3.exoplayer.dash.manifest.RangedUri) DataSpec(androidx.media3.datasource.DataSpec) Representation(androidx.media3.exoplayer.dash.manifest.Representation) ContainerMediaChunk(androidx.media3.exoplayer.source.chunk.ContainerMediaChunk) Nullable(androidx.annotation.Nullable)

Example 9 with Representation

use of androidx.media3.exoplayer.dash.manifest.Representation in project media by androidx.

the class DashManifest method copyAdaptationSets.

private static ArrayList<AdaptationSet> copyAdaptationSets(List<AdaptationSet> adaptationSets, LinkedList<StreamKey> keys) {
    StreamKey key = keys.poll();
    int periodIndex = key.periodIndex;
    ArrayList<AdaptationSet> copyAdaptationSets = new ArrayList<>();
    do {
        int adaptationSetIndex = key.groupIndex;
        AdaptationSet adaptationSet = adaptationSets.get(adaptationSetIndex);
        List<Representation> representations = adaptationSet.representations;
        ArrayList<Representation> copyRepresentations = new ArrayList<>();
        do {
            Representation representation = representations.get(key.streamIndex);
            copyRepresentations.add(representation);
            key = keys.poll();
        } while (key.periodIndex == periodIndex && key.groupIndex == adaptationSetIndex);
        copyAdaptationSets.add(new AdaptationSet(adaptationSet.id, adaptationSet.type, copyRepresentations, adaptationSet.accessibilityDescriptors, adaptationSet.essentialProperties, adaptationSet.supplementalProperties));
    } while (key.periodIndex == periodIndex);
    // Add back the last key which doesn't belong to the period being processed
    keys.addFirst(key);
    return copyAdaptationSets;
}
Also used : ArrayList(java.util.ArrayList) StreamKey(androidx.media3.common.StreamKey)

Example 10 with Representation

use of androidx.media3.exoplayer.dash.manifest.Representation in project media by androidx.

the class DashManifestParser method parseRepresentation.

// Representation parsing.
protected RepresentationInfo parseRepresentation(XmlPullParser xpp, List<BaseUrl> parentBaseUrls, @Nullable String adaptationSetMimeType, @Nullable String adaptationSetCodecs, int adaptationSetWidth, int adaptationSetHeight, float adaptationSetFrameRate, int adaptationSetAudioChannels, int adaptationSetAudioSamplingRate, @Nullable String adaptationSetLanguage, List<Descriptor> adaptationSetRoleDescriptors, List<Descriptor> adaptationSetAccessibilityDescriptors, List<Descriptor> adaptationSetEssentialProperties, List<Descriptor> adaptationSetSupplementalProperties, @Nullable SegmentBase segmentBase, long periodStartUnixTimeMs, long periodDurationMs, long baseUrlAvailabilityTimeOffsetUs, long segmentBaseAvailabilityTimeOffsetUs, long timeShiftBufferDepthMs, boolean dvbProfileDeclared) throws XmlPullParserException, IOException {
    String id = xpp.getAttributeValue(null, "id");
    int bandwidth = parseInt(xpp, "bandwidth", Format.NO_VALUE);
    String mimeType = parseString(xpp, "mimeType", adaptationSetMimeType);
    String codecs = parseString(xpp, "codecs", adaptationSetCodecs);
    int width = parseInt(xpp, "width", adaptationSetWidth);
    int height = parseInt(xpp, "height", adaptationSetHeight);
    float frameRate = parseFrameRate(xpp, adaptationSetFrameRate);
    int audioChannels = adaptationSetAudioChannels;
    int audioSamplingRate = parseInt(xpp, "audioSamplingRate", adaptationSetAudioSamplingRate);
    String drmSchemeType = null;
    ArrayList<SchemeData> drmSchemeDatas = new ArrayList<>();
    ArrayList<Descriptor> inbandEventStreams = new ArrayList<>();
    ArrayList<Descriptor> essentialProperties = new ArrayList<>(adaptationSetEssentialProperties);
    ArrayList<Descriptor> supplementalProperties = new ArrayList<>(adaptationSetSupplementalProperties);
    ArrayList<BaseUrl> baseUrls = new ArrayList<>();
    boolean seenFirstBaseUrl = false;
    do {
        xpp.next();
        if (XmlPullParserUtil.isStartTag(xpp, "BaseURL")) {
            if (!seenFirstBaseUrl) {
                baseUrlAvailabilityTimeOffsetUs = parseAvailabilityTimeOffsetUs(xpp, baseUrlAvailabilityTimeOffsetUs);
                seenFirstBaseUrl = true;
            }
            baseUrls.addAll(parseBaseUrl(xpp, parentBaseUrls, dvbProfileDeclared));
        } else if (XmlPullParserUtil.isStartTag(xpp, "AudioChannelConfiguration")) {
            audioChannels = parseAudioChannelConfiguration(xpp);
        } else if (XmlPullParserUtil.isStartTag(xpp, "SegmentBase")) {
            segmentBase = parseSegmentBase(xpp, (SingleSegmentBase) segmentBase);
        } else if (XmlPullParserUtil.isStartTag(xpp, "SegmentList")) {
            segmentBaseAvailabilityTimeOffsetUs = parseAvailabilityTimeOffsetUs(xpp, segmentBaseAvailabilityTimeOffsetUs);
            segmentBase = parseSegmentList(xpp, (SegmentList) segmentBase, periodStartUnixTimeMs, periodDurationMs, baseUrlAvailabilityTimeOffsetUs, segmentBaseAvailabilityTimeOffsetUs, timeShiftBufferDepthMs);
        } else if (XmlPullParserUtil.isStartTag(xpp, "SegmentTemplate")) {
            segmentBaseAvailabilityTimeOffsetUs = parseAvailabilityTimeOffsetUs(xpp, segmentBaseAvailabilityTimeOffsetUs);
            segmentBase = parseSegmentTemplate(xpp, (SegmentTemplate) segmentBase, adaptationSetSupplementalProperties, periodStartUnixTimeMs, periodDurationMs, baseUrlAvailabilityTimeOffsetUs, segmentBaseAvailabilityTimeOffsetUs, timeShiftBufferDepthMs);
        } else if (XmlPullParserUtil.isStartTag(xpp, "ContentProtection")) {
            Pair<String, SchemeData> contentProtection = parseContentProtection(xpp);
            if (contentProtection.first != null) {
                drmSchemeType = contentProtection.first;
            }
            if (contentProtection.second != null) {
                drmSchemeDatas.add(contentProtection.second);
            }
        } else if (XmlPullParserUtil.isStartTag(xpp, "InbandEventStream")) {
            inbandEventStreams.add(parseDescriptor(xpp, "InbandEventStream"));
        } else if (XmlPullParserUtil.isStartTag(xpp, "EssentialProperty")) {
            essentialProperties.add(parseDescriptor(xpp, "EssentialProperty"));
        } else if (XmlPullParserUtil.isStartTag(xpp, "SupplementalProperty")) {
            supplementalProperties.add(parseDescriptor(xpp, "SupplementalProperty"));
        } else {
            maybeSkipTag(xpp);
        }
    } while (!XmlPullParserUtil.isEndTag(xpp, "Representation"));
    Format format = buildFormat(id, mimeType, width, height, frameRate, audioChannels, audioSamplingRate, bandwidth, adaptationSetLanguage, adaptationSetRoleDescriptors, adaptationSetAccessibilityDescriptors, codecs, essentialProperties, supplementalProperties);
    segmentBase = segmentBase != null ? segmentBase : new SingleSegmentBase();
    return new RepresentationInfo(format, !baseUrls.isEmpty() ? baseUrls : parentBaseUrls, segmentBase, drmSchemeType, drmSchemeDatas, inbandEventStreams, essentialProperties, supplementalProperties, Representation.REVISION_ID_DEFAULT);
}
Also used : SingleSegmentBase(androidx.media3.exoplayer.dash.manifest.SegmentBase.SingleSegmentBase) ArrayList(java.util.ArrayList) SchemeData(androidx.media3.common.DrmInitData.SchemeData) SegmentTemplate(androidx.media3.exoplayer.dash.manifest.SegmentBase.SegmentTemplate) Format(androidx.media3.common.Format) Pair(android.util.Pair)

Aggregations

Representation (androidx.media3.exoplayer.dash.manifest.Representation)13 Nullable (androidx.annotation.Nullable)12 RangedUri (androidx.media3.exoplayer.dash.manifest.RangedUri)7 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 Format (androidx.media3.common.Format)4 AdaptationSet (androidx.media3.exoplayer.dash.manifest.AdaptationSet)4 StreamKey (androidx.media3.common.StreamKey)3 DataSpec (androidx.media3.datasource.DataSpec)3 MultiSegmentRepresentation (androidx.media3.exoplayer.dash.manifest.Representation.MultiSegmentRepresentation)3 SingleSegmentRepresentation (androidx.media3.exoplayer.dash.manifest.Representation.SingleSegmentRepresentation)3 SingleSegmentBase (androidx.media3.exoplayer.dash.manifest.SegmentBase.SingleSegmentBase)3 SchemeData (androidx.media3.common.DrmInitData.SchemeData)2 DashSegmentIndex (androidx.media3.exoplayer.dash.DashSegmentIndex)2 BaseUrl (androidx.media3.exoplayer.dash.manifest.BaseUrl)2 BehindLiveWindowException (androidx.media3.exoplayer.source.BehindLiveWindowException)2 ContainerMediaChunk (androidx.media3.exoplayer.source.chunk.ContainerMediaChunk)2 InitializationChunk (androidx.media3.exoplayer.source.chunk.InitializationChunk)2 SingleSampleMediaChunk (androidx.media3.exoplayer.source.chunk.SingleSampleMediaChunk)2 IOException (java.io.IOException)2