Search in sources :

Example 1 with ReadDataResult

use of com.google.android.exoplayer2.source.SampleStream.ReadDataResult in project ExoPlayer by google.

the class MediaCodecRenderer method readSourceOmittingSampleData.

/**
 * Reads from the source when sample data is not required. If a format or an end of stream buffer
 * is read, it will be handled before the call returns.
 *
 * @param readFlags Additional {@link ReadFlags}. {@link SampleStream#FLAG_OMIT_SAMPLE_DATA} is
 *     added internally, and so does not need to be passed.
 * @return Whether a format was read and processed.
 */
private boolean readSourceOmittingSampleData(@SampleStream.ReadFlags int readFlags) throws ExoPlaybackException {
    FormatHolder formatHolder = getFormatHolder();
    noDataBuffer.clear();
    @ReadDataResult int result = readSource(formatHolder, noDataBuffer, readFlags | FLAG_OMIT_SAMPLE_DATA);
    if (result == C.RESULT_FORMAT_READ) {
        onInputFormatChanged(formatHolder);
        return true;
    } else if (result == C.RESULT_BUFFER_READ && noDataBuffer.isEndOfStream()) {
        inputStreamEnded = true;
        processEndOfStream();
    }
    return false;
}
Also used : ReadDataResult(com.google.android.exoplayer2.source.SampleStream.ReadDataResult) FormatHolder(com.google.android.exoplayer2.FormatHolder)

Example 2 with ReadDataResult

use of com.google.android.exoplayer2.source.SampleStream.ReadDataResult in project ExoPlayer by google.

the class DecoderAudioRenderer method render.

@Override
public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException {
    if (outputStreamEnded) {
        try {
            audioSink.playToEndOfStream();
        } catch (AudioSink.WriteException e) {
            throw createRendererException(e, e.format, e.isRecoverable, PlaybackException.ERROR_CODE_AUDIO_TRACK_WRITE_FAILED);
        }
        return;
    }
    // Try and read a format if we don't have one already.
    if (inputFormat == null) {
        // We don't have a format yet, so try and read one.
        FormatHolder formatHolder = getFormatHolder();
        flagsOnlyBuffer.clear();
        @ReadDataResult int result = readSource(formatHolder, flagsOnlyBuffer, FLAG_REQUIRE_FORMAT);
        if (result == C.RESULT_FORMAT_READ) {
            onInputFormatChanged(formatHolder);
        } else if (result == C.RESULT_BUFFER_READ) {
            // End of stream read having not read a format.
            Assertions.checkState(flagsOnlyBuffer.isEndOfStream());
            inputStreamEnded = true;
            try {
                processEndOfStream();
            } catch (AudioSink.WriteException e) {
                throw createRendererException(e, /* format= */
                null, PlaybackException.ERROR_CODE_AUDIO_TRACK_WRITE_FAILED);
            }
            return;
        } else {
            // We still don't have a format and can't make progress without one.
            return;
        }
    }
    // If we don't have a decoder yet, we need to instantiate one.
    maybeInitDecoder();
    if (decoder != null) {
        try {
            // Rendering loop.
            TraceUtil.beginSection("drainAndFeed");
            while (drainOutputBuffer()) {
            }
            while (feedInputBuffer()) {
            }
            TraceUtil.endSection();
        } catch (DecoderException e) {
            // Can happen with dequeueOutputBuffer, dequeueInputBuffer, queueInputBuffer
            Log.e(TAG, "Audio codec error", e);
            eventDispatcher.audioCodecError(e);
            throw createRendererException(e, inputFormat, PlaybackException.ERROR_CODE_DECODING_FAILED);
        } catch (AudioSink.ConfigurationException e) {
            throw createRendererException(e, e.format, PlaybackException.ERROR_CODE_AUDIO_TRACK_INIT_FAILED);
        } catch (AudioSink.InitializationException e) {
            throw createRendererException(e, e.format, e.isRecoverable, PlaybackException.ERROR_CODE_AUDIO_TRACK_INIT_FAILED);
        } catch (AudioSink.WriteException e) {
            throw createRendererException(e, e.format, e.isRecoverable, PlaybackException.ERROR_CODE_AUDIO_TRACK_WRITE_FAILED);
        }
        decoderCounters.ensureUpdated();
    }
}
Also used : DecoderException(com.google.android.exoplayer2.decoder.DecoderException) ReadDataResult(com.google.android.exoplayer2.source.SampleStream.ReadDataResult) FormatHolder(com.google.android.exoplayer2.FormatHolder)

