MuxerTest: Restrict stts tolerance to video tracks

changed equals to return at first instance of deviation instead
of comparing till the end

Bug: 153794127
Test: atest android.mediav2.cts.MuxerTest

Change-Id: I5aef209a9d2a672d5bcb4107a9391faaa641bc7f
diff --git a/tests/media/jni/NativeMuxerTest.cpp b/tests/media/jni/NativeMuxerTest.cpp
index 9e759bc..b4e7858 100644
--- a/tests/media/jni/NativeMuxerTest.cpp
+++ b/tests/media/jni/NativeMuxerTest.cpp
@@ -66,14 +66,14 @@
 
     bool combineMedias(AMediaMuxer* muxer, MuxerNativeTestHelper* that, const int* repeater);
 
-    bool equals(MuxerNativeTestHelper* that);
+    bool isSubsetOf(MuxerNativeTestHelper* that);
 
     void offsetTimeStamp(int trackID, long tsOffset, int sampleOffset);
 
   private:
     void splitMediaToMuxerParameters();
 
-    static const int STTS_TOLERANCE = 100;
+    static const int STTS_TOLERANCE_US = 100;
     const char* mSrcPath;
     const char* mMime;
     int mTrackCount;
@@ -263,7 +263,7 @@
 
 // returns true if 'this' stream is a subset of 'o'. That is all tracks in current media
 // stream are present in ref media stream
