1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.wm.flicker.monitor; 18 19 import static com.android.compatibility.common.util.SystemUtil.runShellCommand; 20 21 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; 22 23 import android.util.Log; 24 25 import java.io.IOException; 26 import java.nio.file.Files; 27 import java.nio.file.Path; 28 import java.util.Locale; 29 30 /** Captures screen contents and saves it as a mp4 video file. */ 31 public class ScreenRecorder implements ITransitionMonitor { 32 private static final String TAG = "FLICKER"; 33 private int mWidth; 34 private int mHeight; 35 private Path mOutputPath; 36 private Thread mRecorderThread; 37 ScreenRecorder()38 public ScreenRecorder() { 39 this(720, 1280, OUTPUT_DIR.resolve("transition.mp4")); 40 } 41 ScreenRecorder(int width, int height, Path outputPath)42 public ScreenRecorder(int width, int height, Path outputPath) { 43 mWidth = width; 44 mHeight = height; 45 mOutputPath = outputPath; 46 } 47 getPath()48 public Path getPath() { 49 return mOutputPath; 50 } 51 52 @Override start()53 public void start() { 54 mOutputPath.getParent().toFile().mkdirs(); 55 String command = 56 String.format( 57 Locale.getDefault(), 58 "screenrecord --size %dx%d %s", 59 mWidth, 60 mHeight, 61 mOutputPath); 62 mRecorderThread = 63 new Thread( 64 () -> { 65 try { 66 Runtime.getRuntime().exec(command); 67 } catch (IOException e) { 68 Log.e(TAG, "Error executing " + command, e); 69 } 70 }); 71 mRecorderThread.start(); 72 } 73 74 @Override stop()75 public void stop() { 76 runShellCommand("killall -s 2 screenrecord"); 77 try { 78 mRecorderThread.join(); 79 } catch (InterruptedException e) { 80 // ignore 81 } 82 } 83 84 @Override save(String testTag)85 public Path save(String testTag) { 86 if (!Files.exists(mOutputPath)) { 87 Log.w(TAG, "No video file found on " + mOutputPath); 88 return null; 89 } 90 91 try { 92 Path targetPath = 93 Files.move(mOutputPath, OUTPUT_DIR.resolve(testTag + ".mp4"), REPLACE_EXISTING); 94 Log.i(TAG, "Video saved to " + targetPath.toString()); 95 return targetPath; 96 } catch (IOException e) { 97 throw new RuntimeException(e); 98 } 99 } 100 } 101