Bug 1349883 - part 1: combine output buffer index and sample records. r=esawin,jya draft
authorJohn Lin <jolin@mozilla.com>
Thu, 06 Apr 2017 15:23:47 +0800
changeset 561000 f9bcc709ac59a8ade9d49da2fdf49daaef4fb14e
parent 560035 b5b5dbed1c409d96aa6b97f2036cd66312fc45ad
child 561001 fc855d595bbf1845c3e12d9caa0de282b97ab471
push id53589
push userbmo:jolin@mozilla.com
push dateWed, 12 Apr 2017 03:42:17 +0000
reviewersesawin, jya
bugs1349883
milestone55.0a1
Bug 1349883 - part 1: combine output buffer index and sample records. r=esawin,jya MozReview-Commit-ID: 7yWMkgjOtQB
mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/Codec.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/Codec.java
+++ b/mobile/android/geckoview/src/main/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);
     }