Fix AudioEngine to allow re-initialization

Add AudioEngine::CompareAndSetEngine and change
VariableSpeed::initializeEngine to use this method so it only initializes
a new engine once rather than asserting on subsequent calls.

Change AudioEngine::DeleteEngine so it ignores repeated calls instead of
asserting. Add VariableSpeed::isReadyToPlay so callers can detect when
the audio library has been properly initialized.

Bug:11511992
Change-Id: I4517cb2ab4c186da43dffc7dbc2a9ad49000059f
diff --git a/variablespeed/jni/jni_entry.cc b/variablespeed/jni/jni_entry.cc
index 93c12ba..20bcf3c 100644
--- a/variablespeed/jni/jni_entry.cc
+++ b/variablespeed/jni/jni_entry.cc
@@ -81,9 +81,12 @@
     jfloat initialRate, jint decodeInitialSize, jint decodeMaxSize,
     jint startPositionMillis, jint audioStreamType) {
   MethodLog _("initializeEngine");
-  AudioEngine::SetEngine(new AudioEngine(targetFrames,
+  AudioEngine *engine = new AudioEngine(targetFrames,
       windowDuration, windowOverlapDuration, maxPlayBufferCount, initialRate,
-      decodeInitialSize, decodeMaxSize, startPositionMillis, audioStreamType));
+      decodeInitialSize, decodeMaxSize, startPositionMillis, audioStreamType);
+  if (!AudioEngine::CompareAndSetEngine(NULL, engine)) {
+    delete engine;
+  }
 }
 
 JNI_METHOD(shutdownEngine, void) (JNIEnv*, jclass) {