Bug 1349883 - Part 1: Combine output buffer index and sample records. r=esawin, r=jya, a=gchang
authorJohn Lin <jolin@mozilla.com>
Thu, 13 Apr 2017 17:30:41 +0800
changeset 395866 fc16131f93233db6253a19ed010d9c5ab6a31303
parent 395865 8130607e5dbfb8931684fe758f0453c8e727d006
child 395867 e0352fcf12c17aee1f3e5ccd5c363b517bbdc0a3
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)
reviewersesawin, jya, gchang
bugs1349883
milestone54.0a2
Bug 1349883 - Part 1: Combine output buffer index and sample records. r=esawin, r=jya, a=gchang MozReview-Commit-ID: ERbFUoyvVhZ
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
@@ -183,36 +183,44 @@ import java.util.concurrent.ConcurrentLi
             if (mStopped) {
                 return;
             }
             mStopped = true;
             reset();
         }
     }
 
+    private static final class Output {
+        public final Sample sample;
+        public final int index;
+
+        public Output(final Sample sample, int index) {
+            this.sample = sample;
+            this.index = index;
+        }
+    }
+
     private class OutputProcessor {
         private final boolean mRenderToSurface;
         private boolean mHasOutputCapacitySet;
-        private Queue<Integer> mSentIndices = new LinkedList<>();
-        private Queue<Sample> mSentOutputs = new LinkedList<>();
+        private Queue<Output> mSentOutputs = new LinkedList<>();
         private boolean mStopped;
 
         private OutputProcessor(boolean renderToSurface) {
             mRenderToSurface = renderToSurface;
         }
 
         private synchronized void onBuffer(int index, MediaCodec.BufferInfo info) {
             if (mStopped) {
                 return;
             }
 
             try {
                 Sample output = obtainOutputSample(index, info);
-                mSentIndices.add(index);
-                mSentOutputs.add(output);
+                mSentOutputs.add(new Output(output, index));
                 mCallbacks.onOutput(output);
             } catch (Exception e) {
                 e.printStackTrace();
                 mCodec.releaseOutputBuffer(index, false);
             }
 
             boolean eos = (info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0;
             if (DEBUG && eos) {
@@ -243,44 +251,40 @@ import java.util.concurrent.ConcurrentLi
                     Log.e(LOGTAG, "Fail to read output buffer:" + e.getMessage());
                 }
             }
 
             return sample;
         }
 
         private synchronized void onRelease(Sample sample, boolean render) {
-            Integer i = mSentIndices.poll();
-            Sample output = mSentOutputs.poll();
-            if (i == null || output == null) {
-                Log.d(LOGTAG, "output buffer#" + i + "(" + output + ")" + ": " + sample + " already released");
+            final Output output = mSentOutputs.poll();
+            if (output == null) {
+                if (DEBUG) { Log.d(LOGTAG, sample + " already released"); }
                 return;
             }
-            mCodec.releaseOutputBuffer(i, render);
-            mSamplePool.recycleOutput(output);
+            mCodec.releaseOutputBuffer(output.index, render);
+            mSamplePool.recycleOutput(output.sample);
 
             sample.dispose();
         }
 
         private void onFormatChanged(MediaFormat format) {
             try {
                 mCallbacks.onOutputFormatChanged(new FormatParam(format));
             } catch (RemoteException re) {
                 // Dead recipient.
                 re.printStackTrace();
             }
         }
 
         private synchronized void reset() {
-            for (int i : mSentIndices) {
-                mCodec.releaseOutputBuffer(i, false);
-            }
-            mSentIndices.clear();
-            for (Sample s : mSentOutputs) {
-                mSamplePool.recycleOutput(s);
+            for (final Output o : mSentOutputs) {
+                mCodec.releaseOutputBuffer(o.index, false);
+                mSamplePool.recycleOutput(o.sample);
             }
             mSentOutputs.clear();
         }
 
         private synchronized void start() {
             if (!mStopped) {
                 return;
             }
@@ -296,17 +300,16 @@ import java.util.concurrent.ConcurrentLi
         }
     }
 
     private volatile ICodecCallbacks mCallbacks;
     private AsyncCodec mCodec;
     private InputProcessor mInputProcessor;
     private OutputProcessor mOutputProcessor;
     private SamplePool mSamplePool;
-    private Queue<Sample> mSentOutputs = new ConcurrentLinkedQueue<>();
     // Value will be updated after configure called.
     private volatile boolean mIsAdaptivePlaybackSupported = false;
 
     public synchronized void setCallbacks(ICodecCallbacks callbacks) throws RemoteException {
         mCallbacks = callbacks;
         callbacks.asBinder().linkToDeath(this, 0);
     }