Search in sources :

Example 1 with SubtitleOutputBuffer

use of androidx.media3.extractor.text.SubtitleOutputBuffer in project media by androidx.

the class SubtitleExtractor method decode.

/**
 * Decodes the subtitle data and stores the samples in the memory of the extractor.
 */
private void decode() throws IOException {
    try {
        @Nullable SubtitleInputBuffer inputBuffer = subtitleDecoder.dequeueInputBuffer();
        while (inputBuffer == null) {
            Thread.sleep(5);
            inputBuffer = subtitleDecoder.dequeueInputBuffer();
        }
        inputBuffer.ensureSpaceForWrite(bytesRead);
        inputBuffer.data.put(subtitleData.getData(), /* offset= */
        0, bytesRead);
        inputBuffer.data.limit(bytesRead);
        subtitleDecoder.queueInputBuffer(inputBuffer);
        @Nullable SubtitleOutputBuffer outputBuffer = subtitleDecoder.dequeueOutputBuffer();
        while (outputBuffer == null) {
            Thread.sleep(5);
            outputBuffer = subtitleDecoder.dequeueOutputBuffer();
        }
        for (int i = 0; i < outputBuffer.getEventTimeCount(); i++) {
            List<Cue> cues = outputBuffer.getCues(outputBuffer.getEventTime(i));
            byte[] cuesSample = cueEncoder.encode(cues);
            timestamps.add(outputBuffer.getEventTime(i));
            samples.add(new ParsableByteArray(cuesSample));
        }
        outputBuffer.release();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new InterruptedIOException();
    } catch (SubtitleDecoderException e) {
        throw ParserException.createForMalformedContainer("SubtitleDecoder failed.", e);
    }
}
Also used : ParsableByteArray(androidx.media3.common.util.ParsableByteArray) InterruptedIOException(java.io.InterruptedIOException) Cue(androidx.media3.common.text.Cue) Nullable(androidx.annotation.Nullable)

Example 2 with SubtitleOutputBuffer

use of androidx.media3.extractor.text.SubtitleOutputBuffer in project media by androidx.

the class Cea608Decoder method dequeueOutputBuffer.

@Nullable
@Override
public SubtitleOutputBuffer dequeueOutputBuffer() throws SubtitleDecoderException {
    SubtitleOutputBuffer outputBuffer = super.dequeueOutputBuffer();
    if (outputBuffer != null) {
        return outputBuffer;
    }
    if (shouldClearStuckCaptions()) {
        outputBuffer = getAvailableOutputBuffer();
        if (outputBuffer != null) {
            cues = Collections.emptyList();
            lastCueUpdateUs = C.TIME_UNSET;
            Subtitle subtitle = createSubtitle();
            outputBuffer.setContent(getPositionUs(), subtitle, Format.OFFSET_SAMPLE_RELATIVE);
            return outputBuffer;
        }
    }
    return null;
}
Also used : Subtitle(androidx.media3.extractor.text.Subtitle) SubtitleOutputBuffer(androidx.media3.extractor.text.SubtitleOutputBuffer) Nullable(androidx.annotation.Nullable)

Example 3 with SubtitleOutputBuffer

use of androidx.media3.extractor.text.SubtitleOutputBuffer in project media by androidx.

the class ExoplayerCuesDecoderTest method dequeueOutputBuffer_queuedOnEndOfStreamInputBuffer_returnsEndOfStreamOutputBuffer.

@Test
public void dequeueOutputBuffer_queuedOnEndOfStreamInputBuffer_returnsEndOfStreamOutputBuffer() throws Exception {
    SubtitleInputBuffer inputBuffer = decoder.dequeueInputBuffer();
    inputBuffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
    decoder.queueInputBuffer(inputBuffer);
    SubtitleOutputBuffer outputBuffer = decoder.dequeueOutputBuffer();
    assertThat(outputBuffer.isEndOfStream()).isTrue();
}
Also used : SubtitleInputBuffer(androidx.media3.extractor.text.SubtitleInputBuffer) SubtitleOutputBuffer(androidx.media3.extractor.text.SubtitleOutputBuffer) Test(org.junit.Test)

Example 4 with SubtitleOutputBuffer

use of androidx.media3.extractor.text.SubtitleOutputBuffer in project media by androidx.

the class ExoplayerCuesDecoderTest method releaseOutputBuffer_calledTwice_fails.

@Test
public void releaseOutputBuffer_calledTwice_fails() throws Exception {
    SubtitleInputBuffer inputBuffer = decoder.dequeueInputBuffer();
    writeDataToInputBuffer(inputBuffer, /* timeUs=*/
    1000, ENCODED_CUES);
    decoder.queueInputBuffer(inputBuffer);
    SubtitleOutputBuffer outputBuffer = decoder.dequeueOutputBuffer();
    outputBuffer.release();
    assertThrows(IllegalStateException.class, outputBuffer::release);
}
Also used : SubtitleInputBuffer(androidx.media3.extractor.text.SubtitleInputBuffer) SubtitleOutputBuffer(androidx.media3.extractor.text.SubtitleOutputBuffer) Test(org.junit.Test)

Example 5 with SubtitleOutputBuffer

use of androidx.media3.extractor.text.SubtitleOutputBuffer in project media by androidx.

the class ExoplayerCuesDecoderTest method decoder_outputsSubtitle.

@Test
public void decoder_outputsSubtitle() throws Exception {
    SubtitleInputBuffer inputBuffer = decoder.dequeueInputBuffer();
    writeDataToInputBuffer(inputBuffer, /* timeUs=*/
    1000, ENCODED_CUES);
    decoder.queueInputBuffer(inputBuffer);
    SubtitleOutputBuffer outputBuffer = decoder.dequeueOutputBuffer();
    assertThat(outputBuffer.getCues(/* timeUs=*/
    999)).isEmpty();
    assertThat(outputBuffer.getCues(1001)).hasSize(1);
    assertThat(outputBuffer.getCues(/* timeUs=*/
    1000)).hasSize(1);
    assertThat(outputBuffer.getCues(/* timeUs=*/
    1000).get(0).text.toString()).isEqualTo("text");
    outputBuffer.release();
}
Also used : SubtitleInputBuffer(androidx.media3.extractor.text.SubtitleInputBuffer) SubtitleOutputBuffer(androidx.media3.extractor.text.SubtitleOutputBuffer) Test(org.junit.Test)

Aggregations

SubtitleOutputBuffer (androidx.media3.extractor.text.SubtitleOutputBuffer)9 SubtitleInputBuffer (androidx.media3.extractor.text.SubtitleInputBuffer)6 Nullable (androidx.annotation.Nullable)5 Test (org.junit.Test)5 Subtitle (androidx.media3.extractor.text.Subtitle)2 Format (androidx.media3.common.Format)1 Cue (androidx.media3.common.text.Cue)1 ParsableByteArray (androidx.media3.common.util.ParsableByteArray)1 ReadDataResult (androidx.media3.exoplayer.source.SampleStream.ReadDataResult)1 SubtitleDecoderException (androidx.media3.extractor.text.SubtitleDecoderException)1 InterruptedIOException (java.io.InterruptedIOException)1