public abstract class

SimpleSubtitleDecoder

extends SimpleDecoder<SubtitleInputBuffer, SubtitleOutputBuffer, SubtitleDecoderException>

implements SubtitleDecoder

 java.lang.Object

androidx.media3.decoder.SimpleDecoder<SubtitleInputBuffer, SubtitleOutputBuffer, SubtitleDecoderException>

↳androidx.media3.extractor.text.SimpleSubtitleDecoder

Subclasses:

SsaDecoder, PgsDecoder, TtmlDecoder, DvbDecoder, SubripDecoder, WebvttDecoder, Mp4WebvttDecoder, Tx3gDecoder

Gradle dependencies

compile group: 'androidx.media3', name: 'media3-extractor', version: '1.5.0-alpha01'

  • groupId: androidx.media3
  • artifactId: media3-extractor
  • version: 1.5.0-alpha01

Artifact androidx.media3:media3-extractor:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

Base class for subtitle parsers that use their own decode thread.

Summary

Constructors
protectedSimpleSubtitleDecoder(java.lang.String name)

Methods
protected abstract DecoderInputBuffercreateInputBuffer()

Creates a new input buffer.

protected abstract DecoderOutputBuffercreateOutputBuffer()

Creates a new output buffer.

protected abstract DecoderExceptioncreateUnexpectedDecodeException(java.lang.Throwable error)

Creates an exception to propagate for an unexpected decode error.

protected abstract Subtitledecode(byte[] data[], int length, boolean reset)

Decodes data into a Subtitle.

protected abstract DecoderExceptiondecode(DecoderInputBuffer inputBuffer, DecoderOutputBuffer outputBuffer, boolean reset)

Decodes the inputBuffer and stores any decoded output in outputBuffer.

public final java.lang.StringgetName()

public voidsetPositionUs(long positionUs)

from SimpleDecoder<I, O, E>dequeueInputBuffer, dequeueOutputBuffer, flush, isAtLeastOutputStartTimeUs, queueInputBuffer, release, releaseOutputBuffer, setInitialInputBufferSize, setOutputStartTimeUs
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

protected SimpleSubtitleDecoder(java.lang.String name)

Parameters:

name: The name of the decoder.

Methods

public final java.lang.String getName()

public void setPositionUs(long positionUs)

protected abstract DecoderInputBuffer createInputBuffer()

Creates a new input buffer.

protected abstract DecoderOutputBuffer createOutputBuffer()

Creates a new output buffer.

protected abstract DecoderException createUnexpectedDecodeException(java.lang.Throwable error)

Creates an exception to propagate for an unexpected decode error.

Parameters:

error: The unexpected decode error.

Returns:

The exception to propagate.

protected abstract DecoderException decode(DecoderInputBuffer inputBuffer, DecoderOutputBuffer outputBuffer, boolean reset)

Decodes the inputBuffer and stores any decoded output in outputBuffer.

Parameters:

inputBuffer: The buffer to decode.
outputBuffer: The output buffer to store decoded data. The output buffer will not be made available to dequeue if its DecoderOutputBuffer.timeUs is not at least the output start time or when it's marked with DecoderOutputBuffer.shouldBeSkipped. The output buffer may not have been populated in these cases.
reset: Whether the decoder must be reset before decoding.

Returns:

A decoder exception if an error occurred, or null if decoding was successful.

protected abstract Subtitle decode(byte[] data[], int length, boolean reset)

Decodes data into a Subtitle.

Parameters:

data: An array holding the data to be decoded, starting at position 0.
length: The number of bytes from data to be decoded.
reset: Whether the decoder must be reset before decoding.

Returns:

The decoded Subtitle.

Source

/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.media3.extractor.text;

import androidx.annotation.Nullable;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.decoder.SimpleDecoder;
import java.nio.ByteBuffer;

/** Base class for subtitle parsers that use their own decode thread. */
@UnstableApi
public abstract class SimpleSubtitleDecoder
    extends SimpleDecoder<SubtitleInputBuffer, SubtitleOutputBuffer, SubtitleDecoderException>
    implements SubtitleDecoder {

  private final String name;

  /**
   * @param name The name of the decoder.
   */
  @SuppressWarnings("nullness:method.invocation")
  protected SimpleSubtitleDecoder(String name) {
    super(new SubtitleInputBuffer[2], new SubtitleOutputBuffer[2]);
    this.name = name;
    setInitialInputBufferSize(1024);
  }

  @Override
  public final String getName() {
    return name;
  }

  @Override
  public void setPositionUs(long positionUs) {
    // Do nothing
  }

  @Override
  protected final SubtitleInputBuffer createInputBuffer() {
    return new SubtitleInputBuffer();
  }

  @Override
  protected final SubtitleOutputBuffer createOutputBuffer() {
    return new SubtitleOutputBuffer() {
      @Override
      public void release() {
        SimpleSubtitleDecoder.this.releaseOutputBuffer(this);
      }
    };
  }

  @Override
  protected final SubtitleDecoderException createUnexpectedDecodeException(Throwable error) {
    return new SubtitleDecoderException("Unexpected decode error", error);
  }

  @SuppressWarnings("ByteBufferBackingArray")
  @Override
  @Nullable
  protected final SubtitleDecoderException decode(
      SubtitleInputBuffer inputBuffer, SubtitleOutputBuffer outputBuffer, boolean reset) {
    try {
      ByteBuffer inputData = Assertions.checkNotNull(inputBuffer.data);
      Subtitle subtitle = decode(inputData.array(), inputData.limit(), reset);
      outputBuffer.setContent(inputBuffer.timeUs, subtitle, inputBuffer.subsampleOffsetUs);
      outputBuffer.shouldBeSkipped = false; // Skipping is handled by TextRenderer
      return null;
    } catch (SubtitleDecoderException e) {
      return e;
    }
  }

  /**
   * Decodes data into a {@link Subtitle}.
   *
   * @param data An array holding the data to be decoded, starting at position 0.
   * @param length The number of bytes from {@code data} to be decoded.
   * @param reset Whether the decoder must be reset before decoding.
   * @return The decoded {@link Subtitle}.
   * @throws SubtitleDecoderException If a decoding error occurs.
   */
  protected abstract Subtitle decode(byte[] data, int length, boolean reset)
      throws SubtitleDecoderException;
}