Example 3 with ReadDataResult

use of com.google.android.exoplayer2.source.SampleStream.ReadDataResult in project ExoPlayer by google.

the class TextRenderer method render.

@Override
public void render(long positionUs, long elapsedRealtimeUs) {
    if (isCurrentStreamFinal() && finalStreamEndPositionUs != C.TIME_UNSET && positionUs >= finalStreamEndPositionUs) {
        releaseBuffers();
        outputStreamEnded = true;
    }
    if (outputStreamEnded) {
        return;
    }
    if (nextSubtitle == null) {
        checkNotNull(decoder).setPositionUs(positionUs);
        try {
            nextSubtitle = checkNotNull(decoder).dequeueOutputBuffer();
        } catch (SubtitleDecoderException e) {
            handleDecoderError(e);
            return;
        }
    }
    if (getState() != STATE_STARTED) {
        return;
    }
    boolean textRendererNeedsUpdate = false;
    if (subtitle != null) {
        // We're iterating through the events in a subtitle. Set textRendererNeedsUpdate if we
        // advance to the next event.
        long subtitleNextEventTimeUs = getNextEventTime();
        while (subtitleNextEventTimeUs <= positionUs) {
            nextSubtitleEventIndex++;
            subtitleNextEventTimeUs = getNextEventTime();
            textRendererNeedsUpdate = true;
        }
    }
    if (nextSubtitle != null) {
        SubtitleOutputBuffer nextSubtitle = this.nextSubtitle;
        if (nextSubtitle.isEndOfStream()) {
            if (!textRendererNeedsUpdate && getNextEventTime() == Long.MAX_VALUE) {
                if (decoderReplacementState == REPLACEMENT_STATE_WAIT_END_OF_STREAM) {
                    replaceDecoder();
                } else {
                    releaseBuffers();
                    outputStreamEnded = true;
                }
            }
        } else if (nextSubtitle.timeUs <= positionUs) {
            // Advance to the next subtitle. Sync the next event index and trigger an update.
            if (subtitle != null) {
                subtitle.release();
            }
            nextSubtitleEventIndex = nextSubtitle.getNextEventTimeIndex(positionUs);
            subtitle = nextSubtitle;
            this.nextSubtitle = null;
            textRendererNeedsUpdate = true;
        }
    }
    if (textRendererNeedsUpdate) {
        // If textRendererNeedsUpdate then subtitle must be non-null.
        checkNotNull(subtitle);
        // textRendererNeedsUpdate is set and we're playing. Update the renderer.
        updateOutput(subtitle.getCues(positionUs));
    }
    if (decoderReplacementState == REPLACEMENT_STATE_WAIT_END_OF_STREAM) {
        return;
    }
    try {
        while (!inputStreamEnded) {
            @Nullable SubtitleInputBuffer nextInputBuffer = this.nextInputBuffer;
            if (nextInputBuffer == null) {
                nextInputBuffer = checkNotNull(decoder).dequeueInputBuffer();
                if (nextInputBuffer == null) {
                    return;
                }
                this.nextInputBuffer = nextInputBuffer;
            }
            if (decoderReplacementState == REPLACEMENT_STATE_SIGNAL_END_OF_STREAM) {
                nextInputBuffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
                checkNotNull(decoder).queueInputBuffer(nextInputBuffer);
                this.nextInputBuffer = null;
                decoderReplacementState = REPLACEMENT_STATE_WAIT_END_OF_STREAM;
                return;
            }
            // Try and read the next subtitle from the source.
            @ReadDataResult int result = readSource(formatHolder, nextInputBuffer, /* readFlags= */
            0);
            if (result == C.RESULT_BUFFER_READ) {
                if (nextInputBuffer.isEndOfStream()) {
                    inputStreamEnded = true;
                    waitingForKeyFrame = false;
                } else {
                    @Nullable Format format = formatHolder.format;
                    if (format == null) {
                        // We haven't received a format yet.
                        return;
                    }
                    nextInputBuffer.subsampleOffsetUs = format.subsampleOffsetUs;
                    nextInputBuffer.flip();
                    waitingForKeyFrame &= !nextInputBuffer.isKeyFrame();
                }
                if (!waitingForKeyFrame) {
                    checkNotNull(decoder).queueInputBuffer(nextInputBuffer);
                    this.nextInputBuffer = null;
                }
            } else if (result == C.RESULT_NOTHING_READ) {
                return;
            }
        }
    } catch (SubtitleDecoderException e) {
        handleDecoderError(e);
    }
}
Also used : Format(com.google.android.exoplayer2.Format) ReadDataResult(com.google.android.exoplayer2.source.SampleStream.ReadDataResult) Nullable(androidx.annotation.Nullable)

