Search in sources :

Example 21 with FormatHolder

use of com.google.android.exoplayer2.FormatHolder in project ExoPlayer by google.

the class EventSampleStreamTest method readDataReturnDataAfterFormat.

/**
 * Tests that {@link EventSampleStream#readData(FormatHolder, DecoderInputBuffer, int)} will
 * return sample data after the first call.
 */
@Test
public void readDataReturnDataAfterFormat() {
    long presentationTimeUs = 1000000;
    EventMessage eventMessage = newEventMessageWithId(1);
    EventStream eventStream = new EventStream(SCHEME_ID, VALUE, TIME_SCALE, new long[] { presentationTimeUs }, new EventMessage[] { eventMessage });
    EventSampleStream sampleStream = new EventSampleStream(eventStream, FORMAT, false);
    // first read - read format
    readData(sampleStream);
    int result = readData(sampleStream);
    assertThat(result).isEqualTo(C.RESULT_BUFFER_READ);
    assertThat(inputBuffer.data.array()).isEqualTo(getEncodedMessage(eventMessage));
}
Also used : EventMessage(com.google.android.exoplayer2.metadata.emsg.EventMessage) EventStream(com.google.android.exoplayer2.source.dash.manifest.EventStream) Test(org.junit.Test)

Example 22 with FormatHolder

use of com.google.android.exoplayer2.FormatHolder in project ExoPlayer by google.

the class HlsSampleStreamWrapper method readData.

public int readData(int sampleQueueIndex, FormatHolder formatHolder, DecoderInputBuffer buffer, @ReadFlags int readFlags) {
    if (isPendingReset()) {
        return C.RESULT_NOTHING_READ;
    }
    // TODO: Split into discard (in discardBuffer) and format change (here and in skipData) steps.
    if (!mediaChunks.isEmpty()) {
        int discardToMediaChunkIndex = 0;
        while (discardToMediaChunkIndex < mediaChunks.size() - 1 && finishedReadingChunk(mediaChunks.get(discardToMediaChunkIndex))) {
            discardToMediaChunkIndex++;
        }
        Util.removeRange(mediaChunks, 0, discardToMediaChunkIndex);
        HlsMediaChunk currentChunk = mediaChunks.get(0);
        Format trackFormat = currentChunk.trackFormat;
        if (!trackFormat.equals(downstreamTrackFormat)) {
            mediaSourceEventDispatcher.downstreamFormatChanged(trackType, trackFormat, currentChunk.trackSelectionReason, currentChunk.trackSelectionData, currentChunk.startTimeUs);
        }
        downstreamTrackFormat = trackFormat;
    }
    if (!mediaChunks.isEmpty() && !mediaChunks.get(0).isPublished()) {
        // Don't read into preload chunks until we can be sure they are permanently published.
        return C.RESULT_NOTHING_READ;
    }
    int result = sampleQueues[sampleQueueIndex].read(formatHolder, buffer, readFlags, loadingFinished);
    if (result == C.RESULT_FORMAT_READ) {
        Format format = Assertions.checkNotNull(formatHolder.format);
        if (sampleQueueIndex == primarySampleQueueIndex) {
            // Fill in primary sample format with information from the track format.
            int chunkUid = sampleQueues[sampleQueueIndex].peekSourceId();
            int chunkIndex = 0;
            while (chunkIndex < mediaChunks.size() && mediaChunks.get(chunkIndex).uid != chunkUid) {
                chunkIndex++;
            }
            Format trackFormat = chunkIndex < mediaChunks.size() ? mediaChunks.get(chunkIndex).trackFormat : Assertions.checkNotNull(upstreamTrackFormat);
            format = format.withManifestFormatInfo(trackFormat);
        }
        formatHolder.format = format;
    }
    return result;
}
Also used : Format(com.google.android.exoplayer2.Format)

Example 23 with FormatHolder

use of com.google.android.exoplayer2.FormatHolder in project ExoPlayer by google.

the class LibvpxVideoRenderer method render.

@Override
public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException {
    if (outputStreamEnded) {
        return;
    }
    if (format == null) {
        // We don't have a format yet, so try and read one.
        flagsOnlyBuffer.clear();
        int result = readSource(formatHolder, flagsOnlyBuffer, true);
        if (result == C.RESULT_FORMAT_READ) {
            onInputFormatChanged(formatHolder.format);
        } else if (result == C.RESULT_BUFFER_READ) {
            // End of stream read having not read a format.
            Assertions.checkState(flagsOnlyBuffer.isEndOfStream());
            inputStreamEnded = true;
            outputStreamEnded = true;
            return;
        } else {
            // We still don't have a format and can't make progress without one.
            return;
        }
    }
    if (isRendererAvailable()) {
        drmSession = pendingDrmSession;
        ExoMediaCrypto mediaCrypto = null;
        if (drmSession != null) {
            int drmSessionState = drmSession.getState();
            if (drmSessionState == DrmSession.STATE_ERROR) {
                throw ExoPlaybackException.createForRenderer(drmSession.getError(), getIndex());
            } else if (drmSessionState == DrmSession.STATE_OPENED || drmSessionState == DrmSession.STATE_OPENED_WITH_KEYS) {
                mediaCrypto = drmSession.getMediaCrypto();
            } else {
                // The drm session isn't open yet.
                return;
            }
        }
        try {
            if (decoder == null) {
                // If we don't have a decoder yet, we need to instantiate one.
                long codecInitializingTimestamp = SystemClock.elapsedRealtime();
                TraceUtil.beginSection("createVpxDecoder");
                decoder = new VpxDecoder(NUM_BUFFERS, NUM_BUFFERS, INITIAL_INPUT_BUFFER_SIZE, mediaCrypto);
                decoder.setOutputMode(outputMode);
                TraceUtil.endSection();
                long codecInitializedTimestamp = SystemClock.elapsedRealtime();
                eventDispatcher.decoderInitialized(decoder.getName(), codecInitializedTimestamp, codecInitializedTimestamp - codecInitializingTimestamp);
                decoderCounters.decoderInitCount++;
            }
            TraceUtil.beginSection("drainAndFeed");
            while (drainOutputBuffer(positionUs)) {
            }
            while (feedInputBuffer()) {
            }
            TraceUtil.endSection();
        } catch (VpxDecoderException e) {
            throw ExoPlaybackException.createForRenderer(e, getIndex());
        }
    } else {
        skipToKeyframeBefore(positionUs);
    }
    decoderCounters.ensureUpdated();
}
Also used : ExoMediaCrypto(com.google.android.exoplayer2.drm.ExoMediaCrypto)

