Search in sources :

Example 1 with ReadDataResult

use of androidx.media3.exoplayer.source.SampleStream.ReadDataResult in project media by androidx.

the class MediaCodecRenderer method bypassRead.

private void bypassRead() throws ExoPlaybackException {
    checkState(!inputStreamEnded);
    FormatHolder formatHolder = getFormatHolder();
    bypassSampleBuffer.clear();
    while (true) {
        bypassSampleBuffer.clear();
        @ReadDataResult int result = readSource(formatHolder, bypassSampleBuffer, /* readFlags= */
        0);
        switch(result) {
            case C.RESULT_FORMAT_READ:
                onInputFormatChanged(formatHolder);
                return;
            case C.RESULT_NOTHING_READ:
                return;
            case C.RESULT_BUFFER_READ:
                if (bypassSampleBuffer.isEndOfStream()) {
                    inputStreamEnded = true;
                    return;
                }
                if (waitingForFirstSampleInFormat) {
                    // This is the first buffer in a new format, the output format must be updated.
                    outputFormat = checkNotNull(inputFormat);
                    onOutputFormatChanged(outputFormat, /* mediaFormat= */
                    null);
                    waitingForFirstSampleInFormat = false;
                }
                // Try to append the buffer to the batch buffer.
                bypassSampleBuffer.flip();
                if (!bypassBatchBuffer.append(bypassSampleBuffer)) {
                    bypassSampleBufferPending = true;
                    return;
                }
                break;
            default:
                throw new IllegalStateException();
        }
    }
}
Also used : ReadDataResult(androidx.media3.exoplayer.source.SampleStream.ReadDataResult) FormatHolder(androidx.media3.exoplayer.FormatHolder)

Example 2 with ReadDataResult

use of androidx.media3.exoplayer.source.SampleStream.ReadDataResult in project media by androidx.

the class MetadataRenderer method readMetadata.

private void readMetadata() {
    if (!inputStreamEnded && pendingMetadata == null) {
        buffer.clear();
        FormatHolder formatHolder = getFormatHolder();
        @ReadDataResult int result = readSource(formatHolder, buffer, /* readFlags= */
        0);
        if (result == C.RESULT_BUFFER_READ) {
            if (buffer.isEndOfStream()) {
                inputStreamEnded = true;
            } else {
                buffer.subsampleOffsetUs = subsampleOffsetUs;
                buffer.flip();
                @Nullable Metadata metadata = castNonNull(decoder).decode(buffer);
                if (metadata != null) {
                    List<Metadata.Entry> entries = new ArrayList<>(metadata.length());
                    decodeWrappedMetadata(metadata, entries);
                    if (!entries.isEmpty()) {
                        Metadata expandedMetadata = new Metadata(entries);
                        pendingMetadata = expandedMetadata;
                        pendingMetadataTimestampUs = buffer.timeUs;
                    }
                }
            }
        } else if (result == C.RESULT_FORMAT_READ) {
            subsampleOffsetUs = Assertions.checkNotNull(formatHolder.format).subsampleOffsetUs;
        }
    }
}
Also used : ReadDataResult(androidx.media3.exoplayer.source.SampleStream.ReadDataResult) FormatHolder(androidx.media3.exoplayer.FormatHolder) Metadata(androidx.media3.common.Metadata) ArrayList(java.util.ArrayList) Nullable(androidx.annotation.Nullable)

Example 3 with ReadDataResult

use of androidx.media3.exoplayer.source.SampleStream.ReadDataResult in project media by androidx.

the class DecoderVideoRenderer method render.

// BaseRenderer implementation.
@Override
public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException {
    if (outputStreamEnded) {
        return;
    }
    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;
            outputStreamEnded = true;
            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(positionUs, elapsedRealtimeUs)) {
            }
            while (feedInputBuffer()) {
            }
            TraceUtil.endSection();
        } catch (DecoderException e) {
            Log.e(TAG, "Video codec error", e);
            eventDispatcher.videoCodecError(e);
            throw createRendererException(e, inputFormat, PlaybackException.ERROR_CODE_DECODING_FAILED);
        }
        decoderCounters.ensureUpdated();
    }
}
Also used : DecoderException(androidx.media3.decoder.DecoderException) ReadDataResult(androidx.media3.exoplayer.source.SampleStream.ReadDataResult) FormatHolder(androidx.media3.exoplayer.FormatHolder)

Example 4 with ReadDataResult

use of androidx.media3.exoplayer.source.SampleStream.ReadDataResult in project media by androidx.

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(androidx.media3.exoplayer.source.SampleStream.ReadDataResult) FormatHolder(androidx.media3.exoplayer.FormatHolder) Nullable(androidx.annotation.Nullable)

Example 5 with ReadDataResult

use of androidx.media3.exoplayer.source.SampleStream.ReadDataResult in project media by androidx.

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(androidx.media3.common.Format) ReadDataResult(androidx.media3.exoplayer.source.SampleStream.ReadDataResult) FormatHolder(androidx.media3.exoplayer.FormatHolder)

Aggregations

ReadDataResult (androidx.media3.exoplayer.source.SampleStream.ReadDataResult)11 FormatHolder (androidx.media3.exoplayer.FormatHolder)9 Nullable (androidx.annotation.Nullable)4 Format (androidx.media3.common.Format)4 DecoderException (androidx.media3.decoder.DecoderException)2 Metadata (androidx.media3.common.Metadata)1 DecoderInputBuffer (androidx.media3.decoder.DecoderInputBuffer)1 SubtitleDecoderException (androidx.media3.extractor.text.SubtitleDecoderException)1 SubtitleInputBuffer (androidx.media3.extractor.text.SubtitleInputBuffer)1 SubtitleOutputBuffer (androidx.media3.extractor.text.SubtitleOutputBuffer)1 ArrayList (java.util.ArrayList)1 RequiresNonNull (org.checkerframework.checker.nullness.qual.RequiresNonNull)1