| Index: media/base/android/java/src/org/chromium/media/MediaCodecBridge.java
|
| diff --git a/media/base/android/java/src/org/chromium/media/MediaCodecBridge.java b/media/base/android/java/src/org/chromium/media/MediaCodecBridge.java
|
| index 6834ed806c4b1dc44de79124f4f67e4e5921e9d4..0821e4d865a0db03bc431c9bf4ffaeb67c79c5e4 100644
|
| --- a/media/base/android/java/src/org/chromium/media/MediaCodecBridge.java
|
| +++ b/media/base/android/java/src/org/chromium/media/MediaCodecBridge.java
|
| @@ -77,6 +77,7 @@ class MediaCodecBridge {
|
| private long mLastPresentationTimeUs;
|
| private String mMime;
|
| private boolean mAdaptivePlaybackSupported;
|
| + private int mSampleRate;
|
|
|
| private static class DequeueInputResult {
|
| private final int mStatus;
|
| @@ -565,6 +566,13 @@ class MediaCodecBridge {
|
| status = MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED;
|
| } else if (indexOrStatus == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
|
| status = MEDIA_CODEC_OUTPUT_FORMAT_CHANGED;
|
| + MediaFormat newFormat = mMediaCodec.getOutputFormat();
|
| + if (newFormat.containsKey(MediaFormat.KEY_SAMPLE_RATE)) {
|
| + int newSampleRate = newFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE);
|
| + if (newSampleRate != mSampleRate && !reconfigureAudioTrack(newFormat)) {
|
| + status = MEDIA_CODEC_ERROR;
|
| + }
|
| + }
|
| } else if (indexOrStatus == MediaCodec.INFO_TRY_AGAIN_LATER) {
|
| status = MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER;
|
| } else {
|
| @@ -664,20 +672,8 @@ class MediaCodecBridge {
|
| boolean playAudio) {
|
| try {
|
| mMediaCodec.configure(format, null, crypto, flags);
|
| - if (playAudio) {
|
| - int sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
|
| - int channelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
|
| - int channelConfig = getAudioFormat(channelCount);
|
| - // Using 16bit PCM for output. Keep this value in sync with
|
| - // kBytesPerAudioOutputSample in media_codec_bridge.cc.
|
| - int minBufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig,
|
| - AudioFormat.ENCODING_PCM_16BIT);
|
| - mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig,
|
| - AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
|
| - if (mAudioTrack.getState() == AudioTrack.STATE_UNINITIALIZED) {
|
| - mAudioTrack = null;
|
| - return false;
|
| - }
|
| + if (playAudio && !reconfigureAudioTrack(format)) {
|
| + return false;
|
| }
|
| return true;
|
| } catch (IllegalStateException e) {
|
| @@ -687,6 +683,35 @@ class MediaCodecBridge {
|
| }
|
|
|
| /**
|
| + * Resets the AudioTrack instance, configured according to the given format.
|
| + * If a previous AudioTrack instance already exists, release it.
|
| + *
|
| + * @param format The format from which to get sample rate and channel count.
|
| + * @return Whether or not creating the AudioTrack succeeded.
|
| + */
|
| + private boolean reconfigureAudioTrack(MediaFormat format) {
|
| + if (mAudioTrack != null) {
|
| + mAudioTrack.release();
|
| + }
|
| +
|
| + mSampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
|
| + int channelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
|
| + int channelConfig = getAudioFormat(channelCount);
|
| + // Using 16bit PCM for output. Keep this value in sync with
|
| + // kBytesPerAudioOutputSample in media_codec_bridge.cc.
|
| + int minBufferSize = AudioTrack.getMinBufferSize(mSampleRate, channelConfig,
|
| + AudioFormat.ENCODING_PCM_16BIT);
|
| + mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, mSampleRate, channelConfig,
|
| + AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
|
| + if (mAudioTrack.getState() == AudioTrack.STATE_UNINITIALIZED) {
|
| + mAudioTrack = null;
|
| + Log.e(TAG, "Failed to initialize AudioTrack");
|
| + return false;
|
| + }
|
| + return true;
|
| + }
|
| +
|
| + /**
|
| * Play the audio buffer that is passed in.
|
| *
|
| * @param buf Audio buffer to be rendered.
|
|
|