Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(493)

Unified Diff: media/base/android/java/src/org/chromium/media/MediaCodecBridge.java

Issue 813643002: MediaCodecBridge: reconfigure AudioTrack when sample rate changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..e2e58d02cf0010320b2c6279a6ddcb5d2e6044aa 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,15 @@ 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) {
+ if (!reconfigureAudioTrack(newFormat)) {
qinmin 2014/12/17 18:12:54 nit: simply combine this if statement with the abo
gunsch 2014/12/17 19:48:12 Done.
+ status = MEDIA_CODEC_ERROR;
+ }
+ }
+ }
} else if (indexOrStatus == MediaCodec.INFO_TRY_AGAIN_LATER) {
status = MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER;
} else {
@@ -664,20 +674,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 +685,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.
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698