Java Code Examples for android.media.MediaFormat#setFloat()

The following examples show how to use android.media.MediaFormat#setFloat() . 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: MediaCodecAudioRenderer.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the framework {@link MediaFormat} that can be used to configure a {@link MediaCodec}
 * for decoding the given {@link Format} for playback.
 *
 * @param format The {@link Format} of the media.
 * @param codecMimeType The MIME type handled by the codec.
 * @param codecMaxInputSize The maximum input size supported by the codec.
 * @param codecOperatingRate The codec operating rate, or {@link #CODEC_OPERATING_RATE_UNSET} if
 *     no codec operating rate should be set.
 * @return The framework {@link MediaFormat}.
 */
@SuppressLint("InlinedApi")
protected MediaFormat getMediaFormat(
    Format format, String codecMimeType, int codecMaxInputSize, float codecOperatingRate) {
  MediaFormat mediaFormat = new MediaFormat();
  // Set format parameters that should always be set.
  mediaFormat.setString(MediaFormat.KEY_MIME, codecMimeType);
  mediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, format.channelCount);
  mediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, format.sampleRate);
  MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
  // Set codec max values.
  MediaFormatUtil.maybeSetInteger(mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, codecMaxInputSize);
  // Set codec configuration values.
  if (Util.SDK_INT >= 23) {
    mediaFormat.setInteger(MediaFormat.KEY_PRIORITY, 0 /* realtime priority */);
    if (codecOperatingRate != CODEC_OPERATING_RATE_UNSET && !deviceDoesntSupportOperatingRate()) {
      mediaFormat.setFloat(MediaFormat.KEY_OPERATING_RATE, codecOperatingRate);
    }
  }
  if (Util.SDK_INT <= 28 && MimeTypes.AUDIO_AC4.equals(format.sampleMimeType)) {
    // On some older builds, the AC-4 decoder expects to receive samples formatted as raw frames
    // not sync frames. Set a format key to override this.
    mediaFormat.setInteger("ac4-is-sync", 1);
  }
  return mediaFormat;
}
 
Example 2
Source File: MediaCodecAudioRenderer.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the framework {@link MediaFormat} that can be used to configure a {@link MediaCodec}
 * for decoding the given {@link Format} for playback.
 *
 * @param format The format of the media.
 * @param codecMimeType The MIME type handled by the codec.
 * @param codecMaxInputSize The maximum input size supported by the codec.
 * @param codecOperatingRate The codec operating rate, or {@link #CODEC_OPERATING_RATE_UNSET} if
 *     no codec operating rate should be set.
 * @return The framework media format.
 */
@SuppressLint("InlinedApi")
protected MediaFormat getMediaFormat(
    Format format, String codecMimeType, int codecMaxInputSize, float codecOperatingRate) {
  MediaFormat mediaFormat = new MediaFormat();
  // Set format parameters that should always be set.
  mediaFormat.setString(MediaFormat.KEY_MIME, codecMimeType);
  mediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, format.channelCount);
  mediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, format.sampleRate);
  MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
  // Set codec max values.
  MediaFormatUtil.maybeSetInteger(mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, codecMaxInputSize);
  // Set codec configuration values.
  if (Util.SDK_INT >= 23) {
    mediaFormat.setInteger(MediaFormat.KEY_PRIORITY, 0 /* realtime priority */);
    if (codecOperatingRate != CODEC_OPERATING_RATE_UNSET && !deviceDoesntSupportOperatingRate()) {
      mediaFormat.setFloat(MediaFormat.KEY_OPERATING_RATE, codecOperatingRate);
    }
  }
  if (Util.SDK_INT <= 28 && MimeTypes.AUDIO_AC4.equals(format.sampleMimeType)) {
    // On some older builds, the AC-4 decoder expects to receive samples formatted as raw frames
    // not sync frames. Set a format key to override this.
    mediaFormat.setInteger("ac4-is-sync", 1);
  }
  return mediaFormat;
}
 
