Create and write the TransformationResult to on-device text file.

PiperOrigin-RevId: 412856100
This commit is contained in:
samrobinson 2021-11-29 12:36:40 +00:00 committed by tonihei
parent 99eb35179d
commit 83408d065e

View File

@ -20,12 +20,14 @@ import static java.util.concurrent.TimeUnit.SECONDS;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.platform.app.InstrumentationRegistry;
import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.transformer.Transformer; import com.google.android.exoplayer2.transformer.Transformer;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import java.io.File; import java.io.File;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -40,9 +42,11 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
/** Information about the result of successfully running a transformer. */ /** Information about the result of successfully running a transformer. */
public static final class TransformationResult { public static final class TransformationResult {
public long outputSizeBytes; public final String testId;
public final long outputSizeBytes;
private TransformationResult(long outputSizeBytes) { private TransformationResult(String testId, long outputSizeBytes) {
this.testId = testId;
this.outputSizeBytes = outputSizeBytes; this.outputSizeBytes = outputSizeBytes;
} }
} }
@ -83,13 +87,13 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
.build(); .build();
Uri uri = Uri.parse(uriString); Uri uri = Uri.parse(uriString);
File externalCacheFile = createExternalCacheFile(context, /* filePrefix= */ testId); File outputVideoFile = createExternalCacheFile(context, /* fileName= */ testId + "-output.mp4");
InstrumentationRegistry.getInstrumentation() InstrumentationRegistry.getInstrumentation()
.runOnMainSync( .runOnMainSync(
() -> { () -> {
try { try {
testTransformer.startTransformation( testTransformer.startTransformation(
MediaItem.fromUri(uri), externalCacheFile.getAbsolutePath()); MediaItem.fromUri(uri), outputVideoFile.getAbsolutePath());
} catch (IOException e) { } catch (IOException e) {
exceptionReference.set(e); exceptionReference.set(e);
} }
@ -102,16 +106,41 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
if (exception != null) { if (exception != null) {
throw exception; throw exception;
} }
long outputSizeBytes = externalCacheFile.length(); long outputSizeBytes = outputVideoFile.length();
return new TransformationResult(outputSizeBytes);
TransformationResult result = new TransformationResult(testId, outputSizeBytes);
writeTransformationResultToFile(context, result);
return result;
} }
private static File createExternalCacheFile(Context context, String filePrefix) private static void writeTransformationResultToFile(Context context, TransformationResult result)
throws IOException { throws IOException {
File file = new File(context.getExternalCacheDir(), filePrefix + "-output.mp4"); File analysisFile =
createExternalCacheFile(context, /* fileName= */ result.testId + "-result.txt");
FileWriter fileWriter = new FileWriter(analysisFile);
String fileContents =
"test="
+ result.testId
+ ", deviceBrand="
+ Build.MANUFACTURER
+ ", deviceModel="
+ Build.MODEL
+ ", sdkVersion="
+ Build.VERSION.SDK_INT
+ ", outputSizeBytes="
+ result.outputSizeBytes;
try {
fileWriter.write(fileContents);
} finally {
fileWriter.close();
}
}
private static File createExternalCacheFile(Context context, String fileName) throws IOException {
File file = new File(context.getExternalCacheDir(), fileName);
Assertions.checkState( Assertions.checkState(
!file.exists() || file.delete(), "Could not delete the previous transformer output file."); !file.exists() || file.delete(), "Could not delete file: " + file.getAbsolutePath());
Assertions.checkState(file.createNewFile(), "Could not create the transformer output file."); Assertions.checkState(file.createNewFile(), "Could not create file: " + file.getAbsolutePath());
return file; return file;
} }