|
| 1 | +/* |
| 2 | + * Copyright 2022 Apple Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. |
| 6 | + * |
| 7 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | + * express or implied. See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.shazam.fork.runner.listeners; |
| 16 | + |
| 17 | +import com.android.ddmlib.AdbCommandRejectedException; |
| 18 | +import com.android.ddmlib.IDevice; |
| 19 | +import com.android.ddmlib.NullOutputReceiver; |
| 20 | +import com.android.ddmlib.ScreenRecorderOptions; |
| 21 | +import com.android.ddmlib.ShellCommandUnresponsiveException; |
| 22 | +import com.android.ddmlib.SyncException; |
| 23 | +import com.android.ddmlib.TimeoutException; |
| 24 | +import com.android.ddmlib.testrunner.TestIdentifier; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.concurrent.ExecutorService; |
| 31 | +import java.util.concurrent.Future; |
| 32 | + |
| 33 | +import static com.shazam.fork.Utils.namedExecutor; |
| 34 | +import static com.shazam.fork.system.io.RemoteFileManager.remoteVideoForTest; |
| 35 | +import static com.shazam.fork.system.io.RemoteFileManager.removeRemotePath; |
| 36 | +import static com.shazam.fork.utils.Utils.millisSinceNanoTime; |
| 37 | +import static java.lang.System.nanoTime; |
| 38 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 39 | + |
| 40 | +public class ScreenRecorderImpl implements ScreenRecorder { |
| 41 | + private static final Logger logger = LoggerFactory.getLogger(ScreenRecorderImpl.class); |
| 42 | + private static final int DURATION = 60; |
| 43 | + private static final int BIT_RATE_MBPS = 1; |
| 44 | + private static final ScreenRecorderOptions RECORDER_OPTIONS = new ScreenRecorderOptions.Builder() |
| 45 | + .setTimeLimit(DURATION, SECONDS) |
| 46 | + .setBitRate(BIT_RATE_MBPS) |
| 47 | + .build(); |
| 48 | + |
| 49 | + private final IDevice deviceInterface; |
| 50 | + private final ScreenRecorderStopper screenRecorderStopper; |
| 51 | + private final ExecutorService recorderExecutor; |
| 52 | + private final ExecutorService fileExecutor; |
| 53 | + |
| 54 | + private State state = State.Stopped; |
| 55 | + private Future<?> recorderTask; |
| 56 | + |
| 57 | + public ScreenRecorderImpl(IDevice deviceInterface) { |
| 58 | + this.deviceInterface = deviceInterface; |
| 59 | + this.screenRecorderStopper = new ScreenRecorderStopper(deviceInterface); |
| 60 | + this.recorderExecutor = namedExecutor(/* numberOfThreads = */ 1, "RecorderExecutor-%d"); |
| 61 | + this.fileExecutor = namedExecutor(/* numberOfThreads = */ 1, "RecorderFileExecutor-%d"); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void startScreenRecording(TestIdentifier test) { |
| 66 | + if (state != State.Stopped) { |
| 67 | + logger.warn("ScreenRecorder is {} -> stopping screen recording", state); |
| 68 | + stopScreenRecordingInternal(); |
| 69 | + } |
| 70 | + |
| 71 | + state = State.Recording; |
| 72 | + recorderTask = recorderExecutor.submit(() -> { |
| 73 | + try { |
| 74 | + String remoteFilePath = remoteVideoForTest(test); |
| 75 | + logger.debug("Started recording video {}", remoteFilePath); |
| 76 | + startRecordingTestVideo(remoteFilePath); |
| 77 | + } catch (TimeoutException e) { |
| 78 | + logger.debug("Screen recording was either interrupted or timed out", e); |
| 79 | + } catch (Exception e) { |
| 80 | + logger.error("Something went wrong while screen recording", e); |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void stopScreenRecording(TestIdentifier test) { |
| 87 | + if (state != State.Recording) { |
| 88 | + logger.warn("ScreenRecorder is {} when we tried to stop recording", state); |
| 89 | + } |
| 90 | + stopScreenRecordingInternal(); |
| 91 | + } |
| 92 | + |
| 93 | + private void stopScreenRecordingInternal() { |
| 94 | + logger.debug("Stopped screen recording"); |
| 95 | + if (recorderTask != null) { |
| 96 | + recorderTask.cancel(true); |
| 97 | + } |
| 98 | + screenRecorderStopper.stopScreenRecord(); |
| 99 | + state = State.Stopped; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void saveScreenRecording(TestIdentifier test, File output) { |
| 104 | + fileExecutor.submit(() -> { |
| 105 | + try { |
| 106 | + String remoteFilePath = remoteVideoForTest(test); |
| 107 | + logger.debug("Save screen recording {} to {}", remoteFilePath, output); |
| 108 | + pullTestVideo(remoteFilePath, output); |
| 109 | + } catch (Exception e) { |
| 110 | + logger.error("Failed to pull a video file", e); |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void removeScreenRecording(TestIdentifier test) { |
| 117 | + fileExecutor.submit(() -> { |
| 118 | + try { |
| 119 | + String remoteFilePath = remoteVideoForTest(test); |
| 120 | + logger.debug("Remove screen recording {}", remoteFilePath); |
| 121 | + removeTestVideo(remoteFilePath); |
| 122 | + } catch (Exception e) { |
| 123 | + logger.error("Failed to remove a video file", e); |
| 124 | + } |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + private void startRecordingTestVideo(String remoteFilePath) throws TimeoutException, |
| 129 | + AdbCommandRejectedException, IOException, ShellCommandUnresponsiveException { |
| 130 | + NullOutputReceiver outputReceiver = new NullOutputReceiver(); |
| 131 | + logger.trace("Started recording video at: {}", remoteFilePath); |
| 132 | + long startNanos = nanoTime(); |
| 133 | + deviceInterface.startScreenRecorder(remoteFilePath, RECORDER_OPTIONS, outputReceiver); |
| 134 | + logger.trace("Recording finished in {}ms {}", millisSinceNanoTime(startNanos), remoteFilePath); |
| 135 | + } |
| 136 | + |
| 137 | + private void pullTestVideo(String remoteFilePath, File output) throws IOException, |
| 138 | + AdbCommandRejectedException, TimeoutException, SyncException { |
| 139 | + logger.trace("Started pulling file {} to {}", remoteFilePath, output); |
| 140 | + long startNanos = nanoTime(); |
| 141 | + deviceInterface.pullFile(remoteFilePath, output.toString()); |
| 142 | + logger.trace("Pulling finished in {}ms {}", millisSinceNanoTime(startNanos), remoteFilePath); |
| 143 | + } |
| 144 | + |
| 145 | + private void removeTestVideo(String remoteFilePath) { |
| 146 | + logger.trace("Started removing file {}", remoteFilePath); |
| 147 | + long startNanos = nanoTime(); |
| 148 | + removeRemotePath(deviceInterface, remoteFilePath); |
| 149 | + logger.trace("Removed file in {}ms {}", millisSinceNanoTime(startNanos), remoteFilePath); |
| 150 | + } |
| 151 | + |
| 152 | + private enum State { |
| 153 | + Recording, |
| 154 | + Stopped |
| 155 | + } |
| 156 | +} |
0 commit comments