Example 24 with FormatHolder

use of com.google.android.exoplayer2.FormatHolder in project ExoPlayer by google.

the class HlsSampleStreamWrapper method readData.

/* package */
int readData(int group, FormatHolder formatHolder, DecoderInputBuffer buffer, boolean requireFormat) {
    if (isPendingReset()) {
        return C.RESULT_NOTHING_READ;
    }
    while (mediaChunks.size() > 1 && finishedReadingChunk(mediaChunks.getFirst())) {
        mediaChunks.removeFirst();
    }
    HlsMediaChunk currentChunk = mediaChunks.getFirst();
    Format trackFormat = currentChunk.trackFormat;
    if (!trackFormat.equals(downstreamTrackFormat)) {
        eventDispatcher.downstreamFormatChanged(trackType, trackFormat, currentChunk.trackSelectionReason, currentChunk.trackSelectionData, currentChunk.startTimeUs);
    }
    downstreamTrackFormat = trackFormat;
    return sampleQueues.valueAt(group).readData(formatHolder, buffer, requireFormat, loadingFinished, lastSeekPositionUs);
}
Also used : Format(com.google.android.exoplayer2.Format)

Example 25 with FormatHolder

use of com.google.android.exoplayer2.FormatHolder in project ExoPlayer by google.

the class FakeRenderer method render.

@Override
public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException {
    if (isEnded) {
        return;
    }
    playbackPositionUs = positionUs;
    while (true) {
        if (!hasPendingBuffer) {
            FormatHolder formatHolder = getFormatHolder();
            buffer.clear();
            @ReadDataResult int result = readSource(formatHolder, buffer, /* readFlags= */
            0);
            if (result == C.RESULT_FORMAT_READ) {
                DrmSession.replaceSession(currentDrmSession, formatHolder.drmSession);
                currentDrmSession = formatHolder.drmSession;
                Format format = Assertions.checkNotNull(formatHolder.format);
                if (MimeTypes.getTrackType(format.sampleMimeType) != getTrackType()) {
                    throw ExoPlaybackException.createForRenderer(new IllegalStateException(Util.formatInvariant("Format track type (%s) doesn't match renderer track type (%s).", MimeTypes.getTrackType(format.sampleMimeType), getTrackType())), getName(), getIndex(), format, C.FORMAT_UNSUPPORTED_TYPE, /* isRecoverable= */
                    false, PlaybackException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED);
                }
                formatsRead.add(format);
                onFormatChanged(format);
            } else if (result == C.RESULT_BUFFER_READ) {
                if (buffer.isEndOfStream()) {
                    isEnded = true;
                    return;
                }
                hasPendingBuffer = true;
            } else {
                Assertions.checkState(result == C.RESULT_NOTHING_READ);
                return;
            }
        }
        if (hasPendingBuffer) {
            if (!shouldProcessBuffer(buffer.timeUs, positionUs)) {
                return;
            }
            lastSamplePositionUs = buffer.timeUs;
            sampleBufferReadCount++;
            hasPendingBuffer = false;
        }
    }
}
Also used : Format(com.google.android.exoplayer2.Format) ReadDataResult(com.google.android.exoplayer2.source.SampleStream.ReadDataResult) FormatHolder(com.google.android.exoplayer2.FormatHolder)

Aggregations

FormatHolder (com.google.android.exoplayer2.FormatHolder)16 Test (org.junit.Test)13 ReadDataResult (com.google.android.exoplayer2.source.SampleStream.ReadDataResult)11 Format (com.google.android.exoplayer2.Format)10 EventStream (com.google.android.exoplayer2.source.dash.manifest.EventStream)9 EventMessage (com.google.android.exoplayer2.metadata.emsg.EventMessage)8 Nullable (androidx.annotation.Nullable)7 DecoderReuseEvaluation (com.google.android.exoplayer2.decoder.DecoderReuseEvaluation)5 DecoderInputBuffer (com.google.android.exoplayer2.decoder.DecoderInputBuffer)4 DrmSession (com.google.android.exoplayer2.drm.DrmSession)4 CallSuper (androidx.annotation.CallSuper)2 DecoderException (com.google.android.exoplayer2.decoder.DecoderException)2 ExoTrackSelection (com.google.android.exoplayer2.trackselection.ExoTrackSelection)2 FixedTrackSelection (com.google.android.exoplayer2.trackselection.FixedTrackSelection)2 Before (org.junit.Before)2 CryptoException (android.media.MediaCodec.CryptoException)1 MediaCryptoException (android.media.MediaCryptoException)1 MediaFormat (android.media.MediaFormat)1 InsufficientCapacityException (com.google.android.exoplayer2.decoder.DecoderInputBuffer.InsufficientCapacityException)1 DecoderDiscardReasons (com.google.android.exoplayer2.decoder.DecoderReuseEvaluation.DecoderDiscardReasons)1