Example 3
Source File: MediaCodecAudioRenderer.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the framework {@link MediaFormat} that can be used to configure a {@link MediaCodec}
 * for decoding the given {@link Format} for playback.
 *
 * @param format The format of the media.
 * @param codecMimeType The MIME type handled by the codec.
 * @param codecMaxInputSize The maximum input size supported by the codec.
 * @param codecOperatingRate The codec operating rate, or {@link #CODEC_OPERATING_RATE_UNSET} if
 *     no codec operating rate should be set.
 * @return The framework media format.
 */
@SuppressLint("InlinedApi")
protected MediaFormat getMediaFormat(
    Format format, String codecMimeType, int codecMaxInputSize, float codecOperatingRate) {
  MediaFormat mediaFormat = new MediaFormat();
  // Set format parameters that should always be set.
  mediaFormat.setString(MediaFormat.KEY_MIME, codecMimeType);
  mediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, format.channelCount);
  mediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, format.sampleRate);
  MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
  // Set codec max values.
  MediaFormatUtil.maybeSetInteger(mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, codecMaxInputSize);
  // Set codec configuration values.
  if (Util.SDK_INT >= 23) {
    mediaFormat.setInteger(MediaFormat.KEY_PRIORITY, 0 /* realtime priority */);
    if (codecOperatingRate != CODEC_OPERATING_RATE_UNSET && !deviceDoesntSupportOperatingRate()) {
      mediaFormat.setFloat(MediaFormat.KEY_OPERATING_RATE, codecOperatingRate);
    }
  }
  if (Util.SDK_INT <= 28 && MimeTypes.AUDIO_AC4.equals(format.sampleMimeType)) {
    // On some older builds, the AC-4 decoder expects to receive samples formatted as raw frames
    // not sync frames. Set a format key to override this.
    mediaFormat.setInteger("ac4-is-sync", 1);
  }
  return mediaFormat;
}
 
Example 4
Source File: MediaExtractor.java    From MediaPlayer-Extended with Apache License 2.0 6 votes vote down vote up
/**
 * Get the track format at the specified index.
 * More detail on the representation can be found at {@link android.media.MediaCodec}
 */
public MediaFormat getTrackFormat(int index) {
    MediaFormat mediaFormat = mApiExtractor.getTrackFormat(index);
    String mime = mediaFormat.getString(MediaFormat.KEY_MIME);

    // Set the default DAR
    //
    // We need to check the existence of the width/height fields because some platforms
    // return unsupported tracks as "video/unknown" mime type without the required fields to
    // calculate the DAR.
    //
    // Example:
    // Samsung Galaxy S5 Android 6.0.1 with thumbnail tracks (jpeg image)
    // MediaFormat{error-type=-1002, mime=video/unknown, isDMCMMExtractor=1, durationUs=323323000}
    if(mime.startsWith("video/")
            && mediaFormat.containsKey(MediaFormat.KEY_WIDTH)
            && mediaFormat.containsKey(MediaFormat.KEY_HEIGHT)) {
        mediaFormat.setFloat(MEDIA_FORMAT_EXTENSION_KEY_DAR,
                (float)mediaFormat.getInteger(MediaFormat.KEY_WIDTH)
                        / mediaFormat.getInteger(MediaFormat.KEY_HEIGHT));
    }

    return mediaFormat;
}
 
Example 5
Source File: DashMediaExtractor.java    From MediaPlayer-Extended with Apache License 2.0 6 votes vote down vote up
@Override
public MediaFormat getTrackFormat(int index) {
    MediaFormat mediaFormat = super.getTrackFormat(index);
    if(mMp4Mode) {
        /* An MP4 that has been converted from a fragmented to an unfragmented container
         * through the isoparser library does only contain the current segment's runtime. To
         * return the total runtime, we take the value from the MPD instead.
         */
        mediaFormat.setLong(MediaFormat.KEY_DURATION, mMPD.mediaPresentationDurationUs);
    }
    if(mediaFormat.getString(MediaFormat.KEY_MIME).startsWith("video/")) {
        // Return the display aspect ratio as defined in the MPD (can be different from the encoded video size)
        mediaFormat.setFloat(MEDIA_FORMAT_EXTENSION_KEY_DAR,
                mAdaptationSet.hasPAR() ? mAdaptationSet.par : mRepresentation.calculatePAR());
    }
    return mediaFormat;
}
 
Example 6
Source File: CameraWriter.java    From ssj with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Configures the encoder
 */