-bool MuxerNativeTestHelper::equals(MuxerNativeTestHelper* that) {
+bool MuxerNativeTestHelper::isSubsetOf(MuxerNativeTestHelper* that) {
     if (this == that) return true;
     if (that == nullptr) return false;
 
@@ -279,31 +279,27 @@
             if (thisMime != nullptr && thatMime != nullptr && !strcmp(thisMime, thatMime)) {
                 if (!isCSDIdentical(thisFormat, thatFormat)) continue;
                 if (mBufferInfo[i].size() == that->mBufferInfo[j].size()) {
-                    int flagsDiff = 0, sizeDiff = 0, tsDiff = 0, buffDiff = 0;
-                    for (int k = 0; k < mBufferInfo[i].size(); k++) {
+                    int tolerance =
+                            strncmp(thisMime, "video/", strlen("video/")) ? 0 : STTS_TOLERANCE_US;
+                    int k = 0;
+                    for (; k < mBufferInfo[i].size(); k++) {
                         AMediaCodecBufferInfo* thisInfo = mBufferInfo[i][k];
                         AMediaCodecBufferInfo* thatInfo = that->mBufferInfo[j][k];
                         if (thisInfo->flags != thatInfo->flags) {
-                            flagsDiff++;
+                            break;
                         }
                         if (thisInfo->size != thatInfo->size) {
-                            sizeDiff++;
+                            break;
                         } else if (memcmp(mBuffer + thisInfo->offset,
                                           that->mBuffer + thatInfo->offset, thisInfo->size)) {
-                            buffDiff++;
+                            break;
                         }
                         if (abs(thisInfo->presentationTimeUs - thatInfo->presentationTimeUs) >
-                            STTS_TOLERANCE) {
-                            tsDiff++;
+                            tolerance) {
+                            break;
                         }
                     }
-                    if (flagsDiff == 0 && sizeDiff == 0 && tsDiff == 0 && buffDiff == 0)
-                        break;
-                    else {
-                        ALOGV("For mime %s, Total Samples %d, flagsDiff %d, sizeDiff %d, tsDiff "
-                              "%d, buffDiff %d", thisMime, (int)mBufferInfo[i].size(), flagsDiff,
-                              sizeDiff, tsDiff, buffDiff);
-                    }
+                    if (k == mBufferInfo[i].size()) break;
                 }
             }
         }
@@ -524,7 +520,7 @@
             fclose(rfp);
             if (muxStatus) {
                 auto* refInfo = new MuxerNativeTestHelper(crefPath);
-                if (!mediaInfoA->equals(refInfo) || !mediaInfoB->equals(refInfo)) {
+                if (!mediaInfoA->isSubsetOf(refInfo) || !mediaInfoB->isSubsetOf(refInfo)) {
                     isPass = false;
                     ALOGE("testMultiTrack: inputs: %s %s, fmt: %d, error ! muxing src A and src B "
                           "failed", csrcPathA, csrcPathB, jformat);
@@ -540,7 +536,7 @@
                             fclose(ofp);
                             if (status) {
                                 auto* dstInfo = new MuxerNativeTestHelper(cdstPath);
-                                if (!dstInfo->equals(refInfo)) {
+                                if (!dstInfo->isSubsetOf(refInfo)) {
                                     isPass = false;
                                     ALOGE("testMultiTrack: inputs: %s %s, fmt: %d, error ! muxing "
                                           "src A: %d, src B: %d failed", csrcPathA, csrcPathB,
@@ -614,7 +610,7 @@
                 AMediaMuxer_delete(muxer);
                 fclose(ofp);
                 auto* outInfo = new MuxerNativeTestHelper(cdstPath);
-                isPass = mediaInfo->equals(outInfo);
+                isPass = mediaInfo->isSubsetOf(outInfo);
                 if (!isPass) {
                     ALOGE("Validation failed after adding timestamp offset to track %d", trackID);
                 }
@@ -668,7 +664,7 @@
                 fclose(ofp);
                 if (muxStatus) {
                     auto* outInfo = new MuxerNativeTestHelper(cdstPath, cmime);
-                    result = mediaInfo->equals(outInfo);
+                    result = mediaInfo->isSubsetOf(outInfo);
                     delete outInfo;
                 }
                 if ((muxStatus && !result) ||
diff --git a/tests/media/src/android/mediav2/cts/MuxerTest.java b/tests/media/src/android/mediav2/cts/MuxerTest.java
index d954f01..0c87645 100644
--- a/tests/media/src/android/mediav2/cts/MuxerTest.java
+++ b/tests/media/src/android/mediav2/cts/MuxerTest.java
@@ -57,7 +57,9 @@
 class MuxerTestHelper {
     private static final String LOG_TAG = MuxerTestHelper.class.getSimpleName();
     private static final boolean ENABLE_LOGS = false;
-    static final int STTS_TOLERANCE = 100;
+    // Stts values within 0.1ms(100us) difference are fudged to save too
+    // many stts entries in MPEG4Writer.
+    static final int STTS_TOLERANCE_US = 100;
     private String mSrcPath;
     private String mMime;
     private int mTrackCount;
@@ -253,10 +255,9 @@
         }
     }
 
-    @Override
     // returns true if 'this' stream is a subset of 'o'. That is all tracks in current media
     // stream are present in ref media stream
-    public boolean equals(Object o) {
+    boolean isSubsetOf(Object o) {
         if (this == o) return true;
         if (o == null || getClass() != o.getClass()) return false;
         MuxerTestHelper that = (MuxerTestHelper) o;
@@ -273,43 +274,37 @@
                 if (thisMime != null && thisMime.equals(thatMime)) {
                     if (!ExtractorTest.isCSDIdentical(thisFormat, thatFormat)) continue;
                     if (mBufferInfo.get(i).size() == that.mBufferInfo.get(j).size()) {
-                        int flagsDiff = 0, sizeDiff = 0, tsDiff = 0, buffDiff = 0;
-                        for (int k = 0; k < mBufferInfo.get(i).size(); k++) {
+                        long tolerance = thisMime.startsWith("video/") ? STTS_TOLERANCE_US : 0;
+                        int k = 0;
+                        for (; k < mBufferInfo.get(i).size(); k++) {
                             MediaCodec.BufferInfo thisInfo = mBufferInfo.get(i).get(k);
                             MediaCodec.BufferInfo thatInfo = that.mBufferInfo.get(j).get(k);
                             if (thisInfo.flags != thatInfo.flags) {
-                                flagsDiff++;
+                                break;
                             }
                             if (thisInfo.size != thatInfo.size) {
-                                sizeDiff++;
+                                break;
                             } else {
                                 mBuff.position(thisInfo.offset);
                                 mBuff.get(refBuffer, 0, thisInfo.size);
                                 that.mBuff.position(thatInfo.offset);
                                 that.mBuff.get(testBuffer, 0, thatInfo.size);
-                                for (int count = 0; count < thisInfo.size; count++) {
+                                int count = 0;
+                                for (; count < thisInfo.size; count++) {
                                     if (refBuffer[count] != testBuffer[count]) {
-                                        buffDiff++;
                                         break;
                                     }
                                 }
+                                if (count != thisInfo.size) break;
                             }
                             if (Math.abs(
                                     thisInfo.presentationTimeUs - thatInfo.presentationTimeUs) >
-                                    STTS_TOLERANCE) {
-                                tsDiff++;
+                                    tolerance) {
+                                break;
                             }
                         }
-                        if (flagsDiff != 0 || sizeDiff != 0 || tsDiff != 0 || buffDiff != 0) {
-                            if (ENABLE_LOGS) {
-                                Log.d(LOG_TAG, "For track: " + thisMime +
-                                        " Total Samples: " + mBufferInfo.get(i).size() +
-                                        " flagsDiff: " + flagsDiff +
-                                        " sizeDiff: " + sizeDiff +
-                                        " tsDiff: " + tsDiff +
-                                        " buffDiff: " + buffDiff);
-                            }
-                        } else break;
+                        // all samples are identical. successful match found. move to next track
+                        if (k == mBufferInfo.get(i).size()) break;
                     } else {
                         if (ENABLE_LOGS) {
                             Log.d(LOG_TAG, "Mime matched but sample count different." +
@@ -742,7 +737,7 @@
             try {
                 mediaInfoA.combineMedias(muxer, mediaInfoB, new int[]{1, 1});
                 refInfo = new MuxerTestHelper(mRefPath);
-                if (!mediaInfoA.equals(refInfo) || !mediaInfoB.equals(refInfo)) {
+                if (!mediaInfoA.isSubsetOf(refInfo) || !mediaInfoB.isSubsetOf(refInfo)) {
                     fail(msg + "error ! muxing src A and src B failed");
                 }
             } catch (Exception e) {
@@ -761,7 +756,7 @@
                 try {
                     mediaInfoA.combineMedias(muxer, mediaInfoB, numTrack);
                     MuxerTestHelper outInfo = new MuxerTestHelper(mOutPath);
-                    if (!outInfo.equals(refInfo)) {
+                    if (!outInfo.isSubsetOf(refInfo)) {
                         fail(msg + " error ! muxing src A: " + numTrack[0] + " src B: " +
                                 numTrack[1] + "failed");
                     }
@@ -856,7 +851,7 @@
                     mOutFormat != MediaMuxer.OutputFormat.MUXER_OUTPUT_WEBM);
             Assume.assumeTrue("TODO(b/146421018)",
                     mOutFormat != MediaMuxer.OutputFormat.MUXER_OUTPUT_OGG);
-            assertTrue(OFFSET_TS > MuxerTestHelper.STTS_TOLERANCE);
+            assertTrue(OFFSET_TS > MuxerTestHelper.STTS_TOLERANCE_US);
             MuxerTestHelper mediaInfo = new MuxerTestHelper(mInpPath);
             for (int trackID = 0; trackID < mediaInfo.getTrackCount(); trackID++) {
                 for (int i = 0; i < mOffsetIndices.length; i++) {
@@ -866,7 +861,7 @@
                 mediaInfo.muxMedia(muxer);
                 muxer.release();
                 MuxerTestHelper outInfo = new MuxerTestHelper(mOutPath);
-                if (!outInfo.equals(mediaInfo)) {
+                if (!outInfo.isSubsetOf(mediaInfo)) {
                     String msg = String.format(
                             "testOffsetPresentationTime: inp: %s, fmt: %d, trackID %d", mSrcFile,
                             mOutFormat, trackID);
@@ -1010,7 +1005,7 @@
                 try {
                     mediaInfo.muxMedia(muxer);
                     MuxerTestHelper outInfo = new MuxerTestHelper(mOutPath);
-                    if (!mediaInfo.equals(outInfo)) {
+                    if (!mediaInfo.isSubsetOf(outInfo)) {
                         fail(msg + "error! output != clone(input)");
                     }
                 } catch (Exception e) {