mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
Clean-up ExoHostedTest/HostActivity.
This change clearly seperates the hosting of a test (HostActivity) from the internals of hosting an ExoPlayer lifecycle (ExoHostedTest). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=163967960
This commit is contained in:
parent
55fe5d21a2
commit
22ea9ed687
@ -73,7 +73,7 @@ public abstract class ExoHostedTest implements HostedTest, Player.EventListener,
|
|||||||
private final long expectedPlayingTimeMs;
|
private final long expectedPlayingTimeMs;
|
||||||
private final DecoderCounters videoDecoderCounters;
|
private final DecoderCounters videoDecoderCounters;
|
||||||
private final DecoderCounters audioDecoderCounters;
|
private final DecoderCounters audioDecoderCounters;
|
||||||
private final ConditionVariable playerFinished;
|
private final ConditionVariable testFinished;
|
||||||
|
|
||||||
private ActionSchedule pendingSchedule;
|
private ActionSchedule pendingSchedule;
|
||||||
private Handler actionHandler;
|
private Handler actionHandler;
|
||||||
@ -116,7 +116,7 @@ public abstract class ExoHostedTest implements HostedTest, Player.EventListener,
|
|||||||
this.tag = tag;
|
this.tag = tag;
|
||||||
this.expectedPlayingTimeMs = expectedPlayingTimeMs;
|
this.expectedPlayingTimeMs = expectedPlayingTimeMs;
|
||||||
this.failOnPlayerError = failOnPlayerError;
|
this.failOnPlayerError = failOnPlayerError;
|
||||||
this.playerFinished = new ConditionVariable();
|
this.testFinished = new ConditionVariable();
|
||||||
this.videoDecoderCounters = new DecoderCounters();
|
this.videoDecoderCounters = new DecoderCounters();
|
||||||
this.audioDecoderCounters = new DecoderCounters();
|
this.audioDecoderCounters = new DecoderCounters();
|
||||||
}
|
}
|
||||||
@ -172,16 +172,13 @@ public abstract class ExoHostedTest implements HostedTest, Player.EventListener,
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final boolean blockUntilEnded(long timeoutMs) {
|
public final boolean blockUntilStopped(long timeoutMs) {
|
||||||
return playerFinished.block(timeoutMs);
|
return testFinished.block(timeoutMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void onStop() {
|
public final boolean forceStop() {
|
||||||
actionHandler.removeCallbacksAndMessages(null);
|
return stopTest();
|
||||||
sourceDurationMs = player.getDuration();
|
|
||||||
player.release();
|
|
||||||
player = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -222,7 +219,7 @@ public abstract class ExoHostedTest implements HostedTest, Player.EventListener,
|
|||||||
playerWasPrepared |= playbackState != Player.STATE_IDLE;
|
playerWasPrepared |= playbackState != Player.STATE_IDLE;
|
||||||
if (playbackState == Player.STATE_ENDED
|
if (playbackState == Player.STATE_ENDED
|
||||||
|| (playbackState == Player.STATE_IDLE && playerWasPrepared)) {
|
|| (playbackState == Player.STATE_IDLE && playerWasPrepared)) {
|
||||||
playerFinished.open();
|
stopTest();
|
||||||
}
|
}
|
||||||
boolean playing = playWhenReady && playbackState == Player.STATE_READY;
|
boolean playing = playWhenReady && playbackState == Player.STATE_READY;
|
||||||
if (!this.playing && playing) {
|
if (!this.playing && playing) {
|
||||||
@ -337,6 +334,25 @@ public abstract class ExoHostedTest implements HostedTest, Player.EventListener,
|
|||||||
|
|
||||||
// Internal logic
|
// Internal logic
|
||||||
|
|
||||||
|
private boolean stopTest() {
|
||||||
|
if (player == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
actionHandler.removeCallbacksAndMessages(null);
|
||||||
|
sourceDurationMs = player.getDuration();
|
||||||
|
player.release();
|
||||||
|
player = null;
|
||||||
|
// We post opening of the finished condition so that any events posted to the main thread as a
|
||||||
|
// result of player.release() are guaranteed to be handled before the test returns.
|
||||||
|
actionHandler.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
testFinished.open();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
protected DrmSessionManager<FrameworkMediaCrypto> buildDrmSessionManager(String userAgent) {
|
protected DrmSessionManager<FrameworkMediaCrypto> buildDrmSessionManager(String userAgent) {
|
||||||
// Do nothing. Interested subclasses may override.
|
// Do nothing. Interested subclasses may override.
|
||||||
return null;
|
return null;
|
||||||
|
@ -56,21 +56,20 @@ public final class HostActivity extends Activity implements SurfaceHolder.Callba
|
|||||||
void onStart(HostActivity host, Surface surface);
|
void onStart(HostActivity host, Surface surface);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called on the main thread to block until the test has stopped or {@link #onStop()} is called.
|
* Called on the main thread to block until the test has stopped or {@link #forceStop()} is
|
||||||
|
* called.
|
||||||
*
|
*
|
||||||
* @param timeoutMs The maximum time to block in milliseconds.
|
* @param timeoutMs The maximum time to block in milliseconds.
|
||||||
* @return Whether the test has stopped successful.
|
* @return Whether the test has stopped successful.
|
||||||
*/
|
*/
|
||||||
boolean blockUntilEnded(long timeoutMs);
|
boolean blockUntilStopped(long timeoutMs);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called on the main thread when the test is stopped.
|
* Called on the main thread to force stop the test (if it is not stopped already).
|
||||||
* <p>
|
*
|
||||||
* The test will be stopped when {@link #blockUntilEnded(long)} returns, if the
|
* @return Whether the test was forced stopped.
|
||||||
* {@link HostActivity} has been paused, or if the {@link HostActivity}'s {@link Surface} has
|
|
||||||
* been destroyed.
|
|
||||||
*/
|
*/
|
||||||
void onStop();
|
boolean forceStop();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called on the test thread after the test has finished and been stopped.
|
* Called on the test thread after the test has finished and been stopped.
|
||||||
@ -89,7 +88,8 @@ public final class HostActivity extends Activity implements SurfaceHolder.Callba
|
|||||||
|
|
||||||
private HostedTest hostedTest;
|
private HostedTest hostedTest;
|
||||||
private boolean hostedTestStarted;
|
private boolean hostedTestStarted;
|
||||||
private boolean forcedFinished;
|
private ConditionVariable hostedTestStartedCondition;
|
||||||
|
private boolean forcedStopped;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes a {@link HostedTest} inside the host.
|
* Executes a {@link HostedTest} inside the host.
|
||||||
@ -109,34 +109,31 @@ public final class HostActivity extends Activity implements SurfaceHolder.Callba
|
|||||||
* @param timeoutMs The number of milliseconds to wait for the test to finish.
|
* @param timeoutMs The number of milliseconds to wait for the test to finish.
|
||||||
* @param failOnTimeout Whether the test fails when the timeout is exceeded.
|
* @param failOnTimeout Whether the test fails when the timeout is exceeded.
|
||||||
*/
|
*/
|
||||||
public void runTest(HostedTest hostedTest, long timeoutMs, boolean failOnTimeout) {
|
public void runTest(final HostedTest hostedTest, long timeoutMs, boolean failOnTimeout) {
|
||||||
Assertions.checkArgument(timeoutMs > 0);
|
Assertions.checkArgument(timeoutMs > 0);
|
||||||
Assertions.checkState(Thread.currentThread() != getMainLooper().getThread());
|
Assertions.checkState(Thread.currentThread() != getMainLooper().getThread());
|
||||||
|
|
||||||
Assertions.checkState(this.hostedTest == null);
|
Assertions.checkState(this.hostedTest == null);
|
||||||
this.hostedTest = Assertions.checkNotNull(hostedTest);
|
Assertions.checkNotNull(hostedTest);
|
||||||
|
hostedTestStartedCondition = new ConditionVariable();
|
||||||
|
forcedStopped = false;
|
||||||
hostedTestStarted = false;
|
hostedTestStarted = false;
|
||||||
forcedFinished = false;
|
|
||||||
|
|
||||||
final ConditionVariable testStarted = new ConditionVariable();
|
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
HostActivity.this.hostedTest = hostedTest;
|
||||||
maybeStartHostedTest();
|
maybeStartHostedTest();
|
||||||
testStarted.open();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
testStarted.block();
|
hostedTestStartedCondition.block();
|
||||||
|
|
||||||
if (hostedTest.blockUntilEnded(timeoutMs)) {
|
if (hostedTest.blockUntilStopped(timeoutMs)) {
|
||||||
hostedTest.onStop();
|
if (!forcedStopped) {
|
||||||
if (!forcedFinished) {
|
Log.d(TAG, "Checking test pass conditions.");
|
||||||
Log.d(TAG, "Test finished. Checking pass conditions.");
|
|
||||||
hostedTest.onFinished();
|
hostedTest.onFinished();
|
||||||
this.hostedTest = null;
|
|
||||||
Log.d(TAG, "Pass conditions checked.");
|
Log.d(TAG, "Pass conditions checked.");
|
||||||
} else {
|
} else {
|
||||||
String message = "Test released before it finished. Activity may have been paused whilst "
|
String message = "Test force stopped. Activity may have been paused whilst "
|
||||||
+ "test was in progress.";
|
+ "test was in progress.";
|
||||||
Log.e(TAG, message);
|
Log.e(TAG, message);
|
||||||
fail(message);
|
fail(message);
|
||||||
@ -147,8 +144,8 @@ public final class HostActivity extends Activity implements SurfaceHolder.Callba
|
|||||||
if (failOnTimeout) {
|
if (failOnTimeout) {
|
||||||
fail(message);
|
fail(message);
|
||||||
}
|
}
|
||||||
maybeStopHostedTest();
|
|
||||||
}
|
}
|
||||||
|
this.hostedTest = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Activity lifecycle
|
// Activity lifecycle
|
||||||
@ -175,12 +172,6 @@ public final class HostActivity extends Activity implements SurfaceHolder.Callba
|
|||||||
super.onStart();
|
super.onStart();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onResume() {
|
|
||||||
super.onResume();
|
|
||||||
maybeStartHostedTest();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
@ -224,14 +215,13 @@ public final class HostActivity extends Activity implements SurfaceHolder.Callba
|
|||||||
hostedTestStarted = true;
|
hostedTestStarted = true;
|
||||||
Log.d(TAG, "Starting test.");
|
Log.d(TAG, "Starting test.");
|
||||||
hostedTest.onStart(this, surface);
|
hostedTest.onStart(this, surface);
|
||||||
|
hostedTestStartedCondition.open();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void maybeStopHostedTest() {
|
private void maybeStopHostedTest() {
|
||||||
if (hostedTest != null && hostedTestStarted) {
|
if (hostedTest != null && hostedTestStarted && !forcedStopped) {
|
||||||
forcedFinished = true;
|
forcedStopped = hostedTest.forceStop();
|
||||||
hostedTest.onStop();
|
|
||||||
hostedTest = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user