private void prepareEncoder(int width, int height, int bitRate) throws IOException
{
    MediaCodecInfo mediaCodecInfo = CameraUtil.selectCodec(options.mimeType.get());
    if (mediaCodecInfo == null)
    {
        throw new IOException("Unable to find an appropriate codec for " + options.mimeType.get());
    }
    //set format properties
    MediaFormat videoFormat = MediaFormat.createVideoFormat(options.mimeType.get(), width, height);
    videoFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, options.colorFormat.get() <= 0
            ? CameraUtil.selectColorFormat(mediaCodecInfo, options.mimeType.get()) : options.colorFormat.get());
    videoFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
    videoFormat.setFloat(MediaFormat.KEY_FRAME_RATE, (float) dFrameRate);
    videoFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, options.iFrameInterval.get());
    //prepare encoder
    super.prepareEncoder(videoFormat, options.mimeType.get(), file.getPath());
    //set video orientation
    if (options.orientation.get() % 90 == 0 || options.orientation.get() % 90 == 90)
    {
        mediaMuxer.setOrientationHint(options.orientation.get());
    } else
    {
        Log.w("Orientation is not valid: " + options.orientation.get());
    }
}
 
Example 7
Source File: MediaCodecAudioRenderer.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the framework {@link MediaFormat} that can be used to configure a {@link MediaCodec}
 * for decoding the given {@link Format} for playback.
 *
 * @param format The format of the media.
 * @param codecMimeType The MIME type handled by the codec.
 * @param codecMaxInputSize The maximum input size supported by the codec.
 * @param codecOperatingRate The codec operating rate, or {@link #CODEC_OPERATING_RATE_UNSET} if
 *     no codec operating rate should be set.
 * @return The framework media format.
 */
@SuppressLint("InlinedApi")
protected MediaFormat getMediaFormat(
    Format format, String codecMimeType, int codecMaxInputSize, float codecOperatingRate) {
  MediaFormat mediaFormat = new MediaFormat();
  // Set format parameters that should always be set.
  mediaFormat.setString(MediaFormat.KEY_MIME, codecMimeType);
  mediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, format.channelCount);
  mediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, format.sampleRate);
  MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
  // Set codec max values.
  MediaFormatUtil.maybeSetInteger(mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, codecMaxInputSize);
  // Set codec configuration values.
  if (Util.SDK_INT >= 23) {
    mediaFormat.setInteger(MediaFormat.KEY_PRIORITY, 0 /* realtime priority */);
    if (codecOperatingRate != CODEC_OPERATING_RATE_UNSET) {
      mediaFormat.setFloat(MediaFormat.KEY_OPERATING_RATE, codecOperatingRate);
    }
  }
  return mediaFormat;
}
 
Example 8
Source File: MediaCodecAudioRenderer.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the framework {@link MediaFormat} that can be used to configure a {@link MediaCodec}
 * for decoding the given {@link Format} for playback.
 *
 * @param format The format of the media.
 * @param codecMimeType The MIME type handled by the codec.
 * @param codecMaxInputSize The maximum input size supported by the codec.
 * @param codecOperatingRate The codec operating rate, or {@link #CODEC_OPERATING_RATE_UNSET} if
 *     no codec operating rate should be set.
 * @return The framework media format.
 */
@SuppressLint("InlinedApi")
protected MediaFormat getMediaFormat(
    Format format, String codecMimeType, int codecMaxInputSize, float codecOperatingRate) {
  MediaFormat mediaFormat = new MediaFormat();
  // Set format parameters that should always be set.
  mediaFormat.setString(MediaFormat.KEY_MIME, codecMimeType);
  mediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, format.channelCount);
  mediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, format.sampleRate);
  MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
  // Set codec max values.
  MediaFormatUtil.maybeSetInteger(mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, codecMaxInputSize);
  // Set codec configuration values.
  if (Util.SDK_INT >= 23) {
    mediaFormat.setInteger(MediaFormat.KEY_PRIORITY, 0 /* realtime priority */);
    if (codecOperatingRate != CODEC_OPERATING_RATE_UNSET) {
      mediaFormat.setFloat(MediaFormat.KEY_OPERATING_RATE, codecOperatingRate);
    }
  }
  return mediaFormat;
}
 
