media: add minimal test for setSyncParams

For now only default params must work, and we must take the sync
tolerance into account in our tests, except in the case when MediaSync
is not yet configured (where tolerance must be 0.)

Bug: 21211755
Change-Id: Ic934c7f841c64cb0a8f65830eb76c2c2bc7c7797
diff --git a/tests/tests/media/src/android/media/cts/MediaPlayerTest.java b/tests/tests/media/src/android/media/cts/MediaPlayerTest.java
index dc90c91..18cd353 100644
--- a/tests/tests/media/src/android/media/cts/MediaPlayerTest.java
+++ b/tests/tests/media/src/android/media/cts/MediaPlayerTest.java
@@ -32,6 +32,7 @@
 import android.media.MediaRecorder;
 import android.media.MediaTimestamp;
 import android.media.PlaybackParams;
+import android.media.SyncParams;
 import android.media.TimedText;
 import android.media.audiofx.AudioEffect;
 import android.media.audiofx.Visualizer;
@@ -70,6 +71,8 @@
     private static final int  RECORDED_VIDEO_WIDTH  = 176;
     private static final int  RECORDED_VIDEO_HEIGHT = 144;
     private static final long RECORDED_DURATION_MS  = 3000;
+    private static final float FLOAT_TOLERANCE = .0001f;
+
     private Vector<Integer> mTimedTextTrackIndex = new Vector<Integer>();
     private int mSelectedTimedTextIndex;
     private Monitor mOnTimedTextCalled = new Monitor();
@@ -842,6 +845,10 @@
 
         mMediaPlayer.setDisplay(mActivity.getSurfaceHolder());
         mMediaPlayer.prepare();
+        SyncParams sync = new SyncParams().allowDefaults();
+        mMediaPlayer.setSyncParams(sync);
+        sync = mMediaPlayer.getSyncParams();
+
         float[] rates = { 0.25f, 0.5f, 1.0f, 2.0f };
         for (float playbackRate : rates) {
             mMediaPlayer.seekTo(0);
@@ -850,6 +857,10 @@
             mMediaPlayer.setPlaybackParams(new PlaybackParams().setSpeed(playbackRate));
             mMediaPlayer.start();
             Thread.sleep(playTime);
+            PlaybackParams pbp = mMediaPlayer.getPlaybackParams();
+            assertEquals(
+                    playbackRate, pbp.getSpeed(),
+                    FLOAT_TOLERANCE + playbackRate * sync.getTolerance());
             assertTrue("MediaPlayer should still be playing", mMediaPlayer.isPlaying());
 
             int playedMediaDurationMs = mMediaPlayer.getCurrentPosition();
@@ -859,6 +870,8 @@
                      + ", play time is " + playTime + " vs expected " + playedMediaDurationMs);
             }
             mMediaPlayer.pause();
+            pbp = mMediaPlayer.getPlaybackParams();
+            assertEquals(0.f, pbp.getSpeed(), FLOAT_TOLERANCE);
         }
         mMediaPlayer.stop();
     }
diff --git a/tests/tests/media/src/android/media/cts/MediaSyncTest.java b/tests/tests/media/src/android/media/cts/MediaSyncTest.java
index dbbda32..0f6ca26 100644
--- a/tests/tests/media/src/android/media/cts/MediaSyncTest.java
+++ b/tests/tests/media/src/android/media/cts/MediaSyncTest.java
@@ -32,6 +32,7 @@
 import android.media.MediaSync;
 import android.media.MediaTimestamp;
 import android.media.PlaybackParams;
+import android.media.SyncParams;
 import android.test.ActivityInstrumentationTestCase2;
 import android.util.Log;
 import android.view.Surface;
@@ -61,6 +62,7 @@
             R.raw.video_480x360_mp4_h264_1350kbps_30fps_aac_stereo_192kbps_44100hz;
     private final int APPLICATION_AUDIO_PERIOD_MS = 200;
     private final int TEST_MAX_SPEED = 2;
+    private static final float FLOAT_TOLERANCE = .00001f;
 
     private Context mContext;
     private Resources mResources;
@@ -186,6 +188,8 @@
         final float rate = (float)TEST_MAX_SPEED;
         try {
             mMediaSync.setPlaybackParams(new PlaybackParams().setSpeed(rate));
+            PlaybackParams pbp = mMediaSync.getPlaybackParams();
+            assertEquals(rate, pbp.getSpeed(), FLOAT_TOLERANCE);
         } catch (IllegalArgumentException e) {
             fail("playback rate " + rate + " is not handled correctly");
         }
@@ -442,6 +446,10 @@
             mHasAudio = true;
         }
 
+        SyncParams sync = new SyncParams().allowDefaults();
+        mMediaSync.setSyncParams(sync);
+        sync = mMediaSync.getSyncParams();
+
         mMediaSync.setPlaybackParams(new PlaybackParams().setSpeed(playbackRate));
 
         synchronized (conditionFirstAudioBuffer) {
@@ -492,7 +500,10 @@
                     + ", play time is " + playTimeUs + " vs expected " + mediaDurationUs,
                     mediaDurationUs,
                     playTimeUs * playbackRate,
-                    mediaDurationUs * PLAYBACK_RATE_TOLERANCE_PERCENT / 100
+                    // sync.getTolerance() is MediaSync's tolerance of the playback rate, whereas
+                    // PLAYBACK_RATE_TOLERANCE_PERCENT / 100 is our test's tolerance.
+                    // We need to add both to get an upperbound for allowable error.
+                    mediaDurationUs * (sync.getTolerance() + PLAYBACK_RATE_TOLERANCE_PERCENT / 100)
                             + TIME_MEASUREMENT_TOLERANCE_US);
         }