Search in sources :

Example 1 with SubtitleInputBuffer

use of androidx.media3.extractor.text.SubtitleInputBuffer 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 SubtitleInputBuffer

use of androidx.media3.extractor.text.SubtitleInputBuffer 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 3 with SubtitleInputBuffer

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

the class ExoplayerCuesDecoderTest method dequeueInputBuffer_withQueuedInput_returnsNull.

@Test
public void dequeueInputBuffer_withQueuedInput_returnsNull() throws Exception {
    SubtitleInputBuffer inputBuffer = decoder.dequeueInputBuffer();
    writeDataToInputBuffer(inputBuffer, /* timeUs=*/
    1000, ENCODED_CUES);
    decoder.queueInputBuffer(inputBuffer);
    assertThat(decoder.dequeueInputBuffer()).isNull();
}
Also used : SubtitleInputBuffer(androidx.media3.extractor.text.SubtitleInputBuffer) Test(org.junit.Test)

Example 4 with SubtitleInputBuffer

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

the class ExoplayerCuesDecoderTest method flush_makesAllInputBuffersAvailable.

@Test
public void flush_makesAllInputBuffersAvailable() throws Exception {
    List<SubtitleInputBuffer> inputBuffers = new ArrayList<>();
    SubtitleInputBuffer inputBuffer = decoder.dequeueInputBuffer();
    while (inputBuffer != null) {
        inputBuffers.add(inputBuffer);
        inputBuffer = decoder.dequeueInputBuffer();
    }
    for (int i = 0; i < inputBuffers.size(); i++) {
        writeDataToInputBuffer(inputBuffers.get(i), /* timeUs=*/
        1000, ENCODED_CUES);
        decoder.queueInputBuffer(inputBuffers.get(i));
    }
    decoder.flush();
    for (int i = 0; i < inputBuffers.size(); i++) {
        assertThat(decoder.dequeueInputBuffer().data.position()).isEqualTo(0);
    }
}
Also used : SubtitleInputBuffer(androidx.media3.extractor.text.SubtitleInputBuffer) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with SubtitleInputBuffer

use of androidx.media3.extractor.text.SubtitleInputBuffer 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)

Aggregations

SubtitleInputBuffer (androidx.media3.extractor.text.SubtitleInputBuffer)11 Test (org.junit.Test)9 SubtitleOutputBuffer (androidx.media3.extractor.text.SubtitleOutputBuffer)6 Nullable (androidx.annotation.Nullable)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 ArrayList (java.util.ArrayList)1