Example 4 with ReadDataResult

use of com.google.android.exoplayer2.source.SampleStream.ReadDataResult in project ExoPlayer by google.

the class CameraMotionRenderer method render.

@Override
public void render(long positionUs, long elapsedRealtimeUs) {
    // Keep reading available samples as long as the sample time is not too far into the future.
    while (!hasReadStreamToEnd() && lastTimestampUs < positionUs + SAMPLE_WINDOW_DURATION_US) {
        buffer.clear();
        FormatHolder formatHolder = getFormatHolder();
        @ReadDataResult int result = readSource(formatHolder, buffer, /* readFlags= */
        0);
        if (result != C.RESULT_BUFFER_READ || buffer.isEndOfStream()) {
            return;
        }
        lastTimestampUs = buffer.timeUs;
        if (listener == null || buffer.isDecodeOnly()) {
            continue;
        }
        buffer.flip();
        @Nullable float[] rotation = parseMetadata(Util.castNonNull(buffer.data));
        if (rotation == null) {
            continue;
        }
        Util.castNonNull(listener).onCameraMotion(lastTimestampUs - offsetUs, rotation);
    }
}
Also used : ReadDataResult(com.google.android.exoplayer2.source.SampleStream.ReadDataResult) FormatHolder(com.google.android.exoplayer2.FormatHolder) Nullable(androidx.annotation.Nullable)

Example 5 with ReadDataResult

use of com.google.android.exoplayer2.source.SampleStream.ReadDataResult in project ExoPlayer by google.

the class TransformerAudioRenderer method ensureConfigured.

/**
 * Attempts to read the input format and to initialize the {@link SamplePipeline}.
 */
@Override
protected boolean ensureConfigured() throws TransformationException {
    if (samplePipeline != null) {
        return true;
    }
    FormatHolder formatHolder = getFormatHolder();
    @ReadDataResult int result = readSource(formatHolder, decoderInputBuffer, /* readFlags= */
    FLAG_REQUIRE_FORMAT);
    if (result != C.RESULT_FORMAT_READ) {
        return false;
    }
    Format inputFormat = checkNotNull(formatHolder.format);
    if (shouldPassthrough(inputFormat)) {
        samplePipeline = new PassthroughSamplePipeline(inputFormat, transformationRequest, fallbackListener);
    } else {
        samplePipeline = new AudioTranscodingSamplePipeline(inputFormat, transformationRequest, decoderFactory, encoderFactory, muxerWrapper.getSupportedSampleMimeTypes(getTrackType()), fallbackListener);
    }
    return true;
}
Also used : Format(com.google.android.exoplayer2.Format) ReadDataResult(com.google.android.exoplayer2.source.SampleStream.ReadDataResult) FormatHolder(com.google.android.exoplayer2.FormatHolder)

Aggregations

ReadDataResult (com.google.android.exoplayer2.source.SampleStream.ReadDataResult)11 FormatHolder (com.google.android.exoplayer2.FormatHolder)9 Nullable (androidx.annotation.Nullable)4 Format (com.google.android.exoplayer2.Format)4 DecoderException (com.google.android.exoplayer2.decoder.DecoderException)2 DecoderInputBuffer (com.google.android.exoplayer2.decoder.DecoderInputBuffer)1 ArrayList (java.util.ArrayList)1 RequiresNonNull (org.checkerframework.checker.nullness.qual.RequiresNonNull)1