Java Code Examples for com.google.android.exoplayer2.C#Encoding

The following examples show how to use com.google.android.exoplayer2.C#Encoding . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MimeTypes.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the {@link C}{@code .ENCODING_*} constant that corresponds to specified MIME type, if
 * it is an encoded (non-PCM) audio format, or {@link C#ENCODING_INVALID} otherwise.
 *
 * @param mimeType The MIME type.
 * @return The {@link C}{@code .ENCODING_*} constant that corresponds to a specified MIME type, or
 *     {@link C#ENCODING_INVALID}.
 */
public static @C.Encoding int getEncoding(String mimeType) {
  switch (mimeType) {
    case MimeTypes.AUDIO_AC3:
      return C.ENCODING_AC3;
    case MimeTypes.AUDIO_E_AC3:
      return C.ENCODING_E_AC3;
    case MimeTypes.AUDIO_E_AC3_JOC:
      return C.ENCODING_E_AC3_JOC;
    case MimeTypes.AUDIO_AC4:
      return C.ENCODING_AC4;
    case MimeTypes.AUDIO_DTS:
      return C.ENCODING_DTS;
    case MimeTypes.AUDIO_DTS_HD:
      return C.ENCODING_DTS_HD;
    case MimeTypes.AUDIO_TRUEHD:
      return C.ENCODING_DOLBY_TRUEHD;
    default:
      return C.ENCODING_INVALID;
  }
}
 
Example 2
Source File: DefaultAudioSink.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
private static int getFramesPerEncodedSample(@C.Encoding int encoding, ByteBuffer buffer) {
  if (encoding == C.ENCODING_DTS || encoding == C.ENCODING_DTS_HD) {
    return DtsUtil.parseDtsAudioSampleCount(buffer);
  } else if (encoding == C.ENCODING_AC3) {
    return Ac3Util.getAc3SyncframeAudioSampleCount();
  } else if (encoding == C.ENCODING_E_AC3 || encoding == C.ENCODING_E_AC3_JOC) {
    return Ac3Util.parseEAc3SyncframeAudioSampleCount(buffer);
  } else if (encoding == C.ENCODING_AC4) {
    return Ac4Util.parseAc4SyncframeAudioSampleCount(buffer);
  } else if (encoding == C.ENCODING_DOLBY_TRUEHD) {
    int syncframeOffset = Ac3Util.findTrueHdSyncframeOffset(buffer);
    return syncframeOffset == C.INDEX_UNSET
        ? 0
        : (Ac3Util.parseTrueHdSyncframeAudioSampleCount(buffer, syncframeOffset)
            * Ac3Util.TRUEHD_RECHUNK_SAMPLE_COUNT);
  } else {
    throw new IllegalStateException("Unexpected audio encoding: " + encoding);
  }
}
 
Example 3
Source File: ResamplingAudioProcessor.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean configure(int sampleRateHz, int channelCount, @C.Encoding int encoding)
    throws UnhandledFormatException {
  if (encoding != C.ENCODING_PCM_8BIT && encoding != C.ENCODING_PCM_16BIT
      && encoding != C.ENCODING_PCM_24BIT && encoding != C.ENCODING_PCM_32BIT) {
    throw new UnhandledFormatException(sampleRateHz, channelCount, encoding);
  }
  if (this.sampleRateHz == sampleRateHz && this.channelCount == channelCount
      && this.encoding == encoding) {
    return false;
  }
  this.sampleRateHz = sampleRateHz;
  this.channelCount = channelCount;
  this.encoding = encoding;
  return true;
}
 
Example 4
Source File: AudioTrackPositionTracker.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Sets the {@link AudioTrack} to wrap. Subsequent method calls on this instance relate to this
 * track's position, until the next call to {@link #reset()}.
 *
 * @param audioTrack The audio track to wrap.
 * @param outputEncoding The encoding of the audio track.
 * @param outputPcmFrameSize For PCM output encodings, the frame size. The value is ignored
 *     otherwise.
 * @param bufferSize The audio track buffer size in bytes.
 */
public void setAudioTrack(
    AudioTrack audioTrack,
    @C.Encoding int outputEncoding,
    int outputPcmFrameSize,
    int bufferSize) {
  this.audioTrack = audioTrack;
  this.outputPcmFrameSize = outputPcmFrameSize;
  this.bufferSize = bufferSize;
  audioTimestampPoller = new AudioTimestampPoller(audioTrack);
  outputSampleRate = audioTrack.getSampleRate();
  needsPassthroughWorkarounds = needsPassthroughWorkarounds(outputEncoding);
  isOutputPcm = Util.isEncodingLinearPcm(outputEncoding);
  bufferSizeUs = isOutputPcm ? framesToDurationUs(bufferSize / outputPcmFrameSize) : C.TIME_UNSET;
  lastRawPlaybackHeadPosition = 0;
  rawPlaybackHeadWrapCount = 0;
  passthroughWorkaroundPauseOffset = 0;
  hasData = false;
  stopTimestampUs = C.TIME_UNSET;
  forceResetWorkaroundTimeMs = C.TIME_UNSET;
  latencyUs = 0;
}
 
Example 5
Source File: MimeTypes.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the {@link C}{@code .ENCODING_*} constant that corresponds to specified MIME type, if
 * it is an encoded (non-PCM) audio format, or {@link C#ENCODING_INVALID} otherwise.
 *
 * @param mimeType The MIME type.
 * @return The {@link C}{@code .ENCODING_*} constant that corresponds to a specified MIME type, or
 *     {@link C#ENCODING_INVALID}.
 */
public static @C.Encoding int getEncoding(String mimeType) {
  switch (mimeType) {
    case MimeTypes.AUDIO_AC3:
      return C.ENCODING_AC3;
    case MimeTypes.AUDIO_E_AC3:
      return C.ENCODING_E_AC3;
    case MimeTypes.AUDIO_E_AC3_JOC:
      return C.ENCODING_E_AC3_JOC;
    case MimeTypes.AUDIO_AC4:
      return C.ENCODING_AC4;
    case MimeTypes.AUDIO_DTS:
      return C.ENCODING_DTS;
    case MimeTypes.AUDIO_DTS_HD:
      return C.ENCODING_DTS_HD;
    case MimeTypes.AUDIO_TRUEHD:
      return C.ENCODING_DOLBY_TRUEHD;
    default:
      return C.ENCODING_INVALID;
  }
}
 
Example 6
Source File: AudioTrackPositionTracker.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the {@link AudioTrack} to wrap. Subsequent method calls on this instance relate to this
 * track's position, until the next call to {@link #reset()}.
 *
 * @param audioTrack The audio track to wrap.
 * @param outputEncoding The encoding of the audio track.
 * @param outputPcmFrameSize For PCM output encodings, the frame size. The value is ignored
 *     otherwise.
 * @param bufferSize The audio track buffer size in bytes.
 */
public void setAudioTrack(
    AudioTrack audioTrack,
    @C.Encoding int outputEncoding,
    int outputPcmFrameSize,
    int bufferSize) {
  this.audioTrack = audioTrack;
  this.outputPcmFrameSize = outputPcmFrameSize;
  this.bufferSize = bufferSize;
  audioTimestampPoller = new AudioTimestampPoller(audioTrack);
  outputSampleRate = audioTrack.getSampleRate();
  needsPassthroughWorkarounds = needsPassthroughWorkarounds(outputEncoding);
  isOutputPcm = Util.isEncodingLinearPcm(outputEncoding);
  bufferSizeUs = isOutputPcm ? framesToDurationUs(bufferSize / outputPcmFrameSize) : C.TIME_UNSET;
  lastRawPlaybackHeadPosition = 0;
  rawPlaybackHeadWrapCount = 0;
  passthroughWorkaroundPauseOffset = 0;
  hasData = false;
  stopTimestampUs = C.TIME_UNSET;
  forceResetWorkaroundTimeMs = C.TIME_UNSET;
  latencyUs = 0;
}
 
Example 7
Source File: DefaultAudioSink.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private static int getMaximumEncodedRateBytesPerSecond(@C.Encoding int encoding) {
  switch (encoding) {
    case C.ENCODING_AC3:
      return 640 * 1000 / 8;
    case C.ENCODING_E_AC3:
    case C.ENCODING_E_AC3_JOC:
      return 6144 * 1000 / 8;
    case C.ENCODING_AC4:
      return 2688 * 1000 / 8;
    case C.ENCODING_DTS:
      // DTS allows an 'open' bitrate, but we assume the maximum listed value: 1536 kbit/s.
      return 1536 * 1000 / 8;
    case C.ENCODING_DTS_HD:
      return 18000 * 1000 / 8;
    case C.ENCODING_DOLBY_TRUEHD:
      return 24500 * 1000 / 8;
    case C.ENCODING_INVALID:
    case C.ENCODING_PCM_16BIT:
    case C.ENCODING_PCM_24BIT:
    case C.ENCODING_PCM_32BIT:
    case C.ENCODING_PCM_8BIT:
    case C.ENCODING_PCM_A_LAW:
    case C.ENCODING_PCM_FLOAT:
    case C.ENCODING_PCM_MU_LAW:
    case Format.NO_VALUE:
    default:
      throw new IllegalArgumentException();
  }
}
 
Example 8
Source File: MediaCodecAudioRenderer.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onOutputFormatChanged(MediaCodec codec, MediaFormat outputFormat)
    throws ExoPlaybackException {
  @C.Encoding int encoding;
  MediaFormat format;
  if (passthroughMediaFormat != null) {
    encoding = MimeTypes.getEncoding(passthroughMediaFormat.getString(MediaFormat.KEY_MIME));
    format = passthroughMediaFormat;
  } else {
    encoding = pcmEncoding;
    format = outputFormat;
  }
  int channelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
  int sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
  int[] channelMap;
  if (codecNeedsDiscardChannelsWorkaround && channelCount == 6 && this.channelCount < 6) {
    channelMap = new int[this.channelCount];
    for (int i = 0; i < this.channelCount; i++) {
      channelMap[i] = i;
    }
  } else {
    channelMap = null;
  }

  try {
    audioSink.configure(encoding, channelCount, sampleRate, 0, channelMap, encoderDelay,
        encoderPadding);
  } catch (AudioSink.ConfigurationException e) {
    throw ExoPlaybackException.createForRenderer(e, getIndex());
  }
}
 
Example 9
Source File: Util.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether {@code encoding} is one of the linear PCM encodings.
 *
 * @param encoding The encoding of the audio data.
 * @return Whether the encoding is one of the PCM encodings.
 */
public static boolean isEncodingLinearPcm(@C.Encoding int encoding) {
  return encoding == C.ENCODING_PCM_8BIT
      || encoding == C.ENCODING_PCM_16BIT
      || encoding == C.ENCODING_PCM_24BIT
      || encoding == C.ENCODING_PCM_32BIT
      || encoding == C.ENCODING_PCM_FLOAT;
}
 
Example 10
Source File: Util.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether {@code encoding} is one of the linear PCM encodings.
 *
 * @param encoding The encoding of the audio data.
 * @return Whether the encoding is one of the PCM encodings.
 */
public static boolean isEncodingLinearPcm(@C.Encoding int encoding) {
  return encoding == C.ENCODING_PCM_8BIT
      || encoding == C.ENCODING_PCM_16BIT
      || encoding == C.ENCODING_PCM_24BIT
      || encoding == C.ENCODING_PCM_32BIT
      || encoding == C.ENCODING_PCM_FLOAT;
}
 
Example 11
Source File: FfmpegDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns the encoding of output audio.
 */
public @C.Encoding int getEncoding() {
  return encoding;
}
 
Example 12
Source File: AudioProcessor.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public UnhandledFormatException(int sampleRateHz, int channelCount, @C.Encoding int encoding) {
  super("Unhandled format: " + sampleRateHz + " Hz, " + channelCount + " channels in encoding "
      + encoding);
}
 
Example 13
Source File: AudioProcessor.java    From K-Sonic with MIT License 4 votes vote down vote up
public UnhandledFormatException(int sampleRateHz, int channelCount, @C.Encoding int encoding) {
  super("Unhandled format: " + sampleRateHz + " Hz, " + channelCount + " channels in encoding "
      + encoding);
}
 
Example 14
Source File: DefaultAudioSink.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Override
public void configure(
    @C.Encoding int inputEncoding,
    int inputChannelCount,
    int inputSampleRate,
    int specifiedBufferSize,
    @Nullable int[] outputChannels,
    int trimStartFrames,
    int trimEndFrames)
    throws ConfigurationException {
  if (Util.SDK_INT < 21 && inputChannelCount == 8 && outputChannels == null) {
    // AudioTrack doesn't support 8 channel output before Android L. Discard the last two (side)
    // channels to give a 6 channel stream that is supported.
    outputChannels = new int[6];
    for (int i = 0; i < outputChannels.length; i++) {
      outputChannels[i] = i;
    }
  }

  boolean isInputPcm = Util.isEncodingLinearPcm(inputEncoding);
  boolean processingEnabled = isInputPcm && inputEncoding != C.ENCODING_PCM_FLOAT;
  int sampleRate = inputSampleRate;
  int channelCount = inputChannelCount;
  @C.Encoding int encoding = inputEncoding;
  boolean shouldConvertHighResIntPcmToFloat =
      enableConvertHighResIntPcmToFloat
          && supportsOutput(inputChannelCount, C.ENCODING_PCM_FLOAT)
          && Util.isEncodingHighResolutionIntegerPcm(inputEncoding);
  AudioProcessor[] availableAudioProcessors =
      shouldConvertHighResIntPcmToFloat
          ? toFloatPcmAvailableAudioProcessors
          : toIntPcmAvailableAudioProcessors;
  if (processingEnabled) {
    trimmingAudioProcessor.setTrimFrameCount(trimStartFrames, trimEndFrames);
    channelMappingAudioProcessor.setChannelMap(outputChannels);
    AudioProcessor.AudioFormat inputAudioFormat =
        new AudioProcessor.AudioFormat(sampleRate, channelCount, encoding);
    AudioProcessor.AudioFormat outputAudioFormat = inputAudioFormat;
    for (AudioProcessor audioProcessor : availableAudioProcessors) {
      try {
        outputAudioFormat = audioProcessor.configure(inputAudioFormat);
      } catch (UnhandledAudioFormatException e) {
        throw new ConfigurationException(e);
      }
      if (audioProcessor.isActive()) {
        inputAudioFormat = outputAudioFormat;
      }
    }
    sampleRate = outputAudioFormat.sampleRate;
    channelCount = outputAudioFormat.channelCount;
    encoding = outputAudioFormat.encoding;
  }

  int outputChannelConfig = getChannelConfig(channelCount, isInputPcm);
  if (outputChannelConfig == AudioFormat.CHANNEL_INVALID) {
    throw new ConfigurationException("Unsupported channel count: " + channelCount);
  }

  int inputPcmFrameSize =
      isInputPcm ? Util.getPcmFrameSize(inputEncoding, inputChannelCount) : C.LENGTH_UNSET;
  int outputPcmFrameSize =
      isInputPcm ? Util.getPcmFrameSize(encoding, channelCount) : C.LENGTH_UNSET;
  boolean canApplyPlaybackParameters = processingEnabled && !shouldConvertHighResIntPcmToFloat;
  Configuration pendingConfiguration =
      new Configuration(
          isInputPcm,
          inputPcmFrameSize,
          inputSampleRate,
          outputPcmFrameSize,
          sampleRate,
          outputChannelConfig,
          encoding,
          specifiedBufferSize,
          processingEnabled,
          canApplyPlaybackParameters,
          availableAudioProcessors);
  if (isInitialized()) {
    this.pendingConfiguration = pendingConfiguration;
  } else {
    configuration = pendingConfiguration;
  }
}
 
Example 15
Source File: AudioTrackPositionTracker.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
/**
 * Returns whether to work around problems with passthrough audio tracks. See [Internal:
 * b/18899620, b/19187573, b/21145353].
 */
private static boolean needsPassthroughWorkarounds(@C.Encoding int outputEncoding) {
  return Util.SDK_INT < 23
      && (outputEncoding == C.ENCODING_AC3 || outputEncoding == C.ENCODING_E_AC3);
}
 
Example 16
Source File: MediaCodecAudioRenderer.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Override
protected void onOutputFormatChanged(MediaCodec codec, MediaFormat outputMediaFormat)
    throws ExoPlaybackException {
  @C.Encoding int encoding;
  MediaFormat mediaFormat;
  if (passthroughMediaFormat != null) {
    mediaFormat = passthroughMediaFormat;
    encoding =
        getPassthroughEncoding(
            mediaFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT),
            mediaFormat.getString(MediaFormat.KEY_MIME));
  } else {
    mediaFormat = outputMediaFormat;
    encoding = getPcmEncoding(inputFormat);
  }
  int channelCount = mediaFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
  int sampleRate = mediaFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE);
  int[] channelMap;
  if (codecNeedsDiscardChannelsWorkaround && channelCount == 6 && inputFormat.channelCount < 6) {
    channelMap = new int[inputFormat.channelCount];
    for (int i = 0; i < inputFormat.channelCount; i++) {
      channelMap[i] = i;
    }
  } else {
    channelMap = null;
  }

  try {
    audioSink.configure(
        encoding,
        channelCount,
        sampleRate,
        0,
        channelMap,
        inputFormat.encoderDelay,
        inputFormat.encoderPadding);
  } catch (AudioSink.ConfigurationException e) {
    // TODO(internal: b/145658993) Use outputFormat instead.
    throw createRendererException(e, inputFormat);
  }
}
 
Example 17
Source File: AudioTrackPositionTracker.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns whether to work around problems with passthrough audio tracks. See [Internal:
 * b/18899620, b/19187573, b/21145353].
 */
private static boolean needsPassthroughWorkarounds(@C.Encoding int outputEncoding) {
  return Util.SDK_INT < 23
      && (outputEncoding == C.ENCODING_AC3 || outputEncoding == C.ENCODING_E_AC3);
}
 
Example 18
Source File: AudioProcessor.java    From TelePlus-Android with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the audio encoding used in the data output by the processor. The value may change as a
 * result of calling {@link #configure(int, int, int)} and is undefined if the instance is not
 * active.
 */
@C.Encoding
int getOutputEncoding();
 
Example 19
Source File: SimpleDecoderAudioRenderer.java    From Telegram with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns whether the sink supports the audio format.
 *
 * @see AudioSink#supportsOutput(int, int)
 */
protected final boolean supportsOutput(int channelCount, @C.Encoding int encoding) {
  return audioSink.supportsOutput(channelCount, encoding);
}
 
Example 20
Source File: AudioProcessor.java    From K-Sonic with MIT License 2 votes vote down vote up
/**
 * Configures the processor to process input audio with the specified format. After calling this
 * method, {@link #isActive()} returns whether the processor needs to handle buffers; if not, the
 * processor will not accept any buffers until it is reconfigured. Returns {@code true} if the
 * processor must be flushed, or if the value returned by {@link #isActive()} has changed as a
 * result of the call. If it's active, {@link #getOutputChannelCount()} and
 * {@link #getOutputEncoding()} return the processor's output format.
 *
 * @param sampleRateHz The sample rate of input audio in Hz.
 * @param channelCount The number of interleaved channels in input audio.
 * @param encoding The encoding of input audio.
 * @return {@code true} if the processor must be flushed or the value returned by
 *     {@link #isActive()} has changed as a result of the call.
 * @throws UnhandledFormatException Thrown if the specified format can't be handled as input.
 */
boolean configure(int sampleRateHz, int channelCount, @C.Encoding int encoding)
    throws UnhandledFormatException;