Example 9
Source File: MediaCodecVideoRenderer.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the framework {@link MediaFormat} that should be used to configure the decoder.
 *
 * @param format The format of media.
 * @param codecMaxValues Codec max values that should be used when configuring the decoder.
 * @param codecOperatingRate The codec operating rate, or {@link #CODEC_OPERATING_RATE_UNSET} if
 *     no codec operating rate should be set.
 * @param deviceNeedsAutoFrcWorkaround Whether the device is known to enable frame-rate conversion
 *     logic that negatively impacts ExoPlayer.
 * @param tunnelingAudioSessionId The audio session id to use for tunneling, or {@link
 *     C#AUDIO_SESSION_ID_UNSET} if tunneling should not be enabled.
 * @return The framework {@link MediaFormat} that should be used to configure the decoder.
 */
@SuppressLint("InlinedApi")
protected MediaFormat getMediaFormat(
    Format format,
    CodecMaxValues codecMaxValues,
    float codecOperatingRate,
    boolean deviceNeedsAutoFrcWorkaround,
    int tunnelingAudioSessionId) {
  MediaFormat mediaFormat = new MediaFormat();
  // Set format parameters that should always be set.
  mediaFormat.setString(MediaFormat.KEY_MIME, format.sampleMimeType);
  mediaFormat.setInteger(MediaFormat.KEY_WIDTH, format.width);
  mediaFormat.setInteger(MediaFormat.KEY_HEIGHT, format.height);
  MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
  // Set format parameters that may be unset.
  MediaFormatUtil.maybeSetFloat(mediaFormat, MediaFormat.KEY_FRAME_RATE, format.frameRate);
  MediaFormatUtil.maybeSetInteger(mediaFormat, MediaFormat.KEY_ROTATION, format.rotationDegrees);
  MediaFormatUtil.maybeSetColorInfo(mediaFormat, format.colorInfo);
  // Set codec max values.
  mediaFormat.setInteger(MediaFormat.KEY_MAX_WIDTH, codecMaxValues.width);
  mediaFormat.setInteger(MediaFormat.KEY_MAX_HEIGHT, codecMaxValues.height);
  MediaFormatUtil.maybeSetInteger(
      mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, codecMaxValues.inputSize);
  // Set codec configuration values.
  if (Util.SDK_INT >= 23) {
    mediaFormat.setInteger(MediaFormat.KEY_PRIORITY, 0 /* realtime priority */);
    if (codecOperatingRate != CODEC_OPERATING_RATE_UNSET) {
      mediaFormat.setFloat(MediaFormat.KEY_OPERATING_RATE, codecOperatingRate);
    }
  }
  if (deviceNeedsAutoFrcWorkaround) {
    mediaFormat.setInteger("auto-frc", 0);
  }
  if (tunnelingAudioSessionId != C.AUDIO_SESSION_ID_UNSET) {
    configureTunnelingV21(mediaFormat, tunnelingAudioSessionId);
  }
  return mediaFormat;
}
 
Example 10
Source File: MediaCodecVideoRenderer.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the framework {@link MediaFormat} that should be used to configure the decoder.
 *
 * @param format The format of media.
 * @param codecMaxValues Codec max values that should be used when configuring the decoder.
 * @param codecOperatingRate The codec operating rate, or {@link #CODEC_OPERATING_RATE_UNSET} if
 *     no codec operating rate should be set.
 * @param deviceNeedsAutoFrcWorkaround Whether the device is known to enable frame-rate conversion
 *     logic that negatively impacts ExoPlayer.
 * @param tunnelingAudioSessionId The audio session id to use for tunneling, or {@link
 *     C#AUDIO_SESSION_ID_UNSET} if tunneling should not be enabled.
 * @return The framework {@link MediaFormat} that should be used to configure the decoder.
 */
@SuppressLint("InlinedApi")
protected MediaFormat getMediaFormat(
    Format format,
    CodecMaxValues codecMaxValues,
    float codecOperatingRate,
    boolean deviceNeedsAutoFrcWorkaround,
    int tunnelingAudioSessionId) {
  MediaFormat mediaFormat = new MediaFormat();
  // Set format parameters that should always be set.
  mediaFormat.setString(MediaFormat.KEY_MIME, format.sampleMimeType);
  mediaFormat.setInteger(MediaFormat.KEY_WIDTH, format.width);
  mediaFormat.setInteger(MediaFormat.KEY_HEIGHT, format.height);
  MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
  // Set format parameters that may be unset.
  MediaFormatUtil.maybeSetFloat(mediaFormat, MediaFormat.KEY_FRAME_RATE, format.frameRate);
  MediaFormatUtil.maybeSetInteger(mediaFormat, MediaFormat.KEY_ROTATION, format.rotationDegrees);
  MediaFormatUtil.maybeSetColorInfo(mediaFormat, format.colorInfo);
  // Set codec max values.
  mediaFormat.setInteger(MediaFormat.KEY_MAX_WIDTH, codecMaxValues.width);
  mediaFormat.setInteger(MediaFormat.KEY_MAX_HEIGHT, codecMaxValues.height);
  MediaFormatUtil.maybeSetInteger(
      mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, codecMaxValues.inputSize);
  // Set codec configuration values.
  if (Util.SDK_INT >= 23) {
    mediaFormat.setInteger(MediaFormat.KEY_PRIORITY, 0 /* realtime priority */);
    if (codecOperatingRate != CODEC_OPERATING_RATE_UNSET) {
      mediaFormat.setFloat(MediaFormat.KEY_OPERATING_RATE, codecOperatingRate);
    }
  }
  if (deviceNeedsAutoFrcWorkaround) {
    mediaFormat.setInteger("auto-frc", 0);
  }
  if (tunnelingAudioSessionId != C.AUDIO_SESSION_ID_UNSET) {
    configureTunnelingV21(mediaFormat, tunnelingAudioSessionId);
  }
  return mediaFormat;
}
 
Example 11
Source File: DefaultStrategy.java    From GIFCompressor with Apache License 2.0 4 votes vote down vote up
@Override
public void createOutputFormat(@NonNull List<MediaFormat> inputFormats,
                               @NonNull MediaFormat outputFormat) {

    // Compute output size in rotation=0 reference.
    ExactSize inSize = getBestInputSize(inputFormats);
    int inWidth = inSize.getWidth();
    int inHeight = inSize.getHeight();
    LOG.i("Input width&height: " + inWidth + "x" + inHeight);
    Size outSize;
    try {
        outSize = options.resizer.getOutputSize(inSize);
    } catch (Exception e) {
        throw new RuntimeException("Resizer error:", e);
    }
    int outWidth, outHeight;
    if (outSize instanceof ExactSize) {
        outWidth = ((ExactSize) outSize).getWidth();
        outHeight = ((ExactSize) outSize).getHeight();
    } else if (inWidth >= inHeight) {
        outWidth = outSize.getMajor();
        outHeight = outSize.getMinor();
    } else {
        outWidth = outSize.getMinor();
        outHeight = outSize.getMajor();
    }
    LOG.i("Output width&height: " + outWidth + "x" + outHeight);

    // Compute output frame rate. It can't be bigger than input frame rate.
    int outFrameRate;
    int inputFrameRate = getMinFrameRate(inputFormats);
    if (inputFrameRate > 0) {
        outFrameRate = Math.min(inputFrameRate, options.targetFrameRate);
    } else {
        outFrameRate = options.targetFrameRate;
    }

    // Create the actual format.
    outputFormat.setString(MediaFormat.KEY_MIME, options.targetMimeType);
    outputFormat.setInteger(MediaFormat.KEY_WIDTH, outWidth);
    outputFormat.setInteger(MediaFormat.KEY_HEIGHT, outHeight);
    outputFormat.setInteger(MediaFormatConstants.KEY_ROTATION_DEGREES, 0);
    outputFormat.setInteger(MediaFormat.KEY_FRAME_RATE, outFrameRate);
    if (Build.VERSION.SDK_INT >= 25) {
        outputFormat.setFloat(MediaFormat.KEY_I_FRAME_INTERVAL, options.targetKeyFrameInterval);
    } else {
        outputFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, (int) Math.ceil(options.targetKeyFrameInterval));
    }
    outputFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
    int outBitRate = (int) (options.targetBitRate == BITRATE_UNKNOWN ?
            estimateBitRate(outWidth, outHeight, outFrameRate) : options.targetBitRate);
    outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, outBitRate);
}
 
Example 12
Source File: Format.java    From K-Sonic with MIT License 4 votes vote down vote up
@TargetApi(16)
private static void maybeSetFloatV16(MediaFormat format, String key, float value) {
  if (value != NO_VALUE) {
    format.setFloat(key, value);
  }
}
 
Example 13
Source File: MediaCodecVideoRenderer.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the framework {@link MediaFormat} that should be used to configure the decoder.
 *
 * @param format The {@link Format} of media.
 * @param codecMimeType The MIME type handled by the codec.
 * @param codecMaxValues Codec max values that should be used when configuring the decoder.
 * @param codecOperatingRate The codec operating rate, or {@link #CODEC_OPERATING_RATE_UNSET} if
 *     no codec operating rate should be set.
 * @param deviceNeedsNoPostProcessWorkaround Whether the device is known to do post processing by
 *     default that isn't compatible with ExoPlayer.
 * @param tunnelingAudioSessionId The audio session id to use for tunneling, or {@link
 *     C#AUDIO_SESSION_ID_UNSET} if tunneling should not be enabled.
 * @return The framework {@link MediaFormat} that should be used to configure the decoder.
 */
@SuppressLint("InlinedApi")
protected MediaFormat getMediaFormat(
    Format format,
    String codecMimeType,
    CodecMaxValues codecMaxValues,
    float codecOperatingRate,
    boolean deviceNeedsNoPostProcessWorkaround,
    int tunnelingAudioSessionId) {
  MediaFormat mediaFormat = new MediaFormat();
  // Set format parameters that should always be set.
  mediaFormat.setString(MediaFormat.KEY_MIME, codecMimeType);
  mediaFormat.setInteger(MediaFormat.KEY_WIDTH, format.width);
  mediaFormat.setInteger(MediaFormat.KEY_HEIGHT, format.height);
  MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
  // Set format parameters that may be unset.
  MediaFormatUtil.maybeSetFloat(mediaFormat, MediaFormat.KEY_FRAME_RATE, format.frameRate);
  MediaFormatUtil.maybeSetInteger(mediaFormat, MediaFormat.KEY_ROTATION, format.rotationDegrees);
  MediaFormatUtil.maybeSetColorInfo(mediaFormat, format.colorInfo);
  if (MimeTypes.VIDEO_DOLBY_VISION.equals(format.sampleMimeType)) {
    // Some phones require the profile to be set on the codec.
    // See https://github.com/google/ExoPlayer/pull/5438.
    Pair<Integer, Integer> codecProfileAndLevel = MediaCodecUtil.getCodecProfileAndLevel(format);
    if (codecProfileAndLevel != null) {
      MediaFormatUtil.maybeSetInteger(
          mediaFormat, MediaFormat.KEY_PROFILE, codecProfileAndLevel.first);
    }
  }
  // Set codec max values.
  mediaFormat.setInteger(MediaFormat.KEY_MAX_WIDTH, codecMaxValues.width);
  mediaFormat.setInteger(MediaFormat.KEY_MAX_HEIGHT, codecMaxValues.height);
  MediaFormatUtil.maybeSetInteger(
      mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, codecMaxValues.inputSize);
  // Set codec configuration values.
  if (Util.SDK_INT >= 23) {
    mediaFormat.setInteger(MediaFormat.KEY_PRIORITY, 0 /* realtime priority */);
    if (codecOperatingRate != CODEC_OPERATING_RATE_UNSET) {
      mediaFormat.setFloat(MediaFormat.KEY_OPERATING_RATE, codecOperatingRate);
    }
  }
  if (deviceNeedsNoPostProcessWorkaround) {
    mediaFormat.setInteger("no-post-process", 1);
    mediaFormat.setInteger("auto-frc", 0);
  }
  if (tunnelingAudioSessionId != C.AUDIO_SESSION_ID_UNSET) {
    configureTunnelingV21(mediaFormat, tunnelingAudioSessionId);
  }
  return mediaFormat;
}
 
Example 14
Source File: MediaCodecVideoRenderer.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns the framework {@link MediaFormat} that should be used to configure the decoder.
 *
 * @param format The format of media.
 * @param codecMimeType The MIME type handled by the codec.
 * @param codecMaxValues Codec max values that should be used when configuring the decoder.
 * @param codecOperatingRate The codec operating rate, or {@link #CODEC_OPERATING_RATE_UNSET} if
 *     no codec operating rate should be set.
 * @param deviceNeedsNoPostProcessWorkaround Whether the device is known to do post processing by
 *     default that isn't compatible with ExoPlayer.
 * @param tunnelingAudioSessionId The audio session id to use for tunneling, or {@link
 *     C#AUDIO_SESSION_ID_UNSET} if tunneling should not be enabled.
 * @return The framework {@link MediaFormat} that should be used to configure the decoder.
 */
@SuppressLint("InlinedApi")
protected MediaFormat getMediaFormat(
    Format format,
    String codecMimeType,
    CodecMaxValues codecMaxValues,
    float codecOperatingRate,
    boolean deviceNeedsNoPostProcessWorkaround,
    int tunnelingAudioSessionId) {
  MediaFormat mediaFormat = new MediaFormat();
  // Set format parameters that should always be set.
  mediaFormat.setString(MediaFormat.KEY_MIME, codecMimeType);
  mediaFormat.setInteger(MediaFormat.KEY_WIDTH, format.width);
  mediaFormat.setInteger(MediaFormat.KEY_HEIGHT, format.height);
  MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
  // Set format parameters that may be unset.
  MediaFormatUtil.maybeSetFloat(mediaFormat, MediaFormat.KEY_FRAME_RATE, format.frameRate);
  MediaFormatUtil.maybeSetInteger(mediaFormat, MediaFormat.KEY_ROTATION, format.rotationDegrees);
  MediaFormatUtil.maybeSetColorInfo(mediaFormat, format.colorInfo);
  if (MimeTypes.VIDEO_DOLBY_VISION.equals(format.sampleMimeType)) {
    // Some phones require the profile to be set on the codec.
    // See https://github.com/google/ExoPlayer/pull/5438.
    Pair<Integer, Integer> codecProfileAndLevel =
        MediaCodecUtil.getCodecProfileAndLevel(format.codecs);
    if (codecProfileAndLevel != null) {
      MediaFormatUtil.maybeSetInteger(
          mediaFormat, MediaFormat.KEY_PROFILE, codecProfileAndLevel.first);
    }
  }
  // Set codec max values.
  mediaFormat.setInteger(MediaFormat.KEY_MAX_WIDTH, codecMaxValues.width);
  mediaFormat.setInteger(MediaFormat.KEY_MAX_HEIGHT, codecMaxValues.height);
  MediaFormatUtil.maybeSetInteger(
      mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, codecMaxValues.inputSize);
  // Set codec configuration values.
  if (Util.SDK_INT >= 23) {
    mediaFormat.setInteger(MediaFormat.KEY_PRIORITY, 0 /* realtime priority */);
    if (codecOperatingRate != CODEC_OPERATING_RATE_UNSET) {
      mediaFormat.setFloat(MediaFormat.KEY_OPERATING_RATE, codecOperatingRate);
    }
  }
  if (deviceNeedsNoPostProcessWorkaround) {
    mediaFormat.setInteger("no-post-process", 1);
    mediaFormat.setInteger("auto-frc", 0);
  }
  if (tunnelingAudioSessionId != C.AUDIO_SESSION_ID_UNSET) {
    configureTunnelingV21(mediaFormat, tunnelingAudioSessionId);
  }
  return mediaFormat;
}
 
Example 15
Source File: MediaCodecVideoRenderer.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns the framework {@link MediaFormat} that should be used to configure the decoder.
 *
 * @param format The format of media.
 * @param codecMimeType The MIME type handled by the codec.
 * @param codecMaxValues Codec max values that should be used when configuring the decoder.
 * @param codecOperatingRate The codec operating rate, or {@link #CODEC_OPERATING_RATE_UNSET} if
 *     no codec operating rate should be set.
 * @param deviceNeedsNoPostProcessWorkaround Whether the device is known to do post processing by
 *     default that isn't compatible with ExoPlayer.
 * @param tunnelingAudioSessionId The audio session id to use for tunneling, or {@link
 *     C#AUDIO_SESSION_ID_UNSET} if tunneling should not be enabled.
 * @return The framework {@link MediaFormat} that should be used to configure the decoder.
 */
@SuppressLint("InlinedApi")
protected MediaFormat getMediaFormat(
    Format format,
    String codecMimeType,
    CodecMaxValues codecMaxValues,
    float codecOperatingRate,
    boolean deviceNeedsNoPostProcessWorkaround,
    int tunnelingAudioSessionId) {
  MediaFormat mediaFormat = new MediaFormat();
  // Set format parameters that should always be set.
  mediaFormat.setString(MediaFormat.KEY_MIME, codecMimeType);
  mediaFormat.setInteger(MediaFormat.KEY_WIDTH, format.width);
  mediaFormat.setInteger(MediaFormat.KEY_HEIGHT, format.height);
  MediaFormatUtil.setCsdBuffers(mediaFormat, format.initializationData);
  // Set format parameters that may be unset.
  MediaFormatUtil.maybeSetFloat(mediaFormat, MediaFormat.KEY_FRAME_RATE, format.frameRate);
  MediaFormatUtil.maybeSetInteger(mediaFormat, MediaFormat.KEY_ROTATION, format.rotationDegrees);
  MediaFormatUtil.maybeSetColorInfo(mediaFormat, format.colorInfo);
  if (MimeTypes.VIDEO_DOLBY_VISION.equals(format.sampleMimeType)) {
    // Some phones require the profile to be set on the codec.
    // See https://github.com/google/ExoPlayer/pull/5438.
    Pair<Integer, Integer> codecProfileAndLevel =
        MediaCodecUtil.getCodecProfileAndLevel(format.codecs);
    if (codecProfileAndLevel != null) {
      MediaFormatUtil.maybeSetInteger(
          mediaFormat, MediaFormat.KEY_PROFILE, codecProfileAndLevel.first);
    }
  }
  // Set codec max values.
  mediaFormat.setInteger(MediaFormat.KEY_MAX_WIDTH, codecMaxValues.width);
  mediaFormat.setInteger(MediaFormat.KEY_MAX_HEIGHT, codecMaxValues.height);
  MediaFormatUtil.maybeSetInteger(
      mediaFormat, MediaFormat.KEY_MAX_INPUT_SIZE, codecMaxValues.inputSize);
  // Set codec configuration values.
  if (Util.SDK_INT >= 23) {
    mediaFormat.setInteger(MediaFormat.KEY_PRIORITY, 0 /* realtime priority */);
    if (codecOperatingRate != CODEC_OPERATING_RATE_UNSET) {
      mediaFormat.setFloat(MediaFormat.KEY_OPERATING_RATE, codecOperatingRate);
    }
  }
  if (deviceNeedsNoPostProcessWorkaround) {
    mediaFormat.setInteger("no-post-process", 1);
    mediaFormat.setInteger("auto-frc", 0);
  }
  if (tunnelingAudioSessionId != C.AUDIO_SESSION_ID_UNSET) {
    configureTunnelingV21(mediaFormat, tunnelingAudioSessionId);
  }
  return mediaFormat;
}
 
Example 16
Source File: MediaFormatUtil.java    From TelePlus-Android with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Sets a {@link MediaFormat} float value. Does nothing if {@code value} is {@link
 * Format#NO_VALUE}.
 *
 * @param format The {@link MediaFormat} being configured.
 * @param key The key to set.
 * @param value The value to set.
 */
public static void maybeSetFloat(MediaFormat format, String key, float value) {
  if (value != Format.NO_VALUE) {
    format.setFloat(key, value);
  }
}
 
Example 17
Source File: MediaFormatUtil.java    From TelePlus-Android with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Sets a {@link MediaFormat} float value. Does nothing if {@code value} is {@link
 * Format#NO_VALUE}.
 *
 * @param format The {@link MediaFormat} being configured.
 * @param key The key to set.
 * @param value The value to set.
 */
public static void maybeSetFloat(MediaFormat format, String key, float value) {
  if (value != Format.NO_VALUE) {
    format.setFloat(key, value);
  }
}
 
Example 18
Source File: MediaFormatUtil.java    From Telegram-FOSS with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Sets a {@link MediaFormat} float value. Does nothing if {@code value} is {@link
 * Format#NO_VALUE}.
 *
 * @param format The {@link MediaFormat} being configured.
 * @param key The key to set.
 * @param value The value to set.
 */
public static void maybeSetFloat(MediaFormat format, String key, float value) {
  if (value != Format.NO_VALUE) {
    format.setFloat(key, value);
  }
}
 
Example 19
Source File: MediaFormatUtil.java    From Telegram with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Sets a {@link MediaFormat} float value. Does nothing if {@code value} is {@link
 * Format#NO_VALUE}.
 *
 * @param format The {@link MediaFormat} being configured.
 * @param key The key to set.
 * @param value The value to set.
 */
public static void maybeSetFloat(MediaFormat format, String key, float value) {
  if (value != Format.NO_VALUE) {
    format.setFloat(key, value);
  }
}
 
Example 20
Source File: MediaFormatUtil.java    From MediaSDK with Apache License 2.0 2 votes vote down vote up
/**
 * Sets a {@link MediaFormat} float value. Does nothing if {@code value} is {@link
 * Format#NO_VALUE}.
 *
 * @param format The {@link MediaFormat} being configured.
 * @param key The key to set.
 * @param value The value to set.
 */
public static void maybeSetFloat(MediaFormat format, String key, float value) {
  if (value != Format.NO_VALUE) {
    format.setFloat(key, value);
  }
}