Bug 1345599 - Part 1: Reduce InputExhausted() calls to avoid input queue flood. r=snorp, a=gchang
authorJohn Lin <jolin@mozilla.com>
Tue, 14 Mar 2017 14:51:52 +0800
changeset 395505 05835c70232e764c5c9e4e47cac6aed9a71e33c8
parent 395504 c31ca64231c3f1c4a5eb719a3fb9110726862cc9
child 395506 d2d9582aca65c41d7eccfa91dd6cf5c73e4ca984
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp, gchang
bugs1345599
milestone54.0a2
Bug 1345599 - Part 1: Reduce InputExhausted() calls to avoid input queue flood. r=snorp, a=gchang MozReview-Commit-ID: Kgl77k9Cx7w
mobile/android/base/java/org/mozilla/gecko/media/Codec.java
--- a/mobile/android/base/java/org/mozilla/gecko/media/Codec.java
+++ b/mobile/android/base/java/org/mozilla/gecko/media/Codec.java
@@ -46,16 +46,17 @@ import java.util.concurrent.ConcurrentLi
 
         @Override
         public void onOutputFormatChanged(AsyncCodec codec, MediaFormat format) {
             mOutputProcessor.onFormatChanged(format);
         }
     }
 
     private final class InputProcessor {
+        private static final int FEW_PENDING_INPUTS = 2;
         private boolean mHasInputCapacitySet;
         private Queue<Integer> mAvailableInputBuffers = new LinkedList<>();
         private Queue<Sample> mDequeuedSamples = new LinkedList<>();
         private Queue<Sample> mInputSamples = new LinkedList<>();
         private boolean mStopped;
 
         private synchronized Sample onAllocate(int size) {
             Sample sample = mSamplePool.obtainInput(size);
@@ -118,31 +119,37 @@ import java.util.concurrent.ConcurrentLi
                 long pts = sample.info.presentationTimeUs;
                 int flags = sample.info.flags;
                 MediaCodec.CryptoInfo cryptoInfo = sample.cryptoInfo;
                 if (!sample.isEOS() && sample.buffer != null) {
                     len = sample.info.size;
                     ByteBuffer buf = mCodec.getInputBuffer(index);
                     try {
                         sample.writeToByteBuffer(buf);
-                        mCallbacks.onInputExhausted();
                     } catch (IOException e) {
                         e.printStackTrace();
-                    } catch (RemoteException e) {
-                        e.printStackTrace();
                     }
                     mSamplePool.recycleInput(sample);
                 }
 
                 if (cryptoInfo != null) {
                     mCodec.queueSecureInputBuffer(index, 0, cryptoInfo, pts, flags);
                 } else {
                     mCodec.queueInputBuffer(index, 0, len, pts, flags);
                 }
             }
+            // To avoid input queue flood, request more input samples only when
+            // there are just a few waiting to be processed.
+            if (mDequeuedSamples.size() + mInputSamples.size() <= FEW_PENDING_INPUTS) {
+                try {
+                    mCallbacks.onInputExhausted();
+                } catch (RemoteException e) {
+                    e.printStackTrace();
+                }
+            }
         }
 
         private synchronized void reset() {
             for (Sample s : mInputSamples) {
                 if (!s.isEOS()) {
                     mSamplePool.recycleInput(s);
                 }
             }