|
16 | 16 |
|
17 | 17 | package com.google.wireless.qa.mobileharness.shared.api.decorator; |
18 | 18 |
|
| 19 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 20 | + |
| 21 | +import com.google.common.base.Joiner; |
| 22 | +import com.google.common.collect.ImmutableList; |
| 23 | +import com.google.common.collect.Sets; |
19 | 24 | import com.google.common.flogger.FluentLogger; |
| 25 | +import com.google.devtools.deviceinfra.platform.android.lightning.internal.sdk.adb.Adb; |
| 26 | +import com.google.devtools.mobileharness.api.model.error.AndroidErrorId; |
20 | 27 | import com.google.devtools.mobileharness.api.model.error.MobileHarnessException; |
| 28 | +import com.google.devtools.mobileharness.shared.util.time.Sleeper; |
21 | 29 | import com.google.wireless.qa.mobileharness.shared.api.driver.Driver; |
22 | 30 | import com.google.wireless.qa.mobileharness.shared.model.job.TestInfo; |
23 | 31 | import com.google.wireless.qa.mobileharness.shared.model.job.in.spec.SpecConfigable; |
24 | 32 | import com.google.wireless.qa.mobileharness.shared.proto.spec.decorator.AndroidEmulatorVideoDecoratorSpec; |
| 33 | +import java.nio.file.Path; |
| 34 | +import java.time.Duration; |
| 35 | +import java.util.Set; |
| 36 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 37 | +import java.util.concurrent.atomic.AtomicInteger; |
| 38 | +import javax.inject.Inject; |
25 | 39 |
|
26 | 40 | /** Decorator for recording video on Android Emulators using the console recording */ |
27 | | -public class AndroidEmulatorVideoDecorator extends BaseDecorator |
| 41 | +public class AndroidEmulatorVideoDecorator extends AsyncTimerDecorator |
28 | 42 | implements SpecConfigable<AndroidEmulatorVideoDecoratorSpec> { |
29 | 43 |
|
30 | 44 | private static final FluentLogger logger = FluentLogger.forEnclosingClass(); |
| 45 | + private static final Duration MAX_EMULATOR_SUPPORTED_VIDEO_DURATION = Duration.ofMinutes(15); |
| 46 | + private static final int DEFAULT_FPS = 5; |
| 47 | + private static final int DEFAULT_BIT_RATE = 100000; |
| 48 | + |
| 49 | + private final Adb adb; |
| 50 | + private final Sleeper sleeper; |
| 51 | + private final AtomicInteger videoCount = new AtomicInteger(0); |
| 52 | + |
| 53 | + private final AtomicBoolean running = new AtomicBoolean(false); |
| 54 | + |
| 55 | + private final Set<Path> generatedFiles = Sets.newConcurrentHashSet(); |
31 | 56 |
|
32 | | - public AndroidEmulatorVideoDecorator(Driver decorated, TestInfo testInfo) { |
| 57 | + private AndroidEmulatorVideoDecoratorSpec emulatorVideoDecoratorSpec; |
| 58 | + |
| 59 | + @Inject |
| 60 | + AndroidEmulatorVideoDecorator(Driver decorated, TestInfo testInfo, Adb adb, Sleeper sleeper) { |
33 | 61 | super(decorated, testInfo); |
| 62 | + this.adb = adb; |
| 63 | + this.sleeper = sleeper; |
34 | 64 | } |
35 | 65 |
|
36 | 66 | @Override |
37 | | - public void run(TestInfo testInfo) throws MobileHarnessException, InterruptedException { |
38 | | - AndroidEmulatorVideoDecoratorSpec spec = testInfo.jobInfo().combinedSpec(this); |
| 67 | + public void onStart(TestInfo testInfo) throws MobileHarnessException, InterruptedException { |
| 68 | + emulatorVideoDecoratorSpec = testInfo.jobInfo().combinedSpec(this); |
| 69 | + } |
39 | 70 |
|
| 71 | + @Override |
| 72 | + long getIntervalMs(TestInfo testInfo) { |
| 73 | + return emulatorVideoDecoratorSpec.hasTimeLimitSecs() |
| 74 | + ? Duration.ofSeconds(emulatorVideoDecoratorSpec.getTimeLimitSecs()).toMillis() |
| 75 | + : MAX_EMULATOR_SUPPORTED_VIDEO_DURATION.toMillis(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + void runTimerTask(TestInfo testInfo) throws MobileHarnessException, InterruptedException { |
| 80 | + // If a recording is already running, stop it before starting a new one. The video file is only |
| 81 | + // written to when the recording is explicitly stopped. Allow a small delay before starting the |
| 82 | + // next recording to let the emulator complete writing the previous video file. If consecutive |
| 83 | + // video recordings are started too quickly, the emulator may not have finished writing the |
| 84 | + // previous video file, resulting in a corrupt or empty video file. |
| 85 | + if (running.get()) { |
| 86 | + stopVideoRecording(testInfo); |
| 87 | + } |
| 88 | + var device = getDevice(); |
| 89 | + var fps = |
| 90 | + emulatorVideoDecoratorSpec.hasFps() ? emulatorVideoDecoratorSpec.getFps() : DEFAULT_FPS; |
| 91 | + var bitRate = |
| 92 | + emulatorVideoDecoratorSpec.hasBitRate() |
| 93 | + ? emulatorVideoDecoratorSpec.getBitRate() |
| 94 | + : DEFAULT_BIT_RATE; |
| 95 | + var timeLimitSecs = |
| 96 | + emulatorVideoDecoratorSpec.hasTimeLimitSecs() |
| 97 | + ? emulatorVideoDecoratorSpec.getTimeLimitSecs() |
| 98 | + : MAX_EMULATOR_SUPPORTED_VIDEO_DURATION.toSeconds(); |
| 99 | + var videoOutputPath = |
| 100 | + Path.of(testInfo.getGenFileDir()) |
| 101 | + .resolve(String.format("video-%d.webm", videoCount.incrementAndGet())); |
| 102 | + var args = |
| 103 | + ImmutableList.of( |
| 104 | + "emu", |
| 105 | + "screenrecord", |
| 106 | + "start", |
| 107 | + "--bit-rate", |
| 108 | + String.valueOf(bitRate), |
| 109 | + "--fps", |
| 110 | + String.valueOf(fps), |
| 111 | + "--time-limit", |
| 112 | + String.valueOf(timeLimitSecs), |
| 113 | + videoOutputPath.toString()); |
| 114 | + |
| 115 | + var output = adb.run(device.getDeviceId(), args.toArray(String[]::new)); |
| 116 | + running.set(true); |
| 117 | + generatedFiles.add(videoOutputPath); |
40 | 118 | testInfo |
41 | 119 | .log() |
42 | 120 | .atInfo() |
43 | 121 | .alsoTo(logger) |
44 | | - .log("------- Starting Android Emulator Video Decorator --------\n Spec: %s", spec); |
| 122 | + .log("------- Emulator screenrecord start --------\n Output: %s", output); |
| 123 | + } |
45 | 124 |
|
46 | | - getDecorated().run(testInfo); |
| 125 | + private void stopVideoRecording(TestInfo testInfo) |
| 126 | + throws MobileHarnessException, InterruptedException { |
| 127 | + var videoOutputPath = |
| 128 | + Path.of(testInfo.getGenFileDir()).resolve(String.format("video-%d.webm", videoCount.get())); |
| 129 | + var args = ImmutableList.of("emu", "screenrecord", "stop", videoOutputPath.toString()); |
| 130 | + var unused = adb.run(getDevice().getDeviceId(), args.toArray(String[]::new)); |
| 131 | + // Sleep for 1 second to let the emulator complete writing the video file. |
| 132 | + sleeper.sleep(Duration.ofSeconds(1)); |
| 133 | + running.set(false); |
| 134 | + } |
47 | 135 |
|
| 136 | + @Override |
| 137 | + void onEnd(TestInfo testInfo) throws MobileHarnessException, InterruptedException { |
| 138 | + stopVideoRecording(testInfo); |
| 139 | + // Sleep for 2 seconds to let the emulator complete writing the video file. |
| 140 | + sleeper.sleep(Duration.ofSeconds(2)); |
48 | 141 | testInfo |
49 | 142 | .log() |
50 | 143 | .atInfo() |
51 | 144 | .alsoTo(logger) |
52 | | - .log("------- Stopping Android Emulator Video Decorator --------"); |
| 145 | + .log("------- Stopped Android Emulator Video Decorator --------"); |
| 146 | + |
| 147 | + var missingFiles = |
| 148 | + generatedFiles.stream() |
| 149 | + .filter(videoFile -> !videoFile.toFile().exists()) |
| 150 | + .collect(toImmutableList()); |
| 151 | + if (!missingFiles.isEmpty()) { |
| 152 | + throw new MobileHarnessException( |
| 153 | + AndroidErrorId.ANDROID_EMULATOR_VIDEO_DECORATOR_VIDEO_FILE_ABSENT, |
| 154 | + "Generated video files absent. Expected: " + Joiner.on(",").join(missingFiles)); |
| 155 | + } |
| 156 | + var emptyFiles = |
| 157 | + generatedFiles.stream() |
| 158 | + .filter(videoFile -> videoFile.toFile().length() == 0L) |
| 159 | + .collect(toImmutableList()); |
| 160 | + if (!emptyFiles.isEmpty()) { |
| 161 | + throw new MobileHarnessException( |
| 162 | + AndroidErrorId.ANDROID_EMULATOR_VIDEO_DECORATOR_VIDEO_FILE_EMPTY, |
| 163 | + "Generated video files empty. Empty files: " + Joiner.on(",").join(emptyFiles)); |
| 164 | + } |
53 | 165 | } |
54 | 166 | } |
0 commit comments